item_list.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*************************************************************************/
  2. /* item_list.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/os.h"
  32. #include "project_settings.h"
  33. void ItemList::add_item(const String &p_item, const Ref<Texture> &p_texture, bool p_selectable) {
  34. Item item;
  35. item.icon = p_texture;
  36. item.icon_region = Rect2i();
  37. item.text = p_item;
  38. item.selectable = p_selectable;
  39. item.selected = false;
  40. item.disabled = false;
  41. item.tooltip_enabled = true;
  42. item.custom_bg = Color(0, 0, 0, 0);
  43. items.push_back(item);
  44. update();
  45. shape_changed = true;
  46. }
  47. void ItemList::add_icon_item(const Ref<Texture> &p_item, bool p_selectable) {
  48. Item item;
  49. item.icon = p_item;
  50. item.icon_region = Rect2i();
  51. //item.text=p_item;
  52. item.selectable = p_selectable;
  53. item.selected = false;
  54. item.disabled = false;
  55. item.tooltip_enabled = true;
  56. item.custom_bg = Color(0, 0, 0, 0);
  57. items.push_back(item);
  58. update();
  59. shape_changed = true;
  60. }
  61. void ItemList::set_item_text(int p_idx, const String &p_text) {
  62. ERR_FAIL_INDEX(p_idx, items.size());
  63. items[p_idx].text = p_text;
  64. update();
  65. shape_changed = true;
  66. }
  67. String ItemList::get_item_text(int p_idx) const {
  68. ERR_FAIL_INDEX_V(p_idx, items.size(), String());
  69. return items[p_idx].text;
  70. }
  71. void ItemList::set_item_tooltip_enabled(int p_idx, const bool p_enabled) {
  72. ERR_FAIL_INDEX(p_idx, items.size());
  73. items[p_idx].tooltip_enabled = p_enabled;
  74. }
  75. bool ItemList::is_item_tooltip_enabled(int p_idx) const {
  76. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  77. return items[p_idx].tooltip_enabled;
  78. }
  79. void ItemList::set_item_tooltip(int p_idx, const String &p_tooltip) {
  80. ERR_FAIL_INDEX(p_idx, items.size());
  81. items[p_idx].tooltip = p_tooltip;
  82. update();
  83. shape_changed = true;
  84. }
  85. String ItemList::get_item_tooltip(int p_idx) const {
  86. ERR_FAIL_INDEX_V(p_idx, items.size(), String());
  87. return items[p_idx].tooltip;
  88. }
  89. void ItemList::set_item_icon(int p_idx, const Ref<Texture> &p_icon) {
  90. ERR_FAIL_INDEX(p_idx, items.size());
  91. items[p_idx].icon = p_icon;
  92. update();
  93. shape_changed = true;
  94. }
  95. Ref<Texture> ItemList::get_item_icon(int p_idx) const {
  96. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>());
  97. return items[p_idx].icon;
  98. }
  99. void ItemList::set_item_icon_region(int p_idx, const Rect2 &p_region) {
  100. ERR_FAIL_INDEX(p_idx, items.size());
  101. items[p_idx].icon_region = p_region;
  102. update();
  103. shape_changed = true;
  104. }
  105. Rect2 ItemList::get_item_icon_region(int p_idx) const {
  106. ERR_FAIL_INDEX_V(p_idx, items.size(), Rect2());
  107. return items[p_idx].icon_region;
  108. }
  109. void ItemList::set_item_custom_bg_color(int p_idx, const Color &p_custom_bg_color) {
  110. ERR_FAIL_INDEX(p_idx, items.size());
  111. items[p_idx].custom_bg = p_custom_bg_color;
  112. }
  113. Color ItemList::get_item_custom_bg_color(int p_idx) const {
  114. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  115. return items[p_idx].custom_bg;
  116. }
  117. void ItemList::set_item_custom_fg_color(int p_idx, const Color &p_custom_fg_color) {
  118. ERR_FAIL_INDEX(p_idx, items.size());
  119. items[p_idx].custom_fg = p_custom_fg_color;
  120. }
  121. Color ItemList::get_item_custom_fg_color(int p_idx) const {
  122. ERR_FAIL_INDEX_V(p_idx, items.size(), Color());
  123. return items[p_idx].custom_fg;
  124. }
  125. void ItemList::set_item_tag_icon(int p_idx, const Ref<Texture> &p_tag_icon) {
  126. ERR_FAIL_INDEX(p_idx, items.size());
  127. items[p_idx].tag_icon = p_tag_icon;
  128. update();
  129. shape_changed = true;
  130. }
  131. Ref<Texture> ItemList::get_item_tag_icon(int p_idx) const {
  132. ERR_FAIL_INDEX_V(p_idx, items.size(), Ref<Texture>());
  133. return items[p_idx].tag_icon;
  134. }
  135. void ItemList::set_item_selectable(int p_idx, bool p_selectable) {
  136. ERR_FAIL_INDEX(p_idx, items.size());
  137. items[p_idx].selectable = p_selectable;
  138. }
  139. bool ItemList::is_item_selectable(int p_idx) const {
  140. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  141. return items[p_idx].selectable;
  142. }
  143. void ItemList::set_item_disabled(int p_idx, bool p_disabled) {
  144. ERR_FAIL_INDEX(p_idx, items.size());
  145. items[p_idx].disabled = p_disabled;
  146. update();
  147. }
  148. bool ItemList::is_item_disabled(int p_idx) const {
  149. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  150. return items[p_idx].disabled;
  151. }
  152. void ItemList::set_item_metadata(int p_idx, const Variant &p_metadata) {
  153. ERR_FAIL_INDEX(p_idx, items.size());
  154. items[p_idx].metadata = p_metadata;
  155. update();
  156. shape_changed = true;
  157. }
  158. Variant ItemList::get_item_metadata(int p_idx) const {
  159. ERR_FAIL_INDEX_V(p_idx, items.size(), Variant());
  160. return items[p_idx].metadata;
  161. }
  162. void ItemList::select(int p_idx, bool p_single) {
  163. ERR_FAIL_INDEX(p_idx, items.size());
  164. if (p_single || select_mode == SELECT_SINGLE) {
  165. if (!items[p_idx].selectable || items[p_idx].disabled) {
  166. return;
  167. }
  168. for (int i = 0; i < items.size(); i++) {
  169. items[i].selected = p_idx == i;
  170. }
  171. current = p_idx;
  172. ensure_selected_visible = false;
  173. } else {
  174. if (items[p_idx].selectable && !items[p_idx].disabled) {
  175. items[p_idx].selected = true;
  176. }
  177. }
  178. update();
  179. }
  180. void ItemList::unselect(int p_idx) {
  181. ERR_FAIL_INDEX(p_idx, items.size());
  182. if (select_mode != SELECT_MULTI) {
  183. items[p_idx].selected = false;
  184. current = -1;
  185. } else {
  186. items[p_idx].selected = false;
  187. }
  188. update();
  189. }
  190. bool ItemList::is_selected(int p_idx) const {
  191. ERR_FAIL_INDEX_V(p_idx, items.size(), false);
  192. return items[p_idx].selected;
  193. }
  194. void ItemList::set_current(int p_current) {
  195. ERR_FAIL_INDEX(p_current, items.size());
  196. if (select_mode == SELECT_SINGLE)
  197. select(p_current, true);
  198. else {
  199. current = p_current;
  200. update();
  201. }
  202. }
  203. int ItemList::get_current() const {
  204. return current;
  205. }
  206. void ItemList::move_item(int p_item, int p_to_pos) {
  207. ERR_FAIL_INDEX(p_item, items.size());
  208. ERR_FAIL_INDEX(p_to_pos, items.size() + 1);
  209. Item it = items[p_item];
  210. items.remove(p_item);
  211. if (p_to_pos > p_item) {
  212. p_to_pos--;
  213. }
  214. if (p_to_pos >= items.size()) {
  215. items.push_back(it);
  216. } else {
  217. items.insert(p_to_pos, it);
  218. }
  219. if (current < 0) {
  220. //do none
  221. } else if (p_item == current) {
  222. current = p_to_pos;
  223. } else if (p_to_pos > p_item && current > p_item && current < p_to_pos) {
  224. current--;
  225. } else if (p_to_pos < p_item && current < p_item && current > p_to_pos) {
  226. current++;
  227. }
  228. update();
  229. }
  230. int ItemList::get_item_count() const {
  231. return items.size();
  232. }
  233. void ItemList::remove_item(int p_idx) {
  234. ERR_FAIL_INDEX(p_idx, items.size());
  235. items.remove(p_idx);
  236. update();
  237. shape_changed = true;
  238. defer_select_single = -1;
  239. }
  240. void ItemList::clear() {
  241. items.clear();
  242. current = -1;
  243. ensure_selected_visible = false;
  244. update();
  245. shape_changed = true;
  246. defer_select_single = -1;
  247. }
  248. void ItemList::set_fixed_column_width(int p_size) {
  249. ERR_FAIL_COND(p_size < 0);
  250. fixed_column_width = p_size;
  251. update();
  252. shape_changed = true;
  253. }
  254. int ItemList::get_fixed_column_width() const {
  255. return fixed_column_width;
  256. }
  257. void ItemList::set_same_column_width(bool p_enable) {
  258. same_column_width = p_enable;
  259. update();
  260. shape_changed = true;
  261. }
  262. bool ItemList::is_same_column_width() const {
  263. return same_column_width;
  264. }
  265. void ItemList::set_max_text_lines(int p_lines) {
  266. ERR_FAIL_COND(p_lines < 1);
  267. max_text_lines = p_lines;
  268. update();
  269. shape_changed = true;
  270. }
  271. int ItemList::get_max_text_lines() const {
  272. return max_text_lines;
  273. }
  274. void ItemList::set_max_columns(int p_amount) {
  275. ERR_FAIL_COND(p_amount < 0);
  276. max_columns = p_amount;
  277. update();
  278. }
  279. int ItemList::get_max_columns() const {
  280. return max_columns;
  281. }
  282. void ItemList::set_select_mode(SelectMode p_mode) {
  283. select_mode = p_mode;
  284. update();
  285. }
  286. ItemList::SelectMode ItemList::get_select_mode() const {
  287. return select_mode;
  288. }
  289. void ItemList::set_icon_mode(IconMode p_mode) {
  290. icon_mode = p_mode;
  291. update();
  292. shape_changed = true;
  293. }
  294. ItemList::IconMode ItemList::get_icon_mode() const {
  295. return icon_mode;
  296. }
  297. void ItemList::set_fixed_icon_size(const Size2 &p_size) {
  298. fixed_icon_size = p_size;
  299. update();
  300. }
  301. Size2 ItemList::get_fixed_icon_size() const {
  302. return fixed_icon_size;
  303. }
  304. Size2 ItemList::Item::get_icon_size() const {
  305. if (icon.is_null())
  306. return Size2();
  307. if (icon_region.has_no_area())
  308. return icon->get_size();
  309. return icon_region.size;
  310. }
  311. void ItemList::_gui_input(const Ref<InputEvent> &p_event) {
  312. Ref<InputEventMouseMotion> mm = p_event;
  313. if (defer_select_single >= 0 && mm.is_valid()) {
  314. defer_select_single = -1;
  315. return;
  316. }
  317. Ref<InputEventMouseButton> mb = p_event;
  318. if (defer_select_single >= 0 && mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  319. select(defer_select_single, true);
  320. emit_signal("multi_selected", defer_select_single, true);
  321. defer_select_single = -1;
  322. return;
  323. }
  324. if (mb.is_valid() && (mb->get_button_index() == BUTTON_LEFT || (allow_rmb_select && mb->get_button_index() == BUTTON_RIGHT)) && mb->is_pressed()) {
  325. search_string = ""; //any mousepress cancels
  326. Vector2 pos = mb->get_position();
  327. Ref<StyleBox> bg = get_stylebox("bg");
  328. pos -= bg->get_offset();
  329. pos.y += scroll_bar->get_value();
  330. int closest = -1;
  331. for (int i = 0; i < items.size(); i++) {
  332. Rect2 rc = items[i].rect_cache;
  333. if (i % current_columns == current_columns - 1) {
  334. rc.size.width = get_size().width; //not right but works
  335. }
  336. if (rc.has_point(pos)) {
  337. closest = i;
  338. break;
  339. }
  340. }
  341. if (closest != -1) {
  342. int i = closest;
  343. if (select_mode == SELECT_MULTI && items[i].selected && mb->get_command()) {
  344. unselect(i);
  345. emit_signal("multi_selected", i, false);
  346. } else if (select_mode == SELECT_MULTI && mb->get_shift() && current >= 0 && current < items.size() && current != i) {
  347. int from = current;
  348. int to = i;
  349. if (i < current) {
  350. SWAP(from, to);
  351. }
  352. for (int j = from; j <= to; j++) {
  353. bool selected = !items[j].selected;
  354. select(j, false);
  355. if (selected)
  356. emit_signal("multi_selected", i, true);
  357. }
  358. if (mb->get_button_index() == BUTTON_RIGHT) {
  359. emit_signal("item_rmb_selected", i, get_local_mouse_position());
  360. }
  361. } else {
  362. if (!mb->is_doubleclick() && !mb->get_command() && select_mode == SELECT_MULTI && items[i].selectable && !items[i].disabled && items[i].selected && mb->get_button_index() == BUTTON_LEFT) {
  363. defer_select_single = i;
  364. return;
  365. }
  366. if (items[i].selected && mb->get_button_index() == BUTTON_RIGHT) {
  367. emit_signal("item_rmb_selected", i, get_local_mouse_position());
  368. } else {
  369. bool selected = !items[i].selected;
  370. select(i, select_mode == SELECT_SINGLE || !mb->get_command());
  371. if (selected) {
  372. if (select_mode == SELECT_SINGLE) {
  373. emit_signal("item_selected", i);
  374. } else
  375. emit_signal("multi_selected", i, true);
  376. }
  377. if (mb->get_button_index() == BUTTON_RIGHT) {
  378. emit_signal("item_rmb_selected", i, get_local_mouse_position());
  379. } else if (/*select_mode==SELECT_SINGLE &&*/ mb->is_doubleclick()) {
  380. emit_signal("item_activated", i);
  381. }
  382. }
  383. }
  384. return;
  385. }
  386. }
  387. if (mb.is_valid() && mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  388. scroll_bar->set_value(scroll_bar->get_value() - scroll_bar->get_page() * mb->get_factor() / 8);
  389. }
  390. if (mb.is_valid() && mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  391. scroll_bar->set_value(scroll_bar->get_value() + scroll_bar->get_page() * mb->get_factor() / 8);
  392. }
  393. if (p_event->is_pressed() && items.size() > 0) {
  394. if (p_event->is_action("ui_up")) {
  395. if (search_string != "") {
  396. uint64_t now = OS::get_singleton()->get_ticks_msec();
  397. uint64_t diff = now - search_time_msec;
  398. if (diff < uint64_t(ProjectSettings::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) {
  399. for (int i = current - 1; i >= 0; i--) {
  400. if (items[i].text.begins_with(search_string)) {
  401. set_current(i);
  402. ensure_current_is_visible();
  403. if (select_mode == SELECT_SINGLE) {
  404. emit_signal("item_selected", current);
  405. }
  406. break;
  407. }
  408. }
  409. accept_event();
  410. return;
  411. }
  412. }
  413. if (current >= current_columns) {
  414. set_current(current - current_columns);
  415. ensure_current_is_visible();
  416. if (select_mode == SELECT_SINGLE) {
  417. emit_signal("item_selected", current);
  418. }
  419. accept_event();
  420. }
  421. } else if (p_event->is_action("ui_down")) {
  422. if (search_string != "") {
  423. uint64_t now = OS::get_singleton()->get_ticks_msec();
  424. uint64_t diff = now - search_time_msec;
  425. if (diff < uint64_t(ProjectSettings::get_singleton()->get("gui/timers/incremental_search_max_interval_msec")) * 2) {
  426. for (int i = current + 1; i < items.size(); i++) {
  427. if (items[i].text.begins_with(search_string)) {
  428. set_current(i);
  429. ensure_current_is_visible();
  430. if (select_mode == SELECT_SINGLE) {
  431. emit_signal("item_selected", current);
  432. }
  433. break;
  434. }
  435. }
  436. accept_event();
  437. return;
  438. }
  439. }
  440. if (current < items.size() - current_columns) {
  441. set_current(current + current_columns);
  442. ensure_current_is_visible();
  443. if (select_mode == SELECT_SINGLE) {
  444. emit_signal("item_selected", current);
  445. }
  446. accept_event();
  447. }
  448. } else if (p_event->is_action("ui_page_up")) {
  449. search_string = ""; //any mousepress cancels
  450. for (int i = 4; i > 0; i--) {
  451. if (current - current_columns * i >= 0) {
  452. set_current(current - current_columns * i);
  453. ensure_current_is_visible();
  454. if (select_mode == SELECT_SINGLE) {
  455. emit_signal("item_selected", current);
  456. }
  457. accept_event();
  458. break;
  459. }
  460. }
  461. } else if (p_event->is_action("ui_page_down")) {
  462. search_string = ""; //any mousepress cancels
  463. for (int i = 4; i > 0; i--) {
  464. if (current + current_columns * i < items.size()) {
  465. set_current(current + current_columns * i);
  466. ensure_current_is_visible();
  467. if (select_mode == SELECT_SINGLE) {
  468. emit_signal("item_selected", current);
  469. }
  470. accept_event();
  471. break;
  472. }
  473. }
  474. } else if (p_event->is_action("ui_left")) {
  475. search_string = ""; //any mousepress cancels
  476. if (current % current_columns != 0) {
  477. set_current(current - 1);
  478. ensure_current_is_visible();
  479. if (select_mode == SELECT_SINGLE) {
  480. emit_signal("item_selected", current);
  481. }
  482. accept_event();
  483. }
  484. } else if (p_event->is_action("ui_right")) {
  485. search_string = ""; //any mousepress cancels
  486. if (current % current_columns != (current_columns - 1)) {
  487. set_current(current + 1);
  488. ensure_current_is_visible();
  489. if (select_mode == SELECT_SINGLE) {
  490. emit_signal("item_selected", current);
  491. }
  492. accept_event();
  493. }
  494. } else if (p_event->is_action("ui_cancel")) {
  495. search_string = "";
  496. } else if (p_event->is_action("ui_select")) {
  497. if (select_mode == SELECT_MULTI && current >= 0 && current < items.size()) {
  498. if (items[current].selectable && !items[current].disabled && !items[current].selected) {
  499. select(current, false);
  500. emit_signal("multi_selected", current, true);
  501. } else if (items[current].selected) {
  502. unselect(current);
  503. emit_signal("multi_selected", current, false);
  504. }
  505. }
  506. } else if (p_event->is_action("ui_accept")) {
  507. search_string = ""; //any mousepress cance
  508. if (current >= 0 && current < items.size()) {
  509. emit_signal("item_activated", current);
  510. }
  511. } else {
  512. Ref<InputEventKey> k = p_event;
  513. if (k.is_valid() && k->get_unicode()) {
  514. uint64_t now = OS::get_singleton()->get_ticks_msec();
  515. uint64_t diff = now - search_time_msec;
  516. uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000));
  517. search_time_msec = now;
  518. if (diff > max_interval) {
  519. search_string = "";
  520. }
  521. search_string += String::chr(k->get_unicode());
  522. for (int i = 0; i < items.size(); i++) {
  523. if (items[i].text.begins_with(search_string)) {
  524. set_current(i);
  525. ensure_current_is_visible();
  526. if (select_mode == SELECT_SINGLE) {
  527. emit_signal("item_selected", current);
  528. }
  529. break;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. void ItemList::ensure_current_is_visible() {
  537. ensure_selected_visible = true;
  538. update();
  539. }
  540. static Rect2 _adjust_to_max_size(Size2 p_size, Size2 p_max_size) {
  541. Size2 size = p_max_size;
  542. int tex_width = p_size.width * size.height / p_size.height;
  543. int tex_height = size.height;
  544. if (tex_width > size.width) {
  545. tex_width = size.width;
  546. tex_height = p_size.height * tex_width / p_size.width;
  547. }
  548. int ofs_x = (size.width - tex_width) / 2;
  549. int ofs_y = (size.height - tex_height) / 2;
  550. return Rect2(ofs_x, ofs_y, tex_width, tex_height);
  551. }
  552. void ItemList::_notification(int p_what) {
  553. if (p_what == NOTIFICATION_RESIZED) {
  554. shape_changed = true;
  555. update();
  556. }
  557. if (p_what == NOTIFICATION_DRAW) {
  558. Ref<StyleBox> bg = get_stylebox("bg");
  559. int mw = scroll_bar->get_minimum_size().x;
  560. scroll_bar->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -mw);
  561. scroll_bar->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  562. scroll_bar->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, bg->get_margin(MARGIN_TOP));
  563. scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -bg->get_margin(MARGIN_BOTTOM));
  564. Size2 size = get_size();
  565. int width = size.width - bg->get_minimum_size().width;
  566. if (scroll_bar->is_visible()) {
  567. width -= mw + bg->get_margin(MARGIN_RIGHT);
  568. }
  569. draw_style_box(bg, Rect2(Point2(), size));
  570. int hseparation = get_constant("hseparation");
  571. int vseparation = get_constant("vseparation");
  572. int icon_margin = get_constant("icon_margin");
  573. int line_separation = get_constant("line_separation");
  574. Ref<StyleBox> sbsel = has_focus() ? get_stylebox("selected_focus") : get_stylebox("selected");
  575. Ref<StyleBox> cursor = has_focus() ? get_stylebox("cursor") : get_stylebox("cursor_unfocused");
  576. Ref<Font> font = get_font("font");
  577. Color guide_color = get_color("guide_color");
  578. Color font_color = get_color("font_color");
  579. Color font_color_selected = get_color("font_color_selected");
  580. int font_height = font->get_height();
  581. Vector<int> line_size_cache;
  582. Vector<int> line_limit_cache;
  583. if (max_text_lines) {
  584. line_size_cache.resize(max_text_lines);
  585. line_limit_cache.resize(max_text_lines);
  586. }
  587. if (has_focus()) {
  588. VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), true);
  589. draw_style_box(get_stylebox("bg_focus"), Rect2(Point2(), size));
  590. VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(), false);
  591. }
  592. if (shape_changed) {
  593. float max_column_width = 0;
  594. //1- compute item minimum sizes
  595. for (int i = 0; i < items.size(); i++) {
  596. Size2 minsize;
  597. if (items[i].icon.is_valid()) {
  598. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  599. minsize = fixed_icon_size * icon_scale;
  600. } else {
  601. minsize = items[i].get_icon_size() * icon_scale;
  602. }
  603. if (items[i].text != "") {
  604. if (icon_mode == ICON_MODE_TOP) {
  605. minsize.y += icon_margin;
  606. } else {
  607. minsize.x += icon_margin;
  608. }
  609. }
  610. }
  611. if (items[i].text != "") {
  612. Size2 s = font->get_string_size(items[i].text);
  613. //s.width=MIN(s.width,fixed_column_width);
  614. if (icon_mode == ICON_MODE_TOP) {
  615. minsize.x = MAX(minsize.x, s.width);
  616. if (max_text_lines > 0) {
  617. minsize.y += (font_height + line_separation) * max_text_lines;
  618. } else {
  619. minsize.y += s.height;
  620. }
  621. } else {
  622. minsize.y = MAX(minsize.y, s.height);
  623. minsize.x += s.width;
  624. }
  625. }
  626. if (fixed_column_width > 0)
  627. minsize.x = fixed_column_width;
  628. max_column_width = MAX(max_column_width, minsize.x);
  629. // elements need to adapt to the selected size
  630. minsize.y += vseparation;
  631. minsize.x += hseparation;
  632. items[i].rect_cache.size = minsize;
  633. items[i].min_rect_cache.size = minsize;
  634. }
  635. int fit_size = size.x - bg->get_minimum_size().width - mw;
  636. //2-attempt best fit
  637. current_columns = 0x7FFFFFFF;
  638. if (max_columns > 0)
  639. current_columns = max_columns;
  640. while (true) {
  641. //repeat util all fits
  642. bool all_fit = true;
  643. Vector2 ofs;
  644. int col = 0;
  645. int max_h = 0;
  646. separators.clear();
  647. for (int i = 0; i < items.size(); i++) {
  648. if (current_columns > 1 && items[i].rect_cache.size.width + ofs.x > fit_size) {
  649. //went past
  650. current_columns = MAX(col, 1);
  651. all_fit = false;
  652. break;
  653. }
  654. if (same_column_width)
  655. items[i].rect_cache.size.x = max_column_width;
  656. items[i].rect_cache.position = ofs;
  657. max_h = MAX(max_h, items[i].rect_cache.size.y);
  658. ofs.x += items[i].rect_cache.size.x + hseparation;
  659. col++;
  660. if (col == current_columns) {
  661. if (i < items.size() - 1)
  662. separators.push_back(ofs.y + max_h + vseparation / 2);
  663. for (int j = i; j >= 0 && col > 0; j--, col--) {
  664. items[j].rect_cache.size.y = max_h;
  665. }
  666. ofs.x = 0;
  667. ofs.y += max_h + vseparation;
  668. col = 0;
  669. max_h = 0;
  670. }
  671. }
  672. for (int j = items.size() - 1; j >= 0 && col > 0; j--, col--) {
  673. items[j].rect_cache.size.y = max_h;
  674. }
  675. if (all_fit) {
  676. float page = size.height - bg->get_minimum_size().height;
  677. float max = MAX(page, ofs.y + max_h);
  678. if (auto_height)
  679. auto_height_value = ofs.y + max_h + bg->get_minimum_size().height;
  680. scroll_bar->set_max(max);
  681. scroll_bar->set_page(page);
  682. if (max <= page) {
  683. scroll_bar->set_value(0);
  684. scroll_bar->hide();
  685. } else {
  686. scroll_bar->show();
  687. }
  688. break;
  689. }
  690. }
  691. shape_changed = false;
  692. }
  693. //ensure_selected_visible needs to be checked before we draw the list.
  694. if (ensure_selected_visible && current >= 0 && current <= items.size()) {
  695. Rect2 r = items[current].rect_cache;
  696. int from = scroll_bar->get_value();
  697. int to = from + scroll_bar->get_page();
  698. if (r.position.y < from) {
  699. scroll_bar->set_value(r.position.y);
  700. } else if (r.position.y + r.size.y > to) {
  701. scroll_bar->set_value(r.position.y + r.size.y - (to - from));
  702. }
  703. }
  704. ensure_selected_visible = false;
  705. Vector2 base_ofs = bg->get_offset();
  706. base_ofs.y -= int(scroll_bar->get_value());
  707. Rect2 clip(Point2(), size - bg->get_minimum_size() + Vector2(0, scroll_bar->get_value()));
  708. for (int i = 0; i < items.size(); i++) {
  709. Rect2 rcache = items[i].rect_cache;
  710. if (!clip.intersects(rcache))
  711. continue;
  712. if (current_columns == 1) {
  713. rcache.size.width = width - rcache.position.x;
  714. }
  715. if (items[i].selected) {
  716. Rect2 r = rcache;
  717. r.position += base_ofs;
  718. r.position.y -= vseparation / 2;
  719. r.size.y += vseparation;
  720. r.position.x -= hseparation / 2;
  721. r.size.x += hseparation;
  722. draw_style_box(sbsel, r);
  723. }
  724. if (items[i].custom_bg.a > 0.001) {
  725. Rect2 r = rcache;
  726. r.position += base_ofs;
  727. // Size rect to make the align the temperature colors
  728. r.position.y -= vseparation / 2;
  729. r.size.y += vseparation;
  730. r.position.x -= hseparation / 2;
  731. r.size.x += hseparation;
  732. draw_rect(r, items[i].custom_bg);
  733. }
  734. Vector2 text_ofs;
  735. if (items[i].icon.is_valid()) {
  736. Size2 icon_size;
  737. //= _adjust_to_max_size(items[i].get_icon_size(),fixed_icon_size) * icon_scale;
  738. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  739. icon_size = fixed_icon_size * icon_scale;
  740. } else {
  741. icon_size = items[i].get_icon_size() * icon_scale;
  742. }
  743. Vector2 icon_ofs;
  744. Point2 pos = items[i].rect_cache.position + icon_ofs + base_ofs;
  745. if (icon_mode == ICON_MODE_TOP) {
  746. pos.x += Math::floor((items[i].rect_cache.size.width - icon_size.width) / 2);
  747. pos.y += MIN(
  748. Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2),
  749. items[i].rect_cache.size.height - items[i].min_rect_cache.size.height);
  750. text_ofs.y = icon_size.height + icon_margin;
  751. text_ofs.y += items[i].rect_cache.size.height - items[i].min_rect_cache.size.height;
  752. } else {
  753. pos.y += Math::floor((items[i].rect_cache.size.height - icon_size.height) / 2);
  754. text_ofs.x = icon_size.width + icon_margin;
  755. }
  756. Rect2 draw_rect = Rect2(pos, icon_size);
  757. if (fixed_icon_size.x > 0 && fixed_icon_size.y > 0) {
  758. Rect2 adj = _adjust_to_max_size(items[i].get_icon_size() * icon_scale, icon_size);
  759. draw_rect.position += adj.position;
  760. draw_rect.size = adj.size;
  761. }
  762. Color modulate = Color(1, 1, 1, 1);
  763. if (items[i].disabled)
  764. modulate.a *= 0.5;
  765. if (items[i].icon_region.has_no_area())
  766. draw_texture_rect(items[i].icon, draw_rect, false, modulate);
  767. else
  768. draw_texture_rect_region(items[i].icon, draw_rect, items[i].icon_region, modulate);
  769. }
  770. if (items[i].tag_icon.is_valid()) {
  771. draw_texture(items[i].tag_icon, items[i].rect_cache.position + base_ofs);
  772. }
  773. if (items[i].text != "") {
  774. int max_len = -1;
  775. Vector2 size = font->get_string_size(items[i].text);
  776. if (fixed_column_width)
  777. max_len = fixed_column_width;
  778. else if (same_column_width)
  779. max_len = items[i].rect_cache.size.x;
  780. else
  781. max_len = size.x;
  782. Color modulate = items[i].selected ? font_color_selected : (items[i].custom_fg != Color() ? items[i].custom_fg : font_color);
  783. if (items[i].disabled)
  784. modulate.a *= 0.5;
  785. if (icon_mode == ICON_MODE_TOP && max_text_lines > 0) {
  786. int ss = items[i].text.length();
  787. float ofs = 0;
  788. int line = 0;
  789. for (int j = 0; j <= ss; j++) {
  790. int cs = j < ss ? font->get_char_size(items[i].text[j], items[i].text[j + 1]).x : 0;
  791. if (ofs + cs > max_len || j == ss) {
  792. line_limit_cache[line] = j;
  793. line_size_cache[line] = ofs;
  794. line++;
  795. ofs = 0;
  796. if (line >= max_text_lines)
  797. break;
  798. } else {
  799. ofs += cs;
  800. }
  801. }
  802. line = 0;
  803. ofs = 0;
  804. text_ofs.y += font->get_ascent();
  805. text_ofs = text_ofs.floor();
  806. text_ofs += base_ofs;
  807. text_ofs += items[i].rect_cache.position;
  808. for (int j = 0; j < ss; j++) {
  809. if (j == line_limit_cache[line]) {
  810. line++;
  811. ofs = 0;
  812. if (line >= max_text_lines)
  813. break;
  814. }
  815. ofs += font->draw_char(get_canvas_item(), text_ofs + Vector2(ofs + (max_len - line_size_cache[line]) / 2, line * (font_height + line_separation)).floor(), items[i].text[j], items[i].text[j + 1], modulate);
  816. }
  817. //special multiline mode
  818. } else {
  819. if (fixed_column_width > 0)
  820. size.x = MIN(size.x, fixed_column_width);
  821. if (icon_mode == ICON_MODE_TOP) {
  822. text_ofs.x += (items[i].rect_cache.size.width - size.x) / 2;
  823. } else {
  824. text_ofs.y += (items[i].rect_cache.size.height - size.y) / 2;
  825. }
  826. text_ofs.y += font->get_ascent();
  827. text_ofs = text_ofs.floor();
  828. text_ofs += base_ofs;
  829. text_ofs += items[i].rect_cache.position;
  830. draw_string(font, text_ofs, items[i].text, modulate, max_len + 1);
  831. }
  832. }
  833. if (select_mode == SELECT_MULTI && i == current) {
  834. Rect2 r = rcache;
  835. r.position += base_ofs;
  836. r.position.y -= vseparation / 2;
  837. r.size.y += vseparation;
  838. r.position.x -= hseparation / 2;
  839. r.size.x += hseparation;
  840. draw_style_box(cursor, r);
  841. }
  842. }
  843. for (int i = 0; i < separators.size(); i++) {
  844. draw_line(Vector2(bg->get_margin(MARGIN_LEFT), base_ofs.y + separators[i]), Vector2(size.width - bg->get_margin(MARGIN_RIGHT), base_ofs.y + separators[i]), guide_color);
  845. }
  846. }
  847. }
  848. void ItemList::_scroll_changed(double) {
  849. update();
  850. }
  851. int ItemList::get_item_at_position(const Point2 &p_pos, bool p_exact) const {
  852. Vector2 pos = p_pos;
  853. Ref<StyleBox> bg = get_stylebox("bg");
  854. pos -= bg->get_offset();
  855. pos.y += scroll_bar->get_value();
  856. int closest = -1;
  857. int closest_dist = 0x7FFFFFFF;
  858. for (int i = 0; i < items.size(); i++) {
  859. Rect2 rc = items[i].rect_cache;
  860. if (i % current_columns == current_columns - 1) {
  861. rc.size.width = get_size().width; //not right but works
  862. }
  863. if (rc.has_point(pos)) {
  864. closest = i;
  865. break;
  866. }
  867. float dist = rc.distance_to(pos);
  868. if (!p_exact && dist < closest_dist) {
  869. closest = i;
  870. closest_dist = dist;
  871. }
  872. }
  873. return closest;
  874. }
  875. bool ItemList::is_pos_at_end_of_items(const Point2 &p_pos) const {
  876. if (items.empty())
  877. return true;
  878. Vector2 pos = p_pos;
  879. Ref<StyleBox> bg = get_stylebox("bg");
  880. pos -= bg->get_offset();
  881. pos.y += scroll_bar->get_value();
  882. Rect2 endrect = items[items.size() - 1].rect_cache;
  883. return (pos.y > endrect.position.y + endrect.size.y);
  884. }
  885. String ItemList::get_tooltip(const Point2 &p_pos) const {
  886. int closest = get_item_at_position(p_pos);
  887. if (closest != -1) {
  888. if (!items[closest].tooltip_enabled) {
  889. return "";
  890. }
  891. if (items[closest].tooltip != "") {
  892. return items[closest].tooltip;
  893. }
  894. if (items[closest].text != "") {
  895. return items[closest].text;
  896. }
  897. }
  898. return Control::get_tooltip(p_pos);
  899. }
  900. void ItemList::sort_items_by_text() {
  901. items.sort();
  902. update();
  903. shape_changed = true;
  904. if (select_mode == SELECT_SINGLE) {
  905. for (int i = 0; i < items.size(); i++) {
  906. if (items[i].selected) {
  907. select(i);
  908. return;
  909. }
  910. }
  911. }
  912. }
  913. int ItemList::find_metadata(const Variant &p_metadata) const {
  914. for (int i = 0; i < items.size(); i++) {
  915. if (items[i].metadata == p_metadata) {
  916. return i;
  917. }
  918. }
  919. return -1;
  920. }
  921. void ItemList::set_allow_rmb_select(bool p_allow) {
  922. allow_rmb_select = p_allow;
  923. }
  924. bool ItemList::get_allow_rmb_select() const {
  925. return allow_rmb_select;
  926. }
  927. void ItemList::set_icon_scale(real_t p_scale) {
  928. icon_scale = p_scale;
  929. }
  930. real_t ItemList::get_icon_scale() const {
  931. return icon_scale;
  932. }
  933. Vector<int> ItemList::get_selected_items() {
  934. Vector<int> selected;
  935. for (int i = 0; i < items.size(); i++) {
  936. if (items[i].selected) {
  937. selected.push_back(i);
  938. if (this->select_mode == SELECT_SINGLE) {
  939. break;
  940. }
  941. }
  942. }
  943. return selected;
  944. }
  945. void ItemList::_set_items(const Array &p_items) {
  946. ERR_FAIL_COND(p_items.size() % 3);
  947. clear();
  948. for (int i = 0; i < p_items.size(); i += 3) {
  949. String text = p_items[i + 0];
  950. Ref<Texture> icon = p_items[i + 1];
  951. bool disabled = p_items[i + 2];
  952. int idx = get_item_count();
  953. add_item(text, icon);
  954. set_item_disabled(idx, disabled);
  955. }
  956. }
  957. Array ItemList::_get_items() const {
  958. Array items;
  959. for (int i = 0; i < get_item_count(); i++) {
  960. items.push_back(get_item_text(i));
  961. items.push_back(get_item_icon(i));
  962. items.push_back(is_item_disabled(i));
  963. }
  964. return items;
  965. }
  966. Size2 ItemList::get_minimum_size() const {
  967. if (auto_height) {
  968. return Size2(0, auto_height_value);
  969. }
  970. return Size2();
  971. }
  972. void ItemList::set_auto_height(bool p_enable) {
  973. auto_height = p_enable;
  974. shape_changed = true;
  975. update();
  976. }
  977. bool ItemList::has_auto_height() const {
  978. return auto_height;
  979. }
  980. void ItemList::_bind_methods() {
  981. ClassDB::bind_method(D_METHOD("add_item", "text", "icon", "selectable"), &ItemList::add_item, DEFVAL(Variant()), DEFVAL(true));
  982. ClassDB::bind_method(D_METHOD("add_icon_item", "icon", "selectable"), &ItemList::add_icon_item, DEFVAL(true));
  983. ClassDB::bind_method(D_METHOD("set_item_text", "idx", "text"), &ItemList::set_item_text);
  984. ClassDB::bind_method(D_METHOD("get_item_text", "idx"), &ItemList::get_item_text);
  985. ClassDB::bind_method(D_METHOD("set_item_icon", "idx", "icon"), &ItemList::set_item_icon);
  986. ClassDB::bind_method(D_METHOD("get_item_icon", "idx"), &ItemList::get_item_icon);
  987. ClassDB::bind_method(D_METHOD("set_item_icon_region", "idx", "rect"), &ItemList::set_item_icon_region);
  988. ClassDB::bind_method(D_METHOD("get_item_icon_region", "idx"), &ItemList::get_item_icon_region);
  989. ClassDB::bind_method(D_METHOD("set_item_selectable", "idx", "selectable"), &ItemList::set_item_selectable);
  990. ClassDB::bind_method(D_METHOD("is_item_selectable", "idx"), &ItemList::is_item_selectable);
  991. ClassDB::bind_method(D_METHOD("set_item_disabled", "idx", "disabled"), &ItemList::set_item_disabled);
  992. ClassDB::bind_method(D_METHOD("is_item_disabled", "idx"), &ItemList::is_item_disabled);
  993. ClassDB::bind_method(D_METHOD("set_item_metadata", "idx", "metadata"), &ItemList::set_item_metadata);
  994. ClassDB::bind_method(D_METHOD("get_item_metadata", "idx"), &ItemList::get_item_metadata);
  995. ClassDB::bind_method(D_METHOD("set_item_custom_bg_color", "idx", "custom_bg_color"), &ItemList::set_item_custom_bg_color);
  996. ClassDB::bind_method(D_METHOD("get_item_custom_bg_color", "idx"), &ItemList::get_item_custom_bg_color);
  997. ClassDB::bind_method(D_METHOD("set_item_tooltip_enabled", "idx", "enable"), &ItemList::set_item_tooltip_enabled);
  998. ClassDB::bind_method(D_METHOD("is_item_tooltip_enabled", "idx"), &ItemList::is_item_tooltip_enabled);
  999. ClassDB::bind_method(D_METHOD("set_item_tooltip", "idx", "tooltip"), &ItemList::set_item_tooltip);
  1000. ClassDB::bind_method(D_METHOD("get_item_tooltip", "idx"), &ItemList::get_item_tooltip);
  1001. ClassDB::bind_method(D_METHOD("select", "idx", "single"), &ItemList::select, DEFVAL(true));
  1002. ClassDB::bind_method(D_METHOD("unselect", "idx"), &ItemList::unselect);
  1003. ClassDB::bind_method(D_METHOD("is_selected", "idx"), &ItemList::is_selected);
  1004. ClassDB::bind_method(D_METHOD("get_selected_items"), &ItemList::get_selected_items);
  1005. ClassDB::bind_method(D_METHOD("get_item_count"), &ItemList::get_item_count);
  1006. ClassDB::bind_method(D_METHOD("remove_item", "idx"), &ItemList::remove_item);
  1007. ClassDB::bind_method(D_METHOD("clear"), &ItemList::clear);
  1008. ClassDB::bind_method(D_METHOD("sort_items_by_text"), &ItemList::sort_items_by_text);
  1009. ClassDB::bind_method(D_METHOD("set_fixed_column_width", "width"), &ItemList::set_fixed_column_width);
  1010. ClassDB::bind_method(D_METHOD("get_fixed_column_width"), &ItemList::get_fixed_column_width);
  1011. ClassDB::bind_method(D_METHOD("set_same_column_width", "enable"), &ItemList::set_same_column_width);
  1012. ClassDB::bind_method(D_METHOD("is_same_column_width"), &ItemList::is_same_column_width);
  1013. ClassDB::bind_method(D_METHOD("set_max_text_lines", "lines"), &ItemList::set_max_text_lines);
  1014. ClassDB::bind_method(D_METHOD("get_max_text_lines"), &ItemList::get_max_text_lines);
  1015. ClassDB::bind_method(D_METHOD("set_max_columns", "amount"), &ItemList::set_max_columns);
  1016. ClassDB::bind_method(D_METHOD("get_max_columns"), &ItemList::get_max_columns);
  1017. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &ItemList::set_select_mode);
  1018. ClassDB::bind_method(D_METHOD("get_select_mode"), &ItemList::get_select_mode);
  1019. ClassDB::bind_method(D_METHOD("set_icon_mode", "mode"), &ItemList::set_icon_mode);
  1020. ClassDB::bind_method(D_METHOD("get_icon_mode"), &ItemList::get_icon_mode);
  1021. ClassDB::bind_method(D_METHOD("set_fixed_icon_size", "size"), &ItemList::set_fixed_icon_size);
  1022. ClassDB::bind_method(D_METHOD("get_fixed_icon_size"), &ItemList::get_fixed_icon_size);
  1023. ClassDB::bind_method(D_METHOD("set_icon_scale", "scale"), &ItemList::set_icon_scale);
  1024. ClassDB::bind_method(D_METHOD("get_icon_scale"), &ItemList::get_icon_scale);
  1025. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &ItemList::set_allow_rmb_select);
  1026. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &ItemList::get_allow_rmb_select);
  1027. ClassDB::bind_method(D_METHOD("set_auto_height", "enable"), &ItemList::set_auto_height);
  1028. ClassDB::bind_method(D_METHOD("has_auto_height"), &ItemList::has_auto_height);
  1029. ClassDB::bind_method(D_METHOD("get_item_at_position", "position", "exact"), &ItemList::get_item_at_position, DEFVAL(false));
  1030. ClassDB::bind_method(D_METHOD("ensure_current_is_visible"), &ItemList::ensure_current_is_visible);
  1031. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ItemList::get_v_scroll);
  1032. ClassDB::bind_method(D_METHOD("_scroll_changed"), &ItemList::_scroll_changed);
  1033. ClassDB::bind_method(D_METHOD("_gui_input"), &ItemList::_gui_input);
  1034. ClassDB::bind_method(D_METHOD("_set_items"), &ItemList::_set_items);
  1035. ClassDB::bind_method(D_METHOD("_get_items"), &ItemList::_get_items);
  1036. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "items", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_items", "_get_items");
  1037. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Multi"), "set_select_mode", "get_select_mode");
  1038. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  1039. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "max_text_lines"), "set_max_text_lines", "get_max_text_lines");
  1040. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "auto_height"), "set_auto_height", "has_auto_height");
  1041. ADD_GROUP("Columns", "");
  1042. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "max_columns"), "set_max_columns", "get_max_columns");
  1043. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "same_column_width"), "set_same_column_width", "is_same_column_width");
  1044. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "fixed_column_width"), "set_fixed_column_width", "get_fixed_column_width");
  1045. ADD_GROUP("Icon", "");
  1046. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_mode", PROPERTY_HINT_ENUM, "Top,Left"), "set_icon_mode", "get_icon_mode");
  1047. ADD_PROPERTYNO(PropertyInfo(Variant::REAL, "icon_scale"), "set_icon_scale", "get_icon_scale");
  1048. BIND_ENUM_CONSTANT(ICON_MODE_TOP);
  1049. BIND_ENUM_CONSTANT(ICON_MODE_LEFT);
  1050. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  1051. BIND_ENUM_CONSTANT(SELECT_MULTI);
  1052. ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index")));
  1053. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::VECTOR2, "at_position")));
  1054. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "selected")));
  1055. ADD_SIGNAL(MethodInfo("item_activated", PropertyInfo(Variant::INT, "index")));
  1056. GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000);
  1057. }
  1058. ItemList::ItemList() {
  1059. current = -1;
  1060. select_mode = SELECT_SINGLE;
  1061. icon_mode = ICON_MODE_LEFT;
  1062. fixed_column_width = 0;
  1063. same_column_width = false;
  1064. max_text_lines = 1;
  1065. max_columns = 1;
  1066. auto_height = false;
  1067. auto_height_value = 0.0f;
  1068. scroll_bar = memnew(VScrollBar);
  1069. add_child(scroll_bar);
  1070. shape_changed = true;
  1071. scroll_bar->connect("value_changed", this, "_scroll_changed");
  1072. set_focus_mode(FOCUS_ALL);
  1073. current_columns = 1;
  1074. search_time_msec = 0;
  1075. ensure_selected_visible = false;
  1076. defer_select_single = -1;
  1077. allow_rmb_select = false;
  1078. icon_scale = 1.0f;
  1079. set_clip_contents(true);
  1080. }
  1081. ItemList::~ItemList() {
  1082. }