Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/base_button.h
20903 views
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
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
/* Signals */
107
108
bool is_pressed() const; ///< return whether button is pressed (toggled in)
109
bool is_pressing() const; ///< return whether button is pressed (toggled in)
110
bool is_hovered() const;
111
112
void set_pressed(bool p_pressed); // Only works in toggle mode.
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
};
169
170