82 lines
3.2 KiB
Luau
82 lines
3.2 KiB
Luau
-- ImGradient: edit a gradient (drag points, click to select, double-click to add), drawn behind the widget,
|
|
-- and converted to a Roblox ColorSequence.
|
|
local RS = game:GetService("ReplicatedStorage")
|
|
local RunService = game:GetService("RunService")
|
|
local G = require(RS:WaitForChild("ImGui"))
|
|
local ImGradient = require(RS:WaitForChild("ImGradient"))(G)
|
|
local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4
|
|
|
|
-- Delegate: points are ImVec4(r, g, b, position)
|
|
local Gradient = {
|
|
Points = { ImVec4(1, 0.2, 0.2, 0), ImVec4(1, 0.9, 0.2, 0.4), ImVec4(0.2, 0.4, 1, 1) },
|
|
}
|
|
function Gradient:GetPointCount() return #self.Points end
|
|
function Gradient:GetPoints() return self.Points end
|
|
function Gradient:EditPoint(i, v) self.Points[i] = v; return i end
|
|
function Gradient:AddPoint(v) table.insert(self.Points, v) end
|
|
function Gradient:Sorted()
|
|
local s = table.clone(self.Points)
|
|
table.sort(s, function(a, b) return a.w < b.w end)
|
|
return s
|
|
end
|
|
function Gradient:GetPoint(t)
|
|
local s = self:Sorted()
|
|
if t <= s[1].w then return ImVec4(s[1].x, s[1].y, s[1].z, t) end
|
|
for i = 2, #s do
|
|
local a, b = s[i - 1], s[i]
|
|
if t <= b.w then
|
|
local k = (b.w > a.w) and (t - a.w) / (b.w - a.w) or 0
|
|
return ImVec4(a.x + (b.x - a.x) * k, a.y + (b.y - a.y) * k, a.z + (b.z - a.z) * k, t)
|
|
end
|
|
end
|
|
local l = s[#s]
|
|
return ImVec4(l.x, l.y, l.z, t)
|
|
end
|
|
-- Roblox ColorSequence (needs keypoints at 0 and 1, at most 20)
|
|
function Gradient:ToColorSequence()
|
|
local kps, s = {}, self:Sorted()
|
|
local function add(t, c) table.insert(kps, ColorSequenceKeypoint.new(t, Color3.new(c.x, c.y, c.z))) end
|
|
add(0, self:GetPoint(0))
|
|
for i = 1, math.min(#s, 18) do if s[i].w > 0 and s[i].w < 1 then add(s[i].w, s[i]) end end
|
|
add(1, self:GetPoint(1))
|
|
return ColorSequence.new(kps)
|
|
end
|
|
|
|
local selection = -1
|
|
local Size = ImVec2(400, 24)
|
|
|
|
ImGui.CreateContext()
|
|
G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui
|
|
RunService.RenderStepped:Connect(function()
|
|
G.ImGui_ImplRoblox.NewFrame()
|
|
ImGui.NewFrame()
|
|
ImGui.Begin("ImGradient")
|
|
-- the widget only draws the points: draw the gradient behind it
|
|
local pos = ImGui.GetCursorScreenPos()
|
|
local dl, s = ImGui.GetWindowDrawList(), Gradient:Sorted()
|
|
local x0 = pos.x
|
|
for i = 0, #s do
|
|
local a, b = s[math.max(i, 1)], s[math.min(i + 1, #s)]
|
|
local x1 = pos.x + Size.x * (i < #s and b.w or 1)
|
|
local ca = ImGui.ColorConvertFloat4ToU32(ImVec4(a.x, a.y, a.z, 1))
|
|
local cb = ImGui.ColorConvertFloat4ToU32(ImVec4(b.x, b.y, b.z, 1))
|
|
if x1 > x0 then dl:AddRectFilledMultiColor(ImVec2(x0, pos.y), ImVec2(x1, pos.y + Size.y), ca, cb, cb, ca) end
|
|
x0 = x1
|
|
end
|
|
local changed
|
|
changed, selection = ImGradient.Edit(Gradient, Size, selection)
|
|
ImGui.Text("points: %d selection: %d", #Gradient.Points, selection)
|
|
local p = Gradient.Points[selection]
|
|
if p then
|
|
local col = ImVec4(p.x, p.y, p.z, 1)
|
|
if ImGui.ColorEdit3("color", col) then Gradient:EditPoint(selection, ImVec4(col.x, col.y, col.z, p.w)) end
|
|
if ImGui.Button("Delete point") and #Gradient.Points > 2 then table.remove(Gradient.Points, selection); selection = -1 end
|
|
end
|
|
if changed and ColorSequence then
|
|
Gradient.ColorSequence = Gradient:ToColorSequence() -- e.g. assign to a UIGradient/Beam/ParticleEmitter .Color
|
|
end
|
|
ImGui.End()
|
|
ImGui.Render()
|
|
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
|
end)
|