replication_editor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. #include "replication_editor.h"
  31. #include "../multiplayer_synchronizer.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/inspector_dock.h"
  37. #include "editor/property_selector.h"
  38. #include "editor/scene_tree_editor.h"
  39. #include "scene/gui/dialogs.h"
  40. #include "scene/gui/separator.h"
  41. #include "scene/gui/tree.h"
  42. void ReplicationEditor::_pick_node_filter_text_changed(const String &p_newtext) {
  43. TreeItem *root_item = pick_node->get_scene_tree()->get_scene_tree()->get_root();
  44. Vector<Node *> select_candidates;
  45. Node *to_select = nullptr;
  46. String filter = pick_node->get_filter_line_edit()->get_text();
  47. _pick_node_select_recursive(root_item, filter, select_candidates);
  48. if (!select_candidates.is_empty()) {
  49. for (int i = 0; i < select_candidates.size(); ++i) {
  50. Node *candidate = select_candidates[i];
  51. if (((String)candidate->get_name()).to_lower().begins_with(filter.to_lower())) {
  52. to_select = candidate;
  53. break;
  54. }
  55. }
  56. if (!to_select) {
  57. to_select = select_candidates[0];
  58. }
  59. }
  60. pick_node->get_scene_tree()->set_selected(to_select);
  61. }
  62. void ReplicationEditor::_pick_node_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates) {
  63. if (!p_item) {
  64. return;
  65. }
  66. NodePath np = p_item->get_metadata(0);
  67. Node *node = get_node(np);
  68. if (!p_filter.is_empty() && ((String)node->get_name()).findn(p_filter) != -1) {
  69. p_select_candidates.push_back(node);
  70. }
  71. TreeItem *c = p_item->get_first_child();
  72. while (c) {
  73. _pick_node_select_recursive(c, p_filter, p_select_candidates);
  74. c = c->get_next();
  75. }
  76. }
  77. void ReplicationEditor::_pick_node_filter_input(const Ref<InputEvent> &p_ie) {
  78. Ref<InputEventKey> k = p_ie;
  79. if (k.is_valid()) {
  80. switch (k->get_keycode()) {
  81. case Key::UP:
  82. case Key::DOWN:
  83. case Key::PAGEUP:
  84. case Key::PAGEDOWN: {
  85. pick_node->get_scene_tree()->get_scene_tree()->gui_input(k);
  86. pick_node->get_filter_line_edit()->accept_event();
  87. } break;
  88. default:
  89. break;
  90. }
  91. }
  92. }
  93. void ReplicationEditor::_pick_node_selected(NodePath p_path) {
  94. Node *root = current->get_node(current->get_root_path());
  95. ERR_FAIL_COND(!root);
  96. Node *node = get_node(p_path);
  97. ERR_FAIL_COND(!node);
  98. NodePath path_to = root->get_path_to(node);
  99. adding_node_path = path_to;
  100. prop_selector->select_property_from_instance(node);
  101. }
  102. void ReplicationEditor::_pick_new_property() {
  103. if (current == nullptr) {
  104. EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));
  105. return;
  106. }
  107. Node *root = current->get_node(current->get_root_path());
  108. if (!root) {
  109. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));
  110. return;
  111. }
  112. pick_node->popup_scenetree_dialog();
  113. pick_node->get_filter_line_edit()->clear();
  114. pick_node->get_filter_line_edit()->grab_focus();
  115. }
  116. void ReplicationEditor::_add_sync_property(String p_path) {
  117. config = current->get_replication_config();
  118. if (config.is_valid() && config->has_property(p_path)) {
  119. EditorNode::get_singleton()->show_warning(TTR("Property is already being synchronized."));
  120. return;
  121. }
  122. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  123. undo_redo->create_action(TTR("Add property to synchronizer"));
  124. if (config.is_null()) {
  125. config.instantiate();
  126. current->set_replication_config(config);
  127. undo_redo->add_do_method(current, "set_replication_config", config);
  128. undo_redo->add_undo_method(current, "set_replication_config", Ref<SceneReplicationConfig>());
  129. _update_config();
  130. }
  131. undo_redo->add_do_method(config.ptr(), "add_property", p_path);
  132. undo_redo->add_undo_method(config.ptr(), "remove_property", p_path);
  133. undo_redo->add_do_method(this, "_update_config");
  134. undo_redo->add_undo_method(this, "_update_config");
  135. undo_redo->commit_action();
  136. }
  137. void ReplicationEditor::_pick_node_property_selected(String p_name) {
  138. String adding_prop_path = String(adding_node_path) + ":" + p_name;
  139. _add_sync_property(adding_prop_path);
  140. }
  141. /// ReplicationEditor
  142. ReplicationEditor::ReplicationEditor() {
  143. set_v_size_flags(SIZE_EXPAND_FILL);
  144. set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  145. delete_dialog = memnew(ConfirmationDialog);
  146. delete_dialog->connect("canceled", callable_mp(this, &ReplicationEditor::_dialog_closed).bind(false));
  147. delete_dialog->connect("confirmed", callable_mp(this, &ReplicationEditor::_dialog_closed).bind(true));
  148. add_child(delete_dialog);
  149. error_dialog = memnew(AcceptDialog);
  150. error_dialog->set_ok_button_text(TTR("Close"));
  151. error_dialog->set_title(TTR("Error!"));
  152. add_child(error_dialog);
  153. VBoxContainer *vb = memnew(VBoxContainer);
  154. vb->set_v_size_flags(SIZE_EXPAND_FILL);
  155. add_child(vb);
  156. pick_node = memnew(SceneTreeDialog);
  157. add_child(pick_node);
  158. pick_node->register_text_enter(pick_node->get_filter_line_edit());
  159. pick_node->set_title(TTR("Pick a node to synchronize:"));
  160. pick_node->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_selected));
  161. pick_node->get_filter_line_edit()->connect("text_changed", callable_mp(this, &ReplicationEditor::_pick_node_filter_text_changed));
  162. pick_node->get_filter_line_edit()->connect("gui_input", callable_mp(this, &ReplicationEditor::_pick_node_filter_input));
  163. prop_selector = memnew(PropertySelector);
  164. add_child(prop_selector);
  165. prop_selector->connect("selected", callable_mp(this, &ReplicationEditor::_pick_node_property_selected));
  166. HBoxContainer *hb = memnew(HBoxContainer);
  167. vb->add_child(hb);
  168. add_pick_button = memnew(Button);
  169. add_pick_button->connect("pressed", callable_mp(this, &ReplicationEditor::_pick_new_property));
  170. add_pick_button->set_text(TTR("Add property to sync..."));
  171. hb->add_child(add_pick_button);
  172. VSeparator *vs = memnew(VSeparator);
  173. vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));
  174. hb->add_child(vs);
  175. hb->add_child(memnew(Label(TTR("Path:"))));
  176. np_line_edit = memnew(LineEdit);
  177. np_line_edit->set_placeholder(":property");
  178. np_line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  179. hb->add_child(np_line_edit);
  180. add_from_path_button = memnew(Button);
  181. add_from_path_button->connect("pressed", callable_mp(this, &ReplicationEditor::_add_pressed));
  182. add_from_path_button->set_text(TTR("Add from path"));
  183. hb->add_child(add_from_path_button);
  184. vs = memnew(VSeparator);
  185. vs->set_custom_minimum_size(Size2(30 * EDSCALE, 0));
  186. hb->add_child(vs);
  187. pin = memnew(Button);
  188. pin->set_flat(true);
  189. pin->set_toggle_mode(true);
  190. hb->add_child(pin);
  191. tree = memnew(Tree);
  192. tree->set_hide_root(true);
  193. tree->set_columns(4);
  194. tree->set_column_titles_visible(true);
  195. tree->set_column_title(0, TTR("Properties"));
  196. tree->set_column_expand(0, true);
  197. tree->set_column_title(1, TTR("Spawn"));
  198. tree->set_column_expand(1, false);
  199. tree->set_column_custom_minimum_width(1, 100);
  200. tree->set_column_title(2, TTR("Sync"));
  201. tree->set_column_custom_minimum_width(2, 100);
  202. tree->set_column_expand(2, false);
  203. tree->set_column_expand(3, false);
  204. tree->create_item();
  205. tree->connect("button_clicked", callable_mp(this, &ReplicationEditor::_tree_button_pressed));
  206. tree->connect("item_edited", callable_mp(this, &ReplicationEditor::_tree_item_edited));
  207. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  208. vb->add_child(tree);
  209. drop_label = memnew(Label);
  210. drop_label->set_text(TTR("Add properties using the buttons above or\ndrag them them from the inspector and drop them here."));
  211. drop_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  212. drop_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  213. tree->add_child(drop_label);
  214. drop_label->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  215. SET_DRAG_FORWARDING_CDU(tree, ReplicationEditor);
  216. }
  217. void ReplicationEditor::_bind_methods() {
  218. ClassDB::bind_method(D_METHOD("_update_config"), &ReplicationEditor::_update_config);
  219. ClassDB::bind_method(D_METHOD("_update_checked", "property", "column", "checked"), &ReplicationEditor::_update_checked);
  220. }
  221. bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  222. Dictionary d = p_data;
  223. if (!d.has("type")) {
  224. return false;
  225. }
  226. String t = d["type"];
  227. if (t != "obj_property") {
  228. return false;
  229. }
  230. Object *obj = d["object"];
  231. if (!obj) {
  232. return false;
  233. }
  234. Node *node = Object::cast_to<Node>(obj);
  235. if (!node) {
  236. return false;
  237. }
  238. return true;
  239. }
  240. void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  241. if (current == nullptr) {
  242. EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));
  243. return;
  244. }
  245. Node *root = current->get_node(current->get_root_path());
  246. if (!root) {
  247. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));
  248. return;
  249. }
  250. Dictionary d = p_data;
  251. if (!d.has("type")) {
  252. return;
  253. }
  254. String t = d["type"];
  255. if (t != "obj_property") {
  256. return;
  257. }
  258. Object *obj = d["object"];
  259. if (!obj) {
  260. return;
  261. }
  262. Node *node = Object::cast_to<Node>(obj);
  263. if (!node) {
  264. return;
  265. }
  266. String path = root->get_path_to(node);
  267. path += ":" + String(d["property"]);
  268. _add_sync_property(path);
  269. }
  270. void ReplicationEditor::_notification(int p_what) {
  271. switch (p_what) {
  272. case NOTIFICATION_ENTER_TREE:
  273. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  274. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  275. add_pick_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  276. pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")));
  277. } break;
  278. }
  279. }
  280. void ReplicationEditor::_add_pressed() {
  281. if (!current) {
  282. error_dialog->set_text(TTR("Please select a MultiplayerSynchronizer first."));
  283. error_dialog->popup_centered();
  284. return;
  285. }
  286. if (current->get_root_path().is_empty()) {
  287. error_dialog->set_text(TTR("The MultiplayerSynchronizer needs a root path."));
  288. error_dialog->popup_centered();
  289. return;
  290. }
  291. String np_text = np_line_edit->get_text();
  292. int idx = np_text.find(":");
  293. if (idx == -1) {
  294. np_text = ".:" + np_text;
  295. } else if (idx == 0) {
  296. np_text = "." + np_text;
  297. }
  298. NodePath path = NodePath(np_text);
  299. _add_sync_property(path);
  300. }
  301. void ReplicationEditor::_tree_item_edited() {
  302. TreeItem *ti = tree->get_edited();
  303. if (!ti || config.is_null()) {
  304. return;
  305. }
  306. int column = tree->get_edited_column();
  307. ERR_FAIL_COND(column < 1 || column > 2);
  308. const NodePath prop = ti->get_metadata(0);
  309. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  310. bool value = ti->is_checked(column);
  311. String method;
  312. if (column == 1) {
  313. undo_redo->create_action(TTR("Set spawn property"));
  314. method = "property_set_spawn";
  315. } else {
  316. undo_redo->create_action(TTR("Set sync property"));
  317. method = "property_set_sync";
  318. }
  319. undo_redo->add_do_method(config.ptr(), method, prop, value);
  320. undo_redo->add_undo_method(config.ptr(), method, prop, !value);
  321. undo_redo->add_do_method(this, "_update_checked", prop, column, value);
  322. undo_redo->add_undo_method(this, "_update_checked", prop, column, !value);
  323. undo_redo->commit_action();
  324. }
  325. void ReplicationEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  326. if (p_button != MouseButton::LEFT) {
  327. return;
  328. }
  329. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  330. if (!ti) {
  331. return;
  332. }
  333. deleting = ti->get_metadata(0);
  334. delete_dialog->set_text(TTR("Delete Property?") + "\n\"" + ti->get_text(0) + "\"");
  335. delete_dialog->popup_centered();
  336. }
  337. void ReplicationEditor::_dialog_closed(bool p_confirmed) {
  338. if (deleting.is_empty() || config.is_null()) {
  339. return;
  340. }
  341. if (p_confirmed) {
  342. const NodePath prop = deleting;
  343. int idx = config->property_get_index(prop);
  344. bool spawn = config->property_get_spawn(prop);
  345. bool sync = config->property_get_sync(prop);
  346. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  347. undo_redo->create_action(TTR("Remove Property"));
  348. undo_redo->add_do_method(config.ptr(), "remove_property", prop);
  349. undo_redo->add_undo_method(config.ptr(), "add_property", prop, idx);
  350. undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, spawn);
  351. undo_redo->add_undo_method(config.ptr(), "property_set_sync", prop, sync);
  352. undo_redo->add_do_method(this, "_update_config");
  353. undo_redo->add_undo_method(this, "_update_config");
  354. undo_redo->commit_action();
  355. }
  356. deleting = NodePath();
  357. }
  358. void ReplicationEditor::_update_checked(const NodePath &p_prop, int p_column, bool p_checked) {
  359. if (!tree->get_root()) {
  360. return;
  361. }
  362. TreeItem *ti = tree->get_root()->get_first_child();
  363. while (ti) {
  364. if (ti->get_metadata(0).operator NodePath() == p_prop) {
  365. ti->set_checked(p_column, p_checked);
  366. return;
  367. }
  368. ti = ti->get_next();
  369. }
  370. }
  371. void ReplicationEditor::_update_config() {
  372. deleting = NodePath();
  373. tree->clear();
  374. tree->create_item();
  375. drop_label->set_visible(true);
  376. if (!config.is_valid()) {
  377. return;
  378. }
  379. TypedArray<NodePath> props = config->get_properties();
  380. if (props.size()) {
  381. drop_label->set_visible(false);
  382. }
  383. for (int i = 0; i < props.size(); i++) {
  384. const NodePath path = props[i];
  385. _add_property(path, config->property_get_spawn(path), config->property_get_sync(path));
  386. }
  387. }
  388. void ReplicationEditor::edit(MultiplayerSynchronizer *p_sync) {
  389. if (current == p_sync) {
  390. return;
  391. }
  392. current = p_sync;
  393. if (current) {
  394. config = current->get_replication_config();
  395. } else {
  396. config.unref();
  397. }
  398. _update_config();
  399. }
  400. Ref<Texture2D> ReplicationEditor::_get_class_icon(const Node *p_node) {
  401. if (!p_node || !has_theme_icon(p_node->get_class(), "EditorIcons")) {
  402. return get_theme_icon(SNAME("ImportFail"), SNAME("EditorIcons"));
  403. }
  404. return get_theme_icon(p_node->get_class(), "EditorIcons");
  405. }
  406. void ReplicationEditor::_add_property(const NodePath &p_property, bool p_spawn, bool p_sync) {
  407. String prop = String(p_property);
  408. TreeItem *item = tree->create_item();
  409. item->set_selectable(0, false);
  410. item->set_selectable(1, false);
  411. item->set_selectable(2, false);
  412. item->set_selectable(3, false);
  413. item->set_text(0, prop);
  414. item->set_metadata(0, prop);
  415. Node *root_node = current && !current->get_root_path().is_empty() ? current->get_node(current->get_root_path()) : nullptr;
  416. Ref<Texture2D> icon = _get_class_icon(root_node);
  417. if (root_node) {
  418. String path = prop.substr(0, prop.find(":"));
  419. String subpath = prop.substr(path.size());
  420. Node *node = root_node->get_node_or_null(path);
  421. if (!node) {
  422. node = root_node;
  423. }
  424. item->set_text(0, String(node->get_name()) + ":" + subpath);
  425. icon = _get_class_icon(node);
  426. }
  427. item->set_icon(0, icon);
  428. item->add_button(3, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  429. item->set_text_alignment(1, HORIZONTAL_ALIGNMENT_CENTER);
  430. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  431. item->set_checked(1, p_spawn);
  432. item->set_editable(1, true);
  433. item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_CENTER);
  434. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  435. item->set_checked(2, p_sync);
  436. item->set_editable(2, true);
  437. }