-- ImCurveEdit for imgui-luau. Port of ImCurveEdit.{h,cpp} from https://github.com/CedricGuillemet/ImGuizmo -- (105e4fde45e4e74aae8973d98750820ef7aca7e2). -- Usage (same as C++): -- local ImCurveEdit = require(path.ImCurveEdit)(G) -- G = the table returned by the ImGui module -- local ret, selectedPoints = ImCurveEdit.Edit(delegate, size, id, clippingRect, selectedPoints) -- [Port] Delegate (C++ abstract struct) is any table with these methods (indices are 1-based): -- GetCurveCount() -> n GetMin() / GetMax() -> ImVec2 (edited in place: zoom/scroll) -- GetPointCount(c) -> n GetCurveColor(c) -> ImU32 -- GetPoints(c) -> { ImVec2 } EditPoint(c, p, ImVec2) -> new point index -- AddPoint(c, ImVec2) -- optional (upstream defaults): IsVisible(c) -> true, GetCurveType(c) -> CurveLinear, -- GetBackgroundColor() -> 0xFF202020, BeginEdit(index), EndEdit(). delegate.focused is set by Edit. -- clippingRect: nil or an ImRect ({ Min, Max }). selectedPoints (C++ ImVector*): pass a table to have it -- filled in place, or nil; it is returned as the second value, a 1-based array of { curveIndex = c, pointIndex = p } -- sorted like upstream's std::set. Edit returns ret (0/1) first. -- Like upstream, the interaction state (selection, dragging) is shared by every curve editor (C++ function statics). -- Plain Lua 5.1 syntax so the same file loads on LÖVE (LuaJIT). return function(G) local ImGui, ImVec2 = G.ImGui, G.ImVec2 local ImLerp = G.ImLerp local FLT_EPSILON = 1.192092896e-07 local abs, sqrt, min_, max_ = math.abs, math.sqrt, math.min, math.max local ImCurveEdit = {} ImCurveEdit.CurveNone, ImCurveEdit.CurveDiscrete, ImCurveEdit.CurveLinear, ImCurveEdit.CurveSmooth, ImCurveEdit.CurveBezier = 0, 1, 2, 3, 4 local CurveNone, CurveDiscrete, CurveLinear, CurveSmooth = 0, 1, 2, 3 local function smoothstep(edge0, edge1, x) x = min_(max_((x - edge0) / (edge1 - edge0), 0.0), 1.0) return x * x * (3 - 2 * x) end local function distance(x, y, x1, y1, x2, y2) local A, B, C, D = x - x1, y - y1, x2 - x1, y2 - y1 local dot = A * C + B * D local len_sq = C * C + D * D local param = -1.0 if len_sq > FLT_EPSILON then param = dot / len_sq end local xx, yy if param < 0.0 then xx, yy = x1, y1 elseif param > 1.0 then xx, yy = x2, y2 else xx, yy = x1 + param * C, y1 + param * D end local dx, dy = x - xx, y - yy return sqrt(dx * dx + dy * dy) end 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 localOffsets = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } } local function DrawPoint(draw_list, pos, size, offset, edited) local ret = 0 local io = ImGui.GetIO() local cx, cy = pos.x * size.x + offset.x, pos.y * size.y + offset.y local offsets = {} for i = 1, 4 do offsets[i] = ImVec2(cx + localOffsets[i][1] * 4.5, cy + localOffsets[i][2] * 4.5) end draw_list:AddConvexPolyFilled(offsets, 4, 0xFF000000) if Contains(cx - 5, cy - 5, cx + 5, cy + 5, io.MousePos) then ret = 1 if io.MouseDown[0] then ret = 2 end end if edited then draw_list:AddPolyline(offsets, 4, 0xFFFFFFFF, 3.0) elseif ret ~= 0 then draw_list:AddPolyline(offsets, 4, 0xFF80B0FF, 2.0) else draw_list:AddPolyline(offsets, 4, 0xFF0080FF, 2.0) end return ret end -- std::set: sorted array of { curveIndex, pointIndex } local function less(a, b) if a.curveIndex ~= b.curveIndex then return a.curveIndex < b.curveIndex end return a.pointIndex < b.pointIndex end local function setFind(set, c, p) for i = 1, #set do local e = set[i] if e.curveIndex == c and e.pointIndex == p then return i end end return nil end local function setInsert(set, c, p) if setFind(set, c, p) then return end set[#set + 1] = { curveIndex = c, pointIndex = p } table.sort(set, less) end local function setErase(set, c, p) local i = setFind(set, c, p) if i then table.remove(set, i) end end local function setClear(set) for i = #set, 1, -1 do set[i] = nil end end local function setCopy(set) local t = {} for i = 1, #set do t[i] = { curveIndex = set[i].curveIndex, pointIndex = set[i].pointIndex } end return t end -- optional delegate methods with upstream defaults local function IsVisible(d, c) if d.IsVisible then return d:IsVisible(c) end return true end local function GetCurveType(d, c) if d.GetCurveType then return d:GetCurveType(c) end return CurveLinear end local function GetBackgroundColor(d) if d.GetBackgroundColor then return d:GetBackgroundColor() end return 0xFF202020 end local function BeginEdit(d, i) if d.BeginEdit then d:BeginEdit(i) end end local function EndEdit(d) if d.EndEdit then d:EndEdit() end end -- function statics local selectingQuad = false local quadSelection = ImVec2(0, 0) local overCurve = -1 local movingCurve = -1 local scrollingV = false local selection = {} local overSelectedPoint = false local pointsMoved = false local mousePosOrigin = ImVec2(0, 0) local originalPoints = {} function ImCurveEdit.Edit(delegate, size, id, clippingRect, selectedPoints) local ret = 0 local io = ImGui.GetIO() ImGui.PushStyleVar(G.ImGuiStyleVar.FramePadding, ImVec2(0, 0)) ImGui.PushStyleColor(G.ImGuiCol.Border, 0) ImGui.BeginChild(id, size, G.ImGuiChildFlags.FrameStyle) delegate.focused = ImGui.IsWindowFocused() local draw_list = ImGui.GetWindowDrawList() if clippingRect then draw_list:PushClipRect(clippingRect.Min, clippingRect.Max, true) end local cursor = ImGui.GetCursorScreenPos() local offset = ImVec2(cursor.x, cursor.y + size.y) local ssize = ImVec2(size.x, -size.y) -- container = ImRect(offset + (0, ssize.y), offset + (ssize.x, 0)) local cminx, cminy, cmaxx, cmaxy = offset.x, offset.y + ssize.y, offset.x + ssize.x, offset.y local min = delegate:GetMin() local max = delegate:GetMax() -- handle zoom and VScroll if Contains(cminx, cminy, cmaxx, cmaxy, io.MousePos) then if abs(io.MouseWheel) > FLT_EPSILON then local r = (io.MousePos.y - offset.y) / ssize.y local ratioY = min.y + (max.y - min.y) * r local function scaleValue(v) v = v - ratioY v = v * (1.0 - io.MouseWheel * 0.05) v = v + ratioY return v end min.y = scaleValue(min.y) max.y = scaleValue(max.y) end if not scrollingV and ImGui.IsMouseDown(2) then scrollingV = true end end local range = ImVec2(max.x - min.x + 1.0, max.y - min.y) -- +1 because of inclusive last frame local viewSize = ImVec2(size.x, -size.y) local sizeOfPixel = ImVec2(1.0 / viewSize.x, 1.0 / viewSize.y) local curveCount = delegate:GetCurveCount() if scrollingV then local deltaH = io.MouseDelta.y * range.y * sizeOfPixel.y min.y = min.y - deltaH max.y = max.y - deltaH if not ImGui.IsMouseDown(2) then scrollingV = false end end draw_list:AddRectFilled(offset, ImVec2(offset.x + ssize.x, offset.y + ssize.y), GetBackgroundColor(delegate)) local function pointToRange(pt) return ImVec2((pt.x - min.x) / range.x, (pt.y - min.y) / range.y) end local function rangeToPoint(pt) return ImVec2(pt.x * range.x + min.x, pt.y * range.y + min.y) end local function toScreen(x, y) return ImVec2(x * viewSize.x + offset.x, y * viewSize.y + offset.y) end draw_list:AddLine(toScreen(-1.0, -min.y / range.y), toScreen(1.0, -min.y / range.y), 0xFF000000, 1.5) local overCurveOrPoint = false local localOverCurve = -1 -- make sure highlighted curve is rendered last local curvesIndex = {} for c = 1, curveCount do curvesIndex[c] = c end local highLightedCurveIndex = -1 if overCurve ~= -1 and curveCount > 0 then curvesIndex[overCurve], curvesIndex[curveCount] = curvesIndex[curveCount], curvesIndex[overCurve] highLightedCurveIndex = overCurve end for cur = 1, curveCount do local c = curvesIndex[cur] local ptCount = IsVisible(delegate, c) and delegate:GetPointCount(c) or 0 local curveType = ptCount >= 1 and GetCurveType(delegate, c) or CurveNone if curveType ~= CurveNone then -- continue local pts = delegate:GetPoints(c) local curveColor = delegate:GetCurveColor(c) if (c == highLightedCurveIndex and #selection == 0 and not selectingQuad) or movingCurve == c then curveColor = 0xFFFFFFFF end for p = 1, ptCount - 1 do local p1 = pointToRange(pts[p]) local p2 = pointToRange(pts[p + 1]) if curveType == CurveSmooth or curveType == CurveLinear then local subStepCount = (curveType == CurveSmooth) and 20 or 2 local step = 1.0 / (subStepCount - 1) for substep = 0, subStepCount - 2 do local t = substep * step local sp1 = ImLerp(p1, p2, t) local sp2 = ImLerp(p1, p2, t + step) local rt1 = smoothstep(p1.x, p2.x, sp1.x) local rt2 = smoothstep(p1.x, p2.x, sp2.x) local pos1 = toScreen(sp1.x, p1.y + (p2.y - p1.y) * rt1) local pos2 = toScreen(sp2.x, p1.y + (p2.y - p1.y) * rt2) if distance(io.MousePos.x, io.MousePos.y, pos1.x, pos1.y, pos2.x, pos2.y) < 8.0 and not scrollingV then localOverCurve = c overCurve = c overCurveOrPoint = true end draw_list:AddLine(pos1, pos2, curveColor, 1.3) end -- substep elseif curveType == CurveDiscrete then local dp1 = toScreen(p1.x, p1.y) local dp2 = toScreen(p2.x, p1.y) local dp3 = toScreen(p2.x, p2.y) draw_list:AddLine(dp1, dp2, curveColor, 1.3) draw_list:AddLine(dp2, dp3, curveColor, 1.3) if distance(io.MousePos.x, io.MousePos.y, dp1.x, dp1.y, dp3.x, dp1.y) < 8.0 or distance(io.MousePos.x, io.MousePos.y, dp3.x, dp1.y, dp3.x, dp3.y) < 8.0 then localOverCurve = c overCurve = c overCurveOrPoint = true end end end -- point loop for p = 1, ptCount do local drawState = DrawPoint(draw_list, pointToRange(pts[p]), viewSize, offset, (setFind(selection, c, p) ~= nil and movingCurve == -1 and not scrollingV)) if drawState ~= 0 and movingCurve == -1 and not selectingQuad then overCurveOrPoint = true overSelectedPoint = true overCurve = -1 if drawState == 2 then if not io.KeyShift and setFind(selection, c, p) == nil then setClear(selection) end setInsert(selection, c, p) end end end end end -- curves loop if localOverCurve == -1 then overCurve = -1 end -- move selection if overSelectedPoint and io.MouseDown[0] then if (abs(io.MouseDelta.x) > 0.0 or abs(io.MouseDelta.y) > 0.0) and #selection > 0 then if not pointsMoved then BeginEdit(delegate, 0) mousePosOrigin = ImVec2(io.MousePos.x, io.MousePos.y) originalPoints = {} for index = 1, #selection do local sel = selection[index] local pts = delegate:GetPoints(sel.curveIndex) local o = pts[sel.pointIndex]; originalPoints[index] = ImVec2(o.x, o.y) end end pointsMoved = true ret = 1 local prevSelection = setCopy(selection) for originalIndex = 1, #prevSelection do local sel = prevSelection[originalIndex] local o = pointToRange(originalPoints[originalIndex]) local p = rangeToPoint(ImVec2(o.x + (io.MousePos.x - mousePosOrigin.x) * sizeOfPixel.x, o.y + (io.MousePos.y - mousePosOrigin.y) * sizeOfPixel.y)) local newIndex = delegate:EditPoint(sel.curveIndex, sel.pointIndex, p) if newIndex ~= sel.pointIndex then setErase(selection, sel.curveIndex, sel.pointIndex) setInsert(selection, sel.curveIndex, newIndex) end end end end if overSelectedPoint and not io.MouseDown[0] then overSelectedPoint = false if pointsMoved then pointsMoved = false EndEdit(delegate) end end -- add point if overCurve ~= -1 and io.MouseDoubleClicked[0] then local np = rangeToPoint(ImVec2((io.MousePos.x - offset.x) / viewSize.x, (io.MousePos.y - offset.y) / viewSize.y)) BeginEdit(delegate, overCurve) delegate:AddPoint(overCurve, np) EndEdit(delegate) ret = 1 end -- move curve if movingCurve ~= -1 then local ptCount = delegate:GetPointCount(movingCurve) local pts = delegate:GetPoints(movingCurve) if not pointsMoved then mousePosOrigin = ImVec2(io.MousePos.x, io.MousePos.y) pointsMoved = true originalPoints = {} for index = 1, ptCount do originalPoints[index] = ImVec2(pts[index].x, pts[index].y) end end if ptCount >= 1 then for p = 1, ptCount do local o = pointToRange(originalPoints[p]) delegate:EditPoint(movingCurve, p, rangeToPoint(ImVec2(o.x + (io.MousePos.x - mousePosOrigin.x) * sizeOfPixel.x, o.y + (io.MousePos.y - mousePosOrigin.y) * sizeOfPixel.y))) end ret = 1 end if not io.MouseDown[0] then movingCurve = -1 pointsMoved = false EndEdit(delegate) end end if movingCurve == -1 and overCurve ~= -1 and ImGui.IsMouseClicked(0) and #selection == 0 and not selectingQuad then movingCurve = overCurve BeginEdit(delegate, overCurve) end -- quad selection if selectingQuad then local bmin = ImVec2(min_(quadSelection.x, io.MousePos.x), min_(quadSelection.y, io.MousePos.y)) local bmax = ImVec2(max_(quadSelection.x, io.MousePos.x), max_(quadSelection.y, io.MousePos.y)) draw_list:AddRectFilled(bmin, bmax, 0x40FF0000, 1.0) draw_list:AddRect(bmin, bmax, 0xFFFF0000, 1.0) if not io.MouseDown[0] then if not io.KeyShift then setClear(selection) end -- select everything in quad for c = 1, curveCount do local ptCount = IsVisible(delegate, c) and delegate:GetPointCount(c) or 0 if ptCount >= 1 then local pts = delegate:GetPoints(c) for p = 1, ptCount do local r = pointToRange(pts[p]) if Contains(bmin.x, bmin.y, bmax.x, bmax.y, toScreen(r.x, r.y)) then setInsert(selection, c, p) end end end end -- done selectingQuad = false end end if not overCurveOrPoint and ImGui.IsMouseClicked(0) and not selectingQuad and movingCurve == -1 and not overSelectedPoint and Contains(cminx, cminy, cmaxx, cmaxy, io.MousePos) then selectingQuad = true quadSelection = ImVec2(io.MousePos.x, io.MousePos.y) end if clippingRect then draw_list:PopClipRect() end ImGui.EndChild() ImGui.PopStyleVar() ImGui.PopStyleColor(1) selectedPoints = selectedPoints or {} for i = #selectedPoints, #selection + 1, -1 do selectedPoints[i] = nil end for index = 1, #selection do selectedPoints[index] = { curveIndex = selection[index].curveIndex, pointIndex = selection[index].pointIndex } end return ret, selectedPoints end return ImCurveEdit end