tree.cpp 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753
  1. /**************************************************************************/
  2. /* tree.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/gui/box_container.h"
  39. #include "scene/gui/text_edit.h"
  40. #include "scene/main/window.h"
  41. #include "scene/theme/theme_db.h"
  42. #include <limits.h>
  43. Size2 TreeItem::Cell::get_icon_size() const {
  44. if (icon.is_null()) {
  45. return Size2();
  46. }
  47. if (icon_region == Rect2i()) {
  48. return icon->get_size();
  49. } else {
  50. return icon_region.size;
  51. }
  52. }
  53. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  54. if (icon.is_null()) {
  55. return;
  56. }
  57. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  58. if (icon_region == Rect2i()) {
  59. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  60. } else {
  61. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  62. }
  63. }
  64. void TreeItem::_changed_notify(int p_cell) {
  65. if (tree) {
  66. tree->item_changed(p_cell, this);
  67. }
  68. }
  69. void TreeItem::_changed_notify() {
  70. if (tree) {
  71. tree->item_changed(-1, this);
  72. }
  73. }
  74. void TreeItem::_cell_selected(int p_cell) {
  75. if (tree) {
  76. tree->item_selected(p_cell, this);
  77. }
  78. }
  79. void TreeItem::_cell_deselected(int p_cell) {
  80. if (tree) {
  81. tree->item_deselected(p_cell, this);
  82. }
  83. }
  84. void TreeItem::_change_tree(Tree *p_tree) {
  85. if (p_tree == tree) {
  86. return;
  87. }
  88. TreeItem *c = first_child;
  89. while (c) {
  90. c->_change_tree(p_tree);
  91. c = c->next;
  92. }
  93. if (tree) {
  94. if (tree->root == this) {
  95. tree->root = nullptr;
  96. }
  97. if (tree->popup_edited_item == this) {
  98. tree->popup_edited_item = nullptr;
  99. tree->popup_pressing_edited_item = nullptr;
  100. tree->pressing_for_editor = false;
  101. }
  102. if (tree->cache.hover_item == this) {
  103. tree->cache.hover_item = nullptr;
  104. }
  105. if (tree->selected_item == this) {
  106. for (int i = 0; i < tree->selected_item->cells.size(); i++) {
  107. tree->selected_item->cells.write[i].selected = false;
  108. }
  109. tree->selected_item = nullptr;
  110. }
  111. if (tree->drop_mode_over == this) {
  112. tree->drop_mode_over = nullptr;
  113. }
  114. if (tree->single_select_defer == this) {
  115. tree->single_select_defer = nullptr;
  116. }
  117. if (tree->edited_item == this) {
  118. tree->edited_item = nullptr;
  119. tree->pressing_for_editor = false;
  120. }
  121. tree->queue_redraw();
  122. }
  123. tree = p_tree;
  124. if (tree) {
  125. tree->queue_redraw();
  126. cells.resize(tree->columns.size());
  127. }
  128. }
  129. /* cell mode */
  130. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  131. ERR_FAIL_INDEX(p_column, cells.size());
  132. if (cells[p_column].mode == p_mode) {
  133. return;
  134. }
  135. Cell &c = cells.write[p_column];
  136. c.mode = p_mode;
  137. c.min = 0;
  138. c.max = 100;
  139. c.step = 1;
  140. c.val = 0;
  141. c.checked = false;
  142. c.icon = Ref<Texture2D>();
  143. c.text = "";
  144. c.dirty = true;
  145. c.icon_max_w = 0;
  146. c.cached_minimum_size_dirty = true;
  147. _changed_notify(p_column);
  148. }
  149. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  150. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  151. return cells[p_column].mode;
  152. }
  153. /* multiline editable */
  154. void TreeItem::set_edit_multiline(int p_column, bool p_multiline) {
  155. ERR_FAIL_INDEX(p_column, cells.size());
  156. cells.write[p_column].edit_multiline = p_multiline;
  157. _changed_notify(p_column);
  158. }
  159. bool TreeItem::is_edit_multiline(int p_column) const {
  160. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  161. return cells[p_column].edit_multiline;
  162. }
  163. /* check mode */
  164. void TreeItem::set_checked(int p_column, bool p_checked) {
  165. ERR_FAIL_INDEX(p_column, cells.size());
  166. if (cells[p_column].checked == p_checked) {
  167. return;
  168. }
  169. cells.write[p_column].checked = p_checked;
  170. cells.write[p_column].indeterminate = false;
  171. cells.write[p_column].cached_minimum_size_dirty = true;
  172. _changed_notify(p_column);
  173. }
  174. void TreeItem::set_indeterminate(int p_column, bool p_indeterminate) {
  175. ERR_FAIL_INDEX(p_column, cells.size());
  176. // Prevent uncheck if indeterminate set to false twice
  177. if (p_indeterminate == cells[p_column].indeterminate) {
  178. return;
  179. }
  180. cells.write[p_column].indeterminate = p_indeterminate;
  181. cells.write[p_column].checked = false;
  182. cells.write[p_column].cached_minimum_size_dirty = true;
  183. _changed_notify(p_column);
  184. }
  185. bool TreeItem::is_checked(int p_column) const {
  186. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  187. return cells[p_column].checked;
  188. }
  189. bool TreeItem::is_indeterminate(int p_column) const {
  190. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  191. return cells[p_column].indeterminate;
  192. }
  193. void TreeItem::propagate_check(int p_column, bool p_emit_signal) {
  194. bool ch = cells[p_column].checked;
  195. if (p_emit_signal) {
  196. tree->emit_signal(SNAME("check_propagated_to_item"), this, p_column);
  197. }
  198. _propagate_check_through_children(p_column, ch, p_emit_signal);
  199. _propagate_check_through_parents(p_column, p_emit_signal);
  200. }
  201. void TreeItem::_propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal) {
  202. TreeItem *current = get_first_child();
  203. while (current) {
  204. current->set_checked(p_column, p_checked);
  205. if (p_emit_signal) {
  206. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  207. }
  208. current->_propagate_check_through_children(p_column, p_checked, p_emit_signal);
  209. current = current->get_next();
  210. }
  211. }
  212. void TreeItem::_propagate_check_through_parents(int p_column, bool p_emit_signal) {
  213. TreeItem *current = get_parent();
  214. if (!current) {
  215. return;
  216. }
  217. bool any_checked = false;
  218. bool any_unchecked = false;
  219. bool any_indeterminate = false;
  220. TreeItem *child_item = current->get_first_child();
  221. while (child_item) {
  222. if (!child_item->is_checked(p_column)) {
  223. any_unchecked = true;
  224. if (child_item->is_indeterminate(p_column)) {
  225. any_indeterminate = true;
  226. break;
  227. }
  228. } else {
  229. any_checked = true;
  230. }
  231. child_item = child_item->get_next();
  232. }
  233. if (any_indeterminate || (any_checked && any_unchecked)) {
  234. current->set_indeterminate(p_column, true);
  235. } else if (current->is_indeterminate(p_column) && !any_checked) {
  236. current->set_indeterminate(p_column, false);
  237. } else {
  238. current->set_checked(p_column, any_checked);
  239. }
  240. if (p_emit_signal) {
  241. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  242. }
  243. current->_propagate_check_through_parents(p_column, p_emit_signal);
  244. }
  245. void TreeItem::set_text(int p_column, String p_text) {
  246. ERR_FAIL_INDEX(p_column, cells.size());
  247. if (cells[p_column].text == p_text) {
  248. return;
  249. }
  250. cells.write[p_column].text = p_text;
  251. cells.write[p_column].dirty = true;
  252. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  253. Vector<String> strings = p_text.split(",");
  254. cells.write[p_column].min = INT_MAX;
  255. cells.write[p_column].max = INT_MIN;
  256. for (int i = 0; i < strings.size(); i++) {
  257. int value = i;
  258. if (!strings[i].get_slicec(':', 1).is_empty()) {
  259. value = strings[i].get_slicec(':', 1).to_int();
  260. }
  261. cells.write[p_column].min = MIN(cells[p_column].min, value);
  262. cells.write[p_column].max = MAX(cells[p_column].max, value);
  263. }
  264. cells.write[p_column].step = 0;
  265. } else {
  266. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  267. if (tree && (!cells[p_column].editable || cells[p_column].mode != TreeItem::CELL_MODE_STRING)) {
  268. cells.write[p_column].xl_text = tree->atr(p_text);
  269. } else {
  270. cells.write[p_column].xl_text = p_text;
  271. }
  272. }
  273. cells.write[p_column].cached_minimum_size_dirty = true;
  274. _changed_notify(p_column);
  275. }
  276. String TreeItem::get_text(int p_column) const {
  277. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  278. return cells[p_column].text;
  279. }
  280. void TreeItem::set_text_direction(int p_column, Control::TextDirection p_text_direction) {
  281. ERR_FAIL_INDEX(p_column, cells.size());
  282. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  283. if (cells[p_column].text_direction == p_text_direction) {
  284. return;
  285. }
  286. cells.write[p_column].text_direction = p_text_direction;
  287. cells.write[p_column].dirty = true;
  288. _changed_notify(p_column);
  289. cells.write[p_column].cached_minimum_size_dirty = true;
  290. }
  291. Control::TextDirection TreeItem::get_text_direction(int p_column) const {
  292. ERR_FAIL_INDEX_V(p_column, cells.size(), Control::TEXT_DIRECTION_INHERITED);
  293. return cells[p_column].text_direction;
  294. }
  295. void TreeItem::set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode) {
  296. ERR_FAIL_INDEX(p_column, cells.size());
  297. ERR_FAIL_COND(p_mode < TextServer::AUTOWRAP_OFF || p_mode > TextServer::AUTOWRAP_WORD_SMART);
  298. if (cells[p_column].autowrap_mode == p_mode) {
  299. return;
  300. }
  301. cells.write[p_column].autowrap_mode = p_mode;
  302. cells.write[p_column].dirty = true;
  303. _changed_notify(p_column);
  304. cells.write[p_column].cached_minimum_size_dirty = true;
  305. }
  306. TextServer::AutowrapMode TreeItem::get_autowrap_mode(int p_column) const {
  307. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::AUTOWRAP_OFF);
  308. return cells[p_column].autowrap_mode;
  309. }
  310. void TreeItem::set_text_overrun_behavior(int p_column, TextServer::OverrunBehavior p_behavior) {
  311. ERR_FAIL_INDEX(p_column, cells.size());
  312. if (cells[p_column].text_buf->get_text_overrun_behavior() == p_behavior) {
  313. return;
  314. }
  315. cells.write[p_column].text_buf->set_text_overrun_behavior(p_behavior);
  316. cells.write[p_column].dirty = true;
  317. _changed_notify(p_column);
  318. cells.write[p_column].cached_minimum_size_dirty = true;
  319. }
  320. TextServer::OverrunBehavior TreeItem::get_text_overrun_behavior(int p_column) const {
  321. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::OVERRUN_TRIM_ELLIPSIS);
  322. return cells[p_column].text_buf->get_text_overrun_behavior();
  323. }
  324. void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) {
  325. ERR_FAIL_INDEX(p_column, cells.size());
  326. if (cells[p_column].st_parser != p_parser) {
  327. cells.write[p_column].st_parser = p_parser;
  328. cells.write[p_column].dirty = true;
  329. cells.write[p_column].cached_minimum_size_dirty = true;
  330. _changed_notify(p_column);
  331. }
  332. }
  333. TextServer::StructuredTextParser TreeItem::get_structured_text_bidi_override(int p_column) const {
  334. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::STRUCTURED_TEXT_DEFAULT);
  335. return cells[p_column].st_parser;
  336. }
  337. void TreeItem::set_structured_text_bidi_override_options(int p_column, Array p_args) {
  338. ERR_FAIL_INDEX(p_column, cells.size());
  339. if (cells[p_column].st_args == p_args) {
  340. return;
  341. }
  342. cells.write[p_column].st_args = p_args;
  343. cells.write[p_column].dirty = true;
  344. cells.write[p_column].cached_minimum_size_dirty = true;
  345. _changed_notify(p_column);
  346. }
  347. Array TreeItem::get_structured_text_bidi_override_options(int p_column) const {
  348. ERR_FAIL_INDEX_V(p_column, cells.size(), Array());
  349. return cells[p_column].st_args;
  350. }
  351. void TreeItem::set_language(int p_column, const String &p_language) {
  352. ERR_FAIL_INDEX(p_column, cells.size());
  353. if (cells[p_column].language != p_language) {
  354. cells.write[p_column].language = p_language;
  355. cells.write[p_column].dirty = true;
  356. cells.write[p_column].cached_minimum_size_dirty = true;
  357. _changed_notify(p_column);
  358. }
  359. }
  360. String TreeItem::get_language(int p_column) const {
  361. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  362. return cells[p_column].language;
  363. }
  364. void TreeItem::set_suffix(int p_column, String p_suffix) {
  365. ERR_FAIL_INDEX(p_column, cells.size());
  366. if (cells[p_column].suffix == p_suffix) {
  367. return;
  368. }
  369. cells.write[p_column].suffix = p_suffix;
  370. cells.write[p_column].cached_minimum_size_dirty = true;
  371. _changed_notify(p_column);
  372. }
  373. String TreeItem::get_suffix(int p_column) const {
  374. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  375. return cells[p_column].suffix;
  376. }
  377. void TreeItem::set_icon(int p_column, const Ref<Texture2D> &p_icon) {
  378. ERR_FAIL_INDEX(p_column, cells.size());
  379. if (cells[p_column].icon == p_icon) {
  380. return;
  381. }
  382. cells.write[p_column].icon = p_icon;
  383. cells.write[p_column].cached_minimum_size_dirty = true;
  384. _changed_notify(p_column);
  385. }
  386. Ref<Texture2D> TreeItem::get_icon(int p_column) const {
  387. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  388. return cells[p_column].icon;
  389. }
  390. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  391. ERR_FAIL_INDEX(p_column, cells.size());
  392. if (cells[p_column].icon_region == p_icon_region) {
  393. return;
  394. }
  395. cells.write[p_column].icon_region = p_icon_region;
  396. cells.write[p_column].cached_minimum_size_dirty = true;
  397. _changed_notify(p_column);
  398. }
  399. Rect2 TreeItem::get_icon_region(int p_column) const {
  400. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  401. return cells[p_column].icon_region;
  402. }
  403. void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
  404. ERR_FAIL_INDEX(p_column, cells.size());
  405. if (cells[p_column].icon_color == p_modulate) {
  406. return;
  407. }
  408. cells.write[p_column].icon_color = p_modulate;
  409. _changed_notify(p_column);
  410. }
  411. Color TreeItem::get_icon_modulate(int p_column) const {
  412. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  413. return cells[p_column].icon_color;
  414. }
  415. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  416. ERR_FAIL_INDEX(p_column, cells.size());
  417. if (cells[p_column].icon_max_w == p_max) {
  418. return;
  419. }
  420. cells.write[p_column].icon_max_w = p_max;
  421. cells.write[p_column].cached_minimum_size_dirty = true;
  422. _changed_notify(p_column);
  423. }
  424. int TreeItem::get_icon_max_width(int p_column) const {
  425. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  426. return cells[p_column].icon_max_w;
  427. }
  428. /* range works for mode number or mode combo */
  429. void TreeItem::set_range(int p_column, double p_value) {
  430. ERR_FAIL_INDEX(p_column, cells.size());
  431. if (cells[p_column].step > 0) {
  432. p_value = Math::snapped(p_value, cells[p_column].step);
  433. }
  434. if (p_value < cells[p_column].min) {
  435. p_value = cells[p_column].min;
  436. }
  437. if (p_value > cells[p_column].max) {
  438. p_value = cells[p_column].max;
  439. }
  440. if (cells[p_column].val == p_value) {
  441. return;
  442. }
  443. cells.write[p_column].val = p_value;
  444. cells.write[p_column].dirty = true;
  445. _changed_notify(p_column);
  446. }
  447. double TreeItem::get_range(int p_column) const {
  448. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  449. return cells[p_column].val;
  450. }
  451. bool TreeItem::is_range_exponential(int p_column) const {
  452. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  453. return cells[p_column].expr;
  454. }
  455. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  456. ERR_FAIL_INDEX(p_column, cells.size());
  457. if (cells[p_column].min == p_min && cells[p_column].max == p_max && cells[p_column].step == p_step && cells[p_column].expr == p_exp) {
  458. return;
  459. }
  460. cells.write[p_column].min = p_min;
  461. cells.write[p_column].max = p_max;
  462. cells.write[p_column].step = p_step;
  463. cells.write[p_column].expr = p_exp;
  464. _changed_notify(p_column);
  465. }
  466. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  467. ERR_FAIL_INDEX(p_column, cells.size());
  468. r_min = cells[p_column].min;
  469. r_max = cells[p_column].max;
  470. r_step = cells[p_column].step;
  471. }
  472. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  473. ERR_FAIL_INDEX(p_column, cells.size());
  474. cells.write[p_column].meta = p_meta;
  475. }
  476. Variant TreeItem::get_metadata(int p_column) const {
  477. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  478. return cells[p_column].meta;
  479. }
  480. #ifndef DISABLE_DEPRECATED
  481. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  482. WARN_DEPRECATED_MSG(R"*(The "set_custom_draw()" method is deprecated, use "set_custom_draw_callback()" instead.)*");
  483. ERR_FAIL_INDEX(p_column, cells.size());
  484. ERR_FAIL_NULL(p_object);
  485. cells.write[p_column].custom_draw_callback = Callable(p_object, p_callback);
  486. _changed_notify(p_column);
  487. }
  488. #endif // DISABLE_DEPRECATED
  489. void TreeItem::set_custom_draw_callback(int p_column, const Callable &p_callback) {
  490. ERR_FAIL_INDEX(p_column, cells.size());
  491. cells.write[p_column].custom_draw_callback = p_callback;
  492. _changed_notify(p_column);
  493. }
  494. Callable TreeItem::get_custom_draw_callback(int p_column) const {
  495. ERR_FAIL_INDEX_V(p_column, cells.size(), Callable());
  496. return cells[p_column].custom_draw_callback;
  497. }
  498. void TreeItem::set_collapsed(bool p_collapsed) {
  499. if (collapsed == p_collapsed || !tree) {
  500. return;
  501. }
  502. collapsed = p_collapsed;
  503. TreeItem *ci = tree->selected_item;
  504. if (ci) {
  505. while (ci && ci != this) {
  506. ci = ci->parent;
  507. }
  508. if (ci) { // collapsing cursor/selected, move it!
  509. if (tree->select_mode == Tree::SELECT_MULTI) {
  510. tree->selected_item = this;
  511. emit_signal(SNAME("cell_selected"));
  512. } else {
  513. select(tree->selected_col);
  514. }
  515. tree->queue_redraw();
  516. }
  517. }
  518. _changed_notify();
  519. tree->emit_signal(SNAME("item_collapsed"), this);
  520. }
  521. bool TreeItem::is_collapsed() {
  522. return collapsed;
  523. }
  524. void TreeItem::set_collapsed_recursive(bool p_collapsed) {
  525. if (!tree) {
  526. return;
  527. }
  528. set_collapsed(p_collapsed);
  529. TreeItem *child = get_first_child();
  530. while (child) {
  531. child->set_collapsed_recursive(p_collapsed);
  532. child = child->get_next();
  533. }
  534. }
  535. bool TreeItem::_is_any_collapsed(bool p_only_visible) {
  536. TreeItem *child = get_first_child();
  537. // Check on children directly first (avoid recursing if possible).
  538. while (child) {
  539. if (child->get_first_child() && child->is_collapsed() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count()))) {
  540. return true;
  541. }
  542. child = child->get_next();
  543. }
  544. child = get_first_child();
  545. // Otherwise recurse on children.
  546. while (child) {
  547. if (child->get_first_child() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count())) && child->_is_any_collapsed(p_only_visible)) {
  548. return true;
  549. }
  550. child = child->get_next();
  551. }
  552. return false;
  553. }
  554. bool TreeItem::is_any_collapsed(bool p_only_visible) {
  555. if (p_only_visible && !is_visible_in_tree()) {
  556. return false;
  557. }
  558. // Collapsed if this is collapsed and it has children (only considers visible if only visible is set).
  559. if (is_collapsed() && get_first_child() && (!p_only_visible || get_visible_child_count())) {
  560. return true;
  561. }
  562. return _is_any_collapsed(p_only_visible);
  563. }
  564. void TreeItem::set_visible(bool p_visible) {
  565. if (visible == p_visible) {
  566. return;
  567. }
  568. visible = p_visible;
  569. if (tree) {
  570. tree->queue_redraw();
  571. _changed_notify();
  572. }
  573. _handle_visibility_changed(p_visible);
  574. }
  575. bool TreeItem::is_visible() {
  576. return visible;
  577. }
  578. bool TreeItem::is_visible_in_tree() const {
  579. return visible && parent_visible_in_tree;
  580. }
  581. void TreeItem::_handle_visibility_changed(bool p_visible) {
  582. TreeItem *child = get_first_child();
  583. while (child) {
  584. child->_propagate_visibility_changed(p_visible);
  585. child = child->get_next();
  586. }
  587. }
  588. void TreeItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
  589. parent_visible_in_tree = p_parent_visible_in_tree;
  590. _handle_visibility_changed(p_parent_visible_in_tree);
  591. }
  592. void TreeItem::uncollapse_tree() {
  593. TreeItem *t = this;
  594. while (t) {
  595. t->set_collapsed(false);
  596. t = t->parent;
  597. }
  598. }
  599. void TreeItem::set_custom_minimum_height(int p_height) {
  600. if (custom_min_height == p_height) {
  601. return;
  602. }
  603. custom_min_height = p_height;
  604. for (Cell &c : cells) {
  605. c.cached_minimum_size_dirty = true;
  606. }
  607. _changed_notify();
  608. }
  609. int TreeItem::get_custom_minimum_height() const {
  610. return custom_min_height;
  611. }
  612. /* Item manipulation */
  613. TreeItem *TreeItem::create_child(int p_index) {
  614. TreeItem *ti = memnew(TreeItem(tree));
  615. if (tree) {
  616. ti->cells.resize(tree->columns.size());
  617. tree->queue_redraw();
  618. }
  619. TreeItem *item_prev = nullptr;
  620. TreeItem *item_next = first_child;
  621. int idx = 0;
  622. while (item_next) {
  623. if (idx == p_index) {
  624. item_next->prev = ti;
  625. ti->next = item_next;
  626. break;
  627. }
  628. item_prev = item_next;
  629. item_next = item_next->next;
  630. idx++;
  631. }
  632. if (item_prev) {
  633. item_prev->next = ti;
  634. ti->prev = item_prev;
  635. if (!children_cache.is_empty()) {
  636. if (ti->next) {
  637. children_cache.insert(p_index, ti);
  638. } else {
  639. children_cache.append(ti);
  640. }
  641. }
  642. } else {
  643. first_child = ti;
  644. if (!children_cache.is_empty()) {
  645. children_cache.insert(0, ti);
  646. }
  647. }
  648. ti->parent = this;
  649. ti->parent_visible_in_tree = is_visible_in_tree();
  650. return ti;
  651. }
  652. void TreeItem::add_child(TreeItem *p_item) {
  653. ERR_FAIL_NULL(p_item);
  654. ERR_FAIL_COND(p_item->tree);
  655. ERR_FAIL_COND(p_item->parent);
  656. p_item->_change_tree(tree);
  657. p_item->parent = this;
  658. p_item->parent_visible_in_tree = is_visible_in_tree();
  659. p_item->_handle_visibility_changed(p_item->parent_visible_in_tree);
  660. TreeItem *item_prev = first_child;
  661. while (item_prev && item_prev->next) {
  662. item_prev = item_prev->next;
  663. }
  664. if (item_prev) {
  665. item_prev->next = p_item;
  666. p_item->prev = item_prev;
  667. } else {
  668. first_child = p_item;
  669. }
  670. if (!children_cache.is_empty()) {
  671. children_cache.append(p_item);
  672. }
  673. validate_cache();
  674. }
  675. void TreeItem::remove_child(TreeItem *p_item) {
  676. ERR_FAIL_NULL(p_item);
  677. ERR_FAIL_COND(p_item->parent != this);
  678. p_item->_unlink_from_tree();
  679. p_item->_change_tree(nullptr);
  680. p_item->prev = nullptr;
  681. p_item->next = nullptr;
  682. p_item->parent = nullptr;
  683. validate_cache();
  684. }
  685. Tree *TreeItem::get_tree() const {
  686. return tree;
  687. }
  688. TreeItem *TreeItem::get_next() const {
  689. return next;
  690. }
  691. TreeItem *TreeItem::get_prev() {
  692. if (prev) {
  693. return prev;
  694. }
  695. if (!parent || parent->first_child == this) {
  696. return nullptr;
  697. }
  698. // This is an edge case
  699. TreeItem *l_prev = parent->first_child;
  700. while (l_prev && l_prev->next != this) {
  701. l_prev = l_prev->next;
  702. }
  703. prev = l_prev;
  704. return prev;
  705. }
  706. TreeItem *TreeItem::get_parent() const {
  707. return parent;
  708. }
  709. TreeItem *TreeItem::get_first_child() const {
  710. return first_child;
  711. }
  712. TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
  713. TreeItem *current = this;
  714. TreeItem *prev_item = current->get_prev();
  715. if (!prev_item) {
  716. current = current->parent;
  717. if (current == tree->root && tree->hide_root) {
  718. return nullptr;
  719. } else if (!current) {
  720. if (p_wrap) {
  721. current = this;
  722. TreeItem *temp = get_next_visible();
  723. while (temp) {
  724. current = temp;
  725. temp = temp->get_next_visible();
  726. }
  727. } else {
  728. return nullptr;
  729. }
  730. }
  731. } else {
  732. current = prev_item;
  733. while ((!current->collapsed || p_include_invisible) && current->first_child) {
  734. //go to the very end
  735. current = current->first_child;
  736. while (current->next) {
  737. current = current->next;
  738. }
  739. }
  740. }
  741. return current;
  742. }
  743. TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
  744. TreeItem *loop = this;
  745. TreeItem *prev_item = _get_prev_in_tree(p_wrap);
  746. while (prev_item && !prev_item->is_visible_in_tree()) {
  747. prev_item = prev_item->_get_prev_in_tree(p_wrap);
  748. if (prev_item == loop) {
  749. // Check that we haven't looped all the way around to the start.
  750. prev_item = nullptr;
  751. break;
  752. }
  753. }
  754. return prev_item;
  755. }
  756. TreeItem *TreeItem::_get_next_in_tree(bool p_wrap, bool p_include_invisible) {
  757. TreeItem *current = this;
  758. if ((!current->collapsed || p_include_invisible) && current->first_child) {
  759. current = current->first_child;
  760. } else if (current->next) {
  761. current = current->next;
  762. } else {
  763. while (current && !current->next) {
  764. current = current->parent;
  765. }
  766. if (!current) {
  767. if (p_wrap) {
  768. return tree->root;
  769. } else {
  770. return nullptr;
  771. }
  772. } else {
  773. current = current->next;
  774. }
  775. }
  776. return current;
  777. }
  778. TreeItem *TreeItem::get_next_visible(bool p_wrap) {
  779. TreeItem *loop = this;
  780. TreeItem *next_item = _get_next_in_tree(p_wrap);
  781. while (next_item && !next_item->is_visible_in_tree()) {
  782. next_item = next_item->_get_next_in_tree(p_wrap);
  783. if (next_item == loop) {
  784. // Check that we haven't looped all the way around to the start.
  785. next_item = nullptr;
  786. break;
  787. }
  788. }
  789. return next_item;
  790. }
  791. TreeItem *TreeItem::get_prev_in_tree(bool p_wrap) {
  792. TreeItem *prev_item = _get_prev_in_tree(p_wrap, true);
  793. return prev_item;
  794. }
  795. TreeItem *TreeItem::get_next_in_tree(bool p_wrap) {
  796. TreeItem *next_item = _get_next_in_tree(p_wrap, true);
  797. return next_item;
  798. }
  799. TreeItem *TreeItem::get_child(int p_index) {
  800. _create_children_cache();
  801. if (p_index < 0) {
  802. p_index += children_cache.size();
  803. }
  804. ERR_FAIL_INDEX_V(p_index, children_cache.size(), nullptr);
  805. return children_cache.get(p_index);
  806. }
  807. int TreeItem::get_visible_child_count() {
  808. _create_children_cache();
  809. int visible_count = 0;
  810. for (int i = 0; i < children_cache.size(); i++) {
  811. if (children_cache[i]->is_visible()) {
  812. visible_count += 1;
  813. }
  814. }
  815. return visible_count;
  816. }
  817. int TreeItem::get_child_count() {
  818. _create_children_cache();
  819. return children_cache.size();
  820. }
  821. TypedArray<TreeItem> TreeItem::get_children() {
  822. // Don't need to explicitly create children cache, because get_child_count creates it.
  823. int size = get_child_count();
  824. TypedArray<TreeItem> arr;
  825. arr.resize(size);
  826. for (int i = 0; i < size; i++) {
  827. arr[i] = children_cache[i];
  828. }
  829. return arr;
  830. }
  831. void TreeItem::clear_children() {
  832. TreeItem *c = first_child;
  833. while (c) {
  834. TreeItem *aux = c;
  835. c = c->get_next();
  836. aux->parent = nullptr; // So it won't try to recursively auto-remove from me in here.
  837. memdelete(aux);
  838. }
  839. first_child = nullptr;
  840. };
  841. int TreeItem::get_index() {
  842. int idx = 0;
  843. TreeItem *c = this;
  844. while (c) {
  845. c = c->get_prev();
  846. idx++;
  847. }
  848. return idx - 1;
  849. }
  850. #ifdef DEV_ENABLED
  851. void TreeItem::validate_cache() const {
  852. if (!parent || parent->children_cache.is_empty()) {
  853. return;
  854. }
  855. TreeItem *scan = parent->first_child;
  856. int index = 0;
  857. while (scan) {
  858. DEV_ASSERT(parent->children_cache[index] == scan);
  859. ++index;
  860. scan = scan->get_next();
  861. }
  862. DEV_ASSERT(index == parent->children_cache.size());
  863. }
  864. #endif
  865. void TreeItem::move_before(TreeItem *p_item) {
  866. ERR_FAIL_NULL(p_item);
  867. ERR_FAIL_COND(is_root);
  868. ERR_FAIL_NULL(p_item->parent);
  869. if (p_item == this) {
  870. return;
  871. }
  872. TreeItem *p = p_item->parent;
  873. while (p) {
  874. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  875. p = p->parent;
  876. }
  877. Tree *old_tree = tree;
  878. _unlink_from_tree();
  879. _change_tree(p_item->tree);
  880. parent = p_item->parent;
  881. TreeItem *item_prev = p_item->get_prev();
  882. if (item_prev) {
  883. item_prev->next = this;
  884. parent->children_cache.clear();
  885. } else {
  886. parent->first_child = this;
  887. // If the cache is empty, it has not been built but there
  888. // are items in the tree (note p_item != nullptr) so we cannot update it.
  889. if (!parent->children_cache.is_empty()) {
  890. parent->children_cache.insert(0, this);
  891. }
  892. }
  893. prev = item_prev;
  894. next = p_item;
  895. p_item->prev = this;
  896. if (tree && old_tree == tree) {
  897. tree->queue_redraw();
  898. }
  899. validate_cache();
  900. }
  901. void TreeItem::move_after(TreeItem *p_item) {
  902. ERR_FAIL_NULL(p_item);
  903. ERR_FAIL_COND(is_root);
  904. ERR_FAIL_NULL(p_item->parent);
  905. if (p_item == this) {
  906. return;
  907. }
  908. TreeItem *p = p_item->parent;
  909. while (p) {
  910. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  911. p = p->parent;
  912. }
  913. Tree *old_tree = tree;
  914. _unlink_from_tree();
  915. _change_tree(p_item->tree);
  916. if (p_item->next) {
  917. p_item->next->prev = this;
  918. }
  919. parent = p_item->parent;
  920. prev = p_item;
  921. next = p_item->next;
  922. p_item->next = this;
  923. if (next) {
  924. parent->children_cache.clear();
  925. } else {
  926. // If the cache is empty, it has not been built but there
  927. // are items in the tree (note p_item != nullptr,) so we cannot update it.
  928. if (!parent->children_cache.is_empty()) {
  929. parent->children_cache.append(this);
  930. }
  931. }
  932. if (tree && old_tree == tree) {
  933. tree->queue_redraw();
  934. }
  935. validate_cache();
  936. }
  937. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  938. ERR_FAIL_INDEX(p_column, cells.size());
  939. cells.write[p_column].selectable = p_selectable;
  940. }
  941. bool TreeItem::is_selectable(int p_column) const {
  942. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  943. return cells[p_column].selectable;
  944. }
  945. bool TreeItem::is_selected(int p_column) {
  946. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  947. return cells[p_column].selectable && cells[p_column].selected;
  948. }
  949. void TreeItem::set_as_cursor(int p_column) {
  950. ERR_FAIL_INDEX(p_column, cells.size());
  951. if (!tree) {
  952. return;
  953. }
  954. if (tree->select_mode != Tree::SELECT_MULTI) {
  955. return;
  956. }
  957. if (tree->selected_item == this && tree->selected_col == p_column) {
  958. return;
  959. }
  960. tree->selected_item = this;
  961. tree->selected_col = p_column;
  962. tree->queue_redraw();
  963. }
  964. void TreeItem::select(int p_column) {
  965. ERR_FAIL_INDEX(p_column, cells.size());
  966. _cell_selected(p_column);
  967. }
  968. void TreeItem::deselect(int p_column) {
  969. ERR_FAIL_INDEX(p_column, cells.size());
  970. _cell_deselected(p_column);
  971. }
  972. void TreeItem::add_button(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip) {
  973. ERR_FAIL_INDEX(p_column, cells.size());
  974. ERR_FAIL_COND(!p_button.is_valid());
  975. TreeItem::Cell::Button button;
  976. button.texture = p_button;
  977. if (p_id < 0) {
  978. p_id = cells[p_column].buttons.size();
  979. }
  980. button.id = p_id;
  981. button.disabled = p_disabled;
  982. button.tooltip = p_tooltip;
  983. cells.write[p_column].buttons.push_back(button);
  984. cells.write[p_column].cached_minimum_size_dirty = true;
  985. _changed_notify(p_column);
  986. }
  987. int TreeItem::get_button_count(int p_column) const {
  988. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  989. return cells[p_column].buttons.size();
  990. }
  991. Ref<Texture2D> TreeItem::get_button(int p_column, int p_index) const {
  992. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  993. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Ref<Texture2D>());
  994. return cells[p_column].buttons[p_index].texture;
  995. }
  996. String TreeItem::get_button_tooltip_text(int p_column, int p_index) const {
  997. ERR_FAIL_INDEX_V(p_column, cells.size(), String());
  998. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), String());
  999. return cells[p_column].buttons[p_index].tooltip;
  1000. }
  1001. int TreeItem::get_button_id(int p_column, int p_index) const {
  1002. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1003. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), -1);
  1004. return cells[p_column].buttons[p_index].id;
  1005. }
  1006. void TreeItem::erase_button(int p_column, int p_index) {
  1007. ERR_FAIL_INDEX(p_column, cells.size());
  1008. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1009. cells.write[p_column].buttons.remove_at(p_index);
  1010. _changed_notify(p_column);
  1011. }
  1012. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  1013. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1014. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  1015. if (cells[p_column].buttons[i].id == p_id) {
  1016. return i;
  1017. }
  1018. }
  1019. return -1;
  1020. }
  1021. Color TreeItem::get_button_color(int p_column, int p_index) const {
  1022. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1023. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Color());
  1024. return cells[p_column].buttons[p_index].color;
  1025. }
  1026. void TreeItem::set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip) {
  1027. ERR_FAIL_INDEX(p_column, cells.size());
  1028. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1029. cells.write[p_column].buttons.write[p_index].tooltip = p_tooltip;
  1030. }
  1031. void TreeItem::set_button(int p_column, int p_index, const Ref<Texture2D> &p_button) {
  1032. ERR_FAIL_COND(p_button.is_null());
  1033. ERR_FAIL_INDEX(p_column, cells.size());
  1034. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1035. if (cells[p_column].buttons[p_index].texture == p_button) {
  1036. return;
  1037. }
  1038. cells.write[p_column].buttons.write[p_index].texture = p_button;
  1039. cells.write[p_column].cached_minimum_size_dirty = true;
  1040. _changed_notify(p_column);
  1041. }
  1042. void TreeItem::set_button_color(int p_column, int p_index, const Color &p_color) {
  1043. ERR_FAIL_INDEX(p_column, cells.size());
  1044. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1045. if (cells[p_column].buttons[p_index].color == p_color) {
  1046. return;
  1047. }
  1048. cells.write[p_column].buttons.write[p_index].color = p_color;
  1049. _changed_notify(p_column);
  1050. }
  1051. void TreeItem::set_button_disabled(int p_column, int p_index, bool p_disabled) {
  1052. ERR_FAIL_INDEX(p_column, cells.size());
  1053. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1054. if (cells[p_column].buttons[p_index].disabled == p_disabled) {
  1055. return;
  1056. }
  1057. cells.write[p_column].buttons.write[p_index].disabled = p_disabled;
  1058. cells.write[p_column].cached_minimum_size_dirty = true;
  1059. _changed_notify(p_column);
  1060. }
  1061. bool TreeItem::is_button_disabled(int p_column, int p_index) const {
  1062. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1063. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), false);
  1064. return cells[p_column].buttons[p_index].disabled;
  1065. }
  1066. void TreeItem::set_editable(int p_column, bool p_editable) {
  1067. ERR_FAIL_INDEX(p_column, cells.size());
  1068. if (cells[p_column].editable == p_editable) {
  1069. return;
  1070. }
  1071. cells.write[p_column].editable = p_editable;
  1072. cells.write[p_column].cached_minimum_size_dirty = true;
  1073. _changed_notify(p_column);
  1074. }
  1075. bool TreeItem::is_editable(int p_column) {
  1076. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1077. return cells[p_column].editable;
  1078. }
  1079. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  1080. ERR_FAIL_INDEX(p_column, cells.size());
  1081. if (cells[p_column].custom_color && cells[p_column].color == p_color) {
  1082. return;
  1083. }
  1084. cells.write[p_column].custom_color = true;
  1085. cells.write[p_column].color = p_color;
  1086. _changed_notify(p_column);
  1087. }
  1088. Color TreeItem::get_custom_color(int p_column) const {
  1089. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1090. if (!cells[p_column].custom_color) {
  1091. return Color();
  1092. }
  1093. return cells[p_column].color;
  1094. }
  1095. void TreeItem::clear_custom_color(int p_column) {
  1096. ERR_FAIL_INDEX(p_column, cells.size());
  1097. cells.write[p_column].custom_color = false;
  1098. cells.write[p_column].color = Color();
  1099. _changed_notify(p_column);
  1100. }
  1101. void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
  1102. ERR_FAIL_INDEX(p_column, cells.size());
  1103. if (cells[p_column].custom_font == p_font) {
  1104. return;
  1105. }
  1106. cells.write[p_column].custom_font = p_font;
  1107. cells.write[p_column].cached_minimum_size_dirty = true;
  1108. _changed_notify(p_column);
  1109. }
  1110. Ref<Font> TreeItem::get_custom_font(int p_column) const {
  1111. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
  1112. return cells[p_column].custom_font;
  1113. }
  1114. void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
  1115. ERR_FAIL_INDEX(p_column, cells.size());
  1116. if (cells[p_column].custom_font_size == p_font_size) {
  1117. return;
  1118. }
  1119. cells.write[p_column].custom_font_size = p_font_size;
  1120. cells.write[p_column].cached_minimum_size_dirty = true;
  1121. _changed_notify(p_column);
  1122. }
  1123. int TreeItem::get_custom_font_size(int p_column) const {
  1124. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1125. return cells[p_column].custom_font_size;
  1126. }
  1127. void TreeItem::set_tooltip_text(int p_column, const String &p_tooltip) {
  1128. ERR_FAIL_INDEX(p_column, cells.size());
  1129. cells.write[p_column].tooltip = p_tooltip;
  1130. }
  1131. String TreeItem::get_tooltip_text(int p_column) const {
  1132. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  1133. return cells[p_column].tooltip;
  1134. }
  1135. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  1136. ERR_FAIL_INDEX(p_column, cells.size());
  1137. if (cells[p_column].custom_bg_color && cells[p_column].custom_bg_outline == p_bg_outline && cells[p_column].bg_color == p_color) {
  1138. return;
  1139. }
  1140. cells.write[p_column].custom_bg_color = true;
  1141. cells.write[p_column].custom_bg_outline = p_bg_outline;
  1142. cells.write[p_column].bg_color = p_color;
  1143. _changed_notify(p_column);
  1144. }
  1145. void TreeItem::clear_custom_bg_color(int p_column) {
  1146. ERR_FAIL_INDEX(p_column, cells.size());
  1147. cells.write[p_column].custom_bg_color = false;
  1148. cells.write[p_column].bg_color = Color();
  1149. _changed_notify(p_column);
  1150. }
  1151. Color TreeItem::get_custom_bg_color(int p_column) const {
  1152. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1153. if (!cells[p_column].custom_bg_color) {
  1154. return Color();
  1155. }
  1156. return cells[p_column].bg_color;
  1157. }
  1158. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  1159. ERR_FAIL_INDEX(p_column, cells.size());
  1160. if (cells[p_column].custom_button == p_button) {
  1161. return;
  1162. }
  1163. cells.write[p_column].custom_button = p_button;
  1164. cells.write[p_column].cached_minimum_size_dirty = true;
  1165. _changed_notify(p_column);
  1166. }
  1167. bool TreeItem::is_custom_set_as_button(int p_column) const {
  1168. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1169. return cells[p_column].custom_button;
  1170. }
  1171. void TreeItem::set_text_alignment(int p_column, HorizontalAlignment p_alignment) {
  1172. ERR_FAIL_INDEX(p_column, cells.size());
  1173. if (cells[p_column].text_alignment == p_alignment) {
  1174. return;
  1175. }
  1176. cells.write[p_column].text_alignment = p_alignment;
  1177. cells.write[p_column].cached_minimum_size_dirty = true;
  1178. _changed_notify(p_column);
  1179. }
  1180. HorizontalAlignment TreeItem::get_text_alignment(int p_column) const {
  1181. ERR_FAIL_INDEX_V(p_column, cells.size(), HORIZONTAL_ALIGNMENT_LEFT);
  1182. return cells[p_column].text_alignment;
  1183. }
  1184. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  1185. ERR_FAIL_INDEX(p_column, cells.size());
  1186. if (cells[p_column].expand_right == p_enable) {
  1187. return;
  1188. }
  1189. cells.write[p_column].expand_right = p_enable;
  1190. cells.write[p_column].cached_minimum_size_dirty = true;
  1191. _changed_notify(p_column);
  1192. }
  1193. bool TreeItem::get_expand_right(int p_column) const {
  1194. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1195. return cells[p_column].expand_right;
  1196. }
  1197. void TreeItem::set_disable_folding(bool p_disable) {
  1198. if (disable_folding == p_disable) {
  1199. return;
  1200. }
  1201. disable_folding = p_disable;
  1202. for (Cell &c : cells) {
  1203. c.cached_minimum_size_dirty = true;
  1204. }
  1205. _changed_notify(0);
  1206. }
  1207. bool TreeItem::is_folding_disabled() const {
  1208. return disable_folding;
  1209. }
  1210. Size2 TreeItem::get_minimum_size(int p_column) {
  1211. ERR_FAIL_INDEX_V(p_column, cells.size(), Size2());
  1212. Tree *parent_tree = get_tree();
  1213. ERR_FAIL_NULL_V(parent_tree, Size2());
  1214. const TreeItem::Cell &cell = cells[p_column];
  1215. if (cell.cached_minimum_size_dirty) {
  1216. Size2 size;
  1217. // Text.
  1218. if (!cell.text.is_empty()) {
  1219. if (cell.dirty) {
  1220. parent_tree->update_item_cell(this, p_column);
  1221. }
  1222. Size2 text_size = cell.text_buf->get_size();
  1223. size.width += text_size.width;
  1224. size.height = MAX(size.height, text_size.height);
  1225. }
  1226. // Icon.
  1227. if (cell.mode == CELL_MODE_CHECK) {
  1228. size.width += parent_tree->theme_cache.checked->get_width() + parent_tree->theme_cache.h_separation;
  1229. }
  1230. if (cell.icon.is_valid()) {
  1231. Size2i icon_size = parent_tree->_get_cell_icon_size(cell);
  1232. size.width += icon_size.width + parent_tree->theme_cache.h_separation;
  1233. size.height = MAX(size.height, icon_size.height);
  1234. }
  1235. // Buttons.
  1236. for (int i = 0; i < cell.buttons.size(); i++) {
  1237. Ref<Texture2D> texture = cell.buttons[i].texture;
  1238. if (texture.is_valid()) {
  1239. Size2 button_size = texture->get_size() + parent_tree->theme_cache.button_pressed->get_minimum_size();
  1240. size.width += button_size.width;
  1241. size.height = MAX(size.height, button_size.height);
  1242. }
  1243. }
  1244. if (cell.buttons.size() >= 2) {
  1245. size.width += (cell.buttons.size() - 1) * parent_tree->theme_cache.button_margin;
  1246. }
  1247. cells.write[p_column].cached_minimum_size = size;
  1248. cells.write[p_column].cached_minimum_size_dirty = false;
  1249. }
  1250. return cell.cached_minimum_size;
  1251. }
  1252. void TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1253. if (p_argcount < 1) {
  1254. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1255. r_error.expected = 1;
  1256. return;
  1257. }
  1258. if (p_args[0]->get_type() != Variant::STRING && p_args[0]->get_type() != Variant::STRING_NAME) {
  1259. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1260. r_error.argument = 0;
  1261. r_error.expected = Variant::STRING_NAME;
  1262. return;
  1263. }
  1264. StringName method = *p_args[0];
  1265. call_recursive(method, &p_args[1], p_argcount - 1, r_error);
  1266. }
  1267. void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1268. if (!p_item) {
  1269. return;
  1270. }
  1271. p_item->callp(p_method, p_args, p_argcount, r_error);
  1272. TreeItem *c = p_item->get_first_child();
  1273. while (c) {
  1274. recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
  1275. c = c->get_next();
  1276. }
  1277. }
  1278. void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1279. recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
  1280. }
  1281. void TreeItem::_bind_methods() {
  1282. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  1283. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  1284. ClassDB::bind_method(D_METHOD("set_edit_multiline", "column", "multiline"), &TreeItem::set_edit_multiline);
  1285. ClassDB::bind_method(D_METHOD("is_edit_multiline", "column"), &TreeItem::is_edit_multiline);
  1286. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  1287. ClassDB::bind_method(D_METHOD("set_indeterminate", "column", "indeterminate"), &TreeItem::set_indeterminate);
  1288. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  1289. ClassDB::bind_method(D_METHOD("is_indeterminate", "column"), &TreeItem::is_indeterminate);
  1290. ClassDB::bind_method(D_METHOD("propagate_check", "column", "emit_signal"), &TreeItem::propagate_check, DEFVAL(true));
  1291. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  1292. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  1293. ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
  1294. ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
  1295. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "column", "autowrap_mode"), &TreeItem::set_autowrap_mode);
  1296. ClassDB::bind_method(D_METHOD("get_autowrap_mode", "column"), &TreeItem::get_autowrap_mode);
  1297. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "column", "overrun_behavior"), &TreeItem::set_text_overrun_behavior);
  1298. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior", "column"), &TreeItem::get_text_overrun_behavior);
  1299. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
  1300. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
  1301. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "column", "args"), &TreeItem::set_structured_text_bidi_override_options);
  1302. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options", "column"), &TreeItem::get_structured_text_bidi_override_options);
  1303. ClassDB::bind_method(D_METHOD("set_language", "column", "language"), &TreeItem::set_language);
  1304. ClassDB::bind_method(D_METHOD("get_language", "column"), &TreeItem::get_language);
  1305. ClassDB::bind_method(D_METHOD("set_suffix", "column", "text"), &TreeItem::set_suffix);
  1306. ClassDB::bind_method(D_METHOD("get_suffix", "column"), &TreeItem::get_suffix);
  1307. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  1308. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  1309. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  1310. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  1311. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  1312. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  1313. ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
  1314. ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
  1315. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  1316. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  1317. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  1318. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  1319. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  1320. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  1321. #ifndef DISABLE_DEPRECATED
  1322. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  1323. #endif // DISABLE_DEPRECATED
  1324. ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
  1325. ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
  1326. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  1327. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  1328. ClassDB::bind_method(D_METHOD("set_collapsed_recursive", "enable"), &TreeItem::set_collapsed_recursive);
  1329. ClassDB::bind_method(D_METHOD("is_any_collapsed", "only_visible"), &TreeItem::is_any_collapsed, DEFVAL(false));
  1330. ClassDB::bind_method(D_METHOD("set_visible", "enable"), &TreeItem::set_visible);
  1331. ClassDB::bind_method(D_METHOD("is_visible"), &TreeItem::is_visible);
  1332. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &TreeItem::is_visible_in_tree);
  1333. ClassDB::bind_method(D_METHOD("uncollapse_tree"), &TreeItem::uncollapse_tree);
  1334. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  1335. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  1336. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  1337. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  1338. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  1339. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  1340. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  1341. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  1342. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  1343. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  1344. ClassDB::bind_method(D_METHOD("get_custom_color", "column"), &TreeItem::get_custom_color);
  1345. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  1346. ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
  1347. ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
  1348. ClassDB::bind_method(D_METHOD("set_custom_font_size", "column", "font_size"), &TreeItem::set_custom_font_size);
  1349. ClassDB::bind_method(D_METHOD("get_custom_font_size", "column"), &TreeItem::get_custom_font_size);
  1350. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  1351. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  1352. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  1353. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  1354. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  1355. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "id", "disabled", "tooltip_text"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""));
  1356. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  1357. ClassDB::bind_method(D_METHOD("get_button_tooltip_text", "column", "button_index"), &TreeItem::get_button_tooltip_text);
  1358. ClassDB::bind_method(D_METHOD("get_button_id", "column", "button_index"), &TreeItem::get_button_id);
  1359. ClassDB::bind_method(D_METHOD("get_button_by_id", "column", "id"), &TreeItem::get_button_by_id);
  1360. ClassDB::bind_method(D_METHOD("get_button_color", "column", "id"), &TreeItem::get_button_color);
  1361. ClassDB::bind_method(D_METHOD("get_button", "column", "button_index"), &TreeItem::get_button);
  1362. ClassDB::bind_method(D_METHOD("set_button_tooltip_text", "column", "button_index", "tooltip"), &TreeItem::set_button_tooltip_text);
  1363. ClassDB::bind_method(D_METHOD("set_button", "column", "button_index", "button"), &TreeItem::set_button);
  1364. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_index"), &TreeItem::erase_button);
  1365. ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_index", "disabled"), &TreeItem::set_button_disabled);
  1366. ClassDB::bind_method(D_METHOD("set_button_color", "column", "button_index", "color"), &TreeItem::set_button_color);
  1367. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_index"), &TreeItem::is_button_disabled);
  1368. ClassDB::bind_method(D_METHOD("set_tooltip_text", "column", "tooltip"), &TreeItem::set_tooltip_text);
  1369. ClassDB::bind_method(D_METHOD("get_tooltip_text", "column"), &TreeItem::get_tooltip_text);
  1370. ClassDB::bind_method(D_METHOD("set_text_alignment", "column", "text_alignment"), &TreeItem::set_text_alignment);
  1371. ClassDB::bind_method(D_METHOD("get_text_alignment", "column"), &TreeItem::get_text_alignment);
  1372. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  1373. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  1374. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  1375. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  1376. ClassDB::bind_method(D_METHOD("create_child", "index"), &TreeItem::create_child, DEFVAL(-1));
  1377. ClassDB::bind_method(D_METHOD("add_child", "child"), &TreeItem::add_child);
  1378. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::remove_child);
  1379. ClassDB::bind_method(D_METHOD("get_tree"), &TreeItem::get_tree);
  1380. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  1381. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  1382. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  1383. ClassDB::bind_method(D_METHOD("get_first_child"), &TreeItem::get_first_child);
  1384. ClassDB::bind_method(D_METHOD("get_next_in_tree", "wrap"), &TreeItem::get_next_in_tree, DEFVAL(false));
  1385. ClassDB::bind_method(D_METHOD("get_prev_in_tree", "wrap"), &TreeItem::get_prev_in_tree, DEFVAL(false));
  1386. ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
  1387. ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
  1388. ClassDB::bind_method(D_METHOD("get_child", "index"), &TreeItem::get_child);
  1389. ClassDB::bind_method(D_METHOD("get_child_count"), &TreeItem::get_child_count);
  1390. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  1391. ClassDB::bind_method(D_METHOD("get_index"), &TreeItem::get_index);
  1392. ClassDB::bind_method(D_METHOD("move_before", "item"), &TreeItem::move_before);
  1393. ClassDB::bind_method(D_METHOD("move_after", "item"), &TreeItem::move_after);
  1394. {
  1395. MethodInfo mi;
  1396. mi.name = "call_recursive";
  1397. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1398. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
  1399. }
  1400. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  1401. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1402. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  1403. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  1404. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  1405. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  1406. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  1407. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  1408. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  1409. }
  1410. TreeItem::TreeItem(Tree *p_tree) {
  1411. tree = p_tree;
  1412. }
  1413. TreeItem::~TreeItem() {
  1414. _unlink_from_tree();
  1415. _change_tree(nullptr);
  1416. validate_cache();
  1417. prev = nullptr;
  1418. clear_children();
  1419. }
  1420. /**********************************************/
  1421. /**********************************************/
  1422. /**********************************************/
  1423. /**********************************************/
  1424. /**********************************************/
  1425. /**********************************************/
  1426. void Tree::_update_theme_item_cache() {
  1427. Control::_update_theme_item_cache();
  1428. theme_cache.base_scale = get_theme_default_base_scale();
  1429. }
  1430. Size2 Tree::_get_cell_icon_size(const TreeItem::Cell &p_cell) const {
  1431. Size2i icon_size = p_cell.get_icon_size();
  1432. int max_width = 0;
  1433. if (theme_cache.icon_max_width > 0) {
  1434. max_width = theme_cache.icon_max_width;
  1435. }
  1436. if (p_cell.icon_max_w > 0 && (max_width == 0 || p_cell.icon_max_w < max_width)) {
  1437. max_width = p_cell.icon_max_w;
  1438. }
  1439. if (max_width > 0 && icon_size.width > max_width) {
  1440. icon_size.height = icon_size.height * max_width / icon_size.width;
  1441. icon_size.width = max_width;
  1442. }
  1443. return icon_size;
  1444. }
  1445. int Tree::compute_item_height(TreeItem *p_item) const {
  1446. if ((p_item == root && hide_root) || !p_item->is_visible_in_tree()) {
  1447. return 0;
  1448. }
  1449. ERR_FAIL_COND_V(theme_cache.font.is_null(), 0);
  1450. int height = 0;
  1451. for (int i = 0; i < columns.size(); i++) {
  1452. if (p_item->cells[i].dirty) {
  1453. const_cast<Tree *>(this)->update_item_cell(p_item, i);
  1454. }
  1455. height = MAX(height, p_item->cells[i].text_buf->get_size().y);
  1456. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  1457. Size2i s; // = cache.button_pressed->get_minimum_size();
  1458. s += p_item->cells[i].buttons[j].texture->get_size();
  1459. if (s.height > height) {
  1460. height = s.height;
  1461. }
  1462. }
  1463. switch (p_item->cells[i].mode) {
  1464. case TreeItem::CELL_MODE_CHECK: {
  1465. int check_icon_h = theme_cache.checked->get_height();
  1466. if (height < check_icon_h) {
  1467. height = check_icon_h;
  1468. }
  1469. [[fallthrough]];
  1470. }
  1471. case TreeItem::CELL_MODE_STRING:
  1472. case TreeItem::CELL_MODE_CUSTOM:
  1473. case TreeItem::CELL_MODE_ICON: {
  1474. Ref<Texture2D> icon = p_item->cells[i].icon;
  1475. if (!icon.is_null()) {
  1476. Size2i s = _get_cell_icon_size(p_item->cells[i]);
  1477. if (s.height > height) {
  1478. height = s.height;
  1479. }
  1480. }
  1481. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  1482. height += theme_cache.custom_button->get_minimum_size().height;
  1483. }
  1484. } break;
  1485. default: {
  1486. }
  1487. }
  1488. }
  1489. int item_min_height = MAX(theme_cache.font->get_height(theme_cache.font_size), p_item->get_custom_minimum_height());
  1490. if (height < item_min_height) {
  1491. height = item_min_height;
  1492. }
  1493. height += theme_cache.v_separation;
  1494. return height;
  1495. }
  1496. int Tree::get_item_height(TreeItem *p_item) const {
  1497. if (!p_item->is_visible_in_tree()) {
  1498. return 0;
  1499. }
  1500. int height = compute_item_height(p_item);
  1501. height += theme_cache.v_separation;
  1502. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1503. TreeItem *c = p_item->first_child;
  1504. while (c) {
  1505. height += get_item_height(c);
  1506. c = c->next;
  1507. }
  1508. }
  1509. return height;
  1510. }
  1511. 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) {
  1512. ERR_FAIL_COND(theme_cache.font.is_null());
  1513. Rect2i rect = p_rect.grow_individual(-theme_cache.inner_item_margin_left, -theme_cache.inner_item_margin_top, -theme_cache.inner_item_margin_right, -theme_cache.inner_item_margin_bottom);
  1514. Size2 ts = p_cell.text_buf->get_size();
  1515. bool rtl = is_layout_rtl();
  1516. int w = 0;
  1517. Size2i bmsize;
  1518. if (!p_cell.icon.is_null()) {
  1519. bmsize = _get_cell_icon_size(p_cell);
  1520. w += bmsize.width + theme_cache.h_separation;
  1521. if (rect.size.width > 0 && (w + ts.width) > rect.size.width) {
  1522. ts.width = rect.size.width - w;
  1523. }
  1524. }
  1525. w += ts.width;
  1526. switch (p_cell.text_alignment) {
  1527. case HORIZONTAL_ALIGNMENT_FILL:
  1528. case HORIZONTAL_ALIGNMENT_LEFT: {
  1529. if (rtl) {
  1530. rect.position.x += MAX(0, (rect.size.width - w));
  1531. }
  1532. } break;
  1533. case HORIZONTAL_ALIGNMENT_CENTER:
  1534. rect.position.x += MAX(0, (rect.size.width - w) / 2);
  1535. break;
  1536. case HORIZONTAL_ALIGNMENT_RIGHT:
  1537. if (!rtl) {
  1538. rect.position.x += MAX(0, (rect.size.width - w));
  1539. }
  1540. break;
  1541. }
  1542. RID ci = get_canvas_item();
  1543. if (rtl && rect.size.width > 0) {
  1544. Point2 draw_pos = rect.position;
  1545. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1546. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1547. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1548. }
  1549. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1550. rect.position.x += ts.width + theme_cache.h_separation;
  1551. rect.size.x -= ts.width + theme_cache.h_separation;
  1552. }
  1553. if (!p_cell.icon.is_null()) {
  1554. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
  1555. rect.position.x += bmsize.x + theme_cache.h_separation;
  1556. rect.size.x -= bmsize.x + theme_cache.h_separation;
  1557. }
  1558. if (!rtl && rect.size.width > 0) {
  1559. Point2 draw_pos = rect.position;
  1560. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1561. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1562. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1563. }
  1564. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1565. }
  1566. }
  1567. void Tree::update_column(int p_col) {
  1568. columns.write[p_col].text_buf->clear();
  1569. if (columns[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1570. columns.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1571. } else {
  1572. columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction);
  1573. }
  1574. columns.write[p_col].xl_title = atr(columns[p_col].title);
  1575. columns.write[p_col].text_buf->add_string(columns[p_col].xl_title, theme_cache.tb_font, theme_cache.tb_font_size, columns[p_col].language);
  1576. columns.write[p_col].cached_minimum_width_dirty = true;
  1577. }
  1578. void Tree::update_item_cell(TreeItem *p_item, int p_col) {
  1579. String valtext;
  1580. p_item->cells.write[p_col].text_buf->clear();
  1581. if (p_item->cells[p_col].mode == TreeItem::CELL_MODE_RANGE) {
  1582. if (!p_item->cells[p_col].text.is_empty()) {
  1583. if (!p_item->cells[p_col].editable) {
  1584. return;
  1585. }
  1586. int option = (int)p_item->cells[p_col].val;
  1587. valtext = atr(ETR("(Other)"));
  1588. Vector<String> strings = p_item->cells[p_col].text.split(",");
  1589. for (int j = 0; j < strings.size(); j++) {
  1590. int value = j;
  1591. if (!strings[j].get_slicec(':', 1).is_empty()) {
  1592. value = strings[j].get_slicec(':', 1).to_int();
  1593. }
  1594. if (option == value) {
  1595. valtext = atr(strings[j].get_slicec(':', 0));
  1596. break;
  1597. }
  1598. }
  1599. } else {
  1600. valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
  1601. }
  1602. } else {
  1603. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  1604. if (!p_item->cells[p_col].editable || p_item->cells[p_col].mode != TreeItem::CELL_MODE_STRING) {
  1605. p_item->cells.write[p_col].xl_text = atr(p_item->cells[p_col].text);
  1606. } else {
  1607. p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text;
  1608. }
  1609. valtext = p_item->cells[p_col].xl_text;
  1610. }
  1611. if (!p_item->cells[p_col].suffix.is_empty()) {
  1612. if (!valtext.is_empty()) {
  1613. valtext += " ";
  1614. }
  1615. valtext += p_item->cells[p_col].suffix;
  1616. }
  1617. if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1618. p_item->cells.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1619. } else {
  1620. p_item->cells.write[p_col].text_buf->set_direction((TextServer::Direction)p_item->cells[p_col].text_direction);
  1621. }
  1622. Ref<Font> font;
  1623. if (p_item->cells[p_col].custom_font.is_valid()) {
  1624. font = p_item->cells[p_col].custom_font;
  1625. } else {
  1626. font = theme_cache.font;
  1627. }
  1628. int font_size;
  1629. if (p_item->cells[p_col].custom_font_size > 0) {
  1630. font_size = p_item->cells[p_col].custom_font_size;
  1631. } else {
  1632. font_size = theme_cache.font_size;
  1633. }
  1634. p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].language);
  1635. BitField<TextServer::LineBreakFlag> break_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_EDGE_SPACES;
  1636. switch (p_item->cells.write[p_col].autowrap_mode) {
  1637. case TextServer::AUTOWRAP_OFF:
  1638. break;
  1639. case TextServer::AUTOWRAP_ARBITRARY:
  1640. break_flags.set_flag(TextServer::BREAK_GRAPHEME_BOUND);
  1641. break;
  1642. case TextServer::AUTOWRAP_WORD:
  1643. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1644. break;
  1645. case TextServer::AUTOWRAP_WORD_SMART:
  1646. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1647. break_flags.set_flag(TextServer::BREAK_ADAPTIVE);
  1648. break;
  1649. }
  1650. p_item->cells.write[p_col].text_buf->set_break_flags(break_flags);
  1651. 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));
  1652. p_item->cells.write[p_col].dirty = false;
  1653. }
  1654. void Tree::update_item_cache(TreeItem *p_item) {
  1655. for (int i = 0; i < p_item->cells.size(); i++) {
  1656. update_item_cell(p_item, i);
  1657. }
  1658. TreeItem *c = p_item->first_child;
  1659. while (c) {
  1660. update_item_cache(c);
  1661. c = c->next;
  1662. }
  1663. }
  1664. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height) {
  1665. if (p_pos.y - theme_cache.offset.y > (p_draw_size.height)) {
  1666. return -1; //draw no more!
  1667. }
  1668. if (!p_item->is_visible_in_tree()) {
  1669. return 0;
  1670. }
  1671. RID ci = get_canvas_item();
  1672. int htotal = 0;
  1673. int label_h = 0;
  1674. bool rtl = cache.rtl;
  1675. /* Draw label, if height fits */
  1676. bool skip = (p_item == root && hide_root);
  1677. if (!skip) {
  1678. // Draw separation.
  1679. ERR_FAIL_COND_V(theme_cache.font.is_null(), -1);
  1680. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
  1681. int skip2 = 0;
  1682. for (int i = 0; i < columns.size(); i++) {
  1683. if (skip2) {
  1684. skip2--;
  1685. continue;
  1686. }
  1687. int item_width = get_column_width(i);
  1688. if (i == 0) {
  1689. item_width -= ofs;
  1690. if (item_width <= 0) {
  1691. ofs = get_column_width(0);
  1692. continue;
  1693. }
  1694. } else {
  1695. ofs += theme_cache.h_separation;
  1696. item_width -= theme_cache.h_separation;
  1697. }
  1698. if (p_item->cells[i].expand_right) {
  1699. int plus = 1;
  1700. 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].xl_text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
  1701. item_width += get_column_width(i + plus);
  1702. plus++;
  1703. skip2++;
  1704. }
  1705. }
  1706. if (!rtl && p_item->cells[i].buttons.size()) {
  1707. int buttons_width = 0;
  1708. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1709. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1710. buttons_width += button_texture->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  1711. }
  1712. int total_ofs = ofs - theme_cache.offset.x;
  1713. if (total_ofs + item_width > p_draw_size.width) {
  1714. item_width = MAX(buttons_width, p_draw_size.width - total_ofs);
  1715. }
  1716. }
  1717. int item_width_with_buttons = item_width; // used later for drawing buttons
  1718. int buttons_width = 0;
  1719. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1720. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1721. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  1722. item_width -= button_size.width + theme_cache.button_margin;
  1723. buttons_width += button_size.width + theme_cache.button_margin;
  1724. }
  1725. int text_width = item_width - theme_cache.inner_item_margin_left - theme_cache.inner_item_margin_right;
  1726. if (p_item->cells[i].icon.is_valid()) {
  1727. text_width -= _get_cell_icon_size(p_item->cells[i]).x + theme_cache.h_separation;
  1728. }
  1729. p_item->cells.write[i].text_buf->set_width(text_width);
  1730. r_self_height = compute_item_height(p_item);
  1731. label_h = r_self_height + theme_cache.v_separation;
  1732. if (p_pos.y + label_h - theme_cache.offset.y < 0) {
  1733. continue; // No need to draw.
  1734. }
  1735. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(item_width, label_h));
  1736. Rect2i cell_rect = item_rect;
  1737. if (i != 0) {
  1738. cell_rect.position.x -= theme_cache.h_separation;
  1739. cell_rect.size.x += theme_cache.h_separation;
  1740. }
  1741. if (theme_cache.draw_guides) {
  1742. Rect2 r = cell_rect;
  1743. if (rtl) {
  1744. r.position.x = get_size().width - r.position.x - r.size.x;
  1745. }
  1746. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(r.position.x, r.position.y + r.size.height), r.position + r.size, theme_cache.guide_color, 1);
  1747. }
  1748. if (i == 0) {
  1749. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  1750. const Rect2 content_rect = _get_content_rect();
  1751. Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
  1752. if (rtl) {
  1753. row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
  1754. }
  1755. if (has_focus()) {
  1756. theme_cache.selected_focus->draw(ci, row_rect);
  1757. } else {
  1758. theme_cache.selected->draw(ci, row_rect);
  1759. }
  1760. }
  1761. }
  1762. if ((select_mode == SELECT_ROW && selected_item == p_item) || p_item->cells[i].selected || !p_item->has_meta("__focus_rect")) {
  1763. Rect2i r = cell_rect;
  1764. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  1765. if (select_mode != SELECT_ROW) {
  1766. if (rtl) {
  1767. r.position.x = get_size().width - r.position.x - r.size.x;
  1768. }
  1769. if (p_item->cells[i].selected) {
  1770. if (has_focus()) {
  1771. theme_cache.selected_focus->draw(ci, r);
  1772. } else {
  1773. theme_cache.selected->draw(ci, r);
  1774. }
  1775. }
  1776. }
  1777. }
  1778. if (p_item->cells[i].custom_bg_color) {
  1779. Rect2 r = cell_rect;
  1780. if (i == 0) {
  1781. r.position.x = p_draw_ofs.x;
  1782. r.size.x = item_width + ofs;
  1783. } else {
  1784. r.position.x -= theme_cache.h_separation;
  1785. r.size.x += theme_cache.h_separation;
  1786. }
  1787. if (rtl) {
  1788. r.position.x = get_size().width - r.position.x - r.size.x;
  1789. }
  1790. if (p_item->cells[i].custom_bg_outline) {
  1791. 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);
  1792. 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);
  1793. 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);
  1794. 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);
  1795. } else {
  1796. RenderingServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  1797. }
  1798. }
  1799. if (drop_mode_flags && drop_mode_over) {
  1800. Rect2 r = cell_rect;
  1801. if (rtl) {
  1802. r.position.x = get_size().width - r.position.x - r.size.x;
  1803. }
  1804. if (drop_mode_over == p_item) {
  1805. if (drop_mode_section == 0 || drop_mode_section == -1) {
  1806. // Line above.
  1807. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1808. }
  1809. if (drop_mode_section == 0) {
  1810. // Side lines.
  1811. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
  1812. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
  1813. }
  1814. if (drop_mode_section == 0 || (drop_mode_section == 1 && (!p_item->get_first_child() || p_item->is_collapsed()))) {
  1815. // Line below.
  1816. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), theme_cache.drop_position_color);
  1817. }
  1818. } else if (drop_mode_over == p_item->get_parent()) {
  1819. if (drop_mode_section == 1 && !p_item->get_prev() /* && !drop_mode_over->is_collapsed() */) { // The drop_mode_over shouldn't ever be collapsed in here, otherwise we would be drawing a child of a collapsed item.
  1820. // Line above.
  1821. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1822. }
  1823. }
  1824. }
  1825. Color cell_color;
  1826. if (p_item->cells[i].custom_color) {
  1827. cell_color = p_item->cells[i].color;
  1828. } else {
  1829. cell_color = p_item->cells[i].selected ? theme_cache.font_selected_color : theme_cache.font_color;
  1830. }
  1831. Color font_outline_color = theme_cache.font_outline_color;
  1832. int outline_size = theme_cache.font_outline_size;
  1833. Color icon_col = p_item->cells[i].icon_color;
  1834. if (p_item->cells[i].dirty) {
  1835. const_cast<Tree *>(this)->update_item_cell(p_item, i);
  1836. }
  1837. if (rtl) {
  1838. item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
  1839. }
  1840. Point2i text_pos = item_rect.position;
  1841. text_pos.y += Math::floor(p_draw_ofs.y) - _get_title_button_height();
  1842. switch (p_item->cells[i].mode) {
  1843. case TreeItem::CELL_MODE_STRING: {
  1844. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  1845. } break;
  1846. case TreeItem::CELL_MODE_CHECK: {
  1847. Point2i check_ofs = item_rect.position;
  1848. check_ofs.y += Math::floor((real_t)(item_rect.size.y - theme_cache.checked->get_height()) / 2);
  1849. if (p_item->cells[i].editable) {
  1850. if (p_item->cells[i].indeterminate) {
  1851. theme_cache.indeterminate->draw(ci, check_ofs);
  1852. } else if (p_item->cells[i].checked) {
  1853. theme_cache.checked->draw(ci, check_ofs);
  1854. } else {
  1855. theme_cache.unchecked->draw(ci, check_ofs);
  1856. }
  1857. } else {
  1858. if (p_item->cells[i].indeterminate) {
  1859. theme_cache.indeterminate_disabled->draw(ci, check_ofs);
  1860. } else if (p_item->cells[i].checked) {
  1861. theme_cache.checked_disabled->draw(ci, check_ofs);
  1862. } else {
  1863. theme_cache.unchecked_disabled->draw(ci, check_ofs);
  1864. }
  1865. }
  1866. int check_w = theme_cache.checked->get_width() + theme_cache.h_separation;
  1867. text_pos.x += check_w;
  1868. item_rect.size.x -= check_w;
  1869. item_rect.position.x += check_w;
  1870. if (!p_item->cells[i].editable) {
  1871. cell_color = theme_cache.font_disabled_color;
  1872. }
  1873. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  1874. } break;
  1875. case TreeItem::CELL_MODE_RANGE: {
  1876. if (!p_item->cells[i].text.is_empty()) {
  1877. if (!p_item->cells[i].editable) {
  1878. break;
  1879. }
  1880. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  1881. int cell_width = item_rect.size.x - downarrow->get_width();
  1882. if (rtl) {
  1883. if (outline_size > 0 && font_outline_color.a > 0) {
  1884. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  1885. }
  1886. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  1887. } else {
  1888. if (outline_size > 0 && font_outline_color.a > 0) {
  1889. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  1890. }
  1891. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  1892. }
  1893. Point2i arrow_pos = item_rect.position;
  1894. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  1895. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  1896. downarrow->draw(ci, arrow_pos);
  1897. } else {
  1898. Ref<Texture2D> updown = theme_cache.updown;
  1899. int cell_width = item_rect.size.x - updown->get_width();
  1900. if (rtl) {
  1901. if (outline_size > 0 && font_outline_color.a > 0) {
  1902. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  1903. }
  1904. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  1905. } else {
  1906. if (outline_size > 0 && font_outline_color.a > 0) {
  1907. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  1908. }
  1909. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  1910. }
  1911. if (!p_item->cells[i].editable) {
  1912. break;
  1913. }
  1914. Point2i updown_pos = item_rect.position;
  1915. updown_pos.x += item_rect.size.x - updown->get_width();
  1916. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  1917. updown->draw(ci, updown_pos);
  1918. }
  1919. } break;
  1920. case TreeItem::CELL_MODE_ICON: {
  1921. if (p_item->cells[i].icon.is_null()) {
  1922. break;
  1923. }
  1924. Size2i icon_size = _get_cell_icon_size(p_item->cells[i]);
  1925. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  1926. icon_ofs += item_rect.position;
  1927. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  1928. } break;
  1929. case TreeItem::CELL_MODE_CUSTOM: {
  1930. if (p_item->cells[i].custom_draw_callback.is_valid()) {
  1931. Variant args[] = { p_item, Rect2(item_rect) };
  1932. const Variant *argptrs[] = { &args[0], &args[1] };
  1933. Callable::CallError ce;
  1934. Variant ret;
  1935. p_item->cells[i].custom_draw_callback.callp(argptrs, 2, ret, ce);
  1936. if (ce.error != Callable::CallError::CALL_OK) {
  1937. ERR_PRINT("Error calling custom draw method: " + Variant::get_callable_error_text(p_item->cells[i].custom_draw_callback, argptrs, 2, ce) + ".");
  1938. }
  1939. }
  1940. if (!p_item->cells[i].editable) {
  1941. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  1942. break;
  1943. }
  1944. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  1945. Rect2i ir = item_rect;
  1946. Point2i arrow_pos = item_rect.position;
  1947. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  1948. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  1949. ir.size.width -= downarrow->get_width();
  1950. if (p_item->cells[i].custom_button) {
  1951. if (cache.hover_item == p_item && cache.hover_cell == i) {
  1952. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  1953. draw_style_box(theme_cache.custom_button_pressed, ir);
  1954. } else {
  1955. draw_style_box(theme_cache.custom_button_hover, ir);
  1956. cell_color = theme_cache.custom_button_font_highlight;
  1957. }
  1958. } else {
  1959. draw_style_box(theme_cache.custom_button, ir);
  1960. }
  1961. ir.size -= theme_cache.custom_button->get_minimum_size();
  1962. ir.position += theme_cache.custom_button->get_offset();
  1963. }
  1964. draw_item_rect(p_item->cells.write[i], ir, cell_color, icon_col, outline_size, font_outline_color);
  1965. downarrow->draw(ci, arrow_pos);
  1966. } break;
  1967. }
  1968. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1969. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1970. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  1971. Point2i button_ofs = Point2i(ofs + item_width_with_buttons - button_size.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
  1972. 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) {
  1973. // Being pressed.
  1974. Point2 od = button_ofs;
  1975. if (rtl) {
  1976. od.x = get_size().width - od.x - button_size.x;
  1977. }
  1978. theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  1979. }
  1980. button_ofs.y += (label_h - button_size.height) / 2;
  1981. button_ofs += theme_cache.button_pressed->get_offset();
  1982. if (rtl) {
  1983. button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
  1984. }
  1985. p_item->cells.write[i].buttons.write[j].rect = Rect2i(button_ofs, button_size);
  1986. button_texture->draw(ci, button_ofs, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  1987. item_width_with_buttons -= button_size.width + theme_cache.button_margin;
  1988. }
  1989. if (i == 0) {
  1990. ofs = get_column_width(0);
  1991. } else {
  1992. ofs += item_width + buttons_width;
  1993. }
  1994. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  1995. if (is_layout_rtl()) {
  1996. cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
  1997. }
  1998. if (has_focus()) {
  1999. theme_cache.cursor->draw(ci, cell_rect);
  2000. } else {
  2001. theme_cache.cursor_unfocus->draw(ci, cell_rect);
  2002. }
  2003. }
  2004. }
  2005. if (!p_item->disable_folding && !hide_folding && p_item->first_child && p_item->get_visible_child_count() != 0) { //has visible children, draw the guide box
  2006. Ref<Texture2D> arrow;
  2007. if (p_item->collapsed) {
  2008. if (is_layout_rtl()) {
  2009. arrow = theme_cache.arrow_collapsed_mirrored;
  2010. } else {
  2011. arrow = theme_cache.arrow_collapsed;
  2012. }
  2013. } else {
  2014. arrow = theme_cache.arrow;
  2015. }
  2016. Point2 apos = p_pos + Point2i(0, (label_h - arrow->get_height()) / 2) - theme_cache.offset + p_draw_ofs;
  2017. apos.x += theme_cache.item_margin - arrow->get_width();
  2018. if (rtl) {
  2019. apos.x = get_size().width - apos.x - arrow->get_width();
  2020. }
  2021. arrow->draw(ci, apos);
  2022. }
  2023. }
  2024. Point2 children_pos = p_pos;
  2025. if (!skip) {
  2026. children_pos.x += theme_cache.item_margin;
  2027. htotal += label_h;
  2028. children_pos.y += htotal;
  2029. }
  2030. if (!p_item->collapsed) { /* if not collapsed, check the children */
  2031. TreeItem *c = p_item->first_child;
  2032. int base_ofs = children_pos.y - theme_cache.offset.y + p_draw_ofs.y;
  2033. int prev_ofs = base_ofs;
  2034. int prev_hl_ofs = base_ofs;
  2035. while (c) {
  2036. int child_h = -1;
  2037. int child_self_height = 0;
  2038. if (htotal >= 0) {
  2039. child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c, child_self_height);
  2040. child_self_height += theme_cache.v_separation;
  2041. }
  2042. // Draw relationship lines.
  2043. if (theme_cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root) && c->is_visible_in_tree()) {
  2044. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
  2045. int parent_ofs = p_pos.x + theme_cache.item_margin;
  2046. Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height / 2) - theme_cache.offset + p_draw_ofs;
  2047. if (c->get_visible_child_count() > 0) {
  2048. root_pos -= Point2i(theme_cache.arrow->get_width(), 0);
  2049. }
  2050. float line_width = theme_cache.relationship_line_width * Math::round(theme_cache.base_scale);
  2051. float parent_line_width = theme_cache.parent_hl_line_width * Math::round(theme_cache.base_scale);
  2052. float children_line_width = theme_cache.children_hl_line_width * Math::round(theme_cache.base_scale);
  2053. Point2i parent_pos = Point2i(parent_ofs - theme_cache.arrow->get_width() / 2, p_pos.y + label_h / 2 + theme_cache.arrow->get_height() / 2) - theme_cache.offset + p_draw_ofs;
  2054. int more_prev_ofs = 0;
  2055. if (root_pos.y + line_width >= 0) {
  2056. if (rtl) {
  2057. root_pos.x = get_size().width - root_pos.x;
  2058. parent_pos.x = get_size().width - parent_pos.x;
  2059. }
  2060. // Order of parts on this bend: the horizontal line first, then the vertical line.
  2061. if (_is_branch_selected(c)) {
  2062. // If this item or one of its children is selected, we draw the line using parent highlight style.
  2063. if (htotal >= 0) {
  2064. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), theme_cache.parent_hl_line_color, parent_line_width);
  2065. }
  2066. 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), theme_cache.parent_hl_line_color, parent_line_width);
  2067. more_prev_ofs = theme_cache.parent_hl_line_margin;
  2068. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  2069. } else if (p_item->is_selected(0)) {
  2070. // If parent item is selected (but this item is not), we draw the line using children highlight style.
  2071. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2072. if (_is_sibling_branch_selected(c)) {
  2073. if (htotal >= 0) {
  2074. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(parent_line_width / 2), root_pos.y), theme_cache.children_hl_line_color, children_line_width);
  2075. }
  2076. 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), theme_cache.parent_hl_line_color, parent_line_width);
  2077. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  2078. } else {
  2079. if (htotal >= 0) {
  2080. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(children_line_width / 2), root_pos.y), theme_cache.children_hl_line_color, children_line_width);
  2081. }
  2082. 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)), theme_cache.children_hl_line_color, children_line_width);
  2083. }
  2084. } else {
  2085. // If nothing of the above is true, we draw the line using normal style.
  2086. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2087. if (_is_sibling_branch_selected(c)) {
  2088. if (htotal >= 0) {
  2089. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y), theme_cache.relationship_line_color, line_width);
  2090. }
  2091. 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), theme_cache.parent_hl_line_color, parent_line_width);
  2092. prev_hl_ofs = root_pos.y + Math::floor(parent_line_width / 2);
  2093. } else {
  2094. if (htotal >= 0) {
  2095. RenderingServer::get_singleton()->canvas_item_add_line(ci, root_pos, Point2i(parent_pos.x + Math::floor(line_width / 2), root_pos.y), theme_cache.relationship_line_color, line_width);
  2096. }
  2097. 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)), theme_cache.relationship_line_color, line_width);
  2098. }
  2099. }
  2100. }
  2101. prev_ofs = root_pos.y + more_prev_ofs;
  2102. }
  2103. if (child_h < 0) {
  2104. if (htotal == -1) {
  2105. break; // Last loop done, stop.
  2106. }
  2107. if (theme_cache.draw_relationship_lines == 0) {
  2108. return -1; // No need to draw anymore, full stop.
  2109. }
  2110. htotal = -1;
  2111. children_pos.y = theme_cache.offset.y + p_draw_size.height;
  2112. } else {
  2113. htotal += child_h;
  2114. children_pos.y += child_h;
  2115. }
  2116. c = c->next;
  2117. }
  2118. }
  2119. return htotal;
  2120. }
  2121. int Tree::_count_selected_items(TreeItem *p_from) const {
  2122. int count = 0;
  2123. for (int i = 0; i < columns.size(); i++) {
  2124. if (p_from->is_selected(i)) {
  2125. count++;
  2126. }
  2127. }
  2128. for (TreeItem *c = p_from->get_first_child(); c; c = c->get_next()) {
  2129. count += _count_selected_items(c);
  2130. }
  2131. return count;
  2132. }
  2133. bool Tree::_is_branch_selected(TreeItem *p_from) const {
  2134. for (int i = 0; i < columns.size(); i++) {
  2135. if (p_from->is_selected(i)) {
  2136. return true;
  2137. }
  2138. }
  2139. TreeItem *child_item = p_from->get_first_child();
  2140. while (child_item) {
  2141. if (_is_branch_selected(child_item)) {
  2142. return true;
  2143. }
  2144. child_item = child_item->get_next();
  2145. }
  2146. return false;
  2147. }
  2148. bool Tree::_is_sibling_branch_selected(TreeItem *p_from) const {
  2149. TreeItem *sibling_item = p_from->get_next();
  2150. while (sibling_item) {
  2151. if (_is_branch_selected(sibling_item)) {
  2152. return true;
  2153. }
  2154. sibling_item = sibling_item->get_next();
  2155. }
  2156. return false;
  2157. }
  2158. 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) {
  2159. popup_editor->hide();
  2160. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  2161. bool switched = false;
  2162. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2163. *r_in_range = true;
  2164. switched = true;
  2165. }
  2166. bool emitted_row = false;
  2167. for (int i = 0; i < columns.size(); i++) {
  2168. TreeItem::Cell &c = p_current->cells.write[i];
  2169. if (!c.selectable) {
  2170. continue;
  2171. }
  2172. if (select_mode == SELECT_ROW) {
  2173. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  2174. c.selected = true;
  2175. selected_item = p_selected;
  2176. selected_col = 0;
  2177. if (!emitted_row) {
  2178. emit_signal(SNAME("item_selected"));
  2179. emitted_row = true;
  2180. }
  2181. } else if (c.selected) {
  2182. if (p_selected != p_current) {
  2183. // Deselect other rows.
  2184. c.selected = false;
  2185. }
  2186. }
  2187. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  2188. if (!r_in_range && &selected_cell == &c) {
  2189. if (!selected_cell.selected || allow_reselect) {
  2190. selected_cell.selected = true;
  2191. selected_item = p_selected;
  2192. selected_col = i;
  2193. emit_signal(SNAME("cell_selected"));
  2194. if (select_mode == SELECT_MULTI) {
  2195. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2196. } else if (select_mode == SELECT_SINGLE) {
  2197. emit_signal(SNAME("item_selected"));
  2198. }
  2199. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  2200. selected_item = p_selected;
  2201. selected_col = i;
  2202. emit_signal(SNAME("cell_selected"));
  2203. }
  2204. } else {
  2205. if (r_in_range && *r_in_range && !p_force_deselect) {
  2206. if (!c.selected && c.selectable) {
  2207. c.selected = true;
  2208. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2209. }
  2210. } else if (!r_in_range || p_force_deselect) {
  2211. if (select_mode == SELECT_MULTI && c.selected) {
  2212. emit_signal(SNAME("multi_selected"), p_current, i, false);
  2213. }
  2214. c.selected = false;
  2215. }
  2216. //p_current->deselected_signal.call(p_col);
  2217. }
  2218. }
  2219. }
  2220. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2221. *r_in_range = false;
  2222. }
  2223. TreeItem *c = p_current->first_child;
  2224. while (c) {
  2225. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  2226. c = c->next;
  2227. }
  2228. }
  2229. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  2230. return Rect2();
  2231. }
  2232. void Tree::_range_click_timeout() {
  2233. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  2234. Point2 pos = get_local_mouse_position() - theme_cache.panel_style->get_offset();
  2235. if (show_column_titles) {
  2236. pos.y -= _get_title_button_height();
  2237. if (pos.y < 0) {
  2238. range_click_timer->stop();
  2239. return;
  2240. }
  2241. }
  2242. if (!root) {
  2243. return;
  2244. }
  2245. click_handled = false;
  2246. Ref<InputEventMouseButton> mb;
  2247. mb.instantiate();
  2248. int x_limit = get_size().width - theme_cache.panel_style->get_minimum_size().width;
  2249. if (v_scroll->is_visible()) {
  2250. x_limit -= v_scroll->get_minimum_size().width;
  2251. }
  2252. cache.rtl = is_layout_rtl();
  2253. 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)
  2254. blocked++;
  2255. propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, false, root, MouseButton::LEFT, mb);
  2256. blocked--;
  2257. if (range_click_timer->is_one_shot()) {
  2258. range_click_timer->set_wait_time(0.05);
  2259. range_click_timer->set_one_shot(false);
  2260. range_click_timer->start();
  2261. }
  2262. if (!click_handled) {
  2263. range_click_timer->stop();
  2264. }
  2265. if (propagate_mouse_activated) {
  2266. emit_signal(SNAME("item_activated"));
  2267. propagate_mouse_activated = false;
  2268. }
  2269. } else {
  2270. range_click_timer->stop();
  2271. }
  2272. }
  2273. int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod) {
  2274. if (p_item && !p_item->is_visible_in_tree()) {
  2275. // Skip any processing of invisible items.
  2276. return 0;
  2277. }
  2278. int item_h = compute_item_height(p_item) + theme_cache.v_separation;
  2279. bool skip = (p_item == root && hide_root);
  2280. if (!skip && p_pos.y < item_h) {
  2281. // check event!
  2282. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  2283. return -1;
  2284. }
  2285. if (!p_item->disable_folding && !hide_folding && p_item->first_child && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + theme_cache.item_margin))) {
  2286. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2287. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2288. } else {
  2289. p_item->set_collapsed(!p_item->is_collapsed());
  2290. }
  2291. return -1;
  2292. }
  2293. int x = p_pos.x;
  2294. /* find clicked column */
  2295. int col = -1;
  2296. int col_ofs = 0;
  2297. int col_width = 0;
  2298. int limit_w = x_limit;
  2299. for (int i = 0; i < columns.size(); i++) {
  2300. col_width = get_column_width(i);
  2301. if (p_item->cells[i].expand_right) {
  2302. int plus = 1;
  2303. 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.is_empty() && p_item->cells[i + plus].icon.is_null()) {
  2304. col_width += theme_cache.h_separation;
  2305. col_width += get_column_width(i + plus);
  2306. plus++;
  2307. }
  2308. }
  2309. if (x > col_width) {
  2310. col_ofs += col_width;
  2311. x -= col_width;
  2312. limit_w -= col_width;
  2313. continue;
  2314. }
  2315. col = i;
  2316. break;
  2317. }
  2318. if (col == -1) {
  2319. return -1;
  2320. } else if (col == 0) {
  2321. int margin = x_ofs + theme_cache.item_margin; //-theme_cache.h_separation;
  2322. //int lm = theme_cache.panel_style->get_margin(SIDE_LEFT);
  2323. col_width -= margin;
  2324. limit_w -= margin;
  2325. col_ofs += margin;
  2326. x -= margin;
  2327. } else {
  2328. col_width -= theme_cache.h_separation;
  2329. limit_w -= theme_cache.h_separation;
  2330. x -= theme_cache.h_separation;
  2331. }
  2332. const TreeItem::Cell &c = p_item->cells[col];
  2333. if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) {
  2334. int button_w = 0;
  2335. for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) {
  2336. Ref<Texture2D> b = p_item->cells[col].buttons[j].texture;
  2337. button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  2338. }
  2339. col_width = MAX(button_w, MIN(limit_w, col_width));
  2340. }
  2341. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2342. Ref<Texture2D> b = c.buttons[j].texture;
  2343. int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
  2344. if (x > col_width - w) {
  2345. if (c.buttons[j].disabled) {
  2346. pressed_button = -1;
  2347. cache.click_type = Cache::CLICK_NONE;
  2348. return -1;
  2349. }
  2350. // Make sure the click is correct.
  2351. Point2 click_pos = get_global_mouse_position() - get_global_position();
  2352. if (!get_item_at_position(click_pos)) {
  2353. pressed_button = -1;
  2354. cache.click_type = Cache::CLICK_NONE;
  2355. return -1;
  2356. }
  2357. pressed_button = j;
  2358. cache.click_type = Cache::CLICK_BUTTON;
  2359. cache.click_index = j;
  2360. cache.click_id = c.buttons[j].id;
  2361. cache.click_item = p_item;
  2362. cache.click_column = col;
  2363. cache.click_pos = click_pos;
  2364. queue_redraw();
  2365. return -1;
  2366. }
  2367. col_width -= w + theme_cache.button_margin;
  2368. }
  2369. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
  2370. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2371. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2372. } else {
  2373. p_item->set_collapsed(!p_item->is_collapsed());
  2374. }
  2375. return -1; // Collapse/uncollapse, because nothing can be done with the item.
  2376. }
  2377. bool already_selected = c.selected;
  2378. bool already_cursor = (p_item == selected_item) && col == selected_col;
  2379. if (p_button == MouseButton::LEFT || (p_button == MouseButton::RIGHT && allow_rmb_select)) {
  2380. /* process selection */
  2381. 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
  2382. // Emits the "item_activated" signal.
  2383. propagate_mouse_activated = true;
  2384. incr_search.clear();
  2385. return -1;
  2386. }
  2387. if (c.selectable) {
  2388. if (select_mode == SELECT_MULTI && p_mod->is_command_or_control_pressed()) {
  2389. if (c.selected && p_button == MouseButton::LEFT) {
  2390. p_item->deselect(col);
  2391. emit_signal(SNAME("multi_selected"), p_item, col, false);
  2392. } else {
  2393. p_item->select(col);
  2394. emit_signal(SNAME("multi_selected"), p_item, col, true);
  2395. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2396. }
  2397. } else {
  2398. if (select_mode == SELECT_MULTI && p_mod->is_shift_pressed() && selected_item && selected_item != p_item) {
  2399. bool inrange = false;
  2400. select_single_item(p_item, root, col, selected_item, &inrange);
  2401. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2402. } else {
  2403. int icount = _count_selected_items(root);
  2404. if (select_mode == SELECT_MULTI && icount > 1 && p_button != MouseButton::RIGHT) {
  2405. single_select_defer = p_item;
  2406. single_select_defer_column = col;
  2407. } else {
  2408. if (p_button != MouseButton::RIGHT || !c.selected) {
  2409. select_single_item(p_item, root, col);
  2410. }
  2411. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2412. }
  2413. }
  2414. queue_redraw();
  2415. }
  2416. }
  2417. }
  2418. if (!c.editable) {
  2419. return -1; // if cell is not editable, don't bother
  2420. }
  2421. /* editing */
  2422. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  2423. String editor_text = c.text;
  2424. switch (c.mode) {
  2425. case TreeItem::CELL_MODE_STRING: {
  2426. //nothing in particular
  2427. if (select_mode == SELECT_MULTI && (get_viewport()->get_processed_events_count() == focus_in_id || !already_cursor)) {
  2428. bring_up_editor = false;
  2429. }
  2430. } break;
  2431. case TreeItem::CELL_MODE_CHECK: {
  2432. bring_up_editor = false; //checkboxes are not edited with editor
  2433. if (force_edit_checkbox_only_on_checkbox) {
  2434. if (x < theme_cache.checked->get_width()) {
  2435. p_item->set_checked(col, !c.checked);
  2436. item_edited(col, p_item, p_button);
  2437. }
  2438. } else {
  2439. p_item->set_checked(col, !c.checked);
  2440. item_edited(col, p_item, p_button);
  2441. }
  2442. click_handled = true;
  2443. //p_item->edited_signal.call(col);
  2444. } break;
  2445. case TreeItem::CELL_MODE_RANGE: {
  2446. if (!c.text.is_empty()) {
  2447. //if (x >= (get_column_width(col)-item_h/2)) {
  2448. popup_menu->clear();
  2449. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2450. String s = c.text.get_slicec(',', i);
  2451. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
  2452. }
  2453. popup_menu->set_size(Size2(col_width, 0));
  2454. popup_menu->set_position(get_screen_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - theme_cache.offset);
  2455. popup_menu->popup();
  2456. popup_edited_item = p_item;
  2457. popup_edited_item_col = col;
  2458. //}
  2459. bring_up_editor = false;
  2460. } else {
  2461. if (x >= (col_width - item_h / 2)) {
  2462. /* touching the combo */
  2463. bool up = p_pos.y < (item_h / 2);
  2464. if (p_button == MouseButton::LEFT) {
  2465. if (range_click_timer->get_time_left() == 0) {
  2466. range_item_last = p_item;
  2467. range_up_last = up;
  2468. range_click_timer->set_wait_time(0.6);
  2469. range_click_timer->set_one_shot(true);
  2470. range_click_timer->start();
  2471. } else if (up != range_up_last) {
  2472. return -1; // break. avoid changing direction on mouse held
  2473. }
  2474. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  2475. item_edited(col, p_item, p_button);
  2476. } else if (p_button == MouseButton::RIGHT) {
  2477. p_item->set_range(col, (up ? c.max : c.min));
  2478. item_edited(col, p_item, p_button);
  2479. } else if (p_button == MouseButton::WHEEL_UP) {
  2480. p_item->set_range(col, c.val + c.step);
  2481. item_edited(col, p_item, p_button);
  2482. } else if (p_button == MouseButton::WHEEL_DOWN) {
  2483. p_item->set_range(col, c.val - c.step);
  2484. item_edited(col, p_item, p_button);
  2485. }
  2486. //p_item->edited_signal.call(col);
  2487. bring_up_editor = false;
  2488. } else {
  2489. editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
  2490. if (select_mode == SELECT_MULTI && get_viewport()->get_processed_events_count() == focus_in_id) {
  2491. bring_up_editor = false;
  2492. }
  2493. }
  2494. }
  2495. click_handled = true;
  2496. } break;
  2497. case TreeItem::CELL_MODE_ICON: {
  2498. bring_up_editor = false;
  2499. } break;
  2500. case TreeItem::CELL_MODE_CUSTOM: {
  2501. edited_item = p_item;
  2502. edited_col = col;
  2503. bool on_arrow = x > col_width - theme_cache.select_arrow->get_width();
  2504. custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - theme_cache.offset.y), Size2(get_column_width(col), item_h));
  2505. if (on_arrow || !p_item->cells[col].custom_button) {
  2506. emit_signal(SNAME("custom_popup_edited"), ((bool)(x >= (col_width - item_h / 2))));
  2507. }
  2508. if (!p_item->cells[col].custom_button || !on_arrow) {
  2509. item_edited(col, p_item, p_button);
  2510. }
  2511. click_handled = true;
  2512. return -1;
  2513. } break;
  2514. };
  2515. if (!bring_up_editor || p_button != MouseButton::LEFT) {
  2516. return -1;
  2517. }
  2518. click_handled = true;
  2519. popup_pressing_edited_item = p_item;
  2520. popup_pressing_edited_item_column = col;
  2521. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - theme_cache.offset, Size2(col_width, item_h));
  2522. pressing_for_editor_text = editor_text;
  2523. pressing_for_editor = true;
  2524. return -1; //select
  2525. } else {
  2526. Point2i new_pos = p_pos;
  2527. if (!skip) {
  2528. x_ofs += theme_cache.item_margin;
  2529. //new_pos.x-=theme_cache.item_margin;
  2530. y_ofs += item_h;
  2531. new_pos.y -= item_h;
  2532. }
  2533. if (!p_item->collapsed) { /* if not collapsed, check the children */
  2534. TreeItem *c = p_item->first_child;
  2535. while (c) {
  2536. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, x_limit, p_double_click, c, p_button, p_mod);
  2537. if (child_h < 0) {
  2538. return -1; // break, stop propagating, no need to anymore
  2539. }
  2540. new_pos.y -= child_h;
  2541. y_ofs += child_h;
  2542. c = c->next;
  2543. item_h += child_h;
  2544. }
  2545. }
  2546. if (p_item == root) {
  2547. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), p_button);
  2548. }
  2549. }
  2550. return item_h; // nothing found
  2551. }
  2552. void Tree::_text_editor_popup_modal_close() {
  2553. if (Input::get_singleton()->is_key_pressed(Key::ESCAPE) ||
  2554. Input::get_singleton()->is_key_pressed(Key::KP_ENTER) ||
  2555. Input::get_singleton()->is_key_pressed(Key::ENTER)) {
  2556. return;
  2557. }
  2558. if (value_editor->has_point(value_editor->get_local_mouse_position())) {
  2559. return;
  2560. }
  2561. if (!popup_edited_item) {
  2562. return;
  2563. }
  2564. if (popup_edited_item->is_edit_multiline(popup_edited_item_col) && popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_STRING) {
  2565. _apply_multiline_edit();
  2566. } else {
  2567. _line_editor_submit(line_editor->get_text());
  2568. }
  2569. }
  2570. void Tree::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  2571. if (p_event->is_action_pressed("ui_text_newline_blank", true)) {
  2572. accept_event();
  2573. } else if (p_event->is_action_pressed("ui_text_newline")) {
  2574. popup_editor->hide();
  2575. _apply_multiline_edit();
  2576. accept_event();
  2577. }
  2578. }
  2579. void Tree::_apply_multiline_edit() {
  2580. if (!popup_edited_item) {
  2581. return;
  2582. }
  2583. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2584. return;
  2585. }
  2586. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2587. switch (c.mode) {
  2588. case TreeItem::CELL_MODE_STRING: {
  2589. c.text = text_editor->get_text();
  2590. } break;
  2591. default: {
  2592. ERR_FAIL();
  2593. }
  2594. }
  2595. item_edited(popup_edited_item_col, popup_edited_item);
  2596. queue_redraw();
  2597. }
  2598. void Tree::_line_editor_submit(String p_text) {
  2599. popup_editor->hide();
  2600. if (!popup_edited_item) {
  2601. return;
  2602. }
  2603. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2604. return;
  2605. }
  2606. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2607. switch (c.mode) {
  2608. case TreeItem::CELL_MODE_STRING: {
  2609. c.text = p_text;
  2610. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2611. } break;
  2612. case TreeItem::CELL_MODE_RANGE: {
  2613. c.val = p_text.to_float();
  2614. if (c.step > 0) {
  2615. c.val = Math::snapped(c.val, c.step);
  2616. }
  2617. if (c.val < c.min) {
  2618. c.val = c.min;
  2619. } else if (c.val > c.max) {
  2620. c.val = c.max;
  2621. }
  2622. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2623. } break;
  2624. default: {
  2625. ERR_FAIL();
  2626. }
  2627. }
  2628. item_edited(popup_edited_item_col, popup_edited_item);
  2629. queue_redraw();
  2630. }
  2631. void Tree::value_editor_changed(double p_value) {
  2632. if (updating_value_editor) {
  2633. return;
  2634. }
  2635. if (!popup_edited_item) {
  2636. return;
  2637. }
  2638. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2639. c.val = p_value;
  2640. line_editor->set_text(String::num(c.val, Math::range_step_decimals(c.step)));
  2641. item_edited(popup_edited_item_col, popup_edited_item);
  2642. queue_redraw();
  2643. }
  2644. void Tree::popup_select(int p_option) {
  2645. if (!popup_edited_item) {
  2646. return;
  2647. }
  2648. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2649. return;
  2650. }
  2651. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  2652. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  2653. queue_redraw();
  2654. item_edited(popup_edited_item_col, popup_edited_item);
  2655. }
  2656. void Tree::_go_left() {
  2657. if (selected_col == 0) {
  2658. if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  2659. selected_item->set_collapsed(true);
  2660. } else {
  2661. if (columns.size() == 1) { // goto parent with one column
  2662. TreeItem *parent = selected_item->get_parent();
  2663. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  2664. select_single_item(parent, get_root(), selected_col);
  2665. }
  2666. } else if (selected_item->get_prev_visible()) {
  2667. selected_col = columns.size() - 1;
  2668. _go_up(); // go to upper column if possible
  2669. }
  2670. }
  2671. } else {
  2672. if (select_mode == SELECT_MULTI) {
  2673. selected_col--;
  2674. emit_signal(SNAME("cell_selected"));
  2675. } else {
  2676. selected_item->select(selected_col - 1);
  2677. }
  2678. }
  2679. queue_redraw();
  2680. accept_event();
  2681. ensure_cursor_is_visible();
  2682. }
  2683. void Tree::_go_right() {
  2684. if (selected_col == (columns.size() - 1)) {
  2685. if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  2686. selected_item->set_collapsed(false);
  2687. } else if (selected_item->get_next_visible()) {
  2688. selected_col = 0;
  2689. _go_down();
  2690. }
  2691. } else {
  2692. if (select_mode == SELECT_MULTI) {
  2693. selected_col++;
  2694. emit_signal(SNAME("cell_selected"));
  2695. } else {
  2696. selected_item->select(selected_col + 1);
  2697. }
  2698. }
  2699. queue_redraw();
  2700. ensure_cursor_is_visible();
  2701. accept_event();
  2702. }
  2703. void Tree::_go_up() {
  2704. TreeItem *prev = nullptr;
  2705. if (!selected_item) {
  2706. prev = get_last_item();
  2707. selected_col = 0;
  2708. } else {
  2709. prev = selected_item->get_prev_visible();
  2710. }
  2711. if (select_mode == SELECT_MULTI) {
  2712. if (!prev) {
  2713. return;
  2714. }
  2715. select_single_item(prev, get_root(), selected_col);
  2716. queue_redraw();
  2717. } else {
  2718. int col = selected_col < 0 ? 0 : selected_col;
  2719. while (prev && !prev->cells[col].selectable) {
  2720. prev = prev->get_prev_visible();
  2721. }
  2722. if (!prev) {
  2723. return; // do nothing..
  2724. }
  2725. prev->select(col);
  2726. }
  2727. ensure_cursor_is_visible();
  2728. accept_event();
  2729. }
  2730. void Tree::_go_down() {
  2731. TreeItem *next = nullptr;
  2732. if (!selected_item) {
  2733. if (root) {
  2734. next = hide_root ? root->get_next_visible() : root;
  2735. }
  2736. } else {
  2737. next = selected_item->get_next_visible();
  2738. }
  2739. if (select_mode == SELECT_MULTI) {
  2740. if (!next) {
  2741. return;
  2742. }
  2743. select_single_item(next, get_root(), selected_col);
  2744. queue_redraw();
  2745. } else {
  2746. int col = selected_col < 0 ? 0 : selected_col;
  2747. while (next && !next->cells[col].selectable) {
  2748. next = next->get_next_visible();
  2749. }
  2750. if (!next) {
  2751. return; // do nothing..
  2752. }
  2753. next->select(col);
  2754. }
  2755. ensure_cursor_is_visible();
  2756. accept_event();
  2757. }
  2758. bool Tree::_scroll(bool p_horizontal, float p_pages) {
  2759. ScrollBar *scroll = p_horizontal ? (ScrollBar *)h_scroll : (ScrollBar *)v_scroll;
  2760. double prev_value = scroll->get_value();
  2761. scroll->set_value(scroll->get_value() + scroll->get_page() * p_pages);
  2762. return scroll->get_value() != prev_value;
  2763. }
  2764. Rect2 Tree::_get_scrollbar_layout_rect() const {
  2765. const Size2 control_size = get_size();
  2766. const Ref<StyleBox> background = theme_cache.panel_style;
  2767. // This is the background stylebox's content rect.
  2768. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  2769. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  2770. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  2771. // Use the stylebox's margins by default. Can be overridden by `scrollbar_margin_*`.
  2772. const real_t top = theme_cache.scrollbar_margin_top < 0 ? content_rect.get_position().y : theme_cache.scrollbar_margin_top;
  2773. const real_t right = theme_cache.scrollbar_margin_right < 0 ? content_rect.get_end().x : (control_size.x - theme_cache.scrollbar_margin_right);
  2774. const real_t bottom = theme_cache.scrollbar_margin_bottom < 0 ? content_rect.get_end().y : (control_size.y - theme_cache.scrollbar_margin_bottom);
  2775. const real_t left = theme_cache.scrollbar_margin_left < 0 ? content_rect.get_position().x : theme_cache.scrollbar_margin_left;
  2776. return Rect2(left, top, right - left, bottom - top);
  2777. }
  2778. Rect2 Tree::_get_content_rect() const {
  2779. const Size2 control_size = get_size();
  2780. const Ref<StyleBox> background = theme_cache.panel_style;
  2781. // This is the background stylebox's content rect.
  2782. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  2783. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  2784. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  2785. // Scrollbars won't affect Tree's content rect if they're not visible or placed inside the stylebox margin area.
  2786. const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_combined_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;
  2787. const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_combined_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;
  2788. const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);
  2789. const Size2 offset = (content_rect.get_end() - scroll_begin).max(Vector2(0, 0));
  2790. return content_rect.grow_individual(0, 0, -offset.x, -offset.y);
  2791. }
  2792. void Tree::gui_input(const Ref<InputEvent> &p_event) {
  2793. ERR_FAIL_COND(p_event.is_null());
  2794. Ref<InputEventKey> k = p_event;
  2795. bool is_command = k.is_valid() && k->is_command_or_control_pressed();
  2796. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  2797. if (!cursor_can_exit_tree) {
  2798. accept_event();
  2799. }
  2800. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  2801. return;
  2802. }
  2803. if (k.is_valid() && k->is_shift_pressed()) {
  2804. selected_item->set_collapsed_recursive(false);
  2805. } else {
  2806. _go_right();
  2807. }
  2808. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  2809. if (!cursor_can_exit_tree) {
  2810. accept_event();
  2811. }
  2812. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  2813. return;
  2814. }
  2815. if (k.is_valid() && k->is_shift_pressed()) {
  2816. selected_item->set_collapsed_recursive(true);
  2817. } else {
  2818. _go_left();
  2819. }
  2820. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  2821. if (!cursor_can_exit_tree) {
  2822. accept_event();
  2823. }
  2824. _go_up();
  2825. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  2826. if (!cursor_can_exit_tree) {
  2827. accept_event();
  2828. }
  2829. _go_down();
  2830. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  2831. if (!cursor_can_exit_tree) {
  2832. accept_event();
  2833. }
  2834. TreeItem *next = nullptr;
  2835. if (!selected_item) {
  2836. return;
  2837. }
  2838. next = selected_item;
  2839. for (int i = 0; i < 10; i++) {
  2840. TreeItem *_n = next->get_next_visible();
  2841. if (_n) {
  2842. next = _n;
  2843. } else {
  2844. break;
  2845. }
  2846. }
  2847. if (next == selected_item) {
  2848. return;
  2849. }
  2850. if (select_mode == SELECT_MULTI) {
  2851. selected_item = next;
  2852. emit_signal(SNAME("cell_selected"));
  2853. queue_redraw();
  2854. } else {
  2855. while (next && !next->cells[selected_col].selectable) {
  2856. next = next->get_next_visible();
  2857. }
  2858. if (!next) {
  2859. return; // do nothing..
  2860. }
  2861. next->select(selected_col);
  2862. }
  2863. ensure_cursor_is_visible();
  2864. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  2865. if (!cursor_can_exit_tree) {
  2866. accept_event();
  2867. }
  2868. TreeItem *prev = nullptr;
  2869. if (!selected_item) {
  2870. return;
  2871. }
  2872. prev = selected_item;
  2873. for (int i = 0; i < 10; i++) {
  2874. TreeItem *_n = prev->get_prev_visible();
  2875. if (_n) {
  2876. prev = _n;
  2877. } else {
  2878. break;
  2879. }
  2880. }
  2881. if (prev == selected_item) {
  2882. return;
  2883. }
  2884. if (select_mode == SELECT_MULTI) {
  2885. selected_item = prev;
  2886. emit_signal(SNAME("cell_selected"));
  2887. queue_redraw();
  2888. } else {
  2889. while (prev && !prev->cells[selected_col].selectable) {
  2890. prev = prev->get_prev_visible();
  2891. }
  2892. if (!prev) {
  2893. return; // do nothing..
  2894. }
  2895. prev->select(selected_col);
  2896. }
  2897. ensure_cursor_is_visible();
  2898. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  2899. if (selected_item) {
  2900. //bring up editor if possible
  2901. if (!edit_selected()) {
  2902. emit_signal(SNAME("item_activated"));
  2903. incr_search.clear();
  2904. }
  2905. }
  2906. accept_event();
  2907. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  2908. if (select_mode == SELECT_MULTI) {
  2909. if (!selected_item) {
  2910. return;
  2911. }
  2912. if (selected_item->is_selected(selected_col)) {
  2913. selected_item->deselect(selected_col);
  2914. emit_signal(SNAME("multi_selected"), selected_item, selected_col, false);
  2915. } else if (selected_item->is_selectable(selected_col)) {
  2916. selected_item->select(selected_col);
  2917. emit_signal(SNAME("multi_selected"), selected_item, selected_col, true);
  2918. }
  2919. }
  2920. accept_event();
  2921. }
  2922. if (allow_search && k.is_valid()) { // Incremental search
  2923. if (!k->is_pressed()) {
  2924. return;
  2925. }
  2926. if (k->is_command_or_control_pressed() || (k->is_shift_pressed() && k->get_unicode() == 0) || k->is_meta_pressed()) {
  2927. return;
  2928. }
  2929. if (!root) {
  2930. return;
  2931. }
  2932. if (hide_root && !root->get_next_visible()) {
  2933. return;
  2934. }
  2935. if (k->get_unicode() > 0) {
  2936. _do_incr_search(String::chr(k->get_unicode()));
  2937. accept_event();
  2938. return;
  2939. } else {
  2940. if (k->get_keycode() != Key::SHIFT) {
  2941. last_keypress = 0;
  2942. }
  2943. }
  2944. }
  2945. Ref<InputEventMouseMotion> mm = p_event;
  2946. if (mm.is_valid()) {
  2947. Ref<StyleBox> bg = theme_cache.panel_style;
  2948. bool rtl = is_layout_rtl();
  2949. Point2 pos = mm->get_position();
  2950. if (rtl) {
  2951. pos.x = get_size().width - pos.x;
  2952. }
  2953. pos -= theme_cache.panel_style->get_offset();
  2954. Cache::ClickType old_hover = cache.hover_type;
  2955. int old_index = cache.hover_index;
  2956. cache.hover_type = Cache::CLICK_NONE;
  2957. cache.hover_index = 0;
  2958. if (show_column_titles) {
  2959. pos.y -= _get_title_button_height();
  2960. if (pos.y < 0) {
  2961. pos.x += theme_cache.offset.x;
  2962. int len = 0;
  2963. for (int i = 0; i < columns.size(); i++) {
  2964. len += get_column_width(i);
  2965. if (pos.x < len) {
  2966. cache.hover_type = Cache::CLICK_TITLE;
  2967. cache.hover_index = i;
  2968. break;
  2969. }
  2970. }
  2971. }
  2972. }
  2973. if (root) {
  2974. Point2 mpos = mm->get_position();
  2975. if (rtl) {
  2976. mpos.x = get_size().width - mpos.x;
  2977. }
  2978. mpos -= theme_cache.panel_style->get_offset();
  2979. mpos.y -= _get_title_button_height();
  2980. if (mpos.y >= 0) {
  2981. if (h_scroll->is_visible_in_tree()) {
  2982. mpos.x += h_scroll->get_value();
  2983. }
  2984. if (v_scroll->is_visible_in_tree()) {
  2985. mpos.y += v_scroll->get_value();
  2986. }
  2987. TreeItem *old_it = cache.hover_item;
  2988. int old_col = cache.hover_cell;
  2989. int col, h, section;
  2990. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  2991. if (drop_mode_flags) {
  2992. if (it != drop_mode_over) {
  2993. drop_mode_over = it;
  2994. queue_redraw();
  2995. }
  2996. if (it && section != drop_mode_section) {
  2997. drop_mode_section = section;
  2998. queue_redraw();
  2999. }
  3000. }
  3001. cache.hover_item = it;
  3002. cache.hover_cell = col;
  3003. if (it != old_it || col != old_col) {
  3004. if (old_it && old_col >= old_it->cells.size()) {
  3005. // Columns may have changed since last redraw().
  3006. queue_redraw();
  3007. } else {
  3008. // Only need to update if mouse enters/exits a button
  3009. bool was_over_button = old_it && old_it->cells[old_col].custom_button;
  3010. bool is_over_button = it && it->cells[col].custom_button;
  3011. if (was_over_button || is_over_button) {
  3012. queue_redraw();
  3013. }
  3014. }
  3015. }
  3016. }
  3017. }
  3018. // Update if mouse enters/exits columns
  3019. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  3020. queue_redraw();
  3021. }
  3022. if (pressing_for_editor && popup_pressing_edited_item && (popup_pressing_edited_item->get_cell_mode(popup_pressing_edited_item_column) == TreeItem::CELL_MODE_RANGE)) {
  3023. /* This needs to happen now, because the popup can be closed when pressing another item, and must remain the popup edited item until it actually closes */
  3024. popup_edited_item = popup_pressing_edited_item;
  3025. popup_edited_item_col = popup_pressing_edited_item_column;
  3026. popup_pressing_edited_item = nullptr;
  3027. popup_pressing_edited_item_column = -1;
  3028. if (!range_drag_enabled) {
  3029. //range drag
  3030. Vector2 cpos = mm->get_position();
  3031. if (rtl) {
  3032. cpos.x = get_size().width - cpos.x;
  3033. }
  3034. if (cpos.distance_to(pressing_pos) > 2) {
  3035. range_drag_enabled = true;
  3036. range_drag_capture_pos = cpos;
  3037. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  3038. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  3039. }
  3040. } else {
  3041. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  3042. float diff_y = -mm->get_relative().y;
  3043. diff_y = Math::pow(ABS(diff_y), 1.8f) * SIGN(diff_y);
  3044. diff_y *= 0.1;
  3045. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  3046. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  3047. item_edited(popup_edited_item_col, popup_edited_item);
  3048. }
  3049. }
  3050. if (drag_touching && !drag_touching_deaccel) {
  3051. drag_accum -= mm->get_relative().y;
  3052. v_scroll->set_value(drag_from + drag_accum);
  3053. drag_speed = -mm->get_velocity().y;
  3054. }
  3055. }
  3056. Ref<InputEventMouseButton> mb = p_event;
  3057. if (mb.is_valid()) {
  3058. bool rtl = is_layout_rtl();
  3059. if (!mb->is_pressed()) {
  3060. if (mb->get_button_index() == MouseButton::LEFT ||
  3061. mb->get_button_index() == MouseButton::RIGHT) {
  3062. Point2 pos = mb->get_position();
  3063. if (rtl) {
  3064. pos.x = get_size().width - pos.x;
  3065. }
  3066. pos -= theme_cache.panel_style->get_offset();
  3067. if (show_column_titles) {
  3068. pos.y -= _get_title_button_height();
  3069. if (pos.y < 0) {
  3070. pos.x += theme_cache.offset.x;
  3071. int len = 0;
  3072. for (int i = 0; i < columns.size(); i++) {
  3073. len += get_column_width(i);
  3074. if (pos.x < static_cast<real_t>(len)) {
  3075. emit_signal(SNAME("column_title_clicked"), i, mb->get_button_index());
  3076. break;
  3077. }
  3078. }
  3079. }
  3080. }
  3081. }
  3082. if (mb->get_button_index() == MouseButton::LEFT) {
  3083. if (single_select_defer) {
  3084. select_single_item(single_select_defer, root, single_select_defer_column);
  3085. single_select_defer = nullptr;
  3086. }
  3087. range_click_timer->stop();
  3088. if (pressing_for_editor) {
  3089. if (range_drag_enabled) {
  3090. range_drag_enabled = false;
  3091. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  3092. warp_mouse(range_drag_capture_pos);
  3093. } else {
  3094. Rect2 rect = get_selected()->get_meta("__focus_rect");
  3095. Point2 mpos = mb->get_position();
  3096. int icon_size_x = 0;
  3097. Ref<Texture2D> icon = get_selected()->get_icon(selected_col);
  3098. if (icon.is_valid()) {
  3099. Rect2i icon_region = get_selected()->get_icon_region(selected_col);
  3100. if (icon_region == Rect2i()) {
  3101. icon_size_x = icon->get_width();
  3102. } else {
  3103. icon_size_x = icon_region.size.width;
  3104. }
  3105. }
  3106. // Icon is treated as if it is outside of the rect so that double clicking on it will emit the item_icon_double_clicked signal.
  3107. if (rtl) {
  3108. mpos.x = get_size().width - (mpos.x + icon_size_x);
  3109. } else {
  3110. mpos.x -= icon_size_x;
  3111. }
  3112. if (rect.has_point(mpos)) {
  3113. if (!edit_selected()) {
  3114. emit_signal(SNAME("item_icon_double_clicked"));
  3115. }
  3116. } else {
  3117. emit_signal(SNAME("item_icon_double_clicked"));
  3118. }
  3119. }
  3120. pressing_for_editor = false;
  3121. }
  3122. if (drag_touching) {
  3123. if (drag_speed == 0) {
  3124. drag_touching_deaccel = false;
  3125. drag_touching = false;
  3126. set_physics_process_internal(false);
  3127. } else {
  3128. drag_touching_deaccel = true;
  3129. }
  3130. }
  3131. }
  3132. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != nullptr) {
  3133. // make sure in case of wrong reference after reconstructing whole TreeItems
  3134. cache.click_item = get_item_at_position(cache.click_pos);
  3135. emit_signal("button_clicked", cache.click_item, cache.click_column, cache.click_id, mb->get_button_index());
  3136. }
  3137. cache.click_type = Cache::CLICK_NONE;
  3138. cache.click_index = -1;
  3139. cache.click_id = -1;
  3140. cache.click_item = nullptr;
  3141. cache.click_column = 0;
  3142. queue_redraw();
  3143. return;
  3144. }
  3145. if (range_drag_enabled) {
  3146. return;
  3147. }
  3148. switch (mb->get_button_index()) {
  3149. case MouseButton::RIGHT:
  3150. case MouseButton::LEFT: {
  3151. Ref<StyleBox> bg = theme_cache.panel_style;
  3152. Point2 pos = mb->get_position();
  3153. if (rtl) {
  3154. pos.x = get_size().width - pos.x;
  3155. }
  3156. pos -= bg->get_offset();
  3157. cache.click_type = Cache::CLICK_NONE;
  3158. if (show_column_titles) {
  3159. pos.y -= _get_title_button_height();
  3160. if (pos.y < 0) {
  3161. pos.x += theme_cache.offset.x;
  3162. int len = 0;
  3163. for (int i = 0; i < columns.size(); i++) {
  3164. len += get_column_width(i);
  3165. if (pos.x < static_cast<real_t>(len)) {
  3166. cache.click_type = Cache::CLICK_TITLE;
  3167. cache.click_index = i;
  3168. queue_redraw();
  3169. break;
  3170. }
  3171. }
  3172. break;
  3173. }
  3174. }
  3175. if (!root || (!root->get_first_child() && hide_root)) {
  3176. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
  3177. break;
  3178. }
  3179. click_handled = false;
  3180. pressing_for_editor = false;
  3181. propagate_mouse_activated = false;
  3182. int x_limit = get_size().width - theme_cache.panel_style->get_minimum_size().width;
  3183. if (v_scroll->is_visible()) {
  3184. x_limit -= v_scroll->get_minimum_size().width;
  3185. }
  3186. cache.rtl = is_layout_rtl();
  3187. blocked++;
  3188. propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, mb->is_double_click(), root, mb->get_button_index(), mb);
  3189. blocked--;
  3190. if (pressing_for_editor) {
  3191. pressing_pos = mb->get_position();
  3192. if (rtl) {
  3193. pressing_pos.x = get_size().width - pressing_pos.x;
  3194. }
  3195. }
  3196. if (mb->get_button_index() == MouseButton::RIGHT) {
  3197. break;
  3198. }
  3199. if (drag_touching) {
  3200. set_physics_process_internal(false);
  3201. drag_touching_deaccel = false;
  3202. drag_touching = false;
  3203. drag_speed = 0;
  3204. drag_from = 0;
  3205. }
  3206. if (!click_handled) {
  3207. drag_speed = 0;
  3208. drag_accum = 0;
  3209. //last_drag_accum=0;
  3210. drag_from = v_scroll->get_value();
  3211. drag_touching = DisplayServer::get_singleton()->is_touchscreen_available();
  3212. drag_touching_deaccel = false;
  3213. if (drag_touching) {
  3214. set_physics_process_internal(true);
  3215. }
  3216. if (mb->get_button_index() == MouseButton::LEFT) {
  3217. if (get_item_at_position(mb->get_position()) == nullptr && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) {
  3218. emit_signal(SNAME("nothing_selected"));
  3219. }
  3220. }
  3221. }
  3222. if (propagate_mouse_activated) {
  3223. emit_signal(SNAME("item_activated"));
  3224. propagate_mouse_activated = false;
  3225. }
  3226. } break;
  3227. case MouseButton::WHEEL_UP: {
  3228. if (_scroll(false, -mb->get_factor() / 8)) {
  3229. accept_event();
  3230. }
  3231. } break;
  3232. case MouseButton::WHEEL_DOWN: {
  3233. if (_scroll(false, mb->get_factor() / 8)) {
  3234. accept_event();
  3235. }
  3236. } break;
  3237. case MouseButton::WHEEL_LEFT: {
  3238. if (_scroll(true, -mb->get_factor() / 8)) {
  3239. accept_event();
  3240. }
  3241. } break;
  3242. case MouseButton::WHEEL_RIGHT: {
  3243. if (_scroll(true, mb->get_factor() / 8)) {
  3244. accept_event();
  3245. }
  3246. } break;
  3247. default:
  3248. break;
  3249. }
  3250. }
  3251. Ref<InputEventPanGesture> pan_gesture = p_event;
  3252. if (pan_gesture.is_valid()) {
  3253. double prev_v = v_scroll->get_value();
  3254. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  3255. double prev_h = h_scroll->get_value();
  3256. if (is_layout_rtl()) {
  3257. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * -pan_gesture->get_delta().x / 8);
  3258. } else {
  3259. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  3260. }
  3261. if (v_scroll->get_value() != prev_v || h_scroll->get_value() != prev_h) {
  3262. accept_event();
  3263. }
  3264. }
  3265. }
  3266. bool Tree::edit_selected(bool p_force_edit) {
  3267. TreeItem *s = get_selected();
  3268. ERR_FAIL_NULL_V_MSG(s, false, "No item selected.");
  3269. ensure_cursor_is_visible();
  3270. int col = get_selected_column();
  3271. ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
  3272. if (!s->cells[col].editable && !p_force_edit) {
  3273. return false;
  3274. }
  3275. Rect2 rect = s->get_meta("__focus_rect");
  3276. popup_edited_item = s;
  3277. popup_edited_item_col = col;
  3278. const TreeItem::Cell &c = s->cells[col];
  3279. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  3280. s->set_checked(col, !c.checked);
  3281. item_edited(col, s);
  3282. return true;
  3283. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  3284. edited_item = s;
  3285. edited_col = col;
  3286. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  3287. emit_signal(SNAME("custom_popup_edited"), false);
  3288. item_edited(col, s);
  3289. return true;
  3290. } else if (c.mode == TreeItem::CELL_MODE_RANGE && !c.text.is_empty()) {
  3291. popup_menu->clear();
  3292. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  3293. String s2 = c.text.get_slicec(',', i);
  3294. popup_menu->add_item(s2.get_slicec(':', 0), s2.get_slicec(':', 1).is_empty() ? i : s2.get_slicec(':', 1).to_int());
  3295. }
  3296. popup_menu->set_size(Size2(rect.size.width, 0));
  3297. popup_menu->set_position(get_screen_position() + rect.position + Point2i(0, rect.size.height));
  3298. popup_menu->popup();
  3299. popup_edited_item = s;
  3300. popup_edited_item_col = col;
  3301. return true;
  3302. } else if ((c.mode == TreeItem::CELL_MODE_STRING && !c.edit_multiline) || c.mode == TreeItem::CELL_MODE_RANGE) {
  3303. Rect2 popup_rect;
  3304. int value_editor_height = c.mode == TreeItem::CELL_MODE_RANGE ? value_editor->get_minimum_size().height : 0;
  3305. // "floor()" centers vertically.
  3306. Vector2 ofs(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height - value_editor_height) - rect.size.height) / 2));
  3307. popup_rect.position = get_screen_position() + rect.position - ofs;
  3308. popup_rect.size = rect.size;
  3309. // Account for icon.
  3310. Size2 icon_size = _get_cell_icon_size(c);
  3311. popup_rect.position.x += icon_size.x;
  3312. popup_rect.size.x -= icon_size.x;
  3313. line_editor->clear();
  3314. line_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
  3315. line_editor->select_all();
  3316. line_editor->show();
  3317. text_editor->hide();
  3318. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  3319. popup_rect.size.y += value_editor_height;
  3320. value_editor->show();
  3321. updating_value_editor = true;
  3322. value_editor->set_min(c.min);
  3323. value_editor->set_max(c.max);
  3324. value_editor->set_step(c.step);
  3325. value_editor->set_value(c.val);
  3326. value_editor->set_exp_ratio(c.expr);
  3327. updating_value_editor = false;
  3328. } else {
  3329. value_editor->hide();
  3330. }
  3331. popup_editor->set_position(popup_rect.position);
  3332. popup_editor->set_size(popup_rect.size);
  3333. popup_editor->popup();
  3334. popup_editor->child_controls_changed();
  3335. line_editor->grab_focus();
  3336. return true;
  3337. } else if (c.mode == TreeItem::CELL_MODE_STRING && c.edit_multiline) {
  3338. line_editor->hide();
  3339. text_editor->clear();
  3340. text_editor->set_text(c.text);
  3341. text_editor->select_all();
  3342. text_editor->show();
  3343. popup_editor->set_position(get_screen_position() + rect.position);
  3344. popup_editor->set_size(rect.size);
  3345. popup_editor->popup();
  3346. popup_editor->child_controls_changed();
  3347. text_editor->grab_focus();
  3348. return true;
  3349. }
  3350. return false;
  3351. }
  3352. bool Tree::is_editing() {
  3353. return popup_editor->is_visible();
  3354. }
  3355. void Tree::set_editor_selection(int p_from_line, int p_to_line, int p_from_column, int p_to_column, int p_caret) {
  3356. if (p_from_column == -1 || p_to_column == -1) {
  3357. line_editor->select(p_from_line, p_to_line);
  3358. } else {
  3359. text_editor->select(p_from_line, p_from_column, p_to_line, p_to_column, p_caret);
  3360. }
  3361. }
  3362. Size2 Tree::get_internal_min_size() const {
  3363. Size2i size;
  3364. if (root) {
  3365. size.height += get_item_height(root);
  3366. }
  3367. for (int i = 0; i < columns.size(); i++) {
  3368. size.width += get_column_minimum_width(i);
  3369. }
  3370. return size;
  3371. }
  3372. void Tree::update_scrollbars() {
  3373. const Size2 control_size = get_size();
  3374. const Ref<StyleBox> background = theme_cache.panel_style;
  3375. // This is the background stylebox's content rect.
  3376. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3377. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3378. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3379. const Size2 hmin = h_scroll->get_combined_minimum_size();
  3380. const Size2 vmin = v_scroll->get_combined_minimum_size();
  3381. const Size2 internal_min_size = get_internal_min_size();
  3382. const int title_button_height = _get_title_button_height();
  3383. Size2 tree_content_size = content_rect.get_size() - Vector2(0, title_button_height);
  3384. bool display_vscroll = internal_min_size.height > tree_content_size.height;
  3385. bool display_hscroll = internal_min_size.width > tree_content_size.width;
  3386. for (int i = 0; i < 2; i++) {
  3387. // Check twice, as both values are dependent on each other.
  3388. if (display_hscroll) {
  3389. tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height;
  3390. display_vscroll = internal_min_size.height > tree_content_size.height;
  3391. }
  3392. if (display_vscroll) {
  3393. tree_content_size.width = content_rect.get_size().width - vmin.width;
  3394. display_hscroll = internal_min_size.width > tree_content_size.width;
  3395. }
  3396. }
  3397. if (display_vscroll) {
  3398. v_scroll->show();
  3399. v_scroll->set_max(internal_min_size.height);
  3400. v_scroll->set_page(tree_content_size.height);
  3401. theme_cache.offset.y = v_scroll->get_value();
  3402. } else {
  3403. v_scroll->hide();
  3404. theme_cache.offset.y = 0;
  3405. }
  3406. if (display_hscroll) {
  3407. h_scroll->show();
  3408. h_scroll->set_max(internal_min_size.width);
  3409. h_scroll->set_page(tree_content_size.width);
  3410. theme_cache.offset.x = h_scroll->get_value();
  3411. } else {
  3412. h_scroll->hide();
  3413. theme_cache.offset.x = 0;
  3414. }
  3415. const Rect2 scroll_rect = _get_scrollbar_layout_rect();
  3416. v_scroll->set_begin(scroll_rect.get_position() + Vector2(scroll_rect.get_size().x - vmin.width, 0));
  3417. v_scroll->set_end(scroll_rect.get_end() - Vector2(0, display_hscroll ? hmin.height : 0));
  3418. h_scroll->set_begin(scroll_rect.get_position() + Vector2(0, scroll_rect.get_size().y - hmin.height));
  3419. h_scroll->set_end(scroll_rect.get_end() - Vector2(display_vscroll ? vmin.width : 0, 0));
  3420. }
  3421. int Tree::_get_title_button_height() const {
  3422. ERR_FAIL_COND_V(theme_cache.tb_font.is_null() || theme_cache.title_button.is_null(), 0);
  3423. int h = 0;
  3424. if (show_column_titles) {
  3425. for (int i = 0; i < columns.size(); i++) {
  3426. h = MAX(h, columns[i].text_buf->get_size().y + theme_cache.title_button->get_minimum_size().height);
  3427. }
  3428. }
  3429. return h;
  3430. }
  3431. void Tree::_notification(int p_what) {
  3432. switch (p_what) {
  3433. case NOTIFICATION_FOCUS_ENTER: {
  3434. if (get_viewport()) {
  3435. focus_in_id = get_viewport()->get_processed_events_count();
  3436. }
  3437. } break;
  3438. case NOTIFICATION_MOUSE_EXIT: {
  3439. if (cache.hover_type != Cache::CLICK_NONE) {
  3440. cache.hover_type = Cache::CLICK_NONE;
  3441. queue_redraw();
  3442. }
  3443. } break;
  3444. case NOTIFICATION_VISIBILITY_CHANGED: {
  3445. drag_touching = false;
  3446. } break;
  3447. case NOTIFICATION_DRAG_END: {
  3448. drop_mode_flags = 0;
  3449. scrolling = false;
  3450. set_physics_process_internal(false);
  3451. queue_redraw();
  3452. } break;
  3453. case NOTIFICATION_DRAG_BEGIN: {
  3454. single_select_defer = nullptr;
  3455. if (theme_cache.scroll_speed > 0) {
  3456. scrolling = true;
  3457. set_physics_process_internal(true);
  3458. }
  3459. } break;
  3460. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  3461. if (drag_touching) {
  3462. if (drag_touching_deaccel) {
  3463. float pos = v_scroll->get_value();
  3464. pos += drag_speed * get_physics_process_delta_time();
  3465. bool turnoff = false;
  3466. if (pos < 0) {
  3467. pos = 0;
  3468. turnoff = true;
  3469. set_physics_process_internal(false);
  3470. drag_touching = false;
  3471. drag_touching_deaccel = false;
  3472. }
  3473. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  3474. pos = v_scroll->get_max() - v_scroll->get_page();
  3475. turnoff = true;
  3476. }
  3477. v_scroll->set_value(pos);
  3478. float sgn = drag_speed < 0 ? -1 : 1;
  3479. float val = Math::abs(drag_speed);
  3480. val -= 1000 * get_physics_process_delta_time();
  3481. if (val < 0) {
  3482. turnoff = true;
  3483. }
  3484. drag_speed = sgn * val;
  3485. if (turnoff) {
  3486. set_physics_process_internal(false);
  3487. drag_touching = false;
  3488. drag_touching_deaccel = false;
  3489. }
  3490. }
  3491. }
  3492. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  3493. if (scrolling && get_rect().grow(theme_cache.scroll_border).has_point(mouse_position)) {
  3494. Point2 point;
  3495. if ((ABS(mouse_position.x) < ABS(mouse_position.x - get_size().width)) && (ABS(mouse_position.x) < theme_cache.scroll_border)) {
  3496. point.x = mouse_position.x - theme_cache.scroll_border;
  3497. } else if (ABS(mouse_position.x - get_size().width) < theme_cache.scroll_border) {
  3498. point.x = mouse_position.x - (get_size().width - theme_cache.scroll_border);
  3499. }
  3500. if ((ABS(mouse_position.y) < ABS(mouse_position.y - get_size().height)) && (ABS(mouse_position.y) < theme_cache.scroll_border)) {
  3501. point.y = mouse_position.y - theme_cache.scroll_border;
  3502. } else if (ABS(mouse_position.y - get_size().height) < theme_cache.scroll_border) {
  3503. point.y = mouse_position.y - (get_size().height - theme_cache.scroll_border);
  3504. }
  3505. point *= theme_cache.scroll_speed * get_physics_process_delta_time();
  3506. point += get_scroll();
  3507. h_scroll->set_value(point.x);
  3508. v_scroll->set_value(point.y);
  3509. }
  3510. } break;
  3511. case NOTIFICATION_DRAW: {
  3512. v_scroll->set_custom_step(theme_cache.font->get_height(theme_cache.font_size));
  3513. update_scrollbars();
  3514. RID ci = get_canvas_item();
  3515. Ref<StyleBox> bg = theme_cache.panel_style;
  3516. const Rect2 content_rect = _get_content_rect();
  3517. Point2 draw_ofs = content_rect.position;
  3518. Size2 draw_size = content_rect.size;
  3519. bg->draw(ci, Rect2(Point2(), get_size()));
  3520. int tbh = _get_title_button_height();
  3521. draw_ofs.y += tbh;
  3522. draw_size.y -= tbh;
  3523. cache.rtl = is_layout_rtl();
  3524. if (root && get_size().x > 0 && get_size().y > 0) {
  3525. int self_height = 0; // Just to pass a reference, we don't need the root's `self_height`.
  3526. draw_item(Point2(), draw_ofs, draw_size, root, self_height);
  3527. }
  3528. if (show_column_titles) {
  3529. //title buttons
  3530. int ofs2 = theme_cache.panel_style->get_margin(SIDE_LEFT);
  3531. for (int i = 0; i < columns.size(); i++) {
  3532. Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? theme_cache.title_button_pressed : ((cache.hover_type == Cache::CLICK_TITLE && cache.hover_index == i) ? theme_cache.title_button_hover : theme_cache.title_button);
  3533. Rect2 tbrect = Rect2(ofs2 - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  3534. if (cache.rtl) {
  3535. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  3536. }
  3537. sb->draw(ci, tbrect);
  3538. ofs2 += tbrect.size.width;
  3539. //text
  3540. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  3541. columns.write[i].text_buf->set_width(clip_w);
  3542. columns.write[i].cached_minimum_width_dirty = true;
  3543. Vector2 text_pos = Point2i(tbrect.position.x, tbrect.position.y + (tbrect.size.height - columns[i].text_buf->get_size().y) / 2);
  3544. switch (columns[i].title_alignment) {
  3545. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT: {
  3546. text_pos.x += cache.rtl ? tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x) : sb->get_offset().x;
  3547. break;
  3548. }
  3549. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT: {
  3550. text_pos.x += cache.rtl ? sb->get_offset().x : tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x);
  3551. break;
  3552. }
  3553. default: {
  3554. text_pos.x += sb->get_offset().x + (tbrect.size.width - columns[i].text_buf->get_size().x) / 2;
  3555. break;
  3556. }
  3557. }
  3558. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  3559. columns[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  3560. }
  3561. columns[i].text_buf->draw(ci, text_pos, theme_cache.title_button_color);
  3562. }
  3563. }
  3564. // Draw the focus outline last, so that it is drawn in front of the section headings.
  3565. // Otherwise, section heading backgrounds can appear to be in front of the focus outline when scrolling.
  3566. if (has_focus()) {
  3567. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  3568. theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
  3569. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  3570. }
  3571. } break;
  3572. case NOTIFICATION_THEME_CHANGED:
  3573. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  3574. case NOTIFICATION_TRANSLATION_CHANGED: {
  3575. _update_all();
  3576. } break;
  3577. case NOTIFICATION_RESIZED:
  3578. case NOTIFICATION_TRANSFORM_CHANGED: {
  3579. if (popup_edited_item != nullptr) {
  3580. Rect2 rect = popup_edited_item->get_meta("__focus_rect");
  3581. popup_editor->set_position(get_global_position() + rect.position);
  3582. popup_editor->set_size(rect.size);
  3583. popup_editor->child_controls_changed();
  3584. }
  3585. } break;
  3586. }
  3587. }
  3588. void Tree::_update_all() {
  3589. for (int i = 0; i < columns.size(); i++) {
  3590. update_column(i);
  3591. }
  3592. if (root) {
  3593. update_item_cache(root);
  3594. }
  3595. }
  3596. Size2 Tree::get_minimum_size() const {
  3597. if (h_scroll_enabled && v_scroll_enabled) {
  3598. return Size2();
  3599. } else {
  3600. Vector2 min_size = get_internal_min_size();
  3601. Ref<StyleBox> bg = theme_cache.panel_style;
  3602. if (bg.is_valid()) {
  3603. min_size.x += bg->get_margin(SIDE_LEFT) + bg->get_margin(SIDE_RIGHT);
  3604. min_size.y += bg->get_margin(SIDE_TOP) + bg->get_margin(SIDE_BOTTOM);
  3605. }
  3606. return Vector2(h_scroll_enabled ? 0 : min_size.x, v_scroll_enabled ? 0 : min_size.y);
  3607. }
  3608. }
  3609. TreeItem *Tree::create_item(TreeItem *p_parent, int p_index) {
  3610. ERR_FAIL_COND_V(blocked > 0, nullptr);
  3611. TreeItem *ti = nullptr;
  3612. if (p_parent) {
  3613. ERR_FAIL_COND_V_MSG(p_parent->tree != this, nullptr, "A different tree owns the given parent");
  3614. ti = p_parent->create_child(p_index);
  3615. } else {
  3616. if (!root) {
  3617. // No root exists, make the given item the new root.
  3618. ti = memnew(TreeItem(this));
  3619. ERR_FAIL_NULL_V(ti, nullptr);
  3620. ti->cells.resize(columns.size());
  3621. ti->is_root = true;
  3622. root = ti;
  3623. } else {
  3624. // Root exists, append or insert to root.
  3625. ti = create_item(root, p_index);
  3626. }
  3627. }
  3628. return ti;
  3629. }
  3630. TreeItem *Tree::get_root() const {
  3631. return root;
  3632. }
  3633. TreeItem *Tree::get_last_item() const {
  3634. TreeItem *last = root;
  3635. while (last) {
  3636. if (last->next) {
  3637. last = last->next;
  3638. } else if (last->first_child) {
  3639. last = last->first_child;
  3640. } else {
  3641. break;
  3642. }
  3643. }
  3644. return last;
  3645. }
  3646. void Tree::item_edited(int p_column, TreeItem *p_item, MouseButton p_custom_mouse_index) {
  3647. edited_item = p_item;
  3648. edited_col = p_column;
  3649. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  3650. edited_item->cells.write[p_column].dirty = true;
  3651. }
  3652. emit_signal(SNAME("item_edited"));
  3653. if (p_custom_mouse_index != MouseButton::NONE) {
  3654. emit_signal(SNAME("custom_item_clicked"), p_custom_mouse_index);
  3655. }
  3656. }
  3657. void Tree::item_changed(int p_column, TreeItem *p_item) {
  3658. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  3659. p_item->cells.write[p_column].dirty = true;
  3660. columns.write[p_column].cached_minimum_width_dirty = true;
  3661. }
  3662. queue_redraw();
  3663. }
  3664. void Tree::item_selected(int p_column, TreeItem *p_item) {
  3665. if (select_mode == SELECT_MULTI) {
  3666. if (!p_item->cells[p_column].selectable) {
  3667. return;
  3668. }
  3669. p_item->cells.write[p_column].selected = true;
  3670. //emit_signal(SNAME("multi_selected"),p_item,p_column,true); - NO this is for TreeItem::select
  3671. selected_col = p_column;
  3672. selected_item = p_item;
  3673. } else {
  3674. select_single_item(p_item, root, p_column);
  3675. }
  3676. queue_redraw();
  3677. }
  3678. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  3679. if (select_mode == SELECT_SINGLE && selected_item == p_item && selected_col == p_column) {
  3680. selected_item = nullptr;
  3681. selected_col = -1;
  3682. } else {
  3683. if (select_mode == SELECT_ROW && selected_item == p_item) {
  3684. selected_item = nullptr;
  3685. selected_col = -1;
  3686. } else {
  3687. if (select_mode == SELECT_MULTI) {
  3688. selected_item = p_item;
  3689. selected_col = p_column;
  3690. }
  3691. }
  3692. }
  3693. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  3694. p_item->cells.write[p_column].selected = false;
  3695. } else if (select_mode == SELECT_ROW) {
  3696. for (int i = 0; i < p_item->cells.size(); i++) {
  3697. p_item->cells.write[i].selected = false;
  3698. }
  3699. }
  3700. queue_redraw();
  3701. }
  3702. void Tree::set_select_mode(SelectMode p_mode) {
  3703. select_mode = p_mode;
  3704. }
  3705. Tree::SelectMode Tree::get_select_mode() const {
  3706. return select_mode;
  3707. }
  3708. void Tree::deselect_all() {
  3709. if (root) {
  3710. TreeItem *item = root;
  3711. while (item) {
  3712. if (select_mode == SELECT_ROW) {
  3713. item->deselect(0);
  3714. } else {
  3715. for (int i = 0; i < columns.size(); i++) {
  3716. item->deselect(i);
  3717. }
  3718. }
  3719. TreeItem *prev_item = item;
  3720. item = get_next_selected(root);
  3721. ERR_FAIL_COND(item == prev_item);
  3722. }
  3723. }
  3724. selected_item = nullptr;
  3725. selected_col = -1;
  3726. queue_redraw();
  3727. }
  3728. bool Tree::is_anything_selected() {
  3729. return (selected_item != nullptr);
  3730. }
  3731. void Tree::clear() {
  3732. ERR_FAIL_COND(blocked > 0);
  3733. if (pressing_for_editor) {
  3734. if (range_drag_enabled) {
  3735. range_drag_enabled = false;
  3736. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  3737. warp_mouse(range_drag_capture_pos);
  3738. }
  3739. pressing_for_editor = false;
  3740. }
  3741. if (root) {
  3742. memdelete(root);
  3743. root = nullptr;
  3744. };
  3745. selected_item = nullptr;
  3746. edited_item = nullptr;
  3747. popup_edited_item = nullptr;
  3748. popup_pressing_edited_item = nullptr;
  3749. queue_redraw();
  3750. };
  3751. void Tree::set_hide_root(bool p_enabled) {
  3752. if (hide_root == p_enabled) {
  3753. return;
  3754. }
  3755. hide_root = p_enabled;
  3756. queue_redraw();
  3757. }
  3758. bool Tree::is_root_hidden() const {
  3759. return hide_root;
  3760. }
  3761. void Tree::set_column_custom_minimum_width(int p_column, int p_min_width) {
  3762. ERR_FAIL_INDEX(p_column, columns.size());
  3763. if (columns[p_column].custom_min_width == p_min_width) {
  3764. return;
  3765. }
  3766. if (p_min_width < 0) {
  3767. return;
  3768. }
  3769. columns.write[p_column].custom_min_width = p_min_width;
  3770. columns.write[p_column].cached_minimum_width_dirty = true;
  3771. queue_redraw();
  3772. }
  3773. void Tree::set_column_expand(int p_column, bool p_expand) {
  3774. ERR_FAIL_INDEX(p_column, columns.size());
  3775. if (columns[p_column].expand == p_expand) {
  3776. return;
  3777. }
  3778. columns.write[p_column].expand = p_expand;
  3779. columns.write[p_column].cached_minimum_width_dirty = true;
  3780. queue_redraw();
  3781. }
  3782. void Tree::set_column_expand_ratio(int p_column, int p_ratio) {
  3783. ERR_FAIL_INDEX(p_column, columns.size());
  3784. if (columns[p_column].expand_ratio == p_ratio) {
  3785. return;
  3786. }
  3787. columns.write[p_column].expand_ratio = p_ratio;
  3788. columns.write[p_column].cached_minimum_width_dirty = true;
  3789. queue_redraw();
  3790. }
  3791. void Tree::set_column_clip_content(int p_column, bool p_fit) {
  3792. ERR_FAIL_INDEX(p_column, columns.size());
  3793. if (columns[p_column].clip_content == p_fit) {
  3794. return;
  3795. }
  3796. columns.write[p_column].clip_content = p_fit;
  3797. columns.write[p_column].cached_minimum_width_dirty = true;
  3798. queue_redraw();
  3799. }
  3800. bool Tree::is_column_expanding(int p_column) const {
  3801. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  3802. return columns[p_column].expand;
  3803. }
  3804. int Tree::get_column_expand_ratio(int p_column) const {
  3805. ERR_FAIL_INDEX_V(p_column, columns.size(), 1);
  3806. return columns[p_column].expand_ratio;
  3807. }
  3808. bool Tree::is_column_clipping_content(int p_column) const {
  3809. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  3810. return columns[p_column].clip_content;
  3811. }
  3812. TreeItem *Tree::get_selected() const {
  3813. return selected_item;
  3814. }
  3815. void Tree::set_selected(TreeItem *p_item, int p_column) {
  3816. ERR_FAIL_INDEX(p_column, columns.size());
  3817. ERR_FAIL_NULL(p_item);
  3818. ERR_FAIL_COND_MSG(p_item->get_tree() != this, "The provided TreeItem does not belong to this Tree. Ensure that the TreeItem is a part of the Tree before setting it as selected.");
  3819. select_single_item(p_item, get_root(), p_column);
  3820. }
  3821. int Tree::get_selected_column() const {
  3822. return selected_col;
  3823. }
  3824. TreeItem *Tree::get_edited() const {
  3825. return edited_item;
  3826. }
  3827. int Tree::get_edited_column() const {
  3828. return edited_col;
  3829. }
  3830. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  3831. if (!root) {
  3832. return nullptr;
  3833. }
  3834. while (true) {
  3835. if (!p_item) {
  3836. p_item = root;
  3837. } else {
  3838. if (p_item->first_child) {
  3839. p_item = p_item->first_child;
  3840. } else if (p_item->next) {
  3841. p_item = p_item->next;
  3842. } else {
  3843. while (!p_item->next) {
  3844. p_item = p_item->parent;
  3845. if (p_item == nullptr) {
  3846. return nullptr;
  3847. }
  3848. }
  3849. p_item = p_item->next;
  3850. }
  3851. }
  3852. for (int i = 0; i < columns.size(); i++) {
  3853. if (p_item->cells[i].selected) {
  3854. return p_item;
  3855. }
  3856. }
  3857. }
  3858. return nullptr;
  3859. }
  3860. int Tree::get_column_minimum_width(int p_column) const {
  3861. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  3862. if (columns[p_column].cached_minimum_width_dirty) {
  3863. // Use the custom minimum width.
  3864. int min_width = columns[p_column].custom_min_width;
  3865. // Check if the visible title of the column is wider.
  3866. if (show_column_titles) {
  3867. min_width = MAX(theme_cache.font->get_string_size(columns[p_column].xl_title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + theme_cache.panel_style->get_margin(SIDE_LEFT) + theme_cache.panel_style->get_margin(SIDE_RIGHT), min_width);
  3868. }
  3869. if (!columns[p_column].clip_content) {
  3870. int depth = 0;
  3871. TreeItem *next;
  3872. for (TreeItem *item = get_root(); item; item = next) {
  3873. next = item->get_next_visible();
  3874. // Compute the depth in tree.
  3875. if (next && p_column == 0) {
  3876. if (next->get_parent() == item) {
  3877. depth += 1;
  3878. } else {
  3879. TreeItem *common_parent = item->get_parent();
  3880. while (common_parent != next->get_parent() && common_parent) {
  3881. common_parent = common_parent->get_parent();
  3882. depth -= 1;
  3883. }
  3884. }
  3885. }
  3886. // Get the item minimum size.
  3887. Size2 item_size = item->get_minimum_size(p_column);
  3888. if (p_column == 0) {
  3889. item_size.width += theme_cache.item_margin * depth;
  3890. } else {
  3891. item_size.width += theme_cache.h_separation;
  3892. }
  3893. // Check if the item is wider.
  3894. min_width = MAX(min_width, item_size.width);
  3895. }
  3896. }
  3897. columns.get(p_column).cached_minimum_width = min_width;
  3898. columns.get(p_column).cached_minimum_width_dirty = false;
  3899. }
  3900. return columns[p_column].cached_minimum_width;
  3901. }
  3902. int Tree::get_column_width(int p_column) const {
  3903. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  3904. int column_width = get_column_minimum_width(p_column);
  3905. if (columns[p_column].expand) {
  3906. int expand_area = _get_content_rect().size.width;
  3907. int expanding_total = 0;
  3908. for (int i = 0; i < columns.size(); i++) {
  3909. expand_area -= get_column_minimum_width(i);
  3910. if (columns[i].expand) {
  3911. expanding_total += columns[i].expand_ratio;
  3912. }
  3913. }
  3914. if (expand_area >= expanding_total && expanding_total > 0) {
  3915. column_width += expand_area * columns[p_column].expand_ratio / expanding_total;
  3916. }
  3917. }
  3918. return column_width;
  3919. }
  3920. void Tree::propagate_set_columns(TreeItem *p_item) {
  3921. p_item->cells.resize(columns.size());
  3922. TreeItem *c = p_item->get_first_child();
  3923. while (c) {
  3924. propagate_set_columns(c);
  3925. c = c->next;
  3926. }
  3927. }
  3928. void Tree::set_columns(int p_columns) {
  3929. ERR_FAIL_COND(p_columns < 1);
  3930. ERR_FAIL_COND(blocked > 0);
  3931. columns.resize(p_columns);
  3932. if (root) {
  3933. propagate_set_columns(root);
  3934. }
  3935. if (selected_col >= p_columns) {
  3936. selected_col = p_columns - 1;
  3937. }
  3938. queue_redraw();
  3939. }
  3940. int Tree::get_columns() const {
  3941. return columns.size();
  3942. }
  3943. void Tree::_scroll_moved(float) {
  3944. queue_redraw();
  3945. }
  3946. Rect2 Tree::get_custom_popup_rect() const {
  3947. return custom_popup_rect;
  3948. }
  3949. int Tree::get_item_offset(TreeItem *p_item) const {
  3950. TreeItem *it = root;
  3951. int ofs = _get_title_button_height();
  3952. if (!it) {
  3953. return 0;
  3954. }
  3955. while (true) {
  3956. if (it == p_item) {
  3957. return ofs;
  3958. }
  3959. if ((it != root || !hide_root) && it->is_visible_in_tree()) {
  3960. ofs += compute_item_height(it);
  3961. ofs += theme_cache.v_separation;
  3962. }
  3963. if (it->first_child && !it->collapsed) {
  3964. it = it->first_child;
  3965. } else if (it->next) {
  3966. it = it->next;
  3967. } else {
  3968. while (!it->next) {
  3969. it = it->parent;
  3970. if (it == nullptr) {
  3971. return 0;
  3972. }
  3973. }
  3974. it = it->next;
  3975. }
  3976. }
  3977. return -1; //not found
  3978. }
  3979. void Tree::ensure_cursor_is_visible() {
  3980. if (!is_inside_tree()) {
  3981. return;
  3982. }
  3983. if (!selected_item || (selected_col == -1)) {
  3984. return; // Nothing under cursor.
  3985. }
  3986. // Note: Code below similar to Tree::scroll_to_item(), in case of bug fix both.
  3987. const Size2 area_size = _get_content_rect().size;
  3988. int y_offset = get_item_offset(selected_item);
  3989. if (y_offset != -1) {
  3990. const int tbh = _get_title_button_height();
  3991. y_offset -= tbh;
  3992. const int cell_h = compute_item_height(selected_item) + theme_cache.v_separation;
  3993. int screen_h = area_size.height - tbh;
  3994. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  3995. v_scroll->set_value(y_offset);
  3996. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  3997. callable_mp((Range *)v_scroll, &Range::set_value).call_deferred(y_offset - screen_h + cell_h);
  3998. } else if (y_offset < v_scroll->get_value()) {
  3999. v_scroll->set_value(y_offset);
  4000. }
  4001. }
  4002. if (select_mode != SELECT_ROW) { // Cursor always at col 0 in this mode.
  4003. int x_offset = 0;
  4004. for (int i = 0; i < selected_col; i++) {
  4005. x_offset += get_column_width(i);
  4006. }
  4007. const int cell_w = get_column_width(selected_col);
  4008. const int screen_w = area_size.width;
  4009. if (cell_w > screen_w) {
  4010. h_scroll->set_value(x_offset);
  4011. } else if (x_offset + cell_w > h_scroll->get_value() + screen_w) {
  4012. callable_mp((Range *)h_scroll, &Range::set_value).call_deferred(x_offset - screen_w + cell_w);
  4013. } else if (x_offset < h_scroll->get_value()) {
  4014. h_scroll->set_value(x_offset);
  4015. }
  4016. }
  4017. }
  4018. int Tree::get_pressed_button() const {
  4019. return pressed_button;
  4020. }
  4021. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column, int p_button) const {
  4022. ERR_FAIL_NULL_V(p_item, Rect2());
  4023. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  4024. if (p_column != -1) {
  4025. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  4026. }
  4027. if (p_button != -1) {
  4028. ERR_FAIL_COND_V(p_column == -1, Rect2()); // pass a column if you want to pass a button
  4029. ERR_FAIL_INDEX_V(p_button, p_item->cells[p_column].buttons.size(), Rect2());
  4030. }
  4031. int ofs = get_item_offset(p_item);
  4032. int height = compute_item_height(p_item);
  4033. Rect2 r;
  4034. r.position.y = ofs;
  4035. r.size.height = height;
  4036. if (p_column == -1) {
  4037. r.position.x = 0;
  4038. r.size.x = get_size().width;
  4039. } else {
  4040. int accum = 0;
  4041. for (int i = 0; i < p_column; i++) {
  4042. accum += get_column_width(i);
  4043. }
  4044. r.position.x = accum;
  4045. r.size.x = get_column_width(p_column);
  4046. if (p_button != -1) {
  4047. const TreeItem::Cell &c = p_item->cells[p_column];
  4048. Vector2 ofst = Vector2(r.position.x + r.size.x, r.position.y);
  4049. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4050. Ref<Texture2D> b = c.buttons[j].texture;
  4051. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4052. ofst.x -= size.x;
  4053. if (j == p_button) {
  4054. return Rect2(ofst, size);
  4055. }
  4056. }
  4057. }
  4058. }
  4059. return r;
  4060. }
  4061. void Tree::set_column_titles_visible(bool p_show) {
  4062. if (show_column_titles == p_show) {
  4063. return;
  4064. }
  4065. show_column_titles = p_show;
  4066. queue_redraw();
  4067. }
  4068. bool Tree::are_column_titles_visible() const {
  4069. return show_column_titles;
  4070. }
  4071. void Tree::set_column_title(int p_column, const String &p_title) {
  4072. ERR_FAIL_INDEX(p_column, columns.size());
  4073. if (columns[p_column].title == p_title) {
  4074. return;
  4075. }
  4076. columns.write[p_column].title = p_title;
  4077. columns.write[p_column].xl_title = atr(p_title);
  4078. update_column(p_column);
  4079. queue_redraw();
  4080. }
  4081. String Tree::get_column_title(int p_column) const {
  4082. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4083. return columns[p_column].title;
  4084. }
  4085. void Tree::set_column_title_alignment(int p_column, HorizontalAlignment p_alignment) {
  4086. ERR_FAIL_INDEX(p_column, columns.size());
  4087. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  4088. WARN_PRINT("HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.");
  4089. }
  4090. if (columns[p_column].title_alignment == p_alignment) {
  4091. return;
  4092. }
  4093. columns.write[p_column].title_alignment = p_alignment;
  4094. update_column(p_column);
  4095. queue_redraw();
  4096. }
  4097. HorizontalAlignment Tree::get_column_title_alignment(int p_column) const {
  4098. ERR_FAIL_INDEX_V(p_column, columns.size(), HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  4099. return columns[p_column].title_alignment;
  4100. }
  4101. void Tree::set_column_title_direction(int p_column, Control::TextDirection p_text_direction) {
  4102. ERR_FAIL_INDEX(p_column, columns.size());
  4103. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  4104. if (columns[p_column].text_direction != p_text_direction) {
  4105. columns.write[p_column].text_direction = p_text_direction;
  4106. update_column(p_column);
  4107. queue_redraw();
  4108. }
  4109. }
  4110. Control::TextDirection Tree::get_column_title_direction(int p_column) const {
  4111. ERR_FAIL_INDEX_V(p_column, columns.size(), TEXT_DIRECTION_INHERITED);
  4112. return columns[p_column].text_direction;
  4113. }
  4114. void Tree::set_column_title_language(int p_column, const String &p_language) {
  4115. ERR_FAIL_INDEX(p_column, columns.size());
  4116. if (columns[p_column].language != p_language) {
  4117. columns.write[p_column].language = p_language;
  4118. update_column(p_column);
  4119. queue_redraw();
  4120. }
  4121. }
  4122. String Tree::get_column_title_language(int p_column) const {
  4123. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4124. return columns[p_column].language;
  4125. }
  4126. Point2 Tree::get_scroll() const {
  4127. Point2 ofs;
  4128. if (h_scroll->is_visible_in_tree()) {
  4129. ofs.x = h_scroll->get_value();
  4130. }
  4131. if (v_scroll->is_visible_in_tree()) {
  4132. ofs.y = v_scroll->get_value();
  4133. }
  4134. return ofs;
  4135. }
  4136. void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) {
  4137. ERR_FAIL_NULL(p_item);
  4138. update_scrollbars();
  4139. // Note: Code below similar to Tree::ensure_cursor_is_visible(), in case of bug fix both.
  4140. const Size2 area_size = _get_content_rect().size;
  4141. int y_offset = get_item_offset(p_item);
  4142. if (y_offset != -1) {
  4143. const int tbh = _get_title_button_height();
  4144. y_offset -= tbh;
  4145. const int cell_h = compute_item_height(p_item) + theme_cache.v_separation;
  4146. int screen_h = area_size.height - tbh;
  4147. if (p_center_on_item) {
  4148. v_scroll->set_value(y_offset - (screen_h - cell_h) / 2.0f);
  4149. } else {
  4150. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  4151. v_scroll->set_value(y_offset);
  4152. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  4153. v_scroll->set_value(y_offset - screen_h + cell_h);
  4154. } else if (y_offset < v_scroll->get_value()) {
  4155. v_scroll->set_value(y_offset);
  4156. }
  4157. }
  4158. }
  4159. }
  4160. void Tree::set_h_scroll_enabled(bool p_enable) {
  4161. if (h_scroll_enabled == p_enable) {
  4162. return;
  4163. }
  4164. h_scroll_enabled = p_enable;
  4165. update_minimum_size();
  4166. }
  4167. bool Tree::is_h_scroll_enabled() const {
  4168. return h_scroll_enabled;
  4169. }
  4170. void Tree::set_v_scroll_enabled(bool p_enable) {
  4171. if (v_scroll_enabled == p_enable) {
  4172. return;
  4173. }
  4174. v_scroll_enabled = p_enable;
  4175. update_minimum_size();
  4176. }
  4177. bool Tree::is_v_scroll_enabled() const {
  4178. return v_scroll_enabled;
  4179. }
  4180. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  4181. TreeItem *from = p_at;
  4182. TreeItem *loop = nullptr; // Safe-guard against infinite loop.
  4183. while (p_at) {
  4184. for (int i = 0; i < columns.size(); i++) {
  4185. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  4186. if (r_col) {
  4187. *r_col = i;
  4188. }
  4189. return p_at;
  4190. }
  4191. }
  4192. if (p_backwards) {
  4193. p_at = p_at->get_prev_visible(true);
  4194. } else {
  4195. p_at = p_at->get_next_visible(true);
  4196. }
  4197. if ((p_at) == from) {
  4198. break;
  4199. }
  4200. if (!loop) {
  4201. loop = p_at;
  4202. } else if (loop == p_at) {
  4203. break;
  4204. }
  4205. }
  4206. return nullptr;
  4207. }
  4208. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  4209. TreeItem *from = get_selected();
  4210. if (!from) {
  4211. from = root;
  4212. }
  4213. if (!from) {
  4214. return nullptr;
  4215. }
  4216. return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
  4217. }
  4218. TreeItem *Tree::get_item_with_text(const String &p_find) const {
  4219. for (TreeItem *current = root; current; current = current->get_next_visible()) {
  4220. for (int i = 0; i < columns.size(); i++) {
  4221. if (current->get_text(i) == p_find) {
  4222. return current;
  4223. }
  4224. }
  4225. }
  4226. return nullptr;
  4227. }
  4228. TreeItem *Tree::get_item_with_metadata(const Variant &p_find, int p_column) const {
  4229. if (p_column < 0) {
  4230. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4231. for (int i = 0; i < columns.size(); i++) {
  4232. if (current->get_metadata(i) == p_find) {
  4233. return current;
  4234. }
  4235. }
  4236. }
  4237. return nullptr;
  4238. }
  4239. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4240. if (current->get_metadata(p_column) == p_find) {
  4241. return current;
  4242. }
  4243. }
  4244. return nullptr;
  4245. }
  4246. void Tree::_do_incr_search(const String &p_add) {
  4247. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  4248. uint64_t diff = time - last_keypress;
  4249. if (diff > uint64_t(GLOBAL_GET("gui/timers/incremental_search_max_interval_msec"))) {
  4250. incr_search = p_add;
  4251. } else if (incr_search != p_add) {
  4252. incr_search += p_add;
  4253. }
  4254. last_keypress = time;
  4255. int col;
  4256. TreeItem *item = search_item_text(incr_search, &col, true);
  4257. if (!item) {
  4258. return;
  4259. }
  4260. if (select_mode == SELECT_MULTI) {
  4261. item->set_as_cursor(col);
  4262. } else {
  4263. item->select(col);
  4264. }
  4265. ensure_cursor_is_visible();
  4266. }
  4267. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  4268. Point2 pos = p_pos;
  4269. if ((root != p_item || !hide_root) && p_item->is_visible_in_tree()) {
  4270. h = compute_item_height(p_item) + theme_cache.v_separation;
  4271. if (pos.y < h) {
  4272. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  4273. section = 0;
  4274. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  4275. section = pos.y < h / 2 ? -1 : 1;
  4276. } else if (pos.y < h / 4) {
  4277. section = -1;
  4278. } else if (pos.y >= (h * 3 / 4)) {
  4279. section = 1;
  4280. } else {
  4281. section = 0;
  4282. }
  4283. for (int i = 0; i < columns.size(); i++) {
  4284. int w = get_column_width(i);
  4285. if (pos.x < w) {
  4286. r_column = i;
  4287. return p_item;
  4288. }
  4289. pos.x -= w;
  4290. }
  4291. return nullptr;
  4292. } else {
  4293. pos.y -= h;
  4294. }
  4295. } else {
  4296. h = 0;
  4297. }
  4298. if (p_item->is_collapsed() || !p_item->is_visible_in_tree()) {
  4299. return nullptr; // do not try children, it's collapsed
  4300. }
  4301. TreeItem *n = p_item->get_first_child();
  4302. while (n) {
  4303. int ch;
  4304. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  4305. pos.y -= ch;
  4306. h += ch;
  4307. if (r) {
  4308. return r;
  4309. }
  4310. n = n->get_next();
  4311. }
  4312. return nullptr;
  4313. }
  4314. int Tree::get_column_at_position(const Point2 &p_pos) const {
  4315. if (root) {
  4316. Point2 pos = p_pos;
  4317. if (is_layout_rtl()) {
  4318. pos.x = get_size().width - pos.x;
  4319. }
  4320. pos -= theme_cache.panel_style->get_offset();
  4321. pos.y -= _get_title_button_height();
  4322. if (pos.y < 0) {
  4323. return -1;
  4324. }
  4325. if (h_scroll->is_visible_in_tree()) {
  4326. pos.x += h_scroll->get_value();
  4327. }
  4328. if (v_scroll->is_visible_in_tree()) {
  4329. pos.y += v_scroll->get_value();
  4330. }
  4331. int col, h, section;
  4332. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  4333. if (it) {
  4334. return col;
  4335. }
  4336. }
  4337. return -1;
  4338. }
  4339. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  4340. if (root) {
  4341. Point2 pos = p_pos;
  4342. if (is_layout_rtl()) {
  4343. pos.x = get_size().width - pos.x;
  4344. }
  4345. pos -= theme_cache.panel_style->get_offset();
  4346. pos.y -= _get_title_button_height();
  4347. if (pos.y < 0) {
  4348. return -100;
  4349. }
  4350. if (h_scroll->is_visible_in_tree()) {
  4351. pos.x += h_scroll->get_value();
  4352. }
  4353. if (v_scroll->is_visible_in_tree()) {
  4354. pos.y += v_scroll->get_value();
  4355. }
  4356. int col, h, section;
  4357. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  4358. if (it) {
  4359. return section;
  4360. }
  4361. }
  4362. return -100;
  4363. }
  4364. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  4365. if (root) {
  4366. Point2 pos = p_pos;
  4367. if (is_layout_rtl()) {
  4368. pos.x = get_size().width - pos.x;
  4369. }
  4370. pos -= theme_cache.panel_style->get_offset();
  4371. pos.y -= _get_title_button_height();
  4372. if (pos.y < 0) {
  4373. return nullptr;
  4374. }
  4375. if (h_scroll->is_visible_in_tree()) {
  4376. pos.x += h_scroll->get_value();
  4377. }
  4378. if (v_scroll->is_visible_in_tree()) {
  4379. pos.y += v_scroll->get_value();
  4380. }
  4381. int col, h, section;
  4382. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  4383. if (it) {
  4384. return it;
  4385. }
  4386. }
  4387. return nullptr;
  4388. }
  4389. int Tree::get_button_id_at_position(const Point2 &p_pos) const {
  4390. if (root) {
  4391. Point2 pos = p_pos;
  4392. pos -= theme_cache.panel_style->get_offset();
  4393. pos.y -= _get_title_button_height();
  4394. if (pos.y < 0) {
  4395. return -1;
  4396. }
  4397. if (h_scroll->is_visible_in_tree()) {
  4398. pos.x += h_scroll->get_value();
  4399. }
  4400. if (v_scroll->is_visible_in_tree()) {
  4401. pos.y += v_scroll->get_value();
  4402. }
  4403. int col, h, section;
  4404. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  4405. if (it) {
  4406. const TreeItem::Cell &c = it->cells[col];
  4407. int col_width = get_column_width(col);
  4408. for (int i = 0; i < col; i++) {
  4409. pos.x -= get_column_width(i);
  4410. }
  4411. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4412. Ref<Texture2D> b = c.buttons[j].texture;
  4413. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4414. if (pos.x > col_width - size.width) {
  4415. return c.buttons[j].id;
  4416. }
  4417. col_width -= size.width;
  4418. }
  4419. }
  4420. }
  4421. return -1;
  4422. }
  4423. String Tree::get_tooltip(const Point2 &p_pos) const {
  4424. if (root) {
  4425. Point2 pos = p_pos;
  4426. pos -= theme_cache.panel_style->get_offset();
  4427. pos.y -= _get_title_button_height();
  4428. if (pos.y < 0) {
  4429. return Control::get_tooltip(p_pos);
  4430. }
  4431. Point2 button_pos = pos;
  4432. if (h_scroll->is_visible_in_tree()) {
  4433. pos.x += h_scroll->get_value();
  4434. }
  4435. if (v_scroll->is_visible_in_tree()) {
  4436. pos.y += v_scroll->get_value();
  4437. }
  4438. int col, h, section;
  4439. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  4440. if (it) {
  4441. const TreeItem::Cell &c = it->cells[col];
  4442. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4443. if (c.buttons[j].rect.has_point(button_pos)) {
  4444. String tooltip = c.buttons[j].tooltip;
  4445. if (!tooltip.is_empty()) {
  4446. return tooltip;
  4447. }
  4448. }
  4449. }
  4450. String ret;
  4451. if (it->get_tooltip_text(col) == "") {
  4452. ret = it->get_text(col);
  4453. } else {
  4454. ret = it->get_tooltip_text(col);
  4455. }
  4456. return ret;
  4457. }
  4458. }
  4459. return Control::get_tooltip(p_pos);
  4460. }
  4461. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  4462. cursor_can_exit_tree = p_enable;
  4463. }
  4464. void Tree::set_hide_folding(bool p_hide) {
  4465. if (hide_folding == p_hide) {
  4466. return;
  4467. }
  4468. hide_folding = p_hide;
  4469. queue_redraw();
  4470. }
  4471. bool Tree::is_folding_hidden() const {
  4472. return hide_folding;
  4473. }
  4474. void Tree::set_enable_recursive_folding(bool p_enable) {
  4475. enable_recursive_folding = p_enable;
  4476. }
  4477. bool Tree::is_recursive_folding_enabled() const {
  4478. return enable_recursive_folding;
  4479. }
  4480. void Tree::set_drop_mode_flags(int p_flags) {
  4481. if (drop_mode_flags == p_flags) {
  4482. return;
  4483. }
  4484. drop_mode_flags = p_flags;
  4485. if (drop_mode_flags == 0) {
  4486. drop_mode_over = nullptr;
  4487. }
  4488. queue_redraw();
  4489. }
  4490. int Tree::get_drop_mode_flags() const {
  4491. return drop_mode_flags;
  4492. }
  4493. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  4494. force_edit_checkbox_only_on_checkbox = p_enable;
  4495. }
  4496. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  4497. return force_edit_checkbox_only_on_checkbox;
  4498. }
  4499. void Tree::set_allow_rmb_select(bool p_allow) {
  4500. allow_rmb_select = p_allow;
  4501. }
  4502. bool Tree::get_allow_rmb_select() const {
  4503. return allow_rmb_select;
  4504. }
  4505. void Tree::set_allow_reselect(bool p_allow) {
  4506. allow_reselect = p_allow;
  4507. }
  4508. bool Tree::get_allow_reselect() const {
  4509. return allow_reselect;
  4510. }
  4511. void Tree::set_allow_search(bool p_allow) {
  4512. allow_search = p_allow;
  4513. }
  4514. bool Tree::get_allow_search() const {
  4515. return allow_search;
  4516. }
  4517. void Tree::_bind_methods() {
  4518. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  4519. ClassDB::bind_method(D_METHOD("create_item", "parent", "index"), &Tree::create_item, DEFVAL(Variant()), DEFVAL(-1));
  4520. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  4521. ClassDB::bind_method(D_METHOD("set_column_custom_minimum_width", "column", "min_width"), &Tree::set_column_custom_minimum_width);
  4522. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  4523. ClassDB::bind_method(D_METHOD("set_column_expand_ratio", "column", "ratio"), &Tree::set_column_expand_ratio);
  4524. ClassDB::bind_method(D_METHOD("set_column_clip_content", "column", "enable"), &Tree::set_column_clip_content);
  4525. ClassDB::bind_method(D_METHOD("is_column_expanding", "column"), &Tree::is_column_expanding);
  4526. ClassDB::bind_method(D_METHOD("is_column_clipping_content", "column"), &Tree::is_column_clipping_content);
  4527. ClassDB::bind_method(D_METHOD("get_column_expand_ratio", "column"), &Tree::get_column_expand_ratio);
  4528. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  4529. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  4530. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  4531. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::get_next_selected);
  4532. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  4533. ClassDB::bind_method(D_METHOD("set_selected", "item", "column"), &Tree::set_selected);
  4534. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  4535. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  4536. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  4537. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  4538. ClassDB::bind_method(D_METHOD("deselect_all"), &Tree::deselect_all);
  4539. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  4540. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  4541. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  4542. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  4543. ClassDB::bind_method(D_METHOD("edit_selected", "force_edit"), &Tree::edit_selected, DEFVAL(false));
  4544. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  4545. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column", "button_index"), &Tree::get_item_rect, DEFVAL(-1), DEFVAL(-1));
  4546. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  4547. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  4548. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  4549. ClassDB::bind_method(D_METHOD("get_button_id_at_position", "position"), &Tree::get_button_id_at_position);
  4550. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  4551. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  4552. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  4553. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  4554. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  4555. ClassDB::bind_method(D_METHOD("set_column_title_alignment", "column", "title_alignment"), &Tree::set_column_title_alignment);
  4556. ClassDB::bind_method(D_METHOD("get_column_title_alignment", "column"), &Tree::get_column_title_alignment);
  4557. ClassDB::bind_method(D_METHOD("set_column_title_direction", "column", "direction"), &Tree::set_column_title_direction);
  4558. ClassDB::bind_method(D_METHOD("get_column_title_direction", "column"), &Tree::get_column_title_direction);
  4559. ClassDB::bind_method(D_METHOD("set_column_title_language", "column", "language"), &Tree::set_column_title_language);
  4560. ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language);
  4561. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  4562. ClassDB::bind_method(D_METHOD("scroll_to_item", "item", "center_on_item"), &Tree::scroll_to_item, DEFVAL(false));
  4563. ClassDB::bind_method(D_METHOD("set_h_scroll_enabled", "h_scroll"), &Tree::set_h_scroll_enabled);
  4564. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled);
  4565. ClassDB::bind_method(D_METHOD("set_v_scroll_enabled", "h_scroll"), &Tree::set_v_scroll_enabled);
  4566. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &Tree::is_v_scroll_enabled);
  4567. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  4568. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  4569. ClassDB::bind_method(D_METHOD("set_enable_recursive_folding", "enable"), &Tree::set_enable_recursive_folding);
  4570. ClassDB::bind_method(D_METHOD("is_recursive_folding_enabled"), &Tree::is_recursive_folding_enabled);
  4571. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  4572. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  4573. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  4574. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  4575. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  4576. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  4577. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &Tree::set_allow_search);
  4578. ClassDB::bind_method(D_METHOD("get_allow_search"), &Tree::get_allow_search);
  4579. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  4580. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "column_titles_visible"), "set_column_titles_visible", "are_column_titles_visible");
  4581. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  4582. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  4583. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  4584. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  4585. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_recursive_folding"), "set_enable_recursive_folding", "is_recursive_folding_enabled");
  4586. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  4587. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In Between"), "set_drop_mode_flags", "get_drop_mode_flags");
  4588. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  4589. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_h_scroll_enabled", "is_h_scroll_enabled");
  4590. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_v_scroll_enabled", "is_v_scroll_enabled");
  4591. ADD_SIGNAL(MethodInfo("item_selected"));
  4592. ADD_SIGNAL(MethodInfo("cell_selected"));
  4593. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  4594. ADD_SIGNAL(MethodInfo("item_mouse_selected", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  4595. ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  4596. ADD_SIGNAL(MethodInfo("item_edited"));
  4597. ADD_SIGNAL(MethodInfo("custom_item_clicked", PropertyInfo(Variant::INT, "mouse_button_index")));
  4598. ADD_SIGNAL(MethodInfo("item_icon_double_clicked"));
  4599. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  4600. ADD_SIGNAL(MethodInfo("check_propagated_to_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column")));
  4601. ADD_SIGNAL(MethodInfo("button_clicked", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "mouse_button_index")));
  4602. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  4603. ADD_SIGNAL(MethodInfo("item_activated"));
  4604. ADD_SIGNAL(MethodInfo("column_title_clicked", PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "mouse_button_index")));
  4605. ADD_SIGNAL(MethodInfo("nothing_selected"));
  4606. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  4607. BIND_ENUM_CONSTANT(SELECT_ROW);
  4608. BIND_ENUM_CONSTANT(SELECT_MULTI);
  4609. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  4610. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  4611. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  4612. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, panel_style, "panel");
  4613. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, focus_style, "focus");
  4614. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Tree, font);
  4615. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Tree, font_size);
  4616. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, Tree, tb_font, "title_button_font");
  4617. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, Tree, tb_font_size, "title_button_font_size");
  4618. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected);
  4619. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected_focus);
  4620. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor);
  4621. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor_unfocus, "cursor_unfocused");
  4622. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_pressed);
  4623. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked);
  4624. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked);
  4625. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked_disabled);
  4626. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked_disabled);
  4627. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate);
  4628. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate_disabled);
  4629. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow);
  4630. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed);
  4631. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed_mirrored);
  4632. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, select_arrow);
  4633. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, updown);
  4634. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button);
  4635. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_hover);
  4636. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_pressed);
  4637. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, custom_button_font_highlight);
  4638. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_color);
  4639. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_selected_color);
  4640. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_disabled_color);
  4641. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, drop_position_color);
  4642. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, h_separation);
  4643. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, v_separation);
  4644. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_bottom);
  4645. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_left);
  4646. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_right);
  4647. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_top);
  4648. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, item_margin);
  4649. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, button_margin);
  4650. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, icon_max_width);
  4651. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_outline_color);
  4652. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Tree, font_outline_size, "outline_size");
  4653. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_guides);
  4654. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, guide_color);
  4655. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_relationship_lines);
  4656. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, relationship_line_width);
  4657. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_width);
  4658. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, children_hl_line_width);
  4659. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_margin);
  4660. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, relationship_line_color);
  4661. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, parent_hl_line_color);
  4662. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, children_hl_line_color);
  4663. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_border);
  4664. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_speed);
  4665. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_top);
  4666. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_right);
  4667. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_bottom);
  4668. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_left);
  4669. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_h_separation);
  4670. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_v_separation);
  4671. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button, "title_button_normal");
  4672. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_pressed);
  4673. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_hover);
  4674. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, title_button_color);
  4675. }
  4676. Tree::Tree() {
  4677. columns.resize(1);
  4678. set_focus_mode(FOCUS_ALL);
  4679. popup_menu = memnew(PopupMenu);
  4680. popup_menu->hide();
  4681. add_child(popup_menu, false, INTERNAL_MODE_FRONT);
  4682. popup_editor = memnew(Popup);
  4683. add_child(popup_editor, false, INTERNAL_MODE_FRONT);
  4684. popup_editor_vb = memnew(VBoxContainer);
  4685. popup_editor_vb->add_theme_constant_override("separation", 0);
  4686. popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  4687. popup_editor->add_child(popup_editor_vb);
  4688. line_editor = memnew(LineEdit);
  4689. line_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  4690. line_editor->hide();
  4691. popup_editor_vb->add_child(line_editor);
  4692. text_editor = memnew(TextEdit);
  4693. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  4694. text_editor->hide();
  4695. popup_editor_vb->add_child(text_editor);
  4696. value_editor = memnew(HSlider);
  4697. value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  4698. value_editor->hide();
  4699. popup_editor_vb->add_child(value_editor);
  4700. h_scroll = memnew(HScrollBar);
  4701. v_scroll = memnew(VScrollBar);
  4702. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  4703. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  4704. range_click_timer = memnew(Timer);
  4705. range_click_timer->connect("timeout", callable_mp(this, &Tree::_range_click_timeout));
  4706. add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
  4707. h_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
  4708. v_scroll->connect("value_changed", callable_mp(this, &Tree::_scroll_moved));
  4709. line_editor->connect("text_submitted", callable_mp(this, &Tree::_line_editor_submit));
  4710. text_editor->connect("gui_input", callable_mp(this, &Tree::_text_editor_gui_input));
  4711. popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_popup_modal_close));
  4712. popup_menu->connect("id_pressed", callable_mp(this, &Tree::popup_select));
  4713. value_editor->connect("value_changed", callable_mp(this, &Tree::value_editor_changed));
  4714. set_notify_transform(true);
  4715. set_mouse_filter(MOUSE_FILTER_STOP);
  4716. set_clip_contents(true);
  4717. }
  4718. Tree::~Tree() {
  4719. if (root) {
  4720. memdelete(root);
  4721. }
  4722. }