base_button.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "core/input/shortcut.h"
  33. #include "scene/gui/control.h"
  34. class ButtonGroup;
  35. class BaseButton : public Control {
  36. GDCLASS(BaseButton, Control);
  37. public:
  38. enum ActionMode {
  39. ACTION_MODE_BUTTON_PRESS,
  40. ACTION_MODE_BUTTON_RELEASE,
  41. };
  42. private:
  43. BitField<MouseButtonMask> button_mask = MouseButtonMask::LEFT;
  44. bool toggle_mode = false;
  45. bool shortcut_in_tooltip = true;
  46. bool was_mouse_pressed = false;
  47. bool keep_pressed_outside = false;
  48. bool shortcut_feedback = true;
  49. Ref<Shortcut> shortcut;
  50. ObjectID shortcut_context;
  51. ActionMode action_mode = ACTION_MODE_BUTTON_RELEASE;
  52. struct Status {
  53. bool pressed = false;
  54. bool hovering = false;
  55. bool press_attempt = false;
  56. bool pressing_inside = false;
  57. bool disabled = false;
  58. } status;
  59. Ref<ButtonGroup> button_group;
  60. void _unpress_group();
  61. void _pressed();
  62. void _toggled(bool p_pressed);
  63. void on_action_event(Ref<InputEvent> p_event);
  64. Timer *shortcut_feedback_timer = nullptr;
  65. bool in_shortcut_feedback = false;
  66. void _shortcut_feedback_timeout();
  67. protected:
  68. virtual void pressed();
  69. virtual void toggled(bool p_pressed);
  70. static void _bind_methods();
  71. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  72. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  73. void _notification(int p_what);
  74. bool _was_pressed_by_mouse() const;
  75. GDVIRTUAL0(_pressed)
  76. GDVIRTUAL1(_toggled, bool)
  77. public:
  78. enum DrawMode {
  79. DRAW_NORMAL,
  80. DRAW_PRESSED,
  81. DRAW_HOVER,
  82. DRAW_DISABLED,
  83. DRAW_HOVER_PRESSED,
  84. };
  85. DrawMode get_draw_mode() const;
  86. /* Signals */
  87. bool is_pressed() const; ///< return whether button is pressed (toggled in)
  88. bool is_pressing() const; ///< return whether button is pressed (toggled in)
  89. bool is_hovered() const;
  90. void set_pressed(bool p_pressed); // Only works in toggle mode.
  91. void set_pressed_no_signal(bool p_pressed);
  92. void set_toggle_mode(bool p_on);
  93. bool is_toggle_mode() const;
  94. void set_shortcut_in_tooltip(bool p_on);
  95. bool is_shortcut_in_tooltip_enabled() const;
  96. void set_disabled(bool p_disabled);
  97. bool is_disabled() const;
  98. void set_action_mode(ActionMode p_mode);
  99. ActionMode get_action_mode() const;
  100. void set_keep_pressed_outside(bool p_on);
  101. bool is_keep_pressed_outside() const;
  102. void set_shortcut_feedback(bool p_enable);
  103. bool is_shortcut_feedback() const;
  104. void set_button_mask(BitField<MouseButtonMask> p_mask);
  105. BitField<MouseButtonMask> get_button_mask() const;
  106. void set_shortcut(const Ref<Shortcut> &p_shortcut);
  107. Ref<Shortcut> get_shortcut() const;
  108. virtual Control *make_custom_tooltip(const String &p_text) const override;
  109. void set_button_group(const Ref<ButtonGroup> &p_group);
  110. Ref<ButtonGroup> get_button_group() const;
  111. PackedStringArray get_configuration_warnings() const override;
  112. BaseButton();
  113. ~BaseButton();
  114. };
  115. VARIANT_ENUM_CAST(BaseButton::DrawMode)
  116. VARIANT_ENUM_CAST(BaseButton::ActionMode)
  117. class ButtonGroup : public Resource {
  118. GDCLASS(ButtonGroup, Resource);
  119. friend class BaseButton;
  120. HashSet<BaseButton *> buttons;
  121. bool allow_unpress = false;
  122. protected:
  123. static void _bind_methods();
  124. public:
  125. BaseButton *get_pressed_button();
  126. void get_buttons(List<BaseButton *> *r_buttons);
  127. TypedArray<BaseButton> _get_buttons();
  128. void set_allow_unpress(bool p_enabled);
  129. bool is_allow_unpress();
  130. ButtonGroup();
  131. };
  132. #endif // BASE_BUTTON_H