canvas_item_editor_plugin.cpp 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871
  1. /*************************************************************************/
  2. /* canvas_item_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "canvas_item_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/math/geometry_2d.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/string/print_string.h"
  36. #include "editor/debugger/editor_debugger_node.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_toaster.h"
  41. #include "editor/plugins/animation_player_editor_plugin.h"
  42. #include "editor/plugins/script_editor_plugin.h"
  43. #include "editor/scene_tree_dock.h"
  44. #include "scene/2d/cpu_particles_2d.h"
  45. #include "scene/2d/gpu_particles_2d.h"
  46. #include "scene/2d/light_2d.h"
  47. #include "scene/2d/polygon_2d.h"
  48. #include "scene/2d/skeleton_2d.h"
  49. #include "scene/2d/sprite_2d.h"
  50. #include "scene/2d/touch_screen_button.h"
  51. #include "scene/gui/grid_container.h"
  52. #include "scene/gui/nine_patch_rect.h"
  53. #include "scene/gui/subviewport_container.h"
  54. #include "scene/gui/view_panner.h"
  55. #include "scene/main/canvas_layer.h"
  56. #include "scene/main/window.h"
  57. #include "scene/resources/packed_scene.h"
  58. // Min and Max are power of two in order to play nicely with successive increment.
  59. // That way, we can naturally reach a 100% zoom from boundaries.
  60. constexpr real_t MIN_ZOOM = 1. / 128;
  61. constexpr real_t MAX_ZOOM = 128;
  62. #define RULER_WIDTH (15 * EDSCALE)
  63. constexpr real_t SCALE_HANDLE_DISTANCE = 25;
  64. constexpr real_t MOVE_HANDLE_DISTANCE = 25;
  65. class SnapDialog : public ConfirmationDialog {
  66. GDCLASS(SnapDialog, ConfirmationDialog);
  67. friend class CanvasItemEditor;
  68. SpinBox *grid_offset_x;
  69. SpinBox *grid_offset_y;
  70. SpinBox *grid_step_x;
  71. SpinBox *grid_step_y;
  72. SpinBox *primary_grid_steps;
  73. SpinBox *rotation_offset;
  74. SpinBox *rotation_step;
  75. SpinBox *scale_step;
  76. public:
  77. SnapDialog() {
  78. const int SPIN_BOX_GRID_RANGE = 16384;
  79. const int SPIN_BOX_ROTATION_RANGE = 360;
  80. const real_t SPIN_BOX_SCALE_MIN = 0.01;
  81. const real_t SPIN_BOX_SCALE_MAX = 100;
  82. Label *label;
  83. VBoxContainer *container;
  84. GridContainer *child_container;
  85. set_title(TTR("Configure Snap"));
  86. container = memnew(VBoxContainer);
  87. add_child(container);
  88. child_container = memnew(GridContainer);
  89. child_container->set_columns(3);
  90. container->add_child(child_container);
  91. label = memnew(Label);
  92. label->set_text(TTR("Grid Offset:"));
  93. child_container->add_child(label);
  94. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  95. grid_offset_x = memnew(SpinBox);
  96. grid_offset_x->set_min(-SPIN_BOX_GRID_RANGE);
  97. grid_offset_x->set_max(SPIN_BOX_GRID_RANGE);
  98. grid_offset_x->set_allow_lesser(true);
  99. grid_offset_x->set_allow_greater(true);
  100. grid_offset_x->set_suffix("px");
  101. grid_offset_x->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  102. child_container->add_child(grid_offset_x);
  103. grid_offset_y = memnew(SpinBox);
  104. grid_offset_y->set_min(-SPIN_BOX_GRID_RANGE);
  105. grid_offset_y->set_max(SPIN_BOX_GRID_RANGE);
  106. grid_offset_y->set_allow_lesser(true);
  107. grid_offset_y->set_allow_greater(true);
  108. grid_offset_y->set_suffix("px");
  109. grid_offset_y->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  110. child_container->add_child(grid_offset_y);
  111. label = memnew(Label);
  112. label->set_text(TTR("Grid Step:"));
  113. child_container->add_child(label);
  114. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  115. grid_step_x = memnew(SpinBox);
  116. grid_step_x->set_min(0.01);
  117. grid_step_x->set_max(SPIN_BOX_GRID_RANGE);
  118. grid_step_x->set_allow_greater(true);
  119. grid_step_x->set_suffix("px");
  120. grid_step_x->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  121. child_container->add_child(grid_step_x);
  122. grid_step_y = memnew(SpinBox);
  123. grid_step_y->set_min(0.01);
  124. grid_step_y->set_max(SPIN_BOX_GRID_RANGE);
  125. grid_step_y->set_allow_greater(true);
  126. grid_step_y->set_suffix("px");
  127. grid_step_y->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  128. child_container->add_child(grid_step_y);
  129. child_container = memnew(GridContainer);
  130. child_container->set_columns(2);
  131. container->add_child(child_container);
  132. label = memnew(Label);
  133. label->set_text(TTR("Primary Line Every:"));
  134. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  135. child_container->add_child(label);
  136. primary_grid_steps = memnew(SpinBox);
  137. primary_grid_steps->set_min(0);
  138. primary_grid_steps->set_step(1);
  139. primary_grid_steps->set_max(100);
  140. primary_grid_steps->set_allow_greater(true);
  141. primary_grid_steps->set_suffix(TTR("steps"));
  142. primary_grid_steps->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  143. child_container->add_child(primary_grid_steps);
  144. container->add_child(memnew(HSeparator));
  145. // We need to create another GridContainer with the same column count,
  146. // so we can put an HSeparator above
  147. child_container = memnew(GridContainer);
  148. child_container->set_columns(2);
  149. container->add_child(child_container);
  150. label = memnew(Label);
  151. label->set_text(TTR("Rotation Offset:"));
  152. child_container->add_child(label);
  153. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  154. rotation_offset = memnew(SpinBox);
  155. rotation_offset->set_min(-SPIN_BOX_ROTATION_RANGE);
  156. rotation_offset->set_max(SPIN_BOX_ROTATION_RANGE);
  157. rotation_offset->set_suffix("deg");
  158. rotation_offset->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  159. child_container->add_child(rotation_offset);
  160. label = memnew(Label);
  161. label->set_text(TTR("Rotation Step:"));
  162. child_container->add_child(label);
  163. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  164. rotation_step = memnew(SpinBox);
  165. rotation_step->set_min(-SPIN_BOX_ROTATION_RANGE);
  166. rotation_step->set_max(SPIN_BOX_ROTATION_RANGE);
  167. rotation_step->set_suffix("deg");
  168. rotation_step->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  169. child_container->add_child(rotation_step);
  170. container->add_child(memnew(HSeparator));
  171. child_container = memnew(GridContainer);
  172. child_container->set_columns(2);
  173. container->add_child(child_container);
  174. label = memnew(Label);
  175. label->set_text(TTR("Scale Step:"));
  176. child_container->add_child(label);
  177. label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  178. scale_step = memnew(SpinBox);
  179. scale_step->set_min(SPIN_BOX_SCALE_MIN);
  180. scale_step->set_max(SPIN_BOX_SCALE_MAX);
  181. scale_step->set_allow_greater(true);
  182. scale_step->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  183. scale_step->set_step(0.01f);
  184. child_container->add_child(scale_step);
  185. }
  186. void set_fields(const Point2 p_grid_offset, const Point2 p_grid_step, const int p_primary_grid_steps, const real_t p_rotation_offset, const real_t p_rotation_step, const real_t p_scale_step) {
  187. grid_offset_x->set_value(p_grid_offset.x);
  188. grid_offset_y->set_value(p_grid_offset.y);
  189. grid_step_x->set_value(p_grid_step.x);
  190. grid_step_y->set_value(p_grid_step.y);
  191. primary_grid_steps->set_value(p_primary_grid_steps);
  192. rotation_offset->set_value(Math::rad2deg(p_rotation_offset));
  193. rotation_step->set_value(Math::rad2deg(p_rotation_step));
  194. scale_step->set_value(p_scale_step);
  195. }
  196. void get_fields(Point2 &p_grid_offset, Point2 &p_grid_step, int &p_primary_grid_steps, real_t &p_rotation_offset, real_t &p_rotation_step, real_t &p_scale_step) {
  197. p_grid_offset = Point2(grid_offset_x->get_value(), grid_offset_y->get_value());
  198. p_grid_step = Point2(grid_step_x->get_value(), grid_step_y->get_value());
  199. p_primary_grid_steps = int(primary_grid_steps->get_value());
  200. p_rotation_offset = Math::deg2rad(rotation_offset->get_value());
  201. p_rotation_step = Math::deg2rad(rotation_step->get_value());
  202. p_scale_step = scale_step->get_value();
  203. }
  204. };
  205. bool CanvasItemEditor::_is_node_locked(const Node *p_node) {
  206. return p_node->has_meta("_edit_lock_") && p_node->get_meta("_edit_lock_");
  207. }
  208. bool CanvasItemEditor::_is_node_movable(const Node *p_node, bool p_popup_warning) {
  209. if (_is_node_locked(p_node)) {
  210. return false;
  211. }
  212. if (Object::cast_to<Control>(p_node) && Object::cast_to<Container>(p_node->get_parent())) {
  213. if (p_popup_warning) {
  214. EditorToaster::get_singleton()->popup_str("Children of a container get their position and size determined only by their parent.", EditorToaster::SEVERITY_WARNING);
  215. }
  216. return false;
  217. }
  218. return true;
  219. }
  220. void CanvasItemEditor::_snap_if_closer_float(
  221. const real_t p_value,
  222. real_t &r_current_snap, SnapTarget &r_current_snap_target,
  223. const real_t p_target_value, const SnapTarget p_snap_target,
  224. const real_t p_radius) {
  225. const real_t radius = p_radius / zoom;
  226. const real_t dist = Math::abs(p_value - p_target_value);
  227. if ((p_radius < 0 || dist < radius) && (r_current_snap_target == SNAP_TARGET_NONE || dist < Math::abs(r_current_snap - p_value))) {
  228. r_current_snap = p_target_value;
  229. r_current_snap_target = p_snap_target;
  230. }
  231. }
  232. void CanvasItemEditor::_snap_if_closer_point(
  233. Point2 p_value,
  234. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  235. Point2 p_target_value, const SnapTarget p_snap_target,
  236. const real_t rotation,
  237. const real_t p_radius) {
  238. Transform2D rot_trans = Transform2D(rotation, Point2());
  239. p_value = rot_trans.inverse().xform(p_value);
  240. p_target_value = rot_trans.inverse().xform(p_target_value);
  241. r_current_snap = rot_trans.inverse().xform(r_current_snap);
  242. _snap_if_closer_float(
  243. p_value.x,
  244. r_current_snap.x,
  245. r_current_snap_target[0],
  246. p_target_value.x,
  247. p_snap_target,
  248. p_radius);
  249. _snap_if_closer_float(
  250. p_value.y,
  251. r_current_snap.y,
  252. r_current_snap_target[1],
  253. p_target_value.y,
  254. p_snap_target,
  255. p_radius);
  256. r_current_snap = rot_trans.xform(r_current_snap);
  257. }
  258. void CanvasItemEditor::_snap_other_nodes(
  259. const Point2 p_value,
  260. const Transform2D p_transform_to_snap,
  261. Point2 &r_current_snap, SnapTarget (&r_current_snap_target)[2],
  262. const SnapTarget p_snap_target, List<const CanvasItem *> p_exceptions,
  263. const Node *p_current) {
  264. const CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_current);
  265. // Check if the element is in the exception
  266. bool exception = false;
  267. for (const CanvasItem *&E : p_exceptions) {
  268. if (E == p_current) {
  269. exception = true;
  270. break;
  271. }
  272. };
  273. if (canvas_item && !exception) {
  274. Transform2D ci_transform = canvas_item->get_global_transform_with_canvas();
  275. if (fmod(ci_transform.get_rotation() - p_transform_to_snap.get_rotation(), (real_t)360.0) == 0.0) {
  276. if (canvas_item->_edit_use_rect()) {
  277. Point2 begin = ci_transform.xform(canvas_item->_edit_get_rect().get_position());
  278. Point2 end = ci_transform.xform(canvas_item->_edit_get_rect().get_position() + canvas_item->_edit_get_rect().get_size());
  279. _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, begin, p_snap_target, ci_transform.get_rotation());
  280. _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, end, p_snap_target, ci_transform.get_rotation());
  281. } else {
  282. Point2 position = ci_transform.xform(Point2());
  283. _snap_if_closer_point(p_value, r_current_snap, r_current_snap_target, position, p_snap_target, ci_transform.get_rotation());
  284. }
  285. }
  286. }
  287. for (int i = 0; i < p_current->get_child_count(); i++) {
  288. _snap_other_nodes(p_value, p_transform_to_snap, r_current_snap, r_current_snap_target, p_snap_target, p_exceptions, p_current->get_child(i));
  289. }
  290. }
  291. Point2 CanvasItemEditor::snap_point(Point2 p_target, unsigned int p_modes, unsigned int p_forced_modes, const CanvasItem *p_self_canvas_item, List<CanvasItem *> p_other_nodes_exceptions) {
  292. snap_target[0] = SNAP_TARGET_NONE;
  293. snap_target[1] = SNAP_TARGET_NONE;
  294. bool is_snap_active = smart_snap_active ^ Input::get_singleton()->is_key_pressed(Key::CTRL);
  295. // Smart snap using the canvas position
  296. Vector2 output = p_target;
  297. real_t rotation = 0.0;
  298. if (p_self_canvas_item) {
  299. rotation = p_self_canvas_item->get_global_transform_with_canvas().get_rotation();
  300. // Parent sides and center
  301. if ((is_snap_active && snap_node_parent && (p_modes & SNAP_NODE_PARENT)) || (p_forced_modes & SNAP_NODE_PARENT)) {
  302. if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
  303. Point2 begin = p_self_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(0, 0)));
  304. Point2 end = p_self_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(1, 1)));
  305. _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
  306. _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
  307. _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
  308. } else if (const CanvasItem *parent_ci = Object::cast_to<CanvasItem>(p_self_canvas_item->get_parent())) {
  309. if (parent_ci->_edit_use_rect()) {
  310. Point2 begin = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position());
  311. Point2 end = p_self_canvas_item->get_transform().affine_inverse().xform(parent_ci->_edit_get_rect().get_position() + parent_ci->_edit_get_rect().get_size());
  312. _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_PARENT, rotation);
  313. _snap_if_closer_point(p_target, output, snap_target, (begin + end) / 2.0, SNAP_TARGET_PARENT, rotation);
  314. _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_PARENT, rotation);
  315. } else {
  316. Point2 position = p_self_canvas_item->get_transform().affine_inverse().xform(Point2());
  317. _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_PARENT, rotation);
  318. }
  319. }
  320. }
  321. // Self anchors
  322. if ((is_snap_active && snap_node_anchors && (p_modes & SNAP_NODE_ANCHORS)) || (p_forced_modes & SNAP_NODE_ANCHORS)) {
  323. if (const Control *c = Object::cast_to<Control>(p_self_canvas_item)) {
  324. Point2 begin = p_self_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(c->get_anchor(SIDE_LEFT), c->get_anchor(SIDE_TOP))));
  325. Point2 end = p_self_canvas_item->get_global_transform_with_canvas().xform(_anchor_to_position(c, Point2(c->get_anchor(SIDE_RIGHT), c->get_anchor(SIDE_BOTTOM))));
  326. _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF_ANCHORS, rotation);
  327. _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF_ANCHORS, rotation);
  328. }
  329. }
  330. // Self sides
  331. if ((is_snap_active && snap_node_sides && (p_modes & SNAP_NODE_SIDES)) || (p_forced_modes & SNAP_NODE_SIDES)) {
  332. if (p_self_canvas_item->_edit_use_rect()) {
  333. Point2 begin = p_self_canvas_item->get_global_transform_with_canvas().xform(p_self_canvas_item->_edit_get_rect().get_position());
  334. Point2 end = p_self_canvas_item->get_global_transform_with_canvas().xform(p_self_canvas_item->_edit_get_rect().get_position() + p_self_canvas_item->_edit_get_rect().get_size());
  335. _snap_if_closer_point(p_target, output, snap_target, begin, SNAP_TARGET_SELF, rotation);
  336. _snap_if_closer_point(p_target, output, snap_target, end, SNAP_TARGET_SELF, rotation);
  337. }
  338. }
  339. // Self center
  340. if ((is_snap_active && snap_node_center && (p_modes & SNAP_NODE_CENTER)) || (p_forced_modes & SNAP_NODE_CENTER)) {
  341. if (p_self_canvas_item->_edit_use_rect()) {
  342. Point2 center = p_self_canvas_item->get_global_transform_with_canvas().xform(p_self_canvas_item->_edit_get_rect().get_center());
  343. _snap_if_closer_point(p_target, output, snap_target, center, SNAP_TARGET_SELF, rotation);
  344. } else {
  345. Point2 position = p_self_canvas_item->get_global_transform_with_canvas().xform(Point2());
  346. _snap_if_closer_point(p_target, output, snap_target, position, SNAP_TARGET_SELF, rotation);
  347. }
  348. }
  349. }
  350. // Other nodes sides
  351. if ((is_snap_active && snap_other_nodes && (p_modes & SNAP_OTHER_NODES)) || (p_forced_modes & SNAP_OTHER_NODES)) {
  352. Transform2D to_snap_transform = Transform2D();
  353. List<const CanvasItem *> exceptions = List<const CanvasItem *>();
  354. for (const CanvasItem *E : p_other_nodes_exceptions) {
  355. exceptions.push_back(E);
  356. }
  357. if (p_self_canvas_item) {
  358. exceptions.push_back(p_self_canvas_item);
  359. to_snap_transform = p_self_canvas_item->get_global_transform_with_canvas();
  360. }
  361. _snap_other_nodes(
  362. p_target, to_snap_transform,
  363. output, snap_target,
  364. SNAP_TARGET_OTHER_NODE,
  365. exceptions,
  366. get_tree()->get_edited_scene_root());
  367. }
  368. if (((is_snap_active && snap_guides && (p_modes & SNAP_GUIDES)) || (p_forced_modes & SNAP_GUIDES)) && fmod(rotation, (real_t)360.0) == 0.0) {
  369. // Guides
  370. if (EditorNode::get_singleton()->get_edited_scene() && EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_vertical_guides_")) {
  371. Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_");
  372. for (int i = 0; i < vguides.size(); i++) {
  373. _snap_if_closer_float(p_target.x, output.x, snap_target[0], vguides[i], SNAP_TARGET_GUIDE);
  374. }
  375. }
  376. if (EditorNode::get_singleton()->get_edited_scene() && EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_horizontal_guides_")) {
  377. Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_");
  378. for (int i = 0; i < hguides.size(); i++) {
  379. _snap_if_closer_float(p_target.y, output.y, snap_target[1], hguides[i], SNAP_TARGET_GUIDE);
  380. }
  381. }
  382. }
  383. if (((grid_snap_active && (p_modes & SNAP_GRID)) || (p_forced_modes & SNAP_GRID)) && fmod(rotation, (real_t)360.0) == 0.0) {
  384. // Grid
  385. Point2 offset = grid_offset;
  386. if (snap_relative) {
  387. List<CanvasItem *> selection = _get_edited_canvas_items();
  388. if (selection.size() == 1 && Object::cast_to<Node2D>(selection[0])) {
  389. offset = Object::cast_to<Node2D>(selection[0])->get_global_position();
  390. } else if (selection.size() > 0) {
  391. offset = _get_encompassing_rect_from_list(selection).position;
  392. }
  393. }
  394. Point2 grid_output;
  395. grid_output.x = Math::snapped(p_target.x - offset.x, grid_step.x * Math::pow(2.0, grid_step_multiplier)) + offset.x;
  396. grid_output.y = Math::snapped(p_target.y - offset.y, grid_step.y * Math::pow(2.0, grid_step_multiplier)) + offset.y;
  397. _snap_if_closer_point(p_target, output, snap_target, grid_output, SNAP_TARGET_GRID, 0.0, -1.0);
  398. }
  399. if (((snap_pixel && (p_modes & SNAP_PIXEL)) || (p_forced_modes & SNAP_PIXEL)) && rotation == 0.0) {
  400. // Pixel
  401. output = output.snapped(Size2(1, 1));
  402. }
  403. snap_transform = Transform2D(rotation, output);
  404. return output;
  405. }
  406. real_t CanvasItemEditor::snap_angle(real_t p_target, real_t p_start) const {
  407. if (((smart_snap_active || snap_rotation) ^ Input::get_singleton()->is_key_pressed(Key::CTRL)) && snap_rotation_step != 0) {
  408. if (snap_relative) {
  409. return Math::snapped(p_target - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset + (p_start - (int)(p_start / snap_rotation_step) * snap_rotation_step);
  410. } else {
  411. return Math::snapped(p_target - snap_rotation_offset, snap_rotation_step) + snap_rotation_offset;
  412. }
  413. } else {
  414. return p_target;
  415. }
  416. }
  417. void CanvasItemEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
  418. ERR_FAIL_COND(p_ev.is_null());
  419. Ref<InputEventKey> k = p_ev;
  420. if (!is_visible_in_tree()) {
  421. return;
  422. }
  423. if (k.is_valid()) {
  424. if (k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT) {
  425. viewport->update();
  426. }
  427. if (k->is_pressed() && !k->is_ctrl_pressed() && !k->is_echo() && (grid_snap_active || _is_grid_visible())) {
  428. if (multiply_grid_step_shortcut.is_valid() && multiply_grid_step_shortcut->matches_event(p_ev)) {
  429. // Multiply the grid size
  430. grid_step_multiplier = MIN(grid_step_multiplier + 1, 12);
  431. viewport->update();
  432. } else if (divide_grid_step_shortcut.is_valid() && divide_grid_step_shortcut->matches_event(p_ev)) {
  433. // Divide the grid size
  434. Point2 new_grid_step = grid_step * Math::pow(2.0, grid_step_multiplier - 1);
  435. if (new_grid_step.x >= 1.0 && new_grid_step.y >= 1.0) {
  436. grid_step_multiplier--;
  437. }
  438. viewport->update();
  439. }
  440. }
  441. }
  442. }
  443. Object *CanvasItemEditor::_get_editor_data(Object *p_what) {
  444. CanvasItem *ci = Object::cast_to<CanvasItem>(p_what);
  445. if (!ci) {
  446. return nullptr;
  447. }
  448. return memnew(CanvasItemEditorSelectedItem);
  449. }
  450. void CanvasItemEditor::_keying_changed() {
  451. if (AnimationPlayerEditor::get_singleton()->get_track_editor()->is_visible_in_tree()) {
  452. animation_hb->show();
  453. } else {
  454. animation_hb->hide();
  455. }
  456. }
  457. Rect2 CanvasItemEditor::_get_encompassing_rect_from_list(List<CanvasItem *> p_list) {
  458. ERR_FAIL_COND_V(p_list.is_empty(), Rect2());
  459. // Handles the first element
  460. CanvasItem *canvas_item = p_list.front()->get();
  461. Rect2 rect = Rect2(canvas_item->get_global_transform_with_canvas().xform(canvas_item->_edit_get_rect().get_center()), Size2());
  462. // Expand with the other ones
  463. for (CanvasItem *canvas_item2 : p_list) {
  464. Transform2D xform = canvas_item2->get_global_transform_with_canvas();
  465. Rect2 current_rect = canvas_item2->_edit_get_rect();
  466. rect.expand_to(xform.xform(current_rect.position));
  467. rect.expand_to(xform.xform(current_rect.position + Vector2(current_rect.size.x, 0)));
  468. rect.expand_to(xform.xform(current_rect.position + current_rect.size));
  469. rect.expand_to(xform.xform(current_rect.position + Vector2(0, current_rect.size.y)));
  470. }
  471. return rect;
  472. }
  473. void CanvasItemEditor::_expand_encompassing_rect_using_children(Rect2 &r_rect, const Node *p_node, bool &r_first, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform, bool include_locked_nodes) {
  474. if (!p_node) {
  475. return;
  476. }
  477. if (Object::cast_to<Viewport>(p_node)) {
  478. return;
  479. }
  480. const CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node);
  481. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  482. if (canvas_item && !canvas_item->is_set_as_top_level()) {
  483. _expand_encompassing_rect_using_children(r_rect, p_node->get_child(i), r_first, p_parent_xform * canvas_item->get_transform(), p_canvas_xform);
  484. } else {
  485. const CanvasLayer *canvas_layer = Object::cast_to<CanvasLayer>(p_node);
  486. _expand_encompassing_rect_using_children(r_rect, p_node->get_child(i), r_first, Transform2D(), canvas_layer ? canvas_layer->get_transform() : p_canvas_xform);
  487. }
  488. }
  489. if (canvas_item && canvas_item->is_visible_in_tree() && (include_locked_nodes || !_is_node_locked(canvas_item))) {
  490. Transform2D xform = p_parent_xform * p_canvas_xform * canvas_item->get_transform();
  491. Rect2 rect = canvas_item->_edit_get_rect();
  492. if (r_first) {
  493. r_rect = Rect2(xform.xform(rect.get_center()), Size2());
  494. r_first = false;
  495. }
  496. r_rect.expand_to(xform.xform(rect.position));
  497. r_rect.expand_to(xform.xform(rect.position + Point2(rect.size.x, 0)));
  498. r_rect.expand_to(xform.xform(rect.position + Point2(0, rect.size.y)));
  499. r_rect.expand_to(xform.xform(rect.position + rect.size));
  500. }
  501. }
  502. Rect2 CanvasItemEditor::_get_encompassing_rect(const Node *p_node) {
  503. Rect2 rect;
  504. bool first = true;
  505. _expand_encompassing_rect_using_children(rect, p_node, first);
  506. return rect;
  507. }
  508. void CanvasItemEditor::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_node, Vector<_SelectResult> &r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
  509. if (!p_node) {
  510. return;
  511. }
  512. if (Object::cast_to<Viewport>(p_node)) {
  513. return;
  514. }
  515. const real_t grab_distance = EDITOR_GET("editors/polygon_editor/point_grab_radius");
  516. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node);
  517. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  518. if (canvas_item) {
  519. if (!canvas_item->is_set_as_top_level()) {
  520. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, p_parent_xform * canvas_item->get_transform(), p_canvas_xform);
  521. } else {
  522. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, canvas_item->get_transform(), p_canvas_xform);
  523. }
  524. } else {
  525. CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
  526. _find_canvas_items_at_pos(p_pos, p_node->get_child(i), r_items, Transform2D(), cl ? cl->get_transform() : p_canvas_xform);
  527. }
  528. }
  529. if (canvas_item && canvas_item->is_visible_in_tree()) {
  530. Transform2D xform = (p_parent_xform * p_canvas_xform * canvas_item->get_transform()).affine_inverse();
  531. const real_t local_grab_distance = xform.basis_xform(Vector2(grab_distance, 0)).length() / zoom;
  532. if (canvas_item->_edit_is_selected_on_click(xform.xform(p_pos), local_grab_distance)) {
  533. Node2D *node = Object::cast_to<Node2D>(canvas_item);
  534. _SelectResult res;
  535. res.item = canvas_item;
  536. res.z_index = node ? node->get_z_index() : 0;
  537. res.has_z = node;
  538. r_items.push_back(res);
  539. }
  540. }
  541. }
  542. void CanvasItemEditor::_get_canvas_items_at_pos(const Point2 &p_pos, Vector<_SelectResult> &r_items, bool p_allow_locked) {
  543. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  544. _find_canvas_items_at_pos(p_pos, scene, r_items);
  545. //Remove invalid results
  546. for (int i = 0; i < r_items.size(); i++) {
  547. Node *node = r_items[i].item;
  548. // Make sure the selected node is in the current scene, or editable
  549. if (node && node != get_tree()->get_edited_scene_root()) {
  550. node = scene->get_deepest_editable_node(node);
  551. }
  552. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(node);
  553. if (!p_allow_locked) {
  554. // Replace the node by the group if grouped
  555. while (node && node != scene->get_parent()) {
  556. CanvasItem *canvas_item_tmp = Object::cast_to<CanvasItem>(node);
  557. if (canvas_item_tmp && node->has_meta("_edit_group_")) {
  558. canvas_item = canvas_item_tmp;
  559. }
  560. node = node->get_parent();
  561. }
  562. }
  563. // Check if the canvas item is already in the list (for groups or scenes)
  564. bool duplicate = false;
  565. for (int j = 0; j < i; j++) {
  566. if (r_items[j].item == canvas_item) {
  567. duplicate = true;
  568. break;
  569. }
  570. }
  571. //Remove the item if invalid
  572. if (!canvas_item || duplicate || (canvas_item != scene && canvas_item->get_owner() != scene && !scene->is_editable_instance(canvas_item->get_owner())) || (!p_allow_locked && _is_node_locked(canvas_item))) {
  573. r_items.remove_at(i);
  574. i--;
  575. } else {
  576. r_items.write[i].item = canvas_item;
  577. }
  578. }
  579. }
  580. void CanvasItemEditor::_find_canvas_items_in_rect(const Rect2 &p_rect, Node *p_node, List<CanvasItem *> *r_items, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
  581. if (!p_node) {
  582. return;
  583. }
  584. if (Object::cast_to<Viewport>(p_node)) {
  585. return;
  586. }
  587. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node);
  588. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  589. bool editable = p_node == scene || p_node->get_owner() == scene || p_node == scene->get_deepest_editable_node(p_node);
  590. bool lock_children = p_node->has_meta("_edit_group_") && p_node->get_meta("_edit_group_");
  591. bool locked = _is_node_locked(p_node);
  592. if (!lock_children || !editable) {
  593. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  594. if (canvas_item) {
  595. if (!canvas_item->is_set_as_top_level()) {
  596. _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, p_parent_xform * canvas_item->get_transform(), p_canvas_xform);
  597. } else {
  598. _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, canvas_item->get_transform(), p_canvas_xform);
  599. }
  600. } else {
  601. CanvasLayer *canvas_layer = Object::cast_to<CanvasLayer>(p_node);
  602. _find_canvas_items_in_rect(p_rect, p_node->get_child(i), r_items, Transform2D(), canvas_layer ? canvas_layer->get_transform() : p_canvas_xform);
  603. }
  604. }
  605. }
  606. if (canvas_item && canvas_item->is_visible_in_tree() && !locked && editable) {
  607. Transform2D xform = p_parent_xform * p_canvas_xform * canvas_item->get_transform();
  608. if (canvas_item->_edit_use_rect()) {
  609. Rect2 rect = canvas_item->_edit_get_rect();
  610. if (p_rect.has_point(xform.xform(rect.position)) &&
  611. p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, 0))) &&
  612. p_rect.has_point(xform.xform(rect.position + Vector2(rect.size.x, rect.size.y))) &&
  613. p_rect.has_point(xform.xform(rect.position + Vector2(0, rect.size.y)))) {
  614. r_items->push_back(canvas_item);
  615. }
  616. } else {
  617. if (p_rect.has_point(xform.xform(Point2()))) {
  618. r_items->push_back(canvas_item);
  619. }
  620. }
  621. }
  622. }
  623. bool CanvasItemEditor::_select_click_on_item(CanvasItem *item, Point2 p_click_pos, bool p_append) {
  624. bool still_selected = true;
  625. if (p_append && !editor_selection->get_selected_node_list().is_empty()) {
  626. if (editor_selection->is_selected(item)) {
  627. // Already in the selection, remove it from the selected nodes
  628. editor_selection->remove_node(item);
  629. still_selected = false;
  630. if (editor_selection->get_selected_node_list().size() == 1) {
  631. EditorNode::get_singleton()->push_item(editor_selection->get_selected_node_list()[0]);
  632. }
  633. } else {
  634. // Add the item to the selection
  635. editor_selection->add_node(item);
  636. }
  637. } else {
  638. if (!editor_selection->is_selected(item)) {
  639. // Select a new one and clear previous selection
  640. editor_selection->clear();
  641. editor_selection->add_node(item);
  642. // Reselect
  643. if (Engine::get_singleton()->is_editor_hint()) {
  644. selected_from_canvas = true;
  645. EditorNode::get_singleton()->edit_node(item);
  646. }
  647. }
  648. }
  649. viewport->update();
  650. return still_selected;
  651. }
  652. List<CanvasItem *> CanvasItemEditor::_get_edited_canvas_items(bool retrieve_locked, bool remove_canvas_item_if_parent_in_selection) {
  653. List<CanvasItem *> selection;
  654. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  655. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E.key);
  656. if (canvas_item && canvas_item->is_visible_in_tree() && canvas_item->get_viewport() == EditorNode::get_singleton()->get_scene_root() && (retrieve_locked || !_is_node_locked(canvas_item))) {
  657. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  658. if (se) {
  659. selection.push_back(canvas_item);
  660. }
  661. }
  662. }
  663. if (remove_canvas_item_if_parent_in_selection) {
  664. List<CanvasItem *> filtered_selection;
  665. for (CanvasItem *E : selection) {
  666. if (!selection.find(E->get_parent())) {
  667. filtered_selection.push_back(E);
  668. }
  669. }
  670. return filtered_selection;
  671. } else {
  672. return selection;
  673. }
  674. }
  675. Vector2 CanvasItemEditor::_anchor_to_position(const Control *p_control, Vector2 anchor) {
  676. ERR_FAIL_COND_V(!p_control, Vector2());
  677. Transform2D parent_transform = p_control->get_transform().affine_inverse();
  678. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  679. if (p_control->is_layout_rtl()) {
  680. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x - parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  681. } else {
  682. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  683. }
  684. }
  685. Vector2 CanvasItemEditor::_position_to_anchor(const Control *p_control, Vector2 position) {
  686. ERR_FAIL_COND_V(!p_control, Vector2());
  687. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  688. Vector2 output = Vector2();
  689. if (p_control->is_layout_rtl()) {
  690. output.x = (parent_rect.size.x == 0) ? 0.0 : (parent_rect.size.x - p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  691. } else {
  692. output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  693. }
  694. output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
  695. return output;
  696. }
  697. void CanvasItemEditor::_save_canvas_item_state(List<CanvasItem *> p_canvas_items, bool save_bones) {
  698. for (CanvasItem *canvas_item : p_canvas_items) {
  699. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  700. if (se) {
  701. se->undo_state = canvas_item->_edit_get_state();
  702. se->pre_drag_xform = canvas_item->get_global_transform_with_canvas();
  703. if (canvas_item->_edit_use_rect()) {
  704. se->pre_drag_rect = canvas_item->_edit_get_rect();
  705. } else {
  706. se->pre_drag_rect = Rect2();
  707. }
  708. }
  709. }
  710. }
  711. void CanvasItemEditor::_restore_canvas_item_state(List<CanvasItem *> p_canvas_items, bool restore_bones) {
  712. for (CanvasItem *canvas_item : drag_selection) {
  713. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  714. canvas_item->_edit_set_state(se->undo_state);
  715. }
  716. }
  717. void CanvasItemEditor::_commit_canvas_item_state(List<CanvasItem *> p_canvas_items, String action_name, bool commit_bones) {
  718. List<CanvasItem *> modified_canvas_items;
  719. for (CanvasItem *canvas_item : p_canvas_items) {
  720. Dictionary old_state = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item)->undo_state;
  721. Dictionary new_state = canvas_item->_edit_get_state();
  722. if (old_state.hash() != new_state.hash()) {
  723. modified_canvas_items.push_back(canvas_item);
  724. }
  725. }
  726. if (modified_canvas_items.is_empty()) {
  727. return;
  728. }
  729. undo_redo->create_action(action_name);
  730. for (CanvasItem *canvas_item : modified_canvas_items) {
  731. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  732. if (se) {
  733. undo_redo->add_do_method(canvas_item, "_edit_set_state", canvas_item->_edit_get_state());
  734. undo_redo->add_undo_method(canvas_item, "_edit_set_state", se->undo_state);
  735. if (commit_bones) {
  736. for (const Dictionary &F : se->pre_drag_bones_undo_state) {
  737. canvas_item = Object::cast_to<CanvasItem>(canvas_item->get_parent());
  738. undo_redo->add_do_method(canvas_item, "_edit_set_state", canvas_item->_edit_get_state());
  739. undo_redo->add_undo_method(canvas_item, "_edit_set_state", F);
  740. }
  741. }
  742. }
  743. }
  744. undo_redo->add_do_method(viewport, "update");
  745. undo_redo->add_undo_method(viewport, "update");
  746. undo_redo->commit_action();
  747. }
  748. void CanvasItemEditor::_snap_changed() {
  749. static_cast<SnapDialog *>(snap_dialog)->get_fields(grid_offset, grid_step, primary_grid_steps, snap_rotation_offset, snap_rotation_step, snap_scale_step);
  750. grid_step_multiplier = 0;
  751. viewport->update();
  752. }
  753. void CanvasItemEditor::_selection_result_pressed(int p_result) {
  754. if (selection_results.size() <= p_result) {
  755. return;
  756. }
  757. CanvasItem *item = selection_results[p_result].item;
  758. if (item) {
  759. _select_click_on_item(item, Point2(), selection_menu_additive_selection);
  760. }
  761. }
  762. void CanvasItemEditor::_selection_menu_hide() {
  763. selection_results.clear();
  764. selection_menu->clear();
  765. selection_menu->reset_size();
  766. }
  767. void CanvasItemEditor::_add_node_pressed(int p_result) {
  768. List<Node *> nodes_to_move;
  769. switch (p_result) {
  770. case ADD_NODE: {
  771. SceneTreeDock::get_singleton()->open_add_child_dialog();
  772. } break;
  773. case ADD_INSTANCE: {
  774. SceneTreeDock::get_singleton()->open_instance_child_dialog();
  775. } break;
  776. case ADD_PASTE: {
  777. nodes_to_move = SceneTreeDock::get_singleton()->paste_nodes();
  778. [[fallthrough]];
  779. }
  780. case ADD_MOVE: {
  781. if (p_result == ADD_MOVE) {
  782. nodes_to_move = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  783. }
  784. if (nodes_to_move.is_empty()) {
  785. return;
  786. }
  787. undo_redo->create_action(TTR("Move Node(s) to Position"));
  788. for (Node *node : nodes_to_move) {
  789. CanvasItem *ci = Object::cast_to<CanvasItem>(node);
  790. if (ci) {
  791. Transform2D xform = ci->get_global_transform_with_canvas().affine_inverse() * ci->get_transform();
  792. undo_redo->add_do_method(ci, "_edit_set_position", xform.xform(node_create_position));
  793. undo_redo->add_undo_method(ci, "_edit_set_position", ci->_edit_get_position());
  794. }
  795. }
  796. undo_redo->commit_action();
  797. _reset_create_position();
  798. } break;
  799. }
  800. }
  801. void CanvasItemEditor::_node_created(Node *p_node) {
  802. if (node_create_position == Point2()) {
  803. return;
  804. }
  805. CanvasItem *c = Object::cast_to<CanvasItem>(p_node);
  806. if (c) {
  807. Transform2D xform = c->get_global_transform_with_canvas().affine_inverse() * c->get_transform();
  808. c->_edit_set_position(xform.xform(node_create_position));
  809. }
  810. call_deferred(SNAME("_reset_create_position")); // Defer the call in case more than one node is added.
  811. }
  812. void CanvasItemEditor::_reset_create_position() {
  813. node_create_position = Point2();
  814. }
  815. bool CanvasItemEditor::_is_grid_visible() const {
  816. switch (grid_visibility) {
  817. case GRID_VISIBILITY_SHOW:
  818. return true;
  819. case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
  820. return grid_snap_active;
  821. case GRID_VISIBILITY_HIDE:
  822. return false;
  823. }
  824. ERR_FAIL_V_MSG(true, "Unexpected grid_visibility value");
  825. }
  826. void CanvasItemEditor::_prepare_grid_menu() {
  827. for (int i = GRID_VISIBILITY_SHOW; i <= GRID_VISIBILITY_HIDE; i++) {
  828. grid_menu->set_item_checked(i, i == grid_visibility);
  829. }
  830. }
  831. void CanvasItemEditor::_on_grid_menu_id_pressed(int p_id) {
  832. switch (p_id) {
  833. case GRID_VISIBILITY_SHOW:
  834. case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
  835. case GRID_VISIBILITY_HIDE:
  836. grid_visibility = (GridVisibility)p_id;
  837. viewport->update();
  838. view_menu->get_popup()->hide();
  839. return;
  840. }
  841. // Toggle grid: go to the least restrictive option possible.
  842. if (grid_snap_active) {
  843. switch (grid_visibility) {
  844. case GRID_VISIBILITY_SHOW:
  845. case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
  846. grid_visibility = GRID_VISIBILITY_HIDE;
  847. break;
  848. case GRID_VISIBILITY_HIDE:
  849. grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
  850. break;
  851. }
  852. } else {
  853. switch (grid_visibility) {
  854. case GRID_VISIBILITY_SHOW:
  855. grid_visibility = GRID_VISIBILITY_SHOW_WHEN_SNAPPING;
  856. break;
  857. case GRID_VISIBILITY_SHOW_WHEN_SNAPPING:
  858. case GRID_VISIBILITY_HIDE:
  859. grid_visibility = GRID_VISIBILITY_SHOW;
  860. break;
  861. }
  862. }
  863. viewport->update();
  864. }
  865. bool CanvasItemEditor::_gui_input_rulers_and_guides(const Ref<InputEvent> &p_event) {
  866. Ref<InputEventMouseButton> b = p_event;
  867. Ref<InputEventMouseMotion> m = p_event;
  868. if (drag_type == DRAG_NONE) {
  869. if (show_guides && show_rulers && EditorNode::get_singleton()->get_edited_scene()) {
  870. Transform2D xform = viewport_scrollable->get_transform() * transform;
  871. // Retrieve the guide lists
  872. Array vguides;
  873. if (EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_vertical_guides_")) {
  874. vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_");
  875. }
  876. Array hguides;
  877. if (EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_horizontal_guides_")) {
  878. hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_");
  879. }
  880. // Hover over guides
  881. real_t minimum = 1e20;
  882. is_hovering_h_guide = false;
  883. is_hovering_v_guide = false;
  884. if (m.is_valid() && m->get_position().x < RULER_WIDTH) {
  885. // Check if we are hovering an existing horizontal guide
  886. for (int i = 0; i < hguides.size(); i++) {
  887. if (ABS(xform.xform(Point2(0, hguides[i])).y - m->get_position().y) < MIN(minimum, 8)) {
  888. is_hovering_h_guide = true;
  889. is_hovering_v_guide = false;
  890. break;
  891. }
  892. }
  893. } else if (m.is_valid() && m->get_position().y < RULER_WIDTH) {
  894. // Check if we are hovering an existing vertical guide
  895. for (int i = 0; i < vguides.size(); i++) {
  896. if (ABS(xform.xform(Point2(vguides[i], 0)).x - m->get_position().x) < MIN(minimum, 8)) {
  897. is_hovering_v_guide = true;
  898. is_hovering_h_guide = false;
  899. break;
  900. }
  901. }
  902. }
  903. // Start dragging a guide
  904. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
  905. // Press button
  906. if (b->get_position().x < RULER_WIDTH && b->get_position().y < RULER_WIDTH) {
  907. // Drag a new double guide
  908. drag_type = DRAG_DOUBLE_GUIDE;
  909. dragged_guide_index = -1;
  910. return true;
  911. } else if (b->get_position().x < RULER_WIDTH) {
  912. // Check if we drag an existing horizontal guide
  913. dragged_guide_index = -1;
  914. for (int i = 0; i < hguides.size(); i++) {
  915. if (ABS(xform.xform(Point2(0, hguides[i])).y - b->get_position().y) < MIN(minimum, 8)) {
  916. dragged_guide_index = i;
  917. }
  918. }
  919. if (dragged_guide_index >= 0) {
  920. // Drag an existing horizontal guide
  921. drag_type = DRAG_H_GUIDE;
  922. } else {
  923. // Drag a new vertical guide
  924. drag_type = DRAG_V_GUIDE;
  925. }
  926. return true;
  927. } else if (b->get_position().y < RULER_WIDTH) {
  928. // Check if we drag an existing vertical guide
  929. dragged_guide_index = -1;
  930. for (int i = 0; i < vguides.size(); i++) {
  931. if (ABS(xform.xform(Point2(vguides[i], 0)).x - b->get_position().x) < MIN(minimum, 8)) {
  932. dragged_guide_index = i;
  933. }
  934. }
  935. if (dragged_guide_index >= 0) {
  936. // Drag an existing vertical guide
  937. drag_type = DRAG_V_GUIDE;
  938. } else {
  939. // Drag a new vertical guide
  940. drag_type = DRAG_H_GUIDE;
  941. }
  942. drag_from = xform.affine_inverse().xform(b->get_position());
  943. return true;
  944. }
  945. }
  946. }
  947. }
  948. if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE || drag_type == DRAG_H_GUIDE) {
  949. // Move the guide
  950. if (m.is_valid()) {
  951. Transform2D xform = viewport_scrollable->get_transform() * transform;
  952. drag_to = xform.affine_inverse().xform(m->get_position());
  953. dragged_guide_pos = xform.xform(snap_point(drag_to, SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES));
  954. viewport->update();
  955. return true;
  956. }
  957. // Release confirms the guide move
  958. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
  959. if (show_guides && EditorNode::get_singleton()->get_edited_scene()) {
  960. Transform2D xform = viewport_scrollable->get_transform() * transform;
  961. // Retrieve the guide lists
  962. Array vguides;
  963. if (EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_vertical_guides_")) {
  964. vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_");
  965. }
  966. Array hguides;
  967. if (EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_horizontal_guides_")) {
  968. hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_");
  969. }
  970. Point2 edited = snap_point(xform.affine_inverse().xform(b->get_position()), SNAP_GRID | SNAP_PIXEL | SNAP_OTHER_NODES);
  971. if (drag_type == DRAG_V_GUIDE) {
  972. Array prev_vguides = vguides.duplicate();
  973. if (b->get_position().x > RULER_WIDTH) {
  974. // Adds a new vertical guide
  975. if (dragged_guide_index >= 0) {
  976. vguides[dragged_guide_index] = edited.x;
  977. undo_redo->create_action(TTR("Move Vertical Guide"));
  978. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
  979. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
  980. undo_redo->add_undo_method(viewport, "update");
  981. undo_redo->commit_action();
  982. } else {
  983. vguides.push_back(edited.x);
  984. undo_redo->create_action(TTR("Create Vertical Guide"));
  985. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
  986. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
  987. undo_redo->add_undo_method(viewport, "update");
  988. undo_redo->commit_action();
  989. }
  990. } else {
  991. if (dragged_guide_index >= 0) {
  992. vguides.remove_at(dragged_guide_index);
  993. undo_redo->create_action(TTR("Remove Vertical Guide"));
  994. if (vguides.is_empty()) {
  995. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_vertical_guides_");
  996. } else {
  997. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
  998. }
  999. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
  1000. undo_redo->add_undo_method(viewport, "update");
  1001. undo_redo->commit_action();
  1002. }
  1003. }
  1004. } else if (drag_type == DRAG_H_GUIDE) {
  1005. Array prev_hguides = hguides.duplicate();
  1006. if (b->get_position().y > RULER_WIDTH) {
  1007. // Adds a new horizontal guide
  1008. if (dragged_guide_index >= 0) {
  1009. hguides[dragged_guide_index] = edited.y;
  1010. undo_redo->create_action(TTR("Move Horizontal Guide"));
  1011. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
  1012. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
  1013. undo_redo->add_undo_method(viewport, "update");
  1014. undo_redo->commit_action();
  1015. } else {
  1016. hguides.push_back(edited.y);
  1017. undo_redo->create_action(TTR("Create Horizontal Guide"));
  1018. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
  1019. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
  1020. undo_redo->add_undo_method(viewport, "update");
  1021. undo_redo->commit_action();
  1022. }
  1023. } else {
  1024. if (dragged_guide_index >= 0) {
  1025. hguides.remove_at(dragged_guide_index);
  1026. undo_redo->create_action(TTR("Remove Horizontal Guide"));
  1027. if (hguides.is_empty()) {
  1028. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "remove_meta", "_edit_horizontal_guides_");
  1029. } else {
  1030. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
  1031. }
  1032. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
  1033. undo_redo->add_undo_method(viewport, "update");
  1034. undo_redo->commit_action();
  1035. }
  1036. }
  1037. } else if (drag_type == DRAG_DOUBLE_GUIDE) {
  1038. Array prev_hguides = hguides.duplicate();
  1039. Array prev_vguides = vguides.duplicate();
  1040. if (b->get_position().x > RULER_WIDTH && b->get_position().y > RULER_WIDTH) {
  1041. // Adds a new horizontal guide a new vertical guide
  1042. vguides.push_back(edited.x);
  1043. hguides.push_back(edited.y);
  1044. undo_redo->create_action(TTR("Create Horizontal and Vertical Guides"));
  1045. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", vguides);
  1046. undo_redo->add_do_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", hguides);
  1047. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_vertical_guides_", prev_vguides);
  1048. undo_redo->add_undo_method(EditorNode::get_singleton()->get_edited_scene(), "set_meta", "_edit_horizontal_guides_", prev_hguides);
  1049. undo_redo->add_undo_method(viewport, "update");
  1050. undo_redo->commit_action();
  1051. }
  1052. }
  1053. }
  1054. snap_target[0] = SNAP_TARGET_NONE;
  1055. snap_target[1] = SNAP_TARGET_NONE;
  1056. drag_type = DRAG_NONE;
  1057. viewport->update();
  1058. return true;
  1059. }
  1060. }
  1061. return false;
  1062. }
  1063. bool CanvasItemEditor::_gui_input_zoom_or_pan(const Ref<InputEvent> &p_event, bool p_already_accepted) {
  1064. panner->set_force_drag(tool == TOOL_PAN);
  1065. bool panner_active = panner->gui_input(p_event, warped_panning ? viewport->get_global_rect() : Rect2());
  1066. if (panner->is_panning() != pan_pressed) {
  1067. pan_pressed = panner->is_panning();
  1068. _update_cursor();
  1069. }
  1070. if (panner_active) {
  1071. return true;
  1072. }
  1073. Ref<InputEventKey> k = p_event;
  1074. if (k.is_valid()) {
  1075. if (k->is_pressed()) {
  1076. if (ED_GET_SHORTCUT("canvas_item_editor/zoom_3.125_percent")->matches_event(p_event)) {
  1077. _shortcut_zoom_set(1.0 / 32.0);
  1078. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_6.25_percent")->matches_event(p_event)) {
  1079. _shortcut_zoom_set(1.0 / 16.0);
  1080. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_12.5_percent")->matches_event(p_event)) {
  1081. _shortcut_zoom_set(1.0 / 8.0);
  1082. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_25_percent")->matches_event(p_event)) {
  1083. _shortcut_zoom_set(1.0 / 4.0);
  1084. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_50_percent")->matches_event(p_event)) {
  1085. _shortcut_zoom_set(1.0 / 2.0);
  1086. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_100_percent")->matches_event(p_event)) {
  1087. _shortcut_zoom_set(1.0);
  1088. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_200_percent")->matches_event(p_event)) {
  1089. _shortcut_zoom_set(2.0);
  1090. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_400_percent")->matches_event(p_event)) {
  1091. _shortcut_zoom_set(4.0);
  1092. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_800_percent")->matches_event(p_event)) {
  1093. _shortcut_zoom_set(8.0);
  1094. } else if (ED_GET_SHORTCUT("canvas_item_editor/zoom_1600_percent")->matches_event(p_event)) {
  1095. _shortcut_zoom_set(16.0);
  1096. }
  1097. }
  1098. }
  1099. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1100. if (magnify_gesture.is_valid() && !p_already_accepted) {
  1101. // Zoom gesture
  1102. _zoom_on_position(zoom * magnify_gesture->get_factor(), magnify_gesture->get_position());
  1103. return true;
  1104. }
  1105. Ref<InputEventPanGesture> pan_gesture = p_event;
  1106. if (pan_gesture.is_valid() && !p_already_accepted) {
  1107. // If ctrl key pressed, then zoom instead of pan.
  1108. if (pan_gesture->is_ctrl_pressed()) {
  1109. const real_t factor = pan_gesture->get_delta().y;
  1110. zoom_widget->set_zoom_by_increments(1);
  1111. if (factor != 1.f) {
  1112. zoom_widget->set_zoom(zoom * ((zoom_widget->get_zoom() / zoom - 1.f) * factor + 1.f));
  1113. }
  1114. _zoom_on_position(zoom_widget->get_zoom(), pan_gesture->get_position());
  1115. return true;
  1116. }
  1117. // Pan gesture
  1118. const Vector2 delta = (pan_speed / zoom) * pan_gesture->get_delta();
  1119. view_offset.x += delta.x;
  1120. view_offset.y += delta.y;
  1121. update_viewport();
  1122. return true;
  1123. }
  1124. return false;
  1125. }
  1126. void CanvasItemEditor::_scroll_callback(Vector2 p_scroll_vec, bool p_alt) {
  1127. _pan_callback(-p_scroll_vec * pan_speed);
  1128. }
  1129. void CanvasItemEditor::_pan_callback(Vector2 p_scroll_vec) {
  1130. view_offset.x -= p_scroll_vec.x / zoom;
  1131. view_offset.y -= p_scroll_vec.y / zoom;
  1132. update_viewport();
  1133. }
  1134. void CanvasItemEditor::_zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt) {
  1135. zoom_widget->set_zoom_by_increments(-1, p_alt);
  1136. if (!Math::is_equal_approx(p_scroll_vec.y, (real_t)1.0)) {
  1137. // Handle high-precision (analog) scrolling.
  1138. zoom_widget->set_zoom(zoom * ((zoom_widget->get_zoom() / zoom - 1.f) * p_scroll_vec.y + 1.f));
  1139. }
  1140. _zoom_on_position(zoom_widget->get_zoom(), p_origin);
  1141. }
  1142. bool CanvasItemEditor::_gui_input_pivot(const Ref<InputEvent> &p_event) {
  1143. Ref<InputEventMouseMotion> m = p_event;
  1144. Ref<InputEventMouseButton> b = p_event;
  1145. Ref<InputEventKey> k = p_event;
  1146. // Drag the pivot (in pivot mode / with V key)
  1147. if (drag_type == DRAG_NONE) {
  1148. if ((b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
  1149. (k.is_valid() && k->is_pressed() && !k->is_echo() && k->get_keycode() == Key::V && tool == TOOL_SELECT && k->get_modifiers_mask() == Key::NONE)) {
  1150. List<CanvasItem *> selection = _get_edited_canvas_items();
  1151. // Filters the selection with nodes that allow setting the pivot
  1152. drag_selection = List<CanvasItem *>();
  1153. for (CanvasItem *canvas_item : selection) {
  1154. if (canvas_item->_edit_use_pivot()) {
  1155. drag_selection.push_back(canvas_item);
  1156. }
  1157. }
  1158. // Start dragging if we still have nodes
  1159. if (drag_selection.size() > 0) {
  1160. _save_canvas_item_state(drag_selection);
  1161. drag_from = transform.affine_inverse().xform((b.is_valid()) ? b->get_position() : viewport->get_local_mouse_position());
  1162. Vector2 new_pos;
  1163. if (drag_selection.size() == 1) {
  1164. new_pos = snap_point(drag_from, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, drag_selection[0]);
  1165. } else {
  1166. new_pos = snap_point(drag_from, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, nullptr, drag_selection);
  1167. }
  1168. for (CanvasItem *canvas_item : drag_selection) {
  1169. canvas_item->_edit_set_pivot(canvas_item->get_global_transform_with_canvas().affine_inverse().xform(new_pos));
  1170. }
  1171. drag_type = DRAG_PIVOT;
  1172. }
  1173. return true;
  1174. }
  1175. }
  1176. if (drag_type == DRAG_PIVOT) {
  1177. // Move the pivot
  1178. if (m.is_valid()) {
  1179. drag_to = transform.affine_inverse().xform(m->get_position());
  1180. _restore_canvas_item_state(drag_selection);
  1181. Vector2 new_pos;
  1182. if (drag_selection.size() == 1) {
  1183. new_pos = snap_point(drag_to, SNAP_NODE_SIDES | SNAP_NODE_CENTER | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, drag_selection[0]);
  1184. } else {
  1185. new_pos = snap_point(drag_to, SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL);
  1186. }
  1187. for (CanvasItem *canvas_item : drag_selection) {
  1188. canvas_item->_edit_set_pivot(canvas_item->get_global_transform_with_canvas().affine_inverse().xform(new_pos));
  1189. }
  1190. return true;
  1191. }
  1192. // Confirm the pivot move
  1193. if (drag_selection.size() >= 1 &&
  1194. ((b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT && tool == TOOL_EDIT_PIVOT) ||
  1195. (k.is_valid() && !k->is_pressed() && k->get_keycode() == Key::V))) {
  1196. _commit_canvas_item_state(
  1197. drag_selection,
  1198. vformat(
  1199. TTR("Set CanvasItem \"%s\" Pivot Offset to (%d, %d)"),
  1200. drag_selection[0]->get_name(),
  1201. drag_selection[0]->_edit_get_pivot().x,
  1202. drag_selection[0]->_edit_get_pivot().y));
  1203. drag_type = DRAG_NONE;
  1204. return true;
  1205. }
  1206. // Cancel a drag
  1207. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1208. _restore_canvas_item_state(drag_selection);
  1209. drag_type = DRAG_NONE;
  1210. viewport->update();
  1211. return true;
  1212. }
  1213. }
  1214. return false;
  1215. }
  1216. bool CanvasItemEditor::_gui_input_rotate(const Ref<InputEvent> &p_event) {
  1217. Ref<InputEventMouseButton> b = p_event;
  1218. Ref<InputEventMouseMotion> m = p_event;
  1219. // Start rotation
  1220. if (drag_type == DRAG_NONE) {
  1221. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
  1222. if ((b->is_command_pressed() && !b->is_alt_pressed() && tool == TOOL_SELECT) || tool == TOOL_ROTATE) {
  1223. List<CanvasItem *> selection = _get_edited_canvas_items();
  1224. // Remove not movable nodes
  1225. for (CanvasItem *E : selection) {
  1226. if (!_is_node_movable(E, true)) {
  1227. selection.erase(E);
  1228. }
  1229. }
  1230. drag_selection = selection;
  1231. if (drag_selection.size() > 0) {
  1232. drag_type = DRAG_ROTATE;
  1233. drag_from = transform.affine_inverse().xform(b->get_position());
  1234. CanvasItem *canvas_item = drag_selection[0];
  1235. if (canvas_item->_edit_use_pivot()) {
  1236. drag_rotation_center = canvas_item->get_global_transform_with_canvas().xform(canvas_item->_edit_get_pivot());
  1237. } else {
  1238. drag_rotation_center = canvas_item->get_global_transform_with_canvas().get_origin();
  1239. }
  1240. _save_canvas_item_state(drag_selection);
  1241. return true;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. if (drag_type == DRAG_ROTATE) {
  1247. // Rotate the node
  1248. if (m.is_valid()) {
  1249. _restore_canvas_item_state(drag_selection);
  1250. for (CanvasItem *canvas_item : drag_selection) {
  1251. drag_to = transform.affine_inverse().xform(m->get_position());
  1252. //Rotate the opposite way if the canvas item's compounded scale has an uneven number of negative elements
  1253. bool opposite = (canvas_item->get_global_transform().get_scale().sign().dot(canvas_item->get_transform().get_scale().sign()) == 0);
  1254. canvas_item->_edit_set_rotation(snap_angle(canvas_item->_edit_get_rotation() + (opposite ? -1 : 1) * (drag_from - drag_rotation_center).angle_to(drag_to - drag_rotation_center), canvas_item->_edit_get_rotation()));
  1255. viewport->update();
  1256. }
  1257. return true;
  1258. }
  1259. // Confirms the node rotation
  1260. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
  1261. if (drag_selection.size() != 1) {
  1262. _commit_canvas_item_state(
  1263. drag_selection,
  1264. vformat(TTR("Rotate %d CanvasItems"), drag_selection.size()),
  1265. true);
  1266. } else {
  1267. _commit_canvas_item_state(
  1268. drag_selection,
  1269. vformat(TTR("Rotate CanvasItem \"%s\" to %d degrees"),
  1270. drag_selection[0]->get_name(),
  1271. Math::rad2deg(drag_selection[0]->_edit_get_rotation())),
  1272. true);
  1273. }
  1274. if (key_auto_insert_button->is_pressed()) {
  1275. _insert_animation_keys(false, true, false, true);
  1276. }
  1277. drag_type = DRAG_NONE;
  1278. return true;
  1279. }
  1280. // Cancel a drag
  1281. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1282. _restore_canvas_item_state(drag_selection);
  1283. drag_type = DRAG_NONE;
  1284. viewport->update();
  1285. return true;
  1286. }
  1287. }
  1288. return false;
  1289. }
  1290. bool CanvasItemEditor::_gui_input_open_scene_on_double_click(const Ref<InputEvent> &p_event) {
  1291. Ref<InputEventMouseButton> b = p_event;
  1292. // Open a sub-scene on double-click
  1293. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && b->is_double_click() && tool == TOOL_SELECT) {
  1294. List<CanvasItem *> selection = _get_edited_canvas_items();
  1295. if (selection.size() == 1) {
  1296. CanvasItem *canvas_item = selection[0];
  1297. if (!canvas_item->get_scene_file_path().is_empty() && canvas_item != EditorNode::get_singleton()->get_edited_scene()) {
  1298. EditorNode::get_singleton()->open_request(canvas_item->get_scene_file_path());
  1299. return true;
  1300. }
  1301. }
  1302. }
  1303. return false;
  1304. }
  1305. bool CanvasItemEditor::_gui_input_anchors(const Ref<InputEvent> &p_event) {
  1306. Ref<InputEventMouseButton> b = p_event;
  1307. Ref<InputEventMouseMotion> m = p_event;
  1308. // Starts anchor dragging if needed
  1309. if (drag_type == DRAG_NONE) {
  1310. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
  1311. List<CanvasItem *> selection = _get_edited_canvas_items();
  1312. if (selection.size() == 1) {
  1313. Control *control = Object::cast_to<Control>(selection[0]);
  1314. if (control && _is_node_movable(control)) {
  1315. Vector2 anchor_pos[4];
  1316. anchor_pos[0] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_TOP));
  1317. anchor_pos[1] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_TOP));
  1318. anchor_pos[2] = Vector2(control->get_anchor(SIDE_RIGHT), control->get_anchor(SIDE_BOTTOM));
  1319. anchor_pos[3] = Vector2(control->get_anchor(SIDE_LEFT), control->get_anchor(SIDE_BOTTOM));
  1320. Rect2 anchor_rects[4];
  1321. for (int i = 0; i < 4; i++) {
  1322. anchor_pos[i] = (transform * control->get_global_transform_with_canvas()).xform(_anchor_to_position(control, anchor_pos[i]));
  1323. anchor_rects[i] = Rect2(anchor_pos[i], anchor_handle->get_size());
  1324. if (control->is_layout_rtl()) {
  1325. anchor_rects[i].position -= anchor_handle->get_size() * Vector2(real_t(i == 1 || i == 2), real_t(i <= 1));
  1326. } else {
  1327. anchor_rects[i].position -= anchor_handle->get_size() * Vector2(real_t(i == 0 || i == 3), real_t(i <= 1));
  1328. }
  1329. }
  1330. const DragType dragger[] = {
  1331. DRAG_ANCHOR_TOP_LEFT,
  1332. DRAG_ANCHOR_TOP_RIGHT,
  1333. DRAG_ANCHOR_BOTTOM_RIGHT,
  1334. DRAG_ANCHOR_BOTTOM_LEFT,
  1335. };
  1336. for (int i = 0; i < 4; i++) {
  1337. if (anchor_rects[i].has_point(b->get_position())) {
  1338. if ((anchor_pos[0] == anchor_pos[2]) && (anchor_pos[0].distance_to(b->get_position()) < anchor_handle->get_size().length() / 3.0)) {
  1339. drag_type = DRAG_ANCHOR_ALL;
  1340. } else {
  1341. drag_type = dragger[i];
  1342. }
  1343. drag_from = transform.affine_inverse().xform(b->get_position());
  1344. drag_selection = List<CanvasItem *>();
  1345. drag_selection.push_back(control);
  1346. _save_canvas_item_state(drag_selection);
  1347. return true;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. }
  1354. if (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_RIGHT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT || drag_type == DRAG_ANCHOR_ALL) {
  1355. // Drag the anchor
  1356. if (m.is_valid()) {
  1357. _restore_canvas_item_state(drag_selection);
  1358. Control *control = Object::cast_to<Control>(drag_selection[0]);
  1359. drag_to = transform.affine_inverse().xform(m->get_position());
  1360. Transform2D xform = control->get_global_transform_with_canvas().affine_inverse();
  1361. Point2 previous_anchor;
  1362. previous_anchor.x = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_BOTTOM_LEFT) ? control->get_anchor(SIDE_LEFT) : control->get_anchor(SIDE_RIGHT);
  1363. previous_anchor.y = (drag_type == DRAG_ANCHOR_TOP_LEFT || drag_type == DRAG_ANCHOR_TOP_RIGHT) ? control->get_anchor(SIDE_TOP) : control->get_anchor(SIDE_BOTTOM);
  1364. previous_anchor = xform.affine_inverse().xform(_anchor_to_position(control, previous_anchor));
  1365. Vector2 new_anchor = xform.xform(snap_point(previous_anchor + (drag_to - drag_from), SNAP_GRID | SNAP_OTHER_NODES, SNAP_NODE_PARENT | SNAP_NODE_SIDES | SNAP_NODE_CENTER, control));
  1366. new_anchor = _position_to_anchor(control, new_anchor).snapped(Vector2(0.001, 0.001));
  1367. bool use_single_axis = m->is_shift_pressed();
  1368. Vector2 drag_vector = xform.xform(drag_to) - xform.xform(drag_from);
  1369. bool use_y = Math::abs(drag_vector.y) > Math::abs(drag_vector.x);
  1370. switch (drag_type) {
  1371. case DRAG_ANCHOR_TOP_LEFT:
  1372. if (!use_single_axis || !use_y) {
  1373. control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
  1374. }
  1375. if (!use_single_axis || use_y) {
  1376. control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
  1377. }
  1378. break;
  1379. case DRAG_ANCHOR_TOP_RIGHT:
  1380. if (!use_single_axis || !use_y) {
  1381. control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
  1382. }
  1383. if (!use_single_axis || use_y) {
  1384. control->set_anchor(SIDE_TOP, new_anchor.y, false, false);
  1385. }
  1386. break;
  1387. case DRAG_ANCHOR_BOTTOM_RIGHT:
  1388. if (!use_single_axis || !use_y) {
  1389. control->set_anchor(SIDE_RIGHT, new_anchor.x, false, false);
  1390. }
  1391. if (!use_single_axis || use_y) {
  1392. control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
  1393. }
  1394. break;
  1395. case DRAG_ANCHOR_BOTTOM_LEFT:
  1396. if (!use_single_axis || !use_y) {
  1397. control->set_anchor(SIDE_LEFT, new_anchor.x, false, false);
  1398. }
  1399. if (!use_single_axis || use_y) {
  1400. control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, false);
  1401. }
  1402. break;
  1403. case DRAG_ANCHOR_ALL:
  1404. if (!use_single_axis || !use_y) {
  1405. control->set_anchor(SIDE_LEFT, new_anchor.x, false, true);
  1406. control->set_anchor(SIDE_RIGHT, new_anchor.x, false, true);
  1407. }
  1408. if (!use_single_axis || use_y) {
  1409. control->set_anchor(SIDE_TOP, new_anchor.y, false, true);
  1410. control->set_anchor(SIDE_BOTTOM, new_anchor.y, false, true);
  1411. }
  1412. break;
  1413. default:
  1414. break;
  1415. }
  1416. return true;
  1417. }
  1418. // Confirms new anchor position
  1419. if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
  1420. _commit_canvas_item_state(
  1421. drag_selection,
  1422. vformat(TTR("Move CanvasItem \"%s\" Anchor"), drag_selection[0]->get_name()));
  1423. drag_type = DRAG_NONE;
  1424. return true;
  1425. }
  1426. // Cancel a drag
  1427. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1428. _restore_canvas_item_state(drag_selection);
  1429. drag_type = DRAG_NONE;
  1430. viewport->update();
  1431. return true;
  1432. }
  1433. }
  1434. return false;
  1435. }
  1436. bool CanvasItemEditor::_gui_input_resize(const Ref<InputEvent> &p_event) {
  1437. Ref<InputEventMouseButton> b = p_event;
  1438. Ref<InputEventMouseMotion> m = p_event;
  1439. // Drag resize handles
  1440. if (drag_type == DRAG_NONE) {
  1441. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT) {
  1442. List<CanvasItem *> selection = _get_edited_canvas_items();
  1443. if (selection.size() == 1) {
  1444. CanvasItem *canvas_item = selection[0];
  1445. if (canvas_item->_edit_use_rect() && _is_node_movable(canvas_item)) {
  1446. Rect2 rect = canvas_item->_edit_get_rect();
  1447. Transform2D xform = transform * canvas_item->get_global_transform_with_canvas();
  1448. const Vector2 endpoints[4] = {
  1449. xform.xform(rect.position),
  1450. xform.xform(rect.position + Vector2(rect.size.x, 0)),
  1451. xform.xform(rect.position + rect.size),
  1452. xform.xform(rect.position + Vector2(0, rect.size.y))
  1453. };
  1454. const DragType dragger[] = {
  1455. DRAG_TOP_LEFT,
  1456. DRAG_TOP,
  1457. DRAG_TOP_RIGHT,
  1458. DRAG_RIGHT,
  1459. DRAG_BOTTOM_RIGHT,
  1460. DRAG_BOTTOM,
  1461. DRAG_BOTTOM_LEFT,
  1462. DRAG_LEFT
  1463. };
  1464. DragType resize_drag = DRAG_NONE;
  1465. real_t radius = (select_handle->get_size().width / 2) * 1.5;
  1466. for (int i = 0; i < 4; i++) {
  1467. int prev = (i + 3) % 4;
  1468. int next = (i + 1) % 4;
  1469. Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
  1470. ofs *= (select_handle->get_size().width / 2);
  1471. ofs += endpoints[i];
  1472. if (ofs.distance_to(b->get_position()) < radius) {
  1473. resize_drag = dragger[i * 2];
  1474. }
  1475. ofs = (endpoints[i] + endpoints[next]) / 2;
  1476. ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (select_handle->get_size().width / 2);
  1477. if (ofs.distance_to(b->get_position()) < radius) {
  1478. resize_drag = dragger[i * 2 + 1];
  1479. }
  1480. }
  1481. if (resize_drag != DRAG_NONE) {
  1482. drag_type = resize_drag;
  1483. drag_from = transform.affine_inverse().xform(b->get_position());
  1484. drag_selection = List<CanvasItem *>();
  1485. drag_selection.push_back(canvas_item);
  1486. _save_canvas_item_state(drag_selection);
  1487. return true;
  1488. }
  1489. }
  1490. }
  1491. }
  1492. }
  1493. if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT || drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM ||
  1494. drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
  1495. // Resize the node
  1496. if (m.is_valid()) {
  1497. CanvasItem *canvas_item = drag_selection[0];
  1498. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  1499. //Reset state
  1500. canvas_item->_edit_set_state(se->undo_state);
  1501. bool uniform = m->is_shift_pressed();
  1502. bool symmetric = m->is_alt_pressed();
  1503. Rect2 local_rect = canvas_item->_edit_get_rect();
  1504. real_t aspect = local_rect.get_size().y / local_rect.get_size().x;
  1505. Point2 current_begin = local_rect.get_position();
  1506. Point2 current_end = local_rect.get_position() + local_rect.get_size();
  1507. Point2 max_begin = (symmetric) ? (current_begin + current_end - canvas_item->_edit_get_minimum_size()) / 2.0 : current_end - canvas_item->_edit_get_minimum_size();
  1508. Point2 min_end = (symmetric) ? (current_begin + current_end + canvas_item->_edit_get_minimum_size()) / 2.0 : current_begin + canvas_item->_edit_get_minimum_size();
  1509. Point2 center = (current_begin + current_end) / 2.0;
  1510. drag_to = transform.affine_inverse().xform(m->get_position());
  1511. Transform2D xform = canvas_item->get_global_transform_with_canvas().affine_inverse();
  1512. Point2 drag_to_snapped_begin;
  1513. Point2 drag_to_snapped_end;
  1514. // last call decides which snapping lines are drawn
  1515. if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT) {
  1516. drag_to_snapped_end = snap_point(xform.affine_inverse().xform(current_end) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, canvas_item);
  1517. drag_to_snapped_begin = snap_point(xform.affine_inverse().xform(current_begin) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, canvas_item);
  1518. } else {
  1519. drag_to_snapped_begin = snap_point(xform.affine_inverse().xform(current_begin) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, canvas_item);
  1520. drag_to_snapped_end = snap_point(xform.affine_inverse().xform(current_end) + (drag_to - drag_from), SNAP_NODE_ANCHORS | SNAP_NODE_PARENT | SNAP_OTHER_NODES | SNAP_GRID | SNAP_PIXEL, 0, canvas_item);
  1521. }
  1522. Point2 drag_begin = xform.xform(drag_to_snapped_begin);
  1523. Point2 drag_end = xform.xform(drag_to_snapped_end);
  1524. // Horizontal resize
  1525. if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
  1526. current_begin.x = MIN(drag_begin.x, max_begin.x);
  1527. } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
  1528. current_end.x = MAX(drag_end.x, min_end.x);
  1529. }
  1530. // Vertical resize
  1531. if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
  1532. current_begin.y = MIN(drag_begin.y, max_begin.y);
  1533. } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
  1534. current_end.y = MAX(drag_end.y, min_end.y);
  1535. }
  1536. // Uniform resize
  1537. if (uniform) {
  1538. if (drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT) {
  1539. current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
  1540. } else if (drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM) {
  1541. current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
  1542. } else {
  1543. if (aspect >= 1.0) {
  1544. if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
  1545. current_begin.y = current_end.y - aspect * (current_end.x - current_begin.x);
  1546. } else {
  1547. current_end.y = current_begin.y + aspect * (current_end.x - current_begin.x);
  1548. }
  1549. } else {
  1550. if (drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
  1551. current_begin.x = current_end.x - (current_end.y - current_begin.y) / aspect;
  1552. } else {
  1553. current_end.x = current_begin.x + (current_end.y - current_begin.y) / aspect;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. // Symmetric resize
  1559. if (symmetric) {
  1560. if (drag_type == DRAG_LEFT || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_BOTTOM_LEFT) {
  1561. current_end.x = 2.0 * center.x - current_begin.x;
  1562. } else if (drag_type == DRAG_RIGHT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_RIGHT) {
  1563. current_begin.x = 2.0 * center.x - current_end.x;
  1564. }
  1565. if (drag_type == DRAG_TOP || drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT) {
  1566. current_end.y = 2.0 * center.y - current_begin.y;
  1567. } else if (drag_type == DRAG_BOTTOM || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT) {
  1568. current_begin.y = 2.0 * center.y - current_end.y;
  1569. }
  1570. }
  1571. canvas_item->_edit_set_rect(Rect2(current_begin, current_end - current_begin));
  1572. return true;
  1573. }
  1574. // Confirm resize
  1575. if (drag_selection.size() >= 1 && b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
  1576. const Node2D *node2d = Object::cast_to<Node2D>(drag_selection[0]);
  1577. if (node2d) {
  1578. // Extends from Node2D.
  1579. // Node2D doesn't have an actual stored rect size, unlike Controls.
  1580. _commit_canvas_item_state(
  1581. drag_selection,
  1582. vformat(
  1583. TTR("Scale Node2D \"%s\" to (%s, %s)"),
  1584. drag_selection[0]->get_name(),
  1585. Math::snapped(drag_selection[0]->_edit_get_scale().x, 0.01),
  1586. Math::snapped(drag_selection[0]->_edit_get_scale().y, 0.01)),
  1587. true);
  1588. } else {
  1589. // Extends from Control.
  1590. _commit_canvas_item_state(
  1591. drag_selection,
  1592. vformat(
  1593. TTR("Resize Control \"%s\" to (%d, %d)"),
  1594. drag_selection[0]->get_name(),
  1595. drag_selection[0]->_edit_get_rect().size.x,
  1596. drag_selection[0]->_edit_get_rect().size.y),
  1597. true);
  1598. }
  1599. if (key_auto_insert_button->is_pressed()) {
  1600. _insert_animation_keys(false, false, true, true);
  1601. }
  1602. snap_target[0] = SNAP_TARGET_NONE;
  1603. snap_target[1] = SNAP_TARGET_NONE;
  1604. drag_type = DRAG_NONE;
  1605. viewport->update();
  1606. return true;
  1607. }
  1608. // Cancel a drag
  1609. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1610. _restore_canvas_item_state(drag_selection);
  1611. snap_target[0] = SNAP_TARGET_NONE;
  1612. snap_target[1] = SNAP_TARGET_NONE;
  1613. drag_type = DRAG_NONE;
  1614. viewport->update();
  1615. return true;
  1616. }
  1617. }
  1618. return false;
  1619. }
  1620. bool CanvasItemEditor::_gui_input_scale(const Ref<InputEvent> &p_event) {
  1621. Ref<InputEventMouseButton> b = p_event;
  1622. Ref<InputEventMouseMotion> m = p_event;
  1623. // Drag resize handles
  1624. if (drag_type == DRAG_NONE) {
  1625. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && ((b->is_alt_pressed() && b->is_ctrl_pressed()) || tool == TOOL_SCALE)) {
  1626. List<CanvasItem *> selection = _get_edited_canvas_items();
  1627. if (selection.size() == 1) {
  1628. CanvasItem *canvas_item = selection[0];
  1629. if (_is_node_movable(canvas_item)) {
  1630. Transform2D xform = transform * canvas_item->get_global_transform_with_canvas();
  1631. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  1632. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  1633. drag_type = DRAG_SCALE_BOTH;
  1634. if (show_transformation_gizmos) {
  1635. Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE);
  1636. Rect2 x_handle_rect = Rect2(scale_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  1637. if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
  1638. drag_type = DRAG_SCALE_X;
  1639. }
  1640. Rect2 y_handle_rect = Rect2(-5 * EDSCALE, scale_factor.y * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  1641. if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
  1642. drag_type = DRAG_SCALE_Y;
  1643. }
  1644. }
  1645. drag_from = transform.affine_inverse().xform(b->get_position());
  1646. drag_selection = List<CanvasItem *>();
  1647. drag_selection.push_back(canvas_item);
  1648. _save_canvas_item_state(drag_selection);
  1649. return true;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. if (drag_type == DRAG_SCALE_BOTH || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y) {
  1655. // Resize the node
  1656. if (m.is_valid()) {
  1657. _restore_canvas_item_state(drag_selection);
  1658. CanvasItem *canvas_item = drag_selection[0];
  1659. drag_to = transform.affine_inverse().xform(m->get_position());
  1660. Transform2D parent_xform = canvas_item->get_global_transform_with_canvas() * canvas_item->get_transform().affine_inverse();
  1661. Transform2D unscaled_transform = (transform * parent_xform * canvas_item->_edit_get_transform()).orthonormalized();
  1662. Transform2D simple_xform = (viewport->get_transform() * unscaled_transform).affine_inverse() * transform;
  1663. bool uniform = m->is_shift_pressed();
  1664. bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  1665. Point2 drag_from_local = simple_xform.xform(drag_from);
  1666. Point2 drag_to_local = simple_xform.xform(drag_to);
  1667. Point2 offset = drag_to_local - drag_from_local;
  1668. Size2 scale = canvas_item->_edit_get_scale();
  1669. Size2 original_scale = scale;
  1670. real_t ratio = scale.y / scale.x;
  1671. if (drag_type == DRAG_SCALE_BOTH) {
  1672. Size2 scale_factor = drag_to_local / drag_from_local;
  1673. if (uniform) {
  1674. scale *= (scale_factor.x + scale_factor.y) / 2.0;
  1675. } else {
  1676. scale *= scale_factor;
  1677. }
  1678. } else {
  1679. Size2 scale_factor = Vector2(offset.x, -offset.y) / SCALE_HANDLE_DISTANCE;
  1680. Size2 parent_scale = parent_xform.get_scale();
  1681. scale_factor *= Vector2(1.0 / parent_scale.x, 1.0 / parent_scale.y);
  1682. if (drag_type == DRAG_SCALE_X) {
  1683. scale.x += scale_factor.x;
  1684. if (uniform) {
  1685. scale.y = scale.x * ratio;
  1686. }
  1687. } else if (drag_type == DRAG_SCALE_Y) {
  1688. scale.y -= scale_factor.y;
  1689. if (uniform) {
  1690. scale.x = scale.y / ratio;
  1691. }
  1692. }
  1693. }
  1694. if (snap_scale && !is_ctrl) {
  1695. if (snap_relative) {
  1696. scale.x = original_scale.x * (roundf((scale.x / original_scale.x) / snap_scale_step) * snap_scale_step);
  1697. scale.y = original_scale.y * (roundf((scale.y / original_scale.y) / snap_scale_step) * snap_scale_step);
  1698. } else {
  1699. scale.x = roundf(scale.x / snap_scale_step) * snap_scale_step;
  1700. scale.y = roundf(scale.y / snap_scale_step) * snap_scale_step;
  1701. }
  1702. }
  1703. canvas_item->_edit_set_scale(scale);
  1704. return true;
  1705. }
  1706. // Confirm resize
  1707. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && !b->is_pressed()) {
  1708. if (drag_selection.size() != 1) {
  1709. _commit_canvas_item_state(
  1710. drag_selection,
  1711. vformat(TTR("Scale %d CanvasItems"), drag_selection.size()),
  1712. true);
  1713. } else {
  1714. _commit_canvas_item_state(
  1715. drag_selection,
  1716. vformat(TTR("Scale CanvasItem \"%s\" to (%s, %s)"),
  1717. drag_selection[0]->get_name(),
  1718. Math::snapped(drag_selection[0]->_edit_get_scale().x, 0.01),
  1719. Math::snapped(drag_selection[0]->_edit_get_scale().y, 0.01)),
  1720. true);
  1721. }
  1722. if (key_auto_insert_button->is_pressed()) {
  1723. _insert_animation_keys(false, false, true, true);
  1724. }
  1725. drag_type = DRAG_NONE;
  1726. viewport->update();
  1727. return true;
  1728. }
  1729. // Cancel a drag
  1730. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1731. _restore_canvas_item_state(drag_selection);
  1732. drag_type = DRAG_NONE;
  1733. viewport->update();
  1734. return true;
  1735. }
  1736. }
  1737. return false;
  1738. }
  1739. bool CanvasItemEditor::_gui_input_move(const Ref<InputEvent> &p_event) {
  1740. Ref<InputEventMouseButton> b = p_event;
  1741. Ref<InputEventMouseMotion> m = p_event;
  1742. Ref<InputEventKey> k = p_event;
  1743. if (drag_type == DRAG_NONE) {
  1744. //Start moving the nodes
  1745. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed()) {
  1746. if ((b->is_alt_pressed() && !b->is_ctrl_pressed()) || tool == TOOL_MOVE) {
  1747. List<CanvasItem *> selection = _get_edited_canvas_items();
  1748. drag_selection.clear();
  1749. for (int i = 0; i < selection.size(); i++) {
  1750. if (_is_node_movable(selection[i], true)) {
  1751. drag_selection.push_back(selection[i]);
  1752. }
  1753. }
  1754. if (selection.size() > 0) {
  1755. drag_type = DRAG_MOVE;
  1756. CanvasItem *canvas_item = selection[0];
  1757. Transform2D parent_xform = canvas_item->get_global_transform_with_canvas() * canvas_item->get_transform().affine_inverse();
  1758. Transform2D unscaled_transform = (transform * parent_xform * canvas_item->_edit_get_transform()).orthonormalized();
  1759. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  1760. if (show_transformation_gizmos) {
  1761. Size2 move_factor = Size2(MOVE_HANDLE_DISTANCE, MOVE_HANDLE_DISTANCE);
  1762. Rect2 x_handle_rect = Rect2(move_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  1763. if (x_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
  1764. drag_type = DRAG_MOVE_X;
  1765. }
  1766. Rect2 y_handle_rect = Rect2(-5 * EDSCALE, move_factor.y * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  1767. if (y_handle_rect.has_point(simple_xform.affine_inverse().xform(b->get_position()))) {
  1768. drag_type = DRAG_MOVE_Y;
  1769. }
  1770. }
  1771. drag_from = transform.affine_inverse().xform(b->get_position());
  1772. _save_canvas_item_state(drag_selection);
  1773. }
  1774. return true;
  1775. }
  1776. }
  1777. }
  1778. if (drag_type == DRAG_MOVE || drag_type == DRAG_MOVE_X || drag_type == DRAG_MOVE_Y) {
  1779. // Move the nodes
  1780. if (m.is_valid()) {
  1781. _restore_canvas_item_state(drag_selection, true);
  1782. drag_to = transform.affine_inverse().xform(m->get_position());
  1783. Point2 previous_pos;
  1784. if (!drag_selection.is_empty()) {
  1785. if (drag_selection.size() == 1) {
  1786. Transform2D xform = drag_selection[0]->get_global_transform_with_canvas() * drag_selection[0]->get_transform().affine_inverse();
  1787. previous_pos = xform.xform(drag_selection[0]->_edit_get_position());
  1788. } else {
  1789. previous_pos = _get_encompassing_rect_from_list(drag_selection).position;
  1790. }
  1791. }
  1792. Point2 new_pos = snap_point(previous_pos + (drag_to - drag_from), SNAP_GRID | SNAP_GUIDES | SNAP_PIXEL | SNAP_NODE_PARENT | SNAP_NODE_ANCHORS | SNAP_OTHER_NODES, 0, nullptr, drag_selection);
  1793. if (drag_type == DRAG_MOVE_X) {
  1794. new_pos.y = previous_pos.y;
  1795. } else if (drag_type == DRAG_MOVE_Y) {
  1796. new_pos.x = previous_pos.x;
  1797. }
  1798. bool single_axis = m->is_shift_pressed();
  1799. if (single_axis) {
  1800. if (ABS(new_pos.x - previous_pos.x) > ABS(new_pos.y - previous_pos.y)) {
  1801. new_pos.y = previous_pos.y;
  1802. } else {
  1803. new_pos.x = previous_pos.x;
  1804. }
  1805. }
  1806. int index = 0;
  1807. for (CanvasItem *canvas_item : drag_selection) {
  1808. Transform2D xform = canvas_item->get_global_transform_with_canvas().affine_inverse() * canvas_item->get_transform();
  1809. canvas_item->_edit_set_position(canvas_item->_edit_get_position() + xform.xform(new_pos) - xform.xform(previous_pos));
  1810. index++;
  1811. }
  1812. return true;
  1813. }
  1814. // Confirm the move (only if it was moved)
  1815. if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
  1816. if (transform.affine_inverse().xform(b->get_position()) != drag_from) {
  1817. if (drag_selection.size() != 1) {
  1818. _commit_canvas_item_state(
  1819. drag_selection,
  1820. vformat(TTR("Move %d CanvasItems"), drag_selection.size()),
  1821. true);
  1822. } else {
  1823. _commit_canvas_item_state(
  1824. drag_selection,
  1825. vformat(
  1826. TTR("Move CanvasItem \"%s\" to (%d, %d)"),
  1827. drag_selection[0]->get_name(),
  1828. drag_selection[0]->_edit_get_position().x,
  1829. drag_selection[0]->_edit_get_position().y),
  1830. true);
  1831. }
  1832. }
  1833. if (key_auto_insert_button->is_pressed()) {
  1834. _insert_animation_keys(true, false, false, true);
  1835. }
  1836. //Make sure smart snapping lines disappear.
  1837. snap_target[0] = SNAP_TARGET_NONE;
  1838. snap_target[1] = SNAP_TARGET_NONE;
  1839. drag_type = DRAG_NONE;
  1840. viewport->update();
  1841. return true;
  1842. }
  1843. // Cancel a drag
  1844. if (b.is_valid() && b->get_button_index() == MouseButton::RIGHT && b->is_pressed()) {
  1845. _restore_canvas_item_state(drag_selection, true);
  1846. snap_target[0] = SNAP_TARGET_NONE;
  1847. snap_target[1] = SNAP_TARGET_NONE;
  1848. drag_type = DRAG_NONE;
  1849. viewport->update();
  1850. return true;
  1851. }
  1852. }
  1853. // Move the canvas items with the arrow keys
  1854. if (k.is_valid() && k->is_pressed() && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
  1855. (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
  1856. if (!k->is_echo()) {
  1857. // Start moving the canvas items with the keyboard, if they are movable
  1858. List<CanvasItem *> selection = _get_edited_canvas_items();
  1859. drag_selection.clear();
  1860. for (CanvasItem *item : selection) {
  1861. if (_is_node_movable(item, true)) {
  1862. drag_selection.push_back(item);
  1863. }
  1864. }
  1865. drag_type = DRAG_KEY_MOVE;
  1866. drag_from = Vector2();
  1867. drag_to = Vector2();
  1868. _save_canvas_item_state(drag_selection, true);
  1869. }
  1870. if (drag_selection.size() > 0) {
  1871. _restore_canvas_item_state(drag_selection, true);
  1872. bool move_local_base = k->is_alt_pressed();
  1873. bool move_local_base_rotated = k->is_ctrl_pressed() || k->is_meta_pressed();
  1874. Vector2 dir;
  1875. if (k->get_keycode() == Key::UP) {
  1876. dir += Vector2(0, -1);
  1877. } else if (k->get_keycode() == Key::DOWN) {
  1878. dir += Vector2(0, 1);
  1879. } else if (k->get_keycode() == Key::LEFT) {
  1880. dir += Vector2(-1, 0);
  1881. } else if (k->get_keycode() == Key::RIGHT) {
  1882. dir += Vector2(1, 0);
  1883. }
  1884. if (k->is_shift_pressed()) {
  1885. dir *= grid_step * Math::pow(2.0, grid_step_multiplier);
  1886. }
  1887. drag_to += dir;
  1888. if (k->is_shift_pressed()) {
  1889. drag_to = drag_to.snapped(grid_step * Math::pow(2.0, grid_step_multiplier));
  1890. }
  1891. Point2 previous_pos;
  1892. if (drag_selection.size() == 1) {
  1893. Transform2D xform = drag_selection[0]->get_global_transform_with_canvas() * drag_selection[0]->get_transform().affine_inverse();
  1894. previous_pos = xform.xform(drag_selection[0]->_edit_get_position());
  1895. } else {
  1896. previous_pos = _get_encompassing_rect_from_list(drag_selection).position;
  1897. }
  1898. Point2 new_pos;
  1899. if (drag_selection.size() == 1) {
  1900. Node2D *node_2d = Object::cast_to<Node2D>(drag_selection[0]);
  1901. if (node_2d && move_local_base_rotated) {
  1902. Transform2D m2;
  1903. m2.rotate(node_2d->get_rotation());
  1904. new_pos += m2.xform(drag_to);
  1905. } else if (move_local_base) {
  1906. new_pos += drag_to;
  1907. } else {
  1908. new_pos = previous_pos + (drag_to - drag_from);
  1909. }
  1910. } else {
  1911. new_pos = previous_pos + (drag_to - drag_from);
  1912. }
  1913. int index = 0;
  1914. for (CanvasItem *canvas_item : drag_selection) {
  1915. Transform2D xform = canvas_item->get_global_transform_with_canvas().affine_inverse() * canvas_item->get_transform();
  1916. canvas_item->_edit_set_position(canvas_item->_edit_get_position() + xform.xform(new_pos) - xform.xform(previous_pos));
  1917. index++;
  1918. }
  1919. }
  1920. return true;
  1921. }
  1922. if (k.is_valid() && !k->is_pressed() && drag_type == DRAG_KEY_MOVE && (tool == TOOL_SELECT || tool == TOOL_MOVE) &&
  1923. (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)) {
  1924. // Confirm canvas items move by arrow keys
  1925. if ((!Input::get_singleton()->is_key_pressed(Key::UP)) &&
  1926. (!Input::get_singleton()->is_key_pressed(Key::DOWN)) &&
  1927. (!Input::get_singleton()->is_key_pressed(Key::LEFT)) &&
  1928. (!Input::get_singleton()->is_key_pressed(Key::RIGHT))) {
  1929. if (drag_selection.size() > 1) {
  1930. _commit_canvas_item_state(
  1931. drag_selection,
  1932. vformat(TTR("Move %d CanvasItems"), drag_selection.size()),
  1933. true);
  1934. } else if (drag_selection.size() == 1) {
  1935. _commit_canvas_item_state(
  1936. drag_selection,
  1937. vformat(TTR("Move CanvasItem \"%s\" to (%d, %d)"),
  1938. drag_selection[0]->get_name(),
  1939. drag_selection[0]->_edit_get_position().x,
  1940. drag_selection[0]->_edit_get_position().y),
  1941. true);
  1942. }
  1943. drag_type = DRAG_NONE;
  1944. }
  1945. viewport->update();
  1946. return true;
  1947. }
  1948. return (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::LEFT || k->get_keycode() == Key::RIGHT)); // Accept the key event in any case
  1949. }
  1950. bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) {
  1951. Ref<InputEventMouseButton> b = p_event;
  1952. Ref<InputEventMouseMotion> m = p_event;
  1953. Ref<InputEventKey> k = p_event;
  1954. if (drag_type == DRAG_NONE) {
  1955. if (b.is_valid() && b->is_pressed() &&
  1956. ((b->get_button_index() == MouseButton::RIGHT && b->is_alt_pressed() && tool == TOOL_SELECT) ||
  1957. (b->get_button_index() == MouseButton::LEFT && tool == TOOL_LIST_SELECT))) {
  1958. // Popup the selection menu list
  1959. Point2 click = transform.affine_inverse().xform(b->get_position());
  1960. _get_canvas_items_at_pos(click, selection_results, b->is_alt_pressed() && tool == TOOL_SELECT);
  1961. if (selection_results.size() == 1) {
  1962. CanvasItem *item = selection_results[0].item;
  1963. selection_results.clear();
  1964. _select_click_on_item(item, click, b->is_shift_pressed());
  1965. return true;
  1966. } else if (!selection_results.is_empty()) {
  1967. // Sorts items according the their z-index
  1968. selection_results.sort();
  1969. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  1970. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  1971. for (int i = 0; i < selection_results.size(); i++) {
  1972. CanvasItem *item = selection_results[i].item;
  1973. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(item, "Node");
  1974. String node_path = "/" + root_name + "/" + root_path.rel_path_to(item->get_path());
  1975. int locked = 0;
  1976. if (_is_node_locked(item)) {
  1977. locked = 1;
  1978. } else {
  1979. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  1980. Node *node = item;
  1981. while (node && node != scene->get_parent()) {
  1982. CanvasItem *canvas_item_tmp = Object::cast_to<CanvasItem>(node);
  1983. if (canvas_item_tmp && node->has_meta("_edit_group_")) {
  1984. locked = 2;
  1985. }
  1986. node = node->get_parent();
  1987. }
  1988. }
  1989. String suffix = String();
  1990. if (locked == 1) {
  1991. suffix = " (" + TTR("Locked") + ")";
  1992. } else if (locked == 2) {
  1993. suffix = " (" + TTR("Grouped") + ")";
  1994. }
  1995. selection_menu->add_item((String)item->get_name() + suffix);
  1996. selection_menu->set_item_icon(i, icon);
  1997. selection_menu->set_item_metadata(i, node_path);
  1998. selection_menu->set_item_tooltip(i, String(item->get_name()) + "\nType: " + item->get_class() + "\nPath: " + node_path);
  1999. }
  2000. selection_menu_additive_selection = b->is_shift_pressed();
  2001. selection_menu->set_position(get_screen_position() + b->get_position());
  2002. selection_menu->reset_size();
  2003. selection_menu->popup();
  2004. return true;
  2005. }
  2006. }
  2007. if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) {
  2008. add_node_menu->clear();
  2009. add_node_menu->add_icon_item(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), TTR("Add Node Here"), ADD_NODE);
  2010. add_node_menu->add_icon_item(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), TTR("Instantiate Scene Here"), ADD_INSTANCE);
  2011. for (Node *node : SceneTreeDock::get_singleton()->get_node_clipboard()) {
  2012. if (Object::cast_to<CanvasItem>(node)) {
  2013. add_node_menu->add_icon_item(get_theme_icon(SNAME("ActionPaste"), SNAME("EditorIcons")), TTR("Paste Node(s) Here"), ADD_PASTE);
  2014. break;
  2015. }
  2016. }
  2017. for (Node *node : EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list()) {
  2018. if (Object::cast_to<CanvasItem>(node)) {
  2019. add_node_menu->add_icon_item(get_theme_icon(SNAME("ToolMove"), SNAME("EditorIcons")), TTR("Move Node(s) Here"), ADD_MOVE);
  2020. break;
  2021. }
  2022. }
  2023. add_node_menu->reset_size();
  2024. add_node_menu->set_position(viewport->get_screen_transform().xform(b->get_position()));
  2025. add_node_menu->popup();
  2026. node_create_position = transform.affine_inverse().xform(b->get_position());
  2027. return true;
  2028. }
  2029. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT && b->is_pressed() && tool == TOOL_SELECT && !panner->is_panning()) {
  2030. // Single item selection
  2031. Point2 click = transform.affine_inverse().xform(b->get_position());
  2032. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  2033. if (!scene) {
  2034. return true;
  2035. }
  2036. // Find the item to select
  2037. CanvasItem *canvas_item = nullptr;
  2038. Vector<_SelectResult> selection = Vector<_SelectResult>();
  2039. // Retrieve the canvas items
  2040. _get_canvas_items_at_pos(click, selection);
  2041. if (!selection.is_empty()) {
  2042. canvas_item = selection[0].item;
  2043. }
  2044. if (!canvas_item) {
  2045. // Start a box selection
  2046. if (!b->is_shift_pressed()) {
  2047. // Clear the selection if not additive
  2048. editor_selection->clear();
  2049. viewport->update();
  2050. selected_from_canvas = true;
  2051. };
  2052. drag_from = click;
  2053. drag_type = DRAG_BOX_SELECTION;
  2054. box_selecting_to = drag_from;
  2055. return true;
  2056. } else {
  2057. bool still_selected = _select_click_on_item(canvas_item, click, b->is_shift_pressed());
  2058. // Start dragging
  2059. if (still_selected) {
  2060. // Drag the node(s) if requested
  2061. drag_start_origin = click;
  2062. drag_type = DRAG_QUEUED;
  2063. }
  2064. // Select the item
  2065. return true;
  2066. }
  2067. }
  2068. }
  2069. if (drag_type == DRAG_QUEUED) {
  2070. if (b.is_valid() && !b->is_pressed()) {
  2071. drag_type = DRAG_NONE;
  2072. return true;
  2073. }
  2074. if (m.is_valid()) {
  2075. Point2 click = transform.affine_inverse().xform(m->get_position());
  2076. bool movement_threshold_passed = drag_start_origin.distance_to(click) > (8 * MAX(1, EDSCALE)) / zoom;
  2077. if (m.is_valid() && movement_threshold_passed) {
  2078. List<CanvasItem *> selection2 = _get_edited_canvas_items();
  2079. drag_selection.clear();
  2080. for (int i = 0; i < selection2.size(); i++) {
  2081. if (_is_node_movable(selection2[i], true)) {
  2082. drag_selection.push_back(selection2[i]);
  2083. }
  2084. }
  2085. if (selection2.size() > 0) {
  2086. drag_type = DRAG_MOVE;
  2087. drag_from = drag_start_origin;
  2088. _save_canvas_item_state(drag_selection);
  2089. }
  2090. return true;
  2091. }
  2092. }
  2093. }
  2094. if (drag_type == DRAG_BOX_SELECTION) {
  2095. if (b.is_valid() && !b->is_pressed() && b->get_button_index() == MouseButton::LEFT) {
  2096. // Confirms box selection
  2097. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  2098. if (scene) {
  2099. List<CanvasItem *> selitems;
  2100. Point2 bsfrom = drag_from;
  2101. Point2 bsto = box_selecting_to;
  2102. if (bsfrom.x > bsto.x) {
  2103. SWAP(bsfrom.x, bsto.x);
  2104. }
  2105. if (bsfrom.y > bsto.y) {
  2106. SWAP(bsfrom.y, bsto.y);
  2107. }
  2108. _find_canvas_items_in_rect(Rect2(bsfrom, bsto - bsfrom), scene, &selitems);
  2109. if (selitems.size() == 1 && editor_selection->get_selected_node_list().is_empty()) {
  2110. EditorNode::get_singleton()->push_item(selitems[0]);
  2111. }
  2112. for (CanvasItem *E : selitems) {
  2113. editor_selection->add_node(E);
  2114. }
  2115. }
  2116. drag_type = DRAG_NONE;
  2117. viewport->update();
  2118. return true;
  2119. }
  2120. if (b.is_valid() && b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) {
  2121. // Cancel box selection
  2122. drag_type = DRAG_NONE;
  2123. viewport->update();
  2124. return true;
  2125. }
  2126. if (m.is_valid()) {
  2127. // Update box selection
  2128. box_selecting_to = transform.affine_inverse().xform(m->get_position());
  2129. viewport->update();
  2130. return true;
  2131. }
  2132. }
  2133. if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::ESCAPE && drag_type == DRAG_NONE && tool == TOOL_SELECT) {
  2134. // Unselect everything
  2135. editor_selection->clear();
  2136. viewport->update();
  2137. }
  2138. return false;
  2139. }
  2140. bool CanvasItemEditor::_gui_input_ruler_tool(const Ref<InputEvent> &p_event) {
  2141. if (tool != TOOL_RULER) {
  2142. ruler_tool_active = false;
  2143. return false;
  2144. }
  2145. Ref<InputEventMouseButton> b = p_event;
  2146. Ref<InputEventMouseMotion> m = p_event;
  2147. Point2 previous_origin = ruler_tool_origin;
  2148. if (!ruler_tool_active) {
  2149. ruler_tool_origin = snap_point(viewport->get_local_mouse_position() / zoom + view_offset);
  2150. }
  2151. if (b.is_valid() && b->get_button_index() == MouseButton::LEFT) {
  2152. if (b->is_pressed()) {
  2153. ruler_tool_active = true;
  2154. } else {
  2155. ruler_tool_active = false;
  2156. }
  2157. viewport->update();
  2158. return true;
  2159. }
  2160. if (m.is_valid() && (ruler_tool_active || (grid_snap_active && previous_origin != ruler_tool_origin))) {
  2161. viewport->update();
  2162. return true;
  2163. }
  2164. return false;
  2165. }
  2166. bool CanvasItemEditor::_gui_input_hover(const Ref<InputEvent> &p_event) {
  2167. Ref<InputEventMouseMotion> m = p_event;
  2168. if (m.is_valid()) {
  2169. Point2 click = transform.affine_inverse().xform(m->get_position());
  2170. // Checks if the hovered items changed, update the viewport if so
  2171. Vector<_SelectResult> hovering_results_items;
  2172. _get_canvas_items_at_pos(click, hovering_results_items);
  2173. hovering_results_items.sort();
  2174. // Compute the nodes names and icon position
  2175. Vector<_HoverResult> hovering_results_tmp;
  2176. for (int i = 0; i < hovering_results_items.size(); i++) {
  2177. CanvasItem *canvas_item = hovering_results_items[i].item;
  2178. if (canvas_item->_edit_use_rect()) {
  2179. continue;
  2180. }
  2181. _HoverResult hover_result;
  2182. hover_result.position = canvas_item->get_global_transform_with_canvas().get_origin();
  2183. hover_result.icon = EditorNode::get_singleton()->get_object_icon(canvas_item);
  2184. hover_result.name = canvas_item->get_name();
  2185. hovering_results_tmp.push_back(hover_result);
  2186. }
  2187. // Check if changed, if so, update.
  2188. bool changed = false;
  2189. if (hovering_results_tmp.size() == hovering_results.size()) {
  2190. for (int i = 0; i < hovering_results_tmp.size(); i++) {
  2191. _HoverResult a = hovering_results_tmp[i];
  2192. _HoverResult b = hovering_results[i];
  2193. if (a.icon != b.icon || a.name != b.name || a.position != b.position) {
  2194. changed = true;
  2195. break;
  2196. }
  2197. }
  2198. } else {
  2199. changed = true;
  2200. }
  2201. if (changed) {
  2202. hovering_results = hovering_results_tmp;
  2203. viewport->update();
  2204. }
  2205. return true;
  2206. }
  2207. return false;
  2208. }
  2209. void CanvasItemEditor::_gui_input_viewport(const Ref<InputEvent> &p_event) {
  2210. bool accepted = false;
  2211. Ref<InputEventMouseButton> mb = p_event;
  2212. bool release_lmb = (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT); // Required to properly release some stuff (e.g. selection box) while panning.
  2213. if (EditorSettings::get_singleton()->get("editors/panning/simple_panning") || !pan_pressed || release_lmb) {
  2214. if ((accepted = _gui_input_rulers_and_guides(p_event))) {
  2215. // print_line("Rulers and guides");
  2216. } else if ((accepted = EditorNode::get_singleton()->get_editor_plugins_over()->forward_gui_input(p_event))) {
  2217. // print_line("Plugin");
  2218. } else if ((accepted = _gui_input_open_scene_on_double_click(p_event))) {
  2219. // print_line("Open scene on double click");
  2220. } else if ((accepted = _gui_input_scale(p_event))) {
  2221. // print_line("Set scale");
  2222. } else if ((accepted = _gui_input_pivot(p_event))) {
  2223. // print_line("Set pivot");
  2224. } else if ((accepted = _gui_input_resize(p_event))) {
  2225. // print_line("Resize");
  2226. } else if ((accepted = _gui_input_rotate(p_event))) {
  2227. // print_line("Rotate");
  2228. } else if ((accepted = _gui_input_move(p_event))) {
  2229. // print_line("Move");
  2230. } else if ((accepted = _gui_input_anchors(p_event))) {
  2231. // print_line("Anchors");
  2232. } else if ((accepted = _gui_input_select(p_event))) {
  2233. // print_line("Selection");
  2234. } else if ((accepted = _gui_input_ruler_tool(p_event))) {
  2235. // print_line("Measure");
  2236. } else {
  2237. // print_line("Not accepted");
  2238. }
  2239. }
  2240. accepted = (_gui_input_zoom_or_pan(p_event, accepted) || accepted);
  2241. if (accepted) {
  2242. accept_event();
  2243. }
  2244. // Handles the mouse hovering
  2245. _gui_input_hover(p_event);
  2246. // Change the cursor
  2247. _update_cursor();
  2248. // Grab focus
  2249. if (!viewport->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
  2250. viewport->call_deferred(SNAME("grab_focus"));
  2251. }
  2252. }
  2253. void CanvasItemEditor::_update_cursor() {
  2254. // Compute an eventual rotation of the cursor
  2255. const CursorShape rotation_array[4] = { CURSOR_HSIZE, CURSOR_BDIAGSIZE, CURSOR_VSIZE, CURSOR_FDIAGSIZE };
  2256. int rotation_array_index = 0;
  2257. List<CanvasItem *> selection = _get_edited_canvas_items();
  2258. if (selection.size() == 1) {
  2259. const double angle = Math::fposmod((double)selection[0]->get_global_transform_with_canvas().get_rotation(), Math_PI);
  2260. if (angle > Math_PI * 7.0 / 8.0) {
  2261. rotation_array_index = 0;
  2262. } else if (angle > Math_PI * 5.0 / 8.0) {
  2263. rotation_array_index = 1;
  2264. } else if (angle > Math_PI * 3.0 / 8.0) {
  2265. rotation_array_index = 2;
  2266. } else if (angle > Math_PI * 1.0 / 8.0) {
  2267. rotation_array_index = 3;
  2268. } else {
  2269. rotation_array_index = 0;
  2270. }
  2271. }
  2272. // Choose the correct cursor
  2273. CursorShape c = CURSOR_ARROW;
  2274. switch (drag_type) {
  2275. case DRAG_NONE:
  2276. switch (tool) {
  2277. case TOOL_MOVE:
  2278. c = CURSOR_MOVE;
  2279. break;
  2280. case TOOL_EDIT_PIVOT:
  2281. c = CURSOR_CROSS;
  2282. break;
  2283. case TOOL_PAN:
  2284. c = CURSOR_DRAG;
  2285. break;
  2286. case TOOL_RULER:
  2287. c = CURSOR_CROSS;
  2288. break;
  2289. default:
  2290. break;
  2291. }
  2292. break;
  2293. case DRAG_LEFT:
  2294. case DRAG_RIGHT:
  2295. c = rotation_array[rotation_array_index];
  2296. break;
  2297. case DRAG_V_GUIDE:
  2298. c = CURSOR_HSIZE;
  2299. break;
  2300. case DRAG_TOP:
  2301. case DRAG_BOTTOM:
  2302. c = rotation_array[(rotation_array_index + 2) % 4];
  2303. break;
  2304. case DRAG_H_GUIDE:
  2305. c = CURSOR_VSIZE;
  2306. break;
  2307. case DRAG_TOP_LEFT:
  2308. case DRAG_BOTTOM_RIGHT:
  2309. c = rotation_array[(rotation_array_index + 3) % 4];
  2310. break;
  2311. case DRAG_DOUBLE_GUIDE:
  2312. c = CURSOR_FDIAGSIZE;
  2313. break;
  2314. case DRAG_TOP_RIGHT:
  2315. case DRAG_BOTTOM_LEFT:
  2316. c = rotation_array[(rotation_array_index + 1) % 4];
  2317. break;
  2318. case DRAG_MOVE:
  2319. c = CURSOR_MOVE;
  2320. break;
  2321. default:
  2322. break;
  2323. }
  2324. if (is_hovering_h_guide) {
  2325. c = CURSOR_VSIZE;
  2326. } else if (is_hovering_v_guide) {
  2327. c = CURSOR_HSIZE;
  2328. }
  2329. if (pan_pressed) {
  2330. c = CURSOR_DRAG;
  2331. }
  2332. if (c != viewport->get_default_cursor_shape()) {
  2333. viewport->set_default_cursor_shape(c);
  2334. // Force refresh cursor if it's over the viewport.
  2335. if (viewport->get_global_rect().has_point(get_global_mouse_position())) {
  2336. DisplayServer::CursorShape ds_cursor_shape = (DisplayServer::CursorShape)viewport->get_default_cursor_shape();
  2337. DisplayServer::get_singleton()->cursor_set_shape(ds_cursor_shape);
  2338. }
  2339. }
  2340. }
  2341. void CanvasItemEditor::_draw_text_at_position(Point2 p_position, String p_string, Side p_side) {
  2342. Color color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  2343. color.a = 0.8;
  2344. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  2345. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  2346. Size2 text_size = font->get_string_size(p_string, font_size);
  2347. switch (p_side) {
  2348. case SIDE_LEFT:
  2349. p_position += Vector2(-text_size.x - 5, text_size.y / 2);
  2350. break;
  2351. case SIDE_TOP:
  2352. p_position += Vector2(-text_size.x / 2, -5);
  2353. break;
  2354. case SIDE_RIGHT:
  2355. p_position += Vector2(5, text_size.y / 2);
  2356. break;
  2357. case SIDE_BOTTOM:
  2358. p_position += Vector2(-text_size.x / 2, text_size.y + 5);
  2359. break;
  2360. }
  2361. viewport->draw_string(font, p_position, p_string, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color);
  2362. }
  2363. void CanvasItemEditor::_draw_margin_at_position(int p_value, Point2 p_position, Side p_side) {
  2364. String str = TS->format_number(vformat("%d " + TTR("px"), p_value));
  2365. if (p_value != 0) {
  2366. _draw_text_at_position(p_position, str, p_side);
  2367. }
  2368. }
  2369. void CanvasItemEditor::_draw_percentage_at_position(real_t p_value, Point2 p_position, Side p_side) {
  2370. String str = TS->format_number(vformat("%.1f ", p_value * 100.0)) + TS->percent_sign();
  2371. if (p_value != 0) {
  2372. _draw_text_at_position(p_position, str, p_side);
  2373. }
  2374. }
  2375. void CanvasItemEditor::_draw_focus() {
  2376. // Draw the focus around the base viewport
  2377. if (viewport->has_focus()) {
  2378. get_theme_stylebox(SNAME("FocusViewport"), SNAME("EditorStyles"))->draw(viewport->get_canvas_item(), Rect2(Point2(), viewport->get_size()));
  2379. }
  2380. }
  2381. void CanvasItemEditor::_draw_guides() {
  2382. Color guide_color = EditorSettings::get_singleton()->get("editors/2d/guides_color");
  2383. Transform2D xform = viewport_scrollable->get_transform() * transform;
  2384. // Guides already there
  2385. if (EditorNode::get_singleton()->get_edited_scene() && EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_vertical_guides_")) {
  2386. Array vguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_vertical_guides_");
  2387. for (int i = 0; i < vguides.size(); i++) {
  2388. if (drag_type == DRAG_V_GUIDE && i == dragged_guide_index) {
  2389. continue;
  2390. }
  2391. real_t x = xform.xform(Point2(vguides[i], 0)).x;
  2392. viewport->draw_line(Point2(x, 0), Point2(x, viewport->get_size().y), guide_color, Math::round(EDSCALE));
  2393. }
  2394. }
  2395. if (EditorNode::get_singleton()->get_edited_scene() && EditorNode::get_singleton()->get_edited_scene()->has_meta("_edit_horizontal_guides_")) {
  2396. Array hguides = EditorNode::get_singleton()->get_edited_scene()->get_meta("_edit_horizontal_guides_");
  2397. for (int i = 0; i < hguides.size(); i++) {
  2398. if (drag_type == DRAG_H_GUIDE && i == dragged_guide_index) {
  2399. continue;
  2400. }
  2401. real_t y = xform.xform(Point2(0, hguides[i])).y;
  2402. viewport->draw_line(Point2(0, y), Point2(viewport->get_size().x, y), guide_color, Math::round(EDSCALE));
  2403. }
  2404. }
  2405. // Dragged guide
  2406. Color text_color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  2407. Color outline_color = text_color.inverted();
  2408. const float outline_size = 2;
  2409. if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_V_GUIDE) {
  2410. String str = TS->format_number(vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).x)));
  2411. Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  2412. int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  2413. Size2 text_size = font->get_string_size(str, font_size);
  2414. viewport->draw_string(font, Point2(dragged_guide_pos.x + 10, RULER_WIDTH + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color, outline_size, outline_color);
  2415. viewport->draw_line(Point2(dragged_guide_pos.x, 0), Point2(dragged_guide_pos.x, viewport->get_size().y), guide_color, Math::round(EDSCALE));
  2416. }
  2417. if (drag_type == DRAG_DOUBLE_GUIDE || drag_type == DRAG_H_GUIDE) {
  2418. String str = TS->format_number(vformat("%d px", Math::round(xform.affine_inverse().xform(dragged_guide_pos).y)));
  2419. Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  2420. int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  2421. Size2 text_size = font->get_string_size(str, font_size);
  2422. viewport->draw_string(font, Point2(RULER_WIDTH + 10, dragged_guide_pos.y + text_size.y / 2 + 10), str, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, text_color, outline_size, outline_color);
  2423. viewport->draw_line(Point2(0, dragged_guide_pos.y), Point2(viewport->get_size().x, dragged_guide_pos.y), guide_color, Math::round(EDSCALE));
  2424. }
  2425. }
  2426. void CanvasItemEditor::_draw_smart_snapping() {
  2427. Color line_color = EditorSettings::get_singleton()->get("editors/2d/smart_snapping_line_color");
  2428. if (snap_target[0] != SNAP_TARGET_NONE && snap_target[0] != SNAP_TARGET_GRID) {
  2429. viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
  2430. viewport->draw_line(Point2(0, -1.0e+10F), Point2(0, 1.0e+10F), line_color);
  2431. viewport->draw_set_transform_matrix(viewport->get_transform());
  2432. }
  2433. if (snap_target[1] != SNAP_TARGET_NONE && snap_target[1] != SNAP_TARGET_GRID) {
  2434. viewport->draw_set_transform_matrix(viewport->get_transform() * transform * snap_transform);
  2435. viewport->draw_line(Point2(-1.0e+10F, 0), Point2(1.0e+10F, 0), line_color);
  2436. viewport->draw_set_transform_matrix(viewport->get_transform());
  2437. }
  2438. }
  2439. void CanvasItemEditor::_draw_rulers() {
  2440. Color bg_color = get_theme_color(SNAME("dark_color_2"), SNAME("Editor"));
  2441. Color graduation_color = get_theme_color(SNAME("font_color"), SNAME("Editor")).lerp(bg_color, 0.5);
  2442. Color font_color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  2443. font_color.a = 0.8;
  2444. Ref<Font> font = get_theme_font(SNAME("rulers"), SNAME("EditorFonts"));
  2445. int font_size = get_theme_font_size(SNAME("rulers_size"), SNAME("EditorFonts"));
  2446. // The rule transform
  2447. Transform2D ruler_transform = Transform2D();
  2448. if (grid_snap_active || _is_grid_visible()) {
  2449. List<CanvasItem *> selection = _get_edited_canvas_items();
  2450. if (snap_relative && selection.size() > 0) {
  2451. ruler_transform.translate(_get_encompassing_rect_from_list(selection).position);
  2452. ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier));
  2453. } else {
  2454. ruler_transform.translate(grid_offset);
  2455. ruler_transform.scale_basis(grid_step * Math::pow(2.0, grid_step_multiplier));
  2456. }
  2457. while ((transform * ruler_transform).get_scale().x < 50 || (transform * ruler_transform).get_scale().y < 50) {
  2458. ruler_transform.scale_basis(Point2(2, 2));
  2459. }
  2460. } else {
  2461. real_t basic_rule = 100;
  2462. for (int i = 0; basic_rule * zoom > 100; i++) {
  2463. basic_rule /= (i % 2) ? 5.0 : 2.0;
  2464. }
  2465. for (int i = 0; basic_rule * zoom < 100; i++) {
  2466. basic_rule *= (i % 2) ? 2.0 : 5.0;
  2467. }
  2468. ruler_transform.scale(Size2(basic_rule, basic_rule));
  2469. }
  2470. // Subdivisions
  2471. int major_subdivision = 2;
  2472. Transform2D major_subdivide = Transform2D();
  2473. major_subdivide.scale(Size2(1.0 / major_subdivision, 1.0 / major_subdivision));
  2474. int minor_subdivision = 5;
  2475. Transform2D minor_subdivide = Transform2D();
  2476. minor_subdivide.scale(Size2(1.0 / minor_subdivision, 1.0 / minor_subdivision));
  2477. // First and last graduations to draw (in the ruler space)
  2478. Point2 first = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(Point2(RULER_WIDTH, RULER_WIDTH));
  2479. Point2 last = (transform * ruler_transform * major_subdivide * minor_subdivide).affine_inverse().xform(viewport->get_size());
  2480. // Draw top ruler
  2481. viewport->draw_rect(Rect2(Point2(RULER_WIDTH, 0), Size2(viewport->get_size().x, RULER_WIDTH)), bg_color);
  2482. for (int i = Math::ceil(first.x); i < last.x; i++) {
  2483. Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)).round();
  2484. if (i % (major_subdivision * minor_subdivision) == 0) {
  2485. viewport->draw_line(Point2(position.x, 0), Point2(position.x, RULER_WIDTH), graduation_color, Math::round(EDSCALE));
  2486. real_t val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(i, 0)).x;
  2487. viewport->draw_string(font, Point2(position.x + 2, font->get_height(font_size)), TS->format_number(vformat(((int)val == val) ? "%d" : "%.1f", val)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
  2488. } else {
  2489. if (i % minor_subdivision == 0) {
  2490. viewport->draw_line(Point2(position.x, RULER_WIDTH * 0.33), Point2(position.x, RULER_WIDTH), graduation_color, Math::round(EDSCALE));
  2491. } else {
  2492. viewport->draw_line(Point2(position.x, RULER_WIDTH * 0.75), Point2(position.x, RULER_WIDTH), graduation_color, Math::round(EDSCALE));
  2493. }
  2494. }
  2495. }
  2496. // Draw left ruler
  2497. viewport->draw_rect(Rect2(Point2(0, RULER_WIDTH), Size2(RULER_WIDTH, viewport->get_size().y)), bg_color);
  2498. for (int i = Math::ceil(first.y); i < last.y; i++) {
  2499. Point2 position = (transform * ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)).round();
  2500. if (i % (major_subdivision * minor_subdivision) == 0) {
  2501. viewport->draw_line(Point2(0, position.y), Point2(RULER_WIDTH, position.y), graduation_color, Math::round(EDSCALE));
  2502. real_t val = (ruler_transform * major_subdivide * minor_subdivide).xform(Point2(0, i)).y;
  2503. Transform2D text_xform = Transform2D(-Math_PI / 2.0, Point2(font->get_height(font_size), position.y - 2));
  2504. viewport->draw_set_transform_matrix(viewport->get_transform() * text_xform);
  2505. viewport->draw_string(font, Point2(), TS->format_number(vformat(((int)val == val) ? "%d" : "%.1f", val)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
  2506. viewport->draw_set_transform_matrix(viewport->get_transform());
  2507. } else {
  2508. if (i % minor_subdivision == 0) {
  2509. viewport->draw_line(Point2(RULER_WIDTH * 0.33, position.y), Point2(RULER_WIDTH, position.y), graduation_color, Math::round(EDSCALE));
  2510. } else {
  2511. viewport->draw_line(Point2(RULER_WIDTH * 0.75, position.y), Point2(RULER_WIDTH, position.y), graduation_color, Math::round(EDSCALE));
  2512. }
  2513. }
  2514. }
  2515. // Draw the top left corner
  2516. viewport->draw_rect(Rect2(Point2(), Size2(RULER_WIDTH, RULER_WIDTH)), graduation_color);
  2517. }
  2518. void CanvasItemEditor::_draw_grid() {
  2519. if (_is_grid_visible()) {
  2520. // Draw the grid
  2521. Vector2 real_grid_offset;
  2522. const List<CanvasItem *> selection = _get_edited_canvas_items();
  2523. if (snap_relative && selection.size() > 0) {
  2524. const Vector2 topleft = _get_encompassing_rect_from_list(selection).position;
  2525. real_grid_offset.x = fmod(topleft.x, grid_step.x * (real_t)Math::pow(2.0, grid_step_multiplier));
  2526. real_grid_offset.y = fmod(topleft.y, grid_step.y * (real_t)Math::pow(2.0, grid_step_multiplier));
  2527. } else {
  2528. real_grid_offset = grid_offset;
  2529. }
  2530. // Draw a "primary" line every several lines to make measurements easier.
  2531. // The step is configurable in the Configure Snap dialog.
  2532. const Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/2d/grid_color");
  2533. const Color primary_grid_color =
  2534. Color(secondary_grid_color.r, secondary_grid_color.g, secondary_grid_color.b, secondary_grid_color.a * 2.5);
  2535. const Size2 viewport_size = viewport->get_size();
  2536. const Transform2D xform = transform.affine_inverse();
  2537. int last_cell = 0;
  2538. if (grid_step.x != 0) {
  2539. for (int i = 0; i < viewport_size.width; i++) {
  2540. const int cell =
  2541. Math::fast_ftoi(Math::floor((xform.xform(Vector2(i, 0)).x - real_grid_offset.x) / (grid_step.x * Math::pow(2.0, grid_step_multiplier))));
  2542. if (i == 0) {
  2543. last_cell = cell;
  2544. }
  2545. if (last_cell != cell) {
  2546. Color grid_color;
  2547. if (primary_grid_steps == 0) {
  2548. grid_color = secondary_grid_color;
  2549. } else {
  2550. grid_color = cell % primary_grid_steps == 0 ? primary_grid_color : secondary_grid_color;
  2551. }
  2552. viewport->draw_line(Point2(i, 0), Point2(i, viewport_size.height), grid_color, Math::round(EDSCALE));
  2553. }
  2554. last_cell = cell;
  2555. }
  2556. }
  2557. if (grid_step.y != 0) {
  2558. for (int i = 0; i < viewport_size.height; i++) {
  2559. const int cell =
  2560. Math::fast_ftoi(Math::floor((xform.xform(Vector2(0, i)).y - real_grid_offset.y) / (grid_step.y * Math::pow(2.0, grid_step_multiplier))));
  2561. if (i == 0) {
  2562. last_cell = cell;
  2563. }
  2564. if (last_cell != cell) {
  2565. Color grid_color;
  2566. if (primary_grid_steps == 0) {
  2567. grid_color = secondary_grid_color;
  2568. } else {
  2569. grid_color = cell % primary_grid_steps == 0 ? primary_grid_color : secondary_grid_color;
  2570. }
  2571. viewport->draw_line(Point2(0, i), Point2(viewport_size.width, i), grid_color, Math::round(EDSCALE));
  2572. }
  2573. last_cell = cell;
  2574. }
  2575. }
  2576. }
  2577. }
  2578. void CanvasItemEditor::_draw_ruler_tool() {
  2579. if (tool != TOOL_RULER) {
  2580. return;
  2581. }
  2582. if (ruler_tool_active) {
  2583. Color ruler_primary_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  2584. Color ruler_secondary_color = ruler_primary_color;
  2585. ruler_secondary_color.a = 0.5;
  2586. Point2 begin = (ruler_tool_origin - view_offset) * zoom;
  2587. Point2 end = snap_point(viewport->get_local_mouse_position() / zoom + view_offset) * zoom - view_offset * zoom;
  2588. Point2 corner = Point2(begin.x, end.y);
  2589. Vector2 length_vector = (begin - end).abs() / zoom;
  2590. Ref<Font> font = get_theme_font(SNAME("bold"), SNAME("EditorFonts"));
  2591. int font_size = get_theme_font_size(SNAME("bold_size"), SNAME("EditorFonts"));
  2592. Color font_color = get_theme_color(SNAME("font_color"), SNAME("Editor"));
  2593. Color font_secondary_color = font_color;
  2594. font_secondary_color.set_v(font_secondary_color.get_v() > 0.5 ? 0.7 : 0.3);
  2595. Color outline_color = font_color.inverted();
  2596. float text_height = font->get_height(font_size);
  2597. const float outline_size = 2;
  2598. const float text_width = 76;
  2599. const float angle_text_width = 54;
  2600. Point2 text_pos = (begin + end) / 2 - Vector2(text_width / 2, text_height / 2);
  2601. text_pos.x = CLAMP(text_pos.x, text_width / 2, viewport->get_rect().size.x - text_width * 1.5);
  2602. text_pos.y = CLAMP(text_pos.y, text_height * 1.5, viewport->get_rect().size.y - text_height * 1.5);
  2603. if (begin.is_equal_approx(end)) {
  2604. viewport->draw_string(font, text_pos, (String)ruler_tool_origin, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color, outline_size, outline_color);
  2605. Ref<Texture2D> position_icon = get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
  2606. viewport->draw_texture(get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons")), (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
  2607. return;
  2608. }
  2609. viewport->draw_string(font, text_pos, TS->format_number(vformat("%.1f px", length_vector.length())), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color, outline_size, outline_color);
  2610. bool draw_secondary_lines = !(Math::is_equal_approx(begin.y, corner.y) || Math::is_equal_approx(end.x, corner.x));
  2611. viewport->draw_line(begin, end, ruler_primary_color, Math::round(EDSCALE * 3));
  2612. if (draw_secondary_lines) {
  2613. viewport->draw_line(begin, corner, ruler_secondary_color, Math::round(EDSCALE));
  2614. viewport->draw_line(corner, end, ruler_secondary_color, Math::round(EDSCALE));
  2615. }
  2616. if (draw_secondary_lines) {
  2617. const real_t horizontal_angle_rad = length_vector.angle();
  2618. const real_t vertical_angle_rad = Math_PI / 2.0 - horizontal_angle_rad;
  2619. const int horizontal_angle = round(180 * horizontal_angle_rad / Math_PI);
  2620. const int vertical_angle = round(180 * vertical_angle_rad / Math_PI);
  2621. Point2 text_pos2 = text_pos;
  2622. text_pos2.x = begin.x < text_pos.x ? MIN(text_pos.x - text_width, begin.x - text_width / 2) : MAX(text_pos.x + text_width, begin.x - text_width / 2);
  2623. viewport->draw_string(font, text_pos2, TS->format_number(vformat("%.1f px", length_vector.y)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2624. Point2 v_angle_text_pos = Point2();
  2625. v_angle_text_pos.x = CLAMP(begin.x - angle_text_width / 2, angle_text_width / 2, viewport->get_rect().size.x - angle_text_width);
  2626. v_angle_text_pos.y = begin.y < end.y ? MIN(text_pos2.y - 2 * text_height, begin.y - text_height * 0.5) : MAX(text_pos2.y + text_height * 3, begin.y + text_height * 1.5);
  2627. viewport->draw_string(font, v_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), vertical_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2628. text_pos2 = text_pos;
  2629. text_pos2.y = end.y < text_pos.y ? MIN(text_pos.y - text_height * 2, end.y - text_height / 2) : MAX(text_pos.y + text_height * 2, end.y - text_height / 2);
  2630. viewport->draw_string(font, text_pos2, TS->format_number(vformat("%.1f px", length_vector.x)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2631. Point2 h_angle_text_pos = Point2();
  2632. h_angle_text_pos.x = CLAMP(end.x - angle_text_width / 2, angle_text_width / 2, viewport->get_rect().size.x - angle_text_width);
  2633. if (begin.y < end.y) {
  2634. h_angle_text_pos.y = end.y + text_height * 1.5;
  2635. if (ABS(text_pos2.x - h_angle_text_pos.x) < text_width) {
  2636. int height_multiplier = 1.5 + (int)grid_snap_active;
  2637. h_angle_text_pos.y = MAX(text_pos.y + height_multiplier * text_height, MAX(end.y + text_height * 1.5, text_pos2.y + height_multiplier * text_height));
  2638. }
  2639. } else {
  2640. h_angle_text_pos.y = end.y - text_height * 0.5;
  2641. if (ABS(text_pos2.x - h_angle_text_pos.x) < text_width) {
  2642. int height_multiplier = 1 + (int)grid_snap_active;
  2643. h_angle_text_pos.y = MIN(text_pos.y - height_multiplier * text_height, MIN(end.y - text_height * 0.5, text_pos2.y - height_multiplier * text_height));
  2644. }
  2645. }
  2646. viewport->draw_string(font, h_angle_text_pos, TS->format_number(vformat(String::utf8("%d°"), horizontal_angle)), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2647. // Angle arcs
  2648. int arc_point_count = 8;
  2649. real_t arc_radius_max_length_percent = 0.1;
  2650. real_t ruler_length = length_vector.length() * zoom;
  2651. real_t arc_max_radius = 50.0;
  2652. real_t arc_line_width = 2.0;
  2653. const Vector2 end_to_begin = (end - begin);
  2654. real_t arc_1_start_angle = end_to_begin.x < 0
  2655. ? (end_to_begin.y < 0 ? 3.0 * Math_PI / 2.0 - vertical_angle_rad : Math_PI / 2.0)
  2656. : (end_to_begin.y < 0 ? 3.0 * Math_PI / 2.0 : Math_PI / 2.0 - vertical_angle_rad);
  2657. real_t arc_1_end_angle = arc_1_start_angle + vertical_angle_rad;
  2658. // Constrain arc to triangle height & max size
  2659. real_t arc_1_radius = MIN(MIN(arc_radius_max_length_percent * ruler_length, ABS(end_to_begin.y)), arc_max_radius);
  2660. real_t arc_2_start_angle = end_to_begin.x < 0
  2661. ? (end_to_begin.y < 0 ? 0.0 : -horizontal_angle_rad)
  2662. : (end_to_begin.y < 0 ? Math_PI - horizontal_angle_rad : Math_PI);
  2663. real_t arc_2_end_angle = arc_2_start_angle + horizontal_angle_rad;
  2664. // Constrain arc to triangle width & max size
  2665. real_t arc_2_radius = MIN(MIN(arc_radius_max_length_percent * ruler_length, ABS(end_to_begin.x)), arc_max_radius);
  2666. viewport->draw_arc(begin, arc_1_radius, arc_1_start_angle, arc_1_end_angle, arc_point_count, ruler_primary_color, Math::round(EDSCALE * arc_line_width));
  2667. viewport->draw_arc(end, arc_2_radius, arc_2_start_angle, arc_2_end_angle, arc_point_count, ruler_primary_color, Math::round(EDSCALE * arc_line_width));
  2668. }
  2669. if (grid_snap_active) {
  2670. text_pos = (begin + end) / 2 + Vector2(-text_width / 2, text_height / 2);
  2671. text_pos.x = CLAMP(text_pos.x, text_width / 2, viewport->get_rect().size.x - text_width * 1.5);
  2672. text_pos.y = CLAMP(text_pos.y, text_height * 2.5, viewport->get_rect().size.y - text_height / 2);
  2673. if (draw_secondary_lines) {
  2674. viewport->draw_string(font, text_pos, TS->format_number(vformat("%.2f " + TTR("units"), (length_vector / grid_step).length())), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color, outline_size, outline_color);
  2675. Point2 text_pos2 = text_pos;
  2676. text_pos2.x = begin.x < text_pos.x ? MIN(text_pos.x - text_width, begin.x - text_width / 2) : MAX(text_pos.x + text_width, begin.x - text_width / 2);
  2677. viewport->draw_string(font, text_pos2, TS->format_number(vformat("%d " + TTR("units"), roundf(length_vector.y / grid_step.y))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2678. text_pos2 = text_pos;
  2679. text_pos2.y = end.y < text_pos.y ? MIN(text_pos.y - text_height * 2, end.y + text_height / 2) : MAX(text_pos.y + text_height * 2, end.y + text_height / 2);
  2680. viewport->draw_string(font, text_pos2, TS->format_number(vformat("%d " + TTR("units"), roundf(length_vector.x / grid_step.x))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_secondary_color, outline_size, outline_color);
  2681. } else {
  2682. viewport->draw_string(font, text_pos, TS->format_number(vformat("%d " + TTR("units"), roundf((length_vector / grid_step).length()))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color, outline_size, outline_color);
  2683. }
  2684. }
  2685. } else {
  2686. if (grid_snap_active) {
  2687. Ref<Texture2D> position_icon = get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
  2688. viewport->draw_texture(get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons")), (ruler_tool_origin - view_offset) * zoom - position_icon->get_size() / 2);
  2689. }
  2690. }
  2691. }
  2692. void CanvasItemEditor::_draw_control_anchors(Control *control) {
  2693. Transform2D xform = transform * control->get_global_transform_with_canvas();
  2694. RID ci = viewport->get_canvas_item();
  2695. if (tool == TOOL_SELECT && !Object::cast_to<Container>(control->get_parent())) {
  2696. // Compute the anchors
  2697. real_t anchors_values[4];
  2698. anchors_values[0] = control->get_anchor(SIDE_LEFT);
  2699. anchors_values[1] = control->get_anchor(SIDE_TOP);
  2700. anchors_values[2] = control->get_anchor(SIDE_RIGHT);
  2701. anchors_values[3] = control->get_anchor(SIDE_BOTTOM);
  2702. Vector2 anchors_pos[4];
  2703. for (int i = 0; i < 4; i++) {
  2704. Vector2 value = Vector2((i % 2 == 0) ? anchors_values[i] : anchors_values[(i + 1) % 4], (i % 2 == 1) ? anchors_values[i] : anchors_values[(i + 1) % 4]);
  2705. anchors_pos[i] = xform.xform(_anchor_to_position(control, value));
  2706. }
  2707. // Draw the anchors handles
  2708. Rect2 anchor_rects[4];
  2709. if (control->is_layout_rtl()) {
  2710. anchor_rects[0] = Rect2(anchors_pos[0] - Vector2(0.0, anchor_handle->get_size().y), Point2(-anchor_handle->get_size().x, anchor_handle->get_size().y));
  2711. anchor_rects[1] = Rect2(anchors_pos[1] - anchor_handle->get_size(), anchor_handle->get_size());
  2712. anchor_rects[2] = Rect2(anchors_pos[2] - Vector2(anchor_handle->get_size().x, 0.0), Point2(anchor_handle->get_size().x, -anchor_handle->get_size().y));
  2713. anchor_rects[3] = Rect2(anchors_pos[3], -anchor_handle->get_size());
  2714. } else {
  2715. anchor_rects[0] = Rect2(anchors_pos[0] - anchor_handle->get_size(), anchor_handle->get_size());
  2716. anchor_rects[1] = Rect2(anchors_pos[1] - Vector2(0.0, anchor_handle->get_size().y), Point2(-anchor_handle->get_size().x, anchor_handle->get_size().y));
  2717. anchor_rects[2] = Rect2(anchors_pos[2], -anchor_handle->get_size());
  2718. anchor_rects[3] = Rect2(anchors_pos[3] - Vector2(anchor_handle->get_size().x, 0.0), Point2(anchor_handle->get_size().x, -anchor_handle->get_size().y));
  2719. }
  2720. for (int i = 0; i < 4; i++) {
  2721. anchor_handle->draw_rect(ci, anchor_rects[i]);
  2722. }
  2723. }
  2724. }
  2725. void CanvasItemEditor::_draw_control_helpers(Control *control) {
  2726. Transform2D xform = transform * control->get_global_transform_with_canvas();
  2727. if (tool == TOOL_SELECT && show_helpers && !Object::cast_to<Container>(control->get_parent())) {
  2728. // Draw the helpers
  2729. Color color_base = Color(0.8, 0.8, 0.8, 0.5);
  2730. // Compute the anchors
  2731. real_t anchors_values[4];
  2732. anchors_values[0] = control->get_anchor(SIDE_LEFT);
  2733. anchors_values[1] = control->get_anchor(SIDE_TOP);
  2734. anchors_values[2] = control->get_anchor(SIDE_RIGHT);
  2735. anchors_values[3] = control->get_anchor(SIDE_BOTTOM);
  2736. Vector2 anchors[4];
  2737. Vector2 anchors_pos[4];
  2738. for (int i = 0; i < 4; i++) {
  2739. anchors[i] = Vector2((i % 2 == 0) ? anchors_values[i] : anchors_values[(i + 1) % 4], (i % 2 == 1) ? anchors_values[i] : anchors_values[(i + 1) % 4]);
  2740. anchors_pos[i] = xform.xform(_anchor_to_position(control, anchors[i]));
  2741. }
  2742. // Get which anchor is dragged
  2743. int dragged_anchor = -1;
  2744. switch (drag_type) {
  2745. case DRAG_ANCHOR_ALL:
  2746. case DRAG_ANCHOR_TOP_LEFT:
  2747. dragged_anchor = 0;
  2748. break;
  2749. case DRAG_ANCHOR_TOP_RIGHT:
  2750. dragged_anchor = 1;
  2751. break;
  2752. case DRAG_ANCHOR_BOTTOM_RIGHT:
  2753. dragged_anchor = 2;
  2754. break;
  2755. case DRAG_ANCHOR_BOTTOM_LEFT:
  2756. dragged_anchor = 3;
  2757. break;
  2758. default:
  2759. break;
  2760. }
  2761. if (dragged_anchor >= 0) {
  2762. // Draw the 4 lines when dragged
  2763. Color color_snapped = Color(0.64, 0.93, 0.67, 0.5);
  2764. Vector2 corners_pos[4];
  2765. for (int i = 0; i < 4; i++) {
  2766. corners_pos[i] = xform.xform(_anchor_to_position(control, Vector2((i == 0 || i == 3) ? ANCHOR_BEGIN : ANCHOR_END, (i <= 1) ? ANCHOR_BEGIN : ANCHOR_END)));
  2767. }
  2768. Vector2 line_starts[4];
  2769. Vector2 line_ends[4];
  2770. for (int i = 0; i < 4; i++) {
  2771. real_t anchor_val = (i >= 2) ? ANCHOR_END - anchors_values[i] : anchors_values[i];
  2772. line_starts[i] = corners_pos[i].lerp(corners_pos[(i + 1) % 4], anchor_val);
  2773. line_ends[i] = corners_pos[(i + 3) % 4].lerp(corners_pos[(i + 2) % 4], anchor_val);
  2774. bool anchor_snapped = anchors_values[i] == 0.0 || anchors_values[i] == 0.5 || anchors_values[i] == 1.0;
  2775. viewport->draw_line(line_starts[i], line_ends[i], anchor_snapped ? color_snapped : color_base, (i == dragged_anchor || (i + 3) % 4 == dragged_anchor) ? 2 : 1);
  2776. }
  2777. // Display the percentages next to the lines
  2778. real_t percent_val;
  2779. percent_val = anchors_values[(dragged_anchor + 2) % 4] - anchors_values[dragged_anchor];
  2780. percent_val = (dragged_anchor >= 2) ? -percent_val : percent_val;
  2781. _draw_percentage_at_position(percent_val, (anchors_pos[dragged_anchor] + anchors_pos[(dragged_anchor + 1) % 4]) / 2, (Side)((dragged_anchor + 1) % 4));
  2782. percent_val = anchors_values[(dragged_anchor + 3) % 4] - anchors_values[(dragged_anchor + 1) % 4];
  2783. percent_val = ((dragged_anchor + 1) % 4 >= 2) ? -percent_val : percent_val;
  2784. _draw_percentage_at_position(percent_val, (anchors_pos[dragged_anchor] + anchors_pos[(dragged_anchor + 3) % 4]) / 2, (Side)(dragged_anchor));
  2785. percent_val = anchors_values[(dragged_anchor + 1) % 4];
  2786. percent_val = ((dragged_anchor + 1) % 4 >= 2) ? ANCHOR_END - percent_val : percent_val;
  2787. _draw_percentage_at_position(percent_val, (line_starts[dragged_anchor] + anchors_pos[dragged_anchor]) / 2, (Side)(dragged_anchor));
  2788. percent_val = anchors_values[dragged_anchor];
  2789. percent_val = (dragged_anchor >= 2) ? ANCHOR_END - percent_val : percent_val;
  2790. _draw_percentage_at_position(percent_val, (line_ends[(dragged_anchor + 1) % 4] + anchors_pos[dragged_anchor]) / 2, (Side)((dragged_anchor + 1) % 4));
  2791. }
  2792. // Draw the margin values and the node width/height when dragging control side
  2793. const real_t ratio = 0.33;
  2794. Transform2D parent_transform = xform * control->get_transform().affine_inverse();
  2795. real_t node_pos_in_parent[4];
  2796. Rect2 parent_rect = control->get_parent_anchorable_rect();
  2797. node_pos_in_parent[0] = control->get_anchor(SIDE_LEFT) * parent_rect.size.width + control->get_offset(SIDE_LEFT) + parent_rect.position.x;
  2798. node_pos_in_parent[1] = control->get_anchor(SIDE_TOP) * parent_rect.size.height + control->get_offset(SIDE_TOP) + parent_rect.position.y;
  2799. node_pos_in_parent[2] = control->get_anchor(SIDE_RIGHT) * parent_rect.size.width + control->get_offset(SIDE_RIGHT) + parent_rect.position.x;
  2800. node_pos_in_parent[3] = control->get_anchor(SIDE_BOTTOM) * parent_rect.size.height + control->get_offset(SIDE_BOTTOM) + parent_rect.position.y;
  2801. Point2 start, end;
  2802. switch (drag_type) {
  2803. case DRAG_LEFT:
  2804. case DRAG_TOP_LEFT:
  2805. case DRAG_BOTTOM_LEFT:
  2806. _draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), SIDE_BOTTOM);
  2807. [[fallthrough]];
  2808. case DRAG_MOVE:
  2809. start = Vector2(node_pos_in_parent[0], Math::lerp(node_pos_in_parent[1], node_pos_in_parent[3], ratio));
  2810. end = start - Vector2(control->get_offset(SIDE_LEFT), 0);
  2811. _draw_margin_at_position(control->get_offset(SIDE_LEFT), parent_transform.xform((start + end) / 2), SIDE_TOP);
  2812. viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, Math::round(EDSCALE));
  2813. break;
  2814. default:
  2815. break;
  2816. }
  2817. switch (drag_type) {
  2818. case DRAG_RIGHT:
  2819. case DRAG_TOP_RIGHT:
  2820. case DRAG_BOTTOM_RIGHT:
  2821. _draw_margin_at_position(control->get_size().width, parent_transform.xform(Vector2((node_pos_in_parent[0] + node_pos_in_parent[2]) / 2, node_pos_in_parent[3])) + Vector2(0, 5), SIDE_BOTTOM);
  2822. [[fallthrough]];
  2823. case DRAG_MOVE:
  2824. start = Vector2(node_pos_in_parent[2], Math::lerp(node_pos_in_parent[3], node_pos_in_parent[1], ratio));
  2825. end = start - Vector2(control->get_offset(SIDE_RIGHT), 0);
  2826. _draw_margin_at_position(control->get_offset(SIDE_RIGHT), parent_transform.xform((start + end) / 2), SIDE_BOTTOM);
  2827. viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, Math::round(EDSCALE));
  2828. break;
  2829. default:
  2830. break;
  2831. }
  2832. switch (drag_type) {
  2833. case DRAG_TOP:
  2834. case DRAG_TOP_LEFT:
  2835. case DRAG_TOP_RIGHT:
  2836. _draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2)) + Vector2(5, 0), SIDE_RIGHT);
  2837. [[fallthrough]];
  2838. case DRAG_MOVE:
  2839. start = Vector2(Math::lerp(node_pos_in_parent[0], node_pos_in_parent[2], ratio), node_pos_in_parent[1]);
  2840. end = start - Vector2(0, control->get_offset(SIDE_TOP));
  2841. _draw_margin_at_position(control->get_offset(SIDE_TOP), parent_transform.xform((start + end) / 2), SIDE_LEFT);
  2842. viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, Math::round(EDSCALE));
  2843. break;
  2844. default:
  2845. break;
  2846. }
  2847. switch (drag_type) {
  2848. case DRAG_BOTTOM:
  2849. case DRAG_BOTTOM_LEFT:
  2850. case DRAG_BOTTOM_RIGHT:
  2851. _draw_margin_at_position(control->get_size().height, parent_transform.xform(Vector2(node_pos_in_parent[2], (node_pos_in_parent[1] + node_pos_in_parent[3]) / 2) + Vector2(5, 0)), SIDE_RIGHT);
  2852. [[fallthrough]];
  2853. case DRAG_MOVE:
  2854. start = Vector2(Math::lerp(node_pos_in_parent[2], node_pos_in_parent[0], ratio), node_pos_in_parent[3]);
  2855. end = start - Vector2(0, control->get_offset(SIDE_BOTTOM));
  2856. _draw_margin_at_position(control->get_offset(SIDE_BOTTOM), parent_transform.xform((start + end) / 2), SIDE_RIGHT);
  2857. viewport->draw_line(parent_transform.xform(start), parent_transform.xform(end), color_base, Math::round(EDSCALE));
  2858. break;
  2859. default:
  2860. break;
  2861. }
  2862. switch (drag_type) {
  2863. //Draw the ghost rect if the node if rotated/scaled
  2864. case DRAG_LEFT:
  2865. case DRAG_TOP_LEFT:
  2866. case DRAG_TOP:
  2867. case DRAG_TOP_RIGHT:
  2868. case DRAG_RIGHT:
  2869. case DRAG_BOTTOM_RIGHT:
  2870. case DRAG_BOTTOM:
  2871. case DRAG_BOTTOM_LEFT:
  2872. case DRAG_MOVE:
  2873. if (control->get_rotation() != 0.0 || control->get_scale() != Vector2(1, 1)) {
  2874. Rect2 rect = Rect2(Vector2(node_pos_in_parent[0], node_pos_in_parent[1]), control->get_size());
  2875. viewport->draw_rect(parent_transform.xform(rect), color_base, false, Math::round(EDSCALE));
  2876. }
  2877. break;
  2878. default:
  2879. break;
  2880. }
  2881. }
  2882. }
  2883. void CanvasItemEditor::_draw_selection() {
  2884. Ref<Texture2D> pivot_icon = get_theme_icon(SNAME("EditorPivot"), SNAME("EditorIcons"));
  2885. Ref<Texture2D> position_icon = get_theme_icon(SNAME("EditorPosition"), SNAME("EditorIcons"));
  2886. Ref<Texture2D> previous_position_icon = get_theme_icon(SNAME("EditorPositionPrevious"), SNAME("EditorIcons"));
  2887. RID ci = viewport->get_canvas_item();
  2888. List<CanvasItem *> selection = _get_edited_canvas_items(true, false);
  2889. bool single = selection.size() == 1;
  2890. for (CanvasItem *E : selection) {
  2891. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E);
  2892. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  2893. bool item_locked = canvas_item->has_meta("_edit_lock_");
  2894. // Draw the previous position if we are dragging the node
  2895. if (show_helpers &&
  2896. (drag_type == DRAG_MOVE || drag_type == DRAG_ROTATE ||
  2897. drag_type == DRAG_LEFT || drag_type == DRAG_RIGHT || drag_type == DRAG_TOP || drag_type == DRAG_BOTTOM ||
  2898. drag_type == DRAG_TOP_LEFT || drag_type == DRAG_TOP_RIGHT || drag_type == DRAG_BOTTOM_LEFT || drag_type == DRAG_BOTTOM_RIGHT)) {
  2899. const Transform2D pre_drag_xform = transform * se->pre_drag_xform;
  2900. const Color pre_drag_color = Color(0.4, 0.6, 1, 0.7);
  2901. if (canvas_item->_edit_use_rect()) {
  2902. Vector2 pre_drag_endpoints[4] = {
  2903. pre_drag_xform.xform(se->pre_drag_rect.position),
  2904. pre_drag_xform.xform(se->pre_drag_rect.position + Vector2(se->pre_drag_rect.size.x, 0)),
  2905. pre_drag_xform.xform(se->pre_drag_rect.position + se->pre_drag_rect.size),
  2906. pre_drag_xform.xform(se->pre_drag_rect.position + Vector2(0, se->pre_drag_rect.size.y))
  2907. };
  2908. for (int i = 0; i < 4; i++) {
  2909. viewport->draw_line(pre_drag_endpoints[i], pre_drag_endpoints[(i + 1) % 4], pre_drag_color, Math::round(2 * EDSCALE));
  2910. }
  2911. } else {
  2912. viewport->draw_texture(previous_position_icon, (pre_drag_xform.xform(Point2()) - (previous_position_icon->get_size() / 2)).floor());
  2913. }
  2914. }
  2915. Transform2D xform = transform * canvas_item->get_global_transform_with_canvas();
  2916. // Draw the selected items position / surrounding boxes
  2917. if (canvas_item->_edit_use_rect()) {
  2918. Rect2 rect = canvas_item->_edit_get_rect();
  2919. const Vector2 endpoints[4] = {
  2920. xform.xform(rect.position),
  2921. xform.xform(rect.position + Vector2(rect.size.x, 0)),
  2922. xform.xform(rect.position + rect.size),
  2923. xform.xform(rect.position + Vector2(0, rect.size.y))
  2924. };
  2925. Color c = Color(1, 0.6, 0.4, 0.7);
  2926. if (item_locked) {
  2927. c = Color(0.7, 0.7, 0.7, 0.7);
  2928. }
  2929. for (int i = 0; i < 4; i++) {
  2930. viewport->draw_line(endpoints[i], endpoints[(i + 1) % 4], c, Math::round(2 * EDSCALE));
  2931. }
  2932. } else {
  2933. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  2934. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  2935. viewport->draw_set_transform_matrix(simple_xform);
  2936. viewport->draw_texture(position_icon, -(position_icon->get_size() / 2));
  2937. viewport->draw_set_transform_matrix(viewport->get_transform());
  2938. }
  2939. if (single && !item_locked && (tool == TOOL_SELECT || tool == TOOL_MOVE || tool == TOOL_SCALE || tool == TOOL_ROTATE || tool == TOOL_EDIT_PIVOT)) { //kind of sucks
  2940. // Draw the pivot
  2941. if (canvas_item->_edit_use_pivot()) {
  2942. // Draw the node's pivot
  2943. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  2944. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  2945. viewport->draw_set_transform_matrix(simple_xform);
  2946. viewport->draw_texture(pivot_icon, -(pivot_icon->get_size() / 2).floor());
  2947. viewport->draw_set_transform_matrix(viewport->get_transform());
  2948. }
  2949. // Draw control-related helpers
  2950. Control *control = Object::cast_to<Control>(canvas_item);
  2951. if (control && _is_node_movable(control)) {
  2952. _draw_control_anchors(control);
  2953. _draw_control_helpers(control);
  2954. }
  2955. // Draw the resize handles
  2956. if (tool == TOOL_SELECT && canvas_item->_edit_use_rect() && _is_node_movable(canvas_item)) {
  2957. Rect2 rect = canvas_item->_edit_get_rect();
  2958. const Vector2 endpoints[4] = {
  2959. xform.xform(rect.position),
  2960. xform.xform(rect.position + Vector2(rect.size.x, 0)),
  2961. xform.xform(rect.position + rect.size),
  2962. xform.xform(rect.position + Vector2(0, rect.size.y))
  2963. };
  2964. for (int i = 0; i < 4; i++) {
  2965. int prev = (i + 3) % 4;
  2966. int next = (i + 1) % 4;
  2967. Vector2 ofs = ((endpoints[i] - endpoints[prev]).normalized() + ((endpoints[i] - endpoints[next]).normalized())).normalized();
  2968. ofs *= Math_SQRT2 * (select_handle->get_size().width / 2);
  2969. select_handle->draw(ci, (endpoints[i] + ofs - (select_handle->get_size() / 2)).floor());
  2970. ofs = (endpoints[i] + endpoints[next]) / 2;
  2971. ofs += (endpoints[next] - endpoints[i]).orthogonal().normalized() * (select_handle->get_size().width / 2);
  2972. select_handle->draw(ci, (ofs - (select_handle->get_size() / 2)).floor());
  2973. }
  2974. }
  2975. // Draw the move handles
  2976. bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  2977. bool is_alt = Input::get_singleton()->is_key_pressed(Key::ALT);
  2978. if (tool == TOOL_MOVE && show_transformation_gizmos) {
  2979. if (_is_node_movable(canvas_item)) {
  2980. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  2981. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  2982. Size2 move_factor = Size2(MOVE_HANDLE_DISTANCE, MOVE_HANDLE_DISTANCE);
  2983. viewport->draw_set_transform_matrix(simple_xform);
  2984. Vector<Point2> points = {
  2985. Vector2(move_factor.x * EDSCALE, 5 * EDSCALE),
  2986. Vector2(move_factor.x * EDSCALE, -5 * EDSCALE),
  2987. Vector2((move_factor.x + 10) * EDSCALE, 0)
  2988. };
  2989. viewport->draw_colored_polygon(points, get_theme_color(SNAME("axis_x_color"), SNAME("Editor")));
  2990. viewport->draw_line(Point2(), Point2(move_factor.x * EDSCALE, 0), get_theme_color(SNAME("axis_x_color"), SNAME("Editor")), Math::round(EDSCALE));
  2991. points.clear();
  2992. points.push_back(Vector2(5 * EDSCALE, move_factor.y * EDSCALE));
  2993. points.push_back(Vector2(-5 * EDSCALE, move_factor.y * EDSCALE));
  2994. points.push_back(Vector2(0, (move_factor.y + 10) * EDSCALE));
  2995. viewport->draw_colored_polygon(points, get_theme_color(SNAME("axis_y_color"), SNAME("Editor")));
  2996. viewport->draw_line(Point2(), Point2(0, move_factor.y * EDSCALE), get_theme_color(SNAME("axis_y_color"), SNAME("Editor")), Math::round(EDSCALE));
  2997. viewport->draw_set_transform_matrix(viewport->get_transform());
  2998. }
  2999. }
  3000. // Draw the rescale handles
  3001. if (show_transformation_gizmos && ((is_alt && is_ctrl) || tool == TOOL_SCALE || drag_type == DRAG_SCALE_X || drag_type == DRAG_SCALE_Y)) {
  3002. if (_is_node_movable(canvas_item)) {
  3003. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  3004. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  3005. Size2 scale_factor = Size2(SCALE_HANDLE_DISTANCE, SCALE_HANDLE_DISTANCE);
  3006. bool uniform = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  3007. Point2 offset = (simple_xform.affine_inverse().xform(drag_to) - simple_xform.affine_inverse().xform(drag_from)) * zoom;
  3008. if (drag_type == DRAG_SCALE_X) {
  3009. scale_factor.x += offset.x;
  3010. if (uniform) {
  3011. scale_factor.y += offset.x;
  3012. }
  3013. } else if (drag_type == DRAG_SCALE_Y) {
  3014. scale_factor.y += offset.y;
  3015. if (uniform) {
  3016. scale_factor.x += offset.y;
  3017. }
  3018. }
  3019. viewport->draw_set_transform_matrix(simple_xform);
  3020. Rect2 x_handle_rect = Rect2(scale_factor.x * EDSCALE, -5 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  3021. viewport->draw_rect(x_handle_rect, get_theme_color(SNAME("axis_x_color"), SNAME("Editor")));
  3022. viewport->draw_line(Point2(), Point2(scale_factor.x * EDSCALE, 0), get_theme_color(SNAME("axis_x_color"), SNAME("Editor")), Math::round(EDSCALE));
  3023. Rect2 y_handle_rect = Rect2(-5 * EDSCALE, scale_factor.y * EDSCALE, 10 * EDSCALE, 10 * EDSCALE);
  3024. viewport->draw_rect(y_handle_rect, get_theme_color(SNAME("axis_y_color"), SNAME("Editor")));
  3025. viewport->draw_line(Point2(), Point2(0, scale_factor.y * EDSCALE), get_theme_color(SNAME("axis_y_color"), SNAME("Editor")), Math::round(EDSCALE));
  3026. viewport->draw_set_transform_matrix(viewport->get_transform());
  3027. }
  3028. }
  3029. }
  3030. }
  3031. if (drag_type == DRAG_BOX_SELECTION) {
  3032. // Draw the dragging box
  3033. Point2 bsfrom = transform.xform(drag_from);
  3034. Point2 bsto = transform.xform(box_selecting_to);
  3035. viewport->draw_rect(
  3036. Rect2(bsfrom, bsto - bsfrom),
  3037. get_theme_color(SNAME("box_selection_fill_color"), SNAME("Editor")));
  3038. viewport->draw_rect(
  3039. Rect2(bsfrom, bsto - bsfrom),
  3040. get_theme_color(SNAME("box_selection_stroke_color"), SNAME("Editor")),
  3041. false,
  3042. Math::round(EDSCALE));
  3043. }
  3044. if (drag_type == DRAG_ROTATE) {
  3045. // Draw the line when rotating a node
  3046. viewport->draw_line(
  3047. transform.xform(drag_rotation_center),
  3048. transform.xform(drag_to),
  3049. get_theme_color(SNAME("accent_color"), SNAME("Editor")) * Color(1, 1, 1, 0.6),
  3050. Math::round(2 * EDSCALE));
  3051. }
  3052. }
  3053. void CanvasItemEditor::_draw_straight_line(Point2 p_from, Point2 p_to, Color p_color) {
  3054. // Draw a line going through the whole screen from a vector
  3055. RID ci = viewport->get_canvas_item();
  3056. Vector<Point2> points;
  3057. Point2 from = transform.xform(p_from);
  3058. Point2 to = transform.xform(p_to);
  3059. Size2 viewport_size = viewport->get_size();
  3060. if (to.x == from.x) {
  3061. // Vertical line
  3062. points.push_back(Point2(to.x, 0));
  3063. points.push_back(Point2(to.x, viewport_size.y));
  3064. } else if (to.y == from.y) {
  3065. // Horizontal line
  3066. points.push_back(Point2(0, to.y));
  3067. points.push_back(Point2(viewport_size.x, to.y));
  3068. } else {
  3069. real_t y_for_zero_x = (to.y * from.x - from.y * to.x) / (from.x - to.x);
  3070. real_t x_for_zero_y = (to.x * from.y - from.x * to.y) / (from.y - to.y);
  3071. real_t y_for_viewport_x = ((to.y - from.y) * (viewport_size.x - from.x)) / (to.x - from.x) + from.y;
  3072. real_t x_for_viewport_y = ((to.x - from.x) * (viewport_size.y - from.y)) / (to.y - from.y) + from.x; // faux
  3073. //bool start_set = false;
  3074. if (y_for_zero_x >= 0 && y_for_zero_x <= viewport_size.y) {
  3075. points.push_back(Point2(0, y_for_zero_x));
  3076. }
  3077. if (x_for_zero_y >= 0 && x_for_zero_y <= viewport_size.x) {
  3078. points.push_back(Point2(x_for_zero_y, 0));
  3079. }
  3080. if (y_for_viewport_x >= 0 && y_for_viewport_x <= viewport_size.y) {
  3081. points.push_back(Point2(viewport_size.x, y_for_viewport_x));
  3082. }
  3083. if (x_for_viewport_y >= 0 && x_for_viewport_y <= viewport_size.x) {
  3084. points.push_back(Point2(x_for_viewport_y, viewport_size.y));
  3085. }
  3086. }
  3087. if (points.size() >= 2) {
  3088. RenderingServer::get_singleton()->canvas_item_add_line(ci, points[0], points[1], p_color);
  3089. }
  3090. }
  3091. void CanvasItemEditor::_draw_axis() {
  3092. if (show_origin) {
  3093. _draw_straight_line(Point2(), Point2(1, 0), get_theme_color(SNAME("axis_x_color"), SNAME("Editor")) * Color(1, 1, 1, 0.75));
  3094. _draw_straight_line(Point2(), Point2(0, 1), get_theme_color(SNAME("axis_y_color"), SNAME("Editor")) * Color(1, 1, 1, 0.75));
  3095. }
  3096. if (show_viewport) {
  3097. RID ci = viewport->get_canvas_item();
  3098. Color area_axis_color = EditorSettings::get_singleton()->get("editors/2d/viewport_border_color");
  3099. Size2 screen_size = Size2(ProjectSettings::get_singleton()->get("display/window/size/viewport_width"), ProjectSettings::get_singleton()->get("display/window/size/viewport_height"));
  3100. Vector2 screen_endpoints[4] = {
  3101. transform.xform(Vector2(0, 0)),
  3102. transform.xform(Vector2(screen_size.width, 0)),
  3103. transform.xform(Vector2(screen_size.width, screen_size.height)),
  3104. transform.xform(Vector2(0, screen_size.height))
  3105. };
  3106. for (int i = 0; i < 4; i++) {
  3107. RenderingServer::get_singleton()->canvas_item_add_line(ci, screen_endpoints[i], screen_endpoints[(i + 1) % 4], area_axis_color);
  3108. }
  3109. }
  3110. }
  3111. void CanvasItemEditor::_draw_invisible_nodes_positions(Node *p_node, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
  3112. ERR_FAIL_COND(!p_node);
  3113. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  3114. if (p_node != scene && p_node->get_owner() != scene && !scene->is_editable_instance(p_node->get_owner())) {
  3115. return;
  3116. }
  3117. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node);
  3118. if (canvas_item && !canvas_item->is_visible_in_tree()) {
  3119. return;
  3120. }
  3121. Transform2D parent_xform = p_parent_xform;
  3122. Transform2D canvas_xform = p_canvas_xform;
  3123. if (canvas_item && !canvas_item->is_set_as_top_level()) {
  3124. parent_xform = parent_xform * canvas_item->get_transform();
  3125. } else {
  3126. CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
  3127. parent_xform = Transform2D();
  3128. canvas_xform = cl ? cl->get_transform() : p_canvas_xform;
  3129. }
  3130. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  3131. _draw_invisible_nodes_positions(p_node->get_child(i), parent_xform, canvas_xform);
  3132. }
  3133. if (canvas_item && !canvas_item->_edit_use_rect() && (!editor_selection->is_selected(canvas_item) || _is_node_locked(canvas_item))) {
  3134. Transform2D xform = transform * canvas_xform * parent_xform;
  3135. // Draw the node's position
  3136. Ref<Texture2D> position_icon = get_theme_icon(SNAME("EditorPositionUnselected"), SNAME("EditorIcons"));
  3137. Transform2D unscaled_transform = (xform * canvas_item->get_transform().affine_inverse() * canvas_item->_edit_get_transform()).orthonormalized();
  3138. Transform2D simple_xform = viewport->get_transform() * unscaled_transform;
  3139. viewport->draw_set_transform_matrix(simple_xform);
  3140. viewport->draw_texture(position_icon, -position_icon->get_size() / 2, Color(1.0, 1.0, 1.0, 0.5));
  3141. viewport->draw_set_transform_matrix(viewport->get_transform());
  3142. }
  3143. }
  3144. void CanvasItemEditor::_draw_hover() {
  3145. List<Rect2> previous_rects;
  3146. for (int i = 0; i < hovering_results.size(); i++) {
  3147. Ref<Texture2D> node_icon = hovering_results[i].icon;
  3148. String node_name = hovering_results[i].name;
  3149. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  3150. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  3151. Size2 node_name_size = font->get_string_size(node_name, font_size);
  3152. Size2 item_size = Size2(node_icon->get_size().x + 4 + node_name_size.x, MAX(node_icon->get_size().y, node_name_size.y - 3));
  3153. Point2 pos = transform.xform(hovering_results[i].position) - Point2(0, item_size.y) + (Point2(node_icon->get_size().x, -node_icon->get_size().y) / 4);
  3154. // Rectify the position to avoid overlapping items
  3155. for (const Rect2 &E : previous_rects) {
  3156. if (E.intersects(Rect2(pos, item_size))) {
  3157. pos.y = E.get_position().y - item_size.y;
  3158. }
  3159. }
  3160. previous_rects.push_back(Rect2(pos, item_size));
  3161. // Draw icon
  3162. viewport->draw_texture(node_icon, pos, Color(1.0, 1.0, 1.0, 0.5));
  3163. // Draw name
  3164. viewport->draw_string(font, pos + Point2(node_icon->get_size().x + 4, item_size.y - 3), node_name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1.0, 1.0, 1.0, 0.5));
  3165. }
  3166. }
  3167. void CanvasItemEditor::_draw_locks_and_groups(Node *p_node, const Transform2D &p_parent_xform, const Transform2D &p_canvas_xform) {
  3168. ERR_FAIL_COND(!p_node);
  3169. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  3170. if (p_node != scene && p_node->get_owner() != scene && !scene->is_editable_instance(p_node->get_owner())) {
  3171. return;
  3172. }
  3173. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(p_node);
  3174. if (canvas_item && !canvas_item->is_visible_in_tree()) {
  3175. return;
  3176. }
  3177. Transform2D parent_xform = p_parent_xform;
  3178. Transform2D canvas_xform = p_canvas_xform;
  3179. if (canvas_item && !canvas_item->is_set_as_top_level()) {
  3180. parent_xform = parent_xform * canvas_item->get_transform();
  3181. } else {
  3182. CanvasLayer *cl = Object::cast_to<CanvasLayer>(p_node);
  3183. parent_xform = Transform2D();
  3184. canvas_xform = cl ? cl->get_transform() : p_canvas_xform;
  3185. }
  3186. for (int i = p_node->get_child_count() - 1; i >= 0; i--) {
  3187. _draw_locks_and_groups(p_node->get_child(i), parent_xform, canvas_xform);
  3188. }
  3189. RID viewport_canvas_item = viewport->get_canvas_item();
  3190. if (canvas_item) {
  3191. real_t offset = 0;
  3192. Ref<Texture2D> lock = get_theme_icon(SNAME("LockViewport"), SNAME("EditorIcons"));
  3193. if (p_node->has_meta("_edit_lock_") && show_edit_locks) {
  3194. lock->draw(viewport_canvas_item, (transform * canvas_xform * parent_xform).xform(Point2(0, 0)) + Point2(offset, 0));
  3195. offset += lock->get_size().x;
  3196. }
  3197. Ref<Texture2D> group = get_theme_icon(SNAME("GroupViewport"), SNAME("EditorIcons"));
  3198. if (canvas_item->has_meta("_edit_group_") && show_edit_locks) {
  3199. group->draw(viewport_canvas_item, (transform * canvas_xform * parent_xform).xform(Point2(0, 0)) + Point2(offset, 0));
  3200. //offset += group->get_size().x;
  3201. }
  3202. }
  3203. }
  3204. void CanvasItemEditor::_draw_viewport() {
  3205. // Update the transform
  3206. transform = Transform2D();
  3207. transform.scale_basis(Size2(zoom, zoom));
  3208. transform.columns[2] = -view_offset * zoom;
  3209. EditorNode::get_singleton()->get_scene_root()->set_global_canvas_transform(transform);
  3210. // hide/show buttons depending on the selection
  3211. bool all_locked = true;
  3212. bool all_group = true;
  3213. List<Node *> selection = editor_selection->get_selected_node_list();
  3214. if (selection.is_empty()) {
  3215. all_locked = false;
  3216. all_group = false;
  3217. } else {
  3218. for (Node *E : selection) {
  3219. if (Object::cast_to<CanvasItem>(E) && !Object::cast_to<CanvasItem>(E)->has_meta("_edit_lock_")) {
  3220. all_locked = false;
  3221. break;
  3222. }
  3223. }
  3224. for (Node *E : selection) {
  3225. if (Object::cast_to<CanvasItem>(E) && !Object::cast_to<CanvasItem>(E)->has_meta("_edit_group_")) {
  3226. all_group = false;
  3227. break;
  3228. }
  3229. }
  3230. }
  3231. lock_button->set_visible(!all_locked);
  3232. lock_button->set_disabled(selection.is_empty());
  3233. unlock_button->set_visible(all_locked);
  3234. group_button->set_visible(!all_group);
  3235. group_button->set_disabled(selection.is_empty());
  3236. ungroup_button->set_visible(all_group);
  3237. _draw_grid();
  3238. _draw_ruler_tool();
  3239. _draw_axis();
  3240. if (EditorNode::get_singleton()->get_edited_scene()) {
  3241. _draw_locks_and_groups(EditorNode::get_singleton()->get_edited_scene());
  3242. _draw_invisible_nodes_positions(EditorNode::get_singleton()->get_edited_scene());
  3243. }
  3244. _draw_selection();
  3245. RID ci = viewport->get_canvas_item();
  3246. RenderingServer::get_singleton()->canvas_item_add_set_transform(ci, Transform2D());
  3247. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  3248. if (!over_plugin_list->is_empty()) {
  3249. over_plugin_list->forward_canvas_draw_over_viewport(viewport);
  3250. }
  3251. EditorPluginList *force_over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_force_over();
  3252. if (!force_over_plugin_list->is_empty()) {
  3253. force_over_plugin_list->forward_canvas_force_draw_over_viewport(viewport);
  3254. }
  3255. if (show_rulers) {
  3256. _draw_rulers();
  3257. }
  3258. if (show_guides) {
  3259. _draw_guides();
  3260. }
  3261. _draw_smart_snapping();
  3262. _draw_focus();
  3263. _draw_hover();
  3264. }
  3265. void CanvasItemEditor::update_viewport() {
  3266. _update_scrollbars();
  3267. viewport->update();
  3268. }
  3269. void CanvasItemEditor::set_current_tool(Tool p_tool) {
  3270. _button_tool_select(p_tool);
  3271. }
  3272. void CanvasItemEditor::_update_editor_settings() {
  3273. select_button->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  3274. list_select_button->set_icon(get_theme_icon(SNAME("ListSelect"), SNAME("EditorIcons")));
  3275. move_button->set_icon(get_theme_icon(SNAME("ToolMove"), SNAME("EditorIcons")));
  3276. scale_button->set_icon(get_theme_icon(SNAME("ToolScale"), SNAME("EditorIcons")));
  3277. rotate_button->set_icon(get_theme_icon(SNAME("ToolRotate"), SNAME("EditorIcons")));
  3278. smart_snap_button->set_icon(get_theme_icon(SNAME("Snap"), SNAME("EditorIcons")));
  3279. grid_snap_button->set_icon(get_theme_icon(SNAME("SnapGrid"), SNAME("EditorIcons")));
  3280. snap_config_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  3281. skeleton_menu->set_icon(get_theme_icon(SNAME("Bone"), SNAME("EditorIcons")));
  3282. override_camera_button->set_icon(get_theme_icon(SNAME("Camera2D"), SNAME("EditorIcons")));
  3283. pan_button->set_icon(get_theme_icon(SNAME("ToolPan"), SNAME("EditorIcons")));
  3284. ruler_button->set_icon(get_theme_icon(SNAME("Ruler"), SNAME("EditorIcons")));
  3285. pivot_button->set_icon(get_theme_icon(SNAME("EditPivot"), SNAME("EditorIcons")));
  3286. select_handle = get_theme_icon(SNAME("EditorHandle"), SNAME("EditorIcons"));
  3287. anchor_handle = get_theme_icon(SNAME("EditorControlAnchor"), SNAME("EditorIcons"));
  3288. lock_button->set_icon(get_theme_icon(SNAME("Lock"), SNAME("EditorIcons")));
  3289. unlock_button->set_icon(get_theme_icon(SNAME("Unlock"), SNAME("EditorIcons")));
  3290. group_button->set_icon(get_theme_icon(SNAME("Group"), SNAME("EditorIcons")));
  3291. ungroup_button->set_icon(get_theme_icon(SNAME("Ungroup"), SNAME("EditorIcons")));
  3292. key_loc_button->set_icon(get_theme_icon(SNAME("KeyPosition"), SNAME("EditorIcons")));
  3293. key_rot_button->set_icon(get_theme_icon(SNAME("KeyRotation"), SNAME("EditorIcons")));
  3294. key_scale_button->set_icon(get_theme_icon(SNAME("KeyScale"), SNAME("EditorIcons")));
  3295. key_insert_button->set_icon(get_theme_icon(SNAME("Key"), SNAME("EditorIcons")));
  3296. key_auto_insert_button->set_icon(get_theme_icon(SNAME("AutoKey"), SNAME("EditorIcons")));
  3297. // Use a different color for the active autokey icon to make them easier
  3298. // to distinguish from the other key icons at the top. On a light theme,
  3299. // the icon will be dark, so we need to lighten it before blending it
  3300. // with the red color.
  3301. const Color key_auto_color = EditorSettings::get_singleton()->is_dark_theme() ? Color(1, 1, 1) : Color(4.25, 4.25, 4.25);
  3302. key_auto_insert_button->add_theme_color_override("icon_pressed_color", key_auto_color.lerp(Color(1, 0, 0), 0.55));
  3303. animation_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  3304. _update_context_menu_stylebox();
  3305. panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/2d_editor_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EditorSettings::get_singleton()->get("editors/panning/simple_panning")));
  3306. pan_speed = int(EditorSettings::get_singleton()->get("editors/panning/2d_editor_pan_speed"));
  3307. warped_panning = bool(EditorSettings::get_singleton()->get("editors/panning/warped_mouse_panning"));
  3308. }
  3309. void CanvasItemEditor::_notification(int p_what) {
  3310. switch (p_what) {
  3311. case NOTIFICATION_PHYSICS_PROCESS: {
  3312. EditorNode::get_singleton()->get_scene_root()->set_snap_controls_to_pixels(GLOBAL_GET("gui/common/snap_controls_to_pixels"));
  3313. int nb_having_pivot = 0;
  3314. // Update the viewport if the canvas_item changes
  3315. List<CanvasItem *> selection = _get_edited_canvas_items(true);
  3316. for (CanvasItem *canvas_item : selection) {
  3317. CanvasItemEditorSelectedItem *se = editor_selection->get_node_editor_data<CanvasItemEditorSelectedItem>(canvas_item);
  3318. Rect2 rect;
  3319. if (canvas_item->_edit_use_rect()) {
  3320. rect = canvas_item->_edit_get_rect();
  3321. } else {
  3322. rect = Rect2();
  3323. }
  3324. Transform2D xform = canvas_item->get_transform();
  3325. if (rect != se->prev_rect || xform != se->prev_xform) {
  3326. viewport->update();
  3327. se->prev_rect = rect;
  3328. se->prev_xform = xform;
  3329. }
  3330. Control *control = Object::cast_to<Control>(canvas_item);
  3331. if (control) {
  3332. real_t anchors[4];
  3333. Vector2 pivot;
  3334. pivot = control->get_pivot_offset();
  3335. anchors[SIDE_LEFT] = control->get_anchor(SIDE_LEFT);
  3336. anchors[SIDE_RIGHT] = control->get_anchor(SIDE_RIGHT);
  3337. anchors[SIDE_TOP] = control->get_anchor(SIDE_TOP);
  3338. anchors[SIDE_BOTTOM] = control->get_anchor(SIDE_BOTTOM);
  3339. if (pivot != se->prev_pivot || anchors[SIDE_LEFT] != se->prev_anchors[SIDE_LEFT] || anchors[SIDE_RIGHT] != se->prev_anchors[SIDE_RIGHT] || anchors[SIDE_TOP] != se->prev_anchors[SIDE_TOP] || anchors[SIDE_BOTTOM] != se->prev_anchors[SIDE_BOTTOM]) {
  3340. se->prev_pivot = pivot;
  3341. se->prev_anchors[SIDE_LEFT] = anchors[SIDE_LEFT];
  3342. se->prev_anchors[SIDE_RIGHT] = anchors[SIDE_RIGHT];
  3343. se->prev_anchors[SIDE_TOP] = anchors[SIDE_TOP];
  3344. se->prev_anchors[SIDE_BOTTOM] = anchors[SIDE_BOTTOM];
  3345. viewport->update();
  3346. }
  3347. }
  3348. if (canvas_item->_edit_use_pivot()) {
  3349. nb_having_pivot++;
  3350. }
  3351. }
  3352. // Activate / Deactivate the pivot tool
  3353. pivot_button->set_disabled(nb_having_pivot == 0);
  3354. // Update the viewport if bones changes
  3355. for (KeyValue<BoneKey, BoneList> &E : bone_list) {
  3356. Object *b = ObjectDB::get_instance(E.key.from);
  3357. if (!b) {
  3358. viewport->update();
  3359. break;
  3360. }
  3361. Node2D *b2 = Object::cast_to<Node2D>(b);
  3362. if (!b2 || !b2->is_inside_tree()) {
  3363. continue;
  3364. }
  3365. Transform2D global_xform = b2->get_global_transform();
  3366. if (global_xform != E.value.xform) {
  3367. E.value.xform = global_xform;
  3368. viewport->update();
  3369. }
  3370. Bone2D *bone = Object::cast_to<Bone2D>(b);
  3371. if (bone && bone->get_length() != E.value.length) {
  3372. E.value.length = bone->get_length();
  3373. viewport->update();
  3374. }
  3375. }
  3376. } break;
  3377. case NOTIFICATION_ENTER_TREE: {
  3378. select_sb->set_texture(get_theme_icon(SNAME("EditorRect2D"), SNAME("EditorIcons")));
  3379. for (int i = 0; i < 4; i++) {
  3380. select_sb->set_margin_size(Side(i), 4);
  3381. select_sb->set_default_margin(Side(i), 4);
  3382. }
  3383. AnimationPlayerEditor::get_singleton()->get_track_editor()->connect("visibility_changed", callable_mp(this, &CanvasItemEditor::_keying_changed));
  3384. _keying_changed();
  3385. _update_editor_settings();
  3386. } break;
  3387. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  3388. select_sb->set_texture(get_theme_icon(SNAME("EditorRect2D"), SNAME("EditorIcons")));
  3389. _update_editor_settings();
  3390. } break;
  3391. case NOTIFICATION_VISIBILITY_CHANGED: {
  3392. if (!is_visible() && override_camera_button->is_pressed()) {
  3393. EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
  3394. debugger->set_camera_override(EditorDebuggerNode::OVERRIDE_NONE);
  3395. override_camera_button->set_pressed(false);
  3396. }
  3397. } break;
  3398. }
  3399. }
  3400. void CanvasItemEditor::_selection_changed() {
  3401. if (!selected_from_canvas) {
  3402. drag_type = DRAG_NONE;
  3403. }
  3404. selected_from_canvas = false;
  3405. }
  3406. void CanvasItemEditor::edit(CanvasItem *p_canvas_item) {
  3407. Array selection = editor_selection->get_selected_nodes();
  3408. if (selection.size() != 1 || Object::cast_to<Node>(selection[0]) != p_canvas_item) {
  3409. drag_type = DRAG_NONE;
  3410. // Clear the selection
  3411. editor_selection->clear(); //_clear_canvas_items();
  3412. editor_selection->add_node(p_canvas_item);
  3413. }
  3414. }
  3415. void CanvasItemEditor::_update_context_menu_stylebox() {
  3416. // This must be called when the theme changes to follow the new accent color.
  3417. Ref<StyleBoxFlat> context_menu_stylebox = memnew(StyleBoxFlat);
  3418. const Color accent_color = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  3419. context_menu_stylebox->set_bg_color(accent_color * Color(1, 1, 1, 0.1));
  3420. // Add an underline to the StyleBox, but prevent its minimum vertical size from changing.
  3421. context_menu_stylebox->set_border_color(accent_color);
  3422. context_menu_stylebox->set_border_width(SIDE_BOTTOM, Math::round(2 * EDSCALE));
  3423. context_menu_stylebox->set_default_margin(SIDE_BOTTOM, 0);
  3424. context_menu_container->add_theme_style_override("panel", context_menu_stylebox);
  3425. }
  3426. void CanvasItemEditor::_update_scrollbars() {
  3427. updating_scroll = true;
  3428. // Move the zoom buttons.
  3429. Point2 controls_vb_begin = Point2(5, 5);
  3430. controls_vb_begin += (show_rulers) ? Point2(RULER_WIDTH, RULER_WIDTH) : Point2();
  3431. controls_vb->set_begin(controls_vb_begin);
  3432. Size2 hmin = h_scroll->get_minimum_size();
  3433. Size2 vmin = v_scroll->get_minimum_size();
  3434. // Get the visible frame.
  3435. Size2 screen_rect = Size2(ProjectSettings::get_singleton()->get("display/window/size/viewport_width"), ProjectSettings::get_singleton()->get("display/window/size/viewport_height"));
  3436. Rect2 local_rect = Rect2(Point2(), viewport->get_size() - Size2(vmin.width, hmin.height));
  3437. // Calculate scrollable area.
  3438. Rect2 canvas_item_rect = Rect2(Point2(), screen_rect);
  3439. if (EditorNode::get_singleton()->is_inside_tree() && EditorNode::get_singleton()->get_edited_scene()) {
  3440. Rect2 content_rect = _get_encompassing_rect(EditorNode::get_singleton()->get_edited_scene());
  3441. canvas_item_rect.expand_to(content_rect.position);
  3442. canvas_item_rect.expand_to(content_rect.position + content_rect.size);
  3443. }
  3444. canvas_item_rect.size += screen_rect * 2;
  3445. canvas_item_rect.position -= screen_rect;
  3446. // Constraints the view offset and updates the scrollbars.
  3447. Size2 size = viewport->get_size();
  3448. Point2 begin = canvas_item_rect.position;
  3449. Point2 end = canvas_item_rect.position + canvas_item_rect.size - local_rect.size / zoom;
  3450. bool constrain_editor_view = bool(EditorSettings::get_singleton()->get("editors/2d/constrain_editor_view"));
  3451. if (canvas_item_rect.size.height <= (local_rect.size.y / zoom)) {
  3452. real_t centered = -(size.y / 2) / zoom + screen_rect.y / 2;
  3453. if (constrain_editor_view && ABS(centered - previous_update_view_offset.y) < ABS(centered - view_offset.y)) {
  3454. view_offset.y = previous_update_view_offset.y;
  3455. }
  3456. v_scroll->hide();
  3457. } else {
  3458. if (constrain_editor_view && view_offset.y > end.y && view_offset.y > previous_update_view_offset.y) {
  3459. view_offset.y = MAX(end.y, previous_update_view_offset.y);
  3460. }
  3461. if (constrain_editor_view && view_offset.y < begin.y && view_offset.y < previous_update_view_offset.y) {
  3462. view_offset.y = MIN(begin.y, previous_update_view_offset.y);
  3463. }
  3464. v_scroll->show();
  3465. v_scroll->set_min(MIN(view_offset.y, begin.y));
  3466. v_scroll->set_max(MAX(view_offset.y, end.y) + screen_rect.y);
  3467. v_scroll->set_page(screen_rect.y);
  3468. }
  3469. if (canvas_item_rect.size.width <= (local_rect.size.x / zoom)) {
  3470. real_t centered = -(size.x / 2) / zoom + screen_rect.x / 2;
  3471. if (constrain_editor_view && ABS(centered - previous_update_view_offset.x) < ABS(centered - view_offset.x)) {
  3472. view_offset.x = previous_update_view_offset.x;
  3473. }
  3474. h_scroll->hide();
  3475. } else {
  3476. if (constrain_editor_view && view_offset.x > end.x && view_offset.x > previous_update_view_offset.x) {
  3477. view_offset.x = MAX(end.x, previous_update_view_offset.x);
  3478. }
  3479. if (constrain_editor_view && view_offset.x < begin.x && view_offset.x < previous_update_view_offset.x) {
  3480. view_offset.x = MIN(begin.x, previous_update_view_offset.x);
  3481. }
  3482. h_scroll->show();
  3483. h_scroll->set_min(MIN(view_offset.x, begin.x));
  3484. h_scroll->set_max(MAX(view_offset.x, end.x) + screen_rect.x);
  3485. h_scroll->set_page(screen_rect.x);
  3486. }
  3487. // Move and resize the scrollbars, avoiding overlap.
  3488. if (is_layout_rtl()) {
  3489. v_scroll->set_begin(Point2(0, (show_rulers) ? RULER_WIDTH : 0));
  3490. v_scroll->set_end(Point2(vmin.width, size.height - (h_scroll->is_visible() ? hmin.height : 0)));
  3491. } else {
  3492. v_scroll->set_begin(Point2(size.width - vmin.width, (show_rulers) ? RULER_WIDTH : 0));
  3493. v_scroll->set_end(Point2(size.width, size.height - (h_scroll->is_visible() ? hmin.height : 0)));
  3494. }
  3495. h_scroll->set_begin(Point2((show_rulers) ? RULER_WIDTH : 0, size.height - hmin.height));
  3496. h_scroll->set_end(Point2(size.width - (v_scroll->is_visible() ? vmin.width : 0), size.height));
  3497. // Calculate scrollable area.
  3498. v_scroll->set_value(view_offset.y);
  3499. h_scroll->set_value(view_offset.x);
  3500. previous_update_view_offset = view_offset;
  3501. updating_scroll = false;
  3502. }
  3503. void CanvasItemEditor::_update_scroll(real_t) {
  3504. if (updating_scroll) {
  3505. return;
  3506. }
  3507. view_offset.x = h_scroll->get_value();
  3508. view_offset.y = v_scroll->get_value();
  3509. viewport->update();
  3510. }
  3511. void CanvasItemEditor::_zoom_on_position(real_t p_zoom, Point2 p_position) {
  3512. p_zoom = CLAMP(p_zoom, MIN_ZOOM, MAX_ZOOM);
  3513. if (p_zoom == zoom) {
  3514. zoom_widget->set_zoom(p_zoom);
  3515. return;
  3516. }
  3517. real_t prev_zoom = zoom;
  3518. zoom = p_zoom;
  3519. view_offset += p_position / prev_zoom - p_position / zoom;
  3520. // We want to align in-scene pixels to screen pixels, this prevents blurry rendering
  3521. // in small details (texts, lines).
  3522. // This correction adds a jitter movement when zooming, so we correct only when the
  3523. // zoom factor is an integer. (in the other cases, all pixels won't be aligned anyway)
  3524. const real_t closest_zoom_factor = Math::round(zoom);
  3525. if (Math::is_zero_approx(zoom - closest_zoom_factor)) {
  3526. // make sure scene pixel at view_offset is aligned on a screen pixel
  3527. Vector2 view_offset_int = view_offset.floor();
  3528. Vector2 view_offset_frac = view_offset - view_offset_int;
  3529. view_offset = view_offset_int + (view_offset_frac * closest_zoom_factor).round() / closest_zoom_factor;
  3530. }
  3531. zoom_widget->set_zoom(zoom);
  3532. update_viewport();
  3533. }
  3534. void CanvasItemEditor::_update_zoom(real_t p_zoom) {
  3535. _zoom_on_position(p_zoom, viewport_scrollable->get_size() / 2.0);
  3536. }
  3537. void CanvasItemEditor::_shortcut_zoom_set(real_t p_zoom) {
  3538. _zoom_on_position(p_zoom * MAX(1, EDSCALE), viewport->get_local_mouse_position());
  3539. }
  3540. void CanvasItemEditor::_button_toggle_smart_snap(bool p_status) {
  3541. smart_snap_active = p_status;
  3542. viewport->update();
  3543. }
  3544. void CanvasItemEditor::_button_toggle_grid_snap(bool p_status) {
  3545. grid_snap_active = p_status;
  3546. viewport->update();
  3547. }
  3548. void CanvasItemEditor::_button_override_camera(bool p_pressed) {
  3549. EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
  3550. if (p_pressed) {
  3551. debugger->set_camera_override(EditorDebuggerNode::OVERRIDE_2D);
  3552. } else {
  3553. debugger->set_camera_override(EditorDebuggerNode::OVERRIDE_NONE);
  3554. }
  3555. }
  3556. void CanvasItemEditor::_button_tool_select(int p_index) {
  3557. Button *tb[TOOL_MAX] = { select_button, list_select_button, move_button, scale_button, rotate_button, pivot_button, pan_button, ruler_button };
  3558. for (int i = 0; i < TOOL_MAX; i++) {
  3559. tb[i]->set_pressed(i == p_index);
  3560. }
  3561. tool = (Tool)p_index;
  3562. viewport->update();
  3563. _update_cursor();
  3564. }
  3565. void CanvasItemEditor::_insert_animation_keys(bool p_location, bool p_rotation, bool p_scale, bool p_on_existing) {
  3566. const Map<Node *, Object *> &selection = editor_selection->get_selection();
  3567. for (const KeyValue<Node *, Object *> &E : selection) {
  3568. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E.key);
  3569. if (!canvas_item || !canvas_item->is_visible_in_tree()) {
  3570. continue;
  3571. }
  3572. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3573. continue;
  3574. }
  3575. if (Object::cast_to<Node2D>(canvas_item)) {
  3576. Node2D *n2d = Object::cast_to<Node2D>(canvas_item);
  3577. if (key_pos && p_location) {
  3578. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(n2d, "position", n2d->get_position(), p_on_existing);
  3579. }
  3580. if (key_rot && p_rotation) {
  3581. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(n2d, "rotation", n2d->get_rotation(), p_on_existing);
  3582. }
  3583. if (key_scale && p_scale) {
  3584. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(n2d, "scale", n2d->get_scale(), p_on_existing);
  3585. }
  3586. if (n2d->has_meta("_edit_bone_") && n2d->get_parent_item()) {
  3587. //look for an IK chain
  3588. List<Node2D *> ik_chain;
  3589. Node2D *n = Object::cast_to<Node2D>(n2d->get_parent_item());
  3590. bool has_chain = false;
  3591. while (n) {
  3592. ik_chain.push_back(n);
  3593. if (n->has_meta("_edit_ik_")) {
  3594. has_chain = true;
  3595. break;
  3596. }
  3597. if (!n->get_parent_item()) {
  3598. break;
  3599. }
  3600. n = Object::cast_to<Node2D>(n->get_parent_item());
  3601. }
  3602. if (has_chain && ik_chain.size()) {
  3603. for (Node2D *&F : ik_chain) {
  3604. if (key_pos) {
  3605. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(F, "position", F->get_position(), p_on_existing);
  3606. }
  3607. if (key_rot) {
  3608. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(F, "rotation", F->get_rotation(), p_on_existing);
  3609. }
  3610. if (key_scale) {
  3611. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(F, "scale", F->get_scale(), p_on_existing);
  3612. }
  3613. }
  3614. }
  3615. }
  3616. } else if (Object::cast_to<Control>(canvas_item)) {
  3617. Control *ctrl = Object::cast_to<Control>(canvas_item);
  3618. if (key_pos) {
  3619. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(ctrl, "rect_position", ctrl->get_position(), p_on_existing);
  3620. }
  3621. if (key_rot) {
  3622. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(ctrl, "rect_rotation", ctrl->get_rotation(), p_on_existing);
  3623. }
  3624. if (key_scale) {
  3625. AnimationPlayerEditor::get_singleton()->get_track_editor()->insert_node_value_key(ctrl, "rect_size", ctrl->get_size(), p_on_existing);
  3626. }
  3627. }
  3628. }
  3629. }
  3630. void CanvasItemEditor::_update_override_camera_button(bool p_game_running) {
  3631. if (p_game_running) {
  3632. override_camera_button->set_disabled(false);
  3633. override_camera_button->set_tooltip(TTR("Project Camera Override\nOverrides the running project's camera with the editor viewport camera."));
  3634. } else {
  3635. override_camera_button->set_disabled(true);
  3636. override_camera_button->set_pressed(false);
  3637. override_camera_button->set_tooltip(TTR("Project Camera Override\nNo project instance running. Run the project from the editor to use this feature."));
  3638. }
  3639. }
  3640. void CanvasItemEditor::_popup_callback(int p_op) {
  3641. last_option = MenuOption(p_op);
  3642. switch (p_op) {
  3643. case SHOW_ORIGIN: {
  3644. show_origin = !show_origin;
  3645. int idx = view_menu->get_popup()->get_item_index(SHOW_ORIGIN);
  3646. view_menu->get_popup()->set_item_checked(idx, show_origin);
  3647. viewport->update();
  3648. } break;
  3649. case SHOW_VIEWPORT: {
  3650. show_viewport = !show_viewport;
  3651. int idx = view_menu->get_popup()->get_item_index(SHOW_VIEWPORT);
  3652. view_menu->get_popup()->set_item_checked(idx, show_viewport);
  3653. viewport->update();
  3654. } break;
  3655. case SHOW_EDIT_LOCKS: {
  3656. show_edit_locks = !show_edit_locks;
  3657. int idx = view_menu->get_popup()->get_item_index(SHOW_EDIT_LOCKS);
  3658. view_menu->get_popup()->set_item_checked(idx, show_edit_locks);
  3659. viewport->update();
  3660. } break;
  3661. case SHOW_TRANSFORMATION_GIZMOS: {
  3662. show_transformation_gizmos = !show_transformation_gizmos;
  3663. int idx = view_menu->get_popup()->get_item_index(SHOW_TRANSFORMATION_GIZMOS);
  3664. view_menu->get_popup()->set_item_checked(idx, show_transformation_gizmos);
  3665. viewport->update();
  3666. } break;
  3667. case SNAP_USE_NODE_PARENT: {
  3668. snap_node_parent = !snap_node_parent;
  3669. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT);
  3670. smartsnap_config_popup->set_item_checked(idx, snap_node_parent);
  3671. } break;
  3672. case SNAP_USE_NODE_ANCHORS: {
  3673. snap_node_anchors = !snap_node_anchors;
  3674. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS);
  3675. smartsnap_config_popup->set_item_checked(idx, snap_node_anchors);
  3676. } break;
  3677. case SNAP_USE_NODE_SIDES: {
  3678. snap_node_sides = !snap_node_sides;
  3679. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES);
  3680. smartsnap_config_popup->set_item_checked(idx, snap_node_sides);
  3681. } break;
  3682. case SNAP_USE_NODE_CENTER: {
  3683. snap_node_center = !snap_node_center;
  3684. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_CENTER);
  3685. smartsnap_config_popup->set_item_checked(idx, snap_node_center);
  3686. } break;
  3687. case SNAP_USE_OTHER_NODES: {
  3688. snap_other_nodes = !snap_other_nodes;
  3689. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES);
  3690. smartsnap_config_popup->set_item_checked(idx, snap_other_nodes);
  3691. } break;
  3692. case SNAP_USE_GUIDES: {
  3693. snap_guides = !snap_guides;
  3694. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_GUIDES);
  3695. smartsnap_config_popup->set_item_checked(idx, snap_guides);
  3696. } break;
  3697. case SNAP_USE_ROTATION: {
  3698. snap_rotation = !snap_rotation;
  3699. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION);
  3700. snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation);
  3701. } break;
  3702. case SNAP_USE_SCALE: {
  3703. snap_scale = !snap_scale;
  3704. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_SCALE);
  3705. snap_config_menu->get_popup()->set_item_checked(idx, snap_scale);
  3706. } break;
  3707. case SNAP_RELATIVE: {
  3708. snap_relative = !snap_relative;
  3709. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE);
  3710. snap_config_menu->get_popup()->set_item_checked(idx, snap_relative);
  3711. viewport->update();
  3712. } break;
  3713. case SNAP_USE_PIXEL: {
  3714. snap_pixel = !snap_pixel;
  3715. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL);
  3716. snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel);
  3717. } break;
  3718. case SNAP_CONFIGURE: {
  3719. static_cast<SnapDialog *>(snap_dialog)->set_fields(grid_offset, grid_step, primary_grid_steps, snap_rotation_offset, snap_rotation_step, snap_scale_step);
  3720. snap_dialog->popup_centered(Size2(220, 160) * EDSCALE);
  3721. } break;
  3722. case SKELETON_SHOW_BONES: {
  3723. List<Node *> selection = editor_selection->get_selected_node_list();
  3724. for (Node *E : selection) {
  3725. // Add children nodes so they are processed
  3726. for (int child = 0; child < E->get_child_count(); child++) {
  3727. selection.push_back(E->get_child(child));
  3728. }
  3729. Bone2D *bone_2d = Object::cast_to<Bone2D>(E);
  3730. if (!bone_2d || !bone_2d->is_inside_tree()) {
  3731. continue;
  3732. }
  3733. bone_2d->_editor_set_show_bone_gizmo(!bone_2d->_editor_get_show_bone_gizmo());
  3734. }
  3735. } break;
  3736. case SHOW_HELPERS: {
  3737. show_helpers = !show_helpers;
  3738. int idx = view_menu->get_popup()->get_item_index(SHOW_HELPERS);
  3739. view_menu->get_popup()->set_item_checked(idx, show_helpers);
  3740. viewport->update();
  3741. } break;
  3742. case SHOW_RULERS: {
  3743. show_rulers = !show_rulers;
  3744. int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS);
  3745. view_menu->get_popup()->set_item_checked(idx, show_rulers);
  3746. _update_scrollbars();
  3747. viewport->update();
  3748. } break;
  3749. case SHOW_GUIDES: {
  3750. show_guides = !show_guides;
  3751. int idx = view_menu->get_popup()->get_item_index(SHOW_GUIDES);
  3752. view_menu->get_popup()->set_item_checked(idx, show_guides);
  3753. viewport->update();
  3754. } break;
  3755. case LOCK_SELECTED: {
  3756. undo_redo->create_action(TTR("Lock Selected"));
  3757. List<Node *> selection = editor_selection->get_selected_node_list();
  3758. for (Node *E : selection) {
  3759. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E);
  3760. if (!canvas_item || !canvas_item->is_inside_tree()) {
  3761. continue;
  3762. }
  3763. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3764. continue;
  3765. }
  3766. undo_redo->add_do_method(canvas_item, "set_meta", "_edit_lock_", true);
  3767. undo_redo->add_undo_method(canvas_item, "remove_meta", "_edit_lock_");
  3768. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  3769. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  3770. }
  3771. undo_redo->add_do_method(viewport, "update");
  3772. undo_redo->add_undo_method(viewport, "update");
  3773. undo_redo->commit_action();
  3774. } break;
  3775. case UNLOCK_SELECTED: {
  3776. undo_redo->create_action(TTR("Unlock Selected"));
  3777. List<Node *> selection = editor_selection->get_selected_node_list();
  3778. for (Node *E : selection) {
  3779. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E);
  3780. if (!canvas_item || !canvas_item->is_inside_tree()) {
  3781. continue;
  3782. }
  3783. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3784. continue;
  3785. }
  3786. undo_redo->add_do_method(canvas_item, "remove_meta", "_edit_lock_");
  3787. undo_redo->add_undo_method(canvas_item, "set_meta", "_edit_lock_", true);
  3788. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  3789. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  3790. }
  3791. undo_redo->add_do_method(viewport, "update");
  3792. undo_redo->add_undo_method(viewport, "update");
  3793. undo_redo->commit_action();
  3794. } break;
  3795. case GROUP_SELECTED: {
  3796. undo_redo->create_action(TTR("Group Selected"));
  3797. List<Node *> selection = editor_selection->get_selected_node_list();
  3798. for (Node *E : selection) {
  3799. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E);
  3800. if (!canvas_item || !canvas_item->is_inside_tree()) {
  3801. continue;
  3802. }
  3803. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3804. continue;
  3805. }
  3806. undo_redo->add_do_method(canvas_item, "set_meta", "_edit_group_", true);
  3807. undo_redo->add_undo_method(canvas_item, "remove_meta", "_edit_group_");
  3808. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  3809. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  3810. }
  3811. undo_redo->add_do_method(viewport, "update");
  3812. undo_redo->add_undo_method(viewport, "update");
  3813. undo_redo->commit_action();
  3814. } break;
  3815. case UNGROUP_SELECTED: {
  3816. undo_redo->create_action(TTR("Ungroup Selected"));
  3817. List<Node *> selection = editor_selection->get_selected_node_list();
  3818. for (Node *E : selection) {
  3819. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E);
  3820. if (!canvas_item || !canvas_item->is_inside_tree()) {
  3821. continue;
  3822. }
  3823. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3824. continue;
  3825. }
  3826. undo_redo->add_do_method(canvas_item, "remove_meta", "_edit_group_");
  3827. undo_redo->add_undo_method(canvas_item, "set_meta", "_edit_group_", true);
  3828. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  3829. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  3830. }
  3831. undo_redo->add_do_method(viewport, "update");
  3832. undo_redo->add_undo_method(viewport, "update");
  3833. undo_redo->commit_action();
  3834. } break;
  3835. case ANIM_INSERT_KEY:
  3836. case ANIM_INSERT_KEY_EXISTING: {
  3837. bool existing = p_op == ANIM_INSERT_KEY_EXISTING;
  3838. _insert_animation_keys(true, true, true, existing);
  3839. } break;
  3840. case ANIM_INSERT_POS: {
  3841. key_pos = key_loc_button->is_pressed();
  3842. } break;
  3843. case ANIM_INSERT_ROT: {
  3844. key_rot = key_rot_button->is_pressed();
  3845. } break;
  3846. case ANIM_INSERT_SCALE: {
  3847. key_scale = key_scale_button->is_pressed();
  3848. } break;
  3849. case ANIM_COPY_POSE: {
  3850. pose_clipboard.clear();
  3851. const Map<Node *, Object *> &selection = editor_selection->get_selection();
  3852. for (const KeyValue<Node *, Object *> &E : selection) {
  3853. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E.key);
  3854. if (!canvas_item || !canvas_item->is_visible_in_tree()) {
  3855. continue;
  3856. }
  3857. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3858. continue;
  3859. }
  3860. if (Object::cast_to<Node2D>(canvas_item)) {
  3861. Node2D *n2d = Object::cast_to<Node2D>(canvas_item);
  3862. PoseClipboard pc;
  3863. pc.pos = n2d->get_position();
  3864. pc.rot = n2d->get_rotation();
  3865. pc.scale = n2d->get_scale();
  3866. pc.id = n2d->get_instance_id();
  3867. pose_clipboard.push_back(pc);
  3868. }
  3869. }
  3870. } break;
  3871. case ANIM_PASTE_POSE: {
  3872. if (!pose_clipboard.size()) {
  3873. break;
  3874. }
  3875. undo_redo->create_action(TTR("Paste Pose"));
  3876. for (const PoseClipboard &E : pose_clipboard) {
  3877. Node2D *n2d = Object::cast_to<Node2D>(ObjectDB::get_instance(E.id));
  3878. if (!n2d) {
  3879. continue;
  3880. }
  3881. undo_redo->add_do_method(n2d, "set_position", E.pos);
  3882. undo_redo->add_do_method(n2d, "set_rotation", E.rot);
  3883. undo_redo->add_do_method(n2d, "set_scale", E.scale);
  3884. undo_redo->add_undo_method(n2d, "set_position", n2d->get_position());
  3885. undo_redo->add_undo_method(n2d, "set_rotation", n2d->get_rotation());
  3886. undo_redo->add_undo_method(n2d, "set_scale", n2d->get_scale());
  3887. }
  3888. undo_redo->commit_action();
  3889. } break;
  3890. case ANIM_CLEAR_POSE: {
  3891. Map<Node *, Object *> &selection = editor_selection->get_selection();
  3892. for (const KeyValue<Node *, Object *> &E : selection) {
  3893. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E.key);
  3894. if (!canvas_item || !canvas_item->is_visible_in_tree()) {
  3895. continue;
  3896. }
  3897. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3898. continue;
  3899. }
  3900. if (Object::cast_to<Node2D>(canvas_item)) {
  3901. Node2D *n2d = Object::cast_to<Node2D>(canvas_item);
  3902. if (key_pos) {
  3903. n2d->set_position(Vector2());
  3904. }
  3905. if (key_rot) {
  3906. n2d->set_rotation(0);
  3907. }
  3908. if (key_scale) {
  3909. n2d->set_scale(Vector2(1, 1));
  3910. }
  3911. } else if (Object::cast_to<Control>(canvas_item)) {
  3912. Control *ctrl = Object::cast_to<Control>(canvas_item);
  3913. if (key_pos) {
  3914. ctrl->set_position(Point2());
  3915. }
  3916. }
  3917. }
  3918. } break;
  3919. case CLEAR_GUIDES: {
  3920. Node *const root = EditorNode::get_singleton()->get_edited_scene();
  3921. if (root && (root->has_meta("_edit_horizontal_guides_") || root->has_meta("_edit_vertical_guides_"))) {
  3922. undo_redo->create_action(TTR("Clear Guides"));
  3923. if (root->has_meta("_edit_horizontal_guides_")) {
  3924. Array hguides = root->get_meta("_edit_horizontal_guides_");
  3925. undo_redo->add_do_method(root, "remove_meta", "_edit_horizontal_guides_");
  3926. undo_redo->add_undo_method(root, "set_meta", "_edit_horizontal_guides_", hguides);
  3927. }
  3928. if (root->has_meta("_edit_vertical_guides_")) {
  3929. Array vguides = root->get_meta("_edit_vertical_guides_");
  3930. undo_redo->add_do_method(root, "remove_meta", "_edit_vertical_guides_");
  3931. undo_redo->add_undo_method(root, "set_meta", "_edit_vertical_guides_", vguides);
  3932. }
  3933. undo_redo->add_undo_method(viewport, "update");
  3934. undo_redo->commit_action();
  3935. }
  3936. } break;
  3937. case VIEW_CENTER_TO_SELECTION:
  3938. case VIEW_FRAME_TO_SELECTION: {
  3939. _focus_selection(p_op);
  3940. } break;
  3941. case PREVIEW_CANVAS_SCALE: {
  3942. bool preview = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(PREVIEW_CANVAS_SCALE));
  3943. preview = !preview;
  3944. RS::get_singleton()->canvas_set_disable_scale(!preview);
  3945. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(PREVIEW_CANVAS_SCALE), preview);
  3946. } break;
  3947. case SKELETON_MAKE_BONES: {
  3948. Map<Node *, Object *> &selection = editor_selection->get_selection();
  3949. Node *editor_root = EditorNode::get_singleton()->get_edited_scene()->get_tree()->get_edited_scene_root();
  3950. undo_redo->create_action(TTR("Create Custom Bone2D(s) from Node(s)"));
  3951. for (const KeyValue<Node *, Object *> &E : selection) {
  3952. Node2D *n2d = Object::cast_to<Node2D>(E.key);
  3953. Bone2D *new_bone = memnew(Bone2D);
  3954. String new_bone_name = n2d->get_name();
  3955. new_bone_name += "Bone2D";
  3956. new_bone->set_name(new_bone_name);
  3957. new_bone->set_transform(n2d->get_transform());
  3958. Node *n2d_parent = n2d->get_parent();
  3959. if (!n2d_parent) {
  3960. continue;
  3961. }
  3962. undo_redo->add_do_method(n2d_parent, "add_child", new_bone);
  3963. undo_redo->add_do_method(n2d_parent, "remove_child", n2d);
  3964. undo_redo->add_do_method(new_bone, "add_child", n2d);
  3965. undo_redo->add_do_method(n2d, "set_transform", Transform2D());
  3966. undo_redo->add_do_method(this, "_set_owner_for_node_and_children", new_bone, editor_root);
  3967. undo_redo->add_undo_method(new_bone, "remove_child", n2d);
  3968. undo_redo->add_undo_method(n2d_parent, "add_child", n2d);
  3969. undo_redo->add_undo_method(n2d, "set_transform", new_bone->get_transform());
  3970. undo_redo->add_undo_method(new_bone, "queue_free");
  3971. undo_redo->add_undo_method(this, "_set_owner_for_node_and_children", n2d, editor_root);
  3972. }
  3973. undo_redo->commit_action();
  3974. } break;
  3975. }
  3976. }
  3977. void CanvasItemEditor::_set_owner_for_node_and_children(Node *p_node, Node *p_owner) {
  3978. p_node->set_owner(p_owner);
  3979. for (int i = 0; i < p_node->get_child_count(); i++) {
  3980. _set_owner_for_node_and_children(p_node->get_child(i), p_owner);
  3981. }
  3982. }
  3983. void CanvasItemEditor::_focus_selection(int p_op) {
  3984. Vector2 center(0.f, 0.f);
  3985. Rect2 rect;
  3986. int count = 0;
  3987. const Map<Node *, Object *> &selection = editor_selection->get_selection();
  3988. for (const KeyValue<Node *, Object *> &E : selection) {
  3989. CanvasItem *canvas_item = Object::cast_to<CanvasItem>(E.key);
  3990. if (!canvas_item) {
  3991. continue;
  3992. }
  3993. if (canvas_item->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  3994. continue;
  3995. }
  3996. // counting invisible items, for now
  3997. //if (!canvas_item->is_visible_in_tree()) continue;
  3998. ++count;
  3999. Rect2 item_rect;
  4000. if (canvas_item->_edit_use_rect()) {
  4001. item_rect = canvas_item->_edit_get_rect();
  4002. } else {
  4003. item_rect = Rect2();
  4004. }
  4005. Vector2 pos = canvas_item->get_global_transform().get_origin();
  4006. Vector2 scale = canvas_item->get_global_transform().get_scale();
  4007. real_t angle = canvas_item->get_global_transform().get_rotation();
  4008. Transform2D t(angle, Vector2(0.f, 0.f));
  4009. item_rect = t.xform(item_rect);
  4010. Rect2 canvas_item_rect(pos + scale * item_rect.position, scale * item_rect.size);
  4011. if (count == 1) {
  4012. rect = canvas_item_rect;
  4013. } else {
  4014. rect = rect.merge(canvas_item_rect);
  4015. }
  4016. };
  4017. if (p_op == VIEW_CENTER_TO_SELECTION) {
  4018. center = rect.get_center();
  4019. Vector2 offset = viewport->get_size() / 2 - EditorNode::get_singleton()->get_scene_root()->get_global_canvas_transform().xform(center);
  4020. view_offset -= (offset / zoom).round();
  4021. update_viewport();
  4022. } else { // VIEW_FRAME_TO_SELECTION
  4023. if (rect.size.x > CMP_EPSILON && rect.size.y > CMP_EPSILON) {
  4024. real_t scale_x = viewport->get_size().x / rect.size.x;
  4025. real_t scale_y = viewport->get_size().y / rect.size.y;
  4026. zoom = scale_x < scale_y ? scale_x : scale_y;
  4027. zoom *= 0.90;
  4028. viewport->update();
  4029. zoom_widget->set_zoom(zoom);
  4030. call_deferred(SNAME("_popup_callback"), VIEW_CENTER_TO_SELECTION);
  4031. }
  4032. }
  4033. }
  4034. void CanvasItemEditor::_bind_methods() {
  4035. ClassDB::bind_method(D_METHOD("_update_override_camera_button", "game_running"), &CanvasItemEditor::_update_override_camera_button);
  4036. ClassDB::bind_method("_get_editor_data", &CanvasItemEditor::_get_editor_data);
  4037. ClassDB::bind_method(D_METHOD("set_state"), &CanvasItemEditor::set_state);
  4038. ClassDB::bind_method(D_METHOD("update_viewport"), &CanvasItemEditor::update_viewport);
  4039. ClassDB::bind_method(D_METHOD("_zoom_on_position"), &CanvasItemEditor::_zoom_on_position);
  4040. ClassDB::bind_method("_set_owner_for_node_and_children", &CanvasItemEditor::_set_owner_for_node_and_children);
  4041. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  4042. ADD_SIGNAL(MethodInfo("item_group_status_changed"));
  4043. }
  4044. Dictionary CanvasItemEditor::get_state() const {
  4045. Dictionary state;
  4046. // Take the editor scale into account.
  4047. state["zoom"] = zoom / MAX(1, EDSCALE);
  4048. state["ofs"] = view_offset;
  4049. state["grid_offset"] = grid_offset;
  4050. state["grid_step"] = grid_step;
  4051. state["primary_grid_steps"] = primary_grid_steps;
  4052. state["snap_rotation_offset"] = snap_rotation_offset;
  4053. state["snap_rotation_step"] = snap_rotation_step;
  4054. state["snap_scale_step"] = snap_scale_step;
  4055. state["smart_snap_active"] = smart_snap_active;
  4056. state["grid_snap_active"] = grid_snap_active;
  4057. state["snap_node_parent"] = snap_node_parent;
  4058. state["snap_node_anchors"] = snap_node_anchors;
  4059. state["snap_node_sides"] = snap_node_sides;
  4060. state["snap_node_center"] = snap_node_center;
  4061. state["snap_other_nodes"] = snap_other_nodes;
  4062. state["snap_guides"] = snap_guides;
  4063. state["grid_visibility"] = grid_visibility;
  4064. state["show_origin"] = show_origin;
  4065. state["show_viewport"] = show_viewport;
  4066. state["show_rulers"] = show_rulers;
  4067. state["show_guides"] = show_guides;
  4068. state["show_helpers"] = show_helpers;
  4069. state["show_zoom_control"] = zoom_widget->is_visible();
  4070. state["show_edit_locks"] = show_edit_locks;
  4071. state["show_transformation_gizmos"] = show_transformation_gizmos;
  4072. state["snap_rotation"] = snap_rotation;
  4073. state["snap_scale"] = snap_scale;
  4074. state["snap_relative"] = snap_relative;
  4075. state["snap_pixel"] = snap_pixel;
  4076. return state;
  4077. }
  4078. void CanvasItemEditor::set_state(const Dictionary &p_state) {
  4079. bool update_scrollbars = false;
  4080. Dictionary state = p_state;
  4081. if (state.has("zoom")) {
  4082. // Compensate the editor scale, so that the editor scale can be changed
  4083. // and the zoom level will still be the same (relative to the editor scale).
  4084. zoom = real_t(p_state["zoom"]) * MAX(1, EDSCALE);
  4085. zoom_widget->set_zoom(zoom);
  4086. }
  4087. if (state.has("ofs")) {
  4088. view_offset = p_state["ofs"];
  4089. previous_update_view_offset = view_offset;
  4090. update_scrollbars = true;
  4091. }
  4092. if (state.has("grid_offset")) {
  4093. grid_offset = state["grid_offset"];
  4094. }
  4095. if (state.has("grid_step")) {
  4096. grid_step = state["grid_step"];
  4097. }
  4098. if (state.has("primary_grid_steps")) {
  4099. primary_grid_steps = state["primary_grid_steps"];
  4100. }
  4101. if (state.has("snap_rotation_step")) {
  4102. snap_rotation_step = state["snap_rotation_step"];
  4103. }
  4104. if (state.has("snap_rotation_offset")) {
  4105. snap_rotation_offset = state["snap_rotation_offset"];
  4106. }
  4107. if (state.has("snap_scale_step")) {
  4108. snap_scale_step = state["snap_scale_step"];
  4109. }
  4110. if (state.has("smart_snap_active")) {
  4111. smart_snap_active = state["smart_snap_active"];
  4112. smart_snap_button->set_pressed(smart_snap_active);
  4113. }
  4114. if (state.has("grid_snap_active")) {
  4115. grid_snap_active = state["grid_snap_active"];
  4116. grid_snap_button->set_pressed(grid_snap_active);
  4117. }
  4118. if (state.has("snap_node_parent")) {
  4119. snap_node_parent = state["snap_node_parent"];
  4120. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_PARENT);
  4121. smartsnap_config_popup->set_item_checked(idx, snap_node_parent);
  4122. }
  4123. if (state.has("snap_node_anchors")) {
  4124. snap_node_anchors = state["snap_node_anchors"];
  4125. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_ANCHORS);
  4126. smartsnap_config_popup->set_item_checked(idx, snap_node_anchors);
  4127. }
  4128. if (state.has("snap_node_sides")) {
  4129. snap_node_sides = state["snap_node_sides"];
  4130. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_SIDES);
  4131. smartsnap_config_popup->set_item_checked(idx, snap_node_sides);
  4132. }
  4133. if (state.has("snap_node_center")) {
  4134. snap_node_center = state["snap_node_center"];
  4135. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_NODE_CENTER);
  4136. smartsnap_config_popup->set_item_checked(idx, snap_node_center);
  4137. }
  4138. if (state.has("snap_other_nodes")) {
  4139. snap_other_nodes = state["snap_other_nodes"];
  4140. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_OTHER_NODES);
  4141. smartsnap_config_popup->set_item_checked(idx, snap_other_nodes);
  4142. }
  4143. if (state.has("snap_guides")) {
  4144. snap_guides = state["snap_guides"];
  4145. int idx = smartsnap_config_popup->get_item_index(SNAP_USE_GUIDES);
  4146. smartsnap_config_popup->set_item_checked(idx, snap_guides);
  4147. }
  4148. if (state.has("grid_visibility")) {
  4149. grid_visibility = (GridVisibility)(int)(state["grid_visibility"]);
  4150. }
  4151. if (state.has("show_origin")) {
  4152. show_origin = state["show_origin"];
  4153. int idx = view_menu->get_popup()->get_item_index(SHOW_ORIGIN);
  4154. view_menu->get_popup()->set_item_checked(idx, show_origin);
  4155. }
  4156. if (state.has("show_viewport")) {
  4157. show_viewport = state["show_viewport"];
  4158. int idx = view_menu->get_popup()->get_item_index(SHOW_VIEWPORT);
  4159. view_menu->get_popup()->set_item_checked(idx, show_viewport);
  4160. }
  4161. if (state.has("show_rulers")) {
  4162. show_rulers = state["show_rulers"];
  4163. int idx = view_menu->get_popup()->get_item_index(SHOW_RULERS);
  4164. view_menu->get_popup()->set_item_checked(idx, show_rulers);
  4165. update_scrollbars = true;
  4166. }
  4167. if (state.has("show_guides")) {
  4168. show_guides = state["show_guides"];
  4169. int idx = view_menu->get_popup()->get_item_index(SHOW_GUIDES);
  4170. view_menu->get_popup()->set_item_checked(idx, show_guides);
  4171. }
  4172. if (state.has("show_helpers")) {
  4173. show_helpers = state["show_helpers"];
  4174. int idx = view_menu->get_popup()->get_item_index(SHOW_HELPERS);
  4175. view_menu->get_popup()->set_item_checked(idx, show_helpers);
  4176. }
  4177. if (state.has("show_edit_locks")) {
  4178. show_edit_locks = state["show_edit_locks"];
  4179. int idx = view_menu->get_popup()->get_item_index(SHOW_EDIT_LOCKS);
  4180. view_menu->get_popup()->set_item_checked(idx, show_edit_locks);
  4181. }
  4182. if (state.has("show_transformation_gizmos")) {
  4183. show_transformation_gizmos = state["show_transformation_gizmos"];
  4184. int idx = view_menu->get_popup()->get_item_index(SHOW_TRANSFORMATION_GIZMOS);
  4185. view_menu->get_popup()->set_item_checked(idx, show_transformation_gizmos);
  4186. }
  4187. if (state.has("show_zoom_control")) {
  4188. // This one is not user-controllable, but instrumentable
  4189. zoom_widget->set_visible(state["show_zoom_control"]);
  4190. }
  4191. if (state.has("snap_rotation")) {
  4192. snap_rotation = state["snap_rotation"];
  4193. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_ROTATION);
  4194. snap_config_menu->get_popup()->set_item_checked(idx, snap_rotation);
  4195. }
  4196. if (state.has("snap_scale")) {
  4197. snap_scale = state["snap_scale"];
  4198. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_SCALE);
  4199. snap_config_menu->get_popup()->set_item_checked(idx, snap_scale);
  4200. }
  4201. if (state.has("snap_relative")) {
  4202. snap_relative = state["snap_relative"];
  4203. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_RELATIVE);
  4204. snap_config_menu->get_popup()->set_item_checked(idx, snap_relative);
  4205. }
  4206. if (state.has("snap_pixel")) {
  4207. snap_pixel = state["snap_pixel"];
  4208. int idx = snap_config_menu->get_popup()->get_item_index(SNAP_USE_PIXEL);
  4209. snap_config_menu->get_popup()->set_item_checked(idx, snap_pixel);
  4210. }
  4211. if (update_scrollbars) {
  4212. _update_scrollbars();
  4213. }
  4214. viewport->update();
  4215. }
  4216. void CanvasItemEditor::add_control_to_menu_panel(Control *p_control) {
  4217. ERR_FAIL_COND(!p_control);
  4218. hbc_context_menu->add_child(p_control);
  4219. }
  4220. void CanvasItemEditor::remove_control_from_menu_panel(Control *p_control) {
  4221. hbc_context_menu->remove_child(p_control);
  4222. }
  4223. void CanvasItemEditor::add_control_to_left_panel(Control *p_control) {
  4224. left_panel_split->add_child(p_control);
  4225. left_panel_split->move_child(p_control, 0);
  4226. }
  4227. void CanvasItemEditor::add_control_to_right_panel(Control *p_control) {
  4228. right_panel_split->add_child(p_control);
  4229. right_panel_split->move_child(p_control, 1);
  4230. }
  4231. void CanvasItemEditor::remove_control_from_left_panel(Control *p_control) {
  4232. left_panel_split->remove_child(p_control);
  4233. }
  4234. void CanvasItemEditor::remove_control_from_right_panel(Control *p_control) {
  4235. right_panel_split->remove_child(p_control);
  4236. }
  4237. VSplitContainer *CanvasItemEditor::get_bottom_split() {
  4238. return bottom_split;
  4239. }
  4240. void CanvasItemEditor::focus_selection() {
  4241. _focus_selection(VIEW_CENTER_TO_SELECTION);
  4242. }
  4243. CanvasItemEditor::CanvasItemEditor() {
  4244. zoom = 1.0 / MAX(1, EDSCALE);
  4245. view_offset = Point2(-150 - RULER_WIDTH, -95 - RULER_WIDTH);
  4246. previous_update_view_offset = view_offset; // Moves the view a little bit to the left so that (0,0) is visible. The values a relative to a 16/10 screen
  4247. grid_offset = Point2();
  4248. grid_step = Point2(8, 8); // A power-of-two value works better as a default
  4249. primary_grid_steps = 8; // A power-of-two value works better as a default
  4250. grid_step_multiplier = 0;
  4251. snap_rotation_offset = 0;
  4252. snap_rotation_step = Math::deg2rad(15.0);
  4253. snap_scale_step = 0.1f;
  4254. snap_target[0] = SNAP_TARGET_NONE;
  4255. snap_target[1] = SNAP_TARGET_NONE;
  4256. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  4257. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  4258. editor_selection->add_editor_plugin(this);
  4259. editor_selection->connect("selection_changed", callable_mp((CanvasItem *)this, &CanvasItem::update));
  4260. editor_selection->connect("selection_changed", callable_mp(this, &CanvasItemEditor::_selection_changed));
  4261. SceneTreeDock::get_singleton()->connect("node_created", callable_mp(this, &CanvasItemEditor::_node_created));
  4262. SceneTreeDock::get_singleton()->connect("add_node_used", callable_mp(this, &CanvasItemEditor::_reset_create_position));
  4263. EditorNode::get_singleton()->call_deferred(SNAME("connect"), "play_pressed", Callable(this, "_update_override_camera_button"), make_binds(true));
  4264. EditorNode::get_singleton()->call_deferred(SNAME("connect"), "stop_pressed", Callable(this, "_update_override_camera_button"), make_binds(false));
  4265. hb = memnew(HBoxContainer);
  4266. add_child(hb);
  4267. hb->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  4268. bottom_split = memnew(VSplitContainer);
  4269. add_child(bottom_split);
  4270. bottom_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4271. left_panel_split = memnew(HSplitContainer);
  4272. bottom_split->add_child(left_panel_split);
  4273. left_panel_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4274. right_panel_split = memnew(HSplitContainer);
  4275. left_panel_split->add_child(right_panel_split);
  4276. right_panel_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4277. viewport_scrollable = memnew(Control);
  4278. right_panel_split->add_child(viewport_scrollable);
  4279. viewport_scrollable->set_mouse_filter(MOUSE_FILTER_PASS);
  4280. viewport_scrollable->set_clip_contents(true);
  4281. viewport_scrollable->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4282. viewport_scrollable->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  4283. viewport_scrollable->connect("draw", callable_mp(this, &CanvasItemEditor::_update_scrollbars));
  4284. SubViewportContainer *scene_tree = memnew(SubViewportContainer);
  4285. viewport_scrollable->add_child(scene_tree);
  4286. scene_tree->set_stretch(true);
  4287. scene_tree->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  4288. scene_tree->add_child(EditorNode::get_singleton()->get_scene_root());
  4289. controls_vb = memnew(VBoxContainer);
  4290. controls_vb->set_begin(Point2(5, 5));
  4291. zoom_widget = memnew(EditorZoomWidget);
  4292. controls_vb->add_child(zoom_widget);
  4293. zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
  4294. zoom_widget->connect("zoom_changed", callable_mp(this, &CanvasItemEditor::_update_zoom));
  4295. panner.instantiate();
  4296. panner->set_callbacks(callable_mp(this, &CanvasItemEditor::_scroll_callback), callable_mp(this, &CanvasItemEditor::_pan_callback), callable_mp(this, &CanvasItemEditor::_zoom_callback));
  4297. viewport = memnew(CanvasItemEditorViewport(this));
  4298. viewport_scrollable->add_child(viewport);
  4299. viewport->set_mouse_filter(MOUSE_FILTER_PASS);
  4300. viewport->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  4301. viewport->set_clip_contents(true);
  4302. viewport->set_focus_mode(FOCUS_ALL);
  4303. viewport->connect("draw", callable_mp(this, &CanvasItemEditor::_draw_viewport));
  4304. viewport->connect("gui_input", callable_mp(this, &CanvasItemEditor::_gui_input_viewport));
  4305. viewport->connect("focus_exited", callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
  4306. h_scroll = memnew(HScrollBar);
  4307. viewport->add_child(h_scroll);
  4308. h_scroll->connect("value_changed", callable_mp(this, &CanvasItemEditor::_update_scroll));
  4309. h_scroll->hide();
  4310. v_scroll = memnew(VScrollBar);
  4311. viewport->add_child(v_scroll);
  4312. v_scroll->connect("value_changed", callable_mp(this, &CanvasItemEditor::_update_scroll));
  4313. v_scroll->hide();
  4314. viewport->add_child(controls_vb);
  4315. // Add some margin to the left for better aesthetics.
  4316. // This prevents the first button's hover/pressed effect from "touching" the panel's border,
  4317. // which looks ugly.
  4318. Control *margin_left = memnew(Control);
  4319. hb->add_child(margin_left);
  4320. margin_left->set_custom_minimum_size(Size2(2, 0) * EDSCALE);
  4321. select_button = memnew(Button);
  4322. select_button->set_flat(true);
  4323. hb->add_child(select_button);
  4324. select_button->set_toggle_mode(true);
  4325. select_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_SELECT));
  4326. select_button->set_pressed(true);
  4327. select_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/select_mode", TTR("Select Mode"), Key::Q));
  4328. select_button->set_shortcut_context(this);
  4329. select_button->set_tooltip(keycode_get_string((Key)KeyModifierMask::CMD) + TTR("Drag: Rotate selected node around pivot.") + "\n" + TTR("Alt+Drag: Move selected node.") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD) + TTR("Alt+Drag: Scale selected node.") + "\n" + TTR("V: Set selected node's pivot position.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked.") + "\n" + keycode_get_string((Key)KeyModifierMask::CMD) + TTR("RMB: Add node at position clicked."));
  4330. hb->add_child(memnew(VSeparator));
  4331. move_button = memnew(Button);
  4332. move_button->set_flat(true);
  4333. hb->add_child(move_button);
  4334. move_button->set_toggle_mode(true);
  4335. move_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_MOVE));
  4336. move_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/move_mode", TTR("Move Mode"), Key::W));
  4337. move_button->set_shortcut_context(this);
  4338. move_button->set_tooltip(TTR("Move Mode"));
  4339. rotate_button = memnew(Button);
  4340. rotate_button->set_flat(true);
  4341. hb->add_child(rotate_button);
  4342. rotate_button->set_toggle_mode(true);
  4343. rotate_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_ROTATE));
  4344. rotate_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/rotate_mode", TTR("Rotate Mode"), Key::E));
  4345. rotate_button->set_shortcut_context(this);
  4346. rotate_button->set_tooltip(TTR("Rotate Mode"));
  4347. scale_button = memnew(Button);
  4348. scale_button->set_flat(true);
  4349. hb->add_child(scale_button);
  4350. scale_button->set_toggle_mode(true);
  4351. scale_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_SCALE));
  4352. scale_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/scale_mode", TTR("Scale Mode"), Key::S));
  4353. scale_button->set_shortcut_context(this);
  4354. scale_button->set_tooltip(TTR("Shift: Scale proportionally."));
  4355. hb->add_child(memnew(VSeparator));
  4356. list_select_button = memnew(Button);
  4357. list_select_button->set_flat(true);
  4358. hb->add_child(list_select_button);
  4359. list_select_button->set_toggle_mode(true);
  4360. list_select_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_LIST_SELECT));
  4361. list_select_button->set_tooltip(TTR("Show list of selectable nodes at position clicked."));
  4362. pivot_button = memnew(Button);
  4363. pivot_button->set_flat(true);
  4364. hb->add_child(pivot_button);
  4365. pivot_button->set_toggle_mode(true);
  4366. pivot_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_EDIT_PIVOT));
  4367. pivot_button->set_tooltip(TTR("Click to change object's rotation pivot."));
  4368. pan_button = memnew(Button);
  4369. pan_button->set_flat(true);
  4370. hb->add_child(pan_button);
  4371. pan_button->set_toggle_mode(true);
  4372. pan_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_PAN));
  4373. pan_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/pan_mode", TTR("Pan Mode"), Key::G));
  4374. pan_button->set_shortcut_context(this);
  4375. pan_button->set_tooltip(TTR("You can also use Pan View shortcut (Space by default) to pan in any mode."));
  4376. ruler_button = memnew(Button);
  4377. ruler_button->set_flat(true);
  4378. hb->add_child(ruler_button);
  4379. ruler_button->set_toggle_mode(true);
  4380. ruler_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_button_tool_select), make_binds(TOOL_RULER));
  4381. ruler_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/ruler_mode", TTR("Ruler Mode"), Key::R));
  4382. ruler_button->set_shortcut_context(this);
  4383. ruler_button->set_tooltip(TTR("Ruler Mode"));
  4384. hb->add_child(memnew(VSeparator));
  4385. smart_snap_button = memnew(Button);
  4386. smart_snap_button->set_flat(true);
  4387. hb->add_child(smart_snap_button);
  4388. smart_snap_button->set_toggle_mode(true);
  4389. smart_snap_button->connect("toggled", callable_mp(this, &CanvasItemEditor::_button_toggle_smart_snap));
  4390. smart_snap_button->set_tooltip(TTR("Toggle smart snapping."));
  4391. smart_snap_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/use_smart_snap", TTR("Use Smart Snap"), KeyModifierMask::SHIFT | Key::S));
  4392. smart_snap_button->set_shortcut_context(this);
  4393. grid_snap_button = memnew(Button);
  4394. grid_snap_button->set_flat(true);
  4395. hb->add_child(grid_snap_button);
  4396. grid_snap_button->set_toggle_mode(true);
  4397. grid_snap_button->connect("toggled", callable_mp(this, &CanvasItemEditor::_button_toggle_grid_snap));
  4398. grid_snap_button->set_tooltip(TTR("Toggle grid snapping."));
  4399. grid_snap_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/use_grid_snap", TTR("Use Grid Snap"), KeyModifierMask::SHIFT | Key::G));
  4400. grid_snap_button->set_shortcut_context(this);
  4401. snap_config_menu = memnew(MenuButton);
  4402. snap_config_menu->set_shortcut_context(this);
  4403. hb->add_child(snap_config_menu);
  4404. snap_config_menu->set_h_size_flags(SIZE_SHRINK_END);
  4405. snap_config_menu->set_tooltip(TTR("Snapping Options"));
  4406. snap_config_menu->set_switch_on_hover(true);
  4407. PopupMenu *p = snap_config_menu->get_popup();
  4408. p->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
  4409. p->set_hide_on_checkable_item_selection(false);
  4410. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_rotation_snap", TTR("Use Rotation Snap")), SNAP_USE_ROTATION);
  4411. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_scale_snap", TTR("Use Scale Snap")), SNAP_USE_SCALE);
  4412. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_relative", TTR("Snap Relative")), SNAP_RELATIVE);
  4413. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/use_pixel_snap", TTR("Use Pixel Snap")), SNAP_USE_PIXEL);
  4414. p->add_submenu_item(TTR("Smart Snapping"), "SmartSnapping");
  4415. p->add_separator();
  4416. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/configure_snap", TTR("Configure Snap...")), SNAP_CONFIGURE);
  4417. smartsnap_config_popup = memnew(PopupMenu);
  4418. p->add_child(smartsnap_config_popup);
  4419. smartsnap_config_popup->set_name("SmartSnapping");
  4420. smartsnap_config_popup->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
  4421. smartsnap_config_popup->set_hide_on_checkable_item_selection(false);
  4422. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_parent", TTR("Snap to Parent")), SNAP_USE_NODE_PARENT);
  4423. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_anchors", TTR("Snap to Node Anchor")), SNAP_USE_NODE_ANCHORS);
  4424. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_sides", TTR("Snap to Node Sides")), SNAP_USE_NODE_SIDES);
  4425. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_node_center", TTR("Snap to Node Center")), SNAP_USE_NODE_CENTER);
  4426. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_other_nodes", TTR("Snap to Other Nodes")), SNAP_USE_OTHER_NODES);
  4427. smartsnap_config_popup->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/snap_guides", TTR("Snap to Guides")), SNAP_USE_GUIDES);
  4428. hb->add_child(memnew(VSeparator));
  4429. lock_button = memnew(Button);
  4430. lock_button->set_flat(true);
  4431. hb->add_child(lock_button);
  4432. lock_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(LOCK_SELECTED));
  4433. lock_button->set_tooltip(TTR("Lock selected node, preventing selection and movement."));
  4434. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  4435. lock_button->set_shortcut(ED_SHORTCUT("editor/lock_selected_nodes", TTR("Lock Selected Node(s)"), KeyModifierMask::CMD | Key::L));
  4436. unlock_button = memnew(Button);
  4437. unlock_button->set_flat(true);
  4438. hb->add_child(unlock_button);
  4439. unlock_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(UNLOCK_SELECTED));
  4440. unlock_button->set_tooltip(TTR("Unlock selected node, allowing selection and movement."));
  4441. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  4442. unlock_button->set_shortcut(ED_SHORTCUT("editor/unlock_selected_nodes", TTR("Unlock Selected Node(s)"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::L));
  4443. group_button = memnew(Button);
  4444. group_button->set_flat(true);
  4445. hb->add_child(group_button);
  4446. group_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(GROUP_SELECTED));
  4447. group_button->set_tooltip(TTR("Makes sure the object's children are not selectable."));
  4448. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  4449. group_button->set_shortcut(ED_SHORTCUT("editor/group_selected_nodes", TTR("Group Selected Node(s)"), KeyModifierMask::CMD | Key::G));
  4450. ungroup_button = memnew(Button);
  4451. ungroup_button->set_flat(true);
  4452. hb->add_child(ungroup_button);
  4453. ungroup_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(UNGROUP_SELECTED));
  4454. ungroup_button->set_tooltip(TTR("Restores the object's children's ability to be selected."));
  4455. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  4456. ungroup_button->set_shortcut(ED_SHORTCUT("editor/ungroup_selected_nodes", TTR("Ungroup Selected Node(s)"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::G));
  4457. hb->add_child(memnew(VSeparator));
  4458. skeleton_menu = memnew(MenuButton);
  4459. skeleton_menu->set_shortcut_context(this);
  4460. hb->add_child(skeleton_menu);
  4461. skeleton_menu->set_tooltip(TTR("Skeleton Options"));
  4462. skeleton_menu->set_switch_on_hover(true);
  4463. p = skeleton_menu->get_popup();
  4464. p->set_hide_on_checkable_item_selection(false);
  4465. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_show_bones", TTR("Show Bones")), SKELETON_SHOW_BONES);
  4466. p->add_separator();
  4467. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/skeleton_make_bones", TTR("Make Bone2D Node(s) from Node(s)"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::B), SKELETON_MAKE_BONES);
  4468. p->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
  4469. hb->add_child(memnew(VSeparator));
  4470. override_camera_button = memnew(Button);
  4471. override_camera_button->set_flat(true);
  4472. hb->add_child(override_camera_button);
  4473. override_camera_button->connect("toggled", callable_mp(this, &CanvasItemEditor::_button_override_camera));
  4474. override_camera_button->set_toggle_mode(true);
  4475. override_camera_button->set_disabled(true);
  4476. _update_override_camera_button(false);
  4477. hb->add_child(memnew(VSeparator));
  4478. view_menu = memnew(MenuButton);
  4479. view_menu->set_shortcut_context(this);
  4480. view_menu->set_text(TTR("View"));
  4481. hb->add_child(view_menu);
  4482. view_menu->get_popup()->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
  4483. view_menu->set_switch_on_hover(true);
  4484. p = view_menu->get_popup();
  4485. p->set_hide_on_checkable_item_selection(false);
  4486. grid_menu = memnew(PopupMenu);
  4487. grid_menu->connect("about_to_popup", callable_mp(this, &CanvasItemEditor::_prepare_grid_menu));
  4488. grid_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_on_grid_menu_id_pressed));
  4489. grid_menu->set_name("GridMenu");
  4490. grid_menu->add_radio_check_item(TTR("Show"), GRID_VISIBILITY_SHOW);
  4491. grid_menu->add_radio_check_item(TTR("Show When Snapping"), GRID_VISIBILITY_SHOW_WHEN_SNAPPING);
  4492. grid_menu->add_radio_check_item(TTR("Hide"), GRID_VISIBILITY_HIDE);
  4493. grid_menu->add_separator();
  4494. grid_menu->add_shortcut(ED_SHORTCUT("canvas_item_editor/toggle_grid", TTR("Toggle Grid"), KeyModifierMask::CMD | Key::APOSTROPHE));
  4495. p->add_child(grid_menu);
  4496. p->add_submenu_item(TTR("Grid"), "GridMenu");
  4497. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_helpers", TTR("Show Helpers"), Key::H), SHOW_HELPERS);
  4498. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_rulers", TTR("Show Rulers")), SHOW_RULERS);
  4499. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_guides", TTR("Show Guides"), Key::Y), SHOW_GUIDES);
  4500. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_origin", TTR("Show Origin")), SHOW_ORIGIN);
  4501. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_viewport", TTR("Show Viewport")), SHOW_VIEWPORT);
  4502. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_edit_locks", TTR("Show Group And Lock Icons")), SHOW_EDIT_LOCKS);
  4503. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/show_transformation_gizmos", TTR("Show Transformation Gizmos")), SHOW_TRANSFORMATION_GIZMOS);
  4504. p->add_separator();
  4505. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/center_selection", TTR("Center Selection"), Key::F), VIEW_CENTER_TO_SELECTION);
  4506. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/frame_selection", TTR("Frame Selection"), KeyModifierMask::SHIFT | Key::F), VIEW_FRAME_TO_SELECTION);
  4507. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/clear_guides", TTR("Clear Guides")), CLEAR_GUIDES);
  4508. p->add_separator();
  4509. p->add_check_shortcut(ED_SHORTCUT("canvas_item_editor/preview_canvas_scale", TTR("Preview Canvas Scale"), KeyModifierMask::SHIFT | KeyModifierMask::CMD | Key::P), PREVIEW_CANVAS_SCALE);
  4510. hb->add_child(memnew(VSeparator));
  4511. context_menu_container = memnew(PanelContainer);
  4512. hbc_context_menu = memnew(HBoxContainer);
  4513. context_menu_container->add_child(hbc_context_menu);
  4514. // Use a custom stylebox to make contextual menu items stand out from the rest.
  4515. // This helps with editor usability as contextual menu items change when selecting nodes,
  4516. // even though it may not be immediately obvious at first.
  4517. hb->add_child(context_menu_container);
  4518. _update_context_menu_stylebox();
  4519. // Animation controls.
  4520. animation_hb = memnew(HBoxContainer);
  4521. hbc_context_menu->add_child(animation_hb);
  4522. animation_hb->add_child(memnew(VSeparator));
  4523. animation_hb->hide();
  4524. key_loc_button = memnew(Button);
  4525. key_loc_button->set_flat(true);
  4526. key_loc_button->set_toggle_mode(true);
  4527. key_loc_button->set_pressed(true);
  4528. key_loc_button->set_focus_mode(FOCUS_NONE);
  4529. key_loc_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(ANIM_INSERT_POS));
  4530. key_loc_button->set_tooltip(TTR("Translation mask for inserting keys."));
  4531. animation_hb->add_child(key_loc_button);
  4532. key_rot_button = memnew(Button);
  4533. key_rot_button->set_flat(true);
  4534. key_rot_button->set_toggle_mode(true);
  4535. key_rot_button->set_pressed(true);
  4536. key_rot_button->set_focus_mode(FOCUS_NONE);
  4537. key_rot_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(ANIM_INSERT_ROT));
  4538. key_rot_button->set_tooltip(TTR("Rotation mask for inserting keys."));
  4539. animation_hb->add_child(key_rot_button);
  4540. key_scale_button = memnew(Button);
  4541. key_scale_button->set_flat(true);
  4542. key_scale_button->set_toggle_mode(true);
  4543. key_scale_button->set_focus_mode(FOCUS_NONE);
  4544. key_scale_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(ANIM_INSERT_SCALE));
  4545. key_scale_button->set_tooltip(TTR("Scale mask for inserting keys."));
  4546. animation_hb->add_child(key_scale_button);
  4547. key_insert_button = memnew(Button);
  4548. key_insert_button->set_flat(true);
  4549. key_insert_button->set_focus_mode(FOCUS_NONE);
  4550. key_insert_button->connect("pressed", callable_mp(this, &CanvasItemEditor::_popup_callback), varray(ANIM_INSERT_KEY));
  4551. key_insert_button->set_tooltip(TTR("Insert keys (based on mask)."));
  4552. key_insert_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/anim_insert_key", TTR("Insert Key"), Key::INSERT));
  4553. key_insert_button->set_shortcut_context(this);
  4554. animation_hb->add_child(key_insert_button);
  4555. key_auto_insert_button = memnew(Button);
  4556. key_auto_insert_button->set_flat(true);
  4557. key_auto_insert_button->set_toggle_mode(true);
  4558. key_auto_insert_button->set_focus_mode(FOCUS_NONE);
  4559. key_auto_insert_button->set_tooltip(TTR("Auto insert keys when objects are translated, rotated or scaled (based on mask).\nKeys are only added to existing tracks, no new tracks will be created.\nKeys must be inserted manually for the first time."));
  4560. key_auto_insert_button->set_shortcut(ED_SHORTCUT("canvas_item_editor/anim_auto_insert_key", TTR("Auto Insert Key")));
  4561. key_auto_insert_button->set_shortcut_context(this);
  4562. animation_hb->add_child(key_auto_insert_button);
  4563. animation_menu = memnew(MenuButton);
  4564. animation_menu->set_shortcut_context(this);
  4565. animation_menu->set_tooltip(TTR("Animation Key and Pose Options"));
  4566. animation_hb->add_child(animation_menu);
  4567. animation_menu->get_popup()->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_popup_callback));
  4568. animation_menu->set_switch_on_hover(true);
  4569. p = animation_menu->get_popup();
  4570. p->add_shortcut(ED_GET_SHORTCUT("canvas_item_editor/anim_insert_key"), ANIM_INSERT_KEY);
  4571. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/anim_insert_key_existing_tracks", TTR("Insert Key (Existing Tracks)"), KeyModifierMask::CMD + Key::INSERT), ANIM_INSERT_KEY_EXISTING);
  4572. p->add_separator();
  4573. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/anim_copy_pose", TTR("Copy Pose")), ANIM_COPY_POSE);
  4574. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/anim_paste_pose", TTR("Paste Pose")), ANIM_PASTE_POSE);
  4575. p->add_shortcut(ED_SHORTCUT("canvas_item_editor/anim_clear_pose", TTR("Clear Pose"), KeyModifierMask::SHIFT | Key::K), ANIM_CLEAR_POSE);
  4576. snap_dialog = memnew(SnapDialog);
  4577. snap_dialog->connect("confirmed", callable_mp(this, &CanvasItemEditor::_snap_changed));
  4578. add_child(snap_dialog);
  4579. select_sb = Ref<StyleBoxTexture>(memnew(StyleBoxTexture));
  4580. selection_menu = memnew(PopupMenu);
  4581. add_child(selection_menu);
  4582. selection_menu->set_min_size(Vector2(100, 0));
  4583. selection_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_selection_result_pressed));
  4584. selection_menu->connect("popup_hide", callable_mp(this, &CanvasItemEditor::_selection_menu_hide));
  4585. add_node_menu = memnew(PopupMenu);
  4586. add_child(add_node_menu);
  4587. add_node_menu->connect("id_pressed", callable_mp(this, &CanvasItemEditor::_add_node_pressed));
  4588. multiply_grid_step_shortcut = ED_SHORTCUT("canvas_item_editor/multiply_grid_step", TTR("Multiply grid step by 2"), Key::KP_MULTIPLY);
  4589. divide_grid_step_shortcut = ED_SHORTCUT("canvas_item_editor/divide_grid_step", TTR("Divide grid step by 2"), Key::KP_DIVIDE);
  4590. skeleton_menu->get_popup()->set_item_checked(skeleton_menu->get_popup()->get_item_index(SKELETON_SHOW_BONES), true);
  4591. // Store the singleton instance.
  4592. singleton = this;
  4593. // To ensure that scripts can parse the list of shortcuts correctly, we have to define
  4594. // those shortcuts one by one.
  4595. // Resetting zoom to 100% is a duplicate shortcut of `canvas_item_editor/reset_zoom`,
  4596. // but it ensures both 1 and Ctrl + 0 can be used to reset zoom.
  4597. ED_SHORTCUT("canvas_item_editor/zoom_3.125_percent", TTR("Zoom to 3.125%"), KeyModifierMask::SHIFT | Key::KEY_5);
  4598. ED_SHORTCUT("canvas_item_editor/zoom_6.25_percent", TTR("Zoom to 6.25%"), KeyModifierMask::SHIFT | Key::KEY_4);
  4599. ED_SHORTCUT("canvas_item_editor/zoom_12.5_percent", TTR("Zoom to 12.5%"), KeyModifierMask::SHIFT | Key::KEY_3);
  4600. ED_SHORTCUT("canvas_item_editor/zoom_25_percent", TTR("Zoom to 25%"), KeyModifierMask::SHIFT | Key::KEY_2);
  4601. ED_SHORTCUT("canvas_item_editor/zoom_50_percent", TTR("Zoom to 50%"), KeyModifierMask::SHIFT | Key::KEY_1);
  4602. ED_SHORTCUT("canvas_item_editor/zoom_100_percent", TTR("Zoom to 100%"), Key::KEY_1);
  4603. ED_SHORTCUT("canvas_item_editor/zoom_200_percent", TTR("Zoom to 200%"), Key::KEY_2);
  4604. ED_SHORTCUT("canvas_item_editor/zoom_400_percent", TTR("Zoom to 400%"), Key::KEY_3);
  4605. ED_SHORTCUT("canvas_item_editor/zoom_800_percent", TTR("Zoom to 800%"), Key::KEY_4);
  4606. ED_SHORTCUT("canvas_item_editor/zoom_1600_percent", TTR("Zoom to 1600%"), Key::KEY_5);
  4607. set_process_shortcut_input(true);
  4608. // Update the menus' checkboxes
  4609. call_deferred(SNAME("set_state"), get_state());
  4610. }
  4611. CanvasItemEditor *CanvasItemEditor::singleton = nullptr;
  4612. void CanvasItemEditorPlugin::edit(Object *p_object) {
  4613. canvas_item_editor->set_undo_redo(&get_undo_redo());
  4614. canvas_item_editor->edit(Object::cast_to<CanvasItem>(p_object));
  4615. }
  4616. bool CanvasItemEditorPlugin::handles(Object *p_object) const {
  4617. return p_object->is_class("CanvasItem");
  4618. }
  4619. void CanvasItemEditorPlugin::make_visible(bool p_visible) {
  4620. if (p_visible) {
  4621. canvas_item_editor->show();
  4622. canvas_item_editor->set_physics_process(true);
  4623. RenderingServer::get_singleton()->viewport_set_disable_2d(EditorNode::get_singleton()->get_scene_root()->get_viewport_rid(), false);
  4624. } else {
  4625. canvas_item_editor->hide();
  4626. canvas_item_editor->set_physics_process(false);
  4627. RenderingServer::get_singleton()->viewport_set_disable_2d(EditorNode::get_singleton()->get_scene_root()->get_viewport_rid(), true);
  4628. }
  4629. }
  4630. Dictionary CanvasItemEditorPlugin::get_state() const {
  4631. return canvas_item_editor->get_state();
  4632. }
  4633. void CanvasItemEditorPlugin::set_state(const Dictionary &p_state) {
  4634. canvas_item_editor->set_state(p_state);
  4635. }
  4636. CanvasItemEditorPlugin::CanvasItemEditorPlugin() {
  4637. canvas_item_editor = memnew(CanvasItemEditor);
  4638. canvas_item_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4639. EditorNode::get_singleton()->get_main_control()->add_child(canvas_item_editor);
  4640. canvas_item_editor->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  4641. canvas_item_editor->hide();
  4642. }
  4643. CanvasItemEditorPlugin::~CanvasItemEditorPlugin() {
  4644. }
  4645. void CanvasItemEditorViewport::_on_mouse_exit() {
  4646. if (!selector->is_visible()) {
  4647. _remove_preview();
  4648. }
  4649. }
  4650. void CanvasItemEditorViewport::_on_select_type(Object *selected) {
  4651. CheckBox *check = Object::cast_to<CheckBox>(selected);
  4652. String type = check->get_text();
  4653. selector->set_title(vformat(TTR("Add %s"), type));
  4654. label->set_text(vformat(TTR("Adding %s..."), type));
  4655. }
  4656. void CanvasItemEditorViewport::_on_change_type_confirmed() {
  4657. if (!button_group->get_pressed_button()) {
  4658. return;
  4659. }
  4660. CheckBox *check = Object::cast_to<CheckBox>(button_group->get_pressed_button());
  4661. default_texture_node_type = check->get_text();
  4662. _perform_drop_data();
  4663. selector->hide();
  4664. }
  4665. void CanvasItemEditorViewport::_on_change_type_closed() {
  4666. _remove_preview();
  4667. }
  4668. void CanvasItemEditorViewport::_create_preview(const Vector<String> &files) const {
  4669. bool add_preview = false;
  4670. for (int i = 0; i < files.size(); i++) {
  4671. String path = files[i];
  4672. RES res = ResourceLoader::load(path);
  4673. ERR_FAIL_COND(res.is_null());
  4674. Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(*res));
  4675. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  4676. if (texture != nullptr || scene != nullptr) {
  4677. if (texture != nullptr) {
  4678. Sprite2D *sprite = memnew(Sprite2D);
  4679. sprite->set_texture(texture);
  4680. sprite->set_modulate(Color(1, 1, 1, 0.7f));
  4681. preview_node->add_child(sprite);
  4682. label->show();
  4683. label_desc->show();
  4684. label_desc->set_text(TTR("Drag and drop to add as child of current scene's root node.\nHold Ctrl when dropping to add as child of selected node.\nHold Shift when dropping to add as sibling of selected node.\nHold Alt when dropping to add as a different node type."));
  4685. } else {
  4686. if (scene.is_valid()) {
  4687. Node *instance = scene->instantiate();
  4688. if (instance) {
  4689. preview_node->add_child(instance);
  4690. label_desc->show();
  4691. label_desc->set_text(TTR("Drag and drop to add as child of current scene's root node.\nHold Ctrl when dropping to add as child of selected node.\nHold Shift when dropping to add as sibling of selected node."));
  4692. }
  4693. }
  4694. }
  4695. add_preview = true;
  4696. }
  4697. }
  4698. if (add_preview) {
  4699. EditorNode::get_singleton()->get_scene_root()->add_child(preview_node);
  4700. }
  4701. }
  4702. void CanvasItemEditorViewport::_remove_preview() {
  4703. if (preview_node->get_parent()) {
  4704. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  4705. Node *node = preview_node->get_child(i);
  4706. node->queue_delete();
  4707. preview_node->remove_child(node);
  4708. }
  4709. EditorNode::get_singleton()->get_scene_root()->remove_child(preview_node);
  4710. label->hide();
  4711. label_desc->hide();
  4712. }
  4713. }
  4714. bool CanvasItemEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  4715. if (p_desired_node->get_scene_file_path() == p_target_scene_path) {
  4716. return true;
  4717. }
  4718. int childCount = p_desired_node->get_child_count();
  4719. for (int i = 0; i < childCount; i++) {
  4720. Node *child = p_desired_node->get_child(i);
  4721. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  4722. return true;
  4723. }
  4724. }
  4725. return false;
  4726. }
  4727. void CanvasItemEditorViewport::_create_nodes(Node *parent, Node *child, String &path, const Point2 &p_point) {
  4728. // Adjust casing according to project setting. The file name is expected to be in snake_case, but will work for others.
  4729. String name = path.get_file().get_basename();
  4730. switch (ProjectSettings::get_singleton()->get("editor/node_naming/name_casing").operator int()) {
  4731. case NAME_CASING_PASCAL_CASE:
  4732. name = name.capitalize().replace(" ", "");
  4733. break;
  4734. case NAME_CASING_CAMEL_CASE:
  4735. name = name.capitalize().replace(" ", "");
  4736. name[0] = name.to_lower()[0];
  4737. break;
  4738. case NAME_CASING_SNAKE_CASE:
  4739. name = name.capitalize().replace(" ", "_").to_lower();
  4740. break;
  4741. }
  4742. child->set_name(name);
  4743. Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(ResourceCache::get(path)));
  4744. if (parent) {
  4745. editor_data->get_undo_redo().add_do_method(parent, "add_child", child, true);
  4746. editor_data->get_undo_redo().add_do_method(child, "set_owner", EditorNode::get_singleton()->get_edited_scene());
  4747. editor_data->get_undo_redo().add_do_reference(child);
  4748. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", child);
  4749. } else { // If no parent is selected, set as root node of the scene.
  4750. editor_data->get_undo_redo().add_do_method(EditorNode::get_singleton(), "set_edited_scene", child);
  4751. editor_data->get_undo_redo().add_do_method(child, "set_owner", EditorNode::get_singleton()->get_edited_scene());
  4752. editor_data->get_undo_redo().add_do_reference(child);
  4753. editor_data->get_undo_redo().add_undo_method(EditorNode::get_singleton(), "set_edited_scene", (Object *)nullptr);
  4754. }
  4755. if (parent) {
  4756. String new_name = parent->validate_child_name(child);
  4757. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  4758. editor_data->get_undo_redo().add_do_method(ed, "live_debug_create_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent), child->get_class(), new_name);
  4759. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
  4760. }
  4761. if (Object::cast_to<TouchScreenButton>(child) || Object::cast_to<TextureButton>(child)) {
  4762. editor_data->get_undo_redo().add_do_property(child, "texture_normal", texture);
  4763. } else {
  4764. editor_data->get_undo_redo().add_do_property(child, "texture", texture);
  4765. }
  4766. // make visible for certain node type
  4767. if (Object::cast_to<Control>(child)) {
  4768. Size2 texture_size = texture->get_size();
  4769. editor_data->get_undo_redo().add_do_property(child, "rect_size", texture_size);
  4770. } else if (Object::cast_to<Polygon2D>(child)) {
  4771. Size2 texture_size = texture->get_size();
  4772. Vector<Vector2> list = {
  4773. Vector2(0, 0),
  4774. Vector2(texture_size.width, 0),
  4775. Vector2(texture_size.width, texture_size.height),
  4776. Vector2(0, texture_size.height)
  4777. };
  4778. editor_data->get_undo_redo().add_do_property(child, "polygon", list);
  4779. }
  4780. // Compute the global position
  4781. Transform2D xform = canvas_item_editor->get_canvas_transform();
  4782. Point2 target_position = xform.affine_inverse().xform(p_point);
  4783. // there's nothing to be used as source position so snapping will work as absolute if enabled
  4784. target_position = canvas_item_editor->snap_point(target_position);
  4785. editor_data->get_undo_redo().add_do_method(child, "set_global_position", target_position);
  4786. }
  4787. bool CanvasItemEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  4788. Ref<PackedScene> sdata = ResourceLoader::load(path);
  4789. if (!sdata.is_valid()) { // invalid scene
  4790. return false;
  4791. }
  4792. Node *instantiated_scene = sdata->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  4793. if (!instantiated_scene) { // error on instancing
  4794. return false;
  4795. }
  4796. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  4797. if (!edited_scene->get_scene_file_path().is_empty()) { // cyclical instancing
  4798. if (_cyclical_dependency_exists(edited_scene->get_scene_file_path(), instantiated_scene)) {
  4799. memdelete(instantiated_scene);
  4800. return false;
  4801. }
  4802. }
  4803. instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(path));
  4804. editor_data->get_undo_redo().add_do_method(parent, "add_child", instantiated_scene, true);
  4805. editor_data->get_undo_redo().add_do_method(instantiated_scene, "set_owner", edited_scene);
  4806. editor_data->get_undo_redo().add_do_reference(instantiated_scene);
  4807. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instantiated_scene);
  4808. String new_name = parent->validate_child_name(instantiated_scene);
  4809. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  4810. editor_data->get_undo_redo().add_do_method(ed, "live_debug_instance_node", edited_scene->get_path_to(parent), path, new_name);
  4811. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(edited_scene->get_path_to(parent)) + "/" + new_name));
  4812. CanvasItem *instance_ci = Object::cast_to<CanvasItem>(instantiated_scene);
  4813. if (instance_ci) {
  4814. Vector2 target_pos = canvas_item_editor->get_canvas_transform().affine_inverse().xform(p_point);
  4815. target_pos = canvas_item_editor->snap_point(target_pos);
  4816. CanvasItem *parent_ci = Object::cast_to<CanvasItem>(parent);
  4817. if (parent_ci) {
  4818. target_pos = parent_ci->get_global_transform_with_canvas().affine_inverse().xform(target_pos);
  4819. }
  4820. // Preserve instance position of the original scene.
  4821. target_pos += instance_ci->_edit_get_position();
  4822. editor_data->get_undo_redo().add_do_method(instantiated_scene, "set_position", target_pos);
  4823. }
  4824. return true;
  4825. }
  4826. void CanvasItemEditorViewport::_perform_drop_data() {
  4827. _remove_preview();
  4828. // Without root dropping multiple files is not allowed
  4829. if (!target_node && selected_files.size() > 1) {
  4830. accept->set_text(TTR("Cannot instantiate multiple nodes without root."));
  4831. accept->popup_centered();
  4832. return;
  4833. }
  4834. Vector<String> error_files;
  4835. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  4836. for (int i = 0; i < selected_files.size(); i++) {
  4837. String path = selected_files[i];
  4838. RES res = ResourceLoader::load(path);
  4839. if (res.is_null()) {
  4840. continue;
  4841. }
  4842. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  4843. if (scene != nullptr && scene.is_valid()) {
  4844. if (!target_node) {
  4845. // Without root node act the same as "Load Inherited Scene"
  4846. Error err = EditorNode::get_singleton()->load_scene(path, false, true);
  4847. if (err != OK) {
  4848. error_files.push_back(path);
  4849. }
  4850. } else {
  4851. bool success = _create_instance(target_node, path, drop_pos);
  4852. if (!success) {
  4853. error_files.push_back(path);
  4854. }
  4855. }
  4856. } else {
  4857. Ref<Texture2D> texture = Ref<Texture2D>(Object::cast_to<Texture2D>(*res));
  4858. if (texture != nullptr && texture.is_valid()) {
  4859. Node *child = _make_texture_node_type(default_texture_node_type);
  4860. _create_nodes(target_node, child, path, drop_pos);
  4861. }
  4862. }
  4863. }
  4864. editor_data->get_undo_redo().commit_action();
  4865. if (error_files.size() > 0) {
  4866. String files_str;
  4867. for (int i = 0; i < error_files.size(); i++) {
  4868. files_str += error_files[i].get_file().get_basename() + ",";
  4869. }
  4870. files_str = files_str.substr(0, files_str.length() - 1);
  4871. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.get_data()));
  4872. accept->popup_centered();
  4873. }
  4874. }
  4875. bool CanvasItemEditorViewport::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  4876. Dictionary d = p_data;
  4877. if (d.has("type")) {
  4878. if (String(d["type"]) == "files") {
  4879. Vector<String> files = d["files"];
  4880. bool can_instantiate = false;
  4881. List<String> scene_extensions;
  4882. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  4883. List<String> texture_extensions;
  4884. ResourceLoader::get_recognized_extensions_for_type("Texture2D", &texture_extensions);
  4885. for (int i = 0; i < files.size(); i++) {
  4886. // Check if dragged files with texture or scene extension can be created at least once.
  4887. if (texture_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  4888. RES res = ResourceLoader::load(files[i]);
  4889. if (res.is_null()) {
  4890. continue;
  4891. }
  4892. Ref<PackedScene> scn = res;
  4893. if (scn.is_valid()) {
  4894. Node *instantiated_scene = scn->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  4895. if (!instantiated_scene) {
  4896. continue;
  4897. }
  4898. memdelete(instantiated_scene);
  4899. }
  4900. can_instantiate = true;
  4901. break;
  4902. }
  4903. }
  4904. if (can_instantiate) {
  4905. if (!preview_node->get_parent()) { // create preview only once
  4906. _create_preview(files);
  4907. }
  4908. Transform2D trans = canvas_item_editor->get_canvas_transform();
  4909. preview_node->set_position((p_point - trans.get_origin()) / trans.get_scale().x);
  4910. label->set_text(vformat(TTR("Adding %s..."), default_texture_node_type));
  4911. }
  4912. return can_instantiate;
  4913. }
  4914. }
  4915. label->hide();
  4916. return false;
  4917. }
  4918. void CanvasItemEditorViewport::_show_resource_type_selector() {
  4919. _remove_preview();
  4920. List<BaseButton *> btn_list;
  4921. button_group->get_buttons(&btn_list);
  4922. for (int i = 0; i < btn_list.size(); i++) {
  4923. CheckBox *check = Object::cast_to<CheckBox>(btn_list[i]);
  4924. check->set_pressed(check->get_text() == default_texture_node_type);
  4925. }
  4926. selector->set_title(vformat(TTR("Add %s"), default_texture_node_type));
  4927. selector->popup_centered();
  4928. }
  4929. bool CanvasItemEditorViewport::_only_packed_scenes_selected() const {
  4930. for (int i = 0; i < selected_files.size(); ++i) {
  4931. if (ResourceLoader::load(selected_files[i])->get_class() != "PackedScene") {
  4932. return false;
  4933. }
  4934. }
  4935. return true;
  4936. }
  4937. void CanvasItemEditorViewport::drop_data(const Point2 &p_point, const Variant &p_data) {
  4938. bool is_shift = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  4939. bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  4940. bool is_alt = Input::get_singleton()->is_key_pressed(Key::ALT);
  4941. selected_files.clear();
  4942. Dictionary d = p_data;
  4943. if (d.has("type") && String(d["type"]) == "files") {
  4944. selected_files = d["files"];
  4945. }
  4946. if (selected_files.size() == 0) {
  4947. return;
  4948. }
  4949. List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  4950. Node *root_node = EditorNode::get_singleton()->get_edited_scene();
  4951. if (selected_nodes.size() > 0) {
  4952. Node *selected_node = selected_nodes[0];
  4953. target_node = root_node;
  4954. if (is_ctrl) {
  4955. target_node = selected_node;
  4956. } else if (is_shift && selected_node != root_node) {
  4957. target_node = selected_node->get_parent();
  4958. }
  4959. } else {
  4960. if (root_node) {
  4961. target_node = root_node;
  4962. } else {
  4963. target_node = nullptr;
  4964. }
  4965. }
  4966. drop_pos = p_point;
  4967. if (is_alt && !_only_packed_scenes_selected()) {
  4968. _show_resource_type_selector();
  4969. } else {
  4970. _perform_drop_data();
  4971. }
  4972. }
  4973. Node *CanvasItemEditorViewport::_make_texture_node_type(String texture_node_type) {
  4974. Node *node = nullptr;
  4975. if (texture_node_type == "Sprite2D") {
  4976. node = memnew(Sprite2D);
  4977. } else if (texture_node_type == "PointLight2D") {
  4978. node = memnew(PointLight2D);
  4979. } else if (texture_node_type == "CPUParticles2D") {
  4980. node = memnew(CPUParticles2D);
  4981. } else if (texture_node_type == "GPUParticles2D") {
  4982. node = memnew(GPUParticles2D);
  4983. } else if (texture_node_type == "Polygon2D") {
  4984. node = memnew(Polygon2D);
  4985. } else if (texture_node_type == "TouchScreenButton") {
  4986. node = memnew(TouchScreenButton);
  4987. } else if (texture_node_type == "TextureRect") {
  4988. node = memnew(TextureRect);
  4989. } else if (texture_node_type == "TextureButton") {
  4990. node = memnew(TextureButton);
  4991. } else if (texture_node_type == "NinePatchRect") {
  4992. node = memnew(NinePatchRect);
  4993. }
  4994. return node;
  4995. }
  4996. void CanvasItemEditorViewport::_update_theme() {
  4997. List<BaseButton *> btn_list;
  4998. button_group->get_buttons(&btn_list);
  4999. for (int i = 0; i < btn_list.size(); i++) {
  5000. CheckBox *check = Object::cast_to<CheckBox>(btn_list[i]);
  5001. check->set_icon(get_theme_icon(check->get_text(), SNAME("EditorIcons")));
  5002. }
  5003. label->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  5004. }
  5005. void CanvasItemEditorViewport::_notification(int p_what) {
  5006. switch (p_what) {
  5007. case NOTIFICATION_THEME_CHANGED: {
  5008. _update_theme();
  5009. } break;
  5010. case NOTIFICATION_ENTER_TREE: {
  5011. _update_theme();
  5012. connect("mouse_exited", callable_mp(this, &CanvasItemEditorViewport::_on_mouse_exit));
  5013. } break;
  5014. case NOTIFICATION_EXIT_TREE: {
  5015. disconnect("mouse_exited", callable_mp(this, &CanvasItemEditorViewport::_on_mouse_exit));
  5016. } break;
  5017. }
  5018. }
  5019. void CanvasItemEditorViewport::_bind_methods() {
  5020. }
  5021. CanvasItemEditorViewport::CanvasItemEditorViewport(CanvasItemEditor *p_canvas_item_editor) {
  5022. default_texture_node_type = "Sprite2D";
  5023. // Node2D
  5024. texture_node_types.push_back("Sprite2D");
  5025. texture_node_types.push_back("PointLight2D");
  5026. texture_node_types.push_back("CPUParticles2D");
  5027. texture_node_types.push_back("GPUParticles2D");
  5028. texture_node_types.push_back("Polygon2D");
  5029. texture_node_types.push_back("TouchScreenButton");
  5030. // Control
  5031. texture_node_types.push_back("TextureRect");
  5032. texture_node_types.push_back("TextureButton");
  5033. texture_node_types.push_back("NinePatchRect");
  5034. target_node = nullptr;
  5035. editor_data = SceneTreeDock::get_singleton()->get_editor_data();
  5036. canvas_item_editor = p_canvas_item_editor;
  5037. preview_node = memnew(Control);
  5038. accept = memnew(AcceptDialog);
  5039. EditorNode::get_singleton()->get_gui_base()->add_child(accept);
  5040. selector = memnew(AcceptDialog);
  5041. EditorNode::get_singleton()->get_gui_base()->add_child(selector);
  5042. selector->set_title(TTR("Change Default Type"));
  5043. selector->connect("confirmed", callable_mp(this, &CanvasItemEditorViewport::_on_change_type_confirmed));
  5044. selector->connect("cancelled", callable_mp(this, &CanvasItemEditorViewport::_on_change_type_closed));
  5045. VBoxContainer *vbc = memnew(VBoxContainer);
  5046. selector->add_child(vbc);
  5047. vbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5048. vbc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5049. vbc->set_custom_minimum_size(Size2(240, 260) * EDSCALE);
  5050. btn_group = memnew(VBoxContainer);
  5051. vbc->add_child(btn_group);
  5052. btn_group->set_h_size_flags(SIZE_EXPAND_FILL);
  5053. button_group.instantiate();
  5054. for (int i = 0; i < texture_node_types.size(); i++) {
  5055. CheckBox *check = memnew(CheckBox);
  5056. btn_group->add_child(check);
  5057. check->set_text(texture_node_types[i]);
  5058. check->connect("button_down", callable_mp(this, &CanvasItemEditorViewport::_on_select_type), varray(check));
  5059. check->set_button_group(button_group);
  5060. }
  5061. label = memnew(Label);
  5062. label->add_theme_color_override("font_shadow_color", Color(0, 0, 0, 1));
  5063. label->add_theme_constant_override("shadow_outline_size", 1 * EDSCALE);
  5064. label->hide();
  5065. canvas_item_editor->get_controls_container()->add_child(label);
  5066. label_desc = memnew(Label);
  5067. label_desc->add_theme_color_override("font_color", Color(0.6f, 0.6f, 0.6f, 1));
  5068. label_desc->add_theme_color_override("font_shadow_color", Color(0.2f, 0.2f, 0.2f, 1));
  5069. label_desc->add_theme_constant_override("shadow_outline_size", 1 * EDSCALE);
  5070. label_desc->add_theme_constant_override("line_spacing", 0);
  5071. label_desc->hide();
  5072. canvas_item_editor->get_controls_container()->add_child(label_desc);
  5073. RS::get_singleton()->canvas_set_disable_scale(true);
  5074. }
  5075. CanvasItemEditorViewport::~CanvasItemEditorViewport() {
  5076. memdelete(preview_node);
  5077. }