From ebe90ebecacdbc2620b2c3ed2ada2a3a28c83681 Mon Sep 17 00:00:00 2001 From: Patrick Dawson Date: Sat, 8 Oct 2022 18:22:42 +0200 Subject: [PATCH] use better viewport id --- README.md | 2 +- addons/imgui-godot/ImGuiGDInternal.cs | 11 ++++------- addons/imgui-godot/ImGuiGodot.cs | 5 ++++- data/demo2.tscn | 5 ++++- src/MyNode.cs | 11 ----------- src/MySecondNode.cs | 9 +++++++++ 6 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2b16ac1..819fc01 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ After installing the plugin, usage is as simple as this: ```csharp public partial class MyNode : Node { - public override void _Process() + public override void _Process(double delta) { ImGui.Begin("ImGui on Godot 4"); ImGui.Text("hello world"); diff --git a/addons/imgui-godot/ImGuiGDInternal.cs b/addons/imgui-godot/ImGuiGDInternal.cs index 06288f7..e46fd95 100644 --- a/addons/imgui-godot/ImGuiGDInternal.cs +++ b/addons/imgui-godot/ImGuiGDInternal.cs @@ -18,19 +18,19 @@ internal static class ImGuiGDInternal private static GCHandle _backendName = GCHandle.Alloc(Encoding.ASCII.GetBytes("imgui_impl_godot4"), GCHandleType.Pinned); // necessary because we can't construct arbitrary RIDs without using reflection - private static Dictionary _rids = new(); + private static Dictionary _texrids = new(); public static IntPtr BindTexture(Texture2D tex) { RID rid = tex.GetRid(); IntPtr texid = (IntPtr)rid.Id; - _rids.TryAdd(texid, rid); + _texrids.TryAdd(texid, rid); return texid; } public static void UnbindTexture(IntPtr texid) { - _rids.Remove(texid); + _texrids.Remove(texid); } public static unsafe ImFontPtr AddFont(FontFile fontData, float fontSize, bool merge) @@ -176,8 +176,6 @@ internal static class ImGuiGDInternal { // TODO: correct mouse pos CurrentSubViewport.PushInput(evt, true); - if (evt is not InputEventMouseMotion) - return true; } var io = ImGui.GetIO(); @@ -391,8 +389,7 @@ internal static class ImGuiGDInternal RID child = _children[nodeN++]; - IntPtr texid = drawCmd.GetTexID(); - RID texrid = _rids[texid]; + RID texrid = _texrids[drawCmd.GetTexID()]; RenderingServer.CanvasItemClear(child); RenderingServer.CanvasItemSetClip(child, true); RenderingServer.CanvasItemSetCustomRect(child, true, new Rect2( diff --git a/addons/imgui-godot/ImGuiGodot.cs b/addons/imgui-godot/ImGuiGodot.cs index aa704fa..34d7075 100644 --- a/addons/imgui-godot/ImGuiGodot.cs +++ b/addons/imgui-godot/ImGuiGodot.cs @@ -18,7 +18,10 @@ public static class ImGuiGodot var pos_max = new Vector2(pos.X + vpSize.X, pos.Y + vpSize.Y); ImGui.GetWindowDrawList().AddImage(ImGuiGD.BindTexture(vp.GetTexture()), pos, pos_max); - ImGui.InvisibleButton(string.Format("{0}##{1}", vp.Name, vp.GetViewportRid().Id), vpSize); + ImGui.PushID(vp.NativeInstance); + ImGui.InvisibleButton("godot_subviewport", vpSize); + ImGui.PopID(); + if (ImGui.IsItemHovered()) { ImGuiGDInternal.CurrentSubViewport = vp; diff --git a/data/demo2.tscn b/data/demo2.tscn index a4207af..62e82a3 100644 --- a/data/demo2.tscn +++ b/data/demo2.tscn @@ -19,6 +19,7 @@ color = Color(0.0784314, 0, 0.117647, 1) script = ExtResource("2_gnjei") [node name="ShowHideButton" type="Button" parent="."] +unique_name_in_owner = true layout_mode = 1 anchors_preset = 3 anchor_left = 1.0 @@ -31,7 +32,9 @@ offset_right = -7.0 offset_bottom = -8.0 grow_horizontal = 0 grow_vertical = 0 -text = "show/hide" +focus_mode = 0 +toggle_mode = true +text = "hide" [node name="SubViewport" type="SubViewport" parent="."] unique_name_in_owner = true diff --git a/src/MyNode.cs b/src/MyNode.cs index 4e77fbc..452a00a 100644 --- a/src/MyNode.cs +++ b/src/MyNode.cs @@ -3,17 +3,6 @@ using ImGuiNET; public partial class MyNode : Node { - public override void _EnterTree() - { - if (ImGuiLayer.Instance is null) - { - // if the plugin is disabled, we can do some basic setup to avoid crashes - ImGuiGD.Init(); - ImGuiGD.RebuildFontAtlas(); - ImGui.NewFrame(); - } - } - public override void _Process(double delta) { ImGui.ShowDemoWindow(); diff --git a/src/MySecondNode.cs b/src/MySecondNode.cs index 31e889e..ac5589a 100644 --- a/src/MySecondNode.cs +++ b/src/MySecondNode.cs @@ -82,5 +82,14 @@ public partial class MySecondNode : Node private void _on_show_hide() { ImGuiLayer.Instance.Visible = !ImGuiLayer.Instance.Visible; + if (ImGuiLayer.Instance.Visible) + { + GetNode