Files

71 lines
3.2 KiB
Luau

-- 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)