From ad9b3fd14013658f779227a881afc69a7f66a317 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 17 Oct 2026 08:24:33 -0500 Subject: [PATCH] ImSequencer: port of ImGuizmo's ImSequencer (@105e4fd), tests, Roblox example (MySequence + ImCurveEdit RampEdit) --- .gitmodules | 6 + ImSequencer.lua | 565 +++++++++++++++++++++++ LICENSE | 24 + README.md | 55 +++ default.project.json | 12 + examples/roblox/main.client.luau | 143 ++++++ tests/t_edit.luau | 89 ++++ tests/t_load.luau | 5 + tools/core | 1 + tools/imcurveedit | 1 + tools/test.sh | 15 + tools/upstream/LICENSE | 21 + tools/upstream/UPSTREAM.txt | 1 + tools/upstream/src/ImSequencer.cpp | 695 +++++++++++++++++++++++++++++ tools/upstream/src/ImSequencer.h | 79 ++++ wally.toml | 11 + 16 files changed, 1723 insertions(+) create mode 100644 .gitmodules create mode 100644 ImSequencer.lua create mode 100644 LICENSE create mode 100644 README.md create mode 100644 default.project.json create mode 100644 examples/roblox/main.client.luau create mode 100644 tests/t_edit.luau create mode 100644 tests/t_load.luau create mode 160000 tools/core create mode 160000 tools/imcurveedit create mode 100755 tools/test.sh create mode 100644 tools/upstream/LICENSE create mode 100644 tools/upstream/UPSTREAM.txt create mode 100644 tools/upstream/src/ImSequencer.cpp create mode 100644 tools/upstream/src/ImSequencer.h create mode 100644 wally.toml diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..692151a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "tools/core"] + path = tools/core + url = https://git.bug.tools/bug/imgui-luau.git +[submodule "tools/imcurveedit"] + path = tools/imcurveedit + url = https://git.bug.tools/bug/imcurveedit-luau.git diff --git a/ImSequencer.lua b/ImSequencer.lua new file mode 100644 index 0000000..31aba99 --- /dev/null +++ b/ImSequencer.lua @@ -0,0 +1,565 @@ +-- ImSequencer for imgui-luau. Port of ImSequencer.{h,cpp} from https://github.com/CedricGuillemet/ImGuizmo +-- (105e4fde45e4e74aae8973d98750820ef7aca7e2). +-- Usage (same as C++): +-- local ImSequencer = require(path.ImSequencer)(G) -- G = the table returned by the ImGui module +-- ret, currentFrame, expanded, selectedEntry, firstFrame = +-- ImSequencer.Sequencer(sequence, currentFrame, expanded, selectedEntry, firstFrame, sequenceOptions) +-- [Port] pointer arguments are passed by value and returned after `ret` (pass nil for a C++ NULL pointer, it is +-- returned as nil). Item and type indices are 1-based; selectedEntry -1 = none. +-- [Port] SequenceInterface (C++ abstract struct) is any table with these methods: +-- required: GetFrameMin(), GetFrameMax(), GetItemCount(), +-- Get(index) -> start, end, type, color (C++ Get(index, int** start, int** end, int* type, unsigned* color)) +-- Set(index, start, end) ([Port] writes through C++'s int** start/end when a slot is dragged) +-- optional (upstream defaults): BeginEdit(index), EndEdit(), GetItemTypeCount() -> 0, GetItemTypeName(typeIndex) -> "", +-- GetItemLabel(index) -> "", GetCollapseFmt() -> "%d Frames / %d entries", Add(typeIndex), Del(index), +-- Duplicate(index), Copy(), Paste(), GetCustomHeight(index) -> 0, DoubleClick(index), +-- CustomDraw(index, draw_list, rc, legendRect, clippingRect, legendClippingRect), +-- CustomDrawCompact(index, draw_list, rc, clippingRect) (rects are ImRect) +-- sequence.focused is set by Sequencer. +-- Like upstream, zoom/drag state is shared by every sequencer (C++ function statics). +-- Plain Lua 5.1 syntax so the same file loads on LÖVE (LuaJIT). +return function(G) + local ImGui, ImVec2, ImRect = G.ImGui, G.ImVec2, G.ImRect + local floor, ceil, abs, min_, max_ = math.floor, math.ceil, math.abs, math.min, math.max + local FLT_EPSILON = 1.192092896e-07 + local band = bit32.band + + local ImSequencer = {} + ImSequencer.SEQUENCER_EDIT_NONE = 0 + ImSequencer.SEQUENCER_EDIT_STARTEND = 2 + ImSequencer.SEQUENCER_CHANGE_FRAME = 8 + ImSequencer.SEQUENCER_ADD = 16 + ImSequencer.SEQUENCER_DEL = 32 + ImSequencer.SEQUENCER_COPYPASTE = 64 + ImSequencer.SEQUENCER_EDIT_ALL = 2 + 8 + local SEQUENCER_EDIT_STARTEND, SEQUENCER_CHANGE_FRAME, SEQUENCER_ADD, SEQUENCER_DEL, SEQUENCER_COPYPASTE = 2, 8, 16, 32, 64 + + local function int(x) -- C (int) cast: truncation toward zero + if x >= 0 then return floor(x) end + return ceil(x) + end + local function clamp(v, lo, hi) if v < lo then return lo elseif v > hi then return hi end return v end + + -- optional interface methods with upstream defaults + local function call(seq, name, default, ...) + local f = seq[name] + if f then return f(seq, ...) end + return default + end + + local function SequencerAddDelButton(draw_list, pos, add) + if add == nil then add = true end + local io = ImGui.GetIO() + local btnRect = ImRect(pos, ImVec2(pos.x + 16, pos.y + 16)) + local overBtn = btnRect:Contains(io.MousePos) + local containedClick = overBtn and btnRect:Contains(io.MouseClickedPos[0]) + local clickedBtn = containedClick and io.MouseReleased[0] + local btnColor = overBtn and 0xAAEAFFAA or 0x77A3B2AA + if containedClick and io.MouseDownDuration[0] > 0 then + btnRect:Expand(2.0) + end + + local midy = pos.y + 16 / 2 - 0.5 + local midx = pos.x + 16 / 2 - 0.5 + draw_list:AddRect(btnRect.Min, btnRect.Max, btnColor, 4) + draw_list:AddLine(ImVec2(btnRect.Min.x + 3, midy), ImVec2(btnRect.Max.x - 3, midy), btnColor, 2) + if add then + draw_list:AddLine(ImVec2(midx, btnRect.Min.y + 3), ImVec2(midx, btnRect.Max.y - 3), btnColor, 2) + end + return clickedBtn + end + + -- function statics + local framePixelWidth = 10.0 + local framePixelWidthTarget = 10.0 + local movingEntry = -1 + local movingPos = -1 + local movingPart = -1 + local MovingScrollBar = false + local MovingCurrentFrame = false + local panningView = false + local panningViewSource = ImVec2(0, 0) + local panningViewFrame = 0 + local sizingRBar = false + local sizingLBar = false + local cursorWidth = 8.0 + local MinBarWidth = 44.0 + + function ImSequencer.Sequencer(sequence, currentFrame, expanded, selectedEntry, firstFrame, sequenceOptions) + local ret = false + local io = ImGui.GetIO() + local cx = int(io.MousePos.x) + local cy = int(io.MousePos.y) + local legendWidth = 200 + + local delEntry = -1 + local dupEntry = -1 + local ItemHeight = 20 + + local popupOpened = false + local sequenceCount = sequence:GetItemCount() + if sequenceCount == 0 then + return false, currentFrame, expanded, selectedEntry, firstFrame + end + local FrameMin, FrameMax = sequence:GetFrameMin(), sequence:GetFrameMax() + ImGui.BeginGroup() + + local draw_list = ImGui.GetWindowDrawList() + local canvas_pos = ImGui.GetCursorScreenPos() -- ImDrawList API uses screen coordinates! + local canvas_size = ImGui.GetContentRegionAvail() -- Resize canvas to what's available + local firstFrameUsed = firstFrame or 0 + + local controlHeight = sequenceCount * ItemHeight + for i = 1, sequenceCount do + controlHeight = controlHeight + int(call(sequence, "GetCustomHeight", 0, i)) + end + local frameCount = max_(FrameMax - FrameMin, 1) + + local customDraws = {} + local compactCustomDraws = {} + -- zoom in/out + local visibleFrameCount = floor((canvas_size.x - legendWidth) / framePixelWidth) + local barWidthRatio = min_(visibleFrameCount / frameCount, 1.0) + local barWidthInPixels = barWidthRatio * (canvas_size.x - legendWidth) + + + if ImGui.IsWindowFocused() and io.KeyAlt and io.MouseDown[2] then + if not panningView then + panningViewSource = ImVec2(io.MousePos.x, io.MousePos.y) + panningView = true + panningViewFrame = firstFrame + end + firstFrame = panningViewFrame - int((io.MousePos.x - panningViewSource.x) / framePixelWidth) + firstFrame = clamp(firstFrame, FrameMin, FrameMax - visibleFrameCount) + end + if panningView and not io.MouseDown[2] then + panningView = false + end + framePixelWidthTarget = clamp(framePixelWidthTarget, 0.1, 50.0) + + framePixelWidth = framePixelWidth + (framePixelWidthTarget - framePixelWidth) * 0.33 + + frameCount = FrameMax - FrameMin + if visibleFrameCount >= frameCount and firstFrame then + firstFrame = FrameMin + end + + -- -- + if expanded ~= nil and not expanded then + ImGui.InvisibleButton("canvas", ImVec2(canvas_size.x - canvas_pos.x, ItemHeight)) + draw_list:AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0) + local tmps = string.format(call(sequence, "GetCollapseFmt", "%d Frames / %d entries"), frameCount, sequenceCount) + draw_list:AddText(ImVec2(canvas_pos.x + 26, canvas_pos.y + 2), 0xFFFFFFFF, tmps) + else + local hasScrollBar = true + -- test scroll area + local headerSize = ImVec2(canvas_size.x, ItemHeight) + local scrollBarSize = ImVec2(canvas_size.x, 14.0) + ImGui.InvisibleButton("topBar", headerSize) + draw_list:AddRectFilled(canvas_pos, ImVec2(canvas_pos.x + headerSize.x, canvas_pos.y + headerSize.y), 0xFFFF0000, 0) + local childFramePos = ImGui.GetCursorScreenPos() + local childFrameSize = ImVec2(canvas_size.x, canvas_size.y - 8.0 - headerSize.y - (hasScrollBar and scrollBarSize.y or 0)) + local childFrameRect = ImRect(childFramePos, ImVec2(childFramePos.x + childFrameSize.x, childFramePos.y + childFrameSize.y)) + ImGui.PushStyleColor(G.ImGuiCol.FrameBg, 0) + ImGui.BeginChild(889, childFrameSize, G.ImGuiChildFlags.FrameStyle) + sequence.focused = ImGui.IsWindowFocused() + ImGui.InvisibleButton("contentBar", ImVec2(canvas_size.x, controlHeight)) + local contentMin = ImGui.GetItemRectMin() + local contentMax = ImGui.GetItemRectMax() + local contentHeight = contentMax.y - contentMin.y + + -- full background + draw_list:AddRectFilled(canvas_pos, ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + canvas_size.y), 0xFF242424, 0) + + -- current frame top + local topRect = ImRect(ImVec2(canvas_pos.x + legendWidth, canvas_pos.y), ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + ItemHeight)) + + if not MovingCurrentFrame and not MovingScrollBar and movingEntry == -1 and band(sequenceOptions, SEQUENCER_CHANGE_FRAME) ~= 0 + and currentFrame and currentFrame >= 0 and topRect:Contains(io.MousePos) and io.MouseDown[0] then + MovingCurrentFrame = true + end + if MovingCurrentFrame then + if frameCount ~= 0 then + currentFrame = int((io.MousePos.x - topRect.Min.x) / framePixelWidth) + firstFrameUsed + if currentFrame < FrameMin then currentFrame = FrameMin end + if currentFrame >= FrameMax then currentFrame = FrameMax end + end + if not io.MouseDown[0] then + MovingCurrentFrame = false + end + end + + -- header + draw_list:AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0) + if band(sequenceOptions, SEQUENCER_ADD) ~= 0 then + if SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + legendWidth - ItemHeight, canvas_pos.y + 2), true) then + ImGui.OpenPopup("addEntry") + end + + if ImGui.BeginPopup("addEntry") then + for i = 1, call(sequence, "GetItemTypeCount", 0) do + if ImGui.Selectable(call(sequence, "GetItemTypeName", "", i)) then + call(sequence, "Add", nil, i) + selectedEntry = sequence:GetItemCount() + end + end + ImGui.EndPopup() + popupOpened = true + end + end + + -- header frame number and lines + local modFrameCount = 10 + local frameStep = 1 + while (modFrameCount * framePixelWidth) < 150 do + modFrameCount = modFrameCount * 2 + frameStep = frameStep * 2 + end + local halfModFrameCount = int(modFrameCount / 2) + + local function drawLine(i, regionHeight) + local baseIndex = ((i % modFrameCount) == 0) or (i == FrameMax or i == FrameMin) + local halfIndex = (i % halfModFrameCount) == 0 + local px = int(canvas_pos.x) + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth) + local tiretStart = baseIndex and 4 or (halfIndex and 10 or 14) + local tiretEnd = baseIndex and regionHeight or ItemHeight + + if px <= (canvas_size.x + canvas_pos.x) and px >= (canvas_pos.x + legendWidth) then + draw_list:AddLine(ImVec2(px, canvas_pos.y + tiretStart), ImVec2(px, canvas_pos.y + tiretEnd - 1), 0xFF606060, 1) + draw_list:AddLine(ImVec2(px, canvas_pos.y + ItemHeight), ImVec2(px, canvas_pos.y + regionHeight - 1), 0x30606060, 1) + end + + if baseIndex and px > (canvas_pos.x + legendWidth) then + draw_list:AddText(ImVec2(px + 3.0, canvas_pos.y), 0xFFBBBBBB, tostring(i)) + end + end + + local function drawLineContent(i, regionHeight) + local px = int(canvas_pos.x) + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth) + local tiretStart = int(contentMin.y) + local tiretEnd = int(contentMax.y) + + if px <= (canvas_size.x + canvas_pos.x) and px >= (canvas_pos.x + legendWidth) then + draw_list:AddLine(ImVec2(px, tiretStart), ImVec2(px, tiretEnd), 0x30606060, 1) + end + end + for i = FrameMin, FrameMax, frameStep do + drawLine(i, ItemHeight) + end + drawLine(FrameMin, ItemHeight) + drawLine(FrameMax, ItemHeight) + + -- clip content + draw_list:PushClipRect(childFrameRect.Min, childFrameRect.Max, true) + + -- draw item names in the legend rect on the left + local customHeight = 0 + for i = 1, sequenceCount do + local tpos = ImVec2(contentMin.x + 3, contentMin.y + (i - 1) * ItemHeight + 2 + customHeight) + draw_list:AddText(tpos, 0xFFFFFFFF, call(sequence, "GetItemLabel", "", i)) + + if band(sequenceOptions, SEQUENCER_DEL) ~= 0 then + if SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight + 2 - 10, tpos.y + 2), false) then + delEntry = i + end + if SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight - ItemHeight + 2 - 10, tpos.y + 2), true) then + dupEntry = i + end + end + customHeight = customHeight + call(sequence, "GetCustomHeight", 0, i) + end + + -- slots background + customHeight = 0 + for i = 1, sequenceCount do + local col = ((i - 1) % 2 == 1) and 0xFF3A3636 or 0xFF413D3D + + local localCustomHeight = call(sequence, "GetCustomHeight", 0, i) + local pos = ImVec2(contentMin.x + legendWidth, contentMin.y + ItemHeight * (i - 1) + 1 + customHeight) + local sz = ImVec2(canvas_size.x + canvas_pos.x, pos.y + ItemHeight - 1 + localCustomHeight) + if not popupOpened and cy >= pos.y and cy < pos.y + (ItemHeight + localCustomHeight) and movingEntry == -1 + and cx > contentMin.x and cx < contentMin.x + canvas_size.x then + col = (col + 0x80201008) % 0x100000000 + pos.x = pos.x - legendWidth + end + draw_list:AddRectFilled(pos, sz, col, 0) + customHeight = customHeight + localCustomHeight + end + + draw_list:PushClipRect(ImVec2(childFramePos.x + legendWidth, childFramePos.y), childFrameRect.Max, true) + + -- vertical frame lines in content area + for i = FrameMin, FrameMax, frameStep do + drawLineContent(i, int(contentHeight)) + end + drawLineContent(FrameMin, int(contentHeight)) + drawLineContent(FrameMax, int(contentHeight)) + + -- selection + local selected = selectedEntry ~= nil and selectedEntry >= 1 + if selected then + customHeight = 0 + for i = 1, selectedEntry - 1 do + customHeight = customHeight + call(sequence, "GetCustomHeight", 0, i) + end + draw_list:AddRectFilled(ImVec2(contentMin.x, contentMin.y + ItemHeight * (selectedEntry - 1) + customHeight), + ImVec2(contentMin.x + canvas_size.x, contentMin.y + ItemHeight * selectedEntry + customHeight), 0x801080FF, 1.0) + end + + -- slots + customHeight = 0 + for i = 1, sequenceCount do + local start, end_, _, color = sequence:Get(i) + local localCustomHeight = call(sequence, "GetCustomHeight", 0, i) + + local pos = ImVec2(contentMin.x + legendWidth - firstFrameUsed * framePixelWidth, contentMin.y + ItemHeight * (i - 1) + 1 + customHeight) + local slotP1 = ImVec2(pos.x + start * framePixelWidth, pos.y + 2) + local slotP2 = ImVec2(pos.x + end_ * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2) + local slotP3 = ImVec2(pos.x + end_ * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2 + localCustomHeight) + local slotColor = bit32.bor(color, 0xFF000000) + local slotColorHalf = bit32.bor(band(color, 0xFFFFFF), 0x40000000) + + if slotP1.x <= (canvas_size.x + contentMin.x) and slotP2.x >= (contentMin.x + legendWidth) then + draw_list:AddRectFilled(slotP1, slotP3, slotColorHalf, 2) + draw_list:AddRectFilled(slotP1, slotP2, slotColor, 2) + end + if ImRect(slotP1, slotP2):Contains(io.MousePos) and io.MouseDoubleClicked[0] then + call(sequence, "DoubleClick", nil, i) + end + -- Ensure grabbable handles + local max_handle_width = slotP2.x - slotP1.x / 3.0 -- (sic, upstream) + local min_handle_width = min_(10.0, max_handle_width) + local handle_width = clamp(framePixelWidth / 2.0, min_handle_width, max_handle_width) + local rects = { ImRect(slotP1, ImVec2(slotP1.x + handle_width, slotP2.y)), + ImRect(ImVec2(slotP2.x - handle_width, slotP1.y), slotP2), + ImRect(slotP1, slotP2) } + + local quadColor = { 0xFFFFFFFF, 0xFFFFFFFF, (slotColor + (selected and 0 or 0x202020)) % 0x100000000 } + if movingEntry == -1 and band(sequenceOptions, SEQUENCER_EDIT_STARTEND) ~= 0 then + for j = 3, 1, -1 do + local rc = rects[j] + if rc:Contains(io.MousePos) then + draw_list:AddRectFilled(rc.Min, rc.Max, quadColor[j], 2) + end + end + + for j = 1, 3 do + local rc = rects[j] + if rc:Contains(io.MousePos) and childFrameRect:Contains(io.MousePos) then + if ImGui.IsMouseClicked(0) and not MovingScrollBar and not MovingCurrentFrame then + movingEntry = i + movingPos = cx + movingPart = j + call(sequence, "BeginEdit", nil, movingEntry) + break + end + end + end + end + + -- custom draw + if localCustomHeight > 0 then + local rp = ImVec2(canvas_pos.x, contentMin.y + ItemHeight * (i - 1) + 1 + customHeight) + local customRect = ImRect(ImVec2(rp.x + legendWidth - (firstFrameUsed - FrameMin - 0.5) * framePixelWidth, rp.y + ItemHeight), + ImVec2(rp.x + legendWidth + (FrameMax - firstFrameUsed - 0.5 + 2.0) * framePixelWidth, rp.y + localCustomHeight + ItemHeight)) + local clippingRect = ImRect(ImVec2(rp.x + legendWidth, rp.y + ItemHeight), ImVec2(rp.x + canvas_size.x, rp.y + localCustomHeight + ItemHeight)) + + local legendRect = ImRect(ImVec2(rp.x, rp.y + ItemHeight), ImVec2(rp.x + legendWidth, rp.y + localCustomHeight)) + local legendClippingRect = ImRect(ImVec2(canvas_pos.x, canvas_pos.y + ItemHeight), ImVec2(canvas_pos.x + legendWidth, canvas_pos.y + localCustomHeight + ItemHeight)) + customDraws[#customDraws + 1] = { index = i, customRect = customRect, legendRect = legendRect, clippingRect = clippingRect, legendClippingRect = legendClippingRect } + else + local rp = ImVec2(canvas_pos.x, contentMin.y + ItemHeight * (i - 1) + customHeight) + local customRect = ImRect(ImVec2(rp.x + legendWidth - (firstFrameUsed - FrameMin - 0.5) * framePixelWidth, rp.y), + ImVec2(rp.x + legendWidth + (FrameMax - firstFrameUsed - 0.5 + 2.0) * framePixelWidth, rp.y + ItemHeight)) + local clippingRect = ImRect(ImVec2(rp.x + legendWidth, rp.y), ImVec2(rp.x + canvas_size.x, rp.y + ItemHeight)) + + compactCustomDraws[#compactCustomDraws + 1] = { index = i, customRect = customRect, legendRect = ImRect(), clippingRect = clippingRect, legendClippingRect = ImRect() } + end + customHeight = customHeight + localCustomHeight + end + + -- moving + if movingEntry >= 1 then + ImGui.SetNextFrameWantCaptureMouse(true) + local diffFrame = int((cx - movingPos) / framePixelWidth) + if abs(diffFrame) > 0 then + local l, r = sequence:Get(movingEntry) + if selectedEntry then + selectedEntry = movingEntry + end + if band(movingPart, 1) ~= 0 then l = l + diffFrame end + if band(movingPart, 2) ~= 0 then r = r + diffFrame end + if l < 0 then + if band(movingPart, 2) ~= 0 then r = r - l end + l = 0 + end + if band(movingPart, 1) ~= 0 and l > r then l = r end + if band(movingPart, 2) ~= 0 and r < l then r = l end + sequence:Set(movingEntry, l, r) + movingPos = movingPos + int(diffFrame * framePixelWidth) + end + if not io.MouseDown[0] then + -- single select + if diffFrame == 0 and movingPart ~= 0 and selectedEntry then + selectedEntry = movingEntry + ret = true + end + + movingEntry = -1 + call(sequence, "EndEdit", nil) + end + end + + -- cursor + if currentFrame and firstFrame and currentFrame >= firstFrame and currentFrame <= FrameMax then + local cursorOffset = contentMin.x + legendWidth + (currentFrame - firstFrameUsed) * framePixelWidth + framePixelWidth / 2 - cursorWidth * 0.5 + draw_list:AddLine(ImVec2(cursorOffset, canvas_pos.y), ImVec2(cursorOffset, contentMax.y), 0xA02A2AFF, cursorWidth) + draw_list:AddText(ImVec2(cursorOffset + 10, canvas_pos.y + 2), 0xFF2A2AFF, tostring(currentFrame)) + end + + draw_list:PopClipRect() + draw_list:PopClipRect() + + for _, customDraw in ipairs(customDraws) do + call(sequence, "CustomDraw", nil, customDraw.index, draw_list, customDraw.customRect, customDraw.legendRect, customDraw.clippingRect, customDraw.legendClippingRect) + end + for _, customDraw in ipairs(compactCustomDraws) do + call(sequence, "CustomDrawCompact", nil, customDraw.index, draw_list, customDraw.customRect, customDraw.clippingRect) + end + + -- copy paste + if band(sequenceOptions, SEQUENCER_COPYPASTE) ~= 0 then + local rectCopy = ImRect(ImVec2(contentMin.x + 100, canvas_pos.y + 2), ImVec2(contentMin.x + 100 + 30, canvas_pos.y + ItemHeight - 2)) + local inRectCopy = rectCopy:Contains(io.MousePos) + draw_list:AddText(rectCopy.Min, inRectCopy and 0xFF1080FF or 0xFF000000, "Copy") + + local rectPaste = ImRect(ImVec2(contentMin.x + 140, canvas_pos.y + 2), ImVec2(contentMin.x + 140 + 30, canvas_pos.y + ItemHeight - 2)) + local inRectPaste = rectPaste:Contains(io.MousePos) + draw_list:AddText(rectPaste.Min, inRectPaste and 0xFF1080FF or 0xFF000000, "Paste") + + if inRectCopy and io.MouseReleased[0] then + call(sequence, "Copy", nil) + end + if inRectPaste and io.MouseReleased[0] then + call(sequence, "Paste", nil) + end + end + + ImGui.EndChild() + ImGui.PopStyleColor() + if hasScrollBar then + ImGui.InvisibleButton("scrollBar", scrollBarSize) + local scrollBarMin = ImGui.GetItemRectMin() + local scrollBarMax = ImGui.GetItemRectMax() + + -- ratio = number of frames visible in control / number to total frames + local startFrameOffset = ((firstFrameUsed - FrameMin) / frameCount) * (canvas_size.x - legendWidth) + local scrollBarA = ImVec2(scrollBarMin.x + legendWidth, scrollBarMin.y - 2) + local scrollBarB = ImVec2(scrollBarMin.x + canvas_size.x, scrollBarMax.y - 1) + draw_list:AddRectFilled(scrollBarA, scrollBarB, 0xFF222222, 0) + + local inScrollBar = ImRect(scrollBarA, scrollBarB):Contains(io.MousePos) + + draw_list:AddRectFilled(scrollBarA, scrollBarB, 0xFF101010, 8) + + local scrollBarC = ImVec2(scrollBarMin.x + legendWidth + startFrameOffset, scrollBarMin.y) + local scrollBarD = ImVec2(scrollBarMin.x + legendWidth + barWidthInPixels + startFrameOffset, scrollBarMax.y - 2) + draw_list:AddRectFilled(scrollBarC, scrollBarD, (inScrollBar or MovingScrollBar) and 0xFF606060 or 0xFF505050, 6) + + local barHandleLeft = ImRect(scrollBarC, ImVec2(scrollBarC.x + 14, scrollBarD.y)) + local barHandleRight = ImRect(ImVec2(scrollBarD.x - 14, scrollBarC.y), scrollBarD) + + local onLeft = barHandleLeft:Contains(io.MousePos) + local onRight = barHandleRight:Contains(io.MousePos) + + draw_list:AddRectFilled(barHandleLeft.Min, barHandleLeft.Max, (onLeft or sizingLBar) and 0xFFAAAAAA or 0xFF666666, 6) + draw_list:AddRectFilled(barHandleRight.Min, barHandleRight.Max, (onRight or sizingRBar) and 0xFFAAAAAA or 0xFF666666, 6) + + local scrollBarThumb = ImRect(scrollBarC, scrollBarD) + if sizingRBar then + if not io.MouseDown[0] then + sizingRBar = false + else + local barNewWidth = max_(barWidthInPixels + io.MouseDelta.x, MinBarWidth) + local barRatio = barNewWidth / barWidthInPixels + framePixelWidth = framePixelWidth / barRatio + framePixelWidthTarget = framePixelWidth + local newVisibleFrameCount = int((canvas_size.x - legendWidth) / framePixelWidthTarget) + local lastFrame = firstFrame + newVisibleFrameCount + if lastFrame > FrameMax then + framePixelWidth = (canvas_size.x - legendWidth) / (FrameMax - firstFrame) + framePixelWidthTarget = framePixelWidth + end + end + elseif sizingLBar then + if not io.MouseDown[0] then + sizingLBar = false + else + if abs(io.MouseDelta.x) > FLT_EPSILON then + local barNewWidth = max_(barWidthInPixels - io.MouseDelta.x, MinBarWidth) + local barRatio = barNewWidth / barWidthInPixels + local previousFramePixelWidthTarget = framePixelWidthTarget + framePixelWidth = framePixelWidth / barRatio + framePixelWidthTarget = framePixelWidth + local newVisibleFrameCount = int(visibleFrameCount / barRatio) + local newFirstFrame = firstFrame + newVisibleFrameCount - visibleFrameCount + newFirstFrame = clamp(newFirstFrame, FrameMin, max_(FrameMax - visibleFrameCount, FrameMin)) + if newFirstFrame == firstFrame then + framePixelWidth = previousFramePixelWidthTarget + framePixelWidthTarget = previousFramePixelWidthTarget + else + firstFrame = newFirstFrame + end + end + end + else + if MovingScrollBar then + if not io.MouseDown[0] then + MovingScrollBar = false + else + local framesPerPixelInBar = barWidthInPixels / visibleFrameCount + firstFrame = int((io.MousePos.x - panningViewSource.x) / framesPerPixelInBar) - panningViewFrame + firstFrame = clamp(firstFrame, FrameMin, max_(FrameMax - visibleFrameCount, FrameMin)) + end + else + if scrollBarThumb:Contains(io.MousePos) and ImGui.IsMouseClicked(0) and firstFrame and not MovingCurrentFrame and movingEntry == -1 then + MovingScrollBar = true + panningViewSource = ImVec2(io.MousePos.x, io.MousePos.y) + panningViewFrame = -firstFrame + end + if not sizingRBar and onRight and ImGui.IsMouseClicked(0) then + sizingRBar = true + end + if not sizingLBar and onLeft and ImGui.IsMouseClicked(0) then + sizingLBar = true + end + end + end + end + end + + ImGui.EndGroup() + + -- (upstream: mouse wheel zoom over the region is #if 0'd out) + + if expanded ~= nil then + if SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + 2, canvas_pos.y + 2), not expanded) then + expanded = not expanded + end + end + + if delEntry ~= -1 then + call(sequence, "Del", nil, delEntry) + if selectedEntry and (selectedEntry == delEntry or selectedEntry > sequence:GetItemCount()) then + selectedEntry = -1 + end + end + + if dupEntry ~= -1 then + call(sequence, "Duplicate", nil, dupEntry) + end + return ret, currentFrame, expanded, selectedEntry, firstFrame + end + + return ImSequencer +end diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1f9a5ae --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..088c1bd --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# imsequencer-luau + +Timeline/sequencer widget for imgui-luau: items as slots on a frame timeline, drag slots and their edges, scrub the +current frame, zoom/scroll with the scrollbar, add/delete/duplicate items, expandable rows with custom drawing. + +Luau port of ImSequencer 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/imsequencer-luau) / [GitHub](https://github.com/4DBug/imsequencer-luau). +The example embeds [imcurveedit-luau](https://git.bug.tools/bug/imcurveedit-luau) in expanded rows, like upstream's. + +## Usage + +```lua +local G = require(ReplicatedStorage.ImGui) +local ImSequencer = require(ReplicatedStorage.ImSequencer)(G) + +-- SequenceInterface (C++ ImSequencer::SequenceInterface): any table with these methods, indices 1-based +local Seq = { Items = { { s = 10, e = 30 }, { s = 20, e = 60 } } } +function Seq:GetFrameMin() return 0 end +function Seq:GetFrameMax() return 100 end +function Seq:GetItemCount() return #self.Items end +function Seq:Get(i) local it = self.Items[i]; return it.s, it.e, 1, 0xFFAA8080 end -- start, end, type, color +function Seq:Set(i, s, e) self.Items[i].s, self.Items[i].e = s, e end -- [Port] see below +-- optional: GetItemLabel(i), GetItemTypeCount(), GetItemTypeName(t), Add(t), Del(i), Duplicate(i), Copy(), Paste(), +-- GetCustomHeight(i), DoubleClick(i), CustomDraw(...), CustomDrawCompact(...), BeginEdit(i), EndEdit(), GetCollapseFmt() + +local currentFrame, expanded, selectedEntry, firstFrame = 0, true, -1, 0 +local flags = bit32.bor(ImSequencer.SEQUENCER_EDIT_STARTEND, ImSequencer.SEQUENCER_CHANGE_FRAME) +-- each frame, inside a window: +local selected +selected, currentFrame, expanded, selectedEntry, firstFrame = + ImSequencer.Sequencer(Seq, currentFrame, expanded, selectedEntry, firstFrame, flags) +``` + +`[Port]` differences: +- The pointer arguments (`int* currentFrame`, `bool* expanded`, `int* selectedEntry`, `int* firstFrame`) are passed by + value and returned after the result; pass `nil` where C++ passes `NULL` (e.g. `expanded = nil` hides the expand button). +- `Get(index, int** start, int** end, int* type, unsigned* color)` becomes `Get(index) -> start, end, type, color`, and + writes through the `int**` become a required `Set(index, start, end)`. +- Item and type indices are 1-based, `selectedEntry` `-1` = none. `CustomDraw`/`CustomDrawCompact` receive `ImRect`s. +- As upstream, zoom and drag state is shared by all sequencers (C++ function statics). + +Full example (upstream's `MySequence` + `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/imsequencer-luau.git +bash tools/test.sh # tests/t_*.luau in the core's Roblox mock + the Roblox example (uses tools/imcurveedit) +``` + +Upstream source (pinned) is in `tools/upstream/`; port-specific comments are tagged `[Port]`. + +## License + +MIT, see LICENSE (upstream copyright kept). diff --git a/default.project.json b/default.project.json new file mode 100644 index 0000000..9d77672 --- /dev/null +++ b/default.project.json @@ -0,0 +1,12 @@ +{ + "name": "imsequencer-luau", + "tree": { + "$className": "DataModel", + "ReplicatedStorage": { + "ImGui": { "$path": "tools/core/build/ImGuiTyped.luau", "Impl": { "$path": "tools/core/build/ImGui.luau" } }, + "ImSequencer": { "$path": "ImSequencer.lua" }, + "ImCurveEdit": { "$path": "tools/imcurveedit/ImCurveEdit.lua" } + }, + "StarterPlayer": { "StarterPlayerScripts": { "ImSequencerExample": { "$path": "examples/roblox/main.client.luau" } } } + } +} diff --git a/examples/roblox/main.client.luau b/examples/roblox/main.client.luau new file mode 100644 index 0000000..61d93ba --- /dev/null +++ b/examples/roblox/main.client.luau @@ -0,0 +1,143 @@ +-- ImSequencer: upstream's example (MySequence with 5 items; double-click an item to expand it into an ImCurveEdit +-- RampEdit). Drag slots / their edges, scrub the frame in the header, zoom with the scrollbar handles, +-- +/- buttons add/delete/duplicate items. +local RS = game:GetService("ReplicatedStorage") +local RunService = game:GetService("RunService") +local G = require(RS:WaitForChild("ImGui")) +local ImSequencer = require(RS:WaitForChild("ImSequencer"))(G) +local ImCurveEdit = require(RS:WaitForChild("ImCurveEdit"))(G) +local ImGui, ImVec2, ImRect = G.ImGui, G.ImVec2, G.ImRect + +local SequencerItemTypeNames = { "Camera", "Music", "ScreenEffect", "FadeIn", "Animation" } + +-- ImCurveEdit delegate (upstream RampEdit) +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(0, 0), + mMax = ImVec2(1, 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 + +-- ImSequencer interface (upstream MySequence) +local MySequence = { mFrameMin = -100, mFrameMax = 1000, myItems = {} } +local function Item(type, s, e, expanded) return { mType = type, mFrameStart = s, mFrameEnd = e, mExpanded = expanded } end +MySequence.myItems = { Item(1, 10, 30, false), Item(2, 20, 30, true), Item(4, 12, 60, false), Item(3, 61, 90, false), Item(5, 90, 99, false) } +function MySequence:GetFrameMin() return self.mFrameMin end +function MySequence:GetFrameMax() return self.mFrameMax end +function MySequence:GetItemCount() return #self.myItems end +function MySequence:GetItemTypeCount() return #SequencerItemTypeNames end +function MySequence:GetItemTypeName(typeIndex) return SequencerItemTypeNames[typeIndex] end +function MySequence:GetItemLabel(index) + return string.format("[%02d] %s", index - 1, SequencerItemTypeNames[self.myItems[index].mType]) +end +function MySequence:Get(index) + local item = self.myItems[index] + return item.mFrameStart, item.mFrameEnd, item.mType, 0xFFAA8080 -- same color for everyone, return color based on type +end +function MySequence:Set(index, s, e) + local item = self.myItems[index] + item.mFrameStart, item.mFrameEnd = s, e +end +function MySequence:Add(type) table.insert(self.myItems, Item(type, 0, 10, false)) end +function MySequence:Del(index) table.remove(self.myItems, index) end +function MySequence:Duplicate(index) table.insert(self.myItems, table.clone(self.myItems[index])) end +function MySequence:GetCustomHeight(index) return self.myItems[index].mExpanded and 300 or 0 end +function MySequence:DoubleClick(index) + if self.myItems[index].mExpanded then + self.myItems[index].mExpanded = false + return + end + for _, item in self.myItems do item.mExpanded = false end + self.myItems[index].mExpanded = not self.myItems[index].mExpanded +end +local labels = { "Translation", "Rotation", "Scale" } +function MySequence:CustomDraw(index, draw_list, rc, legendRect, clippingRect, legendClippingRect) + RampEdit.mMax = ImVec2(self.mFrameMax, 1) + RampEdit.mMin = ImVec2(self.mFrameMin, 0) + draw_list:PushClipRect(legendClippingRect.Min, legendClippingRect.Max, true) + for i = 1, 3 do + local pta = ImVec2(legendRect.Min.x + 30, legendRect.Min.y + (i - 1) * 14) + local ptb = ImVec2(legendRect.Max.x, legendRect.Min.y + i * 14) + draw_list:AddText(pta, RampEdit.mbVisible[i] and 0xFFFFFFFF or 0x80FFFFFF, labels[i]) + if ImRect(pta, ptb):Contains(ImGui.GetMousePos()) and ImGui.IsMouseClicked(0) then + RampEdit.mbVisible[i] = not RampEdit.mbVisible[i] + end + end + draw_list:PopClipRect() + + ImGui.SetCursorScreenPos(rc.Min) + ImCurveEdit.Edit(RampEdit, ImVec2(rc.Max.x - rc.Min.x, rc.Max.y - rc.Min.y), 137 + index, clippingRect) +end +function MySequence:CustomDrawCompact(index, draw_list, rc, clippingRect) + RampEdit.mMax = ImVec2(self.mFrameMax, 1) + RampEdit.mMin = ImVec2(self.mFrameMin, 0) + draw_list:PushClipRect(clippingRect.Min, clippingRect.Max, true) + for i = 1, 3 do + for _, pt in RampEdit.mPts[i] do + local p = pt.x + local item = self.myItems[index] + if p >= item.mFrameStart and p <= item.mFrameEnd then + local r = (p - self.mFrameMin) / (self.mFrameMax - self.mFrameMin) + local x = rc.Min.x + (rc.Max.x - rc.Min.x) * r + draw_list:AddLine(ImVec2(x, rc.Min.y + 6), ImVec2(x, rc.Max.y - 4), 0xAA000000, 4) + end + end + end + draw_list:PopClipRect() +end + +local selectedEntry, firstFrame, expanded, currentFrame = -1, 0, true, 100 +local S = ImSequencer +local options = bit32.bor(S.SEQUENCER_EDIT_STARTEND, S.SEQUENCER_ADD, S.SEQUENCER_DEL, S.SEQUENCER_COPYPASTE, S.SEQUENCER_CHANGE_FRAME) + +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui +RunService.RenderStepped:Connect(function() + G.ImGui_ImplRoblox.NewFrame() + ImGui.NewFrame() + ImGui.SetNextWindowSize(ImVec2(900, 600), G.ImGuiCond.FirstUseEver) + ImGui.Begin("ImSequencer") + ImGui.PushItemWidth(130) + MySequence.mFrameMin = ImGui.InputInt("Frame Min", MySequence.mFrameMin) + ImGui.SameLine() + currentFrame = ImGui.InputInt("Frame ", currentFrame) + ImGui.SameLine() + MySequence.mFrameMax = ImGui.InputInt("Frame Max", MySequence.mFrameMax) + ImGui.PopItemWidth() + _, currentFrame, expanded, selectedEntry, firstFrame = ImSequencer.Sequencer(MySequence, currentFrame, expanded, selectedEntry, firstFrame, options) + -- add a UI to edit that particular item + if selectedEntry ~= -1 then + local item = MySequence.myItems[selectedEntry] + ImGui.Text("I am a %s, please edit me", SequencerItemTypeNames[item.mType]) + end + ImGui.End() + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) +end) diff --git a/tests/t_edit.luau b/tests/t_edit.luau new file mode 100644 index 0000000..a73eaf6 --- /dev/null +++ b/tests/t_edit.luau @@ -0,0 +1,89 @@ +-- Scrub the current frame in the header, drag a slot body (start+end move), drag a slot's right edge (end only), +-- click a slot to select it; empty sequences return early; Del via the "-" button clears the selection. +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImSequencer = require(RS:WaitForChild("ImSequencer"))(G) +local ImGui, ImVec2 = G.ImGui, G.ImVec2 +local S = ImSequencer + +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() +local io = ImGui.GetIO() + +local Seq = { Items = { { s = 10, e = 20 }, { s = 30, e = 40 } }, Edits = 0, Open = 0 } +function Seq:GetFrameMin() return 0 end +function Seq:GetFrameMax() return 100 end +function Seq:GetItemCount() return #self.Items end +function Seq:Get(i) return self.Items[i].s, self.Items[i].e, 1, 0xFF8080AA end +function Seq:Set(i, s, e) self.Items[i].s, self.Items[i].e = s, e end +function Seq:BeginEdit() self.Open += 1 end +function Seq:EndEdit() self.Open -= 1 end +function Seq:Del(i) table.remove(self.Items, i) end + +-- empty sequence: returns false and the values untouched +local empty = { GetFrameMin = function() return 0 end, GetFrameMax = function() return 10 end, GetItemCount = function() return 0 end } + +local frame, done, origin = 0, false, nil +local ret, currentFrame, expanded, selectedEntry, firstFrame = false, 0, true, -1, 0 +local opts = bit32.bor(S.SEQUENCER_EDIT_STARTEND, S.SEQUENCER_CHANGE_FRAME, S.SEQUENCER_DEL) +local fpw = 10 -- framePixelWidth (static default; firstFrame stays 0 since all frames fit) +local function frameX(f) return origin.x + 200 + f * fpw end +local function rowY(i) return origin.y + 20 + (i - 1) * 20 + 10 end -- header (20) + row center +game:GetService("RunService").Stepped:Connect(function() + if done then return end + frame += 1 + G.ImGui_ImplRoblox.NewFrame() + io.DeltaTime = 1 / 60 + -- scrub: press in the header at frame 50 + if frame == 3 then io:AddMousePosEvent(frameX(50) + 2, origin.y + 10) + elseif frame == 4 then io:AddMouseButtonEvent(0, true) + elseif frame == 5 then io:AddMouseButtonEvent(0, false) + -- drag item 1's body (frames 10..20) by +5 frames + elseif frame == 8 then io:AddMousePosEvent(frameX(15), rowY(1)) + elseif frame == 9 then io:AddMouseButtonEvent(0, true) + elseif frame == 10 then io:AddMousePosEvent(frameX(20), rowY(1)) + elseif frame == 11 then io:AddMouseButtonEvent(0, false) + -- drag item 2's right edge (end 40, edge at frame 41) by +3 frames + elseif frame == 14 then io:AddMousePosEvent(frameX(41) - 2, rowY(2)) + elseif frame == 15 then io:AddMouseButtonEvent(0, true) + elseif frame == 16 then io:AddMousePosEvent(frameX(44) - 2, rowY(2)) + elseif frame == 17 then io:AddMouseButtonEvent(0, false) + -- click item 1 without moving: selects it + elseif frame == 20 then io:AddMousePosEvent(frameX(18), rowY(1)) + elseif frame == 21 then io:AddMouseButtonEvent(0, true) + elseif frame == 22 then io:AddMouseButtonEvent(0, false) + -- "-" (delete) button of item 1: legend x = 200 - 20 + 2 - 10 = 172 + elseif frame == 25 then io:AddMousePosEvent(origin.x + 172 + 8, origin.y + 20 + 2 + 2 + 8) + elseif frame == 26 then io:AddMouseButtonEvent(0, true) + elseif frame == 27 then io:AddMouseButtonEvent(0, false) + end + ImGui.NewFrame() + ImGui.SetNextWindowPos(ImVec2(0, 0)) + ImGui.SetNextWindowSize(ImVec2(1300, 400)) + ImGui.Begin("Seq", nil, bit32.bor(G.ImGuiWindowFlags.NoMove, G.ImGuiWindowFlags.NoTitleBar)) + origin = ImGui.GetCursorScreenPos() + local r = ImSequencer.Sequencer(empty, 5, true, 2, 0, opts) + assert(r == false) + ret, currentFrame, expanded, selectedEntry, firstFrame = ImSequencer.Sequencer(Seq, currentFrame, expanded, selectedEntry, firstFrame, opts) + ImGui.End() + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) + if frame == 5 then + assert(currentFrame == 50, "scrub: currentFrame " .. currentFrame) + elseif frame == 12 then + assert(Seq.Items[1].s == 15 and Seq.Items[1].e == 25, ("body drag: %d..%d"):format(Seq.Items[1].s, Seq.Items[1].e)) + assert(selectedEntry == 1, "dragging did not select the item") + assert(Seq.Open == 0, "BeginEdit/EndEdit unbalanced") + elseif frame == 18 then + assert(Seq.Items[2].s == 30 and Seq.Items[2].e == 43, ("edge drag: %d..%d"):format(Seq.Items[2].s, Seq.Items[2].e)) + assert(selectedEntry == 2) + elseif frame == 22 then + assert(ret == true and selectedEntry == 1, "click did not select item 1") + elseif frame == 30 then + done = true + assert(#Seq.Items == 1 and Seq.Items[1].s == 30, "delete button did not remove item 1") + assert(selectedEntry == -1, "selection not cleared after delete") + assert(expanded == true and firstFrame == 0) + print("OK") + end +end) diff --git a/tests/t_load.luau b/tests/t_load.luau new file mode 100644 index 0000000..12ff93b --- /dev/null +++ b/tests/t_load.luau @@ -0,0 +1,5 @@ +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImSequencer = require(RS:WaitForChild("ImSequencer"))(G) +assert(type(ImSequencer.Sequencer) == "function" and ImSequencer.SEQUENCER_EDIT_ALL == 10) +print("OK") diff --git a/tools/core b/tools/core new file mode 160000 index 0000000..e2d1cf5 --- /dev/null +++ b/tools/core @@ -0,0 +1 @@ +Subproject commit e2d1cf5cf9469ebb88e3217299de80d079be854e diff --git a/tools/imcurveedit b/tools/imcurveedit new file mode 160000 index 0000000..e9e7884 --- /dev/null +++ b/tools/imcurveedit @@ -0,0 +1 @@ +Subproject commit e9e7884f3fc3ad674391d0ad3efa699ead535876 diff --git a/tools/test.sh b/tools/test.sh new file mode 100755 index 0000000..17be3fb --- /dev/null +++ b/tools/test.sh @@ -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 ImSequencer=ImSequencer.lua --module ImCurveEdit=tools/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 diff --git a/tools/upstream/LICENSE b/tools/upstream/LICENSE new file mode 100644 index 0000000..dcbee45 --- /dev/null +++ b/tools/upstream/LICENSE @@ -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. diff --git a/tools/upstream/UPSTREAM.txt b/tools/upstream/UPSTREAM.txt new file mode 100644 index 0000000..c5c0913 --- /dev/null +++ b/tools/upstream/UPSTREAM.txt @@ -0,0 +1 @@ +https://github.com/CedricGuillemet/ImGuizmo @ 105e4fde45e4e74aae8973d98750820ef7aca7e2 diff --git a/tools/upstream/src/ImSequencer.cpp b/tools/upstream/src/ImSequencer.cpp new file mode 100644 index 0000000..3e181c4 --- /dev/null +++ b/tools/upstream/src/ImSequencer.cpp @@ -0,0 +1,695 @@ +// 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 "ImSequencer.h" +#include "imgui.h" +#include "imgui_internal.h" +#include + +namespace ImSequencer +{ +#ifndef IMGUI_DEFINE_MATH_OPERATORS + static ImVec2 operator+(const ImVec2& a, const ImVec2& b) { + return ImVec2(a.x + b.x, a.y + b.y); + } +#endif + static bool SequencerAddDelButton(ImDrawList* draw_list, ImVec2 pos, bool add = true) + { + ImGuiIO& io = ImGui::GetIO(); + ImRect btnRect(pos, ImVec2(pos.x + 16, pos.y + 16)); + bool overBtn = btnRect.Contains(io.MousePos); + bool containedClick = overBtn && btnRect.Contains(io.MouseClickedPos[0]); + bool clickedBtn = containedClick && io.MouseReleased[0]; + int btnColor = overBtn ? 0xAAEAFFAA : 0x77A3B2AA; + if (containedClick && io.MouseDownDuration[0] > 0) + btnRect.Expand(2.0f); + + float midy = pos.y + 16 / 2 - 0.5f; + float midx = pos.x + 16 / 2 - 0.5f; + draw_list->AddRect(btnRect.Min, btnRect.Max, btnColor, 4); + draw_list->AddLine(ImVec2(btnRect.Min.x + 3, midy), ImVec2(btnRect.Max.x - 3, midy), btnColor, 2); + if (add) + draw_list->AddLine(ImVec2(midx, btnRect.Min.y + 3), ImVec2(midx, btnRect.Max.y - 3), btnColor, 2); + return clickedBtn; + } + + bool Sequencer(SequenceInterface* sequence, int* currentFrame, bool* expanded, int* selectedEntry, int* firstFrame, int sequenceOptions) + { + bool ret = false; + ImGuiIO& io = ImGui::GetIO(); + int cx = (int)(io.MousePos.x); + int cy = (int)(io.MousePos.y); + static float framePixelWidth = 10.f; + static float framePixelWidthTarget = 10.f; + int legendWidth = 200; + + static int movingEntry = -1; + static int movingPos = -1; + static int movingPart = -1; + int delEntry = -1; + int dupEntry = -1; + int ItemHeight = 20; + + bool popupOpened = false; + int sequenceCount = sequence->GetItemCount(); + if (!sequenceCount) + return false; + ImGui::BeginGroup(); + + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + ImVec2 canvas_pos = ImGui::GetCursorScreenPos(); // ImDrawList API uses screen coordinates! + ImVec2 canvas_size = ImGui::GetContentRegionAvail(); // Resize canvas to what's available + int firstFrameUsed = firstFrame ? *firstFrame : 0; + + + int controlHeight = sequenceCount * ItemHeight; + for (int i = 0; i < sequenceCount; i++) + controlHeight += int(sequence->GetCustomHeight(i)); + int frameCount = ImMax(sequence->GetFrameMax() - sequence->GetFrameMin(), 1); + + static bool MovingScrollBar = false; + static bool MovingCurrentFrame = false; + struct CustomDraw + { + int index; + ImRect customRect; + ImRect legendRect; + ImRect clippingRect; + ImRect legendClippingRect; + }; + ImVector customDraws; + ImVector compactCustomDraws; + // zoom in/out + const int visibleFrameCount = (int)floorf((canvas_size.x - legendWidth) / framePixelWidth); + const float barWidthRatio = ImMin(visibleFrameCount / (float)frameCount, 1.f); + const float barWidthInPixels = barWidthRatio * (canvas_size.x - legendWidth); + + ImRect regionRect(canvas_pos, canvas_pos + canvas_size); + + static bool panningView = false; + static ImVec2 panningViewSource; + static int panningViewFrame; + if (ImGui::IsWindowFocused() && io.KeyAlt && io.MouseDown[2]) + { + if (!panningView) + { + panningViewSource = io.MousePos; + panningView = true; + panningViewFrame = *firstFrame; + } + *firstFrame = panningViewFrame - int((io.MousePos.x - panningViewSource.x) / framePixelWidth); + *firstFrame = ImClamp(*firstFrame, sequence->GetFrameMin(), sequence->GetFrameMax() - visibleFrameCount); + } + if (panningView && !io.MouseDown[2]) + { + panningView = false; + } + framePixelWidthTarget = ImClamp(framePixelWidthTarget, 0.1f, 50.f); + + framePixelWidth = ImLerp(framePixelWidth, framePixelWidthTarget, 0.33f); + + frameCount = sequence->GetFrameMax() - sequence->GetFrameMin(); + if (visibleFrameCount >= frameCount && firstFrame) + *firstFrame = sequence->GetFrameMin(); + + + // -- + if (expanded && !*expanded) + { + ImGui::InvisibleButton("canvas", ImVec2(canvas_size.x - canvas_pos.x, (float)ItemHeight)); + draw_list->AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0); + char tmps[512]; + ImFormatString(tmps, IM_ARRAYSIZE(tmps), sequence->GetCollapseFmt(), frameCount, sequenceCount); + draw_list->AddText(ImVec2(canvas_pos.x + 26, canvas_pos.y + 2), 0xFFFFFFFF, tmps); + } + else + { + bool hasScrollBar(true); + /* + int framesPixelWidth = int(frameCount * framePixelWidth); + if ((framesPixelWidth + legendWidth) >= canvas_size.x) + { + hasScrollBar = true; + } + */ + // test scroll area + ImVec2 headerSize(canvas_size.x, (float)ItemHeight); + ImVec2 scrollBarSize(canvas_size.x, 14.f); + ImGui::InvisibleButton("topBar", headerSize); + draw_list->AddRectFilled(canvas_pos, canvas_pos + headerSize, 0xFFFF0000, 0); + ImVec2 childFramePos = ImGui::GetCursorScreenPos(); + ImVec2 childFrameSize(canvas_size.x, canvas_size.y - 8.f - headerSize.y - (hasScrollBar ? scrollBarSize.y : 0)); + ImGui::PushStyleColor(ImGuiCol_FrameBg, 0); + ImGui::BeginChild(889, childFrameSize, ImGuiChildFlags_FrameStyle); + sequence->focused = ImGui::IsWindowFocused(); + ImGui::InvisibleButton("contentBar", ImVec2(canvas_size.x, float(controlHeight))); + const ImVec2 contentMin = ImGui::GetItemRectMin(); + const ImVec2 contentMax = ImGui::GetItemRectMax(); + const ImRect contentRect(contentMin, contentMax); + const float contentHeight = contentMax.y - contentMin.y; + + // full background + draw_list->AddRectFilled(canvas_pos, canvas_pos + canvas_size, 0xFF242424, 0); + + // current frame top + ImRect topRect(ImVec2(canvas_pos.x + legendWidth, canvas_pos.y), ImVec2(canvas_pos.x + canvas_size.x, canvas_pos.y + ItemHeight)); + + if (!MovingCurrentFrame && !MovingScrollBar && movingEntry == -1 && sequenceOptions & SEQUENCER_CHANGE_FRAME && currentFrame && *currentFrame >= 0 && topRect.Contains(io.MousePos) && io.MouseDown[0]) + { + MovingCurrentFrame = true; + } + if (MovingCurrentFrame) + { + if (frameCount) + { + *currentFrame = (int)((io.MousePos.x - topRect.Min.x) / framePixelWidth) + firstFrameUsed; + if (*currentFrame < sequence->GetFrameMin()) + *currentFrame = sequence->GetFrameMin(); + if (*currentFrame >= sequence->GetFrameMax()) + *currentFrame = sequence->GetFrameMax(); + } + if (!io.MouseDown[0]) + MovingCurrentFrame = false; + } + + //header + draw_list->AddRectFilled(canvas_pos, ImVec2(canvas_size.x + canvas_pos.x, canvas_pos.y + ItemHeight), 0xFF3D3837, 0); + if (sequenceOptions & SEQUENCER_ADD) + { + if (SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + legendWidth - ItemHeight, canvas_pos.y + 2), true)) + ImGui::OpenPopup("addEntry"); + + if (ImGui::BeginPopup("addEntry")) + { + for (int i = 0; i < sequence->GetItemTypeCount(); i++) + if (ImGui::Selectable(sequence->GetItemTypeName(i))) + { + sequence->Add(i); + *selectedEntry = sequence->GetItemCount() - 1; + } + + ImGui::EndPopup(); + popupOpened = true; + } + } + + //header frame number and lines + int modFrameCount = 10; + int frameStep = 1; + while ((modFrameCount * framePixelWidth) < 150) + { + modFrameCount *= 2; + frameStep *= 2; + }; + int halfModFrameCount = modFrameCount / 2; + + auto drawLine = [&](int i, int regionHeight) { + bool baseIndex = ((i % modFrameCount) == 0) || (i == sequence->GetFrameMax() || i == sequence->GetFrameMin()); + bool halfIndex = (i % halfModFrameCount) == 0; + int px = (int)canvas_pos.x + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth); + int tiretStart = baseIndex ? 4 : (halfIndex ? 10 : 14); + int tiretEnd = baseIndex ? regionHeight : ItemHeight; + + if (px <= (canvas_size.x + canvas_pos.x) && px >= (canvas_pos.x + legendWidth)) + { + draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)tiretStart), ImVec2((float)px, canvas_pos.y + (float)tiretEnd - 1), 0xFF606060, 1); + + draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)ItemHeight), ImVec2((float)px, canvas_pos.y + (float)regionHeight - 1), 0x30606060, 1); + } + + if (baseIndex && px > (canvas_pos.x + legendWidth)) + { + char tmps[512]; + ImFormatString(tmps, IM_ARRAYSIZE(tmps), "%d", i); + draw_list->AddText(ImVec2((float)px + 3.f, canvas_pos.y), 0xFFBBBBBB, tmps); + } + + }; + + auto drawLineContent = [&](int i, int /*regionHeight*/) { + int px = (int)canvas_pos.x + int(i * framePixelWidth) + legendWidth - int(firstFrameUsed * framePixelWidth); + int tiretStart = int(contentMin.y); + int tiretEnd = int(contentMax.y); + + if (px <= (canvas_size.x + canvas_pos.x) && px >= (canvas_pos.x + legendWidth)) + { + //draw_list->AddLine(ImVec2((float)px, canvas_pos.y + (float)tiretStart), ImVec2((float)px, canvas_pos.y + (float)tiretEnd - 1), 0xFF606060, 1); + + draw_list->AddLine(ImVec2(float(px), float(tiretStart)), ImVec2(float(px), float(tiretEnd)), 0x30606060, 1); + } + }; + for (int i = sequence->GetFrameMin(); i <= sequence->GetFrameMax(); i += frameStep) + { + drawLine(i, ItemHeight); + } + drawLine(sequence->GetFrameMin(), ItemHeight); + drawLine(sequence->GetFrameMax(), ItemHeight); + /* + draw_list->AddLine(canvas_pos, ImVec2(canvas_pos.x, canvas_pos.y + controlHeight), 0xFF000000, 1); + draw_list->AddLine(ImVec2(canvas_pos.x, canvas_pos.y + ItemHeight), ImVec2(canvas_size.x, canvas_pos.y + ItemHeight), 0xFF000000, 1); + */ + // clip content + + draw_list->PushClipRect(childFramePos, childFramePos + childFrameSize, true); + + // draw item names in the legend rect on the left + size_t customHeight = 0; + for (int i = 0; i < sequenceCount; i++) + { + int type; + sequence->Get(i, NULL, NULL, &type, NULL); + ImVec2 tpos(contentMin.x + 3, contentMin.y + i * ItemHeight + 2 + customHeight); + draw_list->AddText(tpos, 0xFFFFFFFF, sequence->GetItemLabel(i)); + + if (sequenceOptions & SEQUENCER_DEL) + { + if (SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight + 2 - 10, tpos.y + 2), false)) + delEntry = i; + + if (SequencerAddDelButton(draw_list, ImVec2(contentMin.x + legendWidth - ItemHeight - ItemHeight + 2 - 10, tpos.y + 2), true)) + dupEntry = i; + } + customHeight += sequence->GetCustomHeight(i); + } + + // slots background + customHeight = 0; + for (int i = 0; i < sequenceCount; i++) + { + unsigned int col = (i & 1) ? 0xFF3A3636 : 0xFF413D3D; + + size_t localCustomHeight = sequence->GetCustomHeight(i); + ImVec2 pos = ImVec2(contentMin.x + legendWidth, contentMin.y + ItemHeight * i + 1 + customHeight); + ImVec2 sz = ImVec2(canvas_size.x + canvas_pos.x, pos.y + ItemHeight - 1 + localCustomHeight); + if (!popupOpened && cy >= pos.y && cy < pos.y + (ItemHeight + localCustomHeight) && movingEntry == -1 && cx>contentMin.x && cx < contentMin.x + canvas_size.x) + { + col += 0x80201008; + pos.x -= legendWidth; + } + draw_list->AddRectFilled(pos, sz, col, 0); + customHeight += localCustomHeight; + } + + draw_list->PushClipRect(childFramePos + ImVec2(float(legendWidth), 0.f), childFramePos + childFrameSize, true); + + // vertical frame lines in content area + for (int i = sequence->GetFrameMin(); i <= sequence->GetFrameMax(); i += frameStep) + { + drawLineContent(i, int(contentHeight)); + } + drawLineContent(sequence->GetFrameMin(), int(contentHeight)); + drawLineContent(sequence->GetFrameMax(), int(contentHeight)); + + // selection + bool selected = selectedEntry && (*selectedEntry >= 0); + if (selected) + { + customHeight = 0; + for (int i = 0; i < *selectedEntry; i++) + customHeight += sequence->GetCustomHeight(i); + draw_list->AddRectFilled(ImVec2(contentMin.x, contentMin.y + ItemHeight * *selectedEntry + customHeight), ImVec2(contentMin.x + canvas_size.x, contentMin.y + ItemHeight * (*selectedEntry + 1) + customHeight), 0x801080FF, 1.f); + } + + // slots + customHeight = 0; + for (int i = 0; i < sequenceCount; i++) + { + int* start, * end; + unsigned int color; + sequence->Get(i, &start, &end, NULL, &color); + size_t localCustomHeight = sequence->GetCustomHeight(i); + + ImVec2 pos = ImVec2(contentMin.x + legendWidth - firstFrameUsed * framePixelWidth, contentMin.y + ItemHeight * i + 1 + customHeight); + ImVec2 slotP1(pos.x + *start * framePixelWidth, pos.y + 2); + ImVec2 slotP2(pos.x + *end * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2); + ImVec2 slotP3(pos.x + *end * framePixelWidth + framePixelWidth, pos.y + ItemHeight - 2 + localCustomHeight); + unsigned int slotColor = color | 0xFF000000; + unsigned int slotColorHalf = (color & 0xFFFFFF) | 0x40000000; + + if (slotP1.x <= (canvas_size.x + contentMin.x) && slotP2.x >= (contentMin.x + legendWidth)) + { + draw_list->AddRectFilled(slotP1, slotP3, slotColorHalf, 2); + draw_list->AddRectFilled(slotP1, slotP2, slotColor, 2); + } + if (ImRect(slotP1, slotP2).Contains(io.MousePos) && io.MouseDoubleClicked[0]) + { + sequence->DoubleClick(i); + } + // Ensure grabbable handles + const float max_handle_width = slotP2.x - slotP1.x / 3.0f; + const float min_handle_width = ImMin(10.0f, max_handle_width); + const float handle_width = ImClamp(framePixelWidth / 2.0f, min_handle_width, max_handle_width); + ImRect rects[3] = { ImRect(slotP1, ImVec2(slotP1.x + handle_width, slotP2.y)) + , ImRect(ImVec2(slotP2.x - handle_width, slotP1.y), slotP2) + , ImRect(slotP1, slotP2) }; + + const unsigned int quadColor[] = { 0xFFFFFFFF, 0xFFFFFFFF, slotColor + (selected ? 0 : 0x202020) }; + if (movingEntry == -1 && (sequenceOptions & SEQUENCER_EDIT_STARTEND))// TODOFOCUS && backgroundRect.Contains(io.MousePos)) + { + for (int j = 2; j >= 0; j--) + { + ImRect& rc = rects[j]; + if (!rc.Contains(io.MousePos)) + continue; + draw_list->AddRectFilled(rc.Min, rc.Max, quadColor[j], 2); + } + + for (int j = 0; j < 3; j++) + { + ImRect& rc = rects[j]; + if (!rc.Contains(io.MousePos)) + continue; + if (!ImRect(childFramePos, childFramePos + childFrameSize).Contains(io.MousePos)) + continue; + if (ImGui::IsMouseClicked(0) && !MovingScrollBar && !MovingCurrentFrame) + { + movingEntry = i; + movingPos = cx; + movingPart = j + 1; + sequence->BeginEdit(movingEntry); + break; + } + } + } + + // custom draw + if (localCustomHeight > 0) + { + ImVec2 rp(canvas_pos.x, contentMin.y + ItemHeight * i + 1 + customHeight); + ImRect customRect(rp + ImVec2(legendWidth - (firstFrameUsed - sequence->GetFrameMin() - 0.5f) * framePixelWidth, float(ItemHeight)), + rp + ImVec2(legendWidth + (sequence->GetFrameMax() - firstFrameUsed - 0.5f + 2.f) * framePixelWidth, float(localCustomHeight + ItemHeight))); + ImRect clippingRect(rp + ImVec2(float(legendWidth), float(ItemHeight)), rp + ImVec2(canvas_size.x, float(localCustomHeight + ItemHeight))); + + ImRect legendRect(rp + ImVec2(0.f, float(ItemHeight)), rp + ImVec2(float(legendWidth), float(localCustomHeight))); + ImRect legendClippingRect(canvas_pos + ImVec2(0.f, float(ItemHeight)), canvas_pos + ImVec2(float(legendWidth), float(localCustomHeight + ItemHeight))); + customDraws.push_back({ i, customRect, legendRect, clippingRect, legendClippingRect }); + } + else + { + ImVec2 rp(canvas_pos.x, contentMin.y + ItemHeight * i + customHeight); + ImRect customRect(rp + ImVec2(legendWidth - (firstFrameUsed - sequence->GetFrameMin() - 0.5f) * framePixelWidth, float(0.f)), + rp + ImVec2(legendWidth + (sequence->GetFrameMax() - firstFrameUsed - 0.5f + 2.f) * framePixelWidth, float(ItemHeight))); + ImRect clippingRect(rp + ImVec2(float(legendWidth), float(0.f)), rp + ImVec2(canvas_size.x, float(ItemHeight))); + + compactCustomDraws.push_back({ i, customRect, ImRect(), clippingRect, ImRect() }); + } + customHeight += localCustomHeight; + } + + + // moving + if (/*backgroundRect.Contains(io.MousePos) && */movingEntry >= 0) + { +#if IMGUI_VERSION_NUM >= 18723 + ImGui::SetNextFrameWantCaptureMouse(true); +#else + ImGui::CaptureMouseFromApp(); +#endif + int diffFrame = int((cx - movingPos) / framePixelWidth); + if (std::abs(diffFrame) > 0) + { + int* start, * end; + sequence->Get(movingEntry, &start, &end, NULL, NULL); + if (selectedEntry) + *selectedEntry = movingEntry; + int& l = *start; + int& r = *end; + if (movingPart & 1) + l += diffFrame; + if (movingPart & 2) + r += diffFrame; + if (l < 0) + { + if (movingPart & 2) + r -= l; + l = 0; + } + if (movingPart & 1 && l > r) + l = r; + if (movingPart & 2 && r < l) + r = l; + movingPos += int(diffFrame * framePixelWidth); + } + if (!io.MouseDown[0]) + { + // single select + if (!diffFrame && movingPart && selectedEntry) + { + *selectedEntry = movingEntry; + ret = true; + } + + movingEntry = -1; + sequence->EndEdit(); + } + } + + // cursor + if (currentFrame && firstFrame && *currentFrame >= *firstFrame && *currentFrame <= sequence->GetFrameMax()) + { + static const float cursorWidth = 8.f; + float cursorOffset = contentMin.x + legendWidth + (*currentFrame - firstFrameUsed) * framePixelWidth + framePixelWidth / 2 - cursorWidth * 0.5f; + draw_list->AddLine(ImVec2(cursorOffset, canvas_pos.y), ImVec2(cursorOffset, contentMax.y), 0xA02A2AFF, cursorWidth); + char tmps[512]; + ImFormatString(tmps, IM_ARRAYSIZE(tmps), "%d", *currentFrame); + draw_list->AddText(ImVec2(cursorOffset + 10, canvas_pos.y + 2), 0xFF2A2AFF, tmps); + } + + draw_list->PopClipRect(); + draw_list->PopClipRect(); + + for (auto& customDraw : customDraws) + sequence->CustomDraw(customDraw.index, draw_list, customDraw.customRect, customDraw.legendRect, customDraw.clippingRect, customDraw.legendClippingRect); + for (auto& customDraw : compactCustomDraws) + sequence->CustomDrawCompact(customDraw.index, draw_list, customDraw.customRect, customDraw.clippingRect); + + // copy paste + if (sequenceOptions & SEQUENCER_COPYPASTE) + { + ImRect rectCopy(ImVec2(contentMin.x + 100, canvas_pos.y + 2) + , ImVec2(contentMin.x + 100 + 30, canvas_pos.y + ItemHeight - 2)); + bool inRectCopy = rectCopy.Contains(io.MousePos); + unsigned int copyColor = inRectCopy ? 0xFF1080FF : 0xFF000000; + draw_list->AddText(rectCopy.Min, copyColor, "Copy"); + + ImRect rectPaste(ImVec2(contentMin.x + 140, canvas_pos.y + 2) + , ImVec2(contentMin.x + 140 + 30, canvas_pos.y + ItemHeight - 2)); + bool inRectPaste = rectPaste.Contains(io.MousePos); + unsigned int pasteColor = inRectPaste ? 0xFF1080FF : 0xFF000000; + draw_list->AddText(rectPaste.Min, pasteColor, "Paste"); + + if (inRectCopy && io.MouseReleased[0]) + { + sequence->Copy(); + } + if (inRectPaste && io.MouseReleased[0]) + { + sequence->Paste(); + } + } + // + + ImGui::EndChild(); + ImGui::PopStyleColor(); + if (hasScrollBar) + { + ImGui::InvisibleButton("scrollBar", scrollBarSize); + ImVec2 scrollBarMin = ImGui::GetItemRectMin(); + ImVec2 scrollBarMax = ImGui::GetItemRectMax(); + + // ratio = number of frames visible in control / number to total frames + + float startFrameOffset = ((float)(firstFrameUsed - sequence->GetFrameMin()) / (float)frameCount) * (canvas_size.x - legendWidth); + ImVec2 scrollBarA(scrollBarMin.x + legendWidth, scrollBarMin.y - 2); + ImVec2 scrollBarB(scrollBarMin.x + canvas_size.x, scrollBarMax.y - 1); + draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF222222, 0); + + ImRect scrollBarRect(scrollBarA, scrollBarB); + bool inScrollBar = scrollBarRect.Contains(io.MousePos); + + draw_list->AddRectFilled(scrollBarA, scrollBarB, 0xFF101010, 8); + + + ImVec2 scrollBarC(scrollBarMin.x + legendWidth + startFrameOffset, scrollBarMin.y); + ImVec2 scrollBarD(scrollBarMin.x + legendWidth + barWidthInPixels + startFrameOffset, scrollBarMax.y - 2); + draw_list->AddRectFilled(scrollBarC, scrollBarD, (inScrollBar || MovingScrollBar) ? 0xFF606060 : 0xFF505050, 6); + + ImRect barHandleLeft(scrollBarC, ImVec2(scrollBarC.x + 14, scrollBarD.y)); + ImRect barHandleRight(ImVec2(scrollBarD.x - 14, scrollBarC.y), scrollBarD); + + bool onLeft = barHandleLeft.Contains(io.MousePos); + bool onRight = barHandleRight.Contains(io.MousePos); + + static bool sizingRBar = false; + static bool sizingLBar = false; + + draw_list->AddRectFilled(barHandleLeft.Min, barHandleLeft.Max, (onLeft || sizingLBar) ? 0xFFAAAAAA : 0xFF666666, 6); + draw_list->AddRectFilled(barHandleRight.Min, barHandleRight.Max, (onRight || sizingRBar) ? 0xFFAAAAAA : 0xFF666666, 6); + + ImRect scrollBarThumb(scrollBarC, scrollBarD); + static const float MinBarWidth = 44.f; + if (sizingRBar) + { + if (!io.MouseDown[0]) + { + sizingRBar = false; + } + else + { + float barNewWidth = ImMax(barWidthInPixels + io.MouseDelta.x, MinBarWidth); + float barRatio = barNewWidth / barWidthInPixels; + framePixelWidthTarget = framePixelWidth = framePixelWidth / barRatio; + int newVisibleFrameCount = int((canvas_size.x - legendWidth) / framePixelWidthTarget); + int lastFrame = *firstFrame + newVisibleFrameCount; + if (lastFrame > sequence->GetFrameMax()) + { + framePixelWidthTarget = framePixelWidth = (canvas_size.x - legendWidth) / float(sequence->GetFrameMax() - *firstFrame); + } + } + } + else if (sizingLBar) + { + if (!io.MouseDown[0]) + { + sizingLBar = false; + } + else + { + if (fabsf(io.MouseDelta.x) > FLT_EPSILON) + { + float barNewWidth = ImMax(barWidthInPixels - io.MouseDelta.x, MinBarWidth); + float barRatio = barNewWidth / barWidthInPixels; + float previousFramePixelWidthTarget = framePixelWidthTarget; + framePixelWidthTarget = framePixelWidth = framePixelWidth / barRatio; + int newVisibleFrameCount = int(visibleFrameCount / barRatio); + int newFirstFrame = *firstFrame + newVisibleFrameCount - visibleFrameCount; + newFirstFrame = ImClamp(newFirstFrame, sequence->GetFrameMin(), ImMax(sequence->GetFrameMax() - visibleFrameCount, sequence->GetFrameMin())); + if (newFirstFrame == *firstFrame) + { + framePixelWidth = framePixelWidthTarget = previousFramePixelWidthTarget; + } + else + { + *firstFrame = newFirstFrame; + } + } + } + } + else + { + if (MovingScrollBar) + { + if (!io.MouseDown[0]) + { + MovingScrollBar = false; + } + else + { + float framesPerPixelInBar = barWidthInPixels / (float)visibleFrameCount; + *firstFrame = int((io.MousePos.x - panningViewSource.x) / framesPerPixelInBar) - panningViewFrame; + *firstFrame = ImClamp(*firstFrame, sequence->GetFrameMin(), ImMax(sequence->GetFrameMax() - visibleFrameCount, sequence->GetFrameMin())); + } + } + else + { + if (scrollBarThumb.Contains(io.MousePos) && ImGui::IsMouseClicked(0) && firstFrame && !MovingCurrentFrame && movingEntry == -1) + { + MovingScrollBar = true; + panningViewSource = io.MousePos; + panningViewFrame = -*firstFrame; + } + if (!sizingRBar && onRight && ImGui::IsMouseClicked(0)) + sizingRBar = true; + if (!sizingLBar && onLeft && ImGui::IsMouseClicked(0)) + sizingLBar = true; + + } + } + } + } + + ImGui::EndGroup(); + + if (regionRect.Contains(io.MousePos)) + { + bool overCustomDraw = false; + for (auto& custom : customDraws) + { + if (custom.customRect.Contains(io.MousePos)) + { + overCustomDraw = true; + } + } + if (overCustomDraw) + { + } + else + { +#if 0 + frameOverCursor = *firstFrame + (int)(visibleFrameCount * ((io.MousePos.x - (float)legendWidth - canvas_pos.x) / (canvas_size.x - legendWidth))); + //frameOverCursor = max(min(*firstFrame - visibleFrameCount / 2, frameCount - visibleFrameCount), 0); + + /**firstFrame -= frameOverCursor; + *firstFrame *= framePixelWidthTarget / framePixelWidth; + *firstFrame += frameOverCursor;*/ + if (io.MouseWheel < -FLT_EPSILON) + { + *firstFrame -= frameOverCursor; + *firstFrame = int(*firstFrame * 1.1f); + framePixelWidthTarget *= 0.9f; + *firstFrame += frameOverCursor; + } + + if (io.MouseWheel > FLT_EPSILON) + { + *firstFrame -= frameOverCursor; + *firstFrame = int(*firstFrame * 0.9f); + framePixelWidthTarget *= 1.1f; + *firstFrame += frameOverCursor; + } +#endif + } + } + + if (expanded) + { + if (SequencerAddDelButton(draw_list, ImVec2(canvas_pos.x + 2, canvas_pos.y + 2), !*expanded)) + *expanded = !*expanded; + } + + if (delEntry != -1) + { + sequence->Del(delEntry); + if (selectedEntry && (*selectedEntry == delEntry || *selectedEntry >= sequence->GetItemCount())) + *selectedEntry = -1; + } + + if (dupEntry != -1) + { + sequence->Duplicate(dupEntry); + } + return ret; + } +} diff --git a/tools/upstream/src/ImSequencer.h b/tools/upstream/src/ImSequencer.h new file mode 100644 index 0000000..914da2f --- /dev/null +++ b/tools/upstream/src/ImSequencer.h @@ -0,0 +1,79 @@ +// 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 + +struct ImDrawList; +struct ImRect; +namespace ImSequencer +{ + enum SEQUENCER_OPTIONS + { + SEQUENCER_EDIT_NONE = 0, + SEQUENCER_EDIT_STARTEND = 1 << 1, + SEQUENCER_CHANGE_FRAME = 1 << 3, + SEQUENCER_ADD = 1 << 4, + SEQUENCER_DEL = 1 << 5, + SEQUENCER_COPYPASTE = 1 << 6, + SEQUENCER_EDIT_ALL = SEQUENCER_EDIT_STARTEND | SEQUENCER_CHANGE_FRAME + }; + + struct SequenceInterface + { + bool focused = false; + virtual int GetFrameMin() const = 0; + virtual int GetFrameMax() const = 0; + virtual int GetItemCount() const = 0; + + virtual void BeginEdit(int /*index*/) {} + virtual void EndEdit() {} + virtual int GetItemTypeCount() const { return 0; } + virtual const char* GetItemTypeName(int /*typeIndex*/) const { return ""; } + virtual const char* GetItemLabel(int /*index*/) const { return ""; } + virtual const char* GetCollapseFmt() const { return "%d Frames / %d entries"; } + + virtual void Get(int index, int** start, int** end, int* type, unsigned int* color) = 0; + virtual void Add(int /*type*/) {} + virtual void Del(int /*index*/) {} + virtual void Duplicate(int /*index*/) {} + + virtual void Copy() {} + virtual void Paste() {} + + virtual size_t GetCustomHeight(int /*index*/) { return 0; } + virtual void DoubleClick(int /*index*/) {} + virtual void CustomDraw(int /*index*/, ImDrawList* /*draw_list*/, const ImRect& /*rc*/, const ImRect& /*legendRect*/, const ImRect& /*clippingRect*/, const ImRect& /*legendClippingRect*/) {} + virtual void CustomDrawCompact(int /*index*/, ImDrawList* /*draw_list*/, const ImRect& /*rc*/, const ImRect& /*clippingRect*/) {} + + virtual ~SequenceInterface() = default; + }; + + + // return true if selection is made + bool Sequencer(SequenceInterface* sequence, int* currentFrame, bool* expanded, int* selectedEntry, int* firstFrame, int sequenceOptions); + +} diff --git a/wally.toml b/wally.toml new file mode 100644 index 0000000..ff8863e --- /dev/null +++ b/wally.toml @@ -0,0 +1,11 @@ +[package] +name = "4dbug/imsequencer-luau" +version = "0.1.0" +registry = "https://github.com/UpliftGames/wally-index" +realm = "shared" +license = "MIT" +description = "Timeline/sequencer (ImSequencer from ImGuizmo) for imgui-luau." +include = ["ImSequencer.lua", "LICENSE", "README.md", "wally.toml", "default.project.json"] + +[peer-dependencies] +ImGui = "4dbug/imgui-luau@^0.4.1"