Merge pull request #52023 from mhilbrunner/vs-fix-reloaded

Fix VisualScriptEditor after namespaces
This commit is contained in:
Max Hilbrunner 2021-08-25 14:41:28 +02:00 committed by GitHub
commit 4bb65b2bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 33 deletions

View File

@ -1085,8 +1085,8 @@
<member name="TranslationServer" type="TranslationServer" setter="" getter=""> <member name="TranslationServer" type="TranslationServer" setter="" getter="">
The [TranslationServer] singleton. The [TranslationServer] singleton.
</member> </member>
<member name="VisualScriptEditor" type="VisualScriptEditor" setter="" getter=""> <member name="VisualScriptEditor" type="VisualScriptCustomNodes" setter="" getter="">
The [VisualScriptEditor] singleton. The [VisualScriptCustomNodes] singleton.
</member> </member>
<member name="XRServer" type="XRServer" setter="" getter=""> <member name="XRServer" type="XRServer" setter="" getter="">
The [XRServer] singleton. The [XRServer] singleton.

View File

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<class name="VisualScriptEditor" inherits="Object" version="4.0"> <class name="VisualScriptCustomNodes" inherits="Object" version="4.0">
<brief_description> <brief_description>
Manages custom nodes for the Visual Script editor.
</brief_description> </brief_description>
<description> <description>
This singleton can be used to manage (i.e., add or remove) custom nodes for the Visual Script editor.
</description> </description>
<tutorials> <tutorials>
</tutorials> </tutorials>

View File

