summaryrefslogtreecommitdiff
path: root/plugins/hello_gdextension/src/main/cpp/jni/plugin_manager.cpp
blob: 647dd3e14e8cb89fbe63d46873835587d82379cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "plugin_manager.h"

#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/main_loop.hpp>
#include <godot_cpp/classes/resource.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/window.hpp>

#include "utils.h"

namespace godot {

PluginManager *PluginManager::singleton = nullptr;

PluginManager::PluginManager() {
    gdexample_node = memnew(GDExample);

    Ref<Resource> resource_ref = ResourceLoader::get_singleton()->load("res://addons/hello_gdextension_plugin/interface/android_icon.svg");
    gdexample_node->set_texture(resource_ref);
}

PluginManager::~PluginManager() {
    memdelete(gdexample_node);
}

PluginManager *PluginManager::get_singleton() {
    if (singleton == nullptr) {
        singleton = new PluginManager();
    }
    return singleton;
}

void PluginManager::toggle_visibility(bool p_visible) {
    ALOG_ASSERT(gdexample_node != nullptr, "Uninitialized gdexample node");

    gdexample_node->set_visible(p_visible);
}

void PluginManager::add_gdexample_node(const String &p_parent_node_path) {
    ALOG_ASSERT(gdexample_node != nullptr, "Uninitialized gdexample node");

    if (p_parent_node_path.is_empty()) {
        ALOGW("Empty parent node path, aborting...");
        return;
    }

    // Retrieve the parent node.
    Node *parent_node = get_node(p_parent_node_path);
    if (!parent_node) {
        ALOGE("Unable to retrieve parent node with path %s", p_parent_node_path.utf8().get_data());
        return;
    }

    if (gdexample_node->get_parent() != nullptr) {
        gdexample_node->get_parent()->remove_child(gdexample_node);
    }

    parent_node->add_child(gdexample_node);
    gdexample_node->set_owner(parent_node);
    gdexample_node->set_centered(false);
}

Node *PluginManager::get_node(const String &p_node_path) {
    if (p_node_path.is_empty()) {
        ALOGW("Empty node path argument.");
        return nullptr;
    }

    MainLoop *main_loop = Engine::get_singleton()->get_main_loop();
    auto *scene_tree = Object::cast_to<SceneTree>(main_loop);
    if (!scene_tree) {
        ALOGW("Unable to retrieve the scene tree.");
        return nullptr;
    }

    const Window *window = scene_tree->get_root();
    NodePath node_path(p_node_path);
    Node *node = window->get_node_or_null(node_path);
    if (!node) {
        // Try again by treating the parameter as the node's name
        node = window->find_child(p_node_path, true, false);
    }

    return node;
}
}