commit 5ae035ad42e100cb72f9fdda105bb2c8750cbe52 Author: 4DBug <4DBug@github.com> Date: Sat Oct 17 08:18:21 2026 -0500 ImGradient: port of ImGuizmo's ImGradient (@105e4fd), tests, Roblox example diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f73a6ab --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "tools/core"] + path = tools/core + url = https://git.bug.tools/bug/imgui-luau.git diff --git a/ImGradient.lua b/ImGradient.lua new file mode 100644 index 0000000..9b110b3 --- /dev/null +++ b/ImGradient.lua @@ -0,0 +1,98 @@ +-- ImGradient for imgui-luau. Port of ImGradient.{h,cpp} from https://github.com/CedricGuillemet/ImGuizmo +-- (105e4fde45e4e74aae8973d98750820ef7aca7e2). +-- Usage (same as C++): +-- local ImGradient = require(path.ImGradient)(G) -- G = the table returned by the ImGui module +-- changed, selection = ImGradient.Edit(delegate, size, selection) +-- [Port] Delegate (C++ abstract struct) is any table with these methods: +-- delegate:GetPointCount() -> number +-- delegate:GetPoints() -> { ImVec4 } -- 1-based; .xyz = color, .w = position 0..1 +-- delegate:EditPoint(pointIndex, value) -- pointIndex is 1-based; value is a new ImVec4; returns the (new) index +-- delegate:GetPoint(t) -> ImVec4 -- color at t (used when double-clicking to add a point) +-- delegate:AddPoint(value) +-- selection is 1-based, -1 = none (upstream: 0-based, -1 = none). +-- Like upstream, the selection / dragged point are shared by every gradient editor (C++ function statics), +-- and the widget draws only the points: draw the gradient itself behind it. +-- Plain Lua 5.1 syntax so the same file loads on LÖVE (LuaJIT). +return function(G) + local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 + local ImLerp = G.ImLerp + + local ImGradient = {} + + local function Contains(minx, miny, maxx, maxy, p) -- ImRect::Contains + return p.x >= minx and p.y >= miny and p.x < maxx and p.y < maxy + end + + local function DrawPoint(draw_list, color, size, editing, pos) + local io = ImGui.GetIO() + + local p1 = ImLerp(pos, ImVec2(pos.x + size.x - size.y, pos.y), color.w) + p1 = ImVec2(p1.x + 3, p1.y + 3) + local p2 = ImLerp(ImVec2(pos.x + size.y, pos.y + size.y), ImVec2(pos.x + size.x, pos.y + size.y), color.w) + p2 = ImVec2(p2.x - 3, p2.y - 3) + + draw_list:AddRectFilled(p1, p2, ImGui.ColorConvertFloat4ToU32(ImVec4(color.x, color.y, color.z, 1.0))) + if editing then + draw_list:AddRect(p1, p2, 0xFFFFFFFF, 2.0, 2.5) + else + draw_list:AddRect(p1, p2, 0x80FFFFFF, 2.0, 1.25) + end + + if Contains(p1.x, p1.y, p2.x, p2.y, io.MousePos) then + if io.MouseClicked[0] then + return 2 + end + return 1 + end + return 0 + end + + local currentSelection = -1 + local movingPt = -1 + + function ImGradient.Edit(delegate, size, selection) + local ret = false + local io = ImGui.GetIO() + ImGui.PushStyleVar(G.ImGuiStyleVar.FramePadding, ImVec2(0, 0)) + ImGui.BeginChild(137, size, G.ImGuiChildFlags.FrameStyle) + + local draw_list = ImGui.GetWindowDrawList() + local offset = ImGui.GetCursorScreenPos() + + local pts = delegate:GetPoints() + if currentSelection > delegate:GetPointCount() then + currentSelection = -1 + end + if movingPt ~= -1 then + local p = pts[movingPt] + local w = p.w + io.MouseDelta.x / size.x + w = math.min(math.max(w, 0.0), 1.0) + delegate:EditPoint(movingPt, ImVec4(p.x, p.y, p.z, w)) + ret = true + if not io.MouseDown[0] then + movingPt = -1 + end + end + for i = 1, delegate:GetPointCount() do + local ptSel = DrawPoint(draw_list, pts[i], size, i == currentSelection, offset) + if ptSel == 2 then + currentSelection = i + ret = true + end + if ptSel == 1 and io.MouseDown[0] and movingPt == -1 then + movingPt = i + end + end + if Contains(offset.x, offset.y, offset.x + size.x, offset.y + size.y, io.MousePos) and io.MouseDoubleClicked[0] then + local t = (io.MousePos.x - offset.x) / size.x + delegate:AddPoint(delegate:GetPoint(t)) + ret = true + end + ImGui.EndChild() + ImGui.PopStyleVar() + + return ret, currentSelection + end + + return ImGradient +end 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..3d7a6d5 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# imgradient-luau + +Gradient editor widget for imgui-luau: drag color points along a bar, click to select, double-click to add. + +Luau port of ImGradient 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/imgradient-luau) / [GitHub](https://github.com/4DBug/imgradient-luau). + +## Usage + +```lua +local G = require(ReplicatedStorage.ImGui) +local ImGradient = require(ReplicatedStorage.ImGradient)(G) + +-- Delegate (C++ ImGradient::Delegate): any table with these methods +local Gradient = { Points = { G.ImVec4(1, 0, 0, 0), G.ImVec4(0, 0, 1, 1) } } -- rgb + position (w) +function Gradient:GetPointCount() return #self.Points end +function Gradient:GetPoints() return self.Points end -- 1-based array of ImVec4 +function Gradient:EditPoint(i, v) self.Points[i] = v; return i end +function Gradient:GetPoint(t) ... end -- color at t, for double-click insertion +function Gradient:AddPoint(v) table.insert(self.Points, v) end + +-- each frame, inside a window: +local changed +changed, selection = ImGradient.Edit(Gradient, G.ImVec2(400, 24), selection) +``` + +`[Port]` differences: point indices and `selection` are 1-based (`-1` = none); `Edit` returns `changed, selection` +instead of writing `int& selection`. As upstream, the widget draws only the points (draw the gradient behind it), and +selection/drag state is shared by all gradient editors (C++ function statics). + +Full example (gradient drawn behind the widget, color editing, conversion to a Roblox `ColorSequence`): +`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/imgradient-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). diff --git a/default.project.json b/default.project.json new file mode 100644 index 0000000..5813aa0 --- /dev/null +++ b/default.project.json @@ -0,0 +1,11 @@ +{ + "name": "imgradient-luau", + "tree": { + "$className": "DataModel", + "ReplicatedStorage": { + "ImGui": { "$path": "tools/core/build/ImGuiTyped.luau", "Impl": { "$path": "tools/core/build/ImGui.luau" } }, + "ImGradient": { "$path": "ImGradient.lua" } + }, + "StarterPlayer": { "StarterPlayerScripts": { "ImGradientExample": { "$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..b246fba --- /dev/null +++ b/examples/roblox/main.client.luau @@ -0,0 +1,81 @@ +-- ImGradient: edit a gradient (drag points, click to select, double-click to add), drawn behind the widget, +-- and converted to a Roblox ColorSequence. +local RS = game:GetService("ReplicatedStorage") +local RunService = game:GetService("RunService") +local G = require(RS:WaitForChild("ImGui")) +local ImGradient = require(RS:WaitForChild("ImGradient"))(G) +local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 + +-- Delegate: points are ImVec4(r, g, b, position) +local Gradient = { + Points = { ImVec4(1, 0.2, 0.2, 0), ImVec4(1, 0.9, 0.2, 0.4), ImVec4(0.2, 0.4, 1, 1) }, +} +function Gradient:GetPointCount() return #self.Points end +function Gradient:GetPoints() return self.Points end +function Gradient:EditPoint(i, v) self.Points[i] = v; return i end +function Gradient:AddPoint(v) table.insert(self.Points, v) end +function Gradient:Sorted() + local s = table.clone(self.Points) + table.sort(s, function(a, b) return a.w < b.w end) + return s +end +function Gradient:GetPoint(t) + local s = self:Sorted() + if t <= s[1].w then return ImVec4(s[1].x, s[1].y, s[1].z, t) end + for i = 2, #s do + local a, b = s[i - 1], s[i] + if t <= b.w then + local k = (b.w > a.w) and (t - a.w) / (b.w - a.w) or 0 + return ImVec4(a.x + (b.x - a.x) * k, a.y + (b.y - a.y) * k, a.z + (b.z - a.z) * k, t) + end + end + local l = s[#s] + return ImVec4(l.x, l.y, l.z, t) +end +-- Roblox ColorSequence (needs keypoints at 0 and 1, at most 20) +function Gradient:ToColorSequence() + local kps, s = {}, self:Sorted() + local function add(t, c) table.insert(kps, ColorSequenceKeypoint.new(t, Color3.new(c.x, c.y, c.z))) end + add(0, self:GetPoint(0)) + for i = 1, math.min(#s, 18) do if s[i].w > 0 and s[i].w < 1 then add(s[i].w, s[i]) end end + add(1, self:GetPoint(1)) + return ColorSequence.new(kps) +end + +local selection = -1 +local Size = ImVec2(400, 24) + +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() -- defaults to LocalPlayer.PlayerGui +RunService.RenderStepped:Connect(function() + G.ImGui_ImplRoblox.NewFrame() + ImGui.NewFrame() + ImGui.Begin("ImGradient") + -- the widget only draws the points: draw the gradient behind it + local pos = ImGui.GetCursorScreenPos() + local dl, s = ImGui.GetWindowDrawList(), Gradient:Sorted() + local x0 = pos.x + for i = 0, #s do + local a, b = s[math.max(i, 1)], s[math.min(i + 1, #s)] + local x1 = pos.x + Size.x * (i < #s and b.w or 1) + local ca = ImGui.ColorConvertFloat4ToU32(ImVec4(a.x, a.y, a.z, 1)) + local cb = ImGui.ColorConvertFloat4ToU32(ImVec4(b.x, b.y, b.z, 1)) + if x1 > x0 then dl:AddRectFilledMultiColor(ImVec2(x0, pos.y), ImVec2(x1, pos.y + Size.y), ca, cb, cb, ca) end + x0 = x1 + end + local changed + changed, selection = ImGradient.Edit(Gradient, Size, selection) + ImGui.Text("points: %d selection: %d", #Gradient.Points, selection) + local p = Gradient.Points[selection] + if p then + local col = ImVec4(p.x, p.y, p.z, 1) + if ImGui.ColorEdit3("color", col) then Gradient:EditPoint(selection, ImVec4(col.x, col.y, col.z, p.w)) end + if ImGui.Button("Delete point") and #Gradient.Points > 2 then table.remove(Gradient.Points, selection); selection = -1 end + end + if changed and ColorSequence then + Gradient.ColorSequence = Gradient:ToColorSequence() -- e.g. assign to a UIGradient/Beam/ParticleEmitter .Color + 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..96eaafd --- /dev/null +++ b/tests/t_edit.luau @@ -0,0 +1,58 @@ +-- Click selects a point, dragging moves its position (w), double-click on empty space adds a point. +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImGradient = require(RS:WaitForChild("ImGradient"))(G) +local ImGui, ImVec2, ImVec4 = G.ImGui, G.ImVec2, G.ImVec4 + +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() +local io = ImGui.GetIO() + +local D = { Points = { ImVec4(1, 0, 0, 0), ImVec4(0, 0, 1, 0.5) }, Added = nil } +function D:GetPointCount() return #self.Points end +function D:GetPoints() return self.Points end +function D:EditPoint(i, v) self.Points[i] = v; return i end +function D:GetPoint(t) return ImVec4(0, 1, 0, t) end +function D:AddPoint(v) self.Added = v; table.insert(self.Points, v) end + +local Size = ImVec2(400, 20) +local frame, done, origin = 0, false, nil +local selection, changed = -1, false +local function pt(w) -- center of the point square at position w + return ImVec2(origin.x + (Size.x - Size.y) * w + Size.y / 2, origin.y + Size.y / 2) +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 local p = pt(0.5); io:AddMousePosEvent(p.x, p.y) + elseif frame == 4 then io:AddMouseButtonEvent(0, true) + elseif frame >= 6 and frame <= 9 then local p = pt(0.5); io:AddMousePosEvent(p.x + (frame - 5) * 8, p.y) -- frame 5: held still (picks the point) + elseif frame == 10 then io:AddMouseButtonEvent(0, false) + elseif frame == 14 then io:AddMousePosEvent(origin.x + Size.x * 0.9, origin.y + 5) + elseif frame == 15 or frame == 17 then io:AddMouseButtonEvent(0, true) + elseif frame == 16 or frame == 18 then io:AddMouseButtonEvent(0, false) + end + ImGui.NewFrame() + ImGui.SetNextWindowPos(ImVec2(0, 0)) + ImGui.SetNextWindowSize(ImVec2(500, 200)) + ImGui.Begin("Gradient") + origin = ImGui.GetCursorScreenPos() + changed, selection = ImGradient.Edit(D, Size, selection) + ImGui.End() + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) + if frame == 4 then + assert(selection == 2, "click did not select point 2: " .. selection) + elseif frame == 12 then + local w = D.Points[2].w + assert(w > 0.55 and w <= 1, "drag did not move point 2: " .. w) + assert(D.Points[1].w == 0, "point 1 moved") + elseif frame == 20 then + done = true + assert(D.Added and math.abs(D.Added.w - 0.9) < 0.01, "double-click did not add a point at 0.9") + assert(#D.Points == 3) + print("OK") + end +end) diff --git a/tests/t_load.luau b/tests/t_load.luau new file mode 100644 index 0000000..859d177 --- /dev/null +++ b/tests/t_load.luau @@ -0,0 +1,5 @@ +local RS = game:GetService("ReplicatedStorage") +local G = require(RS:WaitForChild("ImGui")) +local ImGradient = require(RS:WaitForChild("ImGradient"))(G) +assert(type(ImGradient.Edit) == "function") +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/test.sh b/tools/test.sh new file mode 100755 index 0000000..98c28aa --- /dev/null +++ b/tools/test.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# Runs every tests/t_*.luau against the core (tools/core submodule) in the Roblox mock, the Roblox example. +# and (if nix-shell is available) the LÖVE checks under LuaJIT. +cd "$(dirname "$0")/.." +fail=0 +run() { python3 tools/core/tools/test/run.py --module ImGradient=ImGradient.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/ImGradient.cpp b/tools/upstream/src/ImGradient.cpp new file mode 100644 index 0000000..f06a111 --- /dev/null +++ b/tools/upstream/src/ImGradient.cpp @@ -0,0 +1,123 @@ +// 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 "ImGradient.h" +#include "imgui.h" +#include "imgui_internal.h" + +namespace ImGradient +{ +#ifndef IMGUI_DEFINE_MATH_OPERATORS + static inline ImVec2 operator*(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x * rhs, lhs.y * rhs); } + static inline ImVec2 operator/(const ImVec2& lhs, const float rhs) { return ImVec2(lhs.x / rhs, lhs.y / rhs); } + static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); } + static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); } + static inline ImVec2 operator*(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x * rhs.x, lhs.y * rhs.y); } + static inline ImVec2 operator/(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x / rhs.x, lhs.y / rhs.y); } +#endif + + static int DrawPoint(ImDrawList* draw_list, ImVec4 color, const ImVec2 size, bool editing, ImVec2 pos) + { + ImGuiIO& io = ImGui::GetIO(); + + ImVec2 p1 = ImLerp(pos, ImVec2(pos + ImVec2(size.x - size.y, 0.f)), color.w) + ImVec2(3, 3); + ImVec2 p2 = ImLerp(pos + ImVec2(size.y, size.y), ImVec2(pos + size), color.w) - ImVec2(3, 3); + ImRect rc(p1, p2); + + color.w = 1.f; + draw_list->AddRectFilled(p1, p2, ImColor(color)); +#if IMGUI_VERSION_NUM < 19276 + if (editing) + draw_list->AddRect(p1, p2, 0xFFFFFFFF, 2.f, 15, 2.5f); + else + draw_list->AddRect(p1, p2, 0x80FFFFFF, 2.f, 15, 1.25f); +#else + if (editing) + draw_list->AddRect(p1, p2, 0xFFFFFFFF, 2.f, 2.5f); + else + draw_list->AddRect(p1, p2, 0x80FFFFFF, 2.f, 1.25f); +#endif + + if (rc.Contains(io.MousePos)) + { + if (io.MouseClicked[0]) + return 2; + return 1; + } + return 0; + } + + bool Edit(Delegate& delegate, const ImVec2& size, int& selection) + { + bool ret = false; + ImGuiIO& io = ImGui::GetIO(); + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0)); + ImGui::BeginChild(137, size, ImGuiChildFlags_FrameStyle); + + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + const ImVec2 offset = ImGui::GetCursorScreenPos(); + + const ImVec4* pts = delegate.GetPoints(); + static int currentSelection = -1; + static int movingPt = -1; + if (currentSelection >= int(delegate.GetPointCount())) + currentSelection = -1; + if (movingPt != -1) + { + ImVec4 current = pts[movingPt]; + current.w += io.MouseDelta.x / size.x; + current.w = ImClamp(current.w, 0.f, 1.f); + delegate.EditPoint(movingPt, current); + ret = true; + if (!io.MouseDown[0]) + movingPt = -1; + } + for (size_t i = 0; i < delegate.GetPointCount(); i++) + { + int ptSel = DrawPoint(draw_list, pts[i], size, i == currentSelection, offset); + if (ptSel == 2) + { + currentSelection = int(i); + ret = true; + } + if (ptSel == 1 && io.MouseDown[0] && movingPt == -1) + { + movingPt = int(i); + } + } + ImRect rc(offset, offset + size); + if (rc.Contains(io.MousePos) && io.MouseDoubleClicked[0]) + { + float t = (io.MousePos.x - offset.x) / size.x; + delegate.AddPoint(delegate.GetPoint(t)); + ret = true; + } + ImGui::EndChild(); + ImGui::PopStyleVar(); + + selection = currentSelection; + return ret; + } +} diff --git a/tools/upstream/src/ImGradient.h b/tools/upstream/src/ImGradient.h new file mode 100644 index 0000000..1f1c0f1 --- /dev/null +++ b/tools/upstream/src/ImGradient.h @@ -0,0 +1,45 @@ +// 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 ImVec4; +struct ImVec2; + +namespace ImGradient +{ + struct Delegate + { + virtual size_t GetPointCount() = 0; + virtual ImVec4* GetPoints() = 0; + virtual int EditPoint(int pointIndex, ImVec4 value) = 0; + virtual ImVec4 GetPoint(float t) = 0; + virtual void AddPoint(ImVec4 value) = 0; + virtual ~Delegate() = default; + }; + + bool Edit(Delegate& delegate, const ImVec2& size, int& selection); +} diff --git a/wally.toml b/wally.toml new file mode 100644 index 0000000..93e865d --- /dev/null +++ b/wally.toml @@ -0,0 +1,11 @@ +[package] +name = "4dbug/imgradient-luau" +version = "0.1.0" +registry = "https://github.com/UpliftGames/wally-index" +realm = "shared" +license = "MIT" +description = "Gradient editor (ImGradient from ImGuizmo) for imgui-luau." +include = ["ImGradient.lua", "LICENSE", "README.md", "wally.toml", "default.project.json"] + +[peer-dependencies] +ImGui = "4dbug/imgui-luau@^0.4.1"