item_list.cpp 78 KB

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