ImCurveEdit: port of ImGuizmo's ImCurveEdit (@105e4fd), tests, Roblox example (RampEdit)
This commit is contained in:
commit
8e992a2d8e
15 files changed
+1226
No files matched your search
@@ -0,0 +1,3 @@
|
||||
[submodule "tools/core"]
|
||||
path = tools/core
|
||||
url = https://git.bug.tools/bug/imgui-luau.git
|
||||
+400
@@ -0,0 +1,400 @@
|
||||
-- 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<EditPoint>*): 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<EditPoint>: 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
|
||||
@@ -0,0 +1,24 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Cedric Guillemet
|
||||
Copyright (c) 2026 4DBug (Luau port)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
Luau port: Copyright (c) 2026 4DBug, same MIT terms.
|
||||
@@ -0,0 +1,51 @@
|
||||
# imcurveedit-luau
|
||||
|
||||
Curve editor widget for imgui-luau: multiple curves (discrete, linear, smooth), point dragging, box selection,
|
||||
double-click to add points, curve dragging, Y zoom/scroll.
|
||||
|
||||
Luau port of ImCurveEdit from [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo) for [imgui-luau](https://git.bug.tools/bug/imgui-luau) ([GitHub](https://github.com/4DBug/imgui-luau)).
|
||||
Mirrors: [git.bug.tools](https://git.bug.tools/bug/imcurveedit-luau) / [GitHub](https://github.com/4DBug/imcurveedit-luau).
|
||||
Pairs with [imsequencer-luau](https://git.bug.tools/bug/imsequencer-luau) (curves inside an expanded sequencer row).
|
||||
|
||||
## Usage
|
||||
|
||||
```lua
|
||||
local G = require(ReplicatedStorage.ImGui)
|
||||
local ImCurveEdit = require(ReplicatedStorage.ImCurveEdit)(G)
|
||||
|
||||
-- Delegate (C++ ImCurveEdit::Delegate): any table with these methods, curve/point indices 1-based
|
||||
local Ramp = { Pts = { { G.ImVec2(0, 0), G.ImVec2(50, 1) } }, Min = G.ImVec2(0, 0), Max = G.ImVec2(100, 1) }
|
||||
function Ramp:GetCurveCount() return #self.Pts end
|
||||
function Ramp:GetPointCount(c) return #self.Pts[c] end
|
||||
function Ramp:GetCurveColor(c) return 0xFF0080FF end
|
||||
function Ramp:GetPoints(c) return self.Pts[c] end -- 1-based array of ImVec2
|
||||
function Ramp:EditPoint(c, p, v) self.Pts[c][p] = v; return p end -- return the point's new index (if you sort)
|
||||
function Ramp:AddPoint(c, v) table.insert(self.Pts[c], v) end
|
||||
function Ramp:GetMin() return self.Min end -- ImVec2, edited in place (zoom/scroll)
|
||||
function Ramp:GetMax() return self.Max end
|
||||
-- optional: IsVisible(c), GetCurveType(c) (default ImCurveEdit.CurveLinear), GetBackgroundColor(), BeginEdit(i), EndEdit()
|
||||
|
||||
-- each frame, inside a window:
|
||||
local ret
|
||||
ret, selectedPoints = ImCurveEdit.Edit(Ramp, G.ImVec2(400, 200), 137, nil, selectedPoints)
|
||||
```
|
||||
|
||||
`[Port]` differences: indices are 1-based. `clippingRect` is `nil` or an `ImRect` (`{ Min, Max }`). `selectedPoints`
|
||||
(C++ `ImVector<EditPoint>*`) is returned as the second value: a 1-based array of `{ curveIndex, pointIndex }`
|
||||
(filled in place when you pass a table). Curve types: `ImCurveEdit.CurveNone/CurveDiscrete/CurveLinear/CurveSmooth/CurveBezier`
|
||||
(Bezier is not drawn, as upstream). As upstream, interaction state is shared by all curve editors (C++ function statics).
|
||||
|
||||
Full example (upstream's `RampEdit`): `examples/roblox/main.client.luau`. The module is plain Lua 5.1 and also loads on LÖVE.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
git clone --recursive https://git.bug.tools/bug/imcurveedit-luau.git
|
||||
bash tools/test.sh # tests/t_*.luau in the core's Roblox mock + the Roblox example
|
||||
```
|
||||
|
||||
Upstream source (pinned) is in `tools/upstream/`; port-specific comments are tagged `[Port]`.
|
||||
|
||||
## License
|
||||
|
||||
MIT, see LICENSE (upstream copyright kept).
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "imcurveedit-luau",
|
||||
"tree": {
|
||||
"$className": "DataModel",
|
||||
"ReplicatedStorage": {
|
||||
"ImGui": { "$path": "tools/core/build/ImGuiTyped.luau", "Impl": { "$path": "tools/core/build/ImGui.luau" } },
|
||||
"ImCurveEdit": { "$path": "ImCurveEdit.lua" }
|
||||
},
|
||||
"StarterPlayer": { "StarterPlayerScripts": { "ImCurveEditExample": { "$path": "examples/roblox/main.client.luau" } } }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
-- ImCurveEdit: the RampEdit delegate from upstream's example (3 smooth curves). Drag points, box-select with the
|
||||
-- left mouse, Shift to add to the selection, double-click a curve to add a point, drag a curve to move it,
|
||||
-- mouse wheel zooms Y, middle mouse scrolls Y.
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local RunService = game:GetService("RunService")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImCurveEdit = require(RS:WaitForChild("ImCurveEdit"))(G)
|
||||
local ImGui, ImVec2 = G.ImGui, G.ImVec2
|
||||
|
||||
local RampEdit = {
|
||||
mPts = {
|
||||
{ ImVec2(-10, 0), ImVec2(20, 0.6), ImVec2(25, 0.2), ImVec2(70, 0.4), ImVec2(120, 1) },
|
||||
{ ImVec2(-50, 0.2), ImVec2(33, 0.7), ImVec2(80, 0.2), ImVec2(82, 0.8) },
|
||||
{ ImVec2(40, 0), ImVec2(60, 0.1), ImVec2(90, 0.82), ImVec2(150, 0.24), ImVec2(200, 0.34), ImVec2(250, 0.12) },
|
||||
},
|
||||
mbVisible = { true, true, true },
|
||||
mMin = ImVec2(-100, 0),
|
||||
mMax = ImVec2(300, 1),
|
||||
}
|
||||
function RampEdit:GetCurveCount() return 3 end
|
||||
function RampEdit:IsVisible(c) return self.mbVisible[c] end
|
||||
function RampEdit:GetPointCount(c) return #self.mPts[c] end
|
||||
function RampEdit:GetCurveColor(c) return ({ 0xFF0000FF, 0xFF00FF00, 0xFFFF0000 })[c] end
|
||||
function RampEdit:GetPoints(c) return self.mPts[c] end
|
||||
function RampEdit:GetCurveType(c) return ImCurveEdit.CurveSmooth end
|
||||
function RampEdit:SortValues(c) table.sort(self.mPts[c], function(a, b) return a.x < b.x end) end
|
||||
function RampEdit:EditPoint(c, p, value)
|
||||
self.mPts[c][p] = ImVec2(value.x, value.y)
|
||||
self:SortValues(c)
|
||||
for i, pt in self.mPts[c] do
|
||||
if pt.x == value.x then return i end
|
||||
end
|
||||
return p
|
||||
end
|
||||
function RampEdit:AddPoint(c, value)
|
||||
if #self.mPts[c] >= 8 then return end
|
||||
table.insert(self.mPts[c], value)
|
||||
self:SortValues(c)
|
||||
end
|
||||
function RampEdit:GetMax() return self.mMax end
|
||||
function RampEdit:GetMin() return self.mMin end
|
||||
function RampEdit:GetBackgroundColor() return 0 end
|
||||
|
||||
local selectedPoints = {}
|
||||
local labels = { "Translation", "Rotation", "Scale" }
|
||||
|
||||
ImGui.CreateContext()
|
||||
G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui
|
||||
RunService.RenderStepped:Connect(function()
|
||||
G.ImGui_ImplRoblox.NewFrame()
|
||||
ImGui.NewFrame()
|
||||
ImGui.SetNextWindowSize(ImVec2(700, 400), G.ImGuiCond.FirstUseEver)
|
||||
ImGui.Begin("ImCurveEdit")
|
||||
for i = 1, 3 do
|
||||
_, RampEdit.mbVisible[i] = ImGui.Checkbox(labels[i], RampEdit.mbVisible[i])
|
||||
ImGui.SameLine()
|
||||
end
|
||||
ImGui.Text("selected: %d", #selectedPoints)
|
||||
local avail = ImGui.GetContentRegionAvail()
|
||||
local _
|
||||
_, selectedPoints = ImCurveEdit.Edit(RampEdit, ImVec2(avail.x, avail.y), 137, nil, selectedPoints)
|
||||
ImGui.End()
|
||||
ImGui.Render()
|
||||
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
||||
end)
|
||||
@@ -0,0 +1,70 @@
|
||||
-- Drag a point, box-select all points, double-click a curve to add a point; BeginEdit/EndEdit pairing.
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImCurveEdit = require(RS:WaitForChild("ImCurveEdit"))(G)
|
||||
local ImGui, ImVec2 = G.ImGui, G.ImVec2
|
||||
|
||||
ImGui.CreateContext()
|
||||
G.ImGui_ImplRoblox.Init()
|
||||
local io = ImGui.GetIO()
|
||||
|
||||
local D = { Pts = { ImVec2(10, 0.2), ImVec2(50, 0.5), ImVec2(90, 0.8) }, Min = ImVec2(0, 0), Max = ImVec2(99, 1), Edits = 0, Open = 0 }
|
||||
function D:GetCurveCount() return 1 end
|
||||
function D:GetPointCount() return #self.Pts end
|
||||
function D:GetCurveColor() return 0xFFFFFFFF end
|
||||
function D:GetPoints() return self.Pts end
|
||||
function D:EditPoint(c, p, v) self.Pts[p] = v; self.Edits += 1; return p end
|
||||
function D:AddPoint(c, v) table.insert(self.Pts, 3, v); self.Added = v end
|
||||
function D:GetMin() return self.Min end
|
||||
function D:GetMax() return self.Max end
|
||||
function D:BeginEdit() self.Open += 1 end
|
||||
function D:EndEdit() self.Open -= 1 end
|
||||
|
||||
local Size = ImVec2(400, 200)
|
||||
local frame, done, cur = 0, false, nil
|
||||
local ret, sel = 0, {}
|
||||
local function screen(x, y) return cur.x + x / 100 * Size.x, cur.y + Size.y - y * Size.y end
|
||||
game:GetService("RunService").Stepped:Connect(function()
|
||||
if done then return end
|
||||
frame += 1
|
||||
G.ImGui_ImplRoblox.NewFrame()
|
||||
io.DeltaTime = 1 / 60
|
||||
if frame == 3 then io:AddMousePosEvent(screen(50, 0.5))
|
||||
elseif frame == 4 then io:AddMouseButtonEvent(0, true)
|
||||
elseif frame >= 5 and frame <= 8 then local x, y = screen(50, 0.5); io:AddMousePosEvent(x + (frame - 4) * 10, y)
|
||||
elseif frame == 10 then io:AddMouseButtonEvent(0, false)
|
||||
elseif frame == 13 then io:AddMousePosEvent(cur.x + 5, cur.y + 5)
|
||||
elseif frame == 14 then io:AddMouseButtonEvent(0, true)
|
||||
elseif frame == 15 then io:AddMousePosEvent(cur.x + 395, cur.y + 195)
|
||||
elseif frame == 16 then io:AddMouseButtonEvent(0, false)
|
||||
elseif frame == 20 then io:AddMousePosEvent(screen(75, 0.65)) -- on the segment between points 2 and 3
|
||||
elseif frame == 22 or frame == 24 then io:AddMouseButtonEvent(0, true)
|
||||
elseif frame == 23 or frame == 25 then io:AddMouseButtonEvent(0, false)
|
||||
end
|
||||
ImGui.NewFrame()
|
||||
ImGui.SetNextWindowPos(ImVec2(0, 0))
|
||||
ImGui.SetNextWindowSize(ImVec2(500, 300))
|
||||
ImGui.Begin("Curves")
|
||||
cur = ImGui.GetCursorScreenPos()
|
||||
ret, sel = ImCurveEdit.Edit(D, Size, 137, nil, sel)
|
||||
ImGui.End()
|
||||
ImGui.Render()
|
||||
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
|
||||
if frame == 4 then
|
||||
assert(#sel == 1 and sel[1].curveIndex == 1 and sel[1].pointIndex == 2, "click did not select point 2")
|
||||
elseif frame == 8 then
|
||||
assert(ret == 1, "dragging did not return 1")
|
||||
elseif frame == 12 then
|
||||
-- 40 px moved, but like upstream the first frame of movement only records the origin: 30 px = 7.5 units
|
||||
assert(math.abs(D.Pts[2].x - 57.5) < 0.5 and math.abs(D.Pts[2].y - 0.5) < 0.01, ("point 2 at %g,%g"):format(D.Pts[2].x, D.Pts[2].y))
|
||||
assert(D.Open == 0, "BeginEdit/EndEdit unbalanced after drag")
|
||||
elseif frame == 18 then
|
||||
assert(#sel == 3, "box selection selected " .. #sel)
|
||||
elseif frame == 28 then
|
||||
done = true
|
||||
assert(D.Added, "double-click did not add a point")
|
||||
assert(math.abs(D.Added.x - 75) < 1 and math.abs(D.Added.y - 0.65) < 0.02, ("added at %g,%g"):format(D.Added.x, D.Added.y))
|
||||
assert(#D.Pts == 4 and D.Open == 0)
|
||||
print("OK")
|
||||
end
|
||||
end)
|
||||
@@ -0,0 +1,5 @@
|
||||
local RS = game:GetService("ReplicatedStorage")
|
||||
local G = require(RS:WaitForChild("ImGui"))
|
||||
local ImCurveEdit = require(RS:WaitForChild("ImCurveEdit"))(G)
|
||||
assert(type(ImCurveEdit.Edit) == "function" and ImCurveEdit.CurveSmooth == 3)
|
||||
print("OK")
|
||||
Submodule
+1
Submodule tools/core added at e2d1cf5cf9.
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# Runs every tests/t_*.luau against the core (tools/core submodule) in the Roblox mock, and the Roblox example.
|
||||
cd "$(dirname "$0")/.."
|
||||
fail=0
|
||||
run() { python3 tools/core/tools/test/run.py --module ImCurveEdit=ImCurveEdit.lua --roblox "$@" 2>&1; }
|
||||
for t in tests/t_*.luau; do
|
||||
out=$(run --main "$t" --quiet)
|
||||
if echo "$out" | grep -q "^OK"; then echo "PASS $t"; else echo "FAIL $t"; echo "$out" | tail -20; fail=1; fi
|
||||
done
|
||||
# example LocalScript: 400 frames of random input (mock drives Stepped, not RenderStepped)
|
||||
ex=$(mktemp --suffix=.luau); sed 's/RenderStepped/Stepped/' examples/roblox/main.client.luau > "$ex"
|
||||
out=$(run --main "$ex")
|
||||
rm -f "$ex"
|
||||
if echo "$out" | grep -q "^avg frame"; then echo "PASS example"; else echo "FAIL example"; echo "$out" | tail -20; fail=1; fi
|
||||
exit $fail
|
||||
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Cedric Guillemet
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1 @@
|
||||
https://github.com/CedricGuillemet/ImGuizmo @ 105e4fde45e4e74aae8973d98750820ef7aca7e2
|
||||
@@ -0,0 +1,466 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#include "ImCurveEdit.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include <stdint.h>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_MSC_VER) || defined(__MINGW32__)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#if !defined(_MSC_VER) && !defined(__MINGW64_VERSION_MAJOR)
|
||||
#define _malloca(x) alloca(x)
|
||||
#define _freea(x)
|
||||
#endif
|
||||
|
||||
namespace ImCurveEdit
|
||||
{
|
||||
|
||||
#ifndef IMGUI_DEFINE_MATH_OPERATORS
|
||||
static ImVec2 operator+(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x + b.x, a.y + b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator-(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x - b.x, a.y - b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator*(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x * b.x, a.y * b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator/(const ImVec2& a, const ImVec2& b) {
|
||||
return ImVec2(a.x / b.x, a.y / b.y);
|
||||
}
|
||||
|
||||
static ImVec2 operator*(const ImVec2& a, const float b) {
|
||||
return ImVec2(a.x * b, a.y * b);
|
||||
}
|
||||
#endif
|
||||
|
||||
static float smoothstep(float edge0, float edge1, float x)
|
||||
{
|
||||
x = ImClamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
|
||||
return x * x * (3 - 2 * x);
|
||||
}
|
||||
|
||||
static float distance(float x, float y, float x1, float y1, float x2, float y2)
|
||||
{
|
||||
float A = x - x1;
|
||||
float B = y - y1;
|
||||
float C = x2 - x1;
|
||||
float D = y2 - y1;
|
||||
|
||||
float dot = A * C + B * D;
|
||||
float len_sq = C * C + D * D;
|
||||
float param = -1.f;
|
||||
if (len_sq > FLT_EPSILON)
|
||||
param = dot / len_sq;
|
||||
|
||||
float xx, yy;
|
||||
|
||||
if (param < 0.f) {
|
||||
xx = x1;
|
||||
yy = y1;
|
||||
}
|
||||
else if (param > 1.f) {
|
||||
xx = x2;
|
||||
yy = y2;
|
||||
}
|
||||
else {
|
||||
xx = x1 + param * C;
|
||||
yy = y1 + param * D;
|
||||
}
|
||||
|
||||
float dx = x - xx;
|
||||
float dy = y - yy;
|
||||
return sqrtf(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
static int DrawPoint(ImDrawList* draw_list, ImVec2 pos, const ImVec2 size, const ImVec2 offset, bool edited)
|
||||
{
|
||||
int ret = 0;
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
static const ImVec2 localOffsets[4] = { ImVec2(1,0), ImVec2(0,1), ImVec2(-1,0), ImVec2(0,-1) };
|
||||
ImVec2 offsets[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
offsets[i] = pos * size + localOffsets[i] * 4.5f + offset;
|
||||
}
|
||||
|
||||
const ImVec2 center = pos * size + offset;
|
||||
const ImRect anchor(center - ImVec2(5, 5), center + ImVec2(5, 5));
|
||||
draw_list->AddConvexPolyFilled(offsets, 4, 0xFF000000);
|
||||
if (anchor.Contains(io.MousePos))
|
||||
{
|
||||
ret = 1;
|
||||
if (io.MouseDown[0])
|
||||
ret = 2;
|
||||
}
|
||||
#if IMGUI_VERSION_NUM < 19276
|
||||
if (edited)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFFFFFFFF, true, 3.0f);
|
||||
else if (ret)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF80B0FF, true, 2.0f);
|
||||
else
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF0080FF, true, 2.0f);
|
||||
#else
|
||||
if (edited)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFFFFFFFF, 3.0f);
|
||||
else if (ret)
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF80B0FF, 2.0f);
|
||||
else
|
||||
draw_list->AddPolyline(offsets, 4, 0xFF0080FF, 2.0f);
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect, ImVector<EditPoint>* selectedPoints)
|
||||
{
|
||||
static bool selectingQuad = false;
|
||||
static ImVec2 quadSelection;
|
||||
static int overCurve = -1;
|
||||
static int movingCurve = -1;
|
||||
static bool scrollingV = false;
|
||||
static std::set<EditPoint> selection;
|
||||
static bool overSelectedPoint = false;
|
||||
|
||||
int ret = 0;
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
|
||||
ImGui::PushStyleColor(ImGuiCol_Border, 0);
|
||||
ImGui::BeginChild(id, size, ImGuiChildFlags_FrameStyle);
|
||||
delegate.focused = ImGui::IsWindowFocused();
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
if (clippingRect)
|
||||
draw_list->PushClipRect(clippingRect->Min, clippingRect->Max, true);
|
||||
|
||||
const ImVec2 offset = ImGui::GetCursorScreenPos() + ImVec2(0.f, size.y);
|
||||
const ImVec2 ssize(size.x, -size.y);
|
||||
const ImRect container(offset + ImVec2(0.f, ssize.y), offset + ImVec2(ssize.x, 0.f));
|
||||
ImVec2& min = delegate.GetMin();
|
||||
ImVec2& max = delegate.GetMax();
|
||||
|
||||
// handle zoom and VScroll
|
||||
if (container.Contains(io.MousePos))
|
||||
{
|
||||
if (fabsf(io.MouseWheel) > FLT_EPSILON)
|
||||
{
|
||||
const float r = (io.MousePos.y - offset.y) / ssize.y;
|
||||
float ratioY = ImLerp(min.y, max.y, r);
|
||||
auto scaleValue = [&](float v) {
|
||||
v -= ratioY;
|
||||
v *= (1.f - io.MouseWheel * 0.05f);
|
||||
v += ratioY;
|
||||
return v;
|
||||
};
|
||||
min.y = scaleValue(min.y);
|
||||
max.y = scaleValue(max.y);
|
||||
}
|
||||
if (!scrollingV && ImGui::IsMouseDown(2))
|
||||
{
|
||||
scrollingV = true;
|
||||
}
|
||||
}
|
||||
ImVec2 range = max - min + ImVec2(1.f, 0.f); // +1 because of inclusive last frame
|
||||
|
||||
const ImVec2 viewSize(size.x, -size.y);
|
||||
const ImVec2 sizeOfPixel = ImVec2(1.f, 1.f) / viewSize;
|
||||
const size_t curveCount = delegate.GetCurveCount();
|
||||
|
||||
if (scrollingV)
|
||||
{
|
||||
float deltaH = io.MouseDelta.y * range.y * sizeOfPixel.y;
|
||||
min.y -= deltaH;
|
||||
max.y -= deltaH;
|
||||
if (!ImGui::IsMouseDown(2))
|
||||
scrollingV = false;
|
||||
}
|
||||
|
||||
draw_list->AddRectFilled(offset, offset + ssize, delegate.GetBackgroundColor());
|
||||
|
||||
auto pointToRange = [&](ImVec2 pt) { return (pt - min) / range; };
|
||||
auto rangeToPoint = [&](ImVec2 pt) { return (pt * range) + min; };
|
||||
|
||||
draw_list->AddLine(ImVec2(-1.f, -min.y / range.y) * viewSize + offset, ImVec2(1.f, -min.y / range.y) * viewSize + offset, 0xFF000000, 1.5f);
|
||||
bool overCurveOrPoint = false;
|
||||
|
||||
int localOverCurve = -1;
|
||||
// make sure highlighted curve is rendered last
|
||||
int* curvesIndex = (int*)_malloca(sizeof(int) * curveCount);
|
||||
for (size_t c = 0; c < curveCount; c++)
|
||||
curvesIndex[c] = int(c);
|
||||
int highLightedCurveIndex = -1;
|
||||
if (overCurve != -1 && curveCount)
|
||||
{
|
||||
ImSwap(curvesIndex[overCurve], curvesIndex[curveCount - 1]);
|
||||
highLightedCurveIndex = overCurve;
|
||||
}
|
||||
|
||||
for (size_t cur = 0; cur < curveCount; cur++)
|
||||
{
|
||||
int c = curvesIndex[cur];
|
||||
if (!delegate.IsVisible(c))
|
||||
continue;
|
||||
const size_t ptCount = delegate.GetPointCount(c);
|
||||
if (ptCount < 1)
|
||||
continue;
|
||||
CurveType curveType = delegate.GetCurveType(c);
|
||||
if (curveType == CurveNone)
|
||||
continue;
|
||||
const ImVec2* pts = delegate.GetPoints(c);
|
||||
uint32_t curveColor = delegate.GetCurveColor(c);
|
||||
if ((c == highLightedCurveIndex && selection.empty() && !selectingQuad) || movingCurve == c)
|
||||
curveColor = 0xFFFFFFFF;
|
||||
|
||||
for (size_t p = 0; p < ptCount - 1; p++)
|
||||
{
|
||||
const ImVec2 p1 = pointToRange(pts[p]);
|
||||
const ImVec2 p2 = pointToRange(pts[p + 1]);
|
||||
|
||||
if (curveType == CurveSmooth || curveType == CurveLinear)
|
||||
{
|
||||
size_t subStepCount = (curveType == CurveSmooth) ? 20 : 2;
|
||||
float step = 1.f / float(subStepCount - 1);
|
||||
for (size_t substep = 0; substep < subStepCount - 1; substep++)
|
||||
{
|
||||
float t = float(substep) * step;
|
||||
|
||||
const ImVec2 sp1 = ImLerp(p1, p2, t);
|
||||
const ImVec2 sp2 = ImLerp(p1, p2, t + step);
|
||||
|
||||
const float rt1 = smoothstep(p1.x, p2.x, sp1.x);
|
||||
const float rt2 = smoothstep(p1.x, p2.x, sp2.x);
|
||||
|
||||
const ImVec2 pos1 = ImVec2(sp1.x, ImLerp(p1.y, p2.y, rt1)) * viewSize + offset;
|
||||
const ImVec2 pos2 = ImVec2(sp2.x, ImLerp(p1.y, p2.y, rt2)) * viewSize + offset;
|
||||
|
||||
if (distance(io.MousePos.x, io.MousePos.y, pos1.x, pos1.y, pos2.x, pos2.y) < 8.f && !scrollingV)
|
||||
{
|
||||
localOverCurve = int(c);
|
||||
overCurve = int(c);
|
||||
overCurveOrPoint = true;
|
||||
}
|
||||
|
||||
draw_list->AddLine(pos1, pos2, curveColor, 1.3f);
|
||||
} // substep
|
||||
}
|
||||
else if (curveType == CurveDiscrete)
|
||||
{
|
||||
ImVec2 dp1 = p1 * viewSize + offset;
|
||||
ImVec2 dp2 = ImVec2(p2.x, p1.y) * viewSize + offset;
|
||||
ImVec2 dp3 = p2 * viewSize + offset;
|
||||
draw_list->AddLine(dp1, dp2, curveColor, 1.3f);
|
||||
draw_list->AddLine(dp2, dp3, curveColor, 1.3f);
|
||||
|
||||
if ((distance(io.MousePos.x, io.MousePos.y, dp1.x, dp1.y, dp3.x, dp1.y) < 8.f ||
|
||||
distance(io.MousePos.x, io.MousePos.y, dp3.x, dp1.y, dp3.x, dp3.y) < 8.f)
|
||||
/*&& localOverCurve == -1*/)
|
||||
{
|
||||
localOverCurve = int(c);
|
||||
overCurve = int(c);
|
||||
overCurveOrPoint = true;
|
||||
}
|
||||
}
|
||||
} // point loop
|
||||
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
const int drawState = DrawPoint(draw_list, pointToRange(pts[p]), viewSize, offset, (selection.find({ int(c), int(p) }) != selection.end() && movingCurve == -1 && !scrollingV));
|
||||
if (drawState && movingCurve == -1 && !selectingQuad)
|
||||
{
|
||||
overCurveOrPoint = true;
|
||||
overSelectedPoint = true;
|
||||
overCurve = -1;
|
||||
if (drawState == 2)
|
||||
{
|
||||
if (!io.KeyShift && selection.find({ int(c), int(p) }) == selection.end())
|
||||
selection.clear();
|
||||
selection.insert({ int(c), int(p) });
|
||||
}
|
||||
}
|
||||
}
|
||||
} // curves loop
|
||||
|
||||
if (localOverCurve == -1)
|
||||
overCurve = -1;
|
||||
|
||||
// move selection
|
||||
static bool pointsMoved = false;
|
||||
static ImVec2 mousePosOrigin;
|
||||
static std::vector<ImVec2> originalPoints;
|
||||
if (overSelectedPoint && io.MouseDown[0])
|
||||
{
|
||||
if ((fabsf(io.MouseDelta.x) > 0.f || fabsf(io.MouseDelta.y) > 0.f) && !selection.empty())
|
||||
{
|
||||
if (!pointsMoved)
|
||||
{
|
||||
delegate.BeginEdit(0);
|
||||
mousePosOrigin = io.MousePos;
|
||||
originalPoints.resize(selection.size());
|
||||
int index = 0;
|
||||
for (auto& sel : selection)
|
||||
{
|
||||
const ImVec2* pts = delegate.GetPoints(sel.curveIndex);
|
||||
originalPoints[index++] = pts[sel.pointIndex];
|
||||
}
|
||||
}
|
||||
pointsMoved = true;
|
||||
ret = 1;
|
||||
auto prevSelection = selection;
|
||||
int originalIndex = 0;
|
||||
for (auto& sel : prevSelection)
|
||||
{
|
||||
const ImVec2 p = rangeToPoint(pointToRange(originalPoints[originalIndex]) + (io.MousePos - mousePosOrigin) * sizeOfPixel);
|
||||
const int newIndex = delegate.EditPoint(sel.curveIndex, sel.pointIndex, p);
|
||||
if (newIndex != sel.pointIndex)
|
||||
{
|
||||
selection.erase(sel);
|
||||
selection.insert({ sel.curveIndex, newIndex });
|
||||
}
|
||||
originalIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (overSelectedPoint && !io.MouseDown[0])
|
||||
{
|
||||
overSelectedPoint = false;
|
||||
if (pointsMoved)
|
||||
{
|
||||
pointsMoved = false;
|
||||
delegate.EndEdit();
|
||||
}
|
||||
}
|
||||
|
||||
// add point
|
||||
if (overCurve != -1 && io.MouseDoubleClicked[0])
|
||||
{
|
||||
const ImVec2 np = rangeToPoint((io.MousePos - offset) / viewSize);
|
||||
delegate.BeginEdit(overCurve);
|
||||
delegate.AddPoint(overCurve, np);
|
||||
delegate.EndEdit();
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
// move curve
|
||||
|
||||
if (movingCurve != -1)
|
||||
{
|
||||
const size_t ptCount = delegate.GetPointCount(movingCurve);
|
||||
const ImVec2* pts = delegate.GetPoints(movingCurve);
|
||||
if (!pointsMoved)
|
||||
{
|
||||
mousePosOrigin = io.MousePos;
|
||||
pointsMoved = true;
|
||||
originalPoints.resize(ptCount);
|
||||
for (size_t index = 0; index < ptCount; index++)
|
||||
{
|
||||
originalPoints[index] = pts[index];
|
||||
}
|
||||
}
|
||||
if (ptCount >= 1)
|
||||
{
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
delegate.EditPoint(movingCurve, int(p), rangeToPoint(pointToRange(originalPoints[p]) + (io.MousePos - mousePosOrigin) * sizeOfPixel));
|
||||
}
|
||||
ret = 1;
|
||||
}
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
movingCurve = -1;
|
||||
pointsMoved = false;
|
||||
delegate.EndEdit();
|
||||
}
|
||||
}
|
||||
if (movingCurve == -1 && overCurve != -1 && ImGui::IsMouseClicked(0) && selection.empty() && !selectingQuad)
|
||||
{
|
||||
movingCurve = overCurve;
|
||||
delegate.BeginEdit(overCurve);
|
||||
}
|
||||
|
||||
// quad selection
|
||||
if (selectingQuad)
|
||||
{
|
||||
const ImVec2 bmin = ImMin(quadSelection, io.MousePos);
|
||||
const ImVec2 bmax = ImMax(quadSelection, io.MousePos);
|
||||
draw_list->AddRectFilled(bmin, bmax, 0x40FF0000, 1.f);
|
||||
draw_list->AddRect(bmin, bmax, 0xFFFF0000, 1.f);
|
||||
const ImRect selectionQuad(bmin, bmax);
|
||||
if (!io.MouseDown[0])
|
||||
{
|
||||
if (!io.KeyShift)
|
||||
selection.clear();
|
||||
// select everythnig is quad
|
||||
for (size_t c = 0; c < curveCount; c++)
|
||||
{
|
||||
if (!delegate.IsVisible(c))
|
||||
continue;
|
||||
|
||||
const size_t ptCount = delegate.GetPointCount(c);
|
||||
if (ptCount < 1)
|
||||
continue;
|
||||
|
||||
const ImVec2* pts = delegate.GetPoints(c);
|
||||
for (size_t p = 0; p < ptCount; p++)
|
||||
{
|
||||
const ImVec2 center = pointToRange(pts[p]) * viewSize + offset;
|
||||
if (selectionQuad.Contains(center))
|
||||
selection.insert({ int(c), int(p) });
|
||||
}
|
||||
}
|
||||
// done
|
||||
selectingQuad = false;
|
||||
}
|
||||
}
|
||||
if (!overCurveOrPoint && ImGui::IsMouseClicked(0) && !selectingQuad && movingCurve == -1 && !overSelectedPoint && container.Contains(io.MousePos))
|
||||
{
|
||||
selectingQuad = true;
|
||||
quadSelection = io.MousePos;
|
||||
}
|
||||
if (clippingRect)
|
||||
draw_list->PopClipRect();
|
||||
|
||||
ImGui::EndChild();
|
||||
ImGui::PopStyleVar();
|
||||
ImGui::PopStyleColor(1);
|
||||
|
||||
if (selectedPoints)
|
||||
{
|
||||
selectedPoints->resize(int(selection.size()));
|
||||
int index = 0;
|
||||
for (auto& point : selection)
|
||||
(*selectedPoints)[index++] = point;
|
||||
}
|
||||
_freea(curvesIndex);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// https://github.com/CedricGuillemet/ImGuizmo
|
||||
// v1.92.5 WIP
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2016-2026 Cedric Guillemet and contributors
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "imgui.h"
|
||||
|
||||
struct ImRect;
|
||||
|
||||
namespace ImCurveEdit
|
||||
{
|
||||
enum CurveType
|
||||
{
|
||||
CurveNone,
|
||||
CurveDiscrete,
|
||||
CurveLinear,
|
||||
CurveSmooth,
|
||||
CurveBezier,
|
||||
};
|
||||
|
||||
struct EditPoint
|
||||
{
|
||||
int curveIndex;
|
||||
int pointIndex;
|
||||
bool operator <(const EditPoint& other) const
|
||||
{
|
||||
if (curveIndex < other.curveIndex)
|
||||
return true;
|
||||
if (curveIndex > other.curveIndex)
|
||||
return false;
|
||||
|
||||
if (pointIndex < other.pointIndex)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct Delegate
|
||||
{
|
||||
bool focused = false;
|
||||
virtual size_t GetCurveCount() = 0;
|
||||
virtual bool IsVisible(size_t /*curveIndex*/) { return true; }
|
||||
virtual CurveType GetCurveType(size_t /*curveIndex*/) const { return CurveLinear; }
|
||||
virtual ImVec2& GetMin() = 0;
|
||||
virtual ImVec2& GetMax() = 0;
|
||||
virtual size_t GetPointCount(size_t curveIndex) = 0;
|
||||
virtual uint32_t GetCurveColor(size_t curveIndex) = 0;
|
||||
virtual ImVec2* GetPoints(size_t curveIndex) = 0;
|
||||
virtual int EditPoint(size_t curveIndex, int pointIndex, ImVec2 value) = 0;
|
||||
virtual void AddPoint(size_t curveIndex, ImVec2 value) = 0;
|
||||
virtual unsigned int GetBackgroundColor() { return 0xFF202020; }
|
||||
// handle undo/redo thru this functions
|
||||
virtual void BeginEdit(int /*index*/) {}
|
||||
virtual void EndEdit() {}
|
||||
|
||||
virtual ~Delegate() = default;
|
||||
};
|
||||
|
||||
int Edit(Delegate& delegate, const ImVec2& size, unsigned int id, const ImRect* clippingRect = NULL, ImVector<EditPoint>* selectedPoints = NULL);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "4dbug/imcurveedit-luau"
|
||||
version = "0.1.0"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
license = "MIT"
|
||||
description = "Curve editor (ImCurveEdit from ImGuizmo) for imgui-luau."
|
||||
include = ["ImCurveEdit.lua", "LICENSE", "README.md", "wally.toml", "default.project.json"]
|
||||
|
||||
[peer-dependencies]
|
||||
ImGui = "4dbug/imgui-luau@^0.4.1"
|
||||
Reference in new issue
Block a user