diff --git a/addons/imgui-godot/ImGuiGodot/Internal/State.cs b/addons/imgui-godot/ImGuiGodot/Internal/State.cs index af39d2a..7b2c602 100644 --- a/addons/imgui-godot/ImGuiGodot/Internal/State.cs +++ b/addons/imgui-godot/ImGuiGodot/Internal/State.cs @@ -17,6 +17,7 @@ private enum RendererType private static readonly IntPtr _backendName = Marshal.StringToCoTaskMemAnsi("godot4_net"); private static IntPtr _rendererName = IntPtr.Zero; + private static nint _clipBuf = 0; private IntPtr _iniFilenameBuffer = IntPtr.Zero; internal Viewports Viewports { get; } @@ -39,6 +40,14 @@ private delegate void PlatformSetImeDataFn( ImGuiPlatformImeDataPtr data); private static readonly PlatformSetImeDataFn _setImeData = SetImeData; + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate void SetClipboardTextFn(nint ud, nint text); + private static readonly SetClipboardTextFn _setClipboardText = SetClipboardText; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + private delegate nint GetClipboardTextFn(nint ud); + private static readonly GetClipboardTextFn _getClipboardText = GetClipboardText; + public State(IRenderer renderer) { Renderer = renderer; @@ -71,6 +80,10 @@ public State(IRenderer renderer) io.NativePtr->BackendPlatformName = (byte*)_backendName; io.NativePtr->BackendRendererName = (byte*)_rendererName; io.NativePtr->PlatformSetImeDataFn = Marshal.GetFunctionPointerForDelegate(_setImeData); + io.NativePtr->SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate( + _setClipboardText); + io.NativePtr->GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate( + _getClipboardText); } Viewports = new Viewports(); @@ -207,5 +220,18 @@ private static void SetImeData(nint ctx, ImGuiViewportPtr vp, ImGuiPlatformImeDa DisplayServer.WindowSetImePosition(pos, windowID); } } + + private static void SetClipboardText(nint ud, nint text) + { + DisplayServer.ClipboardSet(Marshal.PtrToStringUTF8(text)); + } + + private static nint GetClipboardText(nint ud) + { + if (_clipBuf != 0) + Marshal.FreeCoTaskMem(_clipBuf); + _clipBuf = Marshal.StringToCoTaskMemUTF8(DisplayServer.ClipboardGet()); + return _clipBuf; + } } #endif