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