@ -43,7 +43,7 @@
VisualScriptLanguage *visual_script_language = nullptr; VisualScriptLanguage *visual_script_language = nullptr;
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
static vs_bind::VisualScriptEditor *vs_editor_singleton = nullptr; static VisualScriptCustomNodes *vs_custom_nodes_singleton = nullptr;
#endif #endif
void register_visual_script_types() { void register_visual_script_types() {
@ -114,10 +114,10 @@ void register_visual_script_types() {
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
ClassDB::set_current_api(ClassDB::API_EDITOR); ClassDB::set_current_api(ClassDB::API_EDITOR);
GDREGISTER_CLASS(vs_bind::VisualScriptEditor); GDREGISTER_CLASS(VisualScriptCustomNodes);
ClassDB::set_current_api(ClassDB::API_CORE); ClassDB::set_current_api(ClassDB::API_CORE);
vs_editor_singleton = memnew(vs_bind::VisualScriptEditor); vs_custom_nodes_singleton = memnew(VisualScriptCustomNodes);
Engine::get_singleton()->add_singleton(Engine::Singleton("VisualScriptEditor", vs_bind::VisualScriptEditor::get_singleton())); Engine::get_singleton()->add_singleton(Engine::Singleton("VisualScriptEditor", VisualScriptCustomNodes::get_singleton()));
VisualScriptEditor::register_editor(); VisualScriptEditor::register_editor();
#endif #endif
@ -130,8 +130,8 @@ void unregister_visual_script_types() {
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
VisualScriptEditor::free_clipboard(); VisualScriptEditor::free_clipboard();
if (vs_editor_singleton) { if (vs_custom_nodes_singleton) {
memdelete(vs_editor_singleton); memdelete(vs_custom_nodes_singleton);
} }
#endif #endif
if (visual_script_language) { if (visual_script_language) {

View File

@ -4522,46 +4522,44 @@ void VisualScriptEditor::register_editor() {
void VisualScriptEditor::validate() { void VisualScriptEditor::validate() {
} }
namespace vs_bind { // VisualScriptCustomNodes
Ref<VisualScriptNode> VisualScriptEditor::create_node_custom(const String &p_name) { Ref<VisualScriptNode> VisualScriptCustomNodes::create_node_custom(const String &p_name) {
Ref<VisualScriptCustomNode> node; Ref<VisualScriptCustomNode> node;
node.instantiate(); node.instantiate();
node->set_script(singleton->custom_nodes[p_name]); node->set_script(singleton->custom_nodes[p_name]);
return node; return node;
} }
VisualScriptEditor *VisualScriptEditor::singleton = nullptr; VisualScriptCustomNodes *VisualScriptCustomNodes::singleton = nullptr;
Map<String, REF> VisualScriptEditor::custom_nodes; Map<String, REF> VisualScriptCustomNodes::custom_nodes;
VisualScriptEditor::VisualScriptEditor() { VisualScriptCustomNodes::VisualScriptCustomNodes() {
singleton = this; singleton = this;
} }
VisualScriptEditor::~VisualScriptEditor() { VisualScriptCustomNodes::~VisualScriptCustomNodes() {
custom_nodes.clear(); custom_nodes.clear();
} }
void VisualScriptEditor::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) { void VisualScriptCustomNodes::add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script) {
String node_name = "custom/" + p_category + "/" + p_name; String node_name = "custom/" + p_category + "/" + p_name;
custom_nodes.insert(node_name, p_script); custom_nodes.insert(node_name, p_script);
VisualScriptLanguage::singleton->add_register_func(node_name, &VisualScriptEditor::create_node_custom); VisualScriptLanguage::singleton->add_register_func(node_name, &VisualScriptCustomNodes::create_node_custom);
emit_signal(SNAME("custom_nodes_updated")); emit_signal(SNAME("custom_nodes_updated"));
} }
void VisualScriptEditor::remove_custom_node(const String &p_name, const String &p_category) { void VisualScriptCustomNodes::remove_custom_node(const String &p_name, const String &p_category) {
String node_name = "custom/" + p_category + "/" + p_name; String node_name = "custom/" + p_category + "/" + p_name;
custom_nodes.erase(node_name); custom_nodes.erase(node_name);
VisualScriptLanguage::singleton->remove_register_func(node_name); VisualScriptLanguage::singleton->remove_register_func(node_name);
emit_signal(SNAME("custom_nodes_updated")); emit_signal(SNAME("custom_nodes_updated"));
} }
void VisualScriptEditor::_bind_methods() { void VisualScriptCustomNodes::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &VisualScriptEditor::add_custom_node); ClassDB::bind_method(D_METHOD("add_custom_node", "name", "category", "script"), &VisualScriptCustomNodes::add_custom_node);
ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &VisualScriptEditor::remove_custom_node); ClassDB::bind_method(D_METHOD("remove_custom_node", "name", "category"), &VisualScriptCustomNodes::remove_custom_node);
ADD_SIGNAL(MethodInfo("custom_nodes_updated")); ADD_SIGNAL(MethodInfo("custom_nodes_updated"));
} }
} // namespace vs_bind
#endif #endif

View File

@ -46,6 +46,8 @@ class VisualScriptEditorVariableEdit;
// TODO: Maybe this class should be refactored. // TODO: Maybe this class should be refactored.
// See https://github.com/godotengine/godot/issues/51913 // See https://github.com/godotengine/godot/issues/51913
class VisualScriptEditor : public ScriptEditorBase { class VisualScriptEditor : public ScriptEditorBase {
GDCLASS(VisualScriptEditor, ScriptEditorBase);
enum { enum {
TYPE_SEQUENCE = 1000, TYPE_SEQUENCE = 1000,
INDEX_BASE_SEQUENCE = 1024 INDEX_BASE_SEQUENCE = 1024
@ -330,33 +332,29 @@ public:
~VisualScriptEditor(); ~VisualScriptEditor();
}; };
namespace vs_bind {
// Singleton // Singleton
class VisualScriptEditor : public Object { class VisualScriptCustomNodes : public Object {
GDCLASS(VisualScriptEditor, Object); GDCLASS(VisualScriptCustomNodes, Object);
friend class VisualScriptLanguage; friend class VisualScriptLanguage;
protected: protected:
static void _bind_methods(); static void _bind_methods();
static VisualScriptEditor *singleton; static VisualScriptCustomNodes *singleton;
static Map<String, REF> custom_nodes; static Map<String, REF> custom_nodes;
static Ref<VisualScriptNode> create_node_custom(const String &p_name); static Ref<VisualScriptNode> create_node_custom(const String &p_name);
public: public:
static VisualScriptEditor *get_singleton() { return singleton; } static VisualScriptCustomNodes *get_singleton() { return singleton; }
void add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script); void add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script);
void remove_custom_node(const String &p_name, const String &p_category); void remove_custom_node(const String &p_name, const String &p_category);
VisualScriptEditor(); VisualScriptCustomNodes();
~VisualScriptEditor(); ~VisualScriptCustomNodes();
}; };
} // namespace vs_bind
#endif #endif
#endif // VISUALSCRIPT_EDITOR_H #endif // VISUALSCRIPT_EDITOR_H