item_list.cpp 52 KB

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