| 1 | /**************************************************************************/ |
| 2 | /* replication_editor.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "replication_editor.h" |
| 32 | |
| 33 | #include "../multiplayer_synchronizer.h" |
| 34 | |
| 35 | #include "editor/editor_node.h" |
| 36 | #include "editor/editor_string_names.h" |
| 37 | #include "editor/editor_undo_redo_manager.h" |
| 38 | #include "editor/inspector/property_selector.h" |
| 39 | #include "editor/scene/scene_tree_editor.h" |
| 40 | #include "editor/settings/editor_settings.h" |
| 41 | #include "editor/themes/editor_scale.h" |
| 42 | #include "editor/themes/editor_theme_manager.h" |
| 43 | #include "scene/gui/dialogs.h" |
| 44 | #include "scene/gui/line_edit.h" |
| 45 | #include "scene/gui/separator.h" |
| 46 | #include "scene/gui/tree.h" |
| 47 | |
| 48 | void ReplicationEditor::_pick_node_filter_text_changed(const String &p_newtext) { |
| 49 | TreeItem *root_item = pick_node->get_scene_tree()->get_scene_tree()->get_root(); |
| 50 | |
| 51 | Vector<Node *> select_candidates; |
| 52 | Node *to_select = nullptr; |
| 53 | |
| 54 | String filter = pick_node->get_filter_line_edit()->get_text(); |
| 55 | |
| 56 | _pick_node_select_recursive(root_item, filter, select_candidates); |
| 57 | |
| 58 | if (!select_candidates.is_empty()) { |
| 59 | for (int i = 0; i < select_candidates.size(); ++i) { |
| 60 | Node *candidate = select_candidates[i]; |
| 61 | |
| 62 | if (((String)candidate->get_name()).to_lower().begins_with(filter.to_lower())) { |
| 63 | to_select = candidate; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (!to_select) { |
| 69 | to_select = select_candidates[0]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pick_node->get_scene_tree()->set_selected(to_select); |
| 74 | } |
| 75 | |
| 76 | void ReplicationEditor::_pick_node_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates) { |
| 77 | if (!p_item) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | NodePath np = p_item->get_metadata(0); |
| 82 | Node *node = get_node(np); |
| 83 | |
| 84 | if (!p_filter.is_empty() && ((String)node->get_name()).containsn(p_filter)) { |
| 85 | p_select_candidates.push_back(node); |
| 86 | } |
| 87 | |
| 88 | TreeItem *c = p_item->get_first_child(); |
| 89 | |
| 90 | while (c) { |
| 91 | _pick_node_select_recursive(c, p_filter, p_select_candidates); |
| 92 | c = c->get_next(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void ReplicationEditor::_pick_node_selected(NodePath p_path) { |
| 97 | Node *root = current->get_node(current->get_root_path()); |
| 98 | ERR_FAIL_NULL(root); |
| 99 | Node *node = get_node(p_path); |
| 100 | ERR_FAIL_NULL(node); |
| 101 | NodePath path_to = root->get_path_to(node); |
| 102 | adding_node_path = path_to; |
| 103 | prop_selector->select_property_from_instance(node); |
| 104 | } |
| 105 | |
| 106 | void ReplicationEditor::_pick_new_property() { |
| 107 | if (current == nullptr) { |
| 108 | EditorNode::get_singleton()->show_warning(TTRC("Select a replicator node in order to pick a property to add to it.")); |
| 109 | return; |
| 110 | } |
| 111 | Node *root = current->get_node(current->get_root_path()); |
| 112 | if (!root) { |
| 113 | EditorNode::get_singleton()->show_warning(TTRC("Not possible to add a new property to synchronize without a root.")); |
| 114 | return; |
| 115 | } |
| 116 | pick_node->popup_scenetree_dialog(nullptr, current); |
| 117 | pick_node->get_filter_line_edit()->clear(); |
| 118 | pick_node->get_filter_line_edit()->grab_focus(); |
| 119 | } |
| 120 | |
| 121 | void ReplicationEditor::_add_sync_property(String p_path) { |
| 122 | config = current->get_replication_config(); |
| 123 | |
| 124 | if (config.is_valid() && config->has_property(p_path)) { |
| 125 | EditorNode::get_singleton()->show_warning(TTRC("Property is already being synchronized.")); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 130 | undo_redo->create_action(TTR("Add property to synchronizer")); |
| 131 | |
| 132 | if (config.is_null()) { |
| 133 | config.instantiate(); |
| 134 | current->set_replication_config(config); |
| 135 | undo_redo->add_do_method(current, "set_replication_config", config); |
| 136 | undo_redo->add_undo_method(current, "set_replication_config", Ref<SceneReplicationConfig>()); |
| 137 | _update_config(); |
| 138 | } |
| 139 | |
| 140 | undo_redo->add_do_method(config.ptr(), "add_property", p_path); |
| 141 | undo_redo->add_undo_method(config.ptr(), "remove_property", p_path); |
| 142 | undo_redo->add_do_method(this, "_update_config"); |
| 143 | undo_redo->add_undo_method(this, "_update_config"); |
| 144 | undo_redo->commit_action(); |
| 145 | } |
| 146 | |
| 147 | void ReplicationEditor::_pick_node_property_selected(String p_name) { |
| 148 | String adding_prop_path = String(adding_node_path) + ":" + p_name; |
| 149 | |
| 150 | _add_sync_property(adding_prop_path); |
| 151 | } |
| 152 | |
| 153 | /// ReplicationEditor |
| 154 | ReplicationEditor::ReplicationEditor() { |
| 155 | set_v_size_flags(SIZE_EXPAND_FILL); |
| 156 | set_custom_minimum_size(Size2(0, 200) * EDSCALE); |
| 157 | |
| 158 | delete_dialog = memnew(ConfirmationDialog); |
| 159 | delete_dialog->connect("canceled", callable_mp(this, &ReplicationEditor::_dialog_closed).bind(false)); |
| 160 | delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ReplicationEditor::_dialog_closed).bind(true)); |
| 161 | add_child(delete_dialog); |
| 162 | |
| 163 | VBoxContainer *vb = memnew(VBoxContainer); |
| 164 | vb->set_v_size_flags(SIZE_EXPAND_FILL); |
| 165 | add_child(vb); |
| 166 | |
| 167 | pick_node = memnew(SceneTreeDialog); |
| 168 | add_child(pick_node); |
| 169 | pick_node->set_title(TTRC("Pick a node to synchronize:")); |
| 170 | pick_node->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_selected)); |
| 171 | pick_node->get_filter_line_edit()->connect(SceneStringName(text_changed), callable_mp(this, &ReplicationEditor::_pick_node_filter_text_changed)); |
| 172 | |
| 173 | prop_selector = memnew(PropertySelector); |
| 174 | add_child(prop_selector); |
| 175 | // Filter out properties that cannot be synchronized. |
| 176 | // * RIDs do not match across network. |
| 177 | // * Objects are too large for replication. |
| 178 | Vector<Variant::Type> types = { |
| 179 | Variant::BOOL, |
| 180 | Variant::INT, |
| 181 | Variant::FLOAT, |
| 182 | Variant::STRING, |
| 183 | |
| 184 | Variant::VECTOR2, |
| 185 | Variant::VECTOR2I, |
| 186 | Variant::RECT2, |
| 187 | Variant::RECT2I, |
| 188 | Variant::VECTOR3, |
| 189 | Variant::VECTOR3I, |
| 190 | Variant::TRANSFORM2D, |
| 191 | Variant::VECTOR4, |
| 192 | Variant::VECTOR4I, |
| 193 | Variant::PLANE, |
| 194 | Variant::QUATERNION, |
| 195 | Variant::AABB, |
| 196 | Variant::BASIS, |
| 197 | Variant::TRANSFORM3D, |
| 198 | Variant::PROJECTION, |
| 199 | |
| 200 | Variant::COLOR, |
| 201 | Variant::STRING_NAME, |
| 202 | Variant::NODE_PATH, |
| 203 | // Variant::RID, |
| 204 | // Variant::OBJECT, |
| 205 | Variant::SIGNAL, |
| 206 | Variant::DICTIONARY, |
| 207 | Variant::ARRAY, |
| 208 | |
| 209 | Variant::PACKED_BYTE_ARRAY, |
| 210 | Variant::PACKED_INT32_ARRAY, |
| 211 | Variant::PACKED_INT64_ARRAY, |
| 212 | Variant::PACKED_FLOAT32_ARRAY, |
| 213 | Variant::PACKED_FLOAT64_ARRAY, |
| 214 | Variant::PACKED_STRING_ARRAY, |
| 215 | Variant::PACKED_VECTOR2_ARRAY, |
| 216 | Variant::PACKED_VECTOR3_ARRAY, |
| 217 | Variant::PACKED_COLOR_ARRAY, |
| 218 | Variant::PACKED_VECTOR4_ARRAY, |
| 219 | }; |
| 220 | prop_selector->set_type_filter(types); |
| 221 | prop_selector->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_property_selected)); |
| 222 | |
| 223 | HBoxContainer *hb = memnew(HBoxContainer); |
| 224 | vb->add_child(hb); |
| 225 | |
| 226 | add_pick_button = memnew(Button(TTRC("Add property to sync..."))); |
| 227 | add_pick_button->connect(SceneStringName(pressed), callable_mp(this, &ReplicationEditor::_pick_new_property)); |
| 228 | hb->add_child(add_pick_button); |
| 229 | |
| 230 | VSeparator *vs = memnew(VSeparator); |
| 231 | vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0)); |
| 232 | hb->add_child(vs); |
| 233 | hb->add_child(memnew(Label(TTRC("Path:")))); |
| 234 | |
| 235 | np_line_edit = memnew(LineEdit); |
| 236 | np_line_edit->set_placeholder(":property"); |
| 237 | np_line_edit->set_accessibility_name(TTRC("Path:")); |
| 238 | np_line_edit->set_h_size_flags(SIZE_EXPAND_FILL); |
| 239 | np_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ReplicationEditor::_np_text_submitted)); |
| 240 | hb->add_child(np_line_edit); |
| 241 | |
| 242 | add_from_path_button = memnew(Button(TTRC("Add from path"))); |
| 243 | add_from_path_button->connect(SceneStringName(pressed), callable_mp(this, &ReplicationEditor::_add_pressed)); |
| 244 | hb->add_child(add_from_path_button); |
| 245 | |
| 246 | vs = memnew(VSeparator); |
| 247 | vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0)); |
| 248 | hb->add_child(vs); |
| 249 | |
| 250 | pin = memnew(Button); |
| 251 | pin->set_theme_type_variation(SceneStringName(FlatButton)); |
| 252 | pin->set_toggle_mode(true); |
| 253 | pin->set_tooltip_text(TTRC("Pin replication editor")); |
| 254 | hb->add_child(pin); |
| 255 | |
| 256 | tree = memnew(Tree); |
| 257 | tree->set_hide_root(true); |
| 258 | tree->set_columns(4); |
| 259 | tree->set_column_titles_visible(true); |
| 260 | tree->set_column_title(0, TTRC("Properties")); |
| 261 | tree->set_column_expand(0, true); |
| 262 | tree->set_column_title(1, TTRC("Spawn")); |
| 263 | tree->set_column_expand(1, false); |
| 264 | tree->set_column_custom_minimum_width(1, 100); |
| 265 | tree->set_column_title(2, TTRC("Replicate")); |
| 266 | tree->set_column_custom_minimum_width(2, 100); |
| 267 | tree->set_column_expand(2, false); |
| 268 | tree->set_column_expand(3, false); |
| 269 | tree->create_item(); |
| 270 | tree->connect("button_clicked", callable_mp(this, &ReplicationEditor::_tree_button_pressed)); |
| 271 | tree->connect("item_edited", callable_mp(this, &ReplicationEditor::_tree_item_edited)); |
| 272 | tree->set_v_size_flags(SIZE_EXPAND_FILL); |
| 273 | vb->add_child(tree); |
| 274 | |
| 275 | drop_label = memnew(Label(TTRC("Add properties using the options above, or\ndrag them from the inspector and drop them here."))); |
| 276 | drop_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 277 | drop_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 278 | drop_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 279 | tree->add_child(drop_label); |
| 280 | drop_label->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 281 | |
| 282 | SET_DRAG_FORWARDING_CDU(tree, ReplicationEditor); |
| 283 | } |
| 284 | |
| 285 | void ReplicationEditor::_bind_methods() { |
| 286 | ClassDB::bind_method(D_METHOD("_update_config"), &ReplicationEditor::_update_config); |
| 287 | ClassDB::bind_method(D_METHOD("_update_value", "property", "column", "value"), &ReplicationEditor::_update_value); |
| 288 | } |
| 289 | |
| 290 | bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const { |
| 291 | Dictionary d = p_data; |
| 292 | if (!d.has("type")) { |
| 293 | return false; |
| 294 | } |
| 295 | String t = d["type"]; |
| 296 | if (t != "obj_property") { |
| 297 | return false; |
| 298 | } |
| 299 | Object *obj = d["object"]; |
| 300 | if (!obj) { |
| 301 | return false; |
| 302 | } |
| 303 | Node *node = Object::cast_to<Node>(obj); |
| 304 | if (!node) { |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) { |
| 312 | if (current == nullptr) { |
| 313 | EditorNode::get_singleton()->show_warning(TTRC("Select a replicator node in order to pick a property to add to it.")); |
| 314 | return; |
| 315 | } |
| 316 | Node *root = current->get_node(current->get_root_path()); |
| 317 | if (!root) { |
| 318 | EditorNode::get_singleton()->show_warning(TTRC("Not possible to add a new property to synchronize without a root.")); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | Dictionary d = p_data; |
| 323 | if (!d.has("type")) { |
| 324 | return; |
| 325 | } |
| 326 | String t = d["type"]; |
| 327 | if (t != "obj_property") { |
| 328 | return; |
| 329 | } |
| 330 | Object *obj = d["object"]; |
| 331 | if (!obj) { |
| 332 | return; |
| 333 | } |
| 334 | Node *node = Object::cast_to<Node>(obj); |
| 335 | if (!node) { |
| 336 | return; |
| 337 | } |
| 338 | |
| 339 | String path = String(root->get_path_to(node)); |
| 340 | path += ":" + String(d["property"]); |
| 341 | |
| 342 | _add_sync_property(path); |
| 343 | } |
| 344 | |
| 345 | void _set_replication_mode_options(TreeItem *p_item) { |
| 346 | p_item->set_text(2, TTR("Never", "Replication Mode") + "," + TTR("Always", "Replication Mode") + "," + TTR("On Change", "Replication Mode")); |
| 347 | } |
| 348 | |
| 349 | void ReplicationEditor::_notification(int p_what) { |
| 350 | switch (p_what) { |
| 351 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 352 | TreeItem *root = tree->get_root(); |
| 353 | if (root) { |
| 354 | for (TreeItem *ti = root->get_first_child(); ti; ti = ti->get_next()) { |
| 355 | _set_replication_mode_options(ti); |
| 356 | } |
| 357 | } |
| 358 | } break; |
| 359 | |
| 360 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
| 361 | if (!EditorThemeManager::is_generated_theme_outdated()) { |
| 362 | break; |
| 363 | } |
| 364 | [[fallthrough]]; |
| 365 | } |
| 366 | case NOTIFICATION_ENTER_TREE: { |
| 367 | add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SceneStringName(panel), SNAME("Panel"))); |
| 368 | add_pick_button->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons))); |
| 369 | pin->set_button_icon(get_theme_icon(SNAME("Pin"), EditorStringName(EditorIcons))); |
| 370 | } break; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void ReplicationEditor::_add_pressed() { |
| 375 | if (!current) { |
| 376 | EditorNode::get_singleton()->show_warning(TTRC("Please select a MultiplayerSynchronizer first.")); |
| 377 | return; |
| 378 | } |
| 379 | if (current->get_root_path().is_empty()) { |
| 380 | EditorNode::get_singleton()->show_warning(TTRC("The MultiplayerSynchronizer needs a root path.")); |
| 381 | return; |
| 382 | } |
| 383 | String np_text = np_line_edit->get_text(); |
| 384 | |
| 385 | if (np_text.is_empty()) { |
| 386 | EditorNode::get_singleton()->show_warning(TTRC("Property/path must not be empty.")); |
| 387 | return; |
| 388 | } |
| 389 | |
| 390 | int idx = np_text.find_char(':'); |
| 391 | if (idx == -1) { |
| 392 | np_text = ".:" + np_text; |
| 393 | } else if (idx == 0) { |
| 394 | np_text = "." + np_text; |
| 395 | } |
| 396 | NodePath path = NodePath(np_text); |
| 397 | if (path.is_empty()) { |
| 398 | EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid property path: '%s'"), np_text)); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | _add_sync_property(String(path)); |
| 403 | } |
| 404 | |
| 405 | void ReplicationEditor::_np_text_submitted(const String &p_newtext) { |
| 406 | _add_pressed(); |
| 407 | } |
| 408 | |
| 409 | void ReplicationEditor::_tree_item_edited() { |
| 410 | TreeItem *ti = tree->get_edited(); |
| 411 | if (!ti || config.is_null()) { |
| 412 | return; |
| 413 | } |
| 414 | int column = tree->get_edited_column(); |
| 415 | ERR_FAIL_COND(column < 1 || column > 2); |
| 416 | const NodePath prop = ti->get_metadata(0); |
| 417 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 418 | |
| 419 | if (column == 1) { |
| 420 | undo_redo->create_action(TTR("Set spawn property")); |
| 421 | bool value = ti->is_checked(column); |
| 422 | undo_redo->add_do_method(config.ptr(), "property_set_spawn", prop, value); |
| 423 | undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, !value); |
| 424 | undo_redo->add_do_method(this, "_update_value", prop, column, value ? 1 : 0); |
| 425 | undo_redo->add_undo_method(this, "_update_value", prop, column, value ? 0 : 1); |
| 426 | undo_redo->commit_action(); |
| 427 | } else if (column == 2) { |
| 428 | undo_redo->create_action(TTR("Set sync property")); |
| 429 | int value = ti->get_range(column); |
| 430 | int old_value = config->property_get_replication_mode(prop); |
| 431 | // We have a hard limit of 64 watchable properties per synchronizer. |
| 432 | if (value == SceneReplicationConfig::REPLICATION_MODE_ON_CHANGE && config->get_watch_properties().size() >= 64) { |
| 433 | EditorNode::get_singleton()->show_warning(TTRC("Each MultiplayerSynchronizer can have no more than 64 watched properties.")); |
| 434 | ti->set_range(column, old_value); |
| 435 | return; |
| 436 | } |
| 437 | undo_redo->add_do_method(config.ptr(), "property_set_replication_mode", prop, value); |
| 438 | undo_redo->add_undo_method(config.ptr(), "property_set_replication_mode", prop, old_value); |
| 439 | undo_redo->add_do_method(this, "_update_value", prop, column, value); |
| 440 | undo_redo->add_undo_method(this, "_update_value", prop, column, old_value); |
| 441 | undo_redo->commit_action(); |
| 442 | } else { |
| 443 | ERR_FAIL(); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void ReplicationEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) { |
| 448 | if (p_button != MouseButton::LEFT) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | TreeItem *ti = Object::cast_to<TreeItem>(p_item); |
| 453 | if (!ti) { |
| 454 | return; |
| 455 | } |
| 456 | deleting = ti->get_metadata(0); |
| 457 | delete_dialog->set_text(TTR("Delete Property?") + "\n\"" + ti->get_text(0) + "\""); |
| 458 | delete_dialog->popup_centered(); |
| 459 | } |
| 460 | |
| 461 | void ReplicationEditor::_dialog_closed(bool p_confirmed) { |
| 462 | if (deleting.is_empty() || config.is_null()) { |
| 463 | return; |
| 464 | } |
| 465 | if (p_confirmed) { |
| 466 | const NodePath prop = deleting; |
| 467 | int idx = config->property_get_index(prop); |
| 468 | bool spawn = config->property_get_spawn(prop); |
| 469 | SceneReplicationConfig::ReplicationMode mode = config->property_get_replication_mode(prop); |
| 470 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
| 471 | undo_redo->create_action(TTR("Remove Property")); |
| 472 | undo_redo->add_do_method(config.ptr(), "remove_property", prop); |
| 473 | undo_redo->add_undo_method(config.ptr(), "add_property", prop, idx); |
| 474 | undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, spawn); |
| 475 | undo_redo->add_undo_method(config.ptr(), "property_set_replication_mode", prop, mode); |
| 476 | undo_redo->add_do_method(this, "_update_config"); |
| 477 | undo_redo->add_undo_method(this, "_update_config"); |
| 478 | undo_redo->commit_action(); |
| 479 | } |
| 480 | deleting = NodePath(); |
| 481 | } |
| 482 | |
| 483 | void ReplicationEditor::_update_value(const NodePath &p_prop, int p_column, int p_value) { |
| 484 | if (!tree->get_root()) { |
| 485 | return; |
| 486 | } |
| 487 | TreeItem *ti = tree->get_root()->get_first_child(); |
| 488 | while (ti) { |
| 489 | if (ti->get_metadata(0).operator NodePath() == p_prop) { |
| 490 | if (p_column == 1) { |
| 491 | ti->set_checked(p_column, p_value != 0); |
| 492 | } else if (p_column == 2) { |
| 493 | ti->set_range(p_column, p_value); |
| 494 | } |
| 495 | return; |
| 496 | } |
| 497 | ti = ti->get_next(); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | void ReplicationEditor::_update_config() { |
| 502 | deleting = NodePath(); |
| 503 | tree->clear(); |
| 504 | tree->create_item(); |
| 505 | drop_label->set_visible(true); |
| 506 | if (config.is_null()) { |
| 507 | return; |
| 508 | } |
| 509 | TypedArray<NodePath> props = config->get_properties(); |
| 510 | if (props.size()) { |
| 511 | drop_label->set_visible(false); |
| 512 | } |
| 513 | for (int i = 0; i < props.size(); i++) { |
| 514 | const NodePath path = props[i]; |
| 515 | _add_property(path, config->property_get_spawn(path), config->property_get_replication_mode(path)); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void ReplicationEditor::edit(MultiplayerSynchronizer *p_sync) { |
| 520 | if (current == p_sync) { |
| 521 | return; |
| 522 | } |
| 523 | current = p_sync; |
| 524 | if (current) { |
| 525 | config = current->get_replication_config(); |
| 526 | } else { |
| 527 | config.unref(); |
| 528 | } |
| 529 | _update_config(); |
| 530 | } |
| 531 | |
| 532 | Ref<Texture2D> ReplicationEditor::_get_class_icon(const Node *p_node) { |
| 533 | if (!p_node || !has_theme_icon(p_node->get_class(), EditorStringName(EditorIcons))) { |
| 534 | return get_theme_icon(SNAME("ImportFail"), EditorStringName(EditorIcons)); |
| 535 | } |
| 536 | return get_theme_icon(p_node->get_class(), EditorStringName(EditorIcons)); |
| 537 | } |
| 538 | |
| 539 | static bool can_sync(const Variant &p_var) { |
| 540 | switch (p_var.get_type()) { |
| 541 | case Variant::RID: |
| 542 | case Variant::OBJECT: |
| 543 | return false; |
| 544 | case Variant::ARRAY: { |
| 545 | const Array &arr = p_var; |
| 546 | if (arr.is_typed()) { |
| 547 | const uint32_t type = arr.get_typed_builtin(); |
| 548 | return (type != Variant::RID) && (type != Variant::OBJECT); |
| 549 | } |
| 550 | return true; |
| 551 | } |
| 552 | default: |
| 553 | return true; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void ReplicationEditor::_add_property(const NodePath &p_property, bool p_spawn, SceneReplicationConfig::ReplicationMode p_mode) { |
| 558 | String prop = String(p_property); |
| 559 | TreeItem *item = tree->create_item(); |
| 560 | item->set_selectable(0, false); |
| 561 | item->set_selectable(1, false); |
| 562 | item->set_selectable(2, false); |
| 563 | item->set_selectable(3, false); |
| 564 | item->set_text(0, prop); |
| 565 | item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); |
| 566 | item->set_metadata(0, prop); |
| 567 | Node *root_node = current && !current->get_root_path().is_empty() ? current->get_node(current->get_root_path()) : nullptr; |
| 568 | Ref<Texture2D> icon = _get_class_icon(root_node); |
| 569 | if (root_node) { |
| 570 | String path = prop.substr(0, prop.find_char(':')); |
| 571 | String subpath = prop.substr(path.size()); |
| 572 | Node *node = root_node->get_node_or_null(path); |
| 573 | if (!node) { |
| 574 | node = root_node; |
| 575 | } |
| 576 | item->set_text(0, String(node->get_name()) + ":" + subpath); |
| 577 | icon = _get_class_icon(node); |
| 578 | bool valid = false; |
| 579 | Variant value = node->get(subpath, &valid); |
| 580 | if (valid && !can_sync(value)) { |
| 581 | item->set_icon(0, get_theme_icon(SNAME("StatusWarning"), EditorStringName(EditorIcons))); |
| 582 | item->set_tooltip_text(0, TTRC("Property of this type not supported.")); |
| 583 | } else { |
| 584 | item->set_icon(0, icon); |
| 585 | } |
| 586 | } else { |
| 587 | item->set_icon(0, icon); |
| 588 | } |
| 589 | item->add_button(3, get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons))); |
| 590 | item->set_text_alignment(1, HORIZONTAL_ALIGNMENT_CENTER); |
| 591 | item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK); |
| 592 | item->set_checked(1, p_spawn); |
| 593 | item->set_editable(1, true); |
| 594 | item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_CENTER); |
| 595 | item->set_cell_mode(2, TreeItem::CELL_MODE_RANGE); |
| 596 | item->set_range_config(2, 0, 2, 1); |
| 597 | item->set_auto_translate_mode(2, AUTO_TRANSLATE_MODE_DISABLED); |
| 598 | _set_replication_mode_options(item); |
| 599 | item->set_range(2, (int)p_mode); |
| 600 | item->set_editable(2, true); |
| 601 | } |