Skip to content

Commit 98c7c2f

Browse files
committed
Detect when a node is moved (not sure yet about multiple nodes)
1 parent 8be96fb commit 98c7c2f

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

modules/multi_godot/multi_godot.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void MultiGodot::_bind_methods() {
4949
ClassDB::bind_method(D_METHOD("_set_as_script_owner", "path"), &MultiGodot::_set_as_script_owner);
5050
ClassDB::bind_method(D_METHOD("_apply_action"), &MultiGodot::_apply_action);
5151
ClassDB::bind_method(D_METHOD("_instantiate_resource", "node_path", "resource_path", "type"), &MultiGodot::_instantiate_resource);
52+
ClassDB::bind_method(D_METHOD("_move_node", "old", "new"), &MultiGodot::_move_node);
5253

5354
// Button signals
5455

@@ -603,10 +604,14 @@ void MultiGodot::_sync_colab_scenes() {
603604
selected = scene_tree_editor->get_selected(); if (selected == nullptr) return;
604605
Node *root = EditorNode::get_singleton()->get_edited_scene();
605606

607+
String selected_path = root->get_path_to(selected);
608+
606609
List<PropertyInfo> *property_infos = memnew(List<PropertyInfo>);
607610
selected->get_property_list(property_infos);
608611

609612
if (selected != previous_selected_node) {
613+
last_selected_path = selected_path;
614+
610615
previous_property_names.clear();
611616
previous_property_values.clear();
612617

@@ -616,6 +621,13 @@ void MultiGodot::_sync_colab_scenes() {
616621
return;
617622
}
618623

624+
if (last_selected_path != selected_path) { // Node was moved.
625+
String new_parent_path = root->get_path_to(selected->get_parent());
626+
_call_func(this, "_move_node", {last_selected_path, new_parent_path});
627+
}
628+
629+
last_selected_path = selected_path;
630+
619631
_recurse_node_parameters(root, selected, root->get_path_to(selected));
620632
}
621633

@@ -1025,8 +1037,7 @@ void MultiGodot::_set_as_script_owner(String path) {
10251037
_set_user_data_for_everyone("current_spectating_script", "");
10261038
}
10271039

1028-
void MultiGodot::_apply_action(int type, String node_path, String new_path, String new_name, String property_path,
1029-
Variant new_value) {
1040+
void MultiGodot::_apply_action(int type, String node_path, String new_path, String new_name, String property_path, Variant new_value) {
10301041
if (type == Action::PROPERTY_EDIT) {
10311042
Object *modified_on = EditorNode::get_singleton()->get_edited_scene()->get_node(node_path);
10321043

@@ -1098,6 +1109,27 @@ void MultiGodot::_instantiate_resource(String node_path, String resource_path, S
10981109

10991110
}
11001111

1112+
void MultiGodot::_move_node(String current_path, String new_parent_path) {
1113+
SceneTreeEditor *scene_tree_editor = SceneTreeDock::get_singleton()->get_tree_editor();
1114+
Node *root = EditorNode::get_singleton()->get_edited_scene();
1115+
1116+
Node *moved = root->get_node(current_path);
1117+
if (!moved) {
1118+
print_error("Request to move node but the node at path " + current_path + " does not exist. Was it moved?");
1119+
return;
1120+
}
1121+
1122+
Node *parent = root->get_node(new_parent_path);
1123+
if (!parent) {
1124+
print_error("Request to move a node (that exists) to a parent that doesn't exist at path " + new_parent_path + ". No parents?");
1125+
return;
1126+
}
1127+
1128+
// Remove the child and give it to new parents.
1129+
moved->get_parent()->remove_child(moved);
1130+
parent->add_child(moved);
1131+
}
1132+
11011133
// SIGNALS
11021134

11031135
void MultiGodot::_on_lobby_created(int connect, uint64_t this_lobby_id) {

modules/multi_godot/multi_godot.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class MultiGodot : public Node2D {
3939
MS_OTHER,
4040
};
4141

42+
// CONSTANTS
43+
4244
static const int PACKET_READ_LIMIT = 32;
4345
static const int MAX_MEMBERS = 4; // Possibly increase this in the future if there is a need.
4446
static const int DEFAULT_CHANNEL = 0;
@@ -73,6 +75,7 @@ class MultiGodot : public Node2D {
7375
String live_last_code;
7476
String script_editor_previous_line_text;
7577
String last_scene_data;
78+
String last_selected_path;
7679
Vector<HashMap<String, Variant>> lobby_members;
7780
Vector<String> new_files;
7881
Vector<String> deleted_files; // Both this and new_files will fill up if not cleaned up by the main thread.
@@ -141,9 +144,9 @@ class MultiGodot : public Node2D {
141144
void _rename_file(String from, String to);
142145
void _sync_user_data(uint64_t user_id, Dictionary data);
143146
void _set_as_script_owner(String path);
144-
void _apply_action(int type, String node_path, String new_path, String new_name, String property_path,
145-
Variant value);
147+
void _apply_action(int type, String node_path, String new_path, String new_name, String property_path, Variant value);
146148
void _instantiate_resource(String node_path, String resource_path, String type);
149+
void _move_node(String current_path, String new_path);
147150

148151
// SIGNALS
149152

0 commit comments

Comments
 (0)