tree.cpp 104 KB

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