groups_editor.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*************************************************************************/
  2. /* groups_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "groups_editor.h"
  31. #include "editor_node.h"
  32. #include "scene/gui/box_container.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/resources/packed_scene.h"
  35. void GroupsEditor::_add_group(const String &p_group) {
  36. if (!node)
  37. return;
  38. String name = group_name->get_text();
  39. if (name.strip_edges() == "")
  40. return;
  41. if (node->is_in_group(name))
  42. return;
  43. undo_redo->create_action(TTR("Add to Group"));
  44. undo_redo->add_do_method(node, "add_to_group", name, true);
  45. undo_redo->add_do_method(this, "update_tree");
  46. undo_redo->add_undo_method(node, "remove_from_group", name);
  47. undo_redo->add_undo_method(this, "update_tree");
  48. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  49. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  50. undo_redo->commit_action();
  51. group_name->clear();
  52. }
  53. void GroupsEditor::_remove_group(Object *p_item, int p_column, int p_id) {
  54. if (!node)
  55. return;
  56. TreeItem *ti = p_item->cast_to<TreeItem>();
  57. if (!ti)
  58. return;
  59. String name = ti->get_text(0);
  60. undo_redo->create_action(TTR("Remove from Group"));
  61. undo_redo->add_do_method(node, "remove_from_group", name);
  62. undo_redo->add_do_method(this, "update_tree");
  63. undo_redo->add_undo_method(node, "add_to_group", name, true);
  64. undo_redo->add_undo_method(this, "update_tree");
  65. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  66. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  67. undo_redo->commit_action();
  68. }
  69. struct _GroupInfoComparator {
  70. bool operator()(const Node::GroupInfo &p_a, const Node::GroupInfo &p_b) const {
  71. return p_a.name.operator String() < p_b.name.operator String();
  72. }
  73. };
  74. void GroupsEditor::update_tree() {
  75. tree->clear();
  76. if (!node)
  77. return;
  78. List<Node::GroupInfo> groups;
  79. node->get_groups(&groups);
  80. groups.sort_custom<_GroupInfoComparator>();
  81. TreeItem *root = tree->create_item();
  82. for (List<GroupInfo>::Element *E = groups.front(); E; E = E->next()) {
  83. Node::GroupInfo gi = E->get();
  84. if (!gi.persistent)
  85. continue;
  86. Node *n = node;
  87. bool can_be_deleted = true;
  88. while (n) {
  89. Ref<SceneState> ss = (n == EditorNode::get_singleton()->get_edited_scene()) ? n->get_scene_inherited_state() : n->get_scene_instance_state();
  90. if (ss.is_valid()) {
  91. int path = ss->find_node_by_path(n->get_path_to(node));
  92. if (path != -1) {
  93. if (ss->is_node_in_group(path, gi.name)) {
  94. can_be_deleted = false;
  95. }
  96. }
  97. }
  98. n = n->get_owner();
  99. }
  100. TreeItem *item = tree->create_item(root);
  101. item->set_text(0, gi.name);
  102. if (can_be_deleted) {
  103. item->add_button(0, get_icon("Remove", "EditorIcons"), 0);
  104. } else {
  105. item->set_selectable(0, false);
  106. }
  107. }
  108. }
  109. void GroupsEditor::set_current(Node *p_node) {
  110. node = p_node;
  111. update_tree();
  112. }
  113. void GroupsEditor::_bind_methods() {
  114. ObjectTypeDB::bind_method("_add_group", &GroupsEditor::_add_group);
  115. ObjectTypeDB::bind_method("_remove_group", &GroupsEditor::_remove_group);
  116. ObjectTypeDB::bind_method("update_tree", &GroupsEditor::update_tree);
  117. }
  118. GroupsEditor::GroupsEditor() {
  119. node = NULL;
  120. VBoxContainer *vbc = this;
  121. HBoxContainer *hbc = memnew(HBoxContainer);
  122. vbc->add_child(hbc);
  123. group_name = memnew(LineEdit);
  124. group_name->set_h_size_flags(SIZE_EXPAND_FILL);
  125. hbc->add_child(group_name);
  126. group_name->connect("text_entered", this, "_add_group");
  127. add = memnew(Button);
  128. add->set_text(TTR("Add"));
  129. hbc->add_child(add);
  130. add->connect("pressed", this, "_add_group", varray(String()));
  131. tree = memnew(Tree);
  132. tree->set_hide_root(true);
  133. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  134. vbc->add_child(tree);
  135. tree->connect("button_pressed", this, "_remove_group");
  136. add_constant_override("separation", 3 * EDSCALE);
  137. }
  138. GroupsEditor::~GroupsEditor() {
  139. }