menu_button.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/viewport.h"
  33. void MenuButton::_unhandled_key_input(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() && !p_event->is_echo() && (Object::cast_to<InputEventKey>(p_event.ptr()) || Object::cast_to<InputEventJoypadButton>(p_event.ptr()) || Object::cast_to<InputEventAction>(*p_event))) {
  39. if (!get_parent() || !is_visible_in_tree() || is_disabled()) {
  40. return;
  41. }
  42. bool global_only = (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this));
  43. if (popup->activate_item_by_event(p_event, global_only)) {
  44. accept_event();
  45. }
  46. }
  47. }
  48. void MenuButton::pressed() {
  49. emit_signal("about_to_show");
  50. Size2 size = get_size();
  51. Point2 gp = get_global_position();
  52. popup->set_global_position(gp + Size2(0, size.height * get_global_transform().get_scale().y));
  53. popup->set_size(Size2(size.width, 0));
  54. popup->set_scale(get_global_transform().get_scale());
  55. popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_position()), get_size()));
  56. popup->popup();
  57. }
  58. void MenuButton::_gui_input(Ref<InputEvent> p_event) {
  59. BaseButton::_gui_input(p_event);
  60. }
  61. PopupMenu *MenuButton::get_popup() const {
  62. return popup;
  63. }
  64. void MenuButton::_set_items(const Array &p_items) {
  65. popup->set("items", p_items);
  66. }
  67. Array MenuButton::_get_items() const {
  68. return popup->get("items");
  69. }
  70. void MenuButton::set_switch_on_hover(bool p_enabled) {
  71. switch_on_hover = p_enabled;
  72. }
  73. bool MenuButton::is_switch_on_hover() {
  74. return switch_on_hover;
  75. }
  76. void MenuButton::_notification(int p_what) {
  77. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  78. if (!is_visible_in_tree()) {
  79. popup->hide();
  80. }
  81. }
  82. }
  83. void MenuButton::_bind_methods() {
  84. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  85. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &MenuButton::_unhandled_key_input);
  86. ClassDB::bind_method(D_METHOD("_set_items"), &MenuButton::_set_items);
  87. ClassDB::bind_method(D_METHOD("_get_items"), &MenuButton::_get_items);
  88. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  89. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  90. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  91. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  92. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  93. ADD_SIGNAL(MethodInfo("about_to_show"));
  94. }
  95. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  96. disable_shortcuts = p_disabled;
  97. }
  98. MenuButton::MenuButton() {
  99. switch_on_hover = false;
  100. set_flat(true);
  101. set_toggle_mode(true);
  102. set_disable_shortcuts(false);
  103. set_focus_mode(FOCUS_NONE);
  104. set_process_unhandled_key_input(true);
  105. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  106. popup = memnew(PopupMenu);
  107. popup->hide();
  108. add_child(popup);
  109. popup->set_pass_on_modal_close_click(false);
  110. popup->connect("about_to_show", this, "set_pressed", varray(true)); // For when switching from another MenuButton.
  111. popup->connect("popup_hide", this, "set_pressed", varray(false));
  112. }
  113. MenuButton::~MenuButton() {
  114. }