Skip to content

Commit

Permalink
add Util class
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Dec 2, 2022
1 parent 7d36693 commit bf353e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
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 @@ -147,7 +147,7 @@ public void RenderDrawData(Viewport vp, ImDrawDataPtr drawData)

RID child = children[nodeN++];

RID texrid = State.ConstructRID((ulong)drawCmd.GetTexID());
RID texrid = Util.ConstructRID((ulong)drawCmd.GetTexID());
RenderingServer.CanvasItemClear(child);
Transform2D xform = Transform2D.Identity;
if (drawData.DisplayPos != System.Numerics.Vector2.Zero)
Expand Down
2 changes: 1 addition & 1 deletion addons/imgui-godot/ImGuiGodot/Internal/RdRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private void SetupBuffers(ImDrawDataPtr drawData)
_usedTextures.Add(texid);
if (!_uniformSets.ContainsKey(texid))
{
RID texrid = RenderingServer.TextureGetRdTexture(State.ConstructRID((ulong)texid));
RID texrid = RenderingServer.TextureGetRdTexture(Util.ConstructRID((ulong)texid));
using RDUniform uniform = new()
{
Binding = 0,
Expand Down
20 changes: 0 additions & 20 deletions addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using ImGuiNET;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using CursorShape = Godot.DisplayServer.CursorShape;

Expand Down Expand Up @@ -41,24 +39,6 @@ private class FontParams
}
private static readonly List<FontParams> _fontConfiguration = new();

internal static readonly Func<ulong, RID> ConstructRID;

static State()
{
ConstructorInfo cinfo = typeof(RID).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, new[] { typeof(ulong) });
if (cinfo is null)
{
throw new PlatformNotSupportedException("failed to get RID constructor");
}

DynamicMethod dm = new("ConstructRID", typeof(RID), new[] { typeof(ulong) });
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Newobj, cinfo);
il.Emit(OpCodes.Ret);
ConstructRID = dm.CreateDelegate<Func<ulong, RID>>();
}

public static void AddFont(FontFile fontData, int fontSize, bool merge)
{
_fontConfiguration.Add(new FontParams { Font = fontData, FontSize = fontSize, Merge = merge });
Expand Down
27 changes: 27 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/Util.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Godot;
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace ImGuiGodot.Internal;

internal static class Util
{
public static readonly Func<ulong, RID> ConstructRID;

static Util()
{
ConstructorInfo cinfo = typeof(RID).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, new[] { typeof(ulong) });
if (cinfo is null)
{
throw new PlatformNotSupportedException("failed to get RID constructor");
}

DynamicMethod dm = new("ConstructRID", typeof(RID), new[] { typeof(ulong) });
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Newobj, cinfo);
il.Emit(OpCodes.Ret);
ConstructRID = dm.CreateDelegate<Func<ulong, RID>>();
}
}
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/Viewports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public GodotImGuiWindow(ImGuiViewportPtr vp)
// need to do this after AddChild
GodotWindow.Transparent = true;

Internal.State.AddLayerSubViewport(GodotWindow, out SubViewportContainer svpContainer, out SubViewport svp);
State.AddLayerSubViewport(GodotWindow, out SubViewportContainer svpContainer, out SubViewport svp);
LayerSvp = svp;

Internal.State.Renderer.InitViewport(LayerSvp);
State.Renderer.InitViewport(LayerSvp);
RenderingServer.ViewportSetTransparentBackground(GodotWindow.GetViewportRid(), true);
}

Expand Down

0 comments on commit bf353e7

Please sign in to comment.