tree.cpp 175 KB

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