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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/*************************************************************************/
/* base_button.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "base_button.h"
#include "core/os/keyboard.h"
#include "scene/main/viewport.h"
#include "scene/scene_string_names.h"
void BaseButton::_unpress_group() {
if (!button_group.is_valid())
return;
if (toggle_mode) {
status.pressed = true;
}
for (Set<BaseButton *>::Element *E = button_group->buttons.front(); E; E = E->next()) {
if (E->get() == this)
continue;
E->get()->set_pressed(false);
}
}
void BaseButton::_gui_input(Ref<InputEvent> p_event) {
if (status.disabled) // no interaction with disabled button
return;
Ref<InputEventMouseButton> b = p_event;
if (b.is_valid()) {
if (((1 << (b->get_button_index() - 1)) & button_mask) == 0)
return;
if (status.pressing_button)
return;
if (action_mode == ACTION_MODE_BUTTON_PRESS) {
if (b->is_pressed()) {
emit_signal("button_down");
if (!toggle_mode) { //mouse press attempt
status.press_attempt = true;
status.pressing_inside = true;
pressed();
if (get_script_instance()) {
Variant::CallError ce;
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
_unpress_group();
emit_signal("pressed");
} else {
status.pressed = !status.pressed;
pressed();
_unpress_group();
emit_signal("pressed");
toggled(status.pressed);
if (get_script_instance()) {
get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
}
emit_signal("toggled", status.pressed);
}
} else {
emit_signal("button_up");
/* this is pointless if (status.press_attempt && status.pressing_inside) {
//released();
emit_signal("released");
}
*/
status.press_attempt = false;
}
update();
return;
}
if (b->is_pressed()) {
status.press_attempt = true;
status.pressing_inside = true;
emit_signal("button_down");
} else {
emit_signal("button_up");
if (status.press_attempt && status.pressing_inside) {
if (!toggle_mode) { //mouse press attempt
pressed();
if (get_script_instance()) {
Variant::CallError ce;
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
_unpress_group();
emit_signal("pressed");
} else {
status.pressed = !status.pressed;
pressed();
_unpress_group();
emit_signal("pressed");
toggled(status.pressed);
if (get_script_instance()) {
get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
}
emit_signal("toggled", status.pressed);
}
}
status.press_attempt = false;
}
update();
}
Ref<InputEventMouseMotion> mm = p_event;
if (mm.is_valid()) {
if (status.press_attempt && status.pressing_button == 0) {
bool last_press_inside = status.pressing_inside;
status.pressing_inside = has_point(mm->get_position());
if (last_press_inside != status.pressing_inside)
update();
}
}
if (!mm.is_valid() && !b.is_valid()) {
if (p_event->is_echo()) {
return;
}
if (status.disabled) {
return;
}
if (status.press_attempt && status.pressing_button == 0) {
return;
}
if (p_event->is_action("ui_accept")) {
if (p_event->is_pressed()) {
status.pressing_button++;
status.press_attempt = true;
status.pressing_inside = true;
emit_signal("button_down");
} else if (status.press_attempt) {
if (status.pressing_button)
status.pressing_button--;
if (status.pressing_button)
return;
status.press_attempt = false;
status.pressing_inside = false;
emit_signal("button_up");
if (!toggle_mode) { //mouse press attempt
pressed();
if (get_script_instance()) {
Variant::CallError ce;
get_script_instance()->call(SceneStringNames::get_singleton()->_pressed, NULL, 0, ce);
}
_unpress_group();
emit_signal("pressed");
} else {
status.pressed = !status.pressed;
pressed();
_unpress_group();
emit_signal("pressed");
toggled(status.pressed);
if (get_script_instance()) {
get_script_instance()->call(SceneStringNames::get_singleton()->_toggled, status.pressed);
}
emit_signal("toggled", status.pressed);
}
}
accept_event();
update();
}
}
}
void BaseButton::_notification(int p_what) {
if (p_what == NOTIFICATION_MOUSE_ENTER) {
status.hovering = true;
update();
}
if (p_what == NOTIFICATION_MOUSE_EXIT) {
status.hovering = false;
update();
}
if (p_what == NOTIFICATION_DRAG_BEGIN || p_what == NOTIFICATION_SCROLL_BEGIN) {
if (status.press_attempt) {
status.press_attempt = false;
status.pressing_button = 0;
update();
}
}
if (p_what == NOTIFICATION_FOCUS_ENTER) {
status.hovering = true;
update();
}
if (p_what == NOTIFICATION_FOCUS_EXIT) {
if (status.pressing_button && status.press_attempt) {
status.press_attempt = false;
status.pressing_button = 0;
status.hovering = false;
update();
} else if (status.hovering) {
status.hovering = false;
update();
}
}
if (p_what == NOTIFICATION_ENTER_TREE) {
}
if (p_what == NOTIFICATION_EXIT_TREE) {
}
if (p_what == NOTIFICATION_VISIBILITY_CHANGED && !is_visible_in_tree()) {
if (!toggle_mode) {
status.pressed = false;
}
status.hovering = false;
status.press_attempt = false;
status.pressing_inside = false;
status.pressing_button = 0;
}
}
void BaseButton::pressed() {
if (get_script_instance())
get_script_instance()->call("pressed");
}
void BaseButton::toggled(bool p_pressed) {
if (get_script_instance()) {
get_script_instance()->call("toggled", p_pressed);
}
}
void BaseButton::set_disabled(bool p_disabled) {
if (status.disabled == p_disabled)
return;
status.disabled = p_disabled;
if (p_disabled) {
if (!toggle_mode) {
status.pressed = false;
}
status.press_attempt = false;
status.pressing_inside = false;
status.pressing_button = 0;
}
update();
_change_notify("disabled");
}
bool BaseButton::is_disabled() const {
return status.disabled;
}
void BaseButton::set_pressed(bool p_pressed) {
if (!toggle_mode)
return;
if (status.pressed == p_pressed)
return;
_change_notify("pressed");
status.pressed = p_pressed;
if (p_pressed) {
_unpress_group();
}
update();
}
bool BaseButton::is_pressing() const {
return status.press_attempt;
}
bool BaseButton::is_pressed() const {
return toggle_mode ? status.pressed : status.press_attempt;
}
bool BaseButton::is_hovered() const {
return status.hovering;
}
BaseButton::DrawMode BaseButton::get_draw_mode() const {
if (status.disabled) {
return DRAW_DISABLED;
};
if (!status.press_attempt && status.hovering) {
if (status.pressed)
return DRAW_HOVER_PRESSED;
return DRAW_HOVER;
} else {
/* determine if pressed or not */
bool pressing;
if (status.press_attempt) {
pressing = status.pressing_inside;
if (status.pressed)
pressing = !pressing;
} else {
pressing = status.pressed;
}
if (pressing)
return DRAW_PRESSED;
else
return DRAW_NORMAL;
}
return DRAW_NORMAL;
}
void BaseButton::set_toggle_mode(bool p_on) {
toggle_mode = p_on;
}
bool BaseButton::is_toggle_mode() const {
return toggle_mode;
}
void BaseButton::set_shortcut_in_tooltip(bool p_on) {
shortcut_in_tooltip = p_on;
}
bool BaseButton::is_shortcut_in_tooltip_enabled() const {
return shortcut_in_tooltip;
}
void BaseButton::set_action_mode(ActionMode p_mode) {
action_mode = p_mode;
}
BaseButton::ActionMode BaseButton::get_action_mode() const {
return action_mode;
}
void BaseButton::set_button_mask(int p_mask) {
button_mask = p_mask;
}
int BaseButton::get_button_mask() const {
return button_mask;
}
void BaseButton::set_enabled_focus_mode(FocusMode p_mode) {
enabled_focus_mode = p_mode;
if (!status.disabled) {
set_focus_mode(p_mode);
}
}
Control::FocusMode BaseButton::get_enabled_focus_mode() const {
return enabled_focus_mode;
}
void BaseButton::set_shortcut(const Ref<ShortCut> &p_shortcut) {
if (shortcut.is_null() == p_shortcut.is_null())
return;
shortcut = p_shortcut;
set_process_unhandled_input(shortcut.is_valid());
}
Ref<ShortCut> BaseButton::get_shortcut() const {
return shortcut;
}
void BaseButton::_unhandled_input(Ref<InputEvent> p_event) {
if (!is_disabled() && is_visible_in_tree() && p_event->is_pressed() && !p_event->is_echo() && shortcut.is_valid() && shortcut->is_shortcut(p_event)) {
if (get_viewport()->get_modal_stack_top() && !get_viewport()->get_modal_stack_top()->is_a_parent_of(this))
return; //ignore because of modal window
if (is_toggle_mode()) {
set_pressed(!is_pressed());
emit_signal("toggled", is_pressed());
}
emit_signal("pressed");
}
}
String BaseButton::get_tooltip(const Point2 &p_pos) const {
String tooltip = Control::get_tooltip(p_pos);
if (shortcut_in_tooltip && shortcut.is_valid() && shortcut->is_valid()) {
String text = shortcut->get_name() + " (" + shortcut->get_as_text() + ")";
if (shortcut->get_name().nocasecmp_to(tooltip) != 0) {
text += "\n" + tooltip;
}
tooltip = text;
}
return tooltip;
}
void BaseButton::set_button_group(const Ref<ButtonGroup> &p_group) {
if (button_group.is_valid()) {
button_group->buttons.erase(this);
}
button_group = p_group;
if (button_group.is_valid()) {
button_group->buttons.insert(this);
}
update(); //checkbox changes to radio if set a buttongroup
}
Ref<ButtonGroup> BaseButton::get_button_group() const {
return button_group;
}
void BaseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("_gui_input"), &BaseButton::_gui_input);
ClassDB::bind_method(D_METHOD("_unhandled_input"), &BaseButton::_unhandled_input);
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
ClassDB::bind_method(D_METHOD("is_hovered"), &BaseButton::is_hovered);
ClassDB::bind_method(D_METHOD("set_toggle_mode", "enabled"), &BaseButton::set_toggle_mode);
ClassDB::bind_method(D_METHOD("is_toggle_mode"), &BaseButton::is_toggle_mode);
ClassDB::bind_method(D_METHOD("set_shortcut_in_tooltip", "enabled"), &BaseButton::set_shortcut_in_tooltip);
ClassDB::bind_method(D_METHOD("is_shortcut_in_tooltip_enabled"), &BaseButton::is_shortcut_in_tooltip_enabled);
ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &BaseButton::set_disabled);
ClassDB::bind_method(D_METHOD("is_disabled"), &BaseButton::is_disabled);
ClassDB::bind_method(D_METHOD("set_action_mode", "mode"), &BaseButton::set_action_mode);
ClassDB::bind_method(D_METHOD("get_action_mode"), &BaseButton::get_action_mode);
ClassDB::bind_method(D_METHOD("set_button_mask", "mask"), &BaseButton::set_button_mask);
ClassDB::bind_method(D_METHOD("get_button_mask"), &BaseButton::get_button_mask);
ClassDB::bind_method(D_METHOD("get_draw_mode"), &BaseButton::get_draw_mode);
ClassDB::bind_method(D_METHOD("set_enabled_focus_mode", "mode"), &BaseButton::set_enabled_focus_mode);
ClassDB::bind_method(D_METHOD("get_enabled_focus_mode"), &BaseButton::get_enabled_focus_mode);
ClassDB::bind_method(D_METHOD("set_shortcut", "shortcut"), &BaseButton::set_shortcut);
ClassDB::bind_method(D_METHOD("get_shortcut"), &BaseButton::get_shortcut);
ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);
BIND_VMETHOD(MethodInfo("_pressed"));
BIND_VMETHOD(MethodInfo("_toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
ADD_SIGNAL(MethodInfo("pressed"));
ADD_SIGNAL(MethodInfo("button_up"));
ADD_SIGNAL(MethodInfo("button_down"));
ADD_SIGNAL(MethodInfo("toggled", PropertyInfo(Variant::BOOL, "button_pressed")));
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "toggle_mode"), "set_toggle_mode", "is_toggle_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_in_tooltip"), "set_shortcut_in_tooltip", "is_shortcut_in_tooltip_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pressed"), "set_pressed", "is_pressed");
ADD_PROPERTY(PropertyInfo(Variant::INT, "action_mode", PROPERTY_HINT_ENUM, "Button Press,Button Release"), "set_action_mode", "get_action_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
ADD_PROPERTY(PropertyInfo(Variant::INT, "enabled_focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_enabled_focus_mode", "get_enabled_focus_mode");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "ShortCut"), "set_shortcut", "get_shortcut");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");
BIND_ENUM_CONSTANT(DRAW_NORMAL);
BIND_ENUM_CONSTANT(DRAW_PRESSED);
BIND_ENUM_CONSTANT(DRAW_HOVER);
BIND_ENUM_CONSTANT(DRAW_DISABLED);
BIND_ENUM_CONSTANT(DRAW_HOVER_PRESSED);
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_PRESS);
BIND_ENUM_CONSTANT(ACTION_MODE_BUTTON_RELEASE);
}
BaseButton::BaseButton() {
toggle_mode = false;
shortcut_in_tooltip = true;
status.pressed = false;
status.press_attempt = false;
status.hovering = false;
status.pressing_inside = false;
status.disabled = false;
status.pressing_button = 0;
set_focus_mode(FOCUS_ALL);
enabled_focus_mode = FOCUS_ALL;
action_mode = ACTION_MODE_BUTTON_RELEASE;
button_mask = BUTTON_MASK_LEFT;
}
BaseButton::~BaseButton() {
if (button_group.is_valid()) {
button_group->buttons.erase(this);
}
}
void ButtonGroup::get_buttons(List<BaseButton *> *r_buttons) {
for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
r_buttons->push_back(E->get());
}
}
Array ButtonGroup::_get_buttons() {
Array btns;
for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
btns.push_back(E->get());
}
return btns;
}
BaseButton *ButtonGroup::get_pressed_button() {
for (Set<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
if (E->get()->is_pressed())
return E->get();
}
return NULL;
}
void ButtonGroup::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button);
ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons);
}
ButtonGroup::ButtonGroup() {
set_local_to_scene(true);
}
|