tree.cpp 178 KB

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