2
0

tree.cpp 110 KB

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