menu_button.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**************************************************************************/
  2. /* menu_button.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 "menu_button.h"
  31. #include "scene/main/window.h"
  32. void MenuButton::shortcut_input(const Ref<InputEvent> &p_event) {
  33. ERR_FAIL_COND(p_event.is_null());
  34. if (disable_shortcuts) {
  35. return;
  36. }
  37. if (p_event->is_pressed() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
  38. accept_event();
  39. return;
  40. }
  41. Button::shortcut_input(p_event);
  42. }
  43. void MenuButton::_popup_visibility_changed(bool p_visible) {
  44. set_pressed(p_visible);
  45. if (!p_visible) {
  46. set_process_internal(false);
  47. return;
  48. }
  49. if (switch_on_hover) {
  50. set_process_internal(true);
  51. }
  52. }
  53. void MenuButton::pressed() {
  54. if (popup->is_visible()) {
  55. popup->hide();
  56. return;
  57. }
  58. show_popup();
  59. }
  60. PopupMenu *MenuButton::get_popup() const {
  61. return popup;
  62. }
  63. void MenuButton::show_popup() {
  64. if (!get_viewport()) {
  65. return;
  66. }
  67. emit_signal(SNAME("about_to_popup"));
  68. Rect2 rect = get_screen_rect();
  69. rect.position.y += rect.size.height;
  70. rect.size.height = 0;
  71. popup->set_size(rect.size);
  72. if (is_layout_rtl()) {
  73. rect.position.x += rect.size.width - popup->get_size().width;
  74. }
  75. popup->set_position(rect.position);
  76. // If not triggered by the mouse, start the popup with its first enabled item focused.
  77. if (!_was_pressed_by_mouse()) {
  78. for (int i = 0; i < popup->get_item_count(); i++) {
  79. if (!popup->is_item_disabled(i)) {
  80. popup->set_focused_item(i);
  81. break;
  82. }
  83. }
  84. }
  85. popup->popup();
  86. }
  87. void MenuButton::set_switch_on_hover(bool p_enabled) {
  88. switch_on_hover = p_enabled;
  89. }
  90. bool MenuButton::is_switch_on_hover() {
  91. return switch_on_hover;
  92. }
  93. void MenuButton::set_item_count(int p_count) {
  94. ERR_FAIL_COND(p_count < 0);
  95. if (popup->get_item_count() == p_count) {
  96. return;
  97. }
  98. popup->set_item_count(p_count);
  99. notify_property_list_changed();
  100. }
  101. int MenuButton::get_item_count() const {
  102. return popup->get_item_count();
  103. }
  104. void MenuButton::_notification(int p_what) {
  105. switch (p_what) {
  106. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  107. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  108. } break;
  109. case NOTIFICATION_VISIBILITY_CHANGED: {
  110. if (!is_visible_in_tree()) {
  111. popup->hide();
  112. }
  113. } break;
  114. case NOTIFICATION_INTERNAL_PROCESS: {
  115. MenuButton *menu_btn_other = Object::cast_to<MenuButton>(get_viewport()->gui_get_hovered_control());
  116. if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
  117. (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
  118. popup->hide();
  119. menu_btn_other->pressed();
  120. // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
  121. menu_btn_other->get_popup()->set_focused_item(-1);
  122. }
  123. } break;
  124. }
  125. }
  126. bool MenuButton::_set(const StringName &p_name, const Variant &p_value) {
  127. const String sname = p_name;
  128. if (property_helper.is_property_valid(sname)) {
  129. bool valid;
  130. popup->set(sname.trim_prefix("popup/"), p_value, &valid);
  131. return valid;
  132. }
  133. return false;
  134. }
  135. bool MenuButton::_get(const StringName &p_name, Variant &r_ret) const {
  136. const String sname = p_name;
  137. if (property_helper.is_property_valid(sname)) {
  138. bool valid;
  139. r_ret = popup->get(sname.trim_prefix("popup/"), &valid);
  140. return valid;
  141. }
  142. return false;
  143. }
  144. void MenuButton::_bind_methods() {
  145. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  146. ClassDB::bind_method(D_METHOD("show_popup"), &MenuButton::show_popup);
  147. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  148. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  149. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  150. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MenuButton::set_item_count);
  151. ClassDB::bind_method(D_METHOD("get_item_count"), &MenuButton::get_item_count);
  152. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  153. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  154. ADD_SIGNAL(MethodInfo("about_to_popup"));
  155. PopupMenu::Item defaults(true);
  156. base_property_helper.set_prefix("popup/item_");
  157. base_property_helper.set_array_length_getter(&MenuButton::get_item_count);
  158. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text);
  159. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon);
  160. base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As Checkbox,As Radio Button"), defaults.checkable_type);
  161. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked);
  162. base_property_helper.register_property(PropertyInfo(Variant::INT, "id", PROPERTY_HINT_RANGE, "0,10,1,or_greater", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL), defaults.id);
  163. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled);
  164. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator);
  165. PropertyListHelper::register_base_helper(&base_property_helper);
  166. }
  167. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  168. disable_shortcuts = p_disabled;
  169. }
  170. MenuButton::MenuButton(const String &p_text) :
  171. Button(p_text) {
  172. set_flat(true);
  173. set_toggle_mode(true);
  174. set_disable_shortcuts(false);
  175. set_process_shortcut_input(true);
  176. set_focus_mode(FOCUS_NONE);
  177. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  178. popup = memnew(PopupMenu);
  179. popup->hide();
  180. add_child(popup, false, INTERNAL_MODE_FRONT);
  181. popup->connect("about_to_popup", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(true));
  182. popup->connect("popup_hide", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(false));
  183. property_helper.setup_for_instance(base_property_helper, this);
  184. }
  185. MenuButton::~MenuButton() {
  186. }