item_list.cpp 81 KB

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