item_list.cpp 55 KB

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