replication_editor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*************************************************************************/
  2. /* replication_editor.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.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. Ref<EditorUndoRedoManager> &undo_redo = EditorNode::get_singleton()->get_undo_redo();
  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("cancelled", 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. tree->set_drag_forwarding(this);
  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. ClassDB::bind_method("_can_drop_data_fw", &ReplicationEditor::_can_drop_data_fw);
  221. ClassDB::bind_method("_drop_data_fw", &ReplicationEditor::_drop_data_fw);
  222. }
  223. bool ReplicationEditor::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  224. Dictionary d = p_data;
  225. if (!d.has("type")) {
  226. return false;
  227. }
  228. String t = d["type"];
  229. if (t != "obj_property") {
  230. return false;
  231. }
  232. Object *obj = d["object"];
  233. if (!obj) {
  234. return false;
  235. }
  236. Node *node = Object::cast_to<Node>(obj);
  237. if (!node) {
  238. return false;
  239. }
  240. return true;
  241. }
  242. void ReplicationEditor::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  243. if (current == nullptr) {
  244. EditorNode::get_singleton()->show_warning(TTR("Select a replicator node in order to pick a property to add to it."));
  245. return;
  246. }
  247. Node *root = current->get_node(current->get_root_path());
  248. if (!root) {
  249. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new property to synchronize without a root."));
  250. return;
  251. }
  252. Dictionary d = p_data;
  253. if (!d.has("type")) {
  254. return;
  255. }
  256. String t = d["type"];
  257. if (t != "obj_property") {
  258. return;
  259. }
  260. Object *obj = d["object"];
  261. if (!obj) {
  262. return;
  263. }
  264. Node *node = Object::cast_to<Node>(obj);
  265. if (!node) {
  266. return;
  267. }
  268. String path = root->get_path_to(node);
  269. path += ":" + String(d["property"]);
  270. _add_sync_property(path);
  271. }
  272. void ReplicationEditor::_notification(int p_what) {
  273. switch (p_what) {
  274. case NOTIFICATION_ENTER_TREE:
  275. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  276. add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("panel"), SNAME("Panel")));
  277. add_pick_button->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  278. pin->set_icon(get_theme_icon(SNAME("Pin"), SNAME("EditorIcons")));
  279. } break;
  280. }
  281. }
  282. void ReplicationEditor::_add_pressed() {
  283. if (!current) {
  284. error_dialog->set_text(TTR("Please select a MultiplayerSynchronizer first."));
  285. error_dialog->popup_centered();
  286. return;
  287. }
  288. if (current->get_root_path().is_empty()) {
  289. error_dialog->set_text(TTR("The MultiplayerSynchronizer needs a root path."));
  290. error_dialog->popup_centered();
  291. return;
  292. }
  293. String np_text = np_line_edit->get_text();
  294. int idx = np_text.find(":");
  295. if (idx == -1) {
  296. np_text = ".:" + np_text;
  297. } else if (idx == 0) {
  298. np_text = "." + np_text;
  299. }
  300. NodePath path = NodePath(np_text);
  301. _add_sync_property(path);
  302. }
  303. void ReplicationEditor::_tree_item_edited() {
  304. TreeItem *ti = tree->get_edited();
  305. if (!ti || config.is_null()) {
  306. return;
  307. }
  308. int column = tree->get_edited_column();
  309. ERR_FAIL_COND(column < 1 || column > 2);
  310. const NodePath prop = ti->get_metadata(0);
  311. Ref<EditorUndoRedoManager> &undo_redo = EditorNode::get_undo_redo();
  312. bool value = ti->is_checked(column);
  313. String method;
  314. if (column == 1) {
  315. undo_redo->create_action(TTR("Set spawn property"));
  316. method = "property_set_spawn";
  317. } else {
  318. undo_redo->create_action(TTR("Set sync property"));
  319. method = "property_set_sync";
  320. }
  321. undo_redo->add_do_method(config.ptr(), method, prop, value);
  322. undo_redo->add_undo_method(config.ptr(), method, prop, !value);
  323. undo_redo->add_do_method(this, "_update_checked", prop, column, value);
  324. undo_redo->add_undo_method(this, "_update_checked", prop, column, !value);
  325. undo_redo->commit_action();
  326. }
  327. void ReplicationEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  328. if (p_button != MouseButton::LEFT) {
  329. return;
  330. }
  331. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  332. if (!ti) {
  333. return;
  334. }
  335. deleting = ti->get_metadata(0);
  336. delete_dialog->set_text(TTR("Delete Property?") + "\n\"" + ti->get_text(0) + "\"");
  337. delete_dialog->popup_centered();
  338. }
  339. void ReplicationEditor::_dialog_closed(bool p_confirmed) {
  340. if (deleting.is_empty() || config.is_null()) {
  341. return;
  342. }
  343. if (p_confirmed) {
  344. const NodePath prop = deleting;
  345. int idx = config->property_get_index(prop);
  346. bool spawn = config->property_get_spawn(prop);
  347. bool sync = config->property_get_sync(prop);
  348. Ref<EditorUndoRedoManager> &undo_redo = EditorNode::get_undo_redo();
  349. undo_redo->create_action(TTR("Remove Property"));
  350. undo_redo->add_do_method(config.ptr(), "remove_property", prop);
  351. undo_redo->add_undo_method(config.ptr(), "add_property", prop, idx);
  352. undo_redo->add_undo_method(config.ptr(), "property_set_spawn", prop, spawn);
  353. undo_redo->add_undo_method(config.ptr(), "property_set_sync", prop, sync);
  354. undo_redo->add_do_method(this, "_update_config");
  355. undo_redo->add_undo_method(this, "_update_config");
  356. undo_redo->commit_action();
  357. }
  358. deleting = NodePath();
  359. }
  360. void ReplicationEditor::_update_checked(const NodePath &p_prop, int p_column, bool p_checked) {
  361. if (!tree->get_root()) {
  362. return;
  363. }
  364. TreeItem *ti = tree->get_root()->get_first_child();
  365. while (ti) {
  366. if (ti->get_metadata(0).operator NodePath() == p_prop) {
  367. ti->set_checked(p_column, p_checked);
  368. return;
  369. }
  370. ti = ti->get_next();
  371. }
  372. }
  373. void ReplicationEditor::_update_config() {
  374. deleting = NodePath();
  375. tree->clear();
  376. tree->create_item();
  377. drop_label->set_visible(true);
  378. if (!config.is_valid()) {
  379. return;
  380. }
  381. TypedArray<NodePath> props = config->get_properties();
  382. if (props.size()) {
  383. drop_label->set_visible(false);
  384. }
  385. for (int i = 0; i < props.size(); i++) {
  386. const NodePath path = props[i];
  387. _add_property(path, config->property_get_spawn(path), config->property_get_sync(path));
  388. }
  389. }
  390. void ReplicationEditor::edit(MultiplayerSynchronizer *p_sync) {
  391. if (current == p_sync) {
  392. return;
  393. }
  394. current = p_sync;
  395. if (current) {
  396. config = current->get_replication_config();
  397. } else {
  398. config.unref();
  399. }
  400. _update_config();
  401. }
  402. Ref<Texture2D> ReplicationEditor::_get_class_icon(const Node *p_node) {
  403. if (!p_node || !has_theme_icon(p_node->get_class(), "EditorIcons")) {
  404. return get_theme_icon(SNAME("ImportFail"), SNAME("EditorIcons"));
  405. }
  406. return get_theme_icon(p_node->get_class(), "EditorIcons");
  407. }
  408. void ReplicationEditor::_add_property(const NodePath &p_property, bool p_spawn, bool p_sync) {
  409. String prop = String(p_property);
  410. TreeItem *item = tree->create_item();
  411. item->set_selectable(0, false);
  412. item->set_selectable(1, false);
  413. item->set_selectable(2, false);
  414. item->set_selectable(3, false);
  415. item->set_text(0, prop);
  416. item->set_metadata(0, prop);
  417. Node *root_node = current && !current->get_root_path().is_empty() ? current->get_node(current->get_root_path()) : nullptr;
  418. Ref<Texture2D> icon = _get_class_icon(root_node);
  419. if (root_node) {
  420. String path = prop.substr(0, prop.find(":"));
  421. String subpath = prop.substr(path.size());
  422. Node *node = root_node->get_node_or_null(path);
  423. if (!node) {
  424. node = root_node;
  425. }
  426. item->set_text(0, String(node->get_name()) + ":" + subpath);
  427. icon = _get_class_icon(node);
  428. }
  429. item->set_icon(0, icon);
  430. item->add_button(3, get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  431. item->set_text_alignment(1, HORIZONTAL_ALIGNMENT_CENTER);
  432. item->set_cell_mode(1, TreeItem::CELL_MODE_CHECK);
  433. item->set_checked(1, p_spawn);
  434. item->set_editable(1, true);
  435. item->set_text_alignment(2, HORIZONTAL_ALIGNMENT_CENTER);
  436. item->set_cell_mode(2, TreeItem::CELL_MODE_CHECK);
  437. item->set_checked(2, p_sync);
  438. item->set_editable(2, true);
  439. }