Skip to content

Commit

Permalink
fix nullable warnings, some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Sep 18, 2023
1 parent 05a6b99 commit ea9ecdc
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 209 deletions.
3 changes: 2 additions & 1 deletion Dear ImGui for Godot Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<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\**" />
Expand Down
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.

53 changes: 6 additions & 47 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,35 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
#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!;

/// <summary>
/// Do NOT connect to this directly, please use <see cref="Connect"/> instead
/// </summary>
[Signal] public delegate void ImGuiLayoutEventHandler();

private Window _window;
private Window _window = null!;
private Rid _subViewportRid;
private Vector2I _subViewportSize = Vector2I.Zero;
private Rid _ci;
private Transform2D _finalTransform = Transform2D.Identity;
private UpdateFirst _updateFirst;
private UpdateFirst _updateFirst = null!;
private static readonly HashSet<GodotObject> _connectedObjects = new();
private int sizeCheck = 0;
private bool _headless = false;

private sealed partial class UpdateFirst : 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 @@ -131,9 +123,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 @@ -212,36 +201,6 @@ public static void Connect(ImGuiLayoutEventHandler 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 static void OnNodeRemoved(Node node)
{
// signals declared in C# don't (yet?) work like normal Godot signals,
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
8 changes: 4 additions & 4 deletions addons/imgui-godot/ImGuiGodot/Internal/Fonts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace ImGuiGodot.Internal;

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

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 @@ -31,12 +31,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
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 @@ -7,7 +7,7 @@ namespace ImGuiGodot.Internal;

internal sealed class Input
{
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 @@ -132,7 +132,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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal sealed class RdRendererThreadSafe : RdRenderer, IRenderer
public new string Name => "imgui_impl_godot4_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 @@ -26,7 +26,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
20 changes: 10 additions & 10 deletions addons/imgui-godot/ImGuiGodot/Internal/Viewports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,63 +288,63 @@ private static void Godot_DestroyWindow(ImGuiViewportPtr vp)
{
if (vp.PlatformHandle != IntPtr.Zero)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.Dispose();
vp.PlatformHandle = IntPtr.Zero;
}
}

private static void Godot_ShowWindow(ImGuiViewportPtr vp)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.ShowWindow();
}

private static void Godot_SetWindowPos(ImGuiViewportPtr vp, Vector2 pos)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.SetWindowPos(pos.ToVector2I());
}

private static void Godot_GetWindowPos(ImGuiViewportPtr vp, out Vector2 pos)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
pos = window.GetWindowPos().ToImVec2();
}

private static void Godot_SetWindowSize(ImGuiViewportPtr vp, Vector2 size)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.SetWindowSize(size.ToVector2I());
}

private static void Godot_GetWindowSize(ImGuiViewportPtr vp, out Vector2 size)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
size = window.GetWindowSize().ToImVec2();
}

private static void Godot_SetWindowFocus(ImGuiViewportPtr vp)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.SetWindowFocus();
}

private static bool Godot_GetWindowFocus(ImGuiViewportPtr vp)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
return window.GetWindowFocus();
}

private static bool Godot_GetWindowMinimized(ImGuiViewportPtr vp)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
return window.GetWindowMinimized();
}

private static void Godot_SetWindowTitle(ImGuiViewportPtr vp, string title)
{
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformHandle).Target!;
window.SetWindowTitle(title);
}
}
2 changes: 1 addition & 1 deletion src/MyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DemoProject;

public partial class MyNode : Node
{
private Window _window;
private Window _window = null!;

public override void _Ready()
{
Expand Down
Loading

0 comments on commit ea9ecdc

Please sign in to comment.