groups_editor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*************************************************************************/
  2. /* groups_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  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. /*************************************************************************/
  29. #include "groups_editor.h"
  30. #include "scene/gui/box_container.h"
  31. #include "scene/gui/label.h"
  32. #include "print_string.h"
  33. void GroupsEditor::_notification(int p_what) {
  34. if (p_what==NOTIFICATION_ENTER_SCENE) {
  35. connect("confirmed", this,"_close");
  36. }
  37. }
  38. void GroupsEditor::_close() {
  39. hide();
  40. }
  41. void GroupsEditor::_add() {
  42. if (!node)
  43. return;
  44. undo_redo->create_action("Add To Group");
  45. undo_redo->add_do_method(node,"add_to_group",group_name->get_text(),true);
  46. undo_redo->add_undo_method(node,"remove_from_group",group_name->get_text());
  47. undo_redo->add_do_method(this,"update_tree");
  48. undo_redo->add_undo_method(this,"update_tree");
  49. undo_redo->commit_action();
  50. }
  51. void GroupsEditor::_remove() {
  52. if (!tree->get_selected())
  53. return;
  54. if (!node)
  55. return;
  56. TreeItem *sel = tree->get_selected();
  57. if (!sel)
  58. return;
  59. node->remove_from_group( sel->get_text(0) );
  60. update_tree();
  61. }
  62. void GroupsEditor::update_tree() {
  63. tree->clear();
  64. if (!node)
  65. return;
  66. List<GroupInfo> groups;
  67. node->get_groups(&groups);
  68. TreeItem *root=tree->create_item();
  69. for(List<GroupInfo>::Element *E=groups.front();E;E=E->next()) {
  70. if (!E->get().persistent)
  71. continue;
  72. TreeItem *item=tree->create_item(root);
  73. item->set_text(0, E->get().name);
  74. }
  75. }
  76. void GroupsEditor::set_current(Node* p_node) {
  77. node=p_node;
  78. update_tree();
  79. }
  80. void GroupsEditor::_bind_methods() {
  81. ObjectTypeDB::bind_method("_add",&GroupsEditor::_add);
  82. ObjectTypeDB::bind_method("_close",&GroupsEditor::_close);
  83. ObjectTypeDB::bind_method("_remove",&GroupsEditor::_remove);
  84. ObjectTypeDB::bind_method("update_tree",&GroupsEditor::update_tree);
  85. }
  86. GroupsEditor::GroupsEditor() {
  87. set_title("Group Editor");
  88. Label * label = memnew( Label );
  89. label->set_pos( Point2( 8,11) );
  90. label->set_text("Groups:");
  91. add_child(label);
  92. group_name = memnew(LineEdit);
  93. group_name->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  94. group_name->set_begin( Point2( 15,28) );
  95. group_name->set_end( Point2( 94,48 ) );
  96. add_child(group_name);
  97. tree = memnew( Tree );
  98. tree->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  99. tree->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  100. tree->set_begin( Point2( 15,52) );
  101. tree->set_end( Point2( 94,42 ) );
  102. tree->set_hide_root(true);
  103. add_child(tree);
  104. add = memnew( Button );
  105. add->set_anchor( MARGIN_LEFT, ANCHOR_END );
  106. add->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  107. add->set_begin( Point2( 90, 28 ) );
  108. add->set_end( Point2( 15, 48 ) );
  109. add->set_text("Add");
  110. add_child(add);
  111. remove = memnew( Button );
  112. remove->set_anchor( MARGIN_LEFT, ANCHOR_END );
  113. remove->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  114. remove->set_begin( Point2( 90, 52 ) );
  115. remove->set_end( Point2( 15, 72 ) );
  116. remove->set_text("Remove");
  117. add_child(remove);
  118. get_ok()->set_text("Close");
  119. add->connect("pressed", this,"_add");
  120. remove->connect("pressed", this,"_remove");
  121. node=NULL;
  122. }
  123. GroupsEditor::~GroupsEditor()
  124. {
  125. }