Ver código fonte

Add drag n drop export variables

fkeyz 5 meses atrás
pai
commit
02d8cf84b0
2 arquivos alterados com 171 adições e 9 exclusões
  1. 160 9
      editor/script/script_text_editor.cpp
  2. 11 0
      editor/script/script_text_editor.h

+ 160 - 9
editor/script/script_text_editor.cpp

@@ -38,10 +38,13 @@
 #include "editor/debugger/editor_debugger_node.h"
 #include "editor/doc/editor_help.h"
 #include "editor/docks/filesystem_dock.h"
+#include "editor/editor_interface.h"
 #include "editor/editor_node.h"
 #include "editor/editor_string_names.h"
 #include "editor/gui/editor_toaster.h"
 #include "editor/inspector/editor_context_menu_plugin.h"
+#include "editor/inspector/editor_inspector.h"
+#include "editor/inspector/multi_node_edit.h"
 #include "editor/settings/editor_command_palette.h"
 #include "editor/settings/editor_settings.h"
 #include "editor/themes/editor_scale.h"
@@ -145,6 +148,10 @@ void ScriptTextEditor::apply_code() {
 	}
 	script->set_source_code(code_editor->get_text_editor()->get_text());
 	script->update_exports();
+	if (!pending_dragged_exports.is_empty()) {
+		_assign_dragged_export_variables();
+	}
+
 	code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
 }
 
@@ -868,6 +875,10 @@ void ScriptTextEditor::_validate_script() {
 	_update_errors();
 	_update_background_color();
 
+	if (!pending_dragged_exports.is_empty()) {
+		_assign_dragged_export_variables();
+	}
+
 	emit_signal(SNAME("name_changed"));
 	emit_signal(SNAME("edited_script_changed"));
 }
@@ -2198,7 +2209,7 @@ static String _quote_drop_data(const String &str) {
 	return escaped.quote(using_single_quotes ? "'" : "\"");
 }
 
-static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p_create_field, bool p_allow_uid) {
+static String _get_dropped_resource_as_member(const Ref<Resource> &p_resource, bool p_create_field, bool p_allow_uid) {
 	String path = p_resource->get_path();
 	if (p_allow_uid) {
 		ResourceUID::ID id = ResourceLoader::get_resource_uid(path);
@@ -2225,6 +2236,30 @@ static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p
 	return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path));
 }
 
+String ScriptTextEditor::_get_dropped_resource_as_exported_member(const Ref<Resource> &p_resource, const Vector<ObjectID> &p_script_instance_obj_ids) {
+	String variable_name = p_resource->get_name();
+	if (variable_name.is_empty()) {
+		variable_name = p_resource->get_path().get_file().get_basename();
+	}
+
+	variable_name = variable_name.to_snake_case().validate_unicode_identifier();
+	for (ObjectID obj_id : p_script_instance_obj_ids) {
+		pending_dragged_exports.push_back(DraggedExport{ obj_id, variable_name, p_resource });
+	}
+
+	StringName class_name = p_resource->get_class();
+	Ref<Script> resource_script = p_resource->get_script();
+
+	if (resource_script.is_valid()) {
+		StringName global_resource_script_name = resource_script->get_global_name();
+		if (!global_resource_script_name.is_empty()) {
+			class_name = global_resource_script_name;
+		}
+	}
+
+	return vformat("@export var %s: %s", variable_name, class_name);
+}
+
 void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
 	Dictionary d = p_data;
 
@@ -2242,7 +2277,11 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 		is_empty_line = drop_at_column <= te->get_first_non_whitespace_column(drop_at_line) && te->get_selection_to_column(selection_index) == te->get_line(te->get_selection_to_line(selection_index)).length();
 	}
 
-	const bool drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
+	Node *scene_root = get_tree()->get_edited_scene_root();
+
+	const bool member_drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
+	const bool export_drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::ALT);
+
 	const bool allow_uid = Input::get_singleton()->is_key_pressed(Key::SHIFT) != bool(EDITOR_GET("text_editor/behavior/files/drop_preload_resources_as_uid"));
 	const String &line = te->get_line(drop_at_line);
 
