menu_button.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* menu_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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::unhandled_key_input(const Ref<InputEvent> &p_event) {
  34. ERR_FAIL_COND(p_event.is_null());
  35. if (!_is_focus_owner_in_shorcut_context()) {
  36. return;
  37. }
  38. if (disable_shortcuts) {
  39. return;
  40. }
  41. if (p_event->is_pressed() && !p_event->is_echo() && (Object::cast_to<InputEventKey>(p_event.ptr()) || Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventAction>(*p_event) || Object::cast_to<InputEventShortcut>(*p_event))) {
  42. if (!get_parent() || !is_visible_in_tree() || is_disabled()) {
  43. return;
  44. }
  45. if (popup->activate_item_by_event(p_event, false)) {
  46. accept_event();
  47. }
  48. }
  49. }
  50. void MenuButton::_popup_visibility_changed(bool p_visible) {
  51. set_pressed(p_visible);
  52. if (!p_visible) {
  53. set_process_internal(false);
  54. return;
  55. }
  56. if (switch_on_hover) {
  57. Window *window = Object::cast_to<Window>(get_viewport());
  58. if (window) {
  59. mouse_pos_adjusted = window->get_position();
  60. if (window->is_embedded()) {
  61. Window *window_parent = Object::cast_to<Window>(window->get_parent()->get_viewport());
  62. while (window_parent) {
  63. if (!window_parent->is_embedded()) {
  64. mouse_pos_adjusted += window_parent->get_position();
  65. break;
  66. }
  67. window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
  68. }
  69. }
  70. set_process_internal(true);
  71. }
  72. }
  73. }
  74. void MenuButton::pressed() {
  75. emit_signal(SNAME("about_to_popup"));
  76. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  77. popup->set_size(Size2(size.width, 0));
  78. Point2 gp = get_screen_position();
  79. gp.y += size.y;
  80. if (is_layout_rtl()) {
  81. gp.x += size.width - popup->get_size().width;
  82. }
  83. popup->set_position(gp);
  84. popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size));
  85. popup->take_mouse_focus();
  86. popup->popup();
  87. }
  88. void MenuButton::gui_input(const Ref<InputEvent> &p_event) {
  89. BaseButton::gui_input(p_event);
  90. }
  91. PopupMenu *MenuButton::get_popup() const {
  92. return popup;
  93. }
  94. void MenuButton::set_switch_on_hover(bool p_enabled) {
  95. switch_on_hover = p_enabled;
  96. }
  97. bool MenuButton::is_switch_on_hover() {
  98. return switch_on_hover;
  99. }
  100. void MenuButton::set_item_count(int p_count) {
  101. ERR_FAIL_COND(p_count < 0);
  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_VISIBILITY_CHANGED: {
  111. if (!is_visible_in_tree()) {
  112. popup->hide();
  113. }
  114. } break;
  115. case NOTIFICATION_INTERNAL_PROCESS: {
  116. Vector2i mouse_pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
  117. MenuButton *menu_btn_other = Object::cast_to<MenuButton>(get_viewport()->gui_find_control(mouse_pos));
  118. if (menu_btn_other && menu_btn_other != this && menu_btn_other->is_switch_on_hover() && !menu_btn_other->is_disabled() &&
  119. (get_parent()->is_ancestor_of(menu_btn_other) || menu_btn_other->get_parent()->is_ancestor_of(popup))) {
  120. popup->hide();
  121. menu_btn_other->pressed();
  122. }
  123. } break;
  124. }
  125. }
  126. bool MenuButton::_set(const StringName &p_name, const Variant &p_value) {
  127. Vector<String> components = String(p_name).split("/", true, 2);
  128. if (components.size() >= 2 && components[0] == "popup") {
  129. bool valid;
  130. popup->set(String(p_name).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. Vector<String> components = String(p_name).split("/", true, 2);
  137. if (components.size() >= 2 && components[0] == "popup") {
  138. bool valid;
  139. r_ret = popup->get(String(p_name).trim_prefix("popup/"), &valid);
  140. return valid;
  141. }
  142. return false;
  143. }
  144. void MenuButton::_get_property_list(List<PropertyInfo> *p_list) const {
  145. for (int i = 0; i < popup->get_item_count(); i++) {
  146. p_list->push_back(PropertyInfo(Variant::STRING, vformat("popup/item_%d/text", i)));
  147. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("popup/item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  148. pi.usage &= ~(popup->get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  149. p_list->push_back(pi);
  150. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/checkable", i), PROPERTY_HINT_ENUM, "No,As checkbox,As radio button");
  151. pi.usage &= ~(!popup->is_item_checkable(i) ? PROPERTY_USAGE_STORAGE : 0);
  152. p_list->push_back(pi);
  153. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/checked", i));
  154. pi.usage &= ~(!popup->is_item_checked(i) ? PROPERTY_USAGE_STORAGE : 0);
  155. p_list->push_back(pi);
  156. pi = PropertyInfo(Variant::INT, vformat("popup/item_%d/id", i), PROPERTY_HINT_RANGE, "1,10,1,or_greater");
  157. p_list->push_back(pi);
  158. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/disabled", i));
  159. pi.usage &= ~(!popup->is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  160. p_list->push_back(pi);
  161. pi = PropertyInfo(Variant::BOOL, vformat("popup/item_%d/separator", i));
  162. pi.usage &= ~(!popup->is_item_separator(i) ? PROPERTY_USAGE_STORAGE : 0);
  163. p_list->push_back(pi);
  164. }
  165. }
  166. void MenuButton::_bind_methods() {
  167. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  168. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  169. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  170. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  171. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &MenuButton::set_item_count);
  172. ClassDB::bind_method(D_METHOD("get_item_count"), &MenuButton::get_item_count);
  173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  174. ADD_ARRAY_COUNT("Items", "items_count", "set_item_count", "get_item_count", "popup/item_");
  175. ADD_SIGNAL(MethodInfo("about_to_popup"));
  176. }
  177. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  178. disable_shortcuts = p_disabled;
  179. }
  180. MenuButton::MenuButton() {
  181. set_flat(true);
  182. set_toggle_mode(true);
  183. set_disable_shortcuts(false);
  184. set_process_unhandled_key_input(true);
  185. set_focus_mode(FOCUS_NONE);
  186. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  187. popup = memnew(PopupMenu);
  188. popup->hide();
  189. add_child(popup, false, INTERNAL_MODE_FRONT);
  190. popup->connect("about_to_popup", callable_mp(this, &MenuButton::_popup_visibility_changed), varray(true));
  191. popup->connect("popup_hide", callable_mp(this, &MenuButton::_popup_visibility_changed), varray(false));
  192. }
  193. MenuButton::~MenuButton() {
  194. }