menu_button.cpp 8.1 KB

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