Debian Codegraph @

scene/gui/item_list.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* item_list.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 "item_list.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/os/os.h"
35 #include "scene/theme/theme_db.h"
36
37 void ItemList::_shape_text(int p_idx) {
38 Item &item = items.write[p_idx];
39
40 item.text_buf->clear();
41 if (item.text_direction == Control::TEXT_DIRECTION_INHERITED) {
42 item.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
43 } else {
44 item.text_buf->set_direction((TextServer::Direction)item.text_direction);
45 }
46 const String &lang = item.language.is_empty() ? _get_locale() : item.language;
47 item.text_buf->add_string(item.xl_text, theme_cache.font, theme_cache.font_size, lang);
48 if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
49 item.text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
50 } else {
51 item.text_buf->set_break_flags(TextServer::BREAK_NONE);
52 }
53 item.text_buf->set_text_overrun_behavior(text_overrun_behavior);
54 item.text_buf->set_max_lines_visible(max_text_lines);
55 }
56
57 int ItemList::add_item(const String &p_item, const Ref<Texture2D> &p_texture, bool p_selectable) {
58 Item item;
59 item.icon = p_texture;
60 item.text = p_item;
61 item.selectable = p_selectable;
62 items.push_back(item);
63 int item_id = items.size() - 1;
64
65 items.write[item_id].xl_text = _atr(item_id, p_item);
66 _shape_text(item_id);
67
68 queue_accessibility_update();
69 queue_redraw();
70 shape_changed = true;
71 notify_property_list_changed();
72 return item_id;
73 }
74
75 int ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) {
76 Item item;
77 item.icon = p_item;
78 item.selectable = p_selectable;
79 items.push_back(item);
80 int item_id = items.size() - 1;
81
82 queue_accessibility_update();
83 queue_redraw();
84 shape_changed = true;
85 notify_property_list_changed();
86 return item_id;
87 }
88
89 void ItemList::set_item_text(int p_idx, const String &p_text) {
90 if (p_idx < 0) {
91 p_idx += get_item_count();
92 }
93 ERR_FAIL_INDEX(p_idx, items.size());
94
95 if (items[p_idx].text == p_text) {
96 return;
97 }
98
99 items.write[p_idx].text = p_text;
100 items.write[p_idx].xl_text = _atr(p_idx, p_text);
101 _shape_text(p_idx);
102 queue_accessibility_update();
103 queue_redraw();
104 shape_changed = true;
105 }
106
107 String ItemList::get_item_text(int p_idx) const {
108 ERR_FAIL_INDEX_V(p_idx, items.size(), String());
109 return items[p_idx].text;
110 }
111
112 void ItemList::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) {
113 if (p_idx < 0) {
114 p_idx += get_item_count();
115 }
116 ERR_FAIL_INDEX(p_idx, items.size());
117 ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
118 if (items[p_idx].text_direction != p_text_direction) {
119 items.write[p_idx].text_direction = p_text_direction;
120 _shape_text(p_idx);
121 queue_accessibility_update();
122 queue_redraw();
123 }
124 }
125
126 Control::TextDirection ItemList::get_item_text_direction(int p_idx) const {
127 ERR_FAIL_INDEX_V(p_idx, items.size(), TEXT_DIRECTION_INHERITED);
128 return items[p_idx].text_direction;
129 }
130
131 void ItemList::set_item_language(int p_idx, const String &p_language) {
132 if (p_idx < 0) {
133 p_idx += get_item_count();
134 }
135 ERR_FAIL_INDEX(p_idx, items.size());
136 if (items[p_idx].language != p_language) {
137 items.write[p_idx].language = p_language;
138 _shape_text(p_idx);
139 queue_accessibility_update();
140 queue_redraw();
141 }
142 }
143
144 String ItemList::get_item_language(int p_idx) const {
145 ERR_FAIL_INDEX_V(p_idx, items.size(), "");
146 return items[p_idx].language;
147 }
148
149 void ItemList::set_item_auto_translate_mode(int p_idx, AutoTranslateMode p_mode) {
150 if (p_idx < 0) {
151 p_idx += get_item_count();
152 }
153 ERR_FAIL_INDEX(p_idx, items.size());
154 if (items[p_idx].auto_translate_mode != p_mode) {
155 items.write[p_idx].auto_translate_mode = p_mode;
156 items.write[p_idx].xl_text = _atr(p_idx, items[p_idx].text);
157 _shape_text(p_idx);
158 queue_accessibility_update();
159 queue_redraw();
160 }
161 }
162
163 Node::AutoTranslateMode ItemList::get_item_auto_translate_mode(int p_idx) const {
164 ERR_FAIL_INDEX_V(p_idx, items.size(), AUTO_TRANSLATE_MODE_INHERIT);
165 return items[p_idx].auto_translate_mode;
166 }
167
168 void ItemList::set_item_tooltip_enabled(int p_idx, const bool p_enabled) {
169 if (p_idx < 0) {
170 p_idx += get_item_count();
171 }
172 ERR_FAIL_INDEX(p_idx, items.size());
173 if (items[p_idx].tooltip_enabled != p_enabled) {
174 items.write[p_idx].tooltip_enabled = p_enabled;
175 items.write[p_idx].accessibility_item_dirty = true;
176 queue_accessibility_update();
177 }
178 }
179
180 bool ItemList::is_item_tooltip_enabled(int p_idx) const {
181 ERR_FAIL_INDEX_V(p_idx, items.size(), false);
182 return items[p_idx].tooltip_enabled;
183 }
184
185 void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) {
186 if (p_idx < 0) {
187 p_idx += get_item_count();
188 }
189 ERR_FAIL_INDEX(p_idx, items.size());
190
191 if (items[p_idx].tooltip == p_tooltip) {
192 return;
193 }
194
195 items.write[p_idx].tooltip = p_tooltip;
196 queue_accessibility_update();
197 queue_redraw();
198 shape_changed = true;
199 }
200
201 String ItemList::get_item_tooltip(int p_idx) const {
202 ERR_FAIL_INDEX_V(p_idx, items.size(), String());
203 return items[p_idx].tooltip;
204 }
205
206 void ItemList::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
207 if (p_idx < 0) {
208 p_idx += get_item_count();
209 }
210 ERR_FAIL_INDEX(p_idx, items.size());
211
212 Item &item = items.write[p_idx];
213 if (item.icon == p_icon) {
214 return;
215 }
216
217 const Callable redraw = callable_mp((CanvasItem *)this, &CanvasItem::queue_redraw);
218 if (item.icon.is_valid()) {
219 item.icon->disconnect_changed(redraw);
220 }
221 item.icon = p_icon;
222 if (p_icon.is_valid()) {
223 p_icon->connect_changed(redraw);
224 }
225
226 queue_redraw();
227 shape_changed = true;
228 }
229
230 Ref<Texture2D> ItemList::get_item_icon(int p_idx) const {
231 ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
232
233 return items[p_idx].icon;
234 }
235
236 void ItemList::set_item_icon_transposed(int p_idx, const bool p_transposed) {
237 if (p_idx < 0) {
238 p_idx += get_item_count();
239 }
240 ERR_FAIL_INDEX(p_idx, items.size());
241
242 if (items[p_idx].icon_transposed == p_transposed) {
243 return;
244 }
245
246 items.write[p_idx].icon_transposed = p_transposed;
247 queue_redraw();
248 shape_changed = true;
249 }
250
251 bool ItemList::is_item_icon_transposed(int p_idx) const {
252 ERR_FAIL_INDEX_V(p_idx, items.size(), false);
253
254 return items[p_idx].icon_transposed;
255 }
256
257 void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) {
258 if (p_idx < 0) {
259 p_idx += get_item_count();
260 }
261 ERR_FAIL_INDEX(p_idx, items.size());
262
263 if (items[p_idx].icon_region == p_region) {
264 return;
265 }
266
267 items.write[p_idx].icon_region = p_region;
268 queue_redraw();
269 shape_changed = true;
270 }
271
272 Rect2 ItemList::get_item_icon_region(int p_idx) const {
273 ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
274
275 return items[p_idx].icon_region;
276 }
277
278 void ItemList::set_item_icon_modulate(int p_idx, const Color &p_modulate) {
279 if (p_idx < 0) {
280 p_idx += get_item_count();
281 }
282 ERR_FAIL_INDEX(p_idx, items.size());
283
284 if (items[p_idx].icon_modulate == p_modulate) {
285 return;
286 }
287
288 items.write[p_idx].icon_modulate = p_modulate;
289 queue_redraw();
290 }
291
292 Color ItemList::get_item_icon_modulate(int p_idx) const {
293 ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
294
295 return items[p_idx].icon_modulate;
296 }
297
298 void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) {
299 if (p_idx < 0) {
300 p_idx += get_item_count();
301 }
302 ERR_FAIL_INDEX(p_idx, items.size());
303
304 if (items[p_idx].custom_bg == p_custom_bg_color) {
305 return;
306 }
307
308 items.write[p_idx].custom_bg = p_custom_bg_color;
309 queue_redraw();
310 }
311
312 Color ItemList::get_item_custom_bg_color(int p_idx) const {
313 ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
314
315 return items[p_idx].custom_bg;
316 }
317
318 void ItemList::set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color) {
319 if (p_idx < 0) {
320 p_idx += get_item_count();
321 }
322 ERR_FAIL_INDEX(p_idx, items.size());
323
324 if (items[p_idx].custom_fg == p_custom_fg_color) {
325 return;
326 }
327
328 items.write[p_idx].custom_fg = p_custom_fg_color;
329 queue_redraw();
330 }
331
332 Color ItemList::get_item_custom_fg_color(int p_idx) const {
333 ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
334
335 return items[p_idx].custom_fg;
336 }
337
338 Rect2 ItemList::get_item_rect(int p_idx, bool p_expand) const {
339 ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
340
341 Rect2 ret = items[p_idx].rect_cache;
342 if (p_expand && p_idx % current_columns == current_columns - 1) {
343 int width = get_size().width - theme_cache.panel_style->get_minimum_size().width;
344 if (scroll_bar_v->is_visible()) {
345 width -= scroll_bar_v->get_combined_minimum_size().width;
346 }
347 ret.size.width = width - ret.position.x;
348 }
349 ret.position += theme_cache.panel_style->get_offset();
350 return ret;
351 }
352
353 void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon) {
354 if (p_idx < 0) {
355 p_idx += get_item_count();
356 }
357 ERR_FAIL_INDEX(p_idx, items.size());
358
359 if (items[p_idx].tag_icon == p_tag_icon) {
360 return;
361 }
362
363 items.write[p_idx].tag_icon = p_tag_icon;
364 queue_redraw();
365 shape_changed = true;
366 }
367
368 void ItemList::set_item_selectable(int p_idx, bool p_selectable) {
369 if (p_idx < 0) {
370 p_idx += get_item_count();
371 }
372 ERR_FAIL_INDEX(p_idx, items.size());
373
374 items.write[p_idx].selectable = p_selectable;
375 items.write[p_idx].accessibility_item_dirty = true;
376 queue_accessibility_update();
377 }
378
379 bool ItemList::is_item_selectable(int p_idx) const {
380 ERR_FAIL_INDEX_V(p_idx, items.size(), false);
381 return items[p_idx].selectable;
382 }
383
384 void ItemList::set_item_disabled(int p_idx, bool p_disabled) {
385 if (p_idx < 0) {
386 p_idx += get_item_count();
387 }
388 ERR_FAIL_INDEX(p_idx, items.size());
389
390 if (items[p_idx].disabled == p_disabled) {
391 return;
392 }
393
394 items.write[p_idx].disabled = p_disabled;
395 items.write[p_idx].accessibility_item_dirty = true;
396 queue_accessibility_update();
397 queue_redraw();
398 }
399
400 bool ItemList::is_item_disabled(int p_idx) const {
401 ERR_FAIL_INDEX_V(p_idx, items.size(), false);
402 return items[p_idx].disabled;
403 }
404
405 void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) {
406 if (p_idx < 0) {
407 p_idx += get_item_count();
408 }
409 ERR_FAIL_INDEX(p_idx, items.size());
410
411 if (items[p_idx].metadata == p_metadata) {
412 return;
413 }
414
415 items.write[p_idx].metadata = p_metadata;
416 queue_redraw();
417 shape_changed = true;
418 }
419
420 Variant ItemList::get_item_metadata(int p_idx) const {
421 ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
422 return items[p_idx].metadata;
423 }
424
425 void ItemList::select(int p_idx, bool p_single) {
426 ERR_FAIL_INDEX(p_idx, items.size());
427
428 if (p_single || select_mode == SELECT_SINGLE) {
429 if (!items[p_idx].selectable || items[p_idx].disabled) {
430 return;
431 }
432
433 for (int i = 0; i < items.size(); i++) {
434 if (items.write[i].selected != (p_idx == i)) {
435 items.write[i].selected = (p_idx == i);
436 items.write[i].accessibility_item_dirty = true;
437 }
438 }
439
440 current = p_idx;
441 ensure_selected_visible = false;
442 } else {
443 if (items[p_idx].selectable && !items[p_idx].disabled) {
444 items.write[p_idx].selected = true;
445 items.write[p_idx].accessibility_item_dirty = true;
446 }
447 }
448 queue_accessibility_update();
449 queue_redraw();
450 }
451
452 void ItemList::deselect(int p_idx) {
453 ERR_FAIL_INDEX(p_idx, items.size());
454
455 if (select_mode == SELECT_SINGLE) {
456 items.write[p_idx].selected = false;
457 current = -1;
458 } else {
459 items.write[p_idx].selected = false;
460 }
461 items.write[p_idx].accessibility_item_dirty = true;
462 queue_accessibility_update();
463 queue_redraw();
464 }
465
466 void ItemList::deselect_all() {
467 if (items.is_empty()) {
468 return;
469 }
470
471 for (int i = 0; i < items.size(); i++) {
472 if (items.write[i].selected) {
473 items.write[i].selected = false;
474 items.write[i].accessibility_item_dirty = true;
475 }
476 }
477 current = -1;
478 queue_accessibility_update();
479 queue_redraw();
480 }
481
482 bool ItemList::is_selected(int p_idx) const {
483 ERR_FAIL_INDEX_V(p_idx, items.size(), false);
484
485 return items[p_idx].selected;
486 }
487
488 void ItemList::set_current(int p_current) {
489 ERR_FAIL_INDEX(p_current, items.size());
490
491 if (current == p_current) {
492 return;
493 }
494
495 if (select_mode == SELECT_SINGLE) {
496 select(p_current, true);
497 } else {
498 current = p_current;
499 queue_accessibility_update();
500 queue_redraw();
501 }
502 }
503
504 int ItemList::get_current() const {
505 return current;
506 }
507
508 void ItemList::move_item(int p_from_idx, int p_to_idx) {
509 ERR_FAIL_INDEX(p_from_idx, items.size());
510 ERR_FAIL_INDEX(p_to_idx, items.size());
511
512 if (is_anything_selected() && get_selected_items()[0] == p_from_idx) {
513 current = p_to_idx;
514 }
515
516 Item item = items[p_from_idx];
517 items.remove_at(p_from_idx);
518 items.insert(p_to_idx, item);
519
520 queue_accessibility_update();
521 queue_redraw();
522 shape_changed = true;
523 notify_property_list_changed();
524 }
525
526 void ItemList::set_item_count(int p_count) {
527 ERR_FAIL_COND(p_count < 0);
528
529 if (items.size() == p_count) {
530 return;
531 }
532
533 if (items.size() > p_count) {
534 for (int i = p_count; i < items.size(); i++) {
535 if (items[i].accessibility_item_element.is_valid()) {
536 DisplayServer::get_singleton()->accessibility_free_element(items.write[i].accessibility_item_element);
537 items.write[i].accessibility_item_element = RID();
538 }
539 }
540 }
541
542 items.resize(p_count);
543 queue_accessibility_update();
544 queue_redraw();
545 shape_changed = true;
546 notify_property_list_changed();
547 }
548
549 int ItemList::get_item_count() const {
550 return items.size();
551 }
552
553 void ItemList::remove_item(int p_idx) {
554 ERR_FAIL_INDEX(p_idx, items.size());
555
556 if (items[p_idx].accessibility_item_element.is_valid()) {
557 DisplayServer::get_singleton()->accessibility_free_element(items.write[p_idx].accessibility_item_element);
558 items.write[p_idx].accessibility_item_element = RID();
559 }
560 items.remove_at(p_idx);
561 if (current == p_idx) {
562 current = -1;
563 }
564 queue_accessibility_update();
565 queue_redraw();
566 shape_changed = true;
567 defer_select_single = -1;
568 notify_property_list_changed();
569 }
570
571 void ItemList::clear() {
572 for (int i = 0; i < items.size(); i++) {
573 if (items[i].accessibility_item_element.is_valid()) {
574 DisplayServer::get_singleton()->accessibility_free_element(items.write[i].accessibility_item_element);
575 items.write[i].accessibility_item_element = RID();
576 }
577 }
578 items.clear();
579 current = -1;
580 ensure_selected_visible = false;
581 queue_accessibility_update();
582 queue_redraw();
583 shape_changed = true;
584 defer_select_single = -1;
585 notify_property_list_changed();
586 }
587
588 void ItemList::set_fixed_column_width(int p_size) {
589 ERR_FAIL_COND(p_size < 0);
590
591 if (fixed_column_width == p_size) {
592 return;
593 }
594
595 fixed_column_width = p_size;
596 queue_redraw();
597 shape_changed = true;
598 }
599
600 int ItemList::get_fixed_column_width() const {
601 return fixed_column_width;
602 }
603
604 void ItemList::set_same_column_width(bool p_enable) {
605 if (same_column_width == p_enable) {
606 return;
607 }
608
609 same_column_width = p_enable;
610 queue_redraw();
611 shape_changed = true;
612 }
613
614 bool ItemList::is_same_column_width() const {
615 return same_column_width;
616 }
617
618 void ItemList::set_max_text_lines(int p_lines) {
619 ERR_FAIL_COND(p_lines < 1);
620 if (max_text_lines != p_lines) {
621 max_text_lines = p_lines;
622 for (int i = 0; i < items.size(); i++) {
623 if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
624 items.write[i].text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
625 items.write[i].text_buf->set_max_lines_visible(p_lines);
626 } else {
627 items.write[i].text_buf->set_break_flags(TextServer::BREAK_NONE);
628 }
629 }
630 shape_changed = true;
631 queue_accessibility_update();
632 queue_redraw();
633 }
634 }
635
636 int ItemList::get_max_text_lines() const {
637 return max_text_lines;
638 }
639
640 void ItemList::set_max_columns(int p_amount) {
641 ERR_FAIL_COND(p_amount < 0);
642
643 if (max_columns == p_amount) {
644 return;
645 }
646
647 max_columns = p_amount;
648 queue_accessibility_update();
649 queue_redraw();
650 shape_changed = true;
651 }
652
653 int ItemList::get_max_columns() const {
654 return max_columns;
655 }
656
657 void ItemList::set_select_mode(SelectMode p_mode) {
658 if (select_mode == p_mode) {
659 return;
660 }
661
662 select_mode = p_mode;
663 queue_accessibility_update();
664 queue_redraw();
665 }
666
667 ItemList::SelectMode ItemList::get_select_mode() const {
668 return select_mode;
669 }
670
671 void ItemList::set_icon_mode(IconMode p_mode) {
672 ERR_FAIL_INDEX((int)p_mode, 2);
673 if (icon_mode != p_mode) {
674 icon_mode = p_mode;
675 for (int i = 0; i < items.size(); i++) {
676 if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
677 items.write[i].text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES);
678 } else {
679 items.write[i].text_buf->set_break_flags(TextServer::BREAK_NONE);
680 }
681 }
682 shape_changed = true;
683 queue_redraw();
684 }
685 }
686
687 ItemList::IconMode ItemList::get_icon_mode() const {
688 return icon_mode;
689 }
690
691 void ItemList::set_fixed_icon_size(const Size2i &p_size) {
692 if (fixed_icon_size == p_size) {
693 return;
694 }
695
696 fixed_icon_size = p_size;
697 queue_redraw();
698 shape_changed = true;
699 }
700
701 Size2i ItemList::get_fixed_icon_size() const {
702 return fixed_icon_size;
703 }
704
705 Size2 ItemList::Item::get_icon_size() const {
706 if (icon.is_null()) {
707 return Size2();
708 }
709
710 Size2 size_result = Size2(icon_region.size).abs();
711 if (icon_region.size.x == 0 || icon_region.size.y == 0) {
712 size_result = icon->get_size();
713 }
714
715 if (icon_transposed) {
716 Size2 size_tmp = size_result;
717 size_result.x = size_tmp.y;
718 size_result.y = size_tmp.x;
719 }
720
721 return size_result;
722 }
723
724 void ItemList::set_fixed_tag_icon_size(const Size2i &p_size) {
725 if (fixed_tag_icon_size == p_size) {
726 return;
727 }
728
729 fixed_tag_icon_size = p_size;
730 queue_redraw();
731 shape_changed = true;
732 }
733
734 void ItemList::gui_input(const Ref<InputEvent> &p_event) {
735 ERR_FAIL_COND(p_event.is_null());
736
737 #define CAN_SELECT(i) (items[i].selectable && !items[i].disabled)
738 #define IS_SAME_ROW(i, row) (i / current_columns == row)
739
740 double prev_scroll_v = scroll_bar_v->get_value();
741 double prev_scroll_h = scroll_bar_h->get_value();
742 bool scroll_value_modified = false;
743
744 Ref<InputEventMouseMotion> mm = p_event;
745 if (defer_select_single >= 0 && mm.is_valid()) {
746 defer_select_single = -1;
747 return;
748 }
749
750 Ref<InputEventMouseButton> mb = p_event;
751 Ref<InputEventKey> ev_key = p_event;
752
753 if (ev_key.is_valid() && ev_key->get_keycode() == Key::SHIFT && !ev_key->is_pressed()) {
754 shift_anchor = -1;
755 }
756
757 if (defer_select_single >= 0 && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
758 select(defer_select_single, true);
759
760 emit_signal(SNAME("multi_selected"), defer_select_single, true);
761 defer_select_single = -1;
762 return;
763 }
764
765 if (mm.is_valid()) {
766 int closest = get_item_at_position(mm->get_position(), true);
767 if (closest != hovered) {
768 prev_hovered = hovered;
769 hovered = closest;
770 queue_accessibility_update();
771 queue_redraw();
772 }
773 }
774
775 if (mb.is_valid() && mb->is_pressed()) {
776 search_string = ""; //any mousepress cancels
777
778 int closest = get_item_at_position(mb->get_position(), true);
779
780 if (closest != -1 && (mb->get_button_index() == MouseButton::LEFT || (allow_rmb_select && mb->get_button_index() == MouseButton::RIGHT))) {
781 int i = closest;
782
783 if (items[i].disabled) {
784 // Don't emit any signal or do any action with clicked item when disabled.
785 return;
786 }
787
788 if (select_mode == SELECT_MULTI && items[i].selected && mb->is_command_or_control_pressed()) {
789 deselect(i);
790 emit_signal(SNAME("multi_selected"), i, false);
791 emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index());
792
793 } else if (select_mode == SELECT_MULTI && mb->is_shift_pressed() && current >= 0 && current < items.size() && current != i) {
794 // Range selection.
795
796 int from = current;
797 int to = i;
798 if (i < current) {
799 SWAP(from, to);
800 }
801 for (int j = from; j <= to; j++) {
802 if (!CAN_SELECT(j)) {
803 // Item is not selectable during a range selection, so skip it.
804 continue;
805 }
806 bool selected = !items[j].selected;
807 select(j, false);
808 if (selected) {
809 emit_signal(SNAME("multi_selected"), j, true);
810 }
811 }
812 emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index());
813
814 } else {
815 if (!mb->is_double_click() &&
816 !mb->is_command_or_control_pressed() &&
817 select_mode == SELECT_MULTI &&
818 items[i].selectable &&
819 items[i].selected &&
820 mb->get_button_index() == MouseButton::LEFT) {
821 defer_select_single = i;
822 return;
823 }
824
825 if (select_mode == SELECT_TOGGLE) {
826 if (items[i].selectable) {
827 if (items[i].selected) {
828 deselect(i);
829 current = i;
830 emit_signal(SNAME("multi_selected"), i, false);
831 } else {
832 select(i, false);
833 current = i;
834 emit_signal(SNAME("multi_selected"), i, true);
835 }
836 }
837 } else if (items[i].selectable && (!items[i].selected || allow_reselect)) {
838 select(i, select_mode == SELECT_SINGLE || !mb->is_command_or_control_pressed());
839
840 if (select_mode == SELECT_SINGLE) {
841 emit_signal(SceneStringName(item_selected), i);
842 } else {
843 emit_signal(SNAME("multi_selected"), i, true);
844 }
845 }
846
847 emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index());
848
849 if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) {
850 emit_signal(SNAME("item_activated"), i);
851 }
852 }
853
854 return;
855 } else if (closest != -1) {
856 if (!items[closest].disabled) {
857 emit_signal(SNAME("item_clicked"), closest, get_local_mouse_position(), mb->get_button_index());
858 }
859 } else {
860 // Since closest is null, more likely we clicked on empty space, so send signal to interested controls. Allows, for example, implement items deselecting.
861 emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
862 }
863 }
864 if (mb.is_valid()) { // Copied from ScrollContainer.
865 if (mb->is_pressed()) {
866 bool v_scroll_hidden = !scroll_bar_v->is_visible();
867 if (mb->get_button_index() == MouseButton::WHEEL_UP) {
868 // By default, the vertical orientation takes precedence. This is an exception.
869 if (mb->is_shift_pressed() || v_scroll_hidden) {
870 scroll_bar_h->scroll(-scroll_bar_h->get_page() / 8 * mb->get_factor());
871 scroll_value_modified = true;
872 } else {
873 scroll_bar_v->scroll(-scroll_bar_v->get_page() / 8 * mb->get_factor());
874 scroll_value_modified = true;
875 }
876 }
877 if (mb->get_button_index() == MouseButton::WHEEL_DOWN) {
878 if (mb->is_shift_pressed() || v_scroll_hidden) {
879 scroll_bar_h->scroll(scroll_bar_h->get_page() / 8 * mb->get_factor());
880 scroll_value_modified = true;
881 } else {
882 scroll_bar_v->scroll(scroll_bar_v->get_page() / 8 * mb->get_factor());
883 scroll_value_modified = true;
884 }
885 }
886
887 bool h_scroll_hidden = !scroll_bar_h->is_visible();
888 if (mb->get_button_index() == MouseButton::WHEEL_LEFT) {
889 // By default, the horizontal orientation takes precedence. This is an exception.
890 if (mb->is_shift_pressed() || h_scroll_hidden) {
891 scroll_bar_v->scroll(-scroll_bar_v->get_page() / 8 * mb->get_factor());
892 scroll_value_modified = true;
893 } else {
894 scroll_bar_h->scroll(-scroll_bar_h->get_page() / 8 * mb->get_factor());
895 scroll_value_modified = true;
896 }
897 }
898 if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) {
899 if (mb->is_shift_pressed() || h_scroll_hidden) {
900 scroll_bar_v->scroll(scroll_bar_v->get_page() / 8 * mb->get_factor());
901 scroll_value_modified = true;
902 } else {
903 scroll_bar_h->scroll(scroll_bar_h->get_page() / 8 * mb->get_factor());
904 scroll_value_modified = true;
905 }
906 }
907 }
908 }
909
910 if (p_event->is_pressed() && items.size() > 0) {
911 if (p_event->is_action("ui_menu", true)) {
912 if (current != -1 && allow_rmb_select) {
913 int i = current;
914
915 if (items[i].disabled) {
916 // Don't emit any signal or do any action with clicked item when disabled.
917 return;
918 }
919
920 emit_signal(SNAME("item_clicked"), i, get_item_rect(i).position, MouseButton::RIGHT);
921
922 accept_event();
923 return;
924 }
925 }
926 // Shift Up Selection.
927 if (select_mode == SELECT_MULTI && p_event->is_action("ui_up", false) && ev_key.is_valid() && ev_key->is_shift_pressed()) {
928 int next = MAX(current - max_columns, 0);
929 _shift_range_select(current, next);
930 accept_event();
931 }
932
933 else if (p_event->is_action("ui_up", true)) {
934 if (!search_string.is_empty()) {
935 uint64_t now = OS::get_singleton()->get_ticks_msec();
936 uint64_t diff = now - search_time_msec;
937
938 if (diff < uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec")) * 2) {
939 for (int i = current - 1; i >= 0; i--) {
940 if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
941 set_current(i);
942 ensure_current_is_visible();
943 if (select_mode == SELECT_SINGLE) {
944 emit_signal(SceneStringName(item_selected), current);
945 }
946
947 break;
948 }
949 }
950 accept_event();
951 return;
952 }
953 }
954
955 if (current >= current_columns) {
956 int next = current - current_columns;
957 while (next >= 0 && !CAN_SELECT(next)) {
958 next = next - current_columns;
959 }
960 if (next < 0) {
961 accept_event();
962 return;
963 }
964 set_current(next);
965 ensure_current_is_visible();
966 if (select_mode == SELECT_SINGLE) {
967 emit_signal(SceneStringName(item_selected), current);
968 }
969 accept_event();
970 }
971 }
972
973 // Shift Down Selection.
974 else if (select_mode == SELECT_MULTI && p_event->is_action("ui_down", false) && ev_key.is_valid() && ev_key->is_shift_pressed()) {
975 int next = MIN(current + max_columns, items.size() - 1);
976 _shift_range_select(current, next);
977 accept_event();
978 }
979
980 else if (p_event->is_action("ui_down", true)) {
981 if (!search_string.is_empty()) {
982 uint64_t now = OS::get_singleton()->get_ticks_msec();
983 uint64_t diff = now - search_time_msec;
984
985 if (diff < uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec")) * 2) {
986 for (int i = current + 1; i < items.size(); i++) {
987 if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
988 set_current(i);
989 ensure_current_is_visible();
990 if (select_mode == SELECT_SINGLE) {
991 emit_signal(SceneStringName(item_selected), current);
992 }
993 break;
994 }
995 }
996 accept_event();
997 return;
998 }
999 }
1000
1001 if (current < items.size() - current_columns) {
1002 int next = current + current_columns;
1003 while (next < items.size() && !CAN_SELECT(next)) {
1004 next = next + current_columns;
1005 }
1006 if (next >= items.size()) {
1007 accept_event();
1008 return;
1009 }
1010 set_current(next);
1011 ensure_current_is_visible();
1012 if (select_mode == SELECT_SINGLE) {
1013 emit_signal(SceneStringName(item_selected), current);
1014 }
1015 accept_event();
1016 }
1017 } else if (p_event->is_action("ui_page_up", true)) {
1018 search_string = ""; //any mousepress cancels
1019
1020 for (int i = 4; i > 0; i--) {
1021 int index = current - current_columns * i;
1022 if (index >= 0 && index < items.size() && CAN_SELECT(index)) {
1023 set_current(index);
1024 ensure_current_is_visible();
1025 if (select_mode == SELECT_SINGLE) {
1026 emit_signal(SceneStringName(item_selected), current);
1027 }
1028 accept_event();
1029 break;
1030 }
1031 }
1032 } else if (p_event->is_action("ui_page_down", true)) {
1033 search_string = ""; //any mousepress cancels
1034
1035 for (int i = 4; i > 0; i--) {
1036 int index = current + current_columns * i;
1037 if (index >= 0 && index < items.size() && CAN_SELECT(index)) {
1038 set_current(index);
1039 ensure_current_is_visible();
1040 if (select_mode == SELECT_SINGLE) {
1041 emit_signal(SceneStringName(item_selected), current);
1042 }
1043 accept_event();
1044
1045 break;
1046 }
1047 }
1048 }
1049
1050 // Shift Left Selection.
1051 else if (select_mode == SELECT_MULTI && p_event->is_action("ui_left", false) && ev_key.is_valid() && ev_key->is_shift_pressed()) {
1052 int next = MAX(current - 1, 0);
1053 _shift_range_select(current, next);
1054 accept_event();
1055 }
1056
1057 else if (p_event->is_action("ui_left", true)) {
1058 search_string = ""; //any mousepress cancels
1059
1060 if (current % current_columns != 0) {
1061 int current_row = current / current_columns;
1062 int next = current - 1;
1063 while (next >= 0 && !CAN_SELECT(next)) {
1064 next = next - 1;
1065 }
1066 if (next < 0 || !IS_SAME_ROW(next, current_row)) {
1067 accept_event();
1068 return;
1069 }
1070 set_current(next);
1071 ensure_current_is_visible();
1072 if (select_mode == SELECT_SINGLE) {
1073 emit_signal(SceneStringName(item_selected), current);
1074 }
1075 accept_event();
1076 }
1077 }
1078
1079 // Shift Right Selection.
1080 else if (select_mode == SELECT_MULTI && p_event->is_action("ui_right", false) && ev_key.is_valid() && ev_key->is_shift_pressed()) {
1081 int next = MIN(current + 1, items.size() - 1);
1082 _shift_range_select(current, next);
1083 accept_event();
1084 }
1085
1086 else if (p_event->is_action("ui_right", true)) {
1087 search_string = ""; //any mousepress cancels
1088
1089 if (current % current_columns != (current_columns - 1) && current + 1 < items.size()) {
1090 int current_row = current / current_columns;
1091 int next = current + 1;
1092 while (next < items.size() && !CAN_SELECT(next)) {
1093 next = next + 1;
1094 }
1095 if (items.size() <= next || !IS_SAME_ROW(next, current_row)) {
1096 accept_event();
1097 return;
1098 }
1099 set_current(next);
1100 ensure_current_is_visible();
1101 if (select_mode == SELECT_SINGLE) {
1102 emit_signal(SceneStringName(item_selected), current);
1103 }
1104 accept_event();
1105 }
1106 } else if (p_event->is_action("ui_cancel", true)) {
1107 search_string = "";
1108 } else if (p_event->is_action("ui_select", true) && (select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE)) {
1109 if (current >= 0 && current < items.size()) {
1110 if (CAN_SELECT(current) && !items[current].selected) {
1111 select(current, false);
1112 emit_signal(SNAME("multi_selected"), current, true);
1113 } else if (items[current].selected) {
1114 deselect(current);
1115 emit_signal(SNAME("multi_selected"), current, false);
1116 }
1117 }
1118 } else if (p_event->is_action("ui_accept", true)) {
1119 search_string = ""; //any mousepress cancels
1120
1121 if (current >= 0 && current < items.size() && !items[current].disabled) {
1122 emit_signal(SNAME("item_activated"), current);
1123 }
1124 } else {
1125 Ref<InputEventKey> k = p_event;
1126
1127 if (allow_search && k.is_valid() && k->get_unicode()) {
1128 uint64_t now = OS::get_singleton()->get_ticks_msec();
1129 uint64_t diff = now - search_time_msec;
1130 uint64_t max_interval = uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec"));
1131 search_time_msec = now;
1132
1133 if (diff > max_interval) {
1134 search_string = "";
1135 }
1136
1137 if (String::chr(k->get_unicode()) != search_string) {
1138 search_string += String::chr(k->get_unicode());
1139 }
1140
1141 for (int i = current + 1; i <= items.size(); i++) {
1142 if (i == items.size()) {
1143 if (current == 0 || current == -1) {
1144 break;
1145 } else {
1146 i = 0;
1147 }
1148 }
1149
1150 if (i == current) {
1151 break;
1152 }
1153
1154 if (items[i].text.findn(search_string) == 0) {
1155 set_current(i);
1156 ensure_current_is_visible();
1157 if (select_mode == SELECT_SINGLE) {
1158 emit_signal(SceneStringName(item_selected), current);
1159 }
1160 break;
1161 }
1162 }
1163 }
1164 }
1165 }
1166
1167 Ref<InputEventPanGesture> pan_gesture = p_event;
1168 if (pan_gesture.is_valid()) {
1169 scroll_bar_v->set_value(scroll_bar_v->get_value() + scroll_bar_v->get_page() * pan_gesture->get_delta().y / 8);
1170 scroll_bar_h->set_value(scroll_bar_h->get_value() + scroll_bar_h->get_page() * pan_gesture->get_delta().x / 8);
1171 }
1172
1173 if (scroll_value_modified && (scroll_bar_v->get_value() != prev_scroll_v || scroll_bar_h->get_value() != prev_scroll_h)) {
1174 accept_event(); //accept event if scroll changed
1175 }
1176
1177 #undef CAN_SELECT
1178 #undef IS_SAME_ROW
1179 }
1180
1181 void ItemList::ensure_current_is_visible() {
1182 ensure_selected_visible = true;
1183 queue_redraw();
1184 }
1185
1186 static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) {
1187 Size2 size = p_max_size;
1188 int tex_width = p_size.width * size.height / p_size.height;
1189 int tex_height = size.height;
1190
1191 if (tex_width > size.width) {
1192 tex_width = size.width;
1193 tex_height = p_size.height * tex_width / p_size.width;
1194 }
1195
1196 int ofs_x = (size.width - tex_width) / 2;
1197 int ofs_y = (size.height - tex_height) / 2;
1198
1199 return Rect2(ofs_x, ofs_y, tex_width, tex_height);
1200 }
1201
1202 RID ItemList::get_focused_accessibility_element() const {
1203 if (current == -1) {
1204 return get_accessibility_element();
1205 } else {
1206 const Item &item = items[current];
1207 return item.accessibility_item_element;
1208 }
1209 }
1210
1211 void ItemList::_accessibility_action_scroll_set(const Variant &p_data) {
1212 const Point2 &pos = p_data;
1213 scroll_bar_h->set_value(pos.x);
1214 scroll_bar_v->set_value(pos.y);
1215 }
1216
1217 void ItemList::_accessibility_action_scroll_up(const Variant &p_data) {
1218 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
1219 scroll_bar_v->set_value(scroll_bar_v->get_value() - scroll_bar_v->get_page() / 4);
1220 } else {
1221 scroll_bar_v->set_value(scroll_bar_v->get_value() - scroll_bar_v->get_page());
1222 }
1223 }
1224
1225 void ItemList::_accessibility_action_scroll_down(const Variant &p_data) {
1226 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
1227 scroll_bar_v->set_value(scroll_bar_v->get_value() + scroll_bar_v->get_page() / 4);
1228 } else {
1229 scroll_bar_v->set_value(scroll_bar_v->get_value() + scroll_bar_v->get_page());
1230 }
1231 }
1232
1233 void ItemList::_accessibility_action_scroll_left(const Variant &p_data) {
1234 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
1235 scroll_bar_h->set_value(scroll_bar_h->get_value() - scroll_bar_h->get_page() / 4);
1236 } else {
1237 scroll_bar_h->set_value(scroll_bar_h->get_value() - scroll_bar_h->get_page());
1238 }
1239 }
1240
1241 void ItemList::_accessibility_action_scroll_right(const Variant &p_data) {
1242 if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
1243 scroll_bar_h->set_value(scroll_bar_h->get_value() + scroll_bar_h->get_page() / 4);
1244 } else {
1245 scroll_bar_h->set_value(scroll_bar_h->get_value() + scroll_bar_h->get_page());
1246 }
1247 }
1248
1249 void ItemList::_accessibility_action_scroll_into_view(const Variant &p_data, int p_index) {
1250 ERR_FAIL_INDEX(p_index, items.size());
1251
1252 Rect2 r = items[p_index].rect_cache;
1253 int from_v = scroll_bar_v->get_value();
1254 int to_v = from_v + scroll_bar_v->get_page();
1255 int from_h = scroll_bar_h->get_value();
1256 int to_h = from_h + scroll_bar_h->get_page();
1257
1258 if (r.position.y < from_v) {
1259 scroll_bar_v->set_value(r.position.y);
1260 } else if (r.position.y + r.size.y > to_v) {
1261 scroll_bar_v->set_value(r.position.y + r.size.y - (to_v - from_v));
1262 }
1263 if (r.position.x < from_h) {
1264 scroll_bar_h->set_value(r.position.x);
1265 } else if (r.position.x + r.size.x > to_h) {
1266 scroll_bar_h->set_value(r.position.x + r.size.x - (to_h - from_h));
1267 }
1268 }
1269
1270 void ItemList::_accessibility_action_focus(const Variant &p_data, int p_index) {
1271 select(p_index);
1272 }
1273
1274 void ItemList::_accessibility_action_blur(const Variant &p_data, int p_index) {
1275 deselect(p_index);
1276 }
1277
1278 void ItemList::_notification(int p_what) {
1279 switch (p_what) {
1280 case NOTIFICATION_EXIT_TREE:
1281 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
1282 for (int i = 0; i < items.size(); i++) {
1283 items.write[i].accessibility_item_element = RID();
1284 }
1285 accessibility_scroll_element = RID();
1286 } break;
1287
1288 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1289 RID ae = get_accessibility_element();
1290 ERR_FAIL_COND(ae.is_null());
1291
1292 force_update_list_size();
1293
1294 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_LIST_BOX);
1295 DisplayServer::get_singleton()->accessibility_update_set_list_item_count(ae, items.size());
1296 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_MULTISELECTABLE, select_mode == SELECT_MULTI);
1297 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &ItemList::_accessibility_action_scroll_down));
1298 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &ItemList::_accessibility_action_scroll_up));
1299 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &ItemList::_accessibility_action_scroll_left));
1300 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &ItemList::_accessibility_action_scroll_right));
1301 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &ItemList::_accessibility_action_scroll_set));
1302
1303 if (accessibility_scroll_element.is_null()) {
1304 accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
1305 }
1306
1307 Transform2D scroll_xform;
1308 scroll_xform.set_origin(Vector2i(-scroll_bar_h->get_value(), -scroll_bar_v->get_value()));
1309 DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
1310 DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, scroll_bar_h->get_max(), scroll_bar_v->get_max()));
1311
1312 for (int i = 0; i < items.size(); i++) {
1313 const Item &item = items.write[i];
1314
1315 if (item.accessibility_item_element.is_null()) {
1316 item.accessibility_item_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_LIST_BOX_OPTION);
1317 item.accessibility_item_dirty = true;
1318 }
1319 if (item.accessibility_item_dirty || i == hovered || i == prev_hovered) {
1320 DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &ItemList::_accessibility_action_scroll_into_view).bind(i));
1321 DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &ItemList::_accessibility_action_focus).bind(i));
1322 DisplayServer::get_singleton()->accessibility_update_add_action(item.accessibility_item_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &ItemList::_accessibility_action_blur).bind(i));
1323
1324 DisplayServer::get_singleton()->accessibility_update_set_list_item_index(item.accessibility_item_element, i);
1325 DisplayServer::get_singleton()->accessibility_update_set_list_item_level(item.accessibility_item_element, 0);
1326 DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(item.accessibility_item_element, item.selected);
1327 DisplayServer::get_singleton()->accessibility_update_set_name(item.accessibility_item_element, item.xl_text);
1328 DisplayServer::get_singleton()->accessibility_update_set_flag(item.accessibility_item_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, item.disabled);
1329 if (item.tooltip_enabled) {
1330 DisplayServer::get_singleton()->accessibility_update_set_tooltip(item.accessibility_item_element, item.tooltip);
1331 }
1332
1333 Rect2 r = get_item_rect(i);
1334 DisplayServer::get_singleton()->accessibility_update_set_bounds(item.accessibility_item_element, Rect2(r.position, r.size));
1335
1336 item.accessibility_item_dirty = false;
1337 }
1338 }
1339 prev_hovered = -1;
1340
1341 } break;
1342
1343 case NOTIFICATION_RESIZED: {
1344 shape_changed = true;
1345 queue_redraw();
1346 } break;
1347
1348 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
1349 case NOTIFICATION_THEME_CHANGED: {
1350 for (int i = 0; i < items.size(); i++) {
1351 _shape_text(i);
1352 }
1353 shape_changed = true;
1354 queue_accessibility_update();
1355 queue_redraw();
1356 } break;
1357 case NOTIFICATION_TRANSLATION_CHANGED: {
1358 for (int i = 0; i < items.size(); i++) {
1359 items.write[i].xl_text = _atr(i, items[i].text);
1360 _shape_text(i);
1361 }
1362 shape_changed = true;
1363 queue_accessibility_update();
1364 queue_redraw();
1365 } break;
1366
1367 case NOTIFICATION_DRAW: {
1368 force_update_list_size();
1369
1370 Size2 scroll_bar_h_min = scroll_bar_h->is_visible() ? scroll_bar_h->get_combined_minimum_size() : Size2();
1371 Size2 scroll_bar_v_min = scroll_bar_v->is_visible() ? scroll_bar_v->get_combined_minimum_size() : Size2();
1372
1373 int left_margin = is_layout_rtl() ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT);
1374 int right_margin = is_layout_rtl() ? theme_cache.panel_style->get_margin(SIDE_LEFT) : theme_cache.panel_style->get_margin(SIDE_RIGHT);
1375
1376 scroll_bar_v->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_bar_v_min.width - right_margin);
1377 scroll_bar_v->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -right_margin);
1378 scroll_bar_v->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, theme_cache.panel_style->get_margin(SIDE_TOP));
1379 scroll_bar_v->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -scroll_bar_h_min.height - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
1380
1381 scroll_bar_h->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, left_margin);
1382 scroll_bar_h->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -right_margin - scroll_bar_v_min.width);
1383 scroll_bar_h->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -scroll_bar_h_min.height - theme_cache.panel_style->get_margin(SIDE_BOTTOM));
1384 scroll_bar_h->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -theme_cache.panel_style->get_margin(SIDE_BOTTOM));
1385
1386 Size2 size = get_size();
1387 int width = size.width - theme_cache.panel_style->get_minimum_size().width;
1388 if (scroll_bar_v->is_visible()) {
1389 width -= scroll_bar_v_min.width;
1390 }
1391
1392 draw_style_box(theme_cache.panel_style, Rect2(Point2(), size));
1393
1394 Ref<StyleBox> sbsel;
1395 Ref<StyleBox> cursor;
1396
1397 if (has_focus(true)) {
1398 sbsel = theme_cache.selected_focus_style;
1399 cursor = theme_cache.cursor_focus_style;
1400 } else {
1401 sbsel = theme_cache.selected_style;
1402 cursor = theme_cache.cursor_style;
1403 }
1404 bool rtl = is_layout_rtl();
1405
1406 // Ensure_selected_visible needs to be checked before we draw the list.
1407 if (ensure_selected_visible && current >= 0 && current < items.size()) {
1408 Rect2 r = items[current].rect_cache;
1409 int from_v = scroll_bar_v->get_value();
1410 int to_v = from_v + scroll_bar_v->get_page();
1411
1412 if (r.position.y < from_v) {
1413 scroll_bar_v->set_value(r.position.y);
1414 } else if (r.position.y + r.size.y > to_v) {
1415 scroll_bar_v->set_value(r.position.y + r.size.y - (to_v - from_v));
1416 }
1417 int from_h = scroll_bar_h->get_value();
1418 int to_h = from_h + scroll_bar_h->get_page();
1419
1420 if (r.position.x < from_h) {
1421 scroll_bar_h->set_value(r.position.x);
1422 } else if (r.position.x + r.size.x > to_h) {
1423 scroll_bar_h->set_value(r.position.x + r.size.x - (to_h - from_h));
1424 }
1425 }
1426
1427 ensure_selected_visible = false;
1428
1429 Vector2 base_ofs = theme_cache.panel_style->get_offset();
1430 base_ofs.y -= int(scroll_bar_v->get_value());
1431 if (rtl) {
1432 base_ofs.x += int(scroll_bar_h->get_value());
1433 } else {
1434 base_ofs.x -= int(scroll_bar_h->get_value());
1435 }
1436
1437 // Define a visible frame to check against and optimize drawing.
1438 if (!wraparound_items) {
1439 size.width += (scroll_bar_h->get_max() - scroll_bar_h->get_page());
1440 }
1441 const Rect2 clip(-base_ofs, size);
1442
1443 // Do a binary search to find the first separator that is below clip_position.y.
1444 int64_t first_visible_separator = separators.span().bisect(clip.position.y, true);
1445
1446 // If not in thumbnails mode, draw visible separators.
1447 if (icon_mode != ICON_MODE_TOP) {
1448 for (int i = first_visible_separator; i < separators.size(); i++) {
1449 if (separators[i] > clip.position.y + clip.size.y) {
1450 break; // done
1451 }
1452
1453 const int y = base_ofs.y + separators[i];
1454 if (rtl && scroll_bar_v->is_visible()) {
1455 draw_line(Vector2(theme_cache.panel_style->get_margin(SIDE_LEFT) + scroll_bar_v_min.width, y), Vector2(width + theme_cache.panel_style->get_margin(SIDE_LEFT) + scroll_bar_v_min.width, y), theme_cache.guide_color);
1456 } else {
1457 draw_line(Vector2(theme_cache.panel_style->get_margin(SIDE_LEFT), y), Vector2(width + theme_cache.panel_style->get_margin(SIDE_LEFT), y), theme_cache.guide_color);
1458 }
1459 }
1460 }
1461
1462 // Do a binary search to find the first item whose rect reaches below clip.position.y.
1463 int first_item_visible;
1464 {
1465 int lo = 0;
1466 int hi = items.size();
1467 while (lo < hi) {
1468 const int mid = (lo + hi) / 2;
1469 const Rect2 &rcache = items[mid].rect_cache;
1470 if (rcache.position.y + rcache.size.y < clip.position.y) {
1471 lo = mid + 1;
1472 } else {
1473 hi = mid;
1474 }
1475 }
1476
1477 // We might end up with an item in columns 2, 3, etc, but we need the one from the first column.
1478 // We can also end up in a state where lo reached hi, and so no items can be rendered; we skip that.
1479 while (lo < hi && lo > 0 && items[lo].column > 0) {
1480 lo -= 1;
1481 }
1482
1483 first_item_visible = lo;
1484 }
1485
1486 Rect2 cursor_rcache; // Place to save the position of the cursor and draw it after everything else.
1487
1488 // Draw visible items.
1489 for (int i = first_item_visible; i < items.size(); i++) {
1490 Rect2 rcache = items[i].rect_cache;
1491
1492 if (rcache.position.y > clip.position.y + clip.size.y) {
1493 break; // done
1494 }
1495
1496 if (!clip.intersects(rcache)) {
1497 continue;
1498 }
1499
1500 if (current_columns == 1) {
1501 rcache.size.width = width - rcache.position.x;
1502 }
1503
1504 bool should_draw_selected_bg = items[i].selected && hovered != i;
1505 bool should_draw_hovered_selected_bg = items[i].selected && hovered == i;
1506 bool should_draw_hovered_bg = hovered == i && !items[i].selected;
1507 bool should_draw_custom_bg = items[i].custom_bg.a > 0.001;
1508
1509 if (should_draw_selected_bg || should_draw_hovered_selected_bg || should_draw_hovered_bg || should_draw_custom_bg) {
1510 Rect2 r = rcache;
1511 r.position += base_ofs;
1512
1513 if (rtl) {
1514 r.position.x = size.width - r.position.x - r.size.x + theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT);
1515 }
1516
1517 if (should_draw_selected_bg) {
1518 draw_style_box(sbsel, r);
1519 }
1520 if (should_draw_hovered_selected_bg) {
1521 if (has_focus(true)) {
1522 draw_style_box(theme_cache.hovered_selected_focus_style, r);
1523 } else {
1524 draw_style_box(theme_cache.hovered_selected_style, r);
1525 }
1526 }
1527 if (should_draw_hovered_bg) {
1528 draw_style_box(theme_cache.hovered_style, r);
1529 }
1530 if (should_draw_custom_bg) {
1531 draw_rect(r, items[i].custom_bg);
1532 }
1533 }
1534
1535 Vector2 text_ofs;
1536 Size2 icon_size;
1537 if (items[i].icon.is_valid()) {
1538 if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
1539 icon_size = fixed_icon_size * icon_scale;
1540 } else {
1541 icon_size = items[i].get_icon_size() * icon_scale;
1542 }
1543
1544 Point2 pos = items[i].rect_cache.position + base_ofs;
1545
1546 if (icon_mode == ICON_MODE_TOP) {
1547 pos.y += MAX(theme_cache.v_separation, 0) / 2;
1548 } else {
1549 pos.x += MAX(theme_cache.h_separation, 0) / 2;
1550 }
1551
1552 if (icon_mode == ICON_MODE_TOP) {
1553 pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2);
1554 text_ofs.y = icon_size.height + theme_cache.icon_margin;
1555 } else {
1556 pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2);
1557 text_ofs.x = icon_size.width + theme_cache.icon_margin;
1558 }
1559
1560 Rect2 draw_rect = Rect2(pos, icon_size);
1561
1562 if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
1563 Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size);
1564 draw_rect.position += adj.position;
1565 draw_rect.size = adj.size;
1566 }
1567
1568 Color icon_modulate = items[i].icon_modulate;
1569 if (items[i].disabled) {
1570 icon_modulate.a *= 0.5;
1571 }
1572
1573 // If the icon is transposed, we have to switch the size so that it is drawn correctly
1574 if (items[i].icon_transposed) {
1575 Size2 size_tmp = draw_rect.size;
1576 draw_rect.size.x = size_tmp.y;
1577 draw_rect.size.y = size_tmp.x;
1578 }
1579
1580 Rect2 region = (items[i].icon_region.size.x == 0 || items[i].icon_region.size.y == 0) ? Rect2(Vector2(), items[i].icon->get_size()) : Rect2(items[i].icon_region);
1581
1582 if (rtl) {
1583 draw_rect.position.x = size.width - draw_rect.position.x - draw_rect.size.x;
1584 }
1585 draw_texture_rect_region(items[i].icon, draw_rect, region, icon_modulate, items[i].icon_transposed);
1586 }
1587
1588 if (items[i].tag_icon.is_valid()) {
1589 Size2 tag_icon_size;
1590 if (fixed_tag_icon_size.x > 0 && fixed_tag_icon_size.y > 0) {
1591 tag_icon_size = fixed_tag_icon_size;
1592 } else {
1593 tag_icon_size = items[i].tag_icon->get_size();
1594 }
1595
1596 Point2 draw_pos = items[i].rect_cache.position + base_ofs;
1597 draw_pos.x += MAX(theme_cache.h_separation, 0) / 2;
1598 draw_pos.y += MAX(theme_cache.v_separation, 0) / 2;
1599 if (rtl) {
1600 draw_pos.x = size.width - draw_pos.x - tag_icon_size.x;
1601 }
1602
1603 draw_texture_rect(items[i].tag_icon, Rect2(draw_pos, tag_icon_size));
1604 }
1605
1606 if (!items[i].text.is_empty()) {
1607 Color txt_modulate;
1608 if (items[i].selected && hovered == i) {
1609 txt_modulate = theme_cache.font_hovered_selected_color;
1610 } else if (items[i].selected) {
1611 txt_modulate = theme_cache.font_selected_color;
1612 } else if (hovered == i) {
1613 txt_modulate = theme_cache.font_hovered_color;
1614 } else if (items[i].custom_fg != Color()) {
1615 txt_modulate = items[i].custom_fg;
1616 } else {
1617 txt_modulate = theme_cache.font_color;
1618 }
1619
1620 if (items[i].disabled) {
1621 txt_modulate.a *= 0.5;
1622 }
1623
1624 if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
1625 text_ofs.y += MAX(theme_cache.v_separation, 0) / 2;
1626 text_ofs.x += MAX(theme_cache.h_separation, 0) / 2;
1627
1628 items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_CENTER);
1629
1630 float text_w = items[i].rect_cache.size.width - text_ofs.x * 2;
1631 if (wraparound_items && text_w + text_ofs.x > width) {
1632 text_w = width - text_ofs.x;
1633 }
1634 items.write[i].text_buf->set_width(text_w);
1635
1636 text_ofs += base_ofs;
1637 text_ofs += items[i].rect_cache.position;
1638
1639 if (rtl) {
1640 text_ofs.x = size.width - text_ofs.x - text_w;
1641 }
1642
1643 if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
1644 items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, theme_cache.font_outline_size, theme_cache.font_outline_color);
1645 }
1646
1647 items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
1648 } else {
1649 text_ofs.y += (items[i].rect_cache.size.height - items[i].text_buf->get_size().y) / 2;
1650 text_ofs.x += MAX(theme_cache.h_separation, 0) / 2;
1651
1652 real_t text_width_ofs = text_ofs.x;
1653
1654 text_ofs += base_ofs;
1655 text_ofs += items[i].rect_cache.position;
1656
1657 float text_w = items[i].rect_cache.size.width - text_width_ofs;
1658 if (wraparound_items && items[i].rect_cache.size.width > width) {
1659 text_w -= items[i].rect_cache.size.width - width;
1660 }
1661 items.write[i].text_buf->set_width(text_w);
1662
1663 if (rtl) {
1664 text_ofs.x = size.width - items[i].rect_cache.size.width + icon_size.x - text_ofs.x + MAX(theme_cache.h_separation, 0);
1665 if (wraparound_items) {
1666 text_ofs.x += MAX(items[i].rect_cache.size.width - width, 0);
1667 }
1668 items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
1669 } else {
1670 items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_LEFT);
1671 }
1672
1673 if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
1674 items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, theme_cache.font_outline_size, theme_cache.font_outline_color);
1675 }
1676
1677 if (fixed_column_width > 0) {
1678 if (items[i].rect_cache.size.width - icon_size.x - MAX(theme_cache.h_separation, 0) > 0) {
1679 items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
1680 }
1681 } else {
1682 if (wraparound_items) {
1683 if (width - icon_size.x - MAX(theme_cache.h_separation, 0) - int(scroll_bar_h->get_value()) > 0) {
1684 items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
1685 }
1686 } else {
1687 items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
1688 }
1689 }
1690 }
1691 }
1692
1693 if (i == current && (select_mode == SELECT_MULTI || select_mode == SELECT_TOGGLE)) {
1694 cursor_rcache = rcache;
1695 }
1696 }
1697
1698 if (cursor_rcache.size != Size2()) { // Draw cursor last, so border isn't cut off.
1699 cursor_rcache.position += base_ofs;
1700
1701 if (rtl) {
1702 cursor_rcache.position.x = size.width - cursor_rcache.position.x - cursor_rcache.size.x;
1703 }
1704
1705 draw_style_box(cursor, cursor_rcache);
1706 }
1707
1708 if (scroll_hint_mode != SCROLL_HINT_MODE_DISABLED) {
1709 Size2 control_size = get_size();
1710 float v_scroll_value = scroll_bar_v->get_value();
1711 bool v_scroll_below_max = v_scroll_value < (scroll_bar_v->get_max() - scroll_bar_v->get_page() - 1);
1712 if (v_scroll_value > 1 || v_scroll_below_max) {
1713 int hint_height = theme_cache.scroll_hint->get_height();
1714 if ((scroll_hint_mode == SCROLL_HINT_MODE_BOTH || scroll_hint_mode == SCROLL_HINT_MODE_TOP) && v_scroll_value > 1) {
1715 draw_texture_rect(theme_cache.scroll_hint, Rect2(Point2(), Size2(control_size.width, hint_height)), tile_scroll_hint, theme_cache.scroll_hint_color);
1716 }
1717 if ((scroll_hint_mode == SCROLL_HINT_MODE_BOTH || scroll_hint_mode == SCROLL_HINT_MODE_BOTTOM) && v_scroll_below_max) {
1718 draw_texture_rect(theme_cache.scroll_hint, Rect2(Point2(0, control_size.height - hint_height), Size2(control_size.width, -hint_height)), tile_scroll_hint, theme_cache.scroll_hint_color);
1719 }
1720 }
1721 }
1722
1723 if (has_focus(true)) {
1724 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true);
1725 size.x -= (scroll_bar_h->get_max() - scroll_bar_h->get_page());
1726 draw_style_box(theme_cache.focus_style, Rect2(Point2(), size));
1727 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false);
1728 }
1729 } break;
1730 }
1731 }
1732
1733 void ItemList::force_update_list_size() {
1734 if (!shape_changed) {
1735 return;
1736 }
1737
1738 int scroll_bar_v_minwidth = scroll_bar_v->get_minimum_size().x;
1739 Size2 size = get_size();
1740 float max_column_width = 0.0;
1741
1742 //1- compute item minimum sizes
1743 for (int i = 0; i < items.size(); i++) {
1744 Size2 minsize;
1745 if (items[i].icon.is_valid()) {
1746 if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
1747 minsize = fixed_icon_size * icon_scale;
1748 } else {
1749 minsize = items[i].get_icon_size() * icon_scale;
1750 }
1751
1752 if (!items[i].text.is_empty()) {
1753 if (icon_mode == ICON_MODE_TOP) {
1754 minsize.y += theme_cache.icon_margin;
1755 } else {
1756 minsize.x += theme_cache.icon_margin;
1757 }
1758 }
1759 }
1760
1761 if (!items[i].text.is_empty()) {
1762 int max_width = -1;
1763 if (fixed_column_width) {
1764 max_width = fixed_column_width;
1765 }
1766 items.write[i].text_buf->set_width(max_width);
1767 Size2 s = items[i].text_buf->get_size();
1768
1769 if (icon_mode == ICON_MODE_TOP) {
1770 minsize.x = MAX(minsize.x, s.width);
1771 if (max_text_lines > 0) {
1772 minsize.y += s.height + theme_cache.line_separation * max_text_lines;
1773 } else {
1774 minsize.y += s.height;
1775 }
1776
1777 } else {
1778 minsize.y = MAX(minsize.y, s.height);
1779 minsize.x += s.width;
1780 }
1781 }
1782
1783 if (fixed_column_width > 0) {
1784 minsize.x = fixed_column_width;
1785 }
1786 max_column_width = MAX(max_column_width, minsize.x);
1787
1788 // Elements need to adapt to the selected size.
1789 minsize.y += MAX(theme_cache.v_separation, 0);
1790 minsize.x += MAX(theme_cache.h_separation, 0);
1791
1792 items.write[i].rect_cache.size = minsize;
1793 items.write[i].min_rect_cache.size = minsize;
1794
1795 items.write[i].accessibility_item_dirty = true;
1796 }
1797
1798 int fit_size = size.x - theme_cache.panel_style->get_minimum_size().width;
1799 if (!wraparound_items) {
1800 fit_size += (scroll_bar_h->get_max() - scroll_bar_h->get_page());
1801 }
1802
1803 //2-attempt best fit
1804 current_columns = 0x7FFFFFFF;
1805 if (max_columns > 0) {
1806 current_columns = max_columns;
1807 }
1808
1809 // Repeat until all items fit.
1810 while (true) {
1811 bool all_fit = true;
1812 Vector2 ofs;
1813 int col = 0;
1814 int max_w = 0;
1815 int max_h = 0;
1816
1817 separators.clear();
1818
1819 for (int i = 0; i < items.size(); i++) {
1820 if (current_columns > 1 && items[i].rect_cache.size.width + ofs.x > fit_size && !auto_width && wraparound_items) {
1821 // Went past.
1822 current_columns = MAX(col, 1);
1823 all_fit = false;
1824 break;
1825 }
1826
1827 if (same_column_width) {
1828 items.write[i].rect_cache.size.x = max_column_width + MAX(theme_cache.h_separation, 0);
1829 }
1830 items.write[i].rect_cache.position = ofs;
1831
1832 max_h = MAX(max_h, items[i].rect_cache.size.y);
1833 ofs.x += items[i].rect_cache.size.x;
1834 max_w = MAX(max_w, ofs.x);
1835
1836 items.write[i].column = col;
1837 col++;
1838 if (col == current_columns) {
1839 if (i < items.size() - 1) {
1840 separators.push_back(ofs.y + max_h);
1841 }
1842
1843 for (int j = i; j >= 0 && col > 0; j--, col--) {
1844 items.write[j].rect_cache.size.y = max_h;
1845 }
1846
1847 ofs.x = 0;
1848 ofs.y += max_h;
1849 col = 0;
1850 max_h = 0;
1851 }
1852 }
1853
1854 float scroll_bar_v_page = MAX(0, size.height - theme_cache.panel_style->get_minimum_size().height);
1855 float scroll_bar_v_max = MAX(scroll_bar_v_page, ofs.y + max_h);
1856 float scroll_bar_h_page = MAX(0, size.width - theme_cache.panel_style->get_minimum_size().width);
1857 float scroll_bar_h_max = 0;
1858 if (!wraparound_items) {
1859 scroll_bar_h_max = MAX(scroll_bar_h_page, max_w);
1860 }
1861
1862 if (scroll_bar_v_page >= scroll_bar_v_max || is_layout_rtl()) {
1863 fit_size -= scroll_bar_v_minwidth;
1864 }
1865
1866 if (all_fit) {
1867 for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) {
1868 items.write[j].rect_cache.size.y = max_h;
1869 }
1870
1871 if (auto_height) {
1872 auto_height_value = ofs.y + max_h + theme_cache.panel_style->get_minimum_size().height;
1873 }
1874 if (auto_width) {
1875 auto_width_value = max_w + theme_cache.panel_style->get_minimum_size().width;
1876 }
1877 scroll_bar_v->set_max(scroll_bar_v_max);
1878 scroll_bar_v->set_page(scroll_bar_v_page);
1879 if (scroll_bar_v_max <= scroll_bar_v_page) {
1880 scroll_bar_v->set_value(0);
1881 scroll_bar_v->hide();
1882 } else {
1883 auto_width_value += scroll_bar_v_minwidth;
1884 scroll_bar_v->show();
1885
1886 if (do_autoscroll_to_bottom) {
1887 scroll_bar_v->set_value(scroll_bar_v_max);
1888 }
1889 }
1890
1891 if (is_layout_rtl() && !wraparound_items) {
1892 scroll_bar_h->set_max(scroll_bar_h_page);
1893 scroll_bar_h->set_min(-(scroll_bar_h_max - scroll_bar_h_page));
1894 } else {
1895 scroll_bar_h->set_max(scroll_bar_h_max);
1896 scroll_bar_h->set_min(0);
1897 }
1898 scroll_bar_h->set_page(scroll_bar_h_page);
1899 if (scroll_bar_h_max <= scroll_bar_h_page) {
1900 scroll_bar_h->set_value(0);
1901 scroll_bar_h->hide();
1902 } else {
1903 auto_height_value += scroll_bar_h->get_minimum_size().y;
1904 scroll_bar_h->show();
1905 }
1906 break;
1907 }
1908 }
1909
1910 update_minimum_size();
1911 shape_changed = false;
1912 }
1913
1914 void ItemList::_scroll_changed(double) {
1915 queue_redraw();
1916 }
1917
1918 void ItemList::_mouse_exited() {
1919 if (hovered > -1) {
1920 prev_hovered = hovered;
1921 hovered = -1;
1922 queue_accessibility_update();
1923 queue_redraw();
1924 }
1925 }
1926
1927 void ItemList::_shift_range_select(int p_from, int p_to) {
1928 ERR_FAIL_INDEX(p_from, items.size());
1929 ERR_FAIL_INDEX(p_to, items.size());
1930
1931 if (shift_anchor == -1) {
1932 shift_anchor = p_from;
1933 }
1934
1935 for (int i = 0; i < items.size(); i++) {
1936 if (i >= MIN(shift_anchor, p_to) && i <= MAX(shift_anchor, p_to)) {
1937 if (!is_selected(i)) {
1938 select(i, false);
1939 emit_signal(SNAME("multi_selected"), i, true);
1940 }
1941 } else if (is_selected(i)) {
1942 deselect(i);
1943 emit_signal(SNAME("multi_selected"), i, false);
1944 }
1945 }
1946
1947 current = p_to;
1948 queue_redraw();
1949 ensure_current_is_visible();
1950 }
1951
1952 String ItemList::_atr(int p_idx, const String &p_text) const {
1953 ERR_FAIL_INDEX_V(p_idx, items.size(), atr(p_text));
1954 switch (items[p_idx].auto_translate_mode) {
1955 case AUTO_TRANSLATE_MODE_INHERIT: {
1956 return atr(p_text);
1957 } break;
1958 case AUTO_TRANSLATE_MODE_ALWAYS: {
1959 return tr(p_text);
1960 } break;
1961 case AUTO_TRANSLATE_MODE_DISABLED: {
1962 return p_text;
1963 } break;
1964 }
1965
1966 ERR_FAIL_V_MSG(atr(p_text), "Unexpected auto translate mode: " + itos(items[p_idx].auto_translate_mode));
1967 }
1968
1969 int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const {
1970 Vector2 pos = p_pos;
1971 pos -= theme_cache.panel_style->get_offset();
1972 pos.y += scroll_bar_v->get_value();
1973 pos.x += scroll_bar_h->get_value();
1974
1975 if (is_layout_rtl()) {
1976 pos.x = get_size().width - pos.x - scroll_bar_h->get_value() - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT);
1977 }
1978
1979 int closest = -1;
1980 int closest_dist = 0x7FFFFFFF;
1981
1982 for (int i = 0; i < items.size(); i++) {
1983 Rect2 rc = items[i].rect_cache;
1984
1985 if (i % current_columns == current_columns - 1) { // Make sure you can still select the last item when clicking past the column.
1986 if (is_layout_rtl()) {
1987 rc.size.width = get_size().width - scroll_bar_h->get_value() + rc.position.x;
1988 } else {
1989 rc.size.width = get_size().width + scroll_bar_h->get_value() - rc.position.x;
1990 }
1991 }
1992
1993 if (rc.size.x < 0) {
1994 continue; // Skip negative item sizes, because they are off screen.
1995 }
1996
1997 if (rc.has_point(pos)) {
1998 closest = i;
1999 break;
2000 }
2001
2002 float dist = rc.distance_to(pos);
2003 if (!p_exact && dist < closest_dist) {
2004 closest = i;
2005 closest_dist = dist;
2006 }
2007 }
2008
2009 return closest;
2010 }
2011
2012 bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const {
2013 if (items.is_empty()) {
2014 return true;
2015 }
2016
2017 Vector2 pos = p_pos;
2018 pos -= theme_cache.panel_style->get_offset();
2019 pos.y += scroll_bar_v->get_value();
2020
2021 if (is_layout_rtl()) {
2022 pos.x = get_size().width - pos.x;
2023 }
2024
2025 Rect2 endrect = items[items.size() - 1].rect_cache;
2026 return (pos.y > endrect.position.y + endrect.size.y);
2027 }
2028
2029 String ItemList::get_tooltip(const Point2 &p_pos) const {
2030 int closest = get_item_at_position(p_pos, true);
2031
2032 if (closest != -1) {
2033 if (!items[closest].tooltip_enabled) {
2034 return "";
2035 }
2036 if (!items[closest].tooltip.is_empty()) {
2037 return items[closest].tooltip;
2038 }
2039 if (!items[closest].text.is_empty()) {
2040 return items[closest].text;
2041 }
2042 }
2043
2044 return Control::get_tooltip(p_pos);
2045 }
2046
2047 void ItemList::sort_items_by_text() {
2048 items.sort();
2049 queue_accessibility_update();
2050 queue_redraw();
2051 shape_changed = true;
2052
2053 if (select_mode == SELECT_SINGLE) {
2054 for (int i = 0; i < items.size(); i++) {
2055 if (items[i].selected) {
2056 select(i);
2057 return;
2058 }
2059 }
2060 }
2061 }
2062
2063 int ItemList::find_metadata(const Variant &p_metadata) const {
2064 for (int i = 0; i < items.size(); i++) {
2065 if (items[i].metadata == p_metadata) {
2066 return i;
2067 }
2068 }
2069
2070 return -1;
2071 }
2072
2073 void ItemList::set_allow_rmb_select(bool p_allow) {
2074 allow_rmb_select = p_allow;
2075 }
2076
2077 bool ItemList::get_allow_rmb_select() const {
2078 return allow_rmb_select;
2079 }
2080
2081 void ItemList::set_allow_reselect(bool p_allow) {
2082 allow_reselect = p_allow;
2083 }
2084
2085 bool ItemList::get_allow_reselect() const {
2086 return allow_reselect;
2087 }
2088
2089 void ItemList::set_allow_search(bool p_allow) {
2090 allow_search = p_allow;
2091 }
2092
2093 bool ItemList::get_allow_search() const {
2094 return allow_search;
2095 }
2096
2097 void ItemList::set_icon_scale(real_t p_scale) {
2098 ERR_FAIL_COND(!Math::is_finite(p_scale));
2099
2100 if (icon_scale == p_scale) {
2101 return;
2102 }
2103
2104 icon_scale = p_scale;
2105 queue_redraw();
2106 shape_changed = true;
2107 }
2108
2109 real_t ItemList::get_icon_scale() const {
2110 return icon_scale;
2111 }
2112
2113 Vector<int> ItemList::get_selected_items() {
2114 Vector<int> selected;
2115 for (int i = 0; i < items.size(); i++) {
2116 if (items[i].selected) {
2117 selected.push_back(i);
2118 if (select_mode == SELECT_SINGLE) {
2119 break;
2120 }
2121 }
2122 }
2123 return selected;
2124 }
2125
2126 bool ItemList::is_anything_selected() {
2127 for (int i = 0; i < items.size(); i++) {
2128 if (items[i].selected) {
2129 return true;
2130 }
2131 }
2132
2133 return false;
2134 }
2135
2136 Size2 ItemList::get_minimum_size() const {
2137 Size2 min_size;
2138 if (auto_width) {
2139 min_size.x = auto_width_value;
2140 }
2141
2142 if (auto_height) {
2143 min_size.y = auto_height_value;
2144 }
2145 return min_size;
2146 }
2147
2148 void ItemList::set_autoscroll_to_bottom(const bool p_enable) {
2149 do_autoscroll_to_bottom = p_enable;
2150 }
2151
2152 void ItemList::set_auto_width(bool p_enable) {
2153 if (auto_width == p_enable) {
2154 return;
2155 }
2156
2157 auto_width = p_enable;
2158 shape_changed = true;
2159 queue_accessibility_update();
2160 queue_redraw();
2161 }
2162
2163 bool ItemList::has_auto_width() const {
2164 return auto_width;
2165 }
2166
2167 void ItemList::set_auto_height(bool p_enable) {
2168 if (auto_height == p_enable) {
2169 return;
2170 }
2171
2172 auto_height = p_enable;
2173 shape_changed = true;
2174 queue_accessibility_update();
2175 queue_redraw();
2176 }
2177
2178 bool ItemList::has_auto_height() const {
2179 return auto_height;
2180 }
2181
2182 void ItemList::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
2183 if (text_overrun_behavior != p_behavior) {
2184 text_overrun_behavior = p_behavior;
2185 for (int i = 0; i < items.size(); i++) {
2186 items.write[i].text_buf->set_text_overrun_behavior(p_behavior);
2187 }
2188 shape_changed = true;
2189 queue_redraw();
2190 }
2191 }
2192
2193 TextServer::OverrunBehavior ItemList::get_text_overrun_behavior() const {
2194 return text_overrun_behavior;
2195 }
2196
2197 void ItemList::set_wraparound_items(bool p_enable) {
2198 if (wraparound_items == p_enable) {
2199 return;
2200 }
2201
2202 wraparound_items = p_enable;
2203 shape_changed = true;
2204 queue_redraw();
2205 }
2206
2207 bool ItemList::has_wraparound_items() const {
2208 return wraparound_items;
2209 }
2210
2211 void ItemList::set_scroll_hint_mode(ScrollHintMode p_mode) {
2212 if (scroll_hint_mode == p_mode) {
2213 return;
2214 }
2215
2216 scroll_hint_mode = p_mode;
2217 queue_redraw();
2218 }
2219
2220 ItemList::ScrollHintMode ItemList::get_scroll_hint_mode() const {
2221 return scroll_hint_mode;
2222 }
2223
2224 void ItemList::set_tile_scroll_hint(bool p_enable) {
2225 if (tile_scroll_hint == p_enable) {
2226 return;
2227 }
2228
2229 tile_scroll_hint = p_enable;
2230 queue_redraw();
2231 }
2232
2233 bool ItemList::is_scroll_hint_tiled() {
2234 return tile_scroll_hint;
2235 }
2236
2237 bool ItemList::_set(const StringName &p_name, const Variant &p_value) {
2238 if (property_helper.property_set_value(p_name, p_value)) {
2239 return true;
2240 }
2241
2242 #ifndef DISABLE_DEPRECATED
2243 // Compatibility.
2244 if (p_name == "items") {
2245 Array arr = p_value;
2246 ERR_FAIL_COND_V(arr.size() % 3, false);
2247 clear();
2248
2249 for (int i = 0; i < arr.size(); i += 3) {
2250 String text = arr[i + 0];
2251 Ref<Texture2D> icon = arr[i + 1];
2252 bool disabled = arr[i + 2];
2253
2254 int idx = get_item_count();
2255 add_item(text, icon);
2256 set_item_disabled(idx, disabled);
2257 }
2258 }
2259 #endif
2260 return false;
2261 }
2262
2263 void ItemList::_bind_methods() {
2264 ClassDB::bind_method(D_METHOD("add_item", "text", "icon", "selectable"), &ItemList::add_item, DEFVAL(Variant()), DEFVAL(true));
2265 ClassDB::bind_method(D_METHOD("add_icon_item", "icon", "selectable"), &ItemList::add_icon_item, DEFVAL(true));
2266
2267 ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &ItemList::set_item_text);
2268 ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &ItemList::get_item_text);
2269
2270 ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &ItemList::set_item_icon);
2271 ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &ItemList::get_item_icon);
2272
2273 ClassDB::bind_method(D_METHOD("set_item_text_direction", "idx", "direction"), &ItemList::set_item_text_direction);
2274 ClassDB::bind_method(D_METHOD("get_item_text_direction", "idx"), &ItemList::get_item_text_direction);
2275
2276 ClassDB::bind_method(D_METHOD("set_item_language", "idx", "language"), &ItemList::set_item_language);
2277 ClassDB::bind_method(D_METHOD("get_item_language", "idx"), &ItemList::get_item_language);
2278
2279 ClassDB::bind_method(D_METHOD("set_item_auto_translate_mode", "idx", "mode"), &ItemList::set_item_auto_translate_mode);
2280 ClassDB::bind_method(D_METHOD("get_item_auto_translate_mode", "idx"), &ItemList::get_item_auto_translate_mode);
2281
2282 ClassDB::bind_method(D_METHOD("set_item_icon_transposed", "idx", "transposed"), &ItemList::set_item_icon_transposed);
2283 ClassDB::bind_method(D_METHOD("is_item_icon_transposed", "idx"), &ItemList::is_item_icon_transposed);
2284
2285 ClassDB::bind_method(D_METHOD("set_item_icon_region", "idx", "rect"), &ItemList::set_item_icon_region);
2286 ClassDB::bind_method(D_METHOD("get_item_icon_region", "idx"), &ItemList::get_item_icon_region);
2287
2288 ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "idx", "modulate"), &ItemList::set_item_icon_modulate);
2289 ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "idx"), &ItemList::get_item_icon_modulate);
2290
2291 ClassDB::bind_method(D_METHOD("set_item_selectable", "idx", "selectable"), &ItemList::set_item_selectable);
2292 ClassDB::bind_method(D_METHOD("is_item_selectable", "idx"), &ItemList::is_item_selectable);
2293
2294 ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &ItemList::set_item_disabled);
2295 ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &ItemList::is_item_disabled);
2296
2297 ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &ItemList::set_item_metadata);
2298 ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &ItemList::get_item_metadata);
2299
2300 ClassDB::bind_method(D_METHOD("set_item_custom_bg_color", "idx", "custom_bg_color"), &ItemList::set_item_custom_bg_color);
2301 ClassDB::bind_method(D_METHOD("get_item_custom_bg_color", "idx"), &ItemList::get_item_custom_bg_color);
2302
2303 ClassDB::bind_method(D_METHOD("set_item_custom_fg_color", "idx", "custom_fg_color"), &ItemList::set_item_custom_fg_color);
2304 ClassDB::bind_method(D_METHOD("get_item_custom_fg_color", "idx"), &ItemList::get_item_custom_fg_color);
2305
2306 ClassDB::bind_method(D_METHOD("get_item_rect", "idx", "expand"), &ItemList::get_item_rect, DEFVAL(true));
2307
2308 ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled", "idx", "enable"), &ItemList::set_item_tooltip_enabled);
2309 ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled", "idx"), &ItemList::is_item_tooltip_enabled);
2310
2311 ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &ItemList::set_item_tooltip);
2312 ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &ItemList::get_item_tooltip);
2313
2314 ClassDB::bind_method(D_METHOD("select", "idx", "single"), &ItemList::select, DEFVAL(true));
2315 ClassDB::bind_method(D_METHOD("deselect", "idx"), &ItemList::deselect);
2316 ClassDB::bind_method(D_METHOD("deselect_all"), &ItemList::deselect_all);
2317
2318 ClassDB::bind_method(D_METHOD("is_selected", "idx"), &ItemList::is_selected);
2319 ClassDB::bind_method(D_METHOD("get_selected_items"), &ItemList::get_selected_items);
2320
2321 ClassDB::bind_method(D_METHOD("move_item", "from_idx", "to_idx"), &ItemList::move_item);
2322
2323 ClassDB::bind_method(D_METHOD("set_item_count", "count"), &ItemList::set_item_count);
2324 ClassDB::bind_method(D_METHOD("get_item_count"), &ItemList::get_item_count);
2325 ClassDB::bind_method(D_METHOD("remove_item", "idx"), &ItemList::remove_item);
2326
2327 ClassDB::bind_method(D_METHOD("clear"), &ItemList::clear);
2328 ClassDB::bind_method(D_METHOD("sort_items_by_text"), &ItemList::sort_items_by_text);
2329
2330 ClassDB::bind_method(D_METHOD("set_fixed_column_width", "width"), &ItemList::set_fixed_column_width);
2331 ClassDB::bind_method(D_METHOD("get_fixed_column_width"), &ItemList::get_fixed_column_width);
2332
2333 ClassDB::bind_method(D_METHOD("set_same_column_width", "enable"), &ItemList::set_same_column_width);
2334 ClassDB::bind_method(D_METHOD("is_same_column_width"), &ItemList::is_same_column_width);
2335
2336 ClassDB::bind_method(D_METHOD("set_max_text_lines", "lines"), &ItemList::set_max_text_lines);
2337 ClassDB::bind_method(D_METHOD("get_max_text_lines"), &ItemList::get_max_text_lines);
2338
2339 ClassDB::bind_method(D_METHOD("set_max_columns", "amount"), &ItemList::set_max_columns);
2340 ClassDB::bind_method(D_METHOD("get_max_columns"), &ItemList::get_max_columns);
2341
2342 ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &ItemList::set_select_mode);
2343 ClassDB::bind_method(D_METHOD("get_select_mode"), &ItemList::get_select_mode);
2344
2345 ClassDB::bind_method(D_METHOD("set_icon_mode", "mode"), &ItemList::set_icon_mode);
2346 ClassDB::bind_method(D_METHOD("get_icon_mode"), &ItemList::get_icon_mode);
2347
2348 ClassDB::bind_method(D_METHOD("set_fixed_icon_size", "size"), &ItemList::set_fixed_icon_size);
2349 ClassDB::bind_method(D_METHOD("get_fixed_icon_size"), &ItemList::get_fixed_icon_size);
2350
2351 ClassDB::bind_method(D_METHOD("set_icon_scale", "scale"), &ItemList::set_icon_scale);
2352 ClassDB::bind_method(D_METHOD("get_icon_scale"), &ItemList::get_icon_scale);
2353
2354 ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &ItemList::set_allow_rmb_select);
2355 ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &ItemList::get_allow_rmb_select);
2356
2357 ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &ItemList::set_allow_reselect);
2358 ClassDB::bind_method(D_METHOD("get_allow_reselect"), &ItemList::get_allow_reselect);
2359
2360 ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &ItemList::set_allow_search);
2361 ClassDB::bind_method(D_METHOD("get_allow_search"), &ItemList::get_allow_search);
2362
2363 ClassDB::bind_method(D_METHOD("set_auto_width", "enable"), &ItemList::set_auto_width);
2364 ClassDB::bind_method(D_METHOD("has_auto_width"), &ItemList::has_auto_width);
2365
2366 ClassDB::bind_method(D_METHOD("set_auto_height", "enable"), &ItemList::set_auto_height);
2367 ClassDB::bind_method(D_METHOD("has_auto_height"), &ItemList::has_auto_height);
2368
2369 ClassDB::bind_method(D_METHOD("is_anything_selected"), &ItemList::is_anything_selected);
2370
2371 ClassDB::bind_method(D_METHOD("get_item_at_position", "position", "exact"), &ItemList::get_item_at_position, DEFVAL(false));
2372
2373 ClassDB::bind_method(D_METHOD("ensure_current_is_visible"), &ItemList::ensure_current_is_visible);
2374
2375 ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ItemList::get_v_scroll_bar);
2376 ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &ItemList::get_h_scroll_bar);
2377
2378 ClassDB::bind_method(D_METHOD("set_scroll_hint_mode", "scroll_hint_mode"), &ItemList::set_scroll_hint_mode);
2379 ClassDB::bind_method(D_METHOD("get_scroll_hint_mode"), &ItemList::get_scroll_hint_mode);
2380
2381 ClassDB::bind_method(D_METHOD("set_tile_scroll_hint", "tile_scroll_hint"), &ItemList::set_tile_scroll_hint);
2382 ClassDB::bind_method(D_METHOD("is_scroll_hint_tiled"), &ItemList::is_scroll_hint_tiled);
2383
2384 ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &ItemList::set_text_overrun_behavior);
2385 ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &ItemList::get_text_overrun_behavior);
2386
2387 ClassDB::bind_method(D_METHOD("set_wraparound_items", "enable"), &ItemList::set_wraparound_items);
2388 ClassDB::bind_method(D_METHOD("has_wraparound_items"), &ItemList::has_wraparound_items);
2389
2390 ClassDB::bind_method(D_METHOD("force_update_list_size"), &ItemList::force_update_list_size);
2391
2392 ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi,Toggle"), "set_select_mode", "get_select_mode");
2393 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
2394 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
2395 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
2396 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_text_lines", PROPERTY_HINT_RANGE, "1,10,1,or_greater"), "set_max_text_lines", "get_max_text_lines");
2397 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_width"), "set_auto_width", "has_auto_width");
2398 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_height"), "set_auto_height", "has_auto_height");
2399 ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
2400 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "wraparound_items"), "set_wraparound_items", "has_wraparound_items");
2401 ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_hint_mode", PROPERTY_HINT_ENUM, "Disabled,Both,Top,Bottom"), "set_scroll_hint_mode", "get_scroll_hint_mode");
2402 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tile_scroll_hint"), "set_tile_scroll_hint", "is_scroll_hint_tiled");
2403 ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_");
2404 ADD_GROUP("Columns", "");
2405 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_columns", PROPERTY_HINT_RANGE, "0,10,1,or_greater"), "set_max_columns", "get_max_columns");
2406 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "same_column_width"), "set_same_column_width", "is_same_column_width");
2407 ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_column_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater,suffix:px"), "set_fixed_column_width", "get_fixed_column_width");
2408 ADD_GROUP("Icon", "");
2409 ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_mode", PROPERTY_HINT_ENUM, "Top,Left"), "set_icon_mode", "get_icon_mode");
2410 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "icon_scale"), "set_icon_scale", "get_icon_scale");
2411 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "fixed_icon_size", PROPERTY_HINT_NONE, "suffix:px"), "set_fixed_icon_size", "get_fixed_icon_size");
2412
2413 BIND_ENUM_CONSTANT(ICON_MODE_TOP);
2414 BIND_ENUM_CONSTANT(ICON_MODE_LEFT);
2415
2416 BIND_ENUM_CONSTANT(SELECT_SINGLE);
2417 BIND_ENUM_CONSTANT(SELECT_MULTI);
2418 BIND_ENUM_CONSTANT(SELECT_TOGGLE);
2419
2420 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_DISABLED);
2421 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_BOTH);
2422 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_TOP);
2423 BIND_ENUM_CONSTANT(SCROLL_HINT_MODE_BOTTOM);
2424
2425 ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
2426 ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
2427 ADD_SIGNAL(MethodInfo("item_clicked", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
2428 ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "selected")));
2429 ADD_SIGNAL(MethodInfo("item_activated", PropertyInfo(Variant::INT, "index")));
2430
2431 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, h_separation);
2432 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, v_separation);
2433
2434 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, panel_style, "panel");
2435 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, focus_style, "focus");
2436
2437 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, ItemList, font);
2438 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, ItemList, font_size);
2439 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_color);
2440 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_hovered_color);
2441 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_hovered_selected_color);
2442 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_selected_color);
2443 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ItemList, font_outline_size, "outline_size");
2444 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_outline_color);
2445 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ItemList, scroll_hint);
2446 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, scroll_hint_color);
2447
2448 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, line_separation);
2449 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, icon_margin);
2450 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, hovered_style, "hovered");
2451 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, hovered_selected_style, "hovered_selected");
2452 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, hovered_selected_focus_style, "hovered_selected_focus");
2453 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, selected_style, "selected");
2454 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, selected_focus_style, "selected_focus");
2455 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_style, "cursor_unfocused");
2456 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_focus_style, "cursor");
2457 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, guide_color);
2458
2459 Item defaults(true);
2460
2461 base_property_helper.set_prefix("item_");
2462 base_property_helper.set_array_length_getter(&ItemList::get_item_count);
2463 base_property_helper.register_property(PropertyInfo(Variant::STRING, "text"), defaults.text, &ItemList::set_item_text, &ItemList::get_item_text);
2464 base_property_helper.register_property(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), defaults.icon, &ItemList::set_item_icon, &ItemList::get_item_icon);
2465 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "selectable"), defaults.selectable, &ItemList::set_item_selectable, &ItemList::is_item_selectable);
2466 base_property_helper.register_property(PropertyInfo(Variant::BOOL, "disabled"), defaults.disabled, &ItemList::set_item_disabled, &ItemList::is_item_disabled);
2467 PropertyListHelper::register_base_helper(&base_property_helper);
2468 }
2469
2470 ItemList::ItemList() {
2471 scroll_bar_v = memnew(VScrollBar);
2472 add_child(scroll_bar_v, false, INTERNAL_MODE_FRONT);
2473 scroll_bar_v->connect(SceneStringName(value_changed), callable_mp(this, &ItemList::_scroll_changed));
2474
2475 scroll_bar_h = memnew(HScrollBar);
2476 add_child(scroll_bar_h, false, INTERNAL_MODE_FRONT);
2477 scroll_bar_h->connect(SceneStringName(value_changed), callable_mp(this, &ItemList::_scroll_changed));
2478
2479 connect(SceneStringName(mouse_exited), callable_mp(this, &ItemList::_mouse_exited));
2480
2481 set_focus_mode(FOCUS_ALL);
2482 set_clip_contents(true);
2483
2484 property_helper.setup_for_instance(base_property_helper, this);
2485 }
2486
2487 ItemList::~ItemList() {
2488 }