tree.cpp 173 KB

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