menu_button.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************/
  2. /* menu_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/keyboard.h"
  32. #include "scene/main/viewport.h"
  33. void MenuButton::_unhandled_key_input(Ref<InputEvent> p_event) {
  34. 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))) {
  35. if (!get_parent() || !is_visible_in_tree() || is_disabled())
  36. return;
  37. bool global_only = (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this));
  38. if (popup->activate_item_by_event(p_event, global_only))
  39. accept_event();
  40. }
  41. }
  42. void MenuButton::pressed() {
  43. emit_signal("about_to_show");
  44. Size2 size = get_size();
  45. Point2 gp = get_global_position();
  46. popup->set_global_position(gp + Size2(0, size.height));
  47. popup->set_size(Size2(size.width, 0));
  48. popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_position()), get_size()));
  49. popup->popup();
  50. popup->set_invalidate_click_until_motion();
  51. }
  52. void MenuButton::_gui_input(Ref<InputEvent> p_event) {
  53. /*if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==BUTTON_LEFT) {
  54. clicked=p_event->is_pressed();
  55. }
  56. if (clicked && p_event.type==InputEvent::MOUSE_MOTION && popup->is_visible_in_tree()) {
  57. Point2 gt = Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  58. gt = get_global_transform().xform(gt);
  59. Point2 lt = popup->get_transform().affine_inverse().xform(gt);
  60. if (popup->has_point(lt)) {
  61. //print_line("HAS POINT!!!");
  62. popup->call_deferred("grab_click_focus");
  63. }
  64. }*/
  65. BaseButton::_gui_input(p_event);
  66. }
  67. PopupMenu *MenuButton::get_popup() {
  68. return popup;
  69. }
  70. Array MenuButton::_get_items() const {
  71. return popup->get("items");
  72. }
  73. void MenuButton::_set_items(const Array &p_items) {
  74. popup->set("items", p_items);
  75. }
  76. void MenuButton::_bind_methods() {
  77. ClassDB::bind_method(D_METHOD("get_popup"), &MenuButton::get_popup);
  78. ClassDB::bind_method(D_METHOD("_unhandled_key_input"), &MenuButton::_unhandled_key_input);
  79. ClassDB::bind_method(D_METHOD("_set_items"), &MenuButton::_set_items);
  80. ClassDB::bind_method(D_METHOD("_get_items"), &MenuButton::_get_items);
  81. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items");
  82. ADD_SIGNAL(MethodInfo("about_to_show"));
  83. }
  84. MenuButton::MenuButton() {
  85. set_flat(true);
  86. set_enabled_focus_mode(FOCUS_NONE);
  87. popup = memnew(PopupMenu);
  88. popup->hide();
  89. add_child(popup);
  90. popup->set_as_toplevel(true);
  91. connect("button_up", popup, "call_deferred", make_binds("grab_click_focus"));
  92. set_process_unhandled_key_input(true);
  93. set_action_mode(ACTION_MODE_BUTTON_PRESS);
  94. }
  95. MenuButton::~MenuButton() {
  96. }