Skip to content

Commit

Permalink
Finish SetMainViewport implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed May 13, 2024
1 parent edf1be6 commit 6de6557
Show file tree
Hide file tree
Showing 36 changed files with 622 additions and 401 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/gdext.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ jobs:
pkgdir=~/out/imgui-godot-${plugin_ver}
mkdir -p $pkgdir
mv * $pkgdir
echo "pkgdir=$pkgdir" >> $GITHUB_OUTPUT
- name: Upload package
uses: actions/upload-artifact@v4
Expand All @@ -238,6 +239,12 @@ jobs:
path: |
~/out
- name: Upload asset package
uses: actions/upload-artifact@v4
with:
name: AssetLib-${{ steps.prep.outputs.pkgfn }}
path: ${{ steps.prep.outputs.pkgdir }}/addons

package_cs:
name: 📦Package (C# only)
runs-on: ubuntu-latest
Expand Down
5 changes: 2 additions & 3 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public partial class ImGuiLayer : CanvasLayer
private Transform2D _finalTransform = Transform2D.Identity;
private bool _visible = true;
private Viewport _parentViewport = null!;
public Vector2I ViewportSize { get; private set; }

public override void _EnterTree()
{
Expand All @@ -26,7 +25,7 @@ public override void _EnterTree()
RenderingServer.CanvasItemSetParent(_canvasItem, GetCanvas());

State.Instance.Renderer.InitViewport(_subViewportRid);
State.Instance.Viewports.SetMainViewport(_parentViewport, _subViewportRid);
State.Instance.Viewports.SetMainWindow(GetWindow(), _subViewportRid);
}

public override void _Ready()
Expand Down Expand Up @@ -59,7 +58,7 @@ private void OnChangeVisibility()

public override void _Input(InputEvent @event)
{
if (State.Instance.ProcessInput(@event))
if (State.Instance.Input.ProcessInput(@event))
{
_parentViewport.SetInputAsHandled();
}
Expand Down
3 changes: 2 additions & 1 deletion addons/imgui-godot/ImGuiGodot/Internal/BackendNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ private sealed class MethodName
public static readonly StringName Connect = "Connect";
public static readonly StringName RebuildFontAtlas = "RebuildFontAtlas";
public static readonly StringName ResetFonts = "ResetFonts";
public static readonly StringName SetMainViewport = "SetMainViewport";
public static readonly StringName SubViewport = "SubViewport";
public static readonly StringName ToolInit = "ToolInit";
}
Expand Down Expand Up @@ -81,7 +82,7 @@ public void ResetFonts()
}
public void SetMainViewport(Viewport vp)
{
throw new NotImplementedException();
_gd.Call(MethodName.SetMainViewport, vp);
}

public bool SubViewportWidget(SubViewport svp)
Expand Down
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected void ProcessSubViewportWidget(InputEvent evt)
}
}

protected virtual bool HandleEvent(InputEvent evt)
protected bool HandleEvent(InputEvent evt)
{
var io = ImGui.GetIO();
bool consumed = false;
Expand Down Expand Up @@ -236,7 +236,7 @@ protected virtual bool HandleEvent(InputEvent evt)
return consumed;
}

public bool ProcessInput(InputEvent evt)
public virtual bool ProcessInput(InputEvent evt)
{
ProcessSubViewportWidget(evt);
return HandleEvent(evt);
Expand Down
4 changes: 2 additions & 2 deletions addons/imgui-godot/ImGuiGodot/Internal/InputLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protected override void UpdateMousePos(ImGuiIOPtr io)
// do not use global mouse position
}

protected override bool HandleEvent(InputEvent evt)
public override bool ProcessInput(InputEvent evt)
{
// no support for SubViewport widgets

Expand All @@ -23,7 +23,7 @@ protected override bool HandleEvent(InputEvent evt)
mm.Dispose();
return io.WantCaptureMouse;
}
return base.HandleEvent(evt);
return HandleEvent(evt);
}
}
#endif
11 changes: 0 additions & 11 deletions addons/imgui-godot/ImGuiGodot/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,5 @@ public void Render()
ImGui.UpdatePlatformWindows();
Renderer.Render();
}

/// <summary>
/// Send input event to ImGui
/// </summary>
/// <returns>
/// True if the InputEvent was consumed
/// </returns>
public bool ProcessInput(InputEvent evt)
{
return Input.ProcessInput(evt);
}
}
#endif
110 changes: 14 additions & 96 deletions addons/imgui-godot/ImGuiGodot/Internal/Viewports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,7 @@

namespace ImGuiGodot.Internal;

internal interface IViewportWindow : IDisposable
{
public void ShowWindow();
public void SetWindowPos(Vector2I pos);
public Vector2I GetWindowPos();
public void SetWindowSize(Vector2I size);
public Vector2I GetWindowSize();
public void SetWindowFocus();
public bool GetWindowFocus();
public bool GetWindowMinimized();
public void SetWindowTitle(string title);
}

internal sealed class GodotImGuiWindow : IViewportWindow
internal sealed class GodotImGuiWindow
{
private readonly GCHandle _gcHandle;
private readonly ImGuiViewportPtr _vp;
Expand Down Expand Up @@ -150,68 +137,6 @@ public void SetWindowTitle(string title)
}
}

