tree.cpp 133 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "tree.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/math/math_funcs.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/string/print_string.h"
  37. #include "core/string/translation.h"
  38. #include "scene/main/window.h"
  39. #include "box_container.h"
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/editor_scale.h"
  42. #endif
  43. #include <limits.h>
  44. Size2 TreeItem::Cell::get_icon_size() const {
  45. if (icon.is_null()) {
  46. return Size2();
  47. }
  48. if (icon_region == Rect2i()) {
  49. return icon->get_size();
  50. } else {
  51. return icon_region.size;
  52. }
  53. }
  54. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  55. if (icon.is_null()) {
  56. return;
  57. }
  58. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  59. if (icon_region == Rect2i()) {
  60. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  61. } else {
  62. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  63. }
  64. }
  65. void TreeItem::_changed_notify(int p_cell) {
  66. tree->item_changed(p_cell, this);
  67. }
  68. void TreeItem::_changed_notify() {
  69. tree->item_changed(-1, this);
  70. }
  71. void TreeItem::_cell_selected(int p_cell) {
  72. tree->item_selected(p_cell, this);
  73. }
  74. void TreeItem::_cell_deselected(int p_cell) {
  75. tree->item_deselected(p_cell, this);
  76. }
  77. void TreeItem::_change_tree(Tree *p_tree) {
  78. if (p_tree == tree) {
  79. return;
  80. }
  81. TreeItem *c = first_child;
  82. while (c) {
  83. c->_change_tree(p_tree);
  84. c = c->next;
  85. }
  86. if (tree && tree->root == this) {
  87. tree->root = nullptr;
  88. }
  89. if (tree && tree->popup_edited_item == this) {
  90. tree->popup_edited_item = nullptr;
  91. tree->pressing_for_editor = false;
  92. }
  93. if (tree && tree->cache.hover_item == this) {
  94. tree->cache.hover_item = nullptr;
  95. }
  96. if (tree && tree->selected_item == this) {
  97. tree->selected_item = nullptr;
  98. }
  99. if (tree && tree->drop_mode_over == this) {
  100. tree->drop_mode_over = nullptr;
  101. }
  102. if (tree && tree->single_select_defer == this) {
  103. tree->single_select_defer = nullptr;
  104. }
  105. if (tree && tree->edited_item == this) {
  106. tree->edited_item = nullptr;
  107. tree->pressing_for_editor = false;
  108. }
  109. tree = p_tree;
  110. if (tree) {
  111. cells.resize(tree->columns.size());
  112. }
  113. }
  114. /* cell mode */
  115. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  116. ERR_FAIL_INDEX(p_column, cells.size());
  117. Cell &c = cells.write[p_column];
  118. c.mode = p_mode;
  119. c.min = 0;
  120. c.max = 100;
  121. c.step = 1;
  122. c.val = 0;
  123. c.checked = false;
  124. c.icon = Ref<Texture2D>();
  125. c.text = "";
  126. c.dirty = true;
  127. c.icon_max_w = 0;
  128. _changed_notify(p_column);
  129. }
  130. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  131. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  132. return cells[p_column].mode;
  133. }
  134. /* check mode */
  135. void TreeItem::set_checked(int p_column, bool p_checked) {
  136. ERR_FAIL_INDEX(p_column, cells.size());
  137. cells.write[p_column].checked = p_checked;
  138. _changed_notify(p_column);
  139. }
  140. bool TreeItem::is_checked(int p_column) const {
  141. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  142. return cells[p_column].checked;
  143. }
  144. void TreeItem::set_text(int p_column, String p_text) {
  145. ERR_FAIL_INDEX(p_column, cells.size());
  146. cells.write[p_column].text = p_text;
  147. cells.write[p_column].dirty = true;
  148. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  149. Vector<String> strings = p_text.split(",");
  150. cells.write[p_column].min = INT_MAX;
  151. cells.write[p_column].max = INT_MIN;
  152. for (int i = 0; i < strings.size(); i++) {
  153. int value = i;
  154. if (!strings[i].get_slicec(':', 1).is_empty()) {
  155. value = strings[i].get_slicec(':', 1).to_int();
  156. }
  157. cells.write[p_column].min = MIN(cells[p_column].min, value);
  158. cells.write[p_column].max = MAX(cells[p_column].max, value);
  159. }
  160. cells.write[p_column].step = 0;
  161. }
  162. _changed_notify(p_column);
  163. }
  164. String TreeItem::get_text(int p_column) const {
  165. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  166. return cells[p_column].text;
  167. }
  168. void TreeItem::set_text_direction(int p_column, Control::TextDirection p_text_direction) {
  169. ERR_FAIL_INDEX(p_column, cells.size());
  170. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  171. if (cells[p_column].text_direction != p_text_direction) {
  172. cells.write[p_column].text_direction = p_text_direction;
  173. cells.write[p_column].dirty = true;
  174. _changed_notify(p_column);
  175. }
  176. }
  177. Control::TextDirection TreeItem::get_text_direction(int p_column) const {
  178. ERR_FAIL_INDEX_V(p_column, cells.size(), Control::TEXT_DIRECTION_INHERITED);
  179. return cells[p_column].text_direction;
  180. }
  181. void TreeItem::clear_opentype_features(int p_column) {
  182. ERR_FAIL_INDEX(p_column, cells.size());
  183. cells.write[p_column].opentype_features.clear();
  184. cells.write[p_column].dirty = true;
  185. _changed_notify(p_column);
  186. }
  187. void TreeItem::set_opentype_feature(int p_column, const String &p_name, int p_value) {
  188. ERR_FAIL_INDEX(p_column, cells.size());
  189. int32_t tag = TS->name_to_tag(p_name);
  190. if (!cells[p_column].opentype_features.has(tag) || (int)cells[p_column].opentype_features[tag] != p_value) {
  191. cells.write[p_column].opentype_features[tag] = p_value;
  192. cells.write[p_column].dirty = true;
  193. _changed_notify(p_column);
  194. }
  195. }
  196. int TreeItem::get_opentype_feature(int p_column, const String &p_name) const {
  197. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  198. int32_t tag = TS->name_to_tag(p_name);
  199. if (!cells[p_column].opentype_features.has(tag)) {
  200. return -1;
  201. }
  202. return cells[p_column].opentype_features[tag];
  203. }
  204. void TreeItem::set_structured_text_bidi_override(int p_column, Control::StructuredTextParser p_parser) {
  205. ERR_FAIL_INDEX(p_column, cells.size());
  206. if (cells[p_column].st_parser != p_parser) {
  207. cells.write[p_column].st_parser = p_parser;
  208. cells.write[p_column].dirty = true;
  209. _changed_notify(p_column);
  210. }
  211. }
  212. Control::StructuredTextParser TreeItem::get_structured_text_bidi_override(int p_column) const {
  213. ERR_FAIL_INDEX_V(p_column, cells.size(), Control::STRUCTURED_TEXT_NONE);
  214. return cells[p_column].st_parser;
  215. }
  216. void TreeItem::set_structured_text_bidi_override_options(int p_column, Array p_args) {
  217. ERR_FAIL_INDEX(p_column, cells.size());
  218. cells.write[p_column].st_args = p_args;
  219. cells.write[p_column].dirty = true;
  220. _changed_notify(p_column);
  221. }
  222. Array TreeItem::get_structured_text_bidi_override_options(int p_column) const {
  223. ERR_FAIL_INDEX_V(p_column, cells.size(), Array());
  224. return cells[p_column].st_args;
  225. }
  226. void TreeItem::set_language(int p_column, const String &p_language) {
  227. ERR_FAIL_INDEX(p_column, cells.size());
  228. if (cells[p_column].language != p_language) {
  229. cells.write[p_column].language = p_language;
  230. cells.write[p_column].dirty = true;
  231. _changed_notify(p_column);
  232. }
  233. }
  234. String TreeItem::get_language(int p_column) const {
  235. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  236. return cells[p_column].language;
  237. }
  238. void TreeItem::set_suffix(int p_column, String p_suffix) {
  239. ERR_FAIL_INDEX(p_column, cells.size());
  240. cells.write[p_column].suffix = p_suffix;
  241. _changed_notify(p_column);
  242. }
  243. String TreeItem::get_suffix(int p_column) const {
  244. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  245. return cells[p_column].suffix;
  246. }
  247. void TreeItem::set_icon(int p_column, const Ref<Texture2D> &p_icon) {
  248. ERR_FAIL_INDEX(p_column, cells.size());
  249. cells.write[p_column].icon = p_icon;
  250. _changed_notify(p_column);
  251. }
  252. Ref<Texture2D> TreeItem::get_icon(int p_column) const {
  253. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  254. return cells[p_column].icon;
  255. }
  256. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  257. ERR_FAIL_INDEX(p_column, cells.size());
  258. cells.write[p_column].icon_region = p_icon_region;
  259. _changed_notify(p_column);
  260. }
  261. Rect2 TreeItem::get_icon_region(int p_column) const {
  262. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  263. return cells[p_column].icon_region;
  264. }
  265. void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
  266. ERR_FAIL_INDEX(p_column, cells.size());
  267. cells.write[p_column].icon_color = p_modulate;
  268. _changed_notify(p_column);
  269. }
  270. Color TreeItem::get_icon_modulate(int p_column) const {
  271. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  272. return cells[p_column].icon_color;
  273. }
  274. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  275. ERR_FAIL_INDEX(p_column, cells.size());
  276. cells.write[p_column].icon_max_w = p_max;
  277. _changed_notify(p_column);
  278. }
  279. int TreeItem::get_icon_max_width(int p_column) const {
  280. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  281. return cells[p_column].icon_max_w;
  282. }
  283. /* range works for mode number or mode combo */
  284. void TreeItem::set_range(int p_column, double p_value) {
  285. ERR_FAIL_INDEX(p_column, cells.size());
  286. if (cells[p_column].step > 0) {
  287. p_value = Math::snapped(p_value, cells[p_column].step);
  288. }
  289. if (p_value < cells[p_column].min) {
  290. p_value = cells[p_column].min;
  291. }
  292. if (p_value > cells[p_column].max) {
  293. p_value = cells[p_column].max;
  294. }
  295. cells.write[p_column].val = p_value;
  296. cells.write[p_column].dirty = true;
  297. _changed_notify(p_column);
  298. }
  299. double TreeItem::get_range(int p_column) const {
  300. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  301. return cells[p_column].val;
  302. }
  303. bool TreeItem::is_range_exponential(int p_column) const {
  304. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  305. return cells[p_column].expr;
  306. }
  307. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  308. ERR_FAIL_INDEX(p_column, cells.size());
  309. cells.write[p_column].min = p_min;
  310. cells.write[p_column].max = p_max;
  311. cells.write[p_column].step = p_step;
  312. cells.write[p_column].expr = p_exp;
  313. _changed_notify(p_column);
  314. }
  315. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  316. ERR_FAIL_INDEX(p_column, cells.size());
  317. r_min = cells[p_column].min;
  318. r_max = cells[p_column].max;
  319. r_step = cells[p_column].step;
  320. }
  321. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  322. ERR_FAIL_INDEX(p_column, cells.size());
  323. cells.write[p_column].meta = p_meta;
  324. }
  325. Variant TreeItem::get_metadata(int p_column) const {
  326. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  327. return cells[p_column].meta;
  328. }
  329. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  330. ERR_FAIL_INDEX(p_column, cells.size());
  331. ERR_FAIL_NULL(p_object);
  332. cells.write[p_column].custom_draw_obj = p_object->get_instance_id();
  333. cells.write[p_column].custom_draw_callback = p_callback;
  334. }
  335. void TreeItem::set_collapsed(bool p_collapsed) {
  336. if (collapsed == p_collapsed || !tree) {
  337. return;
  338. }
  339. collapsed = p_collapsed;
  340. TreeItem *ci = tree->selected_item;
  341. if (ci) {
  342. while (ci && ci != this) {
  343. ci = ci->parent;
  344. }
  345. if (ci) { // collapsing cursor/selected, move it!
  346. if (tree->select_mode == Tree::SELECT_MULTI) {
  347. tree->selected_item = this;
  348. emit_signal("cell_selected");
  349. } else {
  350. select(tree->selected_col);
  351. }
  352. tree->update();
  353. }
  354. }
  355. _changed_notify();
  356. tree->emit_signal("item_collapsed", this);
  357. }
  358. bool TreeItem::is_collapsed() {
  359. return collapsed;
  360. }
  361. void TreeItem::uncollapse_tree() {
  362. TreeItem *t = this;
  363. while (t) {
  364. t->set_collapsed(false);
  365. t = t->parent;
  366. }
  367. }
  368. void TreeItem::set_custom_minimum_height(int p_height) {
  369. custom_min_height = p_height;
  370. _changed_notify();
  371. }
  372. int TreeItem::get_custom_minimum_height() const {
  373. return custom_min_height;
  374. }
  375. /* Item manipulation */
  376. TreeItem *TreeItem::create_child(int p_idx) {
  377. TreeItem *ti = memnew(TreeItem(tree));
  378. if (tree) {
  379. ti->cells.resize(tree->columns.size());
  380. }
  381. TreeItem *l_prev = nullptr;
  382. TreeItem *c = first_child;
  383. int idx = 0;
  384. while (c) {
  385. if (idx++ == p_idx) {
  386. c->prev = ti;
  387. ti->next = c;
  388. break;
  389. }
  390. l_prev = c;
  391. c = c->next;
  392. }
  393. if (l_prev) {
  394. l_prev->next = ti;
  395. ti->prev = l_prev;
  396. if (!children_cache.is_empty()) {
  397. if (ti->next) {
  398. children_cache.insert(p_idx, ti);
  399. } else {
  400. children_cache.append(ti);
  401. }
  402. }
  403. } else {
  404. first_child = ti;
  405. if (!children_cache.is_empty()) {
  406. children_cache.insert(0, ti);
  407. }
  408. }
  409. ti->parent = this;
  410. return ti;
  411. }
  412. Tree *TreeItem::get_tree() {
  413. return tree;
  414. }
  415. TreeItem *TreeItem::get_next() {
  416. return next;
  417. }
  418. TreeItem *TreeItem::get_prev() {
  419. if (prev) {
  420. return prev;
  421. }
  422. if (!parent || parent->first_child == this) {
  423. return nullptr;
  424. }
  425. // This is an edge case
  426. TreeItem *l_prev = parent->first_child;
  427. while (l_prev && l_prev->next != this) {
  428. l_prev = l_prev->next;
  429. }
  430. prev = l_prev;
  431. return prev;
  432. }
  433. TreeItem *TreeItem::get_parent() {
  434. return parent;
  435. }
  436. TreeItem *TreeItem::get_first_child() {
  437. return first_child;
  438. }
  439. TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
  440. TreeItem *current = this;
  441. TreeItem *prev = current->get_prev();
  442. if (!prev) {
  443. current = current->parent;
  444. if (current == tree->root && tree->hide_root) {
  445. return nullptr;
  446. } else if (!current) {
  447. if (p_wrap) {
  448. current = this;
  449. TreeItem *temp = this->get_next_visible();
  450. while (temp) {
  451. current = temp;
  452. temp = temp->get_next_visible();
  453. }
  454. } else {
  455. return nullptr;
  456. }
  457. }
  458. } else {
  459. current = prev;
  460. while (!current->collapsed && current->first_child) {
  461. //go to the very end
  462. current = current->first_child;
  463. while (current->next) {
  464. current = current->next;
  465. }
  466. }
  467. }
  468. return current;
  469. }
  470. TreeItem *TreeItem::get_next_visible(bool p_wrap) {
  471. TreeItem *current = this;
  472. if (!current->collapsed && current->first_child) {
  473. current = current->first_child;
  474. } else if (current->next) {
  475. current = current->next;
  476. } else {
  477. while (current && !current->next) {
  478. current = current->parent;
  479. }
  480. if (!current) {
  481. if (p_wrap) {
  482. return tree->root;
  483. } else {
  484. return nullptr;
  485. }
  486. } else {
  487. current = current->next;
  488. }
  489. }
  490. return current;
  491. }
  492. TreeItem *TreeItem::get_child(int p_idx) {
  493. _create_children_cache();
  494. ERR_FAIL_INDEX_V(p_idx, children_cache.size(), nullptr);
  495. return children_cache.get(p_idx);
  496. }
  497. int TreeItem::get_child_count() {
  498. _create_children_cache();
  499. return children_cache.size();
  500. }
  501. Array TreeItem::get_children() {
  502. int size = get_child_count();
  503. Array arr;
  504. arr.resize(size);
  505. for (int i = 0; i < size; i++) {
  506. arr[i] = children_cache[i];
  507. }
  508. return arr;
  509. }
  510. int TreeItem::get_index() {
  511. int idx = 0;
  512. TreeItem *c = this;
  513. while (c) {
  514. c = c->get_prev();
  515. idx++;
  516. }
  517. return idx - 1;
  518. }
  519. void TreeItem::move_before(TreeItem *p_item) {
  520. ERR_FAIL_NULL(p_item);
  521. ERR_FAIL_COND(is_root);
  522. ERR_FAIL_COND(!p_item->parent);
  523. if (p_item == this) {
  524. return;
  525. }
  526. TreeItem *p = p_item->parent;
  527. while (p) {
  528. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  529. p = p->parent;
  530. }
  531. Tree *old_tree = tree;
  532. _unlink_from_tree();
  533. _change_tree(p_item->tree);
  534. parent = p_item->parent;
  535. TreeItem *item_prev = p_item->get_prev();
  536. if (item_prev) {
  537. item_prev->next = this;
  538. parent->children_cache.clear();
  539. } else {
  540. parent->first_child = this;
  541. parent->children_cache.insert(0, this);
  542. }
  543. prev = item_prev;
  544. next = p_item;
  545. p_item->prev = this;
  546. if (old_tree && old_tree != tree) {
  547. old_tree->update();
  548. }
  549. if (tree) {
  550. tree->update();
  551. }
  552. }
  553. void TreeItem::move_after(TreeItem *p_item) {
  554. ERR_FAIL_NULL(p_item);
  555. ERR_FAIL_COND(is_root);
  556. ERR_FAIL_COND(!p_item->parent);
  557. if (p_item == this) {
  558. return;
  559. }
  560. TreeItem *p = p_item->parent;
  561. while (p) {
  562. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  563. p = p->parent;
  564. }
  565. Tree *old_tree = tree;
  566. _unlink_from_tree();
  567. _change_tree(p_item->tree);
  568. if (p_item->next) {
  569. p_item->next->prev = this;
  570. }
  571. parent = p_item->parent;
  572. prev = p_item;
  573. next = p_item->next;
  574. p_item->next = this;
  575. if (next) {
  576. parent->children_cache.clear();
  577. } else {
  578. parent->children_cache.append(this);
  579. }
  580. if (old_tree && old_tree != tree) {
  581. old_tree->update();
  582. }
  583. if (tree) {
  584. tree->update();
  585. }
  586. }
  587. void TreeItem::remove_child(TreeItem *p_item) {
  588. ERR_FAIL_NULL(p_item);
  589. ERR_FAIL_COND(p_item->parent != this);
  590. p_item->_unlink_from_tree();
  591. p_item->prev = nullptr;
  592. p_item->next = nullptr;
  593. p_item->parent = nullptr;
  594. if (tree) {
  595. tree->update();
  596. }
  597. }
  598. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  599. ERR_FAIL_INDEX(p_column, cells.size());
  600. cells.write[p_column].selectable = p_selectable;
  601. }
  602. bool TreeItem::is_selectable(int p_column) const {
  603. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  604. return cells[p_column].selectable;
  605. }
  606. bool TreeItem::is_selected(int p_column) {
  607. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  608. return cells[p_column].selectable && cells[p_column].selected;
  609. }
  610. void TreeItem::set_as_cursor(int p_column) {
  611. ERR_FAIL_INDEX(p_column, cells.size());
  612. if (!tree) {
  613. return;
  614. }
  615. if (tree->select_mode != Tree::SELECT_MULTI) {
  616. return;
  617. }
  618. tree->selected_item = this;
  619. tree->selected_col = p_column;
  620. tree->update();
  621. }
  622. void TreeItem::select(int p_column) {
  623. ERR_FAIL_INDEX(p_column, cells.size());
  624. _cell_selected(p_column);
  625. }
  626. void TreeItem::deselect(int p_column) {
  627. ERR_FAIL_INDEX(p_column, cells.size());
  628. _cell_deselected(p_column);
  629. }
  630. void TreeItem::add_button(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip) {
  631. ERR_FAIL_INDEX(p_column, cells.size());
  632. ERR_FAIL_COND(!p_button.is_valid());
  633. TreeItem::Cell::Button button;
  634. button.texture = p_button;
  635. if (p_id < 0) {
  636. p_id = cells[p_column].buttons.size();
  637. }
  638. button.id = p_id;
  639. button.disabled = p_disabled;
  640. button.tooltip = p_tooltip;
  641. cells.write[p_column].buttons.push_back(button);
  642. _changed_notify(p_column);
  643. }
  644. int TreeItem::get_button_count(int p_column) const {
  645. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  646. return cells[p_column].buttons.size();
  647. }
  648. Ref<Texture2D> TreeItem::get_button(int p_column, int p_idx) const {
  649. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  650. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), Ref<Texture2D>());
  651. return cells[p_column].buttons[p_idx].texture;
  652. }
  653. String TreeItem::get_button_tooltip(int p_column, int p_idx) const {
  654. ERR_FAIL_INDEX_V(p_column, cells.size(), String());
  655. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), String());
  656. return cells[p_column].buttons[p_idx].tooltip;
  657. }
  658. void TreeItem::erase_button(int p_column, int p_idx) {
  659. ERR_FAIL_INDEX(p_column, cells.size());
  660. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  661. cells.write[p_column].buttons.remove(p_idx);
  662. _changed_notify(p_column);
  663. }
  664. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  665. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  666. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  667. if (cells[p_column].buttons[i].id == p_id) {
  668. return i;
  669. }
  670. }
  671. return -1;
  672. }
  673. void TreeItem::set_button(int p_column, int p_idx, const Ref<Texture2D> &p_button) {
  674. ERR_FAIL_COND(p_button.is_null());
  675. ERR_FAIL_INDEX(p_column, cells.size());
  676. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  677. cells.write[p_column].buttons.write[p_idx].texture = p_button;
  678. _changed_notify(p_column);
  679. }
  680. void TreeItem::set_button_color(int p_column, int p_idx, const Color &p_color) {
  681. ERR_FAIL_INDEX(p_column, cells.size());
  682. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  683. cells.write[p_column].buttons.write[p_idx].color = p_color;
  684. _changed_notify(p_column);
  685. }
  686. void TreeItem::set_button_disabled(int p_column, int p_idx, bool p_disabled) {
  687. ERR_FAIL_INDEX(p_column, cells.size());
  688. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  689. cells.write[p_column].buttons.write[p_idx].disabled = p_disabled;
  690. _changed_notify(p_column);
  691. }
  692. bool TreeItem::is_button_disabled(int p_column, int p_idx) const {
  693. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  694. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), false);
  695. return cells[p_column].buttons[p_idx].disabled;
  696. }
  697. void TreeItem::set_editable(int p_column, bool p_editable) {
  698. ERR_FAIL_INDEX(p_column, cells.size());
  699. cells.write[p_column].editable = p_editable;
  700. _changed_notify(p_column);
  701. }
  702. bool TreeItem::is_editable(int p_column) {
  703. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  704. return cells[p_column].editable;
  705. }
  706. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  707. ERR_FAIL_INDEX(p_column, cells.size());
  708. cells.write[p_column].custom_color = true;
  709. cells.write[p_column].color = p_color;
  710. _changed_notify(p_column);
  711. }
  712. Color TreeItem::get_custom_color(int p_column) const {
  713. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  714. if (!cells[p_column].custom_color) {
  715. return Color();
  716. }
  717. return cells[p_column].color;
  718. }
  719. void TreeItem::clear_custom_color(int p_column) {
  720. ERR_FAIL_INDEX(p_column, cells.size());
  721. cells.write[p_column].custom_color = false;
  722. cells.write[p_column].color = Color();
  723. _changed_notify(p_column);
  724. }
  725. void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
  726. ERR_FAIL_INDEX(p_column, cells.size());
  727. cells.write[p_column].custom_font = p_font;
  728. }
  729. Ref<Font> TreeItem::get_custom_font(int p_column) const {
  730. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
  731. return cells[p_column].custom_font;
  732. }
  733. void TreeItem::set_tooltip(int p_column, const String &p_tooltip) {
  734. ERR_FAIL_INDEX(p_column, cells.size());
  735. cells.write[p_column].tooltip = p_tooltip;
  736. }
  737. String TreeItem::get_tooltip(int p_column) const {
  738. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  739. return cells[p_column].tooltip;
  740. }
  741. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  742. ERR_FAIL_INDEX(p_column, cells.size());
  743. cells.write[p_column].custom_bg_color = true;
  744. cells.write[p_column].custom_bg_outline = p_bg_outline;
  745. cells.write[p_column].bg_color = p_color;
  746. _changed_notify(p_column);
  747. }
  748. void TreeItem::clear_custom_bg_color(int p_column) {
  749. ERR_FAIL_INDEX(p_column, cells.size());
  750. cells.write[p_column].custom_bg_color = false;
  751. cells.write[p_column].bg_color = Color();
  752. _changed_notify(p_column);
  753. }
  754. Color TreeItem::get_custom_bg_color(int p_column) const {
  755. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  756. if (!cells[p_column].custom_bg_color) {
  757. return Color();
  758. }
  759. return cells[p_column].bg_color;
  760. }
  761. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  762. ERR_FAIL_INDEX(p_column, cells.size());
  763. cells.write[p_column].custom_button = p_button;
  764. }
  765. bool TreeItem::is_custom_set_as_button(int p_column) const {
  766. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  767. return cells[p_column].custom_button;
  768. }
  769. void TreeItem::set_text_align(int p_column, TextAlign p_align) {
  770. ERR_FAIL_INDEX(p_column, cells.size());
  771. cells.write[p_column].text_align = p_align;
  772. _changed_notify(p_column);
  773. }
  774. TreeItem::TextAlign TreeItem::get_text_align(int p_column) const {
  775. ERR_FAIL_INDEX_V(p_column, cells.size(), ALIGN_LEFT);
  776. return cells[p_column].text_align;
  777. }
  778. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  779. ERR_FAIL_INDEX(p_column, cells.size());
  780. cells.write[p_column].expand_right = p_enable;
  781. _changed_notify(p_column);
  782. }
  783. bool TreeItem::get_expand_right(int p_column) const {
  784. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  785. return cells[p_column].expand_right;
  786. }
  787. void TreeItem::set_disable_folding(bool p_disable) {
  788. disable_folding = p_disable;
  789. _changed_notify(0);
  790. }
  791. bool TreeItem::is_folding_disabled() const {
  792. return disable_folding;
  793. }
  794. Variant TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  795. if (p_argcount < 1) {
  796. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  797. r_error.argument = 0;
  798. return Variant();
  799. }
  800. if (p_args[0]->get_type() != Variant::STRING && p_args[0]->get_type() != Variant::STRING_NAME) {
  801. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  802. r_error.argument = 0;
  803. r_error.expected = Variant::STRING_NAME;
  804. return Variant();
  805. }
  806. StringName method = *p_args[0];
  807. call_recursive(method, &p_args[1], p_argcount - 1, r_error);
  808. return Variant();
  809. }
  810. void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  811. if (!p_item) {
  812. return;
  813. }
  814. p_item->call(p_method, p_args, p_argcount, r_error);
  815. TreeItem *c = p_item->get_first_child();
  816. while (c) {
  817. recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
  818. c = c->get_next();
  819. }
  820. }
  821. void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  822. recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
  823. }
  824. void TreeItem::_bind_methods() {
  825. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  826. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  827. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  828. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  829. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  830. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  831. ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
  832. ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
  833. ClassDB::bind_method(D_METHOD("set_opentype_feature", "column", "tag", "value"), &TreeItem::set_opentype_feature);
  834. ClassDB::bind_method(D_METHOD("get_opentype_feature", "column", "tag"), &TreeItem::get_opentype_feature);
  835. ClassDB::bind_method(D_METHOD("clear_opentype_features", "column"), &TreeItem::clear_opentype_features);
  836. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
  837. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
  838. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "column", "args"), &TreeItem::set_structured_text_bidi_override_options);
  839. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options", "column"), &TreeItem::get_structured_text_bidi_override_options);
  840. ClassDB::bind_method(D_METHOD("set_language", "column", "language"), &TreeItem::set_language);
  841. ClassDB::bind_method(D_METHOD("get_language", "column"), &TreeItem::get_language);
  842. ClassDB::bind_method(D_METHOD("set_suffix", "column", "text"), &TreeItem::set_suffix);
  843. ClassDB::bind_method(D_METHOD("get_suffix", "column"), &TreeItem::get_suffix);
  844. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  845. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  846. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  847. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  848. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  849. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  850. ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
  851. ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
  852. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  853. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  854. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  855. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  856. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  857. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  858. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  859. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  860. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  861. ClassDB::bind_method(D_METHOD("uncollapse_tree"), &TreeItem::uncollapse_tree);
  862. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  863. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  864. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  865. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  866. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  867. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  868. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  869. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  870. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  871. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  872. ClassDB::bind_method(D_METHOD("get_custom_color", "column"), &TreeItem::get_custom_color);
  873. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  874. ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
  875. ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
  876. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  877. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  878. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  879. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  880. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  881. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "button_idx", "disabled", "tooltip"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""));
  882. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  883. ClassDB::bind_method(D_METHOD("get_button_tooltip", "column", "button_idx"), &TreeItem::get_button_tooltip);
  884. ClassDB::bind_method(D_METHOD("get_button", "column", "button_idx"), &TreeItem::get_button);
  885. ClassDB::bind_method(D_METHOD("set_button", "column", "button_idx", "button"), &TreeItem::set_button);
  886. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_idx"), &TreeItem::erase_button);
  887. ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_idx", "disabled"), &TreeItem::set_button_disabled);
  888. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_idx"), &TreeItem::is_button_disabled);
  889. ClassDB::bind_method(D_METHOD("set_tooltip", "column", "tooltip"), &TreeItem::set_tooltip);
  890. ClassDB::bind_method(D_METHOD("get_tooltip", "column"), &TreeItem::get_tooltip);
  891. ClassDB::bind_method(D_METHOD("set_text_align", "column", "text_align"), &TreeItem::set_text_align);
  892. ClassDB::bind_method(D_METHOD("get_text_align", "column"), &TreeItem::get_text_align);
  893. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  894. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  895. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  896. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  897. ClassDB::bind_method(D_METHOD("create_child", "idx"), &TreeItem::create_child, DEFVAL(-1));
  898. ClassDB::bind_method(D_METHOD("get_tree"), &TreeItem::get_tree);
  899. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  900. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  901. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  902. ClassDB::bind_method(D_METHOD("get_first_child"), &TreeItem::get_first_child);
  903. ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
  904. ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
  905. ClassDB::bind_method(D_METHOD("get_child", "idx"), &TreeItem::get_child);
  906. ClassDB::bind_method(D_METHOD("get_child_count"), &TreeItem::get_child_count);
  907. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  908. ClassDB::bind_method(D_METHOD("get_index"), &TreeItem::get_index);
  909. ClassDB::bind_method(D_METHOD("move_before", "item"), &TreeItem::_move_before);
  910. ClassDB::bind_method(D_METHOD("move_after", "item"), &TreeItem::_move_after);
  911. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::_remove_child);
  912. {
  913. MethodInfo mi;
  914. mi.name = "call_recursive";
  915. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  916. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
  917. }
  918. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  919. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  920. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  921. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  922. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  923. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  924. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  925. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  926. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  927. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  928. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  929. }
  930. void TreeItem::clear_children() {
  931. TreeItem *c = first_child;
  932. while (c) {
  933. TreeItem *aux = c;
  934. c = c->get_next();
  935. aux->parent = nullptr; // so it won't try to recursively autoremove from me in here
  936. memdelete(aux);
  937. }
  938. first_child = nullptr;
  939. };
  940. TreeItem::TreeItem(Tree *p_tree) {
  941. tree = p_tree;
  942. }
  943. TreeItem::~TreeItem() {
  944. _unlink_from_tree();
  945. prev = nullptr;
  946. clear_children();
  947. _change_tree(nullptr);
  948. }
  949. /**********************************************/
  950. /**********************************************/
  951. /**********************************************/
  952. /**********************************************/
  953. /**********************************************/
  954. /**********************************************/
  955. void Tree::update_cache() {
  956. cache.font = get_theme_font("font");
  957. cache.font_size = get_theme_font_size("font_size");
  958. cache.tb_font = get_theme_font("title_button_font");
  959. cache.tb_font_size = get_theme_font_size("title_button_font_size");
  960. cache.bg = get_theme_stylebox("bg");
  961. cache.selected = get_theme_stylebox("selected");
  962. cache.selected_focus = get_theme_stylebox("selected_focus");
  963. cache.cursor = get_theme_stylebox("cursor");
  964. cache.cursor_unfocus = get_theme_stylebox("cursor_unfocused");
  965. cache.button_pressed = get_theme_stylebox("button_pressed");
  966. cache.checked = get_theme_icon("checked");
  967. cache.unchecked = get_theme_icon("unchecked");
  968. if (is_layout_rtl()) {
  969. cache.arrow_collapsed = get_theme_icon("arrow_collapsed_mirrored");
  970. } else {
  971. cache.arrow_collapsed = get_theme_icon("arrow_collapsed");
  972. }
  973. cache.arrow = get_theme_icon("arrow");
  974. cache.select_arrow = get_theme_icon("select_arrow");
  975. cache.updown = get_theme_icon("updown");
  976. cache.custom_button = get_theme_stylebox("custom_button");
  977. cache.custom_button_hover = get_theme_stylebox("custom_button_hover");
  978. cache.custom_button_pressed = get_theme_stylebox("custom_button_pressed");
  979. cache.custom_button_font_highlight = get_theme_color("custom_button_font_highlight");
  980. cache.font_color = get_theme_color("font_color");
  981. cache.font_selected_color = get_theme_color("font_selected_color");
  982. cache.drop_position_color = get_theme_color("drop_position_color");
  983. cache.hseparation = get_theme_constant("hseparation");
  984. cache.vseparation = get_theme_constant("vseparation");
  985. cache.item_margin = get_theme_constant("item_margin");
  986. cache.button_margin = get_theme_constant("button_margin");
  987. cache.draw_guides = get_theme_constant("draw_guides");
  988. cache.guide_color = get_theme_color("guide_color");
  989. cache.draw_relationship_lines = get_theme_constant("draw_relationship_lines");
  990. cache.relationship_line_width = get_theme_constant("relationship_line_width");
  991. cache.parent_hl_line_width = get_theme_constant("parent_hl_line_width");
  992. cache.children_hl_line_width = get_theme_constant("children_hl_line_width");
  993. cache.parent_hl_line_margin = get_theme_constant("parent_hl_line_margin");
  994. cache.relationship_line_color = get_theme_color("relationship_line_color");
  995. cache.parent_hl_line_color = get_theme_color("parent_hl_line_color");
  996. cache.children_hl_line_color = get_theme_color("children_hl_line_color");
  997. cache.scroll_border = get_theme_constant("scroll_border");
  998. cache.scroll_speed = get_theme_constant("scroll_speed");
  999. cache.title_button = get_theme_stylebox("title_button_normal");
  1000. cache.title_button_pressed = get_theme_stylebox("title_button_pressed");
  1001. cache.title_button_hover = get_theme_stylebox("title_button_hover");
  1002. cache.title_button_color = get_theme_color("title_button_color");
  1003. v_scroll->set_custom_step(cache.font->get_height(cache.font_size));
  1004. }
  1005. int Tree::compute_item_height(TreeItem *p_item) const {
  1006. if (p_item == root && hide_root) {
  1007. return 0;
  1008. }
  1009. ERR_FAIL_COND_V(cache.font.is_null(), 0);
  1010. int height = 0;
  1011. for (int i = 0; i < columns.size(); i++) {
  1012. if (p_item->cells[i].dirty) {
  1013. const_cast<Tree *>(this)->update_item_cell(p_item, i);
  1014. }
  1015. height = MAX(height, p_item->cells[i].text_buf->get_size().y);
  1016. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  1017. Size2i s; // = cache.button_pressed->get_minimum_size();
  1018. s += p_item->cells[i].buttons[j].texture->get_size();
  1019. if (s.height > height) {
  1020. height = s.height;
  1021. }
  1022. }
  1023. switch (p_item->cells[i].mode) {
  1024. case TreeItem::CELL_MODE_CHECK: {
  1025. int check_icon_h = cache.checked->get_height();
  1026. if (height < check_icon_h) {
  1027. height = check_icon_h;
  1028. }
  1029. [[fallthrough]];
  1030. }
  1031. case TreeItem::CELL_MODE_STRING:
  1032. case TreeItem::CELL_MODE_CUSTOM:
  1033. case TreeItem::CELL_MODE_ICON: {
  1034. Ref<Texture2D> icon = p_item->cells[i].icon;
  1035. if (!icon.is_null()) {
  1036. Size2i s = p_item->cells[i].get_icon_size();
  1037. if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) {
  1038. s.height = s.height * p_item->cells[i].icon_max_w / s.width;
  1039. }
  1040. if (s.height > height) {
  1041. height = s.height;
  1042. }
  1043. }
  1044. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  1045. height += cache.custom_button->get_minimum_size().height;
  1046. }
  1047. } break;
  1048. default: {
  1049. }
  1050. }
  1051. }
  1052. int item_min_height = p_item->get_custom_minimum_height();
  1053. if (height < item_min_height) {
  1054. height = item_min_height;
  1055. }
  1056. height += cache.vseparation;
  1057. return height;
  1058. }
  1059. int Tree::get_item_height(TreeItem *p_item) const {
  1060. int height = compute_item_height(p_item);
  1061. height += cache.vseparation;
  1062. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1063. TreeItem *c = p_item->first_child;
  1064. while (c) {
  1065. height += get_item_height(c);
  1066. c = c->next;
  1067. }
  1068. }
  1069. return height;
  1070. }
  1071. void Tree::draw_item_rect(TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color) {
  1072. ERR_FAIL_COND(cache.font.is_null());
  1073. Rect2i rect = p_rect;
  1074. Size2 ts = p_cell.text_buf->get_size();
  1075. bool rtl = is_layout_rtl();
  1076. int w = 0;
  1077. if (!p_cell.icon.is_null()) {
  1078. Size2i bmsize = p_cell.get_icon_size();
  1079. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  1080. bmsize.width = p_cell.icon_max_w;
  1081. }
  1082. w += bmsize.width + cache.hseparation;
  1083. if (rect.size.width > 0 && (w + ts.width) > rect.size.width) {
  1084. ts.width = rect.size.width - w;
  1085. }
  1086. }
  1087. w += ts.width;
  1088. switch (p_cell.text_align) {
  1089. case TreeItem::ALIGN_LEFT:
  1090. if (rtl) {
  1091. rect.position.x += MAX(0, (rect.size.width - w));
  1092. }
  1093. break;
  1094. case TreeItem::ALIGN_CENTER:
  1095. rect.position.x += MAX(0, (rect.size.width - w) / 2);
  1096. break;
  1097. case TreeItem::ALIGN_RIGHT:
  1098. if (!rtl) {
  1099. rect.position.x += MAX(0, (rect.size.width - w));
  1100. }
  1101. break;
  1102. }
  1103. RID ci = get_canvas_item();
  1104. if (rtl) {
  1105. Point2 draw_pos = rect.position;
  1106. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) / 2.0);
  1107. p_cell.text_buf->set_width(MAX(0, rect.size.width));
  1108. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1109. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1110. }
  1111. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1112. rect.position.x += ts.width + cache.hseparation;
  1113. rect.size.x -= ts.width + cache.hseparation;
  1114. }
  1115. if (!p_cell.icon.is_null()) {
  1116. Size2i bmsize = p_cell.get_icon_size();
  1117. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  1118. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  1119. bmsize.width = p_cell.icon_max_w;
  1120. }
  1121. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
  1122. rect.position.x += bmsize.x + cache.hseparation;
  1123. rect.size.x -= bmsize.x + cache.hseparation;
  1124. }
  1125. if (!rtl) {
  1126. Point2 draw_pos = rect.position;
  1127. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) / 2.0);
  1128. p_cell.text_buf->set_width(MAX(0, rect.size.width));
  1129. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1130. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1131. }
  1132. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1133. }
  1134. }
  1135. void Tree::update_column(int p_col) {
  1136. columns.write[p_col].text_buf->clear();
  1137. if (columns[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1138. columns.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1139. } else {
  1140. columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction);
  1141. }
  1142. columns.write[p_col].text_buf->add_string(columns[p_col].title, cache.font, cache.font_size, columns[p_col].opentype_features, (columns[p_col].language != "") ? columns[p_col].language : TranslationServer::get_singleton()->get_tool_locale());
  1143. }
  1144. void Tree::update_item_cell(TreeItem *p_item, int p_col) {
  1145. String valtext;
  1146. p_item->cells.write[p_col].text_buf->clear();
  1147. if (p_item->cells[p_col].mode == TreeItem::CELL_MODE_RANGE) {
  1148. if (p_item->cells[p_col].text != "") {
  1149. if (!p_item->cells[p_col].editable) {
  1150. return;
  1151. }
  1152. int option = (int)p_item->cells[p_col].val;
  1153. valtext = RTR("(Other)");
  1154. Vector<String> strings = p_item->cells[p_col].text.split(",");
  1155. for (int j = 0; j < strings.size(); j++) {
  1156. int value = j;
  1157. if (!strings[j].get_slicec(':', 1).is_empty()) {
  1158. value = strings[j].get_slicec(':', 1).to_int();
  1159. }
  1160. if (option == value) {
  1161. valtext = strings[j].get_slicec(':', 0);
  1162. break;
  1163. }
  1164. }
  1165. } else {
  1166. valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
  1167. }
  1168. } else {
  1169. valtext = p_item->cells[p_col].text;
  1170. }
  1171. if (p_item->cells[p_col].suffix != String()) {
  1172. valtext += " " + p_item->cells[p_col].suffix;
  1173. }
  1174. if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1175. p_item->cells.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1176. } else {
  1177. p_item->cells.write[p_col].text_buf->set_direction((TextServer::Direction)p_item->cells[p_col].text_direction);
  1178. }
  1179. Ref<Font> font;
  1180. if (p_item->cells[p_col].custom_font.is_valid()) {
  1181. font = p_item->cells[p_col].custom_font;
  1182. } else {
  1183. font = cache.font;
  1184. }
  1185. p_item->cells.write[p_col].text_buf->add_string(valtext, font, cache.font_size, p_item->cells[p_col].opentype_features, (p_item->cells[p_col].language != "") ? p_item->cells[p_col].language : TranslationServer::get_singleton()->get_tool_locale());
  1186. TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext));
  1187. p_item->cells.write[p_col].dirty = false;
  1188. }
  1189. void Tree::update_item_cache(TreeItem *p_item) {
  1190. for (int i = 0; i < p_item->cells.size(); i++) {
  1191. update_item_cell(p_item, i);
  1192. }
  1193. TreeItem *c = p_item->first_child;
  1194. while (c) {
  1195. update_item_cache(c);
  1196. c = c->next;
  1197. }
  1198. }
  1199. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item) {
  1200. if (p_pos.y - cache.offset.y > (p_draw_size.height)) {
  1201. return -1; //draw no more!
  1202. }
  1203. RID ci = get_canvas_item();
  1204. int htotal = 0;
  1205. int label_h = compute_item_height(p_item);
  1206. bool rtl = is_layout_rtl();
  1207. /* Calculate height of the label part */
  1208. label_h += cache.vseparation;
  1209. /* Draw label, if height fits */
  1210. bool skip = (p_item == root && hide_root);
  1211. if (!skip && (p_pos.y + label_h - cache.offset.y) > 0) {
  1212. //draw separation.
  1213. //if (p_item->get_parent()!=root || !hide_root)
  1214. ERR_FAIL_COND_V(cache.font.is_null(), -1);
  1215. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1216. int skip2 = 0;
  1217. for (int i = 0; i < columns.size(); i++) {
  1218. if (skip2) {
  1219. skip2--;
  1220. continue;
  1221. }
  1222. int w = get_column_width(i);
  1223. if (i == 0) {
  1224. w -= ofs;
  1225. if (w <= 0) {
  1226. ofs = get_column_width(0);
  1227. continue;
  1228. }
  1229. } else {
  1230. ofs += cache.hseparation;
  1231. w -= cache.hseparation;
  1232. }
  1233. if (p_item->cells[i].expand_right) {
  1234. int plus = 1;
  1235. 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()) {
  1236. w += get_column_width(i + plus);
  1237. plus++;
  1238. skip2++;
  1239. }
  1240. }
  1241. int bw = 0;
  1242. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1243. Ref<Texture2D> b = p_item->cells[i].buttons[j].texture;
  1244. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  1245. if (s.height < label_h) {
  1246. s.height = label_h;
  1247. }
  1248. Point2i o = Point2i(ofs + w - s.width, p_pos.y) - cache.offset + p_draw_ofs;
  1249. 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) {
  1250. //being pressed
  1251. Point2 od = o;
  1252. if (rtl) {
  1253. od.x = get_size().width - od.x - s.x;
  1254. }
  1255. cache.button_pressed->draw(get_canvas_item(), Rect2(od, s));
  1256. }
  1257. o.y += (label_h - s.height) / 2;
  1258. o += cache.button_pressed->get_offset();
  1259. if (rtl) {
  1260. o.x = get_size().width - o.x - b->get_width();
  1261. }
  1262. b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  1263. w -= s.width + cache.button_margin;
  1264. bw += s.width + cache.button_margin;
  1265. }
  1266. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h));
  1267. Rect2i cell_rect = item_rect;
  1268. if (i != 0) {
  1269. cell_rect.position.x -= cache.hseparation;
  1270. cell_rect.size.x += cache.hseparation;
  1271. }
  1272. if (cache.draw_guides) {
  1273. Rect2 r = cell_rect;
  1274. if (rtl) {
  1275. r.position.x = get_size().width - r.position.x - r.size.x;
  1276. }
  1277. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(r.position.x, r.position.y + r.size.height), r.position + r.size, cache.guide_color, 1);
  1278. }
  1279. if (i == 0) {
  1280. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  1281. Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(SIDE_LEFT), item_rect.position.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y));
  1282. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  1283. //r.grow(cache.selected->get_margin(SIDE_LEFT));
  1284. if (rtl) {
  1285. row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
  1286. }
  1287. if (has_focus()) {
  1288. cache.selected_focus->draw(ci, row_rect);
  1289. } else {
  1290. cache.selected->draw(ci, row_rect);
  1291. }
  1292. }
  1293. }
  1294. if ((select_mode == SELECT_ROW && selected_item == p_item) || p_item->cells[i].selected || !p_item->has_meta("__focus_rect")) {
  1295. Rect2i r(cell_rect.position, cell_rect.size);
  1296. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  1297. if (rtl) {
  1298. r.position.x = get_size().width - r.position.x - r.size.x;
  1299. }
  1300. if (p_item->cells[i].selected) {
  1301. if (has_focus()) {
  1302. cache.selected_focus->draw(ci, r);
  1303. } else {
  1304. cache.selected->draw(ci, r);
  1305. }
  1306. }
  1307. }
  1308. if (p_item->cells[i].custom_bg_color) {
  1309. Rect2 r = cell_rect;
  1310. if (i == 0) {
  1311. r.position.x = p_draw_ofs.x;
  1312. r.size.x = w + ofs;
  1313. } else {
  1314. r.position.x -= cache.hseparation;
  1315. r.size.x += cache.hseparation;
  1316. }
  1317. if (rtl) {
  1318. r.position.x = get_size().width - r.position.x - r.size.x;
  1319. }
  1320. if (p_item->cells[i].custom_bg_outline) {
  1321. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
  1322. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color);
  1323. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  1324. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  1325. } else {
  1326. RenderingServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  1327. }
  1328. }
  1329. if (drop_mode_flags && drop_mode_over == p_item) {
  1330. Rect2 r = cell_rect;
  1331. bool has_parent = p_item->get_first_child() != nullptr;
  1332. if (rtl) {
  1333. r.position.x = get_size().width - r.position.x - r.size.x;
  1334. }
  1335. if (drop_mode_section == -1 || has_parent || drop_mode_section == 0) {
  1336. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color);
  1337. }
  1338. if (drop_mode_section == 0) {
  1339. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color);
  1340. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), cache.drop_position_color);
  1341. }
  1342. if ((drop_mode_section == 1 && !has_parent) || drop_mode_section == 0) {
  1343. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), cache.drop_position_color);
  1344. }
  1345. }
  1346. Color col = p_item->cells[i].custom_color ? p_item->cells[i].color : get_theme_color(p_item->cells[i].selected ? "font_selected_color" : "font_color");
  1347. Color font_outline_color = get_theme_color("font_outline_color");
  1348. int outline_size = get_theme_constant("outline_size");
  1349. Color icon_col = p_item->cells[i].icon_color;
  1350. if (p_item->cells[i].dirty) {
  1351. const_cast<Tree *>(this)->update_item_cell(p_item, i);
  1352. }
  1353. if (rtl) {
  1354. item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
  1355. }
  1356. Point2i text_pos = item_rect.position;
  1357. text_pos.y += Math::floor((item_rect.size.y - p_item->cells[i].text_buf->get_size().y) / 2);
  1358. int text_width = p_item->cells[i].text_buf->get_size().x;
  1359. switch (p_item->cells[i].mode) {
  1360. case TreeItem::CELL_MODE_STRING: {
  1361. draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
  1362. } break;
  1363. case TreeItem::CELL_MODE_CHECK: {
  1364. Ref<Texture2D> checked = cache.checked;
  1365. Ref<Texture2D> unchecked = cache.unchecked;
  1366. Point2i check_ofs = item_rect.position;
  1367. check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2);
  1368. if (p_item->cells[i].checked) {
  1369. checked->draw(ci, check_ofs);
  1370. } else {
  1371. unchecked->draw(ci, check_ofs);
  1372. }
  1373. int check_w = checked->get_width() + cache.hseparation;
  1374. text_pos.x += check_w;
  1375. item_rect.size.x -= check_w;
  1376. item_rect.position.x += check_w;
  1377. draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
  1378. } break;
  1379. case TreeItem::CELL_MODE_RANGE: {
  1380. if (p_item->cells[i].text != "") {
  1381. if (!p_item->cells[i].editable) {
  1382. break;
  1383. }
  1384. Ref<Texture2D> downarrow = cache.select_arrow;
  1385. int cell_width = item_rect.size.x - downarrow->get_width();
  1386. p_item->cells.write[i].text_buf->set_width(cell_width);
  1387. if (rtl) {
  1388. if (outline_size > 0 && font_outline_color.a > 0) {
  1389. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  1390. }
  1391. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), col);
  1392. } else {
  1393. if (outline_size > 0 && font_outline_color.a > 0) {
  1394. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  1395. }
  1396. p_item->cells[i].text_buf->draw(ci, text_pos, col);
  1397. }
  1398. Point2i arrow_pos = item_rect.position;
  1399. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  1400. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  1401. downarrow->draw(ci, arrow_pos);
  1402. } else {
  1403. Ref<Texture2D> updown = cache.updown;
  1404. int cell_width = item_rect.size.x - updown->get_width();
  1405. if (rtl) {
  1406. if (outline_size > 0 && font_outline_color.a > 0) {
  1407. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  1408. }
  1409. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), col);
  1410. } else {
  1411. if (outline_size > 0 && font_outline_color.a > 0) {
  1412. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  1413. }
  1414. p_item->cells[i].text_buf->draw(ci, text_pos, col);
  1415. }
  1416. if (!p_item->cells[i].editable) {
  1417. break;
  1418. }
  1419. Point2i updown_pos = item_rect.position;
  1420. updown_pos.x += item_rect.size.x - updown->get_width();
  1421. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  1422. updown->draw(ci, updown_pos);
  1423. }
  1424. } break;
  1425. case TreeItem::CELL_MODE_ICON: {
  1426. if (p_item->cells[i].icon.is_null()) {
  1427. break;
  1428. }
  1429. Size2i icon_size = p_item->cells[i].get_icon_size();
  1430. if (p_item->cells[i].icon_max_w > 0 && icon_size.width > p_item->cells[i].icon_max_w) {
  1431. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  1432. icon_size.width = p_item->cells[i].icon_max_w;
  1433. }
  1434. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  1435. icon_ofs += item_rect.position;
  1436. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  1437. } break;
  1438. case TreeItem::CELL_MODE_CUSTOM: {
  1439. if (p_item->cells[i].custom_draw_obj.is_valid()) {
  1440. Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  1441. if (cdo) {
  1442. cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect));
  1443. }
  1444. }
  1445. if (!p_item->cells[i].editable) {
  1446. draw_item_rect(p_item->cells.write[i], item_rect, col, icon_col, outline_size, font_outline_color);
  1447. break;
  1448. }
  1449. Ref<Texture2D> downarrow = cache.select_arrow;
  1450. Rect2i ir = item_rect;
  1451. Point2i arrow_pos = item_rect.position;
  1452. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  1453. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  1454. ir.size.width -= downarrow->get_width();
  1455. if (p_item->cells[i].custom_button) {
  1456. if (cache.hover_item == p_item && cache.hover_cell == i) {
  1457. if (Input::get_singleton()->is_mouse_button_pressed(MOUSE_BUTTON_LEFT)) {
  1458. draw_style_box(cache.custom_button_pressed, ir);
  1459. } else {
  1460. draw_style_box(cache.custom_button_hover, ir);
  1461. col = cache.custom_button_font_highlight;
  1462. }
  1463. } else {
  1464. draw_style_box(cache.custom_button, ir);
  1465. }
  1466. ir.size -= cache.custom_button->get_minimum_size();
  1467. ir.position += cache.custom_button->get_offset();
  1468. }
  1469. draw_item_rect(p_item->cells.write[i], ir, col, icon_col, outline_size, font_outline_color);
  1470. downarrow->draw(ci, arrow_pos);
  1471. } break;
  1472. }
  1473. if (i == 0) {
  1474. ofs = get_column_width(0);
  1475. } else {
  1476. ofs += w + bw;
  1477. }
  1478. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  1479. if (is_layout_rtl()) {
  1480. cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
  1481. }
  1482. if (has_focus()) {
  1483. cache.cursor->draw(ci, cell_rect);
  1484. } else {
  1485. cache.cursor_unfocus->draw(ci, cell_rect);
  1486. }
  1487. }
  1488. }
  1489. if (!p_item->disable_folding && !hide_folding && p_item->first_child) { //has children, draw the guide box
  1490. Ref<Texture2D> arrow;
  1491. if (p_item->collapsed) {
  1492. arrow = cache.arrow_collapsed;
  1493. } else {
  1494. arrow = cache.arrow;
  1495. }
  1496. Point2 apos = p_pos + p_draw_ofs + Point2i(0, (label_h - arrow->get_height()) / 2) - cache.offset;
  1497. if (rtl) {
  1498. apos.x = get_size().width - apos.x - arrow->get_width();
  1499. }
  1500. arrow->draw(ci, apos);
  1501. }
  1502. }
  1503. Point2 children_pos = p_pos;
  1504. if (!skip) {
  1505. children_pos.x += cache.item_margin;
  1506. htotal += label_h;
  1507. children_pos.y += htotal;
  1508. }
  1509. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1510. TreeItem *c = p_item->first_child;
  1511. int base_ofs = children_pos.y - cache.offset.y + p_draw_ofs.y;
  1512. int prev_ofs = base_ofs;
  1513. int prev_hl_ofs = base_ofs;
  1514. while (c) {
  1515. if (cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root)) {
  1516. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1517. int parent_ofs = p_pos.x + cache.item_margin;
  1518. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs;
  1519. if (c->get_first_child() != nullptr) {
  1520. root_pos -= Point2i(cache.arrow->get_width(), 0);
  1521. }
  1522. float line_width = cache.relationship_line_width;
  1523. float parent_line_width = cache.parent_hl_line_width;
  1524. float children_line_width = cache.children_hl_line_width;
  1525. #ifdef TOOLS_ENABLED
  1526. line_width *= Math::round(EDSCALE);
  1527. parent_line_width *= Math::round(EDSCALE);
  1528. children_line_width *= Math::round(EDSCALE);
  1529. #endif
  1530. 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;
  1531. int more_prev_ofs = 0;
  1532. if (root_pos.y + line_width >= 0) {
  1533. if (rtl) {
  1534. root_pos.x = get_size().width - root_pos.x;
  1535. parent_pos.x = get_size().width - parent_pos.x;
  1536. }
  1537. // Order of parts on this bend: the horizontal line first, then the vertical line.
  1538. if (_is_branch_selected(c)) {
  1539. // If this item or one of its children is selected, we draw the line using parent highlight style.
  1540. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), cache.parent_hl_line_color, parent_line_width);
  1541. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), cache.parent_hl_line_color, parent_line_width);
  1542. more_prev_ofs = cache.parent_hl_line_margin;
  1543. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  1544. } else if (p_item->is_selected(0)) {
  1545. // If parent item is selected (but this item is not), we draw the line using children highlight style.
  1546. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  1547. if (_is_sibling_branch_selected(c)) {
  1548. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), cache.children_hl_line_color, children_line_width);
  1549. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), cache.parent_hl_line_color, parent_line_width);
  1550. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  1551. } else {
  1552. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(children_line_width / 2), root_pos.y), cache.children_hl_line_color, children_line_width);
  1553. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(children_line_width / 2)), Point2i(parent_pos.x, prev_ofs + Math::floor(children_line_width / 2)), cache.children_hl_line_color, children_line_width);
  1554. }
  1555. } else {
  1556. // If nothing of the above is true, we draw the line using normal style.
  1557. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  1558. if (_is_sibling_branch_selected(c)) {
  1559. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + cache.parent_hl_line_margin, root_pos.y), cache.relationship_line_color, line_width);
  1560. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(parent_line_width / 2)), Point2i(parent_pos.x, prev_hl_ofs), cache.parent_hl_line_color, parent_line_width);
  1561. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  1562. } else {
  1563. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(line_width / 2), root_pos.y), cache.relationship_line_color, line_width);
  1564. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y + Math::floor(line_width / 2)), Point2i(parent_pos.x, prev_ofs + Math::floor(line_width / 2)), cache.relationship_line_color, line_width);
  1565. }
  1566. }
  1567. }
  1568. if (htotal < 0) {
  1569. return -1;
  1570. }
  1571. prev_ofs = root_pos.y + more_prev_ofs;
  1572. }
  1573. if (htotal >= 0) {
  1574. int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
  1575. if (child_h < 0) {
  1576. if (cache.draw_relationship_lines == 0) {
  1577. return -1; // break, stop drawing, no need to anymore
  1578. }
  1579. htotal = -1;
  1580. children_pos.y = cache.offset.y + p_draw_size.height;
  1581. } else {
  1582. htotal += child_h;
  1583. children_pos.y += child_h;
  1584. }
  1585. }
  1586. c = c->next;
  1587. }
  1588. }
  1589. return htotal;
  1590. }
  1591. int Tree::_count_selected_items(TreeItem *p_from) const {
  1592. int count = 0;
  1593. for (int i = 0; i < columns.size(); i++) {
  1594. if (p_from->is_selected(i)) {
  1595. count++;
  1596. }
  1597. }
  1598. if (p_from->get_first_child()) {
  1599. count += _count_selected_items(p_from->get_first_child());
  1600. }
  1601. if (p_from->get_next()) {
  1602. count += _count_selected_items(p_from->get_next());
  1603. }
  1604. return count;
  1605. }
  1606. bool Tree::_is_branch_selected(TreeItem *p_from) const {
  1607. for (int i = 0; i < columns.size(); i++) {
  1608. if (p_from->is_selected(i)) {
  1609. return true;
  1610. }
  1611. }
  1612. TreeItem *child_item = p_from->get_first_child();
  1613. while (child_item) {
  1614. if (_is_branch_selected(child_item)) {
  1615. return true;
  1616. }
  1617. child_item = child_item->get_next();
  1618. }
  1619. return false;
  1620. }
  1621. bool Tree::_is_sibling_branch_selected(TreeItem *p_from) const {
  1622. TreeItem *sibling_item = p_from->get_next();
  1623. while (sibling_item) {
  1624. if (_is_branch_selected(sibling_item)) {
  1625. return true;
  1626. }
  1627. sibling_item = sibling_item->get_next();
  1628. }
  1629. return false;
  1630. }
  1631. 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) {
  1632. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  1633. bool switched = false;
  1634. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1635. *r_in_range = true;
  1636. switched = true;
  1637. }
  1638. bool emitted_row = false;
  1639. for (int i = 0; i < columns.size(); i++) {
  1640. TreeItem::Cell &c = p_current->cells.write[i];
  1641. if (!c.selectable) {
  1642. continue;
  1643. }
  1644. if (select_mode == SELECT_ROW) {
  1645. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  1646. c.selected = true;
  1647. selected_item = p_selected;
  1648. selected_col = 0;
  1649. if (!emitted_row) {
  1650. emit_signal("item_selected");
  1651. emitted_row = true;
  1652. }
  1653. /*
  1654. if (p_col==i)
  1655. p_current->selected_signal.call(p_col);
  1656. */
  1657. } else if (c.selected) {
  1658. c.selected = false;
  1659. //p_current->deselected_signal.call(p_col);
  1660. }
  1661. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  1662. if (!r_in_range && &selected_cell == &c) {
  1663. if (!selected_cell.selected || allow_reselect) {
  1664. selected_cell.selected = true;
  1665. selected_item = p_selected;
  1666. selected_col = i;
  1667. emit_signal("cell_selected");
  1668. if (select_mode == SELECT_MULTI) {
  1669. emit_signal("multi_selected", p_current, i, true);
  1670. } else if (select_mode == SELECT_SINGLE) {
  1671. emit_signal("item_selected");
  1672. }
  1673. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  1674. selected_item = p_selected;
  1675. selected_col = i;
  1676. emit_signal("cell_selected");
  1677. }
  1678. } else {
  1679. if (r_in_range && *r_in_range && !p_force_deselect) {
  1680. if (!c.selected && c.selectable) {
  1681. c.selected = true;
  1682. emit_signal("multi_selected", p_current, i, true);
  1683. }
  1684. } else if (!r_in_range || p_force_deselect) {
  1685. if (select_mode == SELECT_MULTI && c.selected) {
  1686. emit_signal("multi_selected", p_current, i, false);
  1687. }
  1688. c.selected = false;
  1689. }
  1690. //p_current->deselected_signal.call(p_col);
  1691. }
  1692. }
  1693. }
  1694. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1695. *r_in_range = false;
  1696. }
  1697. TreeItem *c = p_current->first_child;
  1698. while (c) {
  1699. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  1700. c = c->next;
  1701. }
  1702. }
  1703. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  1704. return Rect2();
  1705. }
  1706. void Tree::_range_click_timeout() {
  1707. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(MOUSE_BUTTON_LEFT)) {
  1708. Point2 pos = get_local_mouse_position() - cache.bg->get_offset();
  1709. if (show_column_titles) {
  1710. pos.y -= _get_title_button_height();
  1711. if (pos.y < 0) {
  1712. range_click_timer->stop();
  1713. return;
  1714. }
  1715. }
  1716. click_handled = false;
  1717. Ref<InputEventMouseButton> mb;
  1718. mb.instantiate();
  1719. propagate_mouse_activated = false; // done from outside, so signal handler can't clear the tree in the middle of emit (which is a common case)
  1720. blocked++;
  1721. propagate_mouse_event(pos + cache.offset, 0, 0, false, root, MOUSE_BUTTON_LEFT, mb);
  1722. blocked--;
  1723. if (range_click_timer->is_one_shot()) {
  1724. range_click_timer->set_wait_time(0.05);
  1725. range_click_timer->set_one_shot(false);
  1726. range_click_timer->start();
  1727. }
  1728. if (!click_handled) {
  1729. range_click_timer->stop();
  1730. }
  1731. if (propagate_mouse_activated) {
  1732. emit_signal("item_activated");
  1733. propagate_mouse_activated = false;
  1734. }
  1735. } else {
  1736. range_click_timer->stop();
  1737. }
  1738. }
  1739. int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, bool p_double_click, TreeItem *p_item, int p_button, const Ref<InputEventWithModifiers> &p_mod) {
  1740. int item_h = compute_item_height(p_item) + cache.vseparation;
  1741. bool skip = (p_item == root && hide_root);
  1742. if (!skip && p_pos.y < item_h) {
  1743. // check event!
  1744. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1745. return -1;
  1746. }
  1747. if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
  1748. if (p_item->first_child) {
  1749. p_item->set_collapsed(!p_item->is_collapsed());
  1750. }
  1751. return -1; //handled!
  1752. }
  1753. int x = p_pos.x;
  1754. /* find clicked column */
  1755. int col = -1;
  1756. int col_ofs = 0;
  1757. int col_width = 0;
  1758. for (int i = 0; i < columns.size(); i++) {
  1759. col_width = get_column_width(i);
  1760. if (p_item->cells[i].expand_right) {
  1761. int plus = 1;
  1762. 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()) {
  1763. col_width += cache.hseparation;
  1764. col_width += get_column_width(i + plus);
  1765. plus++;
  1766. }
  1767. }
  1768. if (x > col_width) {
  1769. col_ofs += col_width;
  1770. x -= col_width;
  1771. continue;
  1772. }
  1773. col = i;
  1774. break;
  1775. }
  1776. if (col == -1) {
  1777. return -1;
  1778. } else if (col == 0) {
  1779. int margin = x_ofs + cache.item_margin; //-cache.hseparation;
  1780. //int lm = cache.bg->get_margin(SIDE_LEFT);
  1781. col_width -= margin;
  1782. col_ofs += margin;
  1783. x -= margin;
  1784. } else {
  1785. col_width -= cache.hseparation;
  1786. x -= cache.hseparation;
  1787. }
  1788. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
  1789. p_item->set_collapsed(!p_item->is_collapsed());
  1790. return -1; //collapse/uncollapse because nothing can be done with item
  1791. }
  1792. const TreeItem::Cell &c = p_item->cells[col];
  1793. bool already_selected = c.selected;
  1794. bool already_cursor = (p_item == selected_item) && col == selected_col;
  1795. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  1796. Ref<Texture2D> b = c.buttons[j].texture;
  1797. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1798. if (x > col_width - w) {
  1799. if (c.buttons[j].disabled) {
  1800. pressed_button = -1;
  1801. cache.click_type = Cache::CLICK_NONE;
  1802. return -1;
  1803. }
  1804. pressed_button = j;
  1805. cache.click_type = Cache::CLICK_BUTTON;
  1806. cache.click_index = j;
  1807. cache.click_id = c.buttons[j].id;
  1808. cache.click_item = p_item;
  1809. cache.click_column = col;
  1810. cache.click_pos = get_global_mouse_position() - get_global_position();
  1811. update();
  1812. //emit_signal("button_pressed");
  1813. return -1;
  1814. }
  1815. col_width -= w + cache.button_margin;
  1816. }
  1817. if (p_button == MOUSE_BUTTON_LEFT || (p_button == MOUSE_BUTTON_RIGHT && allow_rmb_select)) {
  1818. /* process selection */
  1819. if (p_double_click && (!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
  1820. propagate_mouse_activated = true;
  1821. incr_search.clear();
  1822. return -1;
  1823. }
  1824. if (select_mode == SELECT_MULTI && p_mod->is_command_pressed() && c.selectable) {
  1825. if (!c.selected || p_button == MOUSE_BUTTON_RIGHT) {
  1826. p_item->select(col);
  1827. emit_signal("multi_selected", p_item, col, true);
  1828. if (p_button == MOUSE_BUTTON_RIGHT) {
  1829. emit_signal("item_rmb_selected", get_local_mouse_position());
  1830. }
  1831. //p_item->selected_signal.call(col);
  1832. } else {
  1833. p_item->deselect(col);
  1834. emit_signal("multi_selected", p_item, col, false);
  1835. //p_item->deselected_signal.call(col);
  1836. }
  1837. } else {
  1838. if (c.selectable) {
  1839. if (select_mode == SELECT_MULTI && p_mod->is_shift_pressed() && selected_item && selected_item != p_item) {
  1840. bool inrange = false;
  1841. select_single_item(p_item, root, col, selected_item, &inrange);
  1842. if (p_button == MOUSE_BUTTON_RIGHT) {
  1843. emit_signal("item_rmb_selected", get_local_mouse_position());
  1844. }
  1845. } else {
  1846. int icount = _count_selected_items(root);
  1847. if (select_mode == SELECT_MULTI && icount > 1 && p_button != MOUSE_BUTTON_RIGHT) {
  1848. single_select_defer = p_item;
  1849. single_select_defer_column = col;
  1850. } else {
  1851. if (p_button != MOUSE_BUTTON_RIGHT || !c.selected) {
  1852. select_single_item(p_item, root, col);
  1853. }
  1854. if (p_button == MOUSE_BUTTON_RIGHT) {
  1855. emit_signal("item_rmb_selected", get_local_mouse_position());
  1856. }
  1857. }
  1858. }
  1859. /*
  1860. if (!c.selected && select_mode==SELECT_MULTI) {
  1861. emit_signal("multi_selected",p_item,col,true);
  1862. }
  1863. */
  1864. update();
  1865. }
  1866. }
  1867. }
  1868. if (!c.editable) {
  1869. return -1; // if cell is not editable, don't bother
  1870. }
  1871. /* editing */
  1872. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  1873. String editor_text = c.text;
  1874. switch (c.mode) {
  1875. case TreeItem::CELL_MODE_STRING: {
  1876. //nothing in particular
  1877. if (select_mode == SELECT_MULTI && (get_viewport()->get_processed_events_count() == focus_in_id || !already_cursor)) {
  1878. bring_up_editor = false;
  1879. }
  1880. } break;
  1881. case TreeItem::CELL_MODE_CHECK: {
  1882. bring_up_editor = false; //checkboxes are not edited with editor
  1883. if (force_edit_checkbox_only_on_checkbox) {
  1884. if (x < cache.checked->get_width()) {
  1885. p_item->set_checked(col, !c.checked);
  1886. item_edited(col, p_item);
  1887. }
  1888. } else {
  1889. p_item->set_checked(col, !c.checked);
  1890. item_edited(col, p_item);
  1891. }
  1892. click_handled = true;
  1893. //p_item->edited_signal.call(col);
  1894. } break;
  1895. case TreeItem::CELL_MODE_RANGE: {
  1896. if (c.text != "") {
  1897. //if (x >= (get_column_width(col)-item_h/2)) {
  1898. popup_menu->clear();
  1899. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  1900. String s = c.text.get_slicec(',', i);
  1901. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
  1902. }
  1903. popup_menu->set_size(Size2(col_width, 0));
  1904. popup_menu->set_position(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset);
  1905. popup_menu->popup();
  1906. popup_edited_item = p_item;
  1907. popup_edited_item_col = col;
  1908. //}
  1909. bring_up_editor = false;
  1910. } else {
  1911. if (x >= (col_width - item_h / 2)) {
  1912. /* touching the combo */
  1913. bool up = p_pos.y < (item_h / 2);
  1914. if (p_button == MOUSE_BUTTON_LEFT) {
  1915. if (range_click_timer->get_time_left() == 0) {
  1916. range_item_last = p_item;
  1917. range_up_last = up;
  1918. range_click_timer->set_wait_time(0.6);
  1919. range_click_timer->set_one_shot(true);
  1920. range_click_timer->start();
  1921. } else if (up != range_up_last) {
  1922. return -1; // break. avoid changing direction on mouse held
  1923. }
  1924. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  1925. item_edited(col, p_item);
  1926. } else if (p_button == MOUSE_BUTTON_RIGHT) {
  1927. p_item->set_range(col, (up ? c.max : c.min));
  1928. item_edited(col, p_item);
  1929. } else if (p_button == MOUSE_BUTTON_WHEEL_UP) {
  1930. p_item->set_range(col, c.val + c.step);
  1931. item_edited(col, p_item);
  1932. } else if (p_button == MOUSE_BUTTON_WHEEL_DOWN) {
  1933. p_item->set_range(col, c.val - c.step);
  1934. item_edited(col, p_item);
  1935. }
  1936. //p_item->edited_signal.call(col);
  1937. bring_up_editor = false;
  1938. } else {
  1939. editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
  1940. if (select_mode == SELECT_MULTI && get_viewport()->get_processed_events_count() == focus_in_id) {
  1941. bring_up_editor = false;
  1942. }
  1943. }
  1944. }
  1945. click_handled = true;
  1946. } break;
  1947. case TreeItem::CELL_MODE_ICON: {
  1948. bring_up_editor = false;
  1949. } break;
  1950. case TreeItem::CELL_MODE_CUSTOM: {
  1951. edited_item = p_item;
  1952. edited_col = col;
  1953. bool on_arrow = x > col_width - cache.select_arrow->get_width();
  1954. 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));
  1955. if (on_arrow || !p_item->cells[col].custom_button) {
  1956. emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2))));
  1957. }
  1958. if (!p_item->cells[col].custom_button || !on_arrow) {
  1959. item_edited(col, p_item, p_button == MOUSE_BUTTON_LEFT);
  1960. }
  1961. click_handled = true;
  1962. return -1;
  1963. } break;
  1964. };
  1965. if (!bring_up_editor || p_button != MOUSE_BUTTON_LEFT) {
  1966. return -1;
  1967. }
  1968. click_handled = true;
  1969. popup_edited_item = p_item;
  1970. popup_edited_item_col = col;
  1971. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h));
  1972. pressing_for_editor_text = editor_text;
  1973. pressing_for_editor = true;
  1974. return -1; //select
  1975. } else {
  1976. Point2i new_pos = p_pos;
  1977. if (!skip) {
  1978. x_ofs += cache.item_margin;
  1979. //new_pos.x-=cache.item_margin;
  1980. y_ofs += item_h;
  1981. new_pos.y -= item_h;
  1982. }
  1983. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1984. TreeItem *c = p_item->first_child;
  1985. while (c) {
  1986. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_double_click, c, p_button, p_mod);
  1987. if (child_h < 0) {
  1988. return -1; // break, stop propagating, no need to anymore
  1989. }
  1990. new_pos.y -= child_h;
  1991. y_ofs += child_h;
  1992. c = c->next;
  1993. item_h += child_h;
  1994. }
  1995. }
  1996. if (p_item == root && p_button == MOUSE_BUTTON_RIGHT) {
  1997. emit_signal("empty_rmb", get_local_mouse_position());
  1998. }
  1999. }
  2000. return item_h; // nothing found
  2001. }
  2002. void Tree::_text_editor_modal_close() {
  2003. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  2004. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  2005. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  2006. return;
  2007. }
  2008. if (value_editor->has_point(value_editor->get_local_mouse_position())) {
  2009. return;
  2010. }
  2011. _text_editor_submit(text_editor->get_text());
  2012. }
  2013. void Tree::_text_editor_submit(String p_text) {
  2014. popup_editor->hide();
  2015. if (!popup_edited_item) {
  2016. return;
  2017. }
  2018. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2019. return;
  2020. }
  2021. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2022. switch (c.mode) {
  2023. case TreeItem::CELL_MODE_STRING: {
  2024. c.text = p_text;
  2025. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2026. } break;
  2027. case TreeItem::CELL_MODE_RANGE: {
  2028. c.val = p_text.to_float();
  2029. if (c.step > 0) {
  2030. c.val = Math::snapped(c.val, c.step);
  2031. }
  2032. if (c.val < c.min) {
  2033. c.val = c.min;
  2034. } else if (c.val > c.max) {
  2035. c.val = c.max;
  2036. }
  2037. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2038. } break;
  2039. default: {
  2040. ERR_FAIL();
  2041. }
  2042. }
  2043. item_edited(popup_edited_item_col, popup_edited_item);
  2044. update();
  2045. }
  2046. void Tree::value_editor_changed(double p_value) {
  2047. if (updating_value_editor) {
  2048. return;
  2049. }
  2050. if (!popup_edited_item) {
  2051. return;
  2052. }
  2053. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2054. c.val = p_value;
  2055. item_edited(popup_edited_item_col, popup_edited_item);
  2056. update();
  2057. }
  2058. void Tree::popup_select(int p_option) {
  2059. if (!popup_edited_item) {
  2060. return;
  2061. }
  2062. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2063. return;
  2064. }
  2065. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  2066. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2067. update();
  2068. item_edited(popup_edited_item_col, popup_edited_item);
  2069. }
  2070. void Tree::_go_left() {
  2071. if (selected_col == 0) {
  2072. if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  2073. selected_item->set_collapsed(true);
  2074. } else {
  2075. if (columns.size() == 1) { // goto parent with one column
  2076. TreeItem *parent = selected_item->get_parent();
  2077. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  2078. select_single_item(parent, get_root(), selected_col);
  2079. }
  2080. } else if (selected_item->get_prev_visible()) {
  2081. selected_col = columns.size() - 1;
  2082. _go_up(); // go to upper column if possible
  2083. }
  2084. }
  2085. } else {
  2086. if (select_mode == SELECT_MULTI) {
  2087. selected_col--;
  2088. emit_signal("cell_selected");
  2089. } else {
  2090. selected_item->select(selected_col - 1);
  2091. }
  2092. }
  2093. update();
  2094. accept_event();
  2095. ensure_cursor_is_visible();
  2096. }
  2097. void Tree::_go_right() {
  2098. if (selected_col == (columns.size() - 1)) {
  2099. if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  2100. selected_item->set_collapsed(false);
  2101. } else if (selected_item->get_next_visible()) {
  2102. selected_col = 0;
  2103. _go_down();
  2104. }
  2105. } else {
  2106. if (select_mode == SELECT_MULTI) {
  2107. selected_col++;
  2108. emit_signal("cell_selected");
  2109. } else {
  2110. selected_item->select(selected_col + 1);
  2111. }
  2112. }
  2113. update();
  2114. ensure_cursor_is_visible();
  2115. accept_event();
  2116. }
  2117. void Tree::_go_up() {
  2118. TreeItem *prev = nullptr;
  2119. if (!selected_item) {
  2120. prev = get_last_item();
  2121. selected_col = 0;
  2122. } else {
  2123. prev = selected_item->get_prev_visible();
  2124. if (last_keypress != 0) {
  2125. //incr search next
  2126. int col;
  2127. prev = _search_item_text(prev, incr_search, &col, true, true);
  2128. if (!prev) {
  2129. accept_event();
  2130. return;
  2131. }
  2132. }
  2133. }
  2134. if (select_mode == SELECT_MULTI) {
  2135. if (!prev) {
  2136. return;
  2137. }
  2138. selected_item = prev;
  2139. emit_signal("cell_selected");
  2140. update();
  2141. } else {
  2142. int col = selected_col < 0 ? 0 : selected_col;
  2143. while (prev && !prev->cells[col].selectable) {
  2144. prev = prev->get_prev_visible();
  2145. }
  2146. if (!prev) {
  2147. return; // do nothing..
  2148. }
  2149. prev->select(col);
  2150. }
  2151. ensure_cursor_is_visible();
  2152. accept_event();
  2153. }
  2154. void Tree::_go_down() {
  2155. TreeItem *next = nullptr;
  2156. if (!selected_item) {
  2157. if (root) {
  2158. next = hide_root ? root->get_next_visible() : root;
  2159. }
  2160. } else {
  2161. next = selected_item->get_next_visible();
  2162. if (last_keypress != 0) {
  2163. //incr search next
  2164. int col;
  2165. next = _search_item_text(next, incr_search, &col, true);
  2166. if (!next) {
  2167. accept_event();
  2168. return;
  2169. }
  2170. }
  2171. }
  2172. if (select_mode == SELECT_MULTI) {
  2173. if (!next) {
  2174. return;
  2175. }
  2176. selected_item = next;
  2177. emit_signal("cell_selected");
  2178. update();
  2179. } else {
  2180. int col = selected_col < 0 ? 0 : selected_col;
  2181. while (next && !next->cells[col].selectable) {
  2182. next = next->get_next_visible();
  2183. }
  2184. if (!next) {
  2185. return; // do nothing..
  2186. }
  2187. next->select(col);
  2188. }
  2189. ensure_cursor_is_visible();
  2190. accept_event();
  2191. }
  2192. void Tree::_gui_input(Ref<InputEvent> p_event) {
  2193. ERR_FAIL_COND(p_event.is_null());
  2194. Ref<InputEventKey> k = p_event;
  2195. bool is_command = k.is_valid() && k->is_command_pressed();
  2196. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  2197. if (!cursor_can_exit_tree) {
  2198. accept_event();
  2199. }
  2200. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  2201. return;
  2202. }
  2203. if (k.is_valid() && k->is_alt_pressed()) {
  2204. selected_item->set_collapsed(false);
  2205. TreeItem *next = selected_item->get_first_child();
  2206. while (next && next != selected_item->next) {
  2207. next->set_collapsed(false);
  2208. next = next->get_next_visible();
  2209. }
  2210. } else {
  2211. _go_right();
  2212. }
  2213. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  2214. if (!cursor_can_exit_tree) {
  2215. accept_event();
  2216. }
  2217. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  2218. return;
  2219. }
  2220. if (k.is_valid() && k->is_alt_pressed()) {
  2221. selected_item->set_collapsed(true);
  2222. TreeItem *next = selected_item->get_first_child();
  2223. while (next && next != selected_item->next) {
  2224. next->set_collapsed(true);
  2225. next = next->get_next_visible();
  2226. }
  2227. } else {
  2228. _go_left();
  2229. }
  2230. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  2231. if (!cursor_can_exit_tree) {
  2232. accept_event();
  2233. }
  2234. _go_up();
  2235. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  2236. if (!cursor_can_exit_tree) {
  2237. accept_event();
  2238. }
  2239. _go_down();
  2240. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  2241. if (!cursor_can_exit_tree) {
  2242. accept_event();
  2243. }
  2244. TreeItem *next = nullptr;
  2245. if (!selected_item) {
  2246. return;
  2247. }
  2248. next = selected_item;
  2249. for (int i = 0; i < 10; i++) {
  2250. TreeItem *_n = next->get_next_visible();
  2251. if (_n) {
  2252. next = _n;
  2253. } else {
  2254. break;
  2255. }
  2256. }
  2257. if (next == selected_item) {
  2258. return;
  2259. }
  2260. if (select_mode == SELECT_MULTI) {
  2261. selected_item = next;
  2262. emit_signal("cell_selected");
  2263. update();
  2264. } else {
  2265. while (next && !next->cells[selected_col].selectable) {
  2266. next = next->get_next_visible();
  2267. }
  2268. if (!next) {
  2269. return; // do nothing..
  2270. }
  2271. next->select(selected_col);
  2272. }
  2273. ensure_cursor_is_visible();
  2274. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  2275. if (!cursor_can_exit_tree) {
  2276. accept_event();
  2277. }
  2278. TreeItem *prev = nullptr;
  2279. if (!selected_item) {
  2280. return;
  2281. }
  2282. prev = selected_item;
  2283. for (int i = 0; i < 10; i++) {
  2284. TreeItem *_n = prev->get_prev_visible();
  2285. if (_n) {
  2286. prev = _n;
  2287. } else {
  2288. break;
  2289. }
  2290. }
  2291. if (prev == selected_item) {
  2292. return;
  2293. }
  2294. if (select_mode == SELECT_MULTI) {
  2295. selected_item = prev;
  2296. emit_signal("cell_selected");
  2297. update();
  2298. } else {
  2299. while (prev && !prev->cells[selected_col].selectable) {
  2300. prev = prev->get_prev_visible();
  2301. }
  2302. if (!prev) {
  2303. return; // do nothing..
  2304. }
  2305. prev->select(selected_col);
  2306. }
  2307. ensure_cursor_is_visible();
  2308. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  2309. if (selected_item) {
  2310. //bring up editor if possible
  2311. if (!edit_selected()) {
  2312. emit_signal("item_activated");
  2313. incr_search.clear();
  2314. }
  2315. }
  2316. accept_event();
  2317. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  2318. if (select_mode == SELECT_MULTI) {
  2319. if (!selected_item) {
  2320. return;
  2321. }
  2322. if (selected_item->is_selected(selected_col)) {
  2323. selected_item->deselect(selected_col);
  2324. emit_signal("multi_selected", selected_item, selected_col, false);
  2325. } else if (selected_item->is_selectable(selected_col)) {
  2326. selected_item->select(selected_col);
  2327. emit_signal("multi_selected", selected_item, selected_col, true);
  2328. }
  2329. }
  2330. accept_event();
  2331. }
  2332. if (k.is_valid()) { // Incremental search
  2333. if (!k->is_pressed()) {
  2334. return;
  2335. }
  2336. if (k->is_command_pressed() || (k->is_shift_pressed() && k->get_unicode() == 0) || k->is_meta_pressed()) {
  2337. return;
  2338. }
  2339. if (!root) {
  2340. return;
  2341. }
  2342. if (hide_root && !root->get_next_visible()) {
  2343. return;
  2344. }
  2345. if (k->get_unicode() > 0) {
  2346. _do_incr_search(String::chr(k->get_unicode()));
  2347. accept_event();
  2348. return;
  2349. } else {
  2350. if (k->get_keycode() != KEY_SHIFT) {
  2351. last_keypress = 0;
  2352. }
  2353. }
  2354. }
  2355. Ref<InputEventMouseMotion> mm = p_event;
  2356. if (mm.is_valid()) {
  2357. if (cache.font.is_null()) { // avoid a strange case that may corrupt stuff
  2358. update_cache();
  2359. }
  2360. Ref<StyleBox> bg = cache.bg;
  2361. bool rtl = is_layout_rtl();
  2362. Point2 pos = mm->get_position();
  2363. if (rtl) {
  2364. pos.x = get_size().width - pos.x;
  2365. }
  2366. pos -= cache.bg->get_offset();
  2367. Cache::ClickType old_hover = cache.hover_type;
  2368. int old_index = cache.hover_index;
  2369. cache.hover_type = Cache::CLICK_NONE;
  2370. cache.hover_index = 0;
  2371. if (show_column_titles) {
  2372. pos.y -= _get_title_button_height();
  2373. if (pos.y < 0) {
  2374. pos.x += cache.offset.x;
  2375. int len = 0;
  2376. for (int i = 0; i < columns.size(); i++) {
  2377. len += get_column_width(i);
  2378. if (pos.x < len) {
  2379. cache.hover_type = Cache::CLICK_TITLE;
  2380. cache.hover_index = i;
  2381. break;
  2382. }
  2383. }
  2384. }
  2385. }
  2386. if (root) {
  2387. Point2 mpos = mm->get_position();
  2388. if (rtl) {
  2389. mpos.x = get_size().width - mpos.x;
  2390. }
  2391. mpos -= cache.bg->get_offset();
  2392. mpos.y -= _get_title_button_height();
  2393. if (mpos.y >= 0) {
  2394. if (h_scroll->is_visible_in_tree()) {
  2395. mpos.x += h_scroll->get_value();
  2396. }
  2397. if (v_scroll->is_visible_in_tree()) {
  2398. mpos.y += v_scroll->get_value();
  2399. }
  2400. TreeItem *old_it = cache.hover_item;
  2401. int old_col = cache.hover_cell;
  2402. int col, h, section;
  2403. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  2404. if (drop_mode_flags) {
  2405. if (it != drop_mode_over) {
  2406. drop_mode_over = it;
  2407. update();
  2408. }
  2409. if (it && section != drop_mode_section) {
  2410. drop_mode_section = section;
  2411. update();
  2412. }
  2413. }
  2414. cache.hover_item = it;
  2415. cache.hover_cell = col;
  2416. if (it != old_it || col != old_col) {
  2417. if (old_it && old_col >= old_it->cells.size()) {
  2418. // Columns may have changed since last update().
  2419. update();
  2420. } else {
  2421. // Only need to update if mouse enters/exits a button
  2422. bool was_over_button = old_it && old_it->cells[old_col].custom_button;
  2423. bool is_over_button = it && it->cells[col].custom_button;
  2424. if (was_over_button || is_over_button) {
  2425. update();
  2426. }
  2427. }
  2428. }
  2429. }
  2430. }
  2431. // Update if mouse enters/exits columns
  2432. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  2433. update();
  2434. }
  2435. if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE)) {
  2436. //range drag
  2437. if (!range_drag_enabled) {
  2438. Vector2 cpos = mm->get_position();
  2439. if (rtl) {
  2440. cpos.x = get_size().width - cpos.x;
  2441. }
  2442. if (cpos.distance_to(pressing_pos) > 2) {
  2443. range_drag_enabled = true;
  2444. range_drag_capture_pos = cpos;
  2445. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  2446. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  2447. }
  2448. } else {
  2449. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  2450. float diff_y = -mm->get_relative().y;
  2451. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  2452. diff_y *= 0.1;
  2453. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  2454. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  2455. item_edited(popup_edited_item_col, popup_edited_item);
  2456. }
  2457. }
  2458. if (drag_touching && !drag_touching_deaccel) {
  2459. drag_accum -= mm->get_relative().y;
  2460. v_scroll->set_value(drag_from + drag_accum);
  2461. drag_speed = -mm->get_speed().y;
  2462. }
  2463. }
  2464. Ref<InputEventMouseButton> b = p_event;
  2465. if (b.is_valid()) {
  2466. if (cache.font.is_null()) { // avoid a strange case that may corrupt stuff
  2467. update_cache();
  2468. }
  2469. bool rtl = is_layout_rtl();
  2470. if (!b->is_pressed()) {
  2471. if (b->get_button_index() == MOUSE_BUTTON_LEFT) {
  2472. Point2 pos = b->get_position();
  2473. if (rtl) {
  2474. pos.x = get_size().width - pos.x;
  2475. }
  2476. pos -= cache.bg->get_offset();
  2477. if (show_column_titles) {
  2478. pos.y -= _get_title_button_height();
  2479. if (pos.y < 0) {
  2480. pos.x += cache.offset.x;
  2481. int len = 0;
  2482. for (int i = 0; i < columns.size(); i++) {
  2483. len += get_column_width(i);
  2484. if (pos.x < len) {
  2485. emit_signal("column_title_pressed", i);
  2486. break;
  2487. }
  2488. }
  2489. }
  2490. }
  2491. if (single_select_defer) {
  2492. select_single_item(single_select_defer, root, single_select_defer_column);
  2493. single_select_defer = nullptr;
  2494. }
  2495. range_click_timer->stop();
  2496. if (pressing_for_editor) {
  2497. if (range_drag_enabled) {
  2498. range_drag_enabled = false;
  2499. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2500. warp_mouse(range_drag_capture_pos);
  2501. } else {
  2502. Rect2 rect = get_selected()->get_meta("__focus_rect");
  2503. Point2 mpos = b->get_position();
  2504. if (rtl) {
  2505. mpos.x = get_size().width - mpos.x;
  2506. }
  2507. if (rect.has_point(mpos)) {
  2508. if (!edit_selected()) {
  2509. emit_signal("item_double_clicked");
  2510. }
  2511. } else {
  2512. emit_signal("item_double_clicked");
  2513. }
  2514. }
  2515. pressing_for_editor = false;
  2516. }
  2517. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != nullptr) {
  2518. // make sure in case of wrong reference after reconstructing whole TreeItems
  2519. cache.click_item = get_item_at_position(cache.click_pos);
  2520. emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id);
  2521. }
  2522. cache.click_type = Cache::CLICK_NONE;
  2523. cache.click_index = -1;
  2524. cache.click_id = -1;
  2525. cache.click_item = nullptr;
  2526. cache.click_column = 0;
  2527. if (drag_touching) {
  2528. if (drag_speed == 0) {
  2529. drag_touching_deaccel = false;
  2530. drag_touching = false;
  2531. set_physics_process_internal(false);
  2532. } else {
  2533. drag_touching_deaccel = true;
  2534. }
  2535. }
  2536. update();
  2537. }
  2538. return;
  2539. }
  2540. if (range_drag_enabled) {
  2541. return;
  2542. }
  2543. switch (b->get_button_index()) {
  2544. case MOUSE_BUTTON_RIGHT:
  2545. case MOUSE_BUTTON_LEFT: {
  2546. Ref<StyleBox> bg = cache.bg;
  2547. Point2 pos = b->get_position();
  2548. if (rtl) {
  2549. pos.x = get_size().width - pos.x;
  2550. }
  2551. pos -= bg->get_offset();
  2552. cache.click_type = Cache::CLICK_NONE;
  2553. if (show_column_titles) {
  2554. pos.y -= _get_title_button_height();
  2555. if (pos.y < 0) {
  2556. if (b->get_button_index() == MOUSE_BUTTON_LEFT) {
  2557. pos.x += cache.offset.x;
  2558. int len = 0;
  2559. for (int i = 0; i < columns.size(); i++) {
  2560. len += get_column_width(i);
  2561. if (pos.x < len) {
  2562. cache.click_type = Cache::CLICK_TITLE;
  2563. cache.click_index = i;
  2564. //cache.click_id=;
  2565. update();
  2566. break;
  2567. }
  2568. }
  2569. }
  2570. break;
  2571. }
  2572. }
  2573. if (!root || (!root->get_first_child() && hide_root)) {
  2574. if (b->get_button_index() == MOUSE_BUTTON_RIGHT && allow_rmb_select) {
  2575. emit_signal("empty_tree_rmb_selected", get_local_mouse_position());
  2576. }
  2577. break;
  2578. }
  2579. click_handled = false;
  2580. pressing_for_editor = false;
  2581. propagate_mouse_activated = false;
  2582. blocked++;
  2583. propagate_mouse_event(pos + cache.offset, 0, 0, b->is_double_click(), root, b->get_button_index(), b);
  2584. blocked--;
  2585. if (pressing_for_editor) {
  2586. pressing_pos = b->get_position();
  2587. if (rtl) {
  2588. pressing_pos.x = get_size().width - pressing_pos.x;
  2589. }
  2590. }
  2591. if (b->get_button_index() == MOUSE_BUTTON_RIGHT) {
  2592. break;
  2593. }
  2594. if (drag_touching) {
  2595. set_physics_process_internal(false);
  2596. drag_touching_deaccel = false;
  2597. drag_touching = false;
  2598. drag_speed = 0;
  2599. drag_from = 0;
  2600. }
  2601. if (!click_handled) {
  2602. drag_speed = 0;
  2603. drag_accum = 0;
  2604. //last_drag_accum=0;
  2605. drag_from = v_scroll->get_value();
  2606. drag_touching = DisplayServer::get_singleton()->screen_is_touchscreen(DisplayServer::get_singleton()->window_get_current_screen(get_viewport()->get_window_id()));
  2607. drag_touching_deaccel = false;
  2608. if (drag_touching) {
  2609. set_physics_process_internal(true);
  2610. }
  2611. if (b->get_button_index() == MOUSE_BUTTON_LEFT) {
  2612. if (get_item_at_position(b->get_position()) == nullptr && !b->is_shift_pressed() && !b->is_ctrl_pressed() && !b->is_command_pressed()) {
  2613. emit_signal("nothing_selected");
  2614. }
  2615. }
  2616. }
  2617. if (propagate_mouse_activated) {
  2618. emit_signal("item_activated");
  2619. propagate_mouse_activated = false;
  2620. }
  2621. } break;
  2622. case MOUSE_BUTTON_WHEEL_UP: {
  2623. double prev_value = v_scroll->get_value();
  2624. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  2625. if (v_scroll->get_value() != prev_value) {
  2626. accept_event();
  2627. }
  2628. } break;
  2629. case MOUSE_BUTTON_WHEEL_DOWN: {
  2630. double prev_value = v_scroll->get_value();
  2631. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  2632. if (v_scroll->get_value() != prev_value) {
  2633. accept_event();
  2634. }
  2635. } break;
  2636. default:
  2637. break;
  2638. }
  2639. }
  2640. Ref<InputEventPanGesture> pan_gesture = p_event;
  2641. if (pan_gesture.is_valid()) {
  2642. double prev_v = v_scroll->get_value();
  2643. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  2644. double prev_h = h_scroll->get_value();
  2645. if (is_layout_rtl()) {
  2646. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * -pan_gesture->get_delta().x / 8);
  2647. } else {
  2648. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  2649. }
  2650. if (v_scroll->get_value() != prev_v || h_scroll->get_value() != prev_h) {
  2651. accept_event();
  2652. }
  2653. }
  2654. }
  2655. bool Tree::edit_selected() {
  2656. TreeItem *s = get_selected();
  2657. ERR_FAIL_COND_V_MSG(!s, false, "No item selected.");
  2658. ensure_cursor_is_visible();
  2659. int col = get_selected_column();
  2660. ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
  2661. if (!s->cells[col].editable) {
  2662. return false;
  2663. }
  2664. Rect2 rect = s->get_meta("__focus_rect");
  2665. popup_edited_item = s;
  2666. popup_edited_item_col = col;
  2667. const TreeItem::Cell &c = s->cells[col];
  2668. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  2669. s->set_checked(col, !c.checked);
  2670. item_edited(col, s);
  2671. return true;
  2672. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  2673. edited_item = s;
  2674. edited_col = col;
  2675. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  2676. emit_signal("custom_popup_edited", false);
  2677. item_edited(col, s);
  2678. return true;
  2679. } else if (c.mode == TreeItem::CELL_MODE_RANGE && c.text != "") {
  2680. popup_menu->clear();
  2681. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2682. String s2 = c.text.get_slicec(',', i);
  2683. popup_menu->add_item(s2.get_slicec(':', 0), s2.get_slicec(':', 1).is_empty() ? i : s2.get_slicec(':', 1).to_int());
  2684. }
  2685. popup_menu->set_size(Size2(rect.size.width, 0));
  2686. popup_menu->set_position(get_global_position() + rect.position + Point2i(0, rect.size.height));
  2687. popup_menu->popup();
  2688. popup_edited_item = s;
  2689. popup_edited_item_col = col;
  2690. return true;
  2691. } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE) {
  2692. Rect2 popup_rect;
  2693. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2694. Point2i textedpos = get_screen_position() + rect.position - ofs;
  2695. cache.text_editor_position = textedpos;
  2696. popup_rect.position = textedpos;
  2697. popup_rect.size = rect.size;
  2698. text_editor->clear();
  2699. text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
  2700. text_editor->select_all();
  2701. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  2702. popup_rect.size.y += value_editor->get_minimum_size().height;
  2703. value_editor->show();
  2704. updating_value_editor = true;
  2705. value_editor->set_min(c.min);
  2706. value_editor->set_max(c.max);
  2707. value_editor->set_step(c.step);
  2708. value_editor->set_value(c.val);
  2709. value_editor->set_exp_ratio(c.expr);
  2710. updating_value_editor = false;
  2711. } else {
  2712. value_editor->hide();
  2713. }
  2714. popup_editor->set_position(popup_rect.position);
  2715. popup_editor->set_size(popup_rect.size);
  2716. popup_editor->popup();
  2717. popup_editor->child_controls_changed();
  2718. text_editor->grab_focus();
  2719. return true;
  2720. }
  2721. return false;
  2722. }
  2723. bool Tree::is_editing() {
  2724. return popup_editor->is_visible();
  2725. }
  2726. Size2 Tree::get_internal_min_size() const {
  2727. Size2i size = cache.bg->get_offset();
  2728. if (root) {
  2729. size.height += get_item_height(root);
  2730. }
  2731. for (int i = 0; i < columns.size(); i++) {
  2732. size.width += columns[i].min_width;
  2733. }
  2734. return size;
  2735. }
  2736. void Tree::update_scrollbars() {
  2737. Size2 size = get_size();
  2738. int tbh;
  2739. if (show_column_titles) {
  2740. tbh = _get_title_button_height();
  2741. } else {
  2742. tbh = 0;
  2743. }
  2744. Size2 hmin = h_scroll->get_combined_minimum_size();
  2745. Size2 vmin = v_scroll->get_combined_minimum_size();
  2746. v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(SIDE_TOP)));
  2747. v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(SIDE_TOP) - cache.bg->get_margin(SIDE_BOTTOM)));
  2748. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  2749. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  2750. Size2 min = get_internal_min_size();
  2751. if (min.height < size.height - hmin.height) {
  2752. v_scroll->hide();
  2753. cache.offset.y = 0;
  2754. } else {
  2755. v_scroll->show();
  2756. v_scroll->set_max(min.height);
  2757. v_scroll->set_page(size.height - hmin.height - tbh);
  2758. cache.offset.y = v_scroll->get_value();
  2759. }
  2760. if (min.width < size.width - vmin.width) {
  2761. h_scroll->hide();
  2762. cache.offset.x = 0;
  2763. } else {
  2764. h_scroll->show();
  2765. h_scroll->set_max(min.width);
  2766. h_scroll->set_page(size.width - vmin.width);
  2767. cache.offset.x = h_scroll->get_value();
  2768. }
  2769. }
  2770. int Tree::_get_title_button_height() const {
  2771. ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
  2772. int h = 0;
  2773. if (show_column_titles) {
  2774. for (int i = 0; i < columns.size(); i++) {
  2775. h = MAX(h, columns[i].text_buf->get_size().y + cache.title_button->get_minimum_size().height);
  2776. }
  2777. }
  2778. return h;
  2779. }
  2780. void Tree::_notification(int p_what) {
  2781. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2782. focus_in_id = get_viewport()->get_processed_events_count();
  2783. }
  2784. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2785. if (cache.hover_type != Cache::CLICK_NONE) {
  2786. cache.hover_type = Cache::CLICK_NONE;
  2787. update();
  2788. }
  2789. }
  2790. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2791. drag_touching = false;
  2792. }
  2793. if (p_what == NOTIFICATION_ENTER_TREE) {
  2794. update_cache();
  2795. }
  2796. if (p_what == NOTIFICATION_DRAG_END) {
  2797. drop_mode_flags = 0;
  2798. scrolling = false;
  2799. set_physics_process_internal(false);
  2800. update();
  2801. }
  2802. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2803. single_select_defer = nullptr;
  2804. if (cache.scroll_speed > 0) {
  2805. scrolling = true;
  2806. set_physics_process_internal(true);
  2807. }
  2808. }
  2809. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  2810. if (drag_touching) {
  2811. if (drag_touching_deaccel) {
  2812. float pos = v_scroll->get_value();
  2813. pos += drag_speed * get_physics_process_delta_time();
  2814. bool turnoff = false;
  2815. if (pos < 0) {
  2816. pos = 0;
  2817. turnoff = true;
  2818. set_physics_process_internal(false);
  2819. drag_touching = false;
  2820. drag_touching_deaccel = false;
  2821. }
  2822. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2823. pos = v_scroll->get_max() - v_scroll->get_page();
  2824. turnoff = true;
  2825. }
  2826. v_scroll->set_value(pos);
  2827. float sgn = drag_speed < 0 ? -1 : 1;
  2828. float val = Math::abs(drag_speed);
  2829. val -= 1000 * get_physics_process_delta_time();
  2830. if (val < 0) {
  2831. turnoff = true;
  2832. }
  2833. drag_speed = sgn * val;
  2834. if (turnoff) {
  2835. set_physics_process_internal(false);
  2836. drag_touching = false;
  2837. drag_touching_deaccel = false;
  2838. }
  2839. }
  2840. }
  2841. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  2842. if (scrolling && get_rect().grow(cache.scroll_border).has_point(mouse_position)) {
  2843. Point2 point;
  2844. if ((ABS(mouse_position.x) < ABS(mouse_position.x - get_size().width)) && (ABS(mouse_position.x) < cache.scroll_border)) {
  2845. point.x = mouse_position.x - cache.scroll_border;
  2846. } else if (ABS(mouse_position.x - get_size().width) < cache.scroll_border) {
  2847. point.x = mouse_position.x - (get_size().width - cache.scroll_border);
  2848. }
  2849. if ((ABS(mouse_position.y) < ABS(mouse_position.y - get_size().height)) && (ABS(mouse_position.y) < cache.scroll_border)) {
  2850. point.y = mouse_position.y - cache.scroll_border;
  2851. } else if (ABS(mouse_position.y - get_size().height) < cache.scroll_border) {
  2852. point.y = mouse_position.y - (get_size().height - cache.scroll_border);
  2853. }
  2854. point *= cache.scroll_speed * get_physics_process_delta_time();
  2855. point += get_scroll();
  2856. h_scroll->set_value(point.x);
  2857. v_scroll->set_value(point.y);
  2858. }
  2859. }
  2860. if (p_what == NOTIFICATION_DRAW) {
  2861. update_cache();
  2862. update_scrollbars();
  2863. RID ci = get_canvas_item();
  2864. Ref<StyleBox> bg = cache.bg;
  2865. Color font_outline_color = get_theme_color("font_outline_color");
  2866. int outline_size = get_theme_constant("outline_size");
  2867. Point2 draw_ofs;
  2868. draw_ofs += bg->get_offset();
  2869. Size2 draw_size = get_size() - bg->get_minimum_size();
  2870. bg->draw(ci, Rect2(Point2(), get_size()));
  2871. int tbh = _get_title_button_height();
  2872. draw_ofs.y += tbh;
  2873. draw_size.y -= tbh;
  2874. if (root) {
  2875. draw_item(Point2(), draw_ofs, draw_size, root);
  2876. }
  2877. if (show_column_titles) {
  2878. //title buttons
  2879. int ofs2 = cache.bg->get_margin(SIDE_LEFT);
  2880. for (int i = 0; i < columns.size(); i++) {
  2881. 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);
  2882. Ref<Font> f = cache.tb_font;
  2883. Rect2 tbrect = Rect2(ofs2 - cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  2884. if (is_layout_rtl()) {
  2885. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  2886. }
  2887. sb->draw(ci, tbrect);
  2888. ofs2 += tbrect.size.width;
  2889. //text
  2890. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2891. columns.write[i].text_buf->set_width(clip_w);
  2892. Vector2 text_pos = tbrect.position + Point2i(sb->get_offset().x + (tbrect.size.width - columns[i].text_buf->get_size().x) / 2, (tbrect.size.height - columns[i].text_buf->get_size().y) / 2);
  2893. if (outline_size > 0 && font_outline_color.a > 0) {
  2894. columns[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  2895. }
  2896. columns[i].text_buf->draw(ci, text_pos, cache.title_button_color);
  2897. }
  2898. }
  2899. // Draw the background focus outline last, so that it is drawn in front of the section headings.
  2900. // Otherwise, section heading backgrounds can appear to be in front of the focus outline when scrolling.
  2901. if (has_focus()) {
  2902. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2903. const Ref<StyleBox> bg_focus = get_theme_stylebox("bg_focus");
  2904. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2905. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2906. }
  2907. }
  2908. if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_LAYOUT_DIRECTION_CHANGED || p_what == NOTIFICATION_TRANSLATION_CHANGED) {
  2909. update_cache();
  2910. _update_all();
  2911. }
  2912. if (p_what == NOTIFICATION_RESIZED || p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  2913. if (popup_edited_item != nullptr) {
  2914. Rect2 rect = popup_edited_item->get_meta("__focus_rect");
  2915. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2916. Point2i textedpos = get_global_position() + rect.position - ofs;
  2917. if (cache.text_editor_position != textedpos) {
  2918. cache.text_editor_position = textedpos;
  2919. text_editor->set_position(textedpos);
  2920. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2921. }
  2922. }
  2923. }
  2924. }
  2925. void Tree::_update_all() {
  2926. for (int i = 0; i < columns.size(); i++) {
  2927. update_column(i);
  2928. }
  2929. if (root) {
  2930. update_item_cache(root);
  2931. }
  2932. }
  2933. Size2 Tree::get_minimum_size() const {
  2934. return Size2(1, 1);
  2935. }
  2936. TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
  2937. ERR_FAIL_COND_V(blocked > 0, nullptr);
  2938. TreeItem *ti = nullptr;
  2939. if (p_parent) {
  2940. ERR_FAIL_COND_V_MSG(p_parent->tree != this, nullptr, "A different tree owns the given parent");
  2941. ti = p_parent->create_child(p_idx);
  2942. } else {
  2943. if (!root) {
  2944. // No root exists, make the given item the new root.
  2945. ti = memnew(TreeItem(this));
  2946. ERR_FAIL_COND_V(!ti, nullptr);
  2947. ti->cells.resize(columns.size());
  2948. ti->is_root = true;
  2949. root = ti;
  2950. } else {
  2951. // Root exists, append or insert to root.
  2952. ti = create_item(root, p_idx);
  2953. }
  2954. }
  2955. return ti;
  2956. }
  2957. TreeItem *Tree::get_root() {
  2958. return root;
  2959. }
  2960. TreeItem *Tree::get_last_item() {
  2961. TreeItem *last = root;
  2962. while (last) {
  2963. if (last->next) {
  2964. last = last->next;
  2965. } else if (last->first_child) {
  2966. last = last->first_child;
  2967. } else {
  2968. break;
  2969. }
  2970. }
  2971. return last;
  2972. }
  2973. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2974. edited_item = p_item;
  2975. edited_col = p_column;
  2976. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  2977. edited_item->cells.write[p_column].dirty = true;
  2978. }
  2979. if (p_lmb) {
  2980. emit_signal("item_edited");
  2981. } else {
  2982. emit_signal("item_rmb_edited");
  2983. }
  2984. }
  2985. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2986. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  2987. p_item->cells.write[p_column].dirty = true;
  2988. }
  2989. update();
  2990. }
  2991. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2992. if (select_mode == SELECT_MULTI) {
  2993. if (!p_item->cells[p_column].selectable) {
  2994. return;
  2995. }
  2996. p_item->cells.write[p_column].selected = true;
  2997. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2998. selected_col = p_column;
  2999. if (!selected_item) {
  3000. selected_item = p_item;
  3001. }
  3002. } else {
  3003. select_single_item(p_item, root, p_column);
  3004. }
  3005. update();
  3006. }
  3007. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  3008. if (selected_item == p_item) {
  3009. selected_item = nullptr;
  3010. if (selected_col == p_column) {
  3011. selected_col = -1;
  3012. }
  3013. }
  3014. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  3015. p_item->cells.write[p_column].selected = false;
  3016. }
  3017. update();
  3018. }
  3019. void Tree::set_select_mode(SelectMode p_mode) {
  3020. select_mode = p_mode;
  3021. }
  3022. Tree::SelectMode Tree::get_select_mode() const {
  3023. return select_mode;
  3024. }
  3025. void Tree::deselect_all() {
  3026. TreeItem *item = get_next_selected(get_root());
  3027. while (item) {
  3028. item->deselect(selected_col);
  3029. TreeItem *prev_item = item;
  3030. item = get_next_selected(get_root());
  3031. ERR_FAIL_COND(item == prev_item);
  3032. }
  3033. selected_item = nullptr;
  3034. selected_col = -1;
  3035. update();
  3036. }
  3037. bool Tree::is_anything_selected() {
  3038. return (selected_item != nullptr);
  3039. }
  3040. void Tree::clear() {
  3041. ERR_FAIL_COND(blocked > 0);
  3042. if (pressing_for_editor) {
  3043. if (range_drag_enabled) {
  3044. range_drag_enabled = false;
  3045. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  3046. warp_mouse(range_drag_capture_pos);
  3047. }
  3048. pressing_for_editor = false;
  3049. }
  3050. if (root) {
  3051. memdelete(root);
  3052. root = nullptr;
  3053. };
  3054. selected_item = nullptr;
  3055. edited_item = nullptr;
  3056. popup_edited_item = nullptr;
  3057. update();
  3058. };
  3059. void Tree::set_hide_root(bool p_enabled) {
  3060. hide_root = p_enabled;
  3061. update();
  3062. }
  3063. bool Tree::is_root_hidden() const {
  3064. return hide_root;
  3065. }
  3066. void Tree::set_column_min_width(int p_column, int p_min_width) {
  3067. ERR_FAIL_INDEX(p_column, columns.size());
  3068. if (p_min_width < 1) {
  3069. return;
  3070. }
  3071. columns.write[p_column].min_width = p_min_width;
  3072. update();
  3073. }
  3074. void Tree::set_column_expand(int p_column, bool p_expand) {
  3075. ERR_FAIL_INDEX(p_column, columns.size());
  3076. columns.write[p_column].expand = p_expand;
  3077. update();
  3078. }
  3079. TreeItem *Tree::get_selected() const {
  3080. return selected_item;
  3081. }
  3082. int Tree::get_selected_column() const {
  3083. return selected_col;
  3084. }
  3085. TreeItem *Tree::get_edited() const {
  3086. return edited_item;
  3087. }
  3088. int Tree::get_edited_column() const {
  3089. return edited_col;
  3090. }
  3091. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  3092. /*
  3093. if (!p_item)
  3094. return nullptr;
  3095. */
  3096. if (!root) {
  3097. return nullptr;
  3098. }
  3099. while (true) {
  3100. if (!p_item) {
  3101. p_item = root;
  3102. } else {
  3103. if (p_item->first_child) {
  3104. p_item = p_item->first_child;
  3105. } else if (p_item->next) {
  3106. p_item = p_item->next;
  3107. } else {
  3108. while (!p_item->next) {
  3109. p_item = p_item->parent;
  3110. if (p_item == nullptr) {
  3111. return nullptr;
  3112. }
  3113. }
  3114. p_item = p_item->next;
  3115. }
  3116. }
  3117. for (int i = 0; i < columns.size(); i++) {
  3118. if (p_item->cells[i].selected) {
  3119. return p_item;
  3120. }
  3121. }
  3122. }
  3123. return nullptr;
  3124. }
  3125. int Tree::get_column_width(int p_column) const {
  3126. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  3127. if (!columns[p_column].expand) {
  3128. return columns[p_column].min_width;
  3129. }
  3130. int expand_area = get_size().width;
  3131. Ref<StyleBox> bg = cache.bg;
  3132. if (bg.is_valid()) {
  3133. expand_area -= bg->get_margin(SIDE_LEFT) + bg->get_margin(SIDE_RIGHT);
  3134. }
  3135. if (v_scroll->is_visible_in_tree()) {
  3136. expand_area -= v_scroll->get_combined_minimum_size().width;
  3137. }
  3138. int expanding_columns = 0;
  3139. int expanding_total = 0;
  3140. for (int i = 0; i < columns.size(); i++) {
  3141. if (!columns[i].expand) {
  3142. expand_area -= columns[i].min_width;
  3143. } else {
  3144. expanding_total += columns[i].min_width;
  3145. expanding_columns++;
  3146. }
  3147. }
  3148. if (expand_area < expanding_total) {
  3149. return columns[p_column].min_width;
  3150. }
  3151. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  3152. return expand_area * columns[p_column].min_width / expanding_total;
  3153. }
  3154. void Tree::propagate_set_columns(TreeItem *p_item) {
  3155. p_item->cells.resize(columns.size());
  3156. TreeItem *c = p_item->get_first_child();
  3157. while (c) {
  3158. propagate_set_columns(c);
  3159. c = c->next;
  3160. }
  3161. }
  3162. void Tree::set_columns(int p_columns) {
  3163. ERR_FAIL_COND(p_columns < 1);
  3164. ERR_FAIL_COND(blocked > 0);
  3165. columns.resize(p_columns);
  3166. if (root) {
  3167. propagate_set_columns(root);
  3168. }
  3169. if (selected_col >= p_columns) {
  3170. selected_col = p_columns - 1;
  3171. }
  3172. update();
  3173. }
  3174. int Tree::get_columns() const {
  3175. return columns.size();
  3176. }
  3177. void Tree::_scroll_moved(float) {
  3178. update();
  3179. }
  3180. Rect2 Tree::get_custom_popup_rect() const {
  3181. return custom_popup_rect;
  3182. }
  3183. int Tree::get_item_offset(TreeItem *p_item) const {
  3184. TreeItem *it = root;
  3185. int ofs = _get_title_button_height();
  3186. if (!it) {
  3187. return 0;
  3188. }
  3189. while (true) {
  3190. if (it == p_item) {
  3191. return ofs;
  3192. }
  3193. ofs += compute_item_height(it);
  3194. if (it != root || !hide_root) {
  3195. ofs += cache.vseparation;
  3196. }
  3197. if (it->first_child && !it->collapsed) {
  3198. it = it->first_child;
  3199. } else if (it->next) {
  3200. it = it->next;
  3201. } else {
  3202. while (!it->next) {
  3203. it = it->parent;
  3204. if (it == nullptr) {
  3205. return 0;
  3206. }
  3207. }
  3208. it = it->next;
  3209. }
  3210. }
  3211. return -1; //not found
  3212. }
  3213. void Tree::ensure_cursor_is_visible() {
  3214. if (!is_inside_tree()) {
  3215. return;
  3216. }
  3217. if (!selected_item || (selected_col == -1)) {
  3218. return; // Nothing under cursor.
  3219. }
  3220. const Size2 area_size = get_size() - cache.bg->get_minimum_size();
  3221. int y_offset = get_item_offset(selected_item);
  3222. if (y_offset != -1) {
  3223. const int tbh = _get_title_button_height();
  3224. y_offset -= tbh;
  3225. const int cell_h = compute_item_height(selected_item) + cache.vseparation;
  3226. const int screen_h = area_size.height - h_scroll->get_combined_minimum_size().height - tbh;
  3227. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  3228. v_scroll->set_value(y_offset);
  3229. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  3230. v_scroll->call_deferred("set_value", y_offset - screen_h + cell_h);
  3231. } else if (y_offset < v_scroll->get_value()) {
  3232. v_scroll->set_value(y_offset);
  3233. }
  3234. }
  3235. if (select_mode != SELECT_ROW) { // Cursor always at col 0 in this mode.
  3236. int x_offset = 0;
  3237. for (int i = 0; i < selected_col; i++) {
  3238. x_offset += get_column_width(i);
  3239. }
  3240. const int cell_w = get_column_width(selected_col);
  3241. const int screen_w = area_size.width - v_scroll->get_combined_minimum_size().width;
  3242. if (cell_w > screen_w) {
  3243. h_scroll->set_value(x_offset);
  3244. } else if (x_offset + cell_w > h_scroll->get_value() + screen_w) {
  3245. h_scroll->call_deferred("set_value", x_offset - screen_w + cell_w);
  3246. } else if (x_offset < h_scroll->get_value()) {
  3247. h_scroll->set_value(x_offset);
  3248. }
  3249. }
  3250. }
  3251. int Tree::get_pressed_button() const {
  3252. return pressed_button;
  3253. }
  3254. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  3255. ERR_FAIL_NULL_V(p_item, Rect2());
  3256. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  3257. if (p_column != -1) {
  3258. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  3259. }
  3260. int ofs = get_item_offset(p_item);
  3261. int height = compute_item_height(p_item);
  3262. Rect2 r;
  3263. r.position.y = ofs;
  3264. r.size.height = height;
  3265. if (p_column == -1) {
  3266. r.position.x = 0;
  3267. r.size.x = get_size().width;
  3268. } else {
  3269. int accum = 0;
  3270. for (int i = 0; i < p_column; i++) {
  3271. accum += get_column_width(i);
  3272. }
  3273. r.position.x = accum;
  3274. r.size.x = get_column_width(p_column);
  3275. }
  3276. return r;
  3277. }
  3278. void Tree::set_column_titles_visible(bool p_show) {
  3279. show_column_titles = p_show;
  3280. update();
  3281. }
  3282. bool Tree::are_column_titles_visible() const {
  3283. return show_column_titles;
  3284. }
  3285. void Tree::set_column_title(int p_column, const String &p_title) {
  3286. ERR_FAIL_INDEX(p_column, columns.size());
  3287. if (cache.font.is_null()) { // avoid a strange case that may corrupt stuff
  3288. update_cache();
  3289. }
  3290. columns.write[p_column].title = p_title;
  3291. update_column(p_column);
  3292. update();
  3293. }
  3294. String Tree::get_column_title(int p_column) const {
  3295. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  3296. return columns[p_column].title;
  3297. }
  3298. void Tree::set_column_title_direction(int p_column, Control::TextDirection p_text_direction) {
  3299. ERR_FAIL_INDEX(p_column, columns.size());
  3300. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  3301. if (columns[p_column].text_direction != p_text_direction) {
  3302. columns.write[p_column].text_direction = p_text_direction;
  3303. update_column(p_column);
  3304. update();
  3305. }
  3306. }
  3307. Control::TextDirection Tree::get_column_title_direction(int p_column) const {
  3308. ERR_FAIL_INDEX_V(p_column, columns.size(), TEXT_DIRECTION_INHERITED);
  3309. return columns[p_column].text_direction;
  3310. }
  3311. void Tree::clear_column_title_opentype_features(int p_column) {
  3312. ERR_FAIL_INDEX(p_column, columns.size());
  3313. columns.write[p_column].opentype_features.clear();
  3314. update_column(p_column);
  3315. update();
  3316. }
  3317. void Tree::set_column_title_opentype_feature(int p_column, const String &p_name, int p_value) {
  3318. ERR_FAIL_INDEX(p_column, columns.size());
  3319. int32_t tag = TS->name_to_tag(p_name);
  3320. if (!columns[p_column].opentype_features.has(tag) || (int)columns[p_column].opentype_features[tag] != p_value) {
  3321. columns.write[p_column].opentype_features[tag] = p_value;
  3322. update_column(p_column);
  3323. update();
  3324. }
  3325. }
  3326. int Tree::get_column_title_opentype_feature(int p_column, const String &p_name) const {
  3327. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  3328. int32_t tag = TS->name_to_tag(p_name);
  3329. if (!columns[p_column].opentype_features.has(tag)) {
  3330. return -1;
  3331. }
  3332. return columns[p_column].opentype_features[tag];
  3333. }
  3334. void Tree::set_column_title_language(int p_column, const String &p_language) {
  3335. ERR_FAIL_INDEX(p_column, columns.size());
  3336. if (columns[p_column].language != p_language) {
  3337. columns.write[p_column].language = p_language;
  3338. update_column(p_column);
  3339. update();
  3340. }
  3341. }
  3342. String Tree::get_column_title_language(int p_column) const {
  3343. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  3344. return columns[p_column].language;
  3345. }
  3346. Point2 Tree::get_scroll() const {
  3347. Point2 ofs;
  3348. if (h_scroll->is_visible_in_tree()) {
  3349. ofs.x = h_scroll->get_value();
  3350. }
  3351. if (v_scroll->is_visible_in_tree()) {
  3352. ofs.y = v_scroll->get_value();
  3353. }
  3354. return ofs;
  3355. }
  3356. void Tree::scroll_to_item(TreeItem *p_item) {
  3357. if (!is_visible_in_tree()) {
  3358. // hack to work around crash in get_item_rect() if Tree is not in tree.
  3359. return;
  3360. }
  3361. // make sure the scrollbar min and max are up to date with latest changes.
  3362. update_scrollbars();
  3363. const Rect2 r = get_item_rect(p_item);
  3364. if (r.position.y <= v_scroll->get_value()) {
  3365. v_scroll->set_value(r.position.y);
  3366. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  3367. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  3368. }
  3369. }
  3370. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  3371. TreeItem *from = p_at;
  3372. TreeItem *loop = nullptr; // Safe-guard against infinite loop.
  3373. while (p_at) {
  3374. for (int i = 0; i < columns.size(); i++) {
  3375. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  3376. if (r_col) {
  3377. *r_col = i;
  3378. }
  3379. return p_at;
  3380. }
  3381. }
  3382. if (p_backwards) {
  3383. p_at = p_at->get_prev_visible(true);
  3384. } else {
  3385. p_at = p_at->get_next_visible(true);
  3386. }
  3387. if ((p_at) == from) {
  3388. break;
  3389. }
  3390. if (!loop) {
  3391. loop = p_at;
  3392. } else if (loop == p_at) {
  3393. break;
  3394. }
  3395. }
  3396. return nullptr;
  3397. }
  3398. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  3399. TreeItem *from = get_selected();
  3400. if (!from) {
  3401. from = root;
  3402. }
  3403. if (!from) {
  3404. return nullptr;
  3405. }
  3406. return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
  3407. }
  3408. TreeItem *Tree::get_item_with_text(const String &p_find) const {
  3409. for (TreeItem *current = root; current; current = current->get_next_visible()) {
  3410. for (int i = 0; i < columns.size(); i++) {
  3411. if (current->get_text(i) == p_find) {
  3412. return current;
  3413. }
  3414. }
  3415. }
  3416. return nullptr;
  3417. }
  3418. void Tree::_do_incr_search(const String &p_add) {
  3419. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  3420. uint64_t diff = time - last_keypress;
  3421. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000))) {
  3422. incr_search = p_add;
  3423. } else if (incr_search != p_add) {
  3424. incr_search += p_add;
  3425. }
  3426. last_keypress = time;
  3427. int col;
  3428. TreeItem *item = search_item_text(incr_search, &col, true);
  3429. if (!item) {
  3430. return;
  3431. }
  3432. item->select(col);
  3433. ensure_cursor_is_visible();
  3434. }
  3435. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  3436. Point2 pos = p_pos;
  3437. if (root != p_item || !hide_root) {
  3438. h = compute_item_height(p_item) + cache.vseparation;
  3439. if (pos.y < h) {
  3440. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  3441. section = 0;
  3442. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  3443. section = pos.y < h / 2 ? -1 : 1;
  3444. } else if (pos.y < h / 4) {
  3445. section = -1;
  3446. } else if (pos.y >= (h * 3 / 4)) {
  3447. section = 1;
  3448. } else {
  3449. section = 0;
  3450. }
  3451. for (int i = 0; i < columns.size(); i++) {
  3452. int w = get_column_width(i);
  3453. if (pos.x < w) {
  3454. r_column = i;
  3455. return p_item;
  3456. }
  3457. pos.x -= w;
  3458. }
  3459. return nullptr;
  3460. } else {
  3461. pos.y -= h;
  3462. }
  3463. } else {
  3464. h = 0;
  3465. }
  3466. if (p_item->is_collapsed()) {
  3467. return nullptr; // do not try children, it's collapsed
  3468. }
  3469. TreeItem *n = p_item->get_first_child();
  3470. while (n) {
  3471. int ch;
  3472. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  3473. pos.y -= ch;
  3474. h += ch;
  3475. if (r) {
  3476. return r;
  3477. }
  3478. n = n->get_next();
  3479. }
  3480. return nullptr;
  3481. }
  3482. int Tree::get_column_at_position(const Point2 &p_pos) const {
  3483. if (root) {
  3484. Point2 pos = p_pos;
  3485. if (is_layout_rtl()) {
  3486. pos.x = get_size().width - pos.x;
  3487. }
  3488. pos -= cache.bg->get_offset();
  3489. pos.y -= _get_title_button_height();
  3490. if (pos.y < 0) {
  3491. return -1;
  3492. }
  3493. if (h_scroll->is_visible_in_tree()) {
  3494. pos.x += h_scroll->get_value();
  3495. }
  3496. if (v_scroll->is_visible_in_tree()) {
  3497. pos.y += v_scroll->get_value();
  3498. }
  3499. int col, h, section;
  3500. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  3501. if (it) {
  3502. return col;
  3503. }
  3504. }
  3505. return -1;
  3506. }
  3507. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  3508. if (root) {
  3509. Point2 pos = p_pos;
  3510. if (is_layout_rtl()) {
  3511. pos.x = get_size().width - pos.x;
  3512. }
  3513. pos -= cache.bg->get_offset();
  3514. pos.y -= _get_title_button_height();
  3515. if (pos.y < 0) {
  3516. return -100;
  3517. }
  3518. if (h_scroll->is_visible_in_tree()) {
  3519. pos.x += h_scroll->get_value();
  3520. }
  3521. if (v_scroll->is_visible_in_tree()) {
  3522. pos.y += v_scroll->get_value();
  3523. }
  3524. int col, h, section;
  3525. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  3526. if (it) {
  3527. return section;
  3528. }
  3529. }
  3530. return -100;
  3531. }
  3532. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  3533. if (root) {
  3534. Point2 pos = p_pos;
  3535. if (is_layout_rtl()) {
  3536. pos.x = get_size().width - pos.x;
  3537. }
  3538. pos -= cache.bg->get_offset();
  3539. pos.y -= _get_title_button_height();
  3540. if (pos.y < 0) {
  3541. return nullptr;
  3542. }
  3543. if (h_scroll->is_visible_in_tree()) {
  3544. pos.x += h_scroll->get_value();
  3545. }
  3546. if (v_scroll->is_visible_in_tree()) {
  3547. pos.y += v_scroll->get_value();
  3548. }
  3549. int col, h, section;
  3550. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  3551. if (it) {
  3552. return it;
  3553. }
  3554. }
  3555. return nullptr;
  3556. }
  3557. int Tree::get_button_id_at_position(const Point2 &p_pos) const {
  3558. if (root) {
  3559. Point2 pos = p_pos;
  3560. pos -= cache.bg->get_offset();
  3561. pos.y -= _get_title_button_height();
  3562. if (pos.y < 0) {
  3563. return -1;
  3564. }
  3565. if (h_scroll->is_visible_in_tree()) {
  3566. pos.x += h_scroll->get_value();
  3567. }
  3568. if (v_scroll->is_visible_in_tree()) {
  3569. pos.y += v_scroll->get_value();
  3570. }
  3571. int col, h, section;
  3572. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  3573. if (it) {
  3574. const TreeItem::Cell &c = it->cells[col];
  3575. int col_width = get_column_width(col);
  3576. for (int i = 0; i < col; i++) {
  3577. pos.x -= get_column_width(i);
  3578. }
  3579. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  3580. Ref<Texture2D> b = c.buttons[j].texture;
  3581. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  3582. if (pos.x > col_width - size.width) {
  3583. return c.buttons[j].id;
  3584. }
  3585. col_width -= size.width;
  3586. }
  3587. }
  3588. }
  3589. return -1;
  3590. }
  3591. String Tree::get_tooltip(const Point2 &p_pos) const {
  3592. if (root) {
  3593. Point2 pos = p_pos;
  3594. pos -= cache.bg->get_offset();
  3595. pos.y -= _get_title_button_height();
  3596. if (pos.y < 0) {
  3597. return Control::get_tooltip(p_pos);
  3598. }
  3599. if (h_scroll->is_visible_in_tree()) {
  3600. pos.x += h_scroll->get_value();
  3601. }
  3602. if (v_scroll->is_visible_in_tree()) {
  3603. pos.y += v_scroll->get_value();
  3604. }
  3605. int col, h, section;
  3606. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  3607. if (it) {
  3608. const TreeItem::Cell &c = it->cells[col];
  3609. int col_width = get_column_width(col);
  3610. for (int i = 0; i < col; i++) {
  3611. pos.x -= get_column_width(i);
  3612. }
  3613. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  3614. Ref<Texture2D> b = c.buttons[j].texture;
  3615. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  3616. if (pos.x > col_width - size.width) {
  3617. String tooltip = c.buttons[j].tooltip;
  3618. if (tooltip != "") {
  3619. return tooltip;
  3620. }
  3621. }
  3622. col_width -= size.width;
  3623. }
  3624. String ret;
  3625. if (it->get_tooltip(col) == "") {
  3626. ret = it->get_text(col);
  3627. } else {
  3628. ret = it->get_tooltip(col);
  3629. }
  3630. return ret;
  3631. }
  3632. }
  3633. return Control::get_tooltip(p_pos);
  3634. }
  3635. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  3636. cursor_can_exit_tree = p_enable;
  3637. }
  3638. void Tree::set_hide_folding(bool p_hide) {
  3639. hide_folding = p_hide;
  3640. update();
  3641. }
  3642. bool Tree::is_folding_hidden() const {
  3643. return hide_folding;
  3644. }
  3645. void Tree::set_drop_mode_flags(int p_flags) {
  3646. if (drop_mode_flags == p_flags) {
  3647. return;
  3648. }
  3649. drop_mode_flags = p_flags;
  3650. if (drop_mode_flags == 0) {
  3651. drop_mode_over = nullptr;
  3652. }
  3653. update();
  3654. }
  3655. int Tree::get_drop_mode_flags() const {
  3656. return drop_mode_flags;
  3657. }
  3658. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  3659. force_edit_checkbox_only_on_checkbox = p_enable;
  3660. }
  3661. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  3662. return force_edit_checkbox_only_on_checkbox;
  3663. }
  3664. void Tree::set_allow_rmb_select(bool p_allow) {
  3665. allow_rmb_select = p_allow;
  3666. }
  3667. bool Tree::get_allow_rmb_select() const {
  3668. return allow_rmb_select;
  3669. }
  3670. void Tree::set_allow_reselect(bool p_allow) {
  3671. allow_reselect = p_allow;
  3672. }
  3673. bool Tree::get_allow_reselect() const {
  3674. return allow_reselect;
  3675. }
  3676. void Tree::_bind_methods() {
  3677. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  3678. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  3679. ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
  3680. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  3681. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  3682. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  3683. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  3684. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  3685. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  3686. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  3687. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  3688. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  3689. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  3690. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  3691. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  3692. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  3693. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  3694. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  3695. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  3696. ClassDB::bind_method(D_METHOD("edit_selected"), &Tree::edit_selected);
  3697. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  3698. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  3699. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  3700. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  3701. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  3702. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  3703. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  3704. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  3705. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  3706. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  3707. ClassDB::bind_method(D_METHOD("set_column_title_direction", "column", "direction"), &Tree::set_column_title_direction);
  3708. ClassDB::bind_method(D_METHOD("get_column_title_direction", "column"), &Tree::get_column_title_direction);
  3709. ClassDB::bind_method(D_METHOD("set_column_title_opentype_feature", "column", "tag", "value"), &Tree::set_column_title_opentype_feature);
  3710. ClassDB::bind_method(D_METHOD("get_column_title_opentype_feature", "column", "tag"), &Tree::get_column_title_opentype_feature);
  3711. ClassDB::bind_method(D_METHOD("clear_column_title_opentype_features", "column"), &Tree::clear_column_title_opentype_features);
  3712. ClassDB::bind_method(D_METHOD("set_column_title_language", "column", "language"), &Tree::set_column_title_language);
  3713. ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language);
  3714. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  3715. ClassDB::bind_method(D_METHOD("scroll_to_item", "item"), &Tree::_scroll_to_item);
  3716. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  3717. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  3718. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  3719. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  3720. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  3721. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  3722. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  3723. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  3724. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  3725. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  3726. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  3727. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  3728. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  3729. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In Between"), "set_drop_mode_flags", "get_drop_mode_flags");
  3730. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  3731. ADD_SIGNAL(MethodInfo("item_selected"));
  3732. ADD_SIGNAL(MethodInfo("cell_selected"));
  3733. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  3734. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  3735. ADD_SIGNAL(MethodInfo("empty_rmb", PropertyInfo(Variant::VECTOR2, "position")));
  3736. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  3737. ADD_SIGNAL(MethodInfo("item_edited"));
  3738. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  3739. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  3740. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  3741. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  3742. //ADD_SIGNAL( MethodInfo("item_double_clicked" ) );
  3743. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  3744. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  3745. ADD_SIGNAL(MethodInfo("item_activated"));
  3746. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  3747. ADD_SIGNAL(MethodInfo("nothing_selected"));
  3748. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  3749. BIND_ENUM_CONSTANT(SELECT_ROW);
  3750. BIND_ENUM_CONSTANT(SELECT_MULTI);
  3751. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  3752. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  3753. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  3754. }
  3755. Tree::Tree() {
  3756. columns.resize(1);
  3757. set_focus_mode(FOCUS_ALL);
  3758. popup_menu = memnew(PopupMenu);
  3759. popup_menu->hide();
  3760. add_child(popup_menu);
  3761. // popup_menu->set_as_top_level(true);
  3762. popup_editor = memnew(Popup);
  3763. popup_editor->set_wrap_controls(true);
  3764. add_child(popup_editor);
  3765. popup_editor_vb = memnew(VBoxContainer);
  3766. popup_editor->add_child(popup_editor_vb);
  3767. popup_editor_vb->add_theme_constant_override("separation", 0);
  3768. popup_editor_vb->set_anchors_and_offsets_preset(PRESET_WIDE);
  3769. text_editor = memnew(LineEdit);
  3770. popup_editor_vb->add_child(text_editor);
  3771. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  3772. text_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  3773. value_editor = memnew(HSlider);
  3774. value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  3775. value_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  3776. popup_editor_vb->add_child(value_editor);
  3777. value_editor->hide();
  3778. h_scroll = memnew(HScrollBar);
  3779. v_scroll = memnew(VScrollBar);
  3780. add_child(h_scroll);
  3781. add_child(v_scroll);
  3782. range_click_timer = memnew(Timer);
  3783. range_click_timer->connect("timeout", callable_mp(this, &Tree::_range_click_timeout));
  3784. add_child(range_click_timer);
  3785. h_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
  3786. v_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
  3787. text_editor->connect("text_submitted", callable_mp(this, &Tree::_text_editor_submit));
  3788. popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_modal_close));
  3789. popup_menu->connect("id_pressed", callable_mp(this, &Tree::popup_select));
  3790. value_editor->connect("value_changed", callable_mp(this, &Tree::value_editor_changed));
  3791. set_notify_transform(true);
  3792. set_mouse_filter(MOUSE_FILTER_STOP);
  3793. set_clip_contents(true);
  3794. update_cache();
  3795. }
  3796. Tree::~Tree() {
  3797. if (root) {
  3798. memdelete(root);
  3799. }
  3800. }