diff --git a/Plugins/RynthCore.Plugin.RynthLua/LibraryImGui.cs b/Plugins/RynthCore.Plugin.RynthLua/LibraryImGui.cs index 8b92ffe..4176767 100644 --- a/Plugins/RynthCore.Plugin.RynthLua/LibraryImGui.cs +++ b/Plugins/RynthCore.Plugin.RynthLua/LibraryImGui.cs @@ -469,6 +469,15 @@ public sealed partial class RynthLuaPlugin return LibraryImGuiValues.NewVec2(s, size.X, size.Y); }); + // ── Clipboard (any time; Windows clipboard, Unicode text) ─────── + Fn("SetClipboardText", args => + { + DynValue v = LuaArgs.At(args, 0); + string text = v.IsNil() ? string.Empty : v.ToPrintString(); + return DynValue.NewBoolean(WinClipboard.SetText(text)); + }); + Fn("GetClipboardText", args => DynValue.NewString(WinClipboard.GetText())); + im["ImGui"] = DynValue.NewTable(im); // require("imgui").ImGui works too var mt = new Table(s); diff --git a/Plugins/RynthCore.Plugin.RynthLua/Types/rynthlua.d.lua b/Plugins/RynthCore.Plugin.RynthLua/Types/rynthlua.d.lua index cb45705..656788c 100644 --- a/Plugins/RynthCore.Plugin.RynthLua/Types/rynthlua.d.lua +++ b/Plugins/RynthCore.Plugin.RynthLua/Types/rynthlua.d.lua @@ -2900,6 +2900,15 @@ function ImGui.GetStyle() end ---@return Vector2 function ImGui.CalcTextSize(text, hideTextAfterDoubleHash, wrapWidth) end +---Any time. Puts text on the Windows clipboard; false when another program kept it busy. +---@param text any +---@return boolean ok +function ImGui.SetClipboardText(text) end + +---Any time. The clipboard's text, "" when it holds none. +---@return string +function ImGui.GetClipboardText() end + ------------------------------------------------------------------------------ -- ImGui enums (globals). Plain two-way tables like the other enums: -- ImGuiCond.FirstUseEver == 4 and ImGuiWindowFlags[43] == "NoDecoration". diff --git a/Plugins/RynthCore.Plugin.RynthLua/WinClipboard.cs b/Plugins/RynthCore.Plugin.RynthLua/WinClipboard.cs new file mode 100644 index 0000000..644acae --- /dev/null +++ b/Plugins/RynthCore.Plugin.RynthLua/WinClipboard.cs @@ -0,0 +1,84 @@ +using System; +using System.Runtime.InteropServices; +using System.Threading; + +namespace RynthCore.Plugin.RynthLua; + +/// +/// The Windows clipboard as Unicode text, for ImGui.SetClipboardText / GetClipboardText. Straight +/// Win32 (no engine op): the clipboard belongs to the desktop, not to the hud's render pass, so it +/// works any time. Another program can hold the clipboard open for a moment; opening it is tried +/// a few times, briefly, and false / "" when it stays busy. +/// +internal static class WinClipboard +{ + /// Tests: stand-ins for the real clipboard (the tests mustn't overwrite the user's). + internal static Func? SetOverride; + internal static Func? GetOverride; + + private const uint CF_UNICODETEXT = 13; + private const uint GMEM_MOVEABLE = 0x0002; + private const int OpenTries = 5; + private const int OpenRetryMs = 2; + + internal static bool SetText(string text) + { + if (SetOverride != null) return SetOverride(text); + if (!Open()) return false; + try + { + if (EmptyClipboard() == 0) return false; + int bytes = (text.Length + 1) * 2; + IntPtr mem = GlobalAlloc(GMEM_MOVEABLE, (UIntPtr)bytes); + if (mem == IntPtr.Zero) return false; + IntPtr p = GlobalLock(mem); + if (p == IntPtr.Zero) { GlobalFree(mem); return false; } + try + { + Marshal.Copy(text.ToCharArray(), 0, p, text.Length); + Marshal.WriteInt16(p, text.Length * 2, 0); + } + finally { GlobalUnlock(mem); } + if (SetClipboardData(CF_UNICODETEXT, mem) == IntPtr.Zero) { GlobalFree(mem); return false; } + return true; // the clipboard owns mem now + } + finally { CloseClipboard(); } + } + + internal static string GetText() + { + if (GetOverride != null) return GetOverride(); + if (IsClipboardFormatAvailable(CF_UNICODETEXT) == 0 || !Open()) return string.Empty; + try + { + IntPtr mem = GetClipboardData(CF_UNICODETEXT); + if (mem == IntPtr.Zero) return string.Empty; + IntPtr p = GlobalLock(mem); + if (p == IntPtr.Zero) return string.Empty; + try { return Marshal.PtrToStringUni(p) ?? string.Empty; } + finally { GlobalUnlock(mem); } + } + finally { CloseClipboard(); } + } + + private static bool Open() + { + for (int i = 0; i < OpenTries; i++) + { + if (OpenClipboard(IntPtr.Zero) != 0) return true; + Thread.Sleep(OpenRetryMs); + } + return false; + } + + [DllImport("user32.dll")] private static extern int OpenClipboard(IntPtr hWndNewOwner); + [DllImport("user32.dll")] private static extern int CloseClipboard(); + [DllImport("user32.dll")] private static extern int EmptyClipboard(); + [DllImport("user32.dll")] private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); + [DllImport("user32.dll")] private static extern IntPtr GetClipboardData(uint uFormat); + [DllImport("user32.dll")] private static extern int IsClipboardFormatAvailable(uint format); + [DllImport("kernel32.dll")] private static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes); + [DllImport("kernel32.dll")] private static extern IntPtr GlobalLock(IntPtr hMem); + [DllImport("kernel32.dll")] private static extern int GlobalUnlock(IntPtr hMem); + [DllImport("kernel32.dll")] private static extern IntPtr GlobalFree(IntPtr hMem); +} diff --git a/Tools/RynthLua.ScriptWindowsTests/Program.cs b/Tools/RynthLua.ScriptWindowsTests/Program.cs index 5cac30a..ec33e40 100644 --- a/Tools/RynthLua.ScriptWindowsTests/Program.cs +++ b/Tools/RynthLua.ScriptWindowsTests/Program.cs @@ -111,6 +111,7 @@ namespace RynthLua.ScriptWindowsTests Run("Lua: SetNextWindow* by ImGuiCond", LuaWindowCalls); Run("Lua: errors (unsupported, refused, outside a pass)", LuaErrors); Run("Lua: metrics and CalcTextSize", LuaMetrics); + Run("Lua: clipboard", LuaClipboard); Run("Lua: the design doc's §1.4 examples", LuaDocExamples); Run("Lua: the Examples folder's window scripts", ExampleScripts); Run("Lua: Vector2 / Vector4 / colours", LuaVectors); @@ -1199,6 +1200,27 @@ namespace RynthLua.ScriptWindowsTests Check(t.G("lh").Number == 14, "GetTextLineHeight works outside a pass"); } + private static void LuaClipboard() + { + string clip = ""; + WinClipboard.SetOverride = text => { clip = text; return true; }; + WinClipboard.GetOverride = () => clip; + try + { + LuaHud t = LuaHudWith("ImGui = require('imgui')"); + Check(t.Eval("ImGui.SetClipboardText('Damage Rating: 12')").CastToBool(), "SetClipboardText works outside a pass and returns true"); + Check(clip == "Damage Rating: 12", "the text reaches the clipboard: " + clip); + Check(t.Eval("ImGui.GetClipboardText()").String == "Damage Rating: 12", "GetClipboardText reads it back"); + t.Eval("ImGui:SetClipboardText(0x8000ABCD)"); + Check(clip == "2147527629", "a number is copied as tostring shows it: " + clip); + t.Eval("ImGui.SetClipboardText(nil)"); + Check(clip == "", "nil copies empty text"); + WinClipboard.SetOverride = _ => false; + Check(!t.Eval("ImGui.SetClipboardText('x')").CastToBool(), "a busy clipboard returns false"); + } + finally { WinClipboard.SetOverride = null; WinClipboard.GetOverride = null; } + } + private static unsafe void LuaMetrics() { LuaHud t = LuaHudWith("ImGui = require('imgui')");