@@ -2267,16 +2306,24 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 			return;
 		}
 
-		if (drop_modifier_pressed) {
+		if (member_drop_modifier_pressed) {
 			if (resource->is_built_in()) {
 				String warning = TTR("Preloading internal resources is not supported.");
 				EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR);
 			} else {
-				text_to_drop = _get_dropped_resource_line(resource, is_empty_line, allow_uid);
+				text_to_drop = _get_dropped_resource_as_member(resource, is_empty_line, allow_uid);
 			}
+		} else if (export_drop_modifier_pressed) {
+			Vector<ObjectID> obj_ids = _get_objects_for_export_assignment();
+			text_to_drop = _get_dropped_resource_as_exported_member(resource, obj_ids);
+
 		} else {
 			text_to_drop = _quote_drop_data(path);
 		}
+
+		if (is_empty_line) {
+			text_to_drop += "\n";
+		}
 	}
 
 	if (type == "files" || type == "files_and_dirs") {
@@ -2284,14 +2331,20 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 		PackedStringArray parts;
 
 		for (const String &path : files) {
-			if (drop_modifier_pressed && ResourceLoader::exists(path)) {
+			if ((member_drop_modifier_pressed || export_drop_modifier_pressed) && ResourceLoader::exists(path)) {
 				Ref<Resource> resource = ResourceLoader::load(path);
 				if (resource.is_null()) {
 					// Resource exists, but failed to load. We need only path and name, so we can use a dummy Resource instead.
 					resource.instantiate();
 					resource->set_path_cache(path);
 				}
-				parts.append(_get_dropped_resource_line(resource, is_empty_line, allow_uid));
+
+				if (member_drop_modifier_pressed) {
+					parts.append(_get_dropped_resource_as_member(resource, is_empty_line, allow_uid));
+				} else if (export_drop_modifier_pressed) {
+					Vector<ObjectID> obj_ids = _get_objects_for_export_assignment();
+					parts.append(_get_dropped_resource_as_exported_member(resource, obj_ids));
+				}
 			} else {
 				parts.append(_quote_drop_data(path));
 			}
@@ -2314,7 +2367,6 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 	}
 
 	if (type == "nodes") {
-		Node *scene_root = get_tree()->get_edited_scene_root();
 		if (!scene_root) {
 			EditorNode::get_singleton()->show_warning(TTR("Can't drop nodes without an open scene."));
 			return;
@@ -2332,7 +2384,7 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 
 		Array nodes = d["nodes"];
 
-		if (drop_modifier_pressed) {
+		if (member_drop_modifier_pressed) {
 			const bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
 			add_new_line = !is_empty_line && drop_at_column != 0;
 
@@ -2358,7 +2410,7 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 					Ref<Script> node_script = node->get_script();
 					if (node_script.is_valid()) {
 						StringName global_node_script_name = node_script->get_global_name();
-						if (global_node_script_name != StringName()) {
+						if (!global_node_script_name.is_empty()) {
 							class_name = global_node_script_name;
 						}
 					}
@@ -2374,6 +2426,31 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 			if (is_empty_line || drop_at_column == 0) {
 				text_to_drop += "\n";
 			}
+		} else if (export_drop_modifier_pressed) {
+			Vector<ObjectID> obj_ids = _get_objects_for_export_assignment();
+
+			for (int i = 0; i < nodes.size(); i++) {
+				NodePath np = nodes[i];
+				Node *node = get_node(np);
+				if (!node) {
+					continue;
+				}
+
+				String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier();
+				StringName class_name = node->get_class_name();
+				Ref<Script> node_script = node->get_script();
+				if (node_script.is_valid()) {
+					StringName global_node_script_name = node_script->get_global_name();
+					if (!global_node_script_name.is_empty()) {
+						class_name = global_node_script_name;
+					}
+				}
+
+				text_to_drop += vformat("@export var %s: %s\n", variable_name, class_name);
+				for (ObjectID obj_id : obj_ids) {
+					pending_dragged_exports.push_back(DraggedExport{ obj_id, variable_name, node });
+				}
+			}
 		} else {
 			for (int i = 0; i < nodes.size(); i++) {
 				if (i > 0) {
@@ -2431,6 +2508,80 @@ void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data
 	te->grab_focus();
 }
 
+Vector<ObjectID> ScriptTextEditor::_get_objects_for_export_assignment() const {
+	Vector<ObjectID> objects;
+	Node *scene_root = get_tree()->get_edited_scene_root();
+	bool assign_export_variables = scene_root && ClassDB::is_parent_class(script->get_instance_base_type(), "Node");
+
+	if (!assign_export_variables) {
+		return objects;
+	}
+
+	EditorInspector *inspector = EditorInterface::get_singleton()->get_inspector();
+	if (inspector) {
+		Object *edited_object = inspector->get_edited_object();
+		Node *node_edit = Object::cast_to<Node>(edited_object);
+		MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(edited_object);
+
+		if (node_edit != nullptr) {
+			if (node_edit->get_script() == script) {
+				objects.push_back(node_edit->get_instance_id());
+			}
+		} else if (multi_node_edit != nullptr) {
+			Node *es = EditorNode::get_singleton()->get_edited_scene();
+			for (int i = 0; i < multi_node_edit->get_node_count(); i++) {
+				NodePath np = multi_node_edit->get_node(i);
+				Node *node = es->get_node(np);
+				if (node->get_script() == script) {
+					objects.push_back(node->get_instance_id());
+				}
+			}
+		}
+	}
+
+	// In case there is no current editor selection/editor selection does not contain this script,
+	// it often still makes sense to try to assign the export variable,
+	// so we default to the first node with the script we find in the scene.
+	if (objects.is_empty()) {
+		Node *sn = _find_script_node(scene_root, script);
+		if (sn) {
+			objects.push_back(sn->get_instance_id());
+		}
+	}
+
+	return objects;
+}
+
+void ScriptTextEditor::_assign_dragged_export_variables() {
+	ERR_FAIL_COND(pending_dragged_exports.is_empty());
+
+	bool export_variable_set = false;
+	for (const DraggedExport &dragged_export : pending_dragged_exports) {
+		Object *obj = ObjectDB::get_instance(dragged_export.obj_id);
+		if (!obj) {
+			WARN_PRINT("Object not found, can't assign export variable.");
+			continue;
+		}
+
+		ScriptInstance *si = obj->get_script_instance();
+		if (!si) {
+			WARN_PRINT("Script on " + obj->to_string() + " does not exist anymore, can't assign export variable.");
+			continue;
+		}
+
+		bool success = si->set(dragged_export.variable_name, dragged_export.value);
+		if (success) {
+			export_variable_set = true;
+		}
+	}
+
+	if (export_variable_set) {
+		EditorInterface::get_singleton()->mark_scene_as_unsaved();
+	}
+
+	pending_dragged_exports.clear();
+}
+
 void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
 	Ref<InputEventMouseButton> mb = ev;
 	Ref<InputEventKey> k = ev;

+ 11 - 0
editor/script/script_text_editor.h

@@ -181,6 +181,17 @@ class ScriptTextEditor : public ScriptEditorBase {
 
 	void _enable_code_editor();
 
+	struct DraggedExport {
+		ObjectID obj_id;
+		String variable_name;
+		Variant value;
+	};
+
+	LocalVector<DraggedExport> pending_dragged_exports;
+	Vector<ObjectID> _get_objects_for_export_assignment() const;
+	String _get_dropped_resource_as_exported_member(const Ref<Resource> &p_resource, const Vector<ObjectID> &p_script_instance_obj_ids);
+	void _assign_dragged_export_variables();
+
 protected:
 	void _update_breakpoint_list();
 	void _breakpoint_item_pressed(int p_idx);