Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/base_button.h
9903 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
38
class BaseButton : public Control {
39
GDCLASS(BaseButton, Control);
40
41
public:
42
enum ActionMode {
43
ACTION_MODE_BUTTON_PRESS,
44
ACTION_MODE_BUTTON_RELEASE,
45
};
46
47
private:
48
BitField<MouseButtonMask> button_mask = MouseButtonMask::LEFT;
49
bool toggle_mode = false;
50
bool shortcut_in_tooltip = true;
51
bool was_mouse_pressed = false;
52
bool keep_pressed_outside = false;
53
bool shortcut_feedback = true;
54
Ref<Shortcut> shortcut;
55
ObjectID shortcut_context;
56
57
ActionMode action_mode = ACTION_MODE_BUTTON_RELEASE;
58
struct Status {
59
bool pressed = false;
60
bool hovering = false;
61
bool press_attempt = false;
62
bool pressing_inside = false;
63
bool pressed_down_with_focus = false;
64
bool disabled = false;
65
66
} status;
67
68
Ref<ButtonGroup> button_group;
69
70
void _unpress_group();
71
void _pressed();
72
void _toggled(bool p_pressed);
73
74
void on_action_event(Ref<InputEvent> p_event);
75
76
Timer *shortcut_feedback_timer = nullptr;
77
bool in_shortcut_feedback = false;
78
void _shortcut_feedback_timeout();
79
80
protected:
81
virtual void pressed();
82
virtual void toggled(bool p_pressed);
83
static void _bind_methods();
84
virtual void gui_input(const Ref<InputEvent> &p_event) override;
85
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
86
void _notification(int p_what);
87
88
bool _was_pressed_by_mouse() const;
89
void _accessibility_action_click(const Variant &p_data);
90
91
GDVIRTUAL0(_pressed)
92
GDVIRTUAL1(_toggled, bool)
93
94
public:
95
enum DrawMode {
96
DRAW_NORMAL,
97
DRAW_PRESSED,
98
DRAW_HOVER,
99
DRAW_DISABLED,
100
DRAW_HOVER_PRESSED,
101
};
102
103
DrawMode get_draw_mode() const;
104
105
/* Signals */
106
107
bool is_pressed() const; ///< return whether button is pressed (toggled in)
108
bool is_pressing() const; ///< return whether button is pressed (toggled in)
109
bool is_hovered() const;
110
111
void set_pressed(bool p_pressed); // Only works in toggle mode.
112
void set_pressed_no_signal(bool p_pressed);
113
void set_toggle_mode(bool p_on);
114
bool is_toggle_mode() const;
115
116
void set_shortcut_in_tooltip(bool p_on);
117
bool is_shortcut_in_tooltip_enabled() const;
118
119
void set_disabled(bool p_disabled);
120
bool is_disabled() const;
121
122
void set_action_mode(ActionMode p_mode);
123
ActionMode get_action_mode() const;
124
125
void set_keep_pressed_outside(bool p_on);
126
bool is_keep_pressed_outside() const;
127
128
void set_shortcut_feedback(bool p_enable);
129
bool is_shortcut_feedback() const;
130
131
void set_button_mask(BitField<MouseButtonMask> p_mask);
132
BitField<MouseButtonMask> get_button_mask() const;
133
134
void set_shortcut(const Ref<Shortcut> &p_shortcut);
135
Ref<Shortcut> get_shortcut() const;
136
137
virtual Control *make_custom_tooltip(const String &p_text) const override;
138
139
void set_button_group(const Ref<ButtonGroup> &p_group);
140
Ref<ButtonGroup> get_button_group() const;
141
142
PackedStringArray get_configuration_warnings() const override;
143
144
BaseButton();
145
~BaseButton();
146
};
147
148
VARIANT_ENUM_CAST(BaseButton::DrawMode)
149
VARIANT_ENUM_CAST(BaseButton::ActionMode)
150
151
class ButtonGroup : public Resource {
152
GDCLASS(ButtonGroup, Resource);
153
friend class BaseButton;
154
HashSet<BaseButton *> buttons;
155
bool allow_unpress = false;
156
157
protected:
158
static void _bind_methods();
159
160
public:
161
BaseButton *get_pressed_button();
162
void get_buttons(List<BaseButton *> *r_buttons);
163
TypedArray<BaseButton> _get_buttons();
164
void set_allow_unpress(bool p_enabled);
165
bool is_allow_unpress();
166
ButtonGroup();
167
};
168
169