Skip to content

Commit 877b010

Browse files
committed
Instantiating scenes
1 parent 3b6a5d0 commit 877b010

File tree

5 files changed

+97
-4
lines changed

5 files changed

+97
-4
lines changed

editor/create_dialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class CreateDialog : public ConfirmationDialog {
5252
};
5353

5454
LineEdit *search_box = nullptr;
55-
Tree *search_options = nullptr;
5655

5756
String base_type;
5857
bool is_base_type_node = false;
@@ -114,6 +113,7 @@ class CreateDialog : public ConfirmationDialog {
114113
void _save_and_update_favorite_list();
115114

116115
public:
116+
Tree *search_options = nullptr;
117117
Variant instantiate_selected();
118118
String get_selected_type();
119119

editor/scene_tree_dock.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ void SceneTreeDock::instantiate_scenes(const Vector<String> &p_files, Node *p_pa
288288
_perform_instantiate_scenes(p_files, parent, -1);
289289
}
290290

291-
void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, Node *p_parent, int p_pos) {
291+
void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, Node *p_parent, int p_pos, bool should_emit) {
292292
ERR_FAIL_NULL(p_parent);
293293

294294
Vector<Node *> instances;
@@ -335,6 +335,31 @@ void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, N
335335
return;
336336
}
337337

338+
if (!should_emit) {
339+
editor_selection->clear();
340+
for (int i = 0; i < instances.size(); i++) {
341+
Node *instantiated_scene = instances[i];
342+
343+
p_parent->add_child(instantiated_scene, true);
344+
if (p_pos >= 0) {
345+
p_parent->move_child(instantiated_scene, p_pos + i);
346+
}
347+
instantiated_scene->set_owner(edited_scene);
348+
editor_selection->add_node(instantiated_scene);
349+
350+
String new_name = p_parent->validate_child_name(instantiated_scene);
351+
EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
352+
ed->live_debug_instantiate_node(edited_scene->get_path_to(p_parent), p_files[i], new_name);
353+
}
354+
355+
_push_item(instances[instances.size() - 1]);
356+
for (int i = 0; i < instances.size(); i++) {
357+
emit_signal(SNAME("node_created"), instances[i]);
358+
}
359+
360+
return;
361+
}
362+
338363
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
339364
undo_redo->create_action_for_history(TTRN("Instantiate Scene", "Instantiate Scenes", instances.size()), editor_data->get_current_edited_scene_history_id());
340365
undo_redo->add_do_method(editor_selection, "clear");
@@ -362,6 +387,8 @@ void SceneTreeDock::_perform_instantiate_scenes(const Vector<String> &p_files, N
362387
for (int i = 0; i < instances.size(); i++) {
363388
emit_signal(SNAME("node_created"), instances[i]);
364389
}
390+
391+
emit_signal("scenes_instantiated", p_parent, p_files, p_pos);
365392
}
366393

