item_list.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  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 "core/string/translation.h"
  34. #include "scene/theme/theme_db.h"
  35. void ItemList::_shape_text(int p_idx) {
  36. Item &item = items.write[p_idx];
  37. item.text_buf->clear();
  38. if (item.text_direction == Control::TEXT_DIRECTION_INHERITED) {
  39. item.text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  40. } else {
  41. item.text_buf->set_direction((TextServer::Direction)item.text_direction);
  42. }
  43. item.text_buf->add_string(item.xl_text, theme_cache.font, theme_cache.font_size, item.language);
  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_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.xl_text = atr(p_item);
  57. item.selectable = p_selectable;
  58. items.push_back(item);
  59. int item_id = items.size() - 1;
  60. _shape_text(items.size() - 1);
  61. queue_redraw();
  62. shape_changed = true;
  63. notify_property_list_changed();
  64. return item_id;
  65. }
  66. int ItemList::add_icon_item(const Ref<Texture2D> &p_item, bool p_selectable) {
  67. Item item;
  68. item.icon = p_item;
  69. item.selectable = p_selectable;
  70. items.push_back(item);
  71. int item_id = items.size() - 1;
  72. queue_redraw();
  73. shape_changed = true;
  74. notify_property_list_changed();
  75. return item_id;
  76. }
  77. void ItemList::set_item_text(int p_idx, const String &p_text) {
  78. if (p_idx < 0) {
  79. p_idx += get_item_count();
  80. }
  81. ERR_FAIL_INDEX(p_idx, items.size());
  82. if (items[p_idx].text == p_text) {
  83. return;
  84. }
  85. items.write[p_idx].text = p_text;
  86. items.write[p_idx].xl_text = atr(p_text);
  87. _shape_text(p_idx);
  88. queue_redraw();
  89. shape_changed = true;
  90. }
  91. String ItemList::get_item_text(int p_idx) const {
  92. ERR_FAIL_INDEX_V(p_idx, items.size(), String());
  93. return items[p_idx].text;
  94. }
  95. void ItemList::set_item_text_direction(int p_idx, Control::TextDirection p_text_direction) {
  96. if (p_idx < 0) {
  97. p_idx += get_item_count();
  98. }
  99. ERR_FAIL_INDEX(p_idx, items.size());
  100. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  101. if (items[p_idx].text_direction != p_text_direction) {
  102. items.write[p_idx].text_direction = p_text_direction;
  103. _shape_text(p_idx);
  104. queue_redraw();
  105. }
  106. }
  107. Control::TextDirection ItemList::get_item_text_direction(int p_idx) const {
  108. ERR_FAIL_INDEX_V(p_idx, items.size(), TEXT_DIRECTION_INHERITED);
  109. return items[p_idx].text_direction;
  110. }
  111. void ItemList::set_item_language(int p_idx, const String &p_language) {
  112. if (p_idx < 0) {
  113. p_idx += get_item_count();
  114. }
  115. ERR_FAIL_INDEX(p_idx, items.size());
  116. if (items[p_idx].language != p_language) {
  117. items.write[p_idx].language = p_language;
  118. _shape_text(p_idx);
  119. queue_redraw();
  120. }
  121. }
  122. String ItemList::get_item_language(int p_idx) const {
  123. ERR_FAIL_INDEX_V(p_idx, items.size(), "");
  124. return items[p_idx].language;
  125. }
  126. void ItemList::set_item_tooltip_enabled(int p_idx, const bool p_enabled) {
  127. if (p_idx < 0) {
  128. p_idx += get_item_count();
  129. }
  130. ERR_FAIL_INDEX(p_idx, items.size());
  131. items.write[p_idx].tooltip_enabled = p_enabled;
  132. }
  133. bool ItemList::is_item_tooltip_enabled(int p_idx) const {
  134. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  135. return items[p_idx].tooltip_enabled;
  136. }
  137. void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) {
  138. if (p_idx < 0) {
  139. p_idx += get_item_count();
  140. }
  141. ERR_FAIL_INDEX(p_idx, items.size());
  142. if (items[p_idx].tooltip == p_tooltip) {
  143. return;
  144. }
  145. items.write[p_idx].tooltip = p_tooltip;
  146. queue_redraw();
  147. shape_changed = true;
  148. }
  149. String ItemList::get_item_tooltip(int p_idx) const {
  150. ERR_FAIL_INDEX_V(p_idx, items.size(), String());
  151. return items[p_idx].tooltip;
  152. }
  153. void ItemList::set_item_icon(int p_idx, const Ref<Texture2D> &p_icon) {
  154. if (p_idx < 0) {
  155. p_idx += get_item_count();
  156. }
  157. ERR_FAIL_INDEX(p_idx, items.size());
  158. if (items[p_idx].icon == p_icon) {
  159. return;
  160. }
  161. items.write[p_idx].icon = p_icon;
  162. queue_redraw();
  163. shape_changed = true;
  164. }
  165. Ref<Texture2D> ItemList::get_item_icon(int p_idx) const {
  166. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture2D>());
  167. return items[p_idx].icon;
  168. }
  169. void ItemList::set_item_icon_transposed(int p_idx, const bool p_transposed) {
  170. if (p_idx < 0) {
  171. p_idx += get_item_count();
  172. }
  173. ERR_FAIL_INDEX(p_idx, items.size());
  174. if (items[p_idx].icon_transposed == p_transposed) {
  175. return;
  176. }
  177. items.write[p_idx].icon_transposed = p_transposed;
  178. queue_redraw();
  179. shape_changed = true;
  180. }
  181. bool ItemList::is_item_icon_transposed(int p_idx) const {
  182. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  183. return items[p_idx].icon_transposed;
  184. }
  185. void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) {
  186. if (p_idx < 0) {
  187. p_idx += get_item_count();
  188. }
  189. ERR_FAIL_INDEX(p_idx, items.size());
  190. if (items[p_idx].icon_region == p_region) {
  191. return;
  192. }
  193. items.write[p_idx].icon_region = p_region;
  194. queue_redraw();
  195. shape_changed = true;
  196. }
  197. Rect2 ItemList::get_item_icon_region(int p_idx) const {
  198. ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
  199. return items[p_idx].icon_region;
  200. }
  201. void ItemList::set_item_icon_modulate(int p_idx, const Color &p_modulate) {
  202. if (p_idx < 0) {
  203. p_idx += get_item_count();
  204. }
  205. ERR_FAIL_INDEX(p_idx, items.size());
  206. if (items[p_idx].icon_modulate == p_modulate) {
  207. return;
  208. }
  209. items.write[p_idx].icon_modulate = p_modulate;
  210. queue_redraw();
  211. }
  212. Color ItemList::get_item_icon_modulate(int p_idx) const {
  213. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  214. return items[p_idx].icon_modulate;
  215. }
  216. void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) {
  217. if (p_idx < 0) {
  218. p_idx += get_item_count();
  219. }
  220. ERR_FAIL_INDEX(p_idx, items.size());
  221. if (items[p_idx].custom_bg == p_custom_bg_color) {
  222. return;
  223. }
  224. items.write[p_idx].custom_bg = p_custom_bg_color;
  225. queue_redraw();
  226. }
  227. Color ItemList::get_item_custom_bg_color(int p_idx) const {
  228. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  229. return items[p_idx].custom_bg;
  230. }
  231. void ItemList::set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color) {
  232. if (p_idx < 0) {
  233. p_idx += get_item_count();
  234. }
  235. ERR_FAIL_INDEX(p_idx, items.size());
  236. if (items[p_idx].custom_fg == p_custom_fg_color) {
  237. return;
  238. }
  239. items.write[p_idx].custom_fg = p_custom_fg_color;
  240. queue_redraw();
  241. }
  242. Color ItemList::get_item_custom_fg_color(int p_idx) const {
  243. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  244. return items[p_idx].custom_fg;
  245. }
  246. Rect2 ItemList::get_item_rect(int p_idx, bool p_expand) const {
  247. ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
  248. Rect2 ret = items[p_idx].rect_cache;
  249. ret.position += theme_cache.panel_style->get_offset();
  250. if (p_expand && p_idx % current_columns == current_columns - 1) {
  251. ret.size.width = get_size().width - ret.position.x;
  252. }
  253. return ret;
  254. }
  255. void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture2D> &p_tag_icon) {
  256. if (p_idx < 0) {
  257. p_idx += get_item_count();
  258. }
  259. ERR_FAIL_INDEX(p_idx, items.size());
  260. if (items[p_idx].tag_icon == p_tag_icon) {
  261. return;
  262. }
  263. items.write[p_idx].tag_icon = p_tag_icon;
  264. queue_redraw();
  265. shape_changed = true;
  266. }
  267. void ItemList::set_item_selectable(int p_idx, bool p_selectable) {
  268. if (p_idx < 0) {
  269. p_idx += get_item_count();
  270. }
  271. ERR_FAIL_INDEX(p_idx, items.size());
  272. items.write[p_idx].selectable = p_selectable;
  273. }
  274. bool ItemList::is_item_selectable(int p_idx) const {
  275. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  276. return items[p_idx].selectable;
  277. }
  278. void ItemList::set_item_disabled(int p_idx, bool p_disabled) {
  279. if (p_idx < 0) {
  280. p_idx += get_item_count();
  281. }
  282. ERR_FAIL_INDEX(p_idx, items.size());
  283. if (items[p_idx].disabled == p_disabled) {
  284. return;
  285. }
  286. items.write[p_idx].disabled = p_disabled;
  287. queue_redraw();
  288. }
  289. bool ItemList::is_item_disabled(int p_idx) const {
  290. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  291. return items[p_idx].disabled;
  292. }
  293. void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) {
  294. if (p_idx < 0) {
  295. p_idx += get_item_count();
  296. }
  297. ERR_FAIL_INDEX(p_idx, items.size());
  298. if (items[p_idx].metadata == p_metadata) {
  299. return;
  300. }
  301. items.write[p_idx].metadata = p_metadata;
  302. queue_redraw();
  303. shape_changed = true;
  304. }
  305. Variant ItemList::get_item_metadata(int p_idx) const {
  306. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  307. return items[p_idx].metadata;
  308. }
  309. void ItemList::select(int p_idx, bool p_single) {
  310. ERR_FAIL_INDEX(p_idx, items.size());
  311. if (p_single || select_mode == SELECT_SINGLE) {
  312. if (!items[p_idx].selectable || items[p_idx].disabled) {
  313. return;
  314. }
  315. for (int i = 0; i < items.size(); i++) {
  316. items.write[i].selected = p_idx == i;
  317. }
  318. current = p_idx;
  319. ensure_selected_visible = false;
  320. } else {
  321. if (items[p_idx].selectable && !items[p_idx].disabled) {
  322. items.write[p_idx].selected = true;
  323. }
  324. }
  325. queue_redraw();
  326. }
  327. void ItemList::deselect(int p_idx) {
  328. ERR_FAIL_INDEX(p_idx, items.size());
  329. if (select_mode != SELECT_MULTI) {
  330. items.write[p_idx].selected = false;
  331. current = -1;
  332. } else {
  333. items.write[p_idx].selected = false;
  334. }
  335. queue_redraw();
  336. }
  337. void ItemList::deselect_all() {
  338. if (items.size() < 1) {
  339. return;
  340. }
  341. for (int i = 0; i < items.size(); i++) {
  342. items.write[i].selected = false;
  343. }
  344. current = -1;
  345. queue_redraw();
  346. }
  347. bool ItemList::is_selected(int p_idx) const {
  348. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  349. return items[p_idx].selected;
  350. }
  351. void ItemList::set_current(int p_current) {
  352. ERR_FAIL_INDEX(p_current, items.size());
  353. if (current == p_current) {
  354. return;
  355. }
  356. if (select_mode == SELECT_SINGLE) {
  357. select(p_current, true);
  358. } else {
  359. current = p_current;
  360. queue_redraw();
  361. }
  362. }
  363. int ItemList::get_current() const {
  364. return current;
  365. }
  366. void ItemList::move_item(int p_from_idx, int p_to_idx) {
  367. ERR_FAIL_INDEX(p_from_idx, items.size());
  368. ERR_FAIL_INDEX(p_to_idx, items.size());
  369. if (is_anything_selected() && get_selected_items()[0] == p_from_idx) {
  370. current = p_to_idx;
  371. }
  372. Item item = items[p_from_idx];
  373. items.remove_at(p_from_idx);
  374. items.insert(p_to_idx, item);
  375. queue_redraw();
  376. shape_changed = true;
  377. notify_property_list_changed();
  378. }
  379. void ItemList::set_item_count(int p_count) {
  380. ERR_FAIL_COND(p_count < 0);
  381. if (items.size() == p_count) {
  382. return;
  383. }
  384. items.resize(p_count);
  385. queue_redraw();
  386. shape_changed = true;
  387. notify_property_list_changed();
  388. }
  389. int ItemList::get_item_count() const {
  390. return items.size();
  391. }
  392. void ItemList::remove_item(int p_idx) {
  393. ERR_FAIL_INDEX(p_idx, items.size());
  394. items.remove_at(p_idx);
  395. if (current == p_idx) {
  396. current = -1;
  397. }
  398. queue_redraw();
  399. shape_changed = true;
  400. defer_select_single = -1;
  401. notify_property_list_changed();
  402. }
  403. void ItemList::clear() {
  404. items.clear();
  405. current = -1;
  406. ensure_selected_visible = false;
  407. queue_redraw();
  408. shape_changed = true;
  409. defer_select_single = -1;
  410. notify_property_list_changed();
  411. }
  412. void ItemList::set_fixed_column_width(int p_size) {
  413. ERR_FAIL_COND(p_size < 0);
  414. if (fixed_column_width == p_size) {
  415. return;
  416. }
  417. fixed_column_width = p_size;
  418. queue_redraw();
  419. shape_changed = true;
  420. }
  421. int ItemList::get_fixed_column_width() const {
  422. return fixed_column_width;
  423. }
  424. void ItemList::set_same_column_width(bool p_enable) {
  425. if (same_column_width == p_enable) {
  426. return;
  427. }
  428. same_column_width = p_enable;
  429. queue_redraw();
  430. shape_changed = true;
  431. }
  432. bool ItemList::is_same_column_width() const {
  433. return same_column_width;
  434. }
  435. void ItemList::set_max_text_lines(int p_lines) {
  436. ERR_FAIL_COND(p_lines < 1);
  437. if (max_text_lines != p_lines) {
  438. max_text_lines = p_lines;
  439. for (int i = 0; i < items.size(); i++) {
  440. if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
  441. items.write[i].text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_EDGE_SPACES);
  442. items.write[i].text_buf->set_max_lines_visible(p_lines);
  443. } else {
  444. items.write[i].text_buf->set_break_flags(TextServer::BREAK_NONE);
  445. }
  446. }
  447. shape_changed = true;
  448. queue_redraw();
  449. }
  450. }
  451. int ItemList::get_max_text_lines() const {
  452. return max_text_lines;
  453. }
  454. void ItemList::set_max_columns(int p_amount) {
  455. ERR_FAIL_COND(p_amount < 0);
  456. if (max_columns == p_amount) {
  457. return;
  458. }
  459. max_columns = p_amount;
  460. queue_redraw();
  461. shape_changed = true;
  462. }
  463. int ItemList::get_max_columns() const {
  464. return max_columns;
  465. }
  466. void ItemList::set_select_mode(SelectMode p_mode) {
  467. if (select_mode == p_mode) {
  468. return;
  469. }
  470. select_mode = p_mode;
  471. queue_redraw();
  472. }
  473. ItemList::SelectMode ItemList::get_select_mode() const {
  474. return select_mode;
  475. }
  476. void ItemList::set_icon_mode(IconMode p_mode) {
  477. ERR_FAIL_INDEX((int)p_mode, 2);
  478. if (icon_mode != p_mode) {
  479. icon_mode = p_mode;
  480. for (int i = 0; i < items.size(); i++) {
  481. if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
  482. items.write[i].text_buf->set_break_flags(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_TRIM_EDGE_SPACES);
  483. } else {
  484. items.write[i].text_buf->set_break_flags(TextServer::BREAK_NONE);
  485. }
  486. }
  487. shape_changed = true;
  488. queue_redraw();
  489. }
  490. }
  491. ItemList::IconMode ItemList::get_icon_mode() const {
  492. return icon_mode;
  493. }
  494. void ItemList::set_fixed_icon_size(const Size2i &p_size) {
  495. if (fixed_icon_size == p_size) {
  496. return;
  497. }
  498. fixed_icon_size = p_size;
  499. queue_redraw();
  500. shape_changed = true;
  501. }
  502. Size2i ItemList::get_fixed_icon_size() const {
  503. return fixed_icon_size;
  504. }
  505. Size2 ItemList::Item::get_icon_size() const {
  506. if (icon.is_null()) {
  507. return Size2();
  508. }
  509. Size2 size_result = Size2(icon_region.size).abs();
  510. if (icon_region.size.x == 0 || icon_region.size.y == 0) {
  511. size_result = icon->get_size();
  512. }
  513. if (icon_transposed) {
  514. Size2 size_tmp = size_result;
  515. size_result.x = size_tmp.y;
  516. size_result.y = size_tmp.x;
  517. }
  518. return size_result;
  519. }
  520. void ItemList::gui_input(const Ref<InputEvent> &p_event) {
  521. ERR_FAIL_COND(p_event.is_null());
  522. #define CAN_SELECT(i) (items[i].selectable && !items[i].disabled)
  523. #define IS_SAME_ROW(i, row) (i / current_columns == row)
  524. double prev_scroll = scroll_bar->get_value();
  525. Ref<InputEventMouseMotion> mm = p_event;
  526. if (defer_select_single >= 0 && mm.is_valid()) {
  527. defer_select_single = -1;
  528. return;
  529. }
  530. Ref<InputEventMouseButton> mb = p_event;
  531. if (defer_select_single >= 0 && mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
  532. select(defer_select_single, true);
  533. emit_signal(SNAME("multi_selected"), defer_select_single, true);
  534. defer_select_single = -1;
  535. return;
  536. }
  537. if (mm.is_valid()) {
  538. int closest = get_item_at_position(mm->get_position(), true);
  539. if (closest != hovered) {
  540. hovered = closest;
  541. queue_redraw();
  542. }
  543. }
  544. if (mb.is_valid() && mb->is_pressed()) {
  545. search_string = ""; //any mousepress cancels
  546. int closest = get_item_at_position(mb->get_position(), true);
  547. if (closest != -1 && (mb->get_button_index() == MouseButton::LEFT || (allow_rmb_select && mb->get_button_index() == MouseButton::RIGHT))) {
  548. int i = closest;
  549. if (items[i].disabled) {
  550. // Don't emit any signal or do any action with clicked item when disabled.
  551. return;
  552. }
  553. if (select_mode == SELECT_MULTI && items[i].selected && mb->is_command_or_control_pressed()) {
  554. deselect(i);
  555. emit_signal(SNAME("multi_selected"), i, false);
  556. } else if (select_mode == SELECT_MULTI && mb->is_shift_pressed() && current >= 0 && current < items.size() && current != i) {
  557. // Range selection.
  558. int from = current;
  559. int to = i;
  560. if (i < current) {
  561. SWAP(from, to);
  562. }
  563. for (int j = from; j <= to; j++) {
  564. if (!CAN_SELECT(j)) {
  565. // Item is not selectable during a range selection, so skip it.
  566. continue;
  567. }
  568. bool selected = !items[j].selected;
  569. select(j, false);
  570. if (selected) {
  571. emit_signal(SNAME("multi_selected"), j, true);
  572. }
  573. }
  574. emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index());
  575. } else {
  576. if (!mb->is_double_click() &&
  577. !mb->is_command_or_control_pressed() &&
  578. select_mode == SELECT_MULTI &&
  579. items[i].selectable &&
  580. items[i].selected &&
  581. mb->get_button_index() == MouseButton::LEFT) {
  582. defer_select_single = i;
  583. return;
  584. }
  585. if (items[i].selectable && (!items[i].selected || allow_reselect)) {
  586. select(i, select_mode == SELECT_SINGLE || !mb->is_command_or_control_pressed());
  587. if (select_mode == SELECT_SINGLE) {
  588. emit_signal(SNAME("item_selected"), i);
  589. } else {
  590. emit_signal(SNAME("multi_selected"), i, true);
  591. }
  592. }
  593. emit_signal(SNAME("item_clicked"), i, get_local_mouse_position(), mb->get_button_index());
  594. if (mb->get_button_index() == MouseButton::LEFT && mb->is_double_click()) {
  595. emit_signal(SNAME("item_activated"), i);
  596. }
  597. }
  598. return;
  599. } else if (closest != -1) {
  600. if (!items[closest].disabled) {
  601. emit_signal(SNAME("item_clicked"), closest, get_local_mouse_position(), mb->get_button_index());
  602. }
  603. } else {
  604. // Since closest is null, more likely we clicked on empty space, so send signal to interested controls. Allows, for example, implement items deselecting.
  605. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
  606. }
  607. }
  608. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_UP && mb->is_pressed()) {
  609. scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() * mb->get_factor() / 8);
  610. }
  611. if (mb.is_valid() && mb->get_button_index() == MouseButton::WHEEL_DOWN && mb->is_pressed()) {
  612. scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * mb->get_factor() / 8);
  613. }
  614. if (p_event->is_pressed() && items.size() > 0) {
  615. if (p_event->is_action("ui_up", true)) {
  616. if (!search_string.is_empty()) {
  617. uint64_t now = OS::get_singleton()->get_ticks_msec();
  618. uint64_t diff = now - search_time_msec;
  619. if (diff < uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec")) * 2) {
  620. for (int i = current - 1; i >= 0; i--) {
  621. if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
  622. set_current(i);
  623. ensure_current_is_visible();
  624. if (select_mode == SELECT_SINGLE) {
  625. emit_signal(SNAME("item_selected"), current);
  626. }
  627. break;
  628. }
  629. }
  630. accept_event();
  631. return;
  632. }
  633. }
  634. if (current >= current_columns) {
  635. int next = current - current_columns;
  636. while (next >= 0 && !CAN_SELECT(next)) {
  637. next = next - current_columns;
  638. }
  639. if (next < 0) {
  640. accept_event();
  641. return;
  642. }
  643. set_current(next);
  644. ensure_current_is_visible();
  645. if (select_mode == SELECT_SINGLE) {
  646. emit_signal(SNAME("item_selected"), current);
  647. }
  648. accept_event();
  649. }
  650. } else if (p_event->is_action("ui_down", true)) {
  651. if (!search_string.is_empty()) {
  652. uint64_t now = OS::get_singleton()->get_ticks_msec();
  653. uint64_t diff = now - search_time_msec;
  654. if (diff < uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec")) * 2) {
  655. for (int i = current + 1; i < items.size(); i++) {
  656. if (CAN_SELECT(i) && items[i].text.begins_with(search_string)) {
  657. set_current(i);
  658. ensure_current_is_visible();
  659. if (select_mode == SELECT_SINGLE) {
  660. emit_signal(SNAME("item_selected"), current);
  661. }
  662. break;
  663. }
  664. }
  665. accept_event();
  666. return;
  667. }
  668. }
  669. if (current < items.size() - current_columns) {
  670. int next = current + current_columns;
  671. while (next < items.size() && !CAN_SELECT(next)) {
  672. next = next + current_columns;
  673. }
  674. if (next >= items.size()) {
  675. accept_event();
  676. return;
  677. }
  678. set_current(next);
  679. ensure_current_is_visible();
  680. if (select_mode == SELECT_SINGLE) {
  681. emit_signal(SNAME("item_selected"), current);
  682. }
  683. accept_event();
  684. }
  685. } else if (p_event->is_action("ui_page_up", true)) {
  686. search_string = ""; //any mousepress cancels
  687. for (int i = 4; i > 0; i--) {
  688. int index = current - current_columns * i;
  689. if (index >= 0 && index < items.size() && CAN_SELECT(index)) {
  690. set_current(index);
  691. ensure_current_is_visible();
  692. if (select_mode == SELECT_SINGLE) {
  693. emit_signal(SNAME("item_selected"), current);
  694. }
  695. accept_event();
  696. break;
  697. }
  698. }
  699. } else if (p_event->is_action("ui_page_down", true)) {
  700. search_string = ""; //any mousepress cancels
  701. for (int i = 4; i > 0; i--) {
  702. int index = current + current_columns * i;
  703. if (index >= 0 && index < items.size() && CAN_SELECT(index)) {
  704. set_current(index);
  705. ensure_current_is_visible();
  706. if (select_mode == SELECT_SINGLE) {
  707. emit_signal(SNAME("item_selected"), current);
  708. }
  709. accept_event();
  710. break;
  711. }
  712. }
  713. } else if (p_event->is_action("ui_left", true)) {
  714. search_string = ""; //any mousepress cancels
  715. if (current % current_columns != 0) {
  716. int current_row = current / current_columns;
  717. int next = current - 1;
  718. while (next >= 0 && !CAN_SELECT(next)) {
  719. next = next - 1;
  720. }
  721. if (next < 0 || !IS_SAME_ROW(next, current_row)) {
  722. accept_event();
  723. return;
  724. }
  725. set_current(next);
  726. ensure_current_is_visible();
  727. if (select_mode == SELECT_SINGLE) {
  728. emit_signal(SNAME("item_selected"), current);
  729. }
  730. accept_event();
  731. }
  732. } else if (p_event->is_action("ui_right", true)) {
  733. search_string = ""; //any mousepress cancels
  734. if (current % current_columns != (current_columns - 1) && current + 1 < items.size()) {
  735. int current_row = current / current_columns;
  736. int next = current + 1;
  737. while (next < items.size() && !CAN_SELECT(next)) {
  738. next = next + 1;
  739. }
  740. if (items.size() <= next || !IS_SAME_ROW(next, current_row)) {
  741. accept_event();
  742. return;
  743. }
  744. set_current(next);
  745. ensure_current_is_visible();
  746. if (select_mode == SELECT_SINGLE) {
  747. emit_signal(SNAME("item_selected"), current);
  748. }
  749. accept_event();
  750. }
  751. } else if (p_event->is_action("ui_cancel", true)) {
  752. search_string = "";
  753. } else if (p_event->is_action("ui_select", true) && select_mode == SELECT_MULTI) {
  754. if (current >= 0 && current < items.size()) {
  755. if (CAN_SELECT(current) && !items[current].selected) {
  756. select(current, false);
  757. emit_signal(SNAME("multi_selected"), current, true);
  758. } else if (items[current].selected) {
  759. deselect(current);
  760. emit_signal(SNAME("multi_selected"), current, false);
  761. }
  762. }
  763. } else if (p_event->is_action("ui_accept", true)) {
  764. search_string = ""; //any mousepress cancels
  765. if (current >= 0 && current < items.size() && !items[current].disabled) {
  766. emit_signal(SNAME("item_activated"), current);
  767. }
  768. } else {
  769. Ref<InputEventKey> k = p_event;
  770. if (allow_search && k.is_valid() && k->get_unicode()) {
  771. uint64_t now = OS::get_singleton()->get_ticks_msec();
  772. uint64_t diff = now - search_time_msec;
  773. uint64_t max_interval = uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec"));
  774. search_time_msec = now;
  775. if (diff > max_interval) {
  776. search_string = "";
  777. }
  778. if (String::chr(k->get_unicode()) != search_string) {
  779. search_string += String::chr(k->get_unicode());
  780. }
  781. for (int i = current + 1; i <= items.size(); i++) {
  782. if (i == items.size()) {
  783. if (current == 0 || current == -1) {
  784. break;
  785. } else {
  786. i = 0;
  787. }
  788. }
  789. if (i == current) {
  790. break;
  791. }
  792. if (items[i].text.findn(search_string) == 0) {
  793. set_current(i);
  794. ensure_current_is_visible();
  795. if (select_mode == SELECT_SINGLE) {
  796. emit_signal(SNAME("item_selected"), current);
  797. }
  798. break;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. Ref<InputEventPanGesture> pan_gesture = p_event;
  805. if (pan_gesture.is_valid()) {
  806. scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * pan_gesture->get_delta().y / 8);
  807. }
  808. if (scroll_bar->get_value() != prev_scroll) {
  809. accept_event(); //accept event if scroll changed
  810. }
  811. #undef CAN_SELECT
  812. #undef IS_SAME_ROW
  813. }
  814. void ItemList::ensure_current_is_visible() {
  815. ensure_selected_visible = true;
  816. queue_redraw();
  817. }
  818. static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) {
  819. Size2 size = p_max_size;
  820. int tex_width = p_size.width * size.height / p_size.height;
  821. int tex_height = size.height;
  822. if (tex_width > size.width) {
  823. tex_width = size.width;
  824. tex_height = p_size.height * tex_width / p_size.width;
  825. }
  826. int ofs_x = (size.width - tex_width) / 2;
  827. int ofs_y = (size.height - tex_height) / 2;
  828. return Rect2(ofs_x, ofs_y, tex_width, tex_height);
  829. }
  830. void ItemList::_notification(int p_what) {
  831. switch (p_what) {
  832. case NOTIFICATION_RESIZED: {
  833. shape_changed = true;
  834. queue_redraw();
  835. } break;
  836. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  837. case NOTIFICATION_THEME_CHANGED: {
  838. for (int i = 0; i < items.size(); i++) {
  839. _shape_text(i);
  840. }
  841. shape_changed = true;
  842. queue_redraw();
  843. } break;
  844. case NOTIFICATION_TRANSLATION_CHANGED: {
  845. for (int i = 0; i < items.size(); i++) {
  846. items.write[i].xl_text = atr(items[i].text);
  847. _shape_text(i);
  848. }
  849. shape_changed = true;
  850. queue_redraw();
  851. } break;
  852. case NOTIFICATION_DRAW: {
  853. force_update_list_size();
  854. int scroll_bar_minwidth = scroll_bar->get_minimum_size().x;
  855. scroll_bar->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_bar_minwidth);
  856. scroll_bar->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  857. scroll_bar->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, theme_cache.panel_style->get_margin(SIDE_TOP));
  858. scroll_bar->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -theme_cache.panel_style->get_margin(SIDE_BOTTOM));
  859. Size2 size = get_size();
  860. int width = size.width - theme_cache.panel_style->get_minimum_size().width;
  861. if (scroll_bar->is_visible()) {
  862. width -= scroll_bar_minwidth;
  863. }
  864. draw_style_box(theme_cache.panel_style, Rect2(Point2(), size));
  865. Ref<StyleBox> sbsel;
  866. Ref<StyleBox> cursor;
  867. if (has_focus()) {
  868. sbsel = theme_cache.selected_focus_style;
  869. cursor = theme_cache.cursor_focus_style;
  870. } else {
  871. sbsel = theme_cache.selected_style;
  872. cursor = theme_cache.cursor_style;
  873. }
  874. bool rtl = is_layout_rtl();
  875. if (has_focus()) {
  876. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true);
  877. draw_style_box(theme_cache.focus_style, Rect2(Point2(), size));
  878. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false);
  879. }
  880. // Ensure_selected_visible needs to be checked before we draw the list.
  881. if (ensure_selected_visible && current >= 0 && current < items.size()) {
  882. Rect2 r = items[current].rect_cache;
  883. int from = scroll_bar->get_value();
  884. int to = from + scroll_bar->get_page();
  885. if (r.position.y < from) {
  886. scroll_bar->set_value(r.position.y);
  887. } else if (r.position.y + r.size.y > to) {
  888. scroll_bar->set_value(r.position.y + r.size.y - (to - from));
  889. }
  890. }
  891. ensure_selected_visible = false;
  892. Vector2 base_ofs = theme_cache.panel_style->get_offset();
  893. base_ofs.y -= int(scroll_bar->get_value());
  894. // Define a visible frame to check against and optimize drawing.
  895. const Rect2 clip(-base_ofs, size);
  896. // Do a binary search to find the first separator that is below clip_position.y.
  897. int first_visible_separator = 0;
  898. {
  899. int lo = 0;
  900. int hi = separators.size();
  901. while (lo < hi) {
  902. const int mid = (lo + hi) / 2;
  903. if (separators[mid] < clip.position.y) {
  904. lo = mid + 1;
  905. } else {
  906. hi = mid;
  907. }
  908. }
  909. first_visible_separator = lo;
  910. }
  911. // If not in thumbnails mode, draw visible separators.
  912. if (icon_mode != ICON_MODE_TOP) {
  913. for (int i = first_visible_separator; i < separators.size(); i++) {
  914. if (separators[i] > clip.position.y + clip.size.y) {
  915. break; // done
  916. }
  917. const int y = base_ofs.y + separators[i];
  918. draw_line(Vector2(theme_cache.panel_style->get_margin(SIDE_LEFT), y), Vector2(width, y), theme_cache.guide_color);
  919. }
  920. }
  921. // Do a binary search to find the first item whose rect reaches below clip.position.y.
  922. int first_item_visible;
  923. {
  924. int lo = 0;
  925. int hi = items.size();
  926. while (lo < hi) {
  927. const int mid = (lo + hi) / 2;
  928. const Rect2 &rcache = items[mid].rect_cache;
  929. if (rcache.position.y + rcache.size.y < clip.position.y) {
  930. lo = mid + 1;
  931. } else {
  932. hi = mid;
  933. }
  934. }
  935. // We might end up with an item in columns 2, 3, etc, but we need the one from the first column.
  936. // We can also end up in a state where lo reached hi, and so no items can be rendered; we skip that.
  937. while (lo < hi && lo > 0 && items[lo].column > 0) {
  938. lo -= 1;
  939. }
  940. first_item_visible = lo;
  941. }
  942. // Draw visible items.
  943. for (int i = first_item_visible; i < items.size(); i++) {
  944. Rect2 rcache = items[i].rect_cache;
  945. if (rcache.position.y > clip.position.y + clip.size.y) {
  946. break; // done
  947. }
  948. if (!clip.intersects(rcache)) {
  949. continue;
  950. }
  951. if (current_columns == 1) {
  952. rcache.size.width = width - rcache.position.x;
  953. }
  954. bool should_draw_selected_bg = items[i].selected;
  955. bool should_draw_hovered_bg = hovered == i && !items[i].selected;
  956. bool should_draw_custom_bg = items[i].custom_bg.a > 0.001;
  957. if (should_draw_selected_bg || should_draw_hovered_bg || should_draw_custom_bg) {
  958. Rect2 r = rcache;
  959. r.position += base_ofs;
  960. r.position.y -= theme_cache.v_separation / 2;
  961. r.size.y += theme_cache.v_separation;
  962. r.position.x -= theme_cache.h_separation / 2;
  963. r.size.x += theme_cache.h_separation;
  964. if (rtl) {
  965. r.position.x = size.width - r.position.x - r.size.x;
  966. }
  967. if (should_draw_selected_bg) {
  968. draw_style_box(sbsel, r);
  969. }
  970. if (should_draw_hovered_bg) {
  971. draw_style_box(theme_cache.hovered_style, r);
  972. }
  973. if (should_draw_custom_bg) {
  974. draw_rect(r, items[i].custom_bg);
  975. }
  976. }
  977. Vector2 text_ofs;
  978. if (items[i].icon.is_valid()) {
  979. Size2 icon_size;
  980. //= _adjust_to_max_size(items[i].get_icon_size(),fixed_icon_size) * icon_scale;
  981. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  982. icon_size = fixed_icon_size * icon_scale;
  983. } else {
  984. icon_size = items[i].get_icon_size() * icon_scale;
  985. }
  986. Vector2 icon_ofs;
  987. Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs;
  988. if (icon_mode == ICON_MODE_TOP) {
  989. pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2);
  990. pos.y += theme_cache.icon_margin;
  991. text_ofs.y = icon_size.height + theme_cache.icon_margin * 2;
  992. } else {
  993. pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2);
  994. text_ofs.x = icon_size.width + theme_cache.icon_margin;
  995. }
  996. Rect2 draw_rect = Rect2(pos, icon_size);
  997. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  998. Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size);
  999. draw_rect.position += adj.position;
  1000. draw_rect.size = adj.size;
  1001. }
  1002. Color icon_modulate = items[i].icon_modulate;
  1003. if (items[i].disabled) {
  1004. icon_modulate.a *= 0.5;
  1005. }
  1006. // If the icon is transposed, we have to switch the size so that it is drawn correctly
  1007. if (items[i].icon_transposed) {
  1008. Size2 size_tmp = draw_rect.size;
  1009. draw_rect.size.x = size_tmp.y;
  1010. draw_rect.size.y = size_tmp.x;
  1011. }
  1012. 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);
  1013. if (rtl) {
  1014. draw_rect.position.x = size.width - draw_rect.position.x - draw_rect.size.x;
  1015. }
  1016. draw_texture_rect_region(items[i].icon, draw_rect, region, icon_modulate, items[i].icon_transposed);
  1017. }
  1018. if (items[i].tag_icon.is_valid()) {
  1019. Point2 draw_pos = items[i].rect_cache.position;
  1020. if (rtl) {
  1021. draw_pos.x = size.width - draw_pos.x - items[i].tag_icon->get_width();
  1022. }
  1023. draw_texture(items[i].tag_icon, draw_pos + base_ofs);
  1024. }
  1025. if (!items[i].text.is_empty()) {
  1026. int max_len = -1;
  1027. Vector2 size2 = items[i].text_buf->get_size();
  1028. if (fixed_column_width) {
  1029. max_len = fixed_column_width;
  1030. } else if (same_column_width) {
  1031. max_len = items[i].rect_cache.size.x;
  1032. } else {
  1033. max_len = size2.x;
  1034. }
  1035. Color txt_modulate;
  1036. if (items[i].selected) {
  1037. txt_modulate = theme_cache.font_selected_color;
  1038. } else if (hovered == i) {
  1039. txt_modulate = theme_cache.font_hovered_color;
  1040. } else if (items[i].custom_fg != Color()) {
  1041. txt_modulate = items[i].custom_fg;
  1042. } else {
  1043. txt_modulate = theme_cache.font_color;
  1044. }
  1045. if (items[i].disabled) {
  1046. txt_modulate.a *= 0.5;
  1047. }
  1048. if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
  1049. text_ofs += base_ofs;
  1050. text_ofs += items[i].rect_cache.position;
  1051. if (rtl) {
  1052. text_ofs.x = size.width - text_ofs.x - max_len;
  1053. }
  1054. items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1055. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  1056. items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, theme_cache.font_outline_size, theme_cache.font_outline_color);
  1057. }
  1058. items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
  1059. } else {
  1060. if (fixed_column_width > 0) {
  1061. size2.x = MIN(size2.x, fixed_column_width);
  1062. }
  1063. if (icon_mode == ICON_MODE_TOP) {
  1064. text_ofs.x += (items[i].rect_cache.size.width - size2.x) / 2;
  1065. } else {
  1066. text_ofs.y += (items[i].rect_cache.size.height - size2.y) / 2;
  1067. }
  1068. text_ofs += base_ofs;
  1069. text_ofs += items[i].rect_cache.position;
  1070. float text_w = width - text_ofs.x;
  1071. items.write[i].text_buf->set_width(text_w);
  1072. if (rtl) {
  1073. text_ofs.x = size.width - width;
  1074. items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  1075. } else {
  1076. items.write[i].text_buf->set_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1077. }
  1078. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  1079. items[i].text_buf->draw_outline(get_canvas_item(), text_ofs, theme_cache.font_outline_size, theme_cache.font_outline_color);
  1080. }
  1081. if (width - text_ofs.x > 0) {
  1082. items[i].text_buf->draw(get_canvas_item(), text_ofs, txt_modulate);
  1083. }
  1084. }
  1085. }
  1086. if (select_mode == SELECT_MULTI && i == current) {
  1087. Rect2 r = rcache;
  1088. r.position += base_ofs;
  1089. r.position.y -= theme_cache.v_separation / 2;
  1090. r.size.y += theme_cache.v_separation;
  1091. r.position.x -= theme_cache.h_separation / 2;
  1092. r.size.x += theme_cache.h_separation;
  1093. if (rtl) {
  1094. r.position.x = size.width - r.position.x - r.size.x;
  1095. }
  1096. draw_style_box(cursor, r);
  1097. }
  1098. }
  1099. } break;
  1100. }
  1101. }
  1102. void ItemList::force_update_list_size() {
  1103. if (!shape_changed) {
  1104. return;
  1105. }
  1106. int scroll_bar_minwidth = scroll_bar->get_minimum_size().x;
  1107. Size2 size = get_size();
  1108. float max_column_width = 0.0;
  1109. //1- compute item minimum sizes
  1110. for (int i = 0; i < items.size(); i++) {
  1111. Size2 minsize;
  1112. if (items[i].icon.is_valid()) {
  1113. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  1114. minsize = fixed_icon_size * icon_scale;
  1115. } else {
  1116. minsize = items[i].get_icon_size() * icon_scale;
  1117. }
  1118. if (!items[i].text.is_empty()) {
  1119. if (icon_mode == ICON_MODE_TOP) {
  1120. minsize.y += theme_cache.icon_margin;
  1121. } else {
  1122. minsize.x += theme_cache.icon_margin;
  1123. }
  1124. }
  1125. }
  1126. if (!items[i].text.is_empty()) {
  1127. int max_width = -1;
  1128. if (fixed_column_width) {
  1129. max_width = fixed_column_width;
  1130. } else if (same_column_width) {
  1131. max_width = items[i].rect_cache.size.x;
  1132. }
  1133. items.write[i].text_buf->set_width(max_width);
  1134. Size2 s = items[i].text_buf->get_size();
  1135. if (icon_mode == ICON_MODE_TOP) {
  1136. minsize.x = MAX(minsize.x, s.width);
  1137. if (max_text_lines > 0) {
  1138. minsize.y += s.height + theme_cache.line_separation * max_text_lines;
  1139. } else {
  1140. minsize.y += s.height;
  1141. }
  1142. } else {
  1143. minsize.y = MAX(minsize.y, s.height);
  1144. minsize.x += s.width;
  1145. }
  1146. }
  1147. if (fixed_column_width > 0) {
  1148. minsize.x = fixed_column_width;
  1149. }
  1150. max_column_width = MAX(max_column_width, minsize.x);
  1151. // elements need to adapt to the selected size
  1152. minsize.y += theme_cache.v_separation;
  1153. minsize.x += theme_cache.h_separation;
  1154. items.write[i].rect_cache.size = minsize;
  1155. items.write[i].min_rect_cache.size = minsize;
  1156. }
  1157. int fit_size = size.x - theme_cache.panel_style->get_minimum_size().width - scroll_bar_minwidth;
  1158. //2-attempt best fit
  1159. current_columns = 0x7FFFFFFF;
  1160. if (max_columns > 0) {
  1161. current_columns = max_columns;
  1162. }
  1163. // Repeat until all items fit.
  1164. while (true) {
  1165. bool all_fit = true;
  1166. Vector2 ofs;
  1167. int col = 0;
  1168. int max_h = 0;
  1169. separators.clear();
  1170. for (int i = 0; i < items.size(); i++) {
  1171. if (current_columns > 1 && items[i].rect_cache.size.width + ofs.x > fit_size) {
  1172. // Went past.
  1173. current_columns = MAX(col, 1);
  1174. all_fit = false;
  1175. break;
  1176. }
  1177. if (same_column_width) {
  1178. items.write[i].rect_cache.size.x = max_column_width;
  1179. }
  1180. items.write[i].rect_cache.position = ofs;
  1181. max_h = MAX(max_h, items[i].rect_cache.size.y);
  1182. ofs.x += items[i].rect_cache.size.x + theme_cache.h_separation;
  1183. items.write[i].column = col;
  1184. col++;
  1185. if (col == current_columns) {
  1186. if (i < items.size() - 1) {
  1187. separators.push_back(ofs.y + max_h + theme_cache.v_separation / 2);
  1188. }
  1189. for (int j = i; j >= 0 && col > 0; j--, col--) {
  1190. items.write[j].rect_cache.size.y = max_h;
  1191. }
  1192. ofs.x = 0;
  1193. ofs.y += max_h + theme_cache.v_separation;
  1194. col = 0;
  1195. max_h = 0;
  1196. }
  1197. }
  1198. if (all_fit) {
  1199. for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) {
  1200. items.write[j].rect_cache.size.y = max_h;
  1201. }
  1202. float page = MAX(0, size.height - theme_cache.panel_style->get_minimum_size().height);
  1203. float max = MAX(page, ofs.y + max_h);
  1204. if (auto_height) {
  1205. auto_height_value = ofs.y + max_h + theme_cache.panel_style->get_minimum_size().height;
  1206. }
  1207. scroll_bar->set_max(max);
  1208. scroll_bar->set_page(page);
  1209. if (max <= page) {
  1210. scroll_bar->set_value(0);
  1211. scroll_bar->hide();
  1212. } else {
  1213. scroll_bar->show();
  1214. if (do_autoscroll_to_bottom) {
  1215. scroll_bar->set_value(max);
  1216. }
  1217. }
  1218. break;
  1219. }
  1220. }
  1221. update_minimum_size();
  1222. shape_changed = false;
  1223. }
  1224. void ItemList::_scroll_changed(double) {
  1225. queue_redraw();
  1226. }
  1227. void ItemList::_mouse_exited() {
  1228. if (hovered > -1) {
  1229. hovered = -1;
  1230. queue_redraw();
  1231. }
  1232. }
  1233. int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const {
  1234. Vector2 pos = p_pos;
  1235. pos -= theme_cache.panel_style->get_offset();
  1236. pos.y += scroll_bar->get_value();
  1237. if (is_layout_rtl()) {
  1238. pos.x = get_size().width - pos.x;
  1239. }
  1240. int closest = -1;
  1241. int closest_dist = 0x7FFFFFFF;
  1242. for (int i = 0; i < items.size(); i++) {
  1243. Rect2 rc = items[i].rect_cache;
  1244. if (i % current_columns == current_columns - 1) {
  1245. rc.size.width = get_size().width - rc.position.x; // Make sure you can still select the last item when clicking past the column.
  1246. }
  1247. if (rc.has_point(pos)) {
  1248. closest = i;
  1249. break;
  1250. }
  1251. float dist = rc.distance_to(pos);
  1252. if (!p_exact && dist < closest_dist) {
  1253. closest = i;
  1254. closest_dist = dist;
  1255. }
  1256. }
  1257. return closest;
  1258. }
  1259. bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const {
  1260. if (items.is_empty()) {
  1261. return true;
  1262. }
  1263. Vector2 pos = p_pos;
  1264. pos -= theme_cache.panel_style->get_offset();
  1265. pos.y += scroll_bar->get_value();
  1266. if (is_layout_rtl()) {
  1267. pos.x = get_size().width - pos.x;
  1268. }
  1269. Rect2 endrect = items[items.size() - 1].rect_cache;
  1270. return (pos.y > endrect.position.y + endrect.size.y);
  1271. }
  1272. String ItemList::get_tooltip(const Point2 &p_pos) const {
  1273. int closest = get_item_at_position(p_pos, true);
  1274. if (closest != -1) {
  1275. if (!items[closest].tooltip_enabled) {
  1276. return "";
  1277. }
  1278. if (!items[closest].tooltip.is_empty()) {
  1279. return items[closest].tooltip;
  1280. }
  1281. if (!items[closest].text.is_empty()) {
  1282. return items[closest].text;
  1283. }
  1284. }
  1285. return Control::get_tooltip(p_pos);
  1286. }
  1287. void ItemList::sort_items_by_text() {
  1288. items.sort();
  1289. queue_redraw();
  1290. shape_changed = true;
  1291. if (select_mode == SELECT_SINGLE) {
  1292. for (int i = 0; i < items.size(); i++) {
  1293. if (items[i].selected) {
  1294. select(i);
  1295. return;
  1296. }
  1297. }
  1298. }
  1299. }
  1300. int ItemList::find_metadata(const Variant &p_metadata) const {
  1301. for (int i = 0; i < items.size(); i++) {
  1302. if (items[i].metadata == p_metadata) {
  1303. return i;
  1304. }
  1305. }
  1306. return -1;
  1307. }
  1308. void ItemList::set_allow_rmb_select(bool p_allow) {
  1309. allow_rmb_select = p_allow;
  1310. }
  1311. bool ItemList::get_allow_rmb_select() const {
  1312. return allow_rmb_select;
  1313. }
  1314. void ItemList::set_allow_reselect(bool p_allow) {
  1315. allow_reselect = p_allow;
  1316. }
  1317. bool ItemList::get_allow_reselect() const {
  1318. return allow_reselect;
  1319. }
  1320. void ItemList::set_allow_search(bool p_allow) {
  1321. allow_search = p_allow;
  1322. }
  1323. bool ItemList::get_allow_search() const {
  1324. return allow_search;
  1325. }
  1326. void ItemList::set_icon_scale(real_t p_scale) {
  1327. ERR_FAIL_COND(!Math::is_finite(p_scale));
  1328. if (icon_scale == p_scale) {
  1329. return;
  1330. }
  1331. icon_scale = p_scale;
  1332. queue_redraw();
  1333. shape_changed = true;
  1334. }
  1335. real_t ItemList::get_icon_scale() const {
  1336. return icon_scale;
  1337. }
  1338. Vector<int> ItemList::get_selected_items() {
  1339. Vector<int> selected;
  1340. for (int i = 0; i < items.size(); i++) {
  1341. if (items[i].selected) {
  1342. selected.push_back(i);
  1343. if (select_mode == SELECT_SINGLE) {
  1344. break;
  1345. }
  1346. }
  1347. }
  1348. return selected;
  1349. }
  1350. bool ItemList::is_anything_selected() {
  1351. for (int i = 0; i < items.size(); i++) {
  1352. if (items[i].selected) {
  1353. return true;
  1354. }
  1355. }
  1356. return false;
  1357. }
  1358. Size2 ItemList::get_minimum_size() const {
  1359. if (auto_height) {
  1360. return Size2(0, auto_height_value);
  1361. }
  1362. return Size2();
  1363. }
  1364. void ItemList::set_autoscroll_to_bottom(const bool p_enable) {
  1365. do_autoscroll_to_bottom = p_enable;
  1366. }
  1367. void ItemList::set_auto_height(bool p_enable) {
  1368. if (auto_height == p_enable) {
  1369. return;
  1370. }
  1371. auto_height = p_enable;
  1372. shape_changed = true;
  1373. queue_redraw();
  1374. }
  1375. bool ItemList::has_auto_height() const {
  1376. return auto_height;
  1377. }
  1378. void ItemList::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  1379. if (text_overrun_behavior != p_behavior) {
  1380. text_overrun_behavior = p_behavior;
  1381. for (int i = 0; i < items.size(); i++) {
  1382. items.write[i].text_buf->set_text_overrun_behavior(p_behavior);
  1383. }
  1384. shape_changed = true;
  1385. queue_redraw();
  1386. }
  1387. }
  1388. TextServer::OverrunBehavior ItemList::get_text_overrun_behavior() const {
  1389. return text_overrun_behavior;
  1390. }
  1391. bool ItemList::_set(const StringName &p_name, const Variant &p_value) {
  1392. Vector<String> components = String(p_name).split("/", true, 2);
  1393. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  1394. int item_index = components[0].trim_prefix("item_").to_int();
  1395. if (components[1] == "text") {
  1396. set_item_text(item_index, p_value);
  1397. return true;
  1398. } else if (components[1] == "icon") {
  1399. set_item_icon(item_index, p_value);
  1400. return true;
  1401. } else if (components[1] == "disabled") {
  1402. set_item_disabled(item_index, p_value);
  1403. return true;
  1404. } else if (components[1] == "selectable") {
  1405. set_item_selectable(item_index, p_value);
  1406. return true;
  1407. }
  1408. }
  1409. #ifndef DISABLE_DEPRECATED
  1410. // Compatibility.
  1411. if (p_name == "items") {
  1412. Array arr = p_value;
  1413. ERR_FAIL_COND_V(arr.size() % 3, false);
  1414. clear();
  1415. for (int i = 0; i < arr.size(); i += 3) {
  1416. String text = arr[i + 0];
  1417. Ref<Texture2D> icon = arr[i + 1];
  1418. bool disabled = arr[i + 2];
  1419. int idx = get_item_count();
  1420. add_item(text, icon);
  1421. set_item_disabled(idx, disabled);
  1422. }
  1423. }
  1424. #endif
  1425. return false;
  1426. }
  1427. bool ItemList::_get(const StringName &p_name, Variant &r_ret) const {
  1428. Vector<String> components = String(p_name).split("/", true, 2);
  1429. if (components.size() >= 2 && components[0].begins_with("item_") && components[0].trim_prefix("item_").is_valid_int()) {
  1430. int item_index = components[0].trim_prefix("item_").to_int();
  1431. if (components[1] == "text") {
  1432. r_ret = get_item_text(item_index);
  1433. return true;
  1434. } else if (components[1] == "icon") {
  1435. r_ret = get_item_icon(item_index);
  1436. return true;
  1437. } else if (components[1] == "disabled") {
  1438. r_ret = is_item_disabled(item_index);
  1439. return true;
  1440. } else if (components[1] == "selectable") {
  1441. r_ret = is_item_selectable(item_index);
  1442. return true;
  1443. }
  1444. }
  1445. return false;
  1446. }
  1447. void ItemList::_get_property_list(List<PropertyInfo> *p_list) const {
  1448. for (int i = 0; i < items.size(); i++) {
  1449. p_list->push_back(PropertyInfo(Variant::STRING, vformat("item_%d/text", i)));
  1450. PropertyInfo pi = PropertyInfo(Variant::OBJECT, vformat("item_%d/icon", i), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D");
  1451. pi.usage &= ~(get_item_icon(i).is_null() ? PROPERTY_USAGE_STORAGE : 0);
  1452. p_list->push_back(pi);
  1453. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/selectable", i));
  1454. pi.usage &= ~(is_item_selectable(i) ? PROPERTY_USAGE_STORAGE : 0);
  1455. p_list->push_back(pi);
  1456. pi = PropertyInfo(Variant::BOOL, vformat("item_%d/disabled", i));
  1457. pi.usage &= ~(!is_item_disabled(i) ? PROPERTY_USAGE_STORAGE : 0);
  1458. p_list->push_back(pi);
  1459. }
  1460. }
  1461. void ItemList::_bind_methods() {
  1462. ClassDB::bind_method(D_METHOD("add_item", "text", "icon", "selectable"), &ItemList::add_item, DEFVAL(Variant()), DEFVAL(true));
  1463. ClassDB::bind_method(D_METHOD("add_icon_item", "icon", "selectable"), &ItemList::add_icon_item, DEFVAL(true));
  1464. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &ItemList::set_item_text);
  1465. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &ItemList::get_item_text);
  1466. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &ItemList::set_item_icon);
  1467. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &ItemList::get_item_icon);
  1468. ClassDB::bind_method(D_METHOD("set_item_text_direction", "idx", "direction"), &ItemList::set_item_text_direction);
  1469. ClassDB::bind_method(D_METHOD("get_item_text_direction", "idx"), &ItemList::get_item_text_direction);
  1470. ClassDB::bind_method(D_METHOD("set_item_language", "idx", "language"), &ItemList::set_item_language);
  1471. ClassDB::bind_method(D_METHOD("get_item_language", "idx"), &ItemList::get_item_language);
  1472. ClassDB::bind_method(D_METHOD("set_item_icon_transposed", "idx", "transposed"), &ItemList::set_item_icon_transposed);
  1473. ClassDB::bind_method(D_METHOD("is_item_icon_transposed", "idx"), &ItemList::is_item_icon_transposed);
  1474. ClassDB::bind_method(D_METHOD("set_item_icon_region", "idx", "rect"), &ItemList::set_item_icon_region);
  1475. ClassDB::bind_method(D_METHOD("get_item_icon_region", "idx"), &ItemList::get_item_icon_region);
  1476. ClassDB::bind_method(D_METHOD("set_item_icon_modulate", "idx", "modulate"), &ItemList::set_item_icon_modulate);
  1477. ClassDB::bind_method(D_METHOD("get_item_icon_modulate", "idx"), &ItemList::get_item_icon_modulate);
  1478. ClassDB::bind_method(D_METHOD("set_item_selectable", "idx", "selectable"), &ItemList::set_item_selectable);
  1479. ClassDB::bind_method(D_METHOD("is_item_selectable", "idx"), &ItemList::is_item_selectable);
  1480. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &ItemList::set_item_disabled);
  1481. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &ItemList::is_item_disabled);
  1482. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &ItemList::set_item_metadata);
  1483. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &ItemList::get_item_metadata);
  1484. ClassDB::bind_method(D_METHOD("set_item_custom_bg_color", "idx", "custom_bg_color"), &ItemList::set_item_custom_bg_color);
  1485. ClassDB::bind_method(D_METHOD("get_item_custom_bg_color", "idx"), &ItemList::get_item_custom_bg_color);
  1486. ClassDB::bind_method(D_METHOD("set_item_custom_fg_color", "idx", "custom_fg_color"), &ItemList::set_item_custom_fg_color);
  1487. ClassDB::bind_method(D_METHOD("get_item_custom_fg_color", "idx"), &ItemList::get_item_custom_fg_color);
  1488. ClassDB::bind_method(D_METHOD("get_item_rect", "idx", "expand"), &ItemList::get_item_rect, DEFVAL(true));
  1489. ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled", "idx", "enable"), &ItemList::set_item_tooltip_enabled);
  1490. ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled", "idx"), &ItemList::is_item_tooltip_enabled);
  1491. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &ItemList::set_item_tooltip);
  1492. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &ItemList::get_item_tooltip);
  1493. ClassDB::bind_method(D_METHOD("select", "idx", "single"), &ItemList::select, DEFVAL(true));
  1494. ClassDB::bind_method(D_METHOD("deselect", "idx"), &ItemList::deselect);
  1495. ClassDB::bind_method(D_METHOD("deselect_all"), &ItemList::deselect_all);
  1496. ClassDB::bind_method(D_METHOD("is_selected", "idx"), &ItemList::is_selected);
  1497. ClassDB::bind_method(D_METHOD("get_selected_items"), &ItemList::get_selected_items);
  1498. ClassDB::bind_method(D_METHOD("move_item", "from_idx", "to_idx"), &ItemList::move_item);
  1499. ClassDB::bind_method(D_METHOD("set_item_count", "count"), &ItemList::set_item_count);
  1500. ClassDB::bind_method(D_METHOD("get_item_count"), &ItemList::get_item_count);
  1501. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &ItemList::remove_item);
  1502. ClassDB::bind_method(D_METHOD("clear"), &ItemList::clear);
  1503. ClassDB::bind_method(D_METHOD("sort_items_by_text"), &ItemList::sort_items_by_text);
  1504. ClassDB::bind_method(D_METHOD("set_fixed_column_width", "width"), &ItemList::set_fixed_column_width);
  1505. ClassDB::bind_method(D_METHOD("get_fixed_column_width"), &ItemList::get_fixed_column_width);
  1506. ClassDB::bind_method(D_METHOD("set_same_column_width", "enable"), &ItemList::set_same_column_width);
  1507. ClassDB::bind_method(D_METHOD("is_same_column_width"), &ItemList::is_same_column_width);
  1508. ClassDB::bind_method(D_METHOD("set_max_text_lines", "lines"), &ItemList::set_max_text_lines);
  1509. ClassDB::bind_method(D_METHOD("get_max_text_lines"), &ItemList::get_max_text_lines);
  1510. ClassDB::bind_method(D_METHOD("set_max_columns", "amount"), &ItemList::set_max_columns);
  1511. ClassDB::bind_method(D_METHOD("get_max_columns"), &ItemList::get_max_columns);
  1512. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &ItemList::set_select_mode);
  1513. ClassDB::bind_method(D_METHOD("get_select_mode"), &ItemList::get_select_mode);
  1514. ClassDB::bind_method(D_METHOD("set_icon_mode", "mode"), &ItemList::set_icon_mode);
  1515. ClassDB::bind_method(D_METHOD("get_icon_mode"), &ItemList::get_icon_mode);
  1516. ClassDB::bind_method(D_METHOD("set_fixed_icon_size", "size"), &ItemList::set_fixed_icon_size);
  1517. ClassDB::bind_method(D_METHOD("get_fixed_icon_size"), &ItemList::get_fixed_icon_size);
  1518. ClassDB::bind_method(D_METHOD("set_icon_scale", "scale"), &ItemList::set_icon_scale);
  1519. ClassDB::bind_method(D_METHOD("get_icon_scale"), &ItemList::get_icon_scale);
  1520. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &ItemList::set_allow_rmb_select);
  1521. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &ItemList::get_allow_rmb_select);
  1522. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &ItemList::set_allow_reselect);
  1523. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &ItemList::get_allow_reselect);
  1524. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &ItemList::set_allow_search);
  1525. ClassDB::bind_method(D_METHOD("get_allow_search"), &ItemList::get_allow_search);
  1526. ClassDB::bind_method(D_METHOD("set_auto_height", "enable"), &ItemList::set_auto_height);
  1527. ClassDB::bind_method(D_METHOD("has_auto_height"), &ItemList::has_auto_height);
  1528. ClassDB::bind_method(D_METHOD("is_anything_selected"), &ItemList::is_anything_selected);
  1529. ClassDB::bind_method(D_METHOD("get_item_at_position", "position", "exact"), &ItemList::get_item_at_position, DEFVAL(false));
  1530. ClassDB::bind_method(D_METHOD("ensure_current_is_visible"), &ItemList::ensure_current_is_visible);
  1531. ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &ItemList::get_v_scroll_bar);
  1532. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &ItemList::set_text_overrun_behavior);
  1533. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &ItemList::get_text_overrun_behavior);
  1534. ClassDB::bind_method(D_METHOD("force_update_list_size"), &ItemList::force_update_list_size);
  1535. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi"), "set_select_mode", "get_select_mode");
  1536. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  1537. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  1538. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  1539. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_text_lines", PROPERTY_HINT_RANGE, "1,10,1,or_greater"), "set_max_text_lines", "get_max_text_lines");
  1540. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_height"), "set_auto_height", "has_auto_height");
  1541. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  1542. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "item_");
  1543. ADD_GROUP("Columns", "");
  1544. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_columns", PROPERTY_HINT_RANGE, "0,10,1,or_greater"), "set_max_columns", "get_max_columns");
  1545. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "same_column_width"), "set_same_column_width", "is_same_column_width");
  1546. 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");
  1547. ADD_GROUP("Icon", "");
  1548. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_mode", PROPERTY_HINT_ENUM, "Top,Left"), "set_icon_mode", "get_icon_mode");
  1549. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "icon_scale"), "set_icon_scale", "get_icon_scale");
  1550. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "fixed_icon_size", PROPERTY_HINT_NONE, "suffix:px"), "set_fixed_icon_size", "get_fixed_icon_size");
  1551. BIND_ENUM_CONSTANT(ICON_MODE_TOP);
  1552. BIND_ENUM_CONSTANT(ICON_MODE_LEFT);
  1553. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  1554. BIND_ENUM_CONSTANT(SELECT_MULTI);
  1555. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  1556. ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  1557. ADD_SIGNAL(MethodInfo("item_clicked", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "at_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  1558. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "selected")));
  1559. ADD_SIGNAL(MethodInfo("item_activated", PropertyInfo(Variant::INT, "index")));
  1560. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, h_separation);
  1561. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, v_separation);
  1562. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, panel_style, "panel");
  1563. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, focus_style, "focus");
  1564. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, ItemList, font);
  1565. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, ItemList, font_size);
  1566. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_color);
  1567. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_hovered_color);
  1568. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_selected_color);
  1569. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, ItemList, font_outline_size, "outline_size");
  1570. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, font_outline_color);
  1571. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, line_separation);
  1572. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, ItemList, icon_margin);
  1573. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, hovered_style, "hovered");
  1574. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, selected_style, "selected");
  1575. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, selected_focus_style, "selected_focus");
  1576. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_style, "cursor_unfocused");
  1577. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ItemList, cursor_focus_style, "cursor");
  1578. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, ItemList, guide_color);
  1579. }
  1580. ItemList::ItemList() {
  1581. scroll_bar = memnew(VScrollBar);
  1582. add_child(scroll_bar, false, INTERNAL_MODE_FRONT);
  1583. scroll_bar->connect("value_changed", callable_mp(this, &ItemList::_scroll_changed));
  1584. connect("mouse_exited", callable_mp(this, &ItemList::_mouse_exited));
  1585. set_focus_mode(FOCUS_ALL);
  1586. set_clip_contents(true);
  1587. }
  1588. ItemList::~ItemList() {
  1589. }