base_button.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**************************************************************************/
  2. /* base_button.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifndef BASE_BUTTON_H
  31. #define BASE_BUTTON_H
  32. #include "scene/gui/control.h"
  33. class ButtonGroup;
  34. class BaseButton : public Control {
  35. GDCLASS(BaseButton, Control);
  36. public:
  37. enum ActionMode {
  38. ACTION_MODE_BUTTON_PRESS,
  39. ACTION_MODE_BUTTON_RELEASE,
  40. };
  41. private:
  42. int button_mask;
  43. bool toggle_mode;
  44. bool shortcut_in_tooltip;
  45. bool keep_pressed_outside;
  46. bool was_mouse_pressed;
  47. FocusMode enabled_focus_mode;
  48. Ref<ShortCut> shortcut;
  49. ActionMode action_mode;
  50. struct Status {
  51. bool pressed;
  52. bool hovering;
  53. bool press_attempt;
  54. bool pressing_inside;
  55. bool disabled;
  56. } status;
  57. Ref<ButtonGroup> button_group;
  58. void _unpress_group();
  59. void _pressed();
  60. void _toggled(bool p_pressed);
  61. void on_action_event(Ref<InputEvent> p_event);
  62. protected:
  63. virtual void pressed();
  64. virtual void toggled(bool p_pressed);
  65. static void _bind_methods();
  66. virtual void _gui_input(Ref<InputEvent> p_event);
  67. virtual void _unhandled_input(Ref<InputEvent> p_event);
  68. void _notification(int p_what);
  69. bool _was_pressed_by_mouse() const;
  70. public:
  71. enum DrawMode {
  72. DRAW_NORMAL,
  73. DRAW_PRESSED,
  74. DRAW_HOVER,
  75. DRAW_DISABLED,
  76. DRAW_HOVER_PRESSED,
  77. };
  78. DrawMode get_draw_mode() const;
  79. /* Signals */
  80. bool is_pressed() const; ///< return whether button is pressed (toggled in)
  81. bool is_pressing() const; ///< return whether button is pressed (toggled in)
  82. bool is_hovered() const;
  83. void set_pressed(bool p_pressed); // Only works in toggle mode.
  84. void set_pressed_no_signal(bool p_pressed);
  85. void set_toggle_mode(bool p_on);
  86. bool is_toggle_mode() const;
  87. void set_shortcut_in_tooltip(bool p_on);
  88. bool is_shortcut_in_tooltip_enabled() const;
  89. void set_disabled(bool p_disabled);
  90. bool is_disabled() const;
  91. void set_action_mode(ActionMode p_mode);
  92. ActionMode get_action_mode() const;
  93. void set_keep_pressed_outside(bool p_on);
  94. bool is_keep_pressed_outside() const;
  95. void set_button_mask(int p_mask);
  96. int get_button_mask() const;
  97. void set_enabled_focus_mode(FocusMode p_mode);
  98. FocusMode get_enabled_focus_mode() const;
  99. void set_shortcut(const Ref<ShortCut> &p_shortcut);
  100. Ref<ShortCut> get_shortcut() const;
  101. virtual String get_tooltip(const Point2 &p_pos) const;
  102. void set_button_group(const Ref<ButtonGroup> &p_group);
  103. Ref<ButtonGroup> get_button_group() const;
  104. BaseButton();
  105. ~BaseButton();
  106. };
  107. VARIANT_ENUM_CAST(BaseButton::DrawMode)
  108. VARIANT_ENUM_CAST(BaseButton::ActionMode)
  109. class ButtonGroup : public Resource {
  110. GDCLASS(ButtonGroup, Resource);
  111. friend class BaseButton;
  112. Set<BaseButton *> buttons;
  113. protected:
  114. static void _bind_methods();
  115. public:
  116. BaseButton *get_pressed_button();
  117. void get_buttons(List<BaseButton *> *r_buttons);
  118. Array _get_buttons();
  119. ButtonGroup();
  120. };
  121. #endif // BASE_BUTTON_H