Skip to content

Commit

Permalink
load ImGuiRoot earlier to support editor plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Jul 31, 2023
1 parent cdcd43c commit 782946e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
3 changes: 2 additions & 1 deletion gdext/proj/addons/imgui-godot-native/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
extends EditorPlugin

func _enter_tree():
name = "ImGuiGodotNativeEditorPlugin"
if not ClassDB.class_exists("ImGuiGD"):
push_warning("imgui-godot-native: restarting editor to complete installation")
get_editor_interface().restart_editor()
return
var igd = Engine.get_singleton("ImGuiGD")
igd.InitEditor()
igd.InitEditor(self)
add_autoload_singleton("imgui_godot_native", "res://addons/imgui-godot-native/ImGuiGodot.tscn")

func _exit_tree():
Expand Down
7 changes: 7 additions & 0 deletions gdext/samples/sample-plugin/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[plugin]

name="sample-plugin"
description="Editor plugin that uses ImGui"
author="Patrick Dawson"
version="1.0"
script="plugin.gd"
18 changes: 18 additions & 0 deletions gdext/samples/sample-plugin/plugin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@tool
extends EditorPlugin

func _enter_tree():
# Initialization of the plugin goes here.
pass

func _ready():
ImGuiGD.ToolInit()

func _process(_delta):
ImGui.Begin("plugin window")
ImGui.Text("hello from plugin")
ImGui.End()

func _exit_tree():
# Clean-up of the plugin goes here.
pass
10 changes: 4 additions & 6 deletions gdext/src/ImGuiGD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace ImGui::Godot {

void ImGuiGD::_bind_methods()
{
ClassDB::bind_static_method("ImGuiGD", D_METHOD("InitEditor"), &ImGuiGD::InitEditor);
ClassDB::bind_static_method("ImGuiGD", D_METHOD("InitEditor", "parent"), &ImGuiGD::InitEditor);
ClassDB::bind_static_method("ImGuiGD", D_METHOD("ToolInit"), &ImGuiGD::ToolInit);
ClassDB::bind_static_method("ImGuiGD", D_METHOD("Connect", "callable"), &ImGuiGD::Connect);
ClassDB::bind_static_method("ImGuiGD", D_METHOD("ResetFonts"), &ImGuiGD::ResetFonts);
Expand All @@ -37,22 +37,20 @@ void ImGuiGD::_bind_methods()
&ImGuiGD::GetImGuiPtrs);
}

void ImGuiGD::InitEditor()
void ImGuiGD::InitEditor(Node* parent)
{
#ifdef DEBUG_ENABLED
if (!Engine::get_singleton()->is_editor_hint())
return;

MainLoop* ml = Engine::get_singleton()->get_main_loop();
SceneTree* st = Object::cast_to<SceneTree>(ml);
if (st && !Engine::get_singleton()->has_singleton("ImGuiRoot"))
if (!Engine::get_singleton()->has_singleton("ImGuiRoot"))
{
String resPath = "res://addons/imgui-godot-native/ImGuiGodot.tscn";
if (ResourceLoader::get_singleton()->exists(resPath))
{
Ref<PackedScene> scene = ResourceLoader::get_singleton()->load(resPath);
if (scene.is_valid())
st->get_root()->call_deferred("add_child", scene->instantiate());
parent->add_child(scene->instantiate());
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion gdext/src/ImGuiGD.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImGuiGD : public Object
static void _bind_methods();

public:
static void InitEditor();
static void InitEditor(Node* parent);
static void ToolInit();

static void Connect(const Callable& cb);
Expand Down
3 changes: 2 additions & 1 deletion gdext/src/ImGuiRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ void ImGuiRoot::_get_property_list(List<PropertyInfo>* p_list) const

void ImGuiRoot::_enter_tree()
{
if (get_parent() == get_window())
Node* parent = get_parent();
if (parent == get_window() || parent->get_name() == StringName("ImGuiGodotNativeEditorPlugin"))
{
Engine::get_singleton()->register_singleton("ImGuiRoot", this);
ImGuiLayer* igl = memnew(ImGuiLayer);
Expand Down

0 comments on commit 782946e

Please sign in to comment.