From e2d1cf5cf9469ebb88e3217299de80d079be854e Mon Sep 17 00:00:00 2001 From: 4DBug <4DBug@github.com> Date: Sat, 17 Oct 2026 07:31:40 -0500 Subject: [PATCH] 0.4.1 - TextProxy works for any widget that sets PlatformImeData.WantTextInput (+ SelectedText) - TextProxy: key-code fallback waits for the TextBox (fixes doubled typing); no stuck Ctrl after paste - Raster: reversed rects (min > max) draw like upstream (fixes ImNodes/ImPlot box select) - test harness: --module NAME=path loads extra ModuleScripts - new tests: t_textproxy_custom, t_rect_flip - extensions/: submodules for imguicolortextedit-luau, imguizmo-luau, implot-luau, implot3d-luau, imnodes-luau --- .github/README.md | 14 +++- .gitmodules | 15 ++++ README.md | 14 +++- backends/roblox/imgui_impl_roblox.lua | 48 +++++++----- build/ImGui.linemap.json | 2 +- build/ImGui.luau | 67 +++++++++++----- build/imgui_love.linemap.json | 2 +- build/imgui_love.lua | 19 ++++- extensions/imguicolortextedit-luau | 1 + extensions/imguizmo-luau | 1 + extensions/imnodes-luau | 1 + extensions/implot-luau | 1 + extensions/implot3d-luau | 1 + imgui.lua | 1 + imgui_draw.lua | 6 ++ imgui_h.lua | 8 +- imgui_widgets.lua | 4 + tools/test/check_all.sh | 2 + tools/test/roblox_mock.luau | 1 + tools/test/run.py | 5 ++ tools/test/scripts/t_rect_flip.luau | 89 ++++++++++++++++++++++ tools/test/scripts/t_textproxy.luau | 2 +- tools/test/scripts/t_textproxy_custom.luau | 31 ++++++++ wally.toml | 2 +- 24 files changed, 291 insertions(+), 46 deletions(-) create mode 100644 .gitmodules create mode 160000 extensions/imguicolortextedit-luau create mode 160000 extensions/imguizmo-luau create mode 160000 extensions/imnodes-luau create mode 160000 extensions/implot-luau create mode 160000 extensions/implot3d-luau create mode 100644 tools/test/scripts/t_rect_flip.luau create mode 100644 tools/test/scripts/t_textproxy_custom.luau diff --git a/.github/README.md b/.github/README.md index ada5af1..cb7f765 100644 --- a/.github/README.md +++ b/.github/README.md @@ -116,7 +116,7 @@ The Style Editor (`ImGui.ShowStyleEditor()`) and the Metrics/Debugger (`ImGui.Sh ### Getting Started & Integration (Roblox) -With [Wally](https://wally.run) ([4dbug/imgui-luau](https://wally.run/package/4dbug/imgui-luau)): add `ImGui = "4dbug/imgui-luau@0.4.0"` under `[dependencies]` in your `wally.toml`, run `wally install`, then `require(ReplicatedStorage.Packages.ImGui)`. Or from source: +With [Wally](https://wally.run) ([4dbug/imgui-luau](https://wally.run/package/4dbug/imgui-luau)): add `ImGui = "4dbug/imgui-luau@0.4.1"` under `[dependencies]` in your `wally.toml`, run `wally install`, then `require(ReplicatedStorage.Packages.ImGui)`. Or from source: 1. Build the module: `python3 tools/bundle.py` writes `build/ImGui.luau` (`--no-demo` leaves the demo out) and `build/ImGuiTyped.luau`, a small typed entry module (Luau types for the public `ImGui.*` API, `ImVec2`/`ImVec4` and the flag enums) that requires `build/ImGui.luau` as its child `Impl`. The Wally package and the Rojo projects use that layout, so you get autocomplete and type errors in `--!strict` scripts. The single-file `ImGui.luau` also works on its own, just untyped. 2. Sync with [Rojo](https://rojo.space): [`default.project.json`](/default.project.json) puts the module in `ReplicatedStorage.ImGui` and the example LocalScript in `StarterPlayerScripts`. @@ -312,6 +312,18 @@ python3 tools/test/run.py --main tools/test/scripts/t_tables.luau --frames 120 - See [docs/PORTING.md](/docs/PORTING.md) for the porting rules. +### Extensions (work in progress) + +Separate repos, included as submodules under `extensions/` (clone with `--recursive`): + +- [imguicolortextedit-luau](https://git.bug.tools/bug/imguicolortextedit-luau): [ImGuiColorTextEdit](https://github.com/goossens/ImGuiColorTextEdit) (goossens fork), a syntax-highlighting text editor. +- [imguizmo-luau](https://git.bug.tools/bug/imguizmo-luau): [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo), 3D gizmos with Roblox Camera/CFrame helpers. +- [implot-luau](https://git.bug.tools/bug/implot-luau): [ImPlot](https://github.com/epezent/implot), 2D plotting. +- [implot3d-luau](https://git.bug.tools/bug/implot3d-luau): [ImPlot3D](https://github.com/brenocq/implot3d), 3D plotting. +- [imnodes-luau](https://git.bug.tools/bug/imnodes-luau): [imnodes](https://github.com/Nelarius/imnodes), a node editor. + +Usage: `local ImPlot = require(ImPlotModule)(require(ImGuiModule))`. + ### Not ported yet Multi-select / box-select, typing-select, the Assets Browser example, the Item Picker and a few smaller items. The full list is in [docs/TODO.txt](/docs/TODO.txt). diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..07bd0db --- /dev/null +++ b/.gitmodules @@ -0,0 +1,15 @@ +[submodule "extensions/imguicolortextedit-luau"] + path = extensions/imguicolortextedit-luau + url = https://git.bug.tools/bug/imguicolortextedit-luau.git +[submodule "extensions/imguizmo-luau"] + path = extensions/imguizmo-luau + url = https://git.bug.tools/bug/imguizmo-luau.git +[submodule "extensions/implot-luau"] + path = extensions/implot-luau + url = https://git.bug.tools/bug/implot-luau.git +[submodule "extensions/imnodes-luau"] + path = extensions/imnodes-luau + url = https://git.bug.tools/bug/imnodes-luau.git +[submodule "extensions/implot3d-luau"] + path = extensions/implot3d-luau + url = https://git.bug.tools/bug/implot3d-luau.git diff --git a/README.md b/README.md index 2bc20ea..954b4f1 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ The Style Editor (`ImGui.ShowStyleEditor()`) and the Metrics/Debugger (`ImGui.Sh ### Getting Started & Integration (Roblox) -With [Wally](https://wally.run) ([4dbug/imgui-luau](https://wally.run/package/4dbug/imgui-luau)): add `ImGui = "4dbug/imgui-luau@0.4.0"` under `[dependencies]` in your `wally.toml`, run `wally install`, then `require(ReplicatedStorage.Packages.ImGui)`. Or from source: +With [Wally](https://wally.run) ([4dbug/imgui-luau](https://wally.run/package/4dbug/imgui-luau)): add `ImGui = "4dbug/imgui-luau@0.4.1"` under `[dependencies]` in your `wally.toml`, run `wally install`, then `require(ReplicatedStorage.Packages.ImGui)`. Or from source: 1. Build the module: `python3 tools/bundle.py` writes `build/ImGui.luau` (`--no-demo` leaves the demo out) and `build/ImGuiTyped.luau`, a small typed entry module (Luau types for the public `ImGui.*` API, `ImVec2`/`ImVec4` and the flag enums) that requires `build/ImGui.luau` as its child `Impl`. The Wally package and the Rojo projects use that layout, so you get autocomplete and type errors in `--!strict` scripts. The single-file `ImGui.luau` also works on its own, just untyped. 2. Sync with [Rojo](https://rojo.space): [`default.project.json`](default.project.json) puts the module in `ReplicatedStorage.ImGui` and the example LocalScript in `StarterPlayerScripts`. @@ -309,6 +309,18 @@ python3 tools/test/run.py --main tools/test/scripts/t_tables.luau --frames 120 - See [docs/PORTING.md](docs/PORTING.md) for the porting rules. +### Extensions (work in progress) + +Separate repos, included as submodules under `extensions/` (clone with `--recursive`): + +- [imguicolortextedit-luau](https://git.bug.tools/bug/imguicolortextedit-luau): [ImGuiColorTextEdit](https://github.com/goossens/ImGuiColorTextEdit) (goossens fork), a syntax-highlighting text editor. +- [imguizmo-luau](https://git.bug.tools/bug/imguizmo-luau): [ImGuizmo](https://github.com/CedricGuillemet/ImGuizmo), 3D gizmos with Roblox Camera/CFrame helpers. +- [implot-luau](https://git.bug.tools/bug/implot-luau): [ImPlot](https://github.com/epezent/implot), 2D plotting. +- [implot3d-luau](https://git.bug.tools/bug/implot3d-luau): [ImPlot3D](https://github.com/brenocq/implot3d), 3D plotting. +- [imnodes-luau](https://git.bug.tools/bug/imnodes-luau): [imnodes](https://github.com/Nelarius/imnodes), a node editor. + +Usage: `local ImPlot = require(ImPlotModule)(require(ImGuiModule))`. + ### Not ported yet Multi-select / box-select, typing-select, the Assets Browser example, the Item Picker and a few smaller items. The full list is in [docs/TODO.txt](docs/TODO.txt). diff --git a/backends/roblox/imgui_impl_roblox.lua b/backends/roblox/imgui_impl_roblox.lua index c856461..eeac370 100644 --- a/backends/roblox/imgui_impl_roblox.lua +++ b/backends/roblox/imgui_impl_roblox.lua @@ -251,9 +251,13 @@ function ImGui_ImplRoblox_SetTextProxy(enabled) BackendData.ProxyLostId = nil return end - BackendData.ProxyLostId = BackendData.ProxyActiveId + BackendData.ProxyLostId = BackendData.ProxyActiveId -- don't recapture for the same field local g = ImGui.GetCurrentContext() - if g and g.ActiveId ~= 0 and g.ActiveId == BackendData.ProxyActiveId then ImGui.ClearActiveID() end + if g and g.ActiveId ~= 0 and g.ActiveId == BackendData.ProxyActiveId then + ImGui.ClearActiveID() + -- the key falls back to the focused window next: the "don't recapture" covers it too + if g.NavWindow then BackendData.ProxyActiveId = g.NavWindow.ID; BackendData.ProxyLostId = g.NavWindow.ID end + end end) end @@ -307,14 +311,18 @@ function ImGui_ImplRoblox_NewFrame() local proxy = BackendData.Proxy local g = ImGui.GetCurrentContext() local focused_box = proxy and UserInputService:GetFocusedTextBox() + -- key: the active item (InputText), else the focused window (custom widgets setting PlatformImeData.WantTextInput) + local proxy_key = g.ActiveId + if proxy_key == 0 and g.NavWindow then proxy_key = g.NavWindow.ID end + if proxy_key ~= BackendData.ProxyActiveId then BackendData.ProxyLostId = nil end -- (a "don't recapture" never outlives its field) if not proxy then - elseif io.WantTextInput and g.ActiveId ~= 0 then - if g.ActiveId ~= BackendData.ProxyActiveId or g.ActiveIdIsJustActivated or g.IO.MouseClicked[0] then - BackendData.ProxyActiveId = g.ActiveId + elseif io.WantTextInput and proxy_key ~= 0 then + if proxy_key ~= BackendData.ProxyActiveId or (g.ActiveId ~= 0 and g.ActiveIdIsJustActivated) or g.IO.MouseClicked[0] then + BackendData.ProxyActiveId = proxy_key BackendData.ProxyLostId = nil end local mouse_up = not (io.MouseDown[0] or io.MouseDown[1] or io.MouseDown[2]) - if focused_box ~= proxy and mouse_up and BackendData.ProxyLostId ~= g.ActiveId + if focused_box ~= proxy and mouse_up and BackendData.ProxyLostId ~= proxy_key and proxy.Parent and (focused_box == nil or not focused_box:IsDescendantOf(game)) then -- what it already holds is not new input (else re-typing it would collapse imgui's selection) BackendData.ProxyText = proxy.Text @@ -352,17 +360,15 @@ function ImGui_ImplRoblox_NewFrame() for _, cp in utf8.codes(text:sub(#prev + 1)) do io:AddInputCharacter(cp) end end end - if ctrl then io:AddKeyEvent(ImGuiMod_Ctrl, true) end + -- re-press only if still physically held: the text can arrive after Ctrl's InputEnded, and re-pressing + -- then left Ctrl stuck down in imgui (typing ignored, shortcuts broken until Ctrl was pressed again) + if ctrl and (UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) or UserInputService:IsKeyDown(Enum.KeyCode.RightControl)) then + io:AddKeyEvent(ImGuiMod_Ctrl, true) + end used_proxy_text = true BackendData.ProxyText = nil end - local st = g.InputTextState - local sel = "" - if st and st.ID ~= 0 and st.ID == g.ActiveId and st.Stb and st:HasSelection() then - local a, b = math.min(st.Stb.select_start, st.Stb.select_end), - math.max(st.Stb.select_start, st.Stb.select_end) - sel = ImGui._ByteArrayToString(st.TextA.Data, a, b) -- stb positions are 1-based in this port - end + local sel = g.PlatformImeData.SelectedText or "" -- (last frame's widget selection: ImGui.NewFrame() not run yet) if sel ~= BackendData.ProxyText then proxy.Text = sel BackendData.ProxyLastRaw = nil @@ -377,9 +383,17 @@ function ImGui_ImplRoblox_NewFrame() BackendData.DeleteKeySeen = false local pending = BackendData.PendingChars - if #pending > 0 then - if not used_proxy_text then for _, c in ipairs(pending) do io:AddInputCharacter(c) end end - table.clear(pending) + if used_proxy_text then + table.clear(pending) -- the proxy delivered the text: the key-code copies would double it + BackendData.PendingAge = 0 + elseif #pending > 0 then + -- the TextBox's Text can update a frame or two after InputBegan: wait before falling back to key codes + BackendData.PendingAge = (BackendData.PendingAge or 0) + 1 + if not ProxyFocused or BackendData.PendingAge > 3 then + for _, c in ipairs(pending) do io:AddInputCharacter(c) end + table.clear(pending) + BackendData.PendingAge = 0 + end end -- Tiles Active while over imgui, so Roblox stops passing clicks/wheel to the game (camera zoom etc.) diff --git a/build/ImGui.linemap.json b/build/ImGui.linemap.json index 35c6118..03ba5c9 100644 --- a/build/ImGui.linemap.json +++ b/build/ImGui.linemap.json @@ -1 +1 @@ -[[46, "imgui.lua"], [22647, "imgui_demo.lua"], [34504, "imgui_draw.lua"], [42828, "imgui_h.lua"], [45827, "roblox/raster.lua"], [46303, "roblox/textures.lua"], [46474, "roblox/layers.lua"], [46811, "roblox/overlays.lua"], [46892, "roblox/render.lua"], [47737, "roblox/parallel.lua"], [48136, "imgui_impl_roblox.lua"], [48623, "imgui_impl_roblox_fonts.lua"], [55026, "imgui_internal.lua"], [58992, "imgui_tables.lua"], [64084, "imgui_widgets.lua"], [76318, "imstb_rectpack.lua"], [76982, "imstb_textedit.lua"], [78524, "imstb_truetype.lua"], [81312, "imstd_minstdio.lua"]] \ No newline at end of file +[[46, "imgui.lua"], [22648, "imgui_demo.lua"], [34505, "imgui_draw.lua"], [42835, "imgui_h.lua"], [45838, "roblox/raster.lua"], [46314, "roblox/textures.lua"], [46485, "roblox/layers.lua"], [46822, "roblox/overlays.lua"], [46903, "roblox/render.lua"], [47748, "roblox/parallel.lua"], [48147, "imgui_impl_roblox.lua"], [48648, "imgui_impl_roblox_fonts.lua"], [55051, "imgui_internal.lua"], [59017, "imgui_tables.lua"], [64109, "imgui_widgets.lua"], [76347, "imstb_rectpack.lua"], [77011, "imstb_textedit.lua"], [78553, "imstb_truetype.lua"], [81341, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/ImGui.luau b/build/ImGui.luau index 3f9ae9e..0465d94 100644 --- a/build/ImGui.luau +++ b/build/ImGui.luau @@ -14418,6 +14418,7 @@ function ImGui.NewFrame() ImGuiPlatformImeData_Copy(g.PlatformImeDataPrev, g.PlatformImeData) g.PlatformImeData.WantVisible = false g.PlatformImeData.WantTextInput = false + g.PlatformImeData.SelectedText = "" -- [Port] -- Mouse wheel scrolling, scale ImGui.UpdateMouseWheel() @@ -40648,6 +40649,10 @@ function MT.ImDrawList:AddRect(p_min, p_max, col, rounding, thickness, flags) return end + -- [Port] reversed corners (min > max, e.g. a box selection dragged up/left): upstream's min_dim checks drop them, older ImGui drew them + if p_min.x > p_max.x or p_min.y > p_max.y then + p_min, p_max = ImVec2(ImMin(p_min.x, p_max.x), ImMin(p_min.y, p_max.y)), ImVec2(ImMax(p_min.x, p_max.x), ImMax(p_min.y, p_max.y)) + end flags = bit32.bor(flags, self.Flags) local stroke_pos = _GetStrokePos(flags, 131072 --[[ImDrawFlags.StrokeInside]]) if stroke_pos == 917504 --[[ImDrawFlags.StrokeLegacy]] then @@ -40865,6 +40870,8 @@ function MT.ImDrawList:AddRectFilled(p_min, p_max, col, rounding, flags) end local ax, ay, bx, by = p_min.x, p_min.y, p_max.x, p_max.y + if ax > bx then ax, bx = bx, ax end -- [Port] reversed corners: see AddRect + if ay > by then ay, by = by, ay end local width = bx - ax local height = by - ay local min_dim = ImMin(width, height) @@ -44265,6 +44272,7 @@ end --- @field InputPos ImVec2 --- @field InputLineHeight float --- @field ViewportId ImGuiID +--- @field SelectedText string --- @return ImGuiPlatformImeData --- @nodiscard @@ -44274,7 +44282,8 @@ function ImGuiPlatformImeData() WantTextInput = false, -- A widget wants text input, not necessarily IME to be visible. This is automatically set to the upcoming value of io.WantTextInput. InputPos = ImVec2(), -- Position of input cursor (for IME). InputLineHeight = 0.0, -- Line height (for IME). - ViewportId = 0 -- ID of platform window/viewport. + ViewportId = 0, -- ID of platform window/viewport. + SelectedText = "" -- [Port] Text currently selected in the widget wanting text input (backends may mirror it, e.g. for native copy). } end @@ -44286,6 +44295,7 @@ function ImGuiPlatformImeData_Copy(dest, src) ImVec2_Copy(dest.InputPos, src.InputPos) dest.InputLineHeight = src.InputLineHeight dest.ViewportId = src.ViewportId + dest.SelectedText = src.SelectedText -- [Port] end --- @param data1 ImGuiPlatformImeData @@ -44295,7 +44305,8 @@ function ImGuiPlatformImeData_Compare(data1, data2) data1.WantTextInput ~= data2.WantTextInput or data1.InputPos ~= data2.InputPos or data1.InputLineHeight ~= data2.InputLineHeight or - data1.ViewportId ~= data2.ViewportId then + data1.ViewportId ~= data2.ViewportId or + data1.SelectedText ~= data2.SelectedText then return false end @@ -48386,9 +48397,13 @@ function ImGui_ImplRoblox_SetTextProxy(enabled) BackendData.ProxyLostId = nil return end - BackendData.ProxyLostId = BackendData.ProxyActiveId + BackendData.ProxyLostId = BackendData.ProxyActiveId -- don't recapture for the same field local g = ImGui.GetCurrentContext() - if g and g.ActiveId ~= 0 and g.ActiveId == BackendData.ProxyActiveId then ImGui.ClearActiveID() end + if g and g.ActiveId ~= 0 and g.ActiveId == BackendData.ProxyActiveId then + ImGui.ClearActiveID() + -- the key falls back to the focused window next: the "don't recapture" covers it too + if g.NavWindow then BackendData.ProxyActiveId = g.NavWindow.ID; BackendData.ProxyLostId = g.NavWindow.ID end + end end) end @@ -48442,14 +48457,18 @@ function ImGui_ImplRoblox_NewFrame() local proxy = BackendData.Proxy local g = ImGui.GetCurrentContext() local focused_box = proxy and UserInputService:GetFocusedTextBox() + -- key: the active item (InputText), else the focused window (custom widgets setting PlatformImeData.WantTextInput) + local proxy_key = g.ActiveId + if proxy_key == 0 and g.NavWindow then proxy_key = g.NavWindow.ID end + if proxy_key ~= BackendData.ProxyActiveId then BackendData.ProxyLostId = nil end -- (a "don't recapture" never outlives its field) if not proxy then - elseif io.WantTextInput and g.ActiveId ~= 0 then - if g.ActiveId ~= BackendData.ProxyActiveId or g.ActiveIdIsJustActivated or g.IO.MouseClicked[0] then - BackendData.ProxyActiveId = g.ActiveId + elseif io.WantTextInput and proxy_key ~= 0 then + if proxy_key ~= BackendData.ProxyActiveId or (g.ActiveId ~= 0 and g.ActiveIdIsJustActivated) or g.IO.MouseClicked[0] then + BackendData.ProxyActiveId = proxy_key BackendData.ProxyLostId = nil end local mouse_up = not (io.MouseDown[0] or io.MouseDown[1] or io.MouseDown[2]) - if focused_box ~= proxy and mouse_up and BackendData.ProxyLostId ~= g.ActiveId + if focused_box ~= proxy and mouse_up and BackendData.ProxyLostId ~= proxy_key and proxy.Parent and (focused_box == nil or not focused_box:IsDescendantOf(game)) then -- what it already holds is not new input (else re-typing it would collapse imgui's selection) BackendData.ProxyText = proxy.Text @@ -48487,17 +48506,15 @@ function ImGui_ImplRoblox_NewFrame() for _, cp in utf8.codes(text:sub(#prev + 1)) do io:AddInputCharacter(cp) end end end - if ctrl then io:AddKeyEvent(4096 --[[ImGuiMod_Ctrl]], true) end + -- re-press only if still physically held: the text can arrive after Ctrl's InputEnded, and re-pressing + -- then left Ctrl stuck down in imgui (typing ignored, shortcuts broken until Ctrl was pressed again) + if ctrl and (UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) or UserInputService:IsKeyDown(Enum.KeyCode.RightControl)) then + io:AddKeyEvent(4096 --[[ImGuiMod_Ctrl]], true) + end used_proxy_text = true BackendData.ProxyText = nil end - local st = g.InputTextState - local sel = "" - if st and st.ID ~= 0 and st.ID == g.ActiveId and st.Stb and st:HasSelection() then - local a, b = math.min(st.Stb.select_start, st.Stb.select_end), - math.max(st.Stb.select_start, st.Stb.select_end) - sel = ImGui._ByteArrayToString(st.TextA.Data, a, b) -- stb positions are 1-based in this port - end + local sel = g.PlatformImeData.SelectedText or "" -- (last frame's widget selection: ImGui.NewFrame() not run yet) if sel ~= BackendData.ProxyText then proxy.Text = sel BackendData.ProxyLastRaw = nil @@ -48512,9 +48529,17 @@ function ImGui_ImplRoblox_NewFrame() BackendData.DeleteKeySeen = false local pending = BackendData.PendingChars - if #pending > 0 then - if not used_proxy_text then for _, c in ipairs(pending) do io:AddInputCharacter(c) end end - table.clear(pending) + if used_proxy_text then + table.clear(pending) -- the proxy delivered the text: the key-code copies would double it + BackendData.PendingAge = 0 + elseif #pending > 0 then + -- the TextBox's Text can update a frame or two after InputBegan: wait before falling back to key codes + BackendData.PendingAge = (BackendData.PendingAge or 0) + 1 + if not ProxyFocused or BackendData.PendingAge > 3 then + for _, c in ipairs(pending) do io:AddInputCharacter(c) end + table.clear(pending) + BackendData.PendingAge = 0 + end end -- Tiles Active while over imgui, so Roblox stops passing clicks/wheel to the game (camera zoom etc.) @@ -70491,6 +70516,10 @@ function ImGui.InputTextEx(label, hint, buf, buf_size, size_arg, flags, callback ImVec2_Copy(ime_data.InputPos, ImVec2(cursor_screen_pos.x - 1.0, cursor_screen_pos.y - g.FontSize)) ime_data.InputLineHeight = g.FontSize ime_data.ViewportId = window.Viewport.ID + -- [Port] selection for backends (stb positions are 1-based in this port) + if state.Stb and state:HasSelection() then + ime_data.SelectedText = ImGui._ByteArrayToString(state.TextA.Data, ImMin(state.Stb.select_start, state.Stb.select_end), ImMax(state.Stb.select_start, state.Stb.select_end)) + end end end diff --git a/build/imgui_love.linemap.json b/build/imgui_love.linemap.json index 8bed3f2..da1731b 100644 --- a/build/imgui_love.linemap.json +++ b/build/imgui_love.linemap.json @@ -1 +1 @@ -[[183, "imgui.lua"], [22784, "imgui_demo.lua"], [34641, "imgui_draw.lua"], [42965, "imgui_h.lua"], [45962, "imgui_impl_love.lua"], [46506, "imgui_internal.lua"], [50472, "imgui_tables.lua"], [55564, "imgui_widgets.lua"], [67798, "imstb_rectpack.lua"], [68462, "imstb_textedit.lua"], [70004, "imstb_truetype.lua"], [72792, "imstd_minstdio.lua"]] \ No newline at end of file +[[183, "imgui.lua"], [22785, "imgui_demo.lua"], [34642, "imgui_draw.lua"], [42972, "imgui_h.lua"], [45973, "imgui_impl_love.lua"], [46517, "imgui_internal.lua"], [50483, "imgui_tables.lua"], [55575, "imgui_widgets.lua"], [67813, "imstb_rectpack.lua"], [68477, "imstb_textedit.lua"], [70019, "imstb_truetype.lua"], [72807, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/imgui_love.lua b/build/imgui_love.lua index cf60f59..4359005 100644 --- a/build/imgui_love.lua +++ b/build/imgui_love.lua @@ -14555,6 +14555,7 @@ function ImGui.NewFrame() ImGuiPlatformImeData_Copy(g.PlatformImeDataPrev, g.PlatformImeData) g.PlatformImeData.WantVisible = false g.PlatformImeData.WantTextInput = false + g.PlatformImeData.SelectedText = "" -- [Port] -- Mouse wheel scrolling, scale ImGui.UpdateMouseWheel() @@ -40785,6 +40786,10 @@ function MT.ImDrawList:AddRect(p_min, p_max, col, rounding, thickness, flags) return end + -- [Port] reversed corners (min > max, e.g. a box selection dragged up/left): upstream's min_dim checks drop them, older ImGui drew them + if p_min.x > p_max.x or p_min.y > p_max.y then + p_min, p_max = ImVec2(ImMin(p_min.x, p_max.x), ImMin(p_min.y, p_max.y)), ImVec2(ImMax(p_min.x, p_max.x), ImMax(p_min.y, p_max.y)) + end flags = bit32.bor(flags, self.Flags) local stroke_pos = _GetStrokePos(flags, 131072 --[[ImDrawFlags.StrokeInside]]) if stroke_pos == 917504 --[[ImDrawFlags.StrokeLegacy]] then @@ -41002,6 +41007,8 @@ function MT.ImDrawList:AddRectFilled(p_min, p_max, col, rounding, flags) end local ax, ay, bx, by = p_min.x, p_min.y, p_max.x, p_max.y + if ax > bx then ax, bx = bx, ax end -- [Port] reversed corners: see AddRect + if ay > by then ay, by = by, ay end local width = bx - ax local height = by - ay local min_dim = ImMin(width, height) @@ -44402,6 +44409,7 @@ end --- @field InputPos ImVec2 --- @field InputLineHeight float --- @field ViewportId ImGuiID +--- @field SelectedText string --- @return ImGuiPlatformImeData --- @nodiscard @@ -44411,7 +44419,8 @@ function ImGuiPlatformImeData() WantTextInput = false, -- A widget wants text input, not necessarily IME to be visible. This is automatically set to the upcoming value of io.WantTextInput. InputPos = ImVec2(), -- Position of input cursor (for IME). InputLineHeight = 0.0, -- Line height (for IME). - ViewportId = 0 -- ID of platform window/viewport. + ViewportId = 0, -- ID of platform window/viewport. + SelectedText = "" -- [Port] Text currently selected in the widget wanting text input (backends may mirror it, e.g. for native copy). } end @@ -44423,6 +44432,7 @@ function ImGuiPlatformImeData_Copy(dest, src) ImVec2_Copy(dest.InputPos, src.InputPos) dest.InputLineHeight = src.InputLineHeight dest.ViewportId = src.ViewportId + dest.SelectedText = src.SelectedText -- [Port] end --- @param data1 ImGuiPlatformImeData @@ -44432,7 +44442,8 @@ function ImGuiPlatformImeData_Compare(data1, data2) data1.WantTextInput ~= data2.WantTextInput or data1.InputPos ~= data2.InputPos or data1.InputLineHeight ~= data2.InputLineHeight or - data1.ViewportId ~= data2.ViewportId then + data1.ViewportId ~= data2.ViewportId or + data1.SelectedText ~= data2.SelectedText then return false end @@ -61971,6 +61982,10 @@ function ImGui.InputTextEx(label, hint, buf, buf_size, size_arg, flags, callback ImVec2_Copy(ime_data.InputPos, ImVec2(cursor_screen_pos.x - 1.0, cursor_screen_pos.y - g.FontSize)) ime_data.InputLineHeight = g.FontSize ime_data.ViewportId = window.Viewport.ID + -- [Port] selection for backends (stb positions are 1-based in this port) + if state.Stb and state:HasSelection() then + ime_data.SelectedText = ImGui._ByteArrayToString(state.TextA.Data, ImMin(state.Stb.select_start, state.Stb.select_end), ImMax(state.Stb.select_start, state.Stb.select_end)) + end end end diff --git a/extensions/imguicolortextedit-luau b/extensions/imguicolortextedit-luau new file mode 160000 index 0000000..79c95c7 --- /dev/null +++ b/extensions/imguicolortextedit-luau @@ -0,0 +1 @@ +Subproject commit 79c95c7ca41ad00f7d131b90faa2c46e2ba7ff28 diff --git a/extensions/imguizmo-luau b/extensions/imguizmo-luau new file mode 160000 index 0000000..7bef75e --- /dev/null +++ b/extensions/imguizmo-luau @@ -0,0 +1 @@ +Subproject commit 7bef75e7cae8570f0a02b0f2b6eeb69a372133fa diff --git a/extensions/imnodes-luau b/extensions/imnodes-luau new file mode 160000 index 0000000..b882d3b --- /dev/null +++ b/extensions/imnodes-luau @@ -0,0 +1 @@ +Subproject commit b882d3b1efcc18b58ce01a8b078c33c39633ef4a diff --git a/extensions/implot-luau b/extensions/implot-luau new file mode 160000 index 0000000..4e8c21f --- /dev/null +++ b/extensions/implot-luau @@ -0,0 +1 @@ +Subproject commit 4e8c21fdf8acf7108fcbf959d69a2a039bd56562 diff --git a/extensions/implot3d-luau b/extensions/implot3d-luau new file mode 160000 index 0000000..8863490 --- /dev/null +++ b/extensions/implot3d-luau @@ -0,0 +1 @@ +Subproject commit 8863490ea7e8d5bf885a78197a435fb7ffd4a68b diff --git a/imgui.lua b/imgui.lua index 418a59e..37fca78 100644 --- a/imgui.lua +++ b/imgui.lua @@ -14373,6 +14373,7 @@ function ImGui.NewFrame() ImGuiPlatformImeData_Copy(g.PlatformImeDataPrev, g.PlatformImeData) g.PlatformImeData.WantVisible = false g.PlatformImeData.WantTextInput = false + g.PlatformImeData.SelectedText = "" -- [Port] -- Mouse wheel scrolling, scale ImGui.UpdateMouseWheel() diff --git a/imgui_draw.lua b/imgui_draw.lua index dd1fa3a..a7c0176 100644 --- a/imgui_draw.lua +++ b/imgui_draw.lua @@ -6145,6 +6145,10 @@ function MT.ImDrawList:AddRect(p_min, p_max, col, rounding, thickness, flags) return end + -- [Port] reversed corners (min > max, e.g. a box selection dragged up/left): upstream's min_dim checks drop them, older ImGui drew them + if p_min.x > p_max.x or p_min.y > p_max.y then + p_min, p_max = ImVec2(ImMin(p_min.x, p_max.x), ImMin(p_min.y, p_max.y)), ImVec2(ImMax(p_min.x, p_max.x), ImMax(p_min.y, p_max.y)) + end flags = bit32.bor(flags, self.Flags) local stroke_pos = _GetStrokePos(flags, ImDrawFlags.StrokeInside) if stroke_pos == ImDrawFlags.StrokeLegacy then @@ -6362,6 +6366,8 @@ function MT.ImDrawList:AddRectFilled(p_min, p_max, col, rounding, flags) end local ax, ay, bx, by = p_min.x, p_min.y, p_max.x, p_max.y + if ax > bx then ax, bx = bx, ax end -- [Port] reversed corners: see AddRect + if ay > by then ay, by = by, ay end local width = bx - ax local height = by - ay local min_dim = ImMin(width, height) diff --git a/imgui_h.lua b/imgui_h.lua index dbd9409..65fe4cc 100644 --- a/imgui_h.lua +++ b/imgui_h.lua @@ -1438,6 +1438,7 @@ end --- @field InputPos ImVec2 --- @field InputLineHeight float --- @field ViewportId ImGuiID +--- @field SelectedText string --- @return ImGuiPlatformImeData --- @nodiscard @@ -1447,7 +1448,8 @@ function ImGuiPlatformImeData() WantTextInput = false, -- A widget wants text input, not necessarily IME to be visible. This is automatically set to the upcoming value of io.WantTextInput. InputPos = ImVec2(), -- Position of input cursor (for IME). InputLineHeight = 0.0, -- Line height (for IME). - ViewportId = 0 -- ID of platform window/viewport. + ViewportId = 0, -- ID of platform window/viewport. + SelectedText = "" -- [Port] Text currently selected in the widget wanting text input (backends may mirror it, e.g. for native copy). } end @@ -1459,6 +1461,7 @@ function ImGuiPlatformImeData_Copy(dest, src) ImVec2_Copy(dest.InputPos, src.InputPos) dest.InputLineHeight = src.InputLineHeight dest.ViewportId = src.ViewportId + dest.SelectedText = src.SelectedText -- [Port] end --- @param data1 ImGuiPlatformImeData @@ -1468,7 +1471,8 @@ function ImGuiPlatformImeData_Compare(data1, data2) data1.WantTextInput ~= data2.WantTextInput or data1.InputPos ~= data2.InputPos or data1.InputLineHeight ~= data2.InputLineHeight or - data1.ViewportId ~= data2.ViewportId then + data1.ViewportId ~= data2.ViewportId or + data1.SelectedText ~= data2.SelectedText then return false end diff --git a/imgui_widgets.lua b/imgui_widgets.lua index c292890..d464e58 100644 --- a/imgui_widgets.lua +++ b/imgui_widgets.lua @@ -6408,6 +6408,10 @@ function ImGui.InputTextEx(label, hint, buf, buf_size, size_arg, flags, callback ImVec2_Copy(ime_data.InputPos, ImVec2(cursor_screen_pos.x - 1.0, cursor_screen_pos.y - g.FontSize)) ime_data.InputLineHeight = g.FontSize ime_data.ViewportId = window.Viewport.ID + -- [Port] selection for backends (stb positions are 1-based in this port) + if state.Stb and state:HasSelection() then + ime_data.SelectedText = ImGui._ByteArrayToString(state.TextA.Data, ImMin(state.Stb.select_start, state.Stb.select_end), ImMax(state.Stb.select_start, state.Stb.select_end)) + end end end diff --git a/tools/test/check_all.sh b/tools/test/check_all.sh index db56fd4..f84a76e 100755 --- a/tools/test/check_all.sh +++ b/tools/test/check_all.sh @@ -31,6 +31,7 @@ CHECKS=( "passthru|PASSTHRU OK|$R --main tools/test/scripts/t_passthru.luau --frames 40" "fonts|FONTS OK|$R --main tools/test/scripts/t_fonts.luau --frames 8" "textproxy|PROXY OK|$R --main tools/test/scripts/t_textproxy.luau --frames 30" +"textproxy_custom|CUSTOM PROXY OK|$R --main tools/test/scripts/t_textproxy_custom.luau --frames 15" "style_editor|SE OK|$R --main tools/test/scripts/t_style_editor.luau --frames 20" "style_leak|STYLE OK|$R --main tools/test/scripts/t_style_leak.luau --frames 60 --open" "apps|APPS OK|$R --main tools/test/scripts/t_apps.luau --frames 120 --open" @@ -38,6 +39,7 @@ CHECKS=( "demo_open|DRAWHASH|$R --main tools/test/scripts/t_drawhash.luau --frames 150 --open" "demo_random|^|python3 tools/test/run.py --roblox --timeout 600 --frames 400 --open" "pixels|PIXELS SAME|a=\$($R --main tools/test/scripts/t_pixels.luau --frames 600 | grep -P '^PIX\t'); b=\$(RUN_FLAGS=ARG_FORCE=true $R --main tools/test/scripts/t_pixels.luau --frames 600 | grep -P '^PIX\t'); [ -n \"\$a\" ] && [ \"\$a\" = \"\$b\" ] && echo PIXELS SAME" +"rect_flip|RECTFLIP OK|$R --main tools/test/scripts/t_rect_flip.luau --frames 30" "tile_bound|TILEBOUND OK|$R --main tools/test/scripts/t_tile_bound.luau --frames 200" "parallel|PARALLEL OK|$R --main tools/test/scripts/t_parallel.luau --frames 500" "pool_ctx|POOLCTX OK|$R --main tools/test/scripts/t_pool_ctx.luau --frames 60" diff --git a/tools/test/roblox_mock.luau b/tools/test/roblox_mock.luau index 9138da4..47258fd 100644 --- a/tools/test/roblox_mock.luau +++ b/tools/test/roblox_mock.luau @@ -192,6 +192,7 @@ ROBLOX = setmetatable({ }, { __index = realG }) local mod = newInst("ModuleScript", "ImGui", ReplicatedStorage); rawset(mod, "_src", "ImGui.luau") +for _, name in EXTRA_MODULES or {} do rawset(newInst("ModuleScript", name, ReplicatedStorage), "_src", name) end -- Run main.lua as a LocalScript local main = loadAsScript("main.client.luau", newInst("LocalScript", "main")) main() diff --git a/tools/test/run.py b/tools/test/run.py index 1866e97..ff4d932 100644 --- a/tools/test/run.py +++ b/tools/test/run.py @@ -24,6 +24,10 @@ def build_run(args, flags): out.append("SOURCE_FNS[%r] = function(%s)\n%s\nend" % (key, G, s)) add("ImGui.luau", os.path.join(TMP, "ImGui.luau")) add("main.client.luau", args.main) + for m in args.module: # [extensions] NAME=path -> ReplicatedStorage.NAME ModuleScript + name, path = m.split("=", 1) + add(name, path) + out.append("EXTRA_MODULES = {%s}" % ",".join(repr(m.split("=", 1)[0]) for m in args.module)) out.append(open(os.path.join(HERE, "roblox_mock.luau")).read()) global OFFSET, LAST_LINE, STRIPPED bundle = open(os.path.join(TMP, "ImGui.luau")).read() @@ -66,6 +70,7 @@ def main(): ap.add_argument("--profile", action="store_true", help="sampling profile (no codegen) -> profile.out; see tools/test/prof.py") ap.add_argument("--open", action="store_true", help="force all CollapsingHeader/TreeNode open") ap.add_argument("--timeout", type=int, default=120) + ap.add_argument("--module", action="append", default=[], help="NAME=path: extra ModuleScript in ReplicatedStorage (extension repos)") ap.add_argument("--quiet", action="store_true", help="no random input (scripted tests drive io themselves)") args = ap.parse_args() diff --git a/tools/test/scripts/t_rect_flip.luau b/tools/test/scripts/t_rect_flip.luau new file mode 100644 index 0000000..993ca73 --- /dev/null +++ b/tools/test/scripts/t_rect_flip.luau @@ -0,0 +1,89 @@ +-- Reversed rects (min > max, e.g. box-select dragged up/left) must draw like upstream's GPU path: +-- AddRectFilled + AddRect from each of the 4 drag directions composite to identical, non-blank pixels. +local G = require(game:GetService("ReplicatedStorage"):WaitForChild("ImGui")) +local ImGui, ImVec2 = G.ImGui, G.ImVec2 +ImGui.CreateContext() +G.ImGui_ImplRoblox.Init() +G.ImGui_ImplRoblox.SetRenderRate(0) +local io = ImGui.GetIO() +local W, H = 1280, 760 +local function composite(gui) + local screen = buffer.create(W * H * 3) + local function walk(inst, ox, oy, kx0, ky0, kx1, ky1) + local kids = inst:GetChildren() + table.sort(kids, function(a, b) return (a.ZIndex or 1) < (b.ZIndex or 1) end) + for _, c in ipairs(kids) do + if c.Visible ~= false then + local x, y = ox + (c.Position and c.Position.X or 0), oy + (c.Position and c.Position.Y or 0) + local cx0, cy0, cx1, cy1 = kx0, ky0, kx1, ky1 + if c.ClipsDescendants then + cx0, cy0 = math.max(cx0, x), math.max(cy0, y) + cx1, cy1 = math.min(cx1, x + c.Size.X), math.min(cy1, y + c.Size.Y) + end + if c.ClassName == "ImageLabel" then + local img = c.ImageContent.Object + local px, sz = img.Pixels, img.Size.X + for ty = 0, sz - 1 do + local sy = y + ty + if sy >= cy0 and sy < cy1 then + for tx = 0, sz - 1 do + local sx = x + tx + if sx >= cx0 and sx < cx1 then + local o = (ty * sz + tx) * 4 + local a = buffer.readu8(px, o + 3) + if a > 0 then + local d = (sy * W + sx) * 3 + local fa = a / 255 + for ch = 0, 2 do + local v = buffer.readu8(px, o + ch) * fa + buffer.readu8(screen, d + ch) * (1 - fa) + buffer.writeu8(screen, d + ch, math.floor(v + 0.5)) + end + end + end + end + end + end + end + walk(c, x, y, cx0, cy0, cx1, cy1) + end + end + end + walk(gui, 0, 0, 0, 0, W, H) + local h = 0 + for i = 0, W * H * 3 - 4, 4 do h = (h * 31 + buffer.readu32(screen, i)) % 2147483647 end + return h +end + +local A, B = ImVec2(200, 150), ImVec2(520, 400) -- drag start/end, down-right +local DIRS = { "none", "dr", "ul", "ur", "dl" } +local CORNERS = { + dr = { A, B }, ul = { B, A }, + ur = { ImVec2(A.x, B.y), ImVec2(B.x, A.y) }, dl = { ImVec2(B.x, A.y), ImVec2(A.x, B.y) }, +} +local di, pf, res = 1, 0, {} +game:GetService("RunService").Stepped:Connect(function() + local d = DIRS[di] + if not d then return end + pf += 1 + G.ImGui_ImplRoblox.NewFrame() + io.DeltaTime = 1 / 60 + ImGui.NewFrame() + local c = CORNERS[d] + if c then + local dl = ImGui.GetForegroundDrawList() + dl:AddRectFilled(c[1], c[2], 0x6496FF32) + dl:AddRect(c[1], c[2], 0xFFFF9664, 0, 2) + dl:AddRectFilled(c[1], c[2], 0xFF00FF00, 12) -- rounded too + end + ImGui.Render() + G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) + if pf == 4 then + res[d] = composite(game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("ImGui")) + print("RECT", d, res[d]) + di += 1; pf = 0 + if not DIRS[di] then + local ok = res.dr ~= res.none and res.ul == res.dr and res.ur == res.dr and res.dl == res.dr + print(ok and "RECTFLIP OK" or "RECTFLIP MISMATCH") + end + end +end) diff --git a/tools/test/scripts/t_textproxy.luau b/tools/test/scripts/t_textproxy.luau index 3d9c53c..b7020b6 100644 --- a/tools/test/scripts/t_textproxy.luau +++ b/tools/test/scripts/t_textproxy.luau @@ -64,7 +64,7 @@ game:GetService("RunService").Stepped:Connect(function() assert(st.TextLen == 0, "backspace should delete imgui's whole selection, len " .. st.TextLen) print("BACKSPACE OK") end - if f == 27 then + if f == 30 then -- (key-code fallback waits a few frames for the TextBox first) local st = ImGui.GetCurrentContext().InputTextState local got = G.ImGui._ByteArrayToString(st.TextA.Data, 1, st.TextLen + 1) assert(got:find("z"), "key fallback while proxy focused: " .. got) diff --git a/tools/test/scripts/t_textproxy_custom.luau b/tools/test/scripts/t_textproxy_custom.luau new file mode 100644 index 0000000..9941464 --- /dev/null +++ b/tools/test/scripts/t_textproxy_custom.luau @@ -0,0 +1,31 @@ +-- Text proxy for a custom widget (no ActiveId): it sets PlatformImeData.WantTextInput/SelectedText while its window is focused. +local G = require(game:GetService("ReplicatedStorage"):WaitForChild("ImGui")) +local ImGui, ImVec2 = G.ImGui, G.ImVec2 +local UIS = game:GetService("UserInputService") +ImGui.CreateContext(); G.ImGui_ImplRoblox.Init(); G.ImGui_ImplRoblox.SetTextProxy(true) +local io = ImGui.GetIO() +local got, proxy, f = "", nil, 0 +game:GetService("RunService").Stepped:Connect(function() + f += 1 + G.ImGui_ImplRoblox.NewFrame() + proxy = proxy or game:GetService("Players").LocalPlayer.PlayerGui.ImGui.TextProxy + if f == 3 then io:AddMousePosEvent(60, 45); io:AddMouseButtonEvent(0, true) end + if f == 4 then io:AddMouseButtonEvent(0, false) end + if f == 8 then assert(UIS:GetFocusedTextBox() == proxy, "proxy focused"); proxy.Text = "x" end + ImGui.NewFrame() + ImGui.SetNextWindowPos(ImVec2(10, 10)); ImGui.SetNextWindowSize(ImVec2(300, 100)) + ImGui.Begin("Editor") + if ImGui.IsWindowFocused() then + local ime = ImGui.GetCurrentContext().PlatformImeData + ime.WantTextInput = true; ime.SelectedText = "abc" + local q = io.InputQueueCharacters + for i = 1, q.Size do got ..= utf8.char(q.Data[i]) end + end + ImGui.End() + ImGui.Render(); G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData()) + if f == 12 then + assert(got == "x", "widget got: " .. got) + assert(proxy.Text == "abc", "mirror: " .. proxy.Text) + print("CUSTOM PROXY OK") + end +end) diff --git a/wally.toml b/wally.toml index 15634fd..e9ce81b 100644 --- a/wally.toml +++ b/wally.toml @@ -1,6 +1,6 @@ [package] name = "4dbug/imgui-luau" -version = "0.4.0" +version = "0.4.1" registry = "https://github.com/UpliftGames/wally-index" realm = "shared" license = "MIT"