Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Add support for dropping a GDScript file to create a new node in the 2D/3D editor.

Dexter1131e3e207b1
diff --git a/editor/scene/3d/node_3d_editor_viewport.cpp b/editor/scene/3d/node_3d_editor_viewport.cpp
index 48c0cb4a4e9..e5d364381b5 100644
--- a/editor/scene/3d/node_3d_editor_viewport.cpp
+++ b/editor/scene/3d/node_3d_editor_viewport.cpp
@@ -70,6 +70,7 @@
#include "scene/gui/subviewport_container.h"
#include "scene/main/node.h"
#include "scene/main/scene_tree.h"
+#include "scene/property_utils.h"
#include "scene/resources/gradient.h"
#include "scene/resources/immediate_mesh.h"
#include "scene/resources/packed_scene.h"
@@ -5632,6 +5633,19 @@ void Node3DEditorViewport::_create_preview_node(const Vector<String> &files) con
preview_node->add_child(sprite);
add_preview = true;
}
+
+ Ref<Script> script = res;
+ if (script.is_valid()) {
+ String class_name = script->get_global_name();
+ String base_type = script->get_instance_base_type();
+ Sprite3D *sprite = memnew(Sprite3D);
+ sprite->set_texture(EditorNode::get_singleton()->get_class_icon(
+ class_name.is_empty() ? base_type : class_name));
+ sprite->set_billboard_mode(StandardMaterial3D::BILLBOARD_ENABLED);
+ sprite->set_pixel_size(0.005);
+ preview_node->add_child(sprite);
+ add_preview = true;
+ }
}
if (add_preview) {
EditorNode::get_singleton()->get_scene_root()->add_child(preview_node);
@@ -5884,6 +5898,59 @@ bool Node3DEditorViewport::_create_audio_node(Node *p_parent, const String &p_pa
return true;
}
+bool Node3DEditorViewport::_create_script_node(Node *p_parent, const String &p_path, const Point2 &p_point) {
+ Ref<Script> script = ResourceLoader::load(p_path);
+ if (script.is_null()) {
+ return false;
+ }
+
+ String class_name = script->get_global_name();
+ String base_type = script->get_instance_base_type();
+ Object *ob = ClassDB::instantiate(base_type);
+ Node *instantiated_node = Object::cast_to<Node>(ob);
+ if (!instantiated_node) { // Error on instantiation.
+ return false;
+ }
+ if (class_name.is_empty()) {
+ const String &node_name = Node::adjust_name_casing(p_path.get_file().get_basename());
+ if (!node_name.is_empty()) {
+ instantiated_node->set_name(node_name);
+ }
+ } else {
+ instantiated_node->set_name(class_name);
+ PropertyUtils::assign_custom_type_script(ob, script);
+ }
+ instantiated_node->set_script(script);
+
+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
+ undo_redo->add_do_method(p_parent, "add_child", instantiated_node, true);
+ undo_redo->add_do_method(instantiated_node, "set_owner", EditorNode::get_singleton()->get_edited_scene());
+ undo_redo->add_do_reference(instantiated_node);
+ undo_redo->add_undo_method(p_parent, "remove_child", instantiated_node);
+ undo_redo->add_do_method(editor_selection, "add_node", instantiated_node);
+
+ const String new_name = p_parent->validate_child_name(instantiated_node);
+ EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
+ undo_redo->add_do_method(ed, "live_debug_create_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent), instantiated_node->get_class(), new_name);
+ undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent)) + "/" + new_name));
+
+ Transform3D parent_tf;
+ Node3D *parent_node3d = Object::cast_to<Node3D>(p_parent);
+ if (parent_node3d) {
+ parent_tf = parent_node3d->get_global_gizmo_transform();
+ }
+
+ if (ClassDB::has_property(instantiated_node->get_class(), "transform")) {
+ Transform3D new_tf = instantiated_node->get("transform");
+ new_tf.origin = parent_tf.affine_inverse().xform(preview_node_pos + instantiated_node->get("position"));
+ new_tf.basis = parent_tf.affine_inverse().basis * new_tf.basis;
+
+ undo_redo->add_do_method(instantiated_node, "set_transform", new_tf);
+ }
+
+ return true;
+}
+
void Node3DEditorViewport::_perform_drop_data() {
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
if (spatial_editor->get_preview_material_target().is_valid()) {
@@ -5933,6 +6000,13 @@ void Node3DEditorViewport::_perform_drop_data() {
error_files.push_back(path.get_file());
}
}
+
+ Ref<Script> script = res;
+ if (script.is_valid()) {
+ if (!_create_script_node(target_node, path, drop_pos)) {
+ error_files.push_back(path.get_file());
+ }
+ }
}
undo_redo->commit_action();
@@ -5995,12 +6069,18 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
AUDIO = 1 << 2,
MESH = 1 << 3,
MATERIAL = 1 << 4,
+ SCRIPT = 1 << 5,
};
int instantiate_type = 0;
// Track whether a type other than PackedScene is valid to stop checking them and only
// continue to check if the rest of the scenes are valid (don't have cyclic dependencies).
bool is_other_valid = false;
+
+ String error_message;
+
+ StringName script_node_type;
+
// Check if at least one of the dragged files is a mesh, material, texture, or scene.
for (int i = 0; i < files.size(); i++) {
const String &res_type = ResourceLoader::get_resource_type(files[i]);
@@ -6009,8 +6089,9 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
bool is_material = ClassDB::is_parent_class(res_type, "Material");
bool is_texture = ClassDB::is_parent_class(res_type, "Texture");
bool is_audio = ClassDB::is_parent_class(res_type, "AudioStream");
+ bool is_script = ClassDB::is_parent_class(res_type, "Script");
- if (is_mesh || is_scene || is_material || is_texture || is_audio) {
+ if (is_mesh || is_scene || is_material || is_texture || is_audio || is_script) {
Ref<Resource> res = ResourceLoader::load(files[i]);
if (res.is_null()) {
continue;
@@ -6020,6 +6101,7 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
Ref<Material> mat = res;
Ref<Texture2D> tex = res;
Ref<AudioStream> audio = res;
+ Ref<Script> script = res;
if (scn.is_valid()) {
Node *instantiated_scene = scn->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
if (!instantiated_scene) {
@@ -6062,6 +6144,16 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
} else if (!is_other_valid && audio.is_valid()) {
is_other_valid = true;
instantiate_type |= AUDIO;
+ } else if (!is_other_valid && script.is_valid()) {
+ StringName base_type = script->get_instance_base_type();
+ if (ClassDB::is_parent_class(base_type, "Node")) {
+ StringName global_name = script->get_global_name();
+ script_node_type = global_name.is_empty() ? base_type : global_name;
+ is_other_valid = true;
+ instantiate_type |= SCRIPT;
+ } else {
+ error_message = TTR("This script is not a valid node.");
+ }
} else {
continue;
}
@@ -6069,6 +6161,11 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
}
String title = TTRN("Can't drop the file...", "Can't drop the files...", files.size());
+ if (!error_message.is_empty()) {
+ _show_tooltip(title, error_message);
+ return false;
+ }
+
if (is_cyclical_dep) {
_show_tooltip(title, vformat(TTR("Circular dependency found at %s."), error_file));
return false;
@@ -6112,6 +6209,8 @@ bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant
desc += vformat(TTR("[b]Default:[/b] Place in Geometry's Material Override slot.") +
"\n" + TTR("[b]Hold %s:[/b] Place in Mesh's Surface Material Override slot."),
keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL));
+ } else if (instantiate_type & SCRIPT) {
+ title = vformat(TTR("Dropping a Script as a %s node..."), script_node_type);
}
desc += "[/ul]";
diff --git a/editor/scene/3d/node_3d_editor_viewport.h b/editor/scene/3d/node_3d_editor_viewport.h
index 129987e031c..8c7e116e3e4 100644
--- a/editor/scene/3d/node_3d_editor_viewport.h
+++ b/editor/scene/3d/node_3d_editor_viewport.h
@@ -509,6 +509,7 @@ private:
bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) const;
bool _create_instance(Node *p_parent, const String &p_path, const Point2 &p_point);
bool _create_audio_node(Node *p_parent, const String &p_path, const Point2 &p_point);
+ bool _create_script_node(Node *p_parent, const String &p_path, const Point2 &p_point);
void _perform_drop_data();
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
diff --git a/editor/scene/canvas_item_editor_plugin.cpp b/editor/scene/canvas_item_editor_plugin.cpp
index e614d8ab9c3..29e5ad905ae 100644
--- a/editor/scene/canvas_item_editor_plugin.cpp
+++ b/editor/scene/canvas_item_editor_plugin.cpp
@@ -80,6 +80,7 @@
#include "scene/main/scene_tree.h"
#include "scene/main/timer.h"
#include "scene/main/window.h"
+#include "scene/property_utils.h"
#include "scene/resources/gradient.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/style_box_texture.h"
@@ -6471,6 +6472,19 @@ void CanvasItemEditorViewport::_create_preview(const Vector<String> &files) cons
preview_node->add_child(sprite);
add_preview = true;
}
+
+ Ref<Script> script = res;
+ if (script.is_valid()) {
+ String class_name = script->get_global_name();
+ String base_type = script->get_instance_base_type();
+ Sprite2D *sprite = memnew(Sprite2D);
+ sprite->set_texture(EditorNode::get_singleton()->get_class_icon(
+ class_name.is_empty() ? base_type : class_name));
+ sprite->set_modulate(Color(1, 1, 1, 0.7f));
+ sprite->set_position(Vector2(0, -sprite->get_texture()->get_size().height) * EDSCALE);
+ preview_node->add_child(sprite);
+ add_preview = true;
+ }
}
if (add_preview) {
@@ -6544,7 +6558,7 @@ void CanvasItemEditorViewport::_create_audio_node(Node *p_parent, const String &
child->set_name(node_name);
}
- // Compute the global position
+ // Compute the global position.
Transform2D xform = canvas_item_editor->get_canvas_transform();
Point2 target_position = xform.affine_inverse().xform(p_point);
@@ -6563,7 +6577,7 @@ void CanvasItemEditorViewport::_create_mesh_node(Node *p_parent, const String &p
child->set_name(node_name);
}
- // Compute the global position
+ // Compute the global position.
Transform2D xform = canvas_item_editor->get_canvas_transform();
Point2 target_position = xform.affine_inverse().xform(p_point);
@@ -6626,6 +6640,69 @@ bool CanvasItemEditorViewport::_create_instance(Node *p_parent, const String &p_
return true;
}
+void CanvasItemEditorViewport::_create_script_node(Node *p_parent, const String &p_path, const Point2 &p_point) {
+ Ref<Script> script = ResourceLoader::load(p_path);
+ if (script.is_null()) {
+ return;
+ }
+
+ String class_name = script->get_global_name();
+ String base_type = script->get_instance_base_type();
+ Object *ob = ClassDB::instantiate(base_type);
+ Node *instantiated_node = Object::cast_to<Node>(ob);
+ if (!instantiated_node) { // Error on instantiation.
+ return;
+ }
+ if (class_name.is_empty()) {
+ const String &node_name = Node::adjust_name_casing(p_path.get_file().get_basename());
+ if (!node_name.is_empty()) {
+ instantiated_node->set_name(node_name);
+ }
+ } else {
+ instantiated_node->set_name(class_name);
+ PropertyUtils::assign_custom_type_script(ob, script);
+ }
+ instantiated_node->set_script(script);
+
+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
+
+ if (p_parent) {
+ undo_redo->add_do_method(p_parent, "add_child", instantiated_node, true);
+ undo_redo->add_do_method(instantiated_node, "set_owner", EditorNode::get_singleton()->get_edited_scene());
+ undo_redo->add_do_reference(instantiated_node);
+ undo_redo->add_undo_method(p_parent, "remove_child", instantiated_node);
+ } else { // If no parent is selected, set as root node of the scene.
+ undo_redo->add_do_method(EditorNode::get_singleton(), "set_edited_scene", instantiated_node);
+ undo_redo->add_do_method(instantiated_node, "set_owner", EditorNode::get_singleton()->get_edited_scene());
+ undo_redo->add_do_reference(instantiated_node);
+ undo_redo->add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
+ }
+
+ if (p_parent) {
+ String new_name = p_parent->validate_child_name(instantiated_node);
+ EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
+ undo_redo->add_do_method(ed, "live_debug_create_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent), instantiated_node->get_class(), new_name);
+ undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent)) + "/" + new_name));
+ }
+
+ // Compute the global position.
+ Transform2D xform = canvas_item_editor->get_canvas_transform();
+ Point2 target_position = xform.affine_inverse().xform(p_point);
+
+ // There's nothing to be used as source position, so snapping will work as absolute if enabled.
+ target_position = canvas_item_editor->snap_point(target_position);
+
+ CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_parent);
+ Point2 local_target_pos = parent_ci ? parent_ci->get_global_transform().affine_inverse().xform(target_position) : target_position;
+
+ if (ClassDB::has_property(instantiated_node->get_class(), "position")) {
+ undo_redo->add_do_method(instantiated_node, "set_position", local_target_pos);
+ }
+
+ EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
+ undo_redo->add_do_method(editor_selection, "add_node", instantiated_node);
+}
+
void CanvasItemEditorViewport::_perform_drop_data() {
ERR_FAIL_COND(selected_files.is_empty());
@@ -6691,6 +6768,11 @@ void CanvasItemEditorViewport::_perform_drop_data() {
if (mesh.is_valid()) {
_create_mesh_node(target_node, path, drop_pos);
}
+
+ Ref<Script> script = res;
+ if (script.is_valid()) {
+ _create_script_node(target_node, path, drop_pos);
+ }
}
undo_redo->commit_action();
@@ -6740,10 +6822,14 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
TEXTURE = 1 << 1,
AUDIO = 1 << 2,
MESH = 1 << 3,
+ SCRIPT = 1 << 4,
};
int instantiate_type = 0;
String error_message;
+
+ StringName script_node_type;
+
for (const String &path : files) {
const StringName res_type = ResourceLoader::get_resource_type(path);
@@ -6767,6 +6853,17 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
instantiate_type |= AUDIO;
} else if (ClassDB::is_parent_class(res_type, "Mesh")) {
instantiate_type |= MESH;
+ } else if (ClassDB::is_parent_class(res_type, "Script")) {
+ Ref<Script> script = ResourceLoader::load(path);
+ ERR_CONTINUE(script.is_null());
+ StringName base_type = script->get_instance_base_type();
+ if (ClassDB::is_parent_class(base_type, "Node")) {
+ StringName global_name = script->get_global_name();
+ script_node_type = global_name.is_empty() ? base_type : global_name;
+ instantiate_type |= SCRIPT;
+ } else {
+ error_message = TTR("This script is not a valid node.");
+ }
}
}
@@ -6828,6 +6925,8 @@ bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Varian
title = vformat(TTR("Dropping a Texture file as a %s node..."), default_texture_node_type);
} else if (instantiate_type & MESH) {
title = TTR("Dropping a Mesh file...");
+ } else if (instantiate_type & SCRIPT) {
+ title = vformat(TTR("Dropping a Script as a %s node..."), script_node_type);
}
if (instantiate_type & TEXTURE) {
desc += "\n" + vformat(TTR("[b]Hold %s + Shift:[/b] Add Texture as a different node type."), keycode_get_string((Key)KeyModifierMask::ALT));
diff --git a/editor/scene/canvas_item_editor_plugin.h b/editor/scene/canvas_item_editor_plugin.h
index f2f3c4f7fdb..184d60601a5 100644
--- a/editor/scene/canvas_item_editor_plugin.h
+++ b/editor/scene/canvas_item_editor_plugin.h
@@ -725,6 +725,7 @@ class CanvasItemEditorViewport : public Control {
void _create_audio_node(Node *p_parent, const String &p_path, const Point2 &p_point);
bool _create_instance(Node *p_parent, const String &p_path, const Point2 &p_point);
void _create_mesh_node(Node *p_parent, const String &p_path, const Point2 &p_point);
+ void _create_script_node(Node *p_parent, const String &p_path, const Point2 &p_point);
void _perform_drop_data();
void _show_texture_node_type_selector();
void _update_theme();