367394
void SceneTreeDock::_perform_create_audio_stream_players(const Vector<String> &p_files, Node *p_parent, int p_pos) {
@@ -2951,6 +2978,13 @@ Node *SceneTreeDock::_do_create(Node *p_parent) {
29512978
undo_redo->add_do_method(this, "_post_do_create", child);
29522979
undo_redo->commit_action();
29532980

2981+
TreeItem *selected = create_dialog->search_options->get_selected();
2982+
String non_global_type_name = selected->get_text(0);
2983+
Array meta = selected->get_metadata(0).operator Array();
2984+
bool is_custom_type = meta[0].operator bool();
2985+
String type = meta[1].operator String();
2986+
emit_signal("node_created_type", child, type, is_custom_type, non_global_type_name);
2987+
29542988
return child;
29552989
}
29562990

@@ -4643,6 +4677,8 @@ void SceneTreeDock::_bind_methods() {
46434677
ADD_SIGNAL(MethodInfo("add_node_used"));
46444678
ADD_SIGNAL(MethodInfo("node_created", PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
46454679
ADD_SIGNAL(MethodInfo("nodes_reparented", PropertyInfo(Variant::ARRAY, "nodes"), PropertyInfo(Variant::NODE_PATH, "new_parent")));
4680+
ADD_SIGNAL(MethodInfo("node_created_type", PropertyInfo(Variant::OBJECT, "nodes"), PropertyInfo(Variant::STRING, "type"), PropertyInfo(Variant::BOOL, "is_custom_type"), PropertyInfo(Variant::STRING, "weird_type")));
4681+
ADD_SIGNAL(MethodInfo("scenes_instantiated", PropertyInfo(Variant::OBJECT, "parent"), PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths"), PropertyInfo(Variant::INT, "index")));
46464682
}
46474683

46484684
SceneTreeDock *SceneTreeDock::singleton = nullptr;

editor/scene_tree_dock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ class SceneTreeDock : public VBoxContainer {
268268
void _filter_option_selected(int option);
269269
void _append_filter_options_to(PopupMenu *p_menu, bool p_include_separator = true);
270270

271-
void _perform_instantiate_scenes(const Vector<String> &p_files, Node *p_parent, int p_pos);
272271
void _perform_create_audio_stream_players(const Vector<String> &p_files, Node *p_parent, int p_pos);
273272
void _replace_with_branch_scene(const String &p_file, Node *base);
274273

@@ -304,6 +303,7 @@ class SceneTreeDock : public VBoxContainer {
304303
static SceneTreeDock *singleton;
305304

306305
public:
306+
void _perform_instantiate_scenes(const Vector<String> &p_files, Node *p_parent, int p_pos, bool should_emit = true);
307307
static SceneTreeDock *get_singleton() { return singleton; }
308308

309309
protected:

modules/multi_godot/multi_godot.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void MultiGodot::_bind_methods() {
3434
ClassDB::bind_method(D_METHOD("_on_p2p_session_request", "remote_id"), &MultiGodot::_on_p2p_session_request);
3535
ClassDB::bind_method(D_METHOD("_on_p2p_session_connect_fail", "steam_id", "session_error"), &MultiGodot::_on_p2p_session_connect_fail);
3636
ClassDB::bind_method(D_METHOD("_on_nodes_reparented", "nodes", "new_parent"), &MultiGodot::_on_nodes_reparented);
37+
ClassDB::bind_method(D_METHOD("_on_node_created", "node", "type", "is_custom_type", "weird_type"), &MultiGodot::_on_node_created);
38+
ClassDB::bind_method(D_METHOD("_on_scenes_instantiated", "parent", "paths", "index"), &MultiGodot::_on_scenes_instantiated);
3739

3840
// Remote Callables
3941

@@ -52,6 +54,8 @@ void MultiGodot::_bind_methods() {
5254
ClassDB::bind_method(D_METHOD("_apply_action"), &MultiGodot::_apply_action);
5355
ClassDB::bind_method(D_METHOD("_instantiate_resource", "node_path", "resource_path", "type"), &MultiGodot::_instantiate_resource);
5456
ClassDB::bind_method(D_METHOD("_reparent_nodes", "old", "new"), &MultiGodot::_reparent_nodes);
57+
ClassDB::bind_method(D_METHOD("_create_node", "parent_path", "type", "is_custom_type", "weird_type"), &MultiGodot::_create_node);
58+
ClassDB::bind_method(D_METHOD("_instantiate_scenes", "parent_path", "paths", "index"), &MultiGodot::_instantiate_scenes);
5559

5660
// Button signals
5761

@@ -118,7 +122,11 @@ void MultiGodot::_ready() {
118122

119123
filesystem_scanner.start(_threaded_filesystem_scanner, this);
120124

121-
SceneTreeDock::get_singleton()->connect("nodes_reparented", Callable(this, "_on_nodes_reparented"));
125+
SceneTreeDock *scene_tree_dock = SceneTreeDock::get_singleton();
126+
127+
scene_tree_dock->connect("nodes_reparented", Callable(this, "_on_nodes_reparented"));
128+
scene_tree_dock->connect("node_created_type", Callable(this, "_on_node_created"));
129+
scene_tree_dock->connect("scenes_instantiated", Callable(this, "_on_scenes_instantiated"));
122130
button_notifier->connect("editor_tab_changed", Callable(this, "_on_editor_tab_changed"));
123131
button_notifier->connect("current_script_path_changed", Callable(this, "_on_current_script_path_changed"));
124132
steam->connect("lobby_created", Callable(this, "_on_lobby_created"));
@@ -1125,6 +1133,35 @@ void MultiGodot::_reparent_nodes(Array paths, String new_parent_path) {
11251133
}
11261134
}
11271135

1136+
void MultiGodot::_create_node(String parent_path, String type, bool is_custom_type, String weird_type) {
1137+
Node *root = EditorNode::get_singleton()->get_edited_scene();
1138+
Node *parent = root->get_node(parent_path);
1139+
1140+
Variant obj;
1141+
if (is_custom_type) {
1142+
if (ScriptServer::is_global_class(type)) {
1143+
obj = EditorNode::get_editor_data().script_class_instance(type);
1144+
Node *n = Object::cast_to<Node>(obj);
1145+
if (n) {
1146+
n->set_name(type);
1147+
}
1148+
} else {
1149+
obj = EditorNode::get_editor_data().instantiate_custom_type(weird_type, type);
1150+
}
1151+
} else {
1152+
obj = ClassDB::instantiate(type);
1153+
}
1154+
1155+
obj.operator Object *();
1156+
Node *node = Object::cast_to<Node>(obj);
1157+
parent->add_child(node, true);
1158+
}
1159+
1160+
void MultiGodot::_instantiate_scenes(String parent_path, Vector<String> paths, int index) {
1161+
Node *root = EditorNode::get_singleton()->get_edited_scene();
1162+
SceneTreeDock::get_singleton()->_perform_instantiate_scenes(paths, root->get_node(parent_path), index, false);
1163+
}
1164+
11281165
// SIGNALS
11291166

11301167
void MultiGodot::_on_lobby_created(int connect, uint64_t this_lobby_id) {
@@ -1290,6 +1327,22 @@ void MultiGodot::_on_nodes_reparented(Array nodes, NodePath new_parent) {
12901327
_call_func(this, "_reparent_nodes", {nodes, new_parent});
12911328
}
12921329

1330+
void MultiGodot::_on_node_created(Node *node, String type, bool is_custom_type, String weird_type) {
1331+
if (VERBOSE_DEBUG) {
1332+
print_line("Node created with type " + type);
1333+
}
1334+
Node *root = EditorNode::get_singleton()->get_edited_scene();
1335+
_call_func(this, "_create_node", {root->get_path_to(node->get_parent()), type, is_custom_type, weird_type});
1336+
}
1337+
1338+
void MultiGodot::_on_scenes_instantiated(Node *parent, Vector<String> paths, int index) {
1339+
if (VERBOSE_DEBUG) {
1340+
print_line("Some scenes were instantiated");
1341+
}
1342+
Node *root = EditorNode::get_singleton()->get_edited_scene();
1343+
_call_func(this, "_instantiate_scenes", {root->get_path_to(parent), paths, index});
1344+
}
1345+
12931346
// PLUGIN
12941347

12951348
MultiGodotPlugin::MultiGodotPlugin() {

modules/multi_godot/multi_godot.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class MultiGodot : public Node2D {
131131
void _apply_action(String node_path, String property_path, Variant value);
132132
void _instantiate_resource(String node_path, String resource_path, String type);
133133
void _reparent_nodes(Array paths, String new_path);
134+
void _create_node(String parent_path, String type, bool is_custom_type, String weird_type);
135+
void _instantiate_scenes(String parent_path, Vector<String> paths, int index);
134136

135137
// SIGNALS
136138

@@ -143,6 +145,8 @@ class MultiGodot : public Node2D {
143145
void _on_editor_tab_changed(int index);
144146
void _on_current_script_path_changed(String path);
145147
void _on_nodes_reparented(Array nodes, NodePath new_parent);
148+
void _on_node_created(Node *node, String type, bool is_custom_type, String weird_type);
149+
void _on_scenes_instantiated(Node *parent, Vector<String> paths, int index);
146150

147151
public:
148152
MultiGodot();

0 commit comments

Comments
 (0)