Skip to content

Commit

Permalink
Merge branch 'master'
Browse files Browse the repository at this point in the history
Conflicts:
	addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
	addons/imgui-godot/ImGuiGodot/Internal/Input.cs
	addons/imgui-godot/plugin.cfg
	src/MySecondNode.cs
  • Loading branch information
pkdawson committed Sep 20, 2023
2 parents 782946e + e565497 commit 2b72bbe
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 226 deletions.
5 changes: 3 additions & 2 deletions Dear ImGui for Godot Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>DearImGuiforGodotDemo</RootNamespace>
<RootNamespace>DemoProject</RootNamespace>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Remove="doc\**" />
<Compile Remove="gdext\**" />
<EmbeddedResource Remove="doc\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.89.7.1" />
<PackageReference Include="ImGui.NET" Version="1.89.9.1" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Click `Build` in the top right, then run the project.

1. Create a project and, if you haven't already added some C# code, use `Project > Tools > C# > Create C# solution`.

2. [Install the plugin](https://docs.godotengine.org/en/stable/tutorials/plugins/editor/installing_plugins.html) by copying over the `addons` folder. Or [use Chicken](#package-managers).
2. [Install the plugin](https://docs.godotengine.org/en/stable/tutorials/plugins/editor/installing_plugins.html) by copying over the `addons` folder. Or [use GodotEnv](#package-managers).

3. In Visual Studio or another IDE, open the solution and allow unsafe code, and install `ImGui.NET` with NuGet. Save and return to Godot.

Expand Down Expand Up @@ -78,7 +78,7 @@ That's about it. Everything else is provided by ImGui itself, via ImGui.NET.

## Package managers

[Chicken](https://github.com/chickensoft-games/Chicken/) is a dotnet tool that can manage Godot addons with just a little configuration. Use something like:
[GodotEnv](https://github.com/chickensoft-games/GodotEnv/) is a dotnet tool that can manage Godot addons with just a little configuration. Use something like:

```json
{
Expand Down
90 changes: 0 additions & 90 deletions addons/imgui-godot/ImGuiGodot/ImGuiAPI.cs

This file was deleted.

50 changes: 5 additions & 45 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@
using ImGuiGodot.Internal;
using ImGuiNET;
using System;
#if IMGUI_GODOT_DEV
using System.Runtime.InteropServices;
#endif

namespace ImGuiGodot;

public partial class ImGuiLayer : CanvasLayer
{
public static ImGuiLayer Instance { get; private set; }

#if IMGUI_GODOT_DEV
public ImGuiAPI API = new();
#endif
public static ImGuiLayer Instance { get; private set; } = null!;

[Export(PropertyHint.ResourceType, "ImGuiConfig")]
public GodotObject Config = null;
public GodotObject Config = null!;

private Window _window;
private Window _window = null!;
private Rid _subViewportRid;
private Vector2I _subViewportSize = Vector2I.Zero;
private Rid _ci;
Expand All @@ -31,8 +24,8 @@ public partial class ImGuiLayer : CanvasLayer
private sealed partial class ImGuiHelper : Node
{
private uint _counter = 0;
private ImGuiLayer _parent;
private Window _window;
private ImGuiLayer _parent = null!;
private Window _window = null!;

public override void _Ready()
{
Expand Down Expand Up @@ -127,9 +120,6 @@ public override void _ExitTree()
ImGuiGD.Shutdown();
RenderingServer.FreeRid(_ci);
RenderingServer.FreeRid(_subViewportRid);
#if IMGUI_GODOT_DEV
API.Free();
#endif
}

private void OnChangeVisibility()
Expand Down Expand Up @@ -192,36 +182,6 @@ public static void Connect(Action d)
ImGuiGD.Connect(d);
}

#if IMGUI_GODOT_DEV
#pragma warning disable CA1822 // Mark members as static
// WIP, this will probably be changed or moved
public long[] GetImGuiPtrs(string version, int ioSize, int vertSize, int idxSize)
{
if (version != ImGui.GetVersion() ||
ioSize != Marshal.SizeOf<ImGuiIO>() ||
vertSize != Marshal.SizeOf<ImDrawVert>() ||
idxSize != sizeof(ushort))
{
throw new PlatformNotSupportedException($"ImGui version mismatch, use {ImGui.GetVersion()}-docking");
}

IntPtr mem_alloc = IntPtr.Zero;
IntPtr mem_free = IntPtr.Zero;
unsafe
{
void* user_data = null;
ImGui.GetAllocatorFunctions(ref mem_alloc, ref mem_free, ref user_data);
}

return new[] {
(long)ImGui.GetCurrentContext(),
(long)mem_alloc,
(long)mem_free
};
}
#pragma warning restore CA1822 // Mark members as static
#endif

private void CheckContentScale()
{
switch (_window.ContentScaleMode)
Expand Down
10 changes: 5 additions & 5 deletions addons/imgui-godot/ImGuiGodot/Internal/CanvasRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void RenderOne(Rid vprid, ImDrawDataPtr drawData)
ViewportData vd = _vpData[vprid];
Rid parent = vd.RootCanvasItem;

var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(drawData.OwnerViewport.PlatformHandle).Target!;

if (!_canvasItemPools.ContainsKey(parent))
_canvasItemPools[parent] = new();
Expand All @@ -65,9 +65,9 @@ private void RenderOne(Rid vprid, ImDrawDataPtr drawData)

// allocate our CanvasItem pool as needed
int neededNodes = 0;
for (int i = 0; i < drawData.CmdListsCount; ++i)
for (int i = 0; i < drawData.CmdLists.Size; ++i)
{
var cmdBuf = drawData.CmdListsRange[i].CmdBuffer;
var cmdBuf = drawData.CmdLists[i].CmdBuffer;
neededNodes += cmdBuf.Size;
for (int j = 0; j < cmdBuf.Size; ++j)
{
Expand Down Expand Up @@ -96,9 +96,9 @@ private void RenderOne(Rid vprid, ImDrawDataPtr drawData)
drawData.ScaleClipRects(ImGui.GetIO().DisplayFramebufferScale);
int nodeN = 0;

for (int n = 0; n < drawData.CmdListsCount; ++n)
for (int n = 0; n < drawData.CmdLists.Size; ++n)
{
ImDrawListPtr cmdList = drawData.CmdListsRange[n];
ImDrawListPtr cmdList = drawData.CmdLists[n];

int nVert = cmdList.VtxBuffer.Size;

Expand Down
11 changes: 5 additions & 6 deletions addons/imgui-godot/ImGuiGodot/Internal/Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ImGuiGodot.Internal;

internal sealed class Fonts
{
private Texture2D _fontTexture;
private Texture2D? _fontTexture;

/// <summary>
/// Try to calculate how many pixels squared per point. Should be 1 or 2 on non-mobile displays
Expand All @@ -22,7 +22,7 @@ internal sealed class Fonts

private sealed class FontParams
{
public FontFile Font { get; init; }
public FontFile? Font { get; init; }
public int FontSize { get; init; }
public bool Merge { get; init; }
}
Expand All @@ -41,12 +41,12 @@ public void ResetFonts()
_fontConfiguration.Clear();
}

public void AddFont(FontFile fontData, int fontSize, bool merge)
public void AddFont(FontFile? fontData, int fontSize, bool merge)
{
_fontConfiguration.Add(new FontParams { Font = fontData, FontSize = fontSize, Merge = merge });
}

private static unsafe void AddFontToAtlas(FontFile fontData, int fontSize, bool merge)
private static unsafe void AddFontToAtlas(FontFile? fontData, int fontSize, bool merge)
{
ImFontConfig* fc = ImGuiNative.ImFontConfig_ImFontConfig();
if (merge)
Expand Down Expand Up @@ -124,8 +124,7 @@ private static unsafe void ResetStyle()
style.TabRounding = defaultStyle.TabRounding;
style.TabMinWidthForCloseButton = defaultStyle.TabMinWidthForCloseButton;
style.SeparatorTextPadding = defaultStyle.SeparatorTextPadding;
// TODO: 1.89.8
// style.DockingSeparatorSize = defaultStyle.DockingSeparatorSize;
style.DockingSeparatorSize = defaultStyle.DockingSeparatorSize;
style.DisplayWindowPadding = defaultStyle.DisplayWindowPadding;
style.DisplaySafeAreaPadding = defaultStyle.DisplaySafeAreaPadding;
style.MouseCursorScale = defaultStyle.MouseCursorScale;
Expand Down
3 changes: 1 addition & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal sealed class Input
{
internal static float JoyAxisDeadZone { get; set; } = 0.15f;
internal static bool JoyButtonSwapAB { get; set; } = false;
internal SubViewport CurrentSubViewport { get; set; }
internal SubViewport? CurrentSubViewport { get; set; }
internal System.Numerics.Vector2 CurrentSubViewportPos { get; set; }
private Vector2 _mouseWheel = Vector2.Zero;
private ImGuiMouseCursor _currentCursor = ImGuiMouseCursor.None;
Expand Down Expand Up @@ -134,7 +134,6 @@ public bool ProcessInput(InputEvent evt, Window window)
io.AddMouseButtonEvent((int)ImGuiMouseButton.Left, mb.Pressed);
#if GODOT_WINDOWS && !GODOT4_1_OR_GREATER
// if the left mouse button is released, the mouse almost certainly should not be captured
// TODO: remove workaround after Godot 4.2
if (viewportsEnable && !mb.Pressed)
Viewports.MouseCaptureWorkaround();
#endif
Expand Down
12 changes: 6 additions & 6 deletions addons/imgui-godot/ImGuiGodot/Internal/RdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ private void SetupBuffers(ImDrawDataPtr drawData)
byte[] idxBuf = _bufPool.Rent(idxBufSize);
int vertBufSize = drawData.TotalVtxCount * vertSize;
byte[] vertBuf = _bufPool.Rent(vertBufSize);
for (int i = 0; i < drawData.CmdListsCount; ++i)
for (int i = 0; i < drawData.CmdLists.Size; ++i)
{
ImDrawListPtr cmdList = drawData.CmdListsRange[i];
ImDrawListPtr cmdList = drawData.CmdLists[i];

int vertBytes = cmdList.VtxBuffer.Size * vertSize;
Marshal.Copy(cmdList.VtxBuffer.Data, vertBuf, globalVtxOffset, vertBytes);
Expand Down Expand Up @@ -227,9 +227,9 @@ private void SetupBuffers(ImDrawDataPtr drawData)

protected static void ReplaceTextureRids(ImDrawDataPtr drawData)
{
for (int i = 0; i < drawData.CmdListsCount; ++i)
for (int i = 0; i < drawData.CmdLists.Size; ++i)
{
ImDrawListPtr cmdList = drawData.CmdListsRange[i];
ImDrawListPtr cmdList = drawData.CmdLists[i];
for (int cmdi = 0; cmdi < cmdList.CmdBuffer.Size; ++cmdi)
{
ImDrawCmdPtr drawCmd = cmdList.CmdBuffer[cmdi];
Expand Down Expand Up @@ -326,9 +326,9 @@ protected void RenderOne(Rid fb, ImDrawDataPtr drawData)

int globalIdxOffset = 0;
int globalVtxOffset = 0;
for (int i = 0; i < drawData.CmdListsCount; ++i)
for (int i = 0; i < drawData.CmdLists.Size; ++i)
{
ImDrawListPtr cmdList = drawData.CmdListsRange[i];
ImDrawListPtr cmdList = drawData.CmdLists[i];

for (int cmdi = 0; cmdi < cmdList.CmdBuffer.Size; ++cmdi)
{
Expand Down
16 changes: 10 additions & 6 deletions addons/imgui-godot/ImGuiGodot/Internal/RdRendererThreadSafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ internal sealed class ClonedDrawData : IDisposable

public unsafe ClonedDrawData(ImDrawDataPtr inp)
{
// deep swap is difficult because ImGui still owns the draw lists
// TODO: revisit when Godot's threaded renderer is stable

long ddsize = Marshal.SizeOf<ImDrawData>();

// start with a shallow copy
Data = new(ImGui.MemAlloc((uint)ddsize));
Buffer.MemoryCopy(inp.NativePtr, Data.NativePtr, ddsize, ddsize);

// clone the draw data
Data.NativePtr->CmdLists = (ImDrawList**)ImGui.MemAlloc((uint)(Marshal.SizeOf<IntPtr>() * inp.CmdListsCount));
for (int i = 0; i < inp.CmdListsCount; ++i)
int numLists = inp.CmdLists.Size;
IntPtr cmdListPtrs = ImGui.MemAlloc((uint)(Marshal.SizeOf<IntPtr>() * numLists));
Data.NativePtr->CmdLists = new ImVector(numLists, numLists, cmdListPtrs);
for (int i = 0; i < inp.CmdLists.Size; ++i)
{
Data.NativePtr->CmdLists[i] = inp.CmdListsRange[i].CloneOutput().NativePtr;
Data.CmdLists[i] = (IntPtr)inp.CmdLists[i].CloneOutput().NativePtr;
}
}

Expand All @@ -35,9 +40,8 @@ public unsafe void Dispose()

for (int i = 0; i < Data.CmdListsCount; ++i)
{
Data.CmdListsRange[i].Destroy();
Data.CmdLists[i].Destroy();
}
ImGui.MemFree(Data.CmdLists);
Data.Destroy();
Data = new(null);
}
Expand All @@ -63,7 +67,7 @@ internal sealed class RdRendererThreadSafe : RdRenderer, IRenderer
public new string Name => "godot4_net_rd_mt";

private readonly object _sharedDataLock = new();
private SharedList _dataToDraw;
private SharedList? _dataToDraw;

public RdRendererThreadSafe() : base()
{
Expand Down
2 changes: 1 addition & 1 deletion addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class State : IDisposable
internal Fonts Fonts { get; private set; }
internal Input Input { get; private set; }
internal IRenderer Renderer { get; private set; }
internal static State Instance { get; set; }
internal static State Instance { get; set; } = null!;

public State(Window mainWindow, Rid mainSubViewport, IRenderer renderer)
{
Expand Down
Loading

0 comments on commit 2b72bbe

Please sign in to comment.