99 lines
3.9 KiB
Lua
99 lines
3.9 KiB
Lua
-- ImGradient for imgui-luau. Port of ImGradient.{h,cpp} from https://github.com/CedricGuillemet/ImGuizmo
|
|
-- (105e4fde45e4e74aae8973d98750820ef7aca7e2).
|
|
-- Usage (same as C++):
|
|
-- local ImGradient = require(path.ImGradient)(G) -- G = the table returned by the ImGui module
|
|
-- changed, selection = ImGradient.Edit(delegate, size, selection)
|
|
-- [Port] Delegate (C++ abstract struct) is any table with these methods:
|
|
-- delegate:GetPointCount() -> number
|
|
-- delegate:GetPoints() -> { ImVec4 } -- 1-based; .xyz = color, .w = position 0..1
|
|
-- delegate:EditPoint(pointIndex, value) -- pointIndex is 1-based; value is a new ImVec4; returns the (new) index
|
|
-- delegate:GetPoint(t) -> ImVec4 -- color at t (used when double-clicking to add a point)
|
|
-- delegate:AddPoint(value)
|
|
-- selection is 1-based, -1 = none (upstream: 0-based, -1 = none).
|
|
-- Like upstream, the selection / dragged point are shared by every gradient editor (C++ function statics),
|
|
-- and the widget draws only the points: draw the gradient itself behind it.
|
|
-- Plain Lua 5.1 syntax so the same file loads on LÖVE (LuaJIT).
|
|
return function(G)
|
|
local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4
|
|
local ImLerp = G.ImLerp
|
|
|
|
local ImGradient = {}
|
|
|
|
local function Contains(minx, miny, maxx, maxy, p) -- ImRect::Contains
|
|
return p.x >= minx and p.y >= miny and p.x < maxx and p.y < maxy
|
|
end
|
|
|
|
local function DrawPoint(draw_list, color, size, editing, pos)
|
|
local io = ImGui.GetIO()
|
|
|
|
local p1 = ImLerp(pos, ImVec2(pos.x + size.x - size.y, pos.y), color.w)
|
|
p1 = ImVec2(p1.x + 3, p1.y + 3)
|
|
local p2 = ImLerp(ImVec2(pos.x + size.y, pos.y + size.y), ImVec2(pos.x + size.x, pos.y + size.y), color.w)
|
|
p2 = ImVec2(p2.x - 3, p2.y - 3)
|
|
|
|
draw_list:AddRectFilled(p1, p2, ImGui.ColorConvertFloat4ToU32(ImVec4(color.x, color.y, color.z, 1.0)))
|
|
if editing then
|
|
draw_list:AddRect(p1, p2, 0xFFFFFFFF, 2.0, 2.5)
|
|
else
|
|
draw_list:AddRect(p1, p2, 0x80FFFFFF, 2.0, 1.25)
|
|
end
|
|
|
|
if Contains(p1.x, p1.y, p2.x, p2.y, io.MousePos) then
|
|
if io.MouseClicked[0] then
|
|
return 2
|
|
end
|
|
return 1
|
|
end
|
|
return 0
|
|
end
|
|
|
|
local currentSelection = -1
|
|
local movingPt = -1
|
|
|
|
function ImGradient.Edit(delegate, size, selection)
|
|
local ret = false
|
|
local io = ImGui.GetIO()
|
|
ImGui.PushStyleVar(G.ImGuiStyleVar.FramePadding, ImVec2(0, 0))
|
|
ImGui.BeginChild(137, size, G.ImGuiChildFlags.FrameStyle)
|
|
|
|
local draw_list = ImGui.GetWindowDrawList()
|
|
local offset = ImGui.GetCursorScreenPos()
|
|
|
|
local pts = delegate:GetPoints()
|
|
if currentSelection > delegate:GetPointCount() then
|
|
currentSelection = -1
|
|
end
|
|
if movingPt ~= -1 then
|
|
local p = pts[movingPt]
|
|
local w = p.w + io.MouseDelta.x / size.x
|
|
w = math.min(math.max(w, 0.0), 1.0)
|
|
delegate:EditPoint(movingPt, ImVec4(p.x, p.y, p.z, w))
|
|
ret = true
|
|
if not io.MouseDown[0] then
|
|
movingPt = -1
|
|
end
|
|
end
|
|
for i = 1, delegate:GetPointCount() do
|
|
local ptSel = DrawPoint(draw_list, pts[i], size, i == currentSelection, offset)
|
|
if ptSel == 2 then
|
|
currentSelection = i
|
|
ret = true
|
|
end
|
|
if ptSel == 1 and io.MouseDown[0] and movingPt == -1 then
|
|
movingPt = i
|
|
end
|
|
end
|
|
if Contains(offset.x, offset.y, offset.x + size.x, offset.y + size.y, io.MousePos) and io.MouseDoubleClicked[0] then
|
|
local t = (io.MousePos.x - offset.x) / size.x
|
|
delegate:AddPoint(delegate:GetPoint(t))
|
|
ret = true
|
|
end
|
|
ImGui.EndChild()
|
|
ImGui.PopStyleVar()
|
|
|
|
return ret, currentSelection
|
|
end
|
|
|
|
return ImGradient
|
|
end
|