menu_button.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*************************************************************************/
  2. /* menu_button.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "menu_button.h"
  30. #include "os/keyboard.h"
  31. void MenuButton::_unhandled_key_input(InputEvent p_event) {
  32. //check accelerators
  33. if (p_event.type==InputEvent::KEY && p_event.key.pressed) {
  34. if (!get_parent() || !is_visible() || is_disabled())
  35. return;
  36. uint32_t code=p_event.key.scancode;
  37. if (code==0)
  38. code=p_event.key.unicode;
  39. if (p_event.key.mod.control)
  40. code|=KEY_MASK_CTRL;
  41. if (p_event.key.mod.alt)
  42. code|=KEY_MASK_ALT;
  43. if (p_event.key.mod.meta)
  44. code|=KEY_MASK_META;
  45. if (p_event.key.mod.shift)
  46. code|=KEY_MASK_SHIFT;
  47. int item = popup->find_item_by_accelerator(code);
  48. if (item>=0 && ! popup->is_item_disabled(item))
  49. popup->activate_item(item);
  50. /*
  51. for(int i=0;i<items.size();i++) {
  52. if (items[i].accel==0)
  53. continue;
  54. if (items[i].accel==code) {
  55. emit_signal("item_pressed",items[i].ID);
  56. }
  57. }*/
  58. }
  59. }
  60. void MenuButton::pressed() {
  61. emit_signal("about_to_show");
  62. Size2 size=get_size();
  63. Point2 gp = get_global_pos();
  64. popup->set_global_pos( gp + Size2( 0, size.height ) );
  65. popup->set_size( Size2( size.width, 0) );
  66. popup->set_parent_rect( Rect2(Point2(gp-popup->get_global_pos()),get_size()));
  67. popup->popup();
  68. popup->call_deferred("grab_click_focus");
  69. }
  70. void MenuButton::_input_event(InputEvent p_event) {
  71. /*if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT) {
  72. clicked=p_event.mouse_button.pressed;
  73. }
  74. if (clicked && p_event.type==InputEvent::MOUSE_MOTION && popup->is_visible()) {
  75. Point2 gt = Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  76. gt = get_global_transform().xform(gt);
  77. Point2 lt = popup->get_transform().affine_inverse().xform(gt);
  78. if (popup->has_point(lt)) {
  79. //print_line("HAS POINT!!!");
  80. popup->call_deferred("grab_click_focus");
  81. }
  82. }*/
  83. BaseButton::_input_event(p_event);
  84. }
  85. PopupMenu *MenuButton::get_popup() {
  86. return popup;
  87. }
  88. Array MenuButton::_get_items() const {
  89. return popup->get("items");
  90. }
  91. void MenuButton::_set_items(const Array& p_items) {
  92. popup->set("items",p_items);
  93. }
  94. void MenuButton::_bind_methods() {
  95. ObjectTypeDB::bind_method(_MD("get_popup"),&MenuButton::get_popup);
  96. ObjectTypeDB::bind_method(_MD("_unhandled_key_input"),&MenuButton::_unhandled_key_input);
  97. ObjectTypeDB::bind_method(_MD("_set_items"),&MenuButton::_set_items);
  98. ObjectTypeDB::bind_method(_MD("_get_items"),&MenuButton::_get_items);
  99. ADD_PROPERTY( PropertyInfo(Variant::ARRAY,"items",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_items"),_SCS("_get_items") );
  100. ADD_SIGNAL( MethodInfo("about_to_show") );
  101. }
  102. MenuButton::MenuButton() {
  103. set_flat(true);
  104. set_focus_mode(FOCUS_NONE);
  105. popup = memnew( PopupMenu );
  106. popup->hide();
  107. add_child(popup);
  108. popup->set_as_toplevel(true);
  109. set_process_unhandled_key_input(true);
  110. set_click_on_press(true);
  111. }
  112. MenuButton::~MenuButton() {
  113. }