item_list.cpp 67 KB

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