tree.cpp 173 KB

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