tree.cpp 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 || cells[p_column].mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  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_RANGE_EXPRESSION);
  583. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  584. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  585. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  586. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  587. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  588. }
  589. void TreeItem::clear_children() {
  590. TreeItem *c = children;
  591. while (c) {
  592. TreeItem *aux = c;
  593. c = c->get_next();
  594. aux->parent = 0; // so it won't try to recursively autoremove from me in here
  595. memdelete(aux);
  596. }
  597. children = 0;
  598. };
  599. TreeItem::TreeItem(Tree *p_tree) {
  600. tree = p_tree;
  601. collapsed = false;
  602. disable_folding = false;
  603. custom_min_height = 0;
  604. parent = 0; // parent item
  605. next = 0; // next in list
  606. children = 0; //child items
  607. }
  608. TreeItem::~TreeItem() {
  609. clear_children();
  610. if (parent)
  611. parent->remove_child(this);
  612. if (tree && tree->root == this) {
  613. tree->root = 0;
  614. }
  615. if (tree && tree->popup_edited_item == this) {
  616. tree->popup_edited_item = NULL;
  617. tree->pressing_for_editor = false;
  618. }
  619. if (tree && tree->cache.hover_item == this) {
  620. tree->cache.hover_item = NULL;
  621. }
  622. if (tree && tree->selected_item == this)
  623. tree->selected_item = NULL;
  624. if (tree && tree->drop_mode_over == this)
  625. tree->drop_mode_over = NULL;
  626. if (tree && tree->single_select_defer == this)
  627. tree->single_select_defer = NULL;
  628. if (tree && tree->edited_item == this) {
  629. tree->edited_item = NULL;
  630. tree->pressing_for_editor = false;
  631. }
  632. }
  633. /**********************************************/
  634. /**********************************************/
  635. /**********************************************/
  636. /**********************************************/
  637. /**********************************************/
  638. /**********************************************/
  639. void Tree::update_cache() {
  640. cache.font = get_font("font");
  641. cache.tb_font = get_font("title_button_font");
  642. cache.bg = get_stylebox("bg");
  643. cache.selected = get_stylebox("selected");
  644. cache.selected_focus = get_stylebox("selected_focus");
  645. cache.cursor = get_stylebox("cursor");
  646. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  647. cache.button_pressed = get_stylebox("button_pressed");
  648. cache.checked = get_icon("checked");
  649. cache.unchecked = get_icon("unchecked");
  650. cache.arrow_collapsed = get_icon("arrow_collapsed");
  651. cache.arrow = get_icon("arrow");
  652. cache.select_arrow = get_icon("select_arrow");
  653. cache.select_option = get_icon("select_option");
  654. cache.updown = get_icon("updown");
  655. cache.custom_button = get_stylebox("custom_button");
  656. cache.custom_button_hover = get_stylebox("custom_button_hover");
  657. cache.custom_button_pressed = get_stylebox("custom_button_pressed");
  658. cache.custom_button_font_highlight = get_color("custom_button_font_highlight");
  659. cache.font_color = get_color("font_color");
  660. cache.font_color_selected = get_color("font_color_selected");
  661. cache.guide_color = get_color("guide_color");
  662. cache.drop_position_color = get_color("drop_position_color");
  663. cache.hseparation = get_constant("hseparation");
  664. cache.vseparation = get_constant("vseparation");
  665. cache.item_margin = get_constant("item_margin");
  666. cache.button_margin = get_constant("button_margin");
  667. cache.guide_width = get_constant("guide_width");
  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 skip = 0;
  791. for (int i = 0; i < columns.size(); i++) {
  792. if (skip) {
  793. skip--;
  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. skip++;
  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. 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);
  837. if (i == 0) {
  838. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  839. 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));
  840. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  841. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  842. if (has_focus())
  843. cache.selected_focus->draw(ci, row_rect);
  844. else
  845. cache.selected->draw(ci, row_rect);
  846. }
  847. }
  848. if (p_item->cells[i].selected && select_mode != SELECT_ROW) {
  849. Rect2i r(cell_rect.position, cell_rect.size);
  850. if (p_item->cells[i].text.size() > 0) {
  851. float icon_width = p_item->cells[i].get_icon_size().width;
  852. r.position.x += icon_width;
  853. r.size.x -= icon_width;
  854. }
  855. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  856. if (has_focus()) {
  857. cache.selected_focus->draw(ci, r);
  858. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  859. } else {
  860. cache.selected->draw(ci, r);
  861. }
  862. if (text_editor->is_visible_in_tree()) {
  863. Vector2 ofs(0, (text_editor->get_size().height - r.size.height) / 2);
  864. text_editor->set_position(get_global_position() + r.position - ofs);
  865. }
  866. }
  867. if (p_item->cells[i].custom_bg_color) {
  868. Rect2 r = cell_rect;
  869. if (i == 0) {
  870. r.position.x = p_draw_ofs.x;
  871. r.size.x = w + ofs;
  872. } else {
  873. r.position.x -= cache.hseparation;
  874. r.size.x += cache.hseparation;
  875. }
  876. if (p_item->cells[i].custom_bg_outline) {
  877. 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);
  878. 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);
  879. 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);
  880. 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);
  881. } else {
  882. VisualServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  883. }
  884. }
  885. if (drop_mode_flags && drop_mode_over == p_item) {
  886. Rect2 r = cell_rect;
  887. if (drop_mode_section == -1 || drop_mode_section == 0) {
  888. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color);
  889. }
  890. if (drop_mode_section == 0) {
  891. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color);
  892. 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);
  893. }
  894. if (drop_mode_section == 1 || drop_mode_section == 0) {
  895. 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);
  896. }
  897. }
  898. 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");
  899. Color icon_col = p_item->cells[i].icon_color;
  900. Point2i text_pos = item_rect.position;
  901. text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent;
  902. switch (p_item->cells[i].mode) {
  903. case TreeItem::CELL_MODE_STRING: {
  904. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  905. } break;
  906. case TreeItem::CELL_MODE_CHECK: {
  907. Ref<Texture> checked = cache.checked;
  908. Ref<Texture> unchecked = cache.unchecked;
  909. Point2i check_ofs = item_rect.position;
  910. check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2);
  911. if (p_item->cells[i].checked) {
  912. checked->draw(ci, check_ofs, icon_col);
  913. } else {
  914. unchecked->draw(ci, check_ofs, icon_col);
  915. }
  916. int check_w = checked->get_width() + cache.hseparation;
  917. text_pos.x += check_w;
  918. item_rect.size.x -= check_w;
  919. item_rect.position.x += check_w;
  920. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  921. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  922. } break;
  923. case TreeItem::CELL_MODE_RANGE:
  924. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  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 i = 0; i < strings.size(); i++) {
  932. int value = i;
  933. if (!strings[i].get_slicec(':', 1).empty()) {
  934. value = strings[i].get_slicec(':', 1).to_int();
  935. }
  936. if (option == value) {
  937. s = strings[i].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. while (c) {
  1047. if (cache.draw_relationship_lines == 1 && (c->get_parent() != root || c->get_parent() == root && !hide_root)) {
  1048. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1049. int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1050. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs;
  1051. if (c->get_children() != NULL)
  1052. root_pos -= Point2i(cache.arrow->get_width(), 0);
  1053. float line_width = 1.0;
  1054. #ifdef TOOLS_ENABLED
  1055. line_width *= EDSCALE;
  1056. #endif
  1057. 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;
  1058. if (root_pos.y + line_width >= 0) {
  1059. 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);
  1060. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color, line_width);
  1061. }
  1062. if (htotal < 0) {
  1063. return -1;
  1064. }
  1065. }
  1066. if (htotal >= 0) {
  1067. int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
  1068. if (child_h < 0) {
  1069. if (cache.draw_relationship_lines == 0) {
  1070. return -1; // break, stop drawing, no need to anymore
  1071. } else {
  1072. htotal = -1;
  1073. children_pos.y = cache.offset.y + p_draw_size.height;
  1074. }
  1075. } else {
  1076. htotal += child_h;
  1077. children_pos.y += child_h;
  1078. }
  1079. }
  1080. c = c->next;
  1081. }
  1082. }
  1083. return htotal;
  1084. }
  1085. int Tree::_count_selected_items(TreeItem *p_from) const {
  1086. int count = 0;
  1087. for (int i = 0; i < columns.size(); i++) {
  1088. if (p_from->is_selected(i))
  1089. count++;
  1090. }
  1091. if (p_from->get_children()) {
  1092. count += _count_selected_items(p_from->get_children());
  1093. }
  1094. if (p_from->get_next()) {
  1095. count += _count_selected_items(p_from->get_next());
  1096. }
  1097. return count;
  1098. }
  1099. 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) {
  1100. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  1101. bool switched = false;
  1102. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1103. *r_in_range = true;
  1104. switched = true;
  1105. }
  1106. bool emitted_row = false;
  1107. for (int i = 0; i < columns.size(); i++) {
  1108. TreeItem::Cell &c = p_current->cells.write[i];
  1109. if (!c.selectable)
  1110. continue;
  1111. if (select_mode == SELECT_ROW) {
  1112. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  1113. c.selected = true;
  1114. selected_item = p_selected;
  1115. selected_col = 0;
  1116. if (!emitted_row) {
  1117. emit_signal("item_selected");
  1118. emitted_row = true;
  1119. }
  1120. /*
  1121. if (p_col==i)
  1122. p_current->selected_signal.call(p_col);
  1123. */
  1124. } else if (c.selected) {
  1125. c.selected = false;
  1126. //p_current->deselected_signal.call(p_col);
  1127. }
  1128. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  1129. if (!r_in_range && &selected_cell == &c) {
  1130. if (!selected_cell.selected || allow_reselect) {
  1131. selected_cell.selected = true;
  1132. selected_item = p_selected;
  1133. selected_col = i;
  1134. emit_signal("cell_selected");
  1135. if (select_mode == SELECT_MULTI)
  1136. emit_signal("multi_selected", p_current, i, true);
  1137. else if (select_mode == SELECT_SINGLE)
  1138. emit_signal("item_selected");
  1139. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  1140. selected_item = p_selected;
  1141. selected_col = i;
  1142. emit_signal("cell_selected");
  1143. }
  1144. } else {
  1145. if (r_in_range && *r_in_range && !p_force_deselect) {
  1146. if (!c.selected && c.selectable) {
  1147. c.selected = true;
  1148. emit_signal("multi_selected", p_current, i, true);
  1149. }
  1150. } else if (!r_in_range || p_force_deselect) {
  1151. if (select_mode == SELECT_MULTI && c.selected)
  1152. emit_signal("multi_selected", p_current, i, false);
  1153. c.selected = false;
  1154. }
  1155. //p_current->deselected_signal.call(p_col);
  1156. }
  1157. }
  1158. }
  1159. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1160. *r_in_range = false;
  1161. }
  1162. TreeItem *c = p_current->children;
  1163. while (c) {
  1164. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  1165. c = c->next;
  1166. }
  1167. }
  1168. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  1169. return Rect2();
  1170. }
  1171. void Tree::_range_click_timeout() {
  1172. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  1173. Point2 pos = get_local_mouse_position() - cache.bg->get_offset();
  1174. if (show_column_titles) {
  1175. pos.y -= _get_title_button_height();
  1176. if (pos.y < 0) {
  1177. range_click_timer->stop();
  1178. return;
  1179. }
  1180. }
  1181. click_handled = false;
  1182. Ref<InputEventMouseButton> mb;
  1183. mb.instance();
  1184. ;
  1185. blocked++;
  1186. propagate_mouse_event(pos + cache.offset, 0, 0, false, root, BUTTON_LEFT, mb);
  1187. blocked--;
  1188. if (range_click_timer->is_one_shot()) {
  1189. range_click_timer->set_wait_time(0.05);
  1190. range_click_timer->set_one_shot(false);
  1191. range_click_timer->start();
  1192. }
  1193. if (!click_handled)
  1194. range_click_timer->stop();
  1195. } else {
  1196. range_click_timer->stop();
  1197. }
  1198. }
  1199. 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) {
  1200. int item_h = compute_item_height(p_item) + cache.vseparation;
  1201. bool skip = (p_item == root && hide_root);
  1202. if (!skip && p_pos.y < item_h) {
  1203. // check event!
  1204. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1205. return -1;
  1206. }
  1207. if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
  1208. if (p_item->children)
  1209. p_item->set_collapsed(!p_item->is_collapsed());
  1210. return -1; //handled!
  1211. }
  1212. int x = p_pos.x;
  1213. /* find clicked column */
  1214. int col = -1;
  1215. int col_ofs = 0;
  1216. int col_width = 0;
  1217. for (int i = 0; i < columns.size(); i++) {
  1218. col_width = get_column_width(i);
  1219. if (p_item->cells[i].expand_right) {
  1220. int plus = 1;
  1221. 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()) {
  1222. col_width += cache.hseparation;
  1223. col_width += get_column_width(i + plus);
  1224. plus++;
  1225. }
  1226. }
  1227. if (x > col_width) {
  1228. col_ofs += col_width;
  1229. x -= col_width;
  1230. continue;
  1231. }
  1232. col = i;
  1233. break;
  1234. }
  1235. if (col == -1)
  1236. return -1;
  1237. else if (col == 0) {
  1238. int margin = x_ofs + cache.item_margin; //-cache.hseparation;
  1239. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1240. col_width -= margin;
  1241. col_ofs += margin;
  1242. x -= margin;
  1243. } else {
  1244. col_width -= cache.hseparation;
  1245. x -= cache.hseparation;
  1246. }
  1247. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_children()) {
  1248. p_item->set_collapsed(!p_item->is_collapsed());
  1249. return -1; //collapse/uncollapse because nothing can be done with item
  1250. }
  1251. const TreeItem::Cell &c = p_item->cells[col];
  1252. bool already_selected = c.selected;
  1253. bool already_cursor = (p_item == selected_item) && col == selected_col;
  1254. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  1255. Ref<Texture> b = c.buttons[j].texture;
  1256. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1257. if (x > col_width - w) {
  1258. if (c.buttons[j].disabled) {
  1259. pressed_button = -1;
  1260. cache.click_type = Cache::CLICK_NONE;
  1261. return -1;
  1262. }
  1263. pressed_button = j;
  1264. cache.click_type = Cache::CLICK_BUTTON;
  1265. cache.click_index = j;
  1266. cache.click_id = c.buttons[j].id;
  1267. cache.click_item = p_item;
  1268. cache.click_column = col;
  1269. cache.click_pos = get_global_mouse_position() - get_global_position();
  1270. update();
  1271. //emit_signal("button_pressed");
  1272. return -1;
  1273. }
  1274. col_width -= w + cache.button_margin;
  1275. }
  1276. if (p_button == BUTTON_LEFT || (p_button == BUTTON_RIGHT && allow_rmb_select)) {
  1277. /* process selection */
  1278. 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
  1279. emit_signal("item_activated");
  1280. incr_search.clear();
  1281. return -1;
  1282. }
  1283. if (select_mode == SELECT_MULTI && p_mod->get_command() && c.selectable) {
  1284. if (!c.selected || p_button == BUTTON_RIGHT) {
  1285. p_item->select(col);
  1286. emit_signal("multi_selected", p_item, col, true);
  1287. if (p_button == BUTTON_RIGHT) {
  1288. emit_signal("item_rmb_selected", get_local_mouse_position());
  1289. }
  1290. //p_item->selected_signal.call(col);
  1291. } else {
  1292. p_item->deselect(col);
  1293. emit_signal("multi_selected", p_item, col, false);
  1294. //p_item->deselected_signal.call(col);
  1295. }
  1296. } else {
  1297. if (c.selectable) {
  1298. if (select_mode == SELECT_MULTI && p_mod->get_shift() && selected_item && selected_item != p_item) {
  1299. bool inrange = false;
  1300. select_single_item(p_item, root, col, selected_item, &inrange);
  1301. if (p_button == BUTTON_RIGHT) {
  1302. emit_signal("item_rmb_selected", get_local_mouse_position());
  1303. }
  1304. } else {
  1305. int icount = _count_selected_items(root);
  1306. if (select_mode == SELECT_MULTI && icount > 1 && p_button != BUTTON_RIGHT) {
  1307. single_select_defer = p_item;
  1308. single_select_defer_column = col;
  1309. } else {
  1310. if (p_button != BUTTON_RIGHT || !c.selected) {
  1311. select_single_item(p_item, root, col);
  1312. }
  1313. if (p_button == BUTTON_RIGHT) {
  1314. emit_signal("item_rmb_selected", get_local_mouse_position());
  1315. }
  1316. }
  1317. }
  1318. /*
  1319. if (!c.selected && select_mode==SELECT_MULTI) {
  1320. emit_signal("multi_selected",p_item,col,true);
  1321. }
  1322. */
  1323. update();
  1324. }
  1325. }
  1326. }
  1327. if (!c.editable)
  1328. return -1; // if cell is not editable, don't bother
  1329. /* editing */
  1330. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  1331. String editor_text = c.text;
  1332. switch (c.mode) {
  1333. case TreeItem::CELL_MODE_STRING: {
  1334. //nothing in particular
  1335. if (select_mode == SELECT_MULTI && (get_tree()->get_event_count() == focus_in_id || !already_cursor)) {
  1336. bring_up_editor = false;
  1337. }
  1338. } break;
  1339. case TreeItem::CELL_MODE_CHECK: {
  1340. bring_up_editor = false; //checkboxes are not edited with editor
  1341. if (force_edit_checkbox_only_on_checkbox) {
  1342. if (x < cache.checked->get_width()) {
  1343. p_item->set_checked(col, !c.checked);
  1344. item_edited(col, p_item);
  1345. }
  1346. } else {
  1347. p_item->set_checked(col, !c.checked);
  1348. item_edited(col, p_item);
  1349. }
  1350. click_handled = true;
  1351. //p_item->edited_signal.call(col);
  1352. } break;
  1353. case TreeItem::CELL_MODE_RANGE:
  1354. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1355. if (c.text != "") {
  1356. //if (x >= (get_column_width(col)-item_h/2)) {
  1357. popup_menu->clear();
  1358. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  1359. String s = c.text.get_slicec(',', i);
  1360. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.get_slicec(':', 1).to_int());
  1361. }
  1362. popup_menu->set_size(Size2(col_width, 0));
  1363. popup_menu->set_position(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset);
  1364. popup_menu->popup();
  1365. popup_edited_item = p_item;
  1366. popup_edited_item_col = col;
  1367. //}
  1368. bring_up_editor = false;
  1369. } else {
  1370. if (x >= (col_width - item_h / 2)) {
  1371. /* touching the combo */
  1372. bool up = p_pos.y < (item_h / 2);
  1373. if (p_button == BUTTON_LEFT) {
  1374. if (range_click_timer->get_time_left() == 0) {
  1375. range_item_last = p_item;
  1376. range_up_last = up;
  1377. range_click_timer->set_wait_time(0.6);
  1378. range_click_timer->set_one_shot(true);
  1379. range_click_timer->start();
  1380. } else if (up != range_up_last) {
  1381. return -1; // break. avoid changing direction on mouse held
  1382. }
  1383. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  1384. item_edited(col, p_item);
  1385. } else if (p_button == BUTTON_RIGHT) {
  1386. p_item->set_range(col, (up ? c.max : c.min));
  1387. item_edited(col, p_item);
  1388. } else if (p_button == BUTTON_WHEEL_UP) {
  1389. p_item->set_range(col, c.val + c.step);
  1390. item_edited(col, p_item);
  1391. } else if (p_button == BUTTON_WHEEL_DOWN) {
  1392. p_item->set_range(col, c.val - c.step);
  1393. item_edited(col, p_item);
  1394. }
  1395. //p_item->edited_signal.call(col);
  1396. bring_up_editor = false;
  1397. } else {
  1398. editor_text = String::num(p_item->cells[col].val, Math::step_decimals(p_item->cells[col].step));
  1399. if (select_mode == SELECT_MULTI && get_tree()->get_event_count() == focus_in_id)
  1400. bring_up_editor = false;
  1401. }
  1402. }
  1403. click_handled = true;
  1404. } break;
  1405. case TreeItem::CELL_MODE_ICON: {
  1406. bring_up_editor = false;
  1407. } break;
  1408. case TreeItem::CELL_MODE_CUSTOM: {
  1409. edited_item = p_item;
  1410. edited_col = col;
  1411. bool on_arrow = x > col_width - cache.select_arrow->get_width();
  1412. bring_up_editor = false;
  1413. 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));
  1414. if (on_arrow || !p_item->cells[col].custom_button) {
  1415. emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2))));
  1416. }
  1417. if (!p_item->cells[col].custom_button || !on_arrow) {
  1418. item_edited(col, p_item, p_button == BUTTON_LEFT);
  1419. }
  1420. click_handled = true;
  1421. return -1;
  1422. } break;
  1423. };
  1424. if (!bring_up_editor || p_button != BUTTON_LEFT)
  1425. return -1;
  1426. click_handled = true;
  1427. popup_edited_item = p_item;
  1428. popup_edited_item_col = col;
  1429. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h));
  1430. pressing_for_editor_text = editor_text;
  1431. pressing_for_editor = true;
  1432. return -1; //select
  1433. } else {
  1434. Point2i new_pos = p_pos;
  1435. if (!skip) {
  1436. x_ofs += cache.item_margin;
  1437. //new_pos.x-=cache.item_margin;
  1438. y_ofs += item_h;
  1439. new_pos.y -= item_h;
  1440. }
  1441. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1442. TreeItem *c = p_item->children;
  1443. while (c) {
  1444. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_doubleclick, c, p_button, p_mod);
  1445. if (child_h < 0)
  1446. return -1; // break, stop propagating, no need to anymore
  1447. new_pos.y -= child_h;
  1448. y_ofs += child_h;
  1449. c = c->next;
  1450. item_h += child_h;
  1451. }
  1452. }
  1453. }
  1454. return item_h; // nothing found
  1455. }
  1456. void Tree::_text_editor_modal_close() {
  1457. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1458. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  1459. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  1460. return;
  1461. }
  1462. if (value_editor->has_point(value_editor->get_local_mouse_position()))
  1463. return;
  1464. text_editor_enter(text_editor->get_text());
  1465. }
  1466. void Tree::text_editor_enter(String p_text) {
  1467. text_editor->hide();
  1468. value_editor->hide();
  1469. if (!popup_edited_item)
  1470. return;
  1471. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1472. return;
  1473. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1474. switch (c.mode) {
  1475. case TreeItem::CELL_MODE_STRING: {
  1476. c.text = p_text;
  1477. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1478. } break;
  1479. case TreeItem::CELL_MODE_RANGE: {
  1480. c.val = p_text.to_double();
  1481. if (c.step > 0)
  1482. c.val = Math::stepify(c.val, c.step);
  1483. if (c.val < c.min)
  1484. c.val = c.min;
  1485. else if (c.val > c.max)
  1486. c.val = c.max;
  1487. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1488. } break;
  1489. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1490. if (evaluator)
  1491. c.val = evaluator->eval(p_text);
  1492. else
  1493. c.val = p_text.to_double();
  1494. if (c.step > 0)
  1495. c.val = Math::stepify(c.val, c.step);
  1496. if (c.val < c.min)
  1497. c.val = c.min;
  1498. else if (c.val > c.max)
  1499. c.val = c.max;
  1500. } break;
  1501. default: { ERR_FAIL(); }
  1502. }
  1503. item_edited(popup_edited_item_col, popup_edited_item);
  1504. update();
  1505. }
  1506. void Tree::value_editor_changed(double p_value) {
  1507. if (updating_value_editor) {
  1508. return;
  1509. }
  1510. if (!popup_edited_item) {
  1511. return;
  1512. }
  1513. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1514. c.val = p_value;
  1515. item_edited(popup_edited_item_col, popup_edited_item);
  1516. update();
  1517. }
  1518. void Tree::popup_select(int p_option) {
  1519. if (!popup_edited_item)
  1520. return;
  1521. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1522. return;
  1523. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  1524. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1525. update();
  1526. item_edited(popup_edited_item_col, popup_edited_item);
  1527. }
  1528. void Tree::_go_left() {
  1529. if (selected_col == 0) {
  1530. if (selected_item->get_children() != NULL && !selected_item->is_collapsed()) {
  1531. selected_item->set_collapsed(true);
  1532. } else {
  1533. if (columns.size() == 1) { // goto parent with one column
  1534. TreeItem *parent = selected_item->get_parent();
  1535. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  1536. select_single_item(parent, get_root(), selected_col);
  1537. }
  1538. } else if (selected_item->get_prev_visible()) {
  1539. selected_col = columns.size() - 1;
  1540. _go_up(); // go to upper column if possible
  1541. }
  1542. }
  1543. } else {
  1544. if (select_mode == SELECT_MULTI) {
  1545. selected_col--;
  1546. emit_signal("cell_selected");
  1547. } else {
  1548. selected_item->select(selected_col - 1);
  1549. }
  1550. }
  1551. update();
  1552. accept_event();
  1553. ensure_cursor_is_visible();
  1554. }
  1555. void Tree::_go_right() {
  1556. if (selected_col == (columns.size() - 1)) {
  1557. if (selected_item->get_children() != NULL && selected_item->is_collapsed()) {
  1558. selected_item->set_collapsed(false);
  1559. } else if (selected_item->get_next_visible()) {
  1560. selected_item->select(0);
  1561. _go_down();
  1562. return;
  1563. }
  1564. } else {
  1565. if (select_mode == SELECT_MULTI) {
  1566. selected_col++;
  1567. emit_signal("cell_selected");
  1568. } else {
  1569. selected_item->select(selected_col + 1);
  1570. }
  1571. }
  1572. update();
  1573. ensure_cursor_is_visible();
  1574. accept_event();
  1575. }
  1576. void Tree::_go_up() {
  1577. TreeItem *prev = NULL;
  1578. if (!selected_item) {
  1579. prev = get_last_item();
  1580. selected_col = 0;
  1581. } else {
  1582. prev = selected_item->get_prev_visible();
  1583. if (last_keypress != 0) {
  1584. //incr search next
  1585. int col;
  1586. prev = _search_item_text(prev, incr_search, &col, true, true);
  1587. if (!prev) {
  1588. accept_event();
  1589. return;
  1590. }
  1591. }
  1592. }
  1593. if (select_mode == SELECT_MULTI) {
  1594. if (!prev)
  1595. return;
  1596. selected_item = prev;
  1597. emit_signal("cell_selected");
  1598. update();
  1599. } else {
  1600. int col = selected_col < 0 ? 0 : selected_col;
  1601. while (prev && !prev->cells[col].selectable)
  1602. prev = prev->get_prev_visible();
  1603. if (!prev)
  1604. return; // do nothing..
  1605. prev->select(col);
  1606. }
  1607. ensure_cursor_is_visible();
  1608. accept_event();
  1609. }
  1610. void Tree::_go_down() {
  1611. TreeItem *next = NULL;
  1612. if (!selected_item) {
  1613. next = hide_root ? root->get_next_visible() : root;
  1614. selected_item = 0;
  1615. } else {
  1616. next = selected_item->get_next_visible();
  1617. if (last_keypress != 0) {
  1618. //incr search next
  1619. int col;
  1620. next = _search_item_text(next, incr_search, &col, true);
  1621. if (!next) {
  1622. accept_event();
  1623. return;
  1624. }
  1625. }
  1626. }
  1627. if (select_mode == SELECT_MULTI) {
  1628. if (!next) {
  1629. return;
  1630. }
  1631. selected_item = next;
  1632. emit_signal("cell_selected");
  1633. update();
  1634. } else {
  1635. int col = selected_col < 0 ? 0 : selected_col;
  1636. while (next && !next->cells[col].selectable)
  1637. next = next->get_next_visible();
  1638. if (!next) {
  1639. return; // do nothing..
  1640. }
  1641. next->select(col);
  1642. }
  1643. ensure_cursor_is_visible();
  1644. accept_event();
  1645. }
  1646. void Tree::_gui_input(Ref<InputEvent> p_event) {
  1647. Ref<InputEventKey> k = p_event;
  1648. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  1649. if (!cursor_can_exit_tree) accept_event();
  1650. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  1651. return;
  1652. }
  1653. if (k.is_valid() && k->get_alt()) {
  1654. selected_item->set_collapsed(false);
  1655. TreeItem *next = selected_item->get_children();
  1656. while (next && next != selected_item->next) {
  1657. next->set_collapsed(false);
  1658. next = next->get_next_visible();
  1659. }
  1660. } else {
  1661. _go_right();
  1662. }
  1663. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  1664. if (!cursor_can_exit_tree) accept_event();
  1665. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  1666. return;
  1667. }
  1668. if (k.is_valid() && k->get_alt()) {
  1669. selected_item->set_collapsed(true);
  1670. TreeItem *next = selected_item->get_children();
  1671. while (next && next != selected_item->next) {
  1672. next->set_collapsed(true);
  1673. next = next->get_next_visible();
  1674. }
  1675. } else {
  1676. _go_left();
  1677. }
  1678. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !k->get_command()) {
  1679. if (!cursor_can_exit_tree) accept_event();
  1680. _go_up();
  1681. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !k->get_command()) {
  1682. if (!cursor_can_exit_tree) accept_event();
  1683. _go_down();
  1684. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  1685. if (!cursor_can_exit_tree) accept_event();
  1686. TreeItem *next = NULL;
  1687. if (!selected_item)
  1688. return;
  1689. next = selected_item;
  1690. for (int i = 0; i < 10; i++) {
  1691. TreeItem *_n = next->get_next_visible();
  1692. if (_n) {
  1693. next = _n;
  1694. } else {
  1695. return;
  1696. }
  1697. }
  1698. if (next == selected_item)
  1699. return;
  1700. if (select_mode == SELECT_MULTI) {
  1701. selected_item = next;
  1702. emit_signal("cell_selected");
  1703. update();
  1704. } else {
  1705. while (next && !next->cells[selected_col].selectable)
  1706. next = next->get_next_visible();
  1707. if (!next) {
  1708. return; // do nothing..
  1709. }
  1710. next->select(selected_col);
  1711. }
  1712. ensure_cursor_is_visible();
  1713. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  1714. if (!cursor_can_exit_tree) accept_event();
  1715. TreeItem *prev = NULL;
  1716. if (!selected_item)
  1717. return;
  1718. prev = selected_item;
  1719. for (int i = 0; i < 10; i++) {
  1720. TreeItem *_n = prev->get_prev_visible();
  1721. if (_n) {
  1722. prev = _n;
  1723. } else {
  1724. return;
  1725. }
  1726. }
  1727. if (prev == selected_item)
  1728. return;
  1729. if (select_mode == SELECT_MULTI) {
  1730. selected_item = prev;
  1731. emit_signal("cell_selected");
  1732. update();
  1733. } else {
  1734. while (prev && !prev->cells[selected_col].selectable)
  1735. prev = prev->get_prev_visible();
  1736. if (!prev) {
  1737. return; // do nothing..
  1738. }
  1739. prev->select(selected_col);
  1740. }
  1741. ensure_cursor_is_visible();
  1742. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  1743. if (selected_item) {
  1744. //bring up editor if possible
  1745. if (!edit_selected()) {
  1746. emit_signal("item_activated");
  1747. incr_search.clear();
  1748. }
  1749. }
  1750. accept_event();
  1751. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  1752. if (select_mode == SELECT_MULTI) {
  1753. if (!selected_item)
  1754. return;
  1755. if (selected_item->is_selected(selected_col)) {
  1756. selected_item->deselect(selected_col);
  1757. emit_signal("multi_selected", selected_item, selected_col, false);
  1758. } else if (selected_item->is_selectable(selected_col)) {
  1759. selected_item->select(selected_col);
  1760. emit_signal("multi_selected", selected_item, selected_col, true);
  1761. }
  1762. }
  1763. accept_event();
  1764. }
  1765. if (k.is_valid()) { // Incremental search
  1766. if (!k->is_pressed())
  1767. return;
  1768. if (k->get_command() || (k->get_shift() && k->get_unicode() == 0) || k->get_metakey())
  1769. return;
  1770. if (!root)
  1771. return;
  1772. if (hide_root && !root->get_next_visible())
  1773. return;
  1774. if (k->get_unicode() > 0) {
  1775. _do_incr_search(String::chr(k->get_unicode()));
  1776. accept_event();
  1777. return;
  1778. } else {
  1779. if (k->get_scancode() != KEY_SHIFT)
  1780. last_keypress = 0;
  1781. }
  1782. }
  1783. Ref<InputEventMouseMotion> mm = p_event;
  1784. if (mm.is_valid()) {
  1785. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1786. update_cache();
  1787. Ref<StyleBox> bg = cache.bg;
  1788. Point2 pos = mm->get_position() - bg->get_offset();
  1789. Cache::ClickType old_hover = cache.hover_type;
  1790. int old_index = cache.hover_index;
  1791. cache.hover_type = Cache::CLICK_NONE;
  1792. cache.hover_index = 0;
  1793. if (show_column_titles) {
  1794. pos.y -= _get_title_button_height();
  1795. if (pos.y < 0) {
  1796. pos.x += cache.offset.x;
  1797. int len = 0;
  1798. for (int i = 0; i < columns.size(); i++) {
  1799. len += get_column_width(i);
  1800. if (pos.x < len) {
  1801. cache.hover_type = Cache::CLICK_TITLE;
  1802. cache.hover_index = i;
  1803. update();
  1804. break;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. if (root) {
  1810. Point2 mpos = mm->get_position();
  1811. mpos -= cache.bg->get_offset();
  1812. mpos.y -= _get_title_button_height();
  1813. if (mpos.y >= 0) {
  1814. if (h_scroll->is_visible_in_tree())
  1815. mpos.x += h_scroll->get_value();
  1816. if (v_scroll->is_visible_in_tree())
  1817. mpos.y += v_scroll->get_value();
  1818. int col, h, section;
  1819. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  1820. if (drop_mode_flags) {
  1821. if (it != drop_mode_over) {
  1822. drop_mode_over = it;
  1823. update();
  1824. }
  1825. if (it && section != drop_mode_section) {
  1826. drop_mode_section = section;
  1827. update();
  1828. }
  1829. }
  1830. if (it != cache.hover_item) {
  1831. cache.hover_item = it;
  1832. update();
  1833. }
  1834. if (it && col != cache.hover_cell) {
  1835. cache.hover_cell = col;
  1836. update();
  1837. }
  1838. }
  1839. }
  1840. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  1841. update();
  1842. }
  1843. if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE || popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE_EXPRESSION)) {
  1844. //range drag
  1845. if (!range_drag_enabled) {
  1846. Vector2 cpos = mm->get_position();
  1847. if (cpos.distance_to(pressing_pos) > 2) {
  1848. range_drag_enabled = true;
  1849. range_drag_capture_pos = cpos;
  1850. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  1851. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1852. }
  1853. } else {
  1854. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1855. float diff_y = -mm->get_relative().y;
  1856. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  1857. diff_y *= 0.1;
  1858. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1859. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  1860. item_edited(popup_edited_item_col, popup_edited_item);
  1861. }
  1862. }
  1863. if (drag_touching && !drag_touching_deaccel) {
  1864. drag_accum -= mm->get_relative().y;
  1865. v_scroll->set_value(drag_from + drag_accum);
  1866. drag_speed = -mm->get_speed().y;
  1867. }
  1868. }
  1869. Ref<InputEventMouseButton> b = p_event;
  1870. if (b.is_valid()) {
  1871. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1872. update_cache();
  1873. if (!b->is_pressed()) {
  1874. if (b->get_button_index() == BUTTON_LEFT) {
  1875. Point2 pos = b->get_position() - cache.bg->get_offset();
  1876. if (show_column_titles) {
  1877. pos.y -= _get_title_button_height();
  1878. if (pos.y < 0) {
  1879. pos.x += cache.offset.x;
  1880. int len = 0;
  1881. for (int i = 0; i < columns.size(); i++) {
  1882. len += get_column_width(i);
  1883. if (pos.x < len) {
  1884. emit_signal("column_title_pressed", i);
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. }
  1890. if (single_select_defer) {
  1891. select_single_item(single_select_defer, root, single_select_defer_column);
  1892. single_select_defer = NULL;
  1893. }
  1894. range_click_timer->stop();
  1895. if (pressing_for_editor) {
  1896. if (range_drag_enabled) {
  1897. range_drag_enabled = false;
  1898. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1899. warp_mouse(range_drag_capture_pos);
  1900. } else {
  1901. Rect2 rect = get_selected()->get_meta("__focus_rect");
  1902. if (rect.has_point(Point2(b->get_position().x, b->get_position().y))) {
  1903. edit_selected();
  1904. } else {
  1905. emit_signal("item_double_clicked");
  1906. }
  1907. }
  1908. pressing_for_editor = false;
  1909. }
  1910. if (cache.click_type == Cache::CLICK_BUTTON) {
  1911. // make sure in case of wrong reference after reconstructing whole TreeItems
  1912. cache.click_item = get_item_at_position(cache.click_pos);
  1913. emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id);
  1914. }
  1915. cache.click_type = Cache::CLICK_NONE;
  1916. cache.click_index = -1;
  1917. cache.click_id = -1;
  1918. cache.click_item = NULL;
  1919. cache.click_column = 0;
  1920. if (drag_touching) {
  1921. if (drag_speed == 0) {
  1922. drag_touching_deaccel = false;
  1923. drag_touching = false;
  1924. set_physics_process_internal(false);
  1925. } else {
  1926. drag_touching_deaccel = true;
  1927. }
  1928. }
  1929. update();
  1930. }
  1931. return;
  1932. }
  1933. if (range_drag_enabled)
  1934. return;
  1935. switch (b->get_button_index()) {
  1936. case BUTTON_RIGHT:
  1937. case BUTTON_LEFT: {
  1938. Ref<StyleBox> bg = cache.bg;
  1939. Point2 pos = b->get_position() - bg->get_offset();
  1940. cache.click_type = Cache::CLICK_NONE;
  1941. if (show_column_titles) {
  1942. pos.y -= _get_title_button_height();
  1943. if (pos.y < 0) {
  1944. if (b->get_button_index() == BUTTON_LEFT) {
  1945. pos.x += cache.offset.x;
  1946. int len = 0;
  1947. for (int i = 0; i < columns.size(); i++) {
  1948. len += get_column_width(i);
  1949. if (pos.x < len) {
  1950. cache.click_type = Cache::CLICK_TITLE;
  1951. cache.click_index = i;
  1952. //cache.click_id=;
  1953. update();
  1954. break;
  1955. }
  1956. }
  1957. }
  1958. break;
  1959. }
  1960. }
  1961. if (!root || (!root->get_children() && hide_root)) {
  1962. if (b->get_button_index() == BUTTON_RIGHT && allow_rmb_select) {
  1963. emit_signal("empty_tree_rmb_selected", get_local_mouse_position());
  1964. }
  1965. break;
  1966. }
  1967. click_handled = false;
  1968. pressing_for_editor = false;
  1969. blocked++;
  1970. propagate_mouse_event(pos + cache.offset, 0, 0, b->is_doubleclick(), root, b->get_button_index(), b);
  1971. blocked--;
  1972. if (pressing_for_editor) {
  1973. pressing_pos = b->get_position();
  1974. }
  1975. if (b->get_button_index() == BUTTON_RIGHT)
  1976. break;
  1977. if (drag_touching) {
  1978. set_physics_process_internal(false);
  1979. drag_touching_deaccel = false;
  1980. drag_touching = false;
  1981. drag_speed = 0;
  1982. drag_from = 0;
  1983. }
  1984. if (!click_handled) {
  1985. drag_speed = 0;
  1986. drag_accum = 0;
  1987. //last_drag_accum=0;
  1988. drag_from = v_scroll->get_value();
  1989. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  1990. drag_touching_deaccel = false;
  1991. if (drag_touching) {
  1992. set_physics_process_internal(true);
  1993. }
  1994. if (b->get_button_index() == BUTTON_LEFT) {
  1995. if (get_item_at_position(b->get_position()) == NULL && !b->get_shift() && !b->get_control() && !b->get_command())
  1996. emit_signal("nothing_selected");
  1997. }
  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.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text != "") {
  2039. popup_menu->clear();
  2040. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2041. String s = c.text.get_slicec(',', i);
  2042. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.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 || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  2051. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2052. Point2i textedpos = get_global_position() + rect.position - ofs;
  2053. text_editor->set_position(textedpos);
  2054. text_editor->set_size(rect.size);
  2055. text_editor->clear();
  2056. text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::step_decimals(c.step)));
  2057. text_editor->select_all();
  2058. if (c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  2059. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2060. value_editor->set_size(Size2(rect.size.width, 1));
  2061. value_editor->show_modal();
  2062. updating_value_editor = true;
  2063. value_editor->set_min(c.min);
  2064. value_editor->set_max(c.max);
  2065. value_editor->set_step(c.step);
  2066. value_editor->set_value(c.val);
  2067. value_editor->set_exp_ratio(c.expr);
  2068. updating_value_editor = false;
  2069. }
  2070. text_editor->show_modal();
  2071. text_editor->grab_focus();
  2072. return true;
  2073. }
  2074. return false;
  2075. }
  2076. Size2 Tree::get_internal_min_size() const {
  2077. Size2i size = cache.bg->get_offset();
  2078. if (root)
  2079. size.height += get_item_height(root);
  2080. for (int i = 0; i < columns.size(); i++) {
  2081. size.width += columns[i].min_width;
  2082. }
  2083. return size;
  2084. }
  2085. void Tree::update_scrollbars() {
  2086. Size2 size = get_size();
  2087. int tbh;
  2088. if (show_column_titles) {
  2089. tbh = _get_title_button_height();
  2090. } else {
  2091. tbh = 0;
  2092. }
  2093. Size2 hmin = h_scroll->get_combined_minimum_size();
  2094. Size2 vmin = v_scroll->get_combined_minimum_size();
  2095. v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(MARGIN_TOP)));
  2096. v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(MARGIN_TOP) - cache.bg->get_margin(MARGIN_BOTTOM)));
  2097. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  2098. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  2099. Size2 min = get_internal_min_size();
  2100. if (min.height < size.height - hmin.height) {
  2101. v_scroll->hide();
  2102. cache.offset.y = 0;
  2103. } else {
  2104. v_scroll->show();
  2105. v_scroll->set_max(min.height);
  2106. v_scroll->set_page(size.height - hmin.height - tbh);
  2107. cache.offset.y = v_scroll->get_value();
  2108. }
  2109. if (min.width < size.width - vmin.width) {
  2110. h_scroll->hide();
  2111. cache.offset.x = 0;
  2112. } else {
  2113. h_scroll->show();
  2114. h_scroll->set_max(min.width);
  2115. h_scroll->set_page(size.width - vmin.width);
  2116. cache.offset.x = h_scroll->get_value();
  2117. }
  2118. }
  2119. int Tree::_get_title_button_height() const {
  2120. ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
  2121. return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
  2122. }
  2123. void Tree::_notification(int p_what) {
  2124. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2125. focus_in_id = get_tree()->get_event_count();
  2126. }
  2127. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2128. if (cache.hover_type != Cache::CLICK_NONE) {
  2129. cache.hover_type = Cache::CLICK_NONE;
  2130. update();
  2131. }
  2132. }
  2133. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2134. drag_touching = false;
  2135. }
  2136. if (p_what == NOTIFICATION_ENTER_TREE) {
  2137. update_cache();
  2138. }
  2139. if (p_what == NOTIFICATION_DRAG_END) {
  2140. drop_mode_flags = 0;
  2141. scrolling = false;
  2142. set_physics_process_internal(false);
  2143. update();
  2144. }
  2145. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2146. single_select_defer = NULL;
  2147. if (cache.scroll_speed > 0 && get_rect().has_point(get_viewport()->get_mouse_position() - get_global_position())) {
  2148. scrolling = true;
  2149. set_physics_process_internal(true);
  2150. }
  2151. }
  2152. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  2153. if (drag_touching) {
  2154. if (drag_touching_deaccel) {
  2155. float pos = v_scroll->get_value();
  2156. pos += drag_speed * get_physics_process_delta_time();
  2157. bool turnoff = false;
  2158. if (pos < 0) {
  2159. pos = 0;
  2160. turnoff = true;
  2161. set_physics_process_internal(false);
  2162. drag_touching = false;
  2163. drag_touching_deaccel = false;
  2164. }
  2165. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2166. pos = v_scroll->get_max() - v_scroll->get_page();
  2167. turnoff = true;
  2168. }
  2169. v_scroll->set_value(pos);
  2170. float sgn = drag_speed < 0 ? -1 : 1;
  2171. float val = Math::abs(drag_speed);
  2172. val -= 1000 * get_physics_process_delta_time();
  2173. if (val < 0) {
  2174. turnoff = true;
  2175. }
  2176. drag_speed = sgn * val;
  2177. if (turnoff) {
  2178. set_physics_process_internal(false);
  2179. drag_touching = false;
  2180. drag_touching_deaccel = false;
  2181. }
  2182. } else {
  2183. }
  2184. }
  2185. if (scrolling) {
  2186. Point2 point = get_viewport()->get_mouse_position() - get_global_position();
  2187. if (point.x < cache.scroll_border) {
  2188. point.x -= cache.scroll_border;
  2189. } else if (point.x > get_size().width - cache.scroll_border) {
  2190. point.x -= get_size().width - cache.scroll_border;
  2191. } else {
  2192. point.x = 0;
  2193. }
  2194. if (point.y < cache.scroll_border) {
  2195. point.y -= cache.scroll_border;
  2196. } else if (point.y > get_size().height - cache.scroll_border) {
  2197. point.y -= get_size().height - cache.scroll_border;
  2198. } else {
  2199. point.y = 0;
  2200. }
  2201. point *= cache.scroll_speed * get_physics_process_delta_time();
  2202. point += get_scroll();
  2203. h_scroll->set_value(point.x);
  2204. v_scroll->set_value(point.y);
  2205. }
  2206. }
  2207. if (p_what == NOTIFICATION_DRAW) {
  2208. update_cache();
  2209. update_scrollbars();
  2210. RID ci = get_canvas_item();
  2211. Ref<StyleBox> bg = cache.bg;
  2212. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  2213. Point2 draw_ofs;
  2214. draw_ofs += bg->get_offset();
  2215. Size2 draw_size = get_size() - bg->get_minimum_size();
  2216. bg->draw(ci, Rect2(Point2(), get_size()));
  2217. if (has_focus()) {
  2218. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2219. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2220. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2221. }
  2222. int tbh = _get_title_button_height();
  2223. draw_ofs.y += tbh;
  2224. draw_size.y -= tbh;
  2225. if (root) {
  2226. draw_item(Point2(), draw_ofs, draw_size, root);
  2227. }
  2228. int ofs = 0;
  2229. for (int i = 0; i < (columns.size() - 1 - 1); i++) {
  2230. ofs += get_column_width(i);
  2231. }
  2232. if (show_column_titles) {
  2233. //title butons
  2234. int ofs = cache.bg->get_margin(MARGIN_LEFT);
  2235. for (int i = 0; i < columns.size(); i++) {
  2236. 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);
  2237. Ref<Font> f = cache.tb_font;
  2238. Rect2 tbrect = Rect2(ofs - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh);
  2239. sb->draw(ci, tbrect);
  2240. ofs += tbrect.size.width;
  2241. //text
  2242. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2243. 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);
  2244. }
  2245. }
  2246. }
  2247. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2248. update_cache();
  2249. }
  2250. }
  2251. Size2 Tree::get_minimum_size() const {
  2252. return Size2(1, 1);
  2253. }
  2254. TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
  2255. ERR_FAIL_COND_V(blocked > 0, NULL);
  2256. TreeItem *ti = NULL;
  2257. if (p_parent) {
  2258. // Append or insert a new item to the given parent.
  2259. ti = memnew(TreeItem(this));
  2260. ERR_FAIL_COND_V(!ti, NULL);
  2261. ti->cells.resize(columns.size());
  2262. TreeItem *prev = NULL;
  2263. TreeItem *c = p_parent->children;
  2264. int idx = 0;
  2265. while (c) {
  2266. if (idx++ == p_idx) {
  2267. ti->next = c;
  2268. break;
  2269. }
  2270. prev = c;
  2271. c = c->next;
  2272. }
  2273. if (prev)
  2274. prev->next = ti;
  2275. else
  2276. p_parent->children = ti;
  2277. ti->parent = p_parent;
  2278. } else {
  2279. if (!root) {
  2280. // No root exists, make the given item the new root.
  2281. ti = memnew(TreeItem(this));
  2282. ERR_FAIL_COND_V(!ti, NULL);
  2283. ti->cells.resize(columns.size());
  2284. root = ti;
  2285. } else {
  2286. // Root exists, append or insert to root.
  2287. ti = create_item(root, p_idx);
  2288. }
  2289. }
  2290. return ti;
  2291. }
  2292. TreeItem *Tree::get_root() {
  2293. return root;
  2294. }
  2295. TreeItem *Tree::get_last_item() {
  2296. TreeItem *last = root;
  2297. while (last) {
  2298. if (last->next)
  2299. last = last->next;
  2300. else if (last->children)
  2301. last = last->children;
  2302. else
  2303. break;
  2304. }
  2305. return last;
  2306. }
  2307. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2308. edited_item = p_item;
  2309. edited_col = p_column;
  2310. if (p_lmb)
  2311. emit_signal("item_edited");
  2312. else
  2313. emit_signal("item_rmb_edited");
  2314. }
  2315. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2316. update();
  2317. }
  2318. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2319. if (select_mode == SELECT_MULTI) {
  2320. if (!p_item->cells[p_column].selectable)
  2321. return;
  2322. p_item->cells.write[p_column].selected = true;
  2323. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2324. selected_col = p_column;
  2325. } else {
  2326. select_single_item(p_item, root, p_column);
  2327. }
  2328. update();
  2329. }
  2330. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  2331. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  2332. p_item->cells.write[p_column].selected = false;
  2333. }
  2334. update();
  2335. }
  2336. void Tree::set_select_mode(SelectMode p_mode) {
  2337. select_mode = p_mode;
  2338. }
  2339. Tree::SelectMode Tree::get_select_mode() const {
  2340. return select_mode;
  2341. }
  2342. void Tree::deselect_all() {
  2343. TreeItem *item = get_next_selected(get_root());
  2344. while (item) {
  2345. item->deselect(selected_col);
  2346. TreeItem *prev_item = item;
  2347. item = get_next_selected(get_root());
  2348. ERR_FAIL_COND(item == prev_item);
  2349. }
  2350. selected_item = NULL;
  2351. selected_col = -1;
  2352. update();
  2353. }
  2354. bool Tree::is_anything_selected() {
  2355. return (selected_item != NULL);
  2356. }
  2357. void Tree::clear() {
  2358. if (blocked > 0) {
  2359. ERR_FAIL_COND(blocked > 0);
  2360. }
  2361. if (pressing_for_editor) {
  2362. if (range_drag_enabled) {
  2363. range_drag_enabled = false;
  2364. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2365. warp_mouse(range_drag_capture_pos);
  2366. }
  2367. pressing_for_editor = false;
  2368. }
  2369. if (root) {
  2370. memdelete(root);
  2371. root = NULL;
  2372. };
  2373. selected_item = NULL;
  2374. edited_item = NULL;
  2375. popup_edited_item = NULL;
  2376. update();
  2377. };
  2378. void Tree::set_hide_root(bool p_enabled) {
  2379. hide_root = p_enabled;
  2380. update();
  2381. }
  2382. bool Tree::is_root_hidden() const {
  2383. return hide_root;
  2384. }
  2385. void Tree::set_column_min_width(int p_column, int p_min_width) {
  2386. ERR_FAIL_INDEX(p_column, columns.size());
  2387. if (p_min_width < 1)
  2388. return;
  2389. columns.write[p_column].min_width = p_min_width;
  2390. update();
  2391. }
  2392. void Tree::set_column_expand(int p_column, bool p_expand) {
  2393. ERR_FAIL_INDEX(p_column, columns.size());
  2394. columns.write[p_column].expand = p_expand;
  2395. update();
  2396. }
  2397. TreeItem *Tree::get_selected() const {
  2398. return selected_item;
  2399. }
  2400. int Tree::get_selected_column() const {
  2401. return selected_col;
  2402. }
  2403. TreeItem *Tree::get_edited() const {
  2404. return edited_item;
  2405. }
  2406. int Tree::get_edited_column() const {
  2407. return edited_col;
  2408. }
  2409. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  2410. /*
  2411. if (!p_item)
  2412. return NULL;
  2413. */
  2414. if (!root)
  2415. return NULL;
  2416. while (true) {
  2417. if (!p_item) {
  2418. p_item = root;
  2419. } else {
  2420. if (p_item->children) {
  2421. p_item = p_item->children;
  2422. } else if (p_item->next) {
  2423. p_item = p_item->next;
  2424. } else {
  2425. while (!p_item->next) {
  2426. p_item = p_item->parent;
  2427. if (p_item == NULL)
  2428. return NULL;
  2429. }
  2430. p_item = p_item->next;
  2431. }
  2432. }
  2433. for (int i = 0; i < columns.size(); i++)
  2434. if (p_item->cells[i].selected)
  2435. return p_item;
  2436. }
  2437. return NULL;
  2438. }
  2439. int Tree::get_column_width(int p_column) const {
  2440. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  2441. if (!columns[p_column].expand)
  2442. return columns[p_column].min_width;
  2443. Ref<StyleBox> bg = cache.bg;
  2444. int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT));
  2445. if (v_scroll->is_visible_in_tree())
  2446. expand_area -= v_scroll->get_combined_minimum_size().width;
  2447. int expanding_columns = 0;
  2448. int expanding_total = 0;
  2449. for (int i = 0; i < columns.size(); i++) {
  2450. if (!columns[i].expand) {
  2451. expand_area -= columns[i].min_width;
  2452. } else {
  2453. expanding_total += columns[i].min_width;
  2454. expanding_columns++;
  2455. }
  2456. }
  2457. if (expand_area < expanding_total)
  2458. return columns[p_column].min_width;
  2459. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  2460. return expand_area * columns[p_column].min_width / expanding_total;
  2461. }
  2462. void Tree::propagate_set_columns(TreeItem *p_item) {
  2463. p_item->cells.resize(columns.size());
  2464. TreeItem *c = p_item->get_children();
  2465. while (c) {
  2466. propagate_set_columns(c);
  2467. c = c->get_next();
  2468. }
  2469. }
  2470. void Tree::set_columns(int p_columns) {
  2471. ERR_FAIL_COND(p_columns < 1);
  2472. ERR_FAIL_COND(blocked > 0);
  2473. columns.resize(p_columns);
  2474. if (root)
  2475. propagate_set_columns(root);
  2476. if (selected_col >= p_columns)
  2477. selected_col = p_columns - 1;
  2478. update();
  2479. }
  2480. int Tree::get_columns() const {
  2481. return columns.size();
  2482. }
  2483. void Tree::_scroll_moved(float) {
  2484. update();
  2485. }
  2486. Rect2 Tree::get_custom_popup_rect() const {
  2487. return custom_popup_rect;
  2488. }
  2489. int Tree::get_item_offset(TreeItem *p_item) const {
  2490. TreeItem *it = root;
  2491. int ofs = _get_title_button_height();
  2492. if (!it)
  2493. return 0;
  2494. while (true) {
  2495. if (it == p_item)
  2496. return ofs;
  2497. ofs += compute_item_height(it) + cache.vseparation;
  2498. if (it->children && !it->collapsed) {
  2499. it = it->children;
  2500. } else if (it->next) {
  2501. it = it->next;
  2502. } else {
  2503. while (!it->next) {
  2504. it = it->parent;
  2505. if (it == NULL)
  2506. return 0;
  2507. }
  2508. it = it->next;
  2509. }
  2510. }
  2511. return -1; //not found
  2512. }
  2513. void Tree::ensure_cursor_is_visible() {
  2514. if (!is_inside_tree())
  2515. return;
  2516. TreeItem *selected = get_selected();
  2517. if (!selected)
  2518. return;
  2519. int ofs = get_item_offset(selected);
  2520. if (ofs == -1)
  2521. return;
  2522. int h = compute_item_height(selected) + cache.vseparation;
  2523. int screenh = get_size().height - h_scroll->get_combined_minimum_size().height;
  2524. if (ofs + h > v_scroll->get_value() + screenh)
  2525. v_scroll->call_deferred("set_value", ofs - screenh + h);
  2526. else if (ofs < v_scroll->get_value())
  2527. v_scroll->set_value(ofs);
  2528. }
  2529. int Tree::get_pressed_button() const {
  2530. return pressed_button;
  2531. }
  2532. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  2533. ERR_FAIL_NULL_V(p_item, Rect2());
  2534. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  2535. if (p_column != -1) {
  2536. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  2537. }
  2538. int ofs = get_item_offset(p_item);
  2539. int height = compute_item_height(p_item);
  2540. Rect2 r;
  2541. r.position.y = ofs;
  2542. r.size.height = height;
  2543. if (p_column == -1) {
  2544. r.position.x = 0;
  2545. r.size.x = get_size().width;
  2546. } else {
  2547. int accum = 0;
  2548. for (int i = 0; i < p_column; i++) {
  2549. accum += get_column_width(i);
  2550. }
  2551. r.position.x = accum;
  2552. r.size.x = get_column_width(p_column);
  2553. }
  2554. return r;
  2555. }
  2556. void Tree::set_column_titles_visible(bool p_show) {
  2557. show_column_titles = p_show;
  2558. update();
  2559. }
  2560. bool Tree::are_column_titles_visible() const {
  2561. return show_column_titles;
  2562. }
  2563. void Tree::set_column_title(int p_column, const String &p_title) {
  2564. ERR_FAIL_INDEX(p_column, columns.size());
  2565. columns.write[p_column].title = p_title;
  2566. update();
  2567. }
  2568. String Tree::get_column_title(int p_column) const {
  2569. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  2570. return columns[p_column].title;
  2571. }
  2572. Point2 Tree::get_scroll() const {
  2573. Point2 ofs;
  2574. if (h_scroll->is_visible_in_tree())
  2575. ofs.x = h_scroll->get_value();
  2576. if (v_scroll->is_visible_in_tree())
  2577. ofs.y = v_scroll->get_value();
  2578. return ofs;
  2579. }
  2580. void Tree::scroll_to_item(TreeItem *p_item) {
  2581. if (!is_visible_in_tree()) {
  2582. // hack to work around crash in get_item_rect() if Tree is not in tree.
  2583. return;
  2584. }
  2585. // make sure the scrollbar min and max are up to date with latest changes.
  2586. update_scrollbars();
  2587. const Rect2 r = get_item_rect(p_item);
  2588. if (r.position.y < v_scroll->get_value()) {
  2589. v_scroll->set_value(r.position.y);
  2590. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  2591. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  2592. }
  2593. }
  2594. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  2595. while (p_at) {
  2596. for (int i = 0; i < columns.size(); i++) {
  2597. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  2598. if (r_col)
  2599. *r_col = i;
  2600. return p_at;
  2601. }
  2602. }
  2603. if (p_backwards)
  2604. p_at = p_at->get_prev_visible();
  2605. else
  2606. p_at = p_at->get_next_visible();
  2607. }
  2608. return NULL;
  2609. }
  2610. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  2611. if (!root)
  2612. return NULL;
  2613. return _search_item_text(root, p_find, r_col, p_selectable);
  2614. }
  2615. void Tree::_do_incr_search(const String &p_add) {
  2616. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2617. uint64_t diff = time - last_keypress;
  2618. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)))
  2619. incr_search = p_add;
  2620. else
  2621. incr_search += p_add;
  2622. last_keypress = time;
  2623. int col;
  2624. TreeItem *item = search_item_text(incr_search, &col, true);
  2625. if (!item)
  2626. return;
  2627. item->select(col);
  2628. ensure_cursor_is_visible();
  2629. }
  2630. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  2631. Point2 pos = p_pos;
  2632. if (root != p_item || !hide_root) {
  2633. h = compute_item_height(p_item) + cache.vseparation;
  2634. if (pos.y < h) {
  2635. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  2636. section = 0;
  2637. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  2638. section = pos.y < h / 2 ? -1 : 1;
  2639. } else if (pos.y < h / 4) {
  2640. section = -1;
  2641. } else if (pos.y >= (h * 3 / 4)) {
  2642. section = 1;
  2643. } else {
  2644. section = 0;
  2645. }
  2646. for (int i = 0; i < columns.size(); i++) {
  2647. int w = get_column_width(i);
  2648. if (pos.x < w) {
  2649. r_column = i;
  2650. return p_item;
  2651. }
  2652. pos.x -= w;
  2653. }
  2654. return NULL;
  2655. } else {
  2656. pos.y -= h;
  2657. }
  2658. } else {
  2659. h = 0;
  2660. }
  2661. if (p_item->is_collapsed())
  2662. return NULL; // do not try children, it's collapsed
  2663. TreeItem *n = p_item->get_children();
  2664. while (n) {
  2665. int ch;
  2666. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  2667. pos.y -= ch;
  2668. h += ch;
  2669. if (r)
  2670. return r;
  2671. n = n->get_next();
  2672. }
  2673. return NULL;
  2674. }
  2675. int Tree::get_column_at_position(const Point2 &p_pos) const {
  2676. if (root) {
  2677. Point2 pos = p_pos;
  2678. pos -= cache.bg->get_offset();
  2679. pos.y -= _get_title_button_height();
  2680. if (pos.y < 0)
  2681. return -1;
  2682. if (h_scroll->is_visible_in_tree())
  2683. pos.x += h_scroll->get_value();
  2684. if (v_scroll->is_visible_in_tree())
  2685. pos.y += v_scroll->get_value();
  2686. int col, h, section;
  2687. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2688. if (it) {
  2689. return col;
  2690. }
  2691. }
  2692. return -1;
  2693. }
  2694. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  2695. if (root) {
  2696. Point2 pos = p_pos;
  2697. pos -= cache.bg->get_offset();
  2698. pos.y -= _get_title_button_height();
  2699. if (pos.y < 0)
  2700. return -100;
  2701. if (h_scroll->is_visible_in_tree())
  2702. pos.x += h_scroll->get_value();
  2703. if (v_scroll->is_visible_in_tree())
  2704. pos.y += v_scroll->get_value();
  2705. int col, h, section;
  2706. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2707. if (it) {
  2708. return section;
  2709. }
  2710. }
  2711. return -100;
  2712. }
  2713. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  2714. if (root) {
  2715. Point2 pos = p_pos;
  2716. pos -= cache.bg->get_offset();
  2717. pos.y -= _get_title_button_height();
  2718. if (pos.y < 0)
  2719. return NULL;
  2720. if (h_scroll->is_visible_in_tree())
  2721. pos.x += h_scroll->get_value();
  2722. if (v_scroll->is_visible_in_tree())
  2723. pos.y += v_scroll->get_value();
  2724. int col, h, section;
  2725. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2726. if (it) {
  2727. return it;
  2728. }
  2729. }
  2730. return NULL;
  2731. }
  2732. String Tree::get_tooltip(const Point2 &p_pos) const {
  2733. if (root) {
  2734. Point2 pos = p_pos;
  2735. pos -= cache.bg->get_offset();
  2736. pos.y -= _get_title_button_height();
  2737. if (pos.y < 0)
  2738. return Control::get_tooltip(p_pos);
  2739. if (h_scroll->is_visible_in_tree())
  2740. pos.x += h_scroll->get_value();
  2741. if (v_scroll->is_visible_in_tree())
  2742. pos.y += v_scroll->get_value();
  2743. int col, h, section;
  2744. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2745. if (it) {
  2746. const TreeItem::Cell &c = it->cells[col];
  2747. int col_width = get_column_width(col);
  2748. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2749. Ref<Texture> b = c.buttons[j].texture;
  2750. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  2751. if (pos.x > col_width - size.width) {
  2752. String tooltip = c.buttons[j].tooltip;
  2753. if (tooltip != "") {
  2754. return tooltip;
  2755. }
  2756. }
  2757. col_width -= size.width;
  2758. }
  2759. String ret;
  2760. if (it->get_tooltip(col) == "")
  2761. ret = it->get_text(col);
  2762. else
  2763. ret = it->get_tooltip(col);
  2764. return ret;
  2765. }
  2766. }
  2767. return Control::get_tooltip(p_pos);
  2768. }
  2769. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2770. cursor_can_exit_tree = p_enable;
  2771. }
  2772. bool Tree::can_cursor_exit_tree() const {
  2773. return cursor_can_exit_tree;
  2774. }
  2775. void Tree::set_hide_folding(bool p_hide) {
  2776. hide_folding = p_hide;
  2777. update();
  2778. }
  2779. bool Tree::is_folding_hidden() const {
  2780. return hide_folding;
  2781. }
  2782. void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) {
  2783. evaluator = p_evaluator;
  2784. }
  2785. void Tree::set_drop_mode_flags(int p_flags) {
  2786. if (drop_mode_flags == p_flags)
  2787. return;
  2788. drop_mode_flags = p_flags;
  2789. if (drop_mode_flags == 0) {
  2790. drop_mode_over = NULL;
  2791. }
  2792. update();
  2793. }
  2794. int Tree::get_drop_mode_flags() const {
  2795. return drop_mode_flags;
  2796. }
  2797. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  2798. force_edit_checkbox_only_on_checkbox = p_enable;
  2799. }
  2800. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  2801. return force_edit_checkbox_only_on_checkbox;
  2802. }
  2803. void Tree::set_allow_rmb_select(bool p_allow) {
  2804. allow_rmb_select = p_allow;
  2805. }
  2806. bool Tree::get_allow_rmb_select() const {
  2807. return allow_rmb_select;
  2808. }
  2809. void Tree::set_allow_reselect(bool p_allow) {
  2810. allow_reselect = p_allow;
  2811. }
  2812. bool Tree::get_allow_reselect() const {
  2813. return allow_reselect;
  2814. }
  2815. void Tree::_bind_methods() {
  2816. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout);
  2817. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  2818. ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select);
  2819. ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter);
  2820. ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close);
  2821. ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed);
  2822. ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
  2823. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  2824. ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
  2825. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  2826. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  2827. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  2828. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  2829. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  2830. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  2831. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  2832. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  2833. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  2834. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  2835. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  2836. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  2837. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  2838. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  2839. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  2840. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  2841. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  2842. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  2843. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  2844. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  2845. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  2846. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  2847. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  2848. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  2849. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  2850. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  2851. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  2852. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  2853. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  2854. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  2855. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  2856. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  2857. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  2858. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  2859. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  2860. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  2861. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  2862. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  2863. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  2864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  2865. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In between"), "set_drop_mode_flags", "get_drop_mode_flags");
  2866. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  2867. ADD_SIGNAL(MethodInfo("item_selected"));
  2868. ADD_SIGNAL(MethodInfo("cell_selected"));
  2869. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  2870. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2871. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2872. ADD_SIGNAL(MethodInfo("item_edited"));
  2873. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  2874. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  2875. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  2876. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  2877. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2878. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  2879. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  2880. ADD_SIGNAL(MethodInfo("item_activated"));
  2881. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  2882. ADD_SIGNAL(MethodInfo("nothing_selected"));
  2883. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  2884. BIND_ENUM_CONSTANT(SELECT_ROW);
  2885. BIND_ENUM_CONSTANT(SELECT_MULTI);
  2886. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  2887. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  2888. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  2889. }
  2890. Tree::Tree() {
  2891. selected_col = 0;
  2892. columns.resize(1);
  2893. selected_item = NULL;
  2894. edited_item = NULL;
  2895. selected_col = -1;
  2896. edited_col = -1;
  2897. hide_root = false;
  2898. select_mode = SELECT_SINGLE;
  2899. root = 0;
  2900. popup_menu = NULL;
  2901. popup_edited_item = NULL;
  2902. text_editor = NULL;
  2903. set_focus_mode(FOCUS_ALL);
  2904. popup_menu = memnew(PopupMenu);
  2905. popup_menu->hide();
  2906. add_child(popup_menu);
  2907. popup_menu->set_as_toplevel(true);
  2908. text_editor = memnew(LineEdit);
  2909. add_child(text_editor);
  2910. text_editor->set_as_toplevel(true);
  2911. text_editor->hide();
  2912. value_editor = memnew(HSlider);
  2913. add_child(value_editor);
  2914. value_editor->set_as_toplevel(true);
  2915. value_editor->hide();
  2916. h_scroll = memnew(HScrollBar);
  2917. v_scroll = memnew(VScrollBar);
  2918. add_child(h_scroll);
  2919. add_child(v_scroll);
  2920. range_click_timer = memnew(Timer);
  2921. range_click_timer->connect("timeout", this, "_range_click_timeout");
  2922. add_child(range_click_timer);
  2923. h_scroll->connect("value_changed", this, "_scroll_moved");
  2924. v_scroll->connect("value_changed", this, "_scroll_moved");
  2925. text_editor->connect("text_entered", this, "_text_editor_enter");
  2926. text_editor->connect("modal_closed", this, "_text_editor_modal_close");
  2927. popup_menu->connect("id_pressed", this, "_popup_select");
  2928. value_editor->connect("value_changed", this, "_value_editor_changed");
  2929. value_editor->set_as_toplevel(true);
  2930. text_editor->set_as_toplevel(true);
  2931. updating_value_editor = false;
  2932. pressed_button = -1;
  2933. show_column_titles = false;
  2934. cache.click_type = Cache::CLICK_NONE;
  2935. cache.hover_type = Cache::CLICK_NONE;
  2936. cache.hover_index = -1;
  2937. cache.click_index = -1;
  2938. cache.click_id = -1;
  2939. cache.click_item = NULL;
  2940. cache.click_column = 0;
  2941. cache.hover_cell = -1;
  2942. cache.hover_index = -1;
  2943. last_keypress = 0;
  2944. focus_in_id = 0;
  2945. blocked = 0;
  2946. cursor_can_exit_tree = true;
  2947. set_mouse_filter(MOUSE_FILTER_STOP);
  2948. drag_speed = 0;
  2949. drag_touching = false;
  2950. drag_touching_deaccel = false;
  2951. pressing_for_editor = false;
  2952. range_drag_enabled = false;
  2953. hide_folding = false;
  2954. evaluator = NULL;
  2955. drop_mode_flags = 0;
  2956. drop_mode_over = NULL;
  2957. drop_mode_section = 0;
  2958. single_select_defer = NULL;
  2959. allow_rmb_select = false;
  2960. force_edit_checkbox_only_on_checkbox = false;
  2961. set_clip_contents(true);
  2962. cache.hover_item = NULL;
  2963. cache.hover_cell = -1;
  2964. allow_reselect = false;
  2965. }
  2966. Tree::~Tree() {
  2967. if (root) {
  2968. memdelete(root);
  2969. }
  2970. }