Skip to content

Commit

Permalink
Use GDScript node for signal
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jan 17, 2024
1 parent 186e54a commit 91df8a7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 46 deletions.
2 changes: 1 addition & 1 deletion addons/imgui-godot/ImGuiGodot/ImGuiGD.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !GODOT_MOBILE
#if GODOT_PC
using Godot;
using ImGuiNET;
using System;
Expand Down
55 changes: 10 additions & 45 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#if GODOT_PC
using ImGuiNET;
using System;
using System.Collections.Generic;

namespace ImGuiGodot;

Expand All @@ -13,19 +12,14 @@ public partial class ImGuiLayer : CanvasLayer
[Export(PropertyHint.ResourceType, "ImGuiConfig")]
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 = null!;
private Rid _subViewportRid;
private Vector2I _subViewportSize = Vector2I.Zero;
private Rid _ci;
private Transform2D _finalTransform = Transform2D.Identity;
private UpdateFirst _updateFirst = null!;
private static readonly HashSet<GodotObject> _connectedObjects = new();
private bool _headless = false;
public Node Signaler { get; private set; } = null!;

private sealed partial class UpdateFirst : Node
{
Expand Down Expand Up @@ -112,6 +106,10 @@ public override void _EnterTree()
ProcessMode = ProcessModeEnum.Always,
};
AddChild(_updateFirst);

Signaler = (Node)((GDScript)GD.Load("res://addons/imgui-godot/scripts/ImGuiSignaler.gd")).New();
Signaler.Name = "Signaler";
AddChild(Signaler);
}

public override void _Ready()
Expand Down Expand Up @@ -168,7 +166,7 @@ public override void _Process(double delta)
RenderingServer.CanvasItemAddTextureRect(_ci, new(0, 0, _subViewportSize.X, _subViewportSize.Y), vptex);
}

EmitSignal(SignalName.ImGuiLayout);
Signaler.EmitSignal("imgui_layout");
ImGuiGD.Render();
}

Expand All @@ -185,47 +183,14 @@ public override void _Input(InputEvent e)
}
}

public static void Connect(ImGuiLayoutEventHandler d)
public static void Connect(Callable callable)
{
if (Instance is null)
return;

Instance.ImGuiLayout += d;

if (d.Target is GodotObject obj)
{
if (_connectedObjects.Count == 0)
{
Instance.GetTree().NodeRemoved += OnNodeRemoved;
}
_connectedObjects.Add(obj);
}
Instance?.Signaler.Connect("imgui_layout", callable);
}

private static void OnNodeRemoved(Node node)
public static void Connect(Action action)
{
// signals declared in C# don't (yet?) work like normal Godot signals,
// we need to clean up after removed Objects ourselves

if (!_connectedObjects.Contains(node))
return;

_connectedObjects.Remove(node);

// backing_ImGuiLayout is an implementation detail that could change
foreach (Delegate d in Instance.backing_ImGuiLayout.GetInvocationList())
{
// remove ALL delegates with the removed Node as a target
if (d.Target == node)
{
Instance.ImGuiLayout -= (ImGuiLayoutEventHandler)d;
}
}

if (_connectedObjects.Count == 0)
{
Instance.GetTree().NodeRemoved -= OnNodeRemoved;
}
Connect(Callable.From(action));
}

private void CheckContentScale()
Expand Down
3 changes: 3 additions & 0 deletions addons/imgui-godot/scripts/ImGuiSignaler.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends Node

signal imgui_layout

0 comments on commit 91df8a7

Please sign in to comment.