Skip to content

Commit

Permalink
Make space for native backend
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jan 19, 2024
1 parent 0270140 commit b1c9de7
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 27 deletions.
34 changes: 25 additions & 9 deletions addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,59 @@ namespace ImGuiGodot;

public static class ImGuiGD
{
private static readonly Internal.IBackend _backend;

/// <summary>
/// Deadzone for all axes
/// </summary>
public static float JoyAxisDeadZone { get; set; } = 0.15f;
public static float JoyAxisDeadZone
{
get => _backend.JoyAxisDeadZone;
set => _backend.JoyAxisDeadZone = value;
}

/// <summary>
/// Setting this property will reload fonts and modify the ImGuiStyle
/// </summary>
public static float Scale
{
get => Internal.State.Instance.Scale;
get => _backend.Scale;
set
{
//if (_inProcessFrame)
// throw new InvalidOperationException("scale cannot be changed during process frame");

if (Internal.State.Instance.Scale != value && value >= 0.25f)
if (_backend.Scale != value && value >= 0.25f)
{
Internal.State.Instance.Scale = value;
_backend.Scale = value;
RebuildFontAtlas();
}
}
}

static ImGuiGD()
{
_backend = ClassDB.ClassExists("ImGuiGD") ? new Internal.BackendNative() : new Internal.BackendNet();
}

public static IntPtr BindTexture(Texture2D tex)
{
return (IntPtr)tex.GetRid().Id;
}

public static void ResetFonts()
{
Internal.State.Instance.Fonts.ResetFonts();
_backend.ResetFonts();
}

public static void AddFont(FontFile fontData, int fontSize, bool merge = false)
{
Internal.State.Instance.Fonts.AddFont(fontData, fontSize, merge);
_backend.AddFont(fontData, fontSize, merge);
}

public static void AddFontDefault()
{
Internal.State.Instance.Fonts.AddFont(null, 13, false);
_backend.AddFontDefault();
}

public static void RebuildFontAtlas()
Expand All @@ -58,17 +69,22 @@ public static void RebuildFontAtlas()
bool scaleToDpi = (bool)ProjectSettings.GetSetting("display/window/dpi/allow_hidpi");
int dpiFactor = Math.Max(1, DisplayServer.ScreenGetDpi() / 96);

Internal.State.Instance.Fonts.RebuildFontAtlas(scaleToDpi ? Scale * dpiFactor : Scale);
_backend.RebuildFontAtlas(scaleToDpi ? Scale * dpiFactor : Scale);
}

public static void Connect(Callable callable)
{
ImGuiLayer.Instance?.Signaler.Connect("imgui_layout", callable);
_backend.Connect(callable);
}

public static void Connect(Action action)
{
Connect(Callable.From(action));
}

internal static bool SubViewportWidget(SubViewport svp)
{
return _backend.SubViewportWidget(svp);
}
}
#endif
39 changes: 39 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Godot;

namespace ImGuiGodot.Internal;

internal sealed class BackendNative : IBackend
{
public float JoyAxisDeadZone { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }
public float Scale { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); }

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
throw new System.NotImplementedException();
}

public void AddFontDefault()
{
throw new System.NotImplementedException();
}

public void Connect(Callable callable)
{
throw new System.NotImplementedException();
}

public void RebuildFontAtlas(float scale)
{
throw new System.NotImplementedException();
}

public void ResetFonts()
{
throw new System.NotImplementedException();
}

public bool SubViewportWidget(SubViewport svp)
{
throw new System.NotImplementedException();
}
}
61 changes: 61 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/BackendNet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Godot;
using ImGuiNET;
using System;
using Vector2 = System.Numerics.Vector2;

namespace ImGuiGodot.Internal;

internal sealed class BackendNet : IBackend
{
public float JoyAxisDeadZone { get; set; } = 0.15f;
public float Scale
{
get => State.Instance.Scale;
set => State.Instance.Scale = value;
}

public void AddFont(FontFile fontData, int fontSize, bool merge)
{
State.Instance.Fonts.AddFont(fontData, fontSize, merge);
}

public void AddFontDefault()
{
State.Instance.Fonts.AddFont(null, 13, false);
}

public void Connect(Callable callable)
{
ImGuiLayer.Instance?.Signaler.Connect("imgui_layout", callable);
}

public void RebuildFontAtlas(float scale)
{
State.Instance.Fonts.RebuildFontAtlas(scale);
}

public void ResetFonts()
{
State.Instance.Fonts.ResetFonts();
}

public bool SubViewportWidget(SubViewport vp)
{
Vector2 vpSize = new(vp.Size.X, vp.Size.Y);
var pos = ImGui.GetCursorScreenPos();
var pos_max = new Vector2(pos.X + vpSize.X, pos.Y + vpSize.Y);
ImGui.GetWindowDrawList().AddImage((IntPtr)vp.GetTexture().GetRid().Id, pos, pos_max);

ImGui.PushID(vp.NativeInstance);
ImGui.InvisibleButton("godot_subviewport", vpSize);
ImGui.PopID();

if (ImGui.IsItemHovered())
{
State.Instance.Input.CurrentSubViewport = vp;
State.Instance.Input.CurrentSubViewportPos = pos;
return true;
}
return false;
}
}
15 changes: 15 additions & 0 deletions addons/imgui-godot/ImGuiGodot/Internal/IBackend.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Godot;

namespace ImGuiGodot.Internal;

internal interface IBackend
{
public float JoyAxisDeadZone { get; set; }
public float Scale { get; set; }
public void ResetFonts();
public void AddFont(FontFile fontData, int fontSize, bool merge);
public void AddFontDefault();
public void RebuildFontAtlas(float scale);
public void Connect(Callable callable);
public bool SubViewportWidget(SubViewport svp);
}
17 changes: 1 addition & 16 deletions addons/imgui-godot/ImGuiGodot/Widgets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,7 @@ public static class Widgets
/// </returns>
public static bool SubViewport(SubViewport vp)
{
Vector2 vpSize = new(vp.Size.X, vp.Size.Y);
var pos = ImGui.GetCursorScreenPos();
var pos_max = new Vector2(pos.X + vpSize.X, pos.Y + vpSize.Y);
ImGui.GetWindowDrawList().AddImage((IntPtr)vp.GetTexture().GetRid().Id, pos, pos_max);

ImGui.PushID(vp.NativeInstance);
ImGui.InvisibleButton("godot_subviewport", vpSize);
ImGui.PopID();

if (ImGui.IsItemHovered())
{
Internal.State.Instance.Input.CurrentSubViewport = vp;
Internal.State.Instance.Input.CurrentSubViewportPos = pos;
return true;
}
return false;
return ImGuiGD.SubViewportWidget(vp);
}

public static void Image(Texture2D tex, Vector2 size)
Expand Down
4 changes: 2 additions & 2 deletions addons/imgui-godot/scripts/ImGuiRoot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const csharp_sync := "res://addons/imgui-godot/ImGuiGodot/ImGuiSync.cs"

func _enter_tree():
var features := ProjectSettings.get_setting("application/config/features")
if ClassDB.class_exists("ImGuiNative"):
add_child(ClassDB.instantiate("ImGuiNative"))
if ClassDB.class_exists("ImGuiLayerNative"):
add_child(ClassDB.instantiate("ImGuiLayerNative"))
if "C#" in features:
add_child(load(csharp_sync).new())
else:
Expand Down

0 comments on commit b1c9de7

Please sign in to comment.