menu_button.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* menu_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. if (disable_shortcuts)
  35. return;
  36. 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))) {
  37. if (!get_parent() || !is_visible_in_tree() || is_disabled())
  38. return;
  39. bool global_only = (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this));
  40. if (popup->activate_item_by_event(p_event, global_only))
  41. accept_event();
  42. }
  43. }
  44. void MenuButton::pressed() {
  45. emit_signal("about_to_show");
  46. Size2 size = get_size();
  47. Point2 gp = get_global_position();
  48. popup->set_global_position(gp + Size2(0, size.height * get_global_transform().get_scale().y));
  49. popup->set_size(Size2(size.width, 0));
  50. popup->set_scale(get_global_transform().get_scale());
  51. popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_position()), get_size()));
  52. popup->popup();
  53. }
  54. void MenuButton::_gui_input(Ref<InputEvent> p_event) {
  55. BaseButton::_gui_input(p_event);
  56. }
  57. PopupMenu *MenuButton::get_popup() const {
  58. return popup;
  59. }
  60. void MenuButton::_set_items(const Array &p_items) {
  61. popup->set("items", p_items);
  62. }
  63. Array MenuButton::_get_items() const {
  64. return popup->get("items");
  65. }
  66. void MenuButton::set_switch_on_hover(bool p_enabled) {
  67. switch_on_hover = p_enabled;
  68. }
  69. bool MenuButton::is_switch_on_hover() {
  70. return switch_on_hover;
  71. }
  72. void MenuButton::_notification(int p_what) {
  73. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  74. if (!is_visible_in_tree()) {
  75. popup->hide();
  76. }
  77. }
  78. }
  79. void MenuButton::_bind_methods() {
  80. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  81. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &MenuButton::_unhandled_key_input);
  82. ClassDB::bind_method(D_METHOD("_set_items"), &MenuButton::_set_items);
  83. ClassDB::bind_method(D_METHOD("_get_items"), &MenuButton::_get_items);
  84. ClassDB::bind_method(D_METHOD("set_switch_on_hover", "enable"), &MenuButton::set_switch_on_hover);
  85. ClassDB::bind_method(D_METHOD("is_switch_on_hover"), &MenuButton::is_switch_on_hover);
  86. ClassDB::bind_method(D_METHOD("set_disable_shortcuts", "disabled"), &MenuButton::set_disable_shortcuts);
  87. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_items", "_get_items");
  88. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "switch_on_hover"), "set_switch_on_hover", "is_switch_on_hover");
  89. ADD_SIGNAL(MethodInfo("about_to_show"));
  90. }
  91. void MenuButton::set_disable_shortcuts(bool p_disabled) {
  92. disable_shortcuts = p_disabled;
  93. }
  94. MenuButton::MenuButton() {
  95. switch_on_hover = false;
  96. set_flat(true);
  97. set_toggle_mode(true);
  98. set_disable_shortcuts(false);
  99. set_enabled_focus_mode(FOCUS_NONE);
  100. set_process_unhandled_key_input(true);
  101. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  102. popup = memnew(PopupMenu);
  103. popup->hide();
  104. add_child(popup);
  105. popup->set_pass_on_modal_close_click(false);
  106. popup->connect("about_to_show", this, "set_pressed", varray(true)); // For when switching from another MenuButton.
  107. popup->connect("popup_hide", this, "set_pressed", varray(false));
  108. }
  109. MenuButton::~MenuButton() {
  110. }