-
-
Notifications
You must be signed in to change notification settings - Fork 21.7k
/
Copy pathpopup_menu.cpp
3075 lines (2573 loc) · 104 KB
/
popup_menu.cpp
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* popup_menu.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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 "popup_menu.h"
#include "popup_menu.compat.inc"
#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "scene/gui/menu_bar.h"
#include "scene/gui/panel_container.h"
#include "scene/resources/style_box_flat.h"
#include "scene/theme/theme_db.h"
HashMap<NativeMenu::SystemMenus, PopupMenu *> PopupMenu::system_menus;
bool PopupMenu::_set_item_accelerator(int p_index, const Ref<InputEventKey> &p_ie) {
NativeMenu *nmenu = NativeMenu::get_singleton();
if (p_ie->get_physical_keycode() == Key::NONE && p_ie->get_keycode() == Key::NONE && p_ie->get_key_label() != Key::NONE) {
nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_key_label_with_modifiers());
return true;
} else if (p_ie->get_keycode() != Key::NONE) {
nmenu->set_item_accelerator(global_menu, p_index, p_ie->get_keycode_with_modifiers());
return true;
} else if (p_ie->get_physical_keycode() != Key::NONE) {
nmenu->set_item_accelerator(global_menu, p_index, DisplayServer::get_singleton()->keyboard_get_keycode_from_physical(p_ie->get_physical_keycode_with_modifiers()));
return true;
}
return false;
}
void PopupMenu::_set_item_checkable_type(int p_index, int p_checkable_type) {
switch (p_checkable_type) {
case Item::CHECKABLE_TYPE_NONE: {
set_item_as_checkable(p_index, false);
} break;
case Item::CHECKABLE_TYPE_CHECK_BOX: {
set_item_as_checkable(p_index, true);
} break;
case Item::CHECKABLE_TYPE_RADIO_BUTTON: {
set_item_as_radio_checkable(p_index, true);
} break;
}
}
int PopupMenu::_get_item_checkable_type(int p_index) const {
ERR_FAIL_INDEX_V(p_index, items.size(), Item::CHECKABLE_TYPE_NONE);
return items[p_index].checkable_type;
}
RID PopupMenu::bind_global_menu() {
#ifdef TOOLS_ENABLED
if (is_part_of_edited_scene()) {
return RID();
}
#endif
if (!NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_POPUP_MENU)) {
return RID();
}
if (global_menu.is_valid()) {
return global_menu; // Already bound;
}
NativeMenu *nmenu = NativeMenu::get_singleton();
if (system_menu_id != NativeMenu::INVALID_MENU_ID && nmenu->has_system_menu(system_menu_id)) {
if (system_menus.has(system_menu_id)) {
WARN_PRINT(vformat("Attempting to bind PopupMenu to the system menu %s, but another menu is already bound to it. This menu: %s, current menu: %s", nmenu->get_system_menu_name(system_menu_id), get_description(), system_menus[system_menu_id]->get_description()));
global_menu = nmenu->create_menu();
} else {
system_menus[system_menu_id] = this;
system_menu = nmenu->get_system_menu(system_menu_id);
global_menu = system_menu;
}
} else {
global_menu = nmenu->create_menu();
}
nmenu->set_interface_direction(global_menu, control->is_layout_rtl());
nmenu->set_popup_open_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_popup));
nmenu->set_popup_close_callback(global_menu, callable_mp(this, &PopupMenu::_about_to_close));
for (int i = 0; i < items.size(); i++) {
Item &item = items.write[i];
if (item.separator) {
nmenu->add_separator(global_menu);
} else {
int index = nmenu->add_item(global_menu, item.xl_text, callable_mp(this, &PopupMenu::activate_item), item.shortcut_is_global ? callable_mp(this, &PopupMenu::activate_item) : Callable(), i);
if (item.submenu) {
RID submenu_rid = item.submenu->bind_global_menu();
nmenu->set_item_submenu(global_menu, index, submenu_rid);
item.submenu_bound = true;
}
if (item.checkable_type == Item::CHECKABLE_TYPE_CHECK_BOX) {
nmenu->set_item_checkable(global_menu, index, true);
} else if (item.checkable_type == Item::CHECKABLE_TYPE_RADIO_BUTTON) {
nmenu->set_item_radio_checkable(global_menu, index, true);
}
nmenu->set_item_checked(global_menu, index, item.checked);
nmenu->set_item_disabled(global_menu, index, item.disabled);
nmenu->set_item_max_states(global_menu, index, item.max_states);
nmenu->set_item_icon(global_menu, index, item.icon);
nmenu->set_item_state(global_menu, index, item.state);
nmenu->set_item_indentation_level(global_menu, index, item.indent);
nmenu->set_item_tooltip(global_menu, index, item.tooltip);
if (!item.shortcut_is_disabled && item.shortcut.is_valid() && item.shortcut->has_valid_event()) {
Array events = item.shortcut->get_events();
for (int j = 0; j < events.size(); j++) {
Ref<InputEventKey> ie = events[j];
if (ie.is_valid() && _set_item_accelerator(index, ie)) {
break;
}
}
} else if (item.accel != Key::NONE) {
nmenu->set_item_accelerator(global_menu, index, item.accel);
}
}
}
return global_menu;
}
void PopupMenu::unbind_global_menu() {
if (global_menu.is_null()) {
return;
}
if (global_menu == system_menu && system_menus[system_menu_id] == this) {
system_menus.erase(system_menu_id);
}
for (int i = 0; i < items.size(); i++) {
Item &item = items.write[i];
if (item.submenu) {
item.submenu->unbind_global_menu();
item.submenu_bound = false;
}
}
if (system_menu != global_menu) {
NativeMenu::get_singleton()->free_menu(global_menu);
} else {
NativeMenu::get_singleton()->clear(global_menu);
}
system_menu = RID();
global_menu = RID();
}
bool PopupMenu::is_system_menu() const {
return (global_menu == system_menu) && (system_menu_id != NativeMenu::INVALID_MENU_ID);
}
void PopupMenu::set_system_menu(NativeMenu::SystemMenus p_system_menu_id) {
if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
unbind_global_menu();
}
system_menu_id = p_system_menu_id;
if (is_inside_tree() && system_menu_id != NativeMenu::INVALID_MENU_ID) {
bind_global_menu();
}
}
NativeMenu::SystemMenus PopupMenu::get_system_menu() const {
return system_menu_id;
}
String PopupMenu::_get_accel_text(const Item &p_item) const {
if (p_item.shortcut.is_valid()) {
return p_item.shortcut->get_as_text();
} else if (p_item.accel != Key::NONE) {
return keycode_get_string(p_item.accel);
}
return String();
}
Size2 PopupMenu::_get_item_icon_size(int p_idx) const {
const PopupMenu::Item &item = items[p_idx];
Size2 icon_size = item.get_icon_size();
int max_width = 0;
if (theme_cache.icon_max_width > 0) {
max_width = theme_cache.icon_max_width;
}
if (item.icon_max_width > 0 && (max_width == 0 || item.icon_max_width < max_width)) {
max_width = item.icon_max_width;
}
if (max_width > 0 && icon_size.width > max_width) {
icon_size.height = icon_size.height * max_width / icon_size.width;
icon_size.width = max_width;
}
return icon_size;
}
Size2 PopupMenu::_get_contents_minimum_size() const {
Size2 minsize = theme_cache.panel_style->get_minimum_size();
minsize.width += scroll_container->get_v_scroll_bar()->get_size().width;
// Take shadows into account.
minsize.width += panel->get_offset(SIDE_LEFT) - panel->get_offset(SIDE_RIGHT);
minsize.height += panel->get_offset(SIDE_TOP) - panel->get_offset(SIDE_BOTTOM);
float max_w = 0.0;
float icon_w = 0.0;
int check_w = MAX(theme_cache.checked->get_width(), theme_cache.radio_checked->get_width()) + theme_cache.h_separation;
int accel_max_w = 0;
bool has_check = false;
for (int i = 0; i < items.size(); i++) {
Size2 item_size;
_shape_item(i);
Size2 icon_size = _get_item_icon_size(i);
item_size.height = _get_item_height(i);
icon_w = MAX(icon_size.width, icon_w);
item_size.width += items[i].indent * theme_cache.indent;
if (items[i].checkable_type && !items[i].separator) {
has_check = true;
}
item_size.width += items[i].text_buf->get_size().x;
item_size.height += theme_cache.v_separation;
if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
int accel_w = theme_cache.h_separation * 2;
accel_w += items[i].accel_text_buf->get_size().x;
accel_max_w = MAX(accel_w, accel_max_w);
}
if (items[i].submenu) {
item_size.width += theme_cache.submenu->get_width();
}
max_w = MAX(max_w, item_size.width);
minsize.height += item_size.height;
}
int item_side_padding = theme_cache.item_start_padding + theme_cache.item_end_padding;
minsize.width += max_w + icon_w + accel_max_w + item_side_padding;
if (has_check) {
minsize.width += check_w;
}
if (is_inside_tree()) {
int height_limit = get_usable_parent_rect().size.height;
if (minsize.height > height_limit) {
minsize.height = height_limit;
}
}
minsize.height = Math::ceil(minsize.height); // Ensures enough height at fractional content scales to prevent the v_scroll_bar from showing.
return minsize;
}
int PopupMenu::_get_item_height(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, items.size(), 0);
Size2 icon_size = _get_item_icon_size(p_idx);
int icon_height = icon_size.height;
if (items[p_idx].checkable_type && !items[p_idx].separator) {
icon_height = MAX(icon_height, MAX(theme_cache.checked->get_height(), theme_cache.radio_checked->get_height()));
}
int text_height = items[p_idx].text_buf->get_size().height;
if (text_height == 0 && !items[p_idx].separator) {
text_height = theme_cache.font->get_height(theme_cache.font_size);
}
int separator_height = 0;
if (items[p_idx].separator) {
separator_height = MAX(theme_cache.separator_style->get_minimum_size().height, MAX(theme_cache.labeled_separator_left->get_minimum_size().height, theme_cache.labeled_separator_right->get_minimum_size().height));
}
return MAX(separator_height, MAX(text_height, icon_height));
}
int PopupMenu::_get_items_total_height() const {
// Get total height of all items by taking max of icon height and font height
int items_total_height = 0;
for (int i = 0; i < items.size(); i++) {
items_total_height += _get_item_height(i) + theme_cache.v_separation;
}
return items_total_height;
}
int PopupMenu::_get_mouse_over(const Point2 &p_over) const {
// Make the item area exclude shadows and the vertical margins and scrollbar.
Rect2 item_clickable_area = panel->get_global_rect();
if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
const int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width;
if (is_layout_rtl()) {
item_clickable_area.position.x += scroll_width;
item_clickable_area.size.width -= scroll_width;
}
item_clickable_area.size.width -= scroll_width;
}
float win_scale = get_content_scale_factor();
item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT);
item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP);
item_clickable_area.position *= win_scale;
item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
item_clickable_area.size *= win_scale;
if (!item_clickable_area.has_point(p_over)) {
return -1;
}
float ofs = item_clickable_area.position.y + (float)theme_cache.v_separation * win_scale * 0.5;
for (int i = 0; i < items.size(); i++) {
ofs += i > 0 ? (float)theme_cache.v_separation * win_scale : (float)theme_cache.v_separation * win_scale * 0.5;
ofs += _get_item_height(i) * win_scale;
if (p_over.y - control->get_position().y * win_scale < ofs) {
return i;
}
}
return -1;
}
void PopupMenu::_activate_submenu(int p_over, bool p_by_keyboard) {
PopupMenu *submenu_popup = items[p_over].submenu;
if (submenu_popup->is_visible()) {
return; // Already visible.
}
const float win_scale = get_content_scale_factor();
const Point2 panel_ofs_start = Point2(panel->get_offset(SIDE_LEFT), panel->get_offset(SIDE_TOP)) * win_scale;
const Point2 panel_ofs_end = Point2(panel->get_offset(SIDE_RIGHT), panel->get_offset(SIDE_BOTTOM)).abs() * win_scale;
const Point2 this_pos = get_position() + Point2(0, panel_ofs_start.y + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale);
Rect2 this_rect(this_pos, get_size());
const float scroll_offset = control->get_position().y;
const float scaled_ofs_cache = items[p_over]._ofs_cache * win_scale;
const float scaled_height_cache = items[p_over]._height_cache * win_scale;
submenu_popup->reset_size(); // Shrink the popup size to its contents.
const Size2 submenu_size = submenu_popup->get_size();
// Calculate the submenu's position.
Point2 submenu_pos(0, -submenu_popup->get_theme_stylebox(SceneStringName(panel))->get_margin(SIDE_TOP) * submenu_popup->get_content_scale_factor());
Rect2i screen_rect = is_embedded() ? Rect2i(get_embedder()->get_visible_rect()) : get_parent_rect();
if (is_layout_rtl()) {
submenu_pos += this_pos + Point2(-submenu_size.width + panel_ofs_end.x, scaled_ofs_cache + scroll_offset - theme_cache.v_separation / 2);
if (submenu_pos.x < screen_rect.position.x) {
submenu_pos.x = this_pos.x + this_rect.size.width - panel_ofs_start.x;
}
this_rect.position.x += panel_ofs_end.x;
} else {
submenu_pos += this_pos + Point2(this_rect.size.width - panel_ofs_end.x, scaled_ofs_cache + scroll_offset - theme_cache.v_separation / 2);
if (submenu_pos.x + submenu_size.width > screen_rect.position.x + screen_rect.size.width) {
submenu_pos.x = this_pos.x - submenu_size.width + panel_ofs_start.x;
}
this_rect.position.x += panel_ofs_start.x;
}
submenu_popup->set_position(submenu_pos);
PopupMenu *submenu_pum = Object::cast_to<PopupMenu>(submenu_popup);
if (!submenu_pum) {
submenu_popup->popup();
return;
}
submenu_pum->activated_by_keyboard = p_by_keyboard;
// If not triggered by the mouse, start the popup with its first enabled item focused.
if (p_by_keyboard) {
for (int i = 0; i < submenu_pum->get_item_count(); i++) {
if (!submenu_pum->is_item_disabled(i)) {
submenu_pum->set_focused_item(i);
break;
}
}
}
submenu_pum->popup();
// Set autohide areas.
const Rect2 safe_area(get_position(), get_size());
Viewport *vp = submenu_popup->get_embedder();
if (vp) {
vp->subwindow_set_popup_safe_rect(submenu_popup, safe_area);
} else {
DisplayServer::get_singleton()->window_set_popup_safe_rect(submenu_popup->get_window_id(), safe_area);
}
this_rect.position -= submenu_pum->get_position(); // Make the position of the parent popup relative to submenu popup.
this_rect.size.width -= panel_ofs_start.x + panel_ofs_end.x;
this_rect.size.height -= panel_ofs_end.y + (theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM)) * win_scale;
// Autohide area above the submenu item.
submenu_pum->clear_autohide_areas();
submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y - theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale,
this_rect.size.x, scaled_ofs_cache + scroll_offset + theme_cache.panel_style->get_margin(SIDE_TOP) * win_scale - theme_cache.v_separation / 2));
// If there is an area below the submenu item, add an autohide area there.
if (scaled_ofs_cache + scaled_height_cache + scroll_offset <= control->get_size().height * win_scale) {
const int from = scaled_ofs_cache + scaled_height_cache + scroll_offset + theme_cache.v_separation / 2;
submenu_pum->add_autohide_area(Rect2(this_rect.position.x, this_rect.position.y + from, this_rect.size.x, this_rect.size.y - from));
}
}
void PopupMenu::_parent_focused() {
if (is_embedded()) {
Point2 mouse_pos_adjusted;
Window *window_parent = Object::cast_to<Window>(get_parent()->get_viewport());
while (window_parent) {
if (!window_parent->is_embedded()) {
mouse_pos_adjusted += window_parent->get_position();
break;
}
window_parent = Object::cast_to<Window>(window_parent->get_parent()->get_viewport());
}
Rect2 safe_area = get_embedder()->subwindow_get_popup_safe_rect(this);
Point2 pos = DisplayServer::get_singleton()->mouse_get_position() - mouse_pos_adjusted;
if (safe_area == Rect2i() || !safe_area.has_point(pos)) {
Popup::_parent_focused();
} else {
grab_focus();
}
}
}
void PopupMenu::_submenu_timeout() {
if (mouse_over == submenu_over) {
_activate_submenu(mouse_over);
}
submenu_over = -1;
}
void PopupMenu::_input_from_window(const Ref<InputEvent> &p_event) {
if (p_event.is_valid()) {
_input_from_window_internal(p_event);
} else {
WARN_PRINT_ONCE("PopupMenu has received an invalid InputEvent. Consider filtering out invalid events.");
}
Popup::_input_from_window(p_event);
}
void PopupMenu::_input_from_window_internal(const Ref<InputEvent> &p_event) {
if (!items.is_empty()) {
Input *input = Input::get_singleton();
Ref<InputEventJoypadMotion> joypadmotion_event = p_event;
Ref<InputEventJoypadButton> joypadbutton_event = p_event;
bool is_joypad_event = (joypadmotion_event.is_valid() || joypadbutton_event.is_valid());
if (p_event->is_action("ui_down", true) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_down", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over + 1;
if (search_from >= items.size()) {
search_from = 0;
}
bool match_found = false;
for (int i = search_from; i < items.size(); i++) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), items[i].id);
scroll_to_item(i);
control->queue_redraw();
set_input_as_handled();
match_found = true;
break;
}
}
if (!match_found) {
// If the last item is not selectable, try re-searching from the start.
for (int i = 0; i < search_from; i++) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), items[i].id);
scroll_to_item(i);
control->queue_redraw();
set_input_as_handled();
break;
}
}
}
} else if (p_event->is_action("ui_up", true) && p_event->is_pressed()) {
if (is_joypad_event) {
if (!input->is_action_just_pressed("ui_up", true)) {
return;
}
set_process_internal(true);
}
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
}
bool match_found = false;
for (int i = search_from; i >= 0; i--) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), items[i].id);
scroll_to_item(i);
control->queue_redraw();
set_input_as_handled();
match_found = true;
break;
}
}
if (!match_found) {
// If the first item is not selectable, try re-searching from the end.
for (int i = items.size() - 1; i >= search_from; i--) {
if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal(SNAME("id_focused"), items[i].id);
scroll_to_item(i);
control->queue_redraw();
set_input_as_handled();
break;
}
}
}
} else if (p_event->is_action("ui_left", true) && p_event->is_pressed()) {
Node *n = get_parent();
if (n) {
if (Object::cast_to<PopupMenu>(n)) {
hide();
set_input_as_handled();
} else if (Object::cast_to<MenuBar>(n)) {
Object::cast_to<MenuBar>(n)->gui_input(p_event);
set_input_as_handled();
return;
}
}
} else if (p_event->is_action("ui_right", true) && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu && submenu_over != mouse_over) {
_activate_submenu(mouse_over, true);
set_input_as_handled();
} else {
Node *n = get_parent();
if (n && Object::cast_to<MenuBar>(n)) {
Object::cast_to<MenuBar>(n)->gui_input(p_event);
set_input_as_handled();
return;
}
}
} else if (p_event->is_action("ui_accept", true) && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
if (items[mouse_over].submenu && submenu_over != mouse_over) {
_activate_submenu(mouse_over, true);
} else {
activate_item(mouse_over);
}
set_input_as_handled();
}
}
}
// Make the item area exclude shadows and the vertical margins and scrollbar.
Rect2 item_clickable_area = panel->get_global_rect();
if (scroll_container->get_v_scroll_bar()->is_visible_in_tree()) {
int scroll_width = scroll_container->get_v_scroll_bar()->get_size().width;
if (is_layout_rtl()) {
item_clickable_area.position.x += scroll_width;
item_clickable_area.size.width -= scroll_width;
}
item_clickable_area.size.width -= scroll_width;
}
float win_scale = get_content_scale_factor();
item_clickable_area.position.x += theme_cache.panel_style->get_margin(SIDE_LEFT);
item_clickable_area.position.y += theme_cache.panel_style->get_margin(SIDE_TOP);
item_clickable_area.position *= win_scale;
item_clickable_area.size.y -= theme_cache.panel_style->get_margin(SIDE_TOP) + theme_cache.panel_style->get_margin(SIDE_BOTTOM);
item_clickable_area.size *= win_scale;
Ref<InputEventMouseButton> b = p_event;
if (b.is_valid()) {
MouseButton button_idx = b->get_button_index();
// Activate the item on release of either the left mouse button or
// any mouse button held down when the popup was opened.
// This allows for opening the popup and triggering an action in a single mouse click.
if (button_idx == MouseButton::LEFT || initial_button_mask.has_flag(mouse_button_to_mask(button_idx))) {
if (b->is_pressed()) {
during_grabbed_click = false;
is_scrolling = is_layout_rtl() ? b->get_position().x < item_clickable_area.position.x : b->get_position().x > item_clickable_area.size.width;
// Hide it if the shadows have been clicked.
if (get_flag(FLAG_POPUP)) {
Rect2 panel_area = panel->get_global_rect();
panel_area.position *= win_scale;
panel_area.size *= win_scale;
if (!panel_area.has_point(b->get_position())) {
_close_pressed();
return;
}
}
if (!item_clickable_area.has_point(b->get_position())) {
return;
}
_mouse_over_update(b->get_position());
} else {
if (is_scrolling) {
is_scrolling = false;
return;
}
bool was_during_grabbed_click = during_grabbed_click;
during_grabbed_click = false;
initial_button_mask.clear();
if (!item_clickable_area.has_point(b->get_position())) {
return;
}
// Disable clicks under a time threshold to avoid selection right when opening the popup.
if (was_during_grabbed_click && OS::get_singleton()->get_ticks_msec() - popup_time_msec < 400) {
return;
}
int over = _get_mouse_over(b->get_position());
if (over < 0) {
if (!was_during_grabbed_click) {
hide();
}
return;
}
if (items[over].separator || items[over].disabled) {
return;
}
if (items[over].submenu) {
_activate_submenu(over);
return;
}
activate_item(over);
}
}
}
Ref<InputEventMouseMotion> m = p_event;
if (m.is_valid()) {
if (m->get_velocity().is_zero_approx()) {
return;
}
activated_by_keyboard = false;
for (const Rect2 &E : autohide_areas) {
if (!scroll_container->get_global_rect().has_point(m->get_position()) && E.has_point(m->get_position())) {
// The mouse left the safe area, prepare to close.
_close_pressed();
return;
}
}
if (!minimum_lifetime_timer->is_stopped()) {
// The mouse left the safe area, but came back again, so cancel the auto-closing.
minimum_lifetime_timer->stop();
}
if (mouse_over == -1 && !item_clickable_area.has_point(m->get_position())) {
return;
}
_mouse_over_update(m->get_position());
}
Ref<InputEventKey> k = p_event;
if (allow_search && k.is_valid() && k->get_unicode() && k->is_pressed()) {
uint64_t now = OS::get_singleton()->get_ticks_msec();
uint64_t diff = now - search_time_msec;
uint64_t max_interval = uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec"));
search_time_msec = now;
if (diff > max_interval) {
search_string = "";
}
if (String::chr(k->get_unicode()) != search_string) {
search_string += String::chr(k->get_unicode());
}
for (int i = mouse_over + 1; i <= items.size(); i++) {
if (i == items.size()) {
if (mouse_over <= 0) {
break;
} else {
i = 0;
}
}
if (i == mouse_over) {
break;
}
if (items[i].text.findn(search_string) == 0) {
mouse_over = i;
emit_signal(SNAME("id_focused"), items[i].id);
scroll_to_item(i);
control->queue_redraw();
set_input_as_handled();
break;
}
}
}
}
void PopupMenu::_mouse_over_update(const Point2 &p_over) {
int over = _get_mouse_over(p_over);
int id = (over < 0 || items[over].separator || items[over].disabled) ? -1 : (items[over].id >= 0 ? items[over].id : over);
if (id < 0) {
mouse_over = -1;
control->queue_redraw();
return;
}
if (!is_scrolling && items[over].submenu && submenu_over != over) {
submenu_over = over;
submenu_timer->start();
}
if (over != mouse_over) {
mouse_over = over;
control->queue_redraw();
}
}
void PopupMenu::_draw_items() {
control->set_custom_minimum_size(Size2(0, _get_items_total_height()));
RID ci = control->get_canvas_item();
// Space between the item content and the sides of popup menu.
bool rtl = control->is_layout_rtl();
// In Item::checkable_type enum order (less the non-checkable member), with disabled repeated at the end.
Ref<Texture2D> check[] = { theme_cache.checked, theme_cache.radio_checked, theme_cache.checked_disabled, theme_cache.radio_checked_disabled };
Ref<Texture2D> uncheck[] = { theme_cache.unchecked, theme_cache.radio_unchecked, theme_cache.unchecked_disabled, theme_cache.radio_unchecked_disabled };
Ref<Texture2D> submenu;
if (rtl) {
submenu = theme_cache.submenu_mirrored;
} else {
submenu = theme_cache.submenu;
}
float display_width = control->get_size().width;
// Find the widest icon and whether any items have a checkbox, and store the offsets for each.
float icon_ofs = 0.0;
bool has_check = false;
for (int i = 0; i < items.size(); i++) {
if (items[i].separator) {
continue;
}
Size2 icon_size = _get_item_icon_size(i);
icon_ofs = MAX(icon_size.width, icon_ofs);
if (items[i].checkable_type) {
has_check = true;
}
}
if (icon_ofs > 0.0) {
icon_ofs += theme_cache.h_separation;
}
float check_ofs = 0.0;
if (has_check) {
for (int i = 0; i < 4; i++) {
check_ofs = MAX(check_ofs, check[i]->get_width());
check_ofs = MAX(check_ofs, uncheck[i]->get_width());
}
check_ofs += theme_cache.h_separation;
}
Point2 ofs;
// Loop through all items and draw each.
for (int i = 0; i < items.size(); i++) {
// For the first item only add half a separation. For all other items, add a whole separation to the offset.
ofs.y += i > 0 ? theme_cache.v_separation : (float)theme_cache.v_separation / 2;
_shape_item(i);
Point2 item_ofs = ofs;
Size2 icon_size = _get_item_icon_size(i);
float h = _get_item_height(i);
if (i == mouse_over) {
theme_cache.hover_style->draw(ci, Rect2(item_ofs + Point2(0, -theme_cache.v_separation / 2), Size2(display_width, h + theme_cache.v_separation)));
}
String text = items[i].xl_text;
// Separator
item_ofs.x += items[i].indent * theme_cache.indent;
if (items[i].separator) {
if (!text.is_empty() || items[i].icon.is_valid()) {
int content_size = items[i].text_buf->get_size().width + theme_cache.h_separation * 2;
if (items[i].icon.is_valid()) {
content_size += icon_size.width + theme_cache.h_separation;
}
int content_center = display_width / 2;
int content_left = content_center - content_size / 2;
int content_right = content_center + content_size / 2;
if (content_left > item_ofs.x) {
int sep_h = theme_cache.labeled_separator_left->get_minimum_size().height;
int sep_ofs = Math::floor((h - sep_h) / 2.0);
theme_cache.labeled_separator_left->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(MAX(0, content_left - item_ofs.x), sep_h)));
}
if (content_right < display_width) {
int sep_h = theme_cache.labeled_separator_right->get_minimum_size().height;
int sep_ofs = Math::floor((h - sep_h) / 2.0);
theme_cache.labeled_separator_right->draw(ci, Rect2(Point2(content_right, item_ofs.y + sep_ofs), Size2(MAX(0, display_width - content_right), sep_h)));
}
} else {
int sep_h = theme_cache.separator_style->get_minimum_size().height;
int sep_ofs = Math::floor((h - sep_h) / 2.0);
theme_cache.separator_style->draw(ci, Rect2(item_ofs + Point2(0, sep_ofs), Size2(display_width, sep_h)));
}
}
Color icon_color = items[i].icon_modulate;
// For non-separator items, add some padding for the content.
if (!items[i].separator) {
item_ofs.x += theme_cache.item_start_padding;
}
// Checkboxes
if (items[i].checkable_type && !items[i].separator) {
int disabled = int(items[i].disabled) * 2;
Texture2D *icon = (items[i].checked ? check[items[i].checkable_type - 1 + disabled] : uncheck[items[i].checkable_type - 1 + disabled]).ptr();
if (rtl) {
icon->draw(ci, Size2(control->get_size().width - item_ofs.x - icon->get_width(), item_ofs.y) + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
} else {
icon->draw(ci, item_ofs + Point2(0, Math::floor((h - icon->get_height()) / 2.0)), icon_color);
}
}
int separator_ofs = (display_width - items[i].text_buf->get_size().width) / 2;
// Icon
if (items[i].icon.is_valid()) {
const Point2 icon_offset = Point2(0, Math::floor((h - icon_size.height) / 2.0));
Point2 icon_pos;
if (items[i].separator) {
separator_ofs -= (icon_size.width + theme_cache.h_separation) / 2;
if (rtl) {
icon_pos = Size2(control->get_size().width - item_ofs.x - separator_ofs - icon_size.width, item_ofs.y);
} else {
icon_pos = item_ofs + Size2(separator_ofs, 0);
separator_ofs += icon_size.width + theme_cache.h_separation;
}
} else {
if (rtl) {
icon_pos = Size2(control->get_size().width - item_ofs.x - check_ofs - icon_size.width, item_ofs.y);
} else {
icon_pos = item_ofs + Size2(check_ofs, 0);
}
}
items[i].icon->draw_rect(ci, Rect2(icon_pos + icon_offset, icon_size), false, icon_color);
}
// Submenu arrow on right hand side.
if (items[i].submenu) {
if (rtl) {
submenu->draw(ci, Point2(theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
} else {
submenu->draw(ci, Point2(display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - submenu->get_width() - theme_cache.item_end_padding, item_ofs.y + Math::floor(h - submenu->get_height()) / 2), icon_color);
}
}
// Text
if (items[i].separator) {
if (!text.is_empty()) {
Vector2 text_pos = Point2(separator_ofs, item_ofs.y + Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
if (theme_cache.font_separator_outline_size > 0 && theme_cache.font_separator_outline_color.a > 0) {
items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_separator_outline_size, theme_cache.font_separator_outline_color);
}
items[i].text_buf->draw(ci, text_pos, theme_cache.font_separator_color);
}
} else {
item_ofs.x += icon_ofs + check_ofs;
if (rtl) {
Vector2 text_pos = Size2(control->get_size().width - items[i].text_buf->get_size().width - item_ofs.x, item_ofs.y) + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
}
items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
} else {
Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
items[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
}
items[i].text_buf->draw(ci, text_pos, items[i].disabled ? theme_cache.font_disabled_color : (i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_color));
}
}
// Accelerator / Shortcut
if (items[i].accel != Key::NONE || (items[i].shortcut.is_valid() && items[i].shortcut->has_valid_event())) {
if (rtl) {
item_ofs.x = theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.item_end_padding;
} else {
item_ofs.x = display_width - theme_cache.panel_style->get_margin(SIDE_RIGHT) - items[i].accel_text_buf->get_size().x - theme_cache.item_end_padding;
}
Vector2 text_pos = item_ofs + Point2(0, Math::floor((h - items[i].text_buf->get_size().y) / 2.0));
if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
items[i].accel_text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
}
items[i].accel_text_buf->draw(ci, text_pos, i == mouse_over ? theme_cache.font_hover_color : theme_cache.font_accelerator_color);
}
// Cache the item vertical offset from the first item and the height.
items.write[i]._ofs_cache = ofs.y;
items.write[i]._height_cache = h;
ofs.y += h;
}
}
void PopupMenu::_minimum_lifetime_timeout() {
close_allowed = true;
// If the mouse still isn't in this popup after timer expires, close.
if (!activated_by_keyboard && !get_visible_rect().has_point(get_mouse_position())) {
_close_pressed();
}
}
void PopupMenu::_close_pressed() {
// Only apply minimum lifetime to submenus.
PopupMenu *parent_pum = Object::cast_to<PopupMenu>(get_parent());
if (!parent_pum) {
Popup::_close_pressed();
return;
}
// If the timer has expired, close. If timer is still running, do nothing.
if (close_allowed) {
close_allowed = false;
Popup::_close_pressed();
} else if (minimum_lifetime_timer->is_stopped()) {
minimum_lifetime_timer->start();
}
}
void PopupMenu::_shape_item(int p_idx) const {
if (items.write[p_idx].dirty) {
items.write[p_idx].text_buf->clear();
Ref<Font> font = items[p_idx].separator ? theme_cache.font_separator : theme_cache.font;