Skip to content

Commit

Permalink
Use Godot clipboard (C#)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Sep 3, 2024
1 parent b75940b commit 9eb2b1f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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

0 comments on commit 9eb2b1f

Please sign in to comment.