internal sealed class GodotImGuiSubViewport : IViewportWindow
{
private readonly GCHandle _gcHandle;
private readonly SubViewport _svp;

public GodotImGuiSubViewport(ImGuiViewportPtr vp, SubViewport svp, Rid mainSubViewport)
{
_gcHandle = GCHandle.Alloc(this);
vp.PlatformUserData = (IntPtr)_gcHandle;
vp.RendererUserData = (IntPtr)mainSubViewport.Id;
_svp = svp;
}

public void Dispose()
{
if (_gcHandle.IsAllocated)
{
_gcHandle.Free();
}
}

public bool GetWindowFocus()
{
return true;
}

public bool GetWindowMinimized()
{
return false;
}

public Vector2I GetWindowPos()
{
return new(0, 0);
}

public Vector2I GetWindowSize()
{
return _svp.Size;
}

public void SetWindowFocus()
{
}

public void SetWindowPos(Vector2I pos)
{
}

public void SetWindowSize(Vector2I size)
{
}

public void SetWindowTitle(string title)
{
}

public void ShowWindow()
{
}
}

internal static class ViewportsExts
{
internal static Vector2 ToImVec2(this Vector2I v)
Expand Down Expand Up @@ -283,7 +208,7 @@ private static readonly Platform_GetWindowMinimized _getWindowMinimized
private delegate void Platform_SetWindowTitle(ImGuiViewportPtr vp, string title);
private static readonly Platform_SetWindowTitle _setWindowTitle = Godot_SetWindowTitle;

private IViewportWindow _mainWindow = null!;
private GodotImGuiWindow _mainWindow = null!;

private static void UpdateMonitors()
{
Expand Down Expand Up @@ -346,17 +271,10 @@ public Viewports()
UpdateMonitors();
}

public void SetMainViewport(Viewport vp, Rid mainSubViewport)
public void SetMainWindow(Window window, Rid mainSubViewport)
{
_mainWindow?.Dispose();
if (vp is Window window)
{
_mainWindow = new GodotImGuiWindow(ImGui.GetMainViewport(), window, mainSubViewport);
}
else if (vp is SubViewport svp)
{
_mainWindow = new GodotImGuiSubViewport(ImGui.GetMainViewport(), svp, mainSubViewport);
}
_mainWindow = new GodotImGuiWindow(ImGui.GetMainViewport(), window, mainSubViewport);
}

private static void Godot_CreateWindow(ImGuiViewportPtr vp)
Expand All @@ -368,63 +286,63 @@ private static void Godot_DestroyWindow(ImGuiViewportPtr vp)
{
if (vp.PlatformUserData != IntPtr.Zero)
{
var window = (IViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target!;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target!;
window.Dispose();
vp.PlatformUserData = IntPtr.Zero;
}
}

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

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

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

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

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

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

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

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

private static void Godot_SetWindowTitle(ImGuiViewportPtr vp, string title)
{
var window = (IViewportWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target!;
var window = (GodotImGuiWindow)GCHandle.FromIntPtr(vp.PlatformUserData).Target!;
window.SetWindowTitle(title);
}
}
Expand Down
17 changes: 8 additions & 9 deletions addons/imgui-godot/scripts/ImGuiPlugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
extends EditorPlugin

func _enter_tree():
Engine.register_singleton("ImGuiPlugin", self)
add_autoload_singleton("ImGuiRoot", "res://addons/imgui-godot/data/ImGuiRoot.tscn")

# remove obsolete ImGuiLayer autoload
if ProjectSettings.has_setting("autoload/ImGuiLayer"):
remove_autoload_singleton("ImGuiLayer")
add_autoload_singleton("ImGuiRoot", "res://addons/imgui-godot/data/ImGuiRoot.tscn")
Engine.register_singleton("ImGuiPlugin", self)

# warn user if csproj will fail to build
if "C#" in ProjectSettings.get_setting("application/config/features"):
var projPath: String = ProjectSettings.get_setting("dotnet/project/solution_directory")
var fn: String = "%s.csproj" % ProjectSettings.get_setting("dotnet/project/assembly_name")
if projPath != "":
fn = "%s/%s" % [projPath, fn]
check_csproj(fn)
check_csproj(projPath.path_join(fn))

func check_csproj(fn):
var fi := FileAccess.open(fn, FileAccess.READ)
Expand All @@ -29,12 +30,10 @@ func check_csproj(fn):
if netVer < 8:
changesNeeded += "- Set target framework to .NET 8 or later\n"

idx = data.find("<AllowUnsafeBlocks>True")
if idx == -1:
if !data.contains("<AllowUnsafeBlocks>True"):
changesNeeded += "- Allow unsafe blocks\n"

idx = data.find("<PackageReference Include=\"ImGui.NET\"")
if idx == -1:
if !data.contains("<PackageReference Include=\"ImGui.NET\""):
changesNeeded += "- Add NuGet package ImGui.NET\n"

if changesNeeded != "":
Expand Down
2 changes: 1 addition & 1 deletion addons/imgui-godot/scripts/ImGuiRoot.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const csharp_controller := "res://addons/imgui-godot/ImGuiGodot/ImGuiController.
const csharp_sync := "res://addons/imgui-godot/ImGuiGodot/ImGuiSync.cs"

func _enter_tree():
Engine.register_singleton("ImGuiRoot", self)
# Engine.register_singleton("ImGuiRoot", self)
var features := ProjectSettings.get_setting("application/config/features")
if ClassDB.class_exists("ImGuiController"):
# native
Expand Down
Loading

0 comments on commit 6de6557

Please sign in to comment.