tree.cpp 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "tree.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "core/print_string.h"
  36. #include "core/project_settings.h"
  37. #include "scene/main/viewport.h"
  38. #ifdef TOOLS_ENABLED
  39. #include "editor/editor_scale.h"
  40. #endif
  41. #include <limits.h>
  42. void TreeItem::move_to_top() {
  43. if (!parent || parent->children == this)
  44. return; //already on top
  45. TreeItem *prev = get_prev();
  46. prev->next = next;
  47. next = parent->children;
  48. parent->children = this;
  49. }
  50. void TreeItem::move_to_bottom() {
  51. if (!parent || !next)
  52. return;
  53. TreeItem *prev = get_prev();
  54. TreeItem *last = next;
  55. while (last->next)
  56. last = last->next;
  57. if (prev) {
  58. prev->next = next;
  59. } else {
  60. parent->children = next;
  61. }
  62. last->next = this;
  63. next = NULL;
  64. }
  65. Size2 TreeItem::Cell::get_icon_size() const {
  66. if (icon.is_null())
  67. return Size2();
  68. if (icon_region == Rect2i())
  69. return icon->get_size();
  70. else
  71. return icon_region.size;
  72. }
  73. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  74. if (icon.is_null())
  75. return;
  76. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  77. if (icon_region == Rect2i()) {
  78. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  79. } else {
  80. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  81. }
  82. }
  83. void TreeItem::_changed_notify(int p_cell) {
  84. tree->item_changed(p_cell, this);
  85. }
  86. void TreeItem::_changed_notify() {
  87. tree->item_changed(-1, this);
  88. }
  89. void TreeItem::_cell_selected(int p_cell) {
  90. tree->item_selected(p_cell, this);
  91. }
  92. void TreeItem::_cell_deselected(int p_cell) {
  93. tree->item_deselected(p_cell, this);
  94. }
  95. /* cell mode */
  96. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  97. ERR_FAIL_INDEX(p_column, cells.size());
  98. Cell &c = cells.write[p_column];
  99. c.mode = p_mode;
  100. c.min = 0;
  101. c.max = 100;
  102. c.step = 1;
  103. c.val = 0;
  104. c.checked = false;
  105. c.icon = Ref<Texture>();
  106. c.text = "";
  107. c.icon_max_w = 0;
  108. _changed_notify(p_column);
  109. }
  110. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  111. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  112. return cells[p_column].mode;
  113. }
  114. /* check mode */
  115. void TreeItem::set_checked(int p_column, bool p_checked) {
  116. ERR_FAIL_INDEX(p_column, cells.size());
  117. cells.write[p_column].checked = p_checked;
  118. _changed_notify(p_column);
  119. }
  120. bool TreeItem::is_checked(int p_column) const {
  121. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  122. return cells[p_column].checked;
  123. }
  124. void TreeItem::set_text(int p_column, String p_text) {
  125. ERR_FAIL_INDEX(p_column, cells.size());
  126. cells.write[p_column].text = p_text;
  127. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  128. Vector<String> strings = p_text.split(",");
  129. cells.write[p_column].min = INT_MAX;
  130. cells.write[p_column].max = INT_MIN;
  131. for (int i = 0; i < strings.size(); i++) {
  132. int value = i;
  133. if (!strings[i].get_slicec(':', 1).empty()) {
  134. value = strings[i].get_slicec(':', 1).to_int();
  135. }
  136. cells.write[p_column].min = MIN(cells[p_column].min, value);
  137. cells.write[p_column].max = MAX(cells[p_column].max, value);
  138. }
  139. cells.write[p_column].step = 0;
  140. }
  141. _changed_notify(p_column);
  142. }
  143. String TreeItem::get_text(int p_column) const {
  144. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  145. return cells[p_column].text;
  146. }
  147. void TreeItem::set_suffix(int p_column, String p_suffix) {
  148. ERR_FAIL_INDEX(p_column, cells.size());
  149. cells.write[p_column].suffix = p_suffix;
  150. _changed_notify(p_column);
  151. }
  152. String TreeItem::get_suffix(int p_column) const {
  153. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  154. return cells[p_column].suffix;
  155. }
  156. void TreeItem::set_icon(int p_column, const Ref<Texture> &p_icon) {
  157. ERR_FAIL_INDEX(p_column, cells.size());
  158. cells.write[p_column].icon = p_icon;
  159. _changed_notify(p_column);
  160. }
  161. Ref<Texture> TreeItem::get_icon(int p_column) const {
  162. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>());
  163. return cells[p_column].icon;
  164. }
  165. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  166. ERR_FAIL_INDEX(p_column, cells.size());
  167. cells.write[p_column].icon_region = p_icon_region;
  168. _changed_notify(p_column);
  169. }
  170. Rect2 TreeItem::get_icon_region(int p_column) const {
  171. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  172. return cells[p_column].icon_region;
  173. }
  174. void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
  175. ERR_FAIL_INDEX(p_column, cells.size());
  176. cells.write[p_column].icon_color = p_modulate;
  177. _changed_notify(p_column);
  178. }
  179. Color TreeItem::get_icon_modulate(int p_column) const {
  180. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  181. return cells[p_column].icon_color;
  182. }
  183. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  184. ERR_FAIL_INDEX(p_column, cells.size());
  185. cells.write[p_column].icon_max_w = p_max;
  186. _changed_notify(p_column);
  187. }
  188. int TreeItem::get_icon_max_width(int p_column) const {
  189. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  190. return cells[p_column].icon_max_w;
  191. }
  192. /* range works for mode number or mode combo */
  193. void TreeItem::set_range(int p_column, double p_value) {
  194. ERR_FAIL_INDEX(p_column, cells.size());
  195. if (cells[p_column].step > 0)
  196. p_value = Math::stepify(p_value, cells[p_column].step);
  197. if (p_value < cells[p_column].min)
  198. p_value = cells[p_column].min;
  199. if (p_value > cells[p_column].max)
  200. p_value = cells[p_column].max;
  201. cells.write[p_column].val = p_value;
  202. _changed_notify(p_column);
  203. }
  204. double TreeItem::get_range(int p_column) const {
  205. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  206. return cells[p_column].val;
  207. }
  208. bool TreeItem::is_range_exponential(int p_column) const {
  209. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  210. return cells[p_column].expr;
  211. }
  212. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  213. ERR_FAIL_INDEX(p_column, cells.size());
  214. cells.write[p_column].min = p_min;
  215. cells.write[p_column].max = p_max;
  216. cells.write[p_column].step = p_step;
  217. cells.write[p_column].expr = p_exp;
  218. _changed_notify(p_column);
  219. }
  220. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  221. ERR_FAIL_INDEX(p_column, cells.size());
  222. r_min = cells[p_column].min;
  223. r_max = cells[p_column].max;
  224. r_step = cells[p_column].step;
  225. }
  226. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  227. ERR_FAIL_INDEX(p_column, cells.size());
  228. cells.write[p_column].meta = p_meta;
  229. }
  230. Variant TreeItem::get_metadata(int p_column) const {
  231. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  232. return cells[p_column].meta;
  233. }
  234. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  235. ERR_FAIL_INDEX(p_column, cells.size());
  236. ERR_FAIL_NULL(p_object);
  237. cells.write[p_column].custom_draw_obj = p_object->get_instance_id();
  238. cells.write[p_column].custom_draw_callback = p_callback;
  239. }
  240. void TreeItem::set_collapsed(bool p_collapsed) {
  241. if (collapsed == p_collapsed || !tree)
  242. return;
  243. collapsed = p_collapsed;
  244. TreeItem *ci = tree->selected_item;
  245. if (ci) {
  246. while (ci && ci != this) {
  247. ci = ci->parent;
  248. }
  249. if (ci) { // collapsing cursor/selected, move it!
  250. if (tree->select_mode == Tree::SELECT_MULTI) {
  251. tree->selected_item = this;
  252. emit_signal("cell_selected");
  253. } else {
  254. select(tree->selected_col);
  255. }
  256. tree->update();
  257. }
  258. }
  259. _changed_notify();
  260. tree->emit_signal("item_collapsed", this);
  261. }
  262. bool TreeItem::is_collapsed() {
  263. return collapsed;
  264. }
  265. void TreeItem::set_custom_minimum_height(int p_height) {
  266. custom_min_height = p_height;
  267. _changed_notify();
  268. }
  269. int TreeItem::get_custom_minimum_height() const {
  270. return custom_min_height;
  271. }
  272. TreeItem *TreeItem::get_next() {
  273. return next;
  274. }
  275. TreeItem *TreeItem::get_prev() {
  276. if (!parent || parent->children == this)
  277. return NULL;
  278. TreeItem *prev = parent->children;
  279. while (prev && prev->next != this)
  280. prev = prev->next;
  281. return prev;
  282. }
  283. TreeItem *TreeItem::get_parent() {
  284. return parent;
  285. }
  286. TreeItem *TreeItem::get_children() {
  287. return children;
  288. }
  289. TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
  290. TreeItem *current = this;
  291. TreeItem *prev = current->get_prev();
  292. if (!prev) {
  293. current = current->parent;
  294. if (current == tree->root && tree->hide_root) {
  295. return NULL;
  296. } else if (!current) {
  297. if (p_wrap) {
  298. current = this;
  299. TreeItem *temp = this->get_next_visible();
  300. while (temp) {
  301. current = temp;
  302. temp = temp->get_next_visible();
  303. }
  304. } else {
  305. return NULL;
  306. }
  307. }
  308. } else {
  309. current = prev;
  310. while (!current->collapsed && current->children) {
  311. //go to the very end
  312. current = current->children;
  313. while (current->next)
  314. current = current->next;
  315. }
  316. }
  317. return current;
  318. }
  319. TreeItem *TreeItem::get_next_visible(bool p_wrap) {
  320. TreeItem *current = this;
  321. if (!current->collapsed && current->children) {
  322. current = current->children;
  323. } else if (current->next) {
  324. current = current->next;
  325. } else {
  326. while (current && !current->next) {
  327. current = current->parent;
  328. }
  329. if (!current) {
  330. if (p_wrap)
  331. return tree->root;
  332. else
  333. return NULL;
  334. } else {
  335. current = current->next;
  336. }
  337. }
  338. return current;
  339. }
  340. void TreeItem::remove_child(TreeItem *p_item) {
  341. ERR_FAIL_NULL(p_item);
  342. TreeItem **c = &children;
  343. while (*c) {
  344. if ((*c) == p_item) {
  345. TreeItem *aux = *c;
  346. *c = (*c)->next;
  347. aux->parent = NULL;
  348. return;
  349. }
  350. c = &(*c)->next;
  351. }
  352. ERR_FAIL();
  353. }
  354. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  355. ERR_FAIL_INDEX(p_column, cells.size());
  356. cells.write[p_column].selectable = p_selectable;
  357. }
  358. bool TreeItem::is_selectable(int p_column) const {
  359. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  360. return cells[p_column].selectable;
  361. }
  362. bool TreeItem::is_selected(int p_column) {
  363. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  364. return cells[p_column].selectable && cells[p_column].selected;
  365. }
  366. void TreeItem::set_as_cursor(int p_column) {
  367. ERR_FAIL_INDEX(p_column, cells.size());
  368. if (!tree)
  369. return;
  370. if (tree->select_mode != Tree::SELECT_MULTI)
  371. return;
  372. tree->selected_item = this;
  373. tree->selected_col = p_column;
  374. tree->update();
  375. }
  376. void TreeItem::select(int p_column) {
  377. ERR_FAIL_INDEX(p_column, cells.size());
  378. _cell_selected(p_column);
  379. }
  380. void TreeItem::deselect(int p_column) {
  381. ERR_FAIL_INDEX(p_column, cells.size());
  382. _cell_deselected(p_column);
  383. }
  384. void TreeItem::add_button(int p_column, const Ref<Texture> &p_button, int p_id, bool p_disabled, const String &p_tooltip) {
  385. ERR_FAIL_INDEX(p_column, cells.size());
  386. ERR_FAIL_COND(!p_button.is_valid());
  387. TreeItem::Cell::Button button;
  388. button.texture = p_button;
  389. if (p_id < 0)
  390. p_id = cells[p_column].buttons.size();
  391. button.id = p_id;
  392. button.disabled = p_disabled;
  393. button.tooltip = p_tooltip;
  394. cells.write[p_column].buttons.push_back(button);
  395. _changed_notify(p_column);
  396. }
  397. int TreeItem::get_button_count(int p_column) const {
  398. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  399. return cells[p_column].buttons.size();
  400. }
  401. Ref<Texture> TreeItem::get_button(int p_column, int p_idx) const {
  402. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>());
  403. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), Ref<Texture>());
  404. return cells[p_column].buttons[p_idx].texture;
  405. }
  406. String TreeItem::get_button_tooltip(int p_column, int p_idx) const {
  407. ERR_FAIL_INDEX_V(p_column, cells.size(), String());
  408. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), String());
  409. return cells[p_column].buttons[p_idx].tooltip;
  410. }
  411. int TreeItem::get_button_id(int p_column, int p_idx) const {
  412. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  413. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), -1);
  414. return cells[p_column].buttons[p_idx].id;
  415. }
  416. void TreeItem::erase_button(int p_column, int p_idx) {
  417. ERR_FAIL_INDEX(p_column, cells.size());
  418. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  419. cells.write[p_column].buttons.remove(p_idx);
  420. _changed_notify(p_column);
  421. }
  422. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  423. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  424. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  425. if (cells[p_column].buttons[i].id == p_id)
  426. return i;
  427. }
  428. return -1;
  429. }
  430. void TreeItem::set_button(int p_column, int p_idx, const Ref<Texture> &p_button) {
  431. ERR_FAIL_COND(p_button.is_null());
  432. ERR_FAIL_INDEX(p_column, cells.size());
  433. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  434. cells.write[p_column].buttons.write[p_idx].texture = p_button;
  435. _changed_notify(p_column);
  436. }
  437. void TreeItem::set_button_color(int p_column, int p_idx, const Color &p_color) {
  438. ERR_FAIL_INDEX(p_column, cells.size());
  439. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  440. cells.write[p_column].buttons.write[p_idx].color = p_color;
  441. _changed_notify(p_column);
  442. }
  443. void TreeItem::set_button_disabled(int p_column, int p_idx, bool p_disabled) {
  444. ERR_FAIL_INDEX(p_column, cells.size());
  445. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  446. cells.write[p_column].buttons.write[p_idx].disabled = p_disabled;
  447. _changed_notify(p_column);
  448. }
  449. bool TreeItem::is_button_disabled(int p_column, int p_idx) const {
  450. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  451. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), false);
  452. return cells[p_column].buttons[p_idx].disabled;
  453. }
  454. void TreeItem::set_editable(int p_column, bool p_editable) {
  455. ERR_FAIL_INDEX(p_column, cells.size());
  456. cells.write[p_column].editable = p_editable;
  457. _changed_notify(p_column);
  458. }
  459. bool TreeItem::is_editable(int p_column) {
  460. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  461. return cells[p_column].editable;
  462. }
  463. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  464. ERR_FAIL_INDEX(p_column, cells.size());
  465. cells.write[p_column].custom_color = true;
  466. cells.write[p_column].color = p_color;
  467. _changed_notify(p_column);
  468. }
  469. Color TreeItem::get_custom_color(int p_column) const {
  470. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  471. if (!cells[p_column].custom_color)
  472. return Color();
  473. return cells[p_column].color;
  474. }
  475. void TreeItem::clear_custom_color(int p_column) {
  476. ERR_FAIL_INDEX(p_column, cells.size());
  477. cells.write[p_column].custom_color = false;
  478. cells.write[p_column].color = Color();
  479. _changed_notify(p_column);
  480. }
  481. void TreeItem::set_tooltip(int p_column, const String &p_tooltip) {
  482. ERR_FAIL_INDEX(p_column, cells.size());
  483. cells.write[p_column].tooltip = p_tooltip;
  484. }
  485. String TreeItem::get_tooltip(int p_column) const {
  486. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  487. return cells[p_column].tooltip;
  488. }
  489. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  490. ERR_FAIL_INDEX(p_column, cells.size());
  491. cells.write[p_column].custom_bg_color = true;
  492. cells.write[p_column].custom_bg_outline = p_bg_outline;
  493. cells.write[p_column].bg_color = p_color;
  494. _changed_notify(p_column);
  495. }
  496. void TreeItem::clear_custom_bg_color(int p_column) {
  497. ERR_FAIL_INDEX(p_column, cells.size());
  498. cells.write[p_column].custom_bg_color = false;
  499. cells.write[p_column].bg_color = Color();
  500. _changed_notify(p_column);
  501. }
  502. Color TreeItem::get_custom_bg_color(int p_column) const {
  503. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  504. if (!cells[p_column].custom_bg_color)
  505. return Color();
  506. return cells[p_column].bg_color;
  507. }
  508. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  509. ERR_FAIL_INDEX(p_column, cells.size());
  510. cells.write[p_column].custom_button = p_button;
  511. }
  512. bool TreeItem::is_custom_set_as_button(int p_column) const {
  513. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  514. return cells[p_column].custom_button;
  515. }
  516. void TreeItem::set_text_align(int p_column, TextAlign p_align) {
  517. ERR_FAIL_INDEX(p_column, cells.size());
  518. cells.write[p_column].text_align = p_align;
  519. _changed_notify(p_column);
  520. }
  521. TreeItem::TextAlign TreeItem::get_text_align(int p_column) const {
  522. ERR_FAIL_INDEX_V(p_column, cells.size(), ALIGN_LEFT);
  523. return cells[p_column].text_align;
  524. }
  525. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  526. ERR_FAIL_INDEX(p_column, cells.size());
  527. cells.write[p_column].expand_right = p_enable;
  528. _changed_notify(p_column);
  529. }
  530. bool TreeItem::get_expand_right(int p_column) const {
  531. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  532. return cells[p_column].expand_right;
  533. }
  534. void TreeItem::set_disable_folding(bool p_disable) {
  535. disable_folding = p_disable;
  536. _changed_notify(0);
  537. }
  538. bool TreeItem::is_folding_disabled() const {
  539. return disable_folding;
  540. }
  541. void TreeItem::_bind_methods() {
  542. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  543. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  544. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  545. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  546. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  547. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  548. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  549. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  550. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  551. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  552. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  553. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  554. ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
  555. ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
  556. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  557. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  558. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  559. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  560. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  561. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  562. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  563. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  564. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  565. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  566. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  567. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  568. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  569. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  570. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  571. ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
  572. ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
  573. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::_remove_child);
  574. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  575. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  576. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  577. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  578. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  579. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  580. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  581. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  582. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  583. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  584. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  585. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  586. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  587. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  588. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "button_idx", "disabled", "tooltip"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""));
  589. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  590. ClassDB::bind_method(D_METHOD("get_button_tooltip", "column"), &TreeItem::get_button_tooltip);
  591. ClassDB::bind_method(D_METHOD("get_button", "column", "button_idx"), &TreeItem::get_button);
  592. ClassDB::bind_method(D_METHOD("set_button", "column", "button_idx", "button"), &TreeItem::set_button);
  593. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_idx"), &TreeItem::erase_button);
  594. ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_idx", "disabled"), &TreeItem::set_button_disabled);
  595. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_idx"), &TreeItem::is_button_disabled);
  596. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  597. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  598. ClassDB::bind_method(D_METHOD("set_tooltip", "column", "tooltip"), &TreeItem::set_tooltip);
  599. ClassDB::bind_method(D_METHOD("get_tooltip", "column"), &TreeItem::get_tooltip);
  600. ClassDB::bind_method(D_METHOD("set_text_align", "column", "text_align"), &TreeItem::set_text_align);
  601. ClassDB::bind_method(D_METHOD("get_text_align", "column"), &TreeItem::get_text_align);
  602. ClassDB::bind_method(D_METHOD("move_to_top"), &TreeItem::move_to_top);
  603. ClassDB::bind_method(D_METHOD("move_to_bottom"), &TreeItem::move_to_bottom);
  604. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  605. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  606. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  607. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  608. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  609. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  610. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  611. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  612. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  613. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  614. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  615. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  616. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  617. }
  618. void TreeItem::clear_children() {
  619. TreeItem *c = children;
  620. while (c) {
  621. TreeItem *aux = c;
  622. c = c->get_next();
  623. aux->parent = 0; // so it won't try to recursively autoremove from me in here
  624. memdelete(aux);
  625. }
  626. children = 0;
  627. };
  628. TreeItem::TreeItem(Tree *p_tree) {
  629. tree = p_tree;
  630. collapsed = false;
  631. disable_folding = false;
  632. custom_min_height = 0;
  633. parent = 0; // parent item
  634. next = 0; // next in list
  635. children = 0; //child items
  636. }
  637. TreeItem::~TreeItem() {
  638. clear_children();
  639. if (parent)
  640. parent->remove_child(this);
  641. if (tree && tree->root == this) {
  642. tree->root = 0;
  643. }
  644. if (tree && tree->popup_edited_item == this) {
  645. tree->popup_edited_item = NULL;
  646. tree->pressing_for_editor = false;
  647. }
  648. if (tree && tree->cache.hover_item == this) {
  649. tree->cache.hover_item = NULL;
  650. }
  651. if (tree && tree->selected_item == this)
  652. tree->selected_item = NULL;
  653. if (tree && tree->drop_mode_over == this)
  654. tree->drop_mode_over = NULL;
  655. if (tree && tree->single_select_defer == this)
  656. tree->single_select_defer = NULL;
  657. if (tree && tree->edited_item == this) {
  658. tree->edited_item = NULL;
  659. tree->pressing_for_editor = false;
  660. }
  661. }
  662. /**********************************************/
  663. /**********************************************/
  664. /**********************************************/
  665. /**********************************************/
  666. /**********************************************/
  667. /**********************************************/
  668. void Tree::update_cache() {
  669. cache.font = get_font("font");
  670. cache.tb_font = get_font("title_button_font");
  671. cache.bg = get_stylebox("bg");
  672. cache.selected = get_stylebox("selected");
  673. cache.selected_focus = get_stylebox("selected_focus");
  674. cache.cursor = get_stylebox("cursor");
  675. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  676. cache.button_pressed = get_stylebox("button_pressed");
  677. cache.checked = get_icon("checked");
  678. cache.unchecked = get_icon("unchecked");
  679. cache.arrow_collapsed = get_icon("arrow_collapsed");
  680. cache.arrow = get_icon("arrow");
  681. cache.select_arrow = get_icon("select_arrow");
  682. cache.updown = get_icon("updown");
  683. cache.custom_button = get_stylebox("custom_button");
  684. cache.custom_button_hover = get_stylebox("custom_button_hover");
  685. cache.custom_button_pressed = get_stylebox("custom_button_pressed");
  686. cache.custom_button_font_highlight = get_color("custom_button_font_highlight");
  687. cache.font_color = get_color("font_color");
  688. cache.font_color_selected = get_color("font_color_selected");
  689. cache.guide_color = get_color("guide_color");
  690. cache.drop_position_color = get_color("drop_position_color");
  691. cache.hseparation = get_constant("hseparation");
  692. cache.vseparation = get_constant("vseparation");
  693. cache.item_margin = get_constant("item_margin");
  694. cache.button_margin = get_constant("button_margin");
  695. cache.draw_guides = get_constant("draw_guides");
  696. cache.draw_relationship_lines = get_constant("draw_relationship_lines");
  697. cache.relationship_line_color = get_color("relationship_line_color");
  698. cache.scroll_border = get_constant("scroll_border");
  699. cache.scroll_speed = get_constant("scroll_speed");
  700. cache.title_button = get_stylebox("title_button_normal");
  701. cache.title_button_pressed = get_stylebox("title_button_pressed");
  702. cache.title_button_hover = get_stylebox("title_button_hover");
  703. cache.title_button_color = get_color("title_button_color");
  704. v_scroll->set_custom_step(cache.font->get_height());
  705. }
  706. int Tree::compute_item_height(TreeItem *p_item) const {
  707. if (p_item == root && hide_root)
  708. return 0;
  709. ERR_FAIL_COND_V(cache.font.is_null(), 0);
  710. int height = cache.font->get_height();
  711. for (int i = 0; i < columns.size(); i++) {
  712. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  713. Size2i s; // = cache.button_pressed->get_minimum_size();
  714. s += p_item->cells[i].buttons[j].texture->get_size();
  715. if (s.height > height)
  716. height = s.height;
  717. }
  718. switch (p_item->cells[i].mode) {
  719. case TreeItem::CELL_MODE_CHECK: {
  720. int check_icon_h = cache.checked->get_height();
  721. if (height < check_icon_h)
  722. height = check_icon_h;
  723. FALLTHROUGH;
  724. }
  725. case TreeItem::CELL_MODE_STRING:
  726. case TreeItem::CELL_MODE_CUSTOM:
  727. case TreeItem::CELL_MODE_ICON: {
  728. Ref<Texture> icon = p_item->cells[i].icon;
  729. if (!icon.is_null()) {
  730. Size2i s = p_item->cells[i].get_icon_size();
  731. if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) {
  732. s.height = s.height * p_item->cells[i].icon_max_w / s.width;
  733. }
  734. if (s.height > height)
  735. height = s.height;
  736. }
  737. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  738. height += cache.custom_button->get_minimum_size().height;
  739. }
  740. } break;
  741. default: {
  742. }
  743. }
  744. }
  745. int item_min_height = p_item->get_custom_minimum_height();
  746. if (height < item_min_height)
  747. height = item_min_height;
  748. height += cache.vseparation;
  749. return height;
  750. }
  751. int Tree::get_item_height(TreeItem *p_item) const {
  752. int height = compute_item_height(p_item);
  753. height += cache.vseparation;
  754. if (!p_item->collapsed) { /* if not collapsed, check the children */
  755. TreeItem *c = p_item->children;
  756. while (c) {
  757. height += get_item_height(c);
  758. c = c->next;
  759. }
  760. }
  761. return height;
  762. }
  763. void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color) {
  764. ERR_FAIL_COND(cache.font.is_null());
  765. Rect2i rect = p_rect;
  766. Ref<Font> font = cache.font;
  767. String text = p_cell.text;
  768. if (p_cell.suffix != String())
  769. text += " " + p_cell.suffix;
  770. int w = 0;
  771. if (!p_cell.icon.is_null()) {
  772. Size2i bmsize = p_cell.get_icon_size();
  773. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  774. bmsize.width = p_cell.icon_max_w;
  775. }
  776. w += bmsize.width + cache.hseparation;
  777. }
  778. w += font->get_string_size(text).width;
  779. switch (p_cell.text_align) {
  780. case TreeItem::ALIGN_LEFT:
  781. break; //do none
  782. case TreeItem::ALIGN_CENTER:
  783. rect.position.x += MAX(0, (rect.size.width - w) / 2);
  784. break; //do none
  785. case TreeItem::ALIGN_RIGHT:
  786. rect.position.x += MAX(0, (rect.size.width - w));
  787. break; //do none
  788. }
  789. RID ci = get_canvas_item();
  790. if (!p_cell.icon.is_null()) {
  791. Size2i bmsize = p_cell.get_icon_size();
  792. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  793. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  794. bmsize.width = p_cell.icon_max_w;
  795. }
  796. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
  797. rect.position.x += bmsize.x + cache.hseparation;
  798. rect.size.x -= bmsize.x + cache.hseparation;
  799. }
  800. rect.position.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent();
  801. font->draw(ci, rect.position, text, p_color, rect.size.x);
  802. }
  803. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item) {
  804. if (p_pos.y - cache.offset.y > (p_draw_size.height))
  805. return -1; //draw no more!
  806. RID ci = get_canvas_item();
  807. int htotal = 0;
  808. int label_h = compute_item_height(p_item);
  809. /* Calculate height of the label part */
  810. label_h += cache.vseparation;
  811. /* Draw label, if height fits */
  812. bool skip = (p_item == root && hide_root);
  813. if (!skip && (p_pos.y + label_h - cache.offset.y) > 0) {
  814. //draw separation.
  815. //if (p_item->get_parent()!=root || !hide_root)
  816. ERR_FAIL_COND_V(cache.font.is_null(), -1);
  817. Ref<Font> font = cache.font;
  818. int font_ascent = font->get_ascent();
  819. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  820. int skip2 = 0;
  821. for (int i = 0; i < columns.size(); i++) {
  822. if (skip2) {
  823. skip2--;
  824. continue;
  825. }
  826. int w = get_column_width(i);
  827. if (i == 0) {
  828. w -= ofs;
  829. if (w <= 0) {
  830. ofs = get_column_width(0);
  831. continue;
  832. }
  833. } else {
  834. ofs += cache.hseparation;
  835. w -= cache.hseparation;
  836. }
  837. if (p_item->cells[i].expand_right) {
  838. int plus = 1;
  839. while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text == "" && p_item->cells[i + plus].icon.is_null()) {
  840. w += get_column_width(i + plus);
  841. plus++;
  842. skip2++;
  843. }
  844. }
  845. int bw = 0;
  846. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  847. Ref<Texture> b = p_item->cells[i].buttons[j].texture;
  848. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  849. if (s.height < label_h)
  850. s.height = label_h;
  851. Point2i o = Point2i(ofs + w - s.width, p_pos.y) - cache.offset + p_draw_ofs;
  852. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled) {
  853. //being pressed
  854. cache.button_pressed->draw(get_canvas_item(), Rect2(o, s));
  855. }
  856. o.y += (label_h - s.height) / 2;
  857. o += cache.button_pressed->get_offset();
  858. b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  859. w -= s.width + cache.button_margin;
  860. bw += s.width + cache.button_margin;
  861. }
  862. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h));
  863. Rect2i cell_rect = item_rect;
  864. if (i != 0) {
  865. cell_rect.position.x -= cache.hseparation;
  866. cell_rect.size.x += cache.hseparation;
  867. }
  868. if (cache.draw_guides) {
  869. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(cell_rect.position.x, cell_rect.position.y + cell_rect.size.height), cell_rect.position + cell_rect.size, cache.guide_color, 1);
  870. }
  871. if (i == 0) {
  872. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  873. Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(MARGIN_LEFT), item_rect.position.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y));
  874. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  875. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  876. if (has_focus())
  877. cache.selected_focus->draw(ci, row_rect);
  878. else
  879. cache.selected->draw(ci, row_rect);
  880. }
  881. }
  882. if (select_mode != SELECT_ROW && (p_item->cells[i].selected || selected_item == p_item)) {
  883. Rect2i r(cell_rect.position, cell_rect.size);
  884. if (p_item->cells[i].text.size() > 0) {
  885. float icon_width = p_item->cells[i].get_icon_size().width;
  886. r.position.x += icon_width;
  887. r.size.x -= icon_width;
  888. }
  889. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  890. if (p_item->cells[i].selected) {
  891. if (has_focus()) {
  892. cache.selected_focus->draw(ci, r);
  893. } else {
  894. cache.selected->draw(ci, r);
  895. }
  896. }
  897. }
  898. if (p_item->cells[i].custom_bg_color) {
  899. Rect2 r = cell_rect;
  900. if (i == 0) {
  901. r.position.x = p_draw_ofs.x;
  902. r.size.x = w + ofs;
  903. } else {
  904. r.position.x -= cache.hseparation;
  905. r.size.x += cache.hseparation;
  906. }
  907. if (p_item->cells[i].custom_bg_outline) {
  908. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
  909. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color);
  910. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  911. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  912. } else {
  913. VisualServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  914. }
  915. }
  916. if (drop_mode_flags && drop_mode_over == p_item) {
  917. Rect2 r = cell_rect;
  918. if (drop_mode_section == -1 || drop_mode_section == 0) {
  919. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color);
  920. }
  921. if (drop_mode_section == 0) {
  922. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color);
  923. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), cache.drop_position_color);
  924. }
  925. if (drop_mode_section == 1 || drop_mode_section == 0) {
  926. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), cache.drop_position_color);
  927. }
  928. }
  929. Color col = p_item->cells[i].custom_color ? p_item->cells[i].color : get_color(p_item->cells[i].selected ? "font_color_selected" : "font_color");
  930. Color icon_col = p_item->cells[i].icon_color;
  931. Point2i text_pos = item_rect.position;
  932. text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent;
  933. switch (p_item->cells[i].mode) {
  934. case TreeItem::CELL_MODE_STRING: {
  935. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  936. } break;
  937. case TreeItem::CELL_MODE_CHECK: {
  938. Ref<Texture> checked = cache.checked;
  939. Ref<Texture> unchecked = cache.unchecked;
  940. Point2i check_ofs = item_rect.position;
  941. check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2);
  942. if (p_item->cells[i].checked) {
  943. checked->draw(ci, check_ofs);
  944. } else {
  945. unchecked->draw(ci, check_ofs);
  946. }
  947. int check_w = checked->get_width() + cache.hseparation;
  948. text_pos.x += check_w;
  949. item_rect.size.x -= check_w;
  950. item_rect.position.x += check_w;
  951. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  952. } break;
  953. case TreeItem::CELL_MODE_RANGE: {
  954. if (p_item->cells[i].text != "") {
  955. if (!p_item->cells[i].editable)
  956. break;
  957. int option = (int)p_item->cells[i].val;
  958. String s = RTR("(Other)");
  959. Vector<String> strings = p_item->cells[i].text.split(",");
  960. for (int j = 0; j < strings.size(); j++) {
  961. int value = j;
  962. if (!strings[j].get_slicec(':', 1).empty()) {
  963. value = strings[j].get_slicec(':', 1).to_int();
  964. }
  965. if (option == value) {
  966. s = strings[j].get_slicec(':', 0);
  967. break;
  968. }
  969. }
  970. if (p_item->cells[i].suffix != String())
  971. s += " " + p_item->cells[i].suffix;
  972. Ref<Texture> downarrow = cache.select_arrow;
  973. font->draw(ci, text_pos, s, col, item_rect.size.x - downarrow->get_width());
  974. Point2i arrow_pos = item_rect.position;
  975. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  976. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  977. downarrow->draw(ci, arrow_pos);
  978. } else {
  979. Ref<Texture> updown = cache.updown;
  980. String valtext = String::num(p_item->cells[i].val, Math::range_step_decimals(p_item->cells[i].step));
  981. if (p_item->cells[i].suffix != String())
  982. valtext += " " + p_item->cells[i].suffix;
  983. font->draw(ci, text_pos, valtext, col, item_rect.size.x - updown->get_width());
  984. if (!p_item->cells[i].editable)
  985. break;
  986. Point2i updown_pos = item_rect.position;
  987. updown_pos.x += item_rect.size.x - updown->get_width();
  988. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  989. updown->draw(ci, updown_pos);
  990. }
  991. } break;
  992. case TreeItem::CELL_MODE_ICON: {
  993. if (p_item->cells[i].icon.is_null())
  994. break;
  995. Size2i icon_size = p_item->cells[i].get_icon_size();
  996. if (p_item->cells[i].icon_max_w > 0 && icon_size.width > p_item->cells[i].icon_max_w) {
  997. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  998. icon_size.width = p_item->cells[i].icon_max_w;
  999. }
  1000. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  1001. icon_ofs += item_rect.position;
  1002. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  1003. } break;
  1004. case TreeItem::CELL_MODE_CUSTOM: {
  1005. if (p_item->cells[i].custom_draw_obj) {
  1006. Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  1007. if (cdo)
  1008. cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect));
  1009. }
  1010. if (!p_item->cells[i].editable) {
  1011. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  1012. break;
  1013. }
  1014. Ref<Texture> downarrow = cache.select_arrow;
  1015. Rect2i ir = item_rect;
  1016. Point2i arrow_pos = item_rect.position;
  1017. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  1018. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  1019. ir.size.width -= downarrow->get_width();
  1020. if (p_item->cells[i].custom_button) {
  1021. if (cache.hover_item == p_item && cache.hover_cell == i) {
  1022. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  1023. draw_style_box(cache.custom_button_pressed, ir);
  1024. } else {
  1025. draw_style_box(cache.custom_button_hover, ir);
  1026. col = cache.custom_button_font_highlight;
  1027. }
  1028. } else {
  1029. draw_style_box(cache.custom_button, ir);
  1030. }
  1031. ir.size -= cache.custom_button->get_minimum_size();
  1032. ir.position += cache.custom_button->get_offset();
  1033. }
  1034. draw_item_rect(p_item->cells[i], ir, col, icon_col);
  1035. downarrow->draw(ci, arrow_pos);
  1036. } break;
  1037. }
  1038. if (i == 0) {
  1039. ofs = get_column_width(0);
  1040. } else {
  1041. ofs += w + bw;
  1042. }
  1043. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  1044. if (has_focus())
  1045. cache.cursor->draw(ci, cell_rect);
  1046. else
  1047. cache.cursor_unfocus->draw(ci, cell_rect);
  1048. }
  1049. }
  1050. if (!p_item->disable_folding && !hide_folding && p_item->children) { //has children, draw the guide box
  1051. Ref<Texture> arrow;
  1052. if (p_item->collapsed) {
  1053. arrow = cache.arrow_collapsed;
  1054. } else {
  1055. arrow = cache.arrow;
  1056. }
  1057. arrow->draw(ci, p_pos + p_draw_ofs + Point2i(0, (label_h - arrow->get_height()) / 2) - cache.offset);
  1058. }
  1059. }
  1060. Point2 children_pos = p_pos;
  1061. if (!skip) {
  1062. children_pos.x += cache.item_margin;
  1063. htotal += label_h;
  1064. children_pos.y += htotal;
  1065. }
  1066. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1067. TreeItem *c = p_item->children;
  1068. int prev_ofs = children_pos.y - cache.offset.y + p_draw_ofs.y;
  1069. while (c) {
  1070. if (cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root)) {
  1071. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1072. int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1073. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs;
  1074. if (c->get_children() != NULL)
  1075. root_pos -= Point2i(cache.arrow->get_width(), 0);
  1076. float line_width = 1.0;
  1077. #ifdef TOOLS_ENABLED
  1078. line_width *= EDSCALE;
  1079. #endif
  1080. Point2i parent_pos = Point2i(parent_ofs - cache.arrow->get_width() / 2, p_pos.y + label_h / 2 + cache.arrow->get_height() / 2) - cache.offset + p_draw_ofs;
  1081. if (root_pos.y + line_width >= 0) {
  1082. VisualServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x - Math::floor(line_width / 2), root_pos.y), cache.relationship_line_color, line_width);
  1083. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), Point2i(parent_pos.x, prev_ofs), cache.relationship_line_color, line_width);
  1084. }
  1085. if (htotal < 0) {
  1086. return -1;
  1087. }
  1088. prev_ofs = root_pos.y;
  1089. }
  1090. if (htotal >= 0) {
  1091. int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
  1092. if (child_h < 0) {
  1093. if (cache.draw_relationship_lines == 0) {
  1094. return -1; // break, stop drawing, no need to anymore
  1095. } else {
  1096. htotal = -1;
  1097. children_pos.y = cache.offset.y + p_draw_size.height;
  1098. }
  1099. } else {
  1100. htotal += child_h;
  1101. children_pos.y += child_h;
  1102. }
  1103. }
  1104. c = c->next;
  1105. }
  1106. }
  1107. return htotal;
  1108. }
  1109. int Tree::_count_selected_items(TreeItem *p_from) const {
  1110. int count = 0;
  1111. for (int i = 0; i < columns.size(); i++) {
  1112. if (p_from->is_selected(i))
  1113. count++;
  1114. }
  1115. if (p_from->get_children()) {
  1116. count += _count_selected_items(p_from->get_children());
  1117. }
  1118. if (p_from->get_next()) {
  1119. count += _count_selected_items(p_from->get_next());
  1120. }
  1121. return count;
  1122. }
  1123. void Tree::select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev, bool *r_in_range, bool p_force_deselect) {
  1124. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  1125. bool switched = false;
  1126. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1127. *r_in_range = true;
  1128. switched = true;
  1129. }
  1130. bool emitted_row = false;
  1131. for (int i = 0; i < columns.size(); i++) {
  1132. TreeItem::Cell &c = p_current->cells.write[i];
  1133. if (!c.selectable)
  1134. continue;
  1135. if (select_mode == SELECT_ROW) {
  1136. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  1137. c.selected = true;
  1138. selected_item = p_selected;
  1139. selected_col = 0;
  1140. if (!emitted_row) {
  1141. emit_signal("item_selected");
  1142. emitted_row = true;
  1143. }
  1144. /*
  1145. if (p_col==i)
  1146. p_current->selected_signal.call(p_col);
  1147. */
  1148. } else if (c.selected) {
  1149. c.selected = false;
  1150. //p_current->deselected_signal.call(p_col);
  1151. }
  1152. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  1153. if (!r_in_range && &selected_cell == &c) {
  1154. if (!selected_cell.selected || allow_reselect) {
  1155. selected_cell.selected = true;
  1156. selected_item = p_selected;
  1157. selected_col = i;
  1158. emit_signal("cell_selected");
  1159. if (select_mode == SELECT_MULTI)
  1160. emit_signal("multi_selected", p_current, i, true);
  1161. else if (select_mode == SELECT_SINGLE)
  1162. emit_signal("item_selected");
  1163. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  1164. selected_item = p_selected;
  1165. selected_col = i;
  1166. emit_signal("cell_selected");
  1167. }
  1168. } else {
  1169. if (r_in_range && *r_in_range && !p_force_deselect) {
  1170. if (!c.selected && c.selectable) {
  1171. c.selected = true;
  1172. emit_signal("multi_selected", p_current, i, true);
  1173. }
  1174. } else if (!r_in_range || p_force_deselect) {
  1175. if (select_mode == SELECT_MULTI && c.selected)
  1176. emit_signal("multi_selected", p_current, i, false);
  1177. c.selected = false;
  1178. }
  1179. //p_current->deselected_signal.call(p_col);
  1180. }
  1181. }
  1182. }
  1183. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1184. *r_in_range = false;
  1185. }
  1186. TreeItem *c = p_current->children;
  1187. while (c) {
  1188. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  1189. c = c->next;
  1190. }
  1191. }
  1192. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  1193. return Rect2();
  1194. }
  1195. void Tree::_range_click_timeout() {
  1196. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  1197. Point2 pos = get_local_mouse_position() - cache.bg->get_offset();
  1198. if (show_column_titles) {
  1199. pos.y -= _get_title_button_height();
  1200. if (pos.y < 0) {
  1201. range_click_timer->stop();
  1202. return;
  1203. }
  1204. }
  1205. click_handled = false;
  1206. Ref<InputEventMouseButton> mb;
  1207. mb.instance();
  1208. ;
  1209. propagate_mouse_activated = false; // done from outside, so signal handler can't clear the tree in the middle of emit (which is a common case)
  1210. blocked++;
  1211. propagate_mouse_event(pos + cache.offset, 0, 0, false, root, BUTTON_LEFT, mb);
  1212. blocked--;
  1213. if (range_click_timer->is_one_shot()) {
  1214. range_click_timer->set_wait_time(0.05);
  1215. range_click_timer->set_one_shot(false);
  1216. range_click_timer->start();
  1217. }
  1218. if (!click_handled)
  1219. range_click_timer->stop();
  1220. if (propagate_mouse_activated) {
  1221. emit_signal("item_activated");
  1222. propagate_mouse_activated = false;
  1223. }
  1224. } else {
  1225. range_click_timer->stop();
  1226. }
  1227. }
  1228. int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool p_doubleclick, TreeItem *p_item, int p_button, const Ref<InputEventWithModifiers> &p_mod) {
  1229. int item_h = compute_item_height(p_item) + cache.vseparation;
  1230. bool skip = (p_item == root && hide_root);
  1231. if (!skip && p_pos.y < item_h) {
  1232. // check event!
  1233. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1234. return -1;
  1235. }
  1236. if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
  1237. if (p_item->children)
  1238. p_item->set_collapsed(!p_item->is_collapsed());
  1239. return -1; //handled!
  1240. }
  1241. int x = p_pos.x;
  1242. /* find clicked column */
  1243. int col = -1;
  1244. int col_ofs = 0;
  1245. int col_width = 0;
  1246. for (int i = 0; i < columns.size(); i++) {
  1247. col_width = get_column_width(i);
  1248. if (p_item->cells[i].expand_right) {
  1249. int plus = 1;
  1250. while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text == "" && p_item->cells[i + plus].icon.is_null()) {
  1251. col_width += cache.hseparation;
  1252. col_width += get_column_width(i + plus);
  1253. plus++;
  1254. }
  1255. }
  1256. if (x > col_width) {
  1257. col_ofs += col_width;
  1258. x -= col_width;
  1259. continue;
  1260. }
  1261. col = i;
  1262. break;
  1263. }
  1264. if (col == -1)
  1265. return -1;
  1266. else if (col == 0) {
  1267. int margin = x_ofs + cache.item_margin; //-cache.hseparation;
  1268. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1269. col_width -= margin;
  1270. col_ofs += margin;
  1271. x -= margin;
  1272. } else {
  1273. col_width -= cache.hseparation;
  1274. x -= cache.hseparation;
  1275. }
  1276. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_children()) {
  1277. p_item->set_collapsed(!p_item->is_collapsed());
  1278. return -1; //collapse/uncollapse because nothing can be done with item
  1279. }
  1280. const TreeItem::Cell &c = p_item->cells[col];
  1281. bool already_selected = c.selected;
  1282. bool already_cursor = (p_item == selected_item) && col == selected_col;
  1283. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  1284. Ref<Texture> b = c.buttons[j].texture;
  1285. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1286. if (x > col_width - w) {
  1287. if (c.buttons[j].disabled) {
  1288. pressed_button = -1;
  1289. cache.click_type = Cache::CLICK_NONE;
  1290. return -1;
  1291. }
  1292. pressed_button = j;
  1293. cache.click_type = Cache::CLICK_BUTTON;
  1294. cache.click_index = j;
  1295. cache.click_id = c.buttons[j].id;
  1296. cache.click_item = p_item;
  1297. cache.click_column = col;
  1298. cache.click_pos = get_global_mouse_position() - get_global_position();
  1299. update();
  1300. //emit_signal("button_pressed");
  1301. return -1;
  1302. }
  1303. col_width -= w + cache.button_margin;
  1304. }
  1305. if (p_button == BUTTON_LEFT || (p_button == BUTTON_RIGHT && allow_rmb_select)) {
  1306. /* process selection */
  1307. if (p_doubleclick && (!c.editable || c.mode == TreeItem::CELL_MODE_CUSTOM || c.mode == TreeItem::CELL_MODE_ICON /*|| c.mode==TreeItem::CELL_MODE_CHECK*/)) { //it's confusing for check
  1308. propagate_mouse_activated = true;
  1309. incr_search.clear();
  1310. return -1;
  1311. }
  1312. if (select_mode == SELECT_MULTI && p_mod->get_command() && c.selectable) {
  1313. if (!c.selected || p_button == BUTTON_RIGHT) {
  1314. p_item->select(col);
  1315. emit_signal("multi_selected", p_item, col, true);
  1316. if (p_button == BUTTON_RIGHT) {
  1317. emit_signal("item_rmb_selected", get_local_mouse_position());
  1318. }
  1319. //p_item->selected_signal.call(col);
  1320. } else {
  1321. p_item->deselect(col);
  1322. emit_signal("multi_selected", p_item, col, false);
  1323. //p_item->deselected_signal.call(col);
  1324. }
  1325. } else {
  1326. if (c.selectable) {
  1327. if (select_mode == SELECT_MULTI && p_mod->get_shift() && selected_item && selected_item != p_item) {
  1328. bool inrange = false;
  1329. select_single_item(p_item, root, col, selected_item, &inrange);
  1330. if (p_button == BUTTON_RIGHT) {
  1331. emit_signal("item_rmb_selected", get_local_mouse_position());
  1332. }
  1333. } else {
  1334. int icount = _count_selected_items(root);
  1335. if (select_mode == SELECT_MULTI && icount > 1 && p_button != BUTTON_RIGHT) {
  1336. single_select_defer = p_item;
  1337. single_select_defer_column = col;
  1338. } else {
  1339. if (p_button != BUTTON_RIGHT || !c.selected) {
  1340. select_single_item(p_item, root, col);
  1341. }
  1342. if (p_button == BUTTON_RIGHT) {
  1343. emit_signal("item_rmb_selected", get_local_mouse_position());
  1344. }
  1345. }
  1346. }
  1347. /*
  1348. if (!c.selected && select_mode==SELECT_MULTI) {
  1349. emit_signal("multi_selected",p_item,col,true);
  1350. }
  1351. */
  1352. update();
  1353. }
  1354. }
  1355. }
  1356. if (!c.editable)
  1357. return -1; // if cell is not editable, don't bother
  1358. /* editing */
  1359. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  1360. String editor_text = c.text;
  1361. switch (c.mode) {
  1362. case TreeItem::CELL_MODE_STRING: {
  1363. //nothing in particular
  1364. if (select_mode == SELECT_MULTI && (get_tree()->get_event_count() == focus_in_id || !already_cursor)) {
  1365. bring_up_editor = false;
  1366. }
  1367. } break;
  1368. case TreeItem::CELL_MODE_CHECK: {
  1369. bring_up_editor = false; //checkboxes are not edited with editor
  1370. if (force_edit_checkbox_only_on_checkbox) {
  1371. if (x < cache.checked->get_width()) {
  1372. p_item->set_checked(col, !c.checked);
  1373. item_edited(col, p_item);
  1374. }
  1375. } else {
  1376. p_item->set_checked(col, !c.checked);
  1377. item_edited(col, p_item);
  1378. }
  1379. click_handled = true;
  1380. //p_item->edited_signal.call(col);
  1381. } break;
  1382. case TreeItem::CELL_MODE_RANGE: {
  1383. if (c.text != "") {
  1384. //if (x >= (get_column_width(col)-item_h/2)) {
  1385. popup_menu->clear();
  1386. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  1387. String s = c.text.get_slicec(',', i);
  1388. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.get_slicec(':', 1).to_int());
  1389. }
  1390. popup_menu->set_size(Size2(col_width, 0));
  1391. popup_menu->set_position(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset);
  1392. popup_menu->popup();
  1393. popup_edited_item = p_item;
  1394. popup_edited_item_col = col;
  1395. //}
  1396. bring_up_editor = false;
  1397. } else {
  1398. if (x >= (col_width - item_h / 2)) {
  1399. /* touching the combo */
  1400. bool up = p_pos.y < (item_h / 2);
  1401. if (p_button == BUTTON_LEFT) {
  1402. if (range_click_timer->get_time_left() == 0) {
  1403. range_item_last = p_item;
  1404. range_up_last = up;
  1405. range_click_timer->set_wait_time(0.6);
  1406. range_click_timer->set_one_shot(true);
  1407. range_click_timer->start();
  1408. } else if (up != range_up_last) {
  1409. return -1; // break. avoid changing direction on mouse held
  1410. }
  1411. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  1412. item_edited(col, p_item);
  1413. } else if (p_button == BUTTON_RIGHT) {
  1414. p_item->set_range(col, (up ? c.max : c.min));
  1415. item_edited(col, p_item);
  1416. } else if (p_button == BUTTON_WHEEL_UP) {
  1417. p_item->set_range(col, c.val + c.step);
  1418. item_edited(col, p_item);
  1419. } else if (p_button == BUTTON_WHEEL_DOWN) {
  1420. p_item->set_range(col, c.val - c.step);
  1421. item_edited(col, p_item);
  1422. }
  1423. //p_item->edited_signal.call(col);
  1424. bring_up_editor = false;
  1425. } else {
  1426. editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
  1427. if (select_mode == SELECT_MULTI && get_tree()->get_event_count() == focus_in_id)
  1428. bring_up_editor = false;
  1429. }
  1430. }
  1431. click_handled = true;
  1432. } break;
  1433. case TreeItem::CELL_MODE_ICON: {
  1434. bring_up_editor = false;
  1435. } break;
  1436. case TreeItem::CELL_MODE_CUSTOM: {
  1437. edited_item = p_item;
  1438. edited_col = col;
  1439. bool on_arrow = x > col_width - cache.select_arrow->get_width();
  1440. custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - cache.offset.y), Size2(get_column_width(col), item_h));
  1441. if (on_arrow || !p_item->cells[col].custom_button) {
  1442. emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2))));
  1443. }
  1444. if (!p_item->cells[col].custom_button || !on_arrow) {
  1445. item_edited(col, p_item, p_button == BUTTON_LEFT);
  1446. }
  1447. click_handled = true;
  1448. return -1;
  1449. } break;
  1450. };
  1451. if (!bring_up_editor || p_button != BUTTON_LEFT)
  1452. return -1;
  1453. click_handled = true;
  1454. popup_edited_item = p_item;
  1455. popup_edited_item_col = col;
  1456. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h));
  1457. pressing_for_editor_text = editor_text;
  1458. pressing_for_editor = true;
  1459. return -1; //select
  1460. } else {
  1461. Point2i new_pos = p_pos;
  1462. if (!skip) {
  1463. x_ofs += cache.item_margin;
  1464. //new_pos.x-=cache.item_margin;
  1465. y_ofs += item_h;
  1466. new_pos.y -= item_h;
  1467. }
  1468. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1469. TreeItem *c = p_item->children;
  1470. while (c) {
  1471. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_doubleclick, c, p_button, p_mod);
  1472. if (child_h < 0)
  1473. return -1; // break, stop propagating, no need to anymore
  1474. new_pos.y -= child_h;
  1475. y_ofs += child_h;
  1476. c = c->next;
  1477. item_h += child_h;
  1478. }
  1479. }
  1480. if (p_item == root && p_button == BUTTON_RIGHT) {
  1481. emit_signal("empty_rmb", get_local_mouse_position());
  1482. }
  1483. }
  1484. return item_h; // nothing found
  1485. }
  1486. void Tree::_text_editor_modal_close() {
  1487. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1488. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  1489. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  1490. return;
  1491. }
  1492. if (value_editor->has_point(value_editor->get_local_mouse_position()))
  1493. return;
  1494. text_editor_enter(text_editor->get_text());
  1495. }
  1496. void Tree::text_editor_enter(String p_text) {
  1497. text_editor->hide();
  1498. value_editor->hide();
  1499. if (!popup_edited_item)
  1500. return;
  1501. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1502. return;
  1503. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1504. switch (c.mode) {
  1505. case TreeItem::CELL_MODE_STRING: {
  1506. c.text = p_text;
  1507. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1508. } break;
  1509. case TreeItem::CELL_MODE_RANGE: {
  1510. c.val = p_text.to_double();
  1511. if (c.step > 0)
  1512. c.val = Math::stepify(c.val, c.step);
  1513. if (c.val < c.min)
  1514. c.val = c.min;
  1515. else if (c.val > c.max)
  1516. c.val = c.max;
  1517. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1518. } break;
  1519. default: {
  1520. ERR_FAIL();
  1521. }
  1522. }
  1523. item_edited(popup_edited_item_col, popup_edited_item);
  1524. update();
  1525. }
  1526. void Tree::value_editor_changed(double p_value) {
  1527. if (updating_value_editor) {
  1528. return;
  1529. }
  1530. if (!popup_edited_item) {
  1531. return;
  1532. }
  1533. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1534. c.val = p_value;
  1535. item_edited(popup_edited_item_col, popup_edited_item);
  1536. update();
  1537. }
  1538. void Tree::popup_select(int p_option) {
  1539. if (!popup_edited_item)
  1540. return;
  1541. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1542. return;
  1543. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  1544. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1545. update();
  1546. item_edited(popup_edited_item_col, popup_edited_item);
  1547. }
  1548. void Tree::_go_left() {
  1549. if (selected_col == 0) {
  1550. if (selected_item->get_children() != NULL && !selected_item->is_collapsed()) {
  1551. selected_item->set_collapsed(true);
  1552. } else {
  1553. if (columns.size() == 1) { // goto parent with one column
  1554. TreeItem *parent = selected_item->get_parent();
  1555. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  1556. select_single_item(parent, get_root(), selected_col);
  1557. }
  1558. } else if (selected_item->get_prev_visible()) {
  1559. selected_col = columns.size() - 1;
  1560. _go_up(); // go to upper column if possible
  1561. }
  1562. }
  1563. } else {
  1564. if (select_mode == SELECT_MULTI) {
  1565. selected_col--;
  1566. emit_signal("cell_selected");
  1567. } else {
  1568. selected_item->select(selected_col - 1);
  1569. }
  1570. }
  1571. update();
  1572. accept_event();
  1573. ensure_cursor_is_visible();
  1574. }
  1575. void Tree::_go_right() {
  1576. if (selected_col == (columns.size() - 1)) {
  1577. if (selected_item->get_children() != NULL && selected_item->is_collapsed()) {
  1578. selected_item->set_collapsed(false);
  1579. } else if (selected_item->get_next_visible()) {
  1580. selected_item->select(0);
  1581. _go_down();
  1582. return;
  1583. }
  1584. } else {
  1585. if (select_mode == SELECT_MULTI) {
  1586. selected_col++;
  1587. emit_signal("cell_selected");
  1588. } else {
  1589. selected_item->select(selected_col + 1);
  1590. }
  1591. }
  1592. update();
  1593. ensure_cursor_is_visible();
  1594. accept_event();
  1595. }
  1596. void Tree::_go_up() {
  1597. TreeItem *prev = NULL;
  1598. if (!selected_item) {
  1599. prev = get_last_item();
  1600. selected_col = 0;
  1601. } else {
  1602. prev = selected_item->get_prev_visible();
  1603. if (last_keypress != 0) {
  1604. //incr search next
  1605. int col;
  1606. prev = _search_item_text(prev, incr_search, &col, true, true);
  1607. if (!prev) {
  1608. accept_event();
  1609. return;
  1610. }
  1611. }
  1612. }
  1613. if (select_mode == SELECT_MULTI) {
  1614. if (!prev)
  1615. return;
  1616. selected_item = prev;
  1617. emit_signal("cell_selected");
  1618. update();
  1619. } else {
  1620. int col = selected_col < 0 ? 0 : selected_col;
  1621. while (prev && !prev->cells[col].selectable)
  1622. prev = prev->get_prev_visible();
  1623. if (!prev)
  1624. return; // do nothing..
  1625. prev->select(col);
  1626. }
  1627. ensure_cursor_is_visible();
  1628. accept_event();
  1629. }
  1630. void Tree::_go_down() {
  1631. TreeItem *next = NULL;
  1632. if (!selected_item) {
  1633. next = hide_root ? root->get_next_visible() : root;
  1634. selected_item = 0;
  1635. } else {
  1636. next = selected_item->get_next_visible();
  1637. if (last_keypress != 0) {
  1638. //incr search next
  1639. int col;
  1640. next = _search_item_text(next, incr_search, &col, true);
  1641. if (!next) {
  1642. accept_event();
  1643. return;
  1644. }
  1645. }
  1646. }
  1647. if (select_mode == SELECT_MULTI) {
  1648. if (!next) {
  1649. return;
  1650. }
  1651. selected_item = next;
  1652. emit_signal("cell_selected");
  1653. update();
  1654. } else {
  1655. int col = selected_col < 0 ? 0 : selected_col;
  1656. while (next && !next->cells[col].selectable)
  1657. next = next->get_next_visible();
  1658. if (!next) {
  1659. return; // do nothing..
  1660. }
  1661. next->select(col);
  1662. }
  1663. ensure_cursor_is_visible();
  1664. accept_event();
  1665. }
  1666. void Tree::_gui_input(Ref<InputEvent> p_event) {
  1667. Ref<InputEventKey> k = p_event;
  1668. bool is_command = k.is_valid() && k->get_command();
  1669. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  1670. if (!cursor_can_exit_tree) accept_event();
  1671. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  1672. return;
  1673. }
  1674. if (k.is_valid() && k->get_alt()) {
  1675. selected_item->set_collapsed(false);
  1676. TreeItem *next = selected_item->get_children();
  1677. while (next && next != selected_item->next) {
  1678. next->set_collapsed(false);
  1679. next = next->get_next_visible();
  1680. }
  1681. } else {
  1682. _go_right();
  1683. }
  1684. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  1685. if (!cursor_can_exit_tree) accept_event();
  1686. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  1687. return;
  1688. }
  1689. if (k.is_valid() && k->get_alt()) {
  1690. selected_item->set_collapsed(true);
  1691. TreeItem *next = selected_item->get_children();
  1692. while (next && next != selected_item->next) {
  1693. next->set_collapsed(true);
  1694. next = next->get_next_visible();
  1695. }
  1696. } else {
  1697. _go_left();
  1698. }
  1699. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  1700. if (!cursor_can_exit_tree) accept_event();
  1701. _go_up();
  1702. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  1703. if (!cursor_can_exit_tree) accept_event();
  1704. _go_down();
  1705. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  1706. if (!cursor_can_exit_tree) accept_event();
  1707. TreeItem *next = NULL;
  1708. if (!selected_item)
  1709. return;
  1710. next = selected_item;
  1711. for (int i = 0; i < 10; i++) {
  1712. TreeItem *_n = next->get_next_visible();
  1713. if (_n) {
  1714. next = _n;
  1715. } else {
  1716. break;
  1717. }
  1718. }
  1719. if (next == selected_item)
  1720. return;
  1721. if (select_mode == SELECT_MULTI) {
  1722. selected_item = next;
  1723. emit_signal("cell_selected");
  1724. update();
  1725. } else {
  1726. while (next && !next->cells[selected_col].selectable)
  1727. next = next->get_next_visible();
  1728. if (!next) {
  1729. return; // do nothing..
  1730. }
  1731. next->select(selected_col);
  1732. }
  1733. ensure_cursor_is_visible();
  1734. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  1735. if (!cursor_can_exit_tree) accept_event();
  1736. TreeItem *prev = NULL;
  1737. if (!selected_item)
  1738. return;
  1739. prev = selected_item;
  1740. for (int i = 0; i < 10; i++) {
  1741. TreeItem *_n = prev->get_prev_visible();
  1742. if (_n) {
  1743. prev = _n;
  1744. } else {
  1745. break;
  1746. }
  1747. }
  1748. if (prev == selected_item)
  1749. return;
  1750. if (select_mode == SELECT_MULTI) {
  1751. selected_item = prev;
  1752. emit_signal("cell_selected");
  1753. update();
  1754. } else {
  1755. while (prev && !prev->cells[selected_col].selectable)
  1756. prev = prev->get_prev_visible();
  1757. if (!prev) {
  1758. return; // do nothing..
  1759. }
  1760. prev->select(selected_col);
  1761. }
  1762. ensure_cursor_is_visible();
  1763. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  1764. if (selected_item) {
  1765. //bring up editor if possible
  1766. if (!edit_selected()) {
  1767. emit_signal("item_activated");
  1768. incr_search.clear();
  1769. }
  1770. }
  1771. accept_event();
  1772. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  1773. if (select_mode == SELECT_MULTI) {
  1774. if (!selected_item)
  1775. return;
  1776. if (selected_item->is_selected(selected_col)) {
  1777. selected_item->deselect(selected_col);
  1778. emit_signal("multi_selected", selected_item, selected_col, false);
  1779. } else if (selected_item->is_selectable(selected_col)) {
  1780. selected_item->select(selected_col);
  1781. emit_signal("multi_selected", selected_item, selected_col, true);
  1782. }
  1783. }
  1784. accept_event();
  1785. }
  1786. if (k.is_valid()) { // Incremental search
  1787. if (!k->is_pressed())
  1788. return;
  1789. if (k->get_command() || (k->get_shift() && k->get_unicode() == 0) || k->get_metakey())
  1790. return;
  1791. if (!root)
  1792. return;
  1793. if (hide_root && !root->get_next_visible())
  1794. return;
  1795. if (k->get_unicode() > 0) {
  1796. _do_incr_search(String::chr(k->get_unicode()));
  1797. accept_event();
  1798. return;
  1799. } else {
  1800. if (k->get_scancode() != KEY_SHIFT)
  1801. last_keypress = 0;
  1802. }
  1803. }
  1804. Ref<InputEventMouseMotion> mm = p_event;
  1805. if (mm.is_valid()) {
  1806. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1807. update_cache();
  1808. Ref<StyleBox> bg = cache.bg;
  1809. Point2 pos = mm->get_position() - bg->get_offset();
  1810. Cache::ClickType old_hover = cache.hover_type;
  1811. int old_index = cache.hover_index;
  1812. cache.hover_type = Cache::CLICK_NONE;
  1813. cache.hover_index = 0;
  1814. if (show_column_titles) {
  1815. pos.y -= _get_title_button_height();
  1816. if (pos.y < 0) {
  1817. pos.x += cache.offset.x;
  1818. int len = 0;
  1819. for (int i = 0; i < columns.size(); i++) {
  1820. len += get_column_width(i);
  1821. if (pos.x < len) {
  1822. cache.hover_type = Cache::CLICK_TITLE;
  1823. cache.hover_index = i;
  1824. update();
  1825. break;
  1826. }
  1827. }
  1828. }
  1829. }
  1830. if (root) {
  1831. Point2 mpos = mm->get_position();
  1832. mpos -= cache.bg->get_offset();
  1833. mpos.y -= _get_title_button_height();
  1834. if (mpos.y >= 0) {
  1835. if (h_scroll->is_visible_in_tree())
  1836. mpos.x += h_scroll->get_value();
  1837. if (v_scroll->is_visible_in_tree())
  1838. mpos.y += v_scroll->get_value();
  1839. int col, h, section;
  1840. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  1841. if (drop_mode_flags) {
  1842. if (it != drop_mode_over) {
  1843. drop_mode_over = it;
  1844. update();
  1845. }
  1846. if (it && section != drop_mode_section) {
  1847. drop_mode_section = section;
  1848. update();
  1849. }
  1850. }
  1851. if (it != cache.hover_item) {
  1852. cache.hover_item = it;
  1853. update();
  1854. }
  1855. if (it && col != cache.hover_cell) {
  1856. cache.hover_cell = col;
  1857. update();
  1858. }
  1859. }
  1860. }
  1861. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  1862. update();
  1863. }
  1864. if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE)) {
  1865. //range drag
  1866. if (!range_drag_enabled) {
  1867. Vector2 cpos = mm->get_position();
  1868. if (cpos.distance_to(pressing_pos) > 2) {
  1869. range_drag_enabled = true;
  1870. range_drag_capture_pos = cpos;
  1871. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  1872. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1873. }
  1874. } else {
  1875. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1876. float diff_y = -mm->get_relative().y;
  1877. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  1878. diff_y *= 0.1;
  1879. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1880. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  1881. item_edited(popup_edited_item_col, popup_edited_item);
  1882. }
  1883. }
  1884. if (drag_touching && !drag_touching_deaccel) {
  1885. drag_accum -= mm->get_relative().y;
  1886. v_scroll->set_value(drag_from + drag_accum);
  1887. drag_speed = -mm->get_speed().y;
  1888. }
  1889. }
  1890. Ref<InputEventMouseButton> b = p_event;
  1891. if (b.is_valid()) {
  1892. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1893. update_cache();
  1894. if (!b->is_pressed()) {
  1895. if (b->get_button_index() == BUTTON_LEFT) {
  1896. Point2 pos = b->get_position() - cache.bg->get_offset();
  1897. if (show_column_titles) {
  1898. pos.y -= _get_title_button_height();
  1899. if (pos.y < 0) {
  1900. pos.x += cache.offset.x;
  1901. int len = 0;
  1902. for (int i = 0; i < columns.size(); i++) {
  1903. len += get_column_width(i);
  1904. if (pos.x < len) {
  1905. emit_signal("column_title_pressed", i);
  1906. break;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. if (single_select_defer) {
  1912. select_single_item(single_select_defer, root, single_select_defer_column);
  1913. single_select_defer = NULL;
  1914. }
  1915. range_click_timer->stop();
  1916. if (pressing_for_editor) {
  1917. if (range_drag_enabled) {
  1918. range_drag_enabled = false;
  1919. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1920. warp_mouse(range_drag_capture_pos);
  1921. } else {
  1922. Rect2 rect = get_selected()->get_meta("__focus_rect");
  1923. if (rect.has_point(Point2(b->get_position().x, b->get_position().y))) {
  1924. edit_selected();
  1925. } else {
  1926. emit_signal("item_double_clicked");
  1927. }
  1928. }
  1929. pressing_for_editor = false;
  1930. }
  1931. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != NULL) {
  1932. // make sure in case of wrong reference after reconstructing whole TreeItems
  1933. cache.click_item = get_item_at_position(cache.click_pos);
  1934. emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id);
  1935. }
  1936. cache.click_type = Cache::CLICK_NONE;
  1937. cache.click_index = -1;
  1938. cache.click_id = -1;
  1939. cache.click_item = NULL;
  1940. cache.click_column = 0;
  1941. if (drag_touching) {
  1942. if (drag_speed == 0) {
  1943. drag_touching_deaccel = false;
  1944. drag_touching = false;
  1945. set_physics_process_internal(false);
  1946. } else {
  1947. drag_touching_deaccel = true;
  1948. }
  1949. }
  1950. update();
  1951. }
  1952. return;
  1953. }
  1954. if (range_drag_enabled)
  1955. return;
  1956. switch (b->get_button_index()) {
  1957. case BUTTON_RIGHT:
  1958. case BUTTON_LEFT: {
  1959. Ref<StyleBox> bg = cache.bg;
  1960. Point2 pos = b->get_position() - bg->get_offset();
  1961. cache.click_type = Cache::CLICK_NONE;
  1962. if (show_column_titles) {
  1963. pos.y -= _get_title_button_height();
  1964. if (pos.y < 0) {
  1965. if (b->get_button_index() == BUTTON_LEFT) {
  1966. pos.x += cache.offset.x;
  1967. int len = 0;
  1968. for (int i = 0; i < columns.size(); i++) {
  1969. len += get_column_width(i);
  1970. if (pos.x < len) {
  1971. cache.click_type = Cache::CLICK_TITLE;
  1972. cache.click_index = i;
  1973. //cache.click_id=;
  1974. update();
  1975. break;
  1976. }
  1977. }
  1978. }
  1979. break;
  1980. }
  1981. }
  1982. if (!root || (!root->get_children() && hide_root)) {
  1983. if (b->get_button_index() == BUTTON_RIGHT && allow_rmb_select) {
  1984. emit_signal("empty_tree_rmb_selected", get_local_mouse_position());
  1985. }
  1986. break;
  1987. }
  1988. click_handled = false;
  1989. pressing_for_editor = false;
  1990. propagate_mouse_activated = false;
  1991. blocked++;
  1992. propagate_mouse_event(pos + cache.offset, 0, 0, b->is_doubleclick(), root, b->get_button_index(), b);
  1993. blocked--;
  1994. if (pressing_for_editor) {
  1995. pressing_pos = b->get_position();
  1996. }
  1997. if (b->get_button_index() == BUTTON_RIGHT)
  1998. break;
  1999. if (drag_touching) {
  2000. set_physics_process_internal(false);
  2001. drag_touching_deaccel = false;
  2002. drag_touching = false;
  2003. drag_speed = 0;
  2004. drag_from = 0;
  2005. }
  2006. if (!click_handled) {
  2007. drag_speed = 0;
  2008. drag_accum = 0;
  2009. //last_drag_accum=0;
  2010. drag_from = v_scroll->get_value();
  2011. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  2012. drag_touching_deaccel = false;
  2013. if (drag_touching) {
  2014. set_physics_process_internal(true);
  2015. }
  2016. if (b->get_button_index() == BUTTON_LEFT) {
  2017. if (get_item_at_position(b->get_position()) == NULL && !b->get_shift() && !b->get_control() && !b->get_command())
  2018. emit_signal("nothing_selected");
  2019. }
  2020. }
  2021. if (propagate_mouse_activated) {
  2022. emit_signal("item_activated");
  2023. propagate_mouse_activated = false;
  2024. }
  2025. } break;
  2026. case BUTTON_WHEEL_UP: {
  2027. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  2028. } break;
  2029. case BUTTON_WHEEL_DOWN: {
  2030. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  2031. } break;
  2032. }
  2033. }
  2034. Ref<InputEventPanGesture> pan_gesture = p_event;
  2035. if (pan_gesture.is_valid()) {
  2036. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  2037. }
  2038. }
  2039. bool Tree::edit_selected() {
  2040. TreeItem *s = get_selected();
  2041. ERR_FAIL_COND_V_MSG(!s, false, "No item selected.");
  2042. ensure_cursor_is_visible();
  2043. int col = get_selected_column();
  2044. ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
  2045. if (!s->cells[col].editable)
  2046. return false;
  2047. Rect2 rect = s->get_meta("__focus_rect");
  2048. popup_edited_item = s;
  2049. popup_edited_item_col = col;
  2050. const TreeItem::Cell &c = s->cells[col];
  2051. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  2052. s->set_checked(col, !c.checked);
  2053. item_edited(col, s);
  2054. return true;
  2055. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  2056. edited_item = s;
  2057. edited_col = col;
  2058. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  2059. emit_signal("custom_popup_edited", false);
  2060. item_edited(col, s);
  2061. return true;
  2062. } else if (c.mode == TreeItem::CELL_MODE_RANGE && c.text != "") {
  2063. popup_menu->clear();
  2064. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2065. String s2 = c.text.get_slicec(',', i);
  2066. popup_menu->add_item(s2.get_slicec(':', 0), s2.get_slicec(':', 1).empty() ? i : s2.get_slicec(':', 1).to_int());
  2067. }
  2068. popup_menu->set_size(Size2(rect.size.width, 0));
  2069. popup_menu->set_position(get_global_position() + rect.position + Point2i(0, rect.size.height));
  2070. popup_menu->popup();
  2071. popup_edited_item = s;
  2072. popup_edited_item_col = col;
  2073. return true;
  2074. } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE) {
  2075. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2076. Point2i textedpos = get_global_position() + rect.position - ofs;
  2077. cache.text_editor_position = textedpos;
  2078. text_editor->set_position(textedpos);
  2079. text_editor->set_size(rect.size);
  2080. text_editor->clear();
  2081. text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
  2082. text_editor->select_all();
  2083. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  2084. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2085. value_editor->set_size(Size2(rect.size.width, 1));
  2086. value_editor->show_modal();
  2087. updating_value_editor = true;
  2088. value_editor->set_min(c.min);
  2089. value_editor->set_max(c.max);
  2090. value_editor->set_step(c.step);
  2091. value_editor->set_value(c.val);
  2092. value_editor->set_exp_ratio(c.expr);
  2093. updating_value_editor = false;
  2094. }
  2095. text_editor->show_modal();
  2096. text_editor->grab_focus();
  2097. return true;
  2098. }
  2099. return false;
  2100. }
  2101. Size2 Tree::get_internal_min_size() const {
  2102. Size2i size = cache.bg->get_offset();
  2103. if (root)
  2104. size.height += get_item_height(root);
  2105. for (int i = 0; i < columns.size(); i++) {
  2106. size.width += columns[i].min_width;
  2107. }
  2108. return size;
  2109. }
  2110. void Tree::update_scrollbars() {
  2111. Size2 size = get_size();
  2112. int tbh;
  2113. if (show_column_titles) {
  2114. tbh = _get_title_button_height();
  2115. } else {
  2116. tbh = 0;
  2117. }
  2118. Size2 hmin = h_scroll->get_combined_minimum_size();
  2119. Size2 vmin = v_scroll->get_combined_minimum_size();
  2120. v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(MARGIN_TOP)));
  2121. v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(MARGIN_TOP) - cache.bg->get_margin(MARGIN_BOTTOM)));
  2122. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  2123. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  2124. Size2 min = get_internal_min_size();
  2125. if (min.height < size.height - hmin.height) {
  2126. v_scroll->hide();
  2127. cache.offset.y = 0;
  2128. } else {
  2129. v_scroll->show();
  2130. v_scroll->set_max(min.height);
  2131. v_scroll->set_page(size.height - hmin.height - tbh);
  2132. cache.offset.y = v_scroll->get_value();
  2133. }
  2134. if (min.width < size.width - vmin.width) {
  2135. h_scroll->hide();
  2136. cache.offset.x = 0;
  2137. } else {
  2138. h_scroll->show();
  2139. h_scroll->set_max(min.width);
  2140. h_scroll->set_page(size.width - vmin.width);
  2141. cache.offset.x = h_scroll->get_value();
  2142. }
  2143. }
  2144. int Tree::_get_title_button_height() const {
  2145. ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
  2146. return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
  2147. }
  2148. void Tree::_notification(int p_what) {
  2149. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2150. focus_in_id = get_tree()->get_event_count();
  2151. }
  2152. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2153. if (cache.hover_type != Cache::CLICK_NONE) {
  2154. cache.hover_type = Cache::CLICK_NONE;
  2155. update();
  2156. }
  2157. }
  2158. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2159. drag_touching = false;
  2160. }
  2161. if (p_what == NOTIFICATION_ENTER_TREE) {
  2162. update_cache();
  2163. }
  2164. if (p_what == NOTIFICATION_DRAG_END) {
  2165. drop_mode_flags = 0;
  2166. scrolling = false;
  2167. set_physics_process_internal(false);
  2168. update();
  2169. }
  2170. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2171. single_select_defer = NULL;
  2172. if (cache.scroll_speed > 0) {
  2173. scrolling = true;
  2174. set_physics_process_internal(true);
  2175. }
  2176. }
  2177. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  2178. if (drag_touching) {
  2179. if (drag_touching_deaccel) {
  2180. float pos = v_scroll->get_value();
  2181. pos += drag_speed * get_physics_process_delta_time();
  2182. bool turnoff = false;
  2183. if (pos < 0) {
  2184. pos = 0;
  2185. turnoff = true;
  2186. set_physics_process_internal(false);
  2187. drag_touching = false;
  2188. drag_touching_deaccel = false;
  2189. }
  2190. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2191. pos = v_scroll->get_max() - v_scroll->get_page();
  2192. turnoff = true;
  2193. }
  2194. v_scroll->set_value(pos);
  2195. float sgn = drag_speed < 0 ? -1 : 1;
  2196. float val = Math::abs(drag_speed);
  2197. val -= 1000 * get_physics_process_delta_time();
  2198. if (val < 0) {
  2199. turnoff = true;
  2200. }
  2201. drag_speed = sgn * val;
  2202. if (turnoff) {
  2203. set_physics_process_internal(false);
  2204. drag_touching = false;
  2205. drag_touching_deaccel = false;
  2206. }
  2207. }
  2208. }
  2209. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  2210. if (scrolling && get_rect().grow(cache.scroll_border).has_point(mouse_position)) {
  2211. Point2 point;
  2212. if ((ABS(mouse_position.x) < ABS(mouse_position.x - get_size().width)) && (ABS(mouse_position.x) < cache.scroll_border)) {
  2213. point.x = mouse_position.x - cache.scroll_border;
  2214. } else if (ABS(mouse_position.x - get_size().width) < cache.scroll_border) {
  2215. point.x = mouse_position.x - (get_size().width - cache.scroll_border);
  2216. }
  2217. if ((ABS(mouse_position.y) < ABS(mouse_position.y - get_size().height)) && (ABS(mouse_position.y) < cache.scroll_border)) {
  2218. point.y = mouse_position.y - cache.scroll_border;
  2219. } else if (ABS(mouse_position.y - get_size().height) < cache.scroll_border) {
  2220. point.y = mouse_position.y - (get_size().height - cache.scroll_border);
  2221. }
  2222. point *= cache.scroll_speed * get_physics_process_delta_time();
  2223. point += get_scroll();
  2224. h_scroll->set_value(point.x);
  2225. v_scroll->set_value(point.y);
  2226. }
  2227. }
  2228. if (p_what == NOTIFICATION_DRAW) {
  2229. update_cache();
  2230. update_scrollbars();
  2231. RID ci = get_canvas_item();
  2232. Ref<StyleBox> bg = cache.bg;
  2233. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  2234. Point2 draw_ofs;
  2235. draw_ofs += bg->get_offset();
  2236. Size2 draw_size = get_size() - bg->get_minimum_size();
  2237. bg->draw(ci, Rect2(Point2(), get_size()));
  2238. if (has_focus()) {
  2239. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2240. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2241. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2242. }
  2243. int tbh = _get_title_button_height();
  2244. draw_ofs.y += tbh;
  2245. draw_size.y -= tbh;
  2246. if (root) {
  2247. draw_item(Point2(), draw_ofs, draw_size, root);
  2248. }
  2249. int ofs = 0;
  2250. for (int i = 0; i < (columns.size() - 1 - 1); i++) {
  2251. ofs += get_column_width(i);
  2252. }
  2253. if (show_column_titles) {
  2254. //title buttons
  2255. int ofs2 = cache.bg->get_margin(MARGIN_LEFT);
  2256. for (int i = 0; i < columns.size(); i++) {
  2257. Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? cache.title_button_pressed : ((cache.hover_type == Cache::CLICK_TITLE && cache.hover_index == i) ? cache.title_button_hover : cache.title_button);
  2258. Ref<Font> f = cache.tb_font;
  2259. Rect2 tbrect = Rect2(ofs2 - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh);
  2260. sb->draw(ci, tbrect);
  2261. ofs2 += tbrect.size.width;
  2262. //text
  2263. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2264. f->draw_halign(ci, tbrect.position + Point2i(sb->get_offset().x, (tbrect.size.height - f->get_height()) / 2 + f->get_ascent()), HALIGN_CENTER, clip_w, columns[i].title, cache.title_button_color);
  2265. }
  2266. }
  2267. }
  2268. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2269. update_cache();
  2270. }
  2271. if (p_what == NOTIFICATION_RESIZED || p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  2272. if (popup_edited_item != NULL) {
  2273. Rect2 rect = popup_edited_item->get_meta("__focus_rect");
  2274. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2275. Point2i textedpos = get_global_position() + rect.position - ofs;
  2276. if (cache.text_editor_position != textedpos) {
  2277. cache.text_editor_position = textedpos;
  2278. text_editor->set_position(textedpos);
  2279. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2280. }
  2281. }
  2282. }
  2283. }
  2284. Size2 Tree::get_minimum_size() const {
  2285. return Size2(1, 1);
  2286. }
  2287. TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
  2288. ERR_FAIL_COND_V(blocked > 0, NULL);
  2289. TreeItem *ti = NULL;
  2290. if (p_parent) {
  2291. // Append or insert a new item to the given parent.
  2292. ti = memnew(TreeItem(this));
  2293. ERR_FAIL_COND_V(!ti, NULL);
  2294. ti->cells.resize(columns.size());
  2295. TreeItem *prev = NULL;
  2296. TreeItem *c = p_parent->children;
  2297. int idx = 0;
  2298. while (c) {
  2299. if (idx++ == p_idx) {
  2300. ti->next = c;
  2301. break;
  2302. }
  2303. prev = c;
  2304. c = c->next;
  2305. }
  2306. if (prev)
  2307. prev->next = ti;
  2308. else
  2309. p_parent->children = ti;
  2310. ti->parent = p_parent;
  2311. } else {
  2312. if (!root) {
  2313. // No root exists, make the given item the new root.
  2314. ti = memnew(TreeItem(this));
  2315. ERR_FAIL_COND_V(!ti, NULL);
  2316. ti->cells.resize(columns.size());
  2317. root = ti;
  2318. } else {
  2319. // Root exists, append or insert to root.
  2320. ti = create_item(root, p_idx);
  2321. }
  2322. }
  2323. return ti;
  2324. }
  2325. TreeItem *Tree::get_root() {
  2326. return root;
  2327. }
  2328. TreeItem *Tree::get_last_item() {
  2329. TreeItem *last = root;
  2330. while (last) {
  2331. if (last->next)
  2332. last = last->next;
  2333. else if (last->children)
  2334. last = last->children;
  2335. else
  2336. break;
  2337. }
  2338. return last;
  2339. }
  2340. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2341. edited_item = p_item;
  2342. edited_col = p_column;
  2343. if (p_lmb)
  2344. emit_signal("item_edited");
  2345. else
  2346. emit_signal("item_rmb_edited");
  2347. }
  2348. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2349. update();
  2350. }
  2351. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2352. if (select_mode == SELECT_MULTI) {
  2353. if (!p_item->cells[p_column].selectable)
  2354. return;
  2355. p_item->cells.write[p_column].selected = true;
  2356. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2357. selected_col = p_column;
  2358. } else {
  2359. select_single_item(p_item, root, p_column);
  2360. }
  2361. update();
  2362. }
  2363. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  2364. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  2365. p_item->cells.write[p_column].selected = false;
  2366. }
  2367. update();
  2368. }
  2369. void Tree::set_select_mode(SelectMode p_mode) {
  2370. select_mode = p_mode;
  2371. }
  2372. Tree::SelectMode Tree::get_select_mode() const {
  2373. return select_mode;
  2374. }
  2375. void Tree::deselect_all() {
  2376. TreeItem *item = get_next_selected(get_root());
  2377. while (item) {
  2378. item->deselect(selected_col);
  2379. TreeItem *prev_item = item;
  2380. item = get_next_selected(get_root());
  2381. ERR_FAIL_COND(item == prev_item);
  2382. }
  2383. selected_item = NULL;
  2384. selected_col = -1;
  2385. update();
  2386. }
  2387. bool Tree::is_anything_selected() {
  2388. return (selected_item != NULL);
  2389. }
  2390. void Tree::clear() {
  2391. ERR_FAIL_COND(blocked > 0);
  2392. if (pressing_for_editor) {
  2393. if (range_drag_enabled) {
  2394. range_drag_enabled = false;
  2395. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2396. warp_mouse(range_drag_capture_pos);
  2397. }
  2398. pressing_for_editor = false;
  2399. }
  2400. if (root) {
  2401. memdelete(root);
  2402. root = NULL;
  2403. };
  2404. selected_item = NULL;
  2405. edited_item = NULL;
  2406. popup_edited_item = NULL;
  2407. update();
  2408. };
  2409. void Tree::set_hide_root(bool p_enabled) {
  2410. hide_root = p_enabled;
  2411. update();
  2412. }
  2413. bool Tree::is_root_hidden() const {
  2414. return hide_root;
  2415. }
  2416. void Tree::set_column_min_width(int p_column, int p_min_width) {
  2417. ERR_FAIL_INDEX(p_column, columns.size());
  2418. if (p_min_width < 1)
  2419. return;
  2420. columns.write[p_column].min_width = p_min_width;
  2421. update();
  2422. }
  2423. void Tree::set_column_expand(int p_column, bool p_expand) {
  2424. ERR_FAIL_INDEX(p_column, columns.size());
  2425. columns.write[p_column].expand = p_expand;
  2426. update();
  2427. }
  2428. TreeItem *Tree::get_selected() const {
  2429. return selected_item;
  2430. }
  2431. int Tree::get_selected_column() const {
  2432. return selected_col;
  2433. }
  2434. TreeItem *Tree::get_edited() const {
  2435. return edited_item;
  2436. }
  2437. int Tree::get_edited_column() const {
  2438. return edited_col;
  2439. }
  2440. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  2441. /*
  2442. if (!p_item)
  2443. return NULL;
  2444. */
  2445. if (!root)
  2446. return NULL;
  2447. while (true) {
  2448. if (!p_item) {
  2449. p_item = root;
  2450. } else {
  2451. if (p_item->children) {
  2452. p_item = p_item->children;
  2453. } else if (p_item->next) {
  2454. p_item = p_item->next;
  2455. } else {
  2456. while (!p_item->next) {
  2457. p_item = p_item->parent;
  2458. if (p_item == NULL)
  2459. return NULL;
  2460. }
  2461. p_item = p_item->next;
  2462. }
  2463. }
  2464. for (int i = 0; i < columns.size(); i++)
  2465. if (p_item->cells[i].selected)
  2466. return p_item;
  2467. }
  2468. return NULL;
  2469. }
  2470. int Tree::get_column_width(int p_column) const {
  2471. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  2472. if (!columns[p_column].expand)
  2473. return columns[p_column].min_width;
  2474. Ref<StyleBox> bg = cache.bg;
  2475. int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT));
  2476. if (v_scroll->is_visible_in_tree())
  2477. expand_area -= v_scroll->get_combined_minimum_size().width;
  2478. int expanding_columns = 0;
  2479. int expanding_total = 0;
  2480. for (int i = 0; i < columns.size(); i++) {
  2481. if (!columns[i].expand) {
  2482. expand_area -= columns[i].min_width;
  2483. } else {
  2484. expanding_total += columns[i].min_width;
  2485. expanding_columns++;
  2486. }
  2487. }
  2488. if (expand_area < expanding_total)
  2489. return columns[p_column].min_width;
  2490. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  2491. return expand_area * columns[p_column].min_width / expanding_total;
  2492. }
  2493. void Tree::propagate_set_columns(TreeItem *p_item) {
  2494. p_item->cells.resize(columns.size());
  2495. TreeItem *c = p_item->get_children();
  2496. while (c) {
  2497. propagate_set_columns(c);
  2498. c = c->get_next();
  2499. }
  2500. }
  2501. void Tree::set_columns(int p_columns) {
  2502. ERR_FAIL_COND(p_columns < 1);
  2503. ERR_FAIL_COND(blocked > 0);
  2504. columns.resize(p_columns);
  2505. if (root)
  2506. propagate_set_columns(root);
  2507. if (selected_col >= p_columns)
  2508. selected_col = p_columns - 1;
  2509. update();
  2510. }
  2511. int Tree::get_columns() const {
  2512. return columns.size();
  2513. }
  2514. void Tree::_scroll_moved(float) {
  2515. update();
  2516. }
  2517. Rect2 Tree::get_custom_popup_rect() const {
  2518. return custom_popup_rect;
  2519. }
  2520. int Tree::get_item_offset(TreeItem *p_item) const {
  2521. TreeItem *it = root;
  2522. int ofs = _get_title_button_height();
  2523. if (!it)
  2524. return 0;
  2525. while (true) {
  2526. if (it == p_item)
  2527. return ofs;
  2528. ofs += compute_item_height(it) + cache.vseparation;
  2529. if (it->children && !it->collapsed) {
  2530. it = it->children;
  2531. } else if (it->next) {
  2532. it = it->next;
  2533. } else {
  2534. while (!it->next) {
  2535. it = it->parent;
  2536. if (it == NULL)
  2537. return 0;
  2538. }
  2539. it = it->next;
  2540. }
  2541. }
  2542. return -1; //not found
  2543. }
  2544. void Tree::ensure_cursor_is_visible() {
  2545. if (!is_inside_tree())
  2546. return;
  2547. TreeItem *selected = get_selected();
  2548. if (!selected)
  2549. return;
  2550. int ofs = get_item_offset(selected);
  2551. if (ofs == -1)
  2552. return;
  2553. int h = compute_item_height(selected) + cache.vseparation;
  2554. int screenh = get_size().height - h_scroll->get_combined_minimum_size().height;
  2555. if (h > screenh) { //screen size is too small, maybe it was not resized yet.
  2556. v_scroll->set_value(ofs);
  2557. } else if (ofs + h > v_scroll->get_value() + screenh) {
  2558. v_scroll->call_deferred("set_value", ofs - screenh + h);
  2559. } else if (ofs < v_scroll->get_value()) {
  2560. v_scroll->set_value(ofs);
  2561. }
  2562. }
  2563. int Tree::get_pressed_button() const {
  2564. return pressed_button;
  2565. }
  2566. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  2567. ERR_FAIL_NULL_V(p_item, Rect2());
  2568. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  2569. if (p_column != -1) {
  2570. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  2571. }
  2572. int ofs = get_item_offset(p_item);
  2573. int height = compute_item_height(p_item);
  2574. Rect2 r;
  2575. r.position.y = ofs;
  2576. r.size.height = height;
  2577. if (p_column == -1) {
  2578. r.position.x = 0;
  2579. r.size.x = get_size().width;
  2580. } else {
  2581. int accum = 0;
  2582. for (int i = 0; i < p_column; i++) {
  2583. accum += get_column_width(i);
  2584. }
  2585. r.position.x = accum;
  2586. r.size.x = get_column_width(p_column);
  2587. }
  2588. return r;
  2589. }
  2590. void Tree::set_column_titles_visible(bool p_show) {
  2591. show_column_titles = p_show;
  2592. update();
  2593. }
  2594. bool Tree::are_column_titles_visible() const {
  2595. return show_column_titles;
  2596. }
  2597. void Tree::set_column_title(int p_column, const String &p_title) {
  2598. ERR_FAIL_INDEX(p_column, columns.size());
  2599. columns.write[p_column].title = p_title;
  2600. update();
  2601. }
  2602. String Tree::get_column_title(int p_column) const {
  2603. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  2604. return columns[p_column].title;
  2605. }
  2606. Point2 Tree::get_scroll() const {
  2607. Point2 ofs;
  2608. if (h_scroll->is_visible_in_tree())
  2609. ofs.x = h_scroll->get_value();
  2610. if (v_scroll->is_visible_in_tree())
  2611. ofs.y = v_scroll->get_value();
  2612. return ofs;
  2613. }
  2614. void Tree::scroll_to_item(TreeItem *p_item) {
  2615. if (!is_visible_in_tree()) {
  2616. // hack to work around crash in get_item_rect() if Tree is not in tree.
  2617. return;
  2618. }
  2619. // make sure the scrollbar min and max are up to date with latest changes.
  2620. update_scrollbars();
  2621. const Rect2 r = get_item_rect(p_item);
  2622. if (r.position.y < v_scroll->get_value()) {
  2623. v_scroll->set_value(r.position.y);
  2624. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  2625. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  2626. }
  2627. }
  2628. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  2629. TreeItem *from = p_at;
  2630. while (p_at) {
  2631. for (int i = 0; i < columns.size(); i++) {
  2632. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  2633. if (r_col)
  2634. *r_col = i;
  2635. return p_at;
  2636. }
  2637. }
  2638. if (p_backwards)
  2639. p_at = p_at->get_prev_visible(true);
  2640. else
  2641. p_at = p_at->get_next_visible(true);
  2642. if ((p_at) == from)
  2643. break;
  2644. }
  2645. return NULL;
  2646. }
  2647. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  2648. TreeItem *from = get_selected();
  2649. if (!from)
  2650. from = root;
  2651. if (!from)
  2652. return NULL;
  2653. return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
  2654. }
  2655. void Tree::_do_incr_search(const String &p_add) {
  2656. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2657. uint64_t diff = time - last_keypress;
  2658. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)))
  2659. incr_search = p_add;
  2660. else if (incr_search != p_add)
  2661. incr_search += p_add;
  2662. last_keypress = time;
  2663. int col;
  2664. TreeItem *item = search_item_text(incr_search, &col, true);
  2665. if (!item)
  2666. return;
  2667. item->select(col);
  2668. ensure_cursor_is_visible();
  2669. }
  2670. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  2671. Point2 pos = p_pos;
  2672. if (root != p_item || !hide_root) {
  2673. h = compute_item_height(p_item) + cache.vseparation;
  2674. if (pos.y < h) {
  2675. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  2676. section = 0;
  2677. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  2678. section = pos.y < h / 2 ? -1 : 1;
  2679. } else if (pos.y < h / 4) {
  2680. section = -1;
  2681. } else if (pos.y >= (h * 3 / 4)) {
  2682. section = 1;
  2683. } else {
  2684. section = 0;
  2685. }
  2686. for (int i = 0; i < columns.size(); i++) {
  2687. int w = get_column_width(i);
  2688. if (pos.x < w) {
  2689. r_column = i;
  2690. return p_item;
  2691. }
  2692. pos.x -= w;
  2693. }
  2694. return NULL;
  2695. } else {
  2696. pos.y -= h;
  2697. }
  2698. } else {
  2699. h = 0;
  2700. }
  2701. if (p_item->is_collapsed())
  2702. return NULL; // do not try children, it's collapsed
  2703. TreeItem *n = p_item->get_children();
  2704. while (n) {
  2705. int ch;
  2706. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  2707. pos.y -= ch;
  2708. h += ch;
  2709. if (r)
  2710. return r;
  2711. n = n->get_next();
  2712. }
  2713. return NULL;
  2714. }
  2715. int Tree::get_column_at_position(const Point2 &p_pos) const {
  2716. if (root) {
  2717. Point2 pos = p_pos;
  2718. pos -= cache.bg->get_offset();
  2719. pos.y -= _get_title_button_height();
  2720. if (pos.y < 0)
  2721. return -1;
  2722. if (h_scroll->is_visible_in_tree())
  2723. pos.x += h_scroll->get_value();
  2724. if (v_scroll->is_visible_in_tree())
  2725. pos.y += v_scroll->get_value();
  2726. int col, h, section;
  2727. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2728. if (it) {
  2729. return col;
  2730. }
  2731. }
  2732. return -1;
  2733. }
  2734. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  2735. if (root) {
  2736. Point2 pos = p_pos;
  2737. pos -= cache.bg->get_offset();
  2738. pos.y -= _get_title_button_height();
  2739. if (pos.y < 0)
  2740. return -100;
  2741. if (h_scroll->is_visible_in_tree())
  2742. pos.x += h_scroll->get_value();
  2743. if (v_scroll->is_visible_in_tree())
  2744. pos.y += v_scroll->get_value();
  2745. int col, h, section;
  2746. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2747. if (it) {
  2748. return section;
  2749. }
  2750. }
  2751. return -100;
  2752. }
  2753. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  2754. if (root) {
  2755. Point2 pos = p_pos;
  2756. pos -= cache.bg->get_offset();
  2757. pos.y -= _get_title_button_height();
  2758. if (pos.y < 0)
  2759. return NULL;
  2760. if (h_scroll->is_visible_in_tree())
  2761. pos.x += h_scroll->get_value();
  2762. if (v_scroll->is_visible_in_tree())
  2763. pos.y += v_scroll->get_value();
  2764. int col, h, section;
  2765. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2766. if (it) {
  2767. return it;
  2768. }
  2769. }
  2770. return NULL;
  2771. }
  2772. String Tree::get_tooltip(const Point2 &p_pos) const {
  2773. if (root) {
  2774. Point2 pos = p_pos;
  2775. pos -= cache.bg->get_offset();
  2776. pos.y -= _get_title_button_height();
  2777. if (pos.y < 0)
  2778. return Control::get_tooltip(p_pos);
  2779. if (h_scroll->is_visible_in_tree())
  2780. pos.x += h_scroll->get_value();
  2781. if (v_scroll->is_visible_in_tree())
  2782. pos.y += v_scroll->get_value();
  2783. int col, h, section;
  2784. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2785. if (it) {
  2786. const TreeItem::Cell &c = it->cells[col];
  2787. int col_width = get_column_width(col);
  2788. for (int i = 0; i < col; i++)
  2789. pos.x -= get_column_width(i);
  2790. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2791. Ref<Texture> b = c.buttons[j].texture;
  2792. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  2793. if (pos.x > col_width - size.width) {
  2794. String tooltip = c.buttons[j].tooltip;
  2795. if (tooltip != "") {
  2796. return tooltip;
  2797. }
  2798. }
  2799. col_width -= size.width;
  2800. }
  2801. String ret;
  2802. if (it->get_tooltip(col) == "")
  2803. ret = it->get_text(col);
  2804. else
  2805. ret = it->get_tooltip(col);
  2806. return ret;
  2807. }
  2808. }
  2809. return Control::get_tooltip(p_pos);
  2810. }
  2811. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2812. cursor_can_exit_tree = p_enable;
  2813. }
  2814. bool Tree::can_cursor_exit_tree() const {
  2815. return cursor_can_exit_tree;
  2816. }
  2817. void Tree::set_hide_folding(bool p_hide) {
  2818. hide_folding = p_hide;
  2819. update();
  2820. }
  2821. bool Tree::is_folding_hidden() const {
  2822. return hide_folding;
  2823. }
  2824. void Tree::set_drop_mode_flags(int p_flags) {
  2825. if (drop_mode_flags == p_flags)
  2826. return;
  2827. drop_mode_flags = p_flags;
  2828. if (drop_mode_flags == 0) {
  2829. drop_mode_over = NULL;
  2830. }
  2831. update();
  2832. }
  2833. int Tree::get_drop_mode_flags() const {
  2834. return drop_mode_flags;
  2835. }
  2836. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  2837. force_edit_checkbox_only_on_checkbox = p_enable;
  2838. }
  2839. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  2840. return force_edit_checkbox_only_on_checkbox;
  2841. }
  2842. void Tree::set_allow_rmb_select(bool p_allow) {
  2843. allow_rmb_select = p_allow;
  2844. }
  2845. bool Tree::get_allow_rmb_select() const {
  2846. return allow_rmb_select;
  2847. }
  2848. void Tree::set_allow_reselect(bool p_allow) {
  2849. allow_reselect = p_allow;
  2850. }
  2851. bool Tree::get_allow_reselect() const {
  2852. return allow_reselect;
  2853. }
  2854. void Tree::_bind_methods() {
  2855. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout);
  2856. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  2857. ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select);
  2858. ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter);
  2859. ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close);
  2860. ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed);
  2861. ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
  2862. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  2863. ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
  2864. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  2865. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  2866. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  2867. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  2868. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  2869. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  2870. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  2871. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  2872. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  2873. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  2874. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  2875. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  2876. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  2877. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  2878. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  2879. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  2880. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  2881. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  2882. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  2883. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  2884. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  2885. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  2886. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  2887. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  2888. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  2889. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  2890. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  2891. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  2892. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  2893. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  2894. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  2895. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  2896. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  2897. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  2898. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  2899. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  2900. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  2901. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  2902. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  2903. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  2904. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In between"), "set_drop_mode_flags", "get_drop_mode_flags");
  2905. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  2906. ADD_SIGNAL(MethodInfo("item_selected"));
  2907. ADD_SIGNAL(MethodInfo("cell_selected"));
  2908. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  2909. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2910. ADD_SIGNAL(MethodInfo("empty_rmb", PropertyInfo(Variant::VECTOR2, "position")));
  2911. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2912. ADD_SIGNAL(MethodInfo("item_edited"));
  2913. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  2914. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  2915. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  2916. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  2917. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2918. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  2919. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  2920. ADD_SIGNAL(MethodInfo("item_activated"));
  2921. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  2922. ADD_SIGNAL(MethodInfo("nothing_selected"));
  2923. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  2924. BIND_ENUM_CONSTANT(SELECT_ROW);
  2925. BIND_ENUM_CONSTANT(SELECT_MULTI);
  2926. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  2927. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  2928. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  2929. }
  2930. Tree::Tree() {
  2931. selected_col = 0;
  2932. columns.resize(1);
  2933. selected_item = NULL;
  2934. edited_item = NULL;
  2935. selected_col = -1;
  2936. edited_col = -1;
  2937. hide_root = false;
  2938. select_mode = SELECT_SINGLE;
  2939. root = 0;
  2940. popup_menu = NULL;
  2941. popup_edited_item = NULL;
  2942. text_editor = NULL;
  2943. set_focus_mode(FOCUS_ALL);
  2944. popup_menu = memnew(PopupMenu);
  2945. popup_menu->hide();
  2946. add_child(popup_menu);
  2947. popup_menu->set_as_toplevel(true);
  2948. text_editor = memnew(LineEdit);
  2949. add_child(text_editor);
  2950. text_editor->set_as_toplevel(true);
  2951. text_editor->hide();
  2952. value_editor = memnew(HSlider);
  2953. add_child(value_editor);
  2954. value_editor->set_as_toplevel(true);
  2955. value_editor->hide();
  2956. h_scroll = memnew(HScrollBar);
  2957. v_scroll = memnew(VScrollBar);
  2958. add_child(h_scroll);
  2959. add_child(v_scroll);
  2960. range_click_timer = memnew(Timer);
  2961. range_click_timer->connect("timeout", this, "_range_click_timeout");
  2962. add_child(range_click_timer);
  2963. h_scroll->connect("value_changed", this, "_scroll_moved");
  2964. v_scroll->connect("value_changed", this, "_scroll_moved");
  2965. text_editor->connect("text_entered", this, "_text_editor_enter");
  2966. text_editor->connect("modal_closed", this, "_text_editor_modal_close");
  2967. popup_menu->connect("id_pressed", this, "_popup_select");
  2968. value_editor->connect("value_changed", this, "_value_editor_changed");
  2969. value_editor->set_as_toplevel(true);
  2970. text_editor->set_as_toplevel(true);
  2971. set_notify_transform(true);
  2972. updating_value_editor = false;
  2973. pressed_button = -1;
  2974. show_column_titles = false;
  2975. cache.click_type = Cache::CLICK_NONE;
  2976. cache.hover_type = Cache::CLICK_NONE;
  2977. cache.hover_index = -1;
  2978. cache.click_index = -1;
  2979. cache.click_id = -1;
  2980. cache.click_item = NULL;
  2981. cache.click_column = 0;
  2982. cache.hover_cell = -1;
  2983. last_keypress = 0;
  2984. focus_in_id = 0;
  2985. blocked = 0;
  2986. cursor_can_exit_tree = true;
  2987. set_mouse_filter(MOUSE_FILTER_STOP);
  2988. drag_speed = 0;
  2989. drag_touching = false;
  2990. drag_touching_deaccel = false;
  2991. pressing_for_editor = false;
  2992. range_drag_enabled = false;
  2993. hide_folding = false;
  2994. drop_mode_flags = 0;
  2995. drop_mode_over = NULL;
  2996. drop_mode_section = 0;
  2997. single_select_defer = NULL;
  2998. allow_rmb_select = false;
  2999. force_edit_checkbox_only_on_checkbox = false;
  3000. set_clip_contents(true);
  3001. cache.hover_item = NULL;
  3002. cache.hover_cell = -1;
  3003. allow_reselect = false;
  3004. propagate_mouse_activated = false;
  3005. }
  3006. Tree::~Tree() {
  3007. if (root) {
  3008. memdelete(root);
  3009. }
  3010. }