button_group.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* button_group.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "button_group.h"
  30. #include "base_button.h"
  31. void ButtonGroup::_add_button(BaseButton *p_button) {
  32. buttons.insert(p_button);
  33. p_button->set_toggle_mode(true);
  34. p_button->set_click_on_press(true);
  35. p_button->connect("pressed",this,"_pressed",make_binds(p_button));
  36. }
  37. void ButtonGroup::_remove_button(BaseButton *p_button){
  38. buttons.erase(p_button);
  39. p_button->disconnect("pressed",this,"_pressed");
  40. }
  41. void ButtonGroup::set_pressed_button(BaseButton *p_button) {
  42. _pressed(p_button);
  43. }
  44. void ButtonGroup::_pressed(Object *p_button) {
  45. ERR_FAIL_NULL(p_button);
  46. BaseButton *b=p_button->cast_to<BaseButton>();
  47. ERR_FAIL_COND(!b);
  48. for(Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) {
  49. BaseButton *bb=E->get();
  50. bb->set_pressed( b==bb );
  51. }
  52. }
  53. Array ButtonGroup::_get_button_list() const {
  54. List<BaseButton*> b;
  55. get_button_list(&b);
  56. b.sort_custom<Node::Comparator>();
  57. Array arr;
  58. arr.resize(b.size());
  59. int idx=0;
  60. for(List<BaseButton*>::Element *E=b.front();E;E=E->next(),idx++) {
  61. arr[idx]=E->get();
  62. }
  63. return arr;
  64. }
  65. void ButtonGroup::get_button_list(List<BaseButton*> *p_buttons) const {
  66. for(Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) {
  67. p_buttons->push_back(E->get());
  68. }
  69. }
  70. BaseButton *ButtonGroup::get_pressed_button() const {
  71. for(Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) {
  72. if (E->get()->is_pressed())
  73. return E->get();
  74. }
  75. return NULL;
  76. }
  77. BaseButton *ButtonGroup::get_focused_button() const{
  78. for(Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) {
  79. if (E->get()->has_focus())
  80. return E->get();
  81. }
  82. return NULL;
  83. }
  84. int ButtonGroup::get_pressed_button_index() const {
  85. //in tree order, this is bizarre
  86. ERR_FAIL_COND_V(!is_inside_tree(),0);
  87. BaseButton *pressed = get_pressed_button();
  88. if (!pressed)
  89. return -1;
  90. List<BaseButton*> blist;
  91. for(Set<BaseButton*>::Element *E=buttons.front();E;E=E->next()) {
  92. blist.push_back(E->get());
  93. }
  94. blist.sort_custom<Node::Comparator>();
  95. int idx=0;
  96. for(List<BaseButton*>::Element *E=blist.front();E;E=E->next()) {
  97. if (E->get()==pressed)
  98. return idx;
  99. idx++;
  100. }
  101. return -1;
  102. }
  103. void ButtonGroup::_bind_methods() {
  104. ObjectTypeDB::bind_method(_MD("get_pressed_button:BaseButton"),&ButtonGroup::get_pressed_button);
  105. ObjectTypeDB::bind_method(_MD("get_pressed_button_index"),&ButtonGroup::get_pressed_button_index);
  106. ObjectTypeDB::bind_method(_MD("get_focused_button:BaseButton"),&ButtonGroup::get_focused_button);
  107. ObjectTypeDB::bind_method(_MD("get_button_list"),&ButtonGroup::_get_button_list);
  108. ObjectTypeDB::bind_method(_MD("_pressed"),&ButtonGroup::_pressed);
  109. ObjectTypeDB::bind_method(_MD("set_pressed_button","button:BaseButton"),&ButtonGroup::_pressed);
  110. }
  111. ButtonGroup::ButtonGroup() : BoxContainer(true)
  112. {
  113. }