base_button.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #pragma once
  31. #include "core/input/shortcut.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. BitField<MouseButtonMask> button_mask = MouseButtonMask::LEFT;
  43. bool toggle_mode = false;
  44. bool shortcut_in_tooltip = true;
  45. bool was_mouse_pressed = false;
  46. bool keep_pressed_outside = false;
  47. bool shortcut_feedback = true;
  48. Ref<Shortcut> shortcut;
  49. ObjectID shortcut_context;
  50. ActionMode action_mode = ACTION_MODE_BUTTON_RELEASE;
  51. struct Status {
  52. bool pressed = false;
  53. bool hovering = false;
  54. bool press_attempt = false;
  55. bool pressing_inside = false;
  56. bool pressed_down_with_focus = 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. void _accessibility_action_click(const Variant &p_data);
  76. GDVIRTUAL0(_pressed)
  77. GDVIRTUAL1(_toggled, bool)
  78. public:
  79. enum DrawMode {
  80. DRAW_NORMAL,
  81. DRAW_PRESSED,
  82. DRAW_HOVER,
  83. DRAW_DISABLED,
  84. DRAW_HOVER_PRESSED,
  85. };
  86. DrawMode get_draw_mode() const;
  87. /* Signals */
  88. bool is_pressed() const; ///< return whether button is pressed (toggled in)
  89. bool is_pressing() const; ///< return whether button is pressed (toggled in)
  90. bool is_hovered() const;
  91. void set_pressed(bool p_pressed); // Only works in toggle mode.
  92. void set_pressed_no_signal(bool p_pressed);
  93. void set_toggle_mode(bool p_on);
  94. bool is_toggle_mode() const;
  95. void set_shortcut_in_tooltip(bool p_on);
  96. bool is_shortcut_in_tooltip_enabled() const;
  97. void set_disabled(bool p_disabled);
  98. bool is_disabled() const;
  99. void set_action_mode(ActionMode p_mode);
  100. ActionMode get_action_mode() const;
  101. void set_keep_pressed_outside(bool p_on);
  102. bool is_keep_pressed_outside() const;
  103. void set_shortcut_feedback(bool p_enable);
  104. bool is_shortcut_feedback() const;
  105. void set_button_mask(BitField<MouseButtonMask> p_mask);
  106. BitField<MouseButtonMask> get_button_mask() const;
  107. void set_shortcut(const Ref<Shortcut> &p_shortcut);
  108. Ref<Shortcut> get_shortcut() const;
  109. virtual Control *make_custom_tooltip(const String &p_text) const override;
  110. void set_button_group(const Ref<ButtonGroup> &p_group);
  111. Ref<ButtonGroup> get_button_group() const;
  112. PackedStringArray get_configuration_warnings() const override;
  113. BaseButton();
  114. ~BaseButton();
  115. };
  116. VARIANT_ENUM_CAST(BaseButton::DrawMode)
  117. VARIANT_ENUM_CAST(BaseButton::ActionMode)
  118. class ButtonGroup : public Resource {
  119. GDCLASS(ButtonGroup, Resource);
  120. friend class BaseButton;
  121. HashSet<BaseButton *> buttons;
  122. bool allow_unpress = false;
  123. protected:
  124. static void _bind_methods();
  125. public:
  126. BaseButton *get_pressed_button();
  127. void get_buttons(List<BaseButton *> *r_buttons);
  128. TypedArray<BaseButton> _get_buttons();
  129. void set_allow_unpress(bool p_enabled);
  130. bool is_allow_unpress();
  131. ButtonGroup();
  132. };