2
0

menu_button.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*************************************************************************/
  2. /* menu_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 (!_is_focus_owner_in_shortcut_context()) {
  36. return;
  37. }
  38. if (disable_shortcuts) {
  39. return;
  40. }
  41. if (p_event->is_pressed() && !p_event->is_echo() && !is_disabled() && is_visible_in_tree() && popup->activate_item_by_event(p_event, false)) {
  42. accept_event();
  43. return;
  44. }
  45. Button::shortcut_input(p_event);
  46. }
  47. void MenuButton::_popup_visibility_changed(bool p_visible) {
  48. set_pressed(p_visible);
  49. if (!p_visible) {
  50. set_process_internal(false);
  51. return;
  52. }
  53. if (switch_on_hover) {
  54. Window *window = Object::cast_to<Window>(get_viewport());
  55. if (window) {
  56. mouse_pos_adjusted = window->get_position();
  57. if (window->is_embedded()) {
  58. Window *window_parent = Object::cast_to<Window>(window->get_parent()->get_viewport());
  59. while (window_parent) {
  60. if (!window_parent->is_embedded()) {
  61. mouse_pos_adjusted += window_parent->get_position();
  62. break;
  63. }
  64. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  65. }
  66. }
  67. set_process_internal(true);
  68. }
  69. }
  70. }
  71. void MenuButton::pressed() {
  72. if (popup->is_visible()) {
  73. popup->hide();
  74. return;
  75. }
  76. show_popup();
  77. }
  78. PopupMenu *MenuButton::get_popup() const {
  79. return popup;
  80. }
  81. void MenuButton::show_popup() {
  82. if (!get_viewport()) {
  83. return;
  84. }
  85. emit_signal(SNAME("about_to_popup"));
  86. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  87. popup->set_size(Size2(size.width, 0));
  88. Point2 gp = get_screen_position();
  89. gp.y += size.y;
  90. if (is_layout_rtl()) {
  91. gp.x += size.width - popup->get_size().width;
  92. }
  93. popup->set_position(gp);
  94. popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size));
  95. // If not triggered by the mouse, start the popup with its first enabled item focused.
  96. if (!_was_pressed_by_mouse()) {
  97. for (int i = 0; i < popup->get_item_count(); i++) {
  98. if (!popup->is_item_disabled(i)) {
  99. popup->set_focused_item(i);
  100. break;
  101. }
  102. }
  103. }
  104. popup->popup();
  105. }
  106. void MenuButton::set_switch_on_hover(bool p_enabled) {
  107. switch_on_hover = p_enabled;
  108. }
  109. bool MenuButton::is_switch_on_hover() {
  110. return switch_on_hover;
  111. }
  112. void MenuButton::set_item_count(int p_count) {
  113. ERR_FAIL_COND(p_count < 0);
  114. if (popup->get_item_count() == p_count) {
  115. return;
  116. }
  117. popup->set_item_count(p_count);
  118. notify_property_list_changed();
  119. }
  120. int MenuButton::get_item_count() const {
  121. return popup->get_item_count();
  122. }
  123. void MenuButton::_notification(int p_what) {
  124. switch (p_what) {
  125. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  126. popup->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  127. } break;
  128. case NOTIFICATION_VISIBILITY_CHANGED: {
  129. if (!is_visible_in_tree()) {
  130. popup->hide();
  131. }
  132. } break;
  133. case NOTIFICATION_INTERNAL_PROCESS: {
  134. Vector2i mouse_pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
  135. MenuButton *menu_btn_other = Object::cast_to<MenuButton>(get_viewport()->gui_find_control(mouse_pos));
  136. if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
  137. (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
  138. popup->hide();
  139. menu_btn_other->pressed();
  140. // As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
  141. menu_btn_other->get_popup()->set_focused_item(-1);
  142. }
  143. } break;
  144. }
  145. }
  146. bool MenuButton::_set(const StringName &p_name, const Variant &p_value) {
  147. Vector<String> components = String(p_name).split("/", true, 2);
  148. if (components.size() >= 2 && components[0] == "popup") {
  149. bool valid;
  150. popup->set(String(p_name).trim_prefix("popup/"), p_value, &valid);
  151. return valid;
  152. }
  153. return false;
  154. }
  155. bool MenuButton::_get(const StringName &p_name, Variant &r_ret) const {
  156. Vector<String> components = String(p_name).split("/", true, 2);
  157. if (components.size() >= 2 && components[0] == "popup") {
  158. bool valid;
  159. r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid);
  160. return valid;
  161. }
  162. return false;
  163. }
  164. void MenuButton::_get_property_list(List<PropertyInfo> *p_list) const {
  165. for (int i = 0; i < popup->get_item_count(); i++) {
  166. p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text", i)));
  167. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  168. pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  169. p_list->push_back(pi);
  170. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/checkable", i), PROPERTY_HINT_ENUM, "No,As Checkbox,As Radio Button");
  171. pi.usage &= ~(!popup->is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0);
  172. p_list->push_back(pi);
  173. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/checked", i));
  174. pi.usage &= ~(!popup->is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0);
  175. p_list->push_back(pi);
  176. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "0,10,1,or_greater");
  177. p_list->push_back(pi);
  178. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled", i));
  179. pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  180. p_list->push_back(pi);
  181. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator", i));
  182. pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  183. p_list->push_back(pi);
  184. }
  185. }
  186. void MenuButton::_bind_methods() {
  187. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  188. ClassDB::bind_method(D_METHOD("show_popup"), &MenuButton::show_popup);
  189. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  190. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  191. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  192. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MenuButton::set_item_count);
  193. ClassDB::bind_method(D_METHOD("get_item_count"), &MenuButton::get_item_count);
  194. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  195. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_");
  196. ADD_SIGNAL(MethodInfo("about_to_popup"));
  197. }
  198. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  199. disable_shortcuts = p_disabled;
  200. }
  201. MenuButton::MenuButton(const String &p_text) :
  202. Button(p_text) {
  203. set_flat(true);
  204. set_toggle_mode(true);
  205. set_disable_shortcuts(false);
  206. set_process_shortcut_input(true);
  207. set_focus_mode(FOCUS_NONE);
  208. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  209. popup = memnew(PopupMenu);
  210. popup->hide();
  211. add_child(popup, false, INTERNAL_MODE_FRONT);
  212. popup->connect("about_to_popup", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(true));
  213. popup->connect("popup_hide", callable_mp(this, &MenuButton::_popup_visibility_changed).bind(false));
  214. }
  215. MenuButton::~MenuButton() {
  216. }