1/**************************************************************************/
2/* base_button.cpp */
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#include "base_button.h"
32
33#include "core/config/project_settings.h"
34#include "core/os/keyboard.h"
35#include "scene/main/window.h"
36#include "scene/scene_string_names.h"
37
38void BaseButton::_unpress_group() {
39 if (!button_group.is_valid()) {
40 return;
41 }
42
43 if (toggle_mode && !button_group->is_allow_unpress()) {
44 status.pressed = true;
45 }
46
47 for (BaseButton *E : button_group->buttons) {
48 if (E == this) {
49 continue;
50 }
51
52 E->set_pressed(false);
53 }
54}
55
56void BaseButton::gui_input(const Ref<InputEvent> &p_event) {
57 ERR_FAIL_COND(p_event.is_null());
58
59 if (status.disabled) { // no interaction with disabled button
60 return;
61 }
62
63 Ref<InputEventMouseButton> mouse_button = p_event;
64 bool ui_accept = p_event->is_action("ui_accept", true) && !p_event->is_echo();
65
66 bool button_masked = mouse_button.is_valid() && button_mask.has_flag(mouse_button_to_mask(mouse_button->get_button_index()));
67 if (button_masked || ui_accept) {
68 was_mouse_pressed = button_masked;
69 on_action_event(p_event);
70 was_mouse_pressed = false;
71
72 return;
73 }
74
75 Ref<InputEventMouseMotion> mouse_motion = p_event;
76 if (mouse_motion.is_valid()) {
77 if (status.press_attempt) {
78 bool last_press_inside = status.pressing_inside;
79 status.pressing_inside = has_point(mouse_motion->get_position());
80 if (last_press_inside != status.pressing_inside) {
81 queue_redraw();
82 }
83 }
84 }
85}
86
87void BaseButton::_notification(int p_what) {
88 switch (p_what) {
89 case NOTIFICATION_MOUSE_ENTER: {
90 status.hovering = true;
91 queue_redraw();
92 } break;
93
94 case NOTIFICATION_MOUSE_EXIT: {
95 status.hovering = false;
96 queue_redraw();
97 } break;
98
99 case NOTIFICATION_DRAG_BEGIN:
100 case NOTIFICATION_SCROLL_BEGIN: {
101 if (status.press_attempt) {
102 status.press_attempt = false;
103 queue_redraw();
104 }
105 } break;
106
107 case NOTIFICATION_FOCUS_ENTER: {
108 queue_redraw();
109 } break;
110
111 case NOTIFICATION_FOCUS_EXIT: {
112 if (status.press_attempt) {
113 status.press_attempt = false;
114 queue_redraw();
115 } else if (status.hovering) {
116 queue_redraw();
117 }
118 } break;
119
120 case NOTIFICATION_VISIBILITY_CHANGED:
121 case NOTIFICATION_EXIT_TREE: {
122 if (p_what == NOTIFICATION_VISIBILITY_CHANGED && is_visible_in_tree()) {
123 break;
124 }
125 if (!toggle_mode) {
126 status.pressed = false;
127 }
128 status.hovering = false;
129 status.press_attempt = false;
130 status.pressing_inside = false;
131 } break;
132 }
133}
134
135void BaseButton::_pressed() {
136 GDVIRTUAL_CALL(_pressed);
137 pressed();
138 emit_signal(SNAME("pressed"));
139}
140
141void BaseButton::_toggled(bool p_pressed) {
142 GDVIRTUAL_CALL(_toggled, p_pressed);
143 toggled(p_pressed);
144 emit_signal(SNAME("toggled"), p_pressed);
145}
146
147void BaseButton::on_action_event(Ref<InputEvent> p_event) {
148 if (p_event->is_pressed()) {
149 status.press_attempt = true;
150 status.pressing_inside = true;
151 emit_signal(SNAME("button_down"));
152 }
153
154 if (status.press_attempt && status.pressing_inside) {
155 if (toggle_mode) {
156 bool is_pressed = p_event->is_pressed();
157 if ((is_pressed && action_mode == ACTION_MODE_BUTTON_PRESS) || (!is_pressed && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
158 if (action_mode == ACTION_MODE_BUTTON_PRESS) {
159 status.press_attempt = false;
160 status.pressing_inside = false;
161 }
162 status.pressed = !status.pressed;
163 _unpress_group();
164 if (button_group.is_valid()) {
165 button_group->emit_signal(SNAME("pressed"), this);
166 }
167 _toggled(status.pressed);
168 _pressed();
169 }
170 } else {
171 if ((p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_PRESS) || (!p_event->is_pressed() && action_mode == ACTION_MODE_BUTTON_RELEASE)) {
172 _pressed();
173 }
174 }
175 }
176
177 if (!p_event->is_pressed()) {
178 Ref<InputEventMouseButton> mouse_button = p_event;
179 if (mouse_button.is_valid()) {
180 if (!has_point(mouse_button->get_position())) {
181 status.hovering = false;
182 }
183 }
184 status.press_attempt = false;
185 status.pressing_inside = false;
186 emit_signal(SNAME("button_up"));
187 }
188
189 queue_redraw();
190}
191
192void BaseButton::pressed() {
193}
194
195void BaseButton::toggled(bool p_pressed) {
196}
197
198void BaseButton::set_disabled(bool p_disabled) {
199 if (status.disabled == p_disabled) {
200 return;
201 }
202
203 status.disabled = p_disabled;
204 if (p_disabled) {
205 if (!toggle_mode) {
206 status.pressed = false;
207 }
208 status.press_attempt = false;
209 status.pressing_inside = false;
210 }
211 queue_redraw();
212}
213
214bool BaseButton::is_disabled() const {
215 return status.disabled;
216}
217
218void BaseButton::set_pressed(bool p_pressed) {
219 bool prev_pressed = status.pressed;
220 set_pressed_no_signal(p_pressed);
221
222 if (status.pressed == prev_pressed) {
223 return;
224 }
225
226 if (p_pressed) {
227 _unpress_group();
228 if (button_group.is_valid()) {
229 button_group->emit_signal(SNAME("pressed"), this);
230 }
231 }
232 _toggled(status.pressed);
233}
234
235void BaseButton::set_pressed_no_signal(bool p_pressed) {
236 if (!toggle_mode) {
237 return;
238 }
239 if (status.pressed == p_pressed) {
240 return;
241 }
242 status.pressed = p_pressed;
243
244 queue_redraw();
245}
246
247bool BaseButton::is_pressing() const {
248 return status.press_attempt;
249}
250
251bool BaseButton::is_pressed() const {
252 return toggle_mode ? status.pressed : status.press_attempt;
253}
254
255bool BaseButton::is_hovered() const {
256 return status.hovering;
257}
258
259BaseButton::DrawMode BaseButton::get_draw_mode() const {
260 if (status.disabled) {
261 return DRAW_DISABLED;
262 }
263
264 if (in_shortcut_feedback) {
265 return DRAW_HOVER_PRESSED;
266 }
267
268 if (!status.press_attempt && status.hovering) {
269 if (status.pressed) {
270 return DRAW_HOVER_PRESSED;
271 }
272
273 return DRAW_HOVER;
274 } else {
275 // Determine if pressed or not.
276 bool pressing;
277 if (status.press_attempt) {
278 pressing = (status.pressing_inside || keep_pressed_outside);
279 if (status.pressed) {
280 pressing = !pressing;
281 }
282 } else {
283 pressing = status.pressed;
284 }
285
286 if (pressing) {
287 return DRAW_PRESSED;
288 } else {
289 return DRAW_NORMAL;
290 }
291 }
292}
293
294void BaseButton::set_toggle_mode(bool p_on) {
295 // Make sure to set 'pressed' to false if we are not in toggle mode
296 if (!p_on) {
297 set_pressed(false);
298 }
299
300 toggle_mode = p_on;
301 update_configuration_warnings();
302}
303
304bool BaseButton::is_toggle_mode() const {
305 return toggle_mode;
306}
307
308void BaseButton::set_shortcut_in_tooltip(bool p_on) {
309 shortcut_in_tooltip = p_on;
310}
311
312bool BaseButton::is_shortcut_in_tooltip_enabled() const {
313 return shortcut_in_tooltip;
314}
315
316void BaseButton::set_action_mode(ActionMode p_mode) {
317 action_mode = p_mode;
318}
319
320BaseButton::ActionMode BaseButton::get_action_mode() const {
321 return action_mode;
322}
323
324void BaseButton::set_button_mask(BitField<MouseButtonMask> p_mask) {
325 button_mask = p_mask;
326}
327
328BitField<MouseButtonMask> BaseButton::get_button_mask() const {
329 return button_mask;
330}
331
332void BaseButton::set_keep_pressed_outside(bool p_on) {
333 keep_pressed_outside = p_on;
334}
335
336bool BaseButton::is_keep_pressed_outside() const {
337 return keep_pressed_outside;
338}
339
340void BaseButton::set_shortcut_feedback(bool p_enable) {
341 shortcut_feedback = p_enable;
342}
343
344bool BaseButton::is_shortcut_feedback() const {
345 return shortcut_feedback;
346}
347
348void BaseButton::set_shortcut(const Ref<Shortcut> &p_shortcut) {
349 shortcut = p_shortcut;
350 set_process_shortcut_input(shortcut.is_valid());
351}
352
353Ref<Shortcut> BaseButton::get_shortcut() const {
354 return shortcut;
355}
356
357void BaseButton::_shortcut_feedback_timeout() {
358 in_shortcut_feedback = false;
359 queue_redraw();
360}
361
362void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
363 ERR_FAIL_COND(p_event.is_null());
364
365 if (!is_disabled() && p_event->is_pressed() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
366 if (toggle_mode) {
367 status.pressed = !status.pressed;
368
369 _unpress_group();
370 if (button_group.is_valid()) {
371 button_group->emit_signal(SNAME("pressed"), this);
372 }
373
374 _toggled(status.pressed);
375 _pressed();
376
377 } else {
378 _pressed();
379 }
380 queue_redraw();
381 accept_event();
382
383 if (shortcut_feedback) {
384 if (shortcut_feedback_timer == nullptr) {
385 shortcut_feedback_timer = memnew(Timer);
386 shortcut_feedback_timer->set_one_shot(true);
387 add_child(shortcut_feedback_timer);
388 shortcut_feedback_timer->set_wait_time(GLOBAL_GET("gui/timers/button_shortcut_feedback_highlight_time"));
389 shortcut_feedback_timer->connect("timeout", callable_mp(this, &BaseButton::_shortcut_feedback_timeout));
390 }
391
392 in_shortcut_feedback = true;
393 shortcut_feedback_timer->start();
394 }
395 }
396}
397
398String BaseButton::get_tooltip(const Point2 &p_pos) const {
399 String tooltip = Control::get_tooltip(p_pos);
400 if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->has_valid_event()) {
401 String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
402 if (!tooltip.is_empty() && shortcut->get_name().nocasecmp_to(tooltip) != 0) {
403 text += "\n" + atr(tooltip);
404 }
405 tooltip = text;
406 }
407 return tooltip;
408}
409
410void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
411 if (button_group.is_valid()) {
412 button_group->buttons.erase(this);
413 }
414
415 button_group = p_group;
416
417 if (button_group.is_valid()) {
418 button_group->buttons.insert(this);
419 }
420
421 queue_redraw(); //checkbox changes to radio if set a buttongroup
422 update_configuration_warnings();
423}
424
425Ref<ButtonGroup> BaseButton::get_button_group() const {
426 return button_group;
427}
428
429bool BaseButton::_was_pressed_by_mouse() const {
430 return was_mouse_pressed;
431}
432
433PackedStringArray BaseButton::get_configuration_warnings() const {
434 PackedStringArray warnings = Control::get_configuration_warnings();
435
436 if (get_button_group().is_valid() && !is_toggle_mode()) {
437 warnings.push_back(RTR("ButtonGroup is intended to be used only with buttons that have toggle_mode set to true."));
438 }
439
440 return warnings;
441}
442
443void BaseButton::_bind_methods() {
444 ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
445 ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
446 ClassDB::bind_method(D_METHOD("set_pressed_no_signal", "pressed"), &BaseButton::set_pressed_no_signal);
447 ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
448 ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
449 ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
450 ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
451 ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
452 ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
453 ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
454 ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
455 ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
456 ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
457 ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
458 ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
459 ClassDB::bind_method(D_METHOD("set_keep_pressed_outside", "enabled"), &BaseButton::set_keep_pressed_outside);
460 ClassDB::bind_method(D_METHOD("is_keep_pressed_outside"), &BaseButton::is_keep_pressed_outside);
461 ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback);
462 ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback);
463
464 ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
465 ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
466
467 ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
468 ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
469
470 GDVIRTUAL_BIND(_pressed);
471 GDVIRTUAL_BIND(_toggled, "toggled_on");
472
473 ADD_SIGNAL(MethodInfo("pressed"));
474 ADD_SIGNAL(MethodInfo("button_up"));
475 ADD_SIGNAL(MethodInfo("button_down"));
476 ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "toggled_on")));
477
478 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
479 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
480 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "button_pressed"), "set_pressed", "is_pressed");
481 ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
482 ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
483 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
484 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
485
486 ADD_GROUP("Shortcut", "");
487 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
488 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
489 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
490
491 BIND_ENUM_CONSTANT(DRAW_NORMAL);
492 BIND_ENUM_CONSTANT(DRAW_PRESSED);
493 BIND_ENUM_CONSTANT(DRAW_HOVER);
494 BIND_ENUM_CONSTANT(DRAW_DISABLED);
495 BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
496
497 BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
498 BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
499
500 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/button_shortcut_feedback_highlight_time", PROPERTY_HINT_RANGE, "0.01,10,0.01,suffix:s"), 0.2);
501}
502
503BaseButton::BaseButton() {
504 set_focus_mode(FOCUS_ALL);
505}
506
507BaseButton::~BaseButton() {
508 if (button_group.is_valid()) {
509 button_group->buttons.erase(this);
510 }
511}
512
513void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
514 for (BaseButton *E : buttons) {
515 r_buttons->push_back(E);
516 }
517}
518
519TypedArray<BaseButton> ButtonGroup::_get_buttons() {
520 TypedArray<BaseButton> btns;
521 for (const BaseButton *E : buttons) {
522 btns.push_back(E);
523 }
524
525 return btns;
526}
527
528BaseButton *ButtonGroup::get_pressed_button() {
529 for (BaseButton *E : buttons) {
530 if (E->is_pressed()) {
531 return E;
532 }
533 }
534
535 return nullptr;
536}
537
538void ButtonGroup::set_allow_unpress(bool p_enabled) {
539 allow_unpress = p_enabled;
540}
541bool ButtonGroup::is_allow_unpress() {
542 return allow_unpress;
543}
544
545void ButtonGroup::_bind_methods() {
546 ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
547 ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
548 ClassDB::bind_method(D_METHOD("set_allow_unpress", "enabled"), &ButtonGroup::set_allow_unpress);
549 ClassDB::bind_method(D_METHOD("is_allow_unpress"), &ButtonGroup::is_allow_unpress);
550
551 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_unpress"), "set_allow_unpress", "is_allow_unpress");
552
553 ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton")));
554}
555
556ButtonGroup::ButtonGroup() {
557 set_local_to_scene(true);
558}
559