menu_button.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. if (get_viewport()->is_embedding_subwindows() && popup->get_force_native()) {
  71. Transform2D xform = get_viewport()->get_popup_base_transform_native();
  72. rect = xform.xform(rect);
  73. }
  74. rect.size.height = 0;
  75. popup->set_size(rect.size);
  76. if (is_layout_rtl()) {
  77. rect.position.x += rect.size.width - popup->get_size().width;
  78. }
  79. popup->set_position(rect.position);
  80. // If not triggered by the mouse, start the popup with its first enabled item focused.
  81. if (!_was_pressed_by_mouse()) {
  82. for (int i = 0; i < popup->get_item_count(); i++) {
  83. if (!popup->is_item_disabled(i)) {
  84. popup->set_focused_item(i);
  85. break;
  86. }
  87. }
  88. }
  89. popup->popup();
  90. }
  91. void MenuButton::set_switch_on_hover(bool p_enabled) {
  92. switch_on_hover = p_enabled;
  93. }
  94. bool MenuButton::is_switch_on_hover() {
  95. return switch_on_hover;
  96. }
  97. void MenuButton::set_item_count(int p_count) {
  98. ERR_FAIL_COND(p_count < 0);
  99. if (popup->get_item_count() == p_count) {
  100. return;
  101. }
  102. popup->set_item_count(p_count);
  103. notify_property_list_changed();
  104. }
  105. int MenuButton::get_item_count() const {
  106. return popup->get_item_count();
  107. }
  108. void MenuButton::_notification(int p_what) {
  109. switch (p_what) {
  110. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  111. RID ae = get_accessibility_element();
  112. ERR_FAIL_COND(ae.is_null());
  113. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  114. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  115. } break;
  116. case NOTIFICATION_TRANSLATION_CHANGED:
  117. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  118. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  119. } break;
  120. case NOTIFICATION_VISIBILITY_CHANGED: {
  121. if (!is_visible_in_tree()) {
  122. popup->hide();
  123. }
  124. } break;
  125. case NOTIFICATION_INTERNAL_PROCESS: {
  126. MenuButton *menu_btn_other = Object::cast_to<MenuButton>(get_viewport()->gui_get_hovered_control());
  127. if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
  128. (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
  129. popup->hide();
  130. menu_btn_other->pressed();
  131. // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
  132. menu_btn_other->get_popup()->set_focused_item(-1);
  133. }
  134. } break;
  135. }
  136. }
  137. bool MenuButton::_set(const StringName &p_name, const Variant &p_value) {
  138. const String sname = p_name;
  139. if (property_helper.is_property_valid(sname)) {
  140. bool valid;
  141. popup->set(sname.trim_prefix("popup/"), p_value, &valid);
  142. return valid;
  143. }
  144. return false;
  145. }
  146. bool MenuButton::_get(const StringName &p_name, Variant &r_ret) const {
  147. const String sname = p_name;
  148. if (property_helper.is_property_valid(sname)) {
  149. bool valid;
  150. r_ret = popup->get(sname.trim_prefix("popup/"), &valid);
  151. return valid;
  152. }
  153. return false;
  154. }
  155. void MenuButton::_bind_methods() {
  156. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  157. ClassDB::bind_method(D_METHOD("show_popup"), &MenuButton::show_popup);
  158. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  159. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  160. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  161. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MenuButton::set_item_count);
  162. ClassDB::bind_method(D_METHOD("get_item_count"), &MenuButton::get_item_count);
  163. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  164. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  165. ADD_SIGNAL(MethodInfo("about_to_popup"));
  166. ADD_CLASS_DEPENDENCY("PopupMenu");
  167. PopupMenu::Item defaults(true);
  168. base_property_helper.set_prefix("popup/item_");
  169. base_property_helper.set_array_length_getter(&MenuButton::get_item_count);
  170. base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text);
  171. base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon);
  172. base_property_helper.register_property(PropertyInfo(Variant::INT, "checkable", PROPERTY_HINT_ENUM, "No,As Checkbox,As Radio Button"), defaults.checkable_type);
  173. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "checked"), defaults.checked);
  174. 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);
  175. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled);
  176. base_property_helper.register_property(PropertyInfo(Variant::BOOL, "separator"), defaults.separator);
  177. PropertyListHelper::register_base_helper(&base_property_helper);
  178. }
  179. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  180. disable_shortcuts = p_disabled;
  181. }
  182. #ifdef TOOLS_ENABLED
  183. PackedStringArray MenuButton::get_configuration_warnings() const {
  184. PackedStringArray warnings = Button::get_configuration_warnings();
  185. warnings.append_array(popup->get_configuration_warnings());
  186. return warnings;
  187. }
  188. #endif
  189. MenuButton::MenuButton(const String &p_text) :
  190. Button(p_text) {
  191. set_flat(true);
  192. set_toggle_mode(true);
  193. set_disable_shortcuts(false);
  194. set_process_shortcut_input(true);
  195. set_focus_mode(FOCUS_ACCESSIBILITY);
  196. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  197. popup = memnew(PopupMenu);
  198. popup->hide();
  199. add_child(popup, false, INTERNAL_MODE_FRONT);
  200. popup->connect("about_to_popup", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(true));
  201. popup->connect("popup_hide", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(false));
  202. property_helper.setup_for_instance(base_property_helper, this);
  203. }
  204. MenuButton::~MenuButton() {
  205. }