item_list.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. #include "item_list.h"
  2. #include "os/os.h"
  3. #include "globals.h"
  4. void ItemList::add_item(const String& p_item,const Ref<Texture>& p_texture,bool p_selectable) {
  5. Item item;
  6. item.icon=p_texture;
  7. item.text=p_item;
  8. item.selectable=p_selectable;
  9. item.selected=false;
  10. item.disabled=false;
  11. item.custom_bg=Color(0,0,0,0);
  12. items.push_back(item);
  13. update();
  14. shape_changed=true;
  15. }
  16. void ItemList::add_icon_item(const Ref<Texture>& p_item,bool p_selectable){
  17. Item item;
  18. item.icon=p_item;
  19. //item.text=p_item;
  20. item.selectable=p_selectable;
  21. item.selected=false;
  22. item.disabled=false;
  23. item.custom_bg=Color(0,0,0,0);
  24. items.push_back(item);
  25. update();
  26. shape_changed=true;
  27. }
  28. void ItemList::set_item_text(int p_idx,const String& p_text){
  29. ERR_FAIL_INDEX(p_idx,items.size());
  30. items[p_idx].text=p_text;
  31. update();
  32. shape_changed=true;
  33. }
  34. String ItemList::get_item_text(int p_idx) const{
  35. ERR_FAIL_INDEX_V(p_idx,items.size(),String());
  36. return items[p_idx].text;
  37. }
  38. void ItemList::set_item_tooltip(int p_idx,const String& p_tooltip){
  39. ERR_FAIL_INDEX(p_idx,items.size());
  40. items[p_idx].tooltip=p_tooltip;
  41. update();
  42. shape_changed=true;
  43. }
  44. String ItemList::get_item_tooltip(int p_idx) const{
  45. ERR_FAIL_INDEX_V(p_idx,items.size(),String());
  46. return items[p_idx].tooltip;
  47. }
  48. void ItemList::set_item_icon(int p_idx,const Ref<Texture>& p_icon){
  49. ERR_FAIL_INDEX(p_idx,items.size());
  50. items[p_idx].icon=p_icon;
  51. update();
  52. shape_changed=true;
  53. }
  54. Ref<Texture> ItemList::get_item_icon(int p_idx) const{
  55. ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<Texture>());
  56. return items[p_idx].icon;
  57. }
  58. void ItemList::set_item_custom_bg_color(int p_idx,const Color& p_custom_bg_color) {
  59. ERR_FAIL_INDEX(p_idx,items.size());
  60. items[p_idx].custom_bg=p_custom_bg_color;
  61. }
  62. Color ItemList::get_item_custom_bg_color(int p_idx) const {
  63. ERR_FAIL_INDEX_V(p_idx,items.size(),Color());
  64. return items[p_idx].custom_bg;
  65. }
  66. void ItemList::set_item_tag_icon(int p_idx,const Ref<Texture>& p_tag_icon){
  67. ERR_FAIL_INDEX(p_idx,items.size());
  68. items[p_idx].tag_icon=p_tag_icon;
  69. update();
  70. shape_changed=true;
  71. }
  72. Ref<Texture> ItemList::get_item_tag_icon(int p_idx) const{
  73. ERR_FAIL_INDEX_V(p_idx,items.size(),Ref<Texture>());
  74. return items[p_idx].tag_icon;
  75. }
  76. void ItemList::set_item_selectable(int p_idx,bool p_selectable){
  77. ERR_FAIL_INDEX(p_idx,items.size());
  78. items[p_idx].selectable=p_selectable;
  79. }
  80. bool ItemList::is_item_selectable(int p_idx) const{
  81. ERR_FAIL_INDEX_V(p_idx,items.size(),false);
  82. return items[p_idx].selectable;
  83. }
  84. void ItemList::set_item_disabled(int p_idx,bool p_disabled){
  85. ERR_FAIL_INDEX(p_idx,items.size());
  86. items[p_idx].disabled=p_disabled;
  87. }
  88. bool ItemList::is_item_disabled(int p_idx) const{
  89. ERR_FAIL_INDEX_V(p_idx,items.size(),false);
  90. return items[p_idx].disabled;
  91. }
  92. void ItemList::set_item_metadata(int p_idx,const Variant& p_metadata){
  93. ERR_FAIL_INDEX(p_idx,items.size());
  94. items[p_idx].metadata=p_metadata;
  95. update();
  96. shape_changed=true;
  97. }
  98. Variant ItemList::get_item_metadata(int p_idx) const{
  99. ERR_FAIL_INDEX_V(p_idx,items.size(),Variant());
  100. return items[p_idx].metadata;
  101. }
  102. void ItemList::select(int p_idx,bool p_single){
  103. ERR_FAIL_INDEX(p_idx,items.size());
  104. if (p_single || select_mode==SELECT_SINGLE) {
  105. if (!items[p_idx].selectable) {
  106. return;
  107. }
  108. for(int i=0;i<items.size();i++) {
  109. items[i].selected=p_idx==i;
  110. }
  111. current=p_idx;
  112. ensure_selected_visible=false;
  113. } else {
  114. if (items[p_idx].selectable) {
  115. items[p_idx].selected=true;
  116. }
  117. }
  118. update();
  119. }
  120. void ItemList::unselect(int p_idx){
  121. ERR_FAIL_INDEX(p_idx,items.size());
  122. if (select_mode!=SELECT_MULTI) {
  123. items[p_idx].selected=false;
  124. current=-1;
  125. } else {
  126. items[p_idx].selected=false;
  127. }
  128. update();
  129. }
  130. bool ItemList::is_selected(int p_idx) const{
  131. ERR_FAIL_INDEX_V(p_idx,items.size(),false);
  132. return items[p_idx].selected;
  133. }
  134. void ItemList::set_current(int p_current) {
  135. ERR_FAIL_INDEX(p_current,items.size());
  136. if (select_mode==SELECT_SINGLE)
  137. select(p_current,true);
  138. else {
  139. current=p_current;
  140. update();
  141. }
  142. }
  143. int ItemList::get_current() const {
  144. return current;
  145. }
  146. void ItemList::move_item(int p_item,int p_to_pos) {
  147. ERR_FAIL_INDEX(p_item,items.size());
  148. ERR_FAIL_INDEX(p_to_pos,items.size()+1);
  149. Item it=items[p_item];
  150. items.remove(p_item);;
  151. if (p_to_pos>p_item) {
  152. p_to_pos--;
  153. }
  154. if (p_to_pos>=items.size()) {
  155. items.push_back(it);
  156. } else {
  157. items.insert(p_to_pos,it);
  158. }
  159. if (current<0) {
  160. //do none
  161. } if (p_item==current) {
  162. current=p_to_pos;
  163. } else if (p_to_pos>p_item && current>p_item && current<p_to_pos) {
  164. current--;
  165. } else if (p_to_pos<p_item && current<p_item && current>p_to_pos) {
  166. current++;
  167. }
  168. update();
  169. }
  170. int ItemList::get_item_count() const{
  171. return items.size();
  172. }
  173. void ItemList::remove_item(int p_idx){
  174. ERR_FAIL_INDEX(p_idx,items.size());
  175. items.remove(p_idx);
  176. update();
  177. shape_changed=true;
  178. defer_select_single=-1;
  179. }
  180. void ItemList::clear(){
  181. items.clear();
  182. current=-1;
  183. ensure_selected_visible=false;
  184. update();
  185. defer_select_single=-1;
  186. }
  187. void ItemList::set_fixed_column_width(int p_size){
  188. ERR_FAIL_COND(p_size<0);
  189. fixed_column_width=p_size;
  190. update();
  191. shape_changed=true;
  192. }
  193. int ItemList::get_fixed_column_width() const{
  194. return fixed_column_width;
  195. }
  196. void ItemList::set_max_text_lines(int p_lines){
  197. ERR_FAIL_COND(p_lines<1);
  198. max_text_lines=p_lines;
  199. update();
  200. shape_changed=true;
  201. }
  202. int ItemList::get_max_text_lines() const{
  203. return max_text_lines;
  204. }
  205. void ItemList::set_max_columns(int p_amount){
  206. ERR_FAIL_COND(p_amount<0);
  207. max_columns=p_amount;
  208. update();
  209. }
  210. int ItemList::get_max_columns() const{
  211. return max_columns;
  212. }
  213. void ItemList::set_select_mode(SelectMode p_mode) {
  214. select_mode=p_mode;
  215. update();
  216. }
  217. ItemList::SelectMode ItemList::get_select_mode() const {
  218. return select_mode;
  219. }
  220. void ItemList::set_icon_mode(IconMode p_mode){
  221. icon_mode=p_mode;
  222. update();
  223. shape_changed=true;
  224. }
  225. ItemList::IconMode ItemList::get_icon_mode() const{
  226. return icon_mode;
  227. }
  228. void ItemList::set_min_icon_size(const Size2& p_size) {
  229. min_icon_size=p_size;
  230. update();
  231. }
  232. Size2 ItemList::get_min_icon_size() const {
  233. return min_icon_size;
  234. }
  235. void ItemList::_input_event(const InputEvent& p_event) {
  236. if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_MOTION) {
  237. defer_select_single=-1;
  238. return;
  239. }
  240. if (defer_select_single>=0 && p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT && !p_event.mouse_button.pressed) {
  241. select(defer_select_single,true);
  242. emit_signal("multi_selected",defer_select_single,true);
  243. defer_select_single=-1;
  244. return;
  245. }
  246. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_LEFT && p_event.mouse_button.pressed) {
  247. const InputEventMouseButton &mb = p_event.mouse_button;
  248. search_string=""; //any mousepress cancels
  249. Vector2 pos(mb.x,mb.y);
  250. Ref<StyleBox> bg = get_stylebox("bg");
  251. pos-=bg->get_offset();
  252. pos.y+=scroll_bar->get_val();
  253. int closest = -1;
  254. int closest_dist=0x7FFFFFFF;
  255. for(int i=0;i<items.size();i++) {
  256. Rect2 rc = items[i].rect_cache;
  257. if (i%current_columns==current_columns-1) {
  258. rc.size.width=get_size().width; //not right but works
  259. }
  260. if (rc.has_point(pos)) {
  261. closest=i;
  262. break;
  263. }
  264. float dist = rc.distance_to(pos);
  265. if (dist<closest_dist) {
  266. closest=i;
  267. closest_dist=dist;
  268. }
  269. }
  270. if (closest!=-1) {
  271. int i = closest;
  272. if (select_mode==SELECT_MULTI && items[i].selected && mb.mod.command) {
  273. unselect(i);
  274. emit_signal("multi_selected",i,false);
  275. } else if (select_mode==SELECT_MULTI && mb.mod.shift && current>=0 && current<items.size() && current!=i) {
  276. int from = current;
  277. int to = i;
  278. if (i<current) {
  279. SWAP(from,to);
  280. }
  281. for(int j=from;j<=to;j++) {
  282. bool selected = !items[j].selected;
  283. select(j,false);
  284. if (selected)
  285. emit_signal("multi_selected",i,true);
  286. }
  287. } else {
  288. if (!mb.doubleclick && !mb.mod.command && select_mode==SELECT_MULTI && items[i].selectable && items[i].selected) {
  289. defer_select_single=i;
  290. return;
  291. }
  292. bool selected = !items[i].selected;
  293. select(i,select_mode==SELECT_SINGLE || !mb.mod.command);
  294. if (selected) {
  295. if (select_mode==SELECT_SINGLE) {
  296. emit_signal("item_selected",i);
  297. } else
  298. emit_signal("multi_selected",i,true);
  299. }
  300. if (/*select_mode==SELECT_SINGLE &&*/ mb.doubleclick) {
  301. emit_signal("item_activated",i);
  302. }
  303. }
  304. return;
  305. }
  306. }
  307. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_UP && p_event.mouse_button.pressed) {
  308. scroll_bar->set_val( scroll_bar->get_val()-scroll_bar->get_page()/8 );
  309. }
  310. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==BUTTON_WHEEL_DOWN && p_event.mouse_button.pressed) {
  311. scroll_bar->set_val( scroll_bar->get_val()+scroll_bar->get_page()/8 );
  312. }
  313. if (p_event.is_pressed() && items.size()>0) {
  314. if (p_event.is_action("ui_up")) {
  315. if (search_string!="") {
  316. uint64_t now = OS::get_singleton()->get_ticks_msec();
  317. uint64_t diff = now-search_time_msec;
  318. if (diff<int(Globals::get_singleton()->get("gui/incr_search_max_interval_msec"))*2) {
  319. for(int i=current-1;i>=0;i--) {
  320. if (items[i].text.begins_with(search_string)) {
  321. set_current(i);
  322. ensure_current_is_visible();
  323. if (select_mode==SELECT_SINGLE) {
  324. emit_signal("item_selected",current);
  325. }
  326. break;
  327. }
  328. }
  329. accept_event();
  330. return;
  331. }
  332. }
  333. if (current>=current_columns) {
  334. set_current(current-current_columns);
  335. ensure_current_is_visible();
  336. if (select_mode==SELECT_SINGLE) {
  337. emit_signal("item_selected",current);
  338. }
  339. accept_event();
  340. }
  341. } else if (p_event.is_action("ui_down")) {
  342. if (search_string!="") {
  343. uint64_t now = OS::get_singleton()->get_ticks_msec();
  344. uint64_t diff = now-search_time_msec;
  345. if (diff<int(Globals::get_singleton()->get("gui/incr_search_max_interval_msec"))*2) {
  346. for(int i=current+1;i<items.size();i++) {
  347. if (items[i].text.begins_with(search_string)) {
  348. set_current(i);
  349. ensure_current_is_visible();
  350. if (select_mode==SELECT_SINGLE) {
  351. emit_signal("item_selected",current);
  352. }
  353. break;
  354. }
  355. }
  356. accept_event();
  357. return;
  358. }
  359. }
  360. if (current<items.size()-current_columns) {
  361. set_current(current+current_columns);
  362. ensure_current_is_visible();
  363. if (select_mode==SELECT_SINGLE) {
  364. emit_signal("item_selected",current);
  365. }
  366. accept_event();
  367. }
  368. } else if (p_event.is_action("ui_page_up")) {
  369. search_string=""; //any mousepress cancels
  370. for(int i=4;i>0;i--) {
  371. if (current-current_columns*i >=0 ) {
  372. set_current( current- current_columns*i);
  373. ensure_current_is_visible();
  374. if (select_mode==SELECT_SINGLE) {
  375. emit_signal("item_selected",current);
  376. }
  377. accept_event();
  378. break;
  379. }
  380. }
  381. } else if (p_event.is_action("ui_page_down")) {
  382. search_string=""; //any mousepress cancels
  383. for(int i=4;i>0;i--) {
  384. if (current+current_columns*i < items.size() ) {
  385. set_current( current+ current_columns*i);
  386. ensure_current_is_visible();
  387. if (select_mode==SELECT_SINGLE) {
  388. emit_signal("item_selected",current);
  389. }
  390. accept_event();
  391. break;
  392. }
  393. }
  394. } else if (p_event.is_action("ui_left")) {
  395. search_string=""; //any mousepress cancels
  396. if (current%current_columns!=0) {
  397. set_current(current-1);
  398. ensure_current_is_visible();
  399. if (select_mode==SELECT_SINGLE) {
  400. emit_signal("item_selected",current);
  401. }
  402. accept_event();
  403. }
  404. } else if (p_event.is_action("ui_right")) {
  405. search_string=""; //any mousepress cancels
  406. if (current%current_columns!=(current_columns-1)) {
  407. set_current(current+1);
  408. ensure_current_is_visible();
  409. if (select_mode==SELECT_SINGLE) {
  410. emit_signal("item_selected",current);
  411. }
  412. accept_event();
  413. }
  414. } else if (p_event.is_action("ui_cancel")) {
  415. search_string="";
  416. } else if (p_event.is_action("ui_select")) {
  417. if (select_mode==SELECT_MULTI && current>=0 && current<items.size()) {
  418. if (items[current].selectable && !items[current].selected) {
  419. select(current,false);
  420. emit_signal("multi_selected",current,true);
  421. } else if (items[current].selected) {
  422. unselect(current);
  423. emit_signal("multi_selected",current,false);
  424. }
  425. }
  426. } else if (p_event.is_action("ui_accept")) {
  427. search_string=""; //any mousepress cance
  428. if (current>=0 && current<items.size()) {
  429. emit_signal("item_activated",current);
  430. }
  431. } else if (p_event.type==InputEvent::KEY) {
  432. if (p_event.key.unicode) {
  433. uint64_t now = OS::get_singleton()->get_ticks_msec();
  434. uint64_t diff = now-search_time_msec;
  435. uint64_t max_interval = uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000));
  436. search_time_msec = now;
  437. if (diff>max_interval) {
  438. search_string="";
  439. }
  440. search_string+=String::chr(p_event.key.unicode);
  441. for(int i=0;i<items.size();i++) {
  442. if (items[i].text.begins_with(search_string)) {
  443. set_current(i);
  444. ensure_current_is_visible();
  445. if (select_mode==SELECT_SINGLE) {
  446. emit_signal("item_selected",current);
  447. }
  448. break;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. }
  455. void ItemList::ensure_current_is_visible() {
  456. ensure_selected_visible=true;
  457. update();
  458. }
  459. void ItemList::_notification(int p_what) {
  460. if (p_what==NOTIFICATION_RESIZED) {
  461. shape_changed=true;
  462. update();
  463. }
  464. if (p_what==NOTIFICATION_DRAW) {
  465. VS::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
  466. Ref<StyleBox> bg = get_stylebox("bg");
  467. int mw = scroll_bar->get_minimum_size().x;
  468. scroll_bar->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,mw+bg->get_margin(MARGIN_RIGHT));
  469. scroll_bar->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,bg->get_margin(MARGIN_RIGHT));
  470. scroll_bar->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,bg->get_margin(MARGIN_TOP));
  471. scroll_bar->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,bg->get_margin(MARGIN_BOTTOM));
  472. Size2 size = get_size();
  473. float page = size.height-bg->get_minimum_size().height;
  474. int width = size.width - mw - bg->get_minimum_size().width;
  475. scroll_bar->set_page(page);
  476. draw_style_box(bg,Rect2(Point2(),size));
  477. int hseparation = get_constant("hseparation");
  478. int vseparation = get_constant("vseparation");
  479. int icon_margin = get_constant("icon_margin");
  480. int line_separation = get_constant("line_separation");
  481. Ref<StyleBox> sbsel = has_focus()?get_stylebox("selected_focus"):get_stylebox("selected");
  482. Ref<StyleBox> cursor = has_focus()?get_stylebox("cursor"):get_stylebox("cursor_unfocused");
  483. Ref<Font> font = get_font("font");
  484. Color guide_color = get_color("guide_color");
  485. Color font_color = get_color("font_color");
  486. Color font_color_selected = get_color("font_color_selected");
  487. int font_height = font->get_height();
  488. Vector<int> line_size_cache;
  489. Vector<int> line_limit_cache;
  490. if (max_text_lines) {
  491. line_size_cache.resize(max_text_lines);
  492. line_limit_cache.resize(max_text_lines);
  493. }
  494. if (has_focus()) {
  495. VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(),true);
  496. draw_style_box(get_stylebox("bg_focus"),Rect2(Point2(),size));
  497. VisualServer::get_singleton()->canvas_item_add_clip_ignore(get_canvas_item(),false);
  498. }
  499. if (shape_changed) {
  500. //1- compute item minimum sizes
  501. for(int i=0;i<items.size();i++) {
  502. Size2 minsize;
  503. if (items[i].icon.is_valid()) {
  504. minsize=items[i].icon->get_size();
  505. if (min_icon_size.x!=0)
  506. minsize.x = MAX(minsize.x,min_icon_size.x);
  507. if (min_icon_size.y!=0)
  508. minsize.y = MAX(minsize.y,min_icon_size.y);
  509. if (items[i].text!="") {
  510. if (icon_mode==ICON_MODE_TOP) {
  511. minsize.y+=icon_margin;
  512. } else {
  513. minsize.x+=icon_margin;
  514. }
  515. }
  516. }
  517. if (items[i].text!="") {
  518. Size2 s = font->get_string_size(items[i].text);
  519. //s.width=MIN(s.width,fixed_column_width);
  520. if (icon_mode==ICON_MODE_TOP) {
  521. minsize.x=MAX(minsize.x,s.width);
  522. if (max_text_lines>0) {
  523. minsize.y+=(font_height+line_separation)*max_text_lines;
  524. } else {
  525. minsize.y+=s.height;
  526. }
  527. } else {
  528. minsize.y=MAX(minsize.y,s.height);
  529. minsize.x+=s.width;
  530. }
  531. }
  532. items[i].rect_cache.size=minsize;
  533. if (fixed_column_width>0)
  534. items[i].rect_cache.size.x=fixed_column_width;
  535. }
  536. int fit_size = size.x - bg->get_minimum_size().width - mw;
  537. //2-attempt best fit
  538. current_columns = 0x7FFFFFFF;
  539. if (max_columns>0)
  540. current_columns=max_columns;
  541. while(true) {
  542. //repeat util all fits
  543. //print_line("try with "+itos(current_columns));
  544. bool all_fit=true;
  545. Vector2 ofs;
  546. int col=0;
  547. int max_h=0;
  548. separators.clear();;
  549. for(int i=0;i<items.size();i++) {
  550. if (current_columns>1 && items[i].rect_cache.size.width+ofs.x > fit_size) {
  551. //went past
  552. current_columns=MAX(col,1);
  553. all_fit=false;
  554. break;
  555. }
  556. items[i].rect_cache.pos=ofs;
  557. max_h=MAX(max_h,items[i].rect_cache.size.y);
  558. ofs.x+=items[i].rect_cache.size.x;
  559. //print_line("item "+itos(i)+" ofs "+rtos(items[i].rect_cache.size.x));
  560. if (col>0)
  561. ofs.x+=hseparation;
  562. col++;
  563. if (col==current_columns) {
  564. if (i<items.size()-1)
  565. separators.push_back(ofs.y+max_h+vseparation/2);
  566. ofs.x=0;
  567. ofs.y+=max_h+vseparation;
  568. col=0;
  569. max_h=0;
  570. }
  571. }
  572. if (all_fit) {
  573. float max = MAX(page,ofs.y+max_h);
  574. scroll_bar->set_max(max);
  575. //print_line("max: "+rtos(max)+" page "+rtos(page));
  576. if (max<=page) {
  577. scroll_bar->set_val(0);
  578. scroll_bar->hide();
  579. } else {
  580. scroll_bar->show();
  581. }
  582. break;
  583. }
  584. }
  585. shape_changed=false;
  586. }
  587. Vector2 base_ofs = bg->get_offset();
  588. base_ofs.y-=int(scroll_bar->get_val());
  589. Rect2 clip(Point2(),size-bg->get_minimum_size()+Vector2(0,scroll_bar->get_val()));
  590. for(int i=0;i<items.size();i++) {
  591. Rect2 rcache = items[i].rect_cache;
  592. if (!clip.intersects(rcache))
  593. continue;
  594. if (current_columns==1) {
  595. rcache.size.width = width-rcache.pos.x;
  596. }
  597. Rect2 r=rcache;
  598. r.pos+=base_ofs;
  599. // Use stylebox to dimension potential bg color, even if not selected
  600. r.pos.x-=sbsel->get_margin(MARGIN_LEFT);
  601. r.size.x+=sbsel->get_margin(MARGIN_LEFT)+sbsel->get_margin(MARGIN_RIGHT);
  602. r.pos.y-=sbsel->get_margin(MARGIN_TOP);
  603. r.size.y+=sbsel->get_margin(MARGIN_TOP)+sbsel->get_margin(MARGIN_BOTTOM);
  604. if (items[i].selected) {
  605. draw_style_box(sbsel,r);
  606. }
  607. if (items[i].custom_bg.a>0.001) {
  608. r.pos.x+=2;
  609. r.size.x-=4;
  610. r.pos.y+=2;
  611. r.size.y-=4;
  612. draw_rect(r,items[i].custom_bg);
  613. }
  614. Vector2 text_ofs;
  615. if (items[i].icon.is_valid()) {
  616. Vector2 icon_ofs;
  617. if (min_icon_size!=Vector2()) {
  618. icon_ofs = (min_icon_size - items[i].icon->get_size())/2;
  619. }
  620. if (icon_mode==ICON_MODE_TOP) {
  621. draw_texture(items[i].icon,icon_ofs+items[i].rect_cache.pos+Vector2(items[i].rect_cache.size.width/2-items[i].icon->get_width()/2,0).floor()+base_ofs);
  622. text_ofs.y = MAX(items[i].icon->get_height(),min_icon_size.y)+icon_margin;
  623. } else {
  624. draw_texture(items[i].icon,icon_ofs+items[i].rect_cache.pos+Vector2(0,items[i].rect_cache.size.height/2-items[i].icon->get_height()/2).floor()+base_ofs);
  625. text_ofs.x = MAX(items[i].icon->get_width(),min_icon_size.x)+icon_margin;
  626. }
  627. }
  628. if (items[i].tag_icon.is_valid()) {
  629. draw_texture(items[i].tag_icon,items[i].rect_cache.pos+base_ofs);
  630. }
  631. if (items[i].text!="") {
  632. int max_len=-1;
  633. Vector2 size = font->get_string_size(items[i].text);
  634. if (fixed_column_width)
  635. max_len=fixed_column_width;
  636. else
  637. max_len=size.x;
  638. if (icon_mode==ICON_MODE_TOP && max_text_lines>0) {
  639. int ss = items[i].text.length();
  640. float ofs=0;
  641. int line=0;
  642. for(int j=0;j<=ss;j++) {
  643. int cs = j<ss?font->get_char_size(items[i].text[j],items[i].text[j+1]).x:0;
  644. if (ofs+cs>max_len || j==ss) {
  645. line_limit_cache[line]=j;
  646. line_size_cache[line]=ofs;
  647. line++;
  648. ofs=0;
  649. if (line>=max_text_lines)
  650. break;
  651. } else {
  652. ofs+=cs;
  653. }
  654. }
  655. line=0;
  656. ofs=0;
  657. text_ofs.y+=font->get_ascent();
  658. text_ofs=text_ofs.floor();
  659. text_ofs+=base_ofs;
  660. text_ofs+=items[i].rect_cache.pos;
  661. for(int j=0;j<ss;j++) {
  662. if (j==line_limit_cache[line]) {
  663. line++;
  664. ofs=0;
  665. if (line>=max_text_lines)
  666. break;
  667. }
  668. 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],items[i].selected?font_color_selected:font_color);
  669. }
  670. //special multiline mode
  671. } else {
  672. if (fixed_column_width>0)
  673. size.x=MIN(size.x,fixed_column_width);
  674. if (icon_mode==ICON_MODE_TOP) {
  675. text_ofs.x+=(items[i].rect_cache.size.width-size.x)/2;
  676. } else {
  677. text_ofs.y+=(items[i].rect_cache.size.height-size.y)/2;
  678. }
  679. text_ofs.y+=font->get_ascent();
  680. text_ofs=text_ofs.floor();
  681. text_ofs+=base_ofs;
  682. text_ofs+=items[i].rect_cache.pos;
  683. draw_string(font,text_ofs,items[i].text,items[i].selected?font_color_selected:font_color,max_len+1);
  684. }
  685. }
  686. if (select_mode==SELECT_MULTI && i==current) {
  687. Rect2 r=rcache;
  688. r.pos+=base_ofs;
  689. draw_style_box(cursor,r);
  690. }
  691. }
  692. for(int i=0;i<separators.size();i++) {
  693. draw_line(Vector2(bg->get_margin(MARGIN_LEFT),base_ofs.y+separators[i]),Vector2(size.width-bg->get_margin(MARGIN_LEFT),base_ofs.y+separators[i]),guide_color);
  694. }
  695. if (ensure_selected_visible && current>=0 && current <=items.size()) {
  696. Rect2 r = items[current].rect_cache;
  697. int from = scroll_bar->get_val();
  698. int to = from + scroll_bar->get_page();
  699. if (r.pos.y < from) {
  700. scroll_bar->set_val(r.pos.y);
  701. } else if (r.pos.y+r.size.y > to) {
  702. scroll_bar->set_val(r.pos.y+r.size.y - (to-from));
  703. }
  704. }
  705. ensure_selected_visible=false;
  706. }
  707. }
  708. void ItemList::_scroll_changed(double) {
  709. update();
  710. }
  711. String ItemList::get_tooltip(const Point2& p_pos) const {
  712. Vector2 pos=p_pos;
  713. Ref<StyleBox> bg = get_stylebox("bg");
  714. pos-=bg->get_offset();
  715. pos.y+=scroll_bar->get_val();
  716. int closest = -1;
  717. int closest_dist=0x7FFFFFFF;
  718. for(int i=0;i<items.size();i++) {
  719. Rect2 rc = items[i].rect_cache;
  720. if (i%current_columns==current_columns-1) {
  721. rc.size.width=get_size().width; //not right but works
  722. }
  723. if (rc.has_point(pos)) {
  724. closest=i;
  725. break;
  726. }
  727. float dist = rc.distance_to(pos);
  728. if (dist<closest_dist) {
  729. closest=i;
  730. closest_dist=dist;
  731. }
  732. }
  733. if (closest!=-1) {
  734. if (items[closest].tooltip!="") {
  735. return items[closest].tooltip;
  736. }
  737. if (items[closest].text!="") {
  738. return items[closest].text;
  739. }
  740. }
  741. return Control::get_tooltip(p_pos);
  742. }
  743. void ItemList::sort_items_by_text() {
  744. items.sort();
  745. update();
  746. if (select_mode==SELECT_SINGLE) {
  747. for(int i=0;i<items.size();i++) {
  748. if (items[i].selected) {
  749. select(i);
  750. return;
  751. }
  752. }
  753. }
  754. }
  755. int ItemList::find_metadata(const Variant& p_metadata) const {
  756. for(int i=0;i<items.size();i++) {
  757. if (items[i].metadata==p_metadata) {
  758. return i;
  759. }
  760. }
  761. return -1;
  762. }
  763. void ItemList::_bind_methods(){
  764. ObjectTypeDB::bind_method(_MD("add_item","text","icon:Texture","selectable"),&ItemList::add_item,DEFVAL(Variant()),DEFVAL(true));
  765. ObjectTypeDB::bind_method(_MD("add_icon_item","icon:Texture","selectable"),&ItemList::add_icon_item,DEFVAL(true));
  766. ObjectTypeDB::bind_method(_MD("set_item_text","idx","text"),&ItemList::set_item_text);
  767. ObjectTypeDB::bind_method(_MD("get_item_text","idx"),&ItemList::get_item_text);
  768. ObjectTypeDB::bind_method(_MD("set_item_icon","idx","icon:Texture"),&ItemList::set_item_icon);
  769. ObjectTypeDB::bind_method(_MD("get_item_icon:Texture","idx"),&ItemList::get_item_icon);
  770. ObjectTypeDB::bind_method(_MD("set_item_selectable","idx","selectable"),&ItemList::set_item_selectable);
  771. ObjectTypeDB::bind_method(_MD("is_item_selectable","idx"),&ItemList::is_item_selectable);
  772. ObjectTypeDB::bind_method(_MD("set_item_disabled","idx","disabled"),&ItemList::set_item_disabled);
  773. ObjectTypeDB::bind_method(_MD("is_item_disabled","idx"),&ItemList::is_item_disabled);
  774. ObjectTypeDB::bind_method(_MD("set_item_metadata","idx","metadata"),&ItemList::set_item_metadata);
  775. ObjectTypeDB::bind_method(_MD("get_item_metadata","idx"),&ItemList::get_item_metadata);
  776. ObjectTypeDB::bind_method(_MD("set_item_custom_bg_color","idx","custom_bg_color"),&ItemList::set_item_custom_bg_color);
  777. ObjectTypeDB::bind_method(_MD("get_item_custom_bg_color","idx"),&ItemList::get_item_custom_bg_color);
  778. ObjectTypeDB::bind_method(_MD("set_item_tooltip","idx","tooltip"),&ItemList::set_item_tooltip);
  779. ObjectTypeDB::bind_method(_MD("get_item_tooltip","idx"),&ItemList::get_item_tooltip);
  780. ObjectTypeDB::bind_method(_MD("select","idx","single"),&ItemList::select,DEFVAL(true));
  781. ObjectTypeDB::bind_method(_MD("unselect","idx"),&ItemList::unselect);
  782. ObjectTypeDB::bind_method(_MD("is_selected","idx"),&ItemList::is_selected);
  783. ObjectTypeDB::bind_method(_MD("get_item_count"),&ItemList::get_item_count);
  784. ObjectTypeDB::bind_method(_MD("remove_item","idx"),&ItemList::remove_item);
  785. ObjectTypeDB::bind_method(_MD("clear"),&ItemList::clear);
  786. ObjectTypeDB::bind_method(_MD("sort_items_by_text"),&ItemList::clear);
  787. ObjectTypeDB::bind_method(_MD("set_fixed_column_width","width"),&ItemList::set_fixed_column_width);
  788. ObjectTypeDB::bind_method(_MD("get_fixed_column_width"),&ItemList::get_fixed_column_width);
  789. ObjectTypeDB::bind_method(_MD("set_max_text_lines","lines"),&ItemList::set_max_text_lines);
  790. ObjectTypeDB::bind_method(_MD("get_max_text_lines"),&ItemList::get_max_text_lines);
  791. ObjectTypeDB::bind_method(_MD("set_max_columns","amount"),&ItemList::set_max_columns);
  792. ObjectTypeDB::bind_method(_MD("get_max_columns"),&ItemList::get_max_columns);
  793. ObjectTypeDB::bind_method(_MD("set_select_mode","mode"),&ItemList::set_select_mode);
  794. ObjectTypeDB::bind_method(_MD("get_select_mode"),&ItemList::get_select_mode);
  795. ObjectTypeDB::bind_method(_MD("set_icon_mode","mode"),&ItemList::set_icon_mode);
  796. ObjectTypeDB::bind_method(_MD("get_icon_mode"),&ItemList::get_icon_mode);
  797. ObjectTypeDB::bind_method(_MD("set_min_icon_size","size"),&ItemList::set_min_icon_size);
  798. ObjectTypeDB::bind_method(_MD("get_min_icon_size"),&ItemList::get_min_icon_size);
  799. ObjectTypeDB::bind_method(_MD("ensure_current_is_visible"),&ItemList::ensure_current_is_visible);
  800. ObjectTypeDB::bind_method(_MD("_scroll_changed"),&ItemList::_scroll_changed);
  801. ObjectTypeDB::bind_method(_MD("_input_event"),&ItemList::_input_event);
  802. BIND_CONSTANT( ICON_MODE_TOP );
  803. BIND_CONSTANT( ICON_MODE_LEFT );
  804. BIND_CONSTANT( SELECT_SINGLE );
  805. BIND_CONSTANT( SELECT_MULTI );
  806. ADD_SIGNAL( MethodInfo("item_selected",PropertyInfo(Variant::INT,"index")));
  807. ADD_SIGNAL( MethodInfo("multi_selected",PropertyInfo(Variant::INT,"index"),PropertyInfo(Variant::BOOL,"selected")));
  808. ADD_SIGNAL( MethodInfo("item_activated",PropertyInfo(Variant::INT,"index")));
  809. }
  810. ItemList::ItemList() {
  811. current=-1;
  812. select_mode=SELECT_SINGLE;
  813. icon_mode=ICON_MODE_LEFT;
  814. fixed_column_width=0;
  815. max_text_lines=1;
  816. max_columns=1;
  817. scroll_bar = memnew( VScrollBar );
  818. add_child(scroll_bar);
  819. shape_changed=true;
  820. scroll_bar->connect("value_changed",this,"_scroll_changed");
  821. set_focus_mode(FOCUS_ALL);
  822. current_columns=1;
  823. search_time_msec=0;
  824. ensure_selected_visible=false;
  825. defer_select_single=-1;
  826. }
  827. ItemList::~ItemList() {
  828. }