diff --git a/backends/roblox/imgui_impl_roblox.lua b/backends/roblox/imgui_impl_roblox.lua index c856461..6e7c401 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 @@ -356,13 +364,7 @@ function ImGui_ImplRoblox_NewFrame() 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 diff --git a/build/ImGui.linemap.json b/build/ImGui.linemap.json index 35c6118..f24a439 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"], [42829, "imgui_h.lua"], [45832, "roblox/raster.lua"], [46308, "roblox/textures.lua"], [46479, "roblox/layers.lua"], [46816, "roblox/overlays.lua"], [46897, "roblox/render.lua"], [47742, "roblox/parallel.lua"], [48141, "imgui_impl_roblox.lua"], [48630, "imgui_impl_roblox_fonts.lua"], [55033, "imgui_internal.lua"], [58999, "imgui_tables.lua"], [64091, "imgui_widgets.lua"], [76329, "imstb_rectpack.lua"], [76993, "imstb_textedit.lua"], [78535, "imstb_truetype.lua"], [81323, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/ImGui.luau b/build/ImGui.luau index 3f9ae9e..510a5f8 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() @@ -44265,6 +44266,7 @@ end --- @field InputPos ImVec2 --- @field InputLineHeight float --- @field ViewportId ImGuiID +--- @field SelectedText string --- @return ImGuiPlatformImeData --- @nodiscard @@ -44274,7 +44276,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 +44289,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 +44299,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 +48391,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 +48451,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 @@ -48491,13 +48504,7 @@ function ImGui_ImplRoblox_NewFrame() 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 @@ -70491,6 +70498,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..5889bd4 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"], [42966, "imgui_h.lua"], [45967, "imgui_impl_love.lua"], [46511, "imgui_internal.lua"], [50477, "imgui_tables.lua"], [55569, "imgui_widgets.lua"], [67807, "imstb_rectpack.lua"], [68471, "imstb_textedit.lua"], [70013, "imstb_truetype.lua"], [72801, "imstd_minstdio.lua"]] \ No newline at end of file diff --git a/build/imgui_love.lua b/build/imgui_love.lua index cf60f59..8b97ee2 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() @@ -44402,6 +44403,7 @@ end --- @field InputPos ImVec2 --- @field InputLineHeight float --- @field ViewportId ImGuiID +--- @field SelectedText string --- @return ImGuiPlatformImeData --- @nodiscard @@ -44411,7 +44413,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 +44426,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 +44436,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 +61976,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/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_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..c10539f 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" 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)