replication_editor_plugin.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*************************************************************************/
  2. /* replication_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "replication_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/inspector_dock.h"
  35. #include "editor/scene_tree_editor.h"
  36. #include "modules/multiplayer/multiplayer_synchronizer.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/gui/tree.h"
  40. void ReplicationEditor::_pick_node_filter_text_changed(const String &p_newtext) {
  41. TreeItem *root_item = pick_node->get_scene_tree()->get_scene_tree()->get_root();
  42. Vector<Node *> select_candidates;
  43. Node *to_select = nullptr;
  44. String filter = pick_node->get_filter_line_edit()->get_text();
  45. _pick_node_select_recursive(root_item, filter, select_candidates);
  46. if (!select_candidates.is_empty()) {
  47. for (int i = 0; i < select_candidates.size(); ++i) {
  48. Node *candidate = select_candidates[i];
  49. if (((String)candidate->get_name()).to_lower().begins_with(filter.to_lower())) {
  50. to_select = candidate;
  51. break;
  52. }
  53. }
  54. if (!to_select) {
  55. to_select = select_candidates[0];
  56. }
  57. }
  58. pick_node->get_scene_tree()->set_selected(to_select);
  59. }
  60. void ReplicationEditor::_pick_node_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates) {
  61. if (!p_item) {
  62. return;
  63. }
  64. NodePath np = p_item->get_metadata(0);
  65. Node *node = get_node(np);
  66. if (!p_filter.is_empty() && ((String)node->get_name()).findn(p_filter) != -1) {
  67. p_select_candidates.push_back(node);
  68. }
  69. TreeItem *c = p_item->get_first_child();
  70. while (c) {
  71. _pick_node_select_recursive(c, p_filter, p_select_candidates);
  72. c = c->get_next();
  73. }
  74. }
  75. void ReplicationEditor::_pick_node_filter_input(const Ref<InputEvent> &p_ie) {
  76. Ref<InputEventKey> k = p_ie;
  77. if (k.is_valid()) {
  78. switch (k->get_keycode()) {
  79. case Key::UP:
  80. case Key::DOWN:
  81. case Key::PAGEUP:
  82. case Key::PAGEDOWN: {
  83. pick_node->get_scene_tree()->get_scene_tree()->gui_input(k);
  84. pick_node->get_filter_line_edit()->accept_event();
  85. } break;
  86. default:
  87. break;
  88. }
  89. }
  90. }
  91. void ReplicationEditor::_pick_node_selected(NodePath p_path) {
  92. Node *root = current->get_node(current->get_root_path());
  93. ERR_FAIL_COND(!root);
  94. Node *node = get_node(p_path);
  95. ERR_FAIL_COND(!node);
  96. NodePath path_to = root->get_path_to(node);
  97. adding_node_path = path_to;
  98. prop_selector->select_property_from_instance(node);
  99. }
  100. void ReplicationEditor::_pick_new_property() {
  101. if (current == nullptr) {
  102. EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));
  103. return;
  104. }
  105. Node *root = current->get_node(current->get_root_path());
  106. if (!root) {
  107. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));
  108. return;
  109. }
  110. pick_node->popup_scenetree_dialog();
  111. pick_node->get_filter_line_edit()->clear();
  112. pick_node->get_filter_line_edit()->grab_focus();
  113. }
  114. void ReplicationEditor::_add_sync_property(String p_path) {
  115. config = current->get_replication_config();
  116. if (config.is_valid() && config->has_property(p_path)) {
  117. EditorNode::get_singleton()->show_warning(TTR("Property is already being synchronized."));
  118. return;
  119. }
  120. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  121. undo_redo->create_action(TTR("Add property to synchronizer"));
  122. if (config.is_null()) {
  123. config.instantiate();
  124. current->set_replication_config(config);
  125. undo_redo->add_do_method(current, "set_replication_config", config);
  126. undo_redo->add_undo_method(current, "set_replication_config", Ref<SceneReplicationConfig>());
  127. _update_config();
  128. }
  129. undo_redo->add_do_method(config.ptr(), "add_property", p_path);
  130. undo_redo->add_undo_method(config.ptr(), "remove_property", p_path);
  131. undo_redo->add_do_method(this, "_update_config");
  132. undo_redo->add_undo_method(this, "_update_config");
  133. undo_redo->commit_action();
  134. }
  135. void ReplicationEditor::_pick_node_property_selected(String p_name) {
  136. String adding_prop_path = String(adding_node_path) + ":" + p_name;
  137. _add_sync_property(adding_prop_path);
  138. }
  139. /// ReplicationEditor
  140. ReplicationEditor::ReplicationEditor() {
  141. set_v_size_flags(SIZE_EXPAND_FILL);
  142. set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  143. delete_dialog = memnew(ConfirmationDialog);
  144. delete_dialog->connect("cancelled", callable_mp(this, &ReplicationEditor::_dialog_closed).bind(false));
  145. delete_dialog->connect("confirmed", callable_mp(this, &ReplicationEditor::_dialog_closed).bind(true));
  146. add_child(delete_dialog);
  147. error_dialog = memnew(AcceptDialog);
  148. error_dialog->set_ok_button_text(TTR("Close"));
  149. error_dialog->set_title(TTR("Error!"));
  150. add_child(error_dialog);
  151. VBoxContainer *vb = memnew(VBoxContainer);
  152. vb->set_v_size_flags(SIZE_EXPAND_FILL);
  153. add_child(vb);
  154. pick_node = memnew(SceneTreeDialog);
  155. add_child(pick_node);
  156. pick_node->register_text_enter(pick_node->get_filter_line_edit());
  157. pick_node->set_title(TTR("Pick a node to synchronize:"));
  158. pick_node->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_selected));
  159. pick_node->get_filter_line_edit()->connect("text_changed", callable_mp(this, &ReplicationEditor::_pick_node_filter_text_changed));
  160. pick_node->get_filter_line_edit()->connect("gui_input", callable_mp(this, &ReplicationEditor::_pick_node_filter_input));
  161. prop_selector = memnew(PropertySelector);
  162. add_child(prop_selector);
  163. prop_selector->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_property_selected));
  164. HBoxContainer *hb = memnew(HBoxContainer);
  165. vb->add_child(hb);
  166. add_pick_button = memnew(Button);
  167. add_pick_button->connect("pressed", callable_mp(this, &ReplicationEditor::_pick_new_property));
  168. add_pick_button->set_text(TTR("Add property to sync.."));
  169. hb->add_child(add_pick_button);
  170. VSeparator *vs = memnew(VSeparator);
  171. vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));
  172. hb->add_child(vs);
  173. hb->add_child(memnew(Label(TTR("Path:"))));
  174. np_line_edit = memnew(LineEdit);
  175. np_line_edit->set_placeholder(":property");
  176. np_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  177. hb->add_child(np_line_edit);
  178. add_from_path_button = memnew(Button);
  179. add_from_path_button->connect("pressed", callable_mp(this, &ReplicationEditor::_add_pressed));
  180. add_from_path_button->set_text(TTR("Add from path"));
  181. hb->add_child(add_from_path_button);
  182. vs = memnew(VSeparator);
  183. vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));
  184. hb->add_child(vs);
  185. pin = memnew(Button);
  186. pin->set_flat(true);
  187. pin->set_toggle_mode(true);
  188. hb->add_child(pin);
  189. tree = memnew(Tree);
  190. tree->set_hide_root(true);
  191. tree->set_columns(4);
  192. tree->set_column_titles_visible(true);
  193. tree->set_column_title(0, TTR("Properties"));
  194. tree->set_column_expand(0, true);
  195. tree->set_column_title(1, TTR("Spawn"));
  196. tree->set_column_expand(1, false);
  197. tree->set_column_custom_minimum_width(1, 100);
  198. tree->set_column_title(2, TTR("Sync"));
  199. tree->set_column_custom_minimum_width(2, 100);
  200. tree->set_column_expand(2, false);
  201. tree->set_column_expand(3, false);
  202. tree->create_item();
  203. tree->connect("button_clicked", callable_mp(this, &ReplicationEditor::_tree_button_pressed));
  204. tree->connect("item_edited", callable_mp(this, &ReplicationEditor::_tree_item_edited));
  205. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  206. vb->add_child(tree);
  207. drop_label = memnew(Label);
  208. drop_label->set_text(TTR("Add properties using the buttons above or\ndrag them them from the inspector and drop them here."));
  209. drop_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  210. drop_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  211. tree->add_child(drop_label);
  212. drop_label->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  213. tree->set_drag_forwarding(this);
  214. }
  215. void ReplicationEditor::_bind_methods() {
  216. ClassDB::bind_method(D_METHOD("_update_config"), &ReplicationEditor::_update_config);
  217. ClassDB::bind_method(D_METHOD("_update_checked", "property", "column", "checked"), &ReplicationEditor::_update_checked);
  218. ClassDB::bind_method("_can_drop_data_fw", &ReplicationEditor::_can_drop_data_fw);
  219. ClassDB::bind_method("_drop_data_fw", &ReplicationEditor::_drop_data_fw);
  220. ADD_SIGNAL(MethodInfo("keying_changed"));
  221. }
  222. bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  223. Dictionary d = p_data;
  224. if (!d.has("type")) {
  225. return false;
  226. }
  227. String t = d["type"];
  228. if (t != "obj_property") {
  229. return false;
  230. }
  231. Object *obj = d["object"];
  232. if (!obj) {
  233. return false;
  234. }
  235. Node *node = Object::cast_to<Node>(obj);
  236. if (!node) {
  237. return false;
  238. }
  239. return true;
  240. }
  241. void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  242. if (current == nullptr) {
  243. EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));
  244. return;
  245. }
  246. Node *root = current->get_node(current->get_root_path());
  247. if (!root) {
  248. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));
  249. return;
  250. }
  251. Dictionary d = p_data;
  252. if (!d.has("type")) {
  253. return;
  254. }
  255. String t = d["type"];
  256. if (t != "obj_property") {
  257. return;
  258. }
  259. Object *obj = d["object"];
  260. if (!obj) {
  261. return;
  262. }
  263. Node *node = Object::cast_to<Node>(obj);
  264. if (!node) {
  265. return;
  266. }
  267. String path = root->get_path_to(node);
  268. path += ":" + String(d["property"]);
  269. _add_sync_property(path);
  270. }
  271. void ReplicationEditor::_notification(int p_what) {
  272. switch (p_what) {
  273. case NOTIFICATION_ENTER_TREE:
  274. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  275. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  276. add_pick_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  277. pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")));
  278. } break;
  279. case NOTIFICATION_VISIBILITY_CHANGED: {
  280. update_keying();
  281. } break;
  282. }
  283. }
  284. void ReplicationEditor::_add_pressed() {
  285. if (!current) {
  286. error_dialog->set_text(TTR("Please select a MultiplayerSynchronizer first."));
  287. error_dialog->popup_centered();
  288. return;
  289. }
  290. if (current->get_root_path().is_empty()) {
  291. error_dialog->set_text(TTR("The MultiplayerSynchronizer needs a root path."));
  292. error_dialog->popup_centered();
  293. return;
  294. }
  295. String np_text = np_line_edit->get_text();
  296. if (np_text.find(":") == -1) {
  297. np_text = ":" + np_text;
  298. }
  299. NodePath prop = NodePath(np_text);
  300. if (prop.is_empty()) {
  301. return;
  302. }
  303. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  304. undo_redo->create_action(TTR("Add property"));
  305. config = current->get_replication_config();
  306. if (config.is_null()) {
  307. config.instantiate();
  308. current->set_replication_config(config);
  309. undo_redo->add_do_method(current, "set_replication_config", config);
  310. undo_redo->add_undo_method(current, "set_replication_config", Ref<SceneReplicationConfig>());
  311. _update_config();
  312. }
  313. undo_redo->add_do_method(config.ptr(), "add_property", prop);
  314. undo_redo->add_undo_method(config.ptr(), "remove_property", prop);
  315. undo_redo->add_do_method(this, "_update_config");
  316. undo_redo->add_undo_method(this, "_update_config");
  317. undo_redo->commit_action();
  318. }
  319. void ReplicationEditor::_tree_item_edited() {
  320. TreeItem *ti = tree->get_edited();
  321. if (!ti || config.is_null()) {
  322. return;
  323. }
  324. int column = tree->get_edited_column();
  325. ERR_FAIL_COND(column < 1 || column > 2);
  326. const NodePath prop = ti->get_metadata(0);
  327. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  328. bool value = ti->is_checked(column);
  329. String method;
  330. if (column == 1) {
  331. undo_redo->create_action(TTR("Set spawn property"));
  332. method = "property_set_spawn";
  333. } else {
  334. undo_redo->create_action(TTR("Set sync property"));
  335. method = "property_set_sync";
  336. }
  337. undo_redo->add_do_method(config.ptr(), method, prop, value);
  338. undo_redo->add_undo_method(config.ptr(), method, prop, !value);
  339. undo_redo->add_do_method(this, "_update_checked", prop, column, value);
  340. undo_redo->add_undo_method(this, "_update_checked", prop, column, !value);
  341. undo_redo->commit_action();
  342. }
  343. void ReplicationEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  344. if (p_button != MouseButton::LEFT) {
  345. return;
  346. }
  347. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  348. if (!ti) {
  349. return;
  350. }
  351. deleting = ti->get_metadata(0);
  352. delete_dialog->set_text(TTR("Delete Property?") + "\n\"" + ti->get_text(0) + "\"");
  353. delete_dialog->popup_centered();
  354. }
  355. void ReplicationEditor::_dialog_closed(bool p_confirmed) {
  356. if (deleting.is_empty() || config.is_null()) {
  357. return;
  358. }
  359. if (p_confirmed) {
  360. const NodePath prop = deleting;
  361. int idx = config->property_get_index(prop);
  362. bool spawn = config->property_get_spawn(prop);
  363. bool sync = config->property_get_sync(prop);
  364. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  365. undo_redo->create_action(TTR("Remove Property"));
  366. undo_redo->add_do_method(config.ptr(), "remove_property", prop);
  367. undo_redo->add_undo_method(config.ptr(), "add_property", prop, idx);
  368. undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, spawn);
  369. undo_redo->add_undo_method(config.ptr(), "property_set_sync", prop, sync);
  370. undo_redo->add_do_method(this, "_update_config");
  371. undo_redo->add_undo_method(this, "_update_config");
  372. undo_redo->commit_action();
  373. }
  374. deleting = NodePath();
  375. }
  376. void ReplicationEditor::_update_checked(const NodePath &p_prop, int p_column, bool p_checked) {
  377. if (!tree->get_root()) {
  378. return;
  379. }
  380. TreeItem *ti = tree->get_root()->get_first_child();
  381. while (ti) {
  382. if (ti->get_metadata(0).operator NodePath() == p_prop) {
  383. ti->set_checked(p_column, p_checked);
  384. return;
  385. }
  386. ti = ti->get_next();
  387. }
  388. }
  389. void ReplicationEditor::update_keying() {
  390. /// TODO make keying usable.
  391. #if 0
  392. bool keying_enabled = false;
  393. EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
  394. if (is_visible_in_tree() && config.is_valid() && editor_history->get_path_size() > 0) {
  395. Object *obj = ObjectDB::get_instance(editor_history->get_path_object(0));
  396. keying_enabled = Object::cast_to<Node>(obj) != nullptr;
  397. }
  398. if (keying_enabled == keying) {
  399. return;
  400. }
  401. keying = keying_enabled;
  402. emit_signal(SNAME("keying_changed"));
  403. #endif
  404. }
  405. void ReplicationEditor::_update_config() {
  406. deleting = NodePath();
  407. tree->clear();
  408. tree->create_item();
  409. drop_label->set_visible(true);
  410. if (!config.is_valid()) {
  411. update_keying();
  412. return;
  413. }
  414. TypedArray<NodePath> props = config->get_properties();
  415. if (props.size()) {
  416. drop_label->set_visible(false);
  417. }
  418. for (int i = 0; i < props.size(); i++) {
  419. const NodePath path = props[i];
  420. _add_property(path, config->property_get_spawn(path), config->property_get_sync(path));
  421. }
  422. update_keying();
  423. }
  424. void ReplicationEditor::edit(MultiplayerSynchronizer *p_sync) {
  425. if (current == p_sync) {
  426. return;
  427. }
  428. current = p_sync;
  429. if (current) {
  430. config = current->get_replication_config();
  431. } else {
  432. config.unref();
  433. }
  434. _update_config();
  435. }
  436. Ref<Texture2D> ReplicationEditor::_get_class_icon(const Node *p_node) {
  437. if (!p_node || !has_theme_icon(p_node->get_class(), "EditorIcons")) {
  438. return get_theme_icon(SNAME("ImportFail"), SNAME("EditorIcons"));
  439. }
  440. return get_theme_icon(p_node->get_class(), "EditorIcons");
  441. }
  442. void ReplicationEditor::_add_property(const NodePath &p_property, bool p_spawn, bool p_sync) {
  443. String prop = String(p_property);
  444. TreeItem *item = tree->create_item();
  445. item->set_selectable(0, false);
  446. item->set_selectable(1, false);
  447. item->set_selectable(2, false);
  448. item->set_selectable(3, false);
  449. item->set_text(0, prop);
  450. item->set_metadata(0, prop);
  451. Node *root_node = current && !current->get_root_path().is_empty() ? current->get_node(current->get_root_path()) : nullptr;
  452. Ref<Texture2D> icon = _get_class_icon(root_node);
  453. if (root_node) {
  454. String path = prop.substr(0, prop.find(":"));
  455. String subpath = prop.substr(path.size());
  456. Node *node = root_node->get_node_or_null(path);
  457. if (!node) {
  458. node = root_node;
  459. }
  460. item->set_text(0, String(node->get_name()) + ":" + subpath);
  461. icon = _get_class_icon(node);
  462. }
  463. item->set_icon(0, icon);
  464. item->add_button(3, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  465. item->set_text_alignment(1, HORIZONTAL_ALIGNMENT_CENTER);
  466. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  467. item->set_checked(1, p_spawn);
  468. item->set_editable(1, true);
  469. item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_CENTER);
  470. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  471. item->set_checked(2, p_sync);
  472. item->set_editable(2, true);
  473. }
  474. void ReplicationEditor::property_keyed(const String &p_property) {
  475. ERR_FAIL_COND(!current || config.is_null());
  476. Node *root = current->get_node(current->get_root_path());
  477. ERR_FAIL_COND(!root);
  478. EditorSelectionHistory *history = EditorNode::get_singleton()->get_editor_selection_history();
  479. ERR_FAIL_COND(history->get_path_size() == 0);
  480. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(history->get_path_object(0)));
  481. ERR_FAIL_COND(!node);
  482. if (node->is_class("MultiplayerSynchronizer")) {
  483. error_dialog->set_text(TTR("Properties of 'MultiplayerSynchronizer' cannot be configured for replication."));
  484. error_dialog->popup_centered();
  485. return;
  486. }
  487. if (history->get_path_size() > 1 || p_property.get_slice_count(":") > 1) {
  488. error_dialog->set_text(TTR("Subresources cannot yet be configured for replication."));
  489. error_dialog->popup_centered();
  490. return;
  491. }
  492. String path = root->get_path_to(node);
  493. for (int i = 1; i < history->get_path_size(); i++) {
  494. String prop = history->get_path_property(i);
  495. ERR_FAIL_COND(prop == "");
  496. path += ":" + prop;
  497. }
  498. path += ":" + p_property;
  499. NodePath prop = path;
  500. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  501. undo_redo->create_action(TTR("Add property"));
  502. undo_redo->add_do_method(config.ptr(), "add_property", prop);
  503. undo_redo->add_undo_method(config.ptr(), "remove_property", prop);
  504. undo_redo->add_do_method(this, "_update_config");
  505. undo_redo->add_undo_method(this, "_update_config");
  506. undo_redo->commit_action();
  507. }
  508. /// ReplicationEditorPlugin
  509. ReplicationEditorPlugin::ReplicationEditorPlugin() {
  510. repl_editor = memnew(ReplicationEditor);
  511. button = EditorNode::get_singleton()->add_bottom_panel_item(TTR("Replication"), repl_editor);
  512. button->hide();
  513. repl_editor->get_pin()->connect("pressed", callable_mp(this, &ReplicationEditorPlugin::_pinned));
  514. }
  515. ReplicationEditorPlugin::~ReplicationEditorPlugin() {
  516. }
  517. void ReplicationEditorPlugin::_keying_changed() {
  518. // TODO make lock usable.
  519. //InspectorDock::get_inspector_singleton()->set_keying(repl_editor->has_keying(), this);
  520. }
  521. void ReplicationEditorPlugin::_property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance) {
  522. if (!repl_editor->has_keying()) {
  523. return;
  524. }
  525. repl_editor->property_keyed(p_keyed);
  526. }
  527. void ReplicationEditorPlugin::_notification(int p_what) {
  528. switch (p_what) {
  529. case NOTIFICATION_ENTER_TREE: {
  530. //Node3DEditor::get_singleton()->connect("transform_key_request", callable_mp(this, &AnimationPlayerEditorPlugin::_transform_key_request));
  531. InspectorDock::get_inspector_singleton()->connect("property_keyed", callable_mp(this, &ReplicationEditorPlugin::_property_keyed));
  532. repl_editor->connect("keying_changed", callable_mp(this, &ReplicationEditorPlugin::_keying_changed));
  533. // TODO make lock usable.
  534. //InspectorDock::get_inspector_singleton()->connect("object_inspected", callable_mp(repl_editor, &ReplicationEditor::update_keying));
  535. get_tree()->connect("node_removed", callable_mp(this, &ReplicationEditorPlugin::_node_removed));
  536. } break;
  537. }
  538. }
  539. void ReplicationEditorPlugin::_node_removed(Node *p_node) {
  540. if (p_node && p_node == repl_editor->get_current()) {
  541. repl_editor->edit(nullptr);
  542. if (repl_editor->is_visible_in_tree()) {
  543. EditorNode::get_singleton()->hide_bottom_panel();
  544. }
  545. button->hide();
  546. repl_editor->get_pin()->set_pressed(false);
  547. }
  548. }
  549. void ReplicationEditorPlugin::_pinned() {
  550. if (!repl_editor->get_pin()->is_pressed()) {
  551. if (repl_editor->is_visible_in_tree()) {
  552. EditorNode::get_singleton()->hide_bottom_panel();
  553. }
  554. button->hide();
  555. }
  556. }
  557. void ReplicationEditorPlugin::edit(Object *p_object) {
  558. repl_editor->edit(Object::cast_to<MultiplayerSynchronizer>(p_object));
  559. }
  560. bool ReplicationEditorPlugin::handles(Object *p_object) const {
  561. return p_object->is_class("MultiplayerSynchronizer");
  562. }
  563. void ReplicationEditorPlugin::make_visible(bool p_visible) {
  564. if (p_visible) {
  565. //editor->hide_animation_player_editors();
  566. //editor->animation_panel_make_visible(true);
  567. button->show();
  568. EditorNode::get_singleton()->make_bottom_panel_item_visible(repl_editor);
  569. } else if (!repl_editor->get_pin()->is_pressed()) {
  570. if (repl_editor->is_visible_in_tree()) {
  571. EditorNode::get_singleton()->hide_bottom_panel();
  572. }
  573. button->hide();
  574. }
  575. }