item_list.cpp 57 KB

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