Path: blob/master/modules/multiplayer/editor/replication_editor.cpp
10278 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(TTR("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(TTR("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(TTR("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(TTR("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);226add_pick_button->connect(SceneStringName(pressed), callable_mp(this, &ReplicationEditor::_pick_new_property));227add_pick_button->set_text(TTR("Add property to sync..."));228hb->add_child(add_pick_button);229230VSeparator *vs = memnew(VSeparator);231vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));232hb->add_child(vs);233hb->add_child(memnew(Label(TTR("Path:"))));234235np_line_edit = memnew(LineEdit);236np_line_edit->set_placeholder(":property");237np_line_edit->set_accessibility_name(TTRC("Path:"));238np_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);239np_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &ReplicationEditor::_np_text_submitted));240hb->add_child(np_line_edit);241242add_from_path_button = memnew(Button);243add_from_path_button->connect(SceneStringName(pressed), callable_mp(this, &ReplicationEditor::_add_pressed));244add_from_path_button->set_text(TTR("Add from path"));245hb->add_child(add_from_path_button);246247vs = memnew(VSeparator);248vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));249hb->add_child(vs);250251pin = memnew(Button);252pin->set_theme_type_variation(SceneStringName(FlatButton));253pin->set_toggle_mode(true);254pin->set_tooltip_text(TTR("Pin replication editor"));255hb->add_child(pin);256257tree = memnew(Tree);258tree->set_hide_root(true);259tree->set_columns(4);260tree->set_column_titles_visible(true);261tree->set_column_title(0, TTR("Properties"));262tree->set_column_expand(0, true);263tree->set_column_title(1, TTR("Spawn"));264tree->set_column_expand(1, false);265tree->set_column_custom_minimum_width(1, 100);266tree->set_column_title(2, TTR("Replicate"));267tree->set_column_custom_minimum_width(2, 100);268tree->set_column_expand(2, false);269tree->set_column_expand(3, false);270tree->create_item();271tree->connect("button_clicked", callable_mp(this, &ReplicationEditor::_tree_button_pressed));272tree->connect("item_edited", callable_mp(this, &ReplicationEditor::_tree_item_edited));273tree->set_v_size_flags(SIZE_EXPAND_FILL);274vb->add_child(tree);275276drop_label = memnew(Label);277drop_label->set_focus_mode(FOCUS_ACCESSIBILITY);278drop_label->set_text(TTR("Add properties using the options above, or\ndrag them from the inspector and drop them here."));279drop_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);280drop_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);281tree->add_child(drop_label);282drop_label->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);283284SET_DRAG_FORWARDING_CDU(tree, ReplicationEditor);285}286287void ReplicationEditor::_bind_methods() {288ClassDB::bind_method(D_METHOD("_update_config"), &ReplicationEditor::_update_config);289ClassDB::bind_method(D_METHOD("_update_value", "property", "column", "value"), &ReplicationEditor::_update_value);290}291292bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {293Dictionary d = p_data;294if (!d.has("type")) {295return false;296}297String t = d["type"];298if (t != "obj_property") {299return false;300}301Object *obj = d["object"];302if (!obj) {303return false;304}305Node *node = Object::cast_to<Node>(obj);306if (!node) {307return false;308}309310return true;311}312313void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {314if (current == nullptr) {315EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));316return;317}318Node *root = current->get_node(current->get_root_path());319if (!root) {320EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));321return;322}323324Dictionary d = p_data;325if (!d.has("type")) {326return;327}328String t = d["type"];329if (t != "obj_property") {330return;331}332Object *obj = d["object"];333if (!obj) {334return;335}336Node *node = Object::cast_to<Node>(obj);337if (!node) {338return;339}340341String path = String(root->get_path_to(node));342path += ":" + String(d["property"]);343344_add_sync_property(path);345}346347void ReplicationEditor::_notification(int p_what) {348switch (p_what) {349case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {350if (!EditorThemeManager::is_generated_theme_outdated()) {351break;352}353[[fallthrough]];354}355case NOTIFICATION_ENTER_TREE: {356add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SceneStringName(panel), SNAME("Panel")));357add_pick_button->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));358pin->set_button_icon(get_theme_icon(SNAME("Pin"), EditorStringName(EditorIcons)));359} break;360}361}362363void ReplicationEditor::_add_pressed() {364if (!current) {365EditorNode::get_singleton()->show_warning(TTR("Please select a MultiplayerSynchronizer first."));366return;367}368if (current->get_root_path().is_empty()) {369EditorNode::get_singleton()->show_warning(TTR("The MultiplayerSynchronizer needs a root path."));370return;371}372String np_text = np_line_edit->get_text();373374if (np_text.is_empty()) {375EditorNode::get_singleton()->show_warning(TTR("Property/path must not be empty."));376return;377}378379int idx = np_text.find_char(':');380if (idx == -1) {381np_text = ".:" + np_text;382} else if (idx == 0) {383np_text = "." + np_text;384}385NodePath path = NodePath(np_text);386if (path.is_empty()) {387EditorNode::get_singleton()->show_warning(vformat(TTR("Invalid property path: '%s'"), np_text));388return;389}390391_add_sync_property(String(path));392}393394void ReplicationEditor::_np_text_submitted(const String &p_newtext) {395_add_pressed();396}397398void ReplicationEditor::_tree_item_edited() {399TreeItem *ti = tree->get_edited();400if (!ti || config.is_null()) {401return;402}403int column = tree->get_edited_column();404ERR_FAIL_COND(column < 1 || column > 2);405const NodePath prop = ti->get_metadata(0);406EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();407408if (column == 1) {409undo_redo->create_action(TTR("Set spawn property"));410bool value = ti->is_checked(column);411undo_redo->add_do_method(config.ptr(), "property_set_spawn", prop, value);412undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, !value);413undo_redo->add_do_method(this, "_update_value", prop, column, value ? 1 : 0);414undo_redo->add_undo_method(this, "_update_value", prop, column, value ? 0 : 1);415undo_redo->commit_action();416} else if (column == 2) {417undo_redo->create_action(TTR("Set sync property"));418int value = ti->get_range(column);419int old_value = config->property_get_replication_mode(prop);420// We have a hard limit of 64 watchable properties per synchronizer.421if (value == SceneReplicationConfig::REPLICATION_MODE_ON_CHANGE && config->get_watch_properties().size() >= 64) {422EditorNode::get_singleton()->show_warning(TTR("Each MultiplayerSynchronizer can have no more than 64 watched properties."));423ti->set_range(column, old_value);424return;425}426undo_redo->add_do_method(config.ptr(), "property_set_replication_mode", prop, value);427undo_redo->add_undo_method(config.ptr(), "property_set_replication_mode", prop, old_value);428undo_redo->add_do_method(this, "_update_value", prop, column, value);429undo_redo->add_undo_method(this, "_update_value", prop, column, old_value);430undo_redo->commit_action();431} else {432ERR_FAIL();433}434}435436void ReplicationEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {437if (p_button != MouseButton::LEFT) {438return;439}440441TreeItem *ti = Object::cast_to<TreeItem>(p_item);442if (!ti) {443return;444}445deleting = ti->get_metadata(0);446delete_dialog->set_text(TTR("Delete Property?") + "\n\"" + ti->get_text(0) + "\"");447delete_dialog->popup_centered();448}449450void ReplicationEditor::_dialog_closed(bool p_confirmed) {451if (deleting.is_empty() || config.is_null()) {452return;453}454if (p_confirmed) {455const NodePath prop = deleting;456int idx = config->property_get_index(prop);457bool spawn = config->property_get_spawn(prop);458SceneReplicationConfig::ReplicationMode mode = config->property_get_replication_mode(prop);459EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();460undo_redo->create_action(TTR("Remove Property"));461undo_redo->add_do_method(config.ptr(), "remove_property", prop);462undo_redo->add_undo_method(config.ptr(), "add_property", prop, idx);463undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, spawn);464undo_redo->add_undo_method(config.ptr(), "property_set_replication_mode", prop, mode);465undo_redo->add_do_method(this, "_update_config");466undo_redo->add_undo_method(this, "_update_config");467undo_redo->commit_action();468}469deleting = NodePath();470}471472void ReplicationEditor::_update_value(const NodePath &p_prop, int p_column, int p_value) {473if (!tree->get_root()) {474return;475}476TreeItem *ti = tree->get_root()->get_first_child();477while (ti) {478if (ti->get_metadata(0).operator NodePath() == p_prop) {479if (p_column == 1) {480ti->set_checked(p_column, p_value != 0);481} else if (p_column == 2) {482ti->set_range(p_column, p_value);483}484return;485}486ti = ti->get_next();487}488}489490void ReplicationEditor::_update_config() {491deleting = NodePath();492tree->clear();493tree->create_item();494drop_label->set_visible(true);495if (config.is_null()) {496return;497}498TypedArray<NodePath> props = config->get_properties();499if (props.size()) {500drop_label->set_visible(false);501}502for (int i = 0; i < props.size(); i++) {503const NodePath path = props[i];504_add_property(path, config->property_get_spawn(path), config->property_get_replication_mode(path));505}506}507508void ReplicationEditor::edit(MultiplayerSynchronizer *p_sync) {509if (current == p_sync) {510return;511}512current = p_sync;513if (current) {514config = current->get_replication_config();515} else {516config.unref();517}518_update_config();519}520521Ref<Texture2D> ReplicationEditor::_get_class_icon(const Node *p_node) {522if (!p_node || !has_theme_icon(p_node->get_class(), EditorStringName(EditorIcons))) {523return get_theme_icon(SNAME("ImportFail"), EditorStringName(EditorIcons));524}525return get_theme_icon(p_node->get_class(), EditorStringName(EditorIcons));526}527528static bool can_sync(const Variant &p_var) {529switch (p_var.get_type()) {530case Variant::RID:531case Variant::OBJECT:532return false;533case Variant::ARRAY: {534const Array &arr = p_var;535if (arr.is_typed()) {536const uint32_t type = arr.get_typed_builtin();537return (type != Variant::RID) && (type != Variant::OBJECT);538}539return true;540}541default:542return true;543}544}545546void ReplicationEditor::_add_property(const NodePath &p_property, bool p_spawn, SceneReplicationConfig::ReplicationMode p_mode) {547String prop = String(p_property);548TreeItem *item = tree->create_item();549item->set_selectable(0, false);550item->set_selectable(1, false);551item->set_selectable(2, false);552item->set_selectable(3, false);553item->set_text(0, prop);554item->set_metadata(0, prop);555Node *root_node = current && !current->get_root_path().is_empty() ? current->get_node(current->get_root_path()) : nullptr;556Ref<Texture2D> icon = _get_class_icon(root_node);557if (root_node) {558String path = prop.substr(0, prop.find_char(':'));559String subpath = prop.substr(path.size());560Node *node = root_node->get_node_or_null(path);561if (!node) {562node = root_node;563}564item->set_text(0, String(node->get_name()) + ":" + subpath);565icon = _get_class_icon(node);566bool valid = false;567Variant value = node->get(subpath, &valid);568if (valid && !can_sync(value)) {569item->set_icon(0, get_theme_icon(SNAME("StatusWarning"), EditorStringName(EditorIcons)));570item->set_tooltip_text(0, TTR("Property of this type not supported."));571} else {572item->set_icon(0, icon);573}574} else {575item->set_icon(0, icon);576}577item->add_button(3, get_theme_icon(SNAME("Remove"), EditorStringName(EditorIcons)));578item->set_text_alignment(1, HORIZONTAL_ALIGNMENT_CENTER);579item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);580item->set_checked(1, p_spawn);581item->set_editable(1, true);582item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_CENTER);583item->set_cell_mode(2, TreeItem::CELL_MODE_RANGE);584item->set_range_config(2, 0, 2, 1);585item->set_text(2, TTR("Never", "Replication Mode") + "," + TTR("Always", "Replication Mode") + "," + TTR("On Change", "Replication Mode"));586item->set_range(2, (int)p_mode);587item->set_editable(2, true);588}589590591