| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/input/shortcut.h" |
| 34 |
#include "scene/gui/control.h" |
| 35 |
|
| 36 |
class ButtonGroup; |
| 37 |
class Timer; |
| 38 |
|
| 39 |
class BaseButton : public Control { |
| 40 |
GDCLASS(BaseButton, Control); |
| 41 |
|
| 42 |
public: |
| 43 |
enum ActionMode { |
| 44 |
ACTION_MODE_BUTTON_PRESS, |
| 45 |
ACTION_MODE_BUTTON_RELEASE, |
| 46 |
}; |
| 47 |
|
| 48 |
private: |
| 49 |
BitField<MouseButtonMask> button_mask = MouseButtonMask::LEFT; |
| 50 |
bool toggle_mode = false; |
| 51 |
bool shortcut_in_tooltip = true; |
| 52 |
bool was_mouse_pressed = false; |
| 53 |
bool keep_pressed_outside = false; |
| 54 |
bool shortcut_feedback = true; |
| 55 |
Ref<Shortcut> shortcut; |
| 56 |
ObjectID shortcut_context; |
| 57 |
|
| 58 |
ActionMode action_mode = ACTION_MODE_BUTTON_RELEASE; |
| 59 |
struct Status { |
| 60 |
bool pressed = false; |
| 61 |
bool hovering = false; |
| 62 |
bool press_attempt = false; |
| 63 |
bool pressing_inside = false; |
| 64 |
bool pressed_down_with_focus = false; |
| 65 |
bool disabled = false; |
| 66 |
|
| 67 |
} status; |
| 68 |
|
| 69 |
Ref<ButtonGroup> button_group; |
| 70 |
|
| 71 |
void _unpress_group(); |
| 72 |
void _pressed(); |
| 73 |
void _toggled(bool p_pressed); |
| 74 |
|
| 75 |
void on_action_event(Ref<InputEvent> p_event); |
| 76 |
|
| 77 |
Timer *shortcut_feedback_timer = nullptr; |
| 78 |
bool in_shortcut_feedback = false; |
| 79 |
void _shortcut_feedback_timeout(); |
| 80 |
|
| 81 |
protected: |
| 82 |
virtual void pressed(); |
| 83 |
virtual void toggled(bool p_pressed); |
| 84 |
static void _bind_methods(); |
| 85 |
virtual void gui_input(const Ref<InputEvent> &p_event) override; |
| 86 |
virtual void shortcut_input(const Ref<InputEvent> &p_event) override; |
| 87 |
void _notification(int p_what); |
| 88 |
|
| 89 |
bool _was_pressed_by_mouse() const; |
| 90 |
void _accessibility_action_click(const Variant &p_data); |
| 91 |
|
| 92 |
GDVIRTUAL0(_pressed) |
| 93 |
GDVIRTUAL1(_toggled, bool) |
| 94 |
|
| 95 |
public: |
| 96 |
enum DrawMode { |
| 97 |
DRAW_NORMAL, |
| 98 |
DRAW_PRESSED, |
| 99 |
DRAW_HOVER, |
| 100 |
DRAW_DISABLED, |
| 101 |
DRAW_HOVER_PRESSED, |
| 102 |
}; |
| 103 |
|
| 104 |
DrawMode get_draw_mode() const; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
bool is_pressed() const; |
| 109 |
bool is_pressing() const; |
| 110 |
bool is_hovered() const; |
| 111 |
|
| 112 |
void set_pressed(bool p_pressed); |
| 113 |
void set_pressed_no_signal(bool p_pressed); |
| 114 |
void set_toggle_mode(bool p_on); |
| 115 |
bool is_toggle_mode() const; |
| 116 |
|
| 117 |
void set_shortcut_in_tooltip(bool p_on); |
| 118 |
bool is_shortcut_in_tooltip_enabled() const; |
| 119 |
|
| 120 |
void set_disabled(bool p_disabled); |
| 121 |
bool is_disabled() const; |
| 122 |
|
| 123 |
void set_action_mode(ActionMode p_mode); |
| 124 |
ActionMode get_action_mode() const; |
| 125 |
|
| 126 |
void set_keep_pressed_outside(bool p_on); |
| 127 |
bool is_keep_pressed_outside() const; |
| 128 |
|
| 129 |
void set_shortcut_feedback(bool p_enable); |
| 130 |
bool is_shortcut_feedback() const; |
| 131 |
|
| 132 |
void set_button_mask(BitField<MouseButtonMask> p_mask); |
| 133 |
BitField<MouseButtonMask> get_button_mask() const; |
| 134 |
|
| 135 |
void set_shortcut(const Ref<Shortcut> &p_shortcut); |
| 136 |
Ref<Shortcut> get_shortcut() const; |
| 137 |
|
| 138 |
virtual Control *make_custom_tooltip(const String &p_text) const override; |
| 139 |
|
| 140 |
void set_button_group(const Ref<ButtonGroup> &p_group); |
| 141 |
Ref<ButtonGroup> get_button_group() const; |
| 142 |
|
| 143 |
PackedStringArray get_configuration_warnings() const override; |
| 144 |
|
| 145 |
BaseButton(); |
| 146 |
~BaseButton(); |
| 147 |
}; |
| 148 |
|
| 149 |
VARIANT_ENUM_CAST(BaseButton::DrawMode) |
| 150 |
VARIANT_ENUM_CAST(BaseButton::ActionMode) |
| 151 |
|
| 152 |
class ButtonGroup : public Resource { |
| 153 |
GDCLASS(ButtonGroup, Resource); |
| 154 |
friend class BaseButton; |
| 155 |
HashSet<BaseButton *> buttons; |
| 156 |
bool allow_unpress = false; |
| 157 |
|
| 158 |
protected: |
| 159 |
static void _bind_methods(); |
| 160 |
|
| 161 |
public: |
| 162 |
BaseButton *get_pressed_button(); |
| 163 |
void get_buttons(List<BaseButton *> *r_buttons); |
| 164 |
TypedArray<BaseButton> _get_buttons(); |
| 165 |
void set_allow_unpress(bool p_enabled); |
| 166 |
bool is_allow_unpress(); |
| 167 |
ButtonGroup(); |
| 168 |
}; |