laya.core.js 1006 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255
  1. window.Laya= (function (exports) {
  2. 'use strict';
  3. class Config {
  4. }
  5. Config.animationInterval = 50;
  6. Config.isAntialias = false;
  7. Config.isAlpha = false;
  8. Config.premultipliedAlpha = true;
  9. Config.isStencil = true;
  10. Config.preserveDrawingBuffer = true;
  11. Config.webGL2D_MeshAllocMaxMem = true;
  12. Config.is2DPixelArtGame = false;
  13. Config.useWebGL2 = false;
  14. Config.allowGPUInstanceDynamicBatch = true;
  15. Config.useRetinalCanvas = false;
  16. window.Config = Config;
  17. class ILaya {
  18. static regClass(c) {
  19. ILaya.__classMap[c.name] = c;
  20. }
  21. }
  22. ILaya.Laya = null;
  23. ILaya.Timer = null;
  24. ILaya.WorkerLoader = null;
  25. ILaya.Dragging = null;
  26. ILaya.GraphicsBounds = null;
  27. ILaya.Sprite = null;
  28. ILaya.TextRender = null;
  29. ILaya.TextAtlas = null;
  30. ILaya.timer = null;
  31. ILaya.systemTimer = null;
  32. ILaya.startTimer = null;
  33. ILaya.updateTimer = null;
  34. ILaya.lateTimer = null;
  35. ILaya.physicsTimer = null;
  36. ILaya.stage = null;
  37. ILaya.Loader = null;
  38. ILaya.loader = null;
  39. ILaya.TTFLoader = null;
  40. ILaya.SoundManager = null;
  41. ILaya.WebAudioSound = null;
  42. ILaya.AudioSound = null;
  43. ILaya.ShaderCompile = null;
  44. ILaya.ClassUtils = null;
  45. ILaya.SceneUtils = null;
  46. ILaya.Context = null;
  47. ILaya.Render = null;
  48. ILaya.MouseManager = null;
  49. ILaya.Text = null;
  50. ILaya.Browser = null;
  51. ILaya.WebGL = null;
  52. ILaya.Pool = null;
  53. ILaya.Utils = null;
  54. ILaya.Graphics = null;
  55. ILaya.Submit = null;
  56. ILaya.Stage = null;
  57. ILaya.Resource = null;
  58. ILaya.__classMap = {};
  59. class Pool {
  60. static getPoolBySign(sign) {
  61. return Pool._poolDic[sign] || (Pool._poolDic[sign] = []);
  62. }
  63. static clearBySign(sign) {
  64. if (Pool._poolDic[sign])
  65. Pool._poolDic[sign].length = 0;
  66. }
  67. static recover(sign, item) {
  68. if (item[Pool.POOLSIGN])
  69. return;
  70. item[Pool.POOLSIGN] = true;
  71. Pool.getPoolBySign(sign).push(item);
  72. }
  73. static recoverByClass(instance) {
  74. if (instance) {
  75. var className = instance["__className"] || instance.constructor._$gid;
  76. if (className)
  77. Pool.recover(className, instance);
  78. }
  79. }
  80. static _getClassSign(cla) {
  81. var className = cla["__className"] || cla["_$gid"];
  82. if (!className) {
  83. cla["_$gid"] = className = Pool._CLSID + "";
  84. Pool._CLSID++;
  85. }
  86. return className;
  87. }
  88. static createByClass(cls) {
  89. return Pool.getItemByClass(Pool._getClassSign(cls), cls);
  90. }
  91. static getItemByClass(sign, cls) {
  92. if (!Pool._poolDic[sign])
  93. return new cls();
  94. var pool = Pool.getPoolBySign(sign);
  95. if (pool.length) {
  96. var rst = pool.pop();
  97. rst[Pool.POOLSIGN] = false;
  98. }
  99. else {
  100. rst = new cls();
  101. }
  102. return rst;
  103. }
  104. static getItemByCreateFun(sign, createFun, caller = null) {
  105. var pool = Pool.getPoolBySign(sign);
  106. var rst = pool.length ? pool.pop() : createFun.call(caller);
  107. rst[Pool.POOLSIGN] = false;
  108. return rst;
  109. }
  110. static getItem(sign) {
  111. var pool = Pool.getPoolBySign(sign);
  112. var rst = pool.length ? pool.pop() : null;
  113. if (rst) {
  114. rst[Pool.POOLSIGN] = false;
  115. }
  116. return rst;
  117. }
  118. }
  119. Pool._CLSID = 0;
  120. Pool.POOLSIGN = "__InPool";
  121. Pool._poolDic = {};
  122. class AlphaCmd {
  123. static create(alpha) {
  124. var cmd = Pool.getItemByClass("AlphaCmd", AlphaCmd);
  125. cmd.alpha = alpha;
  126. return cmd;
  127. }
  128. recover() {
  129. Pool.recover("AlphaCmd", this);
  130. }
  131. run(context, gx, gy) {
  132. context.alpha(this.alpha);
  133. }
  134. get cmdID() {
  135. return AlphaCmd.ID;
  136. }
  137. }
  138. AlphaCmd.ID = "Alpha";
  139. class DrawCircleCmd {
  140. static create(x, y, radius, fillColor, lineColor, lineWidth, vid) {
  141. var cmd = Pool.getItemByClass("DrawCircleCmd", DrawCircleCmd);
  142. cmd.x = x;
  143. cmd.y = y;
  144. cmd.radius = radius;
  145. cmd.fillColor = fillColor;
  146. cmd.lineColor = lineColor;
  147. cmd.lineWidth = lineWidth;
  148. cmd.vid = vid;
  149. return cmd;
  150. }
  151. recover() {
  152. this.fillColor = null;
  153. this.lineColor = null;
  154. Pool.recover("DrawCircleCmd", this);
  155. }
  156. run(context, gx, gy) {
  157. context._drawCircle(this.x + gx, this.y + gy, this.radius, this.fillColor, this.lineColor, this.lineWidth, this.vid);
  158. }
  159. get cmdID() {
  160. return DrawCircleCmd.ID;
  161. }
  162. }
  163. DrawCircleCmd.ID = "DrawCircle";
  164. class DrawCurvesCmd {
  165. static create(x, y, points, lineColor, lineWidth) {
  166. var cmd = Pool.getItemByClass("DrawCurvesCmd", DrawCurvesCmd);
  167. cmd.x = x;
  168. cmd.y = y;
  169. cmd.points = points;
  170. cmd.lineColor = lineColor;
  171. cmd.lineWidth = lineWidth;
  172. return cmd;
  173. }
  174. recover() {
  175. this.points = null;
  176. this.lineColor = null;
  177. Pool.recover("DrawCurvesCmd", this);
  178. }
  179. run(context, gx, gy) {
  180. if (this.points)
  181. context.drawCurves(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth);
  182. }
  183. get cmdID() {
  184. return DrawCurvesCmd.ID;
  185. }
  186. }
  187. DrawCurvesCmd.ID = "DrawCurves";
  188. class DrawImageCmd {
  189. static create(texture, x, y, width, height) {
  190. var cmd = Pool.getItemByClass("DrawImageCmd", DrawImageCmd);
  191. cmd.texture = texture;
  192. texture._addReference();
  193. cmd.x = x;
  194. cmd.y = y;
  195. cmd.width = width;
  196. cmd.height = height;
  197. return cmd;
  198. }
  199. recover() {
  200. this.texture && this.texture._removeReference();
  201. this.texture = null;
  202. Pool.recover("DrawImageCmd", this);
  203. }
  204. run(context, gx, gy) {
  205. if (this.texture)
  206. context.drawTexture(this.texture, this.x + gx, this.y + gy, this.width, this.height);
  207. }
  208. get cmdID() {
  209. return DrawImageCmd.ID;
  210. }
  211. }
  212. DrawImageCmd.ID = "DrawImage";
  213. class DrawLineCmd {
  214. static create(fromX, fromY, toX, toY, lineColor, lineWidth, vid) {
  215. var cmd = Pool.getItemByClass("DrawLineCmd", DrawLineCmd);
  216. cmd.fromX = fromX;
  217. cmd.fromY = fromY;
  218. cmd.toX = toX;
  219. cmd.toY = toY;
  220. cmd.lineColor = lineColor;
  221. cmd.lineWidth = lineWidth;
  222. cmd.vid = vid;
  223. return cmd;
  224. }
  225. recover() {
  226. Pool.recover("DrawLineCmd", this);
  227. }
  228. run(context, gx, gy) {
  229. context._drawLine(gx, gy, this.fromX, this.fromY, this.toX, this.toY, this.lineColor, this.lineWidth, this.vid);
  230. }
  231. get cmdID() {
  232. return DrawLineCmd.ID;
  233. }
  234. }
  235. DrawLineCmd.ID = "DrawLine";
  236. class DrawLinesCmd {
  237. static create(x, y, points, lineColor, lineWidth, vid) {
  238. var cmd = Pool.getItemByClass("DrawLinesCmd", DrawLinesCmd);
  239. cmd.x = x;
  240. cmd.y = y;
  241. cmd.points = points;
  242. cmd.lineColor = lineColor;
  243. cmd.lineWidth = lineWidth;
  244. cmd.vid = vid;
  245. return cmd;
  246. }
  247. recover() {
  248. this.points = null;
  249. this.lineColor = null;
  250. Pool.recover("DrawLinesCmd", this);
  251. }
  252. run(context, gx, gy) {
  253. this.points && context._drawLines(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth, this.vid);
  254. }
  255. get cmdID() {
  256. return DrawLinesCmd.ID;
  257. }
  258. }
  259. DrawLinesCmd.ID = "DrawLines";
  260. class DrawPathCmd {
  261. static create(x, y, paths, brush, pen) {
  262. var cmd = Pool.getItemByClass("DrawPathCmd", DrawPathCmd);
  263. cmd.x = x;
  264. cmd.y = y;
  265. cmd.paths = paths;
  266. cmd.brush = brush;
  267. cmd.pen = pen;
  268. return cmd;
  269. }
  270. recover() {
  271. this.paths = null;
  272. this.brush = null;
  273. this.pen = null;
  274. Pool.recover("DrawPathCmd", this);
  275. }
  276. run(context, gx, gy) {
  277. this.paths && context._drawPath(this.x + gx, this.y + gy, this.paths, this.brush, this.pen);
  278. }
  279. get cmdID() {
  280. return DrawPathCmd.ID;
  281. }
  282. }
  283. DrawPathCmd.ID = "DrawPath";
  284. class DrawPieCmd {
  285. static create(x, y, radius, startAngle, endAngle, fillColor, lineColor, lineWidth, vid) {
  286. var cmd = Pool.getItemByClass("DrawPieCmd", DrawPieCmd);
  287. cmd.x = x;
  288. cmd.y = y;
  289. cmd.radius = radius;
  290. cmd._startAngle = startAngle;
  291. cmd._endAngle = endAngle;
  292. cmd.fillColor = fillColor;
  293. cmd.lineColor = lineColor;
  294. cmd.lineWidth = lineWidth;
  295. cmd.vid = vid;
  296. return cmd;
  297. }
  298. recover() {
  299. this.fillColor = null;
  300. this.lineColor = null;
  301. Pool.recover("DrawPieCmd", this);
  302. }
  303. run(context, gx, gy) {
  304. context._drawPie(this.x + gx, this.y + gy, this.radius, this._startAngle, this._endAngle, this.fillColor, this.lineColor, this.lineWidth, this.vid);
  305. }
  306. get cmdID() {
  307. return DrawPieCmd.ID;
  308. }
  309. get startAngle() {
  310. return this._startAngle * 180 / Math.PI;
  311. }
  312. set startAngle(value) {
  313. this._startAngle = value * Math.PI / 180;
  314. }
  315. get endAngle() {
  316. return this._endAngle * 180 / Math.PI;
  317. }
  318. set endAngle(value) {
  319. this._endAngle = value * Math.PI / 180;
  320. }
  321. }
  322. DrawPieCmd.ID = "DrawPie";
  323. class DrawPolyCmd {
  324. static create(x, y, points, fillColor, lineColor, lineWidth, isConvexPolygon, vid) {
  325. var cmd = Pool.getItemByClass("DrawPolyCmd", DrawPolyCmd);
  326. cmd.x = x;
  327. cmd.y = y;
  328. cmd.points = points;
  329. cmd.fillColor = fillColor;
  330. cmd.lineColor = lineColor;
  331. cmd.lineWidth = lineWidth;
  332. cmd.isConvexPolygon = isConvexPolygon;
  333. cmd.vid = vid;
  334. return cmd;
  335. }
  336. recover() {
  337. this.points = null;
  338. this.fillColor = null;
  339. this.lineColor = null;
  340. Pool.recover("DrawPolyCmd", this);
  341. }
  342. run(context, gx, gy) {
  343. this.points && context._drawPoly(this.x + gx, this.y + gy, this.points, this.fillColor, this.lineColor, this.lineWidth, this.isConvexPolygon, this.vid);
  344. }
  345. get cmdID() {
  346. return DrawPolyCmd.ID;
  347. }
  348. }
  349. DrawPolyCmd.ID = "DrawPoly";
  350. class DrawRectCmd {
  351. static create(x, y, width, height, fillColor, lineColor, lineWidth) {
  352. var cmd = Pool.getItemByClass("DrawRectCmd", DrawRectCmd);
  353. cmd.x = x;
  354. cmd.y = y;
  355. cmd.width = width;
  356. cmd.height = height;
  357. cmd.fillColor = fillColor;
  358. cmd.lineColor = lineColor;
  359. cmd.lineWidth = lineWidth;
  360. return cmd;
  361. }
  362. recover() {
  363. this.fillColor = null;
  364. this.lineColor = null;
  365. Pool.recover("DrawRectCmd", this);
  366. }
  367. run(context, gx, gy) {
  368. context.drawRect(this.x + gx, this.y + gy, this.width, this.height, this.fillColor, this.lineColor, this.lineWidth);
  369. }
  370. get cmdID() {
  371. return DrawRectCmd.ID;
  372. }
  373. }
  374. DrawRectCmd.ID = "DrawRect";
  375. class Matrix {
  376. constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0, nums = 0) {
  377. this._bTransform = false;
  378. if (Matrix._createFun != null) {
  379. return Matrix._createFun(a, b, c, d, tx, ty, nums);
  380. }
  381. this.a = a;
  382. this.b = b;
  383. this.c = c;
  384. this.d = d;
  385. this.tx = tx;
  386. this.ty = ty;
  387. this._checkTransform();
  388. }
  389. identity() {
  390. this.a = this.d = 1;
  391. this.b = this.tx = this.ty = this.c = 0;
  392. this._bTransform = false;
  393. return this;
  394. }
  395. _checkTransform() {
  396. return this._bTransform = (this.a !== 1 || this.b !== 0 || this.c !== 0 || this.d !== 1);
  397. }
  398. setTranslate(x, y) {
  399. this.tx = x;
  400. this.ty = y;
  401. return this;
  402. }
  403. translate(x, y) {
  404. this.tx += x;
  405. this.ty += y;
  406. return this;
  407. }
  408. scale(x, y) {
  409. this.a *= x;
  410. this.d *= y;
  411. this.c *= x;
  412. this.b *= y;
  413. this.tx *= x;
  414. this.ty *= y;
  415. this._bTransform = true;
  416. return this;
  417. }
  418. rotate(angle) {
  419. var cos = Math.cos(angle);
  420. var sin = Math.sin(angle);
  421. var a1 = this.a;
  422. var c1 = this.c;
  423. var tx1 = this.tx;
  424. this.a = a1 * cos - this.b * sin;
  425. this.b = a1 * sin + this.b * cos;
  426. this.c = c1 * cos - this.d * sin;
  427. this.d = c1 * sin + this.d * cos;
  428. this.tx = tx1 * cos - this.ty * sin;
  429. this.ty = tx1 * sin + this.ty * cos;
  430. this._bTransform = true;
  431. return this;
  432. }
  433. skew(x, y) {
  434. var tanX = Math.tan(x);
  435. var tanY = Math.tan(y);
  436. var a1 = this.a;
  437. var b1 = this.b;
  438. this.a += tanY * this.c;
  439. this.b += tanY * this.d;
  440. this.c += tanX * a1;
  441. this.d += tanX * b1;
  442. return this;
  443. }
  444. invertTransformPoint(out) {
  445. var a1 = this.a;
  446. var b1 = this.b;
  447. var c1 = this.c;
  448. var d1 = this.d;
  449. var tx1 = this.tx;
  450. var n = a1 * d1 - b1 * c1;
  451. var a2 = d1 / n;
  452. var b2 = -b1 / n;
  453. var c2 = -c1 / n;
  454. var d2 = a1 / n;
  455. var tx2 = (c1 * this.ty - d1 * tx1) / n;
  456. var ty2 = -(a1 * this.ty - b1 * tx1) / n;
  457. return out.setTo(a2 * out.x + c2 * out.y + tx2, b2 * out.x + d2 * out.y + ty2);
  458. }
  459. transformPoint(out) {
  460. return out.setTo(this.a * out.x + this.c * out.y + this.tx, this.b * out.x + this.d * out.y + this.ty);
  461. }
  462. transformPointN(out) {
  463. return out.setTo(this.a * out.x + this.c * out.y, this.b * out.x + this.d * out.y);
  464. }
  465. getScaleX() {
  466. return this.b === 0 ? this.a : Math.sqrt(this.a * this.a + this.b * this.b);
  467. }
  468. getScaleY() {
  469. return this.c === 0 ? this.d : Math.sqrt(this.c * this.c + this.d * this.d);
  470. }
  471. invert() {
  472. var a1 = this.a;
  473. var b1 = this.b;
  474. var c1 = this.c;
  475. var d1 = this.d;
  476. var tx1 = this.tx;
  477. var n = a1 * d1 - b1 * c1;
  478. this.a = d1 / n;
  479. this.b = -b1 / n;
  480. this.c = -c1 / n;
  481. this.d = a1 / n;
  482. this.tx = (c1 * this.ty - d1 * tx1) / n;
  483. this.ty = -(a1 * this.ty - b1 * tx1) / n;
  484. return this;
  485. }
  486. setTo(a, b, c, d, tx, ty) {
  487. this.a = a, this.b = b, this.c = c, this.d = d, this.tx = tx, this.ty = ty;
  488. return this;
  489. }
  490. concat(matrix) {
  491. var a = this.a;
  492. var c = this.c;
  493. var tx = this.tx;
  494. this.a = a * matrix.a + this.b * matrix.c;
  495. this.b = a * matrix.b + this.b * matrix.d;
  496. this.c = c * matrix.a + this.d * matrix.c;
  497. this.d = c * matrix.b + this.d * matrix.d;
  498. this.tx = tx * matrix.a + this.ty * matrix.c + matrix.tx;
  499. this.ty = tx * matrix.b + this.ty * matrix.d + matrix.ty;
  500. return this;
  501. }
  502. static mul(m1, m2, out) {
  503. var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
  504. var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
  505. if (bb !== 0 || bc !== 0) {
  506. out.a = aa * ba + ab * bc;
  507. out.b = aa * bb + ab * bd;
  508. out.c = ac * ba + ad * bc;
  509. out.d = ac * bb + ad * bd;
  510. out.tx = ba * atx + bc * aty + btx;
  511. out.ty = bb * atx + bd * aty + bty;
  512. }
  513. else {
  514. out.a = aa * ba;
  515. out.b = ab * bd;
  516. out.c = ac * ba;
  517. out.d = ad * bd;
  518. out.tx = ba * atx + btx;
  519. out.ty = bd * aty + bty;
  520. }
  521. return out;
  522. }
  523. static mul16(m1, m2, out) {
  524. var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
  525. var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
  526. if (bb !== 0 || bc !== 0) {
  527. out[0] = aa * ba + ab * bc;
  528. out[1] = aa * bb + ab * bd;
  529. out[4] = ac * ba + ad * bc;
  530. out[5] = ac * bb + ad * bd;
  531. out[12] = ba * atx + bc * aty + btx;
  532. out[13] = bb * atx + bd * aty + bty;
  533. }
  534. else {
  535. out[0] = aa * ba;
  536. out[1] = ab * bd;
  537. out[4] = ac * ba;
  538. out[5] = ad * bd;
  539. out[12] = ba * atx + btx;
  540. out[13] = bd * aty + bty;
  541. }
  542. return out;
  543. }
  544. scaleEx(x, y) {
  545. var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
  546. if (bb !== 0 || bc !== 0) {
  547. this.a = x * ba;
  548. this.b = x * bb;
  549. this.c = y * bc;
  550. this.d = y * bd;
  551. }
  552. else {
  553. this.a = x * ba;
  554. this.b = 0 * bd;
  555. this.c = 0 * ba;
  556. this.d = y * bd;
  557. }
  558. this._bTransform = true;
  559. }
  560. rotateEx(angle) {
  561. var cos = Math.cos(angle);
  562. var sin = Math.sin(angle);
  563. var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
  564. if (bb !== 0 || bc !== 0) {
  565. this.a = cos * ba + sin * bc;
  566. this.b = cos * bb + sin * bd;
  567. this.c = -sin * ba + cos * bc;
  568. this.d = -sin * bb + cos * bd;
  569. }
  570. else {
  571. this.a = cos * ba;
  572. this.b = sin * bd;
  573. this.c = -sin * ba;
  574. this.d = cos * bd;
  575. }
  576. this._bTransform = true;
  577. }
  578. clone() {
  579. var dec = Matrix.create();
  580. dec.a = this.a;
  581. dec.b = this.b;
  582. dec.c = this.c;
  583. dec.d = this.d;
  584. dec.tx = this.tx;
  585. dec.ty = this.ty;
  586. dec._bTransform = this._bTransform;
  587. return dec;
  588. }
  589. copyTo(dec) {
  590. dec.a = this.a;
  591. dec.b = this.b;
  592. dec.c = this.c;
  593. dec.d = this.d;
  594. dec.tx = this.tx;
  595. dec.ty = this.ty;
  596. dec._bTransform = this._bTransform;
  597. return dec;
  598. }
  599. toString() {
  600. return this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.tx + "," + this.ty;
  601. }
  602. destroy() {
  603. this.recover();
  604. }
  605. recover() {
  606. Pool.recover("Matrix", this.identity());
  607. }
  608. static create() {
  609. return Pool.getItemByClass("Matrix", Matrix);
  610. }
  611. }
  612. Matrix.EMPTY = new Matrix();
  613. Matrix.TEMP = new Matrix();
  614. Matrix._createFun = null;
  615. class Point {
  616. constructor(x = 0, y = 0) {
  617. this.x = x;
  618. this.y = y;
  619. }
  620. static create() {
  621. return Pool.getItemByClass("Point", Point);
  622. }
  623. setTo(x, y) {
  624. this.x = x;
  625. this.y = y;
  626. return this;
  627. }
  628. reset() {
  629. this.x = this.y = 0;
  630. return this;
  631. }
  632. recover() {
  633. Pool.recover("Point", this.reset());
  634. }
  635. distance(x, y) {
  636. return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
  637. }
  638. toString() {
  639. return this.x + "," + this.y;
  640. }
  641. normalize() {
  642. var d = Math.sqrt(this.x * this.x + this.y * this.y);
  643. if (d > 0) {
  644. var id = 1.0 / d;
  645. this.x *= id;
  646. this.y *= id;
  647. }
  648. }
  649. copy(point) {
  650. return this.setTo(point.x, point.y);
  651. }
  652. }
  653. Point.TEMP = new Point();
  654. Point.EMPTY = new Point();
  655. class Rectangle {
  656. constructor(x = 0, y = 0, width = 0, height = 0) {
  657. this.x = x;
  658. this.y = y;
  659. this.width = width;
  660. this.height = height;
  661. }
  662. get right() {
  663. return this.x + this.width;
  664. }
  665. get bottom() {
  666. return this.y + this.height;
  667. }
  668. setTo(x, y, width, height) {
  669. this.x = x;
  670. this.y = y;
  671. this.width = width;
  672. this.height = height;
  673. return this;
  674. }
  675. reset() {
  676. this.x = this.y = this.width = this.height = 0;
  677. return this;
  678. }
  679. recover() {
  680. if (this == Rectangle.TEMP || this == Rectangle.EMPTY) {
  681. console.log("recover Temp or Empty:", this);
  682. return;
  683. }
  684. Pool.recover("Rectangle", this.reset());
  685. }
  686. static create() {
  687. return Pool.getItemByClass("Rectangle", Rectangle);
  688. }
  689. copyFrom(source) {
  690. this.x = source.x;
  691. this.y = source.y;
  692. this.width = source.width;
  693. this.height = source.height;
  694. return this;
  695. }
  696. contains(x, y) {
  697. if (this.width <= 0 || this.height <= 0)
  698. return false;
  699. if (x >= this.x && x < this.right) {
  700. if (y >= this.y && y < this.bottom) {
  701. return true;
  702. }
  703. }
  704. return false;
  705. }
  706. intersects(rect) {
  707. return !(rect.x > (this.x + this.width) || (rect.x + rect.width) < this.x || rect.y > (this.y + this.height) || (rect.y + rect.height) < this.y);
  708. }
  709. intersection(rect, out = null) {
  710. if (!this.intersects(rect))
  711. return null;
  712. out || (out = new Rectangle());
  713. out.x = Math.max(this.x, rect.x);
  714. out.y = Math.max(this.y, rect.y);
  715. out.width = Math.min(this.right, rect.right) - out.x;
  716. out.height = Math.min(this.bottom, rect.bottom) - out.y;
  717. return out;
  718. }
  719. union(source, out = null) {
  720. out || (out = new Rectangle());
  721. this.clone(out);
  722. if (source.width <= 0 || source.height <= 0)
  723. return out;
  724. out.addPoint(source.x, source.y);
  725. out.addPoint(source.right, source.bottom);
  726. return this;
  727. }
  728. clone(out = null) {
  729. out || (out = new Rectangle());
  730. out.x = this.x;
  731. out.y = this.y;
  732. out.width = this.width;
  733. out.height = this.height;
  734. return out;
  735. }
  736. toString() {
  737. return this.x + "," + this.y + "," + this.width + "," + this.height;
  738. }
  739. equals(rect) {
  740. if (!rect || rect.x !== this.x || rect.y !== this.y || rect.width !== this.width || rect.height !== this.height)
  741. return false;
  742. return true;
  743. }
  744. addPoint(x, y) {
  745. this.x > x && (this.width += this.x - x, this.x = x);
  746. this.y > y && (this.height += this.y - y, this.y = y);
  747. if (this.width < x - this.x)
  748. this.width = x - this.x;
  749. if (this.height < y - this.y)
  750. this.height = y - this.y;
  751. return this;
  752. }
  753. _getBoundPoints() {
  754. var rst = Rectangle._temB;
  755. rst.length = 0;
  756. if (this.width == 0 || this.height == 0)
  757. return rst;
  758. rst.push(this.x, this.y, this.x + this.width, this.y, this.x, this.y + this.height, this.x + this.width, this.y + this.height);
  759. return rst;
  760. }
  761. static _getBoundPointS(x, y, width, height) {
  762. var rst = Rectangle._temA;
  763. rst.length = 0;
  764. if (width == 0 || height == 0)
  765. return rst;
  766. rst.push(x, y, x + width, y, x, y + height, x + width, y + height);
  767. return rst;
  768. }
  769. static _getWrapRec(pointList, rst = null) {
  770. if (!pointList || pointList.length < 1)
  771. return rst ? rst.setTo(0, 0, 0, 0) : Rectangle.TEMP.setTo(0, 0, 0, 0);
  772. rst = rst ? rst : Rectangle.create();
  773. var i, len = pointList.length, minX, maxX, minY, maxY, tPoint = Point.TEMP;
  774. minX = minY = 99999;
  775. maxX = maxY = -minX;
  776. for (i = 0; i < len; i += 2) {
  777. tPoint.x = pointList[i];
  778. tPoint.y = pointList[i + 1];
  779. minX = minX < tPoint.x ? minX : tPoint.x;
  780. minY = minY < tPoint.y ? minY : tPoint.y;
  781. maxX = maxX > tPoint.x ? maxX : tPoint.x;
  782. maxY = maxY > tPoint.y ? maxY : tPoint.y;
  783. }
  784. return rst.setTo(minX, minY, maxX - minX, maxY - minY);
  785. }
  786. isEmpty() {
  787. if (this.width <= 0 || this.height <= 0)
  788. return true;
  789. return false;
  790. }
  791. }
  792. Rectangle.EMPTY = new Rectangle();
  793. Rectangle.TEMP = new Rectangle();
  794. Rectangle._temB = [];
  795. Rectangle._temA = [];
  796. class LayaGL {
  797. }
  798. LayaGL.ARRAY_BUFFER_TYPE_DATA = 0;
  799. LayaGL.ARRAY_BUFFER_TYPE_CMD = 1;
  800. LayaGL.ARRAY_BUFFER_REF_REFERENCE = 0;
  801. LayaGL.ARRAY_BUFFER_REF_COPY = 1;
  802. LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_ID = 0;
  803. LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_DATA = 1;
  804. var _sFactor = 1;
  805. var _dFactor = 0;
  806. class WebGLContext {
  807. static __init__() {
  808. var gl = LayaGL.instance;
  809. WebGLContext._depthFunc = gl.LESS;
  810. WebGLContext._blendEquation = gl.FUNC_ADD;
  811. WebGLContext._blendEquationRGB = gl.FUNC_ADD;
  812. WebGLContext._blendEquationAlpha = gl.FUNC_ADD;
  813. _sFactor = gl.ONE;
  814. _dFactor = gl.ZERO;
  815. WebGLContext._sFactorAlpha = gl.ONE;
  816. WebGLContext._dFactorAlpha = gl.ZERO;
  817. WebGLContext._activedTextureID = gl.TEXTURE0;
  818. var maxTexturenum = gl.getParameter(gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS);
  819. WebGLContext._activeTextures = new Array(maxTexturenum);
  820. WebGLContext._glTextureIDs = [gl.TEXTURE0, gl.TEXTURE1, gl.TEXTURE2, gl.TEXTURE3, gl.TEXTURE4, gl.TEXTURE5, gl.TEXTURE6, gl.TEXTURE7, gl.TEXTURE8, gl.TEXTURE9, gl.TEXTURE10, gl.TEXTURE11, gl.TEXTURE12, gl.TEXTURE13, gl.TEXTURE14, gl.TEXTURE15, gl.TEXTURE16, gl.TEXTURE17, gl.TEXTURE18, gl.TEXTURE19, gl.TEXTURE20, gl.TEXTURE21, gl.TEXTURE22, gl.TEXTURE23, gl.TEXTURE24, gl.TEXTURE25, gl.TEXTURE26, gl.TEXTURE27, gl.TEXTURE28, gl.TEXTURE29, gl.TEXTURE30, gl.TEXTURE31];
  821. }
  822. static useProgram(gl, program) {
  823. if (WebGLContext._useProgram === program)
  824. return false;
  825. gl.useProgram(program);
  826. WebGLContext._useProgram = program;
  827. return true;
  828. }
  829. static setDepthTest(gl, value) {
  830. value !== WebGLContext._depthTest && (WebGLContext._depthTest = value, value ? gl.enable(gl.DEPTH_TEST) : gl.disable(gl.DEPTH_TEST));
  831. }
  832. static setDepthMask(gl, value) {
  833. value !== WebGLContext._depthMask && (WebGLContext._depthMask = value, gl.depthMask(value));
  834. }
  835. static setDepthFunc(gl, value) {
  836. value !== WebGLContext._depthFunc && (WebGLContext._depthFunc = value, gl.depthFunc(value));
  837. }
  838. static setBlend(gl, value) {
  839. value !== WebGLContext._blend && (WebGLContext._blend = value, value ? gl.enable(gl.BLEND) : gl.disable(gl.BLEND));
  840. }
  841. static setBlendEquation(gl, blendEquation) {
  842. if (blendEquation !== WebGLContext._blendEquation) {
  843. WebGLContext._blendEquation = blendEquation;
  844. WebGLContext._blendEquationRGB = WebGLContext._blendEquationAlpha = null;
  845. gl.blendEquation(blendEquation);
  846. }
  847. }
  848. static setBlendEquationSeparate(gl, blendEquationRGB, blendEquationAlpha) {
  849. if (blendEquationRGB !== WebGLContext._blendEquationRGB || blendEquationAlpha !== WebGLContext._blendEquationAlpha) {
  850. WebGLContext._blendEquationRGB = blendEquationRGB;
  851. WebGLContext._blendEquationAlpha = blendEquationAlpha;
  852. WebGLContext._blendEquation = null;
  853. gl.blendEquationSeparate(blendEquationRGB, blendEquationAlpha);
  854. }
  855. }
  856. static setBlendFunc(gl, sFactor, dFactor, force = false) {
  857. if (force || sFactor !== _sFactor || dFactor !== _dFactor) {
  858. _sFactor = sFactor;
  859. _dFactor = dFactor;
  860. WebGLContext._sFactorRGB = null;
  861. WebGLContext._dFactorRGB = null;
  862. WebGLContext._sFactorAlpha = null;
  863. WebGLContext._dFactorAlpha = null;
  864. gl.blendFunc(sFactor, dFactor);
  865. }
  866. }
  867. static setBlendFuncSeperate(gl, srcRGB, dstRGB, srcAlpha, dstAlpha) {
  868. if (srcRGB !== WebGLContext._sFactorRGB || dstRGB !== WebGLContext._dFactorRGB || srcAlpha !== WebGLContext._sFactorAlpha || dstAlpha !== WebGLContext._dFactorAlpha) {
  869. WebGLContext._sFactorRGB = srcRGB;
  870. WebGLContext._dFactorRGB = dstRGB;
  871. WebGLContext._sFactorAlpha = srcAlpha;
  872. WebGLContext._dFactorAlpha = dstAlpha;
  873. _sFactor = null;
  874. _dFactor = null;
  875. gl.blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
  876. }
  877. }
  878. static setCullFace(gl, value) {
  879. value !== WebGLContext._cullFace && (WebGLContext._cullFace = value, value ? gl.enable(gl.CULL_FACE) : gl.disable(gl.CULL_FACE));
  880. }
  881. static setFrontFace(gl, value) {
  882. value !== WebGLContext._frontFace && (WebGLContext._frontFace = value, gl.frontFace(value));
  883. }
  884. static activeTexture(gl, textureID) {
  885. if (WebGLContext._activedTextureID !== textureID) {
  886. gl.activeTexture(textureID);
  887. WebGLContext._activedTextureID = textureID;
  888. }
  889. }
  890. static bindTexture(gl, target, texture) {
  891. if (WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] !== texture) {
  892. gl.bindTexture(target, texture);
  893. WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] = texture;
  894. }
  895. }
  896. static __init_native() {
  897. if (!ILaya.Render.supportWebGLPlusRendering)
  898. return;
  899. var webGLContext = WebGLContext;
  900. webGLContext.activeTexture = webGLContext.activeTextureForNative;
  901. webGLContext.bindTexture = webGLContext.bindTextureForNative;
  902. }
  903. static useProgramForNative(gl, program) {
  904. gl.useProgram(program);
  905. return true;
  906. }
  907. static setDepthTestForNative(gl, value) {
  908. if (value)
  909. gl.enable(gl.DEPTH_TEST);
  910. else
  911. gl.disable(gl.DEPTH_TEST);
  912. }
  913. static setDepthMaskForNative(gl, value) {
  914. gl.depthMask(value);
  915. }
  916. static setDepthFuncForNative(gl, value) {
  917. gl.depthFunc(value);
  918. }
  919. static setBlendForNative(gl, value) {
  920. if (value)
  921. gl.enable(gl.BLEND);
  922. else
  923. gl.disable(gl.BLEND);
  924. }
  925. static setBlendFuncForNative(gl, sFactor, dFactor) {
  926. gl.blendFunc(sFactor, dFactor);
  927. }
  928. static setCullFaceForNative(gl, value) {
  929. if (value)
  930. gl.enable(gl.CULL_FACE);
  931. else
  932. gl.disable(gl.CULL_FACE);
  933. }
  934. static setFrontFaceForNative(gl, value) {
  935. gl.frontFace(value);
  936. }
  937. static activeTextureForNative(gl, textureID) {
  938. gl.activeTexture(textureID);
  939. }
  940. static bindTextureForNative(gl, target, texture) {
  941. gl.bindTexture(target, texture);
  942. }
  943. static bindVertexArrayForNative(gl, vertexArray) {
  944. gl.bindVertexArray(vertexArray);
  945. }
  946. }
  947. WebGLContext._activeTextures = new Array(1);
  948. WebGLContext._useProgram = null;
  949. WebGLContext._depthTest = true;
  950. WebGLContext._depthMask = true;
  951. WebGLContext._blend = false;
  952. WebGLContext._cullFace = false;
  953. WebGLContext.mainContext = null;
  954. class Handler {
  955. constructor(caller = null, method = null, args = null, once = false) {
  956. this.once = false;
  957. this._id = 0;
  958. this.setTo(caller, method, args, once);
  959. }
  960. setTo(caller, method, args, once = false) {
  961. this._id = Handler._gid++;
  962. this.caller = caller;
  963. this.method = method;
  964. this.args = args;
  965. this.once = once;
  966. return this;
  967. }
  968. run() {
  969. if (this.method == null)
  970. return null;
  971. var id = this._id;
  972. var result = this.method.apply(this.caller, this.args);
  973. this._id === id && this.once && this.recover();
  974. return result;
  975. }
  976. runWith(data) {
  977. if (this.method == null)
  978. return null;
  979. var id = this._id;
  980. if (data == null)
  981. var result = this.method.apply(this.caller, this.args);
  982. else if (!this.args && !data.unshift)
  983. result = this.method.call(this.caller, data);
  984. else if (this.args)
  985. result = this.method.apply(this.caller, this.args.concat(data));
  986. else
  987. result = this.method.apply(this.caller, data);
  988. this._id === id && this.once && this.recover();
  989. return result;
  990. }
  991. clear() {
  992. this.caller = null;
  993. this.method = null;
  994. this.args = null;
  995. return this;
  996. }
  997. recover() {
  998. if (this._id > 0) {
  999. this._id = 0;
  1000. Handler._pool.push(this.clear());
  1001. }
  1002. }
  1003. static create(caller, method, args = null, once = true) {
  1004. if (Handler._pool.length)
  1005. return Handler._pool.pop().setTo(caller, method, args, once);
  1006. return new Handler(caller, method, args, once);
  1007. }
  1008. }
  1009. Handler._pool = [];
  1010. Handler._gid = 1;
  1011. class EventDispatcher {
  1012. hasListener(type) {
  1013. var listener = this._events && this._events[type];
  1014. return !!listener;
  1015. }
  1016. event(type, data = null) {
  1017. if (!this._events || !this._events[type])
  1018. return false;
  1019. var listeners = this._events[type];
  1020. if (listeners.run) {
  1021. if (listeners.once)
  1022. delete this._events[type];
  1023. data != null ? listeners.runWith(data) : listeners.run();
  1024. }
  1025. else {
  1026. for (var i = 0, n = listeners.length; i < n; i++) {
  1027. var listener = listeners[i];
  1028. if (listener) {
  1029. (data != null) ? listener.runWith(data) : listener.run();
  1030. }
  1031. if (!listener || listener.once) {
  1032. listeners.splice(i, 1);
  1033. i--;
  1034. n--;
  1035. }
  1036. }
  1037. if (listeners.length === 0 && this._events)
  1038. delete this._events[type];
  1039. }
  1040. return true;
  1041. }
  1042. on(type, caller, listener, args = null) {
  1043. return this._createListener(type, caller, listener, args, false);
  1044. }
  1045. once(type, caller, listener, args = null) {
  1046. return this._createListener(type, caller, listener, args, true);
  1047. }
  1048. _createListener(type, caller, listener, args, once, offBefore = true) {
  1049. offBefore && this.off(type, caller, listener, once);
  1050. var handler = EventHandler.create(caller || this, listener, args, once);
  1051. this._events || (this._events = {});
  1052. var events = this._events;
  1053. if (!events[type])
  1054. events[type] = handler;
  1055. else {
  1056. if (!events[type].run)
  1057. events[type].push(handler);
  1058. else
  1059. events[type] = [events[type], handler];
  1060. }
  1061. return this;
  1062. }
  1063. off(type, caller, listener, onceOnly = false) {
  1064. if (!this._events || !this._events[type])
  1065. return this;
  1066. var listeners = this._events[type];
  1067. if (listeners != null) {
  1068. if (listeners.run) {
  1069. if ((!caller || listeners.caller === caller) && (listener == null || listeners.method === listener) && (!onceOnly || listeners.once)) {
  1070. delete this._events[type];
  1071. listeners.recover();
  1072. }
  1073. }
  1074. else {
  1075. var count = 0;
  1076. for (var i = 0, n = listeners.length; i < n; i++) {
  1077. var item = listeners[i];
  1078. if (!item) {
  1079. count++;
  1080. continue;
  1081. }
  1082. if (item && (!caller || item.caller === caller) && (listener == null || item.method === listener) && (!onceOnly || item.once)) {
  1083. count++;
  1084. listeners[i] = null;
  1085. item.recover();
  1086. }
  1087. }
  1088. if (count === n)
  1089. delete this._events[type];
  1090. }
  1091. }
  1092. return this;
  1093. }
  1094. offAll(type = null) {
  1095. var events = this._events;
  1096. if (!events)
  1097. return this;
  1098. if (type) {
  1099. this._recoverHandlers(events[type]);
  1100. delete events[type];
  1101. }
  1102. else {
  1103. for (var name in events) {
  1104. this._recoverHandlers(events[name]);
  1105. }
  1106. this._events = null;
  1107. }
  1108. return this;
  1109. }
  1110. offAllCaller(caller) {
  1111. if (caller && this._events) {
  1112. for (var name in this._events) {
  1113. this.off(name, caller, null);
  1114. }
  1115. }
  1116. return this;
  1117. }
  1118. _recoverHandlers(arr) {
  1119. if (!arr)
  1120. return;
  1121. if (arr.run) {
  1122. arr.recover();
  1123. }
  1124. else {
  1125. for (var i = arr.length - 1; i > -1; i--) {
  1126. if (arr[i]) {
  1127. arr[i].recover();
  1128. arr[i] = null;
  1129. }
  1130. }
  1131. }
  1132. }
  1133. isMouseEvent(type) {
  1134. return EventDispatcher.MOUSE_EVENTS[type] || false;
  1135. }
  1136. }
  1137. EventDispatcher.MOUSE_EVENTS = { "rightmousedown": true, "rightmouseup": true, "rightclick": true, "mousedown": true, "mouseup": true, "mousemove": true, "mouseover": true, "mouseout": true, "click": true, "doubleclick": true };
  1138. class EventHandler extends Handler {
  1139. constructor(caller, method, args, once) {
  1140. super(caller, method, args, once);
  1141. }
  1142. recover() {
  1143. if (this._id > 0) {
  1144. this._id = 0;
  1145. EventHandler._pool.push(this.clear());
  1146. }
  1147. }
  1148. static create(caller, method, args = null, once = true) {
  1149. if (EventHandler._pool.length)
  1150. return EventHandler._pool.pop().setTo(caller, method, args, once);
  1151. return new EventHandler(caller, method, args, once);
  1152. }
  1153. }
  1154. EventHandler._pool = [];
  1155. class URL {
  1156. constructor(url) {
  1157. this._url = URL.formatURL(url);
  1158. this._path = URL.getPath(url);
  1159. }
  1160. get url() {
  1161. return this._url;
  1162. }
  1163. get path() {
  1164. return this._path;
  1165. }
  1166. static set basePath(value) {
  1167. URL._basePath = ILaya.Laya._getUrlPath();
  1168. URL._basePath = URL.formatURL(value);
  1169. }
  1170. static get basePath() {
  1171. return URL._basePath;
  1172. }
  1173. static formatURL(url) {
  1174. if (!url)
  1175. return "null path";
  1176. if (url.indexOf(":") > 0)
  1177. return url;
  1178. if (URL.customFormat != null)
  1179. url = URL.customFormat(url);
  1180. if (url.indexOf(":") > 0)
  1181. return url;
  1182. var char1 = url.charAt(0);
  1183. if (char1 === ".") {
  1184. return URL._formatRelativePath(URL._basePath + url);
  1185. }
  1186. else if (char1 === '~') {
  1187. return URL.rootPath + url.substring(1);
  1188. }
  1189. else if (char1 === "d") {
  1190. if (url.indexOf("data:image") === 0)
  1191. return url;
  1192. }
  1193. else if (char1 === "/") {
  1194. return url;
  1195. }
  1196. return URL._basePath + url;
  1197. }
  1198. static _formatRelativePath(value) {
  1199. var parts = value.split("/");
  1200. for (var i = 0, len = parts.length; i < len; i++) {
  1201. if (parts[i] == '..') {
  1202. parts.splice(i - 1, 2);
  1203. i -= 2;
  1204. }
  1205. }
  1206. return parts.join('/');
  1207. }
  1208. static getPath(url) {
  1209. var ofs = url.lastIndexOf('/');
  1210. return ofs > 0 ? url.substr(0, ofs + 1) : "";
  1211. }
  1212. static getFileName(url) {
  1213. var ofs = url.lastIndexOf('/');
  1214. return ofs > 0 ? url.substr(ofs + 1) : url;
  1215. }
  1216. static getAdptedFilePath(url) {
  1217. if (!URL.exportSceneToJson || !url)
  1218. return url;
  1219. var i, len;
  1220. len = URL._adpteTypeList.length;
  1221. var tArr;
  1222. for (i = 0; i < len; i++) {
  1223. tArr = URL._adpteTypeList[i];
  1224. url = url.replace(tArr[0], tArr[1]);
  1225. }
  1226. return url;
  1227. }
  1228. }
  1229. URL.version = {};
  1230. URL.exportSceneToJson = false;
  1231. URL._basePath = "";
  1232. URL.rootPath = "";
  1233. URL.customFormat = function (url) {
  1234. var newUrl = URL.version[url];
  1235. if (!window.conch && newUrl)
  1236. url += "?v=" + newUrl;
  1237. return url;
  1238. };
  1239. URL._adpteTypeList = [[".scene3d", ".json"], [".scene", ".json"], [".taa", ".json"], [".prefab", ".json"]];
  1240. class Resource extends EventDispatcher {
  1241. constructor() {
  1242. super();
  1243. this._id = 0;
  1244. this._url = null;
  1245. this._cpuMemory = 0;
  1246. this._gpuMemory = 0;
  1247. this._destroyed = false;
  1248. this._referenceCount = 0;
  1249. this.lock = false;
  1250. this.name = null;
  1251. this._id = ++Resource._uniqueIDCounter;
  1252. this._destroyed = false;
  1253. this._referenceCount = 0;
  1254. Resource._idResourcesMap[this.id] = this;
  1255. this.lock = false;
  1256. }
  1257. static get cpuMemory() {
  1258. return Resource._cpuMemory;
  1259. }
  1260. static get gpuMemory() {
  1261. return Resource._gpuMemory;
  1262. }
  1263. static _addCPUMemory(size) {
  1264. Resource._cpuMemory += size;
  1265. }
  1266. static _addGPUMemory(size) {
  1267. Resource._gpuMemory += size;
  1268. }
  1269. static _addMemory(cpuSize, gpuSize) {
  1270. Resource._cpuMemory += cpuSize;
  1271. Resource._gpuMemory += gpuSize;
  1272. }
  1273. static getResourceByID(id) {
  1274. return Resource._idResourcesMap[id];
  1275. }
  1276. static getResourceByURL(url, index = 0) {
  1277. return Resource._urlResourcesMap[url][index];
  1278. }
  1279. static destroyUnusedResources() {
  1280. for (var k in Resource._idResourcesMap) {
  1281. var res = Resource._idResourcesMap[k];
  1282. if (!res.lock && res._referenceCount === 0)
  1283. res.destroy();
  1284. }
  1285. }
  1286. get id() {
  1287. return this._id;
  1288. }
  1289. get url() {
  1290. return this._url;
  1291. }
  1292. get cpuMemory() {
  1293. return this._cpuMemory;
  1294. }
  1295. get gpuMemory() {
  1296. return this._gpuMemory;
  1297. }
  1298. get destroyed() {
  1299. return this._destroyed;
  1300. }
  1301. get referenceCount() {
  1302. return this._referenceCount;
  1303. }
  1304. _setCPUMemory(value) {
  1305. var offsetValue = value - this._cpuMemory;
  1306. this._cpuMemory = value;
  1307. Resource._addCPUMemory(offsetValue);
  1308. }
  1309. _setGPUMemory(value) {
  1310. var offsetValue = value - this._gpuMemory;
  1311. this._gpuMemory = value;
  1312. Resource._addGPUMemory(offsetValue);
  1313. }
  1314. _setCreateURL(url) {
  1315. url = URL.formatURL(url);
  1316. if (this._url !== url) {
  1317. var resList;
  1318. if (this._url) {
  1319. resList = Resource._urlResourcesMap[this._url];
  1320. resList.splice(resList.indexOf(this), 1);
  1321. (resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
  1322. }
  1323. if (url) {
  1324. resList = Resource._urlResourcesMap[url];
  1325. (resList) || (Resource._urlResourcesMap[url] = resList = []);
  1326. resList.push(this);
  1327. }
  1328. this._url = url;
  1329. }
  1330. }
  1331. _addReference(count = 1) {
  1332. this._referenceCount += count;
  1333. }
  1334. _removeReference(count = 1) {
  1335. this._referenceCount -= count;
  1336. }
  1337. _clearReference() {
  1338. this._referenceCount = 0;
  1339. }
  1340. _recoverResource() {
  1341. }
  1342. _disposeResource() {
  1343. }
  1344. _activeResource() {
  1345. }
  1346. destroy() {
  1347. if (this._destroyed)
  1348. return;
  1349. this._destroyed = true;
  1350. this.lock = false;
  1351. this._disposeResource();
  1352. delete Resource._idResourcesMap[this.id];
  1353. var resList;
  1354. if (this._url) {
  1355. resList = Resource._urlResourcesMap[this._url];
  1356. if (resList) {
  1357. resList.splice(resList.indexOf(this), 1);
  1358. (resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
  1359. }
  1360. var resou = ILaya.Loader.loadedMap[this._url];
  1361. (resou == this) && (delete ILaya.Loader.loadedMap[this._url]);
  1362. }
  1363. }
  1364. }
  1365. Resource._uniqueIDCounter = 0;
  1366. Resource._idResourcesMap = {};
  1367. Resource._urlResourcesMap = {};
  1368. Resource._cpuMemory = 0;
  1369. Resource._gpuMemory = 0;
  1370. class Bitmap extends Resource {
  1371. constructor() {
  1372. super();
  1373. this._width = -1;
  1374. this._height = -1;
  1375. }
  1376. get width() {
  1377. return this._width;
  1378. }
  1379. set width(width) {
  1380. this._width = width;
  1381. }
  1382. get height() {
  1383. return this._height;
  1384. }
  1385. set height(height) {
  1386. this._height = height;
  1387. }
  1388. _getSource() {
  1389. throw "Bitmap: must override it.";
  1390. }
  1391. }
  1392. (function (FilterMode) {
  1393. FilterMode[FilterMode["Point"] = 0] = "Point";
  1394. FilterMode[FilterMode["Bilinear"] = 1] = "Bilinear";
  1395. FilterMode[FilterMode["Trilinear"] = 2] = "Trilinear";
  1396. })(exports.FilterMode || (exports.FilterMode = {}));
  1397. (function (TextureFormat) {
  1398. TextureFormat[TextureFormat["R8G8B8"] = 0] = "R8G8B8";
  1399. TextureFormat[TextureFormat["R8G8B8A8"] = 1] = "R8G8B8A8";
  1400. TextureFormat[TextureFormat["R5G6B5"] = 16] = "R5G6B5";
  1401. TextureFormat[TextureFormat["Alpha8"] = 2] = "Alpha8";
  1402. TextureFormat[TextureFormat["DXT1"] = 3] = "DXT1";
  1403. TextureFormat[TextureFormat["DXT5"] = 4] = "DXT5";
  1404. TextureFormat[TextureFormat["ETC1RGB"] = 5] = "ETC1RGB";
  1405. TextureFormat[TextureFormat["PVRTCRGB_2BPPV"] = 9] = "PVRTCRGB_2BPPV";
  1406. TextureFormat[TextureFormat["PVRTCRGBA_2BPPV"] = 10] = "PVRTCRGBA_2BPPV";
  1407. TextureFormat[TextureFormat["PVRTCRGB_4BPPV"] = 11] = "PVRTCRGB_4BPPV";
  1408. TextureFormat[TextureFormat["PVRTCRGBA_4BPPV"] = 12] = "PVRTCRGBA_4BPPV";
  1409. TextureFormat[TextureFormat["R32G32B32A32"] = 15] = "R32G32B32A32";
  1410. TextureFormat[TextureFormat["R16G16B16A16"] = 17] = "R16G16B16A16";
  1411. })(exports.TextureFormat || (exports.TextureFormat = {}));
  1412. (function (WarpMode) {
  1413. WarpMode[WarpMode["Repeat"] = 0] = "Repeat";
  1414. WarpMode[WarpMode["Clamp"] = 1] = "Clamp";
  1415. })(exports.WarpMode || (exports.WarpMode = {}));
  1416. class BaseTexture extends Bitmap {
  1417. constructor(format, mipMap) {
  1418. super();
  1419. this._wrapModeU = exports.WarpMode.Repeat;
  1420. this._wrapModeV = exports.WarpMode.Repeat;
  1421. this._filterMode = exports.FilterMode.Bilinear;
  1422. this._readyed = false;
  1423. this._width = -1;
  1424. this._height = -1;
  1425. this._format = format;
  1426. this._mipmap = mipMap;
  1427. this._anisoLevel = 1;
  1428. this._glTexture = LayaGL.instance.createTexture();
  1429. }
  1430. get mipmap() {
  1431. return this._mipmap;
  1432. }
  1433. get format() {
  1434. return this._format;
  1435. }
  1436. get wrapModeU() {
  1437. return this._wrapModeU;
  1438. }
  1439. set wrapModeU(value) {
  1440. if (this._wrapModeU !== value) {
  1441. this._wrapModeU = value;
  1442. (this._width !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_S, value));
  1443. }
  1444. }
  1445. get wrapModeV() {
  1446. return this._wrapModeV;
  1447. }
  1448. set wrapModeV(value) {
  1449. if (this._wrapModeV !== value) {
  1450. this._wrapModeV = value;
  1451. (this._height !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_T, value));
  1452. }
  1453. }
  1454. get filterMode() {
  1455. return this._filterMode;
  1456. }
  1457. set filterMode(value) {
  1458. if (value !== this._filterMode) {
  1459. this._filterMode = value;
  1460. ((this._width !== -1) && (this._height !== -1)) && (this._setFilterMode(value));
  1461. }
  1462. }
  1463. get anisoLevel() {
  1464. return this._anisoLevel;
  1465. }
  1466. set anisoLevel(value) {
  1467. if (value !== this._anisoLevel) {
  1468. this._anisoLevel = Math.max(1, Math.min(16, value));
  1469. ((this._width !== -1) && (this._height !== -1)) && (this._setAnisotropy(value));
  1470. }
  1471. }
  1472. get mipmapCount() {
  1473. return this._mipmapCount;
  1474. }
  1475. get defaulteTexture() {
  1476. throw "BaseTexture:must override it.";
  1477. }
  1478. _getFormatByteCount() {
  1479. switch (this._format) {
  1480. case exports.TextureFormat.R8G8B8:
  1481. return 3;
  1482. case exports.TextureFormat.R8G8B8A8:
  1483. return 4;
  1484. case exports.TextureFormat.R5G6B5:
  1485. return 1;
  1486. case exports.TextureFormat.Alpha8:
  1487. return 1;
  1488. case exports.TextureFormat.R16G16B16A16:
  1489. return 2;
  1490. case exports.TextureFormat.R32G32B32A32:
  1491. return 4;
  1492. default:
  1493. throw "Texture2D: unknown format.";
  1494. }
  1495. }
  1496. _isPot(size) {
  1497. return (size & (size - 1)) === 0;
  1498. }
  1499. _getGLFormat() {
  1500. var glFormat;
  1501. var gl = LayaGL.instance;
  1502. var gpu = LayaGL.layaGPUInstance;
  1503. switch (this._format) {
  1504. case exports.TextureFormat.R8G8B8:
  1505. case exports.TextureFormat.R5G6B5:
  1506. glFormat = gl.RGB;
  1507. break;
  1508. case exports.TextureFormat.R8G8B8A8:
  1509. glFormat = gl.RGBA;
  1510. break;
  1511. case exports.TextureFormat.Alpha8:
  1512. glFormat = gl.ALPHA;
  1513. break;
  1514. case exports.TextureFormat.R32G32B32A32:
  1515. case exports.TextureFormat.R16G16B16A16:
  1516. glFormat = gl.RGBA;
  1517. break;
  1518. case exports.TextureFormat.DXT1:
  1519. if (gpu._compressedTextureS3tc)
  1520. glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGB_S3TC_DXT1_EXT;
  1521. else
  1522. throw "BaseTexture: not support DXT1 format.";
  1523. break;
  1524. case exports.TextureFormat.DXT5:
  1525. if (gpu._compressedTextureS3tc)
  1526. glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  1527. else
  1528. throw "BaseTexture: not support DXT5 format.";
  1529. break;
  1530. case exports.TextureFormat.ETC1RGB:
  1531. if (gpu._compressedTextureEtc1)
  1532. glFormat = gpu._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL;
  1533. else
  1534. throw "BaseTexture: not support ETC1RGB format.";
  1535. break;
  1536. case exports.TextureFormat.PVRTCRGB_2BPPV:
  1537. if (gpu._compressedTexturePvrtc)
  1538. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  1539. else
  1540. throw "BaseTexture: not support PVRTCRGB_2BPPV format.";
  1541. break;
  1542. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  1543. if (gpu._compressedTexturePvrtc)
  1544. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  1545. else
  1546. throw "BaseTexture: not support PVRTCRGBA_2BPPV format.";
  1547. break;
  1548. case exports.TextureFormat.PVRTCRGB_4BPPV:
  1549. if (gpu._compressedTexturePvrtc)
  1550. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  1551. else
  1552. throw "BaseTexture: not support PVRTCRGB_4BPPV format.";
  1553. break;
  1554. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  1555. if (gpu._compressedTexturePvrtc)
  1556. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  1557. else
  1558. throw "BaseTexture: not support PVRTCRGBA_4BPPV format.";
  1559. break;
  1560. default:
  1561. throw "BaseTexture: unknown texture format.";
  1562. }
  1563. return glFormat;
  1564. }
  1565. _setFilterMode(value) {
  1566. var gl = LayaGL.instance;
  1567. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1568. switch (value) {
  1569. case exports.FilterMode.Point:
  1570. if (this._mipmap)
  1571. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
  1572. else
  1573. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  1574. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  1575. break;
  1576. case exports.FilterMode.Bilinear:
  1577. if (this._mipmap)
  1578. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
  1579. else
  1580. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  1581. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1582. break;
  1583. case exports.FilterMode.Trilinear:
  1584. if (this._mipmap)
  1585. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
  1586. else
  1587. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  1588. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1589. break;
  1590. default:
  1591. throw new Error("BaseTexture:unknown filterMode value.");
  1592. }
  1593. }
  1594. _setWarpMode(orientation, mode) {
  1595. var gl = LayaGL.instance;
  1596. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1597. if (this._isPot(this._width) && this._isPot(this._height)) {
  1598. switch (mode) {
  1599. case exports.WarpMode.Repeat:
  1600. gl.texParameteri(this._glTextureType, orientation, gl.REPEAT);
  1601. break;
  1602. case exports.WarpMode.Clamp:
  1603. gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
  1604. break;
  1605. }
  1606. }
  1607. else {
  1608. gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
  1609. }
  1610. }
  1611. _setAnisotropy(value) {
  1612. var anisotropic = LayaGL.layaGPUInstance._extTextureFilterAnisotropic;
  1613. if (anisotropic) {
  1614. value = Math.max(value, 1);
  1615. var gl = LayaGL.instance;
  1616. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1617. value = Math.min(gl.getParameter(anisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT), value);
  1618. gl.texParameterf(this._glTextureType, anisotropic.TEXTURE_MAX_ANISOTROPY_EXT, value);
  1619. }
  1620. }
  1621. _disposeResource() {
  1622. if (this._glTexture) {
  1623. LayaGL.instance.deleteTexture(this._glTexture);
  1624. this._glTexture = null;
  1625. this._setGPUMemory(0);
  1626. }
  1627. }
  1628. _getSource() {
  1629. if (this._readyed)
  1630. return this._glTexture;
  1631. else
  1632. return null;
  1633. }
  1634. generateMipmap() {
  1635. if (this._isPot(this.width) && this._isPot(this.height))
  1636. LayaGL.instance.generateMipmap(this._glTextureType);
  1637. }
  1638. }
  1639. BaseTexture._rgbmRange = 5.0;
  1640. BaseTexture.FORMAT_R8G8B8 = 0;
  1641. BaseTexture.FORMAT_R8G8B8A8 = 1;
  1642. BaseTexture.FORMAT_ALPHA8 = 2;
  1643. BaseTexture.FORMAT_DXT1 = 3;
  1644. BaseTexture.FORMAT_DXT5 = 4;
  1645. BaseTexture.FORMAT_ETC1RGB = 5;
  1646. BaseTexture.FORMAT_PVRTCRGB_2BPPV = 9;
  1647. BaseTexture.FORMAT_PVRTCRGBA_2BPPV = 10;
  1648. BaseTexture.FORMAT_PVRTCRGB_4BPPV = 11;
  1649. BaseTexture.FORMAT_PVRTCRGBA_4BPPV = 12;
  1650. BaseTexture.RENDERTEXTURE_FORMAT_RGBA_HALF_FLOAT = 14;
  1651. BaseTexture.FORMAT_R32G32B32A32 = 15;
  1652. BaseTexture.FORMAT_DEPTH_16 = 0;
  1653. BaseTexture.FORMAT_STENCIL_8 = 1;
  1654. BaseTexture.FORMAT_DEPTHSTENCIL_16_8 = 2;
  1655. BaseTexture.FORMAT_DEPTHSTENCIL_NONE = 3;
  1656. BaseTexture.FILTERMODE_POINT = 0;
  1657. BaseTexture.FILTERMODE_BILINEAR = 1;
  1658. BaseTexture.FILTERMODE_TRILINEAR = 2;
  1659. BaseTexture.WARPMODE_REPEAT = 0;
  1660. BaseTexture.WARPMODE_CLAMP = 1;
  1661. class Byte {
  1662. constructor(data = null) {
  1663. this._xd_ = true;
  1664. this._allocated_ = 8;
  1665. this._pos_ = 0;
  1666. this._length = 0;
  1667. if (data) {
  1668. this._u8d_ = new Uint8Array(data);
  1669. this._d_ = new DataView(this._u8d_.buffer);
  1670. this._length = this._d_.byteLength;
  1671. }
  1672. else {
  1673. this._resizeBuffer(this._allocated_);
  1674. }
  1675. }
  1676. static getSystemEndian() {
  1677. if (!Byte._sysEndian) {
  1678. var buffer = new ArrayBuffer(2);
  1679. new DataView(buffer).setInt16(0, 256, true);
  1680. Byte._sysEndian = (new Int16Array(buffer))[0] === 256 ? Byte.LITTLE_ENDIAN : Byte.BIG_ENDIAN;
  1681. }
  1682. return Byte._sysEndian;
  1683. }
  1684. get buffer() {
  1685. var rstBuffer = this._d_.buffer;
  1686. if (rstBuffer.byteLength === this._length)
  1687. return rstBuffer;
  1688. return rstBuffer.slice(0, this._length);
  1689. }
  1690. get endian() {
  1691. return this._xd_ ? Byte.LITTLE_ENDIAN : Byte.BIG_ENDIAN;
  1692. }
  1693. set endian(value) {
  1694. this._xd_ = (value === Byte.LITTLE_ENDIAN);
  1695. }
  1696. set length(value) {
  1697. if (this._allocated_ < value)
  1698. this._resizeBuffer(this._allocated_ = Math.floor(Math.max(value, this._allocated_ * 2)));
  1699. else if (this._allocated_ > value)
  1700. this._resizeBuffer(this._allocated_ = value);
  1701. this._length = value;
  1702. }
  1703. get length() {
  1704. return this._length;
  1705. }
  1706. _resizeBuffer(len) {
  1707. try {
  1708. var newByteView = new Uint8Array(len);
  1709. if (this._u8d_ != null) {
  1710. if (this._u8d_.length <= len)
  1711. newByteView.set(this._u8d_);
  1712. else
  1713. newByteView.set(this._u8d_.subarray(0, len));
  1714. }
  1715. this._u8d_ = newByteView;
  1716. this._d_ = new DataView(newByteView.buffer);
  1717. }
  1718. catch (err) {
  1719. throw "Invalid typed array length:" + len;
  1720. }
  1721. }
  1722. getString() {
  1723. return this.readString();
  1724. }
  1725. readString() {
  1726. return this._rUTF(this.getUint16());
  1727. }
  1728. getFloat32Array(start, len) {
  1729. return this.readFloat32Array(start, len);
  1730. }
  1731. readFloat32Array(start, len) {
  1732. var end = start + len;
  1733. end = (end > this._length) ? this._length : end;
  1734. var v = new Float32Array(this._d_.buffer.slice(start, end));
  1735. this._pos_ = end;
  1736. return v;
  1737. }
  1738. getUint8Array(start, len) {
  1739. return this.readUint8Array(start, len);
  1740. }
  1741. readUint8Array(start, len) {
  1742. var end = start + len;
  1743. end = (end > this._length) ? this._length : end;
  1744. var v = new Uint8Array(this._d_.buffer.slice(start, end));
  1745. this._pos_ = end;
  1746. return v;
  1747. }
  1748. getInt16Array(start, len) {
  1749. return this.readInt16Array(start, len);
  1750. }
  1751. readInt16Array(start, len) {
  1752. var end = start + len;
  1753. end = (end > this._length) ? this._length : end;
  1754. var v = new Int16Array(this._d_.buffer.slice(start, end));
  1755. this._pos_ = end;
  1756. return v;
  1757. }
  1758. getFloat32() {
  1759. return this.readFloat32();
  1760. }
  1761. readFloat32() {
  1762. if (this._pos_ + 4 > this._length)
  1763. throw "getFloat32 error - Out of bounds";
  1764. var v = this._d_.getFloat32(this._pos_, this._xd_);
  1765. this._pos_ += 4;
  1766. return v;
  1767. }
  1768. getFloat64() {
  1769. return this.readFloat64();
  1770. }
  1771. readFloat64() {
  1772. if (this._pos_ + 8 > this._length)
  1773. throw "getFloat64 error - Out of bounds";
  1774. var v = this._d_.getFloat64(this._pos_, this._xd_);
  1775. this._pos_ += 8;
  1776. return v;
  1777. }
  1778. writeFloat32(value) {
  1779. this._ensureWrite(this._pos_ + 4);
  1780. this._d_.setFloat32(this._pos_, value, this._xd_);
  1781. this._pos_ += 4;
  1782. }
  1783. writeFloat64(value) {
  1784. this._ensureWrite(this._pos_ + 8);
  1785. this._d_.setFloat64(this._pos_, value, this._xd_);
  1786. this._pos_ += 8;
  1787. }
  1788. getInt32() {
  1789. return this.readInt32();
  1790. }
  1791. readInt32() {
  1792. if (this._pos_ + 4 > this._length)
  1793. throw "getInt32 error - Out of bounds";
  1794. var float = this._d_.getInt32(this._pos_, this._xd_);
  1795. this._pos_ += 4;
  1796. return float;
  1797. }
  1798. getUint32() {
  1799. return this.readUint32();
  1800. }
  1801. readUint32() {
  1802. if (this._pos_ + 4 > this._length)
  1803. throw "getUint32 error - Out of bounds";
  1804. var v = this._d_.getUint32(this._pos_, this._xd_);
  1805. this._pos_ += 4;
  1806. return v;
  1807. }
  1808. writeInt32(value) {
  1809. this._ensureWrite(this._pos_ + 4);
  1810. this._d_.setInt32(this._pos_, value, this._xd_);
  1811. this._pos_ += 4;
  1812. }
  1813. writeUint32(value) {
  1814. this._ensureWrite(this._pos_ + 4);
  1815. this._d_.setUint32(this._pos_, value, this._xd_);
  1816. this._pos_ += 4;
  1817. }
  1818. getInt16() {
  1819. return this.readInt16();
  1820. }
  1821. readInt16() {
  1822. if (this._pos_ + 2 > this._length)
  1823. throw "getInt16 error - Out of bounds";
  1824. var us = this._d_.getInt16(this._pos_, this._xd_);
  1825. this._pos_ += 2;
  1826. return us;
  1827. }
  1828. getUint16() {
  1829. return this.readUint16();
  1830. }
  1831. readUint16() {
  1832. if (this._pos_ + 2 > this._length)
  1833. throw "getUint16 error - Out of bounds";
  1834. var us = this._d_.getUint16(this._pos_, this._xd_);
  1835. this._pos_ += 2;
  1836. return us;
  1837. }
  1838. writeUint16(value) {
  1839. this._ensureWrite(this._pos_ + 2);
  1840. this._d_.setUint16(this._pos_, value, this._xd_);
  1841. this._pos_ += 2;
  1842. }
  1843. writeInt16(value) {
  1844. this._ensureWrite(this._pos_ + 2);
  1845. this._d_.setInt16(this._pos_, value, this._xd_);
  1846. this._pos_ += 2;
  1847. }
  1848. getUint8() {
  1849. return this.readUint8();
  1850. }
  1851. readUint8() {
  1852. if (this._pos_ + 1 > this._length)
  1853. throw "getUint8 error - Out of bounds";
  1854. return this._u8d_[this._pos_++];
  1855. }
  1856. writeUint8(value) {
  1857. this._ensureWrite(this._pos_ + 1);
  1858. this._d_.setUint8(this._pos_, value);
  1859. this._pos_++;
  1860. }
  1861. _getUInt8(pos) {
  1862. return this._readUInt8(pos);
  1863. }
  1864. _readUInt8(pos) {
  1865. return this._d_.getUint8(pos);
  1866. }
  1867. _getUint16(pos) {
  1868. return this._readUint16(pos);
  1869. }
  1870. _readUint16(pos) {
  1871. return this._d_.getUint16(pos, this._xd_);
  1872. }
  1873. _getMatrix() {
  1874. return this._readMatrix();
  1875. }
  1876. _readMatrix() {
  1877. var rst = new Matrix(this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32());
  1878. return rst;
  1879. }
  1880. _rUTF(len) {
  1881. var max = this._pos_ + len, c, c2, c3, f = String.fromCharCode;
  1882. var u = this._u8d_;
  1883. var strs = [];
  1884. var n = 0;
  1885. strs.length = 1000;
  1886. while (this._pos_ < max) {
  1887. c = u[this._pos_++];
  1888. if (c < 0x80) {
  1889. if (c != 0)
  1890. strs[n++] = f(c);
  1891. }
  1892. else if (c < 0xE0) {
  1893. strs[n++] = f(((c & 0x3F) << 6) | (u[this._pos_++] & 0x7F));
  1894. }
  1895. else if (c < 0xF0) {
  1896. c2 = u[this._pos_++];
  1897. strs[n++] = f(((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (u[this._pos_++] & 0x7F));
  1898. }
  1899. else {
  1900. c2 = u[this._pos_++];
  1901. c3 = u[this._pos_++];
  1902. const _code = ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 & 0x7F) << 6) | (u[this._pos_++] & 0x7F);
  1903. if (_code >= 0x10000) {
  1904. const _offset = _code - 0x10000;
  1905. const _lead = 0xd800 | (_offset >> 10);
  1906. const _trail = 0xdc00 | (_offset & 0x3ff);
  1907. strs[n++] = f(_lead);
  1908. strs[n++] = f(_trail);
  1909. }
  1910. else {
  1911. strs[n++] = f(_code);
  1912. }
  1913. }
  1914. }
  1915. strs.length = n;
  1916. return strs.join('');
  1917. }
  1918. getCustomString(len) {
  1919. return this.readCustomString(len);
  1920. }
  1921. readCustomString(len) {
  1922. var v = "", ulen = 0, c, c2, f = String.fromCharCode;
  1923. var u = this._u8d_;
  1924. while (len > 0) {
  1925. c = u[this._pos_];
  1926. if (c < 0x80) {
  1927. v += f(c);
  1928. this._pos_++;
  1929. len--;
  1930. }
  1931. else {
  1932. ulen = c - 0x80;
  1933. this._pos_++;
  1934. len -= ulen;
  1935. while (ulen > 0) {
  1936. c = u[this._pos_++];
  1937. c2 = u[this._pos_++];
  1938. v += f((c2 << 8) | c);
  1939. ulen--;
  1940. }
  1941. }
  1942. }
  1943. return v;
  1944. }
  1945. get pos() {
  1946. return this._pos_;
  1947. }
  1948. set pos(value) {
  1949. this._pos_ = value;
  1950. }
  1951. get bytesAvailable() {
  1952. return this._length - this._pos_;
  1953. }
  1954. clear() {
  1955. this._pos_ = 0;
  1956. this.length = 0;
  1957. }
  1958. __getBuffer() {
  1959. return this._d_.buffer;
  1960. }
  1961. writeUTFBytes(value) {
  1962. value = value + "";
  1963. for (var i = 0, sz = value.length; i < sz; i++) {
  1964. var c = value.charCodeAt(i);
  1965. if (c <= 0x7F) {
  1966. this.writeByte(c);
  1967. }
  1968. else if (c <= 0x7FF) {
  1969. this._ensureWrite(this._pos_ + 2);
  1970. this._u8d_.set([0xC0 | (c >> 6), 0x80 | (c & 0x3F)], this._pos_);
  1971. this._pos_ += 2;
  1972. }
  1973. else if (c >= 0xD800 && c <= 0xDBFF) {
  1974. i++;
  1975. const c2 = value.charCodeAt(i);
  1976. if (!Number.isNaN(c2) && c2 >= 0xDC00 && c2 <= 0xDFFF) {
  1977. const _p1 = (c & 0x3FF) + 0x40;
  1978. const _p2 = c2 & 0x3FF;
  1979. const _b1 = 0xF0 | ((_p1 >> 8) & 0x3F);
  1980. const _b2 = 0x80 | ((_p1 >> 2) & 0x3F);
  1981. const _b3 = 0x80 | ((_p1 & 0x3) << 4) | ((_p2 >> 6) & 0xF);
  1982. const _b4 = 0x80 | (_p2 & 0x3F);
  1983. this._ensureWrite(this._pos_ + 4);
  1984. this._u8d_.set([_b1, _b2, _b3, _b4], this._pos_);
  1985. this._pos_ += 4;
  1986. }
  1987. }
  1988. else if (c <= 0xFFFF) {
  1989. this._ensureWrite(this._pos_ + 3);
  1990. this._u8d_.set([0xE0 | (c >> 12), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)], this._pos_);
  1991. this._pos_ += 3;
  1992. }
  1993. else {
  1994. this._ensureWrite(this._pos_ + 4);
  1995. this._u8d_.set([0xF0 | (c >> 18), 0x80 | ((c >> 12) & 0x3F), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)], this._pos_);
  1996. this._pos_ += 4;
  1997. }
  1998. }
  1999. }
  2000. writeUTFString(value) {
  2001. var tPos = this.pos;
  2002. this.writeUint16(1);
  2003. this.writeUTFBytes(value);
  2004. var dPos = this.pos - tPos - 2;
  2005. this._d_.setUint16(tPos, dPos, this._xd_);
  2006. }
  2007. writeUTFString32(value) {
  2008. var tPos = this.pos;
  2009. this.writeUint32(1);
  2010. this.writeUTFBytes(value);
  2011. var dPos = this.pos - tPos - 4;
  2012. this._d_.setUint32(tPos, dPos, this._xd_);
  2013. }
  2014. readUTFString() {
  2015. return this.readUTFBytes(this.getUint16());
  2016. }
  2017. readUTFString32() {
  2018. return this.readUTFBytes(this.getUint32());
  2019. }
  2020. getUTFString() {
  2021. return this.readUTFString();
  2022. }
  2023. readUTFBytes(len = -1) {
  2024. if (len === 0)
  2025. return "";
  2026. var lastBytes = this.bytesAvailable;
  2027. if (len > lastBytes)
  2028. throw "readUTFBytes error - Out of bounds";
  2029. len = len > 0 ? len : lastBytes;
  2030. return this._rUTF(len);
  2031. }
  2032. getUTFBytes(len = -1) {
  2033. return this.readUTFBytes(len);
  2034. }
  2035. writeByte(value) {
  2036. this._ensureWrite(this._pos_ + 1);
  2037. this._d_.setInt8(this._pos_, value);
  2038. this._pos_ += 1;
  2039. }
  2040. readByte() {
  2041. if (this._pos_ + 1 > this._length)
  2042. throw "readByte error - Out of bounds";
  2043. return this._d_.getInt8(this._pos_++);
  2044. }
  2045. getByte() {
  2046. return this.readByte();
  2047. }
  2048. _ensureWrite(lengthToEnsure) {
  2049. if (this._length < lengthToEnsure)
  2050. this._length = lengthToEnsure;
  2051. if (this._allocated_ < lengthToEnsure)
  2052. this.length = lengthToEnsure;
  2053. }
  2054. writeArrayBuffer(arraybuffer, offset = 0, length = 0) {
  2055. if (offset < 0 || length < 0)
  2056. throw "writeArrayBuffer error - Out of bounds";
  2057. if (length == 0)
  2058. length = arraybuffer.byteLength - offset;
  2059. this._ensureWrite(this._pos_ + length);
  2060. var uint8array = new Uint8Array(arraybuffer);
  2061. this._u8d_.set(uint8array.subarray(offset, offset + length), this._pos_);
  2062. this._pos_ += length;
  2063. }
  2064. readArrayBuffer(length) {
  2065. var rst;
  2066. rst = this._u8d_.buffer.slice(this._pos_, this._pos_ + length);
  2067. this._pos_ = this._pos_ + length;
  2068. return rst;
  2069. }
  2070. }
  2071. Byte.BIG_ENDIAN = "bigEndian";
  2072. Byte.LITTLE_ENDIAN = "littleEndian";
  2073. Byte._sysEndian = null;
  2074. (function (RenderTextureFormat) {
  2075. RenderTextureFormat[RenderTextureFormat["R8G8B8"] = 0] = "R8G8B8";
  2076. RenderTextureFormat[RenderTextureFormat["R8G8B8A8"] = 1] = "R8G8B8A8";
  2077. RenderTextureFormat[RenderTextureFormat["Alpha8"] = 2] = "Alpha8";
  2078. RenderTextureFormat[RenderTextureFormat["R16G16B16A16"] = 14] = "R16G16B16A16";
  2079. RenderTextureFormat[RenderTextureFormat["Depth"] = 15] = "Depth";
  2080. RenderTextureFormat[RenderTextureFormat["ShadowMap"] = 16] = "ShadowMap";
  2081. })(exports.RenderTextureFormat || (exports.RenderTextureFormat = {}));
  2082. (function (RenderTextureDepthFormat) {
  2083. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTH_16"] = 0] = "DEPTH_16";
  2084. RenderTextureDepthFormat[RenderTextureDepthFormat["STENCIL_8"] = 1] = "STENCIL_8";
  2085. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTHSTENCIL_24_8"] = 2] = "DEPTHSTENCIL_24_8";
  2086. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTHSTENCIL_NONE"] = 3] = "DEPTHSTENCIL_NONE";
  2087. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTHSTENCIL_16_8"] = 2] = "DEPTHSTENCIL_16_8";
  2088. })(exports.RenderTextureDepthFormat || (exports.RenderTextureDepthFormat = {}));
  2089. class SystemUtils {
  2090. static get maxTextureCount() {
  2091. return this._maxTextureCount;
  2092. }
  2093. static get maxTextureSize() {
  2094. return this._maxTextureSize;
  2095. }
  2096. static get shaderCapailityLevel() {
  2097. return this._shaderCapailityLevel;
  2098. }
  2099. static supportTextureFormat(format) {
  2100. switch (format) {
  2101. case exports.TextureFormat.R32G32B32A32:
  2102. return (!LayaGL.layaGPUInstance._isWebGL2 && !LayaGL.layaGPUInstance._oesTextureFloat) ? false : true;
  2103. case exports.TextureFormat.R16G16B16A16:
  2104. return (!LayaGL.layaGPUInstance._isWebGL2 && !LayaGL.layaGPUInstance._oesTextureHalfFloat) ? false : true;
  2105. default:
  2106. return true;
  2107. }
  2108. }
  2109. static supportRenderTextureFormat(format) {
  2110. switch (format) {
  2111. case exports.RenderTextureFormat.R16G16B16A16:
  2112. return (((!!LayaGL.layaGPUInstance._isWebGL2) && (!!LayaGL.layaGPUInstance._extColorBufferFloat)) || LayaGL.layaGPUInstance._oesTextureHalfFloat && LayaGL.layaGPUInstance._oesTextureHalfFloatLinear) ? true : false;
  2113. case exports.RenderTextureFormat.Depth:
  2114. return (LayaGL.layaGPUInstance._isWebGL2 || LayaGL.layaGPUInstance._webgl_depth_texture) ? true : false;
  2115. case exports.RenderTextureFormat.ShadowMap:
  2116. return LayaGL.layaGPUInstance._isWebGL2 ? true : false;
  2117. default:
  2118. return true;
  2119. }
  2120. }
  2121. }
  2122. class HalfFloatUtils {
  2123. static __init__() {
  2124. for (var i = 0; i < 256; ++i) {
  2125. var e = i - 127;
  2126. if (e < -27) {
  2127. HalfFloatUtils._baseTable[i | 0x000] = 0x0000;
  2128. HalfFloatUtils._baseTable[i | 0x100] = 0x8000;
  2129. HalfFloatUtils._shiftTable[i | 0x000] = 24;
  2130. HalfFloatUtils._shiftTable[i | 0x100] = 24;
  2131. }
  2132. else if (e < -14) {
  2133. HalfFloatUtils._baseTable[i | 0x000] = 0x0400 >> (-e - 14);
  2134. HalfFloatUtils._baseTable[i | 0x100] = (0x0400 >> (-e - 14)) | 0x8000;
  2135. HalfFloatUtils._shiftTable[i | 0x000] = -e - 1;
  2136. HalfFloatUtils._shiftTable[i | 0x100] = -e - 1;
  2137. }
  2138. else if (e <= 15) {
  2139. HalfFloatUtils._baseTable[i | 0x000] = (e + 15) << 10;
  2140. HalfFloatUtils._baseTable[i | 0x100] = ((e + 15) << 10) | 0x8000;
  2141. HalfFloatUtils._shiftTable[i | 0x000] = 13;
  2142. HalfFloatUtils._shiftTable[i | 0x100] = 13;
  2143. }
  2144. else if (e < 128) {
  2145. HalfFloatUtils._baseTable[i | 0x000] = 0x7c00;
  2146. HalfFloatUtils._baseTable[i | 0x100] = 0xfc00;
  2147. HalfFloatUtils._shiftTable[i | 0x000] = 24;
  2148. HalfFloatUtils._shiftTable[i | 0x100] = 24;
  2149. }
  2150. else {
  2151. HalfFloatUtils._baseTable[i | 0x000] = 0x7c00;
  2152. HalfFloatUtils._baseTable[i | 0x100] = 0xfc00;
  2153. HalfFloatUtils._shiftTable[i | 0x000] = 13;
  2154. HalfFloatUtils._shiftTable[i | 0x100] = 13;
  2155. }
  2156. }
  2157. HalfFloatUtils._mantissaTable[0] = 0;
  2158. for (i = 1; i < 1024; ++i) {
  2159. var m = i << 13;
  2160. e = 0;
  2161. while ((m & 0x00800000) === 0) {
  2162. e -= 0x00800000;
  2163. m <<= 1;
  2164. }
  2165. m &= ~0x00800000;
  2166. e += 0x38800000;
  2167. HalfFloatUtils._mantissaTable[i] = m | e;
  2168. }
  2169. for (i = 1024; i < 2048; ++i) {
  2170. HalfFloatUtils._mantissaTable[i] = 0x38000000 + ((i - 1024) << 13);
  2171. }
  2172. HalfFloatUtils._exponentTable[0] = 0;
  2173. for (i = 1; i < 31; ++i) {
  2174. HalfFloatUtils._exponentTable[i] = i << 23;
  2175. }
  2176. HalfFloatUtils._exponentTable[31] = 0x47800000;
  2177. HalfFloatUtils._exponentTable[32] = 0x80000000;
  2178. for (i = 33; i < 63; ++i) {
  2179. HalfFloatUtils._exponentTable[i] = 0x80000000 + ((i - 32) << 23);
  2180. }
  2181. HalfFloatUtils._exponentTable[63] = 0xc7800000;
  2182. HalfFloatUtils._offsetTable[0] = 0;
  2183. for (i = 1; i < 64; ++i) {
  2184. if (i === 32) {
  2185. HalfFloatUtils._offsetTable[i] = 0;
  2186. }
  2187. else {
  2188. HalfFloatUtils._offsetTable[i] = 1024;
  2189. }
  2190. }
  2191. }
  2192. static roundToFloat16Bits(num) {
  2193. HalfFloatUtils._floatView[0] = num;
  2194. var f = HalfFloatUtils._uint32View[0];
  2195. var e = (f >> 23) & 0x1ff;
  2196. return HalfFloatUtils._baseTable[e] + ((f & 0x007fffff) >> HalfFloatUtils._shiftTable[e]);
  2197. }
  2198. static convertToNumber(float16bits) {
  2199. var m = float16bits >> 10;
  2200. HalfFloatUtils._uint32View[0] = HalfFloatUtils._mantissaTable[HalfFloatUtils._offsetTable[m] + (float16bits & 0x3ff)] + HalfFloatUtils._exponentTable[m];
  2201. return HalfFloatUtils._floatView[0];
  2202. }
  2203. }
  2204. HalfFloatUtils._buffer = new ArrayBuffer(4);
  2205. HalfFloatUtils._floatView = new Float32Array(HalfFloatUtils._buffer);
  2206. HalfFloatUtils._uint32View = new Uint32Array(HalfFloatUtils._buffer);
  2207. HalfFloatUtils._baseTable = new Uint32Array(512);
  2208. HalfFloatUtils._shiftTable = new Uint32Array(512);
  2209. HalfFloatUtils._mantissaTable = new Uint32Array(2048);
  2210. HalfFloatUtils._exponentTable = new Uint32Array(64);
  2211. HalfFloatUtils._offsetTable = new Uint32Array(64);
  2212. class Texture2D extends BaseTexture {
  2213. constructor(width = 0, height = 0, format = exports.TextureFormat.R8G8B8A8, mipmap = true, canRead = false) {
  2214. super(format, mipmap);
  2215. var gl = LayaGL.instance;
  2216. this._glTextureType = gl.TEXTURE_2D;
  2217. this._width = width;
  2218. this._height = height;
  2219. this._canRead = canRead;
  2220. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  2221. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  2222. this._setFilterMode(this._filterMode);
  2223. this._setAnisotropy(this._anisoLevel);
  2224. var compress = this._gpuCompressFormat();
  2225. if (mipmap) {
  2226. var mipCount = Math.max(Math.ceil(Math.log2(width)) + 1, Math.ceil(Math.log2(height)) + 1);
  2227. if (!compress) {
  2228. for (var i = 0; i < mipCount; i++)
  2229. this._setPixels(null, i, Math.max(width >> i, 1), Math.max(height >> i, 1));
  2230. }
  2231. this._mipmapCount = mipCount;
  2232. this._setGPUMemory(width * height * 4 * (1 + 1 / 3));
  2233. }
  2234. else {
  2235. if (!compress)
  2236. this._setPixels(null, 0, width, height);
  2237. this._mipmapCount = 1;
  2238. this._setGPUMemory(width * height * 4);
  2239. }
  2240. }
  2241. static __init__() {
  2242. var pixels = new Uint8Array(3);
  2243. pixels[0] = 128;
  2244. pixels[1] = 128;
  2245. pixels[2] = 128;
  2246. Texture2D.grayTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  2247. Texture2D.grayTexture.setPixels(pixels);
  2248. Texture2D.grayTexture.lock = true;
  2249. pixels[0] = 255;
  2250. pixels[1] = 255;
  2251. pixels[2] = 255;
  2252. Texture2D.whiteTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  2253. Texture2D.whiteTexture.setPixels(pixels);
  2254. Texture2D.whiteTexture.lock = true;
  2255. pixels[0] = 0;
  2256. pixels[1] = 0;
  2257. pixels[2] = 0;
  2258. Texture2D.blackTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  2259. Texture2D.blackTexture.setPixels(pixels);
  2260. Texture2D.blackTexture.lock = true;
  2261. }
  2262. static _parse(data, propertyParams = null, constructParams = null) {
  2263. var texture = constructParams ? new Texture2D(constructParams[0], constructParams[1], constructParams[2], constructParams[3], constructParams[4]) : new Texture2D(0, 0);
  2264. if (propertyParams) {
  2265. texture.wrapModeU = propertyParams.wrapModeU;
  2266. texture.wrapModeV = propertyParams.wrapModeV;
  2267. texture.filterMode = propertyParams.filterMode;
  2268. texture.anisoLevel = propertyParams.anisoLevel;
  2269. }
  2270. switch (texture._format) {
  2271. case exports.TextureFormat.R8G8B8:
  2272. case exports.TextureFormat.R8G8B8A8:
  2273. texture.loadImageSource(data);
  2274. break;
  2275. case exports.TextureFormat.DXT1:
  2276. case exports.TextureFormat.DXT5:
  2277. case exports.TextureFormat.ETC1RGB:
  2278. case exports.TextureFormat.PVRTCRGB_2BPPV:
  2279. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  2280. case exports.TextureFormat.PVRTCRGB_4BPPV:
  2281. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  2282. texture.setCompressData(data);
  2283. break;
  2284. default:
  2285. throw "Texture2D:unkonwn format.";
  2286. }
  2287. return texture;
  2288. }
  2289. static _SimpleAnimatorTextureParse(data, propertyParams = null, constructParams = null) {
  2290. var byte = new Byte(data);
  2291. var version = byte.readUTFString();
  2292. var texture;
  2293. var pixelDataArrays;
  2294. var usePixelData;
  2295. switch (version) {
  2296. case "LAYAANIMATORTEXTURE:0000":
  2297. var textureWidth = byte.readInt32();
  2298. var pixelDataLength = byte.readInt32();
  2299. pixelDataArrays = new Float32Array(textureWidth * textureWidth * 4);
  2300. usePixelData = new Float32Array(byte.readArrayBuffer(pixelDataLength * 4));
  2301. pixelDataArrays.set(usePixelData, 0);
  2302. var texture = new Texture2D(textureWidth, textureWidth, exports.TextureFormat.R32G32B32A32, false, false);
  2303. texture.setPixels(pixelDataArrays, 0);
  2304. texture.filterMode = exports.FilterMode.Point;
  2305. break;
  2306. case "LAYACOMPRESSANIMATORTEXTURE:0000":
  2307. var textureWidth = byte.readInt32();
  2308. var pixelDataLength = byte.readInt32();
  2309. pixelDataArrays = new Uint16Array(byte.readArrayBuffer(pixelDataLength * 2));
  2310. if (!SystemUtils.supportTextureFormat(exports.TextureFormat.R16G16B16A16)) {
  2311. console.log("The platform does not support 16-bit floating-point textures");
  2312. if (!SystemUtils.supportTextureFormat(exports.TextureFormat.R32G32B32A32))
  2313. console.error("The platform does not support 32-bit floating-point textures");
  2314. usePixelData = new Float32Array(textureWidth * textureWidth * 4);
  2315. for (var i = 0, n = pixelDataArrays.length; i < n; i++) {
  2316. usePixelData[i] = HalfFloatUtils.convertToNumber(pixelDataArrays[i]);
  2317. }
  2318. texture = new Texture2D(textureWidth, textureWidth, exports.TextureFormat.R32G32B32A32, false, false);
  2319. texture.setPixels(usePixelData, 0);
  2320. texture.filterMode = exports.FilterMode.Point;
  2321. }
  2322. else {
  2323. usePixelData = new Uint16Array(textureWidth * textureWidth * 4);
  2324. usePixelData.set(pixelDataArrays, 0);
  2325. texture = new Texture2D(textureWidth, textureWidth, exports.TextureFormat.R16G16B16A16, false, false);
  2326. texture.setPixels(usePixelData, 0);
  2327. texture.filterMode = exports.FilterMode.Point;
  2328. }
  2329. break;
  2330. default:
  2331. throw "Laya3D:unknow version.";
  2332. }
  2333. return texture;
  2334. }
  2335. static load(url, complete) {
  2336. ILaya.loader.create(url, complete, null, ILaya.Loader.TEXTURE2D);
  2337. }
  2338. get defaulteTexture() {
  2339. return Texture2D.grayTexture;
  2340. }
  2341. _gpuCompressFormat() {
  2342. return this._format == exports.TextureFormat.DXT1 || this._format == exports.TextureFormat.DXT5 ||
  2343. this._format == exports.TextureFormat.ETC1RGB ||
  2344. this._format == exports.TextureFormat.PVRTCRGB_2BPPV || this._format == exports.TextureFormat.PVRTCRGBA_2BPPV ||
  2345. this._format == exports.TextureFormat.PVRTCRGB_4BPPV || this._format == exports.TextureFormat.PVRTCRGBA_4BPPV;
  2346. }
  2347. _setPixels(pixels, miplevel, width, height) {
  2348. var gl = LayaGL.instance;
  2349. var textureType = this._glTextureType;
  2350. var glFormat = this._getGLFormat();
  2351. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  2352. switch (this.format) {
  2353. case exports.TextureFormat.R8G8B8:
  2354. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  2355. gl.texImage2D(textureType, miplevel, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, pixels);
  2356. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  2357. break;
  2358. case exports.TextureFormat.R5G6B5:
  2359. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 2);
  2360. gl.texImage2D(textureType, miplevel, glFormat, width, height, 0, glFormat, gl.UNSIGNED_SHORT_5_6_5, pixels);
  2361. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  2362. break;
  2363. case exports.TextureFormat.R32G32B32A32:
  2364. if (LayaGL.layaGPUInstance._isWebGL2)
  2365. gl.texImage2D(textureType, miplevel, gl.RGBA32F, width, height, 0, glFormat, gl.FLOAT, pixels);
  2366. else
  2367. gl.texImage2D(textureType, miplevel, gl.RGBA, width, height, 0, glFormat, gl.FLOAT, pixels);
  2368. break;
  2369. case exports.TextureFormat.R16G16B16A16:
  2370. if (LayaGL.layaGPUInstance._isWebGL2)
  2371. gl.texImage2D(textureType, miplevel, gl.RGBA16F, width, height, 0, glFormat, gl.HALF_FLOAT, pixels);
  2372. else
  2373. gl.texImage2D(textureType, miplevel, gl.RGBA, width, height, 0, glFormat, LayaGL.layaGPUInstance._oesTextureHalfFloat.HALF_FLOAT_OES, pixels);
  2374. break;
  2375. default:
  2376. gl.texImage2D(textureType, miplevel, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, pixels);
  2377. }
  2378. }
  2379. _calcualatesCompressedDataSize(format, width, height) {
  2380. switch (format) {
  2381. case exports.TextureFormat.DXT1:
  2382. case exports.TextureFormat.ETC1RGB:
  2383. return ((width + 3) >> 2) * ((height + 3) >> 2) * 8;
  2384. case exports.TextureFormat.DXT5:
  2385. return ((width + 3) >> 2) * ((height + 3) >> 2) * 16;
  2386. case exports.TextureFormat.PVRTCRGB_4BPPV:
  2387. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  2388. return Math.floor((Math.max(width, 8) * Math.max(height, 8) * 4 + 7) / 8);
  2389. case exports.TextureFormat.PVRTCRGB_2BPPV:
  2390. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  2391. return Math.floor((Math.max(width, 16) * Math.max(height, 8) * 2 + 7) / 8);
  2392. default:
  2393. return 0;
  2394. }
  2395. }
  2396. _pharseDDS(arrayBuffer) {
  2397. const FOURCC_DXT1 = 827611204;
  2398. const FOURCC_DXT5 = 894720068;
  2399. const DDPF_FOURCC = 0x4;
  2400. const DDSD_MIPMAPCOUNT = 0x20000;
  2401. const DDS_MAGIC = 0x20534444;
  2402. const DDS_HEADER_LENGTH = 31;
  2403. const DDS_HEADER_MAGIC = 0;
  2404. const DDS_HEADER_SIZE = 1;
  2405. const DDS_HEADER_FLAGS = 2;
  2406. const DDS_HEADER_HEIGHT = 3;
  2407. const DDS_HEADER_WIDTH = 4;
  2408. const DDS_HEADER_MIPMAPCOUNT = 7;
  2409. const DDS_HEADER_PF_FLAGS = 20;
  2410. const DDS_HEADER_PF_FOURCC = 21;
  2411. var header = new Int32Array(arrayBuffer, 0, DDS_HEADER_LENGTH);
  2412. if (header[DDS_HEADER_MAGIC] != DDS_MAGIC)
  2413. throw "Invalid magic number in DDS header";
  2414. if (!(header[DDS_HEADER_PF_FLAGS] & DDPF_FOURCC))
  2415. throw "Unsupported format, must contain a FourCC code";
  2416. var compressedFormat = header[DDS_HEADER_PF_FOURCC];
  2417. switch (this._format) {
  2418. case exports.TextureFormat.DXT1:
  2419. if (compressedFormat !== FOURCC_DXT1)
  2420. throw "the FourCC code is not same with texture format.";
  2421. break;
  2422. case exports.TextureFormat.DXT5:
  2423. if (compressedFormat !== FOURCC_DXT5)
  2424. throw "the FourCC code is not same with texture format.";
  2425. break;
  2426. default:
  2427. throw "unknown texture format.";
  2428. }
  2429. var mipLevels = 1;
  2430. if (header[DDS_HEADER_FLAGS] & DDSD_MIPMAPCOUNT) {
  2431. mipLevels = Math.max(1, header[DDS_HEADER_MIPMAPCOUNT]);
  2432. if (!this._mipmap)
  2433. throw "the mipmap is not same with Texture2D.";
  2434. }
  2435. else {
  2436. if (this._mipmap)
  2437. throw "the mipmap is not same with Texture2D.";
  2438. }
  2439. var width = header[DDS_HEADER_WIDTH];
  2440. var height = header[DDS_HEADER_HEIGHT];
  2441. this._width = width;
  2442. this._height = height;
  2443. var dataOffset = header[DDS_HEADER_SIZE] + 4;
  2444. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 0);
  2445. }
  2446. _pharseKTX(arrayBuffer) {
  2447. const ETC_HEADER_LENGTH = 13;
  2448. const ETC_HEADER_FORMAT = 4;
  2449. const ETC_HEADER_HEIGHT = 7;
  2450. const ETC_HEADER_WIDTH = 6;
  2451. const ETC_HEADER_MIPMAPCOUNT = 11;
  2452. const ETC_HEADER_METADATA = 12;
  2453. var id = new Uint8Array(arrayBuffer, 0, 12);
  2454. if (id[0] != 0xAB || id[1] != 0x4B || id[2] != 0x54 || id[3] != 0x58 || id[4] != 0x20 || id[5] != 0x31 || id[6] != 0x31 || id[7] != 0xBB || id[8] != 0x0D || id[9] != 0x0A || id[10] != 0x1A || id[11] != 0x0A)
  2455. throw ("Invalid fileIdentifier in KTX header");
  2456. var header = new Int32Array(id.buffer, id.length, ETC_HEADER_LENGTH);
  2457. var compressedFormat = header[ETC_HEADER_FORMAT];
  2458. switch (compressedFormat) {
  2459. case LayaGL.layaGPUInstance._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL:
  2460. this._format = exports.TextureFormat.ETC1RGB;
  2461. break;
  2462. default:
  2463. throw "unknown texture format.";
  2464. }
  2465. var mipLevels = header[ETC_HEADER_MIPMAPCOUNT];
  2466. var width = header[ETC_HEADER_WIDTH];
  2467. var height = header[ETC_HEADER_HEIGHT];
  2468. this._width = width;
  2469. this._height = height;
  2470. var dataOffset = 64 + header[ETC_HEADER_METADATA];
  2471. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 4);
  2472. }
  2473. _pharsePVR(arrayBuffer) {
  2474. const PVR_FORMAT_2BPP_RGB = 0;
  2475. const PVR_FORMAT_2BPP_RGBA = 1;
  2476. const PVR_FORMAT_4BPP_RGB = 2;
  2477. const PVR_FORMAT_4BPP_RGBA = 3;
  2478. const PVR_MAGIC = 0x03525650;
  2479. const PVR_HEADER_LENGTH = 13;
  2480. const PVR_HEADER_MAGIC = 0;
  2481. const PVR_HEADER_FORMAT = 2;
  2482. const PVR_HEADER_HEIGHT = 6;
  2483. const PVR_HEADER_WIDTH = 7;
  2484. const PVR_HEADER_MIPMAPCOUNT = 11;
  2485. const PVR_HEADER_METADATA = 12;
  2486. var header = new Int32Array(arrayBuffer, 0, PVR_HEADER_LENGTH);
  2487. if (header[PVR_HEADER_MAGIC] != PVR_MAGIC)
  2488. throw ("Invalid magic number in PVR header");
  2489. var compressedFormat = header[PVR_HEADER_FORMAT];
  2490. switch (compressedFormat) {
  2491. case PVR_FORMAT_2BPP_RGB:
  2492. this._format = exports.TextureFormat.PVRTCRGB_2BPPV;
  2493. break;
  2494. case PVR_FORMAT_4BPP_RGB:
  2495. this._format = exports.TextureFormat.PVRTCRGB_4BPPV;
  2496. break;
  2497. case PVR_FORMAT_2BPP_RGBA:
  2498. this._format = exports.TextureFormat.PVRTCRGBA_2BPPV;
  2499. break;
  2500. case PVR_FORMAT_4BPP_RGBA:
  2501. this._format = exports.TextureFormat.PVRTCRGBA_4BPPV;
  2502. break;
  2503. default:
  2504. throw "Texture2D:unknown PVR format.";
  2505. }
  2506. var mipLevels = header[PVR_HEADER_MIPMAPCOUNT];
  2507. var width = header[PVR_HEADER_WIDTH];
  2508. var height = header[PVR_HEADER_HEIGHT];
  2509. this._width = width;
  2510. this._height = height;
  2511. var dataOffset = header[PVR_HEADER_METADATA] + 52;
  2512. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 0);
  2513. }
  2514. _upLoadCompressedTexImage2D(data, width, height, miplevelCount, dataOffset, imageSizeOffset) {
  2515. var gl = LayaGL.instance;
  2516. var textureType = this._glTextureType;
  2517. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  2518. var glFormat = this._getGLFormat();
  2519. var offset = dataOffset;
  2520. for (var i = 0; i < miplevelCount; i++) {
  2521. offset += imageSizeOffset;
  2522. var mipDataSize = this._calcualatesCompressedDataSize(this._format, width, height);
  2523. var mipData = new Uint8Array(data, offset, mipDataSize);
  2524. gl.compressedTexImage2D(textureType, i, glFormat, width, height, 0, mipData);
  2525. width = Math.max(width >> 1, 1.0);
  2526. height = Math.max(height >> 1, 1.0);
  2527. offset += mipDataSize;
  2528. }
  2529. var memory = offset;
  2530. this._setGPUMemory(memory);
  2531. this._readyed = true;
  2532. this._activeResource();
  2533. }
  2534. loadImageSource(source, premultiplyAlpha = false) {
  2535. var gl = LayaGL.instance;
  2536. var width = source.width;
  2537. var height = source.height;
  2538. this._width = width;
  2539. this._height = height;
  2540. if (!(this._isPot(width) && this._isPot(height)))
  2541. this._mipmap = false;
  2542. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  2543. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  2544. this._setFilterMode(this._filterMode);
  2545. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  2546. var glFormat = this._getGLFormat();
  2547. if (ILaya.Render.isConchApp) {
  2548. if (source.setPremultiplyAlpha) {
  2549. source.setPremultiplyAlpha(premultiplyAlpha);
  2550. }
  2551. gl.texImage2D(this._glTextureType, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
  2552. }
  2553. else {
  2554. (premultiplyAlpha) && (gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true));
  2555. if (this.format == exports.TextureFormat.R5G6B5)
  2556. gl.texImage2D(this._glTextureType, 0, gl.RGB, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, source);
  2557. else
  2558. gl.texImage2D(this._glTextureType, 0, glFormat, glFormat, gl.UNSIGNED_BYTE, source);
  2559. (premultiplyAlpha) && (gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false));
  2560. }
  2561. if (this._mipmap) {
  2562. gl.generateMipmap(this._glTextureType);
  2563. this._setGPUMemory(width * height * 4 * (1 + 1 / 3));
  2564. }
  2565. else {
  2566. this._setGPUMemory(width * height * 4);
  2567. }
  2568. if (this._canRead) {
  2569. if (ILaya.Render.isConchApp) {
  2570. this._pixels = new Uint8Array(source._nativeObj.getImageData(0, 0, width, height));
  2571. }
  2572. else {
  2573. ILaya.Browser.canvas.size(width, height);
  2574. ILaya.Browser.canvas.clear();
  2575. ILaya.Browser.context.drawImage(source, 0, 0, width, height);
  2576. this._pixels = new Uint8Array(ILaya.Browser.context.getImageData(0, 0, width, height).data.buffer);
  2577. }
  2578. }
  2579. this._readyed = true;
  2580. this._activeResource();
  2581. }
  2582. setPixels(pixels, miplevel = 0) {
  2583. if (this._gpuCompressFormat())
  2584. throw "Texture2D:the format is GPU compression format.";
  2585. if (!pixels)
  2586. throw "Texture2D:pixels can't be null.";
  2587. var width = Math.max(this._width >> miplevel, 1);
  2588. var height = Math.max(this._height >> miplevel, 1);
  2589. var pixelsCount = width * height * this._getFormatByteCount();
  2590. if (pixels.length < pixelsCount)
  2591. throw "Texture2D:pixels length should at least " + pixelsCount + ".";
  2592. this._setPixels(pixels, miplevel, width, height);
  2593. if (this._canRead)
  2594. this._pixels = pixels;
  2595. this._readyed = true;
  2596. this._activeResource();
  2597. }
  2598. setSubPixels(x, y, width, height, pixels, miplevel = 0) {
  2599. if (this._gpuCompressFormat())
  2600. throw "Texture2D:the format is GPU compression format.";
  2601. if (!pixels)
  2602. throw "Texture2D:pixels can't be null.";
  2603. var gl = LayaGL.instance;
  2604. var textureType = this._glTextureType;
  2605. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  2606. var glFormat = this._getGLFormat();
  2607. switch (this.format) {
  2608. case exports.TextureFormat.R8G8B8:
  2609. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  2610. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  2611. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  2612. break;
  2613. case exports.TextureFormat.R5G6B5:
  2614. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 2);
  2615. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.UNSIGNED_SHORT_5_6_5, pixels);
  2616. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  2617. break;
  2618. case exports.TextureFormat.R32G32B32A32:
  2619. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.FLOAT, pixels);
  2620. break;
  2621. default:
  2622. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  2623. }
  2624. this._readyed = true;
  2625. this._activeResource();
  2626. }
  2627. setCompressData(data) {
  2628. switch (this._format) {
  2629. case exports.TextureFormat.DXT1:
  2630. case exports.TextureFormat.DXT5:
  2631. this._pharseDDS(data);
  2632. break;
  2633. case exports.TextureFormat.ETC1RGB:
  2634. this._pharseKTX(data);
  2635. break;
  2636. case exports.TextureFormat.PVRTCRGB_2BPPV:
  2637. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  2638. case exports.TextureFormat.PVRTCRGB_4BPPV:
  2639. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  2640. this._pharsePVR(data);
  2641. break;
  2642. default:
  2643. throw "Texture2D:unkonwn format.";
  2644. }
  2645. }
  2646. getPixels() {
  2647. if (this._canRead)
  2648. return this._pixels;
  2649. else
  2650. throw new Error("Texture2D: must set texture canRead is true.");
  2651. }
  2652. }
  2653. Texture2D.TEXTURE2D = "TEXTURE2D";
  2654. Texture2D.grayTexture = null;
  2655. Texture2D.whiteTexture = null;
  2656. Texture2D.blackTexture = null;
  2657. class BaseShader extends Resource {
  2658. constructor() {
  2659. super();
  2660. }
  2661. }
  2662. class RenderState2D {
  2663. static mat2MatArray(mat, matArray) {
  2664. var m = mat;
  2665. var m4 = matArray;
  2666. m4[0] = m.a;
  2667. m4[1] = m.b;
  2668. m4[2] = RenderState2D.EMPTYMAT4_ARRAY[2];
  2669. m4[3] = RenderState2D.EMPTYMAT4_ARRAY[3];
  2670. m4[4] = m.c;
  2671. m4[5] = m.d;
  2672. m4[6] = RenderState2D.EMPTYMAT4_ARRAY[6];
  2673. m4[7] = RenderState2D.EMPTYMAT4_ARRAY[7];
  2674. m4[8] = RenderState2D.EMPTYMAT4_ARRAY[8];
  2675. m4[9] = RenderState2D.EMPTYMAT4_ARRAY[9];
  2676. m4[10] = RenderState2D.EMPTYMAT4_ARRAY[10];
  2677. m4[11] = RenderState2D.EMPTYMAT4_ARRAY[11];
  2678. m4[12] = m.tx;
  2679. m4[13] = m.ty;
  2680. m4[14] = RenderState2D.EMPTYMAT4_ARRAY[14];
  2681. m4[15] = RenderState2D.EMPTYMAT4_ARRAY[15];
  2682. return matArray;
  2683. }
  2684. static restoreTempArray() {
  2685. RenderState2D.TEMPMAT4_ARRAY[0] = 1;
  2686. RenderState2D.TEMPMAT4_ARRAY[1] = 0;
  2687. RenderState2D.TEMPMAT4_ARRAY[4] = 0;
  2688. RenderState2D.TEMPMAT4_ARRAY[5] = 1;
  2689. RenderState2D.TEMPMAT4_ARRAY[12] = 0;
  2690. RenderState2D.TEMPMAT4_ARRAY[13] = 0;
  2691. }
  2692. static clear() {
  2693. RenderState2D.worldScissorTest = false;
  2694. RenderState2D.worldAlpha = 1;
  2695. }
  2696. }
  2697. RenderState2D._MAXSIZE = 99999999;
  2698. RenderState2D.EMPTYMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  2699. RenderState2D.TEMPMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  2700. RenderState2D.worldMatrix4 = RenderState2D.TEMPMAT4_ARRAY;
  2701. RenderState2D.worldMatrix = new Matrix();
  2702. RenderState2D.matWVP = null;
  2703. RenderState2D.worldAlpha = 1.0;
  2704. RenderState2D.worldScissorTest = false;
  2705. RenderState2D.width = 0;
  2706. RenderState2D.height = 0;
  2707. class RenderTexture2D extends BaseTexture {
  2708. constructor(width, height, format = exports.RenderTextureFormat.R8G8B8, depthStencilFormat = exports.RenderTextureDepthFormat.DEPTH_16) {
  2709. super(format, false);
  2710. this._mgrKey = 0;
  2711. this._glTextureType = LayaGL.instance.TEXTURE_2D;
  2712. this._width = width;
  2713. this._height = height;
  2714. this._depthStencilFormat = depthStencilFormat;
  2715. this._create(width, height);
  2716. this.lock = true;
  2717. }
  2718. static get currentActive() {
  2719. return RenderTexture2D._currentActive;
  2720. }
  2721. get depthStencilFormat() {
  2722. return this._depthStencilFormat;
  2723. }
  2724. get defaulteTexture() {
  2725. return Texture2D.grayTexture;
  2726. }
  2727. getIsReady() {
  2728. return true;
  2729. }
  2730. get sourceWidth() {
  2731. return this._width;
  2732. }
  2733. get sourceHeight() {
  2734. return this._height;
  2735. }
  2736. get offsetX() {
  2737. return 0;
  2738. }
  2739. get offsetY() {
  2740. return 0;
  2741. }
  2742. _create(width, height) {
  2743. var gl = LayaGL.instance;
  2744. this._frameBuffer = gl.createFramebuffer();
  2745. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  2746. var glFormat = this._getGLFormat();
  2747. gl.texImage2D(this._glTextureType, 0, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, null);
  2748. this._setGPUMemory(width * height * 4);
  2749. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2750. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, 0);
  2751. if (this._depthStencilFormat !== exports.RenderTextureDepthFormat.DEPTHSTENCIL_NONE) {
  2752. this._depthStencilBuffer = gl.createRenderbuffer();
  2753. gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthStencilBuffer);
  2754. switch (this._depthStencilFormat) {
  2755. case exports.RenderTextureDepthFormat.DEPTH_16:
  2756. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  2757. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2758. break;
  2759. case exports.RenderTextureDepthFormat.STENCIL_8:
  2760. gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, width, height);
  2761. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2762. break;
  2763. case exports.RenderTextureDepthFormat.DEPTHSTENCIL_24_8:
  2764. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
  2765. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2766. break;
  2767. default:
  2768. }
  2769. }
  2770. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2771. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  2772. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  2773. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  2774. this._setFilterMode(this._filterMode);
  2775. this._setAnisotropy(this._anisoLevel);
  2776. this._readyed = true;
  2777. this._activeResource();
  2778. }
  2779. generateMipmap() {
  2780. if (this._isPot(this.width) && this._isPot(this.height)) {
  2781. this._mipmap = true;
  2782. LayaGL.instance.generateMipmap(this._glTextureType);
  2783. this._setFilterMode(this._filterMode);
  2784. this._setGPUMemory(this.width * this.height * 4 * (1 + 1 / 3));
  2785. }
  2786. else {
  2787. this._mipmap = false;
  2788. this._setGPUMemory(this.width * this.height * 4);
  2789. }
  2790. }
  2791. static pushRT() {
  2792. RenderTexture2D.rtStack.push({ rt: RenderTexture2D._currentActive, w: RenderState2D.width, h: RenderState2D.height });
  2793. }
  2794. static popRT() {
  2795. var gl = LayaGL.instance;
  2796. var top = RenderTexture2D.rtStack.pop();
  2797. if (top) {
  2798. if (RenderTexture2D._currentActive != top.rt) {
  2799. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, top.rt ? top.rt._frameBuffer : null);
  2800. RenderTexture2D._currentActive = top.rt;
  2801. }
  2802. gl.viewport(0, 0, top.w, top.h);
  2803. RenderState2D.width = top.w;
  2804. RenderState2D.height = top.h;
  2805. }
  2806. }
  2807. start() {
  2808. var gl = LayaGL.instance;
  2809. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2810. this._lastRT = RenderTexture2D._currentActive;
  2811. RenderTexture2D._currentActive = this;
  2812. this._readyed = true;
  2813. gl.viewport(0, 0, this._width, this._height);
  2814. this._lastWidth = RenderState2D.width;
  2815. this._lastHeight = RenderState2D.height;
  2816. RenderState2D.width = this._width;
  2817. RenderState2D.height = this._height;
  2818. BaseShader.activeShader = null;
  2819. }
  2820. end() {
  2821. var gl = LayaGL.instance;
  2822. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2823. RenderTexture2D._currentActive = null;
  2824. this._readyed = true;
  2825. }
  2826. restore() {
  2827. var gl = LayaGL.instance;
  2828. if (this._lastRT != RenderTexture2D._currentActive) {
  2829. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._lastRT ? this._lastRT._frameBuffer : null);
  2830. RenderTexture2D._currentActive = this._lastRT;
  2831. }
  2832. this._readyed = true;
  2833. gl.viewport(0, 0, this._lastWidth, this._lastHeight);
  2834. RenderState2D.width = this._lastWidth;
  2835. RenderState2D.height = this._lastHeight;
  2836. BaseShader.activeShader = null;
  2837. }
  2838. clear(r = 0.0, g = 0.0, b = 0.0, a = 1.0) {
  2839. var gl = LayaGL.instance;
  2840. gl.clearColor(r, g, b, a);
  2841. var clearFlag = gl.COLOR_BUFFER_BIT;
  2842. switch (this._depthStencilFormat) {
  2843. case gl.DEPTH_COMPONENT16:
  2844. clearFlag |= gl.DEPTH_BUFFER_BIT;
  2845. break;
  2846. case gl.STENCIL_INDEX8:
  2847. clearFlag |= gl.STENCIL_BUFFER_BIT;
  2848. break;
  2849. case gl.DEPTH_STENCIL:
  2850. clearFlag |= gl.DEPTH_BUFFER_BIT;
  2851. clearFlag |= gl.STENCIL_BUFFER_BIT;
  2852. break;
  2853. }
  2854. gl.clear(clearFlag);
  2855. }
  2856. getData(x, y, width, height) {
  2857. if (ILaya.Render.isConchApp && window.conchConfig.threadMode == 2) {
  2858. throw "native 2 thread mode use getDataAsync";
  2859. }
  2860. var gl = LayaGL.instance;
  2861. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2862. var canRead = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE);
  2863. if (!canRead) {
  2864. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2865. return null;
  2866. }
  2867. var pixels = new Uint8Array(this._width * this._height * 4);
  2868. var glFormat = this._getGLFormat();
  2869. gl.readPixels(x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  2870. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2871. return pixels;
  2872. }
  2873. getDataAsync(x, y, width, height, callBack) {
  2874. var gl = LayaGL.instance;
  2875. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2876. gl.readPixelsAsync(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, function (data) {
  2877. callBack(new Uint8Array(data));
  2878. });
  2879. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2880. }
  2881. recycle() {
  2882. }
  2883. _disposeResource() {
  2884. if (this._frameBuffer) {
  2885. var gl = LayaGL.instance;
  2886. gl.deleteTexture(this._glTexture);
  2887. gl.deleteFramebuffer(this._frameBuffer);
  2888. gl.deleteRenderbuffer(this._depthStencilBuffer);
  2889. this._glTexture = null;
  2890. this._frameBuffer = null;
  2891. this._depthStencilBuffer = null;
  2892. this._setGPUMemory(0);
  2893. }
  2894. }
  2895. }
  2896. RenderTexture2D.rtStack = [];
  2897. RenderTexture2D.defuv = [0, 0, 1, 0, 1, 1, 0, 1];
  2898. RenderTexture2D.flipyuv = [0, 1, 1, 1, 1, 0, 0, 0];
  2899. class WebGLRTMgr {
  2900. static getRT(w, h) {
  2901. w = w | 0;
  2902. h = h | 0;
  2903. if (w >= 10000) {
  2904. console.error('getRT error! w too big');
  2905. }
  2906. var ret;
  2907. ret = new RenderTexture2D(w, h, exports.RenderTextureFormat.R8G8B8A8, -1);
  2908. return ret;
  2909. }
  2910. static releaseRT(rt) {
  2911. rt.destroy();
  2912. return;
  2913. }
  2914. }
  2915. WebGLRTMgr.dict = {};
  2916. class BlendMode {
  2917. static _init_(gl) {
  2918. BlendMode.fns = [
  2919. BlendMode.BlendNormal,
  2920. BlendMode.BlendAdd,
  2921. BlendMode.BlendMultiply,
  2922. BlendMode.BlendScreen,
  2923. BlendMode.BlendOverlay,
  2924. BlendMode.BlendLight,
  2925. BlendMode.BlendMask,
  2926. BlendMode.BlendDestinationOut,
  2927. BlendMode.BlendAddOld
  2928. ];
  2929. BlendMode.targetFns = [
  2930. BlendMode.BlendNormalTarget,
  2931. BlendMode.BlendAddTarget,
  2932. BlendMode.BlendMultiplyTarget,
  2933. BlendMode.BlendScreenTarget,
  2934. BlendMode.BlendOverlayTarget,
  2935. BlendMode.BlendLightTarget,
  2936. BlendMode.BlendMask,
  2937. BlendMode.BlendDestinationOut,
  2938. BlendMode.BlendAddTargetOld
  2939. ];
  2940. }
  2941. static BlendNormal(gl) {
  2942. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA, true);
  2943. }
  2944. static BlendAddOld(gl) {
  2945. WebGLContext.setBlendFunc(gl, gl.ONE, gl.DST_ALPHA, true);
  2946. }
  2947. static BlendAdd(gl) {
  2948. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2949. }
  2950. static BlendMultiply(gl) {
  2951. WebGLContext.setBlendFunc(gl, gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA, true);
  2952. }
  2953. static BlendScreen(gl) {
  2954. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2955. }
  2956. static BlendOverlay(gl) {
  2957. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_COLOR, true);
  2958. }
  2959. static BlendLight(gl) {
  2960. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2961. }
  2962. static BlendNormalTarget(gl) {
  2963. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA, true);
  2964. }
  2965. static BlendAddTargetOld(gl) {
  2966. WebGLContext.setBlendFunc(gl, gl.ONE, gl.DST_ALPHA, true);
  2967. }
  2968. static BlendAddTarget(gl) {
  2969. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2970. }
  2971. static BlendMultiplyTarget(gl) {
  2972. WebGLContext.setBlendFunc(gl, gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA, true);
  2973. }
  2974. static BlendScreenTarget(gl) {
  2975. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2976. }
  2977. static BlendOverlayTarget(gl) {
  2978. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_COLOR, true);
  2979. }
  2980. static BlendLightTarget(gl) {
  2981. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE, true);
  2982. }
  2983. static BlendMask(gl) {
  2984. WebGLContext.setBlendFunc(gl, gl.ZERO, gl.SRC_ALPHA, true);
  2985. }
  2986. static BlendDestinationOut(gl) {
  2987. WebGLContext.setBlendFunc(gl, gl.ZERO, gl.ZERO, true);
  2988. }
  2989. }
  2990. BlendMode.activeBlendFunction = null;
  2991. BlendMode.NAMES = [
  2992. "normal",
  2993. "add",
  2994. "multiply",
  2995. "screen",
  2996. "overlay",
  2997. "light",
  2998. "mask",
  2999. "destination-out",
  3000. "add_old"
  3001. ];
  3002. BlendMode.TOINT = {
  3003. "normal": 0,
  3004. "add": 1,
  3005. "multiply": 2,
  3006. "screen": 3,
  3007. "overlay": 4,
  3008. "light": 5,
  3009. "mask": 6,
  3010. "destination-out": 7,
  3011. "lighter": 1,
  3012. "lighter_old": 8,
  3013. "add_old": 8
  3014. };
  3015. BlendMode.NORMAL = "normal";
  3016. BlendMode.MASK = "mask";
  3017. BlendMode.LIGHTER = "lighter";
  3018. class ShaderDefinesBase {
  3019. constructor(name2int, int2name, int2nameMap) {
  3020. this._value = 0;
  3021. this._name2int = name2int;
  3022. this._int2name = int2name;
  3023. this._int2nameMap = int2nameMap;
  3024. }
  3025. add(value) {
  3026. if (typeof (value) == 'string') {
  3027. this._value |= this._name2int[value];
  3028. }
  3029. else {
  3030. this._value |= value;
  3031. }
  3032. return this._value;
  3033. }
  3034. addInt(value) {
  3035. this._value |= value;
  3036. return this._value;
  3037. }
  3038. remove(value) {
  3039. if (typeof (value) == 'string') {
  3040. this._value &= ~(this._name2int[value]);
  3041. }
  3042. else {
  3043. this._value &= (~value);
  3044. }
  3045. return this._value;
  3046. }
  3047. isDefine(def) {
  3048. return (this._value & def) === def;
  3049. }
  3050. getValue() {
  3051. return this._value;
  3052. }
  3053. setValue(value) {
  3054. this._value = value;
  3055. }
  3056. toNameDic() {
  3057. var r = this._int2nameMap[this._value];
  3058. return r ? r : ShaderDefinesBase._toText(this._value, this._int2name, this._int2nameMap);
  3059. }
  3060. static _reg(name, value, _name2int, _int2name) {
  3061. _name2int[name] = value;
  3062. _int2name[value] = name;
  3063. }
  3064. static _toText(value, _int2name, _int2nameMap) {
  3065. var r = _int2nameMap[value];
  3066. if (r)
  3067. return r;
  3068. var o = {};
  3069. var d = 1;
  3070. for (var i = 0; i < 32; i++) {
  3071. d = 1 << i;
  3072. if (d > value)
  3073. break;
  3074. if (value & d) {
  3075. var name = _int2name[d];
  3076. name && (o[name] = "");
  3077. }
  3078. }
  3079. _int2nameMap[value] = o;
  3080. return o;
  3081. }
  3082. static _toInt(names, _name2int) {
  3083. var words = names.split('.');
  3084. var num = 0;
  3085. for (var i = 0, n = words.length; i < n; i++) {
  3086. var value = _name2int[words[i]];
  3087. if (!value)
  3088. throw new Error("Defines to int err:" + names + "/" + words[i]);
  3089. num |= value;
  3090. }
  3091. return num;
  3092. }
  3093. }
  3094. class ShaderDefines2D extends ShaderDefinesBase {
  3095. constructor() {
  3096. super(ShaderDefines2D.__name2int, ShaderDefines2D.__int2name, ShaderDefines2D.__int2nameMap);
  3097. }
  3098. static __init__() {
  3099. ShaderDefines2D.reg("TEXTURE2D", ShaderDefines2D.TEXTURE2D);
  3100. ShaderDefines2D.reg("PRIMITIVE", ShaderDefines2D.PRIMITIVE);
  3101. ShaderDefines2D.reg("GLOW_FILTER", ShaderDefines2D.FILTERGLOW);
  3102. ShaderDefines2D.reg("BLUR_FILTER", ShaderDefines2D.FILTERBLUR);
  3103. ShaderDefines2D.reg("COLOR_FILTER", ShaderDefines2D.FILTERCOLOR);
  3104. ShaderDefines2D.reg("COLOR_ADD", ShaderDefines2D.COLORADD);
  3105. ShaderDefines2D.reg("WORLDMAT", ShaderDefines2D.WORLDMAT);
  3106. ShaderDefines2D.reg("FILLTEXTURE", ShaderDefines2D.FILLTEXTURE);
  3107. ShaderDefines2D.reg('MVP3D', ShaderDefines2D.MVP3D);
  3108. }
  3109. static reg(name, value) {
  3110. this._reg(name, value, ShaderDefines2D.__name2int, ShaderDefines2D.__int2name);
  3111. }
  3112. static toText(value, int2name, int2nameMap) {
  3113. return this._toText(value, int2name, int2nameMap);
  3114. }
  3115. static toInt(names) {
  3116. return this._toInt(names, ShaderDefines2D.__name2int);
  3117. }
  3118. }
  3119. ShaderDefines2D.TEXTURE2D = 0x01;
  3120. ShaderDefines2D.PRIMITIVE = 0x04;
  3121. ShaderDefines2D.FILTERGLOW = 0x08;
  3122. ShaderDefines2D.FILTERBLUR = 0x10;
  3123. ShaderDefines2D.FILTERCOLOR = 0x20;
  3124. ShaderDefines2D.COLORADD = 0x40;
  3125. ShaderDefines2D.WORLDMAT = 0x80;
  3126. ShaderDefines2D.FILLTEXTURE = 0x100;
  3127. ShaderDefines2D.SKINMESH = 0x200;
  3128. ShaderDefines2D.MVP3D = 0x800;
  3129. ShaderDefines2D.NOOPTMASK = ShaderDefines2D.FILTERGLOW | ShaderDefines2D.FILTERBLUR | ShaderDefines2D.FILTERCOLOR | ShaderDefines2D.FILLTEXTURE;
  3130. ShaderDefines2D.__name2int = {};
  3131. ShaderDefines2D.__int2name = [];
  3132. ShaderDefines2D.__int2nameMap = [];
  3133. class Stat {
  3134. static show(x = 0, y = 0) {
  3135. Stat._StatRender.show(x, y);
  3136. }
  3137. static enable() {
  3138. Stat._StatRender.enable();
  3139. }
  3140. static hide() {
  3141. Stat._StatRender.hide();
  3142. }
  3143. static clear() {
  3144. Stat.trianglesFaces = Stat.renderBatches = Stat.savedRenderBatches = Stat.shaderCall = Stat.spriteRenderUseCacheCount = Stat.frustumCulling = Stat.octreeNodeCulling = Stat.canvasNormal = Stat.canvasBitmap = Stat.canvasReCache = 0;
  3145. }
  3146. static set onclick(fn) {
  3147. Stat._StatRender.set_onclick(fn);
  3148. }
  3149. }
  3150. Stat.FPS = 0;
  3151. Stat.loopCount = 0;
  3152. Stat.shaderCall = 0;
  3153. Stat.renderBatches = 0;
  3154. Stat.savedRenderBatches = 0;
  3155. Stat.trianglesFaces = 0;
  3156. Stat.spriteCount = 0;
  3157. Stat.spriteRenderUseCacheCount = 0;
  3158. Stat.frustumCulling = 0;
  3159. Stat.octreeNodeCulling = 0;
  3160. Stat.canvasNormal = 0;
  3161. Stat.canvasBitmap = 0;
  3162. Stat.canvasReCache = 0;
  3163. Stat.renderSlow = false;
  3164. Stat._fpsData = [];
  3165. Stat._timer = 0;
  3166. Stat._count = 0;
  3167. Stat._StatRender = null;
  3168. class StringKey {
  3169. constructor() {
  3170. this._strsToID = {};
  3171. this._idToStrs = [];
  3172. this._length = 0;
  3173. }
  3174. add(str) {
  3175. var index = this._strsToID[str];
  3176. if (index != null)
  3177. return index;
  3178. this._idToStrs[this._length] = str;
  3179. return this._strsToID[str] = this._length++;
  3180. }
  3181. getID(str) {
  3182. var index = this._strsToID[str];
  3183. return index == null ? -1 : index;
  3184. }
  3185. getName(id) {
  3186. var str = this._idToStrs[id];
  3187. return str == null ? undefined : str;
  3188. }
  3189. }
  3190. class Shader extends BaseShader {
  3191. constructor(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  3192. super();
  3193. this._attribInfo = null;
  3194. this.customCompile = false;
  3195. this._curActTexIndex = 0;
  3196. this.tag = {};
  3197. this._program = null;
  3198. this._params = null;
  3199. this._paramsMap = {};
  3200. if ((!vs) || (!ps))
  3201. throw "Shader Error";
  3202. this._attribInfo = bindAttrib;
  3203. this._id = ++Shader._count;
  3204. this._vs = vs;
  3205. this._ps = ps;
  3206. this._nameMap = nameMap ? nameMap : {};
  3207. saveName != null && (Shader.sharders[saveName] = this);
  3208. this.recreateResource();
  3209. this.lock = true;
  3210. }
  3211. static getShader(name) {
  3212. return Shader.sharders[name];
  3213. }
  3214. static create(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  3215. return new Shader(vs, ps, saveName, nameMap, bindAttrib);
  3216. }
  3217. static withCompile(nameID, define, shaderName, createShader) {
  3218. if (shaderName && Shader.sharders[shaderName])
  3219. return Shader.sharders[shaderName];
  3220. var pre = Shader._preCompileShader[Shader.SHADERNAME2ID * nameID];
  3221. if (!pre)
  3222. throw new Error("withCompile shader err!" + nameID);
  3223. return pre.createShader(define, shaderName, createShader, null);
  3224. }
  3225. static withCompile2D(nameID, mainID, define, shaderName, createShader, bindAttrib = null) {
  3226. if (shaderName && Shader.sharders[shaderName])
  3227. return Shader.sharders[shaderName];
  3228. var pre = Shader._preCompileShader[Shader.SHADERNAME2ID * nameID + mainID];
  3229. if (!pre)
  3230. throw new Error("withCompile shader err!" + nameID + " " + mainID);
  3231. return pre.createShader(define, shaderName, createShader, bindAttrib);
  3232. }
  3233. static addInclude(fileName, txt) {
  3234. ILaya.ShaderCompile.addInclude(fileName, txt);
  3235. }
  3236. static preCompile(nameID, vs, ps, nameMap) {
  3237. var id = Shader.SHADERNAME2ID * nameID;
  3238. Shader._preCompileShader[id] = new ILaya.ShaderCompile(vs, ps, nameMap);
  3239. }
  3240. static preCompile2D(nameID, mainID, vs, ps, nameMap) {
  3241. var id = Shader.SHADERNAME2ID * nameID + mainID;
  3242. Shader._preCompileShader[id] = new ILaya.ShaderCompile(vs, ps, nameMap);
  3243. }
  3244. recreateResource() {
  3245. this._compile();
  3246. this._setGPUMemory(0);
  3247. }
  3248. _disposeResource() {
  3249. WebGLContext.mainContext.deleteShader(this._vshader);
  3250. WebGLContext.mainContext.deleteShader(this._pshader);
  3251. WebGLContext.mainContext.deleteProgram(this._program);
  3252. this._vshader = this._pshader = this._program = null;
  3253. this._params = null;
  3254. this._paramsMap = {};
  3255. this._setGPUMemory(0);
  3256. this._curActTexIndex = 0;
  3257. }
  3258. _compile() {
  3259. if (!this._vs || !this._ps || this._params)
  3260. return;
  3261. this._reCompile = true;
  3262. this._params = [];
  3263. var result;
  3264. if (this.customCompile)
  3265. result = ILaya.ShaderCompile.preGetParams(this._vs, this._ps);
  3266. var gl = WebGLContext.mainContext;
  3267. this._program = gl.createProgram();
  3268. this._vshader = Shader._createShader(gl, this._vs, gl.VERTEX_SHADER);
  3269. this._pshader = Shader._createShader(gl, this._ps, gl.FRAGMENT_SHADER);
  3270. gl.attachShader(this._program, this._vshader);
  3271. gl.attachShader(this._program, this._pshader);
  3272. var one, i, n, location;
  3273. var attribDescNum = this._attribInfo ? this._attribInfo.length : 0;
  3274. for (i = 0; i < attribDescNum; i += 2) {
  3275. gl.bindAttribLocation(this._program, this._attribInfo[i + 1], this._attribInfo[i]);
  3276. }
  3277. gl.linkProgram(this._program);
  3278. if (!this.customCompile && !gl.getProgramParameter(this._program, gl.LINK_STATUS)) {
  3279. throw gl.getProgramInfoLog(this._program);
  3280. }
  3281. var nUniformNum = this.customCompile ? result.uniforms.length : gl.getProgramParameter(this._program, gl.ACTIVE_UNIFORMS);
  3282. for (i = 0; i < nUniformNum; i++) {
  3283. var uniform = this.customCompile ? result.uniforms[i] : gl.getActiveUniform(this._program, i);
  3284. location = gl.getUniformLocation(this._program, uniform.name);
  3285. one = { vartype: "uniform", glfun: null, ivartype: 1, location: location, name: uniform.name, type: uniform.type, isArray: false, isSame: false, preValue: null, indexOfParams: 0 };
  3286. if (one.name.indexOf('[0]') > 0) {
  3287. one.name = one.name.substr(0, one.name.length - 3);
  3288. one.isArray = true;
  3289. one.location = gl.getUniformLocation(this._program, one.name);
  3290. }
  3291. this._params.push(one);
  3292. }
  3293. for (i = 0, n = this._params.length; i < n; i++) {
  3294. one = this._params[i];
  3295. one.indexOfParams = i;
  3296. one.index = 1;
  3297. one.value = [one.location, null];
  3298. one.codename = one.name;
  3299. one.name = this._nameMap[one.codename] ? this._nameMap[one.codename] : one.codename;
  3300. this._paramsMap[one.name] = one;
  3301. one._this = this;
  3302. one.uploadedValue = [];
  3303. switch (one.type) {
  3304. case gl.INT:
  3305. one.fun = one.isArray ? this._uniform1iv : this._uniform1i;
  3306. break;
  3307. case gl.FLOAT:
  3308. one.fun = one.isArray ? this._uniform1fv : this._uniform1f;
  3309. break;
  3310. case gl.FLOAT_VEC2:
  3311. one.fun = one.isArray ? this._uniform_vec2v : this._uniform_vec2;
  3312. break;
  3313. case gl.FLOAT_VEC3:
  3314. one.fun = one.isArray ? this._uniform_vec3v : this._uniform_vec3;
  3315. break;
  3316. case gl.FLOAT_VEC4:
  3317. one.fun = one.isArray ? this._uniform_vec4v : this._uniform_vec4;
  3318. break;
  3319. case gl.SAMPLER_2D:
  3320. one.fun = this._uniform_sampler2D;
  3321. break;
  3322. case gl.SAMPLER_CUBE:
  3323. one.fun = this._uniform_samplerCube;
  3324. break;
  3325. case gl.FLOAT_MAT4:
  3326. one.glfun = gl.uniformMatrix4fv;
  3327. one.fun = this._uniformMatrix4fv;
  3328. break;
  3329. case gl.BOOL:
  3330. one.fun = this._uniform1i;
  3331. break;
  3332. case gl.FLOAT_MAT2:
  3333. case gl.FLOAT_MAT3:
  3334. throw new Error("compile shader err!");
  3335. default:
  3336. throw new Error("compile shader err!");
  3337. }
  3338. }
  3339. }
  3340. static _createShader(gl, str, type) {
  3341. var shader = gl.createShader(type);
  3342. gl.shaderSource(shader, str);
  3343. gl.compileShader(shader);
  3344. if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  3345. return shader;
  3346. }
  3347. else {
  3348. console.log(gl.getShaderInfoLog(shader));
  3349. return null;
  3350. }
  3351. }
  3352. getUniform(name) {
  3353. return this._paramsMap[name];
  3354. }
  3355. _uniform1f(one, value) {
  3356. var uploadedValue = one.uploadedValue;
  3357. if (uploadedValue[0] !== value) {
  3358. WebGLContext.mainContext.uniform1f(one.location, uploadedValue[0] = value);
  3359. return 1;
  3360. }
  3361. return 0;
  3362. }
  3363. _uniform1fv(one, value) {
  3364. if (value.length < 4) {
  3365. var uploadedValue = one.uploadedValue;
  3366. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  3367. WebGLContext.mainContext.uniform1fv(one.location, value);
  3368. uploadedValue[0] = value[0];
  3369. uploadedValue[1] = value[1];
  3370. uploadedValue[2] = value[2];
  3371. uploadedValue[3] = value[3];
  3372. return 1;
  3373. }
  3374. return 0;
  3375. }
  3376. else {
  3377. WebGLContext.mainContext.uniform1fv(one.location, value);
  3378. return 1;
  3379. }
  3380. }
  3381. _uniform_vec2(one, value) {
  3382. var uploadedValue = one.uploadedValue;
  3383. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1]) {
  3384. WebGLContext.mainContext.uniform2f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1]);
  3385. return 1;
  3386. }
  3387. return 0;
  3388. }
  3389. _uniform_vec2v(one, value) {
  3390. if (value.length < 2) {
  3391. var uploadedValue = one.uploadedValue;
  3392. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  3393. WebGLContext.mainContext.uniform2fv(one.location, value);
  3394. uploadedValue[0] = value[0];
  3395. uploadedValue[1] = value[1];
  3396. uploadedValue[2] = value[2];
  3397. uploadedValue[3] = value[3];
  3398. return 1;
  3399. }
  3400. return 0;
  3401. }
  3402. else {
  3403. WebGLContext.mainContext.uniform2fv(one.location, value);
  3404. return 1;
  3405. }
  3406. }
  3407. _uniform_vec3(one, value) {
  3408. var uploadedValue = one.uploadedValue;
  3409. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2]) {
  3410. WebGLContext.mainContext.uniform3f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2]);
  3411. return 1;
  3412. }
  3413. return 0;
  3414. }
  3415. _uniform_vec3v(one, value) {
  3416. WebGLContext.mainContext.uniform3fv(one.location, value);
  3417. return 1;
  3418. }
  3419. _uniform_vec4(one, value) {
  3420. var uploadedValue = one.uploadedValue;
  3421. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  3422. WebGLContext.mainContext.uniform4f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2], uploadedValue[3] = value[3]);
  3423. return 1;
  3424. }
  3425. return 0;
  3426. }
  3427. _uniform_vec4v(one, value) {
  3428. WebGLContext.mainContext.uniform4fv(one.location, value);
  3429. return 1;
  3430. }
  3431. _uniformMatrix2fv(one, value) {
  3432. WebGLContext.mainContext.uniformMatrix2fv(one.location, false, value);
  3433. return 1;
  3434. }
  3435. _uniformMatrix3fv(one, value) {
  3436. WebGLContext.mainContext.uniformMatrix3fv(one.location, false, value);
  3437. return 1;
  3438. }
  3439. _uniformMatrix4fv(one, value) {
  3440. WebGLContext.mainContext.uniformMatrix4fv(one.location, false, value);
  3441. return 1;
  3442. }
  3443. _uniform1i(one, value) {
  3444. var uploadedValue = one.uploadedValue;
  3445. if (uploadedValue[0] !== value) {
  3446. WebGLContext.mainContext.uniform1i(one.location, uploadedValue[0] = value);
  3447. return 1;
  3448. }
  3449. return 0;
  3450. }
  3451. _uniform1iv(one, value) {
  3452. WebGLContext.mainContext.uniform1iv(one.location, value);
  3453. return 1;
  3454. }
  3455. _uniform_ivec2(one, value) {
  3456. var uploadedValue = one.uploadedValue;
  3457. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1]) {
  3458. WebGLContext.mainContext.uniform2i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1]);
  3459. return 1;
  3460. }
  3461. return 0;
  3462. }
  3463. _uniform_ivec2v(one, value) {
  3464. WebGLContext.mainContext.uniform2iv(one.location, value);
  3465. return 1;
  3466. }
  3467. _uniform_vec3i(one, value) {
  3468. var uploadedValue = one.uploadedValue;
  3469. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2]) {
  3470. WebGLContext.mainContext.uniform3i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2]);
  3471. return 1;
  3472. }
  3473. return 0;
  3474. }
  3475. _uniform_vec3vi(one, value) {
  3476. WebGLContext.mainContext.uniform3iv(one.location, value);
  3477. return 1;
  3478. }
  3479. _uniform_vec4i(one, value) {
  3480. var uploadedValue = one.uploadedValue;
  3481. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  3482. WebGLContext.mainContext.uniform4i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2], uploadedValue[3] = value[3]);
  3483. return 1;
  3484. }
  3485. return 0;
  3486. }
  3487. _uniform_vec4vi(one, value) {
  3488. WebGLContext.mainContext.uniform4iv(one.location, value);
  3489. return 1;
  3490. }
  3491. _uniform_sampler2D(one, value) {
  3492. var gl = WebGLContext.mainContext;
  3493. var uploadedValue = one.uploadedValue;
  3494. if (uploadedValue[0] == null) {
  3495. uploadedValue[0] = this._curActTexIndex;
  3496. gl.uniform1i(one.location, this._curActTexIndex);
  3497. WebGLContext.activeTexture(gl, gl.TEXTURE0 + this._curActTexIndex);
  3498. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, value);
  3499. this._curActTexIndex++;
  3500. return 1;
  3501. }
  3502. else {
  3503. WebGLContext.activeTexture(gl, gl.TEXTURE0 + uploadedValue[0]);
  3504. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, value);
  3505. return 0;
  3506. }
  3507. }
  3508. _uniform_samplerCube(one, value) {
  3509. var gl = WebGLContext.mainContext;
  3510. var uploadedValue = one.uploadedValue;
  3511. if (uploadedValue[0] == null) {
  3512. uploadedValue[0] = this._curActTexIndex;
  3513. gl.uniform1i(one.location, this._curActTexIndex);
  3514. WebGLContext.activeTexture(gl, gl.TEXTURE0 + this._curActTexIndex);
  3515. WebGLContext.bindTexture(gl, gl.TEXTURE_CUBE_MAP, value);
  3516. this._curActTexIndex++;
  3517. return 1;
  3518. }
  3519. else {
  3520. WebGLContext.activeTexture(gl, gl.TEXTURE0 + uploadedValue[0]);
  3521. WebGLContext.bindTexture(gl, gl.TEXTURE_CUBE_MAP, value);
  3522. return 0;
  3523. }
  3524. }
  3525. _noSetValue(one) {
  3526. console.log("no....:" + one.name);
  3527. }
  3528. uploadOne(name, value) {
  3529. WebGLContext.useProgram(WebGLContext.mainContext, this._program);
  3530. var one = this._paramsMap[name];
  3531. one.fun.call(this, one, value);
  3532. }
  3533. uploadTexture2D(value) {
  3534. var CTX = WebGLContext;
  3535. if (CTX._activeTextures[0] !== value) {
  3536. CTX.bindTexture(WebGLContext.mainContext, LayaGL.instance.TEXTURE_2D, value);
  3537. CTX._activeTextures[0] = value;
  3538. }
  3539. }
  3540. upload(shaderValue, params = null) {
  3541. BaseShader.activeShader = BaseShader.bindShader = this;
  3542. var gl = WebGLContext.mainContext;
  3543. WebGLContext.useProgram(gl, this._program);
  3544. if (this._reCompile) {
  3545. params = this._params;
  3546. this._reCompile = false;
  3547. }
  3548. else {
  3549. params = params || this._params;
  3550. }
  3551. var one, value, n = params.length, shaderCall = 0;
  3552. for (var i = 0; i < n; i++) {
  3553. one = params[i];
  3554. if ((value = shaderValue[one.name]) !== null)
  3555. shaderCall += one.fun.call(this, one, value);
  3556. }
  3557. Stat.shaderCall += shaderCall;
  3558. }
  3559. uploadArray(shaderValue, length, _bufferUsage) {
  3560. BaseShader.activeShader = this;
  3561. BaseShader.bindShader = this;
  3562. WebGLContext.useProgram(WebGLContext.mainContext, this._program);
  3563. var params = this._params, value;
  3564. var one, shaderCall = 0;
  3565. for (var i = length - 2; i >= 0; i -= 2) {
  3566. one = this._paramsMap[shaderValue[i]];
  3567. if (!one)
  3568. continue;
  3569. value = shaderValue[i + 1];
  3570. if (value != null) {
  3571. _bufferUsage && _bufferUsage[one.name] && _bufferUsage[one.name].bind();
  3572. shaderCall += one.fun.call(this, one, value);
  3573. }
  3574. }
  3575. Stat.shaderCall += shaderCall;
  3576. }
  3577. getParams() {
  3578. return this._params;
  3579. }
  3580. setAttributesLocation(attribDesc) {
  3581. this._attribInfo = attribDesc;
  3582. }
  3583. }
  3584. Shader._count = 0;
  3585. Shader._preCompileShader = {};
  3586. Shader.SHADERNAME2ID = 0.0002;
  3587. Shader.nameKey = new StringKey();
  3588. Shader.sharders = new Array(0x20);
  3589. class Shader2X extends Shader {
  3590. constructor(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  3591. super(vs, ps, saveName, nameMap, bindAttrib);
  3592. this._params2dQuick2 = null;
  3593. this._shaderValueWidth = 0;
  3594. this._shaderValueHeight = 0;
  3595. }
  3596. _disposeResource() {
  3597. super._disposeResource();
  3598. this._params2dQuick2 = null;
  3599. }
  3600. upload2dQuick2(shaderValue) {
  3601. this.upload(shaderValue, this._params2dQuick2 || this._make2dQuick2());
  3602. }
  3603. _make2dQuick2() {
  3604. if (!this._params2dQuick2) {
  3605. this._params2dQuick2 = [];
  3606. var params = this._params, one;
  3607. for (var i = 0, n = params.length; i < n; i++) {
  3608. one = params[i];
  3609. if (one.name !== "size")
  3610. this._params2dQuick2.push(one);
  3611. }
  3612. }
  3613. return this._params2dQuick2;
  3614. }
  3615. static create(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  3616. return new Shader2X(vs, ps, saveName, nameMap, bindAttrib);
  3617. }
  3618. }
  3619. class Value2D {
  3620. constructor(mainID, subID) {
  3621. this.defines = new ShaderDefines2D();
  3622. this.size = [0, 0];
  3623. this.alpha = 1.0;
  3624. this.ALPHA = 1.0;
  3625. this.subID = 0;
  3626. this.ref = 1;
  3627. this._cacheID = 0;
  3628. this.clipMatDir = [ILaya.Context._MAXSIZE, 0, 0, ILaya.Context._MAXSIZE];
  3629. this.clipMatPos = [0, 0];
  3630. this.clipOff = [0, 0];
  3631. this.mainID = mainID;
  3632. this.subID = subID;
  3633. this.textureHost = null;
  3634. this.texture = null;
  3635. this.color = null;
  3636. this.colorAdd = null;
  3637. this.u_mmat2 = null;
  3638. this._cacheID = mainID | subID;
  3639. this._inClassCache = Value2D._cache[this._cacheID];
  3640. if (mainID > 0 && !this._inClassCache) {
  3641. this._inClassCache = Value2D._cache[this._cacheID] = [];
  3642. this._inClassCache._length = 0;
  3643. }
  3644. this.clear();
  3645. }
  3646. static _initone(type, classT) {
  3647. Value2D._typeClass[type] = classT;
  3648. Value2D._cache[type] = [];
  3649. Value2D._cache[type]._length = 0;
  3650. }
  3651. static __init__() {
  3652. }
  3653. setValue(value) { }
  3654. _ShaderWithCompile() {
  3655. var ret = Shader.withCompile2D(0, this.mainID, this.defines.toNameDic(), this.mainID | this.defines._value, Shader2X.create, this._attribLocation);
  3656. return ret;
  3657. }
  3658. upload() {
  3659. var renderstate2d = RenderState2D;
  3660. RenderState2D.worldMatrix4 === RenderState2D.TEMPMAT4_ARRAY || this.defines.addInt(ShaderDefines2D.WORLDMAT);
  3661. this.mmat = renderstate2d.worldMatrix4;
  3662. if (RenderState2D.matWVP) {
  3663. this.defines.addInt(ShaderDefines2D.MVP3D);
  3664. this.u_MvpMatrix = RenderState2D.matWVP.elements;
  3665. }
  3666. var sd = Shader.sharders[this.mainID | this.defines._value] || this._ShaderWithCompile();
  3667. if (sd._shaderValueWidth !== renderstate2d.width || sd._shaderValueHeight !== renderstate2d.height) {
  3668. this.size[0] = renderstate2d.width;
  3669. this.size[1] = renderstate2d.height;
  3670. sd._shaderValueWidth = renderstate2d.width;
  3671. sd._shaderValueHeight = renderstate2d.height;
  3672. sd.upload(this, null);
  3673. }
  3674. else {
  3675. sd.upload(this, sd._params2dQuick2 || sd._make2dQuick2());
  3676. }
  3677. }
  3678. setFilters(value) {
  3679. this.filters = value;
  3680. if (!value)
  3681. return;
  3682. var n = value.length, f;
  3683. for (var i = 0; i < n; i++) {
  3684. f = value[i];
  3685. if (f) {
  3686. this.defines.add(f.type);
  3687. f.action.setValue(this);
  3688. }
  3689. }
  3690. }
  3691. clear() {
  3692. this.defines._value = this.subID;
  3693. this.clipOff[0] = 0;
  3694. }
  3695. release() {
  3696. if ((--this.ref) < 1) {
  3697. this._inClassCache && (this._inClassCache[this._inClassCache._length++] = this);
  3698. this.clear();
  3699. this.filters = null;
  3700. this.ref = 1;
  3701. this.clipOff[0] = 0;
  3702. }
  3703. }
  3704. static create(mainType, subType) {
  3705. var types = Value2D._cache[mainType | subType];
  3706. if (types._length)
  3707. return types[--types._length];
  3708. else
  3709. return new Value2D._typeClass[mainType | subType](subType);
  3710. }
  3711. }
  3712. Value2D._cache = [];
  3713. Value2D._typeClass = [];
  3714. Value2D.TEMPMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  3715. class SubmitKey {
  3716. constructor() {
  3717. this.clear();
  3718. }
  3719. clear() {
  3720. this.submitType = -1;
  3721. this.blendShader = this.other = 0;
  3722. }
  3723. copyFrom(src) {
  3724. this.other = src.other;
  3725. this.blendShader = src.blendShader;
  3726. this.submitType = src.submitType;
  3727. }
  3728. copyFrom2(src, submitType, other) {
  3729. this.other = other;
  3730. this.submitType = submitType;
  3731. }
  3732. equal3_2(next, submitType, other) {
  3733. return this.submitType === submitType && this.other === other && this.blendShader === next.blendShader;
  3734. }
  3735. equal4_2(next, submitType, other) {
  3736. return this.submitType === submitType && this.other === other && this.blendShader === next.blendShader;
  3737. }
  3738. equal_3(next) {
  3739. return this.submitType === next.submitType && this.blendShader === next.blendShader;
  3740. }
  3741. equal(next) {
  3742. return this.other === next.other && this.submitType === next.submitType && this.blendShader === next.blendShader;
  3743. }
  3744. }
  3745. class SubmitCMD {
  3746. constructor() {
  3747. this._ref = 1;
  3748. this._key = new SubmitKey();
  3749. }
  3750. renderSubmit() {
  3751. this.fun.apply(this._this, this.args);
  3752. return 1;
  3753. }
  3754. getRenderType() {
  3755. return 0;
  3756. }
  3757. releaseRender() {
  3758. if ((--this._ref) < 1) {
  3759. var pool = SubmitCMD.POOL;
  3760. pool[pool._length++] = this;
  3761. }
  3762. }
  3763. static create(args, fun, thisobj) {
  3764. var o = SubmitCMD.POOL._length ? SubmitCMD.POOL[--SubmitCMD.POOL._length] : new SubmitCMD();
  3765. o.fun = fun;
  3766. o.args = args;
  3767. o._this = thisobj;
  3768. o._ref = 1;
  3769. o._key.clear();
  3770. return o;
  3771. }
  3772. }
  3773. SubmitCMD.POOL = [];
  3774. {
  3775. SubmitCMD.POOL._length = 0;
  3776. }
  3777. class Filter {
  3778. constructor() {
  3779. }
  3780. get type() { return -1; }
  3781. }
  3782. Filter.BLUR = 0x10;
  3783. Filter.COLOR = 0x20;
  3784. Filter.GLOW = 0x08;
  3785. Filter._filter = function (sprite, context, x, y) {
  3786. var webglctx = context;
  3787. var next = this._next;
  3788. if (next) {
  3789. var filters = sprite.filters, len = filters.length;
  3790. if (len == 1 && (filters[0].type == Filter.COLOR)) {
  3791. context.save();
  3792. context.setColorFilter(filters[0]);
  3793. next._fun.call(next, sprite, context, x, y);
  3794. context.restore();
  3795. return;
  3796. }
  3797. var svCP = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  3798. var b;
  3799. var p = Point.TEMP;
  3800. var tMatrix = webglctx._curMat;
  3801. var mat = Matrix.create();
  3802. tMatrix.copyTo(mat);
  3803. var tPadding = 0;
  3804. var tHalfPadding = 0;
  3805. var tIsHaveGlowFilter = false;
  3806. var source = null;
  3807. var out = sprite._cacheStyle.filterCache || null;
  3808. if (!out || sprite.getRepaint() != 0) {
  3809. tIsHaveGlowFilter = sprite._isHaveGlowFilter();
  3810. if (tIsHaveGlowFilter) {
  3811. tPadding = 50;
  3812. tHalfPadding = 25;
  3813. }
  3814. b = new Rectangle();
  3815. b.copyFrom(sprite.getSelfBounds());
  3816. b.x += sprite.x;
  3817. b.y += sprite.y;
  3818. b.x -= sprite.pivotX + 4;
  3819. b.y -= sprite.pivotY + 4;
  3820. var tSX = b.x;
  3821. var tSY = b.y;
  3822. b.width += (tPadding + 8);
  3823. b.height += (tPadding + 8);
  3824. p.x = b.x * mat.a + b.y * mat.c;
  3825. p.y = b.y * mat.d + b.x * mat.b;
  3826. b.x = p.x;
  3827. b.y = p.y;
  3828. p.x = b.width * mat.a + b.height * mat.c;
  3829. p.y = b.height * mat.d + b.width * mat.b;
  3830. b.width = p.x;
  3831. b.height = p.y;
  3832. if (b.width <= 0 || b.height <= 0) {
  3833. return;
  3834. }
  3835. out && WebGLRTMgr.releaseRT(out);
  3836. source = WebGLRTMgr.getRT(b.width, b.height);
  3837. var outRT = out = WebGLRTMgr.getRT(b.width, b.height);
  3838. sprite._getCacheStyle().filterCache = out;
  3839. webglctx.pushRT();
  3840. webglctx.useRT(source);
  3841. var tX = sprite.x - tSX + tHalfPadding;
  3842. var tY = sprite.y - tSY + tHalfPadding;
  3843. next._fun.call(next, sprite, context, tX, tY);
  3844. webglctx.useRT(outRT);
  3845. for (var i = 0; i < len; i++) {
  3846. if (i != 0) {
  3847. webglctx.useRT(source);
  3848. webglctx.drawTarget(outRT, 0, 0, b.width, b.height, Matrix.TEMP.identity(), svCP, null, BlendMode.TOINT.overlay);
  3849. webglctx.useRT(outRT);
  3850. }
  3851. var fil = filters[i];
  3852. switch (fil.type) {
  3853. case Filter.BLUR:
  3854. fil._glRender && fil._glRender.render(source, context, b.width, b.height, fil);
  3855. break;
  3856. case Filter.GLOW:
  3857. fil._glRender && fil._glRender.render(source, context, b.width, b.height, fil);
  3858. break;
  3859. case Filter.COLOR:
  3860. webglctx.setColorFilter(fil);
  3861. webglctx.drawTarget(source, 0, 0, b.width, b.height, Matrix.EMPTY.identity(), Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  3862. webglctx.setColorFilter(null);
  3863. break;
  3864. }
  3865. }
  3866. webglctx.popRT();
  3867. }
  3868. else {
  3869. tIsHaveGlowFilter = sprite._cacheStyle.hasGlowFilter || false;
  3870. if (tIsHaveGlowFilter) {
  3871. tPadding = 50;
  3872. tHalfPadding = 25;
  3873. }
  3874. b = sprite.getBounds();
  3875. if (b.width <= 0 || b.height <= 0) {
  3876. return;
  3877. }
  3878. b.width += tPadding;
  3879. b.height += tPadding;
  3880. p.x = b.x * mat.a + b.y * mat.c;
  3881. p.y = b.y * mat.d + b.x * mat.b;
  3882. b.x = p.x;
  3883. b.y = p.y;
  3884. p.x = b.width * mat.a + b.height * mat.c;
  3885. p.y = b.height * mat.d + b.width * mat.b;
  3886. b.width = p.x;
  3887. b.height = p.y;
  3888. }
  3889. x = x - tHalfPadding - sprite.x;
  3890. y = y - tHalfPadding - sprite.y;
  3891. p.setTo(x, y);
  3892. mat.transformPoint(p);
  3893. x = p.x + b.x;
  3894. y = p.y + b.y;
  3895. webglctx._drawRenderTexture(out, x, y, b.width, b.height, Matrix.TEMP.identity(), 1.0, RenderTexture2D.defuv);
  3896. if (source) {
  3897. var submit = SubmitCMD.create([source], function (s) {
  3898. s.destroy();
  3899. }, this);
  3900. source = null;
  3901. context.addRenderObject(submit);
  3902. }
  3903. mat.destroy();
  3904. }
  3905. };
  3906. class Utils {
  3907. static toRadian(angle) {
  3908. return angle * Utils._pi2;
  3909. }
  3910. static toAngle(radian) {
  3911. return radian * Utils._pi;
  3912. }
  3913. static toHexColor(color) {
  3914. if (color < 0 || isNaN(color))
  3915. return null;
  3916. var str = color.toString(16);
  3917. while (str.length < 6)
  3918. str = "0" + str;
  3919. return "#" + str;
  3920. }
  3921. static getGID() {
  3922. return Utils._gid++;
  3923. }
  3924. static concatArray(source, array) {
  3925. if (!array)
  3926. return source;
  3927. if (!source)
  3928. return array;
  3929. var i, len = array.length;
  3930. for (i = 0; i < len; i++) {
  3931. source.push(array[i]);
  3932. }
  3933. return source;
  3934. }
  3935. static clearArray(array) {
  3936. if (!array)
  3937. return array;
  3938. array.length = 0;
  3939. return array;
  3940. }
  3941. static copyArray(source, array) {
  3942. source || (source = []);
  3943. if (!array)
  3944. return source;
  3945. source.length = array.length;
  3946. var i, len = array.length;
  3947. for (i = 0; i < len; i++) {
  3948. source[i] = array[i];
  3949. }
  3950. return source;
  3951. }
  3952. static getGlobalRecByPoints(sprite, x0, y0, x1, y1) {
  3953. var newLTPoint;
  3954. newLTPoint = Point.create().setTo(x0, y0);
  3955. newLTPoint = sprite.localToGlobal(newLTPoint);
  3956. var newRBPoint;
  3957. newRBPoint = Point.create().setTo(x1, y1);
  3958. newRBPoint = sprite.localToGlobal(newRBPoint);
  3959. var rst = Rectangle._getWrapRec([newLTPoint.x, newLTPoint.y, newRBPoint.x, newRBPoint.y]);
  3960. newLTPoint.recover();
  3961. newRBPoint.recover();
  3962. return rst;
  3963. }
  3964. static getGlobalPosAndScale(sprite) {
  3965. return Utils.getGlobalRecByPoints(sprite, 0, 0, 1, 1);
  3966. }
  3967. static bind(fun, scope) {
  3968. var rst = fun;
  3969. rst = fun.bind(scope);
  3970. return rst;
  3971. }
  3972. static updateOrder(array) {
  3973. if (!array || array.length < 2)
  3974. return false;
  3975. var i = 1, j, len = array.length, key, c;
  3976. while (i < len) {
  3977. j = i;
  3978. c = array[j];
  3979. key = array[j]._zOrder;
  3980. while (--j > -1) {
  3981. if (array[j]._zOrder > key)
  3982. array[j + 1] = array[j];
  3983. else
  3984. break;
  3985. }
  3986. array[j + 1] = c;
  3987. i++;
  3988. }
  3989. return true;
  3990. }
  3991. static transPointList(points, x, y) {
  3992. var i, len = points.length;
  3993. for (i = 0; i < len; i += 2) {
  3994. points[i] += x;
  3995. points[i + 1] += y;
  3996. }
  3997. }
  3998. static parseInt(str, radix = 0) {
  3999. var result = parseInt(str, radix);
  4000. if (isNaN(result))
  4001. return 0;
  4002. return result;
  4003. }
  4004. static getFileExtension(path) {
  4005. Utils._extReg.lastIndex = path.lastIndexOf(".");
  4006. var result = Utils._extReg.exec(path);
  4007. if (result && result.length > 1) {
  4008. return result[1].toLowerCase();
  4009. }
  4010. return null;
  4011. }
  4012. static getFilecompatibleExtension(path) {
  4013. var result = path.split(".");
  4014. var resultlen = result.length;
  4015. if (result.length > 2)
  4016. return result[resultlen - 2] + "." + result[resultlen - 1];
  4017. else
  4018. return null;
  4019. }
  4020. static getTransformRelativeToWindow(coordinateSpace, x, y) {
  4021. var stage = Utils.gStage;
  4022. var globalTransform = Utils.getGlobalPosAndScale(coordinateSpace);
  4023. var canvasMatrix = stage._canvasTransform.clone();
  4024. var canvasLeft = canvasMatrix.tx;
  4025. var canvasTop = canvasMatrix.ty;
  4026. canvasMatrix.rotate(-Math.PI / 180 * stage.canvasDegree);
  4027. canvasMatrix.scale(stage.clientScaleX, stage.clientScaleY);
  4028. var perpendicular = (stage.canvasDegree % 180 != 0);
  4029. var tx, ty;
  4030. if (perpendicular) {
  4031. tx = y + globalTransform.y;
  4032. ty = x + globalTransform.x;
  4033. tx *= canvasMatrix.d;
  4034. ty *= canvasMatrix.a;
  4035. if (stage.canvasDegree == 90) {
  4036. tx = canvasLeft - tx;
  4037. ty += canvasTop;
  4038. }
  4039. else {
  4040. tx += canvasLeft;
  4041. ty = canvasTop - ty;
  4042. }
  4043. }
  4044. else {
  4045. tx = x + globalTransform.x;
  4046. ty = y + globalTransform.y;
  4047. tx *= canvasMatrix.a;
  4048. ty *= canvasMatrix.d;
  4049. tx += canvasLeft;
  4050. ty += canvasTop;
  4051. }
  4052. ty += stage['_safariOffsetY'];
  4053. var domScaleX, domScaleY;
  4054. if (perpendicular) {
  4055. domScaleX = canvasMatrix.d * globalTransform.height;
  4056. domScaleY = canvasMatrix.a * globalTransform.width;
  4057. }
  4058. else {
  4059. domScaleX = canvasMatrix.a * globalTransform.width;
  4060. domScaleY = canvasMatrix.d * globalTransform.height;
  4061. }
  4062. return { x: tx, y: ty, scaleX: domScaleX, scaleY: domScaleY };
  4063. }
  4064. static fitDOMElementInArea(dom, coordinateSpace, x, y, width, height) {
  4065. if (!dom._fitLayaAirInitialized) {
  4066. dom._fitLayaAirInitialized = true;
  4067. dom.style.transformOrigin = dom.style.webKittransformOrigin = "left top";
  4068. dom.style.position = "absolute";
  4069. }
  4070. var transform = Utils.getTransformRelativeToWindow(coordinateSpace, x, y);
  4071. dom.style.transform = dom.style.webkitTransform = "scale(" + transform.scaleX + "," + transform.scaleY + ") rotate(" + (Utils.gStage.canvasDegree) + "deg)";
  4072. dom.style.width = width + 'px';
  4073. dom.style.height = height + 'px';
  4074. dom.style.left = transform.x + 'px';
  4075. dom.style.top = transform.y + 'px';
  4076. }
  4077. static isOkTextureList(textureList) {
  4078. if (!textureList)
  4079. return false;
  4080. var i, len = textureList.length;
  4081. var tTexture;
  4082. for (i = 0; i < len; i++) {
  4083. tTexture = textureList[i];
  4084. if (!tTexture || !tTexture._getSource())
  4085. return false;
  4086. }
  4087. return true;
  4088. }
  4089. static isOKCmdList(cmds) {
  4090. if (!cmds)
  4091. return false;
  4092. var i, len = cmds.length;
  4093. var cmd;
  4094. for (i = 0; i < len; i++) {
  4095. cmd = cmds[i];
  4096. }
  4097. return true;
  4098. }
  4099. static getQueryString(name) {
  4100. if (ILaya.Browser.onMiniGame)
  4101. return null;
  4102. if (!window.location || !window.location.search)
  4103. return null;
  4104. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  4105. var r = window.location.search.substr(1).match(reg);
  4106. if (r != null)
  4107. return unescape(r[2]);
  4108. return null;
  4109. }
  4110. }
  4111. Utils.gStage = null;
  4112. Utils._gid = 1;
  4113. Utils._pi = 180 / Math.PI;
  4114. Utils._pi2 = Math.PI / 180;
  4115. Utils._extReg = /\.(\w+)\??/g;
  4116. Utils.parseXMLFromString = function (value) {
  4117. var rst;
  4118. value = value.replace(/>\s+</g, '><');
  4119. rst = (new DOMParser()).parseFromString(value, 'text/xml');
  4120. if (rst.firstChild.textContent.indexOf("This page contains the following errors") > -1) {
  4121. throw new Error(rst.firstChild.firstChild.textContent);
  4122. }
  4123. return rst;
  4124. };
  4125. class ColorUtils {
  4126. constructor(value) {
  4127. this.arrColor = [];
  4128. if (value == null || value == 'none') {
  4129. this.strColor = "#00000000";
  4130. this.numColor = 0;
  4131. this.arrColor = [0, 0, 0, 0];
  4132. return;
  4133. }
  4134. var i, len;
  4135. var color;
  4136. if (typeof (value) == 'string') {
  4137. if (value.indexOf("rgba(") >= 0 || value.indexOf("rgb(") >= 0) {
  4138. var tStr = value;
  4139. var beginI, endI;
  4140. beginI = tStr.indexOf("(");
  4141. endI = tStr.indexOf(")");
  4142. tStr = tStr.substring(beginI + 1, endI);
  4143. this.arrColor = tStr.split(",");
  4144. len = this.arrColor.length;
  4145. for (i = 0; i < len; i++) {
  4146. this.arrColor[i] = parseFloat(this.arrColor[i]);
  4147. if (i < 3) {
  4148. this.arrColor[i] = Math.round(this.arrColor[i]);
  4149. }
  4150. }
  4151. if (this.arrColor.length == 4) {
  4152. color = ((this.arrColor[0] * 256 + this.arrColor[1]) * 256 + this.arrColor[2]) * 256 + Math.round(this.arrColor[3] * 255);
  4153. }
  4154. else {
  4155. color = ((this.arrColor[0] * 256 + this.arrColor[1]) * 256 + this.arrColor[2]);
  4156. }
  4157. this.strColor = value;
  4158. }
  4159. else {
  4160. this.strColor = value;
  4161. value.charAt(0) === '#' && (value = value.substr(1));
  4162. len = value.length;
  4163. if (len === 3 || len === 4) {
  4164. var temp = "";
  4165. for (i = 0; i < len; i++) {
  4166. temp += (value[i] + value[i]);
  4167. }
  4168. value = temp;
  4169. }
  4170. color = parseInt(value, 16);
  4171. }
  4172. }
  4173. else {
  4174. color = value;
  4175. this.strColor = Utils.toHexColor(color);
  4176. }
  4177. if (this.strColor.indexOf("rgba") >= 0 || this.strColor.length === 9) {
  4178. this.arrColor = [((0xFF000000 & color) >>> 24) / 255, ((0xFF0000 & color) >> 16) / 255, ((0xFF00 & color) >> 8) / 255, (0xFF & color) / 255];
  4179. this.numColor = (0xff000000 & color) >>> 24 | (color & 0xff0000) >> 8 | (color & 0x00ff00) << 8 | ((color & 0xff) << 24);
  4180. }
  4181. else {
  4182. this.arrColor = [((0xFF0000 & color) >> 16) / 255, ((0xFF00 & color) >> 8) / 255, (0xFF & color) / 255, 1];
  4183. this.numColor = 0xff000000 | (color & 0xff0000) >> 16 | (color & 0x00ff00) | (color & 0xff) << 16;
  4184. }
  4185. this.arrColor.__id = ++ColorUtils._COLODID;
  4186. }
  4187. static _initDefault() {
  4188. ColorUtils._DEFAULT = {};
  4189. for (var i in ColorUtils._COLOR_MAP)
  4190. ColorUtils._SAVE[i] = ColorUtils._DEFAULT[i] = new ColorUtils(ColorUtils._COLOR_MAP[i]);
  4191. return ColorUtils._DEFAULT;
  4192. }
  4193. static _initSaveMap() {
  4194. ColorUtils._SAVE_SIZE = 0;
  4195. ColorUtils._SAVE = {};
  4196. for (var i in ColorUtils._DEFAULT)
  4197. ColorUtils._SAVE[i] = ColorUtils._DEFAULT[i];
  4198. }
  4199. static create(value) {
  4200. var key = value + "";
  4201. var color = ColorUtils._SAVE[key];
  4202. if (color != null)
  4203. return color;
  4204. if (ColorUtils._SAVE_SIZE < 1000)
  4205. ColorUtils._initSaveMap();
  4206. return ColorUtils._SAVE[key] = new ColorUtils(value);
  4207. }
  4208. }
  4209. ColorUtils._SAVE = {};
  4210. ColorUtils._SAVE_SIZE = 0;
  4211. ColorUtils._COLOR_MAP = { "purple": "#800080", "orange": "#ffa500", "white": '#FFFFFF', "red": '#FF0000', "green": '#00FF00', "blue": '#0000FF', "black": '#000000', "yellow": '#FFFF00', 'gray': '#808080' };
  4212. ColorUtils._DEFAULT = ColorUtils._initDefault();
  4213. ColorUtils._COLODID = 1;
  4214. class ColorFilter extends Filter {
  4215. constructor(mat = null) {
  4216. super();
  4217. if (!mat)
  4218. mat = this._copyMatrix(ColorFilter.IDENTITY_MATRIX);
  4219. this._mat = new Float32Array(16);
  4220. this._alpha = new Float32Array(4);
  4221. this.setByMatrix(mat);
  4222. }
  4223. gray() {
  4224. return this.setByMatrix(ColorFilter.GRAY_MATRIX);
  4225. }
  4226. color(red = 0, green = 0, blue = 0, alpha = 1) {
  4227. return this.setByMatrix([1, 0, 0, 0, red, 0, 1, 0, 0, green, 0, 0, 1, 0, blue, 0, 0, 0, 1, alpha]);
  4228. }
  4229. setColor(color) {
  4230. var arr = ColorUtils.create(color).arrColor;
  4231. var mt = [0, 0, 0, 0, 256 * arr[0], 0, 0, 0, 0, 256 * arr[1], 0, 0, 0, 0, 256 * arr[2], 0, 0, 0, 1, 0];
  4232. return this.setByMatrix(mt);
  4233. }
  4234. setByMatrix(matrix) {
  4235. if (this._matrix != matrix)
  4236. this._copyMatrix(matrix);
  4237. var j = 0;
  4238. var z = 0;
  4239. for (var i = 0; i < 20; i++) {
  4240. if (i % 5 != 4) {
  4241. this._mat[j++] = matrix[i];
  4242. }
  4243. else {
  4244. this._alpha[z++] = matrix[i];
  4245. }
  4246. }
  4247. return this;
  4248. }
  4249. get type() {
  4250. return Filter.COLOR;
  4251. }
  4252. adjustColor(brightness, contrast, saturation, hue) {
  4253. this.adjustHue(hue);
  4254. this.adjustContrast(contrast);
  4255. this.adjustBrightness(brightness);
  4256. this.adjustSaturation(saturation);
  4257. return this;
  4258. }
  4259. adjustBrightness(brightness) {
  4260. brightness = this._clampValue(brightness, 100);
  4261. if (brightness == 0 || isNaN(brightness))
  4262. return this;
  4263. return this._multiplyMatrix([1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
  4264. }
  4265. adjustContrast(contrast) {
  4266. contrast = this._clampValue(contrast, 100);
  4267. if (contrast == 0 || isNaN(contrast))
  4268. return this;
  4269. var x;
  4270. if (contrast < 0) {
  4271. x = 127 + contrast / 100 * 127;
  4272. }
  4273. else {
  4274. x = contrast % 1;
  4275. if (x == 0) {
  4276. x = ColorFilter.DELTA_INDEX[contrast];
  4277. }
  4278. else {
  4279. x = ColorFilter.DELTA_INDEX[(contrast << 0)] * (1 - x) + ColorFilter.DELTA_INDEX[(contrast << 0) + 1] * x;
  4280. }
  4281. x = x * 127 + 127;
  4282. }
  4283. var x1 = x / 127;
  4284. var x2 = (127 - x) * 0.5;
  4285. return this._multiplyMatrix([x1, 0, 0, 0, x2, 0, x1, 0, 0, x2, 0, 0, x1, 0, x2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
  4286. }
  4287. adjustSaturation(saturation) {
  4288. saturation = this._clampValue(saturation, 100);
  4289. if (saturation == 0 || isNaN(saturation))
  4290. return this;
  4291. var x = 1 + ((saturation > 0) ? 3 * saturation / 100 : saturation / 100);
  4292. var dx = 1 - x;
  4293. var r = 0.3086 * dx;
  4294. var g = 0.6094 * dx;
  4295. var b = 0.0820 * dx;
  4296. return this._multiplyMatrix([r + x, g, b, 0, 0, r, g + x, b, 0, 0, r, g, b + x, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
  4297. }
  4298. adjustHue(hue) {
  4299. hue = this._clampValue(hue, 180) / 180 * Math.PI;
  4300. if (hue == 0 || isNaN(hue))
  4301. return this;
  4302. var cos = Math.cos(hue);
  4303. var sin = Math.sin(hue);
  4304. var r = 0.213;
  4305. var g = 0.715;
  4306. var b = 0.072;
  4307. return this._multiplyMatrix([r + cos * (1 - r) + sin * (-r), g + cos * (-g) + sin * (-g), b + cos * (-b) + sin * (1 - b), 0, 0, r + cos * (-r) + sin * (0.143), g + cos * (1 - g) + sin * (0.140), b + cos * (-b) + sin * (-0.283), 0, 0, r + cos * (-r) + sin * (-(1 - r)), g + cos * (-g) + sin * (g), b + cos * (1 - b) + sin * (b), 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]);
  4308. }
  4309. reset() {
  4310. return this.setByMatrix(this._copyMatrix(ColorFilter.IDENTITY_MATRIX));
  4311. }
  4312. _multiplyMatrix(matrix) {
  4313. var col = [];
  4314. this._matrix = this._fixMatrix(this._matrix);
  4315. for (var i = 0; i < 5; i++) {
  4316. for (var j = 0; j < 5; j++) {
  4317. col[j] = this._matrix[j + i * 5];
  4318. }
  4319. for (j = 0; j < 5; j++) {
  4320. var val = 0;
  4321. for (var k = 0; k < 5; k++) {
  4322. val += matrix[j + k * 5] * col[k];
  4323. }
  4324. this._matrix[j + i * 5] = val;
  4325. }
  4326. }
  4327. return this.setByMatrix(this._matrix);
  4328. }
  4329. _clampValue(val, limit) {
  4330. return Math.min(limit, Math.max(-limit, val));
  4331. }
  4332. _fixMatrix(matrix = null) {
  4333. if (matrix == null)
  4334. return ColorFilter.IDENTITY_MATRIX;
  4335. if (matrix.length < ColorFilter.LENGTH)
  4336. matrix = matrix.slice(0, matrix.length).concat(ColorFilter.IDENTITY_MATRIX.slice(matrix.length, ColorFilter.LENGTH));
  4337. else if (matrix.length > ColorFilter.LENGTH)
  4338. matrix = matrix.slice(0, ColorFilter.LENGTH);
  4339. return matrix;
  4340. }
  4341. _copyMatrix(matrix) {
  4342. var len = ColorFilter.LENGTH;
  4343. if (!this._matrix)
  4344. this._matrix = [];
  4345. for (var i = 0; i < len; i++) {
  4346. this._matrix[i] = matrix[i];
  4347. }
  4348. return this._matrix;
  4349. }
  4350. }
  4351. ColorFilter.DELTA_INDEX = [0, 0.01, 0.02, 0.04, 0.05, 0.06, 0.07, 0.08, 0.1, 0.11, 0.12, 0.14, 0.15, 0.16, 0.17, 0.18, 0.20, 0.21, 0.22, 0.24, 0.25, 0.27, 0.28, 0.30, 0.32, 0.34, 0.36, 0.38, 0.40, 0.42, 0.44, 0.46, 0.48, 0.5, 0.53, 0.56, 0.59, 0.62, 0.65, 0.68, 0.71, 0.74, 0.77, 0.80, 0.83, 0.86, 0.89, 0.92, 0.95, 0.98, 1.0, 1.06, 1.12, 1.18, 1.24, 1.30, 1.36, 1.42, 1.48, 1.54, 1.60, 1.66, 1.72, 1.78, 1.84, 1.90, 1.96, 2.0, 2.12, 2.25, 2.37, 2.50, 2.62, 2.75, 2.87, 3.0, 3.2, 3.4, 3.6, 3.8, 4.0, 4.3, 4.7, 4.9, 5.0, 5.5, 6.0, 6.5, 6.8, 7.0, 7.3, 7.5, 7.8, 8.0, 8.4, 8.7, 9.0, 9.4, 9.6, 9.8, 10.0];
  4352. ColorFilter.GRAY_MATRIX = [0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0.3086, 0.6094, 0.082, 0, 0, 0, 0, 0, 1, 0];
  4353. ColorFilter.IDENTITY_MATRIX = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1];
  4354. ColorFilter.LENGTH = 25;
  4355. class DrawTextureCmd {
  4356. constructor() {
  4357. this.colorFlt = null;
  4358. this.uv = null;
  4359. }
  4360. static create(texture, x, y, width, height, matrix, alpha, color, blendMode, uv) {
  4361. var cmd = Pool.getItemByClass("DrawTextureCmd", DrawTextureCmd);
  4362. cmd.texture = texture;
  4363. texture._addReference();
  4364. cmd.x = x;
  4365. cmd.y = y;
  4366. cmd.width = width;
  4367. cmd.height = height;
  4368. cmd.matrix = matrix;
  4369. cmd.alpha = alpha;
  4370. cmd.color = color;
  4371. cmd.blendMode = blendMode;
  4372. cmd.uv = uv == undefined ? null : uv;
  4373. if (color) {
  4374. cmd.colorFlt = new ColorFilter();
  4375. cmd.colorFlt.setColor(color);
  4376. }
  4377. return cmd;
  4378. }
  4379. recover() {
  4380. this.texture && this.texture._removeReference();
  4381. this.texture = null;
  4382. this.matrix = null;
  4383. Pool.recover("DrawTextureCmd", this);
  4384. }
  4385. run(context, gx, gy) {
  4386. this.texture && context.drawTextureWithTransform(this.texture, this.x, this.y, this.width, this.height, this.matrix, gx, gy, this.alpha, this.blendMode, this.colorFlt, this.uv);
  4387. }
  4388. get cmdID() {
  4389. return DrawTextureCmd.ID;
  4390. }
  4391. }
  4392. DrawTextureCmd.ID = "DrawTexture";
  4393. class FillTextureCmd {
  4394. static create(texture, x, y, width, height, type, offset, other) {
  4395. var cmd = Pool.getItemByClass("FillTextureCmd", FillTextureCmd);
  4396. cmd.texture = texture;
  4397. cmd.x = x;
  4398. cmd.y = y;
  4399. cmd.width = width;
  4400. cmd.height = height;
  4401. cmd.type = type;
  4402. cmd.offset = offset;
  4403. cmd.other = other;
  4404. return cmd;
  4405. }
  4406. recover() {
  4407. this.texture = null;
  4408. this.offset = null;
  4409. this.other = null;
  4410. Pool.recover("FillTextureCmd", this);
  4411. }
  4412. run(context, gx, gy) {
  4413. context.fillTexture(this.texture, this.x + gx, this.y + gy, this.width, this.height, this.type, this.offset, this.other);
  4414. }
  4415. get cmdID() {
  4416. return FillTextureCmd.ID;
  4417. }
  4418. }
  4419. FillTextureCmd.ID = "FillTexture";
  4420. class RestoreCmd {
  4421. static create() {
  4422. var cmd = Pool.getItemByClass("RestoreCmd", RestoreCmd);
  4423. return cmd;
  4424. }
  4425. recover() {
  4426. Pool.recover("RestoreCmd", this);
  4427. }
  4428. run(context, gx, gy) {
  4429. context.restore();
  4430. }
  4431. get cmdID() {
  4432. return RestoreCmd.ID;
  4433. }
  4434. }
  4435. RestoreCmd.ID = "Restore";
  4436. class RotateCmd {
  4437. static create(angle, pivotX, pivotY) {
  4438. var cmd = Pool.getItemByClass("RotateCmd", RotateCmd);
  4439. cmd.angle = angle;
  4440. cmd.pivotX = pivotX;
  4441. cmd.pivotY = pivotY;
  4442. return cmd;
  4443. }
  4444. recover() {
  4445. Pool.recover("RotateCmd", this);
  4446. }
  4447. run(context, gx, gy) {
  4448. context._rotate(this.angle, this.pivotX + gx, this.pivotY + gy);
  4449. }
  4450. get cmdID() {
  4451. return RotateCmd.ID;
  4452. }
  4453. }
  4454. RotateCmd.ID = "Rotate";
  4455. class ScaleCmd {
  4456. static create(scaleX, scaleY, pivotX, pivotY) {
  4457. var cmd = Pool.getItemByClass("ScaleCmd", ScaleCmd);
  4458. cmd.scaleX = scaleX;
  4459. cmd.scaleY = scaleY;
  4460. cmd.pivotX = pivotX;
  4461. cmd.pivotY = pivotY;
  4462. return cmd;
  4463. }
  4464. recover() {
  4465. Pool.recover("ScaleCmd", this);
  4466. }
  4467. run(context, gx, gy) {
  4468. context._scale(this.scaleX, this.scaleY, this.pivotX + gx, this.pivotY + gy);
  4469. }
  4470. get cmdID() {
  4471. return ScaleCmd.ID;
  4472. }
  4473. }
  4474. ScaleCmd.ID = "Scale";
  4475. class TransformCmd {
  4476. static create(matrix, pivotX, pivotY) {
  4477. var cmd = Pool.getItemByClass("TransformCmd", TransformCmd);
  4478. cmd.matrix = matrix;
  4479. cmd.pivotX = pivotX;
  4480. cmd.pivotY = pivotY;
  4481. return cmd;
  4482. }
  4483. recover() {
  4484. this.matrix = null;
  4485. Pool.recover("TransformCmd", this);
  4486. }
  4487. run(context, gx, gy) {
  4488. context._transform(this.matrix, this.pivotX + gx, this.pivotY + gy);
  4489. }
  4490. get cmdID() {
  4491. return TransformCmd.ID;
  4492. }
  4493. }
  4494. TransformCmd.ID = "Transform";
  4495. class TranslateCmd {
  4496. static create(tx, ty) {
  4497. var cmd = Pool.getItemByClass("TranslateCmd", TranslateCmd);
  4498. cmd.tx = tx;
  4499. cmd.ty = ty;
  4500. return cmd;
  4501. }
  4502. recover() {
  4503. Pool.recover("TranslateCmd", this);
  4504. }
  4505. run(context, gx, gy) {
  4506. context.translate(this.tx, this.ty);
  4507. }
  4508. get cmdID() {
  4509. return TranslateCmd.ID;
  4510. }
  4511. }
  4512. TranslateCmd.ID = "Translate";
  4513. class Bezier {
  4514. constructor() {
  4515. this._controlPoints = [new Point(), new Point(), new Point()];
  4516. this._calFun = this.getPoint2;
  4517. }
  4518. _switchPoint(x, y) {
  4519. var tPoint = this._controlPoints.shift();
  4520. tPoint.setTo(x, y);
  4521. this._controlPoints.push(tPoint);
  4522. }
  4523. getPoint2(t, rst) {
  4524. var p1 = this._controlPoints[0];
  4525. var p2 = this._controlPoints[1];
  4526. var p3 = this._controlPoints[2];
  4527. var lineX = Math.pow((1 - t), 2) * p1.x + 2 * t * (1 - t) * p2.x + Math.pow(t, 2) * p3.x;
  4528. var lineY = Math.pow((1 - t), 2) * p1.y + 2 * t * (1 - t) * p2.y + Math.pow(t, 2) * p3.y;
  4529. rst.push(lineX, lineY);
  4530. }
  4531. getPoint3(t, rst) {
  4532. var p1 = this._controlPoints[0];
  4533. var p2 = this._controlPoints[1];
  4534. var p3 = this._controlPoints[2];
  4535. var p4 = this._controlPoints[3];
  4536. var lineX = Math.pow((1 - t), 3) * p1.x + 3 * p2.x * t * (1 - t) * (1 - t) + 3 * p3.x * t * t * (1 - t) + p4.x * Math.pow(t, 3);
  4537. var lineY = Math.pow((1 - t), 3) * p1.y + 3 * p2.y * t * (1 - t) * (1 - t) + 3 * p3.y * t * t * (1 - t) + p4.y * Math.pow(t, 3);
  4538. rst.push(lineX, lineY);
  4539. }
  4540. insertPoints(count, rst) {
  4541. var i;
  4542. count = count > 0 ? count : 5;
  4543. var dLen;
  4544. dLen = 1 / count;
  4545. for (i = 0; i <= 1; i += dLen) {
  4546. this._calFun(i, rst);
  4547. }
  4548. }
  4549. getBezierPoints(pList, inSertCount = 5, count = 2) {
  4550. var i, len;
  4551. len = pList.length;
  4552. if (len < (count + 1) * 2)
  4553. return [];
  4554. var rst = [];
  4555. switch (count) {
  4556. case 2:
  4557. this._calFun = this.getPoint2;
  4558. break;
  4559. case 3:
  4560. this._calFun = this.getPoint3;
  4561. break;
  4562. default:
  4563. return [];
  4564. }
  4565. while (this._controlPoints.length <= count) {
  4566. this._controlPoints.push(Point.create());
  4567. }
  4568. for (i = 0; i < count * 2; i += 2) {
  4569. this._switchPoint(pList[i], pList[i + 1]);
  4570. }
  4571. for (i = count * 2; i < len; i += 2) {
  4572. this._switchPoint(pList[i], pList[i + 1]);
  4573. if ((i / 2) % count == 0)
  4574. this.insertPoints(inSertCount, rst);
  4575. }
  4576. return rst;
  4577. }
  4578. }
  4579. Bezier.I = new Bezier();
  4580. class GrahamScan {
  4581. static multiply(p1, p2, p0) {
  4582. return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
  4583. }
  4584. static dis(p1, p2) {
  4585. return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
  4586. }
  4587. static _getPoints(count, tempUse = false, rst = null) {
  4588. if (!GrahamScan._mPointList)
  4589. GrahamScan._mPointList = [];
  4590. while (GrahamScan._mPointList.length < count)
  4591. GrahamScan._mPointList.push(new Point());
  4592. if (!rst)
  4593. rst = [];
  4594. rst.length = 0;
  4595. if (tempUse) {
  4596. GrahamScan.getFrom(rst, GrahamScan._mPointList, count);
  4597. }
  4598. else {
  4599. GrahamScan.getFromR(rst, GrahamScan._mPointList, count);
  4600. }
  4601. return rst;
  4602. }
  4603. static getFrom(rst, src, count) {
  4604. var i;
  4605. for (i = 0; i < count; i++) {
  4606. rst.push(src[i]);
  4607. }
  4608. return rst;
  4609. }
  4610. static getFromR(rst, src, count) {
  4611. var i;
  4612. for (i = 0; i < count; i++) {
  4613. rst.push(src.pop());
  4614. }
  4615. return rst;
  4616. }
  4617. static pListToPointList(pList, tempUse = false) {
  4618. var i, len = pList.length / 2, rst = GrahamScan._getPoints(len, tempUse, GrahamScan._tempPointList);
  4619. for (i = 0; i < len; i++) {
  4620. rst[i].setTo(pList[i + i], pList[i + i + 1]);
  4621. }
  4622. return rst;
  4623. }
  4624. static pointListToPlist(pointList) {
  4625. var i, len = pointList.length, rst = GrahamScan._temPList, tPoint;
  4626. rst.length = 0;
  4627. for (i = 0; i < len; i++) {
  4628. tPoint = pointList[i];
  4629. rst.push(tPoint.x, tPoint.y);
  4630. }
  4631. return rst;
  4632. }
  4633. static scanPList(pList) {
  4634. return Utils.copyArray(pList, GrahamScan.pointListToPlist(GrahamScan.scan(GrahamScan.pListToPointList(pList, true))));
  4635. }
  4636. static scan(PointSet) {
  4637. var i, j, k = 0, tmp, n = PointSet.length, ch;
  4638. var _tmpDic = {};
  4639. var key;
  4640. ch = GrahamScan._temArr;
  4641. ch.length = 0;
  4642. n = PointSet.length;
  4643. for (i = n - 1; i >= 0; i--) {
  4644. tmp = PointSet[i];
  4645. key = tmp.x + "_" + tmp.y;
  4646. if (!(key in _tmpDic)) {
  4647. _tmpDic[key] = true;
  4648. ch.push(tmp);
  4649. }
  4650. }
  4651. n = ch.length;
  4652. Utils.copyArray(PointSet, ch);
  4653. for (i = 1; i < n; i++)
  4654. if ((PointSet[i].y < PointSet[k].y) || ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
  4655. k = i;
  4656. tmp = PointSet[0];
  4657. PointSet[0] = PointSet[k];
  4658. PointSet[k] = tmp;
  4659. for (i = 1; i < n - 1; i++) {
  4660. k = i;
  4661. for (j = i + 1; j < n; j++)
  4662. if ((GrahamScan.multiply(PointSet[j], PointSet[k], PointSet[0]) > 0) || ((GrahamScan.multiply(PointSet[j], PointSet[k], PointSet[0]) == 0) && (GrahamScan.dis(PointSet[0], PointSet[j]) < GrahamScan.dis(PointSet[0], PointSet[k]))))
  4663. k = j;
  4664. tmp = PointSet[i];
  4665. PointSet[i] = PointSet[k];
  4666. PointSet[k] = tmp;
  4667. }
  4668. ch = GrahamScan._temArr;
  4669. ch.length = 0;
  4670. if (PointSet.length < 3) {
  4671. return Utils.copyArray(ch, PointSet);
  4672. }
  4673. ch.push(PointSet[0], PointSet[1], PointSet[2]);
  4674. for (i = 3; i < n; i++) {
  4675. while (ch.length >= 2 && GrahamScan.multiply(PointSet[i], ch[ch.length - 1], ch[ch.length - 2]) >= 0)
  4676. ch.pop();
  4677. PointSet[i] && ch.push(PointSet[i]);
  4678. }
  4679. return ch;
  4680. }
  4681. }
  4682. GrahamScan._tempPointList = [];
  4683. GrahamScan._temPList = [];
  4684. GrahamScan._temArr = [];
  4685. class DrawStyle {
  4686. constructor(value) {
  4687. this.setValue(value);
  4688. }
  4689. static create(value) {
  4690. if (value) {
  4691. var color = (value instanceof ColorUtils) ? value : ColorUtils.create(value);
  4692. return color._drawStyle || (color._drawStyle = new DrawStyle(value));
  4693. }
  4694. return DrawStyle.DEFAULT;
  4695. }
  4696. setValue(value) {
  4697. if (value) {
  4698. this._color = (value instanceof ColorUtils) ? value : ColorUtils.create(value);
  4699. }
  4700. else
  4701. this._color = ColorUtils.create("#000000");
  4702. }
  4703. reset() {
  4704. this._color = ColorUtils.create("#000000");
  4705. }
  4706. toInt() {
  4707. return this._color.numColor;
  4708. }
  4709. equal(value) {
  4710. if (typeof (value) == 'string')
  4711. return this._color.strColor === value;
  4712. if (value instanceof ColorUtils)
  4713. return this._color.numColor === value.numColor;
  4714. return false;
  4715. }
  4716. toColorStr() {
  4717. return this._color.strColor;
  4718. }
  4719. }
  4720. DrawStyle.DEFAULT = new DrawStyle("#000000");
  4721. class Path {
  4722. constructor() {
  4723. this._lastOriX = 0;
  4724. this._lastOriY = 0;
  4725. this.paths = [];
  4726. this._curPath = null;
  4727. }
  4728. beginPath(convex) {
  4729. this.paths.length = 1;
  4730. this._curPath = this.paths[0] = new renderPath();
  4731. this._curPath.convex = convex;
  4732. }
  4733. closePath() {
  4734. this._curPath.loop = true;
  4735. }
  4736. newPath() {
  4737. this._curPath = new renderPath();
  4738. this.paths.push(this._curPath);
  4739. }
  4740. addPoint(pointX, pointY) {
  4741. this._curPath.path.push(pointX, pointY);
  4742. }
  4743. push(points, convex) {
  4744. if (!this._curPath) {
  4745. this._curPath = new renderPath();
  4746. this.paths.push(this._curPath);
  4747. }
  4748. else if (this._curPath.path.length > 0) {
  4749. this._curPath = new renderPath();
  4750. this.paths.push(this._curPath);
  4751. }
  4752. var rp = this._curPath;
  4753. rp.path = points.slice();
  4754. rp.convex = convex;
  4755. }
  4756. reset() {
  4757. this.paths.length = 0;
  4758. }
  4759. }
  4760. class renderPath {
  4761. constructor() {
  4762. this.path = [];
  4763. this.loop = false;
  4764. this.convex = false;
  4765. }
  4766. }
  4767. class SubmitBase {
  4768. constructor(renderType = SubmitBase.TYPE_2D) {
  4769. this.clipInfoID = -1;
  4770. this._mesh = null;
  4771. this._blendFn = null;
  4772. this._id = 0;
  4773. this._renderType = 0;
  4774. this._parent = null;
  4775. this._key = new SubmitKey();
  4776. this._startIdx = 0;
  4777. this._numEle = 0;
  4778. this._ref = 1;
  4779. this.shaderValue = null;
  4780. this._renderType = renderType;
  4781. this._id = ++SubmitBase.ID;
  4782. }
  4783. static __init__() {
  4784. var s = SubmitBase.RENDERBASE = new SubmitBase(-1);
  4785. s.shaderValue = new Value2D(0, 0);
  4786. s.shaderValue.ALPHA = 1;
  4787. s._ref = 0xFFFFFFFF;
  4788. }
  4789. getID() {
  4790. return this._id;
  4791. }
  4792. getRenderType() {
  4793. return this._renderType;
  4794. }
  4795. toString() {
  4796. return "ibindex:" + this._startIdx + " num:" + this._numEle + " key=" + this._key;
  4797. }
  4798. renderSubmit() { return 1; }
  4799. releaseRender() { }
  4800. }
  4801. SubmitBase.TYPE_2D = 10000;
  4802. SubmitBase.TYPE_CANVAS = 10003;
  4803. SubmitBase.TYPE_CMDSETRT = 10004;
  4804. SubmitBase.TYPE_CUSTOM = 10005;
  4805. SubmitBase.TYPE_BLURRT = 10006;
  4806. SubmitBase.TYPE_CMDDESTORYPRERT = 10007;
  4807. SubmitBase.TYPE_DISABLESTENCIL = 10008;
  4808. SubmitBase.TYPE_OTHERIBVB = 10009;
  4809. SubmitBase.TYPE_PRIMITIVE = 10010;
  4810. SubmitBase.TYPE_RT = 10011;
  4811. SubmitBase.TYPE_BLUR_RT = 10012;
  4812. SubmitBase.TYPE_TARGET = 10013;
  4813. SubmitBase.TYPE_CHANGE_VALUE = 10014;
  4814. SubmitBase.TYPE_SHAPE = 10015;
  4815. SubmitBase.TYPE_TEXTURE = 10016;
  4816. SubmitBase.TYPE_FILLTEXTURE = 10017;
  4817. SubmitBase.KEY_ONCE = -1;
  4818. SubmitBase.KEY_FILLRECT = 1;
  4819. SubmitBase.KEY_DRAWTEXTURE = 2;
  4820. SubmitBase.KEY_VG = 3;
  4821. SubmitBase.KEY_TRIANGLES = 4;
  4822. SubmitBase.ID = 1;
  4823. SubmitBase.preRender = null;
  4824. class SaveBase {
  4825. constructor() {
  4826. }
  4827. static _createArray() {
  4828. var value = [];
  4829. value._length = 0;
  4830. return value;
  4831. }
  4832. static _init() {
  4833. var namemap = SaveBase._namemap = {};
  4834. namemap[SaveBase.TYPE_ALPHA] = "ALPHA";
  4835. namemap[SaveBase.TYPE_FILESTYLE] = "fillStyle";
  4836. namemap[SaveBase.TYPE_FONT] = "font";
  4837. namemap[SaveBase.TYPE_LINEWIDTH] = "lineWidth";
  4838. namemap[SaveBase.TYPE_STROKESTYLE] = "strokeStyle";
  4839. namemap[SaveBase.TYPE_ENABLEMERGE] = "_mergeID";
  4840. namemap[SaveBase.TYPE_MARK] = namemap[SaveBase.TYPE_TRANSFORM] = namemap[SaveBase.TYPE_TRANSLATE] = [];
  4841. namemap[SaveBase.TYPE_TEXTBASELINE] = "textBaseline";
  4842. namemap[SaveBase.TYPE_TEXTALIGN] = "textAlign";
  4843. namemap[SaveBase.TYPE_GLOBALCOMPOSITEOPERATION] = "_nBlendType";
  4844. namemap[SaveBase.TYPE_SHADER] = "shader";
  4845. namemap[SaveBase.TYPE_FILTERS] = "filters";
  4846. namemap[SaveBase.TYPE_COLORFILTER] = '_colorFiler';
  4847. return namemap;
  4848. }
  4849. isSaveMark() { return false; }
  4850. restore(context) {
  4851. this._dataObj[this._valueName] = this._value;
  4852. SaveBase.POOL[SaveBase.POOL._length++] = this;
  4853. this._newSubmit && (context._curSubmit = SubmitBase.RENDERBASE);
  4854. }
  4855. static save(context, type, dataObj, newSubmit) {
  4856. if ((context._saveMark._saveuse & type) !== type) {
  4857. context._saveMark._saveuse |= type;
  4858. var cache = SaveBase.POOL;
  4859. var o = cache._length > 0 ? cache[--cache._length] : (new SaveBase());
  4860. o._value = dataObj[o._valueName = SaveBase._namemap[type]];
  4861. o._dataObj = dataObj;
  4862. o._newSubmit = newSubmit;
  4863. var _save = context._save;
  4864. _save[_save._length++] = o;
  4865. }
  4866. }
  4867. }
  4868. SaveBase.TYPE_ALPHA = 0x1;
  4869. SaveBase.TYPE_FILESTYLE = 0x2;
  4870. SaveBase.TYPE_FONT = 0x8;
  4871. SaveBase.TYPE_LINEWIDTH = 0x100;
  4872. SaveBase.TYPE_STROKESTYLE = 0x200;
  4873. SaveBase.TYPE_MARK = 0x400;
  4874. SaveBase.TYPE_TRANSFORM = 0x800;
  4875. SaveBase.TYPE_TRANSLATE = 0x1000;
  4876. SaveBase.TYPE_ENABLEMERGE = 0x2000;
  4877. SaveBase.TYPE_TEXTBASELINE = 0x4000;
  4878. SaveBase.TYPE_TEXTALIGN = 0x8000;
  4879. SaveBase.TYPE_GLOBALCOMPOSITEOPERATION = 0x10000;
  4880. SaveBase.TYPE_CLIPRECT = 0x20000;
  4881. SaveBase.TYPE_CLIPRECT_STENCIL = 0x40000;
  4882. SaveBase.TYPE_IBVB = 0x80000;
  4883. SaveBase.TYPE_SHADER = 0x100000;
  4884. SaveBase.TYPE_FILTERS = 0x200000;
  4885. SaveBase.TYPE_FILTERS_TYPE = 0x400000;
  4886. SaveBase.TYPE_COLORFILTER = 0x800000;
  4887. SaveBase.POOL = SaveBase._createArray();
  4888. SaveBase._namemap = SaveBase._init();
  4889. class SaveClipRect {
  4890. constructor() {
  4891. this._globalClipMatrix = new Matrix();
  4892. this._clipInfoID = -1;
  4893. this._clipRect = new Rectangle();
  4894. this.incache = false;
  4895. }
  4896. isSaveMark() { return false; }
  4897. restore(context) {
  4898. this._globalClipMatrix.copyTo(context._globalClipMatrix);
  4899. this._clipRect.clone(context._clipRect);
  4900. context._clipInfoID = this._clipInfoID;
  4901. SaveClipRect.POOL[SaveClipRect.POOL._length++] = this;
  4902. context._clipInCache = this.incache;
  4903. }
  4904. static save(context) {
  4905. if ((context._saveMark._saveuse & SaveBase.TYPE_CLIPRECT) == SaveBase.TYPE_CLIPRECT)
  4906. return;
  4907. context._saveMark._saveuse |= SaveBase.TYPE_CLIPRECT;
  4908. var cache = SaveClipRect.POOL;
  4909. var o = cache._length > 0 ? cache[--cache._length] : (new SaveClipRect());
  4910. context._globalClipMatrix.copyTo(o._globalClipMatrix);
  4911. context._clipRect.clone(o._clipRect);
  4912. o._clipInfoID = context._clipInfoID;
  4913. o.incache = context._clipInCache;
  4914. var _save = context._save;
  4915. _save[_save._length++] = o;
  4916. }
  4917. }
  4918. SaveClipRect.POOL = SaveBase._createArray();
  4919. class SaveMark {
  4920. constructor() {
  4921. this._saveuse = 0;
  4922. }
  4923. isSaveMark() {
  4924. return true;
  4925. }
  4926. restore(context) {
  4927. context._saveMark = this._preSaveMark;
  4928. SaveMark.POOL[SaveMark.POOL._length++] = this;
  4929. }
  4930. static Create(context) {
  4931. var no = SaveMark.POOL;
  4932. var o = no._length > 0 ? no[--no._length] : (new SaveMark());
  4933. o._saveuse = 0;
  4934. o._preSaveMark = context._saveMark;
  4935. context._saveMark = o;
  4936. return o;
  4937. }
  4938. }
  4939. SaveMark.POOL = SaveBase._createArray();
  4940. class SaveTransform {
  4941. constructor() {
  4942. this._matrix = new Matrix();
  4943. }
  4944. isSaveMark() { return false; }
  4945. restore(context) {
  4946. context._curMat = this._savematrix;
  4947. SaveTransform.POOL[SaveTransform.POOL._length++] = this;
  4948. }
  4949. static save(context) {
  4950. var _saveMark = context._saveMark;
  4951. if ((_saveMark._saveuse & SaveBase.TYPE_TRANSFORM) === SaveBase.TYPE_TRANSFORM)
  4952. return;
  4953. _saveMark._saveuse |= SaveBase.TYPE_TRANSFORM;
  4954. var no = SaveTransform.POOL;
  4955. var o = no._length > 0 ? no[--no._length] : (new SaveTransform());
  4956. o._savematrix = context._curMat;
  4957. context._curMat = context._curMat.copyTo(o._matrix);
  4958. var _save = context._save;
  4959. _save[_save._length++] = o;
  4960. }
  4961. }
  4962. SaveTransform.POOL = SaveBase._createArray();
  4963. class SaveTranslate {
  4964. constructor() {
  4965. this._mat = new Matrix();
  4966. }
  4967. isSaveMark() { return false; }
  4968. restore(context) {
  4969. this._mat.copyTo(context._curMat);
  4970. SaveTranslate.POOL[SaveTranslate.POOL._length++] = this;
  4971. }
  4972. static save(context) {
  4973. var no = SaveTranslate.POOL;
  4974. var o = no._length > 0 ? no[--no._length] : (new SaveTranslate());
  4975. context._curMat.copyTo(o._mat);
  4976. var _save = context._save;
  4977. _save[_save._length++] = o;
  4978. }
  4979. }
  4980. SaveTranslate.POOL = SaveBase._createArray();
  4981. class BufferStateBase {
  4982. constructor() {
  4983. this._nativeVertexArrayObject = LayaGL.layaGPUInstance.createVertexArray();
  4984. }
  4985. bind() {
  4986. if (BufferStateBase._curBindedBufferState !== this) {
  4987. LayaGL.layaGPUInstance.bindVertexArray(this._nativeVertexArrayObject);
  4988. BufferStateBase._curBindedBufferState = this;
  4989. }
  4990. }
  4991. unBind() {
  4992. if (BufferStateBase._curBindedBufferState === this) {
  4993. LayaGL.layaGPUInstance.bindVertexArray(null);
  4994. BufferStateBase._curBindedBufferState = null;
  4995. }
  4996. else {
  4997. throw "BufferState: must call bind() function first.";
  4998. }
  4999. }
  5000. destroy() {
  5001. LayaGL.layaGPUInstance.deleteVertexArray(this._nativeVertexArrayObject);
  5002. }
  5003. bindForNative() {
  5004. LayaGL.instance.bindVertexArray(this._nativeVertexArrayObject);
  5005. BufferStateBase._curBindedBufferState = this;
  5006. }
  5007. unBindForNative() {
  5008. LayaGL.instance.bindVertexArray(null);
  5009. BufferStateBase._curBindedBufferState = null;
  5010. }
  5011. }
  5012. class BufferState2D extends BufferStateBase {
  5013. constructor() {
  5014. super();
  5015. }
  5016. }
  5017. class Buffer {
  5018. constructor() {
  5019. this._byteLength = 0;
  5020. this._glBuffer = LayaGL.instance.createBuffer();
  5021. }
  5022. get bufferUsage() {
  5023. return this._bufferUsage;
  5024. }
  5025. _bindForVAO() {
  5026. }
  5027. bind() {
  5028. return false;
  5029. }
  5030. destroy() {
  5031. if (this._glBuffer) {
  5032. LayaGL.instance.deleteBuffer(this._glBuffer);
  5033. this._glBuffer = null;
  5034. }
  5035. }
  5036. }
  5037. class RenderInfo {
  5038. }
  5039. RenderInfo.loopStTm = 0;
  5040. RenderInfo.loopCount = 0;
  5041. class Buffer2D extends Buffer {
  5042. constructor() {
  5043. super();
  5044. this._maxsize = 0;
  5045. this._upload = true;
  5046. this._uploadSize = 0;
  5047. this._bufferSize = 0;
  5048. this._u8Array = null;
  5049. }
  5050. static __int__(gl) {
  5051. }
  5052. get bufferLength() {
  5053. return this._buffer.byteLength;
  5054. }
  5055. set byteLength(value) {
  5056. this.setByteLength(value);
  5057. }
  5058. setByteLength(value) {
  5059. if (this._byteLength !== value) {
  5060. value <= this._bufferSize || (this._resizeBuffer(value * 2 + 256, true));
  5061. this._byteLength = value;
  5062. }
  5063. }
  5064. needSize(sz) {
  5065. var old = this._byteLength;
  5066. if (sz) {
  5067. var needsz = this._byteLength + sz;
  5068. needsz <= this._bufferSize || (this._resizeBuffer(needsz << 1, true));
  5069. this._byteLength = needsz;
  5070. }
  5071. return old;
  5072. }
  5073. _bufferData() {
  5074. this._maxsize = Math.max(this._maxsize, this._byteLength);
  5075. if (RenderInfo.loopCount % 30 == 0) {
  5076. if (this._buffer.byteLength > (this._maxsize + 64)) {
  5077. this._buffer = this._buffer.slice(0, this._maxsize + 64);
  5078. this._bufferSize = this._buffer.byteLength;
  5079. this._checkArrayUse();
  5080. }
  5081. this._maxsize = this._byteLength;
  5082. }
  5083. if (this._uploadSize < this._buffer.byteLength) {
  5084. this._uploadSize = this._buffer.byteLength;
  5085. LayaGL.instance.bufferData(this._bufferType, this._uploadSize, this._bufferUsage);
  5086. }
  5087. LayaGL.instance.bufferSubData(this._bufferType, 0, new Uint8Array(this._buffer, 0, this._byteLength));
  5088. }
  5089. _bufferSubData(offset = 0, dataStart = 0, dataLength = 0) {
  5090. this._maxsize = Math.max(this._maxsize, this._byteLength);
  5091. if (RenderInfo.loopCount % 30 == 0) {
  5092. if (this._buffer.byteLength > (this._maxsize + 64)) {
  5093. this._buffer = this._buffer.slice(0, this._maxsize + 64);
  5094. this._bufferSize = this._buffer.byteLength;
  5095. this._checkArrayUse();
  5096. }
  5097. this._maxsize = this._byteLength;
  5098. }
  5099. if (this._uploadSize < this._buffer.byteLength) {
  5100. this._uploadSize = this._buffer.byteLength;
  5101. LayaGL.instance.bufferData(this._bufferType, this._uploadSize, this._bufferUsage);
  5102. }
  5103. if (dataStart || dataLength) {
  5104. var subBuffer = this._buffer.slice(dataStart, dataLength);
  5105. LayaGL.instance.bufferSubData(this._bufferType, offset, subBuffer);
  5106. }
  5107. else {
  5108. LayaGL.instance.bufferSubData(this._bufferType, offset, this._buffer);
  5109. }
  5110. }
  5111. _checkArrayUse() {
  5112. }
  5113. _bind_uploadForVAO() {
  5114. if (!this._upload)
  5115. return false;
  5116. this._upload = false;
  5117. this._bindForVAO();
  5118. this._bufferData();
  5119. return true;
  5120. }
  5121. _bind_upload() {
  5122. if (!this._upload)
  5123. return false;
  5124. this._upload = false;
  5125. this.bind();
  5126. this._bufferData();
  5127. return true;
  5128. }
  5129. _bind_subUpload(offset = 0, dataStart = 0, dataLength = 0) {
  5130. if (!this._upload)
  5131. return false;
  5132. this._upload = false;
  5133. this.bind();
  5134. this._bufferSubData(offset, dataStart, dataLength);
  5135. return true;
  5136. }
  5137. _resizeBuffer(nsz, copy) {
  5138. var buff = this._buffer;
  5139. if (nsz <= buff.byteLength)
  5140. return this;
  5141. var u8buf = this._u8Array;
  5142. if (copy && buff && buff.byteLength > 0) {
  5143. var newbuffer = new ArrayBuffer(nsz);
  5144. var oldU8Arr = (u8buf && u8buf.buffer == buff) ? u8buf : new Uint8Array(buff);
  5145. u8buf = this._u8Array = new Uint8Array(newbuffer);
  5146. u8buf.set(oldU8Arr, 0);
  5147. buff = this._buffer = newbuffer;
  5148. }
  5149. else {
  5150. buff = this._buffer = new ArrayBuffer(nsz);
  5151. this._u8Array = null;
  5152. }
  5153. this._checkArrayUse();
  5154. this._upload = true;
  5155. this._bufferSize = buff.byteLength;
  5156. return this;
  5157. }
  5158. append(data) {
  5159. this._upload = true;
  5160. var byteLen, n;
  5161. byteLen = data.byteLength;
  5162. if (data instanceof Uint8Array) {
  5163. this._resizeBuffer(this._byteLength + byteLen, true);
  5164. n = new Uint8Array(this._buffer, this._byteLength);
  5165. }
  5166. else if (data instanceof Uint16Array) {
  5167. this._resizeBuffer(this._byteLength + byteLen, true);
  5168. n = new Uint16Array(this._buffer, this._byteLength);
  5169. }
  5170. else if (data instanceof Float32Array) {
  5171. this._resizeBuffer(this._byteLength + byteLen, true);
  5172. n = new Float32Array(this._buffer, this._byteLength);
  5173. }
  5174. n.set(data, 0);
  5175. this._byteLength += byteLen;
  5176. this._checkArrayUse();
  5177. }
  5178. appendU16Array(data, len) {
  5179. this._resizeBuffer(this._byteLength + len * 2, true);
  5180. var u = new Uint16Array(this._buffer, this._byteLength, len);
  5181. if (len == 6) {
  5182. u[0] = data[0];
  5183. u[1] = data[1];
  5184. u[2] = data[2];
  5185. u[3] = data[3];
  5186. u[4] = data[4];
  5187. u[5] = data[5];
  5188. }
  5189. else if (len >= 100) {
  5190. u.set(new Uint16Array(data.buffer, 0, len));
  5191. }
  5192. else {
  5193. for (var i = 0; i < len; i++) {
  5194. u[i] = data[i];
  5195. }
  5196. }
  5197. this._byteLength += len * 2;
  5198. this._checkArrayUse();
  5199. }
  5200. appendEx(data, type) {
  5201. this._upload = true;
  5202. var byteLen, n;
  5203. byteLen = data.byteLength;
  5204. this._resizeBuffer(this._byteLength + byteLen, true);
  5205. n = new type(this._buffer, this._byteLength);
  5206. n.set(data, 0);
  5207. this._byteLength += byteLen;
  5208. this._checkArrayUse();
  5209. }
  5210. appendEx2(data, type, dataLen, perDataLen = 1) {
  5211. this._upload = true;
  5212. var byteLen, n;
  5213. byteLen = dataLen * perDataLen;
  5214. this._resizeBuffer(this._byteLength + byteLen, true);
  5215. n = new type(this._buffer, this._byteLength);
  5216. var i;
  5217. for (i = 0; i < dataLen; i++) {
  5218. n[i] = data[i];
  5219. }
  5220. this._byteLength += byteLen;
  5221. this._checkArrayUse();
  5222. }
  5223. getBuffer() {
  5224. return this._buffer;
  5225. }
  5226. setNeedUpload() {
  5227. this._upload = true;
  5228. }
  5229. getNeedUpload() {
  5230. return this._upload;
  5231. }
  5232. upload() {
  5233. var gl = LayaGL.instance;
  5234. var scuess = this._bind_upload();
  5235. gl.bindBuffer(this._bufferType, null);
  5236. if (this._bufferType == gl.ARRAY_BUFFER)
  5237. Buffer._bindedVertexBuffer = null;
  5238. if (this._bufferType == gl.ELEMENT_ARRAY_BUFFER)
  5239. Buffer._bindedIndexBuffer = null;
  5240. BaseShader.activeShader = null;
  5241. return scuess;
  5242. }
  5243. subUpload(offset = 0, dataStart = 0, dataLength = 0) {
  5244. var gl = LayaGL.instance;
  5245. var scuess = this._bind_subUpload();
  5246. gl.bindBuffer(this._bufferType, null);
  5247. if (this._bufferType == gl.ARRAY_BUFFER)
  5248. Buffer._bindedVertexBuffer = null;
  5249. if (this._bufferType == gl.ELEMENT_ARRAY_BUFFER)
  5250. Buffer._bindedIndexBuffer = null;
  5251. BaseShader.activeShader = null;
  5252. return scuess;
  5253. }
  5254. _disposeResource() {
  5255. this._upload = true;
  5256. this._uploadSize = 0;
  5257. }
  5258. clear() {
  5259. this._byteLength = 0;
  5260. this._upload = true;
  5261. }
  5262. }
  5263. Buffer2D.FLOAT32 = 4;
  5264. Buffer2D.SHORT = 2;
  5265. class IndexBuffer2D extends Buffer2D {
  5266. constructor(bufferUsage = 0x88e4) {
  5267. super();
  5268. this._bufferUsage = bufferUsage;
  5269. this._bufferType = LayaGL.instance.ELEMENT_ARRAY_BUFFER;
  5270. this._buffer = new ArrayBuffer(8);
  5271. }
  5272. _checkArrayUse() {
  5273. this._uint16Array && (this._uint16Array = new Uint16Array(this._buffer));
  5274. }
  5275. getUint16Array() {
  5276. return this._uint16Array || (this._uint16Array = new Uint16Array(this._buffer));
  5277. }
  5278. _bindForVAO() {
  5279. var gl = LayaGL.instance;
  5280. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._glBuffer);
  5281. }
  5282. bind() {
  5283. if (Buffer._bindedIndexBuffer !== this._glBuffer) {
  5284. var gl = LayaGL.instance;
  5285. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._glBuffer);
  5286. Buffer._bindedIndexBuffer = this._glBuffer;
  5287. return true;
  5288. }
  5289. return false;
  5290. }
  5291. destory() {
  5292. this._uint16Array = null;
  5293. this._buffer = null;
  5294. }
  5295. disposeResource() {
  5296. this._disposeResource();
  5297. }
  5298. }
  5299. IndexBuffer2D.create = function (bufferUsage = 0x88e4) {
  5300. return new IndexBuffer2D(bufferUsage);
  5301. };
  5302. class VertexBuffer2D extends Buffer2D {
  5303. constructor(vertexStride, bufferUsage) {
  5304. super();
  5305. this._vertexStride = vertexStride;
  5306. this._bufferUsage = bufferUsage;
  5307. this._bufferType = LayaGL.instance.ARRAY_BUFFER;
  5308. this._buffer = new ArrayBuffer(8);
  5309. this._floatArray32 = new Float32Array(this._buffer);
  5310. this._uint32Array = new Uint32Array(this._buffer);
  5311. }
  5312. get vertexStride() {
  5313. return this._vertexStride;
  5314. }
  5315. getFloat32Array() {
  5316. return this._floatArray32;
  5317. }
  5318. appendArray(data) {
  5319. var oldoff = this._byteLength >> 2;
  5320. this.setByteLength(this._byteLength + data.length * 4);
  5321. var vbdata = this.getFloat32Array();
  5322. vbdata.set(data, oldoff);
  5323. this._upload = true;
  5324. }
  5325. _checkArrayUse() {
  5326. this._floatArray32 && (this._floatArray32 = new Float32Array(this._buffer));
  5327. this._uint32Array && (this._uint32Array = new Uint32Array(this._buffer));
  5328. }
  5329. deleteBuffer() {
  5330. super._disposeResource();
  5331. }
  5332. _bindForVAO() {
  5333. var gl = LayaGL.instance;
  5334. gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
  5335. }
  5336. bind() {
  5337. if (Buffer._bindedVertexBuffer !== this._glBuffer) {
  5338. var gl = LayaGL.instance;
  5339. gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
  5340. Buffer._bindedVertexBuffer = this._glBuffer;
  5341. return true;
  5342. }
  5343. return false;
  5344. }
  5345. destroy() {
  5346. super.destroy();
  5347. this._byteLength = 0;
  5348. this._upload = true;
  5349. this._buffer = null;
  5350. this._floatArray32 = null;
  5351. }
  5352. }
  5353. VertexBuffer2D.create = function (vertexStride, bufferUsage = 0x88e8) {
  5354. return new VertexBuffer2D(vertexStride, bufferUsage);
  5355. };
  5356. class Mesh2D {
  5357. constructor(stride, vballoc, iballoc) {
  5358. this._stride = 0;
  5359. this.vertNum = 0;
  5360. this.indexNum = 0;
  5361. this._applied = false;
  5362. this._quadNum = 0;
  5363. this.canReuse = false;
  5364. this._stride = stride;
  5365. this._vb = new VertexBuffer2D(stride, LayaGL.instance.DYNAMIC_DRAW);
  5366. if (vballoc) {
  5367. this._vb._resizeBuffer(vballoc, false);
  5368. }
  5369. else {
  5370. Config.webGL2D_MeshAllocMaxMem && this._vb._resizeBuffer(64 * 1024 * stride, false);
  5371. }
  5372. this._ib = new IndexBuffer2D();
  5373. if (iballoc) {
  5374. this._ib._resizeBuffer(iballoc, false);
  5375. }
  5376. }
  5377. cloneWithNewVB() {
  5378. var mesh = new Mesh2D(this._stride, 0, 0);
  5379. mesh._ib = this._ib;
  5380. mesh._quadNum = this._quadNum;
  5381. mesh._attribInfo = this._attribInfo;
  5382. return mesh;
  5383. }
  5384. cloneWithNewVBIB() {
  5385. var mesh = new Mesh2D(this._stride, 0, 0);
  5386. mesh._attribInfo = this._attribInfo;
  5387. return mesh;
  5388. }
  5389. getVBW() {
  5390. this._vb.setNeedUpload();
  5391. return this._vb;
  5392. }
  5393. getVBR() {
  5394. return this._vb;
  5395. }
  5396. getIBR() {
  5397. return this._ib;
  5398. }
  5399. getIBW() {
  5400. this._ib.setNeedUpload();
  5401. return this._ib;
  5402. }
  5403. createQuadIB(QuadNum) {
  5404. this._quadNum = QuadNum;
  5405. this._ib._resizeBuffer(QuadNum * 6 * 2, false);
  5406. this._ib.byteLength = this._ib.bufferLength;
  5407. var bd = this._ib.getUint16Array();
  5408. var idx = 0;
  5409. var curvert = 0;
  5410. for (var i = 0; i < QuadNum; i++) {
  5411. bd[idx++] = curvert;
  5412. bd[idx++] = curvert + 2;
  5413. bd[idx++] = curvert + 1;
  5414. bd[idx++] = curvert;
  5415. bd[idx++] = curvert + 3;
  5416. bd[idx++] = curvert + 2;
  5417. curvert += 4;
  5418. }
  5419. this._ib.setNeedUpload();
  5420. }
  5421. setAttributes(attribs) {
  5422. this._attribInfo = attribs;
  5423. if (this._attribInfo.length % 3 != 0) {
  5424. throw 'Mesh2D setAttributes error!';
  5425. }
  5426. }
  5427. configVAO(gl) {
  5428. if (this._applied)
  5429. return;
  5430. this._applied = true;
  5431. if (!this._vao) {
  5432. this._vao = new BufferState2D();
  5433. }
  5434. this._vao.bind();
  5435. this._vb._bindForVAO();
  5436. this._ib.setNeedUpload();
  5437. this._ib._bind_uploadForVAO();
  5438. var attribNum = this._attribInfo.length / 3;
  5439. var idx = 0;
  5440. for (var i = 0; i < attribNum; i++) {
  5441. var _size = this._attribInfo[idx + 1];
  5442. var _type = this._attribInfo[idx];
  5443. var _off = this._attribInfo[idx + 2];
  5444. gl.enableVertexAttribArray(i);
  5445. gl.vertexAttribPointer(i, _size, _type, false, this._stride, _off);
  5446. idx += 3;
  5447. }
  5448. this._vao.unBind();
  5449. }
  5450. useMesh(gl) {
  5451. this._applied || this.configVAO(gl);
  5452. this._vao.bind();
  5453. this._vb.bind();
  5454. this._ib._bind_upload() || this._ib.bind();
  5455. this._vb._bind_upload() || this._vb.bind();
  5456. }
  5457. getEleNum() {
  5458. return this._ib.getBuffer().byteLength / 2;
  5459. }
  5460. releaseMesh() { }
  5461. destroy() {
  5462. }
  5463. clearVB() {
  5464. this._vb.clear();
  5465. }
  5466. }
  5467. Mesh2D._gvaoid = 0;
  5468. class MeshQuadTexture extends Mesh2D {
  5469. constructor() {
  5470. super(MeshQuadTexture.const_stride, 4, 4);
  5471. this.canReuse = true;
  5472. this.setAttributes(MeshQuadTexture._fixattriInfo);
  5473. if (!MeshQuadTexture._fixib) {
  5474. this.createQuadIB(MeshQuadTexture._maxIB);
  5475. MeshQuadTexture._fixib = this._ib;
  5476. }
  5477. else {
  5478. this._ib = MeshQuadTexture._fixib;
  5479. this._quadNum = MeshQuadTexture._maxIB;
  5480. }
  5481. }
  5482. static __int__() {
  5483. MeshQuadTexture._fixattriInfo = [5126, 4, 0,
  5484. 5121, 4, 16,
  5485. 5121, 4, 20];
  5486. }
  5487. static getAMesh(mainctx) {
  5488. var ret = null;
  5489. if (MeshQuadTexture._POOL.length) {
  5490. ret = MeshQuadTexture._POOL.pop();
  5491. }
  5492. else
  5493. ret = new MeshQuadTexture();
  5494. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshQuadTexture.const_stride, false);
  5495. return ret;
  5496. }
  5497. releaseMesh() {
  5498. this._vb.setByteLength(0);
  5499. this.vertNum = 0;
  5500. this.indexNum = 0;
  5501. MeshQuadTexture._POOL.push(this);
  5502. }
  5503. destroy() {
  5504. this._vb.destroy();
  5505. this._vb.deleteBuffer();
  5506. }
  5507. addQuad(pos, uv, color, useTex) {
  5508. var vb = this._vb;
  5509. var vpos = (vb._byteLength >> 2);
  5510. vb.setByteLength((vpos + MeshQuadTexture.const_stride) << 2);
  5511. var vbdata = vb._floatArray32 || vb.getFloat32Array();
  5512. var vbu32Arr = vb._uint32Array;
  5513. var cpos = vpos;
  5514. var useTexVal = useTex ? 0xff : 0;
  5515. vbdata[cpos++] = pos[0];
  5516. vbdata[cpos++] = pos[1];
  5517. vbdata[cpos++] = uv[0];
  5518. vbdata[cpos++] = uv[1];
  5519. vbu32Arr[cpos++] = color;
  5520. vbu32Arr[cpos++] = useTexVal;
  5521. vbdata[cpos++] = pos[2];
  5522. vbdata[cpos++] = pos[3];
  5523. vbdata[cpos++] = uv[2];
  5524. vbdata[cpos++] = uv[3];
  5525. vbu32Arr[cpos++] = color;
  5526. vbu32Arr[cpos++] = useTexVal;
  5527. vbdata[cpos++] = pos[4];
  5528. vbdata[cpos++] = pos[5];
  5529. vbdata[cpos++] = uv[4];
  5530. vbdata[cpos++] = uv[5];
  5531. vbu32Arr[cpos++] = color;
  5532. vbu32Arr[cpos++] = useTexVal;
  5533. vbdata[cpos++] = pos[6];
  5534. vbdata[cpos++] = pos[7];
  5535. vbdata[cpos++] = uv[6];
  5536. vbdata[cpos++] = uv[7];
  5537. vbu32Arr[cpos++] = color;
  5538. vbu32Arr[cpos++] = useTexVal;
  5539. vb._upload = true;
  5540. }
  5541. }
  5542. MeshQuadTexture.const_stride = 24;
  5543. MeshQuadTexture._maxIB = 16 * 1024;
  5544. MeshQuadTexture._POOL = [];
  5545. class MeshTexture extends Mesh2D {
  5546. constructor() {
  5547. super(MeshTexture.const_stride, 4, 4);
  5548. this.canReuse = true;
  5549. this.setAttributes(MeshTexture._fixattriInfo);
  5550. }
  5551. static __init__() {
  5552. MeshTexture._fixattriInfo = [5126, 4, 0,
  5553. 5121, 4, 16,
  5554. 5121, 4, 20];
  5555. }
  5556. static getAMesh(mainctx) {
  5557. var ret;
  5558. if (MeshTexture._POOL.length) {
  5559. ret = MeshTexture._POOL.pop();
  5560. }
  5561. else
  5562. ret = new MeshTexture();
  5563. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshTexture.const_stride, false);
  5564. return ret;
  5565. }
  5566. addData(vertices, uvs, idx, matrix, rgba) {
  5567. var vb = this._vb;
  5568. var ib = this._ib;
  5569. var vertsz = vertices.length >> 1;
  5570. var startpos = vb.needSize(vertsz * MeshTexture.const_stride);
  5571. var f32pos = startpos >> 2;
  5572. var vbdata = vb._floatArray32 || vb.getFloat32Array();
  5573. var vbu32Arr = vb._uint32Array;
  5574. var ci = 0;
  5575. var m00 = matrix.a;
  5576. var m01 = matrix.b;
  5577. var m10 = matrix.c;
  5578. var m11 = matrix.d;
  5579. var tx = matrix.tx;
  5580. var ty = matrix.ty;
  5581. var i = 0;
  5582. for (i = 0; i < vertsz; i++) {
  5583. var x = vertices[ci], y = vertices[ci + 1];
  5584. vbdata[f32pos] = x * m00 + y * m10 + tx;
  5585. vbdata[f32pos + 1] = x * m01 + y * m11 + ty;
  5586. vbdata[f32pos + 2] = uvs[ci];
  5587. vbdata[f32pos + 3] = uvs[ci + 1];
  5588. vbu32Arr[f32pos + 4] = rgba;
  5589. vbu32Arr[f32pos + 5] = 0xff;
  5590. f32pos += 6;
  5591. ci += 2;
  5592. }
  5593. vb.setNeedUpload();
  5594. var vertN = this.vertNum;
  5595. var sz = idx.length;
  5596. var stib = ib.needSize(idx.byteLength);
  5597. var cidx = ib.getUint16Array();
  5598. var stibid = stib >> 1;
  5599. if (vertN > 0) {
  5600. var end = stibid + sz;
  5601. var si = 0;
  5602. for (i = stibid; i < end; i++, si++) {
  5603. cidx[i] = idx[si] + vertN;
  5604. }
  5605. }
  5606. else {
  5607. cidx.set(idx, stibid);
  5608. }
  5609. ib.setNeedUpload();
  5610. this.vertNum += vertsz;
  5611. this.indexNum += idx.length;
  5612. }
  5613. releaseMesh() {
  5614. this._vb.setByteLength(0);
  5615. this._ib.setByteLength(0);
  5616. this.vertNum = 0;
  5617. this.indexNum = 0;
  5618. MeshTexture._POOL.push(this);
  5619. }
  5620. destroy() {
  5621. this._ib.destroy();
  5622. this._vb.destroy();
  5623. this._ib.disposeResource();
  5624. this._vb.deleteBuffer();
  5625. }
  5626. }
  5627. MeshTexture.const_stride = 24;
  5628. MeshTexture._POOL = [];
  5629. class MeshVG extends Mesh2D {
  5630. constructor() {
  5631. super(MeshVG.const_stride, 4, 4);
  5632. this.canReuse = true;
  5633. this.setAttributes(MeshVG._fixattriInfo);
  5634. }
  5635. static __init__() {
  5636. MeshVG._fixattriInfo = [5126, 2, 0,
  5637. 5121, 4, 8];
  5638. }
  5639. static getAMesh(mainctx) {
  5640. var ret;
  5641. if (MeshVG._POOL.length) {
  5642. ret = MeshVG._POOL.pop();
  5643. }
  5644. else
  5645. ret = new MeshVG();
  5646. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshVG.const_stride, false);
  5647. return ret;
  5648. }
  5649. addVertAndIBToMesh(ctx, points, rgba, ib) {
  5650. var startpos = this._vb.needSize(points.length / 2 * MeshVG.const_stride);
  5651. var f32pos = startpos >> 2;
  5652. var vbdata = this._vb._floatArray32 || this._vb.getFloat32Array();
  5653. var vbu32Arr = this._vb._uint32Array;
  5654. var ci = 0;
  5655. var sz = points.length / 2;
  5656. for (var i = 0; i < sz; i++) {
  5657. vbdata[f32pos++] = points[ci];
  5658. vbdata[f32pos++] = points[ci + 1];
  5659. ci += 2;
  5660. vbu32Arr[f32pos++] = rgba;
  5661. }
  5662. this._vb.setNeedUpload();
  5663. this._ib.append(new Uint16Array(ib));
  5664. this._ib.setNeedUpload();
  5665. this.vertNum += sz;
  5666. this.indexNum += ib.length;
  5667. }
  5668. releaseMesh() {
  5669. this._vb.setByteLength(0);
  5670. this._ib.setByteLength(0);
  5671. this.vertNum = 0;
  5672. this.indexNum = 0;
  5673. MeshVG._POOL.push(this);
  5674. }
  5675. destroy() {
  5676. this._ib.destroy();
  5677. this._vb.destroy();
  5678. this._ib.disposeResource();
  5679. this._vb.deleteBuffer();
  5680. }
  5681. }
  5682. MeshVG.const_stride = 12;
  5683. MeshVG._POOL = [];
  5684. class WebGLCacheAsNormalCanvas {
  5685. constructor(ctx, sp) {
  5686. this.submitStartPos = 0;
  5687. this.submitEndPos = 0;
  5688. this.touches = [];
  5689. this.submits = [];
  5690. this.sprite = null;
  5691. this.meshlist = [];
  5692. this.cachedClipInfo = new Matrix();
  5693. this.oldTx = 0;
  5694. this.oldTy = 0;
  5695. this.invMat = new Matrix();
  5696. this.context = ctx;
  5697. this.sprite = sp;
  5698. ctx._globalClipMatrix.copyTo(this.cachedClipInfo);
  5699. }
  5700. startRec() {
  5701. let context = this.context;
  5702. if (context._charSubmitCache && context._charSubmitCache._enable) {
  5703. context._charSubmitCache.enable(false, context);
  5704. context._charSubmitCache.enable(true, context);
  5705. }
  5706. context._incache = true;
  5707. this.touches.length = 0;
  5708. context.touches = this.touches;
  5709. context._globalClipMatrix.copyTo(this.cachedClipInfo);
  5710. this.submits.length = 0;
  5711. this.submitStartPos = context._submits._length;
  5712. for (var i = 0, sz = this.meshlist.length; i < sz; i++) {
  5713. var curm = this.meshlist[i];
  5714. curm.canReuse ? (curm.releaseMesh()) : (curm.destroy());
  5715. }
  5716. this.meshlist.length = 0;
  5717. this._mesh = MeshQuadTexture.getAMesh(false);
  5718. this._pathMesh = MeshVG.getAMesh(false);
  5719. this._triangleMesh = MeshTexture.getAMesh(false);
  5720. this.meshlist.push(this._mesh);
  5721. this.meshlist.push(this._pathMesh);
  5722. this.meshlist.push(this._triangleMesh);
  5723. context._curSubmit = SubmitBase.RENDERBASE;
  5724. this._oldMesh = context._mesh;
  5725. this._oldPathMesh = context._pathMesh;
  5726. this._oldTriMesh = context._triangleMesh;
  5727. this._oldMeshList = context.meshlist;
  5728. context._mesh = this._mesh;
  5729. context._pathMesh = this._pathMesh;
  5730. context._triangleMesh = this._triangleMesh;
  5731. context.meshlist = this.meshlist;
  5732. this.oldTx = context._curMat.tx;
  5733. this.oldTy = context._curMat.ty;
  5734. context._curMat.tx = 0;
  5735. context._curMat.ty = 0;
  5736. context._curMat.copyTo(this.invMat);
  5737. this.invMat.invert();
  5738. }
  5739. endRec() {
  5740. let context = this.context;
  5741. if (context._charSubmitCache && context._charSubmitCache._enable) {
  5742. context._charSubmitCache.enable(false, context);
  5743. context._charSubmitCache.enable(true, context);
  5744. }
  5745. var parsubmits = context._submits;
  5746. this.submitEndPos = parsubmits._length;
  5747. var num = this.submitEndPos - this.submitStartPos;
  5748. for (var i = 0; i < num; i++) {
  5749. this.submits.push(parsubmits[this.submitStartPos + i]);
  5750. }
  5751. parsubmits._length -= num;
  5752. context._mesh = this._oldMesh;
  5753. context._pathMesh = this._oldPathMesh;
  5754. context._triangleMesh = this._oldTriMesh;
  5755. context.meshlist = this._oldMeshList;
  5756. context._curSubmit = SubmitBase.RENDERBASE;
  5757. context._curMat.tx = this.oldTx;
  5758. context._curMat.ty = this.oldTy;
  5759. context.touches = null;
  5760. context._incache = false;
  5761. }
  5762. isCacheValid() {
  5763. var curclip = this.context._globalClipMatrix;
  5764. if (curclip.a != this.cachedClipInfo.a || curclip.b != this.cachedClipInfo.b || curclip.c != this.cachedClipInfo.c
  5765. || curclip.d != this.cachedClipInfo.d || curclip.tx != this.cachedClipInfo.tx || curclip.ty != this.cachedClipInfo.ty)
  5766. return false;
  5767. return true;
  5768. }
  5769. flushsubmit() {
  5770. var curSubmit = SubmitBase.RENDERBASE;
  5771. this.submits.forEach(function (subm) {
  5772. if (subm == SubmitBase.RENDERBASE)
  5773. return;
  5774. SubmitBase.preRender = curSubmit;
  5775. curSubmit = subm;
  5776. subm.renderSubmit();
  5777. });
  5778. }
  5779. releaseMem() {
  5780. }
  5781. }
  5782. WebGLCacheAsNormalCanvas.matI = new Matrix();
  5783. var texture_vs = "/*\r\n\ttexture和fillrect使用的。\r\n*/\r\nattribute vec4 posuv;\r\nattribute vec4 attribColor;\r\nattribute vec4 attribFlags;\r\n//attribute vec4 clipDir;\r\n//attribute vec2 clipRect;\r\nuniform vec4 clipMatDir;\r\nuniform vec2 clipMatPos;\t\t// 这个是全局的,不用再应用矩阵了。\r\nvarying vec2 cliped;\r\nuniform vec2 size;\r\nuniform vec2 clipOff;\t\t\t// 使用要把clip偏移。cacheas normal用. 只用了[0]\r\n#ifdef WORLDMAT\r\n\tuniform mat4 mmat;\r\n#endif\r\n#ifdef MVP3D\r\n\tuniform mat4 u_MvpMatrix;\r\n#endif\r\nvarying vec4 v_texcoordAlpha;\r\nvarying vec4 v_color;\r\nvarying float v_useTex;\r\n\r\nvoid main() {\r\n\r\n\tvec4 pos = vec4(posuv.xy,0.,1.);\r\n#ifdef WORLDMAT\r\n\tpos=mmat*pos;\r\n#endif\r\n\tvec4 pos1 =vec4((pos.x/size.x-0.5)*2.0,(0.5-pos.y/size.y)*2.0,0.,1.0);\r\n#ifdef MVP3D\r\n\tgl_Position=u_MvpMatrix*pos1;\r\n#else\r\n\tgl_Position=pos1;\r\n#endif\r\n\tv_texcoordAlpha.xy = posuv.zw;\r\n\t//v_texcoordAlpha.z = attribColor.a/255.0;\r\n\tv_color = attribColor/255.0;\r\n\tv_color.xyz*=v_color.w;//反正后面也要预乘\r\n\t\r\n\tv_useTex = attribFlags.r/255.0;\r\n\tfloat clipw = length(clipMatDir.xy);\r\n\tfloat cliph = length(clipMatDir.zw);\r\n\t\r\n\tvec2 clpos = clipMatPos.xy;\r\n\t#ifdef WORLDMAT\r\n\t\t// 如果有mmat,需要修改clipMatPos,因为 这是cacheas normal (如果不是就错了), clipMatPos被去掉了偏移\r\n\t\tif(clipOff[0]>0.0){\r\n\t\t\tclpos.x+=mmat[3].x;\t//tx\t最简单处理\r\n\t\t\tclpos.y+=mmat[3].y;\t//ty\r\n\t\t}\r\n\t#endif\r\n\tvec2 clippos = pos.xy - clpos;\t//pos已经应用矩阵了,为了减的有意义,clip的位置也要缩放\r\n\tif(clipw>20000. && cliph>20000.)\r\n\t\tcliped = vec2(0.5,0.5);\r\n\telse {\r\n\t\t//转成0到1之间。/clipw/clipw 表示clippos与normalize之后的clip朝向点积之后,再除以clipw\r\n\t\tcliped=vec2( dot(clippos,clipMatDir.xy)/clipw/clipw, dot(clippos,clipMatDir.zw)/cliph/cliph);\r\n\t}\r\n\r\n}";
  5784. var texture_ps = "/*\r\n\ttexture和fillrect使用的。\r\n*/\r\n#if defined(GL_FRAGMENT_PRECISION_HIGH)// 原来的写法会被我们自己的解析流程处理,而我们的解析是不认内置宏的,导致被删掉,所以改成 if defined 了\r\nprecision highp float;\r\n#else\r\nprecision mediump float;\r\n#endif\r\n\r\nvarying vec4 v_texcoordAlpha;\r\nvarying vec4 v_color;\r\nvarying float v_useTex;\r\nuniform sampler2D texture;\r\nvarying vec2 cliped;\r\n\r\n#ifdef BLUR_FILTER\r\nuniform vec4 strength_sig2_2sig2_gauss1;\r\nuniform vec2 blurInfo;\r\n\r\n#define PI 3.141593\r\n\r\nfloat getGaussian(float x, float y){\r\n return strength_sig2_2sig2_gauss1.w*exp(-(x*x+y*y)/strength_sig2_2sig2_gauss1.z);\r\n}\r\n\r\nvec4 blur(){\r\n const float blurw = 9.0;\r\n vec4 vec4Color = vec4(0.0,0.0,0.0,0.0);\r\n vec2 halfsz=vec2(blurw,blurw)/2.0/blurInfo; \r\n vec2 startpos=v_texcoordAlpha.xy-halfsz;\r\n vec2 ctexcoord = startpos;\r\n vec2 step = 1.0/blurInfo; //每个像素 \r\n \r\n for(float y = 0.0;y<=blurw; ++y){\r\n ctexcoord.x=startpos.x;\r\n for(float x = 0.0;x<=blurw; ++x){\r\n //TODO 纹理坐标的固定偏移应该在vs中处理\r\n vec4Color += texture2D(texture, ctexcoord)*getGaussian(x-blurw/2.0,y-blurw/2.0);\r\n ctexcoord.x+=step.x;\r\n }\r\n ctexcoord.y+=step.y;\r\n }\r\n return vec4Color;\r\n}\r\n#endif\r\n\r\n#ifdef COLOR_FILTER\r\nuniform vec4 colorAlpha;\r\nuniform mat4 colorMat;\r\n#endif\r\n\r\n#ifdef GLOW_FILTER\r\nuniform vec4 u_color;\r\nuniform vec4 u_blurInfo1;\r\nuniform vec4 u_blurInfo2;\r\n#endif\r\n\r\n#ifdef COLOR_ADD\r\nuniform vec4 colorAdd;\r\n#endif\r\n\r\n#ifdef FILLTEXTURE\t\r\nuniform vec4 u_TexRange;//startu,startv,urange, vrange\r\n#endif\r\nvoid main() {\r\n\tif(cliped.x<0.) discard;\r\n\tif(cliped.x>1.) discard;\r\n\tif(cliped.y<0.) discard;\r\n\tif(cliped.y>1.) discard;\r\n\t\r\n#ifdef FILLTEXTURE\t\r\n vec4 color= texture2D(texture, fract(v_texcoordAlpha.xy)*u_TexRange.zw + u_TexRange.xy);\r\n#else\r\n vec4 color= texture2D(texture, v_texcoordAlpha.xy);\r\n#endif\r\n\r\n if(v_useTex<=0.)color = vec4(1.,1.,1.,1.);\r\n color.a*=v_color.w;\r\n //color.rgb*=v_color.w;\r\n color.rgb*=v_color.rgb;\r\n gl_FragColor=color;\r\n \r\n #ifdef COLOR_ADD\r\n\tgl_FragColor = vec4(colorAdd.rgb,colorAdd.a*gl_FragColor.a);\r\n\tgl_FragColor.xyz *= colorAdd.a;\r\n #endif\r\n \r\n #ifdef BLUR_FILTER\r\n\tgl_FragColor = blur();\r\n\tgl_FragColor.w*=v_color.w; \r\n #endif\r\n \r\n #ifdef COLOR_FILTER\r\n\tmat4 alphaMat =colorMat;\r\n\r\n\talphaMat[0][3] *= gl_FragColor.a;\r\n\talphaMat[1][3] *= gl_FragColor.a;\r\n\talphaMat[2][3] *= gl_FragColor.a;\r\n\r\n\tgl_FragColor = gl_FragColor * alphaMat;\r\n\tgl_FragColor += colorAlpha/255.0*gl_FragColor.a;\r\n #endif\r\n \r\n #ifdef GLOW_FILTER\r\n\tconst float c_IterationTime = 10.0;\r\n\tfloat floatIterationTotalTime = c_IterationTime * c_IterationTime;\r\n\tvec4 vec4Color = vec4(0.0,0.0,0.0,0.0);\r\n\tvec2 vec2FilterDir = vec2(-(u_blurInfo1.z)/u_blurInfo2.x,-(u_blurInfo1.w)/u_blurInfo2.y);\r\n\tvec2 vec2FilterOff = vec2(u_blurInfo1.x/u_blurInfo2.x/c_IterationTime * 2.0,u_blurInfo1.y/u_blurInfo2.y/c_IterationTime * 2.0);\r\n\tfloat maxNum = u_blurInfo1.x * u_blurInfo1.y;\r\n\tvec2 vec2Off = vec2(0.0,0.0);\r\n\tfloat floatOff = c_IterationTime/2.0;\r\n\tfor(float i = 0.0;i<=c_IterationTime; ++i){\r\n\t\tfor(float j = 0.0;j<=c_IterationTime; ++j){\r\n\t\t\tvec2Off = vec2(vec2FilterOff.x * (i - floatOff),vec2FilterOff.y * (j - floatOff));\r\n\t\t\tvec4Color += texture2D(texture, v_texcoordAlpha.xy + vec2FilterDir + vec2Off)/floatIterationTotalTime;\r\n\t\t}\r\n\t}\r\n\tgl_FragColor = vec4(u_color.rgb,vec4Color.a * u_blurInfo2.z);\r\n\tgl_FragColor.rgb *= gl_FragColor.a; \r\n #endif\r\n \r\n}";
  5785. var prime_vs = "attribute vec4 position;\r\nattribute vec4 attribColor;\r\n//attribute vec4 clipDir;\r\n//attribute vec2 clipRect;\r\nuniform vec4 clipMatDir;\r\nuniform vec2 clipMatPos;\r\n#ifdef WORLDMAT\r\n\tuniform mat4 mmat;\r\n#endif\r\nuniform mat4 u_mmat2;\r\n//uniform vec2 u_pos;\r\nuniform vec2 size;\r\nvarying vec4 color;\r\n//vec4 dirxy=vec4(0.9,0.1, -0.1,0.9);\r\n//vec4 clip=vec4(100.,30.,300.,600.);\r\nvarying vec2 cliped;\r\nvoid main(){\r\n\t\r\n#ifdef WORLDMAT\r\n\tvec4 pos=mmat*vec4(position.xy,0.,1.);\r\n\tgl_Position =vec4((pos.x/size.x-0.5)*2.0,(0.5-pos.y/size.y)*2.0,pos.z,1.0);\r\n#else\r\n\tgl_Position =vec4((position.x/size.x-0.5)*2.0,(0.5-position.y/size.y)*2.0,position.z,1.0);\r\n#endif\t\r\n\tfloat clipw = length(clipMatDir.xy);\r\n\tfloat cliph = length(clipMatDir.zw);\r\n\tvec2 clippos = position.xy - clipMatPos.xy;\t//pos已经应用矩阵了,为了减的有意义,clip的位置也要缩放\r\n\tif(clipw>20000. && cliph>20000.)\r\n\t\tcliped = vec2(0.5,0.5);\r\n\telse {\r\n\t\t//clipdir是带缩放的方向,由于上面clippos是在缩放后的空间计算的,所以需要把方向先normalize一下\r\n\t\tcliped=vec2( dot(clippos,clipMatDir.xy)/clipw/clipw, dot(clippos,clipMatDir.zw)/cliph/cliph);\r\n\t}\r\n //pos2d.x = dot(clippos,dirx);\r\n color=attribColor/255.;\r\n}";
  5786. var prime_ps = "precision mediump float;\r\n//precision mediump float;\r\nvarying vec4 color;\r\n//uniform float alpha;\r\nvarying vec2 cliped;\r\nvoid main(){\r\n\t//vec4 a=vec4(color.r, color.g, color.b, 1);\r\n\t//a.a*=alpha;\r\n gl_FragColor= color;// vec4(color.r, color.g, color.b, alpha);\r\n\tgl_FragColor.rgb*=color.a;\r\n\tif(cliped.x<0.) discard;\r\n\tif(cliped.x>1.) discard;\r\n\tif(cliped.y<0.) discard;\r\n\tif(cliped.y>1.) discard;\r\n}";
  5787. var skin_vs = "attribute vec2 position;\r\nattribute vec2 texcoord;\r\nattribute vec4 color;\r\nuniform vec2 size;\r\nuniform float offsetX;\r\nuniform float offsetY;\r\nuniform mat4 mmat;\r\nuniform mat4 u_mmat2;\r\nvarying vec2 v_texcoord;\r\nvarying vec4 v_color;\r\nvoid main() {\r\n vec4 pos=mmat*u_mmat2*vec4(offsetX+position.x,offsetY+position.y,0,1 );\r\n gl_Position = vec4((pos.x/size.x-0.5)*2.0,(0.5-pos.y/size.y)*2.0,pos.z,1.0);\r\n v_color = color;\r\n v_color.rgb *= v_color.a;\r\n v_texcoord = texcoord; \r\n}";
  5788. var skin_ps = "precision mediump float;\r\nvarying vec2 v_texcoord;\r\nvarying vec4 v_color;\r\nuniform sampler2D texture;\r\nuniform float alpha;\r\nvoid main() {\r\n\tvec4 t_color = texture2D(texture, v_texcoord);\r\n\tgl_FragColor = t_color.rgba * v_color;\r\n\tgl_FragColor *= alpha;\r\n}";
  5789. class Shader2D {
  5790. constructor() {
  5791. this.ALPHA = 1;
  5792. this.defines = new ShaderDefines2D();
  5793. this.shaderType = 0;
  5794. this.fillStyle = DrawStyle.DEFAULT;
  5795. this.strokeStyle = DrawStyle.DEFAULT;
  5796. }
  5797. destroy() {
  5798. this.defines = null;
  5799. this.filters = null;
  5800. }
  5801. static __init__() {
  5802. Shader.preCompile2D(0, ShaderDefines2D.TEXTURE2D, texture_vs, texture_ps, null);
  5803. Shader.preCompile2D(0, ShaderDefines2D.PRIMITIVE, prime_vs, prime_ps, null);
  5804. Shader.preCompile2D(0, ShaderDefines2D.SKINMESH, skin_vs, skin_ps, null);
  5805. }
  5806. }
  5807. class SkinMeshBuffer {
  5808. constructor() {
  5809. var gl = LayaGL.instance;
  5810. this.ib = IndexBuffer2D.create(gl.DYNAMIC_DRAW);
  5811. this.vb = VertexBuffer2D.create(8);
  5812. }
  5813. static getInstance() {
  5814. return SkinMeshBuffer.instance = SkinMeshBuffer.instance || new SkinMeshBuffer();
  5815. }
  5816. addSkinMesh(skinMesh) {
  5817. skinMesh.getData2(this.vb, this.ib, this.vb._byteLength / 32);
  5818. }
  5819. reset() {
  5820. this.vb.clear();
  5821. this.ib.clear();
  5822. }
  5823. }
  5824. class BasePoly {
  5825. static createLine2(p, indices, lineWidth, indexBase, outVertex, loop) {
  5826. if (p.length < 4)
  5827. return null;
  5828. var points = BasePoly.tempData.length > (p.length + 2) ? BasePoly.tempData : new Array(p.length + 2);
  5829. points[0] = p[0];
  5830. points[1] = p[1];
  5831. var newlen = 2;
  5832. var i = 0;
  5833. var length = p.length;
  5834. for (i = 2; i < length; i += 2) {
  5835. if (Math.abs(p[i] - p[i - 2]) + Math.abs(p[i + 1] - p[i - 1]) > 0.01) {
  5836. points[newlen++] = p[i];
  5837. points[newlen++] = p[i + 1];
  5838. }
  5839. }
  5840. if (loop && Math.abs(p[0] - points[newlen - 2]) + Math.abs(p[1] - points[newlen - 1]) > 0.01) {
  5841. points[newlen++] = p[0];
  5842. points[newlen++] = p[1];
  5843. }
  5844. var result = outVertex;
  5845. length = newlen / 2;
  5846. var w = lineWidth / 2;
  5847. var px, py, p1x, p1y, p2x, p2y, p3x, p3y;
  5848. var perpx, perpy, perp2x, perp2y;
  5849. var a1, b1, c1, a2, b2, c2;
  5850. var denom, dist;
  5851. p1x = points[0];
  5852. p1y = points[1];
  5853. p2x = points[2];
  5854. p2y = points[3];
  5855. perpx = -(p1y - p2y);
  5856. perpy = p1x - p2x;
  5857. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5858. perpx = perpx / dist * w;
  5859. perpy = perpy / dist * w;
  5860. result.push(p1x - perpx, p1y - perpy, p1x + perpx, p1y + perpy);
  5861. for (i = 1; i < length - 1; i++) {
  5862. p1x = points[(i - 1) * 2];
  5863. p1y = points[(i - 1) * 2 + 1];
  5864. p2x = points[(i) * 2];
  5865. p2y = points[(i) * 2 + 1];
  5866. p3x = points[(i + 1) * 2];
  5867. p3y = points[(i + 1) * 2 + 1];
  5868. perpx = -(p1y - p2y);
  5869. perpy = p1x - p2x;
  5870. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5871. perpx = perpx / dist * w;
  5872. perpy = perpy / dist * w;
  5873. perp2x = -(p2y - p3y);
  5874. perp2y = p2x - p3x;
  5875. dist = Math.sqrt(perp2x * perp2x + perp2y * perp2y);
  5876. perp2x = perp2x / dist * w;
  5877. perp2y = perp2y / dist * w;
  5878. a1 = (-perpy + p1y) - (-perpy + p2y);
  5879. b1 = (-perpx + p2x) - (-perpx + p1x);
  5880. c1 = (-perpx + p1x) * (-perpy + p2y) - (-perpx + p2x) * (-perpy + p1y);
  5881. a2 = (-perp2y + p3y) - (-perp2y + p2y);
  5882. b2 = (-perp2x + p2x) - (-perp2x + p3x);
  5883. c2 = (-perp2x + p3x) * (-perp2y + p2y) - (-perp2x + p2x) * (-perp2y + p3y);
  5884. denom = a1 * b2 - a2 * b1;
  5885. if (Math.abs(denom) < 0.1) {
  5886. denom += 10.1;
  5887. result.push(p2x - perpx, p2y - perpy, p2x + perpx, p2y + perpy);
  5888. continue;
  5889. }
  5890. px = (b1 * c2 - b2 * c1) / denom;
  5891. py = (a2 * c1 - a1 * c2) / denom;
  5892. result.push(px, py, p2x - (px - p2x), p2y - (py - p2y));
  5893. }
  5894. p1x = points[newlen - 4];
  5895. p1y = points[newlen - 3];
  5896. p2x = points[newlen - 2];
  5897. p2y = points[newlen - 1];
  5898. perpx = -(p1y - p2y);
  5899. perpy = p1x - p2x;
  5900. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5901. perpx = perpx / dist * w;
  5902. perpy = perpy / dist * w;
  5903. result.push(p2x - perpx, p2y - perpy, p2x + perpx, p2y + perpy);
  5904. for (i = 1; i < length; i++) {
  5905. indices.push(indexBase + (i - 1) * 2, indexBase + (i - 1) * 2 + 1, indexBase + i * 2 + 1, indexBase + i * 2 + 1, indexBase + i * 2, indexBase + (i - 1) * 2);
  5906. }
  5907. return result;
  5908. }
  5909. static createLineTriangle(path, color, width, loop, outvb, vbstride, outib) {
  5910. var points = path.slice();
  5911. var ptlen = points.length;
  5912. var p1x = points[0], p1y = points[1];
  5913. var p2x = points[2], p2y = points[2];
  5914. var len = 0;
  5915. var rp = 0;
  5916. var dx = 0, dy = 0;
  5917. var pointnum = ptlen / 2;
  5918. if (pointnum <= 1)
  5919. return;
  5920. if (pointnum == 2) {
  5921. return;
  5922. }
  5923. var tmpData = new Array(pointnum * 4);
  5924. var realPtNum = 0;
  5925. var ci = 0;
  5926. for (var i = 0; i < pointnum - 1; i++) {
  5927. p1x = points[ci++], p1y = points[ci++];
  5928. p2x = points[ci++], p2y = points[ci++];
  5929. dx = p2x - p1x, dy = p2y - p1y;
  5930. if (dx != 0 && dy != 0) {
  5931. len = Math.sqrt(dx * dx + dy * dy);
  5932. if (len > 1e-3) {
  5933. rp = realPtNum * 4;
  5934. tmpData[rp] = p1x;
  5935. tmpData[rp + 1] = p1y;
  5936. tmpData[rp + 2] = dx / len;
  5937. tmpData[rp + 3] = dy / len;
  5938. realPtNum++;
  5939. }
  5940. }
  5941. }
  5942. if (loop) {
  5943. p1x = points[ptlen - 2], p1y = points[ptlen - 1];
  5944. p2x = points[0], p2y = points[1];
  5945. dx = p2x - p1x, dy = p2y - p1y;
  5946. if (dx != 0 && dy != 0) {
  5947. len = Math.sqrt(dx * dx + dy * dy);
  5948. if (len > 1e-3) {
  5949. rp = realPtNum * 4;
  5950. tmpData[rp] = p1x;
  5951. tmpData[rp + 1] = p1y;
  5952. tmpData[rp + 2] = dx / len;
  5953. tmpData[rp + 3] = dy / len;
  5954. realPtNum++;
  5955. }
  5956. }
  5957. }
  5958. else {
  5959. rp = realPtNum * 4;
  5960. tmpData[rp] = p1x;
  5961. tmpData[rp + 1] = p1y;
  5962. tmpData[rp + 2] = dx / len;
  5963. tmpData[rp + 3] = dy / len;
  5964. realPtNum++;
  5965. }
  5966. ci = 0;
  5967. for (i = 0; i < pointnum; i++) {
  5968. p1x = points[ci], p1y = points[ci + 1];
  5969. p2x = points[ci + 2], p2y = points[ci + 3];
  5970. }
  5971. }
  5972. }
  5973. BasePoly.tempData = new Array(256);
  5974. class EarcutNode {
  5975. constructor(i, x, y) {
  5976. this.i = i;
  5977. this.x = x;
  5978. this.y = y;
  5979. this.prev = null;
  5980. this.next = null;
  5981. this.z = null;
  5982. this.prevZ = null;
  5983. this.nextZ = null;
  5984. this.steiner = false;
  5985. }
  5986. }
  5987. class Earcut {
  5988. static earcut(data, holeIndices, dim) {
  5989. dim = dim || 2;
  5990. var hasHoles = holeIndices && holeIndices.length, outerLen = hasHoles ? holeIndices[0] * dim : data.length, outerNode = Earcut.linkedList(data, 0, outerLen, dim, true), triangles = [];
  5991. if (!outerNode)
  5992. return triangles;
  5993. var minX, minY, maxX, maxY, x, y, invSize;
  5994. if (hasHoles)
  5995. outerNode = Earcut.eliminateHoles(data, holeIndices, outerNode, dim);
  5996. if (data.length > 80 * dim) {
  5997. minX = maxX = data[0];
  5998. minY = maxY = data[1];
  5999. for (var i = dim; i < outerLen; i += dim) {
  6000. x = data[i];
  6001. y = data[i + 1];
  6002. if (x < minX)
  6003. minX = x;
  6004. if (y < minY)
  6005. minY = y;
  6006. if (x > maxX)
  6007. maxX = x;
  6008. if (y > maxY)
  6009. maxY = y;
  6010. }
  6011. invSize = Math.max(maxX - minX, maxY - minY);
  6012. invSize = invSize !== 0 ? 1 / invSize : 0;
  6013. }
  6014. Earcut.earcutLinked(outerNode, triangles, dim, minX, minY, invSize);
  6015. return triangles;
  6016. }
  6017. static linkedList(data, start, end, dim, clockwise) {
  6018. var i, last;
  6019. if (clockwise === (Earcut.signedArea(data, start, end, dim) > 0)) {
  6020. for (i = start; i < end; i += dim)
  6021. last = Earcut.insertNode(i, data[i], data[i + 1], last);
  6022. }
  6023. else {
  6024. for (i = end - dim; i >= start; i -= dim)
  6025. last = Earcut.insertNode(i, data[i], data[i + 1], last);
  6026. }
  6027. if (last && Earcut.equals(last, last.next)) {
  6028. Earcut.removeNode(last);
  6029. last = last.next;
  6030. }
  6031. return last;
  6032. }
  6033. static filterPoints(start, end) {
  6034. if (!start)
  6035. return start;
  6036. if (!end)
  6037. end = start;
  6038. var p = start, again;
  6039. do {
  6040. again = false;
  6041. if (!p.steiner && (Earcut.equals(p, p.next) || Earcut.area(p.prev, p, p.next) === 0)) {
  6042. Earcut.removeNode(p);
  6043. p = end = p.prev;
  6044. if (p === p.next)
  6045. break;
  6046. again = true;
  6047. }
  6048. else {
  6049. p = p.next;
  6050. }
  6051. } while (again || p !== end);
  6052. return end;
  6053. }
  6054. static earcutLinked(ear, triangles, dim, minX, minY, invSize, pass = null) {
  6055. if (!ear)
  6056. return;
  6057. if (!pass && invSize)
  6058. Earcut.indexCurve(ear, minX, minY, invSize);
  6059. var stop = ear, prev, next;
  6060. while (ear.prev !== ear.next) {
  6061. prev = ear.prev;
  6062. next = ear.next;
  6063. if (invSize ? Earcut.isEarHashed(ear, minX, minY, invSize) : Earcut.isEar(ear)) {
  6064. triangles.push(prev.i / dim);
  6065. triangles.push(ear.i / dim);
  6066. triangles.push(next.i / dim);
  6067. Earcut.removeNode(ear);
  6068. ear = next.next;
  6069. stop = next.next;
  6070. continue;
  6071. }
  6072. ear = next;
  6073. if (ear === stop) {
  6074. if (!pass) {
  6075. Earcut.earcutLinked(Earcut.filterPoints(ear, null), triangles, dim, minX, minY, invSize, 1);
  6076. }
  6077. else if (pass === 1) {
  6078. ear = Earcut.cureLocalIntersections(ear, triangles, dim);
  6079. Earcut.earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
  6080. }
  6081. else if (pass === 2) {
  6082. Earcut.splitEarcut(ear, triangles, dim, minX, minY, invSize);
  6083. }
  6084. break;
  6085. }
  6086. }
  6087. }
  6088. static isEar(ear) {
  6089. var a = ear.prev, b = ear, c = ear.next;
  6090. if (Earcut.area(a, b, c) >= 0)
  6091. return false;
  6092. var p = ear.next.next;
  6093. while (p !== ear.prev) {
  6094. if (Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  6095. Earcut.area(p.prev, p, p.next) >= 0)
  6096. return false;
  6097. p = p.next;
  6098. }
  6099. return true;
  6100. }
  6101. static isEarHashed(ear, minX, minY, invSize) {
  6102. var a = ear.prev, b = ear, c = ear.next;
  6103. if (Earcut.area(a, b, c) >= 0)
  6104. return false;
  6105. var minTX = a.x < b.x ? (a.x < c.x ? a.x : c.x) : (b.x < c.x ? b.x : c.x), minTY = a.y < b.y ? (a.y < c.y ? a.y : c.y) : (b.y < c.y ? b.y : c.y), maxTX = a.x > b.x ? (a.x > c.x ? a.x : c.x) : (b.x > c.x ? b.x : c.x), maxTY = a.y > b.y ? (a.y > c.y ? a.y : c.y) : (b.y > c.y ? b.y : c.y);
  6106. var minZ = Earcut.zOrder(minTX, minTY, minX, minY, invSize), maxZ = Earcut.zOrder(maxTX, maxTY, minX, minY, invSize);
  6107. var p = ear.nextZ;
  6108. while (p && p.z <= maxZ) {
  6109. if (p !== ear.prev && p !== ear.next &&
  6110. Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  6111. Earcut.area(p.prev, p, p.next) >= 0)
  6112. return false;
  6113. p = p.nextZ;
  6114. }
  6115. p = ear.prevZ;
  6116. while (p && p.z >= minZ) {
  6117. if (p !== ear.prev && p !== ear.next &&
  6118. Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  6119. Earcut.area(p.prev, p, p.next) >= 0)
  6120. return false;
  6121. p = p.prevZ;
  6122. }
  6123. return true;
  6124. }
  6125. static cureLocalIntersections(start, triangles, dim) {
  6126. var p = start;
  6127. do {
  6128. var a = p.prev, b = p.next.next;
  6129. if (!Earcut.equals(a, b) && Earcut.intersects(a, p, p.next, b) && Earcut.locallyInside(a, b) && Earcut.locallyInside(b, a)) {
  6130. triangles.push(a.i / dim);
  6131. triangles.push(p.i / dim);
  6132. triangles.push(b.i / dim);
  6133. Earcut.removeNode(p);
  6134. Earcut.removeNode(p.next);
  6135. p = start = b;
  6136. }
  6137. p = p.next;
  6138. } while (p !== start);
  6139. return p;
  6140. }
  6141. static splitEarcut(start, triangles, dim, minX, minY, invSize) {
  6142. var a = start;
  6143. do {
  6144. var b = a.next.next;
  6145. while (b !== a.prev) {
  6146. if (a.i !== b.i && Earcut.isValidDiagonal(a, b)) {
  6147. var c = Earcut.splitPolygon(a, b);
  6148. a = Earcut.filterPoints(a, a.next);
  6149. c = Earcut.filterPoints(c, c.next);
  6150. Earcut.earcutLinked(a, triangles, dim, minX, minY, invSize);
  6151. Earcut.earcutLinked(c, triangles, dim, minX, minY, invSize);
  6152. return;
  6153. }
  6154. b = b.next;
  6155. }
  6156. a = a.next;
  6157. } while (a !== start);
  6158. }
  6159. static eliminateHoles(data, holeIndices, outerNode, dim) {
  6160. var queue = [], i, len, start, end, list;
  6161. for (i = 0, len = holeIndices.length; i < len; i++) {
  6162. start = holeIndices[i] * dim;
  6163. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  6164. list = Earcut.linkedList(data, start, end, dim, false);
  6165. if (list === list.next)
  6166. list.steiner = true;
  6167. queue.push(Earcut.getLeftmost(list));
  6168. }
  6169. queue.sort(Earcut.compareX);
  6170. for (i = 0; i < queue.length; i++) {
  6171. Earcut.eliminateHole(queue[i], outerNode);
  6172. outerNode = Earcut.filterPoints(outerNode, outerNode.next);
  6173. }
  6174. return outerNode;
  6175. }
  6176. static compareX(a, b) {
  6177. return a.x - b.x;
  6178. }
  6179. static eliminateHole(hole, outerNode) {
  6180. outerNode = Earcut.findHoleBridge(hole, outerNode);
  6181. if (outerNode) {
  6182. var b = Earcut.splitPolygon(outerNode, hole);
  6183. Earcut.filterPoints(b, b.next);
  6184. }
  6185. }
  6186. static findHoleBridge(hole, outerNode) {
  6187. var p = outerNode, hx = hole.x, hy = hole.y, qx = -Infinity, m;
  6188. do {
  6189. if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
  6190. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  6191. if (x <= hx && x > qx) {
  6192. qx = x;
  6193. if (x === hx) {
  6194. if (hy === p.y)
  6195. return p;
  6196. if (hy === p.next.y)
  6197. return p.next;
  6198. }
  6199. m = p.x < p.next.x ? p : p.next;
  6200. }
  6201. }
  6202. p = p.next;
  6203. } while (p !== outerNode);
  6204. if (!m)
  6205. return null;
  6206. if (hx === qx)
  6207. return m.prev;
  6208. var stop = m, mx = m.x, my = m.y, tanMin = Infinity, tan;
  6209. p = m.next;
  6210. while (p !== stop) {
  6211. if (hx >= p.x && p.x >= mx && hx !== p.x &&
  6212. Earcut.pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  6213. tan = Math.abs(hy - p.y) / (hx - p.x);
  6214. if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && Earcut.locallyInside(p, hole)) {
  6215. m = p;
  6216. tanMin = tan;
  6217. }
  6218. }
  6219. p = p.next;
  6220. }
  6221. return m;
  6222. }
  6223. static indexCurve(start, minX, minY, invSize) {
  6224. var p = start;
  6225. do {
  6226. if (p.z === null)
  6227. p.z = Earcut.zOrder(p.x, p.y, minX, minY, invSize);
  6228. p.prevZ = p.prev;
  6229. p.nextZ = p.next;
  6230. p = p.next;
  6231. } while (p !== start);
  6232. p.prevZ.nextZ = null;
  6233. p.prevZ = null;
  6234. Earcut.sortLinked(p);
  6235. }
  6236. static sortLinked(list) {
  6237. var i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
  6238. do {
  6239. p = list;
  6240. list = null;
  6241. tail = null;
  6242. numMerges = 0;
  6243. while (p) {
  6244. numMerges++;
  6245. q = p;
  6246. pSize = 0;
  6247. for (i = 0; i < inSize; i++) {
  6248. pSize++;
  6249. q = q.nextZ;
  6250. if (!q)
  6251. break;
  6252. }
  6253. qSize = inSize;
  6254. while (pSize > 0 || (qSize > 0 && q)) {
  6255. if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
  6256. e = p;
  6257. p = p.nextZ;
  6258. pSize--;
  6259. }
  6260. else {
  6261. e = q;
  6262. q = q.nextZ;
  6263. qSize--;
  6264. }
  6265. if (tail)
  6266. tail.nextZ = e;
  6267. else
  6268. list = e;
  6269. e.prevZ = tail;
  6270. tail = e;
  6271. }
  6272. p = q;
  6273. }
  6274. tail.nextZ = null;
  6275. inSize *= 2;
  6276. } while (numMerges > 1);
  6277. return list;
  6278. }
  6279. static zOrder(x, y, minX, minY, invSize) {
  6280. x = 32767 * (x - minX) * invSize;
  6281. y = 32767 * (y - minY) * invSize;
  6282. x = (x | (x << 8)) & 0x00FF00FF;
  6283. x = (x | (x << 4)) & 0x0F0F0F0F;
  6284. x = (x | (x << 2)) & 0x33333333;
  6285. x = (x | (x << 1)) & 0x55555555;
  6286. y = (y | (y << 8)) & 0x00FF00FF;
  6287. y = (y | (y << 4)) & 0x0F0F0F0F;
  6288. y = (y | (y << 2)) & 0x33333333;
  6289. y = (y | (y << 1)) & 0x55555555;
  6290. return x | (y << 1);
  6291. }
  6292. static getLeftmost(start) {
  6293. var p = start, leftmost = start;
  6294. do {
  6295. if (p.x < leftmost.x)
  6296. leftmost = p;
  6297. p = p.next;
  6298. } while (p !== start);
  6299. return leftmost;
  6300. }
  6301. static pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  6302. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  6303. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  6304. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  6305. }
  6306. static isValidDiagonal(a, b) {
  6307. return a.next.i !== b.i && a.prev.i !== b.i && !Earcut.intersectsPolygon(a, b) &&
  6308. Earcut.locallyInside(a, b) && Earcut.locallyInside(b, a) && Earcut.middleInside(a, b);
  6309. }
  6310. static area(p, q, r) {
  6311. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  6312. }
  6313. static equals(p1, p2) {
  6314. return p1.x === p2.x && p1.y === p2.y;
  6315. }
  6316. static intersects(p1, q1, p2, q2) {
  6317. if ((Earcut.equals(p1, q1) && Earcut.equals(p2, q2)) ||
  6318. (Earcut.equals(p1, q2) && Earcut.equals(p2, q1)))
  6319. return true;
  6320. return Earcut.area(p1, q1, p2) > 0 !== Earcut.area(p1, q1, q2) > 0 &&
  6321. Earcut.area(p2, q2, p1) > 0 !== Earcut.area(p2, q2, q1) > 0;
  6322. }
  6323. static intersectsPolygon(a, b) {
  6324. var p = a;
  6325. do {
  6326. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  6327. Earcut.intersects(p, p.next, a, b))
  6328. return true;
  6329. p = p.next;
  6330. } while (p !== a);
  6331. return false;
  6332. }
  6333. static locallyInside(a, b) {
  6334. return Earcut.area(a.prev, a, a.next) < 0 ?
  6335. Earcut.area(a, b, a.next) >= 0 && Earcut.area(a, a.prev, b) >= 0 :
  6336. Earcut.area(a, b, a.prev) < 0 || Earcut.area(a, a.next, b) < 0;
  6337. }
  6338. static middleInside(a, b) {
  6339. var p = a, inside = false, px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
  6340. do {
  6341. if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
  6342. (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  6343. inside = !inside;
  6344. p = p.next;
  6345. } while (p !== a);
  6346. return inside;
  6347. }
  6348. static splitPolygon(a, b) {
  6349. var a2 = new EarcutNode(a.i, a.x, a.y), b2 = new EarcutNode(b.i, b.x, b.y), an = a.next, bp = b.prev;
  6350. a.next = b;
  6351. b.prev = a;
  6352. a2.next = an;
  6353. an.prev = a2;
  6354. b2.next = a2;
  6355. a2.prev = b2;
  6356. bp.next = b2;
  6357. b2.prev = bp;
  6358. return b2;
  6359. }
  6360. static insertNode(i, x, y, last) {
  6361. var p = new EarcutNode(i, x, y);
  6362. if (!last) {
  6363. p.prev = p;
  6364. p.next = p;
  6365. }
  6366. else {
  6367. p.next = last.next;
  6368. p.prev = last;
  6369. last.next.prev = p;
  6370. last.next = p;
  6371. }
  6372. return p;
  6373. }
  6374. static removeNode(p) {
  6375. p.next.prev = p.prev;
  6376. p.prev.next = p.next;
  6377. if (p.prevZ)
  6378. p.prevZ.nextZ = p.nextZ;
  6379. if (p.nextZ)
  6380. p.nextZ.prevZ = p.prevZ;
  6381. }
  6382. static signedArea(data, start, end, dim) {
  6383. var sum = 0;
  6384. for (var i = start, j = end - dim; i < end; i += dim) {
  6385. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  6386. j = i;
  6387. }
  6388. return sum;
  6389. }
  6390. }
  6391. class CONST3D2D {
  6392. }
  6393. CONST3D2D.BYTES_PE = 4;
  6394. CONST3D2D.BYTES_PIDX = 2;
  6395. CONST3D2D.defaultMatrix4 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  6396. CONST3D2D.defaultMinusYMatrix4 = [1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  6397. CONST3D2D.uniformMatrix3 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
  6398. CONST3D2D._TMPARRAY = [];
  6399. CONST3D2D._OFFSETX = 0;
  6400. CONST3D2D._OFFSETY = 0;
  6401. class Submit extends SubmitBase {
  6402. constructor(renderType = SubmitBase.TYPE_2D) {
  6403. super(renderType);
  6404. }
  6405. renderSubmit() {
  6406. if (this._numEle === 0 || !this._mesh || this._numEle == 0)
  6407. return 1;
  6408. var _tex = this.shaderValue.textureHost;
  6409. if (_tex) {
  6410. var source = _tex._getSource();
  6411. if (!source)
  6412. return 1;
  6413. this.shaderValue.texture = source;
  6414. }
  6415. var gl = WebGLContext.mainContext;
  6416. this._mesh.useMesh(gl);
  6417. this.shaderValue.upload();
  6418. if (BlendMode.activeBlendFunction !== this._blendFn) {
  6419. WebGLContext.setBlend(gl, true);
  6420. this._blendFn(gl);
  6421. BlendMode.activeBlendFunction = this._blendFn;
  6422. }
  6423. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  6424. Stat.renderBatches++;
  6425. Stat.trianglesFaces += this._numEle / 3;
  6426. return 1;
  6427. }
  6428. releaseRender() {
  6429. if (SubmitBase.RENDERBASE == this)
  6430. return;
  6431. if ((--this._ref) < 1) {
  6432. Submit.POOL[Submit._poolSize++] = this;
  6433. this.shaderValue.release();
  6434. this.shaderValue = null;
  6435. this._mesh = null;
  6436. this._parent && (this._parent.releaseRender(), this._parent = null);
  6437. }
  6438. }
  6439. static create(context, mesh, sv) {
  6440. var o = Submit._poolSize ? Submit.POOL[--Submit._poolSize] : new Submit();
  6441. o._ref = 1;
  6442. o._mesh = mesh;
  6443. o._key.clear();
  6444. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  6445. o._numEle = 0;
  6446. var blendType = context._nBlendType;
  6447. o._blendFn = context._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  6448. o.shaderValue = sv;
  6449. o.shaderValue.setValue(context._shader2D);
  6450. var filters = context._shader2D.filters;
  6451. filters && o.shaderValue.setFilters(filters);
  6452. return o;
  6453. }
  6454. static createShape(ctx, mesh, numEle, sv) {
  6455. var o = Submit._poolSize ? Submit.POOL[--Submit._poolSize] : (new Submit());
  6456. o._mesh = mesh;
  6457. o._numEle = numEle;
  6458. o._startIdx = mesh.indexNum * 2;
  6459. o._ref = 1;
  6460. o.shaderValue = sv;
  6461. o.shaderValue.setValue(ctx._shader2D);
  6462. var blendType = ctx._nBlendType;
  6463. o._key.blendShader = blendType;
  6464. o._blendFn = ctx._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  6465. return o;
  6466. }
  6467. }
  6468. Submit._poolSize = 0;
  6469. Submit.POOL = [];
  6470. class SubmitCanvas extends SubmitBase {
  6471. constructor() {
  6472. super(SubmitBase.TYPE_2D);
  6473. this._matrix = new Matrix();
  6474. this._matrix4 = CONST3D2D.defaultMatrix4.concat();
  6475. this.shaderValue = new Value2D(0, 0);
  6476. }
  6477. static create(canvas, alpha, filters) {
  6478. var o = (!SubmitCanvas.POOL._length) ? (new SubmitCanvas()) : SubmitCanvas.POOL[--SubmitCanvas.POOL._length];
  6479. o.canv = canvas;
  6480. o._ref = 1;
  6481. o._numEle = 0;
  6482. var v = o.shaderValue;
  6483. v.alpha = alpha;
  6484. v.defines.setValue(0);
  6485. filters && filters.length && v.setFilters(filters);
  6486. return o;
  6487. }
  6488. renderSubmit() {
  6489. var preAlpha = RenderState2D.worldAlpha;
  6490. var preMatrix4 = RenderState2D.worldMatrix4;
  6491. var preMatrix = RenderState2D.worldMatrix;
  6492. var preFilters = RenderState2D.worldFilters;
  6493. var preWorldShaderDefines = RenderState2D.worldShaderDefines;
  6494. var v = this.shaderValue;
  6495. var m = this._matrix;
  6496. var m4 = this._matrix4;
  6497. var mout = Matrix.TEMP;
  6498. Matrix.mul(m, preMatrix, mout);
  6499. m4[0] = mout.a;
  6500. m4[1] = mout.b;
  6501. m4[4] = mout.c;
  6502. m4[5] = mout.d;
  6503. m4[12] = mout.tx;
  6504. m4[13] = mout.ty;
  6505. RenderState2D.worldMatrix = mout.clone();
  6506. RenderState2D.worldMatrix4 = m4;
  6507. RenderState2D.worldAlpha = RenderState2D.worldAlpha * v.alpha;
  6508. if (v.filters && v.filters.length) {
  6509. RenderState2D.worldFilters = v.filters;
  6510. RenderState2D.worldShaderDefines = v.defines;
  6511. }
  6512. this.canv['flushsubmit']();
  6513. RenderState2D.worldAlpha = preAlpha;
  6514. RenderState2D.worldMatrix4 = preMatrix4;
  6515. RenderState2D.worldMatrix.destroy();
  6516. RenderState2D.worldMatrix = preMatrix;
  6517. RenderState2D.worldFilters = preFilters;
  6518. RenderState2D.worldShaderDefines = preWorldShaderDefines;
  6519. return 1;
  6520. }
  6521. releaseRender() {
  6522. if ((--this._ref) < 1) {
  6523. var cache = SubmitCanvas.POOL;
  6524. this._mesh = null;
  6525. cache[cache._length++] = this;
  6526. }
  6527. }
  6528. getRenderType() {
  6529. return SubmitBase.TYPE_CANVAS;
  6530. }
  6531. ;
  6532. }
  6533. SubmitCanvas.POOL = [];
  6534. {
  6535. SubmitCanvas.POOL._length = 0;
  6536. }
  6537. class SubmitTarget {
  6538. constructor() {
  6539. this.blendType = 0;
  6540. this._ref = 1;
  6541. this._key = new SubmitKey();
  6542. }
  6543. renderSubmit() {
  6544. var gl = WebGLContext.mainContext;
  6545. this._mesh.useMesh(gl);
  6546. var target = this.srcRT;
  6547. if (target) {
  6548. this.shaderValue.texture = target._getSource();
  6549. this.shaderValue.upload();
  6550. this.blend();
  6551. Stat.renderBatches++;
  6552. Stat.trianglesFaces += this._numEle / 3;
  6553. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  6554. }
  6555. return 1;
  6556. }
  6557. blend() {
  6558. if (BlendMode.activeBlendFunction !== BlendMode.fns[this.blendType]) {
  6559. var gl = WebGLContext.mainContext;
  6560. gl.enable(gl.BLEND);
  6561. BlendMode.fns[this.blendType](gl);
  6562. BlendMode.activeBlendFunction = BlendMode.fns[this.blendType];
  6563. }
  6564. }
  6565. getRenderType() {
  6566. return 0;
  6567. }
  6568. releaseRender() {
  6569. if ((--this._ref) < 1) {
  6570. var pool = SubmitTarget.POOL;
  6571. pool[pool._length++] = this;
  6572. }
  6573. }
  6574. static create(context, mesh, sv, rt) {
  6575. var o = SubmitTarget.POOL._length ? SubmitTarget.POOL[--SubmitTarget.POOL._length] : new SubmitTarget();
  6576. o._mesh = mesh;
  6577. o.srcRT = rt;
  6578. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  6579. o._ref = 1;
  6580. o._key.clear();
  6581. o._numEle = 0;
  6582. o.blendType = context._nBlendType;
  6583. o._key.blendShader = o.blendType;
  6584. o.shaderValue = sv;
  6585. o.shaderValue.setValue(context._shader2D);
  6586. if (context._colorFiler) {
  6587. var ft = context._colorFiler;
  6588. sv.defines.add(ft.type);
  6589. sv.colorMat = ft._mat;
  6590. sv.colorAlpha = ft._alpha;
  6591. }
  6592. return o;
  6593. }
  6594. }
  6595. SubmitTarget.POOL = [];
  6596. {
  6597. SubmitTarget.POOL._length = 0;
  6598. }
  6599. class SubmitTexture extends SubmitBase {
  6600. constructor(renderType = SubmitBase.TYPE_2D) {
  6601. super(renderType);
  6602. }
  6603. releaseRender() {
  6604. if ((--this._ref) < 1) {
  6605. SubmitTexture.POOL[SubmitTexture._poolSize++] = this;
  6606. this.shaderValue.release();
  6607. this._mesh = null;
  6608. this._parent && (this._parent.releaseRender(), this._parent = null);
  6609. }
  6610. }
  6611. renderSubmit() {
  6612. if (this._numEle === 0)
  6613. return 1;
  6614. var tex = this.shaderValue.textureHost;
  6615. if (tex) {
  6616. var source = tex ? tex._getSource() : null;
  6617. if (!source)
  6618. return 1;
  6619. }
  6620. var gl = WebGLContext.mainContext;
  6621. this._mesh.useMesh(gl);
  6622. var lastSubmit = SubmitBase.preRender;
  6623. var prekey = SubmitBase.preRender._key;
  6624. if (this._key.blendShader === 0 && (this._key.submitType === prekey.submitType && this._key.blendShader === prekey.blendShader) && BaseShader.activeShader &&
  6625. SubmitBase.preRender.clipInfoID == this.clipInfoID &&
  6626. lastSubmit.shaderValue.defines._value === this.shaderValue.defines._value &&
  6627. (this.shaderValue.defines._value & ShaderDefines2D.NOOPTMASK) == 0) {
  6628. BaseShader.activeShader.uploadTexture2D(source);
  6629. }
  6630. else {
  6631. if (BlendMode.activeBlendFunction !== this._blendFn) {
  6632. WebGLContext.setBlend(gl, true);
  6633. this._blendFn(gl);
  6634. BlendMode.activeBlendFunction = this._blendFn;
  6635. }
  6636. this.shaderValue.texture = source;
  6637. this.shaderValue.upload();
  6638. }
  6639. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  6640. Stat.renderBatches++;
  6641. Stat.trianglesFaces += this._numEle / 3;
  6642. return 1;
  6643. }
  6644. static create(context, mesh, sv) {
  6645. var o = SubmitTexture._poolSize ? SubmitTexture.POOL[--SubmitTexture._poolSize] : new SubmitTexture(SubmitBase.TYPE_TEXTURE);
  6646. o._mesh = mesh;
  6647. o._key.clear();
  6648. o._key.submitType = SubmitBase.KEY_DRAWTEXTURE;
  6649. o._ref = 1;
  6650. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  6651. o._numEle = 0;
  6652. var blendType = context._nBlendType;
  6653. o._key.blendShader = blendType;
  6654. o._blendFn = context._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  6655. o.shaderValue = sv;
  6656. if (context._colorFiler) {
  6657. var ft = context._colorFiler;
  6658. sv.defines.add(ft.type);
  6659. sv.colorMat = ft._mat;
  6660. sv.colorAlpha = ft._alpha;
  6661. }
  6662. return o;
  6663. }
  6664. }
  6665. SubmitTexture._poolSize = 0;
  6666. SubmitTexture.POOL = [];
  6667. class CharSubmitCache {
  6668. constructor() {
  6669. this._data = [];
  6670. this._ndata = 0;
  6671. this._clipid = -1;
  6672. this._clipMatrix = new Matrix();
  6673. this._enable = false;
  6674. }
  6675. clear() {
  6676. this._tex = null;
  6677. this._imgId = -1;
  6678. this._ndata = 0;
  6679. this._enable = false;
  6680. this._colorFiler = null;
  6681. }
  6682. destroy() {
  6683. this.clear();
  6684. this._data.length = 0;
  6685. this._data = null;
  6686. }
  6687. add(ctx, tex, imgid, pos, uv, color) {
  6688. if (this._ndata > 0 && (this._tex != tex || this._imgId != imgid ||
  6689. (this._clipid >= 0 && this._clipid != ctx._clipInfoID))) {
  6690. this.submit(ctx);
  6691. }
  6692. this._clipid = ctx._clipInfoID;
  6693. ctx._globalClipMatrix.copyTo(this._clipMatrix);
  6694. this._tex = tex;
  6695. this._imgId = imgid;
  6696. this._colorFiler = ctx._colorFiler;
  6697. this._data[this._ndata] = pos;
  6698. this._data[this._ndata + 1] = uv;
  6699. this._data[this._ndata + 2] = color;
  6700. this._ndata += 3;
  6701. }
  6702. getPos() {
  6703. if (CharSubmitCache.__nPosPool == 0)
  6704. return new Array(8);
  6705. return CharSubmitCache.__posPool[--CharSubmitCache.__nPosPool];
  6706. }
  6707. enable(value, ctx) {
  6708. if (value === this._enable)
  6709. return;
  6710. this._enable = value;
  6711. this._enable || this.submit(ctx);
  6712. }
  6713. submit(ctx) {
  6714. var n = this._ndata;
  6715. if (!n)
  6716. return;
  6717. var _mesh = ctx._mesh;
  6718. var colorFiler = ctx._colorFiler;
  6719. ctx._colorFiler = this._colorFiler;
  6720. var submit = SubmitTexture.create(ctx, _mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  6721. ctx._submits[ctx._submits._length++] = ctx._curSubmit = submit;
  6722. submit.shaderValue.textureHost = this._tex;
  6723. submit._key.other = this._imgId;
  6724. ctx._colorFiler = colorFiler;
  6725. ctx._copyClipInfo(submit, this._clipMatrix);
  6726. submit.clipInfoID = this._clipid;
  6727. for (var i = 0; i < n; i += 3) {
  6728. _mesh.addQuad(this._data[i], this._data[i + 1], this._data[i + 2], true);
  6729. CharSubmitCache.__posPool[CharSubmitCache.__nPosPool++] = this._data[i];
  6730. }
  6731. n /= 3;
  6732. submit._numEle += n * 6;
  6733. _mesh.indexNum += n * 6;
  6734. _mesh.vertNum += n * 4;
  6735. ctx._drawCount += n;
  6736. this._ndata = 0;
  6737. if (RenderInfo.loopCount % 100 == 0)
  6738. this._data.length = 0;
  6739. }
  6740. }
  6741. CharSubmitCache.__posPool = [];
  6742. CharSubmitCache.__nPosPool = 0;
  6743. class AtlasGrid {
  6744. constructor(width = 0, height = 0, id = 0) {
  6745. this.atlasID = 0;
  6746. this._width = 0;
  6747. this._height = 0;
  6748. this._texCount = 0;
  6749. this._rowInfo = null;
  6750. this._cells = null;
  6751. this._used = 0;
  6752. this._cells = null;
  6753. this._rowInfo = null;
  6754. this.atlasID = id;
  6755. this._init(width, height);
  6756. }
  6757. addRect(type, width, height, pt) {
  6758. if (!this._get(width, height, pt))
  6759. return false;
  6760. this._fill(pt.x, pt.y, width, height, type);
  6761. this._texCount++;
  6762. return true;
  6763. }
  6764. _release() {
  6765. this._cells = null;
  6766. this._rowInfo = null;
  6767. }
  6768. _init(width, height) {
  6769. this._width = width;
  6770. this._height = height;
  6771. this._release();
  6772. if (this._width == 0)
  6773. return false;
  6774. this._cells = new Uint8Array(this._width * this._height * 3);
  6775. this._rowInfo = new Uint8Array(this._height);
  6776. this._used = 0;
  6777. this._clear();
  6778. return true;
  6779. }
  6780. _get(width, height, pt) {
  6781. if (width > this._width || height > this._height) {
  6782. return false;
  6783. }
  6784. var rx = -1;
  6785. var ry = -1;
  6786. var nWidth = this._width;
  6787. var nHeight = this._height;
  6788. var pCellBox = this._cells;
  6789. for (var y = 0; y < nHeight; y++) {
  6790. if (this._rowInfo[y] < width)
  6791. continue;
  6792. for (var x = 0; x < nWidth;) {
  6793. var tm = (y * nWidth + x) * 3;
  6794. if (pCellBox[tm] != 0 || pCellBox[tm + 1] < width || pCellBox[tm + 2] < height) {
  6795. x += pCellBox[tm + 1];
  6796. continue;
  6797. }
  6798. rx = x;
  6799. ry = y;
  6800. for (var xx = 0; xx < width; xx++) {
  6801. if (pCellBox[3 * xx + tm + 2] < height) {
  6802. rx = -1;
  6803. break;
  6804. }
  6805. }
  6806. if (rx < 0) {
  6807. x += pCellBox[tm + 1];
  6808. continue;
  6809. }
  6810. pt.x = rx;
  6811. pt.y = ry;
  6812. return true;
  6813. }
  6814. }
  6815. return false;
  6816. }
  6817. _fill(x, y, w, h, type) {
  6818. var nWidth = this._width;
  6819. var nHeghit = this._height;
  6820. this._check((x + w) <= nWidth && (y + h) <= nHeghit);
  6821. for (var yy = y; yy < (h + y); ++yy) {
  6822. this._check(this._rowInfo[yy] >= w);
  6823. this._rowInfo[yy] -= w;
  6824. for (var xx = 0; xx < w; xx++) {
  6825. var tm = (x + yy * nWidth + xx) * 3;
  6826. this._check(this._cells[tm] == 0);
  6827. this._cells[tm] = type;
  6828. this._cells[tm + 1] = w;
  6829. this._cells[tm + 2] = h;
  6830. }
  6831. }
  6832. if (x > 0) {
  6833. for (yy = 0; yy < h; ++yy) {
  6834. var s = 0;
  6835. for (xx = x - 1; xx >= 0; --xx, ++s) {
  6836. if (this._cells[((y + yy) * nWidth + xx) * 3] != 0)
  6837. break;
  6838. }
  6839. for (xx = s; xx > 0; --xx) {
  6840. this._cells[((y + yy) * nWidth + x - xx) * 3 + 1] = xx;
  6841. this._check(xx > 0);
  6842. }
  6843. }
  6844. }
  6845. if (y > 0) {
  6846. for (xx = x; xx < (x + w); ++xx) {
  6847. s = 0;
  6848. for (yy = y - 1; yy >= 0; --yy, s++) {
  6849. if (this._cells[(xx + yy * nWidth) * 3] != 0)
  6850. break;
  6851. }
  6852. for (yy = s; yy > 0; --yy) {
  6853. this._cells[(xx + (y - yy) * nWidth) * 3 + 2] = yy;
  6854. this._check(yy > 0);
  6855. }
  6856. }
  6857. }
  6858. this._used += (w * h) / (this._width * this._height);
  6859. }
  6860. _check(ret) {
  6861. if (ret == false) {
  6862. console.log("xtexMerger 错误啦");
  6863. }
  6864. }
  6865. _clear() {
  6866. this._texCount = 0;
  6867. for (var y = 0; y < this._height; y++) {
  6868. this._rowInfo[y] = this._width;
  6869. }
  6870. for (var i = 0; i < this._height; i++) {
  6871. for (var j = 0; j < this._width; j++) {
  6872. var tm = (i * this._width + j) * 3;
  6873. this._cells[tm] = 0;
  6874. this._cells[tm + 1] = this._width - j;
  6875. this._cells[tm + 2] = this._width - i;
  6876. }
  6877. }
  6878. }
  6879. }
  6880. class TextTexture extends Resource {
  6881. constructor(textureW, textureH) {
  6882. super();
  6883. this._texW = 0;
  6884. this._texH = 0;
  6885. this.__destroyed = false;
  6886. this._discardTm = 0;
  6887. this.genID = 0;
  6888. this.bitmap = { id: 0, _glTexture: null };
  6889. this.curUsedCovRate = 0;
  6890. this.curUsedCovRateAtlas = 0;
  6891. this.lastTouchTm = 0;
  6892. this.ri = null;
  6893. this._texW = textureW || TextTexture.gTextRender.atlasWidth;
  6894. this._texH = textureH || TextTexture.gTextRender.atlasWidth;
  6895. this.bitmap.id = this.id;
  6896. this.lock = true;
  6897. }
  6898. recreateResource() {
  6899. if (this._source)
  6900. return;
  6901. var gl = LayaGL.instance;
  6902. var glTex = this._source = gl.createTexture();
  6903. this.bitmap._glTexture = glTex;
  6904. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, glTex);
  6905. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this._texW, this._texH, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  6906. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  6907. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  6908. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  6909. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  6910. if (TextTexture.gTextRender.debugUV) {
  6911. this.fillWhite();
  6912. }
  6913. }
  6914. addChar(data, x, y, uv = null) {
  6915. if (TextTexture.gTextRender.isWan1Wan) {
  6916. return this.addCharCanvas(data, x, y, uv);
  6917. }
  6918. !this._source && this.recreateResource();
  6919. var gl = LayaGL.instance;
  6920. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, this._source);
  6921. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  6922. var dt = data.data;
  6923. if (data.data instanceof Uint8ClampedArray)
  6924. dt = new Uint8Array(dt.buffer);
  6925. gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, data.width, data.height, gl.RGBA, gl.UNSIGNED_BYTE, dt);
  6926. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  6927. var u0;
  6928. var v0;
  6929. var u1;
  6930. var v1;
  6931. u0 = x / this._texW;
  6932. v0 = y / this._texH;
  6933. u1 = (x + data.width) / this._texW;
  6934. v1 = (y + data.height) / this._texH;
  6935. uv = uv || new Array(8);
  6936. uv[0] = u0, uv[1] = v0;
  6937. uv[2] = u1, uv[3] = v0;
  6938. uv[4] = u1, uv[5] = v1;
  6939. uv[6] = u0, uv[7] = v1;
  6940. return uv;
  6941. }
  6942. addCharCanvas(canv, x, y, uv = null) {
  6943. !this._source && this.recreateResource();
  6944. var gl = LayaGL.instance;
  6945. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, this._source);
  6946. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  6947. gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, gl.RGBA, gl.UNSIGNED_BYTE, canv);
  6948. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  6949. var u0;
  6950. var v0;
  6951. var u1;
  6952. var v1;
  6953. if (ILaya.Render.isConchApp) {
  6954. u0 = x / this._texW;
  6955. v0 = y / this._texH;
  6956. u1 = (x + canv.width) / this._texW;
  6957. v1 = (y + canv.height) / this._texH;
  6958. }
  6959. else {
  6960. u0 = (x + 1) / this._texW;
  6961. v0 = (y + 1) / this._texH;
  6962. u1 = (x + canv.width - 1) / this._texW;
  6963. v1 = (y + canv.height - 1) / this._texH;
  6964. }
  6965. uv = uv || new Array(8);
  6966. uv[0] = u0, uv[1] = v0;
  6967. uv[2] = u1, uv[3] = v0;
  6968. uv[4] = u1, uv[5] = v1;
  6969. uv[6] = u0, uv[7] = v1;
  6970. return uv;
  6971. }
  6972. fillWhite() {
  6973. !this._source && this.recreateResource();
  6974. var gl = LayaGL.instance;
  6975. var dt = new Uint8Array(this._texW * this._texH * 4);
  6976. dt.fill(0xff);
  6977. gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this._texW, this._texH, gl.RGBA, gl.UNSIGNED_BYTE, dt);
  6978. }
  6979. discard() {
  6980. ILaya.stage.setGlobalRepaint();
  6981. this.destroy();
  6982. return;
  6983. if (this._texW != TextTexture.gTextRender.atlasWidth || this._texH != TextTexture.gTextRender.atlasWidth) {
  6984. this.destroy();
  6985. return;
  6986. }
  6987. this.genID++;
  6988. if (TextTexture.poolLen >= TextTexture.pool.length) {
  6989. TextTexture.pool = TextTexture.pool.concat(new Array(10));
  6990. }
  6991. this._discardTm = RenderInfo.loopStTm;
  6992. TextTexture.pool[TextTexture.poolLen++] = this;
  6993. }
  6994. static getTextTexture(w, h) {
  6995. return new TextTexture(w, h);
  6996. if (w != TextTexture.gTextRender.atlasWidth || w != TextTexture.gTextRender.atlasWidth)
  6997. return new TextTexture(w, h);
  6998. if (TextTexture.poolLen > 0) {
  6999. var ret = TextTexture.pool[--TextTexture.poolLen];
  7000. if (TextTexture.poolLen > 0)
  7001. TextTexture.clean();
  7002. return ret;
  7003. }
  7004. return new TextTexture(w, h);
  7005. }
  7006. destroy() {
  7007. this.__destroyed = true;
  7008. var gl = LayaGL.instance;
  7009. this._source && gl.deleteTexture(this._source);
  7010. this._source = null;
  7011. }
  7012. static clean() {
  7013. var curtm = RenderInfo.loopStTm;
  7014. if (TextTexture.cleanTm === 0)
  7015. TextTexture.cleanTm = curtm;
  7016. if (curtm - TextTexture.cleanTm >= TextTexture.gTextRender.checkCleanTextureDt) {
  7017. for (var i = 0; i < TextTexture.poolLen; i++) {
  7018. var p = TextTexture.pool[i];
  7019. if (curtm - p._discardTm >= TextTexture.gTextRender.destroyUnusedTextureDt) {
  7020. p.destroy();
  7021. TextTexture.pool[i] = TextTexture.pool[TextTexture.poolLen - 1];
  7022. TextTexture.poolLen--;
  7023. i--;
  7024. }
  7025. }
  7026. TextTexture.cleanTm = curtm;
  7027. }
  7028. }
  7029. touchRect(ri, curloop) {
  7030. if (this.lastTouchTm != curloop) {
  7031. this.curUsedCovRate = 0;
  7032. this.curUsedCovRateAtlas = 0;
  7033. this.lastTouchTm = curloop;
  7034. }
  7035. var texw2 = TextTexture.gTextRender.atlasWidth * TextTexture.gTextRender.atlasWidth;
  7036. var gridw2 = ILaya.TextAtlas.atlasGridW * ILaya.TextAtlas.atlasGridW;
  7037. this.curUsedCovRate += (ri.bmpWidth * ri.bmpHeight) / texw2;
  7038. this.curUsedCovRateAtlas += (Math.ceil(ri.bmpWidth / ILaya.TextAtlas.atlasGridW) * Math.ceil(ri.bmpHeight / ILaya.TextAtlas.atlasGridW)) / (texw2 / gridw2);
  7039. }
  7040. get texture() {
  7041. return this;
  7042. }
  7043. _getSource() {
  7044. return this._source;
  7045. }
  7046. drawOnScreen(x, y) {
  7047. }
  7048. }
  7049. TextTexture.gTextRender = null;
  7050. TextTexture.pool = new Array(10);
  7051. TextTexture.poolLen = 0;
  7052. TextTexture.cleanTm = 0;
  7053. class TextAtlas {
  7054. constructor() {
  7055. this.texWidth = 1024;
  7056. this.texHeight = 1024;
  7057. this.texture = null;
  7058. this.charMaps = {};
  7059. this.texHeight = this.texWidth = ILaya.TextRender.atlasWidth;
  7060. this.texture = TextTexture.getTextTexture(this.texWidth, this.texHeight);
  7061. if (this.texWidth / TextAtlas.atlasGridW > 256) {
  7062. TextAtlas.atlasGridW = Math.ceil(this.texWidth / 256);
  7063. }
  7064. this.atlasgrid = new AtlasGrid(this.texWidth / TextAtlas.atlasGridW, this.texHeight / TextAtlas.atlasGridW, this.texture.id);
  7065. }
  7066. setProtecteDist(d) {
  7067. }
  7068. getAEmpty(w, h, pt) {
  7069. var find = this.atlasgrid.addRect(1, Math.ceil(w / TextAtlas.atlasGridW), Math.ceil(h / TextAtlas.atlasGridW), pt);
  7070. if (find) {
  7071. pt.x *= TextAtlas.atlasGridW;
  7072. pt.y *= TextAtlas.atlasGridW;
  7073. }
  7074. return find;
  7075. }
  7076. get usedRate() {
  7077. return this.atlasgrid._used;
  7078. }
  7079. destroy() {
  7080. for (var k in this.charMaps) {
  7081. var ri = this.charMaps[k];
  7082. ri.deleted = true;
  7083. }
  7084. this.texture.discard();
  7085. }
  7086. printDebugInfo() {
  7087. }
  7088. }
  7089. TextAtlas.atlasGridW = 16;
  7090. class Event {
  7091. setTo(type, currentTarget, target) {
  7092. this.type = type;
  7093. this.currentTarget = currentTarget;
  7094. this.target = target;
  7095. return this;
  7096. }
  7097. stopPropagation() {
  7098. this._stoped = true;
  7099. }
  7100. get touches() {
  7101. if (!this.nativeEvent)
  7102. return null;
  7103. var arr = this.nativeEvent.touches;
  7104. if (arr) {
  7105. var stage = ILaya.stage;
  7106. for (var i = 0, n = arr.length; i < n; i++) {
  7107. var e = arr[i];
  7108. var point = Point.TEMP;
  7109. point.setTo(e.clientX, e.clientY);
  7110. stage._canvasTransform.invertTransformPoint(point);
  7111. stage.transform.invertTransformPoint(point);
  7112. e.stageX = point.x;
  7113. e.stageY = point.y;
  7114. }
  7115. }
  7116. return arr;
  7117. }
  7118. get altKey() {
  7119. return this.nativeEvent.altKey;
  7120. }
  7121. get ctrlKey() {
  7122. return this.nativeEvent.ctrlKey;
  7123. }
  7124. get shiftKey() {
  7125. return this.nativeEvent.shiftKey;
  7126. }
  7127. get charCode() {
  7128. return this.nativeEvent.charCode;
  7129. }
  7130. get keyLocation() {
  7131. return this.nativeEvent.location || this.nativeEvent.keyLocation;
  7132. }
  7133. get stageX() {
  7134. return ILaya.stage.mouseX;
  7135. }
  7136. get stageY() {
  7137. return ILaya.stage.mouseY;
  7138. }
  7139. }
  7140. Event.EMPTY = new Event();
  7141. Event.MOUSE_DOWN = "mousedown";
  7142. Event.MOUSE_UP = "mouseup";
  7143. Event.CLICK = "click";
  7144. Event.RIGHT_MOUSE_DOWN = "rightmousedown";
  7145. Event.RIGHT_MOUSE_UP = "rightmouseup";
  7146. Event.RIGHT_CLICK = "rightclick";
  7147. Event.MOUSE_MOVE = "mousemove";
  7148. Event.MOUSE_OVER = "mouseover";
  7149. Event.MOUSE_OUT = "mouseout";
  7150. Event.MOUSE_WHEEL = "mousewheel";
  7151. Event.ROLL_OVER = "mouseover";
  7152. Event.ROLL_OUT = "mouseout";
  7153. Event.DOUBLE_CLICK = "doubleclick";
  7154. Event.CHANGE = "change";
  7155. Event.CHANGED = "changed";
  7156. Event.RESIZE = "resize";
  7157. Event.ADDED = "added";
  7158. Event.REMOVED = "removed";
  7159. Event.DISPLAY = "display";
  7160. Event.UNDISPLAY = "undisplay";
  7161. Event.ERROR = "error";
  7162. Event.COMPLETE = "complete";
  7163. Event.LOADED = "loaded";
  7164. Event.READY = "ready";
  7165. Event.PROGRESS = "progress";
  7166. Event.INPUT = "input";
  7167. Event.RENDER = "render";
  7168. Event.OPEN = "open";
  7169. Event.MESSAGE = "message";
  7170. Event.CLOSE = "close";
  7171. Event.KEY_DOWN = "keydown";
  7172. Event.KEY_PRESS = "keypress";
  7173. Event.KEY_UP = "keyup";
  7174. Event.FRAME = "enterframe";
  7175. Event.DRAG_START = "dragstart";
  7176. Event.DRAG_MOVE = "dragmove";
  7177. Event.DRAG_END = "dragend";
  7178. Event.ENTER = "enter";
  7179. Event.SELECT = "select";
  7180. Event.BLUR = "blur";
  7181. Event.FOCUS = "focus";
  7182. Event.VISIBILITY_CHANGE = "visibilitychange";
  7183. Event.FOCUS_CHANGE = "focuschange";
  7184. Event.PLAYED = "played";
  7185. Event.PAUSED = "paused";
  7186. Event.STOPPED = "stopped";
  7187. Event.START = "start";
  7188. Event.END = "end";
  7189. Event.COMPONENT_ADDED = "componentadded";
  7190. Event.COMPONENT_REMOVED = "componentremoved";
  7191. Event.RELEASED = "released";
  7192. Event.LINK = "link";
  7193. Event.LABEL = "label";
  7194. Event.FULL_SCREEN_CHANGE = "fullscreenchange";
  7195. Event.DEVICE_LOST = "devicelost";
  7196. Event.TRANSFORM_CHANGED = "transformchanged";
  7197. Event.ANIMATION_CHANGED = "animationchanged";
  7198. Event.TRAIL_FILTER_CHANGE = "trailfilterchange";
  7199. Event.TRIGGER_ENTER = "triggerenter";
  7200. Event.TRIGGER_STAY = "triggerstay";
  7201. Event.TRIGGER_EXIT = "triggerexit";
  7202. class Texture extends EventDispatcher {
  7203. constructor(bitmap = null, uv = null, sourceWidth = 0, sourceHeight = 0) {
  7204. super();
  7205. this.uvrect = [0, 0, 1, 1];
  7206. this._destroyed = false;
  7207. this._referenceCount = 0;
  7208. this.$_GID = 0;
  7209. this.offsetX = 0;
  7210. this.offsetY = 0;
  7211. this._w = 0;
  7212. this._h = 0;
  7213. this.sourceWidth = 0;
  7214. this.sourceHeight = 0;
  7215. this.url = null;
  7216. this.scaleRate = 1;
  7217. this.setTo(bitmap, uv, sourceWidth, sourceHeight);
  7218. }
  7219. static moveUV(offsetX, offsetY, uv) {
  7220. for (var i = 0; i < 8; i += 2) {
  7221. uv[i] += offsetX;
  7222. uv[i + 1] += offsetY;
  7223. }
  7224. return uv;
  7225. }
  7226. static create(source, x, y, width, height, offsetX = 0, offsetY = 0, sourceWidth = 0, sourceHeight = 0) {
  7227. return Texture._create(source, x, y, width, height, offsetX, offsetY, sourceWidth, sourceHeight);
  7228. }
  7229. static _create(source, x, y, width, height, offsetX = 0, offsetY = 0, sourceWidth = 0, sourceHeight = 0, outTexture = null) {
  7230. var btex = source instanceof Texture;
  7231. var uv = btex ? source.uv : Texture.DEF_UV;
  7232. var bitmap = btex ? source.bitmap : source;
  7233. if (bitmap.width && (x + width) > bitmap.width)
  7234. width = bitmap.width - x;
  7235. if (bitmap.height && (y + height) > bitmap.height)
  7236. height = bitmap.height - y;
  7237. var tex;
  7238. if (outTexture) {
  7239. tex = outTexture;
  7240. tex.setTo(bitmap, null, sourceWidth || width, sourceHeight || height);
  7241. }
  7242. else {
  7243. tex = new Texture(bitmap, null, sourceWidth || width, sourceHeight || height);
  7244. }
  7245. tex.width = width;
  7246. tex.height = height;
  7247. tex.offsetX = offsetX;
  7248. tex.offsetY = offsetY;
  7249. var dwidth = 1 / bitmap.width;
  7250. var dheight = 1 / bitmap.height;
  7251. x *= dwidth;
  7252. y *= dheight;
  7253. width *= dwidth;
  7254. height *= dheight;
  7255. var u1 = tex.uv[0], v1 = tex.uv[1], u2 = tex.uv[4], v2 = tex.uv[5];
  7256. var inAltasUVWidth = (u2 - u1), inAltasUVHeight = (v2 - v1);
  7257. var oriUV = Texture.moveUV(uv[0], uv[1], [x, y, x + width, y, x + width, y + height, x, y + height]);
  7258. tex.uv = new Float32Array([u1 + oriUV[0] * inAltasUVWidth, v1 + oriUV[1] * inAltasUVHeight,
  7259. u2 - (1 - oriUV[2]) * inAltasUVWidth, v1 + oriUV[3] * inAltasUVHeight,
  7260. u2 - (1 - oriUV[4]) * inAltasUVWidth, v2 - (1 - oriUV[5]) * inAltasUVHeight,
  7261. u1 + oriUV[6] * inAltasUVWidth, v2 - (1 - oriUV[7]) * inAltasUVHeight]);
  7262. var bitmapScale = bitmap.scaleRate;
  7263. if (bitmapScale && bitmapScale != 1) {
  7264. tex.sourceWidth /= bitmapScale;
  7265. tex.sourceHeight /= bitmapScale;
  7266. tex.width /= bitmapScale;
  7267. tex.height /= bitmapScale;
  7268. tex.scaleRate = bitmapScale;
  7269. }
  7270. else {
  7271. tex.scaleRate = 1;
  7272. }
  7273. return tex;
  7274. }
  7275. static createFromTexture(texture, x, y, width, height) {
  7276. var texScaleRate = texture.scaleRate;
  7277. if (texScaleRate != 1) {
  7278. x *= texScaleRate;
  7279. y *= texScaleRate;
  7280. width *= texScaleRate;
  7281. height *= texScaleRate;
  7282. }
  7283. var rect = Rectangle.TEMP.setTo(x - texture.offsetX, y - texture.offsetY, width, height);
  7284. var result = rect.intersection(Texture._rect1.setTo(0, 0, texture.width, texture.height), Texture._rect2);
  7285. if (result)
  7286. var tex = Texture.create(texture, result.x, result.y, result.width, result.height, result.x - rect.x, result.y - rect.y, width, height);
  7287. else
  7288. return null;
  7289. return tex;
  7290. }
  7291. get uv() {
  7292. return this._uv;
  7293. }
  7294. set uv(value) {
  7295. this.uvrect[0] = Math.min(value[0], value[2], value[4], value[6]);
  7296. this.uvrect[1] = Math.min(value[1], value[3], value[5], value[7]);
  7297. this.uvrect[2] = Math.max(value[0], value[2], value[4], value[6]) - this.uvrect[0];
  7298. this.uvrect[3] = Math.max(value[1], value[3], value[5], value[7]) - this.uvrect[1];
  7299. this._uv = value;
  7300. }
  7301. get width() {
  7302. if (this._w)
  7303. return this._w;
  7304. if (!this.bitmap)
  7305. return 0;
  7306. return (this.uv && this.uv !== Texture.DEF_UV) ? (this.uv[2] - this.uv[0]) * this.bitmap.width : this.bitmap.width;
  7307. }
  7308. set width(value) {
  7309. this._w = value;
  7310. this.sourceWidth || (this.sourceWidth = value);
  7311. }
  7312. get height() {
  7313. if (this._h)
  7314. return this._h;
  7315. if (!this.bitmap)
  7316. return 0;
  7317. return (this.uv && this.uv !== Texture.DEF_UV) ? (this.uv[5] - this.uv[1]) * this.bitmap.height : this.bitmap.height;
  7318. }
  7319. set height(value) {
  7320. this._h = value;
  7321. this.sourceHeight || (this.sourceHeight = value);
  7322. }
  7323. get bitmap() {
  7324. return this._bitmap;
  7325. }
  7326. set bitmap(value) {
  7327. this._bitmap && this._bitmap._removeReference(this._referenceCount);
  7328. this._bitmap = value;
  7329. value && (value._addReference(this._referenceCount));
  7330. }
  7331. get destroyed() {
  7332. return this._destroyed;
  7333. }
  7334. _addReference() {
  7335. this._bitmap && this._bitmap._addReference();
  7336. this._referenceCount++;
  7337. }
  7338. _removeReference() {
  7339. this._bitmap && this._bitmap._removeReference();
  7340. this._referenceCount--;
  7341. }
  7342. _getSource(cb = null) {
  7343. if (this._destroyed || !this._bitmap)
  7344. return null;
  7345. this.recoverBitmap(cb);
  7346. return this._bitmap.destroyed ? null : this.bitmap._getSource();
  7347. }
  7348. _onLoaded(complete, context) {
  7349. if (!context) ;
  7350. else if (context == this) ;
  7351. else if (context instanceof Texture) {
  7352. var tex = context;
  7353. Texture._create(context, 0, 0, tex.width, tex.height, 0, 0, tex.sourceWidth, tex.sourceHeight, this);
  7354. }
  7355. else {
  7356. this.bitmap = context;
  7357. this.sourceWidth = this._w = context.width;
  7358. this.sourceHeight = this._h = context.height;
  7359. }
  7360. complete && complete.run();
  7361. this.event(Event.READY, this);
  7362. }
  7363. getIsReady() {
  7364. return this._destroyed ? false : (this._bitmap ? true : false);
  7365. }
  7366. setTo(bitmap = null, uv = null, sourceWidth = 0, sourceHeight = 0) {
  7367. this.bitmap = bitmap;
  7368. this.sourceWidth = sourceWidth;
  7369. this.sourceHeight = sourceHeight;
  7370. if (bitmap) {
  7371. this._w = bitmap.width;
  7372. this._h = bitmap.height;
  7373. this.sourceWidth = this.sourceWidth || bitmap.width;
  7374. this.sourceHeight = this.sourceHeight || bitmap.height;
  7375. }
  7376. this.uv = uv || Texture.DEF_UV;
  7377. }
  7378. load(url, complete = null) {
  7379. if (!this._destroyed)
  7380. ILaya.loader.load(url, Handler.create(this, this._onLoaded, [complete]), null, "htmlimage", 1, true);
  7381. }
  7382. getTexturePixels(x, y, width, height) {
  7383. var st, dst, i;
  7384. var tex2d = this.bitmap;
  7385. var texw = this._w;
  7386. var texh = this._h;
  7387. var sourceWidth = this.sourceWidth;
  7388. var sourceHeight = this.sourceHeight;
  7389. var tex2dw = tex2d.width;
  7390. var tex2dh = tex2d.height;
  7391. var offsetX = this.offsetX;
  7392. var offsetY = this.offsetY;
  7393. let draww = width;
  7394. let drawh = height;
  7395. if (x + width > texw + offsetX)
  7396. draww -= (x + width) - texw - offsetX;
  7397. if (x + width > sourceWidth)
  7398. width -= (x + width) - sourceWidth;
  7399. if (y + height > texh + offsetY)
  7400. drawh -= (y + height) - texh - offsetY;
  7401. if (y + height > sourceHeight)
  7402. height -= (y + height) - sourceHeight;
  7403. if (width <= 0 || height <= 0)
  7404. return null;
  7405. let marginL = offsetX > x ? offsetX - x : 0;
  7406. let marginT = offsetY > y ? offsetY - y : 0;
  7407. let rePosX = x > offsetX ? x - offsetX : 0;
  7408. let rePosY = y > offsetY ? y - offsetY : 0;
  7409. draww -= marginL;
  7410. drawh -= marginT;
  7411. var wstride = width * 4;
  7412. var pix = null;
  7413. try {
  7414. pix = tex2d.getPixels();
  7415. }
  7416. catch (e) {
  7417. }
  7418. if (pix) {
  7419. if (x == 0 && y == 0 && width == tex2dw && height == tex2dh)
  7420. return pix;
  7421. let uv = this._uv.slice();
  7422. let atlasPosX = Math.round(uv[0] * tex2dw);
  7423. let atlasPosY = Math.round(uv[1] * tex2dh);
  7424. var ret = new Uint8Array(width * height * 4);
  7425. wstride = tex2dw * 4;
  7426. dst = (atlasPosY + rePosY) * wstride;
  7427. st = atlasPosX * 4 + rePosX * 4 + dst;
  7428. for (i = 0; i < drawh; i++) {
  7429. ret.set(pix.slice(st, st + draww * 4), width * 4 * (i + marginT) + marginL * 4);
  7430. st += wstride;
  7431. }
  7432. return ret;
  7433. }
  7434. var ctx = new ILaya.Context();
  7435. ctx.size(width, height);
  7436. ctx.asBitmap = true;
  7437. var uv = null;
  7438. if (x != 0 || y != 0 || width != tex2dw || height != tex2dh) {
  7439. uv = this._uv.slice();
  7440. var stu = uv[0];
  7441. var stv = uv[1];
  7442. var uvw = uv[2] - stu;
  7443. var uvh = uv[7] - stv;
  7444. var uk = uvw / texw;
  7445. var vk = uvh / texh;
  7446. uv = [stu + rePosX * uk, stv + rePosY * vk,
  7447. stu + (rePosX + draww) * uk, stv + rePosY * vk,
  7448. stu + (rePosX + draww) * uk, stv + (rePosY + drawh) * vk,
  7449. stu + rePosX * uk, stv + (rePosY + drawh) * vk];
  7450. }
  7451. ctx._drawTextureM(this, marginL, marginT, draww, drawh, null, 1.0, uv);
  7452. ctx._targets.start();
  7453. ctx.flush();
  7454. ctx._targets.end();
  7455. ctx._targets.restore();
  7456. var dt = ctx._targets.getData(0, 0, width, height);
  7457. ctx.destroy();
  7458. ret = new Uint8Array(width * height * 4);
  7459. st = 0;
  7460. dst = (height - 1) * wstride;
  7461. for (i = height - 1; i >= 0; i--) {
  7462. ret.set(dt.slice(dst, dst + wstride), st);
  7463. st += wstride;
  7464. dst -= wstride;
  7465. }
  7466. return ret;
  7467. }
  7468. getPixels(x, y, width, height) {
  7469. if (window.conch) {
  7470. return this._nativeObj.getImageData(x, y, width, height);
  7471. }
  7472. else {
  7473. return this.getTexturePixels(x, y, width, height);
  7474. }
  7475. }
  7476. recoverBitmap(onok = null) {
  7477. var url = this._bitmap.url;
  7478. if (!this._destroyed && (!this._bitmap || this._bitmap.destroyed) && url) {
  7479. let tex = ILaya.Loader.loadedMap[url];
  7480. if (tex) {
  7481. this.bitmap = tex;
  7482. onok && onok();
  7483. }
  7484. else {
  7485. ILaya.loader.load(url, Handler.create(this, function (bit) {
  7486. this.bitmap = bit;
  7487. onok && onok();
  7488. }), null, "htmlimage", 1, true);
  7489. }
  7490. }
  7491. }
  7492. disposeBitmap() {
  7493. if (!this._destroyed && this._bitmap) {
  7494. this._bitmap.destroy();
  7495. }
  7496. }
  7497. destroy(force = false) {
  7498. if (!this._destroyed) {
  7499. this._destroyed = true;
  7500. var bit = this._bitmap;
  7501. if (bit) {
  7502. bit._removeReference(this._referenceCount);
  7503. if (bit.referenceCount === 0 || force)
  7504. bit.destroy();
  7505. bit = null;
  7506. }
  7507. if (this.url && this === ILaya.loader.getRes(this.url))
  7508. ILaya.Loader.clearRes(this.url);
  7509. }
  7510. }
  7511. }
  7512. Texture.DEF_UV = new Float32Array([0, 0, 1.0, 0, 1.0, 1.0, 0, 1.0]);
  7513. Texture.NO_UV = new Float32Array([0, 0, 0, 0, 0, 0, 0, 0]);
  7514. Texture.INV_UV = new Float32Array([0, 1, 1.0, 1, 1.0, 0.0, 0, 0.0]);
  7515. Texture._rect1 = new Rectangle();
  7516. Texture._rect2 = new Rectangle();
  7517. class FontInfo {
  7518. constructor(font) {
  7519. this._font = "14px Arial";
  7520. this._family = "Arial";
  7521. this._size = 14;
  7522. this._italic = false;
  7523. this._bold = false;
  7524. this._id = FontInfo._gfontID++;
  7525. this.setFont(font || this._font);
  7526. }
  7527. static Parse(font) {
  7528. if (font === FontInfo._lastFont) {
  7529. return FontInfo._lastFontInfo;
  7530. }
  7531. var r = FontInfo._cache[font];
  7532. if (!r) {
  7533. r = FontInfo._cache[font] = new FontInfo(font);
  7534. }
  7535. FontInfo._lastFont = font;
  7536. FontInfo._lastFontInfo = r;
  7537. return r;
  7538. }
  7539. setFont(value) {
  7540. this._font = value;
  7541. var _words = value.split(' ');
  7542. var l = _words.length;
  7543. if (l < 2) {
  7544. if (l == 1) {
  7545. if (_words[0].indexOf('px') > 0) {
  7546. this._size = parseInt(_words[0]);
  7547. }
  7548. }
  7549. return;
  7550. }
  7551. var szpos = -1;
  7552. for (var i = 0; i < l; i++) {
  7553. if (_words[i].indexOf('px') > 0 || _words[i].indexOf('pt') > 0) {
  7554. szpos = i;
  7555. this._size = parseInt(_words[i]);
  7556. if (this._size <= 0) {
  7557. console.error('font parse error:' + value);
  7558. this._size = 14;
  7559. }
  7560. break;
  7561. }
  7562. }
  7563. var fpos = szpos + 1;
  7564. var familys = _words[fpos];
  7565. fpos++;
  7566. for (; fpos < l; fpos++) {
  7567. familys += ' ' + _words[fpos];
  7568. }
  7569. this._family = (familys.split(','))[0];
  7570. this._italic = _words.indexOf('italic') >= 0;
  7571. this._bold = _words.indexOf('bold') >= 0;
  7572. }
  7573. }
  7574. FontInfo.EMPTY = new FontInfo(null);
  7575. FontInfo._cache = {};
  7576. FontInfo._gfontID = 0;
  7577. FontInfo._lastFont = '';
  7578. class WordText {
  7579. constructor() {
  7580. this.save = [];
  7581. this.toUpperCase = null;
  7582. this.width = -1;
  7583. this.pageChars = [];
  7584. this.startID = 0;
  7585. this.startIDStroke = 0;
  7586. this.lastGCCnt = 0;
  7587. this.splitRender = false;
  7588. this.scalex = 1;
  7589. this.scaley = 1;
  7590. }
  7591. setText(txt) {
  7592. this.changed = true;
  7593. this._text = txt;
  7594. this.width = -1;
  7595. this.cleanCache();
  7596. }
  7597. toString() {
  7598. return this._text;
  7599. }
  7600. get length() {
  7601. return this._text ? this._text.length : 0;
  7602. }
  7603. charCodeAt(i) {
  7604. return this._text ? this._text.charCodeAt(i) : NaN;
  7605. }
  7606. charAt(i) {
  7607. return this._text ? this._text.charAt(i) : null;
  7608. }
  7609. cleanCache() {
  7610. let pagecharse = this.pageChars;
  7611. for (var i in pagecharse) {
  7612. let p = pagecharse[i];
  7613. var tex = p.tex;
  7614. var words = p.words;
  7615. if (words.length == 1 && tex && tex.ri) {
  7616. tex.destroy();
  7617. }
  7618. }
  7619. this.pageChars = [];
  7620. this.startID = 0;
  7621. this.scalex = 1;
  7622. this.scaley = 1;
  7623. }
  7624. }
  7625. class CharRenderInfo {
  7626. constructor() {
  7627. this.char = '';
  7628. this.deleted = false;
  7629. this.uv = new Array(8);
  7630. this.pos = 0;
  7631. this.orix = 0;
  7632. this.oriy = 0;
  7633. this.touchTick = 0;
  7634. this.isSpace = false;
  7635. }
  7636. touch() {
  7637. var curLoop = RenderInfo.loopCount;
  7638. if (this.touchTick != curLoop) {
  7639. this.tex.touchRect(this, curLoop);
  7640. }
  7641. this.touchTick = curLoop;
  7642. }
  7643. }
  7644. class ICharRender {
  7645. constructor() {
  7646. this.fontsz = 16;
  7647. }
  7648. getWidth(font, str) { return 0; }
  7649. scale(sx, sy) {
  7650. }
  7651. get canvasWidth() {
  7652. return 0;
  7653. }
  7654. set canvasWidth(w) {
  7655. }
  7656. getCharBmp(char, font, lineWidth, colStr, strokeColStr, size, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  7657. return null;
  7658. }
  7659. }
  7660. class Browser {
  7661. static __init__() {
  7662. var Laya = window.Laya || ILaya.Laya;
  7663. if (Browser._window)
  7664. return Browser._window;
  7665. var win = Browser._window = window;
  7666. var doc = Browser._document = win.document;
  7667. var u = Browser.userAgent = win.navigator.userAgent;
  7668. var maxTouchPoints = win.navigator.maxTouchPoints || 0;
  7669. var platform = win.navigator.platform;
  7670. if ("my" in Browser.window) {
  7671. if ("tb" in Browser.window.my) {
  7672. window.tbMiniGame(Laya, Laya);
  7673. if (!Laya["TBMiniAdapter"]) {
  7674. console.error("请先添加淘宝适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-6-0");
  7675. }
  7676. else {
  7677. Laya["TBMiniAdapter"].enable();
  7678. }
  7679. }
  7680. else if (u.indexOf('AlipayMiniGame') > -1) {
  7681. window.aliPayMiniGame(Laya, Laya);
  7682. if (!Laya["ALIMiniAdapter"]) {
  7683. console.error("请先添加阿里小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-6-0");
  7684. }
  7685. else {
  7686. Laya["ALIMiniAdapter"].enable();
  7687. }
  7688. }
  7689. }
  7690. if (u.indexOf('OPPO') == -1 && u.indexOf("MiniGame") > -1 && "wx" in Browser.window) {
  7691. if ("tt" in Browser.window) {
  7692. window.ttMiniGame(Laya, Laya);
  7693. if (!Laya["TTMiniAdapter"]) {
  7694. console.error("请引入字节跳动小游戏的适配库");
  7695. }
  7696. else {
  7697. Laya["TTMiniAdapter"].enable();
  7698. }
  7699. }
  7700. else if ("bl" in Browser.window) {
  7701. window.biliMiniGame(Laya, Laya);
  7702. if (!Laya["BLMiniAdapter"]) {
  7703. console.error("请引入bilibili小游戏的适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-7-0");
  7704. }
  7705. else {
  7706. Laya["BLMiniAdapter"].enable();
  7707. }
  7708. }
  7709. else if ("qq" in Browser.window) {
  7710. window.qqMiniGame(Laya, Laya);
  7711. if (!Laya["QQMiniAdapter"]) {
  7712. console.error("请引入手机QQ小游戏的适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-0-0");
  7713. }
  7714. else {
  7715. Laya["QQMiniAdapter"].enable();
  7716. }
  7717. }
  7718. else {
  7719. window.wxMiniGame(Laya, Laya);
  7720. if (!Laya["MiniAdpter"]) {
  7721. console.error("请先添加小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?nav=zh-ts-5-0-0");
  7722. }
  7723. else {
  7724. Laya["MiniAdpter"].enable();
  7725. }
  7726. }
  7727. }
  7728. if ("hbs" in Browser.window) {
  7729. window.hwMiniGame(Laya, Laya);
  7730. if (!Laya["HWMiniAdapter"]) {
  7731. console.error("请先添加小游戏适配库!");
  7732. }
  7733. else {
  7734. Laya["HWMiniAdapter"].enable();
  7735. }
  7736. }
  7737. if (u.indexOf("SwanGame") > -1) {
  7738. window.bdMiniGame(Laya, Laya);
  7739. if (!Laya["BMiniAdapter"]) {
  7740. console.error("请先添加百度小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-1-0");
  7741. }
  7742. else {
  7743. Laya["BMiniAdapter"].enable();
  7744. }
  7745. }
  7746. if (u.indexOf('QuickGame') > -1) {
  7747. window.miMiniGame(Laya, Laya);
  7748. if (!Laya["KGMiniAdapter"]) {
  7749. console.error("请先添加小米小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-2-0");
  7750. }
  7751. else {
  7752. Laya["KGMiniAdapter"].enable();
  7753. }
  7754. }
  7755. if (u.indexOf('OPPO') > -1 && u.indexOf('MiniGame') > -1) {
  7756. window.qgMiniGame(Laya, Laya);
  7757. if (!Laya["QGMiniAdapter"]) {
  7758. console.error("请先添加OPPO小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-3-0");
  7759. }
  7760. else {
  7761. Laya["QGMiniAdapter"].enable();
  7762. }
  7763. }
  7764. if (u.indexOf('VVGame') > -1) {
  7765. window.vvMiniGame(Laya, Laya);
  7766. if (!Laya["VVMiniAdapter"]) {
  7767. console.error("请先添加VIVO小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?language=zh&nav=zh-ts-5-4-0");
  7768. }
  7769. else {
  7770. Laya["VVMiniAdapter"].enable();
  7771. }
  7772. }
  7773. win.trace = console.log;
  7774. win.requestAnimationFrame = win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || win.oRequestAnimationFrame || win.msRequestAnimationFrame || function (fun) {
  7775. return win.setTimeout(fun, 1000 / 60);
  7776. };
  7777. var bodyStyle = doc.body.style;
  7778. bodyStyle.margin = 0;
  7779. bodyStyle.overflow = 'hidden';
  7780. bodyStyle['-webkit-user-select'] = 'none';
  7781. bodyStyle['-webkit-tap-highlight-color'] = 'rgba(200,200,200,0)';
  7782. var metas = doc.getElementsByTagName('meta');
  7783. var i = 0, flag = false, content = 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no';
  7784. while (i < metas.length) {
  7785. var meta = metas[i];
  7786. if (meta.name == 'viewport') {
  7787. meta.content = content;
  7788. flag = true;
  7789. break;
  7790. }
  7791. i++;
  7792. }
  7793. if (!flag) {
  7794. meta = doc.createElement('meta');
  7795. meta.name = 'viewport', meta.content = content;
  7796. doc.getElementsByTagName('head')[0].appendChild(meta);
  7797. }
  7798. Browser.onMobile = window.isConchApp ? true : u.indexOf("Mobile") > -1;
  7799. Browser.onIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  7800. Browser.onIPhone = u.indexOf("iPhone") > -1;
  7801. Browser.onMac = u.indexOf("Mac OS X") > -1;
  7802. Browser.onIPad = u.indexOf("iPad") > -1 || (platform === 'MacIntel' && maxTouchPoints > 1);
  7803. Browser.onAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  7804. Browser.onWP = u.indexOf("Windows Phone") > -1;
  7805. Browser.onQQBrowser = u.indexOf("QQBrowser") > -1;
  7806. Browser.onMQQBrowser = u.indexOf("MQQBrowser") > -1 || (u.indexOf("Mobile") > -1 && u.indexOf("QQ") > -1);
  7807. Browser.onIE = !!win.ActiveXObject || "ActiveXObject" in win;
  7808. Browser.onWeiXin = u.indexOf('MicroMessenger') > -1;
  7809. Browser.onSafari = u.indexOf("Safari") > -1;
  7810. Browser.onPC = !Browser.onMobile;
  7811. Browser.onMiniGame = u.indexOf('MiniGame') > -1;
  7812. Browser.onBDMiniGame = u.indexOf('SwanGame') > -1;
  7813. Browser.onLayaRuntime = !!Browser.window.conch;
  7814. if (u.indexOf('OPPO') > -1 && u.indexOf('MiniGame') > -1) {
  7815. Browser.onQGMiniGame = true;
  7816. Browser.onMiniGame = false;
  7817. }
  7818. else if ("qq" in Browser.window && u.indexOf('MiniGame') > -1) {
  7819. Browser.onQQMiniGame = true;
  7820. Browser.onMiniGame = false;
  7821. }
  7822. else if ("bl" in Browser.window && u.indexOf('MiniGame') > -1) {
  7823. Browser.onBLMiniGame = true;
  7824. Browser.onMiniGame = false;
  7825. }
  7826. else if ("tt" in Browser.window && u.indexOf('MiniGame') > -1) {
  7827. Browser.onTTMiniGame = true;
  7828. Browser.onMiniGame = false;
  7829. }
  7830. Browser.onHWMiniGame = "hbs" in Browser.window;
  7831. Browser.onVVMiniGame = u.indexOf('VVGame') > -1;
  7832. Browser.onKGMiniGame = u.indexOf('QuickGame') > -1;
  7833. if (u.indexOf('AlipayMiniGame') > -1) {
  7834. Browser.onAlipayMiniGame = true;
  7835. Browser.onMiniGame = false;
  7836. }
  7837. if (u.indexOf('TB/') > -1 || u.indexOf('Taobao/') > -1 || u.indexOf('TM/') > -1) {
  7838. Browser.onTBMiniGame = true;
  7839. }
  7840. return win;
  7841. }
  7842. static createElement(type) {
  7843. Browser.__init__();
  7844. return Browser._document.createElement(type);
  7845. }
  7846. static getElementById(type) {
  7847. Browser.__init__();
  7848. return Browser._document.getElementById(type);
  7849. }
  7850. static removeElement(ele) {
  7851. if (ele && ele.parentNode)
  7852. ele.parentNode.removeChild(ele);
  7853. }
  7854. static now() {
  7855. return Date.now();
  7856. }
  7857. static get clientWidth() {
  7858. Browser.__init__();
  7859. return Browser._window.innerWidth || Browser._document.body.clientWidth;
  7860. }
  7861. static get clientHeight() {
  7862. Browser.__init__();
  7863. return Browser._window.innerHeight || Browser._document.body.clientHeight || Browser._document.documentElement.clientHeight;
  7864. }
  7865. static get width() {
  7866. Browser.__init__();
  7867. return ((ILaya.stage && ILaya.stage.canvasRotation) ? Browser.clientHeight : Browser.clientWidth) * Browser.pixelRatio;
  7868. }
  7869. static get height() {
  7870. Browser.__init__();
  7871. return ((ILaya.stage && ILaya.stage.canvasRotation) ? Browser.clientWidth : Browser.clientHeight) * Browser.pixelRatio;
  7872. }
  7873. static get pixelRatio() {
  7874. if (Browser._pixelRatio < 0) {
  7875. Browser.__init__();
  7876. if (Browser.userAgent.indexOf("Mozilla/6.0(Linux; Android 6.0; HUAWEI NXT-AL10 Build/HUAWEINXT-AL10)") > -1)
  7877. Browser._pixelRatio = 2;
  7878. else {
  7879. Browser._pixelRatio = (Browser._window.devicePixelRatio || 1);
  7880. if (Browser._pixelRatio < 1)
  7881. Browser._pixelRatio = 1;
  7882. }
  7883. }
  7884. return Browser._pixelRatio;
  7885. }
  7886. static get container() {
  7887. if (!Browser._container) {
  7888. Browser.__init__();
  7889. Browser._container = Browser.createElement("div");
  7890. Browser._container.id = "layaContainer";
  7891. Browser._document.body.appendChild(Browser._container);
  7892. }
  7893. return Browser._container;
  7894. }
  7895. static set container(value) {
  7896. Browser._container = value;
  7897. }
  7898. static get window() {
  7899. return Browser._window || Browser.__init__();
  7900. }
  7901. static get document() {
  7902. Browser.__init__();
  7903. return Browser._document;
  7904. }
  7905. }
  7906. Browser._pixelRatio = -1;
  7907. Browser.mainCanvas = null;
  7908. Browser.hanzi = new RegExp("^[\u4E00-\u9FA5]$");
  7909. Browser.fontMap = [];
  7910. Browser.measureText = function (txt, font) {
  7911. var isChinese = Browser.hanzi.test(txt);
  7912. if (isChinese && Browser.fontMap[font]) {
  7913. return Browser.fontMap[font];
  7914. }
  7915. var ctx = Browser.context;
  7916. ctx.font = font;
  7917. var r = ctx.measureText(txt);
  7918. if (isChinese)
  7919. Browser.fontMap[font] = r;
  7920. return r;
  7921. };
  7922. class CharRender_Canvas extends ICharRender {
  7923. constructor(maxw, maxh, scalefont = true, useImageData = true, showdbg = false) {
  7924. super();
  7925. this.ctx = null;
  7926. this.lastScaleX = 1.0;
  7927. this.lastScaleY = 1.0;
  7928. this.maxTexW = 0;
  7929. this.maxTexH = 0;
  7930. this.scaleFontSize = true;
  7931. this.showDbgInfo = false;
  7932. this.supportImageData = true;
  7933. this.maxTexW = maxw;
  7934. this.maxTexH = maxh;
  7935. this.scaleFontSize = scalefont;
  7936. this.supportImageData = useImageData;
  7937. this.showDbgInfo = showdbg;
  7938. if (!CharRender_Canvas.canvas) {
  7939. CharRender_Canvas.canvas = Browser.createElement('canvas');
  7940. CharRender_Canvas.canvas.width = 1024;
  7941. CharRender_Canvas.canvas.height = 512;
  7942. CharRender_Canvas.canvas.style.left = "-10000px";
  7943. CharRender_Canvas.canvas.style.position = "absolute";
  7944. document.body.appendChild(CharRender_Canvas.canvas);
  7945. this.ctx = CharRender_Canvas.canvas.getContext('2d');
  7946. }
  7947. }
  7948. get canvasWidth() {
  7949. return CharRender_Canvas.canvas.width;
  7950. }
  7951. set canvasWidth(w) {
  7952. if (CharRender_Canvas.canvas.width == w)
  7953. return;
  7954. CharRender_Canvas.canvas.width = w;
  7955. if (w > 2048) {
  7956. console.warn("画文字设置的宽度太大,超过2048了");
  7957. }
  7958. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  7959. this.ctx.scale(this.lastScaleX, this.lastScaleY);
  7960. }
  7961. getWidth(font, str) {
  7962. if (!this.ctx)
  7963. return 0;
  7964. if (this.ctx._lastFont != font) {
  7965. this.ctx.font = font;
  7966. this.ctx._lastFont = font;
  7967. }
  7968. return this.ctx.measureText(str).width;
  7969. }
  7970. scale(sx, sy) {
  7971. if (!this.supportImageData) {
  7972. this.lastScaleX = sx;
  7973. this.lastScaleY = sy;
  7974. return;
  7975. }
  7976. if (this.lastScaleX != sx || this.lastScaleY != sy) {
  7977. this.ctx.setTransform(sx, 0, 0, sy, 0, 0);
  7978. this.lastScaleX = sx;
  7979. this.lastScaleY = sy;
  7980. }
  7981. }
  7982. getCharBmp(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  7983. if (!this.supportImageData)
  7984. return this.getCharCanvas(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom);
  7985. var ctx = this.ctx;
  7986. var sz = this.fontsz;
  7987. if (ctx.font != font) {
  7988. ctx.font = font;
  7989. ctx._lastFont = font;
  7990. }
  7991. cri.width = ctx.measureText(char).width;
  7992. var w = cri.width * this.lastScaleX;
  7993. var h = cri.height * this.lastScaleY;
  7994. w += (margin_left + margin_right) * this.lastScaleX;
  7995. h += (margin_top + margin_bottom) * this.lastScaleY;
  7996. w = Math.ceil(w);
  7997. h = Math.ceil(h);
  7998. w = Math.min(w, CharRender_Canvas.canvas.width);
  7999. h = Math.min(h, CharRender_Canvas.canvas.height);
  8000. var clearW = w + lineWidth * 2 + 1;
  8001. var clearH = h + lineWidth * 2 + 1;
  8002. if (rect) {
  8003. clearW = Math.max(clearW, rect[0] + rect[2] + 1);
  8004. clearH = Math.max(clearH, rect[1] + rect[3] + 1);
  8005. }
  8006. ctx.clearRect(0, 0, clearW / this.lastScaleX + 1, clearH / this.lastScaleY + 1);
  8007. ctx.save();
  8008. ctx.textBaseline = "middle";
  8009. if (lineWidth > 0) {
  8010. ctx.strokeStyle = strokeColStr;
  8011. ctx.lineWidth = lineWidth;
  8012. ctx.strokeText(char, margin_left, margin_top + sz / 2);
  8013. }
  8014. if (colStr) {
  8015. ctx.fillStyle = colStr;
  8016. ctx.fillText(char, margin_left, margin_top + sz / 2);
  8017. }
  8018. if (this.showDbgInfo) {
  8019. ctx.strokeStyle = '#ff0000';
  8020. ctx.strokeRect(1, 1, w - 2, h - 2);
  8021. ctx.strokeStyle = '#00ff00';
  8022. ctx.strokeRect(margin_left, margin_top, cri.width, cri.height);
  8023. }
  8024. if (rect) {
  8025. if (rect[2] == -1)
  8026. rect[2] = Math.ceil((cri.width + lineWidth * 2) * this.lastScaleX);
  8027. if (rect[2] <= 0)
  8028. rect[2] = 1;
  8029. }
  8030. var imgdt = rect ? (ctx.getImageData(rect[0], rect[1], rect[2], rect[3] + 1)) : (ctx.getImageData(0, 0, w, h + 1));
  8031. ctx.restore();
  8032. cri.bmpWidth = imgdt.width;
  8033. cri.bmpHeight = imgdt.height;
  8034. return imgdt;
  8035. }
  8036. getCharCanvas(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom) {
  8037. var ctx = this.ctx;
  8038. if (ctx.font != font) {
  8039. ctx.font = font;
  8040. ctx._lastFont = font;
  8041. }
  8042. cri.width = ctx.measureText(char).width;
  8043. var w = cri.width * this.lastScaleX;
  8044. var h = cri.height * this.lastScaleY;
  8045. w += (margin_left + margin_right) * this.lastScaleX;
  8046. h += ((margin_top + margin_bottom) * this.lastScaleY + 1);
  8047. w = Math.min(w, this.maxTexW);
  8048. h = Math.min(h, this.maxTexH);
  8049. CharRender_Canvas.canvas.width = Math.min(w + 1, this.maxTexW);
  8050. CharRender_Canvas.canvas.height = Math.min(h + 1, this.maxTexH);
  8051. ctx.font = font;
  8052. ctx.clearRect(0, 0, w + 1 + lineWidth, h + 1 + lineWidth);
  8053. ctx.setTransform(1, 0, 0, 1, 0, 0);
  8054. ctx.save();
  8055. if (this.scaleFontSize) {
  8056. ctx.scale(this.lastScaleX, this.lastScaleY);
  8057. }
  8058. ctx.translate(margin_left, margin_top);
  8059. ctx.textAlign = "left";
  8060. var sz = this.fontsz;
  8061. ctx.textBaseline = "middle";
  8062. if (lineWidth > 0) {
  8063. ctx.strokeStyle = strokeColStr;
  8064. ctx.fillStyle = colStr;
  8065. ctx.lineWidth = lineWidth;
  8066. if (ctx.fillAndStrokeText) {
  8067. ctx.fillAndStrokeText(char, 0, sz / 2);
  8068. }
  8069. else {
  8070. ctx.strokeText(char, 0, sz / 2);
  8071. ctx.fillText(char, 0, sz / 2);
  8072. }
  8073. }
  8074. else if (colStr) {
  8075. ctx.fillStyle = colStr;
  8076. ctx.fillText(char, 0, sz / 2);
  8077. }
  8078. if (this.showDbgInfo) {
  8079. ctx.strokeStyle = '#ff0000';
  8080. ctx.strokeRect(0, 0, w, h);
  8081. ctx.strokeStyle = '#00ff00';
  8082. ctx.strokeRect(0, 0, cri.width, cri.height);
  8083. }
  8084. ctx.restore();
  8085. cri.bmpWidth = CharRender_Canvas.canvas.width;
  8086. cri.bmpHeight = CharRender_Canvas.canvas.height;
  8087. return CharRender_Canvas.canvas;
  8088. }
  8089. }
  8090. CharRender_Canvas.canvas = null;
  8091. class CharRender_Native extends ICharRender {
  8092. constructor() {
  8093. super();
  8094. this.lastFont = '';
  8095. this.lastScaleX = 1.0;
  8096. this.lastScaleY = 1.0;
  8097. }
  8098. getWidth(font, str) {
  8099. if (!window.conchTextCanvas)
  8100. return 0;
  8101. window.conchTextCanvas.font = font;
  8102. this.lastFont = font;
  8103. return window.conchTextCanvas.measureText(str).width;
  8104. }
  8105. scale(sx, sy) {
  8106. this.lastScaleX = sx;
  8107. this.lastScaleY = sy;
  8108. }
  8109. getCharBmp(char, font, lineWidth, colStr, strokeColStr, size, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  8110. if (!window.conchTextCanvas)
  8111. return null;
  8112. window.conchTextCanvas.font = font;
  8113. this.lastFont = font;
  8114. var w = size.width = window.conchTextCanvas.measureText(char).width;
  8115. var h = size.height;
  8116. w += (margin_left + margin_right);
  8117. h += (margin_top + margin_bottom);
  8118. window.conchTextCanvas.scale && window.conchTextCanvas.scale(this.lastScaleX, this.lastScaleY);
  8119. var c1 = ColorUtils.create(strokeColStr);
  8120. var nStrokeColor = c1.numColor;
  8121. var c2 = ColorUtils.create(colStr);
  8122. var nTextColor = c2.numColor;
  8123. var textInfo = window.conchTextCanvas.getTextBitmapData(char, nTextColor, lineWidth > 2 ? 2 : lineWidth, nStrokeColor);
  8124. size.bmpWidth = textInfo.width;
  8125. size.bmpHeight = textInfo.height;
  8126. return textInfo;
  8127. }
  8128. }
  8129. class TextRender {
  8130. constructor() {
  8131. this.fontSizeInfo = {};
  8132. this.mapFont = {};
  8133. this.fontID = 0;
  8134. this.mapColor = [];
  8135. this.colorID = 0;
  8136. this.fontScaleX = 1.0;
  8137. this.fontScaleY = 1.0;
  8138. this._curStrPos = 0;
  8139. this.textAtlases = [];
  8140. this.isoTextures = [];
  8141. this.lastFont = null;
  8142. this.fontSizeW = 0;
  8143. this.fontSizeH = 0;
  8144. this.fontSizeOffX = 0;
  8145. this.fontSizeOffY = 0;
  8146. this.renderPerChar = true;
  8147. this.tmpAtlasPos = new Point();
  8148. this.textureMem = 0;
  8149. ILaya.TextAtlas = TextAtlas;
  8150. var bugIOS = false;
  8151. var miniadp = ILaya.Laya['MiniAdpter'];
  8152. if (miniadp && miniadp.systemInfo && miniadp.systemInfo.system) {
  8153. bugIOS = miniadp.systemInfo.system.toLowerCase() === 'ios 10.1.1';
  8154. }
  8155. if ((ILaya.Browser.onMiniGame || ILaya.Browser.onTTMiniGame || ILaya.Browser.onBLMiniGame || ILaya.Browser.onAlipayMiniGame || ILaya.Browser.onTBMiniGame) && !bugIOS)
  8156. TextRender.isWan1Wan = true;
  8157. this.charRender = ILaya.Render.isConchApp ? (new CharRender_Native()) : (new CharRender_Canvas(2048, 2048, TextRender.scaleFontWithCtx, !TextRender.isWan1Wan, false));
  8158. TextRender.textRenderInst = this;
  8159. ILaya.Laya['textRender'] = this;
  8160. TextRender.atlasWidth2 = TextRender.atlasWidth * TextRender.atlasWidth;
  8161. }
  8162. setFont(font) {
  8163. if (this.lastFont == font)
  8164. return;
  8165. this.lastFont = font;
  8166. var fontsz = this.getFontSizeInfo(font._family);
  8167. var offx = fontsz >> 24;
  8168. var offy = (fontsz >> 16) & 0xff;
  8169. var fw = (fontsz >> 8) & 0xff;
  8170. var fh = fontsz & 0xff;
  8171. var k = font._size / TextRender.standardFontSize;
  8172. this.fontSizeOffX = Math.ceil(offx * k);
  8173. this.fontSizeOffY = Math.ceil(offy * k);
  8174. this.fontSizeW = Math.ceil(fw * k);
  8175. this.fontSizeH = Math.ceil(fh * k);
  8176. if (font._font.indexOf('italic') >= 0) {
  8177. this.fontStr = font._font.replace('italic', '');
  8178. }
  8179. else {
  8180. this.fontStr = font._font;
  8181. }
  8182. }
  8183. getNextChar(str) {
  8184. var len = str.length;
  8185. var start = this._curStrPos;
  8186. if (!str.substring)
  8187. return null;
  8188. if (start >= len)
  8189. return null;
  8190. var i = start;
  8191. var state = 0;
  8192. for (; i < len; i++) {
  8193. var c = str.charCodeAt(i);
  8194. if ((c >>> 11) == 0x1b) {
  8195. if (state == 1)
  8196. break;
  8197. state = 1;
  8198. i++;
  8199. }
  8200. else if (c === 0xfe0e || c === 0xfe0f) ;
  8201. else if (c == 0x200d) {
  8202. state = 2;
  8203. }
  8204. else {
  8205. if (state == 0)
  8206. state = 1;
  8207. else if (state == 1)
  8208. break;
  8209. }
  8210. }
  8211. this._curStrPos = i;
  8212. return str.substring(start, i);
  8213. }
  8214. filltext(ctx, data, x, y, fontStr, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  8215. if (data.length <= 0)
  8216. return;
  8217. var font = FontInfo.Parse(fontStr);
  8218. var nTextAlign = 0;
  8219. switch (textAlign) {
  8220. case 'center':
  8221. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  8222. break;
  8223. case 'right':
  8224. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  8225. break;
  8226. }
  8227. this._fast_filltext(ctx, data, null, x, y, font, color, strokeColor, lineWidth, nTextAlign, underLine);
  8228. }
  8229. fillWords(ctx, data, x, y, fontStr, color, strokeColor, lineWidth) {
  8230. if (!data)
  8231. return;
  8232. if (data.length <= 0)
  8233. return;
  8234. var font = typeof (fontStr) === 'string' ? FontInfo.Parse(fontStr) : fontStr;
  8235. this._fast_filltext(ctx, null, data, x, y, font, color, strokeColor, lineWidth, 0, 0);
  8236. }
  8237. _fast_filltext(ctx, data, htmlchars, x, y, font, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  8238. if (data && !(data.length >= 1))
  8239. return;
  8240. if (htmlchars && htmlchars.length < 1)
  8241. return;
  8242. if (lineWidth < 0)
  8243. lineWidth = 0;
  8244. this.setFont(font);
  8245. this.fontScaleX = this.fontScaleY = 1.0;
  8246. if (TextRender.scaleFontWithCtx) {
  8247. var sx = 1;
  8248. var sy = 1;
  8249. if (!ILaya.Render.isConchApp || (window.conchTextCanvas.scale)) {
  8250. sx = ctx.getMatScaleX();
  8251. sy = ctx.getMatScaleY();
  8252. }
  8253. if (sx < 1e-4 || sy < 1e-1)
  8254. return;
  8255. if (sx > 1)
  8256. this.fontScaleX = sx;
  8257. if (sy > 1)
  8258. this.fontScaleY = sy;
  8259. }
  8260. font._italic && (ctx._italicDeg = 13);
  8261. var wt = data;
  8262. var isWT = !htmlchars && (data instanceof WordText);
  8263. var str = data && data.toString();
  8264. var isHtmlChar = !!htmlchars;
  8265. var sameTexData = isWT ? wt.pageChars : [];
  8266. var strWidth = 0;
  8267. if (isWT) {
  8268. str = wt._text;
  8269. strWidth = wt.width;
  8270. if (strWidth < 0) {
  8271. strWidth = wt.width = this.charRender.getWidth(this.fontStr, str);
  8272. }
  8273. }
  8274. else {
  8275. strWidth = str ? this.charRender.getWidth(this.fontStr, str) : 0;
  8276. }
  8277. switch (textAlign) {
  8278. case ILaya.Context.ENUM_TEXTALIGN_CENTER:
  8279. x -= strWidth / 2;
  8280. break;
  8281. case ILaya.Context.ENUM_TEXTALIGN_RIGHT:
  8282. x -= strWidth;
  8283. break;
  8284. }
  8285. if (wt && sameTexData) {
  8286. if (this.hasFreedText(sameTexData)) {
  8287. sameTexData = wt.pageChars = [];
  8288. }
  8289. }
  8290. var ri = null;
  8291. var splitTex = this.renderPerChar = (!isWT) || TextRender.forceSplitRender || isHtmlChar || (isWT && wt.splitRender);
  8292. if (!sameTexData || sameTexData.length < 1) {
  8293. if (isWT) {
  8294. wt.scalex = this.fontScaleX;
  8295. wt.scaley = this.fontScaleY;
  8296. }
  8297. if (splitTex) {
  8298. var stx = 0;
  8299. var sty = 0;
  8300. this._curStrPos = 0;
  8301. var curstr;
  8302. while (true) {
  8303. if (htmlchars) {
  8304. var chc = htmlchars[this._curStrPos++];
  8305. if (chc) {
  8306. curstr = chc.char;
  8307. stx = chc.x;
  8308. sty = chc.y;
  8309. }
  8310. else {
  8311. curstr = null;
  8312. }
  8313. }
  8314. else {
  8315. curstr = this.getNextChar(str);
  8316. }
  8317. if (!curstr)
  8318. break;
  8319. ri = this.getCharRenderInfo(curstr, font, color, strokeColor, lineWidth, false);
  8320. if (!ri) {
  8321. break;
  8322. }
  8323. if (ri.isSpace) ;
  8324. else {
  8325. var add = sameTexData[ri.tex.id];
  8326. if (!add) {
  8327. var o1 = { texgen: ri.tex.genID, tex: ri.tex, words: [] };
  8328. sameTexData[ri.tex.id] = o1;
  8329. add = o1.words;
  8330. }
  8331. else {
  8332. add = add.words;
  8333. }
  8334. add.push({ ri: ri, x: stx, y: sty, w: ri.bmpWidth / this.fontScaleX, h: ri.bmpHeight / this.fontScaleY });
  8335. stx += ri.width;
  8336. }
  8337. }
  8338. }
  8339. else {
  8340. var margin = ILaya.Render.isConchApp ? 0 : (font._size / 3 | 0);
  8341. var isotex = TextRender.noAtlas || (strWidth + margin + margin) * this.fontScaleX > TextRender.atlasWidth;
  8342. ri = this.getCharRenderInfo(str, font, color, strokeColor, lineWidth, isotex);
  8343. sameTexData[0] = { texgen: ri.tex.genID, tex: ri.tex, words: [{ ri: ri, x: 0, y: 0, w: ri.bmpWidth / this.fontScaleX, h: ri.bmpHeight / this.fontScaleY }] };
  8344. }
  8345. }
  8346. this._drawResortedWords(ctx, x, y, sameTexData);
  8347. ctx._italicDeg = 0;
  8348. }
  8349. _drawResortedWords(ctx, startx, starty, samePagesData) {
  8350. var isLastRender = ctx._charSubmitCache ? ctx._charSubmitCache._enable : false;
  8351. var mat = ctx._curMat;
  8352. var slen = samePagesData.length;
  8353. for (var id in samePagesData) {
  8354. var dt = samePagesData[id];
  8355. if (!dt)
  8356. continue;
  8357. var pri = dt.words;
  8358. var pisz = pri.length;
  8359. if (pisz <= 0)
  8360. continue;
  8361. var tex = samePagesData[id].tex;
  8362. for (var j = 0; j < pisz; j++) {
  8363. var riSaved = pri[j];
  8364. var ri = riSaved.ri;
  8365. if (ri.isSpace)
  8366. continue;
  8367. ri.touch();
  8368. ctx.drawTexAlign = true;
  8369. if (ILaya.Render.isConchApp) {
  8370. ctx._drawTextureM(tex.texture, startx + riSaved.x - ri.orix, starty + riSaved.y - ri.oriy, riSaved.w, riSaved.h, null, 1.0, ri.uv);
  8371. }
  8372. else {
  8373. let t = tex;
  8374. ctx._inner_drawTexture(t.texture, t.id, startx + riSaved.x - ri.orix, starty + riSaved.y - ri.oriy, riSaved.w, riSaved.h, mat, ri.uv, 1.0, isLastRender);
  8375. }
  8376. if (ctx.touches) {
  8377. ctx.touches.push(ri);
  8378. }
  8379. }
  8380. }
  8381. }
  8382. hasFreedText(txts) {
  8383. for (let i in txts) {
  8384. var pri = txts[i];
  8385. if (!pri)
  8386. continue;
  8387. var tex = pri.tex;
  8388. if (tex.__destroyed || tex.genID != pri.texgen) {
  8389. return true;
  8390. }
  8391. }
  8392. return false;
  8393. }
  8394. getCharRenderInfo(str, font, color, strokeColor, lineWidth, isoTexture = false) {
  8395. var fid = this.mapFont[font._family];
  8396. if (fid == undefined) {
  8397. this.mapFont[font._family] = fid = this.fontID++;
  8398. }
  8399. var key = str + '_' + fid + '_' + font._size + '_' + color;
  8400. if (lineWidth > 0)
  8401. key += '_' + strokeColor + lineWidth;
  8402. if (font._bold)
  8403. key += 'P';
  8404. if (this.fontScaleX != 1 || this.fontScaleY != 1) {
  8405. key += (this.fontScaleX * 20 | 0) + '_' + (this.fontScaleY * 20 | 0);
  8406. }
  8407. var i = 0;
  8408. var sz = this.textAtlases.length;
  8409. var ri;
  8410. var atlas;
  8411. if (!isoTexture) {
  8412. for (i = 0; i < sz; i++) {
  8413. atlas = this.textAtlases[i];
  8414. ri = atlas.charMaps[key];
  8415. if (ri) {
  8416. ri.touch();
  8417. return ri;
  8418. }
  8419. }
  8420. }
  8421. ri = new CharRenderInfo();
  8422. this.charRender.scale(this.fontScaleX, this.fontScaleY);
  8423. ri.char = str;
  8424. ri.height = font._size;
  8425. var margin = ILaya.Render.isConchApp ? 0 : (font._size / 3 | 0);
  8426. var imgdt = null;
  8427. if (!lineWidth) {
  8428. lineWidth = 0;
  8429. }
  8430. var w1 = Math.ceil((this.charRender.getWidth(this.fontStr, str) + 2 * lineWidth) * this.fontScaleX);
  8431. if (w1 > this.charRender.canvasWidth) {
  8432. this.charRender.canvasWidth = Math.min(2048, w1 + margin * 2);
  8433. }
  8434. if (isoTexture) {
  8435. this.charRender.fontsz = font._size;
  8436. imgdt = this.charRender.getCharBmp(str, this.fontStr, lineWidth, color, strokeColor, ri, margin, margin, margin, margin, null);
  8437. if (imgdt) {
  8438. var tex = TextTexture.getTextTexture(imgdt.width, imgdt.height);
  8439. tex.addChar(imgdt, 0, 0, ri.uv);
  8440. ri.tex = tex;
  8441. ri.orix = margin;
  8442. ri.oriy = margin;
  8443. tex.ri = ri;
  8444. this.isoTextures.push(tex);
  8445. }
  8446. }
  8447. else {
  8448. var len = str.length;
  8449. var lineExt = lineWidth * 1;
  8450. var fw = Math.ceil((this.fontSizeW + lineExt * 2) * this.fontScaleX);
  8451. var fh = Math.ceil((this.fontSizeH + lineExt * 2) * this.fontScaleY);
  8452. TextRender.imgdtRect[0] = ((margin - this.fontSizeOffX - lineExt) * this.fontScaleX) | 0;
  8453. TextRender.imgdtRect[1] = ((margin - this.fontSizeOffY - lineExt) * this.fontScaleY) | 0;
  8454. if (this.renderPerChar || len == 1) {
  8455. TextRender.imgdtRect[2] = Math.max(w1, fw);
  8456. TextRender.imgdtRect[3] = Math.max(w1, fh);
  8457. }
  8458. else {
  8459. TextRender.imgdtRect[2] = -1;
  8460. TextRender.imgdtRect[3] = fh;
  8461. }
  8462. this.charRender.fontsz = font._size;
  8463. imgdt = this.charRender.getCharBmp(str, this.fontStr, lineWidth, color, strokeColor, ri, margin, margin, margin, margin, TextRender.imgdtRect);
  8464. if (imgdt) {
  8465. atlas = this.addBmpData(imgdt, ri);
  8466. if (TextRender.isWan1Wan) {
  8467. ri.orix = margin;
  8468. ri.oriy = margin;
  8469. }
  8470. else {
  8471. ri.orix = (this.fontSizeOffX + lineExt);
  8472. ri.oriy = (this.fontSizeOffY + lineExt);
  8473. }
  8474. atlas.charMaps[key] = ri;
  8475. }
  8476. }
  8477. return ri;
  8478. }
  8479. addBmpData(data, ri) {
  8480. var w = data.width;
  8481. var h = data.height;
  8482. var sz = this.textAtlases.length;
  8483. var atlas;
  8484. var find = false;
  8485. for (var i = 0; i < sz; i++) {
  8486. atlas = this.textAtlases[i];
  8487. find = atlas.getAEmpty(w, h, this.tmpAtlasPos);
  8488. if (find) {
  8489. break;
  8490. }
  8491. }
  8492. if (!find) {
  8493. atlas = new TextAtlas();
  8494. this.textAtlases.push(atlas);
  8495. find = atlas.getAEmpty(w, h, this.tmpAtlasPos);
  8496. if (!find) {
  8497. throw 'err1';
  8498. }
  8499. this.cleanAtlases();
  8500. }
  8501. if (find) {
  8502. atlas.texture.addChar(data, this.tmpAtlasPos.x, this.tmpAtlasPos.y, ri.uv);
  8503. ri.tex = atlas.texture;
  8504. }
  8505. return atlas;
  8506. }
  8507. GC() {
  8508. var i = 0;
  8509. var sz = this.textAtlases.length;
  8510. var dt = 0;
  8511. var destroyDt = TextRender.destroyAtlasDt;
  8512. var totalUsedRate = 0;
  8513. var totalUsedRateAtlas = 0;
  8514. var curloop = RenderInfo.loopCount;
  8515. var maxWasteRateID = -1;
  8516. var maxWasteRate = 0;
  8517. var tex = null;
  8518. var curatlas = null;
  8519. for (; i < sz; i++) {
  8520. curatlas = this.textAtlases[i];
  8521. tex = curatlas.texture;
  8522. if (tex) {
  8523. totalUsedRate += tex.curUsedCovRate;
  8524. totalUsedRateAtlas += tex.curUsedCovRateAtlas;
  8525. var waste = curatlas.usedRate - tex.curUsedCovRateAtlas;
  8526. if (maxWasteRate < waste) {
  8527. maxWasteRate = waste;
  8528. maxWasteRateID = i;
  8529. }
  8530. }
  8531. dt = curloop - curatlas.texture.lastTouchTm;
  8532. if (dt > destroyDt) {
  8533. TextRender.showLog && console.log('TextRender GC delete atlas ' + tex ? curatlas.texture.id : 'unk');
  8534. curatlas.destroy();
  8535. this.textAtlases[i] = this.textAtlases[sz - 1];
  8536. sz--;
  8537. i--;
  8538. maxWasteRateID = -1;
  8539. }
  8540. }
  8541. this.textAtlases.length = sz;
  8542. sz = this.isoTextures.length;
  8543. for (i = 0; i < sz; i++) {
  8544. tex = this.isoTextures[i];
  8545. dt = curloop - tex.lastTouchTm;
  8546. if (dt > TextRender.destroyUnusedTextureDt) {
  8547. tex.ri.deleted = true;
  8548. tex.ri.tex = null;
  8549. tex.destroy();
  8550. this.isoTextures[i] = this.isoTextures[sz - 1];
  8551. sz--;
  8552. i--;
  8553. }
  8554. }
  8555. this.isoTextures.length = sz;
  8556. var needGC = this.textAtlases.length > 1 && this.textAtlases.length - totalUsedRateAtlas >= 2;
  8557. if (TextRender.atlasWidth * TextRender.atlasWidth * 4 * this.textAtlases.length > TextRender.cleanMem || needGC || TextRender.simClean) {
  8558. TextRender.simClean = false;
  8559. TextRender.showLog && console.log('清理使用率低的贴图。总使用率:', totalUsedRateAtlas, ':', this.textAtlases.length, '最差贴图:' + maxWasteRateID);
  8560. if (maxWasteRateID >= 0) {
  8561. curatlas = this.textAtlases[maxWasteRateID];
  8562. curatlas.destroy();
  8563. this.textAtlases[maxWasteRateID] = this.textAtlases[this.textAtlases.length - 1];
  8564. this.textAtlases.length = this.textAtlases.length - 1;
  8565. }
  8566. }
  8567. TextTexture.clean();
  8568. }
  8569. cleanAtlases() {
  8570. }
  8571. getCharBmp(c) {
  8572. }
  8573. checkBmpLine(data, l, sx, ex) {
  8574. if (this.bmpData32.buffer != data.data.buffer) {
  8575. this.bmpData32 = new Uint32Array(data.data.buffer);
  8576. }
  8577. var stpos = data.width * l + sx;
  8578. for (var x = sx; x < ex; x++) {
  8579. if (this.bmpData32[stpos++] != 0)
  8580. return true;
  8581. }
  8582. return false;
  8583. }
  8584. updateBbx(data, curbbx, onlyH = false) {
  8585. var w = data.width;
  8586. var h = data.height;
  8587. var x = 0;
  8588. var sy = curbbx[1];
  8589. var ey = 0;
  8590. var y = sy;
  8591. if (this.checkBmpLine(data, sy, 0, w)) {
  8592. while (true) {
  8593. y = (sy + ey) / 2 | 0;
  8594. if (y + 1 >= sy) {
  8595. curbbx[1] = y;
  8596. break;
  8597. }
  8598. if (this.checkBmpLine(data, y, 0, w)) {
  8599. sy = y;
  8600. }
  8601. else {
  8602. ey = y;
  8603. }
  8604. }
  8605. }
  8606. if (curbbx[3] > h)
  8607. curbbx[3] = h;
  8608. else {
  8609. y = sy = curbbx[3];
  8610. ey = h;
  8611. if (this.checkBmpLine(data, sy, 0, w)) {
  8612. while (true) {
  8613. y = (sy + ey) / 2 | 0;
  8614. if (y - 1 <= sy) {
  8615. curbbx[3] = y;
  8616. break;
  8617. }
  8618. if (this.checkBmpLine(data, y, 0, w)) {
  8619. sy = y;
  8620. }
  8621. else {
  8622. ey = y;
  8623. }
  8624. }
  8625. }
  8626. }
  8627. if (onlyH)
  8628. return;
  8629. var minx = curbbx[0];
  8630. var stpos = w * curbbx[1];
  8631. for (y = curbbx[1]; y < curbbx[3]; y++) {
  8632. for (x = 0; x < minx; x++) {
  8633. if (this.bmpData32[stpos + x] != 0) {
  8634. minx = x;
  8635. break;
  8636. }
  8637. }
  8638. stpos += w;
  8639. }
  8640. curbbx[0] = minx;
  8641. var maxx = curbbx[2];
  8642. stpos = w * curbbx[1];
  8643. for (y = curbbx[1]; y < curbbx[3]; y++) {
  8644. for (x = maxx; x < w; x++) {
  8645. if (this.bmpData32[stpos + x] != 0) {
  8646. maxx = x;
  8647. break;
  8648. }
  8649. }
  8650. stpos += w;
  8651. }
  8652. curbbx[2] = maxx;
  8653. }
  8654. getFontSizeInfo(font) {
  8655. var finfo = this.fontSizeInfo[font];
  8656. if (finfo != undefined)
  8657. return finfo;
  8658. var fontstr = 'bold ' + TextRender.standardFontSize + 'px ' + font;
  8659. if (TextRender.isWan1Wan) {
  8660. this.fontSizeW = this.charRender.getWidth(fontstr, '有') * 1.5;
  8661. this.fontSizeH = TextRender.standardFontSize * 1.5;
  8662. var szinfo = this.fontSizeW << 8 | this.fontSizeH;
  8663. this.fontSizeInfo[font] = szinfo;
  8664. return szinfo;
  8665. }
  8666. TextRender.pixelBBX[0] = TextRender.standardFontSize / 2;
  8667. TextRender.pixelBBX[1] = TextRender.standardFontSize / 2;
  8668. TextRender.pixelBBX[2] = TextRender.standardFontSize;
  8669. TextRender.pixelBBX[3] = TextRender.standardFontSize;
  8670. var orix = 16;
  8671. var oriy = 16;
  8672. var marginr = 16;
  8673. var marginb = 16;
  8674. this.charRender.scale(1, 1);
  8675. TextRender.tmpRI.height = TextRender.standardFontSize;
  8676. this.charRender.fontsz = TextRender.standardFontSize;
  8677. var bmpdt = this.charRender.getCharBmp('g', fontstr, 0, 'red', null, TextRender.tmpRI, orix, oriy, marginr, marginb);
  8678. if (ILaya.Render.isConchApp) {
  8679. bmpdt.data = new Uint8ClampedArray(bmpdt.data);
  8680. }
  8681. this.bmpData32 = new Uint32Array(bmpdt.data.buffer);
  8682. this.updateBbx(bmpdt, TextRender.pixelBBX, false);
  8683. bmpdt = this.charRender.getCharBmp('有', fontstr, 0, 'red', null, TextRender.tmpRI, oriy, oriy, marginr, marginb);
  8684. if (ILaya.Render.isConchApp) {
  8685. bmpdt.data = new Uint8ClampedArray(bmpdt.data);
  8686. }
  8687. this.bmpData32 = new Uint32Array(bmpdt.data.buffer);
  8688. if (TextRender.pixelBBX[2] < orix + TextRender.tmpRI.width)
  8689. TextRender.pixelBBX[2] = orix + TextRender.tmpRI.width;
  8690. this.updateBbx(bmpdt, TextRender.pixelBBX, false);
  8691. if (ILaya.Render.isConchApp) {
  8692. orix = 0;
  8693. oriy = 0;
  8694. }
  8695. var xoff = Math.max(orix - TextRender.pixelBBX[0], 0);
  8696. var yoff = Math.max(oriy - TextRender.pixelBBX[1], 0);
  8697. var bbxw = TextRender.pixelBBX[2] - TextRender.pixelBBX[0];
  8698. var bbxh = TextRender.pixelBBX[3] - TextRender.pixelBBX[1];
  8699. var sizeinfo = xoff << 24 | yoff << 16 | bbxw << 8 | bbxh;
  8700. this.fontSizeInfo[font] = sizeinfo;
  8701. return sizeinfo;
  8702. }
  8703. printDbgInfo() {
  8704. console.log('图集个数:' + this.textAtlases.length + ',每个图集大小:' + TextRender.atlasWidth + 'x' + TextRender.atlasWidth, ' 用canvas:', TextRender.isWan1Wan);
  8705. console.log('图集占用空间:' + (TextRender.atlasWidth * TextRender.atlasWidth * 4 / 1024 / 1024 * this.textAtlases.length) + 'M');
  8706. console.log('缓存用到的字体:');
  8707. for (var f in this.mapFont) {
  8708. var fontsz = this.getFontSizeInfo(f);
  8709. var offx = fontsz >> 24;
  8710. var offy = (fontsz >> 16) & 0xff;
  8711. var fw = (fontsz >> 8) & 0xff;
  8712. var fh = fontsz & 0xff;
  8713. console.log(' ' + f, ' off:', offx, offy, ' size:', fw, fh);
  8714. }
  8715. var num = 0;
  8716. console.log('缓存数据:');
  8717. var totalUsedRate = 0;
  8718. var totalUsedRateAtlas = 0;
  8719. this.textAtlases.forEach(function (a) {
  8720. var id = a.texture.id;
  8721. var dt = RenderInfo.loopCount - a.texture.lastTouchTm;
  8722. var dtstr = dt > 0 ? ('' + dt + '帧以前') : '当前帧';
  8723. totalUsedRate += a.texture.curUsedCovRate;
  8724. totalUsedRateAtlas += a.texture.curUsedCovRateAtlas;
  8725. console.log('--图集(id:' + id + ',当前使用率:' + (a.texture.curUsedCovRate * 1000 | 0) + '‰', '当前图集使用率:', (a.texture.curUsedCovRateAtlas * 100 | 0) + '%', '图集使用率:', (a.usedRate * 100 | 0), '%, 使用于:' + dtstr + ')--:');
  8726. for (var k in a.charMaps) {
  8727. var ri = a.charMaps[k];
  8728. console.log(' off:', ri.orix, ri.oriy, ' bmp宽高:', ri.bmpWidth, ri.bmpHeight, '无效:', ri.deleted, 'touchdt:', (RenderInfo.loopCount - ri.touchTick), '位置:', ri.uv[0] * TextRender.atlasWidth | 0, ri.uv[1] * TextRender.atlasWidth | 0, '字符:', ri.char, 'key:', k);
  8729. num++;
  8730. }
  8731. });
  8732. console.log('独立贴图文字(' + this.isoTextures.length + '个):');
  8733. this.isoTextures.forEach(function (tex) {
  8734. console.log(' size:', tex._texW, tex._texH, 'touch间隔:', (RenderInfo.loopCount - tex.lastTouchTm), 'char:', tex.ri.char);
  8735. });
  8736. console.log('总缓存:', num, '总使用率:', totalUsedRate, '总当前图集使用率:', totalUsedRateAtlas);
  8737. }
  8738. showAtlas(n, bgcolor, x, y, w, h) {
  8739. if (!this.textAtlases[n]) {
  8740. console.log('没有这个图集');
  8741. return null;
  8742. }
  8743. var sp = new ILaya.Sprite();
  8744. var texttex = this.textAtlases[n].texture;
  8745. var texture = {
  8746. width: TextRender.atlasWidth,
  8747. height: TextRender.atlasWidth,
  8748. sourceWidth: TextRender.atlasWidth,
  8749. sourceHeight: TextRender.atlasWidth,
  8750. offsetX: 0,
  8751. offsetY: 0,
  8752. getIsReady: function () { return true; },
  8753. _addReference: function () { },
  8754. _removeReference: function () { },
  8755. _getSource: function () { return texttex._getSource(); },
  8756. bitmap: { id: texttex.id },
  8757. _uv: Texture.DEF_UV
  8758. };
  8759. sp.size = function (w, h) {
  8760. this.width = w;
  8761. this.height = h;
  8762. sp.graphics.clear();
  8763. sp.graphics.drawRect(0, 0, sp.width, sp.height, bgcolor);
  8764. sp.graphics.drawTexture(texture, 0, 0, sp.width, sp.height);
  8765. return this;
  8766. };
  8767. sp.graphics.drawRect(0, 0, w, h, bgcolor);
  8768. sp.graphics.drawTexture(texture, 0, 0, w, h);
  8769. sp.pos(x, y);
  8770. ILaya.stage.addChild(sp);
  8771. return sp;
  8772. }
  8773. filltext_native(ctx, data, htmlchars, x, y, fontStr, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  8774. if (data && data.length <= 0)
  8775. return;
  8776. if (htmlchars && htmlchars.length < 1)
  8777. return;
  8778. var font = FontInfo.Parse(fontStr);
  8779. var nTextAlign = 0;
  8780. switch (textAlign) {
  8781. case 'center':
  8782. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  8783. break;
  8784. case 'right':
  8785. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  8786. break;
  8787. }
  8788. return this._fast_filltext(ctx, data, htmlchars, x, y, font, color, strokeColor, lineWidth, nTextAlign, underLine);
  8789. }
  8790. }
  8791. TextRender.useOldCharBook = false;
  8792. TextRender.atlasWidth = 1024;
  8793. TextRender.noAtlas = false;
  8794. TextRender.forceSplitRender = false;
  8795. TextRender.forceWholeRender = false;
  8796. TextRender.scaleFontWithCtx = true;
  8797. TextRender.standardFontSize = 32;
  8798. TextRender.destroyAtlasDt = 10;
  8799. TextRender.checkCleanTextureDt = 2000;
  8800. TextRender.destroyUnusedTextureDt = 3000;
  8801. TextRender.cleanMem = 100 * 1024 * 1024;
  8802. TextRender.isWan1Wan = false;
  8803. TextRender.showLog = false;
  8804. TextRender.debugUV = false;
  8805. TextRender.tmpRI = new CharRenderInfo();
  8806. TextRender.pixelBBX = [0, 0, 0, 0];
  8807. TextRender.imgdtRect = [0, 0, 0, 0];
  8808. TextRender.simClean = false;
  8809. TextTexture.gTextRender = TextRender;
  8810. class Context {
  8811. constructor() {
  8812. this._tmpMatrix = new Matrix();
  8813. this._drawTexToDrawTri_Vert = new Float32Array(8);
  8814. this._drawTexToDrawTri_Index = new Uint16Array([0, 1, 2, 0, 2, 3]);
  8815. this._tempUV = new Float32Array(8);
  8816. this._drawTriUseAbsMatrix = false;
  8817. this._id = ++Context._COUNT;
  8818. this._other = null;
  8819. this._renderNextSubmitIndex = 0;
  8820. this._path = null;
  8821. this._drawCount = 1;
  8822. this._width = Context._MAXSIZE;
  8823. this._height = Context._MAXSIZE;
  8824. this._renderCount = 0;
  8825. this._submits = null;
  8826. this._curSubmit = null;
  8827. this._submitKey = new SubmitKey();
  8828. this._pathMesh = null;
  8829. this._triangleMesh = null;
  8830. this.meshlist = [];
  8831. this._transedPoints = new Array(8);
  8832. this._temp4Points = new Array(8);
  8833. this._clipRect = Context.MAXCLIPRECT;
  8834. this._globalClipMatrix = new Matrix(Context._MAXSIZE, 0, 0, Context._MAXSIZE, 0, 0);
  8835. this._clipInCache = false;
  8836. this._clipInfoID = 0;
  8837. this._clipID_Gen = 0;
  8838. this._lastMatScaleX = 1.0;
  8839. this._lastMatScaleY = 1.0;
  8840. this._lastMat_a = 1.0;
  8841. this._lastMat_b = 0.0;
  8842. this._lastMat_c = 0.0;
  8843. this._lastMat_d = 1.0;
  8844. this._nBlendType = 0;
  8845. this._save = null;
  8846. this._targets = null;
  8847. this._charSubmitCache = null;
  8848. this._saveMark = null;
  8849. this._shader2D = new Shader2D();
  8850. this.sprite = null;
  8851. this._italicDeg = 0;
  8852. this._lastTex = null;
  8853. this._fillColor = 0;
  8854. this._flushCnt = 0;
  8855. this.defTexture = null;
  8856. this._colorFiler = null;
  8857. this.drawTexAlign = false;
  8858. this._incache = false;
  8859. this.isMain = false;
  8860. Context._contextcount++;
  8861. Context._textRender = Context._textRender || new TextRender();
  8862. if (!this.defTexture) {
  8863. var defTex2d = new Texture2D(2, 2);
  8864. defTex2d.setPixels(new Uint8Array(16));
  8865. defTex2d.lock = true;
  8866. this.defTexture = new Texture(defTex2d);
  8867. }
  8868. this._lastTex = this.defTexture;
  8869. this.clear();
  8870. }
  8871. static __init__() {
  8872. Context.MAXCLIPRECT = new Rectangle(0, 0, Context._MAXSIZE, Context._MAXSIZE);
  8873. ContextParams.DEFAULT = new ContextParams();
  8874. }
  8875. drawImage(...args) {
  8876. }
  8877. getImageData(...args) {
  8878. }
  8879. measureText(text) {
  8880. return null;
  8881. }
  8882. setTransform(...args) {
  8883. }
  8884. $transform(a, b, c, d, tx, ty) {
  8885. }
  8886. get lineJoin() {
  8887. return '';
  8888. }
  8889. set lineJoin(value) {
  8890. }
  8891. get lineCap() {
  8892. return '';
  8893. }
  8894. set lineCap(value) {
  8895. }
  8896. get miterLimit() {
  8897. return '';
  8898. }
  8899. set miterLimit(value) {
  8900. }
  8901. clearRect(x, y, width, height) {
  8902. }
  8903. _drawRect(x, y, width, height, style) {
  8904. Stat.renderBatches++;
  8905. style && (this.fillStyle = style);
  8906. this.fillRect(x, y, width, height, null);
  8907. }
  8908. drawTexture2(x, y, pivotX, pivotY, m, args2) {
  8909. }
  8910. transformByMatrix(matrix, tx, ty) {
  8911. this.transform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx + tx, matrix.ty + ty);
  8912. }
  8913. saveTransform(matrix) {
  8914. this.save();
  8915. }
  8916. restoreTransform(matrix) {
  8917. this.restore();
  8918. }
  8919. drawRect(x, y, width, height, fillColor, lineColor, lineWidth) {
  8920. var ctx = this;
  8921. if (fillColor != null) {
  8922. ctx.fillStyle = fillColor;
  8923. ctx.fillRect(x, y, width, height);
  8924. }
  8925. if (lineColor != null) {
  8926. ctx.strokeStyle = lineColor;
  8927. ctx.lineWidth = lineWidth;
  8928. ctx.strokeRect(x, y, width, height);
  8929. }
  8930. }
  8931. alpha(value) {
  8932. this.globalAlpha *= value;
  8933. }
  8934. _transform(mat, pivotX, pivotY) {
  8935. this.translate(pivotX, pivotY);
  8936. this.transform(mat.a, mat.b, mat.c, mat.d, mat.tx, mat.ty);
  8937. this.translate(-pivotX, -pivotY);
  8938. }
  8939. _rotate(angle, pivotX, pivotY) {
  8940. this.translate(pivotX, pivotY);
  8941. this.rotate(angle);
  8942. this.translate(-pivotX, -pivotY);
  8943. }
  8944. _scale(scaleX, scaleY, pivotX, pivotY) {
  8945. this.translate(pivotX, pivotY);
  8946. this.scale(scaleX, scaleY);
  8947. this.translate(-pivotX, -pivotY);
  8948. }
  8949. _drawLine(x, y, fromX, fromY, toX, toY, lineColor, lineWidth, vid) {
  8950. this.beginPath();
  8951. this.strokeStyle = lineColor;
  8952. this.lineWidth = lineWidth;
  8953. this.moveTo(x + fromX, y + fromY);
  8954. this.lineTo(x + toX, y + toY);
  8955. this.stroke();
  8956. }
  8957. _drawLines(x, y, points, lineColor, lineWidth, vid) {
  8958. this.beginPath();
  8959. this.strokeStyle = lineColor;
  8960. this.lineWidth = lineWidth;
  8961. this.addPath(points.slice(), false, false, x, y);
  8962. this.stroke();
  8963. }
  8964. drawCurves(x, y, points, lineColor, lineWidth) {
  8965. this.beginPath();
  8966. this.strokeStyle = lineColor;
  8967. this.lineWidth = lineWidth;
  8968. this.moveTo(x + points[0], y + points[1]);
  8969. var i = 2, n = points.length;
  8970. while (i < n) {
  8971. this.quadraticCurveTo(x + points[i++], y + points[i++], x + points[i++], y + points[i++]);
  8972. }
  8973. this.stroke();
  8974. }
  8975. _fillAndStroke(fillColor, strokeColor, lineWidth, isConvexPolygon = false) {
  8976. if (fillColor != null) {
  8977. this.fillStyle = fillColor;
  8978. this.fill();
  8979. }
  8980. if (strokeColor != null && lineWidth > 0) {
  8981. this.strokeStyle = strokeColor;
  8982. this.lineWidth = lineWidth;
  8983. this.stroke();
  8984. }
  8985. }
  8986. _drawCircle(x, y, radius, fillColor, lineColor, lineWidth, vid) {
  8987. Stat.renderBatches++;
  8988. this.beginPath(true);
  8989. this.arc(x, y, radius, 0, Context.PI2);
  8990. this.closePath();
  8991. this._fillAndStroke(fillColor, lineColor, lineWidth);
  8992. }
  8993. _drawPie(x, y, radius, startAngle, endAngle, fillColor, lineColor, lineWidth, vid) {
  8994. this.beginPath();
  8995. this.moveTo(x, y);
  8996. this.arc(x, y, radius, startAngle, endAngle);
  8997. this.closePath();
  8998. this._fillAndStroke(fillColor, lineColor, lineWidth);
  8999. }
  9000. _drawPoly(x, y, points, fillColor, lineColor, lineWidth, isConvexPolygon, vid) {
  9001. this.beginPath();
  9002. this.addPath(points.slice(), true, isConvexPolygon, x, y);
  9003. this.closePath();
  9004. this._fillAndStroke(fillColor, lineColor, lineWidth, isConvexPolygon);
  9005. }
  9006. _drawPath(x, y, paths, brush, pen) {
  9007. this.beginPath();
  9008. for (var i = 0, n = paths.length; i < n; i++) {
  9009. var path = paths[i];
  9010. switch (path[0]) {
  9011. case "moveTo":
  9012. this.moveTo(x + path[1], y + path[2]);
  9013. break;
  9014. case "lineTo":
  9015. this.lineTo(x + path[1], y + path[2]);
  9016. break;
  9017. case "arcTo":
  9018. this.arcTo(x + path[1], y + path[2], x + path[3], y + path[4], path[5]);
  9019. break;
  9020. case "closePath":
  9021. this.closePath();
  9022. break;
  9023. }
  9024. }
  9025. if (brush != null) {
  9026. this.fillStyle = brush.fillStyle;
  9027. this.fill();
  9028. }
  9029. if (pen != null) {
  9030. this.strokeStyle = pen.strokeStyle;
  9031. this.lineWidth = pen.lineWidth || 1;
  9032. this.lineJoin = pen.lineJoin;
  9033. this.lineCap = pen.lineCap;
  9034. this.miterLimit = pen.miterLimit;
  9035. this.stroke();
  9036. }
  9037. }
  9038. static set2DRenderConfig() {
  9039. var gl = LayaGL.instance;
  9040. WebGLContext.setBlend(gl, true);
  9041. WebGLContext.setBlendEquation(gl, gl.FUNC_ADD);
  9042. BlendMode.activeBlendFunction = null;
  9043. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  9044. WebGLContext.setDepthTest(gl, false);
  9045. WebGLContext.setCullFace(gl, false);
  9046. WebGLContext.setDepthMask(gl, true);
  9047. WebGLContext.setFrontFace(gl, gl.CCW);
  9048. gl.viewport(0, 0, RenderState2D.width, RenderState2D.height);
  9049. }
  9050. clearBG(r, g, b, a) {
  9051. var gl = WebGLContext.mainContext;
  9052. gl.clearColor(r, g, b, a);
  9053. gl.clear(gl.COLOR_BUFFER_BIT);
  9054. }
  9055. _getSubmits() {
  9056. return this._submits;
  9057. }
  9058. _releaseMem(keepRT = false) {
  9059. if (!this._submits)
  9060. return;
  9061. this._curMat && this._curMat.destroy();
  9062. this._curMat = null;
  9063. this._shader2D.destroy();
  9064. this._shader2D = null;
  9065. this._charSubmitCache.clear();
  9066. for (var i = 0, n = this._submits._length; i < n; i++) {
  9067. this._submits[i].releaseRender();
  9068. }
  9069. this._submits.length = 0;
  9070. this._submits._length = 0;
  9071. this._submits = null;
  9072. this._curSubmit = null;
  9073. this._path = null;
  9074. this._save = null;
  9075. var sz;
  9076. for (i = 0, sz = this.meshlist.length; i < sz; i++) {
  9077. var curm = this.meshlist[i];
  9078. curm.destroy();
  9079. }
  9080. this.meshlist.length = 0;
  9081. this.sprite = null;
  9082. if (!keepRT) {
  9083. this._targets && (this._targets.destroy());
  9084. this._targets = null;
  9085. }
  9086. }
  9087. destroy(keepRT = false) {
  9088. --Context._contextcount;
  9089. this.sprite = null;
  9090. this._releaseMem(keepRT);
  9091. this._charSubmitCache && this._charSubmitCache.destroy();
  9092. this._mesh.destroy();
  9093. if (!keepRT) {
  9094. this._targets && this._targets.destroy();
  9095. this._targets = null;
  9096. }
  9097. }
  9098. clear() {
  9099. if (!this._submits) {
  9100. this._other = ContextParams.DEFAULT;
  9101. this._curMat = Matrix.create();
  9102. this._charSubmitCache = new CharSubmitCache();
  9103. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9104. this.meshlist.push(this._mesh);
  9105. this._pathMesh = MeshVG.getAMesh(this.isMain);
  9106. this.meshlist.push(this._pathMesh);
  9107. this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  9108. this.meshlist.push(this._triangleMesh);
  9109. this._submits = [];
  9110. this._save = [SaveMark.Create(this)];
  9111. this._save.length = 10;
  9112. this._shader2D = new Shader2D();
  9113. }
  9114. this._submitKey.clear();
  9115. this._mesh.clearVB();
  9116. this._drawCount = 1;
  9117. this._other = ContextParams.DEFAULT;
  9118. this._other.lineWidth = this._shader2D.ALPHA = 1.0;
  9119. this._nBlendType = 0;
  9120. this._clipRect = Context.MAXCLIPRECT;
  9121. this._curSubmit = SubmitBase.RENDERBASE;
  9122. SubmitBase.RENDERBASE._ref = 0xFFFFFF;
  9123. SubmitBase.RENDERBASE._numEle = 0;
  9124. this._shader2D.fillStyle = this._shader2D.strokeStyle = DrawStyle.DEFAULT;
  9125. for (var i = 0, n = this._submits._length; i < n; i++)
  9126. this._submits[i].releaseRender();
  9127. this._submits._length = 0;
  9128. this._curMat.identity();
  9129. this._other.clear();
  9130. this._saveMark = this._save[0];
  9131. this._save._length = 1;
  9132. }
  9133. size(w, h) {
  9134. if (this._width != w || this._height != h) {
  9135. this._width = w;
  9136. this._height = h;
  9137. if (this._targets) {
  9138. this._targets.destroy();
  9139. this._targets = new RenderTexture2D(w, h, exports.RenderTextureFormat.R8G8B8A8, -1);
  9140. }
  9141. if (this.isMain) {
  9142. WebGLContext.mainContext.viewport(0, 0, w, h);
  9143. RenderState2D.width = w;
  9144. RenderState2D.height = h;
  9145. }
  9146. }
  9147. if (w === 0 && h === 0)
  9148. this._releaseMem();
  9149. }
  9150. set asBitmap(value) {
  9151. if (value) {
  9152. let rt = this._targets;
  9153. if (!this._width || !this._height)
  9154. throw Error("asBitmap no size!");
  9155. if (!rt || rt.width != this._width || rt.height != this._height) {
  9156. if (rt) {
  9157. rt.destroy();
  9158. }
  9159. this._targets = new RenderTexture2D(this._width, this._height, exports.RenderTextureFormat.R8G8B8A8, -1);
  9160. }
  9161. }
  9162. else {
  9163. this._targets && this._targets.destroy();
  9164. this._targets = null;
  9165. }
  9166. }
  9167. getMatScaleX() {
  9168. if (this._lastMat_a == this._curMat.a && this._lastMat_b == this._curMat.b)
  9169. return this._lastMatScaleX;
  9170. this._lastMatScaleX = this._curMat.getScaleX();
  9171. this._lastMat_a = this._curMat.a;
  9172. this._lastMat_b = this._curMat.b;
  9173. return this._lastMatScaleX;
  9174. }
  9175. getMatScaleY() {
  9176. if (this._lastMat_c == this._curMat.c && this._lastMat_d == this._curMat.d)
  9177. return this._lastMatScaleY;
  9178. this._lastMatScaleY = this._curMat.getScaleY();
  9179. this._lastMat_c = this._curMat.c;
  9180. this._lastMat_d = this._curMat.d;
  9181. return this._lastMatScaleY;
  9182. }
  9183. setFillColor(color) {
  9184. this._fillColor = color;
  9185. }
  9186. getFillColor() {
  9187. return this._fillColor;
  9188. }
  9189. set fillStyle(value) {
  9190. if (!this._shader2D.fillStyle.equal(value)) {
  9191. SaveBase.save(this, SaveBase.TYPE_FILESTYLE, this._shader2D, false);
  9192. this._shader2D.fillStyle = DrawStyle.create(value);
  9193. this._submitKey.other = -this._shader2D.fillStyle.toInt();
  9194. }
  9195. }
  9196. get fillStyle() {
  9197. return this._shader2D.fillStyle;
  9198. }
  9199. set globalAlpha(value) {
  9200. value = Math.floor(value * 1000) / 1000;
  9201. if (value != this._shader2D.ALPHA) {
  9202. SaveBase.save(this, SaveBase.TYPE_ALPHA, this._shader2D, false);
  9203. this._shader2D.ALPHA = value;
  9204. }
  9205. }
  9206. get globalAlpha() {
  9207. return this._shader2D.ALPHA;
  9208. }
  9209. set textAlign(value) {
  9210. (this._other.textAlign === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_TEXTALIGN, this._other, false), this._other.textAlign = value);
  9211. }
  9212. get textAlign() {
  9213. return this._other.textAlign;
  9214. }
  9215. set textBaseline(value) {
  9216. (this._other.textBaseline === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_TEXTBASELINE, this._other, false), this._other.textBaseline = value);
  9217. }
  9218. get textBaseline() {
  9219. return this._other.textBaseline;
  9220. }
  9221. set globalCompositeOperation(value) {
  9222. var n = BlendMode.TOINT[value];
  9223. n == null || (this._nBlendType === n) || (SaveBase.save(this, SaveBase.TYPE_GLOBALCOMPOSITEOPERATION, this, true), this._curSubmit = SubmitBase.RENDERBASE, this._nBlendType = n);
  9224. }
  9225. get globalCompositeOperation() {
  9226. return BlendMode.NAMES[this._nBlendType];
  9227. }
  9228. set strokeStyle(value) {
  9229. this._shader2D.strokeStyle.equal(value) || (SaveBase.save(this, SaveBase.TYPE_STROKESTYLE, this._shader2D, false), this._shader2D.strokeStyle = DrawStyle.create(value), this._submitKey.other = -this._shader2D.strokeStyle.toInt());
  9230. }
  9231. get strokeStyle() {
  9232. return this._shader2D.strokeStyle;
  9233. }
  9234. translate(x, y) {
  9235. if (x !== 0 || y !== 0) {
  9236. SaveTranslate.save(this);
  9237. if (this._curMat._bTransform) {
  9238. SaveTransform.save(this);
  9239. this._curMat.tx += (x * this._curMat.a + y * this._curMat.c);
  9240. this._curMat.ty += (x * this._curMat.b + y * this._curMat.d);
  9241. }
  9242. else {
  9243. this._curMat.tx = x;
  9244. this._curMat.ty = y;
  9245. }
  9246. }
  9247. }
  9248. set lineWidth(value) {
  9249. (this._other.lineWidth === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_LINEWIDTH, this._other, false), this._other.lineWidth = value);
  9250. }
  9251. get lineWidth() {
  9252. return this._other.lineWidth;
  9253. }
  9254. save() {
  9255. this._save[this._save._length++] = SaveMark.Create(this);
  9256. }
  9257. restore() {
  9258. var sz = this._save._length;
  9259. var lastBlend = this._nBlendType;
  9260. if (sz < 1)
  9261. return;
  9262. for (var i = sz - 1; i >= 0; i--) {
  9263. var o = this._save[i];
  9264. o.restore(this);
  9265. if (o.isSaveMark()) {
  9266. this._save._length = i;
  9267. return;
  9268. }
  9269. }
  9270. if (lastBlend != this._nBlendType) {
  9271. this._curSubmit = SubmitBase.RENDERBASE;
  9272. }
  9273. }
  9274. set font(str) {
  9275. this._other = this._other.make();
  9276. SaveBase.save(this, SaveBase.TYPE_FONT, this._other, false);
  9277. }
  9278. fillText(txt, x, y, fontStr, color, align, lineWidth = 0, borderColor = "") {
  9279. Context._textRender.filltext(this, txt, x, y, fontStr, color, borderColor, lineWidth, align);
  9280. }
  9281. drawText(text, x, y, font, color, textAlign) {
  9282. Context._textRender.filltext(this, text, x, y, font, color, null, 0, textAlign);
  9283. }
  9284. fillWords(words, x, y, fontStr, color) {
  9285. Context._textRender.fillWords(this, words, x, y, fontStr, color, null, 0);
  9286. }
  9287. strokeWord(text, x, y, font, color, lineWidth, textAlign) {
  9288. Context._textRender.filltext(this, text, x, y, font, null, color, lineWidth, textAlign);
  9289. }
  9290. fillBorderText(txt, x, y, font, color, borderColor, lineWidth, textAlign) {
  9291. Context._textRender.filltext(this, txt, x, y, font, color, borderColor, lineWidth, textAlign);
  9292. }
  9293. fillBorderWords(words, x, y, font, color, borderColor, lineWidth) {
  9294. Context._textRender.fillWords(this, words, x, y, font, color, borderColor, lineWidth);
  9295. }
  9296. _fast_filltext(data, x, y, fontObj, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  9297. Context._textRender._fast_filltext(this, data, null, x, y, fontObj, color, strokeColor, lineWidth, textAlign, underLine);
  9298. }
  9299. _fillRect(x, y, width, height, rgba) {
  9300. var submit = this._curSubmit;
  9301. var sameKey = submit && (submit._key.submitType === SubmitBase.KEY_DRAWTEXTURE && submit._key.blendShader === this._nBlendType);
  9302. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  9303. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9304. this.meshlist.push(this._mesh);
  9305. sameKey = false;
  9306. }
  9307. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  9308. this.transformQuad(x, y, width, height, 0, this._curMat, this._transedPoints);
  9309. if (!this.clipedOff(this._transedPoints)) {
  9310. this._mesh.addQuad(this._transedPoints, Texture.NO_UV, rgba, false);
  9311. if (!sameKey) {
  9312. submit = this._curSubmit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9313. this._submits[this._submits._length++] = submit;
  9314. this._copyClipInfo(submit, this._globalClipMatrix);
  9315. submit.shaderValue.textureHost = this._lastTex;
  9316. submit._key.other = (this._lastTex && this._lastTex.bitmap) ? this._lastTex.bitmap.id : -1;
  9317. submit._renderType = SubmitBase.TYPE_TEXTURE;
  9318. }
  9319. this._curSubmit._numEle += 6;
  9320. this._mesh.indexNum += 6;
  9321. this._mesh.vertNum += 4;
  9322. }
  9323. }
  9324. fillRect(x, y, width, height, fillStyle) {
  9325. var drawstyle = fillStyle ? DrawStyle.create(fillStyle) : this._shader2D.fillStyle;
  9326. var rgba = this.mixRGBandAlpha(drawstyle.toInt());
  9327. this._fillRect(x, y, width, height, rgba);
  9328. }
  9329. fillTexture(texture, x, y, width, height, type, offset, other) {
  9330. if (!texture._getSource()) {
  9331. this.sprite && ILaya.systemTimer.callLater(this, this._repaintSprite);
  9332. return;
  9333. }
  9334. this._fillTexture(texture, texture.width, texture.height, texture.uvrect, x, y, width, height, type, offset.x, offset.y);
  9335. }
  9336. _fillTexture(texture, texw, texh, texuvRect, x, y, width, height, type, offsetx, offsety) {
  9337. var submit = this._curSubmit;
  9338. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  9339. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9340. this.meshlist.push(this._mesh);
  9341. }
  9342. var repeatx = true;
  9343. var repeaty = true;
  9344. switch (type) {
  9345. case "repeat": break;
  9346. case "repeat-x":
  9347. repeaty = false;
  9348. break;
  9349. case "repeat-y":
  9350. repeatx = false;
  9351. break;
  9352. case "no-repeat":
  9353. repeatx = repeaty = false;
  9354. break;
  9355. default: break;
  9356. }
  9357. var uv = this._temp4Points;
  9358. var stu = 0;
  9359. var stv = 0;
  9360. var stx = 0, sty = 0, edx = 0, edy = 0;
  9361. if (offsetx < 0) {
  9362. stx = x;
  9363. stu = (-offsetx % texw) / texw;
  9364. }
  9365. else {
  9366. stx = x + offsetx;
  9367. }
  9368. if (offsety < 0) {
  9369. sty = y;
  9370. stv = (-offsety % texh) / texh;
  9371. }
  9372. else {
  9373. sty = y + offsety;
  9374. }
  9375. edx = x + width;
  9376. edy = y + height;
  9377. (!repeatx) && (edx = Math.min(edx, x + offsetx + texw));
  9378. (!repeaty) && (edy = Math.min(edy, y + offsety + texh));
  9379. if (edx < x || edy < y)
  9380. return;
  9381. if (stx > edx || sty > edy)
  9382. return;
  9383. var edu = (edx - x - offsetx) / texw;
  9384. var edv = (edy - y - offsety) / texh;
  9385. this.transformQuad(stx, sty, edx - stx, edy - sty, 0, this._curMat, this._transedPoints);
  9386. uv[0] = stu;
  9387. uv[1] = stv;
  9388. uv[2] = edu;
  9389. uv[3] = stv;
  9390. uv[4] = edu;
  9391. uv[5] = edv;
  9392. uv[6] = stu;
  9393. uv[7] = edv;
  9394. if (!this.clipedOff(this._transedPoints)) {
  9395. var rgba = this._mixRGBandAlpha(0xffffffff, this._shader2D.ALPHA);
  9396. this._mesh.addQuad(this._transedPoints, uv, rgba, true);
  9397. var sv = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  9398. sv.defines.add(ShaderDefines2D.FILLTEXTURE);
  9399. sv.u_TexRange = texuvRect.concat();
  9400. submit = this._curSubmit = SubmitTexture.create(this, this._mesh, sv);
  9401. this._submits[this._submits._length++] = submit;
  9402. this._copyClipInfo(submit, this._globalClipMatrix);
  9403. submit.shaderValue.textureHost = texture;
  9404. submit._renderType = SubmitBase.TYPE_TEXTURE;
  9405. this._curSubmit._numEle += 6;
  9406. this._mesh.indexNum += 6;
  9407. this._mesh.vertNum += 4;
  9408. }
  9409. this.breakNextMerge();
  9410. }
  9411. setColorFilter(filter) {
  9412. SaveBase.save(this, SaveBase.TYPE_COLORFILTER, this, true);
  9413. this._colorFiler = filter;
  9414. this._curSubmit = SubmitBase.RENDERBASE;
  9415. }
  9416. drawTexture(tex, x, y, width, height) {
  9417. this._drawTextureM(tex, x, y, width, height, null, 1, null);
  9418. }
  9419. drawTextures(tex, pos, tx, ty) {
  9420. if (!tex._getSource()) {
  9421. this.sprite && ILaya.systemTimer.callLater(this, this._repaintSprite);
  9422. return;
  9423. }
  9424. var n = pos.length / 2;
  9425. var ipos = 0;
  9426. var bmpid = tex.bitmap.id;
  9427. for (var i = 0; i < n; i++) {
  9428. this._inner_drawTexture(tex, bmpid, pos[ipos++] + tx, pos[ipos++] + ty, 0, 0, null, null, 1.0, false);
  9429. }
  9430. }
  9431. _drawTextureAddSubmit(imgid, tex) {
  9432. var submit = null;
  9433. submit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9434. this._submits[this._submits._length++] = submit;
  9435. submit.shaderValue.textureHost = tex;
  9436. submit._key.other = imgid;
  9437. submit._renderType = SubmitBase.TYPE_TEXTURE;
  9438. this._curSubmit = submit;
  9439. }
  9440. _drawTextureM(tex, x, y, width, height, m, alpha, uv) {
  9441. var cs = this.sprite;
  9442. if (!tex._getSource(function () {
  9443. if (cs) {
  9444. cs.repaint();
  9445. }
  9446. })) {
  9447. return false;
  9448. }
  9449. return this._inner_drawTexture(tex, tex.bitmap.id, x, y, width, height, m, uv, alpha, false);
  9450. }
  9451. _drawRenderTexture(tex, x, y, width, height, m, alpha, uv) {
  9452. return this._inner_drawTexture(tex, -1, x, y, width, height, m, uv, 1.0, false);
  9453. }
  9454. submitDebugger() {
  9455. this._submits[this._submits._length++] = SubmitCMD.create([], function () { debugger; }, this);
  9456. }
  9457. _copyClipInfo(submit, clipInfo) {
  9458. var cm = submit.shaderValue.clipMatDir;
  9459. cm[0] = clipInfo.a;
  9460. cm[1] = clipInfo.b;
  9461. cm[2] = clipInfo.c;
  9462. cm[3] = clipInfo.d;
  9463. var cmp = submit.shaderValue.clipMatPos;
  9464. cmp[0] = clipInfo.tx;
  9465. cmp[1] = clipInfo.ty;
  9466. submit.clipInfoID = this._clipInfoID;
  9467. if (this._clipInCache) {
  9468. submit.shaderValue.clipOff[0] = 1;
  9469. }
  9470. }
  9471. isSameClipInfo(submit) {
  9472. return (submit.clipInfoID === this._clipInfoID);
  9473. }
  9474. _useNewTex2DSubmit(tex, minVertNum) {
  9475. if (this._mesh.vertNum + minVertNum > Context._MAXVERTNUM) {
  9476. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9477. this.meshlist.push(this._mesh);
  9478. }
  9479. var submit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9480. this._submits[this._submits._length++] = this._curSubmit = submit;
  9481. submit.shaderValue.textureHost = tex;
  9482. this._copyClipInfo(submit, this._globalClipMatrix);
  9483. }
  9484. _drawTexRect(x, y, w, h, uv) {
  9485. this.transformQuad(x, y, w, h, this._italicDeg, this._curMat, this._transedPoints);
  9486. var ops = this._transedPoints;
  9487. ops[0] = (ops[0] + 0.5) | 0;
  9488. ops[1] = (ops[1] + 0.5) | 0;
  9489. ops[2] = (ops[2] + 0.5) | 0;
  9490. ops[3] = (ops[3] + 0.5) | 0;
  9491. ops[4] = (ops[4] + 0.5) | 0;
  9492. ops[5] = (ops[5] + 0.5) | 0;
  9493. ops[6] = (ops[6] + 0.5) | 0;
  9494. ops[7] = (ops[7] + 0.5) | 0;
  9495. if (!this.clipedOff(this._transedPoints)) {
  9496. this._mesh.addQuad(this._transedPoints, uv, this._fillColor, true);
  9497. this._curSubmit._numEle += 6;
  9498. this._mesh.indexNum += 6;
  9499. this._mesh.vertNum += 4;
  9500. }
  9501. }
  9502. drawCallOptimize(enable) {
  9503. this._charSubmitCache.enable(enable, this);
  9504. return enable;
  9505. }
  9506. _inner_drawTexture(tex, imgid, x, y, width, height, m, uv, alpha, lastRender) {
  9507. if (width <= 0 || height <= 0) {
  9508. return false;
  9509. }
  9510. var preKey = this._curSubmit._key;
  9511. uv = uv || tex._uv;
  9512. if (preKey.submitType === SubmitBase.KEY_TRIANGLES && preKey.other === imgid) {
  9513. var tv = this._drawTexToDrawTri_Vert;
  9514. tv[0] = x;
  9515. tv[1] = y;
  9516. tv[2] = x + width, tv[3] = y, tv[4] = x + width, tv[5] = y + height, tv[6] = x, tv[7] = y + height;
  9517. this._drawTriUseAbsMatrix = true;
  9518. var tuv = this._tempUV;
  9519. tuv[0] = uv[0];
  9520. tuv[1] = uv[1];
  9521. tuv[2] = uv[2];
  9522. tuv[3] = uv[3];
  9523. tuv[4] = uv[4];
  9524. tuv[5] = uv[5];
  9525. tuv[6] = uv[6];
  9526. tuv[7] = uv[7];
  9527. this.drawTriangles(tex, 0, 0, tv, tuv, this._drawTexToDrawTri_Index, m, alpha, null, null);
  9528. this._drawTriUseAbsMatrix = false;
  9529. return true;
  9530. }
  9531. var mesh = this._mesh;
  9532. var submit = this._curSubmit;
  9533. var ops = lastRender ? this._charSubmitCache.getPos() : this._transedPoints;
  9534. this.transformQuad(x, y, width || tex.width, height || tex.height, this._italicDeg, m || this._curMat, ops);
  9535. if (this.drawTexAlign) {
  9536. var round = Math.round;
  9537. ops[0] = round(ops[0]);
  9538. ops[1] = round(ops[1]);
  9539. ops[2] = round(ops[2]);
  9540. ops[3] = round(ops[3]);
  9541. ops[4] = round(ops[4]);
  9542. ops[5] = round(ops[5]);
  9543. ops[6] = round(ops[6]);
  9544. ops[7] = round(ops[7]);
  9545. this.drawTexAlign = false;
  9546. }
  9547. var rgba = this._mixRGBandAlpha(0xffffffff, this._shader2D.ALPHA * alpha);
  9548. if (lastRender) {
  9549. this._charSubmitCache.add(this, tex, imgid, ops, uv, rgba);
  9550. return true;
  9551. }
  9552. this._drawCount++;
  9553. var sameKey = imgid >= 0 && preKey.submitType === SubmitBase.KEY_DRAWTEXTURE && preKey.other === imgid;
  9554. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  9555. this._lastTex = tex;
  9556. if (mesh.vertNum + 4 > Context._MAXVERTNUM) {
  9557. mesh = this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9558. this.meshlist.push(mesh);
  9559. sameKey = false;
  9560. }
  9561. {
  9562. mesh.addQuad(ops, uv, rgba, true);
  9563. if (!sameKey) {
  9564. this._submits[this._submits._length++] = this._curSubmit = submit = SubmitTexture.create(this, mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9565. submit.shaderValue.textureHost = tex;
  9566. submit._key.other = imgid;
  9567. this._copyClipInfo(submit, this._globalClipMatrix);
  9568. }
  9569. submit._numEle += 6;
  9570. mesh.indexNum += 6;
  9571. mesh.vertNum += 4;
  9572. return true;
  9573. }
  9574. return false;
  9575. }
  9576. transform4Points(a, m, out) {
  9577. var tx = m.tx;
  9578. var ty = m.ty;
  9579. var ma = m.a;
  9580. var mb = m.b;
  9581. var mc = m.c;
  9582. var md = m.d;
  9583. var a0 = a[0];
  9584. var a1 = a[1];
  9585. var a2 = a[2];
  9586. var a3 = a[3];
  9587. var a4 = a[4];
  9588. var a5 = a[5];
  9589. var a6 = a[6];
  9590. var a7 = a[7];
  9591. if (m._bTransform) {
  9592. out[0] = a0 * ma + a1 * mc + tx;
  9593. out[1] = a0 * mb + a1 * md + ty;
  9594. out[2] = a2 * ma + a3 * mc + tx;
  9595. out[3] = a2 * mb + a3 * md + ty;
  9596. out[4] = a4 * ma + a5 * mc + tx;
  9597. out[5] = a4 * mb + a5 * md + ty;
  9598. out[6] = a6 * ma + a7 * mc + tx;
  9599. out[7] = a6 * mb + a7 * md + ty;
  9600. }
  9601. else {
  9602. out[0] = a0 + tx;
  9603. out[1] = a1 + ty;
  9604. out[2] = a2 + tx;
  9605. out[3] = a3 + ty;
  9606. out[4] = a4 + tx;
  9607. out[5] = a5 + ty;
  9608. out[6] = a6 + tx;
  9609. out[7] = a7 + ty;
  9610. }
  9611. }
  9612. clipedOff(pt) {
  9613. if (this._clipRect.width <= 0 || this._clipRect.height <= 0)
  9614. return true;
  9615. return false;
  9616. }
  9617. transformQuad(x, y, w, h, italicDeg, m, out) {
  9618. var xoff = 0;
  9619. if (italicDeg != 0) {
  9620. xoff = Math.tan(italicDeg * Math.PI / 180) * h;
  9621. }
  9622. var maxx = x + w;
  9623. var maxy = y + h;
  9624. var tx = m.tx;
  9625. var ty = m.ty;
  9626. var ma = m.a;
  9627. var mb = m.b;
  9628. var mc = m.c;
  9629. var md = m.d;
  9630. var a0 = x + xoff;
  9631. var a1 = y;
  9632. var a2 = maxx + xoff;
  9633. var a3 = y;
  9634. var a4 = maxx;
  9635. var a5 = maxy;
  9636. var a6 = x;
  9637. var a7 = maxy;
  9638. if (m._bTransform) {
  9639. out[0] = a0 * ma + a1 * mc + tx;
  9640. out[1] = a0 * mb + a1 * md + ty;
  9641. out[2] = a2 * ma + a3 * mc + tx;
  9642. out[3] = a2 * mb + a3 * md + ty;
  9643. out[4] = a4 * ma + a5 * mc + tx;
  9644. out[5] = a4 * mb + a5 * md + ty;
  9645. out[6] = a6 * ma + a7 * mc + tx;
  9646. out[7] = a6 * mb + a7 * md + ty;
  9647. }
  9648. else {
  9649. out[0] = a0 + tx;
  9650. out[1] = a1 + ty;
  9651. out[2] = a2 + tx;
  9652. out[3] = a3 + ty;
  9653. out[4] = a4 + tx;
  9654. out[5] = a5 + ty;
  9655. out[6] = a6 + tx;
  9656. out[7] = a7 + ty;
  9657. }
  9658. }
  9659. pushRT() {
  9660. this.addRenderObject(SubmitCMD.create(null, RenderTexture2D.pushRT, this));
  9661. }
  9662. popRT() {
  9663. this.addRenderObject(SubmitCMD.create(null, RenderTexture2D.popRT, this));
  9664. this.breakNextMerge();
  9665. }
  9666. useRT(rt) {
  9667. function _use(rt) {
  9668. if (!rt) {
  9669. throw 'error useRT';
  9670. }
  9671. else {
  9672. rt.start();
  9673. rt.clear(0, 0, 0, 0);
  9674. }
  9675. }
  9676. this.addRenderObject(SubmitCMD.create([rt], _use, this));
  9677. this.breakNextMerge();
  9678. }
  9679. RTRestore(rt) {
  9680. function _restore(rt) {
  9681. rt.restore();
  9682. }
  9683. this.addRenderObject(SubmitCMD.create([rt], _restore, this));
  9684. this.breakNextMerge();
  9685. }
  9686. breakNextMerge() {
  9687. this._curSubmit = SubmitBase.RENDERBASE;
  9688. }
  9689. _repaintSprite() {
  9690. this.sprite && this.sprite.repaint();
  9691. }
  9692. drawTextureWithTransform(tex, x, y, width, height, transform, tx, ty, alpha, blendMode, colorfilter = null, uv) {
  9693. var oldcomp;
  9694. var curMat = this._curMat;
  9695. if (blendMode) {
  9696. oldcomp = this.globalCompositeOperation;
  9697. this.globalCompositeOperation = blendMode;
  9698. }
  9699. var oldColorFilter = this._colorFiler;
  9700. if (colorfilter) {
  9701. this.setColorFilter(colorfilter);
  9702. }
  9703. if (!transform) {
  9704. this._drawTextureM(tex, x + tx, y + ty, width, height, curMat, alpha, uv);
  9705. if (blendMode) {
  9706. this.globalCompositeOperation = oldcomp;
  9707. }
  9708. if (colorfilter) {
  9709. this.setColorFilter(oldColorFilter);
  9710. }
  9711. return;
  9712. }
  9713. var tmpMat = this._tmpMatrix;
  9714. tmpMat.a = transform.a;
  9715. tmpMat.b = transform.b;
  9716. tmpMat.c = transform.c;
  9717. tmpMat.d = transform.d;
  9718. tmpMat.tx = transform.tx + tx;
  9719. tmpMat.ty = transform.ty + ty;
  9720. tmpMat._bTransform = transform._bTransform;
  9721. if (transform && curMat._bTransform) {
  9722. Matrix.mul(tmpMat, curMat, tmpMat);
  9723. transform = tmpMat;
  9724. transform._bTransform = true;
  9725. }
  9726. else {
  9727. tmpMat.tx += curMat.tx;
  9728. tmpMat.ty += curMat.ty;
  9729. transform = tmpMat;
  9730. }
  9731. this._drawTextureM(tex, x, y, width, height, transform, alpha, uv);
  9732. if (blendMode) {
  9733. this.globalCompositeOperation = oldcomp;
  9734. }
  9735. if (colorfilter) {
  9736. this.setColorFilter(oldColorFilter);
  9737. }
  9738. }
  9739. _flushToTarget(context, target) {
  9740. RenderState2D.worldScissorTest = false;
  9741. var gl = LayaGL.instance;
  9742. gl.disable(gl.SCISSOR_TEST);
  9743. var preAlpha = RenderState2D.worldAlpha;
  9744. var preMatrix4 = RenderState2D.worldMatrix4;
  9745. var preMatrix = RenderState2D.worldMatrix;
  9746. RenderState2D.worldMatrix = Matrix.EMPTY;
  9747. RenderState2D.restoreTempArray();
  9748. RenderState2D.worldMatrix4 = RenderState2D.TEMPMAT4_ARRAY;
  9749. RenderState2D.worldAlpha = 1;
  9750. BaseShader.activeShader = null;
  9751. target.start();
  9752. if (context._submits._length > 0)
  9753. target.clear(0, 0, 0, 0);
  9754. context._curSubmit = SubmitBase.RENDERBASE;
  9755. context.flush();
  9756. context.clear();
  9757. target.restore();
  9758. context._curSubmit = SubmitBase.RENDERBASE;
  9759. BaseShader.activeShader = null;
  9760. RenderState2D.worldAlpha = preAlpha;
  9761. RenderState2D.worldMatrix4 = preMatrix4;
  9762. RenderState2D.worldMatrix = preMatrix;
  9763. }
  9764. drawCanvas(canvas, x, y, width, height) {
  9765. if (!canvas)
  9766. return;
  9767. var src = canvas.context;
  9768. var submit;
  9769. if (src._targets) {
  9770. if (src._submits._length > 0) {
  9771. submit = SubmitCMD.create([src, src._targets], this._flushToTarget, this);
  9772. this._submits[this._submits._length++] = submit;
  9773. }
  9774. this._drawRenderTexture(src._targets, x, y, width, height, null, 1.0, RenderTexture2D.flipyuv);
  9775. this._curSubmit = SubmitBase.RENDERBASE;
  9776. }
  9777. else {
  9778. var canv = canvas;
  9779. if (canv.touches) {
  9780. canv.touches.forEach(function (v) { v.touch(); });
  9781. }
  9782. submit = SubmitCanvas.create(canvas, this._shader2D.ALPHA, this._shader2D.filters);
  9783. this._submits[this._submits._length++] = submit;
  9784. submit._key.clear();
  9785. var mat = submit._matrix;
  9786. this._curMat.copyTo(mat);
  9787. var tx = mat.tx, ty = mat.ty;
  9788. mat.tx = mat.ty = 0;
  9789. mat.transformPoint(Point.TEMP.setTo(x, y));
  9790. mat.translate(Point.TEMP.x + tx, Point.TEMP.y + ty);
  9791. Matrix.mul(canv.invMat, mat, mat);
  9792. this._curSubmit = SubmitBase.RENDERBASE;
  9793. }
  9794. }
  9795. drawTarget(rt, x, y, width, height, m, shaderValue, uv = null, blend = -1) {
  9796. this._drawCount++;
  9797. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  9798. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9799. this.meshlist.push(this._mesh);
  9800. }
  9801. this.transformQuad(x, y, width, height, 0, m || this._curMat, this._transedPoints);
  9802. if (!this.clipedOff(this._transedPoints)) {
  9803. this._mesh.addQuad(this._transedPoints, uv || Texture.DEF_UV, 0xffffffff, true);
  9804. var submit = this._curSubmit = SubmitTarget.create(this, this._mesh, shaderValue, rt);
  9805. submit.blendType = (blend == -1) ? this._nBlendType : blend;
  9806. this._copyClipInfo(submit, this._globalClipMatrix);
  9807. submit._numEle = 6;
  9808. this._mesh.indexNum += 6;
  9809. this._mesh.vertNum += 4;
  9810. this._submits[this._submits._length++] = submit;
  9811. this._curSubmit = SubmitBase.RENDERBASE;
  9812. return true;
  9813. }
  9814. this._curSubmit = SubmitBase.RENDERBASE;
  9815. return false;
  9816. }
  9817. drawTriangles(tex, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum = 0xffffffff) {
  9818. if (!tex._getSource()) {
  9819. if (this.sprite) {
  9820. ILaya.systemTimer.callLater(this, this._repaintSprite);
  9821. }
  9822. return;
  9823. }
  9824. var oldcomp = null;
  9825. if (blendMode) {
  9826. oldcomp = this.globalCompositeOperation;
  9827. this.globalCompositeOperation = blendMode;
  9828. }
  9829. this._drawCount++;
  9830. var tmpMat = this._tmpMatrix;
  9831. var triMesh = this._triangleMesh;
  9832. var oldColorFilter = null;
  9833. var needRestorFilter = false;
  9834. if (color) {
  9835. oldColorFilter = this._colorFiler;
  9836. this._colorFiler = color;
  9837. this._curSubmit = SubmitBase.RENDERBASE;
  9838. needRestorFilter = oldColorFilter != color;
  9839. }
  9840. var webGLImg = tex.bitmap;
  9841. var preKey = this._curSubmit._key;
  9842. var sameKey = preKey.submitType === SubmitBase.KEY_TRIANGLES && preKey.other === webGLImg.id && preKey.blendShader == this._nBlendType;
  9843. if (triMesh.vertNum + vertices.length / 2 > Context._MAXVERTNUM) {
  9844. triMesh = this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  9845. this.meshlist.push(triMesh);
  9846. sameKey = false;
  9847. }
  9848. if (!sameKey) {
  9849. var submit = this._curSubmit = SubmitTexture.create(this, triMesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9850. submit.shaderValue.textureHost = tex;
  9851. submit._renderType = SubmitBase.TYPE_TEXTURE;
  9852. submit._key.submitType = SubmitBase.KEY_TRIANGLES;
  9853. submit._key.other = webGLImg.id;
  9854. this._copyClipInfo(submit, this._globalClipMatrix);
  9855. this._submits[this._submits._length++] = submit;
  9856. }
  9857. var rgba = this._mixRGBandAlpha(colorNum, this._shader2D.ALPHA * alpha);
  9858. if (!this._drawTriUseAbsMatrix) {
  9859. if (!matrix) {
  9860. tmpMat.a = 1;
  9861. tmpMat.b = 0;
  9862. tmpMat.c = 0;
  9863. tmpMat.d = 1;
  9864. tmpMat.tx = x;
  9865. tmpMat.ty = y;
  9866. }
  9867. else {
  9868. tmpMat.a = matrix.a;
  9869. tmpMat.b = matrix.b;
  9870. tmpMat.c = matrix.c;
  9871. tmpMat.d = matrix.d;
  9872. tmpMat.tx = matrix.tx + x;
  9873. tmpMat.ty = matrix.ty + y;
  9874. }
  9875. Matrix.mul(tmpMat, this._curMat, tmpMat);
  9876. triMesh.addData(vertices, uvs, indices, tmpMat || this._curMat, rgba);
  9877. }
  9878. else {
  9879. triMesh.addData(vertices, uvs, indices, matrix, rgba);
  9880. }
  9881. this._curSubmit._numEle += indices.length;
  9882. if (needRestorFilter) {
  9883. this._colorFiler = oldColorFilter;
  9884. this._curSubmit = SubmitBase.RENDERBASE;
  9885. }
  9886. if (blendMode) {
  9887. this.globalCompositeOperation = oldcomp;
  9888. }
  9889. }
  9890. transform(a, b, c, d, tx, ty) {
  9891. SaveTransform.save(this);
  9892. Matrix.mul(Matrix.TEMP.setTo(a, b, c, d, tx, ty), this._curMat, this._curMat);
  9893. this._curMat._checkTransform();
  9894. }
  9895. _transformByMatrix(matrix, tx, ty) {
  9896. matrix.setTranslate(tx, ty);
  9897. Matrix.mul(matrix, this._curMat, this._curMat);
  9898. matrix.setTranslate(0, 0);
  9899. this._curMat._bTransform = true;
  9900. }
  9901. setTransformByMatrix(value) {
  9902. value.copyTo(this._curMat);
  9903. }
  9904. rotate(angle) {
  9905. SaveTransform.save(this);
  9906. this._curMat.rotateEx(angle);
  9907. }
  9908. scale(scaleX, scaleY) {
  9909. SaveTransform.save(this);
  9910. this._curMat.scaleEx(scaleX, scaleY);
  9911. }
  9912. clipRect(x, y, width, height) {
  9913. SaveClipRect.save(this);
  9914. if (this._clipRect == Context.MAXCLIPRECT) {
  9915. this._clipRect = new Rectangle(x, y, width, height);
  9916. }
  9917. else {
  9918. this._clipRect.width = width;
  9919. this._clipRect.height = height;
  9920. this._clipRect.x = x;
  9921. this._clipRect.y = y;
  9922. }
  9923. this._clipID_Gen++;
  9924. this._clipID_Gen %= 10000;
  9925. this._clipInfoID = this._clipID_Gen;
  9926. var cm = this._globalClipMatrix;
  9927. var minx = cm.tx;
  9928. var miny = cm.ty;
  9929. var maxx = minx + cm.a;
  9930. var maxy = miny + cm.d;
  9931. if (this._clipRect.width >= Context._MAXSIZE) {
  9932. cm.a = cm.d = Context._MAXSIZE;
  9933. cm.b = cm.c = cm.tx = cm.ty = 0;
  9934. }
  9935. else {
  9936. if (this._curMat._bTransform) {
  9937. cm.tx = this._clipRect.x * this._curMat.a + this._clipRect.y * this._curMat.c + this._curMat.tx;
  9938. cm.ty = this._clipRect.x * this._curMat.b + this._clipRect.y * this._curMat.d + this._curMat.ty;
  9939. cm.a = this._clipRect.width * this._curMat.a;
  9940. cm.b = this._clipRect.width * this._curMat.b;
  9941. cm.c = this._clipRect.height * this._curMat.c;
  9942. cm.d = this._clipRect.height * this._curMat.d;
  9943. }
  9944. else {
  9945. cm.tx = this._clipRect.x + this._curMat.tx;
  9946. cm.ty = this._clipRect.y + this._curMat.ty;
  9947. cm.a = this._clipRect.width;
  9948. cm.b = cm.c = 0;
  9949. cm.d = this._clipRect.height;
  9950. }
  9951. if (this._incache) {
  9952. this._clipInCache = true;
  9953. }
  9954. }
  9955. if (cm.a > 0 && cm.d > 0) {
  9956. var cmaxx = cm.tx + cm.a;
  9957. var cmaxy = cm.ty + cm.d;
  9958. if (cmaxx <= minx || cmaxy <= miny || cm.tx >= maxx || cm.ty >= maxy) {
  9959. cm.a = -0.1;
  9960. cm.d = -0.1;
  9961. }
  9962. else {
  9963. if (cm.tx < minx) {
  9964. cm.a -= (minx - cm.tx);
  9965. cm.tx = minx;
  9966. }
  9967. if (cmaxx > maxx) {
  9968. cm.a -= (cmaxx - maxx);
  9969. }
  9970. if (cm.ty < miny) {
  9971. cm.d -= (miny - cm.ty);
  9972. cm.ty = miny;
  9973. }
  9974. if (cmaxy > maxy) {
  9975. cm.d -= (cmaxy - maxy);
  9976. }
  9977. if (cm.a <= 0)
  9978. cm.a = -0.1;
  9979. if (cm.d <= 0)
  9980. cm.d = -0.1;
  9981. }
  9982. }
  9983. }
  9984. drawMesh(x, y, ib, vb, numElement, mat, shader, shaderValues, startIndex = 0) {
  9985. }
  9986. addRenderObject(o) {
  9987. this._submits[this._submits._length++] = o;
  9988. }
  9989. submitElement(start, end) {
  9990. var mainCtx = this.isMain;
  9991. var renderList = this._submits;
  9992. var ret = renderList._length;
  9993. end < 0 && (end = renderList._length);
  9994. var submit = SubmitBase.RENDERBASE;
  9995. while (start < end) {
  9996. this._renderNextSubmitIndex = start + 1;
  9997. if (renderList[start] === SubmitBase.RENDERBASE) {
  9998. start++;
  9999. continue;
  10000. }
  10001. SubmitBase.preRender = submit;
  10002. submit = renderList[start];
  10003. start += submit.renderSubmit();
  10004. }
  10005. return ret;
  10006. }
  10007. flush() {
  10008. this._clipID_Gen = 0;
  10009. var ret = this.submitElement(0, this._submits._length);
  10010. this._path && this._path.reset();
  10011. SkinMeshBuffer.instance && SkinMeshBuffer.getInstance().reset();
  10012. this._curSubmit = SubmitBase.RENDERBASE;
  10013. for (var i = 0, sz = this.meshlist.length; i < sz; i++) {
  10014. var curm = this.meshlist[i];
  10015. curm.canReuse ? (curm.releaseMesh()) : (curm.destroy());
  10016. }
  10017. this.meshlist.length = 0;
  10018. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  10019. this._pathMesh = MeshVG.getAMesh(this.isMain);
  10020. this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  10021. this.meshlist.push(this._mesh, this._pathMesh, this._triangleMesh);
  10022. this._flushCnt++;
  10023. if (this._flushCnt % 60 == 0 && this.isMain) {
  10024. if (TextRender.textRenderInst) {
  10025. TextRender.textRenderInst.GC();
  10026. }
  10027. }
  10028. return ret;
  10029. }
  10030. beginPath(convex = false) {
  10031. var tPath = this._getPath();
  10032. tPath.beginPath(convex);
  10033. }
  10034. closePath() {
  10035. this._path.closePath();
  10036. }
  10037. addPath(points, close, convex, dx, dy) {
  10038. var ci = 0;
  10039. for (var i = 0, sz = points.length / 2; i < sz; i++) {
  10040. var x1 = points[ci] + dx, y1 = points[ci + 1] + dy;
  10041. points[ci] = x1;
  10042. points[ci + 1] = y1;
  10043. ci += 2;
  10044. }
  10045. this._getPath().push(points, convex);
  10046. }
  10047. fill() {
  10048. var m = this._curMat;
  10049. var tPath = this._getPath();
  10050. var submit = this._curSubmit;
  10051. var sameKey = (submit._key.submitType === SubmitBase.KEY_VG && submit._key.blendShader === this._nBlendType);
  10052. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  10053. if (!sameKey) {
  10054. this._curSubmit = this.addVGSubmit(this._pathMesh);
  10055. }
  10056. var rgba = this.mixRGBandAlpha(this.fillStyle.toInt());
  10057. var curEleNum = 0;
  10058. var idx;
  10059. for (var i = 0, sz = tPath.paths.length; i < sz; i++) {
  10060. var p = tPath.paths[i];
  10061. var vertNum = p.path.length / 2;
  10062. if (vertNum < 3 || (vertNum == 3 && !p.convex))
  10063. continue;
  10064. var cpath = p.path.concat();
  10065. var pi = 0;
  10066. var xp, yp;
  10067. var _x, _y;
  10068. if (m._bTransform) {
  10069. for (pi = 0; pi < vertNum; pi++) {
  10070. xp = pi << 1;
  10071. yp = xp + 1;
  10072. _x = cpath[xp];
  10073. _y = cpath[yp];
  10074. cpath[xp] = m.a * _x + m.c * _y + m.tx;
  10075. cpath[yp] = m.b * _x + m.d * _y + m.ty;
  10076. }
  10077. }
  10078. else {
  10079. for (pi = 0; pi < vertNum; pi++) {
  10080. xp = pi << 1;
  10081. yp = xp + 1;
  10082. _x = cpath[xp];
  10083. _y = cpath[yp];
  10084. cpath[xp] = _x + m.tx;
  10085. cpath[yp] = _y + m.ty;
  10086. }
  10087. }
  10088. if (this._pathMesh.vertNum + vertNum > Context._MAXVERTNUM) {
  10089. this._curSubmit._numEle += curEleNum;
  10090. curEleNum = 0;
  10091. this._pathMesh = MeshVG.getAMesh(this.isMain);
  10092. this._curSubmit = this.addVGSubmit(this._pathMesh);
  10093. }
  10094. var curvert = this._pathMesh.vertNum;
  10095. if (p.convex) {
  10096. var faceNum = vertNum - 2;
  10097. idx = new Array(faceNum * 3);
  10098. var idxpos = 0;
  10099. for (var fi = 0; fi < faceNum; fi++) {
  10100. idx[idxpos++] = curvert;
  10101. idx[idxpos++] = fi + 1 + curvert;
  10102. idx[idxpos++] = fi + 2 + curvert;
  10103. }
  10104. }
  10105. else {
  10106. idx = Earcut.earcut(cpath, null, 2);
  10107. if (curvert > 0) {
  10108. for (var ii = 0; ii < idx.length; ii++) {
  10109. idx[ii] += curvert;
  10110. }
  10111. }
  10112. }
  10113. this._pathMesh.addVertAndIBToMesh(this, cpath, rgba, idx);
  10114. curEleNum += idx.length;
  10115. }
  10116. this._curSubmit._numEle += curEleNum;
  10117. }
  10118. addVGSubmit(mesh) {
  10119. var submit = Submit.createShape(this, mesh, 0, Value2D.create(ShaderDefines2D.PRIMITIVE, 0));
  10120. submit._key.submitType = SubmitBase.KEY_VG;
  10121. this._submits[this._submits._length++] = submit;
  10122. this._copyClipInfo(submit, this._globalClipMatrix);
  10123. return submit;
  10124. }
  10125. stroke() {
  10126. if (this.lineWidth > 0) {
  10127. var rgba = this.mixRGBandAlpha(this.strokeStyle._color.numColor);
  10128. var tPath = this._getPath();
  10129. var submit = this._curSubmit;
  10130. var sameKey = (submit._key.submitType === SubmitBase.KEY_VG && submit._key.blendShader === this._nBlendType);
  10131. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  10132. if (!sameKey) {
  10133. this._curSubmit = this.addVGSubmit(this._pathMesh);
  10134. }
  10135. var curEleNum = 0;
  10136. for (var i = 0, sz = tPath.paths.length; i < sz; i++) {
  10137. var p = tPath.paths[i];
  10138. if (p.path.length <= 0)
  10139. continue;
  10140. var idx = [];
  10141. var vertex = [];
  10142. var maxVertexNum = p.path.length * 2;
  10143. if (maxVertexNum < 2)
  10144. continue;
  10145. if (this._pathMesh.vertNum + maxVertexNum > Context._MAXVERTNUM) {
  10146. this._curSubmit._numEle += curEleNum;
  10147. curEleNum = 0;
  10148. this._pathMesh = MeshVG.getAMesh(this.isMain);
  10149. this.meshlist.push(this._pathMesh);
  10150. this._curSubmit = this.addVGSubmit(this._pathMesh);
  10151. }
  10152. BasePoly.createLine2(p.path, idx, this.lineWidth, this._pathMesh.vertNum, vertex, p.loop);
  10153. var ptnum = vertex.length / 2;
  10154. var m = this._curMat;
  10155. var pi = 0;
  10156. var xp, yp;
  10157. var _x, _y;
  10158. if (m._bTransform) {
  10159. for (pi = 0; pi < ptnum; pi++) {
  10160. xp = pi << 1;
  10161. yp = xp + 1;
  10162. _x = vertex[xp];
  10163. _y = vertex[yp];
  10164. vertex[xp] = m.a * _x + m.c * _y + m.tx;
  10165. vertex[yp] = m.b * _x + m.d * _y + m.ty;
  10166. }
  10167. }
  10168. else {
  10169. for (pi = 0; pi < ptnum; pi++) {
  10170. xp = pi << 1;
  10171. yp = xp + 1;
  10172. _x = vertex[xp];
  10173. _y = vertex[yp];
  10174. vertex[xp] = _x + m.tx;
  10175. vertex[yp] = _y + m.ty;
  10176. }
  10177. }
  10178. this._pathMesh.addVertAndIBToMesh(this, vertex, rgba, idx);
  10179. curEleNum += idx.length;
  10180. }
  10181. this._curSubmit._numEle += curEleNum;
  10182. }
  10183. }
  10184. moveTo(x, y) {
  10185. var tPath = this._getPath();
  10186. tPath.newPath();
  10187. tPath._lastOriX = x;
  10188. tPath._lastOriY = y;
  10189. tPath.addPoint(x, y);
  10190. }
  10191. lineTo(x, y) {
  10192. var tPath = this._getPath();
  10193. if (Math.abs(x - tPath._lastOriX) < 1e-3 && Math.abs(y - tPath._lastOriY) < 1e-3)
  10194. return;
  10195. tPath._lastOriX = x;
  10196. tPath._lastOriY = y;
  10197. tPath.addPoint(x, y);
  10198. }
  10199. arcTo(x1, y1, x2, y2, r) {
  10200. var i = 0;
  10201. var x = 0, y = 0;
  10202. var dx = this._path._lastOriX - x1;
  10203. var dy = this._path._lastOriY - y1;
  10204. var len1 = Math.sqrt(dx * dx + dy * dy);
  10205. if (len1 <= 0.000001) {
  10206. return;
  10207. }
  10208. var ndx = dx / len1;
  10209. var ndy = dy / len1;
  10210. var dx2 = x2 - x1;
  10211. var dy2 = y2 - y1;
  10212. var len22 = dx2 * dx2 + dy2 * dy2;
  10213. var len2 = Math.sqrt(len22);
  10214. if (len2 <= 0.000001) {
  10215. return;
  10216. }
  10217. var ndx2 = dx2 / len2;
  10218. var ndy2 = dy2 / len2;
  10219. var odx = ndx + ndx2;
  10220. var ody = ndy + ndy2;
  10221. var olen = Math.sqrt(odx * odx + ody * ody);
  10222. if (olen <= 0.000001) {
  10223. return;
  10224. }
  10225. var nOdx = odx / olen;
  10226. var nOdy = ody / olen;
  10227. var alpha = Math.acos(nOdx * ndx + nOdy * ndy);
  10228. var halfAng = Math.PI / 2 - alpha;
  10229. len1 = r / Math.tan(halfAng);
  10230. var ptx1 = len1 * ndx + x1;
  10231. var pty1 = len1 * ndy + y1;
  10232. var orilen = Math.sqrt(len1 * len1 + r * r);
  10233. var orix = x1 + nOdx * orilen;
  10234. var oriy = y1 + nOdy * orilen;
  10235. var dir = ndx * ndy2 - ndy * ndx2;
  10236. var fChgAng = 0;
  10237. var sinx = 0.0;
  10238. var cosx = 0.0;
  10239. if (dir >= 0) {
  10240. fChgAng = halfAng * 2;
  10241. var fda = fChgAng / Context.SEGNUM;
  10242. sinx = Math.sin(fda);
  10243. cosx = Math.cos(fda);
  10244. }
  10245. else {
  10246. fChgAng = -halfAng * 2;
  10247. fda = fChgAng / Context.SEGNUM;
  10248. sinx = Math.sin(fda);
  10249. cosx = Math.cos(fda);
  10250. }
  10251. var lastx = this._path._lastOriX, lasty = this._path._lastOriY;
  10252. var _x1 = ptx1, _y1 = pty1;
  10253. if (Math.abs(_x1 - this._path._lastOriX) > 0.1 || Math.abs(_y1 - this._path._lastOriY) > 0.1) {
  10254. x = _x1;
  10255. y = _y1;
  10256. lastx = _x1;
  10257. lasty = _y1;
  10258. this._path._lastOriX = x;
  10259. this._path._lastOriY = y;
  10260. this._path.addPoint(x, y);
  10261. }
  10262. var cvx = ptx1 - orix;
  10263. var cvy = pty1 - oriy;
  10264. for (i = 0; i < Context.SEGNUM; i++) {
  10265. var cx = cvx * cosx + cvy * sinx;
  10266. var cy = -cvx * sinx + cvy * cosx;
  10267. x = cx + orix;
  10268. y = cy + oriy;
  10269. if (Math.abs(lastx - x) > 0.1 || Math.abs(lasty - y) > 0.1) {
  10270. this._path._lastOriX = x;
  10271. this._path._lastOriY = y;
  10272. this._path.addPoint(x, y);
  10273. lastx = x;
  10274. lasty = y;
  10275. }
  10276. cvx = cx;
  10277. cvy = cy;
  10278. }
  10279. }
  10280. arc(cx, cy, r, startAngle, endAngle, counterclockwise = false, b = true) {
  10281. var a = 0, da = 0;
  10282. var dx = 0, dy = 0, x = 0, y = 0;
  10283. var i, ndivs;
  10284. da = endAngle - startAngle;
  10285. if (!counterclockwise) {
  10286. if (Math.abs(da) >= Math.PI * 2) {
  10287. da = Math.PI * 2;
  10288. }
  10289. else {
  10290. while (da < 0.0) {
  10291. da += Math.PI * 2;
  10292. }
  10293. }
  10294. }
  10295. else {
  10296. if (Math.abs(da) >= Math.PI * 2) {
  10297. da = -Math.PI * 2;
  10298. }
  10299. else {
  10300. while (da > 0.0) {
  10301. da -= Math.PI * 2;
  10302. }
  10303. }
  10304. }
  10305. var sx = this.getMatScaleX();
  10306. var sy = this.getMatScaleY();
  10307. var sr = r * (sx > sy ? sx : sy);
  10308. var cl = 2 * Math.PI * sr;
  10309. ndivs = (Math.max(cl / 10, 10)) | 0;
  10310. var tPath = this._getPath();
  10311. for (i = 0; i <= ndivs; i++) {
  10312. a = startAngle + da * (i / ndivs);
  10313. dx = Math.cos(a);
  10314. dy = Math.sin(a);
  10315. x = cx + dx * r;
  10316. y = cy + dy * r;
  10317. if (x != this._path._lastOriX || y != this._path._lastOriY) {
  10318. tPath.addPoint(x, y);
  10319. }
  10320. }
  10321. dx = Math.cos(endAngle);
  10322. dy = Math.sin(endAngle);
  10323. x = cx + dx * r;
  10324. y = cy + dy * r;
  10325. if (x != this._path._lastOriX || y != this._path._lastOriY) {
  10326. tPath.addPoint(x, y);
  10327. }
  10328. }
  10329. quadraticCurveTo(cpx, cpy, x, y) {
  10330. var tBezier = Bezier.I;
  10331. var tArray = tBezier.getBezierPoints([this._path._lastOriX, this._path._lastOriY, cpx, cpy, x, y], 30, 2);
  10332. for (var i = 0, n = tArray.length / 2; i < n; i++) {
  10333. this.lineTo(tArray[i * 2], tArray[i * 2 + 1]);
  10334. }
  10335. this.lineTo(x, y);
  10336. }
  10337. mixRGBandAlpha(color) {
  10338. return this._mixRGBandAlpha(color, this._shader2D.ALPHA);
  10339. }
  10340. _mixRGBandAlpha(color, alpha) {
  10341. if (alpha >= 1) {
  10342. return color;
  10343. }
  10344. var a = ((color & 0xff000000) >>> 24);
  10345. if (a != 0) {
  10346. a *= alpha;
  10347. }
  10348. else {
  10349. a = alpha * 255;
  10350. }
  10351. return (color & 0x00ffffff) | (a << 24);
  10352. }
  10353. strokeRect(x, y, width, height, parameterLineWidth) {
  10354. if (this.lineWidth > 0) {
  10355. var rgba = this.mixRGBandAlpha(this.strokeStyle._color.numColor);
  10356. var hw = this.lineWidth / 2;
  10357. this._fillRect(x - hw, y - hw, width + this.lineWidth, this.lineWidth, rgba);
  10358. this._fillRect(x - hw, y - hw + height, width + this.lineWidth, this.lineWidth, rgba);
  10359. this._fillRect(x - hw, y + hw, this.lineWidth, height - this.lineWidth, rgba);
  10360. this._fillRect(x - hw + width, y + hw, this.lineWidth, height - this.lineWidth, rgba);
  10361. }
  10362. }
  10363. clip() {
  10364. }
  10365. drawParticle(x, y, pt) {
  10366. pt.x = x;
  10367. pt.y = y;
  10368. this._submits[this._submits._length++] = pt;
  10369. }
  10370. _getPath() {
  10371. return this._path || (this._path = new Path());
  10372. }
  10373. get canvas() {
  10374. return this._canvas;
  10375. }
  10376. _fillTexture_h(tex, imgid, uv, oriw, orih, x, y, w) {
  10377. if (oriw <= 0)
  10378. console.error('_fillTexture_h error: oriw must>0');
  10379. var stx = x;
  10380. var num = Math.floor(w / oriw);
  10381. var left = w % oriw;
  10382. for (var i = 0; i < num; i++) {
  10383. this._inner_drawTexture(tex, imgid, stx, y, oriw, orih, this._curMat, uv, 1, false);
  10384. stx += oriw;
  10385. }
  10386. if (left > 0) {
  10387. var du = uv[2] - uv[0];
  10388. var uvr = uv[0] + du * (left / oriw);
  10389. var tuv = Context.tmpuv1;
  10390. tuv[0] = uv[0];
  10391. tuv[1] = uv[1];
  10392. tuv[2] = uvr;
  10393. tuv[3] = uv[3];
  10394. tuv[4] = uvr;
  10395. tuv[5] = uv[5];
  10396. tuv[6] = uv[6];
  10397. tuv[7] = uv[7];
  10398. this._inner_drawTexture(tex, imgid, stx, y, left, orih, this._curMat, tuv, 1, false);
  10399. }
  10400. }
  10401. _fillTexture_v(tex, imgid, uv, oriw, orih, x, y, h) {
  10402. if (orih <= 0)
  10403. console.error('_fillTexture_v error: orih must>0');
  10404. var sty = y;
  10405. var num = Math.floor(h / orih);
  10406. var left = h % orih;
  10407. for (var i = 0; i < num; i++) {
  10408. this._inner_drawTexture(tex, imgid, x, sty, oriw, orih, this._curMat, uv, 1, false);
  10409. sty += orih;
  10410. }
  10411. if (left > 0) {
  10412. var dv = uv[7] - uv[1];
  10413. var uvb = uv[1] + dv * (left / orih);
  10414. var tuv = Context.tmpuv1;
  10415. tuv[0] = uv[0];
  10416. tuv[1] = uv[1];
  10417. tuv[2] = uv[2];
  10418. tuv[3] = uv[3];
  10419. tuv[4] = uv[4];
  10420. tuv[5] = uvb;
  10421. tuv[6] = uv[6];
  10422. tuv[7] = uvb;
  10423. this._inner_drawTexture(tex, imgid, x, sty, oriw, left, this._curMat, tuv, 1, false);
  10424. }
  10425. }
  10426. drawTextureWithSizeGrid(tex, tx, ty, width, height, sizeGrid, gx, gy) {
  10427. if (!tex._getSource())
  10428. return;
  10429. tx += gx;
  10430. ty += gy;
  10431. var uv = tex.uv, w = tex.bitmap.width, h = tex.bitmap.height;
  10432. var top = sizeGrid[0];
  10433. var left = sizeGrid[3];
  10434. var right = sizeGrid[1];
  10435. var bottom = sizeGrid[2];
  10436. var repeat = sizeGrid[4];
  10437. var needClip = false;
  10438. if (width == w) {
  10439. left = right = 0;
  10440. }
  10441. if (height == h) {
  10442. top = bottom = 0;
  10443. }
  10444. var d_top = top / h;
  10445. var d_left = left / w;
  10446. var d_right = right / w;
  10447. var d_bottom = bottom / h;
  10448. if (left + right > width) {
  10449. var clipWidth = width;
  10450. needClip = true;
  10451. width = left + right;
  10452. this.save();
  10453. this.clipRect(0 + tx, 0 + ty, clipWidth, height);
  10454. }
  10455. var imgid = tex.bitmap.id;
  10456. var mat = this._curMat;
  10457. var tuv = this._tempUV;
  10458. var uvl = uv[0];
  10459. var uvt = uv[1];
  10460. var uvr = uv[4];
  10461. var uvb = uv[5];
  10462. var uvl_ = uvl;
  10463. var uvt_ = uvt;
  10464. var uvr_ = uvr;
  10465. var uvb_ = uvb;
  10466. if (left && top) {
  10467. uvr_ = uvl + d_left;
  10468. uvb_ = uvt + d_top;
  10469. tuv[0] = uvl, tuv[1] = uvt, tuv[2] = uvr_, tuv[3] = uvt,
  10470. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl, tuv[7] = uvb_;
  10471. this._inner_drawTexture(tex, imgid, tx, ty, left, top, mat, tuv, 1, false);
  10472. }
  10473. if (right && top) {
  10474. uvl_ = uvr - d_right;
  10475. uvt_ = uvt;
  10476. uvr_ = uvr;
  10477. uvb_ = uvt + d_top;
  10478. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10479. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10480. this._inner_drawTexture(tex, imgid, width - right + tx, 0 + ty, right, top, mat, tuv, 1, false);
  10481. }
  10482. if (left && bottom) {
  10483. uvl_ = uvl;
  10484. uvt_ = uvb - d_bottom;
  10485. uvr_ = uvl + d_left;
  10486. uvb_ = uvb;
  10487. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10488. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10489. this._inner_drawTexture(tex, imgid, 0 + tx, height - bottom + ty, left, bottom, mat, tuv, 1, false);
  10490. }
  10491. if (right && bottom) {
  10492. uvl_ = uvr - d_right;
  10493. uvt_ = uvb - d_bottom;
  10494. uvr_ = uvr;
  10495. uvb_ = uvb;
  10496. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10497. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10498. this._inner_drawTexture(tex, imgid, width - right + tx, height - bottom + ty, right, bottom, mat, tuv, 1, false);
  10499. }
  10500. if (top) {
  10501. uvl_ = uvl + d_left;
  10502. uvt_ = uvt;
  10503. uvr_ = uvr - d_right;
  10504. uvb_ = uvt + d_top;
  10505. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10506. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10507. if (repeat) {
  10508. this._fillTexture_h(tex, imgid, tuv, tex.width - left - right, top, left + tx, ty, width - left - right);
  10509. }
  10510. else {
  10511. this._inner_drawTexture(tex, imgid, left + tx, ty, width - left - right, top, mat, tuv, 1, false);
  10512. }
  10513. }
  10514. if (bottom) {
  10515. uvl_ = uvl + d_left;
  10516. uvt_ = uvb - d_bottom;
  10517. uvr_ = uvr - d_right;
  10518. uvb_ = uvb;
  10519. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10520. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10521. if (repeat) {
  10522. this._fillTexture_h(tex, imgid, tuv, tex.width - left - right, bottom, left + tx, height - bottom + ty, width - left - right);
  10523. }
  10524. else {
  10525. this._inner_drawTexture(tex, imgid, left + tx, height - bottom + ty, width - left - right, bottom, mat, tuv, 1, false);
  10526. }
  10527. }
  10528. if (left) {
  10529. uvl_ = uvl;
  10530. uvt_ = uvt + d_top;
  10531. uvr_ = uvl + d_left;
  10532. uvb_ = uvb - d_bottom;
  10533. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10534. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10535. if (repeat) {
  10536. this._fillTexture_v(tex, imgid, tuv, left, tex.height - top - bottom, tx, top + ty, height - top - bottom);
  10537. }
  10538. else {
  10539. this._inner_drawTexture(tex, imgid, tx, top + ty, left, height - top - bottom, mat, tuv, 1, false);
  10540. }
  10541. }
  10542. if (right) {
  10543. uvl_ = uvr - d_right;
  10544. uvt_ = uvt + d_top;
  10545. uvr_ = uvr;
  10546. uvb_ = uvb - d_bottom;
  10547. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10548. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10549. if (repeat) {
  10550. this._fillTexture_v(tex, imgid, tuv, right, tex.height - top - bottom, width - right + tx, top + ty, height - top - bottom);
  10551. }
  10552. else {
  10553. this._inner_drawTexture(tex, imgid, width - right + tx, top + ty, right, height - top - bottom, mat, tuv, 1, false);
  10554. }
  10555. }
  10556. uvl_ = uvl + d_left;
  10557. uvt_ = uvt + d_top;
  10558. uvr_ = uvr - d_right;
  10559. uvb_ = uvb - d_bottom;
  10560. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  10561. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  10562. if (repeat) {
  10563. var tuvr = Context.tmpUVRect;
  10564. tuvr[0] = uvl_;
  10565. tuvr[1] = uvt_;
  10566. tuvr[2] = uvr_ - uvl_;
  10567. tuvr[3] = uvb_ - uvt_;
  10568. this._fillTexture(tex, tex.width - left - right, tex.height - top - bottom, tuvr, left + tx, top + ty, width - left - right, height - top - bottom, 'repeat', 0, 0);
  10569. }
  10570. else {
  10571. this._inner_drawTexture(tex, imgid, left + tx, top + ty, width - left - right, height - top - bottom, mat, tuv, 1, false);
  10572. }
  10573. if (needClip)
  10574. this.restore();
  10575. }
  10576. }
  10577. Context.ENUM_TEXTALIGN_DEFAULT = 0;
  10578. Context.ENUM_TEXTALIGN_CENTER = 1;
  10579. Context.ENUM_TEXTALIGN_RIGHT = 2;
  10580. Context._SUBMITVBSIZE = 32000;
  10581. Context._MAXSIZE = 99999999;
  10582. Context._MAXVERTNUM = 65535;
  10583. Context.MAXCLIPRECT = null;
  10584. Context._COUNT = 0;
  10585. Context.SEGNUM = 32;
  10586. Context._contextcount = 0;
  10587. Context.PI2 = 2 * Math.PI;
  10588. Context._textRender = null;
  10589. Context.tmpuv1 = [0, 0, 0, 0, 0, 0, 0, 0];
  10590. Context.tmpUV = [0, 0, 0, 0, 0, 0, 0, 0];
  10591. Context.tmpUVRect = [0, 0, 0, 0];
  10592. class ContextParams {
  10593. constructor() {
  10594. this.lineWidth = 1;
  10595. }
  10596. clear() {
  10597. this.lineWidth = 1;
  10598. this.textAlign = this.textBaseline = null;
  10599. }
  10600. make() {
  10601. return this === ContextParams.DEFAULT ? new ContextParams() : this;
  10602. }
  10603. }
  10604. class WebGL {
  10605. static _uint8ArraySlice() {
  10606. var _this = this;
  10607. var sz = _this.length;
  10608. var dec = new Uint8Array(_this.length);
  10609. for (var i = 0; i < sz; i++)
  10610. dec[i] = _this[i];
  10611. return dec;
  10612. }
  10613. static _float32ArraySlice() {
  10614. var _this = this;
  10615. var sz = _this.length;
  10616. var dec = new Float32Array(_this.length);
  10617. for (var i = 0; i < sz; i++)
  10618. dec[i] = _this[i];
  10619. return dec;
  10620. }
  10621. static _uint16ArraySlice(...arg) {
  10622. var _this = this;
  10623. var sz;
  10624. var dec;
  10625. var i;
  10626. if (arg.length === 0) {
  10627. sz = _this.length;
  10628. dec = new Uint16Array(sz);
  10629. for (i = 0; i < sz; i++)
  10630. dec[i] = _this[i];
  10631. }
  10632. else if (arg.length === 2) {
  10633. var start = arg[0];
  10634. var end = arg[1];
  10635. if (end > start) {
  10636. sz = end - start;
  10637. dec = new Uint16Array(sz);
  10638. for (i = start; i < end; i++)
  10639. dec[i - start] = _this[i];
  10640. }
  10641. else {
  10642. dec = new Uint16Array(0);
  10643. }
  10644. }
  10645. return dec;
  10646. }
  10647. static _nativeRender_enable() {
  10648. }
  10649. static enable() {
  10650. return true;
  10651. }
  10652. static inner_enable() {
  10653. Float32Array.prototype.slice || (Float32Array.prototype.slice = WebGL._float32ArraySlice);
  10654. Uint16Array.prototype.slice || (Uint16Array.prototype.slice = WebGL._uint16ArraySlice);
  10655. Uint8Array.prototype.slice || (Uint8Array.prototype.slice = WebGL._uint8ArraySlice);
  10656. return true;
  10657. }
  10658. static onStageResize(width, height) {
  10659. if (WebGLContext.mainContext == null)
  10660. return;
  10661. WebGLContext.mainContext.viewport(0, 0, width, height);
  10662. RenderState2D.width = width;
  10663. RenderState2D.height = height;
  10664. }
  10665. }
  10666. WebGL._isWebGL2 = false;
  10667. WebGL.isNativeRender_enable = false;
  10668. class VertexArrayObject {
  10669. constructor() {
  10670. }
  10671. }
  10672. (function () {
  10673. var glErrorShadow = {};
  10674. function error(msg) {
  10675. if (window.console && window.console.error) {
  10676. window.console.error(msg);
  10677. }
  10678. }
  10679. function log(msg) {
  10680. if (window.console && window.console.log) {
  10681. window.console.log(msg);
  10682. }
  10683. }
  10684. function synthesizeGLError(err, opt_msg) {
  10685. glErrorShadow[err] = true;
  10686. if (opt_msg !== undefined) {
  10687. error(opt_msg);
  10688. }
  10689. }
  10690. function wrapGLError(gl) {
  10691. var f = gl.getError;
  10692. gl.getError = function () {
  10693. var err;
  10694. do {
  10695. err = f.apply(gl);
  10696. if (err != gl.NO_ERROR) {
  10697. glErrorShadow[err] = true;
  10698. }
  10699. } while (err != gl.NO_ERROR);
  10700. for (var err1 in glErrorShadow) {
  10701. if (glErrorShadow[err1]) {
  10702. delete glErrorShadow[err1];
  10703. return parseInt(err1);
  10704. }
  10705. }
  10706. return gl.NO_ERROR;
  10707. };
  10708. }
  10709. var WebGLVertexArrayObjectOES = function WebGLVertexArrayObjectOES(ext) {
  10710. var gl = ext.gl;
  10711. this.ext = ext;
  10712. this.isAlive = true;
  10713. this.hasBeenBound = false;
  10714. this.elementArrayBuffer = null;
  10715. this.attribs = new Array(ext.maxVertexAttribs);
  10716. for (var n = 0; n < this.attribs.length; n++) {
  10717. var attrib = new WebGLVertexArrayObjectOES.VertexAttrib(gl);
  10718. this.attribs[n] = attrib;
  10719. }
  10720. this.maxAttrib = 0;
  10721. };
  10722. WebGLVertexArrayObjectOES.VertexAttrib = function VertexAttrib(gl) {
  10723. this.enabled = false;
  10724. this.buffer = null;
  10725. this.size = 4;
  10726. this.type = gl.FLOAT;
  10727. this.normalized = false;
  10728. this.stride = 16;
  10729. this.offset = 0;
  10730. this.cached = "";
  10731. this.recache();
  10732. };
  10733. WebGLVertexArrayObjectOES.VertexAttrib.prototype.recache = function recache() {
  10734. this.cached = [this.size, this.type, this.normalized, this.stride, this.offset].join(":");
  10735. };
  10736. var OESVertexArrayObject = function OESVertexArrayObject(gl) {
  10737. var self = this;
  10738. this.gl = gl;
  10739. wrapGLError(gl);
  10740. var original = this.original = {
  10741. getParameter: gl.getParameter,
  10742. enableVertexAttribArray: gl.enableVertexAttribArray,
  10743. disableVertexAttribArray: gl.disableVertexAttribArray,
  10744. bindBuffer: gl.bindBuffer,
  10745. getVertexAttrib: gl.getVertexAttrib,
  10746. vertexAttribPointer: gl.vertexAttribPointer
  10747. };
  10748. gl.getParameter = function getParameter(pname) {
  10749. if (pname == self.VERTEX_ARRAY_BINDING_OES) {
  10750. if (self.currentVertexArrayObject == self.defaultVertexArrayObject) {
  10751. return null;
  10752. }
  10753. else {
  10754. return self.currentVertexArrayObject;
  10755. }
  10756. }
  10757. return original.getParameter.apply(this, arguments);
  10758. };
  10759. gl.enableVertexAttribArray = function enableVertexAttribArray(index) {
  10760. var vao = self.currentVertexArrayObject;
  10761. vao.maxAttrib = Math.max(vao.maxAttrib, index);
  10762. var attrib = vao.attribs[index];
  10763. attrib.enabled = true;
  10764. return original.enableVertexAttribArray.apply(this, arguments);
  10765. };
  10766. gl.disableVertexAttribArray = function disableVertexAttribArray(index) {
  10767. var vao = self.currentVertexArrayObject;
  10768. vao.maxAttrib = Math.max(vao.maxAttrib, index);
  10769. var attrib = vao.attribs[index];
  10770. attrib.enabled = false;
  10771. return original.disableVertexAttribArray.apply(this, arguments);
  10772. };
  10773. gl.bindBuffer = function bindBuffer(target, buffer) {
  10774. switch (target) {
  10775. case gl.ARRAY_BUFFER:
  10776. self.currentArrayBuffer = buffer;
  10777. break;
  10778. case gl.ELEMENT_ARRAY_BUFFER:
  10779. self.currentVertexArrayObject.elementArrayBuffer = buffer;
  10780. break;
  10781. }
  10782. return original.bindBuffer.apply(this, arguments);
  10783. };
  10784. gl.getVertexAttrib = function getVertexAttrib(index, pname) {
  10785. var vao = self.currentVertexArrayObject;
  10786. var attrib = vao.attribs[index];
  10787. switch (pname) {
  10788. case gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
  10789. return attrib.buffer;
  10790. case gl.VERTEX_ATTRIB_ARRAY_ENABLED:
  10791. return attrib.enabled;
  10792. case gl.VERTEX_ATTRIB_ARRAY_SIZE:
  10793. return attrib.size;
  10794. case gl.VERTEX_ATTRIB_ARRAY_STRIDE:
  10795. return attrib.stride;
  10796. case gl.VERTEX_ATTRIB_ARRAY_TYPE:
  10797. return attrib.type;
  10798. case gl.VERTEX_ATTRIB_ARRAY_NORMALIZED:
  10799. return attrib.normalized;
  10800. default:
  10801. return original.getVertexAttrib.apply(this, arguments);
  10802. }
  10803. };
  10804. gl.vertexAttribPointer = function vertexAttribPointer(indx, size, type, normalized, stride, offset) {
  10805. var vao = self.currentVertexArrayObject;
  10806. vao.maxAttrib = Math.max(vao.maxAttrib, indx);
  10807. var attrib = vao.attribs[indx];
  10808. attrib.buffer = self.currentArrayBuffer;
  10809. attrib.size = size;
  10810. attrib.type = type;
  10811. attrib.normalized = normalized;
  10812. attrib.stride = stride;
  10813. attrib.offset = offset;
  10814. attrib.recache();
  10815. return original.vertexAttribPointer.apply(this, arguments);
  10816. };
  10817. if (gl.instrumentExtension) {
  10818. gl.instrumentExtension(this, "OES_vertex_array_object");
  10819. }
  10820. gl.canvas.addEventListener('webglcontextrestored', function () {
  10821. log("OESVertexArrayObject emulation library context restored");
  10822. self.reset_();
  10823. }, true);
  10824. this.reset_();
  10825. };
  10826. OESVertexArrayObject.prototype.VERTEX_ARRAY_BINDING_OES = 0x85B5;
  10827. OESVertexArrayObject.prototype.reset_ = function reset_() {
  10828. var contextWasLost = this.vertexArrayObjects !== undefined;
  10829. if (contextWasLost) {
  10830. for (var ii = 0; ii < this.vertexArrayObjects.length; ++ii) {
  10831. this.vertexArrayObjects.isAlive = false;
  10832. }
  10833. }
  10834. var gl = this.gl;
  10835. this.maxVertexAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
  10836. this.defaultVertexArrayObject = new WebGLVertexArrayObjectOES(this);
  10837. this.currentVertexArrayObject = null;
  10838. this.currentArrayBuffer = null;
  10839. this.vertexArrayObjects = [this.defaultVertexArrayObject];
  10840. this.bindVertexArrayOES(null);
  10841. };
  10842. OESVertexArrayObject.prototype.createVertexArrayOES = function createVertexArrayOES() {
  10843. var arrayObject = new WebGLVertexArrayObjectOES(this);
  10844. this.vertexArrayObjects.push(arrayObject);
  10845. return arrayObject;
  10846. };
  10847. OESVertexArrayObject.prototype.deleteVertexArrayOES = function deleteVertexArrayOES(arrayObject) {
  10848. arrayObject.isAlive = false;
  10849. this.vertexArrayObjects.splice(this.vertexArrayObjects.indexOf(arrayObject), 1);
  10850. if (this.currentVertexArrayObject == arrayObject) {
  10851. this.bindVertexArrayOES(null);
  10852. }
  10853. };
  10854. OESVertexArrayObject.prototype.isVertexArrayOES = function isVertexArrayOES(arrayObject) {
  10855. if (arrayObject && arrayObject instanceof WebGLVertexArrayObjectOES) {
  10856. if (arrayObject.hasBeenBound && arrayObject.ext == this) {
  10857. return true;
  10858. }
  10859. }
  10860. return false;
  10861. };
  10862. OESVertexArrayObject.prototype.bindVertexArrayOES = function bindVertexArrayOES(arrayObject) {
  10863. var gl = this.gl;
  10864. if (arrayObject && !arrayObject.isAlive) {
  10865. synthesizeGLError(gl.INVALID_OPERATION, "bindVertexArrayOES: attempt to bind deleted arrayObject");
  10866. return;
  10867. }
  10868. var original = this.original;
  10869. var oldVAO = this.currentVertexArrayObject;
  10870. this.currentVertexArrayObject = arrayObject || this.defaultVertexArrayObject;
  10871. this.currentVertexArrayObject.hasBeenBound = true;
  10872. var newVAO = this.currentVertexArrayObject;
  10873. if (oldVAO == newVAO) {
  10874. return;
  10875. }
  10876. if (!oldVAO || newVAO.elementArrayBuffer != oldVAO.elementArrayBuffer) {
  10877. original.bindBuffer.call(gl, gl.ELEMENT_ARRAY_BUFFER, newVAO.elementArrayBuffer);
  10878. }
  10879. var currentBinding = this.currentArrayBuffer;
  10880. var maxAttrib = Math.max(oldVAO ? oldVAO.maxAttrib : 0, newVAO.maxAttrib);
  10881. for (var n = 0; n <= maxAttrib; n++) {
  10882. var attrib = newVAO.attribs[n];
  10883. var oldAttrib = oldVAO ? oldVAO.attribs[n] : null;
  10884. if (!oldVAO || attrib.enabled != oldAttrib.enabled) {
  10885. if (attrib.enabled) {
  10886. original.enableVertexAttribArray.call(gl, n);
  10887. }
  10888. else {
  10889. original.disableVertexAttribArray.call(gl, n);
  10890. }
  10891. }
  10892. if (attrib.enabled) {
  10893. var bufferChanged = false;
  10894. if (!oldVAO || attrib.buffer != oldAttrib.buffer) {
  10895. if (currentBinding != attrib.buffer) {
  10896. original.bindBuffer.call(gl, gl.ARRAY_BUFFER, attrib.buffer);
  10897. currentBinding = attrib.buffer;
  10898. }
  10899. bufferChanged = true;
  10900. }
  10901. if (bufferChanged || attrib.cached != oldAttrib.cached) {
  10902. original.vertexAttribPointer.call(gl, n, attrib.size, attrib.type, attrib.normalized, attrib.stride, attrib.offset);
  10903. }
  10904. }
  10905. }
  10906. if (this.currentArrayBuffer != currentBinding) {
  10907. original.bindBuffer.call(gl, gl.ARRAY_BUFFER, this.currentArrayBuffer);
  10908. }
  10909. };
  10910. window._setupVertexArrayObject = function (gl) {
  10911. var original_getSupportedExtensions = gl.getSupportedExtensions;
  10912. gl.getSupportedExtensions = function getSupportedExtensions() {
  10913. var list = original_getSupportedExtensions.call(this) || [];
  10914. if (list.indexOf("OES_vertex_array_object") < 0) {
  10915. list.push("OES_vertex_array_object");
  10916. }
  10917. return list;
  10918. };
  10919. var original_getExtension = gl.getExtension;
  10920. gl.getExtension = function getExtension(name) {
  10921. var ext = original_getExtension.call(this, name);
  10922. if (ext) {
  10923. return ext;
  10924. }
  10925. if (name !== "OES_vertex_array_object") {
  10926. return null;
  10927. }
  10928. if (!this.__OESVertexArrayObject) {
  10929. console.log("Setup OES_vertex_array_object polyfill");
  10930. this.__OESVertexArrayObject = new OESVertexArrayObject(this);
  10931. }
  10932. return this.__OESVertexArrayObject;
  10933. };
  10934. };
  10935. }());
  10936. class LayaGPU {
  10937. constructor(gl, isWebGL2) {
  10938. this._gl = null;
  10939. this._vaoExt = null;
  10940. this._angleInstancedArrays = null;
  10941. this._isWebGL2 = false;
  10942. this._oesTextureHalfFloat = null;
  10943. this._oes_element_index_uint = null;
  10944. this._oesTextureHalfFloatLinear = null;
  10945. this._oesTextureFloat = null;
  10946. this._extShaderTextureLod = null;
  10947. this._extTextureFilterAnisotropic = null;
  10948. this._compressedTextureS3tc = null;
  10949. this._compressedTexturePvrtc = null;
  10950. this._compressedTextureEtc1 = null;
  10951. this._webgl_depth_texture = null;
  10952. this._extColorBufferFloat = null;
  10953. this._gl = gl;
  10954. this._isWebGL2 = isWebGL2;
  10955. var maxTextureFS = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
  10956. var maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
  10957. if (!isWebGL2) {
  10958. if (!ILaya.Render.isConchApp) {
  10959. if (window._setupVertexArrayObject)
  10960. window._setupVertexArrayObject(gl);
  10961. }
  10962. this._vaoExt = this._getExtension("OES_vertex_array_object");
  10963. this._angleInstancedArrays = this._getExtension("ANGLE_instanced_arrays");
  10964. this._oesTextureHalfFloat = this._getExtension("OES_texture_half_float");
  10965. this._oesTextureHalfFloatLinear = this._getExtension("OES_texture_half_float_linear");
  10966. this._oesTextureFloat = this._getExtension("OES_texture_float");
  10967. this._oes_element_index_uint = this._getExtension("OES_element_index_uint");
  10968. this._extShaderTextureLod = this._getExtension("EXT_shader_texture_lod");
  10969. this._webgl_depth_texture = this._getExtension("WEBGL_depth_texture");
  10970. SystemUtils._shaderCapailityLevel = 30;
  10971. }
  10972. else {
  10973. this._extColorBufferFloat = this._getExtension("EXT_color_buffer_float");
  10974. SystemUtils._shaderCapailityLevel = 35;
  10975. }
  10976. this._extTextureFilterAnisotropic = this._getExtension("EXT_texture_filter_anisotropic");
  10977. this._compressedTextureS3tc = this._getExtension("WEBGL_compressed_texture_s3tc");
  10978. this._compressedTexturePvrtc = this._getExtension("WEBGL_compressed_texture_pvrtc");
  10979. this._compressedTextureEtc1 = this._getExtension("WEBGL_compressed_texture_etc1");
  10980. SystemUtils._maxTextureCount = maxTextureFS;
  10981. SystemUtils._maxTextureSize = maxTextureSize;
  10982. }
  10983. _getExtension(name) {
  10984. var prefixes = LayaGPU._extentionVendorPrefixes;
  10985. for (var k in prefixes) {
  10986. var ext = this._gl.getExtension(prefixes[k] + name);
  10987. if (ext)
  10988. return ext;
  10989. }
  10990. return null;
  10991. }
  10992. createVertexArray() {
  10993. if (this._isWebGL2)
  10994. return this._gl.createVertexArray();
  10995. else
  10996. return this._vaoExt.createVertexArrayOES();
  10997. }
  10998. bindVertexArray(vertexArray) {
  10999. if (this._isWebGL2)
  11000. this._gl.bindVertexArray(vertexArray);
  11001. else
  11002. this._vaoExt.bindVertexArrayOES(vertexArray);
  11003. }
  11004. deleteVertexArray(vertexArray) {
  11005. if (this._isWebGL2)
  11006. this._gl.deleteVertexArray(vertexArray);
  11007. else
  11008. this._vaoExt.deleteVertexArrayOES(vertexArray);
  11009. }
  11010. isVertexArray(vertexArray) {
  11011. if (this._isWebGL2)
  11012. this._gl.isVertexArray(vertexArray);
  11013. else
  11014. this._vaoExt.isVertexArrayOES(vertexArray);
  11015. }
  11016. drawElementsInstanced(mode, count, type, offset, instanceCount) {
  11017. if (this._isWebGL2)
  11018. this._gl.drawElementsInstanced(mode, count, type, offset, instanceCount);
  11019. else
  11020. this._angleInstancedArrays.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);
  11021. }
  11022. drawArraysInstanced(mode, first, count, instanceCount) {
  11023. if (this._isWebGL2)
  11024. this._gl.drawArraysInstanced(mode, first, count, instanceCount);
  11025. else
  11026. this._angleInstancedArrays.drawArraysInstancedANGLE(mode, first, count, instanceCount);
  11027. }
  11028. vertexAttribDivisor(index, divisor) {
  11029. if (this._isWebGL2)
  11030. this._gl.vertexAttribDivisor(index, divisor);
  11031. else
  11032. this._angleInstancedArrays.vertexAttribDivisorANGLE(index, divisor);
  11033. }
  11034. supportInstance() {
  11035. if ((this._isWebGL2 || this._angleInstancedArrays) && Config.allowGPUInstanceDynamicBatch)
  11036. return true;
  11037. else
  11038. return false;
  11039. }
  11040. supportElementIndexUint32() {
  11041. return this._isWebGL2 || this._oes_element_index_uint ? true : false;
  11042. }
  11043. }
  11044. LayaGPU._extentionVendorPrefixes = ["", "WEBKIT_", "MOZ_"];
  11045. class Render {
  11046. constructor(width, height, mainCanv) {
  11047. this._timeId = 0;
  11048. Render._mainCanvas = mainCanv;
  11049. let source = Render._mainCanvas.source;
  11050. source.id = "layaCanvas";
  11051. source.width = width;
  11052. source.height = height;
  11053. if (Render.isConchApp) {
  11054. document.body.appendChild(source);
  11055. }
  11056. this.initRender(Render._mainCanvas, width, height);
  11057. window.requestAnimationFrame(loop);
  11058. function loop(stamp) {
  11059. ILaya.stage._loop();
  11060. window.requestAnimationFrame(loop);
  11061. }
  11062. ILaya.stage.on("visibilitychange", this, this._onVisibilitychange);
  11063. }
  11064. _onVisibilitychange() {
  11065. if (!ILaya.stage.isVisibility) {
  11066. this._timeId = window.setInterval(this._enterFrame, 1000);
  11067. }
  11068. else if (this._timeId != 0) {
  11069. window.clearInterval(this._timeId);
  11070. }
  11071. }
  11072. initRender(canvas, w, h) {
  11073. function getWebGLContext(canvas) {
  11074. var gl;
  11075. var names = ["webgl2", "webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
  11076. if (!Config.useWebGL2 || Browser.onBDMiniGame) {
  11077. names.shift();
  11078. }
  11079. for (var i = 0; i < names.length; i++) {
  11080. try {
  11081. gl = canvas.getContext(names[i], { stencil: Config.isStencil, alpha: Config.isAlpha, antialias: Config.isAntialias, premultipliedAlpha: Config.premultipliedAlpha, preserveDrawingBuffer: Config.preserveDrawingBuffer });
  11082. }
  11083. catch (e) {
  11084. }
  11085. if (gl) {
  11086. (names[i] === 'webgl2') && (WebGL._isWebGL2 = true);
  11087. return gl;
  11088. }
  11089. }
  11090. return null;
  11091. }
  11092. var gl = LayaGL.instance = WebGLContext.mainContext = getWebGLContext(Render._mainCanvas.source);
  11093. if (!gl)
  11094. return false;
  11095. LayaGL.instance = gl;
  11096. LayaGL.layaGPUInstance = new LayaGPU(gl, WebGL._isWebGL2);
  11097. canvas.size(w, h);
  11098. Context.__init__();
  11099. SubmitBase.__init__();
  11100. var ctx = new Context();
  11101. ctx.isMain = true;
  11102. Render._context = ctx;
  11103. canvas._setContext(ctx);
  11104. ShaderDefines2D.__init__();
  11105. Value2D.__init__();
  11106. Shader2D.__init__();
  11107. Buffer2D.__int__(gl);
  11108. BlendMode._init_(gl);
  11109. return true;
  11110. }
  11111. _enterFrame(e = null) {
  11112. ILaya.stage._loop();
  11113. }
  11114. static get context() {
  11115. return Render._context;
  11116. }
  11117. static get canvas() {
  11118. return Render._mainCanvas.source;
  11119. }
  11120. }
  11121. Render.supportWebGLPlusAnimation = false;
  11122. Render.supportWebGLPlusRendering = false;
  11123. Render.isConchApp = false;
  11124. {
  11125. Render.isConchApp = (window.conch != null);
  11126. if (Render.isConchApp) {
  11127. Render.supportWebGLPlusRendering = false;
  11128. }
  11129. else if (window.qq != null && window.qq.webglPlus != null) {
  11130. Render.supportWebGLPlusRendering = false;
  11131. }
  11132. }
  11133. class DrawTrianglesCmd {
  11134. static create(texture, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum) {
  11135. var cmd = Pool.getItemByClass("DrawTrianglesCmd", DrawTrianglesCmd);
  11136. cmd.texture = texture;
  11137. cmd.x = x;
  11138. cmd.y = y;
  11139. cmd.vertices = vertices;
  11140. cmd.uvs = uvs;
  11141. cmd.indices = indices;
  11142. cmd.matrix = matrix;
  11143. cmd.alpha = alpha;
  11144. if (color) {
  11145. cmd.color = new ColorFilter();
  11146. var c = ColorUtils.create(color).arrColor;
  11147. cmd.color.color(c[0] * 255, c[1] * 255, c[2] * 255, c[3] * 255);
  11148. }
  11149. cmd.blendMode = blendMode;
  11150. cmd.colorNum = colorNum;
  11151. return cmd;
  11152. }
  11153. recover() {
  11154. this.texture = null;
  11155. this.vertices = null;
  11156. this.uvs = null;
  11157. this.indices = null;
  11158. this.matrix = null;
  11159. Pool.recover("DrawTrianglesCmd", this);
  11160. }
  11161. run(context, gx, gy) {
  11162. context.drawTriangles(this.texture, this.x + gx, this.y + gy, this.vertices, this.uvs, this.indices, this.matrix, this.alpha, this.color, this.blendMode, this.colorNum);
  11163. }
  11164. get cmdID() {
  11165. return DrawTrianglesCmd.ID;
  11166. }
  11167. }
  11168. DrawTrianglesCmd.ID = "DrawTriangles";
  11169. class Draw9GridTexture {
  11170. constructor() {
  11171. }
  11172. static create(texture, x, y, width, height, sizeGrid) {
  11173. var cmd = Pool.getItemByClass("Draw9GridTexture", Draw9GridTexture);
  11174. cmd.texture = texture;
  11175. texture._addReference();
  11176. cmd.x = x;
  11177. cmd.y = y;
  11178. cmd.width = width;
  11179. cmd.height = height;
  11180. cmd.sizeGrid = sizeGrid;
  11181. return cmd;
  11182. }
  11183. recover() {
  11184. this.texture._removeReference();
  11185. Pool.recover("Draw9GridTexture", this);
  11186. }
  11187. run(context, gx, gy) {
  11188. context.drawTextureWithSizeGrid(this.texture, this.x, this.y, this.width, this.height, this.sizeGrid, gx, gy);
  11189. }
  11190. get cmdID() {
  11191. return Draw9GridTexture.ID;
  11192. }
  11193. }
  11194. Draw9GridTexture.ID = "Draw9GridTexture";
  11195. class SaveCmd {
  11196. static create() {
  11197. var cmd = Pool.getItemByClass("SaveCmd", SaveCmd);
  11198. return cmd;
  11199. }
  11200. recover() {
  11201. Pool.recover("SaveCmd", this);
  11202. }
  11203. run(context, gx, gy) {
  11204. context.save();
  11205. }
  11206. get cmdID() {
  11207. return SaveCmd.ID;
  11208. }
  11209. }
  11210. SaveCmd.ID = "Save";
  11211. class GraphicsBounds {
  11212. constructor() {
  11213. this._cacheBoundsType = false;
  11214. }
  11215. destroy() {
  11216. this._graphics = null;
  11217. this._cacheBoundsType = false;
  11218. if (this._temp)
  11219. this._temp.length = 0;
  11220. if (this._rstBoundPoints)
  11221. this._rstBoundPoints.length = 0;
  11222. if (this._bounds)
  11223. this._bounds.recover();
  11224. this._bounds = null;
  11225. Pool.recover("GraphicsBounds", this);
  11226. }
  11227. static create() {
  11228. return Pool.getItemByClass("GraphicsBounds", GraphicsBounds);
  11229. }
  11230. reset() {
  11231. this._temp && (this._temp.length = 0);
  11232. }
  11233. getBounds(realSize = false) {
  11234. if (!this._bounds || !this._temp || this._temp.length < 1 || realSize != this._cacheBoundsType) {
  11235. this._bounds = Rectangle._getWrapRec(this.getBoundPoints(realSize), this._bounds);
  11236. }
  11237. this._cacheBoundsType = realSize;
  11238. return this._bounds;
  11239. }
  11240. getBoundPoints(realSize = false) {
  11241. if (!this._temp || this._temp.length < 1 || realSize != this._cacheBoundsType)
  11242. this._temp = this._getCmdPoints(realSize);
  11243. this._cacheBoundsType = realSize;
  11244. return this._rstBoundPoints = Utils.copyArray(this._rstBoundPoints, this._temp);
  11245. }
  11246. _getCmdPoints(realSize = false) {
  11247. var cmds = this._graphics.cmds;
  11248. var rst;
  11249. rst = this._temp || (this._temp = []);
  11250. rst.length = 0;
  11251. if (!cmds && this._graphics._one != null) {
  11252. GraphicsBounds._tempCmds.length = 0;
  11253. GraphicsBounds._tempCmds.push(this._graphics._one);
  11254. cmds = GraphicsBounds._tempCmds;
  11255. }
  11256. if (!cmds)
  11257. return rst;
  11258. var matrixs = GraphicsBounds._tempMatrixArrays;
  11259. matrixs.length = 0;
  11260. var tMatrix = GraphicsBounds._initMatrix;
  11261. tMatrix.identity();
  11262. var tempMatrix = GraphicsBounds._tempMatrix;
  11263. var cmd;
  11264. var tex;
  11265. for (var i = 0, n = cmds.length; i < n; i++) {
  11266. cmd = cmds[i];
  11267. switch (cmd.cmdID) {
  11268. case AlphaCmd.ID:
  11269. case SaveCmd.ID:
  11270. matrixs.push(tMatrix);
  11271. tMatrix = tMatrix.clone();
  11272. break;
  11273. case RestoreCmd.ID:
  11274. tMatrix = matrixs.pop();
  11275. break;
  11276. case ScaleCmd.ID:
  11277. tempMatrix.identity();
  11278. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  11279. tempMatrix.scale(cmd.scaleX, cmd.scaleY);
  11280. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  11281. this._switchMatrix(tMatrix, tempMatrix);
  11282. break;
  11283. case RotateCmd.ID:
  11284. tempMatrix.identity();
  11285. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  11286. tempMatrix.rotate(cmd.angle);
  11287. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  11288. this._switchMatrix(tMatrix, tempMatrix);
  11289. break;
  11290. case TranslateCmd.ID:
  11291. tempMatrix.identity();
  11292. tempMatrix.translate(cmd.tx, cmd.ty);
  11293. this._switchMatrix(tMatrix, tempMatrix);
  11294. break;
  11295. case TransformCmd.ID:
  11296. tempMatrix.identity();
  11297. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  11298. tempMatrix.concat(cmd.matrix);
  11299. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  11300. this._switchMatrix(tMatrix, tempMatrix);
  11301. break;
  11302. case DrawImageCmd.ID:
  11303. case FillTextureCmd.ID:
  11304. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  11305. break;
  11306. case DrawTextureCmd.ID:
  11307. tMatrix.copyTo(tempMatrix);
  11308. if (cmd.matrix)
  11309. tempMatrix.concat(cmd.matrix);
  11310. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tempMatrix);
  11311. break;
  11312. case DrawImageCmd.ID:
  11313. tex = cmd.texture;
  11314. if (realSize) {
  11315. if (cmd.width && cmd.height) {
  11316. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  11317. }
  11318. else {
  11319. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), tMatrix);
  11320. }
  11321. }
  11322. else {
  11323. var wRate = (cmd.width || tex.sourceWidth) / tex.width;
  11324. var hRate = (cmd.height || tex.sourceHeight) / tex.height;
  11325. var oWidth = wRate * tex.sourceWidth;
  11326. var oHeight = hRate * tex.sourceHeight;
  11327. var offX = tex.offsetX > 0 ? tex.offsetX : 0;
  11328. var offY = tex.offsetY > 0 ? tex.offsetY : 0;
  11329. offX *= wRate;
  11330. offY *= hRate;
  11331. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - offX, cmd.y - offY, oWidth, oHeight), tMatrix);
  11332. }
  11333. break;
  11334. case FillTextureCmd.ID:
  11335. if (cmd.width && cmd.height) {
  11336. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  11337. }
  11338. else {
  11339. tex = cmd.texture;
  11340. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), tMatrix);
  11341. }
  11342. break;
  11343. case DrawTextureCmd.ID:
  11344. var drawMatrix;
  11345. if (cmd.matrix) {
  11346. tMatrix.copyTo(tempMatrix);
  11347. tempMatrix.concat(cmd.matrix);
  11348. drawMatrix = tempMatrix;
  11349. }
  11350. else {
  11351. drawMatrix = tMatrix;
  11352. }
  11353. if (realSize) {
  11354. if (cmd.width && cmd.height) {
  11355. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), drawMatrix);
  11356. }
  11357. else {
  11358. tex = cmd.texture;
  11359. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), drawMatrix);
  11360. }
  11361. }
  11362. else {
  11363. tex = cmd.texture;
  11364. wRate = (cmd.width || tex.sourceWidth) / tex.width;
  11365. hRate = (cmd.height || tex.sourceHeight) / tex.height;
  11366. oWidth = wRate * tex.sourceWidth;
  11367. oHeight = hRate * tex.sourceHeight;
  11368. offX = tex.offsetX > 0 ? tex.offsetX : 0;
  11369. offY = tex.offsetY > 0 ? tex.offsetY : 0;
  11370. offX *= wRate;
  11371. offY *= hRate;
  11372. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - offX, cmd.y - offY, oWidth, oHeight), drawMatrix);
  11373. }
  11374. break;
  11375. case DrawRectCmd.ID:
  11376. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  11377. break;
  11378. case DrawCircleCmd.ID:
  11379. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - cmd.radius, cmd.y - cmd.radius, cmd.radius + cmd.radius, cmd.radius + cmd.radius), tMatrix);
  11380. break;
  11381. case DrawLineCmd.ID:
  11382. GraphicsBounds._tempPoints.length = 0;
  11383. var lineWidth;
  11384. lineWidth = cmd.lineWidth * 0.5;
  11385. if (cmd.fromX == cmd.toX) {
  11386. GraphicsBounds._tempPoints.push(cmd.fromX + lineWidth, cmd.fromY, cmd.toX + lineWidth, cmd.toY, cmd.fromX - lineWidth, cmd.fromY, cmd.toX - lineWidth, cmd.toY);
  11387. }
  11388. else if (cmd.fromY == cmd.toY) {
  11389. GraphicsBounds._tempPoints.push(cmd.fromX, cmd.fromY + lineWidth, cmd.toX, cmd.toY + lineWidth, cmd.fromX, cmd.fromY - lineWidth, cmd.toX, cmd.toY - lineWidth);
  11390. }
  11391. else {
  11392. GraphicsBounds._tempPoints.push(cmd.fromX, cmd.fromY, cmd.toX, cmd.toY);
  11393. }
  11394. GraphicsBounds._addPointArrToRst(rst, GraphicsBounds._tempPoints, tMatrix);
  11395. break;
  11396. case DrawCurvesCmd.ID:
  11397. GraphicsBounds._addPointArrToRst(rst, Bezier.I.getBezierPoints(cmd.points), tMatrix, cmd.x, cmd.y);
  11398. break;
  11399. case DrawLinesCmd.ID:
  11400. case DrawPolyCmd.ID:
  11401. GraphicsBounds._addPointArrToRst(rst, cmd.points, tMatrix, cmd.x, cmd.y);
  11402. break;
  11403. case DrawPathCmd.ID:
  11404. GraphicsBounds._addPointArrToRst(rst, this._getPathPoints(cmd.paths), tMatrix, cmd.x, cmd.y);
  11405. break;
  11406. case DrawPieCmd.ID:
  11407. GraphicsBounds._addPointArrToRst(rst, this._getPiePoints(cmd.x, cmd.y, cmd.radius, cmd.startAngle, cmd.endAngle), tMatrix);
  11408. break;
  11409. case DrawTrianglesCmd.ID:
  11410. GraphicsBounds._addPointArrToRst(rst, this._getTriAngBBXPoints(cmd.vertices), tMatrix);
  11411. break;
  11412. case Draw9GridTexture.ID:
  11413. GraphicsBounds._addPointArrToRst(rst, this._getDraw9GridBBXPoints(cmd), tMatrix);
  11414. break;
  11415. }
  11416. }
  11417. if (rst.length > 200) {
  11418. rst = Utils.copyArray(rst, Rectangle._getWrapRec(rst)._getBoundPoints());
  11419. }
  11420. else if (rst.length > 8)
  11421. rst = GrahamScan.scanPList(rst);
  11422. return rst;
  11423. }
  11424. _switchMatrix(tMatix, tempMatrix) {
  11425. tempMatrix.concat(tMatix);
  11426. tempMatrix.copyTo(tMatix);
  11427. }
  11428. static _addPointArrToRst(rst, points, matrix, dx = 0, dy = 0) {
  11429. var i, len;
  11430. len = points.length;
  11431. for (i = 0; i < len; i += 2) {
  11432. GraphicsBounds._addPointToRst(rst, points[i] + dx, points[i + 1] + dy, matrix);
  11433. }
  11434. }
  11435. static _addPointToRst(rst, x, y, matrix) {
  11436. var _tempPoint = Point.TEMP;
  11437. _tempPoint.setTo(x ? x : 0, y ? y : 0);
  11438. matrix.transformPoint(_tempPoint);
  11439. rst.push(_tempPoint.x, _tempPoint.y);
  11440. }
  11441. _getPiePoints(x, y, radius, startAngle, endAngle) {
  11442. var rst = GraphicsBounds._tempPoints;
  11443. GraphicsBounds._tempPoints.length = 0;
  11444. var k = Math.PI / 180;
  11445. var d1 = endAngle - startAngle;
  11446. if (d1 >= 360 || d1 <= -360) {
  11447. rst.push(x - radius, y - radius);
  11448. rst.push(x + radius, y - radius);
  11449. rst.push(x + radius, y + radius);
  11450. rst.push(x - radius, y + radius);
  11451. return rst;
  11452. }
  11453. rst.push(x, y);
  11454. var delta = d1 % 360;
  11455. if (delta < 0)
  11456. delta += 360;
  11457. var end1 = startAngle + delta;
  11458. var st = startAngle * k;
  11459. var ed = end1 * k;
  11460. rst.push(x + radius * Math.cos(st), y + radius * Math.sin(st));
  11461. rst.push(x + radius * Math.cos(ed), y + radius * Math.sin(ed));
  11462. var s1 = Math.ceil(startAngle / 90) * 90;
  11463. var s2 = Math.floor(end1 / 90) * 90;
  11464. for (var cs = s1; cs <= s2; cs += 90) {
  11465. var csr = cs * k;
  11466. rst.push(x + radius * Math.cos(csr), y + radius * Math.sin(csr));
  11467. }
  11468. return rst;
  11469. }
  11470. _getTriAngBBXPoints(vert) {
  11471. var vnum = vert.length;
  11472. if (vnum < 2)
  11473. return [];
  11474. var minx = vert[0];
  11475. var miny = vert[1];
  11476. var maxx = minx;
  11477. var maxy = miny;
  11478. for (var i = 2; i < vnum;) {
  11479. var cx = vert[i++];
  11480. var cy = vert[i++];
  11481. if (minx > cx)
  11482. minx = cx;
  11483. if (miny > cy)
  11484. miny = cy;
  11485. if (maxx < cx)
  11486. maxx = cx;
  11487. if (maxy < cy)
  11488. maxy = cy;
  11489. }
  11490. return [minx, miny, maxx, miny, maxx, maxy, minx, maxy];
  11491. }
  11492. _getDraw9GridBBXPoints(cmd) {
  11493. var minx = 0;
  11494. var miny = 0;
  11495. var maxx = cmd.width;
  11496. var maxy = cmd.height;
  11497. return [minx, miny, maxx, miny, maxx, maxy, minx, maxy];
  11498. }
  11499. _getPathPoints(paths) {
  11500. var i, len;
  11501. var rst = GraphicsBounds._tempPoints;
  11502. rst.length = 0;
  11503. len = paths.length;
  11504. var tCMD;
  11505. for (i = 0; i < len; i++) {
  11506. tCMD = paths[i];
  11507. if (tCMD.length > 1) {
  11508. rst.push(tCMD[1], tCMD[2]);
  11509. if (tCMD.length > 3) {
  11510. rst.push(tCMD[3], tCMD[4]);
  11511. }
  11512. }
  11513. }
  11514. return rst;
  11515. }
  11516. }
  11517. GraphicsBounds._tempMatrix = new Matrix();
  11518. GraphicsBounds._initMatrix = new Matrix();
  11519. GraphicsBounds._tempPoints = [];
  11520. GraphicsBounds._tempMatrixArrays = [];
  11521. GraphicsBounds._tempCmds = [];
  11522. class SpriteConst {
  11523. }
  11524. SpriteConst.ALPHA = 0x01;
  11525. SpriteConst.TRANSFORM = 0x02;
  11526. SpriteConst.BLEND = 0x04;
  11527. SpriteConst.CANVAS = 0x08;
  11528. SpriteConst.FILTERS = 0x10;
  11529. SpriteConst.MASK = 0x20;
  11530. SpriteConst.CLIP = 0x40;
  11531. SpriteConst.STYLE = 0x80;
  11532. SpriteConst.TEXTURE = 0x100;
  11533. SpriteConst.GRAPHICS = 0x200;
  11534. SpriteConst.LAYAGL3D = 0x400;
  11535. SpriteConst.CUSTOM = 0x800;
  11536. SpriteConst.ONECHILD = 0x1000;
  11537. SpriteConst.CHILDS = 0x2000;
  11538. SpriteConst.REPAINT_NONE = 0;
  11539. SpriteConst.REPAINT_NODE = 0x01;
  11540. SpriteConst.REPAINT_CACHE = 0x02;
  11541. SpriteConst.REPAINT_ALL = 0x03;
  11542. class ClipRectCmd {
  11543. static create(x, y, width, height) {
  11544. var cmd = Pool.getItemByClass("ClipRectCmd", ClipRectCmd);
  11545. cmd.x = x;
  11546. cmd.y = y;
  11547. cmd.width = width;
  11548. cmd.height = height;
  11549. return cmd;
  11550. }
  11551. recover() {
  11552. Pool.recover("ClipRectCmd", this);
  11553. }
  11554. run(context, gx, gy) {
  11555. context.clipRect(this.x + gx, this.y + gy, this.width, this.height);
  11556. }
  11557. get cmdID() {
  11558. return ClipRectCmd.ID;
  11559. }
  11560. }
  11561. ClipRectCmd.ID = "ClipRect";
  11562. class DrawTexturesCmd {
  11563. static create(texture, pos) {
  11564. var cmd = Pool.getItemByClass("DrawTexturesCmd", DrawTexturesCmd);
  11565. cmd.texture = texture;
  11566. texture._addReference();
  11567. cmd.pos = pos;
  11568. return cmd;
  11569. }
  11570. recover() {
  11571. this.texture._removeReference();
  11572. this.texture = null;
  11573. this.pos = null;
  11574. Pool.recover("DrawTexturesCmd", this);
  11575. }
  11576. run(context, gx, gy) {
  11577. context.drawTextures(this.texture, this.pos, gx, gy);
  11578. }
  11579. get cmdID() {
  11580. return DrawTexturesCmd.ID;
  11581. }
  11582. }
  11583. DrawTexturesCmd.ID = "DrawTextures";
  11584. class FillTextCmd {
  11585. constructor() {
  11586. this._textIsWorldText = false;
  11587. this._fontColor = 0xffffffff;
  11588. this._strokeColor = 0;
  11589. this._fontObj = FillTextCmd._defFontObj;
  11590. this._nTexAlign = 0;
  11591. }
  11592. static create(text, words, x, y, font, color, textAlign, lineWidth, borderColor) {
  11593. var cmd = Pool.getItemByClass("FillTextCmd", FillTextCmd);
  11594. cmd.text = text;
  11595. cmd._textIsWorldText = text instanceof WordText;
  11596. cmd._words = words;
  11597. cmd.x = x;
  11598. cmd.y = y;
  11599. cmd.font = font;
  11600. cmd.color = color;
  11601. cmd.textAlign = textAlign;
  11602. cmd._lineWidth = lineWidth;
  11603. cmd._borderColor = borderColor;
  11604. return cmd;
  11605. }
  11606. recover() {
  11607. Pool.recover("FillTextCmd", this);
  11608. }
  11609. run(context, gx, gy) {
  11610. if (ILaya.stage.isGlobalRepaint()) {
  11611. this._textIsWorldText && this._text.cleanCache();
  11612. }
  11613. if (this._words) {
  11614. Context._textRender.fillWords(context, this._words, this.x + gx, this.y + gy, this._fontObj, this._color, this._borderColor, this._lineWidth);
  11615. }
  11616. else {
  11617. if (this._textIsWorldText) {
  11618. context._fast_filltext(this._text, this.x + gx, this.y + gy, this._fontObj, this._color, this._borderColor, this._lineWidth, this._nTexAlign, 0);
  11619. }
  11620. else {
  11621. Context._textRender.filltext(context, this._text, this.x + gx, this.y + gy, this.font, this.color, this._borderColor, this._lineWidth, this._textAlign);
  11622. }
  11623. }
  11624. }
  11625. get cmdID() {
  11626. return FillTextCmd.ID;
  11627. }
  11628. get text() {
  11629. return this._text;
  11630. }
  11631. set text(value) {
  11632. this._text = value;
  11633. this._textIsWorldText = value instanceof WordText;
  11634. this._textIsWorldText && this._text.cleanCache();
  11635. }
  11636. get font() {
  11637. return this._font;
  11638. }
  11639. set font(value) {
  11640. this._font = value;
  11641. this._fontObj = FontInfo.Parse(value);
  11642. this._textIsWorldText && this._text.cleanCache();
  11643. }
  11644. get color() {
  11645. return this._color;
  11646. }
  11647. set color(value) {
  11648. this._color = value;
  11649. this._fontColor = ColorUtils.create(value).numColor;
  11650. this._textIsWorldText && this._text.cleanCache();
  11651. }
  11652. get textAlign() {
  11653. return this._textAlign;
  11654. }
  11655. set textAlign(value) {
  11656. this._textAlign = value;
  11657. switch (value) {
  11658. case 'center':
  11659. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  11660. break;
  11661. case 'right':
  11662. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  11663. break;
  11664. default:
  11665. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_DEFAULT;
  11666. }
  11667. this._textIsWorldText && this._text.cleanCache();
  11668. }
  11669. }
  11670. FillTextCmd.ID = "FillText";
  11671. FillTextCmd._defFontObj = new FontInfo(null);
  11672. class CacheManger {
  11673. constructor() {
  11674. }
  11675. static regCacheByFunction(disposeFunction, getCacheListFunction) {
  11676. CacheManger.unRegCacheByFunction(disposeFunction, getCacheListFunction);
  11677. var cache;
  11678. cache = { tryDispose: disposeFunction, getCacheList: getCacheListFunction };
  11679. CacheManger._cacheList.push(cache);
  11680. }
  11681. static unRegCacheByFunction(disposeFunction, getCacheListFunction) {
  11682. var i, len;
  11683. len = CacheManger._cacheList.length;
  11684. for (i = 0; i < len; i++) {
  11685. if (CacheManger._cacheList[i].tryDispose == disposeFunction && CacheManger._cacheList[i].getCacheList == getCacheListFunction) {
  11686. CacheManger._cacheList.splice(i, 1);
  11687. return;
  11688. }
  11689. }
  11690. }
  11691. static forceDispose() {
  11692. var i, len = CacheManger._cacheList.length;
  11693. for (i = 0; i < len; i++) {
  11694. CacheManger._cacheList[i].tryDispose(true);
  11695. }
  11696. }
  11697. static beginCheck(waitTime = 15000) {
  11698. ILaya.systemTimer.loop(waitTime, null, CacheManger._checkLoop);
  11699. }
  11700. static stopCheck() {
  11701. ILaya.systemTimer.clear(null, CacheManger._checkLoop);
  11702. }
  11703. static _checkLoop() {
  11704. var cacheList = CacheManger._cacheList;
  11705. if (cacheList.length < 1)
  11706. return;
  11707. var tTime = ILaya.Browser.now();
  11708. var count;
  11709. var len;
  11710. len = count = cacheList.length;
  11711. while (count > 0) {
  11712. CacheManger._index++;
  11713. CacheManger._index = CacheManger._index % len;
  11714. cacheList[CacheManger._index].tryDispose(false);
  11715. if (ILaya.Browser.now() - tTime > CacheManger.loopTimeLimit)
  11716. break;
  11717. count--;
  11718. }
  11719. }
  11720. }
  11721. CacheManger.loopTimeLimit = 2;
  11722. CacheManger._cacheList = [];
  11723. CacheManger._index = 0;
  11724. class VectorGraphManager {
  11725. constructor() {
  11726. this.useDic = {};
  11727. this.shapeDic = {};
  11728. this.shapeLineDic = {};
  11729. this._id = 0;
  11730. this._checkKey = false;
  11731. this._freeIdArray = [];
  11732. CacheManger.regCacheByFunction(this.startDispose.bind(this), this.getCacheList.bind(this));
  11733. }
  11734. static getInstance() {
  11735. return VectorGraphManager.instance = VectorGraphManager.instance || new VectorGraphManager();
  11736. }
  11737. getId() {
  11738. return this._id++;
  11739. }
  11740. addShape(id, shape) {
  11741. this.shapeDic[id] = shape;
  11742. if (!this.useDic[id]) {
  11743. this.useDic[id] = true;
  11744. }
  11745. }
  11746. addLine(id, Line) {
  11747. this.shapeLineDic[id] = Line;
  11748. if (!this.shapeLineDic[id]) {
  11749. this.shapeLineDic[id] = true;
  11750. }
  11751. }
  11752. getShape(id) {
  11753. if (this._checkKey) {
  11754. if (this.useDic[id] != null) {
  11755. this.useDic[id] = true;
  11756. }
  11757. }
  11758. }
  11759. deleteShape(id) {
  11760. if (this.shapeDic[id]) {
  11761. this.shapeDic[id] = null;
  11762. delete this.shapeDic[id];
  11763. }
  11764. if (this.shapeLineDic[id]) {
  11765. this.shapeLineDic[id] = null;
  11766. delete this.shapeLineDic[id];
  11767. }
  11768. if (this.useDic[id] != null) {
  11769. delete this.useDic[id];
  11770. }
  11771. }
  11772. getCacheList() {
  11773. var str;
  11774. var list = [];
  11775. for (str in this.shapeDic) {
  11776. list.push(this.shapeDic[str]);
  11777. }
  11778. for (str in this.shapeLineDic) {
  11779. list.push(this.shapeLineDic[str]);
  11780. }
  11781. return list;
  11782. }
  11783. startDispose(key) {
  11784. var str;
  11785. for (str in this.useDic) {
  11786. this.useDic[str] = false;
  11787. }
  11788. this._checkKey = true;
  11789. }
  11790. endDispose() {
  11791. if (this._checkKey) {
  11792. var str;
  11793. for (str in this.useDic) {
  11794. if (!this.useDic[str]) {
  11795. this.deleteShape(str);
  11796. }
  11797. }
  11798. this._checkKey = false;
  11799. }
  11800. }
  11801. }
  11802. class Graphics {
  11803. constructor() {
  11804. this._sp = null;
  11805. this._one = null;
  11806. this._render = this._renderEmpty;
  11807. this._cmds = null;
  11808. this._vectorgraphArray = null;
  11809. this._graphicBounds = null;
  11810. this.autoDestroy = false;
  11811. this._createData();
  11812. }
  11813. _createData() {
  11814. }
  11815. _clearData() {
  11816. }
  11817. _destroyData() {
  11818. }
  11819. destroy() {
  11820. this.clear(true);
  11821. if (this._graphicBounds)
  11822. this._graphicBounds.destroy();
  11823. this._graphicBounds = null;
  11824. this._vectorgraphArray = null;
  11825. if (this._sp) {
  11826. this._sp._renderType = 0;
  11827. this._sp._setRenderType(0);
  11828. this._sp = null;
  11829. }
  11830. this._destroyData();
  11831. }
  11832. clear(recoverCmds = true) {
  11833. if (recoverCmds) {
  11834. var tCmd = this._one;
  11835. if (this._cmds) {
  11836. var i, len = this._cmds.length;
  11837. for (i = 0; i < len; i++) {
  11838. tCmd = this._cmds[i];
  11839. tCmd.recover();
  11840. }
  11841. this._cmds.length = 0;
  11842. }
  11843. else if (tCmd) {
  11844. tCmd.recover();
  11845. }
  11846. }
  11847. else {
  11848. this._cmds = null;
  11849. }
  11850. this._one = null;
  11851. this._render = this._renderEmpty;
  11852. this._clearData();
  11853. if (this._sp) {
  11854. this._sp._renderType &= ~SpriteConst.GRAPHICS;
  11855. this._sp._setRenderType(this._sp._renderType);
  11856. }
  11857. this._repaint();
  11858. if (this._vectorgraphArray) {
  11859. for (i = 0, len = this._vectorgraphArray.length; i < len; i++) {
  11860. VectorGraphManager.getInstance().deleteShape(this._vectorgraphArray[i]);
  11861. }
  11862. this._vectorgraphArray.length = 0;
  11863. }
  11864. }
  11865. _clearBoundsCache() {
  11866. if (this._graphicBounds)
  11867. this._graphicBounds.reset();
  11868. }
  11869. _initGraphicBounds() {
  11870. if (!this._graphicBounds) {
  11871. this._graphicBounds = GraphicsBounds.create();
  11872. this._graphicBounds._graphics = this;
  11873. }
  11874. }
  11875. _repaint() {
  11876. this._clearBoundsCache();
  11877. this._sp && this._sp.repaint();
  11878. }
  11879. _isOnlyOne() {
  11880. return !this._cmds || this._cmds.length === 0;
  11881. }
  11882. get cmds() {
  11883. return this._cmds;
  11884. }
  11885. set cmds(value) {
  11886. if (this._sp) {
  11887. this._sp._renderType |= SpriteConst.GRAPHICS;
  11888. this._sp._setRenderType(this._sp._renderType);
  11889. }
  11890. this._cmds = value;
  11891. this._render = this._renderAll;
  11892. this._repaint();
  11893. }
  11894. getBounds(realSize = false) {
  11895. this._initGraphicBounds();
  11896. return this._graphicBounds.getBounds(realSize);
  11897. }
  11898. getBoundPoints(realSize = false) {
  11899. this._initGraphicBounds();
  11900. return this._graphicBounds.getBoundPoints(realSize);
  11901. }
  11902. drawImage(texture, x = 0, y = 0, width = 0, height = 0) {
  11903. if (!texture)
  11904. return null;
  11905. if (!width)
  11906. width = texture.sourceWidth;
  11907. if (!height)
  11908. height = texture.sourceHeight;
  11909. if (texture.getIsReady()) {
  11910. var wRate = width / texture.sourceWidth;
  11911. var hRate = height / texture.sourceHeight;
  11912. width = texture.width * wRate;
  11913. height = texture.height * hRate;
  11914. if (width <= 0 || height <= 0)
  11915. return null;
  11916. x += texture.offsetX * wRate;
  11917. y += texture.offsetY * hRate;
  11918. }
  11919. if (this._sp) {
  11920. this._sp._renderType |= SpriteConst.GRAPHICS;
  11921. this._sp._setRenderType(this._sp._renderType);
  11922. }
  11923. var args = DrawImageCmd.create.call(this, texture, x, y, width, height);
  11924. if (this._one == null) {
  11925. this._one = args;
  11926. this._render = this._renderOneImg;
  11927. }
  11928. else {
  11929. this._saveToCmd(null, args);
  11930. }
  11931. this._repaint();
  11932. return args;
  11933. }
  11934. drawTexture(texture, x = 0, y = 0, width = 0, height = 0, matrix = null, alpha = 1, color = null, blendMode = null, uv) {
  11935. if (!texture || alpha < 0.01)
  11936. return null;
  11937. if (!texture.getIsReady())
  11938. return null;
  11939. if (!width)
  11940. width = texture.sourceWidth;
  11941. if (!height)
  11942. height = texture.sourceHeight;
  11943. if (texture.getIsReady()) {
  11944. var wRate = width / texture.sourceWidth;
  11945. var hRate = height / texture.sourceHeight;
  11946. width = texture.width * wRate;
  11947. height = texture.height * hRate;
  11948. if (width <= 0 || height <= 0)
  11949. return null;
  11950. x += texture.offsetX * wRate;
  11951. y += texture.offsetY * hRate;
  11952. }
  11953. if (this._sp) {
  11954. this._sp._renderType |= SpriteConst.GRAPHICS;
  11955. this._sp._setRenderType(this._sp._renderType);
  11956. }
  11957. var args = DrawTextureCmd.create.call(this, texture, x, y, width, height, matrix, alpha, color, blendMode, uv);
  11958. this._repaint();
  11959. return this._saveToCmd(null, args);
  11960. }
  11961. drawTextures(texture, pos) {
  11962. if (!texture)
  11963. return null;
  11964. return this._saveToCmd(Render._context.drawTextures, DrawTexturesCmd.create.call(this, texture, pos));
  11965. }
  11966. drawTriangles(texture, x, y, vertices, uvs, indices, matrix = null, alpha = 1, color = null, blendMode = null, colorNum = 0xffffffff) {
  11967. return this._saveToCmd(Render._context.drawTriangles, DrawTrianglesCmd.create.call(this, texture, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum));
  11968. }
  11969. fillTexture(texture, x, y, width = 0, height = 0, type = "repeat", offset = null) {
  11970. if (texture && texture.getIsReady())
  11971. return this._saveToCmd(Render._context._fillTexture, FillTextureCmd.create.call(this, texture, x, y, width, height, type, offset || Point.EMPTY, {}));
  11972. else
  11973. return null;
  11974. }
  11975. _saveToCmd(fun, args) {
  11976. if (this._sp) {
  11977. this._sp._renderType |= SpriteConst.GRAPHICS;
  11978. this._sp._setRenderType(this._sp._renderType);
  11979. }
  11980. if (this._one == null) {
  11981. this._one = args;
  11982. this._render = this._renderOne;
  11983. }
  11984. else {
  11985. this._render = this._renderAll;
  11986. (this._cmds || (this._cmds = [])).length === 0 && this._cmds.push(this._one);
  11987. this._cmds.push(args);
  11988. }
  11989. this._repaint();
  11990. return args;
  11991. }
  11992. clipRect(x, y, width, height) {
  11993. return this._saveToCmd(Render._context.clipRect, ClipRectCmd.create.call(this, x, y, width, height));
  11994. }
  11995. fillText(text, x, y, font, color, textAlign) {
  11996. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), color, textAlign, 0, ""));
  11997. }
  11998. fillBorderText(text, x, y, font, fillColor, textAlign, lineWidth, borderColor) {
  11999. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), fillColor, textAlign, lineWidth, borderColor));
  12000. }
  12001. fillWords(words, x, y, font, color) {
  12002. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, null, words, x, y, font || ILaya.Text.defaultFontStr(), color, '', 0, null));
  12003. }
  12004. fillBorderWords(words, x, y, font, fillColor, borderColor, lineWidth) {
  12005. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, null, words, x, y, font || ILaya.Text.defaultFontStr(), fillColor, "", lineWidth, borderColor));
  12006. }
  12007. strokeText(text, x, y, font, color, lineWidth, textAlign) {
  12008. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), null, textAlign, lineWidth, color));
  12009. }
  12010. alpha(alpha) {
  12011. return this._saveToCmd(Render._context.alpha, AlphaCmd.create.call(this, alpha));
  12012. }
  12013. transform(matrix, pivotX = 0, pivotY = 0) {
  12014. return this._saveToCmd(Render._context._transform, TransformCmd.create.call(this, matrix, pivotX, pivotY));
  12015. }
  12016. rotate(angle, pivotX = 0, pivotY = 0) {
  12017. return this._saveToCmd(Render._context._rotate, RotateCmd.create.call(this, angle, pivotX, pivotY));
  12018. }
  12019. scale(scaleX, scaleY, pivotX = 0, pivotY = 0) {
  12020. return this._saveToCmd(Render._context._scale, ScaleCmd.create.call(this, scaleX, scaleY, pivotX, pivotY));
  12021. }
  12022. translate(tx, ty) {
  12023. return this._saveToCmd(Render._context.translate, TranslateCmd.create.call(this, tx, ty));
  12024. }
  12025. save() {
  12026. return this._saveToCmd(Render._context._save, SaveCmd.create.call(this));
  12027. }
  12028. restore() {
  12029. return this._saveToCmd(Render._context.restore, RestoreCmd.create.call(this));
  12030. }
  12031. replaceText(text) {
  12032. this._repaint();
  12033. var cmds = this._cmds;
  12034. if (!cmds) {
  12035. if (this._one && this._isTextCmd(this._one)) {
  12036. this._one.text = text;
  12037. return true;
  12038. }
  12039. }
  12040. else {
  12041. for (var i = cmds.length - 1; i > -1; i--) {
  12042. if (this._isTextCmd(cmds[i])) {
  12043. cmds[i].text = text;
  12044. return true;
  12045. }
  12046. }
  12047. }
  12048. return false;
  12049. }
  12050. _isTextCmd(cmd) {
  12051. var cmdID = cmd.cmdID;
  12052. return cmdID == FillTextCmd.ID;
  12053. }
  12054. replaceTextColor(color) {
  12055. this._repaint();
  12056. var cmds = this._cmds;
  12057. if (!cmds) {
  12058. if (this._one && this._isTextCmd(this._one)) {
  12059. this._setTextCmdColor(this._one, color);
  12060. }
  12061. }
  12062. else {
  12063. for (var i = cmds.length - 1; i > -1; i--) {
  12064. if (this._isTextCmd(cmds[i])) {
  12065. this._setTextCmdColor(cmds[i], color);
  12066. }
  12067. }
  12068. }
  12069. }
  12070. _setTextCmdColor(cmdO, color) {
  12071. var cmdID = cmdO.cmdID;
  12072. switch (cmdID) {
  12073. case FillTextCmd.ID:
  12074. cmdO.color = color;
  12075. break;
  12076. }
  12077. }
  12078. loadImage(url, x = 0, y = 0, width = 0, height = 0, complete = null) {
  12079. var tex = ILaya.Loader.getRes(url);
  12080. if (!tex) {
  12081. tex = new Texture();
  12082. tex.load(url);
  12083. ILaya.Loader.cacheTexture(url, tex);
  12084. tex.once(Event.READY, this, this.drawImage, [tex, x, y, width, height]);
  12085. }
  12086. else {
  12087. if (!tex.getIsReady()) {
  12088. tex.once(Event.READY, this, this.drawImage, [tex, x, y, width, height]);
  12089. }
  12090. else
  12091. this.drawImage(tex, x, y, width, height);
  12092. }
  12093. if (complete != null) {
  12094. tex.getIsReady() ? complete.call(this._sp) : tex.on(Event.READY, this._sp, complete);
  12095. }
  12096. }
  12097. _renderEmpty(sprite, context, x, y) {
  12098. }
  12099. _renderAll(sprite, context, x, y) {
  12100. var cmds = this._cmds;
  12101. for (var i = 0, n = cmds.length; i < n; i++) {
  12102. cmds[i].run(context, x, y);
  12103. }
  12104. }
  12105. _renderOne(sprite, context, x, y) {
  12106. context.sprite = sprite;
  12107. this._one.run(context, x, y);
  12108. }
  12109. _renderOneImg(sprite, context, x, y) {
  12110. context.sprite = sprite;
  12111. this._one.run(context, x, y);
  12112. }
  12113. drawLine(fromX, fromY, toX, toY, lineColor, lineWidth = 1) {
  12114. var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
  12115. return this._saveToCmd(Render._context._drawLine, DrawLineCmd.create.call(this, fromX + offset, fromY + offset, toX + offset, toY + offset, lineColor, lineWidth, 0));
  12116. }
  12117. drawLines(x, y, points, lineColor, lineWidth = 1) {
  12118. if (!points || points.length < 4)
  12119. return null;
  12120. var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
  12121. return this._saveToCmd(Render._context._drawLines, DrawLinesCmd.create.call(this, x + offset, y + offset, points, lineColor, lineWidth, 0));
  12122. }
  12123. drawCurves(x, y, points, lineColor, lineWidth = 1) {
  12124. return this._saveToCmd(Render._context.drawCurves, DrawCurvesCmd.create.call(this, x, y, points, lineColor, lineWidth));
  12125. }
  12126. drawRect(x, y, width, height, fillColor, lineColor = null, lineWidth = 1) {
  12127. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  12128. var lineOffset = lineColor ? lineWidth : 0;
  12129. return this._saveToCmd(Render._context.drawRect, DrawRectCmd.create.call(this, x + offset, y + offset, width - lineOffset, height - lineOffset, fillColor, lineColor, lineWidth));
  12130. }
  12131. drawCircle(x, y, radius, fillColor, lineColor = null, lineWidth = 1) {
  12132. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  12133. return this._saveToCmd(Render._context._drawCircle, DrawCircleCmd.create.call(this, x, y, radius - offset, fillColor, lineColor, lineWidth, 0));
  12134. }
  12135. drawPie(x, y, radius, startAngle, endAngle, fillColor, lineColor = null, lineWidth = 1) {
  12136. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  12137. var lineOffset = lineColor ? lineWidth : 0;
  12138. return this._saveToCmd(Render._context._drawPie, DrawPieCmd.create.call(this, x + offset, y + offset, radius - lineOffset, Utils.toRadian(startAngle), Utils.toRadian(endAngle), fillColor, lineColor, lineWidth, 0));
  12139. }
  12140. drawPoly(x, y, points, fillColor, lineColor = null, lineWidth = 1) {
  12141. var tIsConvexPolygon = false;
  12142. if (points.length > 6) {
  12143. tIsConvexPolygon = false;
  12144. }
  12145. else {
  12146. tIsConvexPolygon = true;
  12147. }
  12148. var offset = (lineWidth >= 1 && lineColor) ? (lineWidth % 2 === 0 ? 0 : 0.5) : 0;
  12149. return this._saveToCmd(Render._context._drawPoly, DrawPolyCmd.create.call(this, x + offset, y + offset, points, fillColor, lineColor, lineWidth, tIsConvexPolygon, 0));
  12150. }
  12151. drawPath(x, y, paths, brush = null, pen = null) {
  12152. return this._saveToCmd(Render._context._drawPath, DrawPathCmd.create.call(this, x, y, paths, brush, pen));
  12153. }
  12154. draw9Grid(texture, x = 0, y = 0, width = 0, height = 0, sizeGrid) {
  12155. this._saveToCmd(null, Draw9GridTexture.create(texture, x, y, width, height, sizeGrid));
  12156. }
  12157. }
  12158. class Const {
  12159. }
  12160. Const.NOT_ACTIVE = 0x01;
  12161. Const.ACTIVE_INHIERARCHY = 0x02;
  12162. Const.AWAKED = 0x04;
  12163. Const.NOT_READY = 0x08;
  12164. Const.DISPLAY = 0x10;
  12165. Const.HAS_ZORDER = 0x20;
  12166. Const.HAS_MOUSE = 0x40;
  12167. Const.DISPLAYED_INSTAGE = 0x80;
  12168. Const.DRAWCALL_OPTIMIZE = 0x100;
  12169. class LayaGLQuickRunner {
  12170. static __init__() {
  12171. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TRANSFORM | SpriteConst.GRAPHICS] = LayaGLQuickRunner.alpha_transform_drawLayaGL;
  12172. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.GRAPHICS] = LayaGLQuickRunner.alpha_drawLayaGL;
  12173. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.GRAPHICS] = LayaGLQuickRunner.transform_drawLayaGL;
  12174. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.CHILDS] = LayaGLQuickRunner.transform_drawNodes;
  12175. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TRANSFORM | SpriteConst.TEXTURE] = LayaGLQuickRunner.alpha_transform_drawTexture;
  12176. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TEXTURE] = LayaGLQuickRunner.alpha_drawTexture;
  12177. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.TEXTURE] = LayaGLQuickRunner.transform_drawTexture;
  12178. LayaGLQuickRunner.map[SpriteConst.GRAPHICS | SpriteConst.CHILDS] = LayaGLQuickRunner.drawLayaGL_drawNodes;
  12179. }
  12180. static transform_drawTexture(sprite, context, x, y) {
  12181. var style = sprite._style;
  12182. var tex = sprite.texture;
  12183. context.saveTransform(LayaGLQuickRunner.curMat);
  12184. context.transformByMatrix(sprite.transform, x, y);
  12185. var width = sprite._width || tex.sourceWidth;
  12186. var height = sprite._height || tex.sourceHeight;
  12187. var wRate = width / tex.sourceWidth;
  12188. var hRate = height / tex.sourceHeight;
  12189. width = tex.width * wRate;
  12190. height = tex.height * hRate;
  12191. if (width <= 0 || height <= 0)
  12192. return null;
  12193. var px = -sprite.pivotX + tex.offsetX * wRate;
  12194. var py = -sprite.pivotY + tex.offsetY * hRate;
  12195. context.drawTexture(tex, px, py, width, height);
  12196. context.restoreTransform(LayaGLQuickRunner.curMat);
  12197. }
  12198. static alpha_drawTexture(sprite, context, x, y) {
  12199. var style = sprite._style;
  12200. var alpha;
  12201. var tex = sprite.texture;
  12202. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  12203. var temp = context.globalAlpha;
  12204. context.globalAlpha *= alpha;
  12205. context.drawTexture(tex, x - style.pivotX + tex.offsetX, y - style.pivotY + tex.offsetY, sprite._width || tex.width, sprite._height || tex.height);
  12206. context.globalAlpha = temp;
  12207. }
  12208. }
  12209. static alpha_transform_drawTexture(sprite, context, x, y) {
  12210. var style = sprite._style;
  12211. var alpha;
  12212. var tex = sprite.texture;
  12213. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  12214. var temp = context.globalAlpha;
  12215. context.globalAlpha *= alpha;
  12216. context.saveTransform(LayaGLQuickRunner.curMat);
  12217. context.transformByMatrix(sprite.transform, x, y);
  12218. context.drawTexture(tex, -style.pivotX + tex.offsetX, -style.pivotY + tex.offsetY, sprite._width || tex.width, sprite._height || tex.height);
  12219. context.restoreTransform(LayaGLQuickRunner.curMat);
  12220. context.globalAlpha = temp;
  12221. }
  12222. }
  12223. static alpha_transform_drawLayaGL(sprite, context, x, y) {
  12224. var style = sprite._style;
  12225. var alpha;
  12226. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  12227. var temp = context.globalAlpha;
  12228. context.globalAlpha *= alpha;
  12229. context.saveTransform(LayaGLQuickRunner.curMat);
  12230. context.transformByMatrix(sprite.transform, x, y);
  12231. sprite._graphics && sprite._graphics._render(sprite, context, -style.pivotX, -style.pivotY);
  12232. context.restoreTransform(LayaGLQuickRunner.curMat);
  12233. context.globalAlpha = temp;
  12234. }
  12235. }
  12236. static alpha_drawLayaGL(sprite, context, x, y) {
  12237. var style = sprite._style;
  12238. var alpha;
  12239. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  12240. var temp = context.globalAlpha;
  12241. context.globalAlpha *= alpha;
  12242. sprite._graphics && sprite._graphics._render(sprite, context, x - style.pivotX, y - style.pivotY);
  12243. context.globalAlpha = temp;
  12244. }
  12245. }
  12246. static transform_drawLayaGL(sprite, context, x, y) {
  12247. var style = sprite._style;
  12248. context.saveTransform(LayaGLQuickRunner.curMat);
  12249. context.transformByMatrix(sprite.transform, x, y);
  12250. sprite._graphics && sprite._graphics._render(sprite, context, -style.pivotX, -style.pivotY);
  12251. context.restoreTransform(LayaGLQuickRunner.curMat);
  12252. }
  12253. static transform_drawNodes(sprite, context, x, y) {
  12254. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  12255. var style = sprite._style;
  12256. context.saveTransform(LayaGLQuickRunner.curMat);
  12257. context.transformByMatrix(sprite.transform, x, y);
  12258. x = -style.pivotX;
  12259. y = -style.pivotY;
  12260. var childs = sprite._children, n = childs.length, ele;
  12261. if (style.viewport) {
  12262. var rect = style.viewport;
  12263. var left = rect.x;
  12264. var top = rect.y;
  12265. var right = rect.right;
  12266. var bottom = rect.bottom;
  12267. var _x, _y;
  12268. for (i = 0; i < n; ++i) {
  12269. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  12270. ele.render(context, x, y);
  12271. }
  12272. }
  12273. }
  12274. else {
  12275. for (var i = 0; i < n; ++i)
  12276. (ele = childs[i])._visible && ele.render(context, x, y);
  12277. }
  12278. context.restoreTransform(LayaGLQuickRunner.curMat);
  12279. textLastRender && context.drawCallOptimize(false);
  12280. }
  12281. static drawLayaGL_drawNodes(sprite, context, x, y) {
  12282. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  12283. var style = sprite._style;
  12284. x = x - style.pivotX;
  12285. y = y - style.pivotY;
  12286. sprite._graphics && sprite._graphics._render(sprite, context, x, y);
  12287. var childs = sprite._children, n = childs.length, ele;
  12288. if (style.viewport) {
  12289. var rect = style.viewport;
  12290. var left = rect.x;
  12291. var top = rect.y;
  12292. var right = rect.right;
  12293. var bottom = rect.bottom;
  12294. var _x, _y;
  12295. for (i = 0; i < n; ++i) {
  12296. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  12297. ele.render(context, x, y);
  12298. }
  12299. }
  12300. }
  12301. else {
  12302. for (var i = 0; i < n; ++i)
  12303. (ele = childs[i])._visible && ele.render(context, x, y);
  12304. }
  12305. textLastRender && context.drawCallOptimize(false);
  12306. }
  12307. }
  12308. LayaGLQuickRunner.map = [];
  12309. LayaGLQuickRunner.curMat = new Matrix();
  12310. class RenderSprite {
  12311. constructor(type, next) {
  12312. if (LayaGLQuickRunner.map[type]) {
  12313. this._fun = LayaGLQuickRunner.map[type];
  12314. this._next = RenderSprite.NORENDER;
  12315. return;
  12316. }
  12317. this._next = next || RenderSprite.NORENDER;
  12318. switch (type) {
  12319. case 0:
  12320. this._fun = this._no;
  12321. return;
  12322. case SpriteConst.ALPHA:
  12323. this._fun = this._alpha;
  12324. return;
  12325. case SpriteConst.TRANSFORM:
  12326. this._fun = this._transform;
  12327. return;
  12328. case SpriteConst.BLEND:
  12329. this._fun = this._blend;
  12330. return;
  12331. case SpriteConst.CANVAS:
  12332. this._fun = this._canvas;
  12333. return;
  12334. case SpriteConst.MASK:
  12335. this._fun = this._mask;
  12336. return;
  12337. case SpriteConst.CLIP:
  12338. this._fun = this._clip;
  12339. return;
  12340. case SpriteConst.STYLE:
  12341. this._fun = this._style;
  12342. return;
  12343. case SpriteConst.GRAPHICS:
  12344. this._fun = this._graphics;
  12345. return;
  12346. case SpriteConst.CHILDS:
  12347. this._fun = this._children;
  12348. return;
  12349. case SpriteConst.CUSTOM:
  12350. this._fun = this._custom;
  12351. return;
  12352. case SpriteConst.TEXTURE:
  12353. this._fun = this._texture;
  12354. return;
  12355. case SpriteConst.FILTERS:
  12356. this._fun = Filter._filter;
  12357. return;
  12358. case RenderSprite.INIT:
  12359. this._fun = RenderSprite._initRenderFun;
  12360. return;
  12361. }
  12362. this.onCreate(type);
  12363. }
  12364. static __init__() {
  12365. LayaGLQuickRunner.__init__();
  12366. var i, len;
  12367. var initRender;
  12368. initRender = new RenderSprite(RenderSprite.INIT, null);
  12369. len = RenderSprite.renders.length = SpriteConst.CHILDS * 2;
  12370. for (i = 0; i < len; i++)
  12371. RenderSprite.renders[i] = initRender;
  12372. RenderSprite.renders[0] = new RenderSprite(0, null);
  12373. }
  12374. static _initRenderFun(sprite, context, x, y) {
  12375. var type = sprite._renderType;
  12376. var r = RenderSprite.renders[type] = RenderSprite._getTypeRender(type);
  12377. r._fun(sprite, context, x, y);
  12378. }
  12379. static _getTypeRender(type) {
  12380. if (LayaGLQuickRunner.map[type])
  12381. return new RenderSprite(type, null);
  12382. var rst = null;
  12383. var tType = SpriteConst.CHILDS;
  12384. while (tType > 0) {
  12385. if (tType & type)
  12386. rst = new RenderSprite(tType, rst);
  12387. tType = tType >> 1;
  12388. }
  12389. return rst;
  12390. }
  12391. onCreate(type) {
  12392. }
  12393. _style(sprite, context, x, y) {
  12394. var style = sprite._style;
  12395. if (style.render != null)
  12396. style.render(sprite, context, x, y);
  12397. var next = this._next;
  12398. next._fun.call(next, sprite, context, x, y);
  12399. }
  12400. _no(sprite, context, x, y) {
  12401. }
  12402. _custom(sprite, context, x, y) {
  12403. sprite.customRender(context, x, y);
  12404. this._next._fun.call(this._next, sprite, context, 0, 0);
  12405. }
  12406. _clip(sprite, context, x, y) {
  12407. var next = this._next;
  12408. if (next == RenderSprite.NORENDER)
  12409. return;
  12410. var r = sprite._style.scrollRect;
  12411. var width = r.width;
  12412. var height = r.height;
  12413. if (width === 0 || height === 0) {
  12414. return;
  12415. }
  12416. context.save();
  12417. context.clipRect(x, y, width, height);
  12418. next._fun.call(next, sprite, context, x - r.x, y - r.y);
  12419. context.restore();
  12420. }
  12421. _texture(sprite, context, x, y) {
  12422. var tex = sprite.texture;
  12423. if (tex._getSource()) {
  12424. var width = sprite._width || tex.sourceWidth;
  12425. var height = sprite._height || tex.sourceHeight;
  12426. var wRate = width / tex.sourceWidth;
  12427. var hRate = height / tex.sourceHeight;
  12428. width = tex.width * wRate;
  12429. height = tex.height * hRate;
  12430. if (width <= 0 || height <= 0)
  12431. return;
  12432. var px = x - sprite.pivotX + tex.offsetX * wRate;
  12433. var py = y - sprite.pivotY + tex.offsetY * hRate;
  12434. context.drawTexture(tex, px, py, width, height);
  12435. }
  12436. var next = this._next;
  12437. if (next != RenderSprite.NORENDER)
  12438. next._fun.call(next, sprite, context, x, y);
  12439. }
  12440. _graphics(sprite, context, x, y) {
  12441. var style = sprite._style;
  12442. var g = sprite._graphics;
  12443. g && g._render(sprite, context, x - style.pivotX, y - style.pivotY);
  12444. var next = this._next;
  12445. if (next != RenderSprite.NORENDER)
  12446. next._fun.call(next, sprite, context, x, y);
  12447. }
  12448. _image(sprite, context, x, y) {
  12449. var style = sprite._style;
  12450. context.drawTexture2(x, y, style.pivotX, style.pivotY, sprite.transform, sprite._graphics._one);
  12451. }
  12452. _image2(sprite, context, x, y) {
  12453. var style = sprite._style;
  12454. context.drawTexture2(x, y, style.pivotX, style.pivotY, sprite.transform, sprite._graphics._one);
  12455. }
  12456. _alpha(sprite, context, x, y) {
  12457. var style = sprite._style;
  12458. var alpha;
  12459. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  12460. var temp = context.globalAlpha;
  12461. context.globalAlpha *= alpha;
  12462. var next = this._next;
  12463. next._fun.call(next, sprite, context, x, y);
  12464. context.globalAlpha = temp;
  12465. }
  12466. }
  12467. _transform(sprite, context, x, y) {
  12468. var transform = sprite.transform, _next = this._next;
  12469. var style = sprite._style;
  12470. if (transform && _next != RenderSprite.NORENDER) {
  12471. context.save();
  12472. context.transform(transform.a, transform.b, transform.c, transform.d, transform.tx + x, transform.ty + y);
  12473. _next._fun.call(_next, sprite, context, 0, 0);
  12474. context.restore();
  12475. }
  12476. else {
  12477. if (_next != RenderSprite.NORENDER)
  12478. _next._fun.call(_next, sprite, context, x, y);
  12479. }
  12480. }
  12481. _children(sprite, context, x, y) {
  12482. var style = sprite._style;
  12483. var childs = sprite._children, n = childs.length, ele;
  12484. x = x - sprite.pivotX;
  12485. y = y - sprite.pivotY;
  12486. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  12487. if (style.viewport) {
  12488. var rect = style.viewport;
  12489. var left = rect.x;
  12490. var top = rect.y;
  12491. var right = rect.right;
  12492. var bottom = rect.bottom;
  12493. var _x, _y;
  12494. for (i = 0; i < n; ++i) {
  12495. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  12496. ele.render(context, x, y);
  12497. }
  12498. }
  12499. }
  12500. else {
  12501. for (var i = 0; i < n; ++i)
  12502. (ele = childs[i])._visible && ele.render(context, x, y);
  12503. }
  12504. textLastRender && context.drawCallOptimize(false);
  12505. }
  12506. _canvas(sprite, context, x, y) {
  12507. var _cacheStyle = sprite._cacheStyle;
  12508. var _next = this._next;
  12509. if (!_cacheStyle.enableCanvasRender) {
  12510. _next._fun.call(_next, sprite, context, x, y);
  12511. return;
  12512. }
  12513. _cacheStyle.cacheAs === 'bitmap' ? (Stat.canvasBitmap++) : (Stat.canvasNormal++);
  12514. var cacheNeedRebuild = false;
  12515. var textNeedRestore = false;
  12516. if (_cacheStyle.canvas) {
  12517. var canv = _cacheStyle.canvas;
  12518. var ctx = canv.context;
  12519. var charRIs = canv.touches;
  12520. if (charRIs) {
  12521. for (var ci = 0; ci < charRIs.length; ci++) {
  12522. if (charRIs[ci].deleted) {
  12523. textNeedRestore = true;
  12524. break;
  12525. }
  12526. }
  12527. }
  12528. cacheNeedRebuild = canv.isCacheValid && !canv.isCacheValid();
  12529. }
  12530. if (sprite._needRepaint() || (!_cacheStyle.canvas) || textNeedRestore || cacheNeedRebuild || ILaya.stage.isGlobalRepaint()) {
  12531. if (_cacheStyle.cacheAs === 'normal') {
  12532. if (context._targets) {
  12533. _next._fun.call(_next, sprite, context, x, y);
  12534. return;
  12535. }
  12536. else {
  12537. this._canvas_webgl_normal_repaint(sprite, context);
  12538. }
  12539. }
  12540. else {
  12541. this._canvas_repaint(sprite, context, x, y);
  12542. }
  12543. }
  12544. var tRec = _cacheStyle.cacheRect;
  12545. context.drawCanvas(_cacheStyle.canvas, x + tRec.x, y + tRec.y, tRec.width, tRec.height);
  12546. }
  12547. _canvas_repaint(sprite, context, x, y) {
  12548. var _cacheStyle = sprite._cacheStyle;
  12549. var _next = this._next;
  12550. var tx;
  12551. var canvas = _cacheStyle.canvas;
  12552. var left;
  12553. var top;
  12554. var tRec;
  12555. var tCacheType = _cacheStyle.cacheAs;
  12556. var w, h;
  12557. var scaleX, scaleY;
  12558. var scaleInfo;
  12559. scaleInfo = _cacheStyle._calculateCacheRect(sprite, tCacheType, x, y);
  12560. scaleX = scaleInfo.x;
  12561. scaleY = scaleInfo.y;
  12562. tRec = _cacheStyle.cacheRect;
  12563. w = tRec.width * scaleX;
  12564. h = tRec.height * scaleY;
  12565. left = tRec.x;
  12566. top = tRec.y;
  12567. if (tCacheType === 'bitmap' && (w > 2048 || h > 2048)) {
  12568. console.warn("cache bitmap size larger than 2048,cache ignored");
  12569. _cacheStyle.releaseContext();
  12570. _next._fun.call(_next, sprite, context, x, y);
  12571. return;
  12572. }
  12573. if (!canvas) {
  12574. _cacheStyle.createContext();
  12575. canvas = _cacheStyle.canvas;
  12576. }
  12577. tx = canvas.context;
  12578. tx.sprite = sprite;
  12579. (canvas.width != w || canvas.height != h) && canvas.size(w, h);
  12580. if (tCacheType === 'bitmap')
  12581. tx.asBitmap = true;
  12582. else if (tCacheType === 'normal')
  12583. tx.asBitmap = false;
  12584. tx.clear();
  12585. if (scaleX != 1 || scaleY != 1) {
  12586. var ctx = tx;
  12587. ctx.save();
  12588. ctx.scale(scaleX, scaleY);
  12589. _next._fun.call(_next, sprite, tx, -left, -top);
  12590. ctx.restore();
  12591. sprite._applyFilters();
  12592. }
  12593. else {
  12594. ctx = tx;
  12595. _next._fun.call(_next, sprite, tx, -left, -top);
  12596. sprite._applyFilters();
  12597. }
  12598. if (_cacheStyle.staticCache)
  12599. _cacheStyle.reCache = false;
  12600. Stat.canvasReCache++;
  12601. }
  12602. _canvas_webgl_normal_repaint(sprite, context) {
  12603. var _cacheStyle = sprite._cacheStyle;
  12604. var _next = this._next;
  12605. var canvas = _cacheStyle.canvas;
  12606. var tCacheType = _cacheStyle.cacheAs;
  12607. _cacheStyle._calculateCacheRect(sprite, tCacheType, 0, 0);
  12608. if (!canvas) {
  12609. canvas = new WebGLCacheAsNormalCanvas(context, sprite);
  12610. _cacheStyle.canvas = canvas;
  12611. }
  12612. var tx = canvas.context;
  12613. canvas.startRec();
  12614. _next._fun.call(_next, sprite, tx, sprite.pivotX, sprite.pivotY);
  12615. sprite._applyFilters();
  12616. Stat.canvasReCache++;
  12617. canvas.endRec();
  12618. }
  12619. _blend(sprite, context, x, y) {
  12620. var style = sprite._style;
  12621. var next = this._next;
  12622. if (style.blendMode) {
  12623. context.save();
  12624. context.globalCompositeOperation = style.blendMode;
  12625. next._fun.call(next, sprite, context, x, y);
  12626. context.restore();
  12627. }
  12628. else {
  12629. next._fun.call(next, sprite, context, x, y);
  12630. }
  12631. }
  12632. _mask(sprite, context, x, y) {
  12633. var next = this._next;
  12634. var mask = sprite.mask;
  12635. var ctx = context;
  12636. if (mask) {
  12637. ctx.save();
  12638. var preBlendMode = ctx.globalCompositeOperation;
  12639. var tRect = new Rectangle();
  12640. tRect.copyFrom(mask.getBounds());
  12641. tRect.width = Math.round(tRect.width);
  12642. tRect.height = Math.round(tRect.height);
  12643. tRect.x = Math.round(tRect.x);
  12644. tRect.y = Math.round(tRect.y);
  12645. if (tRect.width > 0 && tRect.height > 0) {
  12646. var w = tRect.width;
  12647. var h = tRect.height;
  12648. var tmpRT = WebGLRTMgr.getRT(w, h);
  12649. ctx.breakNextMerge();
  12650. ctx.pushRT();
  12651. ctx.addRenderObject(SubmitCMD.create([ctx, tmpRT, w, h], RenderSprite.tmpTarget, this));
  12652. mask.render(ctx, -tRect.x, -tRect.y);
  12653. ctx.breakNextMerge();
  12654. ctx.popRT();
  12655. ctx.save();
  12656. let shrink = 0.1;
  12657. ctx.clipRect(x + tRect.x - sprite.getStyle().pivotX + shrink, y + tRect.y - sprite.getStyle().pivotY + shrink, w - shrink * 2, h - shrink * 2);
  12658. next._fun.call(next, sprite, ctx, x, y);
  12659. ctx.restore();
  12660. preBlendMode = ctx.globalCompositeOperation;
  12661. ctx.addRenderObject(SubmitCMD.create(["mask"], RenderSprite.setBlendMode, this));
  12662. var shaderValue = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  12663. var uv = Texture.INV_UV;
  12664. ctx.drawTarget(tmpRT, x + tRect.x - sprite.getStyle().pivotX, y + tRect.y - sprite.getStyle().pivotY, w, h, Matrix.TEMP.identity(), shaderValue, uv, 6);
  12665. ctx.addRenderObject(SubmitCMD.create([tmpRT], RenderSprite.recycleTarget, this));
  12666. ctx.addRenderObject(SubmitCMD.create([preBlendMode], RenderSprite.setBlendMode, this));
  12667. }
  12668. ctx.restore();
  12669. }
  12670. else {
  12671. next._fun.call(next, sprite, context, x, y);
  12672. }
  12673. }
  12674. static tmpTarget(ctx, rt, w, h) {
  12675. rt.start();
  12676. rt.clear(0, 0, 0, 0);
  12677. }
  12678. static recycleTarget(rt) {
  12679. WebGLRTMgr.releaseRT(rt);
  12680. }
  12681. static setBlendMode(blendMode) {
  12682. var gl = WebGLContext.mainContext;
  12683. BlendMode.targetFns[BlendMode.TOINT[blendMode]](gl);
  12684. }
  12685. }
  12686. RenderSprite.INIT = 0x11111;
  12687. RenderSprite.renders = [];
  12688. RenderSprite.NORENDER = new RenderSprite(0, null);
  12689. RenderSprite.tempUV = new Array(8);
  12690. class HTMLCanvas extends Bitmap {
  12691. constructor(createCanvas = false) {
  12692. super();
  12693. if (createCanvas)
  12694. this._source = Browser.createElement("canvas");
  12695. else {
  12696. this._source = this;
  12697. }
  12698. this.lock = true;
  12699. }
  12700. get source() {
  12701. return this._source;
  12702. }
  12703. _getSource() {
  12704. return this._source;
  12705. }
  12706. clear() {
  12707. if (this._ctx) {
  12708. if (this._ctx.clear) {
  12709. this._ctx.clear();
  12710. }
  12711. else {
  12712. this._ctx.clearRect(0, 0, this._width, this._height);
  12713. }
  12714. }
  12715. if (this._texture) {
  12716. this._texture.destroy();
  12717. this._texture = null;
  12718. }
  12719. }
  12720. destroy() {
  12721. super.destroy();
  12722. this._setCPUMemory(0);
  12723. this._ctx && this._ctx.destroy && this._ctx.destroy();
  12724. this._ctx = null;
  12725. }
  12726. release() {
  12727. }
  12728. get context() {
  12729. if (this._ctx)
  12730. return this._ctx;
  12731. if (this._source == this) {
  12732. this._ctx = new ILaya.Context();
  12733. }
  12734. else {
  12735. this._ctx = this._source.getContext(ILaya.Render.isConchApp ? 'layagl' : '2d');
  12736. }
  12737. this._ctx._canvas = this;
  12738. return this._ctx;
  12739. }
  12740. _setContext(context) {
  12741. this._ctx = context;
  12742. }
  12743. getContext(contextID, other = null) {
  12744. return this.context;
  12745. }
  12746. getMemSize() {
  12747. return 0;
  12748. }
  12749. size(w, h) {
  12750. if (this._width != w || this._height != h || (this._source && (this._source.width != w || this._source.height != h))) {
  12751. this._width = w;
  12752. this._height = h;
  12753. this._setCPUMemory(w * h * 4);
  12754. this._ctx && this._ctx.size && this._ctx.size(w, h);
  12755. if (this._source) {
  12756. this._source.height = h;
  12757. this._source.width = w;
  12758. }
  12759. if (this._texture) {
  12760. this._texture.destroy();
  12761. this._texture = null;
  12762. }
  12763. }
  12764. }
  12765. getTexture() {
  12766. if (!this._texture) {
  12767. var bitmap = new Texture2D();
  12768. bitmap.loadImageSource(this.source);
  12769. this._texture = new Texture(bitmap);
  12770. }
  12771. return this._texture;
  12772. }
  12773. toBase64(type, encoderOptions) {
  12774. if (this._source) {
  12775. if (ILaya.Render.isConchApp) {
  12776. var win = window;
  12777. if (win.conchConfig.threadMode == 2) {
  12778. throw "native 2 thread mode use toBase64Async";
  12779. }
  12780. var width = this._ctx._targets.sourceWidth;
  12781. var height = this._ctx._targets.sourceHeight;
  12782. var data = this._ctx._targets.getData(0, 0, width, height);
  12783. return win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
  12784. }
  12785. else {
  12786. return this._source.toDataURL(type, encoderOptions);
  12787. }
  12788. }
  12789. return null;
  12790. }
  12791. toBase64Async(type, encoderOptions, callBack) {
  12792. var width = this._ctx._targets.sourceWidth;
  12793. var height = this._ctx._targets.sourceHeight;
  12794. this._ctx._targets.getDataAsync(0, 0, width, height, function (data) {
  12795. let win = window;
  12796. var base64 = win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
  12797. callBack(base64);
  12798. });
  12799. }
  12800. }
  12801. class HitArea {
  12802. contains(x, y) {
  12803. if (!HitArea._isHitGraphic(x, y, this.hit))
  12804. return false;
  12805. return !HitArea._isHitGraphic(x, y, this.unHit);
  12806. }
  12807. static _isHitGraphic(x, y, graphic) {
  12808. if (!graphic)
  12809. return false;
  12810. var cmds = graphic.cmds;
  12811. if (!cmds && graphic._one) {
  12812. cmds = HitArea._cmds;
  12813. cmds.length = 1;
  12814. cmds[0] = graphic._one;
  12815. }
  12816. if (!cmds)
  12817. return false;
  12818. var i, len;
  12819. len = cmds.length;
  12820. var cmd;
  12821. for (i = 0; i < len; i++) {
  12822. cmd = cmds[i];
  12823. if (!cmd)
  12824. continue;
  12825. switch (cmd.cmdID) {
  12826. case "Translate":
  12827. x -= cmd.tx;
  12828. y -= cmd.ty;
  12829. }
  12830. if (HitArea._isHitCmd(x, y, cmd))
  12831. return true;
  12832. }
  12833. return false;
  12834. }
  12835. static _isHitCmd(x, y, cmd) {
  12836. if (!cmd)
  12837. return false;
  12838. var rst = false;
  12839. switch (cmd.cmdID) {
  12840. case "DrawRect":
  12841. HitArea._rect.setTo(cmd.x, cmd.y, cmd.width, cmd.height);
  12842. rst = HitArea._rect.contains(x, y);
  12843. break;
  12844. case "DrawCircle":
  12845. var d;
  12846. x -= cmd.x;
  12847. y -= cmd.y;
  12848. d = x * x + y * y;
  12849. rst = d < cmd.radius * cmd.radius;
  12850. break;
  12851. case "DrawPoly":
  12852. x -= cmd.x;
  12853. y -= cmd.y;
  12854. rst = HitArea._ptInPolygon(x, y, cmd.points);
  12855. break;
  12856. }
  12857. return rst;
  12858. }
  12859. static _ptInPolygon(x, y, areaPoints) {
  12860. var p = HitArea._ptPoint;
  12861. p.setTo(x, y);
  12862. var nCross = 0;
  12863. var p1x, p1y, p2x, p2y;
  12864. var len;
  12865. len = areaPoints.length;
  12866. for (var i = 0; i < len; i += 2) {
  12867. p1x = areaPoints[i];
  12868. p1y = areaPoints[i + 1];
  12869. p2x = areaPoints[(i + 2) % len];
  12870. p2y = areaPoints[(i + 3) % len];
  12871. if (p1y == p2y)
  12872. continue;
  12873. if (p.y < Math.min(p1y, p2y))
  12874. continue;
  12875. if (p.y >= Math.max(p1y, p2y))
  12876. continue;
  12877. var tx = (p.y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x;
  12878. if (tx > p.x)
  12879. nCross++;
  12880. }
  12881. return (nCross % 2 == 1);
  12882. }
  12883. get hit() {
  12884. if (!this._hit)
  12885. this._hit = new ILaya.Graphics();
  12886. return this._hit;
  12887. }
  12888. set hit(value) {
  12889. this._hit = value;
  12890. }
  12891. get unHit() {
  12892. if (!this._unHit)
  12893. this._unHit = new ILaya.Graphics();
  12894. return this._unHit;
  12895. }
  12896. set unHit(value) {
  12897. this._unHit = value;
  12898. }
  12899. }
  12900. HitArea._cmds = [];
  12901. HitArea._rect = new Rectangle();
  12902. HitArea._ptPoint = new Point();
  12903. class ClassUtils {
  12904. static regClass(className, classDef) {
  12905. ClassUtils._classMap[className] = classDef;
  12906. }
  12907. static regShortClassName(classes) {
  12908. for (var i = 0; i < classes.length; i++) {
  12909. var classDef = classes[i];
  12910. var className = classDef.name;
  12911. ClassUtils._classMap[className] = classDef;
  12912. }
  12913. }
  12914. static getRegClass(className) {
  12915. return ClassUtils._classMap[className];
  12916. }
  12917. static getClass(className) {
  12918. var classObject = ClassUtils._classMap[className] || ClassUtils._classMap['Laya.' + className] || className;
  12919. var glaya = ILaya.Laya;
  12920. if (typeof (classObject) == 'string')
  12921. return (ILaya.__classMap[classObject] || glaya[className]);
  12922. return classObject;
  12923. }
  12924. static getInstance(className) {
  12925. var compClass = ClassUtils.getClass(className);
  12926. if (compClass)
  12927. return new compClass();
  12928. else
  12929. console.warn("[error] Undefined class:", className);
  12930. return null;
  12931. }
  12932. static createByJson(json, node = null, root = null, customHandler = null, instanceHandler = null) {
  12933. if (typeof (json) == 'string')
  12934. json = JSON.parse(json);
  12935. var props = json.props;
  12936. if (!node) {
  12937. node = instanceHandler ? instanceHandler.runWith(json) : ClassUtils.getInstance(props.runtime || json.type);
  12938. if (!node)
  12939. return null;
  12940. }
  12941. var child = json.child;
  12942. if (child) {
  12943. for (var i = 0, n = child.length; i < n; i++) {
  12944. var data = child[i];
  12945. if ((data.props.name === "render" || data.props.renderType === "render") && node["_$set_itemRender"])
  12946. node.itemRender = data;
  12947. else {
  12948. if (data.type == "Graphic") {
  12949. ClassUtils._addGraphicsToSprite(data, node);
  12950. }
  12951. else if (ClassUtils._isDrawType(data.type)) {
  12952. ClassUtils._addGraphicToSprite(data, node, true);
  12953. }
  12954. else {
  12955. var tChild = ClassUtils.createByJson(data, null, root, customHandler, instanceHandler);
  12956. if (data.type === "Script") {
  12957. if ("owner" in tChild) {
  12958. tChild["owner"] = node;
  12959. }
  12960. else if ("target" in tChild) {
  12961. tChild["target"] = node;
  12962. }
  12963. }
  12964. else if (data.props.renderType == "mask") {
  12965. node.mask = tChild;
  12966. }
  12967. else {
  12968. node.addChild(tChild);
  12969. }
  12970. }
  12971. }
  12972. }
  12973. }
  12974. if (props) {
  12975. for (var prop in props) {
  12976. var value = props[prop];
  12977. if (prop === "var" && root) {
  12978. root[value] = node;
  12979. }
  12980. else if (value instanceof Array && node[prop] instanceof Function) {
  12981. node[prop].apply(node, value);
  12982. }
  12983. else {
  12984. node[prop] = value;
  12985. }
  12986. }
  12987. }
  12988. if (customHandler && json.customProps) {
  12989. customHandler.runWith([node, json]);
  12990. }
  12991. if (node["created"])
  12992. node.created();
  12993. return node;
  12994. }
  12995. static _addGraphicsToSprite(graphicO, sprite) {
  12996. var graphics = graphicO.child;
  12997. if (!graphics || graphics.length < 1)
  12998. return;
  12999. var g = ClassUtils._getGraphicsFromSprite(graphicO, sprite);
  13000. var ox = 0;
  13001. var oy = 0;
  13002. if (graphicO.props) {
  13003. ox = ClassUtils._getObjVar(graphicO.props, "x", 0);
  13004. oy = ClassUtils._getObjVar(graphicO.props, "y", 0);
  13005. }
  13006. if (ox != 0 && oy != 0) {
  13007. g.translate(ox, oy);
  13008. }
  13009. var i, len;
  13010. len = graphics.length;
  13011. for (i = 0; i < len; i++) {
  13012. ClassUtils._addGraphicToGraphics(graphics[i], g);
  13013. }
  13014. if (ox != 0 && oy != 0) {
  13015. g.translate(-ox, -oy);
  13016. }
  13017. }
  13018. static _addGraphicToSprite(graphicO, sprite, isChild = false) {
  13019. var g = isChild ? ClassUtils._getGraphicsFromSprite(graphicO, sprite) : sprite.graphics;
  13020. ClassUtils._addGraphicToGraphics(graphicO, g);
  13021. }
  13022. static _getGraphicsFromSprite(dataO, sprite) {
  13023. if (!dataO || !dataO.props)
  13024. return sprite.graphics;
  13025. var propsName = dataO.props.renderType;
  13026. if (propsName === "hit" || propsName === "unHit") {
  13027. var hitArea = sprite._style.hitArea || (sprite.hitArea = new HitArea());
  13028. if (!hitArea[propsName]) {
  13029. hitArea[propsName] = new Graphics();
  13030. }
  13031. var g = hitArea[propsName];
  13032. }
  13033. if (!g)
  13034. g = sprite.graphics;
  13035. return g;
  13036. }
  13037. static _getTransformData(propsO) {
  13038. var m;
  13039. if ("pivotX" in propsO || "pivotY" in propsO) {
  13040. m = m || new Matrix();
  13041. m.translate(-ClassUtils._getObjVar(propsO, "pivotX", 0), -ClassUtils._getObjVar(propsO, "pivotY", 0));
  13042. }
  13043. var sx = ClassUtils._getObjVar(propsO, "scaleX", 1), sy = ClassUtils._getObjVar(propsO, "scaleY", 1);
  13044. var rotate = ClassUtils._getObjVar(propsO, "rotation", 0);
  13045. var skewX = ClassUtils._getObjVar(propsO, "skewX", 0);
  13046. var skewY = ClassUtils._getObjVar(propsO, "skewY", 0);
  13047. if (sx != 1 || sy != 1 || rotate != 0) {
  13048. m = m || new Matrix();
  13049. m.scale(sx, sy);
  13050. m.rotate(rotate * 0.0174532922222222);
  13051. }
  13052. return m;
  13053. }
  13054. static _addGraphicToGraphics(graphicO, graphic) {
  13055. var propsO;
  13056. propsO = graphicO.props;
  13057. if (!propsO)
  13058. return;
  13059. var drawConfig;
  13060. drawConfig = ClassUtils.DrawTypeDic[graphicO.type];
  13061. if (!drawConfig)
  13062. return;
  13063. var g = graphic;
  13064. var params = ClassUtils._getParams(propsO, drawConfig[1], drawConfig[2], drawConfig[3]);
  13065. var m = ClassUtils._tM;
  13066. if (m || ClassUtils._alpha != 1) {
  13067. g.save();
  13068. if (m)
  13069. g.transform(m);
  13070. if (ClassUtils._alpha != 1)
  13071. g.alpha(ClassUtils._alpha);
  13072. }
  13073. g[drawConfig[0]].apply(g, params);
  13074. if (m || ClassUtils._alpha != 1) {
  13075. g.restore();
  13076. }
  13077. }
  13078. static _adptLineData(params) {
  13079. params[2] = parseFloat(params[0]) + parseFloat(params[2]);
  13080. params[3] = parseFloat(params[1]) + parseFloat(params[3]);
  13081. return params;
  13082. }
  13083. static _adptTextureData(params) {
  13084. params[0] = ILaya.Loader.getRes(params[0]);
  13085. return params;
  13086. }
  13087. static _adptLinesData(params) {
  13088. params[2] = ClassUtils._getPointListByStr(params[2]);
  13089. return params;
  13090. }
  13091. static _isDrawType(type) {
  13092. if (type === "Image")
  13093. return false;
  13094. return type in ClassUtils.DrawTypeDic;
  13095. }
  13096. static _getParams(obj, params, xPos = 0, adptFun = null) {
  13097. var rst = ClassUtils._temParam;
  13098. rst.length = params.length;
  13099. var i, len;
  13100. len = params.length;
  13101. for (i = 0; i < len; i++) {
  13102. rst[i] = ClassUtils._getObjVar(obj, params[i][0], params[i][1]);
  13103. }
  13104. ClassUtils._alpha = ClassUtils._getObjVar(obj, "alpha", 1);
  13105. var m;
  13106. m = ClassUtils._getTransformData(obj);
  13107. if (m) {
  13108. if (!xPos)
  13109. xPos = 0;
  13110. m.translate(rst[xPos], rst[xPos + 1]);
  13111. rst[xPos] = rst[xPos + 1] = 0;
  13112. ClassUtils._tM = m;
  13113. }
  13114. else {
  13115. ClassUtils._tM = null;
  13116. }
  13117. if (adptFun && ClassUtils[adptFun]) {
  13118. rst = ClassUtils[adptFun](rst);
  13119. }
  13120. return rst;
  13121. }
  13122. static _getPointListByStr(str) {
  13123. var pointArr = str.split(",");
  13124. var i, len;
  13125. len = pointArr.length;
  13126. for (i = 0; i < len; i++) {
  13127. pointArr[i] = parseFloat(pointArr[i]);
  13128. }
  13129. return pointArr;
  13130. }
  13131. static _getObjVar(obj, key, noValue) {
  13132. if (key in obj) {
  13133. return obj[key];
  13134. }
  13135. return noValue;
  13136. }
  13137. }
  13138. ClassUtils.DrawTypeDic = { "Rect": ["drawRect", [["x", 0], ["y", 0], ["width", 0], ["height", 0], ["fillColor", null], ["lineColor", null], ["lineWidth", 1]]], "Circle": ["drawCircle", [["x", 0], ["y", 0], ["radius", 0], ["fillColor", null], ["lineColor", null], ["lineWidth", 1]]], "Pie": ["drawPie", [["x", 0], ["y", 0], ["radius", 0], ["startAngle", 0], ["endAngle", 0], ["fillColor", null], ["lineColor", null], ["lineWidth", 1]]], "Image": ["drawTexture", [["x", 0], ["y", 0], ["width", 0], ["height", 0]]], "Texture": ["drawTexture", [["skin", null], ["x", 0], ["y", 0], ["width", 0], ["height", 0]], 1, "_adptTextureData"], "FillTexture": ["fillTexture", [["skin", null], ["x", 0], ["y", 0], ["width", 0], ["height", 0], ["repeat", null]], 1, "_adptTextureData"], "FillText": ["fillText", [["text", ""], ["x", 0], ["y", 0], ["font", null], ["color", null], ["textAlign", null]], 1], "Line": ["drawLine", [["x", 0], ["y", 0], ["toX", 0], ["toY", 0], ["lineColor", null], ["lineWidth", 0]], 0, "_adptLineData"], "Lines": ["drawLines", [["x", 0], ["y", 0], ["points", ""], ["lineColor", null], ["lineWidth", 0]], 0, "_adptLinesData"], "Curves": ["drawCurves", [["x", 0], ["y", 0], ["points", ""], ["lineColor", null], ["lineWidth", 0]], 0, "_adptLinesData"], "Poly": ["drawPoly", [["x", 0], ["y", 0], ["points", ""], ["fillColor", null], ["lineColor", null], ["lineWidth", 1]], 0, "_adptLinesData"] };
  13139. ClassUtils._temParam = [];
  13140. ClassUtils._classMap = {};
  13141. class BoundsStyle {
  13142. reset() {
  13143. if (this.bounds)
  13144. this.bounds.recover();
  13145. if (this.userBounds)
  13146. this.userBounds.recover();
  13147. this.bounds = null;
  13148. this.userBounds = null;
  13149. this.temBM = null;
  13150. return this;
  13151. }
  13152. recover() {
  13153. Pool.recover("BoundsStyle", this.reset());
  13154. }
  13155. static create() {
  13156. return Pool.getItemByClass("BoundsStyle", BoundsStyle);
  13157. }
  13158. }
  13159. class CacheStyle {
  13160. constructor() {
  13161. this.reset();
  13162. }
  13163. needBitmapCache() {
  13164. return this.cacheForFilters || !!this.mask;
  13165. }
  13166. needEnableCanvasRender() {
  13167. return this.userSetCache != "none" || this.cacheForFilters || !!this.mask;
  13168. }
  13169. releaseContext() {
  13170. if (this.canvas && this.canvas.size) {
  13171. Pool.recover("CacheCanvas", this.canvas);
  13172. this.canvas.size(0, 0);
  13173. try {
  13174. this.canvas.width = 0;
  13175. this.canvas.height = 0;
  13176. }
  13177. catch (e) {
  13178. }
  13179. }
  13180. this.canvas = null;
  13181. }
  13182. createContext() {
  13183. if (!this.canvas) {
  13184. this.canvas = Pool.getItem("CacheCanvas") || new HTMLCanvas(false);
  13185. var tx = this.canvas.context;
  13186. if (!tx) {
  13187. tx = this.canvas.getContext('2d');
  13188. }
  13189. }
  13190. }
  13191. releaseFilterCache() {
  13192. var fc = this.filterCache;
  13193. if (fc) {
  13194. fc.destroy();
  13195. fc.recycle();
  13196. this.filterCache = null;
  13197. }
  13198. }
  13199. recover() {
  13200. if (this === CacheStyle.EMPTY)
  13201. return;
  13202. Pool.recover("SpriteCache", this.reset());
  13203. }
  13204. reset() {
  13205. this.releaseContext();
  13206. this.releaseFilterCache();
  13207. this.cacheAs = "none";
  13208. this.enableCanvasRender = false;
  13209. this.userSetCache = "none";
  13210. this.cacheForFilters = false;
  13211. this.staticCache = false;
  13212. this.reCache = true;
  13213. this.mask = null;
  13214. this.maskParent = null;
  13215. this.filterCache = null;
  13216. this.filters = null;
  13217. this.hasGlowFilter = false;
  13218. if (this.cacheRect)
  13219. this.cacheRect.recover();
  13220. this.cacheRect = null;
  13221. return this;
  13222. }
  13223. static create() {
  13224. return Pool.getItemByClass("SpriteCache", CacheStyle);
  13225. }
  13226. _calculateCacheRect(sprite, tCacheType, x, y) {
  13227. var _cacheStyle = sprite._cacheStyle;
  13228. if (!_cacheStyle.cacheRect)
  13229. _cacheStyle.cacheRect = Rectangle.create();
  13230. var tRec;
  13231. if (tCacheType === "bitmap") {
  13232. tRec = sprite.getSelfBounds();
  13233. tRec.width = tRec.width + CacheStyle.CANVAS_EXTEND_EDGE * 2;
  13234. tRec.height = tRec.height + CacheStyle.CANVAS_EXTEND_EDGE * 2;
  13235. tRec.x = tRec.x - sprite.pivotX;
  13236. tRec.y = tRec.y - sprite.pivotY;
  13237. tRec.x = tRec.x - CacheStyle.CANVAS_EXTEND_EDGE;
  13238. tRec.y = tRec.y - CacheStyle.CANVAS_EXTEND_EDGE;
  13239. tRec.x = Math.floor(tRec.x + x) - x;
  13240. tRec.y = Math.floor(tRec.y + y) - y;
  13241. tRec.width = Math.floor(tRec.width);
  13242. tRec.height = Math.floor(tRec.height);
  13243. _cacheStyle.cacheRect.copyFrom(tRec);
  13244. }
  13245. else {
  13246. _cacheStyle.cacheRect.setTo(-sprite._style.pivotX, -sprite._style.pivotY, 1, 1);
  13247. }
  13248. tRec = _cacheStyle.cacheRect;
  13249. if (sprite._style.scrollRect) {
  13250. var scrollRect = sprite._style.scrollRect;
  13251. tRec.x -= scrollRect.x;
  13252. tRec.y -= scrollRect.y;
  13253. }
  13254. CacheStyle._scaleInfo.setTo(1, 1);
  13255. return CacheStyle._scaleInfo;
  13256. }
  13257. }
  13258. CacheStyle.EMPTY = new CacheStyle();
  13259. CacheStyle._scaleInfo = new Point();
  13260. CacheStyle.CANVAS_EXTEND_EDGE = 16;
  13261. class SpriteStyle {
  13262. constructor() {
  13263. this.reset();
  13264. }
  13265. reset() {
  13266. this.scaleX = this.scaleY = 1;
  13267. this.skewX = this.skewY = 0;
  13268. this.pivotX = this.pivotY = this.rotation = 0;
  13269. this.alpha = 1;
  13270. if (this.scrollRect)
  13271. this.scrollRect.recover();
  13272. this.scrollRect = null;
  13273. if (this.viewport)
  13274. this.viewport.recover();
  13275. this.viewport = null;
  13276. this.hitArea = null;
  13277. this.dragging = null;
  13278. this.blendMode = null;
  13279. return this;
  13280. }
  13281. recover() {
  13282. if (this === SpriteStyle.EMPTY)
  13283. return;
  13284. Pool.recover("SpriteStyle", this.reset());
  13285. }
  13286. static create() {
  13287. return Pool.getItemByClass("SpriteStyle", SpriteStyle);
  13288. }
  13289. }
  13290. SpriteStyle.EMPTY = new SpriteStyle();
  13291. class Node extends EventDispatcher {
  13292. constructor() {
  13293. super();
  13294. this._bits = 0;
  13295. this._children = Node.ARRAY_EMPTY;
  13296. this._extUIChild = Node.ARRAY_EMPTY;
  13297. this._parent = null;
  13298. this.name = "";
  13299. this.destroyed = false;
  13300. this.createGLBuffer();
  13301. }
  13302. createGLBuffer() {
  13303. }
  13304. _setBit(type, value) {
  13305. if (type === Const.DISPLAY) {
  13306. var preValue = this._getBit(type);
  13307. if (preValue != value)
  13308. this._updateDisplayedInstage();
  13309. }
  13310. if (value)
  13311. this._bits |= type;
  13312. else
  13313. this._bits &= ~type;
  13314. }
  13315. _getBit(type) {
  13316. return (this._bits & type) != 0;
  13317. }
  13318. _setUpNoticeChain() {
  13319. if (this._getBit(Const.DISPLAY))
  13320. this._setBitUp(Const.DISPLAY);
  13321. }
  13322. _setBitUp(type) {
  13323. var ele = this;
  13324. ele._setBit(type, true);
  13325. ele = ele._parent;
  13326. while (ele) {
  13327. if (ele._getBit(type))
  13328. return;
  13329. ele._setBit(type, true);
  13330. ele = ele._parent;
  13331. }
  13332. }
  13333. on(type, caller, listener, args = null) {
  13334. if (type === Event.DISPLAY || type === Event.UNDISPLAY) {
  13335. if (!this._getBit(Const.DISPLAY))
  13336. this._setBitUp(Const.DISPLAY);
  13337. }
  13338. return this._createListener(type, caller, listener, args, false);
  13339. }
  13340. once(type, caller, listener, args = null) {
  13341. if (type === Event.DISPLAY || type === Event.UNDISPLAY) {
  13342. if (!this._getBit(Const.DISPLAY))
  13343. this._setBitUp(Const.DISPLAY);
  13344. }
  13345. return this._createListener(type, caller, listener, args, true);
  13346. }
  13347. destroy(destroyChild = true) {
  13348. this.destroyed = true;
  13349. this._destroyAllComponent();
  13350. this._parent && this._parent.removeChild(this);
  13351. if (this._children) {
  13352. if (destroyChild)
  13353. this.destroyChildren();
  13354. else
  13355. this.removeChildren();
  13356. }
  13357. this.onDestroy();
  13358. this._children = null;
  13359. this.offAll();
  13360. }
  13361. onDestroy() {
  13362. }
  13363. destroyChildren() {
  13364. if (this._children) {
  13365. for (var i = 0, n = this._children.length; i < n; i++) {
  13366. this._children[0].destroy(true);
  13367. }
  13368. }
  13369. }
  13370. addChild(node) {
  13371. if (!node || this.destroyed || node === this)
  13372. return node;
  13373. if (node._zOrder)
  13374. this._setBit(Const.HAS_ZORDER, true);
  13375. if (node._parent === this) {
  13376. var index = this.getChildIndex(node);
  13377. if (index !== this._children.length - 1) {
  13378. this._children.splice(index, 1);
  13379. this._children.push(node);
  13380. this._childChanged();
  13381. }
  13382. }
  13383. else {
  13384. node._parent && node._parent.removeChild(node);
  13385. this._children === Node.ARRAY_EMPTY && (this._children = []);
  13386. this._children.push(node);
  13387. node._setParent(this);
  13388. this._childChanged();
  13389. }
  13390. return node;
  13391. }
  13392. addInputChild(node) {
  13393. if (this._extUIChild == Node.ARRAY_EMPTY) {
  13394. this._extUIChild = [node];
  13395. }
  13396. else {
  13397. if (this._extUIChild.indexOf(node) >= 0) {
  13398. return null;
  13399. }
  13400. this._extUIChild.push(node);
  13401. }
  13402. return null;
  13403. }
  13404. removeInputChild(node) {
  13405. var idx = this._extUIChild.indexOf(node);
  13406. if (idx >= 0) {
  13407. this._extUIChild.splice(idx, 1);
  13408. }
  13409. }
  13410. addChildren(...args) {
  13411. var i = 0, n = args.length;
  13412. while (i < n) {
  13413. this.addChild(args[i++]);
  13414. }
  13415. }
  13416. addChildAt(node, index) {
  13417. if (!node || this.destroyed || node === this)
  13418. return node;
  13419. if (node._zOrder)
  13420. this._setBit(Const.HAS_ZORDER, true);
  13421. if (index >= 0 && index <= this._children.length) {
  13422. if (node._parent === this) {
  13423. var oldIndex = this.getChildIndex(node);
  13424. this._children.splice(oldIndex, 1);
  13425. this._children.splice(index, 0, node);
  13426. this._childChanged();
  13427. }
  13428. else {
  13429. node._parent && node._parent.removeChild(node);
  13430. this._children === Node.ARRAY_EMPTY && (this._children = []);
  13431. this._children.splice(index, 0, node);
  13432. node._setParent(this);
  13433. }
  13434. return node;
  13435. }
  13436. else {
  13437. throw new Error("appendChildAt:The index is out of bounds");
  13438. }
  13439. }
  13440. getChildIndex(node) {
  13441. return this._children.indexOf(node);
  13442. }
  13443. getChildByName(name) {
  13444. var nodes = this._children;
  13445. if (nodes) {
  13446. for (var i = 0, n = nodes.length; i < n; i++) {
  13447. var node = nodes[i];
  13448. if (node.name === name)
  13449. return node;
  13450. }
  13451. }
  13452. return null;
  13453. }
  13454. getChildAt(index) {
  13455. return this._children[index] || null;
  13456. }
  13457. setChildIndex(node, index) {
  13458. var childs = this._children;
  13459. if (index < 0 || index >= childs.length) {
  13460. throw new Error("setChildIndex:The index is out of bounds.");
  13461. }
  13462. var oldIndex = this.getChildIndex(node);
  13463. if (oldIndex < 0)
  13464. throw new Error("setChildIndex:node is must child of this object.");
  13465. childs.splice(oldIndex, 1);
  13466. childs.splice(index, 0, node);
  13467. this._childChanged();
  13468. return node;
  13469. }
  13470. _childChanged(child = null) {
  13471. }
  13472. removeChild(node) {
  13473. if (!this._children)
  13474. return node;
  13475. var index = this._children.indexOf(node);
  13476. return this.removeChildAt(index);
  13477. }
  13478. removeSelf() {
  13479. this._parent && this._parent.removeChild(this);
  13480. return this;
  13481. }
  13482. removeChildByName(name) {
  13483. var node = this.getChildByName(name);
  13484. node && this.removeChild(node);
  13485. return node;
  13486. }
  13487. removeChildAt(index) {
  13488. var node = this.getChildAt(index);
  13489. if (node) {
  13490. this._children.splice(index, 1);
  13491. node._setParent(null);
  13492. }
  13493. return node;
  13494. }
  13495. removeChildren(beginIndex = 0, endIndex = 0x7fffffff) {
  13496. if (this._children && this._children.length > 0) {
  13497. var childs = this._children;
  13498. if (beginIndex === 0 && endIndex >= childs.length - 1) {
  13499. var arr = childs;
  13500. this._children = Node.ARRAY_EMPTY;
  13501. }
  13502. else {
  13503. arr = childs.splice(beginIndex, endIndex - beginIndex + 1);
  13504. }
  13505. for (var i = 0, n = arr.length; i < n; i++) {
  13506. arr[i]._setParent(null);
  13507. }
  13508. }
  13509. return this;
  13510. }
  13511. replaceChild(newNode, oldNode) {
  13512. var index = this._children.indexOf(oldNode);
  13513. if (index > -1) {
  13514. this._children.splice(index, 1, newNode);
  13515. oldNode._setParent(null);
  13516. newNode._setParent(this);
  13517. return newNode;
  13518. }
  13519. return null;
  13520. }
  13521. get numChildren() {
  13522. return this._children.length;
  13523. }
  13524. get parent() {
  13525. return this._parent;
  13526. }
  13527. _setParent(value) {
  13528. if (this._parent !== value) {
  13529. if (value) {
  13530. this._parent = value;
  13531. this._onAdded();
  13532. this.event(Event.ADDED);
  13533. if (this._getBit(Const.DISPLAY)) {
  13534. this._setUpNoticeChain();
  13535. value.displayedInStage && this._displayChild(this, true);
  13536. }
  13537. value._childChanged(this);
  13538. }
  13539. else {
  13540. this._onRemoved();
  13541. this.event(Event.REMOVED);
  13542. this._parent._childChanged();
  13543. if (this._getBit(Const.DISPLAY))
  13544. this._displayChild(this, false);
  13545. this._parent = value;
  13546. }
  13547. }
  13548. }
  13549. get displayedInStage() {
  13550. if (this._getBit(Const.DISPLAY))
  13551. return this._getBit(Const.DISPLAYED_INSTAGE);
  13552. this._setBitUp(Const.DISPLAY);
  13553. return this._getBit(Const.DISPLAYED_INSTAGE);
  13554. }
  13555. _updateDisplayedInstage() {
  13556. var ele;
  13557. ele = this;
  13558. var stage = ILaya.stage;
  13559. var displayedInStage = false;
  13560. while (ele) {
  13561. if (ele._getBit(Const.DISPLAY)) {
  13562. displayedInStage = ele._getBit(Const.DISPLAYED_INSTAGE);
  13563. break;
  13564. }
  13565. if (ele === stage || ele._getBit(Const.DISPLAYED_INSTAGE)) {
  13566. displayedInStage = true;
  13567. break;
  13568. }
  13569. ele = ele._parent;
  13570. }
  13571. this._setBit(Const.DISPLAYED_INSTAGE, displayedInStage);
  13572. }
  13573. _setDisplay(value) {
  13574. if (this._getBit(Const.DISPLAYED_INSTAGE) !== value) {
  13575. this._setBit(Const.DISPLAYED_INSTAGE, value);
  13576. if (value)
  13577. this.event(Event.DISPLAY);
  13578. else
  13579. this.event(Event.UNDISPLAY);
  13580. }
  13581. }
  13582. _displayChild(node, display) {
  13583. var childs = node._children;
  13584. if (childs) {
  13585. for (var i = 0, n = childs.length; i < n; i++) {
  13586. var child = childs[i];
  13587. if (!child._getBit(Const.DISPLAY))
  13588. continue;
  13589. if (child._children.length > 0) {
  13590. this._displayChild(child, display);
  13591. }
  13592. else {
  13593. child._setDisplay(display);
  13594. }
  13595. }
  13596. }
  13597. node._setDisplay(display);
  13598. }
  13599. contains(node) {
  13600. if (node === this)
  13601. return true;
  13602. while (node) {
  13603. if (node._parent === this)
  13604. return true;
  13605. node = node._parent;
  13606. }
  13607. return false;
  13608. }
  13609. timerLoop(delay, caller, method, args = null, coverBefore = true, jumpFrame = false) {
  13610. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13611. timer.loop(delay, caller, method, args, coverBefore, jumpFrame);
  13612. }
  13613. timerOnce(delay, caller, method, args = null, coverBefore = true) {
  13614. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13615. timer._create(false, false, delay, caller, method, args, coverBefore);
  13616. }
  13617. frameLoop(delay, caller, method, args = null, coverBefore = true) {
  13618. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13619. timer._create(true, true, delay, caller, method, args, coverBefore);
  13620. }
  13621. frameOnce(delay, caller, method, args = null, coverBefore = true) {
  13622. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13623. timer._create(true, false, delay, caller, method, args, coverBefore);
  13624. }
  13625. clearTimer(caller, method) {
  13626. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13627. timer.clear(caller, method);
  13628. }
  13629. callLater(method, args = null) {
  13630. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13631. timer.callLater(this, method, args);
  13632. }
  13633. runCallLater(method) {
  13634. var timer = this.scene ? this.scene.timer : ILaya.timer;
  13635. timer.runCallLater(this, method);
  13636. }
  13637. get scene() {
  13638. return this._scene;
  13639. }
  13640. get active() {
  13641. return !this._getBit(Const.NOT_READY) && !this._getBit(Const.NOT_ACTIVE);
  13642. }
  13643. set active(value) {
  13644. value = !!value;
  13645. if (!this._getBit(Const.NOT_ACTIVE) !== value) {
  13646. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  13647. if (value)
  13648. throw "Node: can't set the main inActive node active in hierarchy,if the operate is in main inActive node or it's children script's onDisable Event.";
  13649. else
  13650. throw "Node: can't set the main active node inActive in hierarchy,if the operate is in main active node or it's children script's onEnable Event.";
  13651. }
  13652. else {
  13653. this._setBit(Const.NOT_ACTIVE, !value);
  13654. if (this._parent) {
  13655. if (this._parent.activeInHierarchy) {
  13656. if (value)
  13657. this._processActive();
  13658. else
  13659. this._processInActive();
  13660. }
  13661. }
  13662. }
  13663. }
  13664. }
  13665. get activeInHierarchy() {
  13666. return this._getBit(Const.ACTIVE_INHIERARCHY);
  13667. }
  13668. _onActive() {
  13669. Stat.spriteCount++;
  13670. }
  13671. _onInActive() {
  13672. Stat.spriteCount--;
  13673. }
  13674. _onActiveInScene() {
  13675. }
  13676. _onInActiveInScene() {
  13677. }
  13678. _parse(data, spriteMap) {
  13679. }
  13680. _setBelongScene(scene) {
  13681. if (!this._scene) {
  13682. this._scene = scene;
  13683. this._onActiveInScene();
  13684. for (var i = 0, n = this._children.length; i < n; i++)
  13685. this._children[i]._setBelongScene(scene);
  13686. }
  13687. }
  13688. _setUnBelongScene() {
  13689. if (this._scene !== this) {
  13690. this._onInActiveInScene();
  13691. this._scene = null;
  13692. for (var i = 0, n = this._children.length; i < n; i++)
  13693. this._children[i]._setUnBelongScene();
  13694. }
  13695. }
  13696. onAwake() {
  13697. }
  13698. onEnable() {
  13699. }
  13700. _processActive() {
  13701. (this._activeChangeScripts) || (this._activeChangeScripts = []);
  13702. this._activeHierarchy(this._activeChangeScripts);
  13703. this._activeScripts();
  13704. }
  13705. _activeHierarchy(activeChangeScripts) {
  13706. this._setBit(Const.ACTIVE_INHIERARCHY, true);
  13707. if (this._components) {
  13708. for (var i = 0, n = this._components.length; i < n; i++) {
  13709. var comp = this._components[i];
  13710. if (comp._isScript())
  13711. (comp._enabled) && (activeChangeScripts.push(comp));
  13712. else
  13713. comp._setActive(true);
  13714. }
  13715. }
  13716. this._onActive();
  13717. for (i = 0, n = this._children.length; i < n; i++) {
  13718. var child = this._children[i];
  13719. (!child._getBit(Const.NOT_ACTIVE) && !child._getBit(Const.NOT_READY)) && (child._activeHierarchy(activeChangeScripts));
  13720. }
  13721. if (!this._getBit(Const.AWAKED)) {
  13722. this._setBit(Const.AWAKED, true);
  13723. this.onAwake();
  13724. }
  13725. this.onEnable();
  13726. }
  13727. _activeScripts() {
  13728. for (var i = 0, n = this._activeChangeScripts.length; i < n; i++) {
  13729. var comp = this._activeChangeScripts[i];
  13730. if (!comp._awaked) {
  13731. comp._awaked = true;
  13732. comp._onAwake();
  13733. }
  13734. comp._onEnable();
  13735. }
  13736. this._activeChangeScripts.length = 0;
  13737. }
  13738. _processInActive() {
  13739. (this._activeChangeScripts) || (this._activeChangeScripts = []);
  13740. this._inActiveHierarchy(this._activeChangeScripts);
  13741. this._inActiveScripts();
  13742. }
  13743. _inActiveHierarchy(activeChangeScripts) {
  13744. this._onInActive();
  13745. if (this._components) {
  13746. for (var i = 0, n = this._components.length; i < n; i++) {
  13747. var comp = this._components[i];
  13748. (!comp._isScript()) && comp._setActive(false);
  13749. (comp._isScript() && comp._enabled) && (activeChangeScripts.push(comp));
  13750. }
  13751. }
  13752. this._setBit(Const.ACTIVE_INHIERARCHY, false);
  13753. for (i = 0, n = this._children.length; i < n; i++) {
  13754. var child = this._children[i];
  13755. (child && !child._getBit(Const.NOT_ACTIVE)) && (child._inActiveHierarchy(activeChangeScripts));
  13756. }
  13757. this.onDisable();
  13758. }
  13759. _inActiveScripts() {
  13760. for (var i = 0, n = this._activeChangeScripts.length; i < n; i++)
  13761. this._activeChangeScripts[i].onDisable();
  13762. this._activeChangeScripts.length = 0;
  13763. }
  13764. onDisable() {
  13765. }
  13766. _onAdded() {
  13767. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  13768. throw "Node: can't set the main inActive node active in hierarchy,if the operate is in main inActive node or it's children script's onDisable Event.";
  13769. }
  13770. else {
  13771. var parentScene = this._parent.scene;
  13772. parentScene && this._setBelongScene(parentScene);
  13773. (this._parent.activeInHierarchy && this.active) && this._processActive();
  13774. }
  13775. }
  13776. _onRemoved() {
  13777. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  13778. throw "Node: can't set the main active node inActive in hierarchy,if the operate is in main active node or it's children script's onEnable Event.";
  13779. }
  13780. else {
  13781. (this._parent.activeInHierarchy && this.active) && this._processInActive();
  13782. this._parent.scene && this._setUnBelongScene();
  13783. }
  13784. }
  13785. _addComponentInstance(comp) {
  13786. this._components = this._components || [];
  13787. this._components.push(comp);
  13788. comp.owner = this;
  13789. comp._onAdded();
  13790. if (this.activeInHierarchy)
  13791. comp._setActive(true);
  13792. }
  13793. _destroyComponent(comp) {
  13794. if (this._components) {
  13795. for (var i = 0, n = this._components.length; i < n; i++) {
  13796. var item = this._components[i];
  13797. if (item === comp) {
  13798. item._destroy();
  13799. this._components.splice(i, 1);
  13800. break;
  13801. }
  13802. }
  13803. }
  13804. }
  13805. _destroyAllComponent() {
  13806. if (this._components) {
  13807. for (var i = 0, n = this._components.length; i < n; i++) {
  13808. var item = this._components[i];
  13809. item && item._destroy();
  13810. }
  13811. this._components.length = 0;
  13812. }
  13813. }
  13814. _cloneTo(destObject, srcRoot, dstRoot) {
  13815. var destNode = destObject;
  13816. if (this._components) {
  13817. for (var i = 0, n = this._components.length; i < n; i++) {
  13818. var destComponent = destNode.addComponent(this._components[i].constructor);
  13819. this._components[i]._cloneTo(destComponent);
  13820. }
  13821. }
  13822. }
  13823. addComponentIntance(component) {
  13824. if (component.owner)
  13825. throw "Node:the component has belong to other node.";
  13826. if (component.isSingleton && this.getComponent(component.constructor))
  13827. throw "Node:the component is singleton,can't add the second one.";
  13828. this._addComponentInstance(component);
  13829. return component;
  13830. }
  13831. addComponent(componentType) {
  13832. var comp = Pool.createByClass(componentType);
  13833. comp._destroyed = false;
  13834. if (comp.isSingleton && this.getComponent(componentType))
  13835. throw "无法实例" + componentType + "组件" + "," + componentType + "组件已存在!";
  13836. this._addComponentInstance(comp);
  13837. return comp;
  13838. }
  13839. getComponent(componentType) {
  13840. if (this._components) {
  13841. for (var i = 0, n = this._components.length; i < n; i++) {
  13842. var comp = this._components[i];
  13843. if (comp instanceof componentType)
  13844. return comp;
  13845. }
  13846. }
  13847. return null;
  13848. }
  13849. getComponents(componentType) {
  13850. var arr;
  13851. if (this._components) {
  13852. for (var i = 0, n = this._components.length; i < n; i++) {
  13853. var comp = this._components[i];
  13854. if (comp instanceof componentType) {
  13855. arr = arr || [];
  13856. arr.push(comp);
  13857. }
  13858. }
  13859. }
  13860. return arr;
  13861. }
  13862. get timer() {
  13863. return this.scene ? this.scene.timer : ILaya.timer;
  13864. }
  13865. }
  13866. Node.ARRAY_EMPTY = [];
  13867. ClassUtils.regClass("laya.display.Node", Node);
  13868. ClassUtils.regClass("Laya.Node", Node);
  13869. class Sprite extends Node {
  13870. constructor() {
  13871. super();
  13872. this._x = 0;
  13873. this._y = 0;
  13874. this._width = 0;
  13875. this._height = 0;
  13876. this._visible = true;
  13877. this._mouseState = 0;
  13878. this._zOrder = 0;
  13879. this._renderType = 0;
  13880. this._transform = null;
  13881. this._tfChanged = false;
  13882. this._repaint = SpriteConst.REPAINT_NONE;
  13883. this._texture = null;
  13884. this._style = SpriteStyle.EMPTY;
  13885. this._cacheStyle = CacheStyle.EMPTY;
  13886. this._boundStyle = null;
  13887. this._graphics = null;
  13888. this.mouseThrough = false;
  13889. this.autoSize = false;
  13890. this.hitTestPrior = false;
  13891. }
  13892. destroy(destroyChild = true) {
  13893. super.destroy(destroyChild);
  13894. this._style && this._style.recover();
  13895. this._cacheStyle && this._cacheStyle.recover();
  13896. this._boundStyle && this._boundStyle.recover();
  13897. this._style = null;
  13898. this._cacheStyle = null;
  13899. this._boundStyle = null;
  13900. this._transform = null;
  13901. if (this._graphics && this._graphics.autoDestroy) {
  13902. this._graphics.destroy();
  13903. }
  13904. this._graphics = null;
  13905. this.texture = null;
  13906. }
  13907. updateZOrder() {
  13908. Utils.updateOrder(this._children) && this.repaint();
  13909. }
  13910. _getBoundsStyle() {
  13911. if (!this._boundStyle)
  13912. this._boundStyle = BoundsStyle.create();
  13913. return this._boundStyle;
  13914. }
  13915. _setCustomRender() {
  13916. }
  13917. set customRenderEnable(b) {
  13918. if (b) {
  13919. this._renderType |= SpriteConst.CUSTOM;
  13920. this._setRenderType(this._renderType);
  13921. this._setCustomRender();
  13922. }
  13923. }
  13924. get cacheAs() {
  13925. return this._cacheStyle.cacheAs;
  13926. }
  13927. _setCacheAs(value) {
  13928. }
  13929. set cacheAs(value) {
  13930. if (value === this._cacheStyle.userSetCache)
  13931. return;
  13932. if (this.mask && value === 'normal')
  13933. return;
  13934. this._setCacheAs(value);
  13935. this._getCacheStyle().userSetCache = value;
  13936. this._checkCanvasEnable();
  13937. this.repaint();
  13938. }
  13939. _checkCanvasEnable() {
  13940. var tEnable = this._cacheStyle.needEnableCanvasRender();
  13941. this._getCacheStyle().enableCanvasRender = tEnable;
  13942. if (tEnable) {
  13943. if (this._cacheStyle.needBitmapCache()) {
  13944. this._cacheStyle.cacheAs = "bitmap";
  13945. }
  13946. else {
  13947. this._cacheStyle.cacheAs = this._cacheStyle.userSetCache;
  13948. }
  13949. this._cacheStyle.reCache = true;
  13950. this._renderType |= SpriteConst.CANVAS;
  13951. }
  13952. else {
  13953. this._cacheStyle.cacheAs = "none";
  13954. this._cacheStyle.releaseContext();
  13955. this._renderType &= ~SpriteConst.CANVAS;
  13956. }
  13957. this._setCacheAs(this._cacheStyle.cacheAs);
  13958. this._setRenderType(this._renderType);
  13959. }
  13960. get staticCache() {
  13961. return this._cacheStyle.staticCache;
  13962. }
  13963. set staticCache(value) {
  13964. this._getCacheStyle().staticCache = value;
  13965. if (!value)
  13966. this.reCache();
  13967. }
  13968. reCache() {
  13969. this._cacheStyle.reCache = true;
  13970. this._repaint |= SpriteConst.REPAINT_CACHE;
  13971. }
  13972. getRepaint() {
  13973. return this._repaint;
  13974. }
  13975. _setX(value) {
  13976. this._x = value;
  13977. }
  13978. _setY(value) {
  13979. this._y = value;
  13980. }
  13981. _setWidth(texture, value) {
  13982. }
  13983. _setHeight(texture, value) {
  13984. }
  13985. get x() {
  13986. return this._x;
  13987. }
  13988. set x(value) {
  13989. if (this.destroyed)
  13990. return;
  13991. if (this._x !== value) {
  13992. this._setX(value);
  13993. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  13994. var p = this._cacheStyle.maskParent;
  13995. if (p) {
  13996. p.repaint(SpriteConst.REPAINT_CACHE);
  13997. }
  13998. }
  13999. }
  14000. get y() {
  14001. return this._y;
  14002. }
  14003. set y(value) {
  14004. if (this.destroyed)
  14005. return;
  14006. if (this._y !== value) {
  14007. this._setY(value);
  14008. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  14009. var p = this._cacheStyle.maskParent;
  14010. if (p) {
  14011. p.repaint(SpriteConst.REPAINT_CACHE);
  14012. }
  14013. }
  14014. }
  14015. get width() {
  14016. return this.get_width();
  14017. }
  14018. set width(value) {
  14019. this.set_width(value);
  14020. }
  14021. set_width(value) {
  14022. if (this._width !== value) {
  14023. this._width = value;
  14024. this._setWidth(this.texture, value);
  14025. this._setTranformChange();
  14026. }
  14027. }
  14028. get_width() {
  14029. if (!this.autoSize)
  14030. return this._width || (this.texture ? this.texture.width : 0);
  14031. if (this.texture)
  14032. return this.texture.width;
  14033. if (!this._graphics && this._children.length === 0)
  14034. return 0;
  14035. return this.getSelfBounds().width;
  14036. }
  14037. get height() {
  14038. return this.get_height();
  14039. }
  14040. set height(value) {
  14041. this.set_height(value);
  14042. }
  14043. set_height(value) {
  14044. if (this._height !== value) {
  14045. this._height = value;
  14046. this._setHeight(this.texture, value);
  14047. this._setTranformChange();
  14048. }
  14049. }
  14050. get_height() {
  14051. if (!this.autoSize)
  14052. return this._height || (this.texture ? this.texture.height : 0);
  14053. if (this.texture)
  14054. return this.texture.height;
  14055. if (!this._graphics && this._children.length === 0)
  14056. return 0;
  14057. return this.getSelfBounds().height;
  14058. }
  14059. get displayWidth() {
  14060. return this.width * this.scaleX;
  14061. }
  14062. get displayHeight() {
  14063. return this.height * this.scaleY;
  14064. }
  14065. setSelfBounds(bound) {
  14066. this._getBoundsStyle().userBounds = bound;
  14067. }
  14068. getBounds() {
  14069. return this._getBoundsStyle().bounds = Rectangle._getWrapRec(this._boundPointsToParent());
  14070. }
  14071. getSelfBounds() {
  14072. if (this._boundStyle && this._boundStyle.userBounds)
  14073. return this._boundStyle.userBounds;
  14074. if (!this._graphics && this._children.length === 0 && !this._texture)
  14075. return Rectangle.TEMP.setTo(0, 0, this.width, this.height);
  14076. return this._getBoundsStyle().bounds = Rectangle._getWrapRec(this._getBoundPointsM(false));
  14077. }
  14078. _boundPointsToParent(ifRotate = false) {
  14079. var pX = 0, pY = 0;
  14080. if (this._style) {
  14081. pX = this.pivotX;
  14082. pY = this.pivotY;
  14083. ifRotate = ifRotate || (this._style.rotation !== 0);
  14084. if (this._style.scrollRect) {
  14085. pX += this._style.scrollRect.x;
  14086. pY += this._style.scrollRect.y;
  14087. }
  14088. }
  14089. var pList = this._getBoundPointsM(ifRotate);
  14090. if (!pList || pList.length < 1)
  14091. return pList;
  14092. if (pList.length != 8) {
  14093. pList = ifRotate ? GrahamScan.scanPList(pList) : Rectangle._getWrapRec(pList, Rectangle.TEMP)._getBoundPoints();
  14094. }
  14095. if (!this.transform) {
  14096. Utils.transPointList(pList, this._x - pX, this._y - pY);
  14097. return pList;
  14098. }
  14099. var tPoint = Point.TEMP;
  14100. var i, len = pList.length;
  14101. for (i = 0; i < len; i += 2) {
  14102. tPoint.x = pList[i];
  14103. tPoint.y = pList[i + 1];
  14104. this.toParentPoint(tPoint);
  14105. pList[i] = tPoint.x;
  14106. pList[i + 1] = tPoint.y;
  14107. }
  14108. return pList;
  14109. }
  14110. getGraphicBounds(realSize = false) {
  14111. if (!this._graphics)
  14112. return Rectangle.TEMP.setTo(0, 0, 0, 0);
  14113. return this._graphics.getBounds(realSize);
  14114. }
  14115. _getBoundPointsM(ifRotate = false) {
  14116. if (this._boundStyle && this._boundStyle.userBounds)
  14117. return this._boundStyle.userBounds._getBoundPoints();
  14118. if (!this._boundStyle)
  14119. this._getBoundsStyle();
  14120. if (!this._boundStyle.temBM)
  14121. this._boundStyle.temBM = [];
  14122. if (this._style.scrollRect) {
  14123. var rst = Utils.clearArray(this._boundStyle.temBM);
  14124. var rec = Rectangle.TEMP;
  14125. rec.copyFrom(this._style.scrollRect);
  14126. Utils.concatArray(rst, rec._getBoundPoints());
  14127. return rst;
  14128. }
  14129. var pList;
  14130. if (this._graphics) {
  14131. pList = this._graphics.getBoundPoints();
  14132. }
  14133. else {
  14134. pList = Utils.clearArray(this._boundStyle.temBM);
  14135. }
  14136. if (this._texture) {
  14137. rec = Rectangle.TEMP;
  14138. rec.setTo(0, 0, this.width || this._texture.width, this.height || this._texture.height);
  14139. Utils.concatArray(pList, rec._getBoundPoints());
  14140. }
  14141. var child;
  14142. var cList;
  14143. var __childs;
  14144. __childs = this._children;
  14145. for (var i = 0, n = __childs.length; i < n; i++) {
  14146. child = __childs[i];
  14147. if (child instanceof Sprite && child._visible === true) {
  14148. cList = child._boundPointsToParent(ifRotate);
  14149. if (cList)
  14150. pList = pList ? Utils.concatArray(pList, cList) : cList;
  14151. }
  14152. }
  14153. return pList;
  14154. }
  14155. _getCacheStyle() {
  14156. this._cacheStyle === CacheStyle.EMPTY && (this._cacheStyle = CacheStyle.create());
  14157. return this._cacheStyle;
  14158. }
  14159. getStyle() {
  14160. this._style === SpriteStyle.EMPTY && (this._style = SpriteStyle.create());
  14161. return this._style;
  14162. }
  14163. setStyle(value) {
  14164. this._style = value;
  14165. }
  14166. get scaleX() {
  14167. return this._style.scaleX;
  14168. }
  14169. set scaleX(value) {
  14170. this.set_scaleX(value);
  14171. }
  14172. _setScaleX(value) {
  14173. this._style.scaleX = value;
  14174. }
  14175. get scaleY() {
  14176. return this._style.scaleY;
  14177. }
  14178. set scaleY(value) {
  14179. this.set_scaleY(value);
  14180. }
  14181. _setScaleY(value) {
  14182. this._style.scaleY = value;
  14183. }
  14184. set_scaleX(value) {
  14185. var style = this.getStyle();
  14186. if (style.scaleX !== value) {
  14187. this._setScaleX(value);
  14188. this._setTranformChange();
  14189. }
  14190. }
  14191. get_scaleX() {
  14192. return this._style.scaleX;
  14193. }
  14194. set_scaleY(value) {
  14195. var style = this.getStyle();
  14196. if (style.scaleY !== value) {
  14197. this._setScaleY(value);
  14198. this._setTranformChange();
  14199. }
  14200. }
  14201. get_scaleY() {
  14202. return this._style.scaleY;
  14203. }
  14204. get rotation() {
  14205. return this._style.rotation;
  14206. }
  14207. set rotation(value) {
  14208. var style = this.getStyle();
  14209. if (style.rotation !== value) {
  14210. this._setRotation(value);
  14211. this._setTranformChange();
  14212. }
  14213. }
  14214. _setRotation(value) {
  14215. this._style.rotation = value;
  14216. }
  14217. get skewX() {
  14218. return this._style.skewX;
  14219. }
  14220. set skewX(value) {
  14221. var style = this.getStyle();
  14222. if (style.skewX !== value) {
  14223. this._setSkewX(value);
  14224. this._setTranformChange();
  14225. }
  14226. }
  14227. _setSkewX(value) {
  14228. this._style.skewX = value;
  14229. }
  14230. get skewY() {
  14231. return this._style.skewY;
  14232. }
  14233. set skewY(value) {
  14234. var style = this.getStyle();
  14235. if (style.skewY !== value) {
  14236. this._setSkewY(value);
  14237. this._setTranformChange();
  14238. }
  14239. }
  14240. _setSkewY(value) {
  14241. this._style.skewY = value;
  14242. }
  14243. _createTransform() {
  14244. return Matrix.create();
  14245. }
  14246. _adjustTransform() {
  14247. this._tfChanged = false;
  14248. var style = this._style;
  14249. var sx = style.scaleX, sy = style.scaleY;
  14250. var sskx = style.skewX;
  14251. var ssky = style.skewY;
  14252. var rot = style.rotation;
  14253. var m = this._transform || (this._transform = this._createTransform());
  14254. if (rot || sx !== 1 || sy !== 1 || sskx !== 0 || ssky !== 0) {
  14255. m._bTransform = true;
  14256. var skx = (rot - sskx) * 0.0174532922222222;
  14257. var sky = (rot + ssky) * 0.0174532922222222;
  14258. var cx = Math.cos(sky);
  14259. var ssx = Math.sin(sky);
  14260. var cy = Math.sin(skx);
  14261. var ssy = Math.cos(skx);
  14262. m.a = sx * cx;
  14263. m.b = sx * ssx;
  14264. m.c = -sy * cy;
  14265. m.d = sy * ssy;
  14266. m.tx = m.ty = 0;
  14267. }
  14268. else {
  14269. m.identity();
  14270. this._renderType &= ~SpriteConst.TRANSFORM;
  14271. this._setRenderType(this._renderType);
  14272. }
  14273. return m;
  14274. }
  14275. _setTransform(value) {
  14276. }
  14277. get transform() {
  14278. return this._tfChanged ? this._adjustTransform() : this._transform;
  14279. }
  14280. set transform(value) {
  14281. this.set_transform(value);
  14282. }
  14283. get_transform() {
  14284. return this._tfChanged ? this._adjustTransform() : this._transform;
  14285. }
  14286. set_transform(value) {
  14287. this._tfChanged = false;
  14288. var m = this._transform || (this._transform = this._createTransform());
  14289. value.copyTo(m);
  14290. this._setTransform(m);
  14291. if (value) {
  14292. this._x = m.tx;
  14293. this._y = m.ty;
  14294. m.tx = m.ty = 0;
  14295. }
  14296. if (value)
  14297. this._renderType |= SpriteConst.TRANSFORM;
  14298. else {
  14299. this._renderType &= ~SpriteConst.TRANSFORM;
  14300. }
  14301. this._setRenderType(this._renderType);
  14302. this.parentRepaint();
  14303. }
  14304. _setPivotX(value) {
  14305. var style = this.getStyle();
  14306. style.pivotX = value;
  14307. }
  14308. _getPivotX() {
  14309. return this._style.pivotX;
  14310. }
  14311. _setPivotY(value) {
  14312. var style = this.getStyle();
  14313. style.pivotY = value;
  14314. }
  14315. _getPivotY() {
  14316. return this._style.pivotY;
  14317. }
  14318. get pivotX() {
  14319. return this._getPivotX();
  14320. }
  14321. set pivotX(value) {
  14322. this._setPivotX(value);
  14323. this.repaint();
  14324. }
  14325. get pivotY() {
  14326. return this._getPivotY();
  14327. }
  14328. set pivotY(value) {
  14329. this._setPivotY(value);
  14330. this.repaint();
  14331. }
  14332. _setAlpha(value) {
  14333. if (this._style.alpha !== value) {
  14334. var style = this.getStyle();
  14335. style.alpha = value;
  14336. if (value !== 1)
  14337. this._renderType |= SpriteConst.ALPHA;
  14338. else
  14339. this._renderType &= ~SpriteConst.ALPHA;
  14340. this._setRenderType(this._renderType);
  14341. this.parentRepaint();
  14342. }
  14343. }
  14344. _getAlpha() {
  14345. return this._style.alpha;
  14346. }
  14347. get alpha() {
  14348. return this._getAlpha();
  14349. }
  14350. set alpha(value) {
  14351. value = value < 0 ? 0 : (value > 1 ? 1 : value);
  14352. this._setAlpha(value);
  14353. }
  14354. get visible() {
  14355. return this.get_visible();
  14356. }
  14357. set visible(value) {
  14358. this.set_visible(value);
  14359. }
  14360. get_visible() {
  14361. return this._visible;
  14362. }
  14363. set_visible(value) {
  14364. if (this._visible !== value) {
  14365. this._visible = value;
  14366. this.parentRepaint(SpriteConst.REPAINT_ALL);
  14367. }
  14368. }
  14369. _setBlendMode(value) {
  14370. }
  14371. get blendMode() {
  14372. return this._style.blendMode;
  14373. }
  14374. set blendMode(value) {
  14375. this._setBlendMode(value);
  14376. this.getStyle().blendMode = value;
  14377. if (value && value != "source-over")
  14378. this._renderType |= SpriteConst.BLEND;
  14379. else
  14380. this._renderType &= ~SpriteConst.BLEND;
  14381. this._setRenderType(this._renderType);
  14382. this.parentRepaint();
  14383. }
  14384. get graphics() {
  14385. if (!this._graphics) {
  14386. this.graphics = new Graphics();
  14387. this._graphics.autoDestroy = true;
  14388. }
  14389. return this._graphics;
  14390. }
  14391. _setGraphics(value) {
  14392. }
  14393. _setGraphicsCallBack() {
  14394. }
  14395. set graphics(value) {
  14396. if (this._graphics)
  14397. this._graphics._sp = null;
  14398. this._graphics = value;
  14399. if (value) {
  14400. this._setGraphics(value);
  14401. this._renderType |= SpriteConst.GRAPHICS;
  14402. value._sp = this;
  14403. }
  14404. else {
  14405. this._renderType &= ~SpriteConst.GRAPHICS;
  14406. }
  14407. this._setRenderType(this._renderType);
  14408. this.repaint();
  14409. }
  14410. get scrollRect() {
  14411. return this._style.scrollRect;
  14412. }
  14413. _setScrollRect(value) {
  14414. }
  14415. set scrollRect(value) {
  14416. this.getStyle().scrollRect = value;
  14417. this._setScrollRect(value);
  14418. this.repaint();
  14419. if (value) {
  14420. this._renderType |= SpriteConst.CLIP;
  14421. }
  14422. else {
  14423. this._renderType &= ~SpriteConst.CLIP;
  14424. }
  14425. this._setRenderType(this._renderType);
  14426. }
  14427. pos(x, y, speedMode = false) {
  14428. if (this._x !== x || this._y !== y) {
  14429. if (this.destroyed)
  14430. return this;
  14431. if (speedMode) {
  14432. this._setX(x);
  14433. this._setY(y);
  14434. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  14435. var p = this._cacheStyle.maskParent;
  14436. if (p) {
  14437. p.repaint(SpriteConst.REPAINT_CACHE);
  14438. }
  14439. }
  14440. else {
  14441. this.x = x;
  14442. this.y = y;
  14443. }
  14444. }
  14445. return this;
  14446. }
  14447. pivot(x, y) {
  14448. this.pivotX = x;
  14449. this.pivotY = y;
  14450. return this;
  14451. }
  14452. size(width, height) {
  14453. this.width = width;
  14454. this.height = height;
  14455. return this;
  14456. }
  14457. scale(scaleX, scaleY, speedMode = false) {
  14458. var style = this.getStyle();
  14459. if (style.scaleX != scaleX || style.scaleY != scaleY) {
  14460. if (this.destroyed)
  14461. return this;
  14462. if (speedMode) {
  14463. this._setScaleX(scaleX);
  14464. this._setScaleY(scaleY);
  14465. this._setTranformChange();
  14466. }
  14467. else {
  14468. this.scaleX = scaleX;
  14469. this.scaleY = scaleY;
  14470. }
  14471. }
  14472. return this;
  14473. }
  14474. skew(skewX, skewY) {
  14475. this.skewX = skewX;
  14476. this.skewY = skewY;
  14477. return this;
  14478. }
  14479. render(ctx, x, y) {
  14480. RenderSprite.renders[this._renderType]._fun(this, ctx, x + this._x, y + this._y);
  14481. this._repaint = 0;
  14482. }
  14483. drawToCanvas(canvasWidth, canvasHeight, offsetX, offsetY) {
  14484. return Sprite.drawToCanvas(this, this._renderType, canvasWidth, canvasHeight, offsetX, offsetY);
  14485. }
  14486. drawToTexture(canvasWidth, canvasHeight, offsetX, offsetY, rt = null) {
  14487. return Sprite.drawToTexture(this, this._renderType, canvasWidth, canvasHeight, offsetX, offsetY, rt);
  14488. }
  14489. drawToTexture3D(offx, offy, tex) {
  14490. throw 'not implement';
  14491. }
  14492. static drawToCanvas(sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY) {
  14493. offsetX -= sprite.x;
  14494. offsetY -= sprite.y;
  14495. offsetX |= 0;
  14496. offsetY |= 0;
  14497. canvasWidth |= 0;
  14498. canvasHeight |= 0;
  14499. var ctx = new Context();
  14500. ctx.size(canvasWidth, canvasHeight);
  14501. ctx.asBitmap = true;
  14502. ctx._targets.start();
  14503. ctx._targets.clear(0, 0, 0, 0);
  14504. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  14505. ctx.flush();
  14506. ctx._targets.end();
  14507. ctx._targets.restore();
  14508. var dt = ctx._targets.getData(0, 0, canvasWidth, canvasHeight);
  14509. ctx.destroy();
  14510. var imgdata = new ImageData(canvasWidth, canvasHeight);
  14511. var lineLen = canvasWidth * 4;
  14512. var dst = imgdata.data;
  14513. var y = canvasHeight - 1;
  14514. var off = y * lineLen;
  14515. var srcoff = 0;
  14516. for (; y >= 0; y--) {
  14517. dst.set(dt.subarray(srcoff, srcoff + lineLen), off);
  14518. off -= lineLen;
  14519. srcoff += lineLen;
  14520. }
  14521. var canv = new HTMLCanvas(true);
  14522. canv.size(canvasWidth, canvasHeight);
  14523. var ctx2d = canv.getContext('2d');
  14524. ctx2d.putImageData(imgdata, 0, 0);
  14525. return canv;
  14526. }
  14527. static drawToTexture(sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY, rt = null) {
  14528. if (!Sprite.drawtocanvCtx) {
  14529. Sprite.drawtocanvCtx = new Context();
  14530. }
  14531. offsetX -= sprite.x;
  14532. offsetY -= sprite.y;
  14533. offsetX |= 0;
  14534. offsetY |= 0;
  14535. canvasWidth |= 0;
  14536. canvasHeight |= 0;
  14537. var ctx = rt ? Sprite.drawtocanvCtx : new Context();
  14538. ctx.clear();
  14539. ctx.size(canvasWidth, canvasHeight);
  14540. if (rt) {
  14541. ctx._targets = rt;
  14542. }
  14543. else {
  14544. ctx.asBitmap = true;
  14545. }
  14546. if (ctx._targets) {
  14547. ctx._targets.start();
  14548. ctx._targets.clear(0, 0, 0, 0);
  14549. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  14550. ctx.flush();
  14551. ctx._targets.end();
  14552. ctx._targets.restore();
  14553. }
  14554. if (!rt) {
  14555. var rtex = new Texture(ctx._targets, Texture.INV_UV);
  14556. ctx.destroy(true);
  14557. return rtex;
  14558. }
  14559. sprite._repaint = 0;
  14560. return rt;
  14561. }
  14562. customRender(context, x, y) {
  14563. this._repaint = SpriteConst.REPAINT_ALL;
  14564. }
  14565. _applyFilters() {
  14566. }
  14567. get filters() {
  14568. return this._cacheStyle.filters;
  14569. }
  14570. _setColorFilter(value) { }
  14571. set filters(value) {
  14572. value && value.length === 0 && (value = null);
  14573. if (this._cacheStyle.filters == value)
  14574. return;
  14575. this._getCacheStyle().filters = value ? value.slice() : null;
  14576. if (value && value.length) {
  14577. this._setColorFilter(value[0]);
  14578. this._renderType |= SpriteConst.FILTERS;
  14579. }
  14580. else {
  14581. this._setColorFilter(null);
  14582. this._renderType &= ~SpriteConst.FILTERS;
  14583. }
  14584. this._setRenderType(this._renderType);
  14585. if (value && value.length > 0) {
  14586. if (!this._getBit(Const.DISPLAY))
  14587. this._setBitUp(Const.DISPLAY);
  14588. if (!(value.length == 1 && (value[0] instanceof ColorFilter))) {
  14589. this._getCacheStyle().cacheForFilters = true;
  14590. this._checkCanvasEnable();
  14591. }
  14592. }
  14593. else {
  14594. if (this._cacheStyle.cacheForFilters) {
  14595. this._cacheStyle.cacheForFilters = false;
  14596. this._checkCanvasEnable();
  14597. }
  14598. }
  14599. this._getCacheStyle().hasGlowFilter = this._isHaveGlowFilter();
  14600. this.repaint();
  14601. }
  14602. _isHaveGlowFilter() {
  14603. var i, len;
  14604. if (this.filters) {
  14605. for (i = 0; i < this.filters.length; i++) {
  14606. if (this.filters[i].type == Filter.GLOW) {
  14607. return true;
  14608. }
  14609. }
  14610. }
  14611. for (i = 0, len = this._children.length; i < len; i++) {
  14612. if (this._children[i]._isHaveGlowFilter()) {
  14613. return true;
  14614. }
  14615. }
  14616. return false;
  14617. }
  14618. localToGlobal(point, createNewPoint = false, globalNode = null) {
  14619. if (createNewPoint === true) {
  14620. point = new Point(point.x, point.y);
  14621. }
  14622. var ele = this;
  14623. globalNode = globalNode || ILaya.stage;
  14624. while (ele && !ele.destroyed) {
  14625. if (ele == globalNode)
  14626. break;
  14627. point = ele.toParentPoint(point);
  14628. ele = ele.parent;
  14629. }
  14630. return point;
  14631. }
  14632. globalToLocal(point, createNewPoint = false, globalNode = null) {
  14633. if (createNewPoint) {
  14634. point = new Point(point.x, point.y);
  14635. }
  14636. var ele = this;
  14637. var list = [];
  14638. globalNode = globalNode || ILaya.stage;
  14639. while (ele && !ele.destroyed) {
  14640. if (ele == globalNode)
  14641. break;
  14642. list.push(ele);
  14643. ele = ele.parent;
  14644. }
  14645. var i = list.length - 1;
  14646. while (i >= 0) {
  14647. ele = list[i];
  14648. point = ele.fromParentPoint(point);
  14649. i--;
  14650. }
  14651. return point;
  14652. }
  14653. toParentPoint(point) {
  14654. if (!point)
  14655. return point;
  14656. point.x -= this.pivotX;
  14657. point.y -= this.pivotY;
  14658. if (this.transform) {
  14659. this._transform.transformPoint(point);
  14660. }
  14661. point.x += this._x;
  14662. point.y += this._y;
  14663. var scroll = this._style.scrollRect;
  14664. if (scroll) {
  14665. point.x -= scroll.x;
  14666. point.y -= scroll.y;
  14667. }
  14668. return point;
  14669. }
  14670. fromParentPoint(point) {
  14671. if (!point)
  14672. return point;
  14673. point.x -= this._x;
  14674. point.y -= this._y;
  14675. var scroll = this._style.scrollRect;
  14676. if (scroll) {
  14677. point.x += scroll.x;
  14678. point.y += scroll.y;
  14679. }
  14680. if (this.transform) {
  14681. this._transform.invertTransformPoint(point);
  14682. }
  14683. point.x += this.pivotX;
  14684. point.y += this.pivotY;
  14685. return point;
  14686. }
  14687. fromStagePoint(point) {
  14688. return point;
  14689. }
  14690. on(type, caller, listener, args = null) {
  14691. if (this._mouseState !== 1 && this.isMouseEvent(type)) {
  14692. this.mouseEnabled = true;
  14693. this._setBit(Const.HAS_MOUSE, true);
  14694. if (this._parent) {
  14695. this._onDisplay();
  14696. }
  14697. return this._createListener(type, caller, listener, args, false);
  14698. }
  14699. return super.on(type, caller, listener, args);
  14700. }
  14701. once(type, caller, listener, args = null) {
  14702. if (this._mouseState !== 1 && this.isMouseEvent(type)) {
  14703. this.mouseEnabled = true;
  14704. this._setBit(Const.HAS_MOUSE, true);
  14705. if (this._parent) {
  14706. this._onDisplay();
  14707. }
  14708. return this._createListener(type, caller, listener, args, true);
  14709. }
  14710. return super.once(type, caller, listener, args);
  14711. }
  14712. _onDisplay(v) {
  14713. if (this._mouseState !== 1) {
  14714. var ele = this;
  14715. ele = ele.parent;
  14716. while (ele && ele._mouseState !== 1) {
  14717. if (ele._getBit(Const.HAS_MOUSE))
  14718. break;
  14719. ele.mouseEnabled = true;
  14720. ele._setBit(Const.HAS_MOUSE, true);
  14721. ele = ele.parent;
  14722. }
  14723. }
  14724. }
  14725. _setParent(value) {
  14726. super._setParent(value);
  14727. if (value && this._getBit(Const.HAS_MOUSE)) {
  14728. this._onDisplay();
  14729. }
  14730. }
  14731. loadImage(url, complete = null) {
  14732. if (!url) {
  14733. this.texture = null;
  14734. loaded.call(this);
  14735. }
  14736. else {
  14737. var tex = ILaya.Loader.textureMap[URL.formatURL(url)];
  14738. if (!tex) {
  14739. tex = new Texture();
  14740. tex.load(url);
  14741. ILaya.Loader.cacheTexture(url, tex);
  14742. }
  14743. this.texture = tex;
  14744. if (!tex.getIsReady())
  14745. tex.once(Event.READY, this, loaded);
  14746. else
  14747. loaded.call(this);
  14748. }
  14749. function loaded() {
  14750. this.repaint(SpriteConst.REPAINT_ALL);
  14751. complete && complete.run();
  14752. }
  14753. return this;
  14754. }
  14755. static fromImage(url) {
  14756. return new Sprite().loadImage(url);
  14757. }
  14758. repaint(type = SpriteConst.REPAINT_CACHE) {
  14759. if (!(this._repaint & type)) {
  14760. this._repaint |= type;
  14761. this.parentRepaint(type);
  14762. }
  14763. if (this._cacheStyle && this._cacheStyle.maskParent) {
  14764. this._cacheStyle.maskParent.repaint(type);
  14765. }
  14766. }
  14767. _needRepaint() {
  14768. return (this._repaint & SpriteConst.REPAINT_CACHE) && this._cacheStyle.enableCanvasRender && this._cacheStyle.reCache;
  14769. }
  14770. _childChanged(child = null) {
  14771. if (this._children.length)
  14772. this._renderType |= SpriteConst.CHILDS;
  14773. else
  14774. this._renderType &= ~SpriteConst.CHILDS;
  14775. this._setRenderType(this._renderType);
  14776. if (child && this._getBit(Const.HAS_ZORDER))
  14777. ILaya.systemTimer.callLater(this, this.updateZOrder);
  14778. this.repaint(SpriteConst.REPAINT_ALL);
  14779. }
  14780. parentRepaint(type = SpriteConst.REPAINT_CACHE) {
  14781. var p = this._parent;
  14782. if (p && !(p._repaint & type)) {
  14783. p._repaint |= type;
  14784. p.parentRepaint(type);
  14785. }
  14786. }
  14787. get stage() {
  14788. return ILaya.stage;
  14789. }
  14790. get hitArea() {
  14791. return this._style.hitArea;
  14792. }
  14793. set hitArea(value) {
  14794. this.getStyle().hitArea = value;
  14795. }
  14796. _setMask(value) {
  14797. }
  14798. get mask() {
  14799. return this._cacheStyle.mask;
  14800. }
  14801. set mask(value) {
  14802. if (value && this.mask && this.mask._cacheStyle.maskParent)
  14803. return;
  14804. this._getCacheStyle().mask = value;
  14805. this._setMask(value);
  14806. this._checkCanvasEnable();
  14807. if (value) {
  14808. value._getCacheStyle().maskParent = this;
  14809. }
  14810. else {
  14811. if (this.mask) {
  14812. this.mask._getCacheStyle().maskParent = null;
  14813. }
  14814. }
  14815. this._renderType |= SpriteConst.MASK;
  14816. this._setRenderType(this._renderType);
  14817. this.parentRepaint(SpriteConst.REPAINT_ALL);
  14818. }
  14819. get mouseEnabled() {
  14820. return this._mouseState > 1;
  14821. }
  14822. set mouseEnabled(value) {
  14823. this._mouseState = value ? 2 : 1;
  14824. }
  14825. startDrag(area = null, hasInertia = false, elasticDistance = 0, elasticBackTime = 300, data = null, disableMouseEvent = false, ratio = 0.92) {
  14826. this._style.dragging || (this.getStyle().dragging = new ILaya.Dragging());
  14827. this._style.dragging.start(this, area, hasInertia, elasticDistance, elasticBackTime, data, disableMouseEvent, ratio);
  14828. }
  14829. stopDrag() {
  14830. this._style.dragging && this._style.dragging.stop();
  14831. }
  14832. _setDisplay(value) {
  14833. if (!value) {
  14834. if (this._cacheStyle) {
  14835. this._cacheStyle.releaseContext();
  14836. this._cacheStyle.releaseFilterCache();
  14837. if (this._cacheStyle.hasGlowFilter) {
  14838. this._cacheStyle.hasGlowFilter = false;
  14839. }
  14840. }
  14841. }
  14842. super._setDisplay(value);
  14843. }
  14844. hitTestPoint(x, y) {
  14845. var point = this.globalToLocal(Point.TEMP.setTo(x, y));
  14846. x = point.x;
  14847. y = point.y;
  14848. var rect = this._style.hitArea ? this._style.hitArea : (this._width > 0 && this._height > 0) ? Rectangle.TEMP.setTo(0, 0, this._width, this._height) : this.getSelfBounds();
  14849. return rect.contains(x, y);
  14850. }
  14851. getMousePoint() {
  14852. return this.globalToLocal(Point.TEMP.setTo(ILaya.stage.mouseX, ILaya.stage.mouseY));
  14853. }
  14854. get globalScaleX() {
  14855. var scale = 1;
  14856. var ele = this;
  14857. while (ele) {
  14858. if (ele === ILaya.stage)
  14859. break;
  14860. scale *= ele.scaleX;
  14861. ele = ele.parent;
  14862. }
  14863. return scale;
  14864. }
  14865. get globalRotation() {
  14866. var angle = 0;
  14867. var ele = this;
  14868. while (ele) {
  14869. if (ele === ILaya.stage)
  14870. break;
  14871. angle += ele.rotation;
  14872. ele = ele.parent;
  14873. }
  14874. return angle;
  14875. }
  14876. get globalScaleY() {
  14877. var scale = 1;
  14878. var ele = this;
  14879. while (ele) {
  14880. if (ele === ILaya.stage)
  14881. break;
  14882. scale *= ele.scaleY;
  14883. ele = ele.parent;
  14884. }
  14885. return scale;
  14886. }
  14887. get mouseX() {
  14888. return this.getMousePoint().x;
  14889. }
  14890. get mouseY() {
  14891. return this.getMousePoint().y;
  14892. }
  14893. get zOrder() {
  14894. return this._zOrder;
  14895. }
  14896. set zOrder(value) {
  14897. if (this._zOrder != value) {
  14898. this._zOrder = value;
  14899. if (this._parent) {
  14900. value && this._parent._setBit(Const.HAS_ZORDER, true);
  14901. ILaya.systemTimer.callLater(this._parent, this.updateZOrder);
  14902. }
  14903. }
  14904. }
  14905. get texture() {
  14906. return this._texture;
  14907. }
  14908. _setTexture(value) {
  14909. }
  14910. set texture(value) {
  14911. if (typeof (value) == 'string') {
  14912. this.loadImage(value);
  14913. }
  14914. else if (this._texture != value) {
  14915. this._texture && this._texture._removeReference();
  14916. this._texture = value;
  14917. value && value._addReference();
  14918. this._setTexture(value);
  14919. this._setWidth(this._texture, this.width);
  14920. this._setHeight(this._texture, this.height);
  14921. if (value)
  14922. this._renderType |= SpriteConst.TEXTURE;
  14923. else
  14924. this._renderType &= ~SpriteConst.TEXTURE;
  14925. this._setRenderType(this._renderType);
  14926. this.repaint();
  14927. }
  14928. }
  14929. get viewport() {
  14930. return this._style.viewport;
  14931. }
  14932. set viewport(value) {
  14933. if (typeof (value) == 'string') {
  14934. var recArr;
  14935. recArr = value.split(",");
  14936. if (recArr.length > 3) {
  14937. value = new Rectangle(parseFloat(recArr[0]), parseFloat(recArr[1]), parseFloat(recArr[2]), parseFloat(recArr[3]));
  14938. }
  14939. }
  14940. this.getStyle().viewport = value;
  14941. }
  14942. _setRenderType(type) {
  14943. }
  14944. _setTranformChange() {
  14945. this._tfChanged = true;
  14946. this._renderType |= SpriteConst.TRANSFORM;
  14947. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  14948. }
  14949. _setBgStyleColor(x, y, width, height, fillColor) {
  14950. }
  14951. _setBorderStyleColor(x, y, width, height, fillColor, borderWidth) {
  14952. }
  14953. captureMouseEvent(exclusive) {
  14954. ILaya.MouseManager.instance.setCapture(this, exclusive);
  14955. }
  14956. releaseMouseEvent() {
  14957. ILaya.MouseManager.instance.releaseCapture();
  14958. }
  14959. set drawCallOptimize(value) {
  14960. this._setBit(Const.DRAWCALL_OPTIMIZE, value);
  14961. }
  14962. get drawCallOptimize() {
  14963. return this._getBit(Const.DRAWCALL_OPTIMIZE);
  14964. }
  14965. }
  14966. ClassUtils.regClass("laya.display.Sprite", Sprite);
  14967. ClassUtils.regClass("Laya.Sprite", Sprite);
  14968. class TextStyle extends SpriteStyle {
  14969. constructor() {
  14970. super(...arguments);
  14971. this.italic = false;
  14972. }
  14973. reset() {
  14974. super.reset();
  14975. this.italic = false;
  14976. this.align = "left";
  14977. this.wordWrap = false;
  14978. this.leading = 0;
  14979. this.padding = [0, 0, 0, 0];
  14980. this.bgColor = null;
  14981. this.borderColor = null;
  14982. this.asPassword = false;
  14983. this.stroke = 0;
  14984. this.strokeColor = "#000000";
  14985. this.bold = false;
  14986. this.underline = false;
  14987. this.underlineColor = null;
  14988. this.currBitmapFont = null;
  14989. return this;
  14990. }
  14991. recover() {
  14992. if (this === TextStyle.EMPTY)
  14993. return;
  14994. Pool.recover("TextStyle", this.reset());
  14995. }
  14996. static create() {
  14997. return Pool.getItemByClass("TextStyle", TextStyle);
  14998. }
  14999. render(sprite, context, x, y) {
  15000. (this.bgColor || this.borderColor) && context.drawRect(x - this.pivotX, y - this.pivotY, sprite.width, sprite.height, this.bgColor, this.borderColor, 1);
  15001. }
  15002. }
  15003. TextStyle.EMPTY = new TextStyle();
  15004. class Text extends Sprite {
  15005. constructor() {
  15006. super();
  15007. this._textWidth = 0;
  15008. this._textHeight = 0;
  15009. this._lines = [];
  15010. this._lineWidths = [];
  15011. this._startX = 0;
  15012. this._startY = 0;
  15013. this._charSize = {};
  15014. this._valign = "top";
  15015. this._fontSize = Text.defaultFontSize;
  15016. this._font = Text.defaultFont;
  15017. this._color = "#000000";
  15018. this._singleCharRender = false;
  15019. this.overflow = Text.VISIBLE;
  15020. this._style = TextStyle.EMPTY;
  15021. }
  15022. static defaultFontStr() {
  15023. return Text.defaultFontSize + "px " + Text.defaultFont;
  15024. }
  15025. getStyle() {
  15026. this._style === TextStyle.EMPTY && (this._style = TextStyle.create());
  15027. return this._style;
  15028. }
  15029. _getTextStyle() {
  15030. if (this._style === TextStyle.EMPTY) {
  15031. this._style = TextStyle.create();
  15032. }
  15033. return this._style;
  15034. }
  15035. static registerBitmapFont(name, bitmapFont) {
  15036. Text._bitmapFonts || (Text._bitmapFonts = {});
  15037. Text._bitmapFonts[name] = bitmapFont;
  15038. }
  15039. static unregisterBitmapFont(name, destroy = true) {
  15040. if (Text._bitmapFonts && Text._bitmapFonts[name]) {
  15041. var tBitmapFont = Text._bitmapFonts[name];
  15042. if (destroy)
  15043. tBitmapFont.destroy();
  15044. delete Text._bitmapFonts[name];
  15045. }
  15046. }
  15047. destroy(destroyChild = true) {
  15048. super.destroy(destroyChild);
  15049. this._clipPoint = null;
  15050. this._lines = null;
  15051. this._lineWidths = null;
  15052. this._words && this._words.forEach(function (w) {
  15053. w.cleanCache();
  15054. });
  15055. this._words = null;
  15056. this._charSize = null;
  15057. }
  15058. _getBoundPointsM(ifRotate = false) {
  15059. var rec = Rectangle.TEMP;
  15060. rec.setTo(0, 0, this.width, this.height);
  15061. return rec._getBoundPoints();
  15062. }
  15063. getGraphicBounds(realSize = false) {
  15064. var rec = Rectangle.TEMP;
  15065. rec.setTo(0, 0, this.width, this.height);
  15066. return rec;
  15067. }
  15068. get width() {
  15069. if (this._width)
  15070. return this._width;
  15071. return this.textWidth + this.padding[1] + this.padding[3];
  15072. }
  15073. set width(value) {
  15074. if (value != this._width) {
  15075. super.set_width(value);
  15076. this.isChanged = true;
  15077. if (this.borderColor) {
  15078. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  15079. }
  15080. }
  15081. }
  15082. _getCSSStyle() {
  15083. return this._style;
  15084. }
  15085. get height() {
  15086. if (this._height)
  15087. return this._height;
  15088. return this.textHeight;
  15089. }
  15090. set height(value) {
  15091. if (value != this._height) {
  15092. super.set_height(value);
  15093. this.isChanged = true;
  15094. if (this.borderColor) {
  15095. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  15096. }
  15097. }
  15098. }
  15099. get textWidth() {
  15100. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  15101. return this._textWidth;
  15102. }
  15103. get textHeight() {
  15104. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  15105. return this._textHeight;
  15106. }
  15107. get text() {
  15108. return this._text || "";
  15109. }
  15110. get_text() {
  15111. return this._text || "";
  15112. }
  15113. set_text(value) {
  15114. if (this._text !== value) {
  15115. this.lang(value + "");
  15116. this.isChanged = true;
  15117. this.event(Event.CHANGE);
  15118. if (this.borderColor) {
  15119. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  15120. }
  15121. }
  15122. }
  15123. set text(value) {
  15124. this.set_text(value);
  15125. }
  15126. lang(text, arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null, arg8 = null, arg9 = null, arg10 = null) {
  15127. text = Text.langPacks && Text.langPacks[text] ? Text.langPacks[text] : text;
  15128. if (arguments.length < 2) {
  15129. this._text = text;
  15130. }
  15131. else {
  15132. for (var i = 0, n = arguments.length; i < n; i++) {
  15133. text = text.replace("{" + i + "}", arguments[i + 1]);
  15134. }
  15135. this._text = text;
  15136. }
  15137. }
  15138. get font() {
  15139. return this._font;
  15140. }
  15141. set font(value) {
  15142. if (this._style.currBitmapFont) {
  15143. this._getTextStyle().currBitmapFont = null;
  15144. this.scale(1, 1);
  15145. }
  15146. if (Text._bitmapFonts && Text._bitmapFonts[value]) {
  15147. this._getTextStyle().currBitmapFont = Text._bitmapFonts[value];
  15148. }
  15149. this._font = value;
  15150. this.isChanged = true;
  15151. }
  15152. get fontSize() {
  15153. return this._fontSize;
  15154. }
  15155. set fontSize(value) {
  15156. if (this._fontSize != value) {
  15157. this._fontSize = value;
  15158. this.isChanged = true;
  15159. }
  15160. }
  15161. get bold() {
  15162. return this._style.bold;
  15163. }
  15164. set bold(value) {
  15165. this._getTextStyle().bold = value;
  15166. this.isChanged = true;
  15167. }
  15168. get color() {
  15169. return this._color;
  15170. }
  15171. set color(value) {
  15172. this.set_color(value);
  15173. }
  15174. get_color() {
  15175. return this._color;
  15176. }
  15177. set_color(value) {
  15178. if (this._color != value) {
  15179. this._color = value;
  15180. if (!this._isChanged && this._graphics) {
  15181. this._graphics.replaceTextColor(this.color);
  15182. }
  15183. else {
  15184. this.isChanged = true;
  15185. }
  15186. }
  15187. }
  15188. get italic() {
  15189. return this._style.italic;
  15190. }
  15191. set italic(value) {
  15192. this._getTextStyle().italic = value;
  15193. this.isChanged = true;
  15194. }
  15195. get align() {
  15196. return this._style.align;
  15197. }
  15198. set align(value) {
  15199. this._getTextStyle().align = value;
  15200. this.isChanged = true;
  15201. }
  15202. get valign() {
  15203. return this._valign;
  15204. }
  15205. set valign(value) {
  15206. this._valign = value;
  15207. this.isChanged = true;
  15208. }
  15209. get wordWrap() {
  15210. return this._style.wordWrap;
  15211. }
  15212. set wordWrap(value) {
  15213. this._getTextStyle().wordWrap = value;
  15214. this.isChanged = true;
  15215. }
  15216. get leading() {
  15217. return this._style.leading;
  15218. }
  15219. set leading(value) {
  15220. this._getTextStyle().leading = value;
  15221. this.isChanged = true;
  15222. }
  15223. get padding() {
  15224. return this._style.padding;
  15225. }
  15226. set padding(value) {
  15227. if (typeof (value) == 'string') {
  15228. var arr;
  15229. arr = value.split(",");
  15230. var i, len;
  15231. len = arr.length;
  15232. while (arr.length < 4) {
  15233. arr.push(0);
  15234. }
  15235. for (i = 0; i < len; i++) {
  15236. arr[i] = parseFloat(arr[i]) || 0;
  15237. }
  15238. value = arr;
  15239. }
  15240. this._getTextStyle().padding = value;
  15241. this.isChanged = true;
  15242. }
  15243. get bgColor() {
  15244. return this._style.bgColor;
  15245. }
  15246. set bgColor(value) {
  15247. this.set_bgColor(value);
  15248. }
  15249. set_bgColor(value) {
  15250. this._getTextStyle().bgColor = value;
  15251. this._renderType |= SpriteConst.STYLE;
  15252. this._setBgStyleColor(0, 0, this.width, this.height, value);
  15253. this._setRenderType(this._renderType);
  15254. this.isChanged = true;
  15255. }
  15256. get_bgColor() {
  15257. return this._style.bgColor;
  15258. }
  15259. get borderColor() {
  15260. return this._style.borderColor;
  15261. }
  15262. set borderColor(value) {
  15263. this._getTextStyle().borderColor = value;
  15264. this._renderType |= SpriteConst.STYLE;
  15265. this._setBorderStyleColor(0, 0, this.width, this.height, value, 1);
  15266. this._setRenderType(this._renderType);
  15267. this.isChanged = true;
  15268. }
  15269. get stroke() {
  15270. return this._style.stroke;
  15271. }
  15272. set stroke(value) {
  15273. this._getTextStyle().stroke = value;
  15274. this.isChanged = true;
  15275. }
  15276. get strokeColor() {
  15277. return this._style.strokeColor;
  15278. }
  15279. set strokeColor(value) {
  15280. this._getTextStyle().strokeColor = value;
  15281. this.isChanged = true;
  15282. }
  15283. set isChanged(value) {
  15284. if (this._isChanged !== value) {
  15285. this._isChanged = value;
  15286. value && ILaya.systemTimer.callLater(this, this.typeset);
  15287. }
  15288. }
  15289. _getContextFont() {
  15290. return (this.italic ? "italic " : "") + (this.bold ? "bold " : "") + this.fontSize + "px " + (ILaya.Browser.onIPhone ? (Text.fontFamilyMap[this.font] || this.font) : this.font);
  15291. }
  15292. _isPassWordMode() {
  15293. var style = this._style;
  15294. var password = style.asPassword;
  15295. if (("prompt" in this) && this['prompt'] == this._text)
  15296. password = false;
  15297. return password;
  15298. }
  15299. _getPassWordTxt(txt) {
  15300. var len = txt.length;
  15301. var word;
  15302. word = "";
  15303. for (var j = len; j > 0; j--) {
  15304. word += "●";
  15305. }
  15306. return word;
  15307. }
  15308. _renderText() {
  15309. var padding = this.padding;
  15310. var visibleLineCount = this._lines.length;
  15311. if (this.overflow != Text.VISIBLE) {
  15312. visibleLineCount = Math.min(visibleLineCount, Math.floor((this.height - padding[0] - padding[2]) / (this.leading + this._charSize.height)) + 1);
  15313. }
  15314. var beginLine = this.scrollY / (this._charSize.height + this.leading) | 0;
  15315. var graphics = this.graphics;
  15316. graphics.clear(true);
  15317. var ctxFont = this._getContextFont();
  15318. ILaya.Browser.context.font = ctxFont;
  15319. var startX = padding[3];
  15320. var textAlgin = "left";
  15321. var lines = this._lines;
  15322. var lineHeight = this.leading + this._charSize.height;
  15323. var tCurrBitmapFont = this._style.currBitmapFont;
  15324. if (tCurrBitmapFont) {
  15325. lineHeight = this.leading + tCurrBitmapFont.getMaxHeight();
  15326. }
  15327. var startY = padding[0];
  15328. if ((!tCurrBitmapFont) && this._width > 0 && this._textWidth <= this._width) {
  15329. if (this.align == "right") {
  15330. textAlgin = "right";
  15331. startX = this._width - padding[1];
  15332. }
  15333. else if (this.align == "center") {
  15334. textAlgin = "center";
  15335. startX = this._width * 0.5 + padding[3] - padding[1];
  15336. }
  15337. }
  15338. let bitmapScale = 1;
  15339. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  15340. bitmapScale = tCurrBitmapFont.fontSize / this.fontSize;
  15341. }
  15342. if (this._height > 0) {
  15343. var tempVAlign = (this._textHeight > this._height) ? "top" : this.valign;
  15344. if (tempVAlign === "middle")
  15345. startY = (this._height - visibleLineCount / bitmapScale * lineHeight) * 0.5 + padding[0] - padding[2];
  15346. else if (tempVAlign === "bottom")
  15347. startY = this._height - visibleLineCount / bitmapScale * lineHeight - padding[2];
  15348. }
  15349. if (this._clipPoint) {
  15350. graphics.save();
  15351. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  15352. var tClipWidth;
  15353. var tClipHeight;
  15354. this._width ? tClipWidth = (this._width - padding[3] - padding[1]) : tClipWidth = this._textWidth;
  15355. this._height ? tClipHeight = (this._height - padding[0] - padding[2]) : tClipHeight = this._textHeight;
  15356. tClipWidth *= bitmapScale;
  15357. tClipHeight *= bitmapScale;
  15358. graphics.clipRect(padding[3], padding[0], tClipWidth, tClipHeight);
  15359. }
  15360. else {
  15361. graphics.clipRect(padding[3], padding[0], this._width ? (this._width - padding[3] - padding[1]) : this._textWidth, this._height ? (this._height - padding[0] - padding[2]) : this._textHeight);
  15362. }
  15363. this.repaint();
  15364. }
  15365. var style = this._style;
  15366. var password = style.asPassword;
  15367. if (("prompt" in this) && this['prompt'] == this._text)
  15368. password = false;
  15369. var x = 0, y = 0;
  15370. var end = Math.min(this._lines.length, visibleLineCount + beginLine) || 1;
  15371. for (var i = beginLine; i < end; i++) {
  15372. var word = lines[i];
  15373. var _word;
  15374. if (password) {
  15375. word = "";
  15376. for (var j = word.length; j > 0; j--) {
  15377. word += "●";
  15378. }
  15379. }
  15380. if (word == null)
  15381. word = "";
  15382. x = startX - (this._clipPoint ? this._clipPoint.x : 0);
  15383. y = startY + lineHeight * i - (this._clipPoint ? this._clipPoint.y : 0);
  15384. this.underline && this._drawUnderline(textAlgin, x, y, i);
  15385. if (tCurrBitmapFont) {
  15386. var tWidth = this.width;
  15387. if (tCurrBitmapFont.autoScaleSize) {
  15388. tWidth = this.width * bitmapScale;
  15389. x *= bitmapScale;
  15390. y *= bitmapScale;
  15391. }
  15392. tCurrBitmapFont._drawText(word, this, x, y, this.align, tWidth);
  15393. }
  15394. else {
  15395. this._words || (this._words = []);
  15396. if (this._words.length > (i - beginLine)) {
  15397. _word = this._words[i - beginLine];
  15398. }
  15399. else {
  15400. _word = new WordText();
  15401. this._words.push(_word);
  15402. }
  15403. _word.setText(word);
  15404. _word.splitRender = this._singleCharRender;
  15405. style.stroke ? graphics.fillBorderText(_word, x, y, ctxFont, this.color, textAlgin, style.stroke, style.strokeColor) : graphics.fillText(_word, x, y, ctxFont, this.color, textAlgin);
  15406. }
  15407. }
  15408. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  15409. var tScale = 1 / bitmapScale;
  15410. this.scale(tScale, tScale);
  15411. }
  15412. if (this._clipPoint)
  15413. graphics.restore();
  15414. this._startX = startX;
  15415. this._startY = startY;
  15416. }
  15417. _drawUnderline(align, x, y, lineIndex) {
  15418. var lineWidth = this._lineWidths[lineIndex];
  15419. switch (align) {
  15420. case 'center':
  15421. x -= lineWidth / 2;
  15422. break;
  15423. case 'right':
  15424. x -= lineWidth;
  15425. break;
  15426. case 'left':
  15427. default:
  15428. break;
  15429. }
  15430. y += this._charSize.height;
  15431. this._graphics.drawLine(x, y, x + lineWidth, y, this.underlineColor || this.color, 1);
  15432. }
  15433. typeset() {
  15434. this._isChanged = false;
  15435. if (!this._text) {
  15436. this._clipPoint = null;
  15437. this._textWidth = this._textHeight = 0;
  15438. this.graphics.clear(true);
  15439. return;
  15440. }
  15441. if (ILaya.Render.isConchApp) {
  15442. window.conchTextCanvas.font = this._getContextFont();
  15443. }
  15444. else {
  15445. ILaya.Browser.context.font = this._getContextFont();
  15446. }
  15447. this._lines.length = 0;
  15448. this._lineWidths.length = 0;
  15449. if (this._isPassWordMode()) {
  15450. this._parseLines(this._getPassWordTxt(this._text));
  15451. }
  15452. else
  15453. this._parseLines(this._text);
  15454. this._evalTextSize();
  15455. if (this._checkEnabledViewportOrNot())
  15456. this._clipPoint || (this._clipPoint = new Point(0, 0));
  15457. else
  15458. this._clipPoint = null;
  15459. this._renderText();
  15460. }
  15461. _evalTextSize() {
  15462. var nw, nh;
  15463. nw = Math.max.apply(this, this._lineWidths);
  15464. let bmpFont = this._style.currBitmapFont;
  15465. if (bmpFont) {
  15466. let h = bmpFont.getMaxHeight();
  15467. if (bmpFont.autoScaleSize) {
  15468. h = this.fontSize;
  15469. }
  15470. nh = this._lines.length * (h + this.leading) + this.padding[0] + this.padding[2];
  15471. }
  15472. else {
  15473. nh = this._lines.length * (this._charSize.height + this.leading) + this.padding[0] + this.padding[2];
  15474. if (this._lines.length) {
  15475. nh -= this.leading;
  15476. }
  15477. }
  15478. if (nw != this._textWidth || nh != this._textHeight) {
  15479. this._textWidth = nw;
  15480. this._textHeight = nh;
  15481. }
  15482. }
  15483. _checkEnabledViewportOrNot() {
  15484. return this.overflow == Text.SCROLL && ((this._width > 0 && this._textWidth > this._width) || (this._height > 0 && this._textHeight > this._height));
  15485. }
  15486. changeText(text) {
  15487. if (this._text !== text) {
  15488. this.lang(text + "");
  15489. if (this._graphics && this._graphics.replaceText(this._text)) ;
  15490. else {
  15491. this.typeset();
  15492. }
  15493. }
  15494. }
  15495. _parseLines(text) {
  15496. var needWordWrapOrTruncate = this.wordWrap || this.overflow == Text.HIDDEN;
  15497. if (needWordWrapOrTruncate) {
  15498. var wordWrapWidth = this._getWordWrapWidth();
  15499. }
  15500. var bitmapFont = this._style.currBitmapFont;
  15501. if (bitmapFont) {
  15502. this._charSize.width = bitmapFont.getMaxWidth();
  15503. this._charSize.height = bitmapFont.getMaxHeight();
  15504. }
  15505. else {
  15506. var measureResult = null;
  15507. if (ILaya.Render.isConchApp) {
  15508. measureResult = window.conchTextCanvas.measureText(Text._testWord);
  15509. }
  15510. else {
  15511. measureResult = ILaya.Browser.context.measureText(Text._testWord);
  15512. }
  15513. if (!measureResult)
  15514. measureResult = { width: 100 };
  15515. this._charSize.width = measureResult.width;
  15516. this._charSize.height = (measureResult.height || this.fontSize);
  15517. }
  15518. var lines = text.replace(/\r\n/g, "\n").split("\n");
  15519. for (var i = 0, n = lines.length; i < n; i++) {
  15520. var line = lines[i];
  15521. if (needWordWrapOrTruncate)
  15522. this._parseLine(line, wordWrapWidth);
  15523. else {
  15524. this._lineWidths.push(this._getTextWidth(line));
  15525. this._lines.push(line);
  15526. }
  15527. }
  15528. }
  15529. _parseLine(line, wordWrapWidth) {
  15530. var lines = this._lines;
  15531. var maybeIndex = 0;
  15532. var charsWidth = 0;
  15533. var wordWidth = 0;
  15534. var startIndex = 0;
  15535. charsWidth = this._getTextWidth(line);
  15536. if (charsWidth <= wordWrapWidth) {
  15537. lines.push(line);
  15538. this._lineWidths.push(charsWidth);
  15539. return;
  15540. }
  15541. charsWidth = this._charSize.width;
  15542. maybeIndex = Math.floor(wordWrapWidth / charsWidth);
  15543. (maybeIndex == 0) && (maybeIndex = 1);
  15544. charsWidth = this._getTextWidth(line.substring(0, maybeIndex));
  15545. wordWidth = charsWidth;
  15546. for (var j = maybeIndex, m = line.length; j < m; j++) {
  15547. charsWidth = this._getTextWidth(line.charAt(j));
  15548. wordWidth += charsWidth;
  15549. if (wordWidth > wordWrapWidth) {
  15550. if (this.wordWrap) {
  15551. var newLine = line.substring(startIndex, j);
  15552. if (newLine.charCodeAt(newLine.length - 1) < 255) {
  15553. var execResult = /(?:\w|-)+$/.exec(newLine);
  15554. if (execResult) {
  15555. j = execResult.index + startIndex;
  15556. if (execResult.index == 0)
  15557. j += newLine.length;
  15558. else
  15559. newLine = line.substring(startIndex, j);
  15560. }
  15561. }
  15562. lines.push(newLine);
  15563. this._lineWidths.push(wordWidth - charsWidth);
  15564. startIndex = j;
  15565. if (j + maybeIndex < m) {
  15566. j += maybeIndex;
  15567. charsWidth = this._getTextWidth(line.substring(startIndex, j));
  15568. wordWidth = charsWidth;
  15569. j--;
  15570. }
  15571. else {
  15572. lines.push(line.substring(startIndex, m));
  15573. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  15574. startIndex = -1;
  15575. break;
  15576. }
  15577. }
  15578. else if (this.overflow == Text.HIDDEN) {
  15579. lines.push(line.substring(0, j));
  15580. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  15581. return;
  15582. }
  15583. }
  15584. }
  15585. if (this.wordWrap && startIndex != -1) {
  15586. lines.push(line.substring(startIndex, m));
  15587. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  15588. }
  15589. }
  15590. _getTextWidth(text) {
  15591. var bitmapFont = this._style.currBitmapFont;
  15592. if (bitmapFont)
  15593. return bitmapFont.getTextWidth(text);
  15594. else {
  15595. if (ILaya.Render.isConchApp) {
  15596. return window.conchTextCanvas.measureText(text).width;
  15597. }
  15598. else {
  15599. let ret = ILaya.Browser.context.measureText(text) || { width: 100 };
  15600. return ret.width;
  15601. }
  15602. }
  15603. }
  15604. _getWordWrapWidth() {
  15605. var p = this.padding;
  15606. var w;
  15607. var bitmapFont = this._style.currBitmapFont;
  15608. if (bitmapFont && bitmapFont.autoScaleSize)
  15609. w = this._width * (bitmapFont.fontSize / this.fontSize);
  15610. else
  15611. w = this._width;
  15612. if (w <= 0) {
  15613. w = this.wordWrap ? 100 : ILaya.Browser.width;
  15614. }
  15615. w <= 0 && (w = 100);
  15616. return w - p[3] - p[1];
  15617. }
  15618. getCharPoint(charIndex, out = null) {
  15619. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  15620. var len = 0, lines = this._lines, startIndex = 0;
  15621. for (var i = 0, n = lines.length; i < n; i++) {
  15622. len += lines[i].length;
  15623. if (charIndex < len) {
  15624. var line = i;
  15625. break;
  15626. }
  15627. startIndex = len;
  15628. }
  15629. var ctxFont = (this.italic ? "italic " : "") + (this.bold ? "bold " : "") + this.fontSize + "px " + this.font;
  15630. ILaya.Browser.context.font = ctxFont;
  15631. var width = this._getTextWidth(this._text.substring(startIndex, charIndex));
  15632. var point = out || new Point();
  15633. return point.setTo(this._startX + width - (this._clipPoint ? this._clipPoint.x : 0), this._startY + line * (this._charSize.height + this.leading) - (this._clipPoint ? this._clipPoint.y : 0));
  15634. }
  15635. set scrollX(value) {
  15636. if (this.overflow != Text.SCROLL || (this.textWidth < this._width || !this._clipPoint))
  15637. return;
  15638. value = value < this.padding[3] ? this.padding[3] : value;
  15639. var maxScrollX = this._textWidth - this._width;
  15640. value = value > maxScrollX ? maxScrollX : value;
  15641. this._clipPoint.x = value;
  15642. this._renderText();
  15643. }
  15644. get scrollX() {
  15645. if (!this._clipPoint)
  15646. return 0;
  15647. return this._clipPoint.x;
  15648. }
  15649. set scrollY(value) {
  15650. if (this.overflow != Text.SCROLL || (this.textHeight < this._height || !this._clipPoint))
  15651. return;
  15652. value = value < this.padding[0] ? this.padding[0] : value;
  15653. var maxScrollY = this._textHeight - this._height;
  15654. value = value > maxScrollY ? maxScrollY : value;
  15655. this._clipPoint.y = value;
  15656. this._renderText();
  15657. }
  15658. get scrollY() {
  15659. if (!this._clipPoint)
  15660. return 0;
  15661. return this._clipPoint.y;
  15662. }
  15663. get maxScrollX() {
  15664. return (this.textWidth < this._width) ? 0 : this._textWidth - this._width;
  15665. }
  15666. get maxScrollY() {
  15667. return (this.textHeight < this._height) ? 0 : this._textHeight - this._height;
  15668. }
  15669. get lines() {
  15670. if (this._isChanged)
  15671. this.typeset();
  15672. return this._lines;
  15673. }
  15674. get underlineColor() {
  15675. return this._style.underlineColor;
  15676. }
  15677. set underlineColor(value) {
  15678. this._getTextStyle().underlineColor = value;
  15679. if (!this._isChanged)
  15680. this._renderText();
  15681. }
  15682. get underline() {
  15683. return this._style.underline;
  15684. }
  15685. set underline(value) {
  15686. this._getTextStyle().underline = value;
  15687. }
  15688. set singleCharRender(value) {
  15689. this._singleCharRender = value;
  15690. }
  15691. get singleCharRender() {
  15692. return this._singleCharRender;
  15693. }
  15694. }
  15695. Text.VISIBLE = "visible";
  15696. Text.SCROLL = "scroll";
  15697. Text.HIDDEN = "hidden";
  15698. Text.defaultFontSize = 12;
  15699. Text.defaultFont = "Arial";
  15700. Text.isComplexText = false;
  15701. Text.fontFamilyMap = { "报隶": "报隶-简", "黑体": "黑体-简", "楷体": "楷体-简", "兰亭黑": "兰亭黑-简", "隶变": "隶变-简", "凌慧体": "凌慧体-简", "翩翩体": "翩翩体-简", "苹方": "苹方-简", "手札体": "手札体-简", "宋体": "宋体-简", "娃娃体": "娃娃体-简", "魏碑": "魏碑-简", "行楷": "行楷-简", "雅痞": "雅痞-简", "圆体": "圆体-简" };
  15702. Text._testWord = "游";
  15703. Text.CharacterCache = true;
  15704. Text.RightToLeft = false;
  15705. ILaya.regClass(Text);
  15706. ClassUtils.regClass("laya.display.Text", Text);
  15707. ClassUtils.regClass("Laya.Text", Text);
  15708. class Input extends Text {
  15709. constructor() {
  15710. super();
  15711. this._multiline = false;
  15712. this._editable = true;
  15713. this._maxChars = 1E5;
  15714. this._type = "text";
  15715. this._prompt = '';
  15716. this._promptColor = "#A9A9A9";
  15717. this._originColor = "#000000";
  15718. this._content = '';
  15719. Input.IOS_IFRAME = (ILaya.Browser.onIOS && ILaya.Browser.window.top != ILaya.Browser.window.self);
  15720. this._width = 100;
  15721. this._height = 20;
  15722. this.multiline = false;
  15723. this.overflow = Text.SCROLL;
  15724. this.on(Event.MOUSE_DOWN, this, this._onMouseDown);
  15725. this.on(Event.UNDISPLAY, this, this._onUnDisplay);
  15726. }
  15727. static __init__() {
  15728. Input._createInputElement();
  15729. if (ILaya.Browser.onMobile) {
  15730. var isTrue = false;
  15731. if (ILaya.Browser.onMiniGame || ILaya.Browser.onBDMiniGame || ILaya.Browser.onQGMiniGame || ILaya.Browser.onKGMiniGame || ILaya.Browser.onVVMiniGame || ILaya.Browser.onAlipayMiniGame || ILaya.Browser.onQQMiniGame || ILaya.Browser.onBLMiniGame || ILaya.Browser.onTTMiniGame || ILaya.Browser.onHWMiniGame || ILaya.Browser.onTBMiniGame) {
  15732. isTrue = true;
  15733. }
  15734. ILaya.Render.canvas.addEventListener(Input.IOS_IFRAME ? (isTrue ? "touchend" : "click") : "touchend", Input._popupInputMethod);
  15735. }
  15736. }
  15737. static _popupInputMethod(e) {
  15738. if (!Input.isInputting)
  15739. return;
  15740. var input = Input.inputElement;
  15741. input.focus();
  15742. }
  15743. static _createInputElement() {
  15744. Input._initInput(Input.area = ILaya.Browser.createElement("textarea"));
  15745. Input._initInput(Input.input = ILaya.Browser.createElement("input"));
  15746. Input.inputContainer = ILaya.Browser.createElement("div");
  15747. Input.inputContainer.style.position = "absolute";
  15748. Input.inputContainer.style.zIndex = 1E5;
  15749. ILaya.Browser.container.appendChild(Input.inputContainer);
  15750. Input.inputContainer.setPos = function (x, y) {
  15751. Input.inputContainer.style.left = x + 'px';
  15752. Input.inputContainer.style.top = y + 'px';
  15753. };
  15754. }
  15755. static _initInput(input) {
  15756. var style = input.style;
  15757. style.cssText = "position:absolute;overflow:hidden;resize:none;transform-origin:0 0;-webkit-transform-origin:0 0;-moz-transform-origin:0 0;-o-transform-origin:0 0;";
  15758. style.resize = 'none';
  15759. style.backgroundColor = 'transparent';
  15760. style.border = 'none';
  15761. style.outline = 'none';
  15762. style.zIndex = 1;
  15763. input.addEventListener('input', Input._processInputting);
  15764. input.addEventListener('mousemove', Input._stopEvent);
  15765. input.addEventListener('mousedown', Input._stopEvent);
  15766. input.addEventListener('touchmove', Input._stopEvent);
  15767. input.setFontFace = function (fontFace) { input.style.fontFamily = fontFace; };
  15768. if (!ILaya.Render.isConchApp) {
  15769. input.setColor = function (color) { input.style.color = color; };
  15770. input.setFontSize = function (fontSize) { input.style.fontSize = fontSize + 'px'; };
  15771. }
  15772. }
  15773. static _processInputting(e) {
  15774. var input = Input.inputElement.target;
  15775. if (!input)
  15776. return;
  15777. var value = Input.inputElement.value;
  15778. if (input._restrictPattern) {
  15779. value = value.replace(/\u2006|\x27/g, "");
  15780. if (input._restrictPattern.test(value)) {
  15781. value = value.replace(input._restrictPattern, "");
  15782. Input.inputElement.value = value;
  15783. }
  15784. }
  15785. input._text = value;
  15786. input.event(Event.INPUT);
  15787. }
  15788. static _stopEvent(e) {
  15789. if (e.type == 'touchmove')
  15790. e.preventDefault();
  15791. e.stopPropagation && e.stopPropagation();
  15792. }
  15793. setSelection(startIndex, endIndex) {
  15794. this.focus = true;
  15795. Input.inputElement.selectionStart = startIndex;
  15796. Input.inputElement.selectionEnd = endIndex;
  15797. }
  15798. get multiline() {
  15799. return this._multiline;
  15800. }
  15801. set multiline(value) {
  15802. this._multiline = value;
  15803. this.valign = value ? "top" : "middle";
  15804. }
  15805. get nativeInput() {
  15806. return this._multiline ? Input.area : Input.input;
  15807. }
  15808. _onUnDisplay(e = null) {
  15809. this.focus = false;
  15810. }
  15811. _onMouseDown(e) {
  15812. this.focus = true;
  15813. }
  15814. _syncInputTransform() {
  15815. var inputElement = this.nativeInput;
  15816. var transform = Utils.getTransformRelativeToWindow(this, this.padding[3], this.padding[0]);
  15817. var inputWid = this._width - this.padding[1] - this.padding[3];
  15818. var inputHei = this._height - this.padding[0] - this.padding[2];
  15819. if (ILaya.Render.isConchApp) {
  15820. inputElement.setScale(transform.scaleX, transform.scaleY);
  15821. inputElement.setSize(inputWid, inputHei);
  15822. inputElement.setPos(transform.x, transform.y);
  15823. }
  15824. else {
  15825. Input.inputContainer.style.transform = Input.inputContainer.style.webkitTransform = "scale(" + transform.scaleX + "," + transform.scaleY + ") rotate(" + (ILaya.stage.canvasDegree) + "deg)";
  15826. inputElement.style.width = inputWid + 'px';
  15827. inputElement.style.height = inputHei + 'px';
  15828. Input.inputContainer.style.left = transform.x + 'px';
  15829. Input.inputContainer.style.top = transform.y + 'px';
  15830. }
  15831. }
  15832. select() {
  15833. this.nativeInput.select();
  15834. }
  15835. get focus() {
  15836. return this._focus;
  15837. }
  15838. set focus(value) {
  15839. var input = this.nativeInput;
  15840. if (this._focus !== value) {
  15841. if (value) {
  15842. if (input.target) {
  15843. input.target._focusOut();
  15844. }
  15845. else {
  15846. this._setInputMethod();
  15847. }
  15848. input.target = this;
  15849. this._focusIn();
  15850. }
  15851. else {
  15852. input.target = null;
  15853. this._focusOut();
  15854. ILaya.Browser.document.body.scrollTop = 0;
  15855. input.blur();
  15856. if (ILaya.Render.isConchApp)
  15857. input.setPos(-10000, -10000);
  15858. else if (Input.inputContainer.contains(input))
  15859. Input.inputContainer.removeChild(input);
  15860. }
  15861. }
  15862. }
  15863. _setInputMethod() {
  15864. Input.input.parentElement && (Input.inputContainer.removeChild(Input.input));
  15865. Input.area.parentElement && (Input.inputContainer.removeChild(Input.area));
  15866. Input.inputElement = (this._multiline ? Input.area : Input.input);
  15867. Input.inputContainer.appendChild(Input.inputElement);
  15868. if (Text.RightToLeft) {
  15869. Input.inputElement.style.direction = "rtl";
  15870. }
  15871. }
  15872. _focusIn() {
  15873. Input.isInputting = true;
  15874. var input = this.nativeInput;
  15875. Input.input && (Input.input.type = this._type);
  15876. this._focus = true;
  15877. var cssStyle = input.style;
  15878. cssStyle.whiteSpace = (this.wordWrap ? "pre-wrap" : "nowrap");
  15879. this._setPromptColor();
  15880. input.readOnly = !this._editable;
  15881. if (ILaya.Render.isConchApp) {
  15882. input.setType(this._type);
  15883. input.setForbidEdit(!this._editable);
  15884. }
  15885. input.maxLength = this._maxChars;
  15886. var padding = this.padding;
  15887. input.value = this._content;
  15888. input.placeholder = this._prompt;
  15889. ILaya.stage.off(Event.KEY_DOWN, this, this._onKeyDown);
  15890. ILaya.stage.on(Event.KEY_DOWN, this, this._onKeyDown);
  15891. ILaya.stage.focus = this;
  15892. this.event(Event.FOCUS);
  15893. if (ILaya.Browser.onPC)
  15894. input.focus();
  15895. if (!ILaya.Browser.onMiniGame && !ILaya.Browser.onBDMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame && !ILaya.Browser.onBLMiniGame && !ILaya.Browser.onTTMiniGame && !ILaya.Browser.onHWMiniGame && !ILaya.Browser.onTBMiniGame) {
  15896. var temp = this._text;
  15897. this._text = null;
  15898. }
  15899. this.typeset();
  15900. input.setColor(this._originColor);
  15901. input.setFontSize(this.fontSize);
  15902. input.setFontFace(ILaya.Browser.onIPhone ? (Text.fontFamilyMap[this.font] || this.font) : this.font);
  15903. if (ILaya.Render.isConchApp) {
  15904. input.setMultiAble && input.setMultiAble(this._multiline);
  15905. }
  15906. cssStyle.lineHeight = (this.leading + this.fontSize) + "px";
  15907. cssStyle.fontStyle = (this.italic ? "italic" : "normal");
  15908. cssStyle.fontWeight = (this.bold ? "bold" : "normal");
  15909. cssStyle.textAlign = this.align;
  15910. cssStyle.padding = "0 0";
  15911. this._syncInputTransform();
  15912. if (!ILaya.Render.isConchApp && ILaya.Browser.onPC)
  15913. ILaya.systemTimer.frameLoop(1, this, this._syncInputTransform);
  15914. }
  15915. _setPromptColor() {
  15916. Input.promptStyleDOM = ILaya.Browser.getElementById("promptStyle");
  15917. if (!Input.promptStyleDOM) {
  15918. Input.promptStyleDOM = ILaya.Browser.createElement("style");
  15919. Input.promptStyleDOM.setAttribute("id", "promptStyle");
  15920. ILaya.Browser.document.head.appendChild(Input.promptStyleDOM);
  15921. }
  15922. Input.promptStyleDOM.innerText = "input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {" + "color:" + this._promptColor + "}" + "input:-moz-placeholder, textarea:-moz-placeholder {" + "color:" + this._promptColor + "}" + "input::-moz-placeholder, textarea::-moz-placeholder {" + "color:" + this._promptColor + "}" + "input:-ms-input-placeholder, textarea:-ms-input-placeholder {" + "color:" + this._promptColor + "}";
  15923. }
  15924. _focusOut() {
  15925. if (!Input.isInputting)
  15926. return;
  15927. Input.isInputting = false;
  15928. this._focus = false;
  15929. this._text = null;
  15930. this._content = this.nativeInput.value;
  15931. if (!this._content) {
  15932. super.set_text(this._prompt);
  15933. super.set_color(this._promptColor);
  15934. }
  15935. else {
  15936. super.set_text(this._content);
  15937. super.set_color(this._originColor);
  15938. }
  15939. ILaya.stage.off(Event.KEY_DOWN, this, this._onKeyDown);
  15940. ILaya.stage.focus = null;
  15941. this.event(Event.BLUR);
  15942. this.event(Event.CHANGE);
  15943. if (ILaya.Render.isConchApp)
  15944. this.nativeInput.blur();
  15945. ILaya.Browser.onPC && ILaya.systemTimer.clear(this, this._syncInputTransform);
  15946. }
  15947. _onKeyDown(e) {
  15948. if (e.keyCode === 13) {
  15949. if (ILaya.Browser.onMobile && !this._multiline)
  15950. this.focus = false;
  15951. this.event(Event.ENTER);
  15952. }
  15953. }
  15954. set text(value) {
  15955. super.set_color(this._originColor);
  15956. value += '';
  15957. if (this._focus) {
  15958. this.nativeInput.value = value || '';
  15959. this.event(Event.CHANGE);
  15960. }
  15961. else {
  15962. if (!this._multiline)
  15963. value = value.replace(/\r?\n/g, '');
  15964. this._content = value;
  15965. if (value)
  15966. super.set_text(value);
  15967. else {
  15968. super.set_text(this._prompt);
  15969. super.set_color(this.promptColor);
  15970. }
  15971. }
  15972. }
  15973. get text() {
  15974. if (this._focus)
  15975. return this.nativeInput.value;
  15976. else
  15977. return this._content || "";
  15978. }
  15979. changeText(text) {
  15980. this._content = text;
  15981. if (this._focus) {
  15982. this.nativeInput.value = text || '';
  15983. this.event(Event.CHANGE);
  15984. }
  15985. else
  15986. super.changeText(text);
  15987. }
  15988. set color(value) {
  15989. if (this._focus)
  15990. this.nativeInput.setColor(value);
  15991. super.set_color(this._content ? value : this._promptColor);
  15992. this._originColor = value;
  15993. }
  15994. get color() {
  15995. return super.color;
  15996. }
  15997. set bgColor(value) {
  15998. super.set_bgColor(value);
  15999. if (ILaya.Render.isConchApp)
  16000. this.nativeInput.setBgColor(value);
  16001. }
  16002. get bgColor() {
  16003. return super.bgColor;
  16004. }
  16005. get restrict() {
  16006. if (this._restrictPattern) {
  16007. return this._restrictPattern.source;
  16008. }
  16009. return "";
  16010. }
  16011. set restrict(pattern) {
  16012. if (pattern) {
  16013. pattern = "[^" + pattern + "]";
  16014. if (pattern.indexOf("^^") > -1)
  16015. pattern = pattern.replace("^^", "");
  16016. this._restrictPattern = new RegExp(pattern, "g");
  16017. }
  16018. else
  16019. this._restrictPattern = null;
  16020. }
  16021. set editable(value) {
  16022. this._editable = value;
  16023. if (ILaya.Render.isConchApp) {
  16024. Input.input.setForbidEdit(!value);
  16025. }
  16026. }
  16027. get editable() {
  16028. return this._editable;
  16029. }
  16030. get maxChars() {
  16031. return this._maxChars;
  16032. }
  16033. set maxChars(value) {
  16034. if (value <= 0)
  16035. value = 1E5;
  16036. this._maxChars = value;
  16037. }
  16038. get prompt() {
  16039. return this._prompt;
  16040. }
  16041. set prompt(value) {
  16042. if (!this._text && value)
  16043. super.set_color(this._promptColor);
  16044. this.promptColor = this._promptColor;
  16045. if (this._text)
  16046. super.set_text((this._text == this._prompt) ? value : this._text);
  16047. else
  16048. super.set_text(value);
  16049. this._prompt = Text.langPacks && Text.langPacks[value] ? Text.langPacks[value] : value;
  16050. }
  16051. get promptColor() {
  16052. return this._promptColor;
  16053. }
  16054. set promptColor(value) {
  16055. this._promptColor = value;
  16056. if (!this._content)
  16057. super.set_color(value);
  16058. }
  16059. get type() {
  16060. return this._type;
  16061. }
  16062. set type(value) {
  16063. if (value === "password")
  16064. this._getTextStyle().asPassword = true;
  16065. else
  16066. this._getTextStyle().asPassword = false;
  16067. this._type = value;
  16068. }
  16069. }
  16070. Input.TYPE_TEXT = "text";
  16071. Input.TYPE_PASSWORD = "password";
  16072. Input.TYPE_EMAIL = "email";
  16073. Input.TYPE_URL = "url";
  16074. Input.TYPE_NUMBER = "number";
  16075. Input.TYPE_RANGE = "range";
  16076. Input.TYPE_DATE = "date";
  16077. Input.TYPE_MONTH = "month";
  16078. Input.TYPE_WEEK = "week";
  16079. Input.TYPE_TIME = "time";
  16080. Input.TYPE_DATE_TIME = "datetime";
  16081. Input.TYPE_DATE_TIME_LOCAL = "datetime-local";
  16082. Input.TYPE_SEARCH = "search";
  16083. Input.IOS_IFRAME = false;
  16084. Input.inputHeight = 45;
  16085. Input.isInputting = false;
  16086. ClassUtils.regClass("laya.display.Input", Input);
  16087. ClassUtils.regClass("Laya.Input", Input);
  16088. class TouchManager {
  16089. constructor() {
  16090. this.preOvers = [];
  16091. this.preDowns = [];
  16092. this.preRightDowns = [];
  16093. this.enable = true;
  16094. this._event = new Event();
  16095. this._lastClickTime = 0;
  16096. }
  16097. _clearTempArrs() {
  16098. TouchManager._oldArr.length = 0;
  16099. TouchManager._newArr.length = 0;
  16100. TouchManager._tEleArr.length = 0;
  16101. }
  16102. getTouchFromArr(touchID, arr) {
  16103. var i, len;
  16104. len = arr.length;
  16105. var tTouchO;
  16106. for (i = 0; i < len; i++) {
  16107. tTouchO = arr[i];
  16108. if (tTouchO.id == touchID) {
  16109. return tTouchO;
  16110. }
  16111. }
  16112. return null;
  16113. }
  16114. removeTouchFromArr(touchID, arr) {
  16115. var i;
  16116. for (i = arr.length - 1; i >= 0; i--) {
  16117. if (arr[i].id == touchID) {
  16118. arr.splice(i, 1);
  16119. }
  16120. }
  16121. }
  16122. createTouchO(ele, touchID) {
  16123. var rst;
  16124. rst = Pool.getItem("TouchData") || {};
  16125. rst.id = touchID;
  16126. rst.tar = ele;
  16127. return rst;
  16128. }
  16129. onMouseDown(ele, touchID, isLeft = false) {
  16130. if (!this.enable)
  16131. return;
  16132. var preO;
  16133. var tO;
  16134. var arrs;
  16135. preO = this.getTouchFromArr(touchID, this.preOvers);
  16136. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  16137. if (!preO) {
  16138. tO = this.createTouchO(ele, touchID);
  16139. this.preOvers.push(tO);
  16140. }
  16141. else {
  16142. preO.tar = ele;
  16143. }
  16144. if (Browser.onMobile)
  16145. this.sendEvents(arrs, Event.MOUSE_OVER);
  16146. var preDowns;
  16147. preDowns = isLeft ? this.preDowns : this.preRightDowns;
  16148. preO = this.getTouchFromArr(touchID, preDowns);
  16149. if (!preO) {
  16150. tO = this.createTouchO(ele, touchID);
  16151. preDowns.push(tO);
  16152. }
  16153. else {
  16154. preO.tar = ele;
  16155. }
  16156. this.sendEvents(arrs, isLeft ? Event.MOUSE_DOWN : Event.RIGHT_MOUSE_DOWN);
  16157. this._clearTempArrs();
  16158. }
  16159. sendEvents(eles, type) {
  16160. var i, len;
  16161. len = eles.length;
  16162. this._event._stoped = false;
  16163. var _target;
  16164. _target = eles[0];
  16165. for (i = 0; i < len; i++) {
  16166. var tE = eles[i];
  16167. if (tE.destroyed)
  16168. return;
  16169. tE.event(type, this._event.setTo(type, tE, _target));
  16170. if (this._event._stoped)
  16171. break;
  16172. }
  16173. }
  16174. getEles(start, end = null, rst = null) {
  16175. if (!rst) {
  16176. rst = [];
  16177. }
  16178. else {
  16179. rst.length = 0;
  16180. }
  16181. while (start && start != end) {
  16182. rst.push(start);
  16183. start = start.parent;
  16184. }
  16185. return rst;
  16186. }
  16187. checkMouseOutAndOverOfMove(eleNew, elePre, touchID = 0) {
  16188. if (elePre == eleNew)
  16189. return;
  16190. var tar;
  16191. var arrs;
  16192. var i, len;
  16193. if (elePre.contains(eleNew)) {
  16194. arrs = this.getEles(eleNew, elePre, TouchManager._tEleArr);
  16195. this.sendEvents(arrs, Event.MOUSE_OVER);
  16196. }
  16197. else if (eleNew.contains(elePre)) {
  16198. arrs = this.getEles(elePre, eleNew, TouchManager._tEleArr);
  16199. this.sendEvents(arrs, Event.MOUSE_OUT);
  16200. }
  16201. else {
  16202. arrs = TouchManager._tEleArr;
  16203. arrs.length = 0;
  16204. var oldArr;
  16205. oldArr = this.getEles(elePre, null, TouchManager._oldArr);
  16206. var newArr;
  16207. newArr = this.getEles(eleNew, null, TouchManager._newArr);
  16208. len = oldArr.length;
  16209. var tIndex;
  16210. for (i = 0; i < len; i++) {
  16211. tar = oldArr[i];
  16212. tIndex = newArr.indexOf(tar);
  16213. if (tIndex >= 0) {
  16214. newArr.splice(tIndex, newArr.length - tIndex);
  16215. break;
  16216. }
  16217. else {
  16218. arrs.push(tar);
  16219. }
  16220. }
  16221. if (arrs.length > 0) {
  16222. this.sendEvents(arrs, Event.MOUSE_OUT);
  16223. }
  16224. if (newArr.length > 0) {
  16225. this.sendEvents(newArr, Event.MOUSE_OVER);
  16226. }
  16227. }
  16228. }
  16229. onMouseMove(ele, touchID) {
  16230. if (!this.enable)
  16231. return;
  16232. var preO;
  16233. preO = this.getTouchFromArr(touchID, this.preOvers);
  16234. var arrs;
  16235. if (!preO) {
  16236. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  16237. this.sendEvents(arrs, Event.MOUSE_OVER);
  16238. this.preOvers.push(this.createTouchO(ele, touchID));
  16239. }
  16240. else {
  16241. this.checkMouseOutAndOverOfMove(ele, preO.tar);
  16242. preO.tar = ele;
  16243. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  16244. }
  16245. this.sendEvents(arrs, Event.MOUSE_MOVE);
  16246. this._clearTempArrs();
  16247. }
  16248. getLastOvers() {
  16249. TouchManager._tEleArr.length = 0;
  16250. if (this.preOvers.length > 0 && this.preOvers[0].tar) {
  16251. return this.getEles(this.preOvers[0].tar, null, TouchManager._tEleArr);
  16252. }
  16253. TouchManager._tEleArr.push(ILaya.stage);
  16254. return TouchManager._tEleArr;
  16255. }
  16256. stageMouseOut() {
  16257. var lastOvers;
  16258. lastOvers = this.getLastOvers();
  16259. this.preOvers.length = 0;
  16260. this.sendEvents(lastOvers, Event.MOUSE_OUT);
  16261. }
  16262. onMouseUp(ele, touchID, isLeft = false) {
  16263. if (!this.enable)
  16264. return;
  16265. var preO;
  16266. var arrs;
  16267. var oldArr;
  16268. var i, len;
  16269. var tar;
  16270. var sendArr;
  16271. var onMobile = Browser.onMobile;
  16272. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  16273. this.sendEvents(arrs, isLeft ? Event.MOUSE_UP : Event.RIGHT_MOUSE_UP);
  16274. var preDowns;
  16275. preDowns = isLeft ? this.preDowns : this.preRightDowns;
  16276. preO = this.getTouchFromArr(touchID, preDowns);
  16277. if (!preO) ;
  16278. else {
  16279. var isDouble;
  16280. var now = Browser.now();
  16281. isDouble = now - this._lastClickTime < 300;
  16282. this._lastClickTime = now;
  16283. if (ele == preO.tar) {
  16284. sendArr = arrs;
  16285. }
  16286. else {
  16287. oldArr = this.getEles(preO.tar, null, TouchManager._oldArr);
  16288. sendArr = TouchManager._newArr;
  16289. sendArr.length = 0;
  16290. len = oldArr.length;
  16291. for (i = 0; i < len; i++) {
  16292. tar = oldArr[i];
  16293. if (arrs.indexOf(tar) >= 0) {
  16294. sendArr.push(tar);
  16295. }
  16296. }
  16297. }
  16298. if (sendArr.length > 0) {
  16299. this.sendEvents(sendArr, isLeft ? Event.CLICK : Event.RIGHT_CLICK);
  16300. }
  16301. if (isLeft && isDouble) {
  16302. this.sendEvents(sendArr, Event.DOUBLE_CLICK);
  16303. }
  16304. this.removeTouchFromArr(touchID, preDowns);
  16305. preO.tar = null;
  16306. Pool.recover("TouchData", preO);
  16307. }
  16308. preO = this.getTouchFromArr(touchID, this.preOvers);
  16309. if (!preO) ;
  16310. else {
  16311. if (onMobile) {
  16312. sendArr = this.getEles(preO.tar, null, sendArr);
  16313. if (sendArr && sendArr.length > 0) {
  16314. this.sendEvents(sendArr, Event.MOUSE_OUT);
  16315. }
  16316. this.removeTouchFromArr(touchID, this.preOvers);
  16317. preO.tar = null;
  16318. Pool.recover("TouchData", preO);
  16319. }
  16320. }
  16321. this._clearTempArrs();
  16322. }
  16323. }
  16324. TouchManager.I = new TouchManager();
  16325. TouchManager._oldArr = [];
  16326. TouchManager._newArr = [];
  16327. TouchManager._tEleArr = [];
  16328. class MouseManager {
  16329. constructor() {
  16330. this.mouseX = 0;
  16331. this.mouseY = 0;
  16332. this.disableMouseEvent = false;
  16333. this.mouseDownTime = 0;
  16334. this.mouseMoveAccuracy = 2;
  16335. this._event = new Event();
  16336. this._captureSp = null;
  16337. this._captureChain = [];
  16338. this._captureExlusiveMode = false;
  16339. this._hitCaputreSp = false;
  16340. this._point = new Point();
  16341. this._rect = new Rectangle();
  16342. this._lastMoveTimer = 0;
  16343. this._prePoint = new Point();
  16344. this._touchIDs = {};
  16345. this._curTouchID = NaN;
  16346. this._id = 1;
  16347. }
  16348. __init__(stage, canvas) {
  16349. this._stage = stage;
  16350. var _this = this;
  16351. canvas.oncontextmenu = function (e) {
  16352. if (MouseManager.enabled)
  16353. return false;
  16354. };
  16355. canvas.addEventListener('mousedown', function (e) {
  16356. if (MouseManager.enabled) {
  16357. if (!Browser.onIE)
  16358. (e.cancelable) && (e.preventDefault());
  16359. _this.mouseDownTime = Browser.now();
  16360. _this.runEvent(e);
  16361. }
  16362. });
  16363. canvas.addEventListener('mouseup', function (e) {
  16364. if (MouseManager.enabled) {
  16365. (e.cancelable) && (e.preventDefault());
  16366. _this.mouseDownTime = -Browser.now();
  16367. _this.runEvent(e);
  16368. }
  16369. }, true);
  16370. canvas.addEventListener('mousemove', function (e) {
  16371. if (MouseManager.enabled) {
  16372. (e.cancelable) && (e.preventDefault());
  16373. var now = Browser.now();
  16374. if (now - _this._lastMoveTimer < 10)
  16375. return;
  16376. _this._lastMoveTimer = now;
  16377. _this.runEvent(e);
  16378. }
  16379. }, true);
  16380. canvas.addEventListener("mouseout", function (e) {
  16381. if (MouseManager.enabled)
  16382. _this.runEvent(e);
  16383. });
  16384. canvas.addEventListener("mouseover", function (e) {
  16385. if (MouseManager.enabled)
  16386. _this.runEvent(e);
  16387. });
  16388. canvas.addEventListener("touchstart", function (e) {
  16389. if (MouseManager.enabled) {
  16390. if (!MouseManager._isFirstTouch && !Input.isInputting)
  16391. (e.cancelable) && (e.preventDefault());
  16392. _this.mouseDownTime = Browser.now();
  16393. _this.runEvent(e);
  16394. }
  16395. });
  16396. canvas.addEventListener("touchend", function (e) {
  16397. if (MouseManager.enabled) {
  16398. if (!MouseManager._isFirstTouch && !Input.isInputting)
  16399. (e.cancelable) && (e.preventDefault());
  16400. MouseManager._isFirstTouch = false;
  16401. _this.mouseDownTime = -Browser.now();
  16402. _this.runEvent(e);
  16403. }
  16404. else {
  16405. _this._curTouchID = NaN;
  16406. }
  16407. }, true);
  16408. canvas.addEventListener("touchmove", function (e) {
  16409. if (MouseManager.enabled) {
  16410. (e.cancelable) && (e.preventDefault());
  16411. _this.runEvent(e);
  16412. }
  16413. }, true);
  16414. canvas.addEventListener("touchcancel", function (e) {
  16415. if (MouseManager.enabled) {
  16416. (e.cancelable) && (e.preventDefault());
  16417. _this.runEvent(e);
  16418. }
  16419. else {
  16420. _this._curTouchID = NaN;
  16421. }
  16422. }, true);
  16423. canvas.addEventListener('mousewheel', function (e) {
  16424. if (MouseManager.enabled)
  16425. _this.runEvent(e);
  16426. });
  16427. canvas.addEventListener('DOMMouseScroll', function (e) {
  16428. if (MouseManager.enabled)
  16429. _this.runEvent(e);
  16430. });
  16431. }
  16432. initEvent(e, nativeEvent = null) {
  16433. var _this = this;
  16434. _this._event._stoped = false;
  16435. _this._event.nativeEvent = nativeEvent || e;
  16436. _this._target = null;
  16437. this._point.setTo(e.pageX || e.clientX, e.pageY || e.clientY);
  16438. if (this._stage._canvasTransform) {
  16439. this._stage._canvasTransform.invertTransformPoint(this._point);
  16440. _this.mouseX = this._point.x;
  16441. _this.mouseY = this._point.y;
  16442. }
  16443. _this._event.touchId = e.identifier || 0;
  16444. this._tTouchID = _this._event.touchId;
  16445. var evt;
  16446. evt = TouchManager.I._event;
  16447. evt._stoped = false;
  16448. evt.nativeEvent = _this._event.nativeEvent;
  16449. evt.touchId = _this._event.touchId;
  16450. }
  16451. checkMouseWheel(e) {
  16452. this._event.delta = e.wheelDelta ? e.wheelDelta * 0.025 : -e.detail;
  16453. var _lastOvers = TouchManager.I.getLastOvers();
  16454. for (var i = 0, n = _lastOvers.length; i < n; i++) {
  16455. var ele = _lastOvers[i];
  16456. ele.event(Event.MOUSE_WHEEL, this._event.setTo(Event.MOUSE_WHEEL, ele, this._target));
  16457. }
  16458. }
  16459. onMouseMove(ele) {
  16460. TouchManager.I.onMouseMove(ele, this._tTouchID);
  16461. }
  16462. onMouseDown(ele) {
  16463. if (Input.isInputting && ILaya.stage.focus && ILaya.stage.focus["focus"] && !ILaya.stage.focus.contains(this._target)) {
  16464. var pre_input = ILaya.stage.focus['_tf'] || ILaya.stage.focus;
  16465. var new_input = ele['_tf'] || ele;
  16466. if (new_input instanceof Input && new_input.multiline == pre_input.multiline)
  16467. pre_input['_focusOut']();
  16468. else
  16469. pre_input.focus = false;
  16470. }
  16471. TouchManager.I.onMouseDown(ele, this._tTouchID, this._isLeftMouse);
  16472. }
  16473. onMouseUp(ele) {
  16474. TouchManager.I.onMouseUp(ele, this._tTouchID, this._isLeftMouse);
  16475. }
  16476. check(sp, mouseX, mouseY, callBack) {
  16477. this._point.setTo(mouseX, mouseY);
  16478. sp.fromParentPoint(this._point);
  16479. mouseX = this._point.x;
  16480. mouseY = this._point.y;
  16481. var scrollRect = sp._style.scrollRect;
  16482. if (scrollRect) {
  16483. this._rect.setTo(scrollRect.x, scrollRect.y, scrollRect.width, scrollRect.height);
  16484. if (!this._rect.contains(mouseX, mouseY))
  16485. return false;
  16486. }
  16487. if (!this.disableMouseEvent) {
  16488. if (sp.hitTestPrior && !sp.mouseThrough && !this.hitTest(sp, mouseX, mouseY)) {
  16489. return false;
  16490. }
  16491. for (var i = sp._children.length - 1; i > -1; i--) {
  16492. var child = sp._children[i];
  16493. if (!child.destroyed && child._mouseState > 1 && child._visible) {
  16494. if (this.check(child, mouseX, mouseY, callBack))
  16495. return true;
  16496. }
  16497. }
  16498. for (i = sp._extUIChild.length - 1; i >= 0; i--) {
  16499. var c = sp._extUIChild[i];
  16500. if (!c.destroyed && c._mouseState > 1 && c._visible) {
  16501. if (this.check(c, mouseX, mouseY, callBack))
  16502. return true;
  16503. }
  16504. }
  16505. }
  16506. var isHit = (sp.hitTestPrior && !sp.mouseThrough && !this.disableMouseEvent) ? true : this.hitTest(sp, mouseX, mouseY);
  16507. if (isHit) {
  16508. this._target = sp;
  16509. callBack.call(this, sp);
  16510. if (this._target == this._hitCaputreSp) {
  16511. this._hitCaputreSp = true;
  16512. }
  16513. }
  16514. else if (callBack === this.onMouseUp && sp === this._stage) {
  16515. this._target = this._stage;
  16516. callBack.call(this, this._target);
  16517. }
  16518. return isHit;
  16519. }
  16520. hitTest(sp, mouseX, mouseY) {
  16521. var isHit = false;
  16522. if (sp.scrollRect) {
  16523. mouseX -= sp._style.scrollRect.x;
  16524. mouseY -= sp._style.scrollRect.y;
  16525. }
  16526. var hitArea = sp._style.hitArea;
  16527. if (hitArea && hitArea._hit) {
  16528. return hitArea.contains(mouseX, mouseY);
  16529. }
  16530. if (sp.width > 0 && sp.height > 0 || sp.mouseThrough || hitArea) {
  16531. if (!sp.mouseThrough) {
  16532. isHit = (hitArea ? hitArea : this._rect.setTo(0, 0, sp.width, sp.height)).contains(mouseX, mouseY);
  16533. }
  16534. else {
  16535. isHit = sp.getGraphicBounds().contains(mouseX, mouseY);
  16536. }
  16537. }
  16538. return isHit;
  16539. }
  16540. _checkAllBaseUI(mousex, mousey, callback) {
  16541. var ret = this.handleExclusiveCapture(this.mouseX, this.mouseY, callback);
  16542. if (ret)
  16543. return true;
  16544. ret = this.check(this._stage, this.mouseX, this.mouseY, callback);
  16545. return this.handleCapture(this.mouseX, this.mouseY, callback) || ret;
  16546. }
  16547. check3DUI(mousex, mousey, callback) {
  16548. var uis = this._stage._3dUI;
  16549. var i = 0;
  16550. var ret = false;
  16551. for (; i < uis.length; i++) {
  16552. var curui = uis[i];
  16553. this._stage._curUIBase = curui;
  16554. if (!curui.destroyed && curui._mouseState > 1 && curui._visible) {
  16555. ret = ret || this.check(curui, this.mouseX, this.mouseY, callback);
  16556. }
  16557. }
  16558. this._stage._curUIBase = this._stage;
  16559. return ret;
  16560. }
  16561. handleExclusiveCapture(mousex, mousey, callback) {
  16562. if (this._captureExlusiveMode && this._captureSp && this._captureChain.length > 0) {
  16563. var cursp;
  16564. this._point.setTo(mousex, mousey);
  16565. for (var i = 0; i < this._captureChain.length; i++) {
  16566. cursp = this._captureChain[i];
  16567. cursp.fromParentPoint(this._point);
  16568. }
  16569. this._target = cursp;
  16570. callback.call(this, cursp);
  16571. return true;
  16572. }
  16573. return false;
  16574. }
  16575. handleCapture(mousex, mousey, callback) {
  16576. if (!this._hitCaputreSp && this._captureSp && this._captureChain.length > 0) {
  16577. var cursp;
  16578. this._point.setTo(mousex, mousey);
  16579. for (var i = 0; i < this._captureChain.length; i++) {
  16580. cursp = this._captureChain[i];
  16581. cursp.fromParentPoint(this._point);
  16582. }
  16583. this._target = cursp;
  16584. callback.call(this, cursp);
  16585. return true;
  16586. }
  16587. return false;
  16588. }
  16589. runEvent(evt) {
  16590. var i, n, touch;
  16591. if (evt.type !== 'mousemove')
  16592. this._prePoint.x = this._prePoint.y = -1000000;
  16593. switch (evt.type) {
  16594. case 'mousedown':
  16595. this._touchIDs[0] = this._id++;
  16596. if (!MouseManager._isTouchRespond) {
  16597. this._isLeftMouse = evt.button === 0;
  16598. this.initEvent(evt);
  16599. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseDown);
  16600. }
  16601. else
  16602. MouseManager._isTouchRespond = false;
  16603. break;
  16604. case 'mouseup':
  16605. this._isLeftMouse = evt.button === 0;
  16606. this.initEvent(evt);
  16607. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseUp);
  16608. break;
  16609. case 'mousemove':
  16610. if ((Math.abs(this._prePoint.x - evt.clientX) + Math.abs(this._prePoint.y - evt.clientY)) >= this.mouseMoveAccuracy) {
  16611. this._prePoint.x = evt.clientX;
  16612. this._prePoint.y = evt.clientY;
  16613. this.initEvent(evt);
  16614. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseMove);
  16615. }
  16616. break;
  16617. case "touchstart":
  16618. MouseManager._isTouchRespond = true;
  16619. this._isLeftMouse = true;
  16620. var touches = evt.changedTouches;
  16621. for (i = 0, n = touches.length; i < n; i++) {
  16622. touch = touches[i];
  16623. if (MouseManager.multiTouchEnabled || isNaN(this._curTouchID)) {
  16624. this._curTouchID = touch.identifier;
  16625. if (this._id % 200 === 0)
  16626. this._touchIDs = {};
  16627. this._touchIDs[touch.identifier] = this._id++;
  16628. this.initEvent(touch, evt);
  16629. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseDown);
  16630. }
  16631. }
  16632. break;
  16633. case "touchend":
  16634. case "touchcancel":
  16635. MouseManager._isTouchRespond = true;
  16636. this._isLeftMouse = true;
  16637. var touchends = evt.changedTouches;
  16638. for (i = 0, n = touchends.length; i < n; i++) {
  16639. touch = touchends[i];
  16640. if (MouseManager.multiTouchEnabled || touch.identifier == this._curTouchID) {
  16641. this._curTouchID = NaN;
  16642. this.initEvent(touch, evt);
  16643. var isChecked;
  16644. isChecked = this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseUp);
  16645. if (!isChecked) {
  16646. this.onMouseUp(null);
  16647. }
  16648. }
  16649. }
  16650. break;
  16651. case "touchmove":
  16652. var touchemoves = evt.changedTouches;
  16653. for (i = 0, n = touchemoves.length; i < n; i++) {
  16654. touch = touchemoves[i];
  16655. if (MouseManager.multiTouchEnabled || touch.identifier == this._curTouchID) {
  16656. this.initEvent(touch, evt);
  16657. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseMove);
  16658. }
  16659. }
  16660. break;
  16661. case "wheel":
  16662. case "mousewheel":
  16663. case "DOMMouseScroll":
  16664. this.checkMouseWheel(evt);
  16665. break;
  16666. case "mouseout":
  16667. TouchManager.I.stageMouseOut();
  16668. break;
  16669. case "mouseover":
  16670. this._stage.event(Event.MOUSE_OVER, this._event.setTo(Event.MOUSE_OVER, this._stage, this._stage));
  16671. break;
  16672. }
  16673. }
  16674. setCapture(sp, exclusive = false) {
  16675. this._captureSp = sp;
  16676. this._captureExlusiveMode = exclusive;
  16677. this._captureChain.length = 0;
  16678. this._captureChain.push(sp);
  16679. var cursp = sp;
  16680. while (true) {
  16681. if (cursp == ILaya.stage)
  16682. break;
  16683. if (cursp == ILaya.stage._curUIBase)
  16684. break;
  16685. cursp = cursp.parent;
  16686. if (!cursp)
  16687. break;
  16688. this._captureChain.splice(0, 0, cursp);
  16689. }
  16690. }
  16691. releaseCapture() {
  16692. console.log('release capture');
  16693. this._captureSp = null;
  16694. }
  16695. }
  16696. MouseManager.instance = new MouseManager();
  16697. MouseManager.enabled = true;
  16698. MouseManager.multiTouchEnabled = true;
  16699. MouseManager._isFirstTouch = true;
  16700. class CallLater {
  16701. constructor() {
  16702. this._pool = [];
  16703. this._map = {};
  16704. this._laters = [];
  16705. }
  16706. _update() {
  16707. let laters = this._laters;
  16708. let len = laters.length;
  16709. if (len > 0) {
  16710. for (let i = 0, n = len - 1; i <= n; i++) {
  16711. let handler = laters[i];
  16712. this._map[handler.key] = null;
  16713. if (handler.method !== null) {
  16714. handler.run();
  16715. handler.clear();
  16716. }
  16717. this._pool.push(handler);
  16718. i === n && (n = laters.length - 1);
  16719. }
  16720. laters.length = 0;
  16721. }
  16722. }
  16723. _getHandler(caller, method) {
  16724. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  16725. var mid = method.$_TID || (method.$_TID = (ILaya.Timer._mid++));
  16726. return this._map[cid + '.' + mid];
  16727. }
  16728. callLater(caller, method, args = null) {
  16729. if (this._getHandler(caller, method) == null) {
  16730. let handler;
  16731. if (this._pool.length)
  16732. handler = this._pool.pop();
  16733. else
  16734. handler = new LaterHandler();
  16735. handler.caller = caller;
  16736. handler.method = method;
  16737. handler.args = args;
  16738. var cid = caller ? caller.$_GID : 0;
  16739. var mid = method["$_TID"];
  16740. handler.key = cid + '.' + mid;
  16741. this._map[handler.key] = handler;
  16742. this._laters.push(handler);
  16743. }
  16744. }
  16745. runCallLater(caller, method) {
  16746. var handler = this._getHandler(caller, method);
  16747. if (handler && handler.method != null) {
  16748. this._map[handler.key] = null;
  16749. handler.run();
  16750. handler.clear();
  16751. }
  16752. }
  16753. }
  16754. CallLater.I = new CallLater();
  16755. class LaterHandler {
  16756. clear() {
  16757. this.caller = null;
  16758. this.method = null;
  16759. this.args = null;
  16760. }
  16761. run() {
  16762. var caller = this.caller;
  16763. if (caller && caller.destroyed)
  16764. return this.clear();
  16765. var method = this.method;
  16766. var args = this.args;
  16767. if (method == null)
  16768. return;
  16769. args ? method.apply(caller, args) : method.call(caller);
  16770. }
  16771. }
  16772. class RunDriver {
  16773. }
  16774. RunDriver.createShaderCondition = function (conditionScript) {
  16775. var fn = "(function() {return " + conditionScript + ";})";
  16776. return window.Laya._runScript(fn);
  16777. };
  16778. RunDriver.changeWebGLSize = function (w, h) {
  16779. WebGL.onStageResize(w, h);
  16780. };
  16781. class Stage extends Sprite {
  16782. constructor() {
  16783. super();
  16784. this.offset = new Point();
  16785. this._frameRate = "fast";
  16786. this.designWidth = 0;
  16787. this.designHeight = 0;
  16788. this.canvasRotation = false;
  16789. this.canvasDegree = 0;
  16790. this.renderingEnabled = true;
  16791. this.screenAdaptationEnabled = true;
  16792. this._canvasTransform = new Matrix();
  16793. this._screenMode = "none";
  16794. this._scaleMode = "noscale";
  16795. this._alignV = "top";
  16796. this._alignH = "left";
  16797. this._bgColor = "black";
  16798. this._mouseMoveTime = 0;
  16799. this._renderCount = 0;
  16800. this._safariOffsetY = 0;
  16801. this._frameStartTime = 0;
  16802. this._previousOrientation = Browser.window.orientation;
  16803. this._wgColor = [0, 0, 0, 1];
  16804. this._scene3Ds = [];
  16805. this._globalRepaintSet = false;
  16806. this._globalRepaintGet = false;
  16807. this._3dUI = [];
  16808. this._curUIBase = null;
  16809. this.useRetinalCanvas = false;
  16810. super.set_transform(this._createTransform());
  16811. this.mouseEnabled = true;
  16812. this.hitTestPrior = true;
  16813. this.autoSize = false;
  16814. this._setBit(Const.DISPLAYED_INSTAGE, true);
  16815. this._setBit(Const.ACTIVE_INHIERARCHY, true);
  16816. this._isFocused = true;
  16817. this._isVisibility = true;
  16818. this.useRetinalCanvas = Config.useRetinalCanvas;
  16819. var window = Browser.window;
  16820. window.addEventListener("focus", () => {
  16821. this._isFocused = true;
  16822. this.event(Event.FOCUS);
  16823. this.event(Event.FOCUS_CHANGE);
  16824. });
  16825. window.addEventListener("blur", () => {
  16826. this._isFocused = false;
  16827. this.event(Event.BLUR);
  16828. this.event(Event.FOCUS_CHANGE);
  16829. if (this._isInputting())
  16830. Input["inputElement"].target.focus = false;
  16831. });
  16832. var state = "visibilityState", visibilityChange = "visibilitychange";
  16833. var document = window.document;
  16834. if (typeof document.hidden !== "undefined") {
  16835. visibilityChange = "visibilitychange";
  16836. state = "visibilityState";
  16837. }
  16838. else if (typeof document.mozHidden !== "undefined") {
  16839. visibilityChange = "mozvisibilitychange";
  16840. state = "mozVisibilityState";
  16841. }
  16842. else if (typeof document.msHidden !== "undefined") {
  16843. visibilityChange = "msvisibilitychange";
  16844. state = "msVisibilityState";
  16845. }
  16846. else if (typeof document.webkitHidden !== "undefined") {
  16847. visibilityChange = "webkitvisibilitychange";
  16848. state = "webkitVisibilityState";
  16849. }
  16850. window.document.addEventListener(visibilityChange, () => {
  16851. if (Browser.document[state] == "hidden") {
  16852. this._isVisibility = false;
  16853. if (this._isInputting())
  16854. Input["inputElement"].target.focus = false;
  16855. }
  16856. else {
  16857. this._isVisibility = true;
  16858. }
  16859. this.renderingEnabled = this._isVisibility;
  16860. this.event(Event.VISIBILITY_CHANGE);
  16861. });
  16862. window.addEventListener("resize", () => {
  16863. var orientation = Browser.window.orientation;
  16864. if (orientation != null && orientation != this._previousOrientation && this._isInputting()) {
  16865. Input["inputElement"].target.focus = false;
  16866. }
  16867. this._previousOrientation = orientation;
  16868. if (this._isInputting())
  16869. return;
  16870. if (Browser.onSafari)
  16871. this._safariOffsetY = (Browser.window.__innerHeight || Browser.document.body.clientHeight || Browser.document.documentElement.clientHeight) - Browser.window.innerHeight;
  16872. this._resetCanvas();
  16873. });
  16874. window.addEventListener("orientationchange", (e) => {
  16875. this._resetCanvas();
  16876. });
  16877. this.on(Event.MOUSE_MOVE, this, this._onmouseMove);
  16878. if (Browser.onMobile)
  16879. this.on(Event.MOUSE_DOWN, this, this._onmouseMove);
  16880. }
  16881. _isInputting() {
  16882. return (Browser.onMobile && Input.isInputting);
  16883. }
  16884. set width(value) {
  16885. this.designWidth = value;
  16886. super.set_width(value);
  16887. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16888. }
  16889. get width() {
  16890. return super.get_width();
  16891. }
  16892. set height(value) {
  16893. this.designHeight = value;
  16894. super.set_height(value);
  16895. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16896. }
  16897. get height() {
  16898. return super.get_height();
  16899. }
  16900. set transform(value) {
  16901. super.set_transform(value);
  16902. }
  16903. get transform() {
  16904. if (this._tfChanged)
  16905. this._adjustTransform();
  16906. return (this._transform = this._transform || this._createTransform());
  16907. }
  16908. get isFocused() {
  16909. return this._isFocused;
  16910. }
  16911. get isVisibility() {
  16912. return this._isVisibility;
  16913. }
  16914. _changeCanvasSize() {
  16915. this.setScreenSize(Browser.clientWidth * Browser.pixelRatio, Browser.clientHeight * Browser.pixelRatio);
  16916. }
  16917. _resetCanvas() {
  16918. if (!this.screenAdaptationEnabled)
  16919. return;
  16920. this._changeCanvasSize();
  16921. }
  16922. setScreenSize(screenWidth, screenHeight) {
  16923. var rotation = false;
  16924. if (this._screenMode !== Stage.SCREEN_NONE) {
  16925. var screenType = screenWidth / screenHeight < 1 ? Stage.SCREEN_VERTICAL : Stage.SCREEN_HORIZONTAL;
  16926. rotation = screenType !== this._screenMode;
  16927. if (rotation) {
  16928. var temp = screenHeight;
  16929. screenHeight = screenWidth;
  16930. screenWidth = temp;
  16931. }
  16932. }
  16933. this.canvasRotation = rotation;
  16934. var canvas = Render._mainCanvas;
  16935. var canvasStyle = canvas.source.style;
  16936. var mat = this._canvasTransform.identity();
  16937. var scaleMode = this._scaleMode;
  16938. var scaleX = screenWidth / this.designWidth;
  16939. var scaleY = screenHeight / this.designHeight;
  16940. var canvasWidth = this.useRetinalCanvas ? screenWidth : this.designWidth;
  16941. var canvasHeight = this.useRetinalCanvas ? screenHeight : this.designHeight;
  16942. var realWidth = screenWidth;
  16943. var realHeight = screenHeight;
  16944. var pixelRatio = Browser.pixelRatio;
  16945. this._width = this.designWidth;
  16946. this._height = this.designHeight;
  16947. switch (scaleMode) {
  16948. case Stage.SCALE_NOSCALE:
  16949. scaleX = scaleY = 1;
  16950. realWidth = this.designWidth;
  16951. realHeight = this.designHeight;
  16952. break;
  16953. case Stage.SCALE_SHOWALL:
  16954. scaleX = scaleY = Math.min(scaleX, scaleY);
  16955. canvasWidth = realWidth = Math.round(this.designWidth * scaleX);
  16956. canvasHeight = realHeight = Math.round(this.designHeight * scaleY);
  16957. break;
  16958. case Stage.SCALE_NOBORDER:
  16959. scaleX = scaleY = Math.max(scaleX, scaleY);
  16960. realWidth = Math.round(this.designWidth * scaleX);
  16961. realHeight = Math.round(this.designHeight * scaleY);
  16962. break;
  16963. case Stage.SCALE_FULL:
  16964. scaleX = scaleY = 1;
  16965. this._width = canvasWidth = screenWidth;
  16966. this._height = canvasHeight = screenHeight;
  16967. break;
  16968. case Stage.SCALE_FIXED_WIDTH:
  16969. scaleY = scaleX;
  16970. this._height = canvasHeight = Math.round(screenHeight / scaleX);
  16971. break;
  16972. case Stage.SCALE_FIXED_HEIGHT:
  16973. scaleX = scaleY;
  16974. this._width = canvasWidth = Math.round(screenWidth / scaleY);
  16975. break;
  16976. case Stage.SCALE_FIXED_AUTO:
  16977. if ((screenWidth / screenHeight) < (this.designWidth / this.designHeight)) {
  16978. scaleY = scaleX;
  16979. this._height = canvasHeight = Math.round(screenHeight / scaleX);
  16980. }
  16981. else {
  16982. scaleX = scaleY;
  16983. this._width = canvasWidth = Math.round(screenWidth / scaleY);
  16984. }
  16985. break;
  16986. }
  16987. if (this.useRetinalCanvas) {
  16988. realWidth = canvasWidth = screenWidth;
  16989. realHeight = canvasHeight = screenHeight;
  16990. }
  16991. scaleX *= this.scaleX;
  16992. scaleY *= this.scaleY;
  16993. if (scaleX === 1 && scaleY === 1) {
  16994. this.transform.identity();
  16995. }
  16996. else {
  16997. this.transform.a = this._formatData(scaleX / (realWidth / canvasWidth));
  16998. this.transform.d = this._formatData(scaleY / (realHeight / canvasHeight));
  16999. }
  17000. canvas.size(canvasWidth, canvasHeight);
  17001. RunDriver.changeWebGLSize(canvasWidth, canvasHeight);
  17002. mat.scale(realWidth / canvasWidth / pixelRatio, realHeight / canvasHeight / pixelRatio);
  17003. if (this._alignH === Stage.ALIGN_LEFT)
  17004. this.offset.x = 0;
  17005. else if (this._alignH === Stage.ALIGN_RIGHT)
  17006. this.offset.x = screenWidth - realWidth;
  17007. else
  17008. this.offset.x = (screenWidth - realWidth) * 0.5 / pixelRatio;
  17009. if (this._alignV === Stage.ALIGN_TOP)
  17010. this.offset.y = 0;
  17011. else if (this._alignV === Stage.ALIGN_BOTTOM)
  17012. this.offset.y = screenHeight - realHeight;
  17013. else
  17014. this.offset.y = (screenHeight - realHeight) * 0.5 / pixelRatio;
  17015. this.offset.x = Math.round(this.offset.x);
  17016. this.offset.y = Math.round(this.offset.y);
  17017. mat.translate(this.offset.x, this.offset.y);
  17018. if (this._safariOffsetY)
  17019. mat.translate(0, this._safariOffsetY);
  17020. this.canvasDegree = 0;
  17021. if (rotation) {
  17022. if (this._screenMode === Stage.SCREEN_HORIZONTAL) {
  17023. mat.rotate(Math.PI / 2);
  17024. mat.translate(screenHeight / pixelRatio, 0);
  17025. this.canvasDegree = 90;
  17026. }
  17027. else {
  17028. mat.rotate(-Math.PI / 2);
  17029. mat.translate(0, screenWidth / pixelRatio);
  17030. this.canvasDegree = -90;
  17031. }
  17032. }
  17033. mat.a = this._formatData(mat.a);
  17034. mat.d = this._formatData(mat.d);
  17035. mat.tx = this._formatData(mat.tx);
  17036. mat.ty = this._formatData(mat.ty);
  17037. super.set_transform(this.transform);
  17038. canvasStyle.transformOrigin = canvasStyle.webkitTransformOrigin = canvasStyle.msTransformOrigin = canvasStyle.mozTransformOrigin = canvasStyle.oTransformOrigin = "0px 0px 0px";
  17039. canvasStyle.transform = canvasStyle.webkitTransform = canvasStyle.msTransform = canvasStyle.mozTransform = canvasStyle.oTransform = "matrix(" + mat.toString() + ")";
  17040. canvasStyle.width = canvasWidth;
  17041. canvasStyle.height = canvasHeight;
  17042. if (this._safariOffsetY)
  17043. mat.translate(0, -this._safariOffsetY);
  17044. mat.translate(parseInt(canvasStyle.left) || 0, parseInt(canvasStyle.top) || 0);
  17045. this.visible = true;
  17046. this._repaint |= SpriteConst.REPAINT_CACHE;
  17047. this.event(Event.RESIZE);
  17048. }
  17049. _formatData(value) {
  17050. if (Math.abs(value) < 0.000001)
  17051. return 0;
  17052. if (Math.abs(1 - value) < 0.001)
  17053. return value > 0 ? 1 : -1;
  17054. return value;
  17055. }
  17056. get scaleMode() {
  17057. return this._scaleMode;
  17058. }
  17059. set scaleMode(value) {
  17060. this._scaleMode = value;
  17061. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  17062. }
  17063. get alignH() {
  17064. return this._alignH;
  17065. }
  17066. set alignH(value) {
  17067. this._alignH = value;
  17068. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  17069. }
  17070. get alignV() {
  17071. return this._alignV;
  17072. }
  17073. set alignV(value) {
  17074. this._alignV = value;
  17075. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  17076. }
  17077. get bgColor() {
  17078. return this._bgColor;
  17079. }
  17080. set bgColor(value) {
  17081. this._bgColor = value;
  17082. if (value)
  17083. this._wgColor = ColorUtils.create(value).arrColor;
  17084. else
  17085. this._wgColor = null;
  17086. if (value) {
  17087. Render.canvas.style.background = value;
  17088. }
  17089. else {
  17090. Render.canvas.style.background = "none";
  17091. }
  17092. }
  17093. get mouseX() {
  17094. return Math.round(MouseManager.instance.mouseX / this.clientScaleX);
  17095. }
  17096. get mouseY() {
  17097. return Math.round(MouseManager.instance.mouseY / this.clientScaleY);
  17098. }
  17099. getMousePoint() {
  17100. return Point.TEMP.setTo(this.mouseX, this.mouseY);
  17101. }
  17102. get clientScaleX() {
  17103. return this._transform ? this._transform.getScaleX() : 1;
  17104. }
  17105. get clientScaleY() {
  17106. return this._transform ? this._transform.getScaleY() : 1;
  17107. }
  17108. get screenMode() {
  17109. return this._screenMode;
  17110. }
  17111. set screenMode(value) {
  17112. this._screenMode = value;
  17113. }
  17114. repaint(type = SpriteConst.REPAINT_CACHE) {
  17115. this._repaint |= type;
  17116. }
  17117. parentRepaint(type = SpriteConst.REPAINT_CACHE) {
  17118. }
  17119. _loop() {
  17120. this._globalRepaintGet = this._globalRepaintSet;
  17121. this._globalRepaintSet = false;
  17122. this.render(Render._context, 0, 0);
  17123. return true;
  17124. }
  17125. getFrameTm() {
  17126. return this._frameStartTime;
  17127. }
  17128. _onmouseMove(e) {
  17129. this._mouseMoveTime = Browser.now();
  17130. }
  17131. getTimeFromFrameStart() {
  17132. return Browser.now() - this._frameStartTime;
  17133. }
  17134. set visible(value) {
  17135. if (this.visible !== value) {
  17136. super.set_visible(value);
  17137. var style = Render._mainCanvas.source.style;
  17138. style.visibility = value ? "visible" : "hidden";
  17139. }
  17140. }
  17141. get visible() {
  17142. return super.visible;
  17143. }
  17144. render(context, x, y) {
  17145. if (window.conch) {
  17146. this.renderToNative(context, x, y);
  17147. return;
  17148. }
  17149. if (this._frameRate === Stage.FRAME_SLEEP) {
  17150. var now = Browser.now();
  17151. if (now - this._frameStartTime >= 1000)
  17152. this._frameStartTime = now;
  17153. else
  17154. return;
  17155. }
  17156. else {
  17157. if (!this._visible) {
  17158. this._renderCount++;
  17159. if (this._renderCount % 5 === 0) {
  17160. CallLater.I._update();
  17161. Stat.loopCount++;
  17162. RenderInfo.loopCount = Stat.loopCount;
  17163. this._updateTimers();
  17164. }
  17165. return;
  17166. }
  17167. this._frameStartTime = Browser.now();
  17168. RenderInfo.loopStTm = this._frameStartTime;
  17169. }
  17170. this._renderCount++;
  17171. var frameMode = this._frameRate === Stage.FRAME_MOUSE ? (((this._frameStartTime - this._mouseMoveTime) < 2000) ? Stage.FRAME_FAST : Stage.FRAME_SLOW) : this._frameRate;
  17172. var isFastMode = (frameMode !== Stage.FRAME_SLOW);
  17173. var isDoubleLoop = (this._renderCount % 2 === 0);
  17174. Stat.renderSlow = !isFastMode;
  17175. if (!isFastMode && !isDoubleLoop)
  17176. return;
  17177. CallLater.I._update();
  17178. Stat.loopCount++;
  17179. RenderInfo.loopCount = Stat.loopCount;
  17180. if (this.renderingEnabled) {
  17181. for (var i = 0, n = this._scene3Ds.length; i < n; i++)
  17182. this._scene3Ds[i]._update();
  17183. context.clear();
  17184. super.render(context, x, y);
  17185. Stat._StatRender.renderNotCanvas(context, x, y);
  17186. }
  17187. if (this.renderingEnabled) {
  17188. Stage.clear(this._bgColor);
  17189. context.flush();
  17190. VectorGraphManager.instance && VectorGraphManager.getInstance().endDispose();
  17191. }
  17192. this._updateTimers();
  17193. }
  17194. renderToNative(context, x, y) {
  17195. this._renderCount++;
  17196. if (!this._visible) {
  17197. if (this._renderCount % 5 === 0) {
  17198. CallLater.I._update();
  17199. Stat.loopCount++;
  17200. RenderInfo.loopCount = Stat.loopCount;
  17201. this._updateTimers();
  17202. }
  17203. return;
  17204. }
  17205. CallLater.I._update();
  17206. Stat.loopCount++;
  17207. RenderInfo.loopCount = Stat.loopCount;
  17208. if (this.renderingEnabled) {
  17209. for (var i = 0, n = this._scene3Ds.length; i < n; i++)
  17210. this._scene3Ds[i]._update();
  17211. context.clear();
  17212. super.render(context, x, y);
  17213. Stat._StatRender.renderNotCanvas(context, x, y);
  17214. }
  17215. if (this.renderingEnabled) {
  17216. Stage.clear(this._bgColor);
  17217. context.flush();
  17218. VectorGraphManager.instance && VectorGraphManager.getInstance().endDispose();
  17219. }
  17220. this._updateTimers();
  17221. }
  17222. _updateTimers() {
  17223. ILaya.systemTimer._update();
  17224. ILaya.startTimer._update();
  17225. ILaya.physicsTimer._update();
  17226. ILaya.updateTimer._update();
  17227. ILaya.lateTimer._update();
  17228. ILaya.timer._update();
  17229. }
  17230. set fullScreenEnabled(value) {
  17231. var document = Browser.document;
  17232. var canvas = Render.canvas;
  17233. if (value) {
  17234. canvas.addEventListener('mousedown', this._requestFullscreen);
  17235. canvas.addEventListener('touchstart', this._requestFullscreen);
  17236. document.addEventListener("fullscreenchange", this._fullScreenChanged);
  17237. document.addEventListener("mozfullscreenchange", this._fullScreenChanged);
  17238. document.addEventListener("webkitfullscreenchange", this._fullScreenChanged);
  17239. document.addEventListener("msfullscreenchange", this._fullScreenChanged);
  17240. }
  17241. else {
  17242. canvas.removeEventListener('mousedown', this._requestFullscreen);
  17243. canvas.removeEventListener('touchstart', this._requestFullscreen);
  17244. document.removeEventListener("fullscreenchange", this._fullScreenChanged);
  17245. document.removeEventListener("mozfullscreenchange", this._fullScreenChanged);
  17246. document.removeEventListener("webkitfullscreenchange", this._fullScreenChanged);
  17247. document.removeEventListener("msfullscreenchange", this._fullScreenChanged);
  17248. }
  17249. }
  17250. get frameRate() {
  17251. if (!ILaya.Render.isConchApp) {
  17252. return this._frameRate;
  17253. }
  17254. else {
  17255. return this._frameRateNative;
  17256. }
  17257. }
  17258. set frameRate(value) {
  17259. if (!ILaya.Render.isConchApp) {
  17260. this._frameRate = value;
  17261. }
  17262. else {
  17263. var c = window.conch;
  17264. switch (value) {
  17265. case Stage.FRAME_FAST:
  17266. c.config.setLimitFPS(60);
  17267. break;
  17268. case Stage.FRAME_MOUSE:
  17269. c.config.setMouseFrame(2000);
  17270. break;
  17271. case Stage.FRAME_SLOW:
  17272. c.config.setSlowFrame(true);
  17273. break;
  17274. case Stage.FRAME_SLEEP:
  17275. c.config.setLimitFPS(1);
  17276. break;
  17277. }
  17278. this._frameRateNative = value;
  17279. }
  17280. }
  17281. _requestFullscreen() {
  17282. var element = Browser.document.documentElement;
  17283. if (element.requestFullscreen) {
  17284. element.requestFullscreen();
  17285. }
  17286. else if (element.mozRequestFullScreen) {
  17287. element.mozRequestFullScreen();
  17288. }
  17289. else if (element.webkitRequestFullscreen) {
  17290. element.webkitRequestFullscreen();
  17291. }
  17292. else if (element.msRequestFullscreen) {
  17293. element.msRequestFullscreen();
  17294. }
  17295. }
  17296. _fullScreenChanged() {
  17297. ILaya.stage.event(Event.FULL_SCREEN_CHANGE);
  17298. }
  17299. exitFullscreen() {
  17300. var document = Browser.document;
  17301. if (document.exitFullscreen) {
  17302. document.exitFullscreen();
  17303. }
  17304. else if (document.mozCancelFullScreen) {
  17305. document.mozCancelFullScreen();
  17306. }
  17307. else if (document.webkitExitFullscreen) {
  17308. document.webkitExitFullscreen();
  17309. }
  17310. }
  17311. isGlobalRepaint() {
  17312. return this._globalRepaintGet;
  17313. }
  17314. setGlobalRepaint() {
  17315. this._globalRepaintSet = true;
  17316. }
  17317. add3DUI(uibase) {
  17318. var uiroot = uibase.rootView;
  17319. if (this._3dUI.indexOf(uiroot) >= 0)
  17320. return;
  17321. this._3dUI.push(uiroot);
  17322. }
  17323. remove3DUI(uibase) {
  17324. var uiroot = uibase.rootView;
  17325. var p = this._3dUI.indexOf(uiroot);
  17326. if (p >= 0) {
  17327. this._3dUI.splice(p, 1);
  17328. return true;
  17329. }
  17330. return false;
  17331. }
  17332. }
  17333. Stage.SCALE_NOSCALE = "noscale";
  17334. Stage.SCALE_EXACTFIT = "exactfit";
  17335. Stage.SCALE_SHOWALL = "showall";
  17336. Stage.SCALE_NOBORDER = "noborder";
  17337. Stage.SCALE_FULL = "full";
  17338. Stage.SCALE_FIXED_WIDTH = "fixedwidth";
  17339. Stage.SCALE_FIXED_HEIGHT = "fixedheight";
  17340. Stage.SCALE_FIXED_AUTO = "fixedauto";
  17341. Stage.ALIGN_LEFT = "left";
  17342. Stage.ALIGN_RIGHT = "right";
  17343. Stage.ALIGN_CENTER = "center";
  17344. Stage.ALIGN_TOP = "top";
  17345. Stage.ALIGN_MIDDLE = "middle";
  17346. Stage.ALIGN_BOTTOM = "bottom";
  17347. Stage.SCREEN_NONE = "none";
  17348. Stage.SCREEN_HORIZONTAL = "horizontal";
  17349. Stage.SCREEN_VERTICAL = "vertical";
  17350. Stage.FRAME_FAST = "fast";
  17351. Stage.FRAME_SLOW = "slow";
  17352. Stage.FRAME_MOUSE = "mouse";
  17353. Stage.FRAME_SLEEP = "sleep";
  17354. Stage.clear = function (value) {
  17355. Context.set2DRenderConfig();
  17356. var gl = LayaGL.instance;
  17357. RenderState2D.worldScissorTest && gl.disable(gl.SCISSOR_TEST);
  17358. var ctx = Render.context;
  17359. var c = (ctx._submits._length == 0 || Config.preserveDrawingBuffer) ? ColorUtils.create(value).arrColor : ILaya.stage._wgColor;
  17360. if (c)
  17361. ctx.clearBG(c[0], c[1], c[2], c[3]);
  17362. else
  17363. ctx.clearBG(0, 0, 0, 0);
  17364. RenderState2D.clear();
  17365. };
  17366. ClassUtils.regClass("laya.display.Stage", Stage);
  17367. ClassUtils.regClass("Laya.Stage", Stage);
  17368. class KeyBoardManager {
  17369. static __init__() {
  17370. KeyBoardManager._addEvent("keydown");
  17371. KeyBoardManager._addEvent("keypress");
  17372. KeyBoardManager._addEvent("keyup");
  17373. }
  17374. static _addEvent(type) {
  17375. ILaya.Browser.document.addEventListener(type, function (e) {
  17376. KeyBoardManager._dispatch(e, type);
  17377. }, true);
  17378. }
  17379. static _dispatch(e, type) {
  17380. if (!KeyBoardManager.enabled)
  17381. return;
  17382. KeyBoardManager._event._stoped = false;
  17383. KeyBoardManager._event.nativeEvent = e;
  17384. KeyBoardManager._event.keyCode = e.keyCode || e.which || e.charCode;
  17385. if (type === "keydown")
  17386. KeyBoardManager._pressKeys[KeyBoardManager._event.keyCode] = true;
  17387. else if (type === "keyup")
  17388. KeyBoardManager._pressKeys[KeyBoardManager._event.keyCode] = null;
  17389. var target = (ILaya.stage.focus && (ILaya.stage.focus.event != null) && ILaya.stage.focus.displayedInStage) ? ILaya.stage.focus : ILaya.stage;
  17390. var ct = target;
  17391. while (ct) {
  17392. ct.event(type, KeyBoardManager._event.setTo(type, ct, target));
  17393. ct = ct.parent;
  17394. }
  17395. }
  17396. static hasKeyDown(key) {
  17397. return KeyBoardManager._pressKeys[key];
  17398. }
  17399. }
  17400. KeyBoardManager._pressKeys = {};
  17401. KeyBoardManager.enabled = true;
  17402. KeyBoardManager._event = new Event();
  17403. class SoundChannel extends EventDispatcher {
  17404. constructor() {
  17405. super(...arguments);
  17406. this.isStopped = false;
  17407. }
  17408. set volume(v) {
  17409. }
  17410. get volume() {
  17411. return 1;
  17412. }
  17413. get position() {
  17414. return 0;
  17415. }
  17416. get duration() {
  17417. return 0;
  17418. }
  17419. play() {
  17420. }
  17421. stop() {
  17422. if (this.completeHandler)
  17423. this.completeHandler.runWith(false);
  17424. }
  17425. pause() {
  17426. }
  17427. resume() {
  17428. }
  17429. __runComplete(handler) {
  17430. if (handler) {
  17431. handler.runWith(true);
  17432. }
  17433. }
  17434. }
  17435. class AudioSoundChannel extends SoundChannel {
  17436. constructor(audio) {
  17437. super();
  17438. this._audio = null;
  17439. this._onEnd = this.__onEnd.bind(this);
  17440. this._resumePlay = this.__resumePlay.bind(this);
  17441. audio.addEventListener("ended", this._onEnd);
  17442. this._audio = audio;
  17443. }
  17444. __onEnd(evt) {
  17445. if (this.loops == 1) {
  17446. if (this.completeHandler) {
  17447. ILaya.systemTimer.once(10, this, this.__runComplete, [this.completeHandler], false);
  17448. this.completeHandler = null;
  17449. }
  17450. this.stop();
  17451. this.event(Event.COMPLETE);
  17452. return;
  17453. }
  17454. if (this.loops > 0) {
  17455. this.loops--;
  17456. }
  17457. this.startTime = 0;
  17458. this.play();
  17459. }
  17460. __resumePlay() {
  17461. if (this._audio)
  17462. this._audio.removeEventListener("canplay", this._resumePlay);
  17463. if (this.isStopped)
  17464. return;
  17465. try {
  17466. this._audio.currentTime = this.startTime;
  17467. Browser.container.appendChild(this._audio);
  17468. this._audio.play();
  17469. }
  17470. catch (e) {
  17471. this.event(Event.ERROR);
  17472. }
  17473. }
  17474. play() {
  17475. this.isStopped = false;
  17476. try {
  17477. this._audio.playbackRate = ILaya.SoundManager.playbackRate;
  17478. this._audio.currentTime = this.startTime;
  17479. }
  17480. catch (e) {
  17481. this._audio.addEventListener("canplay", this._resumePlay);
  17482. return;
  17483. }
  17484. ILaya.SoundManager.addChannel(this);
  17485. Browser.container.appendChild(this._audio);
  17486. if ("play" in this._audio)
  17487. this._audio.play();
  17488. }
  17489. get position() {
  17490. if (!this._audio)
  17491. return 0;
  17492. return this._audio.currentTime;
  17493. }
  17494. get duration() {
  17495. if (!this._audio)
  17496. return 0;
  17497. return this._audio.duration;
  17498. }
  17499. stop() {
  17500. super.stop();
  17501. this.isStopped = true;
  17502. ILaya.SoundManager.removeChannel(this);
  17503. this.completeHandler = null;
  17504. if (!this._audio)
  17505. return;
  17506. if ("pause" in this._audio)
  17507. if (ILaya.Render.isConchApp) {
  17508. this._audio.stop();
  17509. }
  17510. this._audio.pause();
  17511. this._audio.removeEventListener("ended", this._onEnd);
  17512. this._audio.removeEventListener("canplay", this._resumePlay);
  17513. if (!ILaya.Browser.onIE) {
  17514. if (this._audio != ILaya.AudioSound._musicAudio) {
  17515. ILaya.Pool.recover("audio:" + this.url, this._audio);
  17516. }
  17517. }
  17518. Browser.removeElement(this._audio);
  17519. this._audio = null;
  17520. if (ILaya.SoundManager.autoReleaseSound)
  17521. ILaya.SoundManager.disposeSoundLater(this.url);
  17522. }
  17523. pause() {
  17524. this.isStopped = true;
  17525. ILaya.SoundManager.removeChannel(this);
  17526. if ("pause" in this._audio)
  17527. this._audio.pause();
  17528. if (ILaya.SoundManager.autoReleaseSound)
  17529. ILaya.SoundManager.disposeSoundLater(this.url);
  17530. }
  17531. resume() {
  17532. var audio = this._audio;
  17533. if (!audio||!audio.load)
  17534. return;
  17535. this.isStopped = false;
  17536. if (audio.readyState == 0) {
  17537. audio.src = this.url;
  17538. audio.addEventListener("canplay", this._resumePlay);
  17539. audio.load();
  17540. }
  17541. ILaya.SoundManager.addChannel(this);
  17542. if ("play" in audio) {
  17543. audio.play();
  17544. }
  17545. }
  17546. set volume(v) {
  17547. if (!this._audio)
  17548. return;
  17549. this._audio.volume = v;
  17550. }
  17551. get volume() {
  17552. if (!this._audio)
  17553. return 1;
  17554. return this._audio.volume;
  17555. }
  17556. }
  17557. class AudioSound extends EventDispatcher {
  17558. constructor() {
  17559. super(...arguments);
  17560. this.loaded = false;
  17561. }
  17562. dispose() {
  17563. var ad = AudioSound._audioCache[this.url];
  17564. Pool.clearBySign("audio:" + this.url);
  17565. if (ad) {
  17566. if (!Render.isConchApp) {
  17567. ad.src = "";
  17568. }
  17569. delete AudioSound._audioCache[this.url];
  17570. }
  17571. }
  17572. static _initMusicAudio() {
  17573. if (AudioSound._musicAudio)
  17574. return;
  17575. if (!AudioSound._musicAudio)
  17576. AudioSound._musicAudio = Browser.createElement("audio");
  17577. if (!Render.isConchApp) {
  17578. Browser.document.addEventListener("mousedown", AudioSound._makeMusicOK);
  17579. }
  17580. }
  17581. static _makeMusicOK() {
  17582. Browser.document.removeEventListener("mousedown", AudioSound._makeMusicOK);
  17583. if (!AudioSound._musicAudio.src) {
  17584. AudioSound._musicAudio.src = "";
  17585. AudioSound._musicAudio.load();
  17586. }
  17587. else {
  17588. AudioSound._musicAudio.play();
  17589. }
  17590. }
  17591. load(url) {
  17592. url = URL.formatURL(url);
  17593. this.url = url;
  17594. var ad;
  17595. if (url == ILaya.SoundManager._bgMusic) {
  17596. AudioSound._initMusicAudio();
  17597. ad = AudioSound._musicAudio;
  17598. if (ad.src != url) {
  17599. delete AudioSound._audioCache[ad.src];
  17600. ad = null;
  17601. }
  17602. }
  17603. else {
  17604. ad = AudioSound._audioCache[url];
  17605. }
  17606. if (ad && ad.readyState >= 2) {
  17607. this.event(Event.COMPLETE);
  17608. return;
  17609. }
  17610. if (!ad) {
  17611. if (url == ILaya.SoundManager._bgMusic) {
  17612. AudioSound._initMusicAudio();
  17613. ad = AudioSound._musicAudio;
  17614. }
  17615. else {
  17616. ad = Browser.createElement("audio");
  17617. }
  17618. AudioSound._audioCache[url] = ad;
  17619. ad.src = url;
  17620. }
  17621. ad.addEventListener("canplaythrough", onLoaded);
  17622. ad.addEventListener("error", onErr);
  17623. var me = this;
  17624. function onLoaded() {
  17625. offs();
  17626. me.loaded = true;
  17627. me.event(Event.COMPLETE);
  17628. }
  17629. function onErr() {
  17630. ad.load = null;
  17631. offs();
  17632. me.event(Event.ERROR);
  17633. }
  17634. function offs() {
  17635. ad.removeEventListener("canplaythrough", onLoaded);
  17636. ad.removeEventListener("error", onErr);
  17637. }
  17638. this.audio = ad;
  17639. if (ad.load) {
  17640. ad.load();
  17641. }
  17642. else {
  17643. onErr();
  17644. }
  17645. }
  17646. play(startTime = 0, loops = 0) {
  17647. if (!this.url)
  17648. return null;
  17649. var ad;
  17650. if (this.url == ILaya.SoundManager._bgMusic) {
  17651. ad = AudioSound._musicAudio;
  17652. if (ad.src != "" && ad.src != this.url) {
  17653. delete AudioSound._audioCache[ad.src];
  17654. AudioSound._audioCache[this.url] = ad;
  17655. }
  17656. }
  17657. else {
  17658. ad = AudioSound._audioCache[this.url];
  17659. }
  17660. if (!ad)
  17661. return null;
  17662. var tAd;
  17663. tAd = Pool.getItem("audio:" + this.url);
  17664. if (Render.isConchApp) {
  17665. if (!tAd) {
  17666. tAd = Browser.createElement("audio");
  17667. tAd.src = this.url;
  17668. }
  17669. }
  17670. else {
  17671. if (this.url == ILaya.SoundManager._bgMusic) {
  17672. AudioSound._initMusicAudio();
  17673. tAd = AudioSound._musicAudio;
  17674. tAd.src = this.url;
  17675. }
  17676. else {
  17677. tAd = tAd ? tAd : ad.cloneNode(true);
  17678. }
  17679. }
  17680. var channel = new AudioSoundChannel(tAd);
  17681. channel.url = this.url;
  17682. channel.loops = loops;
  17683. channel.startTime = startTime;
  17684. channel.play();
  17685. ILaya.SoundManager.addChannel(channel);
  17686. return channel;
  17687. }
  17688. get duration() {
  17689. var ad;
  17690. ad = AudioSound._audioCache[this.url];
  17691. if (!ad)
  17692. return 0;
  17693. return ad.duration;
  17694. }
  17695. }
  17696. AudioSound._audioCache = {};
  17697. class WebAudioSoundChannel extends SoundChannel {
  17698. constructor() {
  17699. super();
  17700. this.bufferSource = null;
  17701. this._currentTime = 0;
  17702. this._volume = 1;
  17703. this._startTime = 0;
  17704. this._pauseTime = 0;
  17705. this.context = ILaya.WebAudioSound.ctx;
  17706. this._onPlayEnd = Utils.bind(this.__onPlayEnd, this);
  17707. if (this.context["createGain"]) {
  17708. this.gain = this.context["createGain"]();
  17709. }
  17710. else {
  17711. this.gain = this.context["createGainNode"]();
  17712. }
  17713. }
  17714. play() {
  17715. ILaya.SoundManager.addChannel(this);
  17716. this.isStopped = false;
  17717. this._clearBufferSource();
  17718. if (!this.audioBuffer)
  17719. return;
  17720. if (this.startTime >= this.duration)
  17721. return stop();
  17722. var context = this.context;
  17723. var gain = this.gain;
  17724. var bufferSource = context.createBufferSource();
  17725. this.bufferSource = bufferSource;
  17726. bufferSource.buffer = this.audioBuffer;
  17727. bufferSource.connect(gain);
  17728. if (gain)
  17729. gain.disconnect();
  17730. gain.connect(context.destination);
  17731. bufferSource.onended = this._onPlayEnd;
  17732. this._startTime = Browser.now();
  17733. if (this.gain.gain.setTargetAtTime) {
  17734. this.gain.gain.setTargetAtTime(this._volume, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  17735. }
  17736. else
  17737. this.gain.gain.value = this._volume;
  17738. if (this.loops == 0) {
  17739. bufferSource.loop = true;
  17740. }
  17741. if (bufferSource.playbackRate.setTargetAtTime) {
  17742. bufferSource.playbackRate.setTargetAtTime(ILaya.SoundManager.playbackRate, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  17743. }
  17744. else
  17745. bufferSource.playbackRate.value = ILaya.SoundManager.playbackRate;
  17746. bufferSource.start(0, this.startTime);
  17747. this._currentTime = 0;
  17748. }
  17749. __onPlayEnd() {
  17750. if (this.loops == 1) {
  17751. if (this.completeHandler) {
  17752. ILaya.timer.once(10, this, this.__runComplete, [this.completeHandler], false);
  17753. this.completeHandler = null;
  17754. }
  17755. this.stop();
  17756. this.event(Event.COMPLETE);
  17757. return;
  17758. }
  17759. if (this.loops > 0) {
  17760. this.loops--;
  17761. }
  17762. this.startTime = 0;
  17763. this.play();
  17764. }
  17765. get position() {
  17766. if (this.bufferSource) {
  17767. return (Browser.now() - this._startTime) / 1000 + this.startTime;
  17768. }
  17769. return 0;
  17770. }
  17771. get duration() {
  17772. if (this.audioBuffer) {
  17773. return this.audioBuffer.duration;
  17774. }
  17775. return 0;
  17776. }
  17777. _clearBufferSource() {
  17778. if (this.bufferSource) {
  17779. var sourceNode = this.bufferSource;
  17780. if (sourceNode.stop) {
  17781. sourceNode.stop(0);
  17782. }
  17783. else {
  17784. sourceNode.noteOff(0);
  17785. }
  17786. sourceNode.disconnect(0);
  17787. sourceNode.onended = null;
  17788. if (!WebAudioSoundChannel._tryCleanFailed)
  17789. this._tryClearBuffer(sourceNode);
  17790. this.bufferSource = null;
  17791. }
  17792. }
  17793. _tryClearBuffer(sourceNode) {
  17794. try {
  17795. sourceNode.buffer = null;
  17796. }
  17797. catch (e) {
  17798. WebAudioSoundChannel._tryCleanFailed = true;
  17799. }
  17800. }
  17801. stop() {
  17802. super.stop();
  17803. this._clearBufferSource();
  17804. this.audioBuffer = null;
  17805. if (this.gain)
  17806. this.gain.disconnect();
  17807. this.isStopped = true;
  17808. ILaya.SoundManager.removeChannel(this);
  17809. this.completeHandler = null;
  17810. if (ILaya.SoundManager.autoReleaseSound)
  17811. ILaya.SoundManager.disposeSoundLater(this.url);
  17812. }
  17813. pause() {
  17814. if (!this.isStopped) {
  17815. this._pauseTime = this.position;
  17816. }
  17817. this._clearBufferSource();
  17818. if (this.gain)
  17819. this.gain.disconnect();
  17820. this.isStopped = true;
  17821. ILaya.SoundManager.removeChannel(this);
  17822. if (ILaya.SoundManager.autoReleaseSound)
  17823. ILaya.SoundManager.disposeSoundLater(this.url);
  17824. }
  17825. resume() {
  17826. this.startTime = this._pauseTime;
  17827. this.play();
  17828. }
  17829. set volume(v) {
  17830. this._volume = v;
  17831. if (this.isStopped) {
  17832. return;
  17833. }
  17834. if (this.gain.gain.setTargetAtTime) {
  17835. this.gain.gain.setTargetAtTime(v, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  17836. }
  17837. else
  17838. this.gain.gain.value = v;
  17839. }
  17840. get volume() {
  17841. return this._volume;
  17842. }
  17843. }
  17844. WebAudioSoundChannel._tryCleanFailed = false;
  17845. WebAudioSoundChannel.SetTargetDelay = 0.001;
  17846. class WebAudioSound extends EventDispatcher {
  17847. constructor() {
  17848. super(...arguments);
  17849. this.loaded = false;
  17850. this._disposed = false;
  17851. }
  17852. static decode() {
  17853. if (WebAudioSound.buffs.length <= 0 || WebAudioSound.isDecoding) {
  17854. return;
  17855. }
  17856. WebAudioSound.isDecoding = true;
  17857. WebAudioSound.tInfo = WebAudioSound.buffs.shift();
  17858. WebAudioSound.ctx.decodeAudioData(WebAudioSound.tInfo["buffer"], WebAudioSound._done, WebAudioSound._fail);
  17859. }
  17860. static _done(audioBuffer) {
  17861. WebAudioSound.e.event("loaded:" + WebAudioSound.tInfo.url, audioBuffer);
  17862. WebAudioSound.isDecoding = false;
  17863. WebAudioSound.decode();
  17864. }
  17865. static _fail() {
  17866. WebAudioSound.e.event("err:" + WebAudioSound.tInfo.url, null);
  17867. WebAudioSound.isDecoding = false;
  17868. WebAudioSound.decode();
  17869. }
  17870. static _playEmptySound() {
  17871. if (WebAudioSound.ctx == null) {
  17872. return;
  17873. }
  17874. var source = WebAudioSound.ctx.createBufferSource();
  17875. source.buffer = WebAudioSound._miniBuffer;
  17876. source.connect(WebAudioSound.ctx.destination);
  17877. source.start(0, 0, 0);
  17878. }
  17879. static _unlock() {
  17880. if (WebAudioSound._unlocked) {
  17881. return;
  17882. }
  17883. WebAudioSound._playEmptySound();
  17884. if (WebAudioSound.ctx.state == "running") {
  17885. window.document.removeEventListener("mousedown", WebAudioSound._unlock, true);
  17886. window.document.removeEventListener("touchend", WebAudioSound._unlock, true);
  17887. window.document.removeEventListener("touchstart", WebAudioSound._unlock, true);
  17888. WebAudioSound._unlocked = true;
  17889. }
  17890. }
  17891. static initWebAudio() {
  17892. if (WebAudioSound.ctx.state != "running") {
  17893. WebAudioSound._unlock();
  17894. window.document.addEventListener("mousedown", WebAudioSound._unlock, true);
  17895. window.document.addEventListener("touchend", WebAudioSound._unlock, true);
  17896. window.document.addEventListener("touchstart", WebAudioSound._unlock, true);
  17897. }
  17898. }
  17899. load(url) {
  17900. var me = this;
  17901. url = URL.formatURL(url);
  17902. this.url = url;
  17903. this.audioBuffer = WebAudioSound._dataCache[url];
  17904. if (this.audioBuffer) {
  17905. this._loaded(this.audioBuffer);
  17906. return;
  17907. }
  17908. WebAudioSound.e.on("loaded:" + url, this, this._loaded);
  17909. WebAudioSound.e.on("err:" + url, this, this._err);
  17910. if (WebAudioSound.__loadingSound[url]) {
  17911. return;
  17912. }
  17913. WebAudioSound.__loadingSound[url] = true;
  17914. var request = new XMLHttpRequest();
  17915. request.open("GET", url, true);
  17916. request.responseType = "arraybuffer";
  17917. request.onload = function () {
  17918. if (me._disposed) {
  17919. me._removeLoadEvents();
  17920. return;
  17921. }
  17922. me.data = request.response;
  17923. WebAudioSound.buffs.push({ "buffer": me.data, "url": me.url });
  17924. WebAudioSound.decode();
  17925. };
  17926. request.onerror = function (e) {
  17927. me._err();
  17928. };
  17929. request.send();
  17930. }
  17931. _err() {
  17932. this._removeLoadEvents();
  17933. WebAudioSound.__loadingSound[this.url] = false;
  17934. this.event(Event.ERROR);
  17935. }
  17936. _loaded(audioBuffer) {
  17937. this._removeLoadEvents();
  17938. if (this._disposed) {
  17939. return;
  17940. }
  17941. this.audioBuffer = audioBuffer;
  17942. WebAudioSound._dataCache[this.url] = this.audioBuffer;
  17943. this.loaded = true;
  17944. this.event(Event.COMPLETE);
  17945. }
  17946. _removeLoadEvents() {
  17947. WebAudioSound.e.off("loaded:" + this.url, this, this._loaded);
  17948. WebAudioSound.e.off("err:" + this.url, this, this._err);
  17949. }
  17950. __playAfterLoaded() {
  17951. if (!this.__toPlays)
  17952. return;
  17953. var i, len;
  17954. var toPlays;
  17955. toPlays = this.__toPlays;
  17956. len = toPlays.length;
  17957. var tParams;
  17958. for (i = 0; i < len; i++) {
  17959. tParams = toPlays[i];
  17960. if (tParams[2] && !tParams[2].isStopped) {
  17961. this.play(tParams[0], tParams[1], tParams[2]);
  17962. }
  17963. }
  17964. this.__toPlays.length = 0;
  17965. }
  17966. play(startTime = 0, loops = 0, channel = null) {
  17967. channel = channel ? channel : new WebAudioSoundChannel();
  17968. if (!this.audioBuffer) {
  17969. if (this.url) {
  17970. if (!this.__toPlays)
  17971. this.__toPlays = [];
  17972. this.__toPlays.push([startTime, loops, channel]);
  17973. this.once(Event.COMPLETE, this, this.__playAfterLoaded);
  17974. this.load(this.url);
  17975. }
  17976. }
  17977. channel.url = this.url;
  17978. channel.loops = loops;
  17979. channel.audioBuffer = this.audioBuffer;
  17980. channel.startTime = startTime;
  17981. channel.play();
  17982. ILaya.SoundManager.addChannel(channel);
  17983. return channel;
  17984. }
  17985. get duration() {
  17986. if (this.audioBuffer) {
  17987. return this.audioBuffer.duration;
  17988. }
  17989. return 0;
  17990. }
  17991. dispose() {
  17992. this._disposed = true;
  17993. delete WebAudioSound._dataCache[this.url];
  17994. delete WebAudioSound.__loadingSound[this.url];
  17995. this.audioBuffer = null;
  17996. this.data = null;
  17997. this.__toPlays = [];
  17998. }
  17999. }
  18000. WebAudioSound._dataCache = {};
  18001. WebAudioSound.webAudioEnabled = window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"];
  18002. WebAudioSound.ctx = WebAudioSound.webAudioEnabled ? new (window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"])() : undefined;
  18003. WebAudioSound.buffs = [];
  18004. WebAudioSound.isDecoding = false;
  18005. WebAudioSound._miniBuffer = WebAudioSound.ctx ? WebAudioSound.ctx.createBuffer(1, 1, 22050) : undefined;
  18006. WebAudioSound.e = new EventDispatcher();
  18007. WebAudioSound._unlocked = false;
  18008. WebAudioSound.__loadingSound = {};
  18009. class SoundManager {
  18010. static __init__() {
  18011. var win = ILaya.Browser.window;
  18012. var supportWebAudio = win["AudioContext"] || win["webkitAudioContext"] || win["mozAudioContext"] ? true : false;
  18013. if (supportWebAudio)
  18014. WebAudioSound.initWebAudio();
  18015. SoundManager._soundClass = supportWebAudio ? WebAudioSound : AudioSound;
  18016. AudioSound._initMusicAudio();
  18017. SoundManager._musicClass = AudioSound;
  18018. return supportWebAudio;
  18019. }
  18020. static addChannel(channel) {
  18021. if (SoundManager._channels.indexOf(channel) >= 0)
  18022. return;
  18023. SoundManager._channels.push(channel);
  18024. }
  18025. static removeChannel(channel) {
  18026. var i;
  18027. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18028. if (SoundManager._channels[i] == channel) {
  18029. SoundManager._channels.splice(i, 1);
  18030. }
  18031. }
  18032. }
  18033. static disposeSoundLater(url) {
  18034. SoundManager._lastSoundUsedTimeDic[url] = ILaya.Browser.now();
  18035. if (!SoundManager._isCheckingDispose) {
  18036. SoundManager._isCheckingDispose = true;
  18037. ILaya.timer.loop(5000, null, SoundManager._checkDisposeSound);
  18038. }
  18039. }
  18040. static _checkDisposeSound() {
  18041. var key;
  18042. var tTime = ILaya.Browser.now();
  18043. var hasCheck = false;
  18044. for (key in SoundManager._lastSoundUsedTimeDic) {
  18045. if (tTime - SoundManager._lastSoundUsedTimeDic[key] > 30000) {
  18046. delete SoundManager._lastSoundUsedTimeDic[key];
  18047. SoundManager.disposeSoundIfNotUsed(key);
  18048. }
  18049. else {
  18050. hasCheck = true;
  18051. }
  18052. }
  18053. if (!hasCheck) {
  18054. SoundManager._isCheckingDispose = false;
  18055. ILaya.timer.clear(null, SoundManager._checkDisposeSound);
  18056. }
  18057. }
  18058. static disposeSoundIfNotUsed(url) {
  18059. var i;
  18060. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18061. if (SoundManager._channels[i].url == url) {
  18062. return;
  18063. }
  18064. }
  18065. SoundManager.destroySound(url);
  18066. }
  18067. static set autoStopMusic(v) {
  18068. ILaya.stage.off(Event.BLUR, null, SoundManager._stageOnBlur);
  18069. ILaya.stage.off(Event.FOCUS, null, SoundManager._stageOnFocus);
  18070. ILaya.stage.off(Event.VISIBILITY_CHANGE, null, SoundManager._visibilityChange);
  18071. SoundManager._autoStopMusic = v;
  18072. if (v) {
  18073. ILaya.stage.on(Event.BLUR, null, SoundManager._stageOnBlur);
  18074. ILaya.stage.on(Event.FOCUS, null, SoundManager._stageOnFocus);
  18075. ILaya.stage.on(Event.VISIBILITY_CHANGE, null, SoundManager._visibilityChange);
  18076. }
  18077. }
  18078. static get autoStopMusic() {
  18079. return SoundManager._autoStopMusic;
  18080. }
  18081. static _visibilityChange() {
  18082. if (ILaya.stage.isVisibility) {
  18083. SoundManager._stageOnFocus();
  18084. }
  18085. else {
  18086. SoundManager._stageOnBlur();
  18087. }
  18088. }
  18089. static _stageOnBlur() {
  18090. SoundManager._isActive = false;
  18091. if (SoundManager._musicChannel) {
  18092. if (!SoundManager._musicChannel.isStopped) {
  18093. SoundManager._blurPaused = true;
  18094. SoundManager._musicChannel.pause();
  18095. }
  18096. }
  18097. SoundManager.stopAllSound();
  18098. ILaya.stage.once(Event.MOUSE_DOWN, null, SoundManager._stageOnFocus);
  18099. }
  18100. static _recoverWebAudio() {
  18101. if (WebAudioSound.ctx && WebAudioSound.ctx.state != "running" && WebAudioSound.ctx.resume)
  18102. WebAudioSound.ctx.resume();
  18103. }
  18104. static _stageOnFocus() {
  18105. SoundManager._isActive = true;
  18106. SoundManager._recoverWebAudio();
  18107. ILaya.stage.off(Event.MOUSE_DOWN, null, SoundManager._stageOnFocus);
  18108. if (SoundManager._blurPaused) {
  18109. if (SoundManager._musicChannel && SoundManager._musicChannel.isStopped) {
  18110. SoundManager._blurPaused = false;
  18111. SoundManager._musicChannel.resume();
  18112. }
  18113. }
  18114. }
  18115. static set muted(value) {
  18116. if (value == SoundManager._muted)
  18117. return;
  18118. if (value) {
  18119. SoundManager.stopAllSound();
  18120. }
  18121. SoundManager.musicMuted = value;
  18122. SoundManager._muted = value;
  18123. }
  18124. static get muted() {
  18125. return SoundManager._muted;
  18126. }
  18127. static set soundMuted(value) {
  18128. SoundManager._soundMuted = value;
  18129. }
  18130. static get soundMuted() {
  18131. return SoundManager._soundMuted;
  18132. }
  18133. static set musicMuted(value) {
  18134. if (value == SoundManager._musicMuted)
  18135. return;
  18136. if (value) {
  18137. if (SoundManager._bgMusic) {
  18138. if (SoundManager._musicChannel && !SoundManager._musicChannel.isStopped) {
  18139. if (ILaya.Render.isConchApp) {
  18140. if (SoundManager._musicChannel._audio)
  18141. SoundManager._musicChannel._audio.muted = true;
  18142. }
  18143. else {
  18144. SoundManager._musicChannel.pause();
  18145. }
  18146. }
  18147. else {
  18148. SoundManager._musicChannel = null;
  18149. }
  18150. }
  18151. else {
  18152. SoundManager._musicChannel = null;
  18153. }
  18154. SoundManager._musicMuted = value;
  18155. }
  18156. else {
  18157. SoundManager._musicMuted = value;
  18158. if (SoundManager._bgMusic) {
  18159. if (SoundManager._musicChannel) {
  18160. if (ILaya.Render.isConchApp) {
  18161. if (SoundManager._musicChannel._audio)
  18162. SoundManager._musicChannel._audio.muted = false;
  18163. }
  18164. else {
  18165. SoundManager._musicChannel.resume();
  18166. }
  18167. }
  18168. }
  18169. }
  18170. }
  18171. static get musicMuted() {
  18172. return SoundManager._musicMuted;
  18173. }
  18174. static get useAudioMusic() {
  18175. return SoundManager._useAudioMusic;
  18176. }
  18177. static set useAudioMusic(value) {
  18178. SoundManager._useAudioMusic = value;
  18179. if (value) {
  18180. SoundManager._musicClass = AudioSound;
  18181. }
  18182. else {
  18183. SoundManager._musicClass = null;
  18184. }
  18185. }
  18186. static playSound(url, loops = 1, complete = null, soundClass = null, startTime = 0) {
  18187. if (!SoundManager._isActive || !url)
  18188. return null;
  18189. if (SoundManager._muted)
  18190. return null;
  18191. SoundManager._recoverWebAudio();
  18192. url = URL.formatURL(url);
  18193. if (url == SoundManager._bgMusic) {
  18194. if (SoundManager._musicMuted)
  18195. return null;
  18196. }
  18197. else {
  18198. if (ILaya.Render.isConchApp) {
  18199. var ext = Utils.getFileExtension(url);
  18200. if (ext != "wav" && ext != "ogg") {
  18201. alert("The sound only supports wav or ogg format,for optimal performance reason,please refer to the official website document.");
  18202. return null;
  18203. }
  18204. }
  18205. if (SoundManager._soundMuted)
  18206. return null;
  18207. }
  18208. var tSound;
  18209. if (!ILaya.Browser.onBDMiniGame && !ILaya.Browser.onMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame && !ILaya.Browser.onBLMiniGame && !ILaya.Browser.onTTMiniGame && !ILaya.Browser.onHWMiniGame && !ILaya.Browser.onTBMiniGame) {
  18210. tSound = ILaya.loader.getRes(url);
  18211. }
  18212. if (!soundClass)
  18213. soundClass = SoundManager._soundClass;
  18214. if (!tSound) {
  18215. tSound = new soundClass();
  18216. tSound.load(url);
  18217. if (!ILaya.Browser.onBDMiniGame && !ILaya.Browser.onMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame && !ILaya.Browser.onBLMiniGame && !ILaya.Browser.onTTMiniGame && !ILaya.Browser.onHWMiniGame && !ILaya.Browser.onTBMiniGame) {
  18218. ILaya.Loader.cacheRes(url, tSound);
  18219. }
  18220. }
  18221. var channel;
  18222. channel = tSound.play(startTime, loops);
  18223. if (!channel)
  18224. return null;
  18225. channel.url = url;
  18226. channel.volume = (url == SoundManager._bgMusic) ? SoundManager.musicVolume : SoundManager.soundVolume;
  18227. channel.completeHandler = complete;
  18228. return channel;
  18229. }
  18230. static destroySound(url) {
  18231. var tSound = ILaya.loader.getRes(url);
  18232. if (tSound) {
  18233. ILaya.Loader.clearRes(url);
  18234. tSound.dispose();
  18235. }
  18236. }
  18237. static playMusic(url, loops = 0, complete = null, startTime = 0) {
  18238. url = URL.formatURL(url);
  18239. SoundManager._bgMusic = url;
  18240. if (SoundManager._musicChannel)
  18241. SoundManager._musicChannel.stop();
  18242. return SoundManager._musicChannel = SoundManager.playSound(url, loops, complete, SoundManager._musicClass, startTime);
  18243. }
  18244. static stopSound(url) {
  18245. url = URL.formatURL(url);
  18246. var i;
  18247. var channel;
  18248. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18249. channel = SoundManager._channels[i];
  18250. if (channel.url == url) {
  18251. channel.stop();
  18252. }
  18253. }
  18254. }
  18255. static stopAll() {
  18256. SoundManager._bgMusic = null;
  18257. var i;
  18258. var channel;
  18259. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18260. channel = SoundManager._channels[i];
  18261. channel.stop();
  18262. }
  18263. }
  18264. static stopAllSound() {
  18265. var i;
  18266. var channel;
  18267. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18268. channel = SoundManager._channels[i];
  18269. if (channel.url != SoundManager._bgMusic) {
  18270. channel.stop();
  18271. }
  18272. }
  18273. }
  18274. static stopMusic() {
  18275. if (SoundManager._musicChannel)
  18276. SoundManager._musicChannel.stop();
  18277. SoundManager._bgMusic = null;
  18278. }
  18279. static setSoundVolume(volume, url = null) {
  18280. if (url) {
  18281. url = URL.formatURL(url);
  18282. SoundManager._setVolume(url, volume);
  18283. }
  18284. else {
  18285. SoundManager.soundVolume = volume;
  18286. var i;
  18287. var channel;
  18288. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18289. channel = SoundManager._channels[i];
  18290. if (channel.url != SoundManager._bgMusic) {
  18291. channel.volume = volume;
  18292. }
  18293. }
  18294. }
  18295. }
  18296. static setMusicVolume(volume) {
  18297. SoundManager.musicVolume = volume;
  18298. SoundManager._setVolume(SoundManager._bgMusic, volume);
  18299. }
  18300. static _setVolume(url, volume) {
  18301. url = URL.formatURL(url);
  18302. var i;
  18303. var channel;
  18304. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  18305. channel = SoundManager._channels[i];
  18306. if (channel.url == url) {
  18307. channel.volume = volume;
  18308. }
  18309. }
  18310. }
  18311. }
  18312. SoundManager.musicVolume = 1;
  18313. SoundManager.soundVolume = 1;
  18314. SoundManager.playbackRate = 1;
  18315. SoundManager._useAudioMusic = true;
  18316. SoundManager._muted = false;
  18317. SoundManager._soundMuted = false;
  18318. SoundManager._musicMuted = false;
  18319. SoundManager._bgMusic = null;
  18320. SoundManager._musicChannel = null;
  18321. SoundManager._channels = [];
  18322. SoundManager._blurPaused = false;
  18323. SoundManager._isActive = true;
  18324. SoundManager._lastSoundUsedTimeDic = {};
  18325. SoundManager._isCheckingDispose = false;
  18326. SoundManager.autoReleaseSound = true;
  18327. class Prefab {
  18328. create() {
  18329. if (this.json)
  18330. return ILaya.SceneUtils.createByData(null, this.json);
  18331. return null;
  18332. }
  18333. }
  18334. class BitmapFont {
  18335. constructor() {
  18336. this._fontCharDic = {};
  18337. this._fontWidthMap = {};
  18338. this._maxWidth = 0;
  18339. this._spaceWidth = 10;
  18340. this.fontSize = 12;
  18341. this.autoScaleSize = false;
  18342. this.letterSpacing = 0;
  18343. }
  18344. loadFont(path, complete) {
  18345. this._path = path;
  18346. this._complete = complete;
  18347. if (!path || path.indexOf(".fnt") === -1) {
  18348. console.error('Bitmap font configuration information must be a ".fnt" file');
  18349. return;
  18350. }
  18351. ILaya.loader.load([{ url: path, type: ILaya.Loader.XML }, { url: path.replace(".fnt", ".png"), type: ILaya.Loader.IMAGE }], Handler.create(this, this._onLoaded));
  18352. }
  18353. _onLoaded() {
  18354. this.parseFont(ILaya.Loader.getRes(this._path), ILaya.Loader.getRes(this._path.replace(".fnt", ".png")));
  18355. this._complete && this._complete.run();
  18356. }
  18357. parseFont(xml, texture) {
  18358. if (xml == null || texture == null)
  18359. return;
  18360. this._texture = texture;
  18361. var tScale = 1;
  18362. var tInfo = xml.getElementsByTagName("info");
  18363. if (!tInfo[0].getAttributeNode) {
  18364. return this.parseFont2(xml, texture);
  18365. }
  18366. this.fontSize = parseInt(tInfo[0].getAttributeNode("size").nodeValue);
  18367. var tPadding = tInfo[0].getAttributeNode("padding").nodeValue;
  18368. var tPaddingArray = tPadding.split(",");
  18369. this._padding = [parseInt(tPaddingArray[0]), parseInt(tPaddingArray[1]), parseInt(tPaddingArray[2]), parseInt(tPaddingArray[3])];
  18370. var chars = xml.getElementsByTagName("char");
  18371. var i = 0;
  18372. for (i = 0; i < chars.length; i++) {
  18373. var tAttribute = chars[i];
  18374. var tId = parseInt(tAttribute.getAttributeNode("id").nodeValue);
  18375. var xOffset = parseInt(tAttribute.getAttributeNode("xoffset").nodeValue) / tScale;
  18376. var yOffset = parseInt(tAttribute.getAttributeNode("yoffset").nodeValue) / tScale;
  18377. var xAdvance = parseInt(tAttribute.getAttributeNode("xadvance").nodeValue) / tScale;
  18378. var region = new Rectangle();
  18379. region.x = parseInt(tAttribute.getAttributeNode("x").nodeValue);
  18380. region.y = parseInt(tAttribute.getAttributeNode("y").nodeValue);
  18381. region.width = parseInt(tAttribute.getAttributeNode("width").nodeValue);
  18382. region.height = parseInt(tAttribute.getAttributeNode("height").nodeValue);
  18383. var tTexture = Texture.create(texture, region.x, region.y, region.width, region.height, xOffset, yOffset);
  18384. this._maxWidth = Math.max(this._maxWidth, xAdvance + this.letterSpacing);
  18385. this._fontCharDic[tId] = tTexture;
  18386. this._fontWidthMap[tId] = xAdvance;
  18387. }
  18388. }
  18389. parseFont2(xml, texture) {
  18390. if (xml == null || texture == null)
  18391. return;
  18392. this._texture = texture;
  18393. var tScale = 1;
  18394. var tInfo = xml.getElementsByTagName("info");
  18395. this.fontSize = parseInt(tInfo[0].attributes["size"].nodeValue);
  18396. var tPadding = tInfo[0].attributes["padding"].nodeValue;
  18397. var tPaddingArray = tPadding.split(",");
  18398. this._padding = [parseInt(tPaddingArray[0]), parseInt(tPaddingArray[1]), parseInt(tPaddingArray[2]), parseInt(tPaddingArray[3])];
  18399. var chars = xml.getElementsByTagName("char");
  18400. var i = 0;
  18401. for (i = 0; i < chars.length; i++) {
  18402. var tAttribute = chars[i].attributes;
  18403. var tId = parseInt(tAttribute["id"].nodeValue);
  18404. var xOffset = parseInt(tAttribute["xoffset"].nodeValue) / tScale;
  18405. var yOffset = parseInt(tAttribute["yoffset"].nodeValue) / tScale;
  18406. var xAdvance = parseInt(tAttribute["xadvance"].nodeValue) / tScale;
  18407. var region = new Rectangle();
  18408. region.x = parseInt(tAttribute["x"].nodeValue);
  18409. region.y = parseInt(tAttribute["y"].nodeValue);
  18410. region.width = parseInt(tAttribute["width"].nodeValue);
  18411. region.height = parseInt(tAttribute["height"].nodeValue);
  18412. var tTexture = Texture.create(texture, region.x, region.y, region.width, region.height, xOffset, yOffset);
  18413. this._maxWidth = Math.max(this._maxWidth, xAdvance + this.letterSpacing);
  18414. this._fontCharDic[tId] = tTexture;
  18415. this._fontWidthMap[tId] = xAdvance;
  18416. }
  18417. }
  18418. getCharTexture(char) {
  18419. return this._fontCharDic[char.charCodeAt(0)];
  18420. }
  18421. destroy() {
  18422. if (this._texture) {
  18423. for (var p in this._fontCharDic) {
  18424. var tTexture = this._fontCharDic[p];
  18425. if (tTexture)
  18426. tTexture.destroy();
  18427. }
  18428. this._texture.destroy();
  18429. this._fontCharDic = null;
  18430. this._fontWidthMap = null;
  18431. this._texture = null;
  18432. this._complete = null;
  18433. this._padding = null;
  18434. }
  18435. }
  18436. setSpaceWidth(spaceWidth) {
  18437. this._spaceWidth = spaceWidth;
  18438. }
  18439. getCharWidth(char) {
  18440. var code = char.charCodeAt(0);
  18441. if (this._fontWidthMap[code])
  18442. return this._fontWidthMap[code] + this.letterSpacing;
  18443. if (char === " ")
  18444. return this._spaceWidth + this.letterSpacing;
  18445. return 0;
  18446. }
  18447. getTextWidth(text) {
  18448. var tWidth = 0;
  18449. for (var i = 0, n = text.length; i < n; i++) {
  18450. tWidth += this.getCharWidth(text.charAt(i));
  18451. }
  18452. return tWidth;
  18453. }
  18454. getMaxWidth() {
  18455. return this._maxWidth;
  18456. }
  18457. getMaxHeight() {
  18458. return this.fontSize;
  18459. }
  18460. _drawText(text, sprite, drawX, drawY, align, width) {
  18461. var tWidth = this.getTextWidth(text);
  18462. var tTexture;
  18463. var dx = 0;
  18464. align === "center" && (dx = (width - tWidth) / 2);
  18465. align === "right" && (dx = (width - tWidth));
  18466. var tx = 0;
  18467. for (var i = 0, n = text.length; i < n; i++) {
  18468. tTexture = this.getCharTexture(text.charAt(i));
  18469. if (tTexture) {
  18470. sprite.graphics.drawImage(tTexture, drawX + tx + dx, drawY);
  18471. tx += this.getCharWidth(text.charAt(i));
  18472. }
  18473. }
  18474. }
  18475. }
  18476. ClassUtils.regClass("laya.display.BitmapFont", BitmapFont);
  18477. ClassUtils.regClass("Laya.BitmapFont", BitmapFont);
  18478. class HttpRequest extends EventDispatcher {
  18479. constructor() {
  18480. super(...arguments);
  18481. this._http = new XMLHttpRequest();
  18482. }
  18483. send(url, data = null, method = "get", responseType = "text", headers = null) {
  18484. this._responseType = responseType;
  18485. this._data = null;
  18486. if (Browser.onVVMiniGame || Browser.onQGMiniGame || Browser.onQQMiniGame || Browser.onAlipayMiniGame || Browser.onBLMiniGame || Browser.onHWMiniGame || Browser.onTTMiniGame || Browser.onTBMiniGame) {
  18487. url = HttpRequest._urlEncode(url);
  18488. }
  18489. this._url = url;
  18490. var _this = this;
  18491. var http = this._http;
  18492. url = URL.getAdptedFilePath(url);
  18493. http.open(method, url, true);
  18494. let isJson = false;
  18495. if (headers) {
  18496. for (var i = 0; i < headers.length; i++) {
  18497. http.setRequestHeader(headers[i++], headers[i]);
  18498. }
  18499. }
  18500. else if (!(window.conch)) {
  18501. if (!data || typeof (data) == 'string')
  18502. http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  18503. else {
  18504. http.setRequestHeader("Content-Type", "application/json");
  18505. if (!(data instanceof ArrayBuffer) && typeof data !== "string") {
  18506. isJson = true;
  18507. }
  18508. }
  18509. }
  18510. let restype = responseType !== "arraybuffer" ? "text" : "arraybuffer";
  18511. http.responseType = restype;
  18512. if (http.dataType) {
  18513. http.dataType = restype;
  18514. }
  18515. http.onerror = function (e) {
  18516. _this._onError(e);
  18517. };
  18518. http.onabort = function (e) {
  18519. _this._onAbort(e);
  18520. };
  18521. http.onprogress = function (e) {
  18522. _this._onProgress(e);
  18523. };
  18524. http.onload = function (e) {
  18525. _this._onLoad(e);
  18526. };
  18527. if (Browser.onBLMiniGame && Browser.onAndroid && !data)
  18528. data = {};
  18529. http.send(isJson ? JSON.stringify(data) : data);
  18530. }
  18531. _onProgress(e) {
  18532. if (e && e.lengthComputable)
  18533. this.event(Event.PROGRESS, e.loaded / e.total);
  18534. }
  18535. _onAbort(e) {
  18536. this.error("Request was aborted by user");
  18537. }
  18538. _onError(e) {
  18539. this.error("Request failed Status:" + this._http.status + " text:" + this._http.statusText);
  18540. }
  18541. _onLoad(e) {
  18542. var http = this._http;
  18543. var status = http.status !== undefined ? http.status : 200;
  18544. if (status === 200 || status === 204 || status === 0) {
  18545. this.complete();
  18546. }
  18547. else {
  18548. this.error("[" + http.status + "]" + http.statusText + ":" + http.responseURL);
  18549. }
  18550. }
  18551. error(message) {
  18552. this.clear();
  18553. console.warn(this.url, message);
  18554. this.event(Event.ERROR, message);
  18555. }
  18556. complete() {
  18557. this.clear();
  18558. var flag = true;
  18559. try {
  18560. if (this._responseType === "json") {
  18561. this._data = JSON.parse(this._http.responseText);
  18562. }
  18563. else if (this._responseType === "xml") {
  18564. this._data = Utils.parseXMLFromString(this._http.responseText);
  18565. }
  18566. else {
  18567. this._data = this._http.response || this._http.responseText;
  18568. }
  18569. }
  18570. catch (e) {
  18571. flag = false;
  18572. this.error(e.message);
  18573. }
  18574. flag && this.event(Event.COMPLETE, this._data instanceof Array ? [this._data] : this._data);
  18575. }
  18576. clear() {
  18577. var http = this._http;
  18578. http.onerror = http.onabort = http.onprogress = http.onload = null;
  18579. }
  18580. get url() {
  18581. return this._url;
  18582. }
  18583. get data() {
  18584. return this._data;
  18585. }
  18586. get http() {
  18587. return this._http;
  18588. }
  18589. }
  18590. HttpRequest._urlEncode = encodeURI;
  18591. class Loader extends EventDispatcher {
  18592. constructor() {
  18593. super(...arguments);
  18594. this._customParse = false;
  18595. }
  18596. static getTypeFromUrl(url) {
  18597. var type = Utils.getFileExtension(url);
  18598. if (type)
  18599. return Loader.typeMap[type];
  18600. console.warn("Not recognize the resources suffix", url);
  18601. return "text";
  18602. }
  18603. load(url, type = null, cache = true, group = null, ignoreCache = false, useWorkerLoader = ILaya.WorkerLoader.enable) {
  18604. if (!url) {
  18605. this.onLoaded(null);
  18606. return;
  18607. }
  18608. Loader.setGroup(url, "666");
  18609. this._url = url;
  18610. if (url.indexOf("data:image") === 0 && !type)
  18611. type = Loader.IMAGE;
  18612. else
  18613. url = URL.formatURL(url);
  18614. this._type = type || (type = Loader.getTypeFromUrl(this._url));
  18615. this._cache = cache;
  18616. this._useWorkerLoader = useWorkerLoader;
  18617. this._data = null;
  18618. if (useWorkerLoader)
  18619. ILaya.WorkerLoader.enableWorkerLoader();
  18620. var cacheRes;
  18621. if (type == Loader.IMAGE)
  18622. cacheRes = Loader.textureMap[url];
  18623. else
  18624. cacheRes = Loader.loadedMap[url];
  18625. if (!ignoreCache && cacheRes) {
  18626. this._data = cacheRes;
  18627. this.event(Event.PROGRESS, 1);
  18628. this.event(Event.COMPLETE, this._data);
  18629. return;
  18630. }
  18631. if (group)
  18632. Loader.setGroup(url, group);
  18633. if (Loader.parserMap[type] != null) {
  18634. this._customParse = true;
  18635. if (Loader.parserMap[type] instanceof Handler)
  18636. Loader.parserMap[type].runWith(this);
  18637. else
  18638. Loader.parserMap[type].call(null, this);
  18639. return;
  18640. }
  18641. this._loadResourceFilter(type, url);
  18642. }
  18643. _loadResourceFilter(type, url) {
  18644. this._loadResource(type, url);
  18645. }
  18646. _loadResource(type, url) {
  18647. switch (type) {
  18648. case Loader.IMAGE:
  18649. case "htmlimage":
  18650. case "nativeimage":
  18651. this._loadImage(url);
  18652. break;
  18653. case Loader.SOUND:
  18654. this._loadSound(url);
  18655. break;
  18656. case Loader.TTF:
  18657. this._loadTTF(url);
  18658. break;
  18659. case Loader.ATLAS:
  18660. case Loader.PREFAB:
  18661. case Loader.PLF:
  18662. this._loadHttpRequestWhat(url, Loader.JSON);
  18663. break;
  18664. case Loader.FONT:
  18665. this._loadHttpRequestWhat(url, Loader.XML);
  18666. break;
  18667. case Loader.PLFB:
  18668. this._loadHttpRequestWhat(url, Loader.BUFFER);
  18669. break;
  18670. default:
  18671. this._loadHttpRequestWhat(url, type);
  18672. }
  18673. }
  18674. _loadHttpRequest(url, contentType, onLoadCaller, onLoad, onProcessCaller, onProcess, onErrorCaller, onError) {
  18675. if (Browser.onVVMiniGame || Browser.onHWMiniGame) {
  18676. this._http = new HttpRequest();
  18677. }
  18678. else {
  18679. if (!this._http)
  18680. this._http = new HttpRequest();
  18681. }
  18682. onProcess && this._http.on(Event.PROGRESS, onProcessCaller, onProcess);
  18683. onLoad && this._http.on(Event.COMPLETE, onLoadCaller, onLoad);
  18684. this._http.on(Event.ERROR, onErrorCaller, onError);
  18685. this._http.send(url, null, "get", contentType);
  18686. }
  18687. _loadHtmlImage(url, onLoadCaller, onLoad, onErrorCaller, onError) {
  18688. var image;
  18689. function clear() {
  18690. var img = image;
  18691. img.onload = null;
  18692. img.onerror = null;
  18693. delete Loader._imgCache[url];
  18694. }
  18695. var onerror = function () {
  18696. clear();
  18697. onError.call(onErrorCaller);
  18698. };
  18699. var onload = function () {
  18700. clear();
  18701. onLoad.call(onLoadCaller, image);
  18702. };
  18703. image = new Browser.window.Image();
  18704. image.crossOrigin = "";
  18705. image.onload = onload;
  18706. image.onerror = onerror;
  18707. image.src = url;
  18708. Loader._imgCache[url] = image;
  18709. }
  18710. _loadHttpRequestWhat(url, contentType) {
  18711. if (Loader.preLoadedMap[url])
  18712. this.onLoaded(Loader.preLoadedMap[url]);
  18713. else
  18714. this._loadHttpRequest(url, contentType, this, this.onLoaded, this, this.onProgress, this, this.onError);
  18715. }
  18716. _loadTTF(url) {
  18717. url = URL.formatURL(url);
  18718. var ttfLoader = new ILaya.TTFLoader();
  18719. ttfLoader.complete = Handler.create(this, this.onLoaded);
  18720. ttfLoader.load(url);
  18721. }
  18722. _loadImage(url, isformatURL = true) {
  18723. var _this = this;
  18724. if (isformatURL)
  18725. url = URL.formatURL(url);
  18726. var onError = function () {
  18727. _this.event(Event.ERROR, "Load image failed");
  18728. };
  18729. if (this._type === "nativeimage") {
  18730. this._loadHtmlImage(url, this, this.onLoaded, this, onError);
  18731. }
  18732. else {
  18733. var ext = Utils.getFileExtension(url);
  18734. if (ext === "ktx" || ext === "pvr")
  18735. this._loadHttpRequest(url, Loader.BUFFER, this, this.onLoaded, this, this.onProgress, this, this.onError);
  18736. else
  18737. this._loadHtmlImage(url, this, this.onLoaded, this, onError);
  18738. }
  18739. }
  18740. _loadSound(url) {
  18741. var sound = (new SoundManager._soundClass());
  18742. var _this = this;
  18743. sound.on(Event.COMPLETE, this, soundOnload);
  18744. sound.on(Event.ERROR, this, soundOnErr);
  18745. sound.load(url);
  18746. function soundOnload() {
  18747. clear();
  18748. _this.onLoaded(sound);
  18749. }
  18750. function soundOnErr() {
  18751. clear();
  18752. sound.dispose();
  18753. _this.event(Event.ERROR, "Load sound failed");
  18754. }
  18755. function clear() {
  18756. sound.offAll();
  18757. }
  18758. }
  18759. onProgress(value) {
  18760. if (this._type === Loader.ATLAS)
  18761. this.event(Event.PROGRESS, value * 0.3);
  18762. else if (this._originType == Loader.HIERARCHY)
  18763. this.event(Event.PROGRESS, value / 3);
  18764. else
  18765. this.event(Event.PROGRESS, value);
  18766. }
  18767. onError(message) {
  18768. this.event(Event.ERROR, message);
  18769. }
  18770. onLoaded(data = null) {
  18771. var type = this._type;
  18772. if (type == Loader.PLFB) {
  18773. this.parsePLFBData(data);
  18774. this.complete(data);
  18775. }
  18776. else if (type == Loader.PLF) {
  18777. this.parsePLFData(data);
  18778. this.complete(data);
  18779. }
  18780. else if (type === Loader.IMAGE) {
  18781. let tex;
  18782. if (data instanceof ArrayBuffer) {
  18783. var ext = Utils.getFileExtension(this._url);
  18784. let format;
  18785. switch (ext) {
  18786. case "ktx":
  18787. format = exports.TextureFormat.ETC1RGB;
  18788. break;
  18789. case "pvr":
  18790. format = exports.TextureFormat.PVRTCRGBA_4BPPV;
  18791. break;
  18792. default: {
  18793. console.error('unknown format', ext);
  18794. return;
  18795. }
  18796. }
  18797. tex = new Texture2D(0, 0, format, false, false);
  18798. tex.wrapModeU = exports.WarpMode.Clamp;
  18799. tex.wrapModeV = exports.WarpMode.Clamp;
  18800. tex.setCompressData(data);
  18801. tex._setCreateURL(this.url);
  18802. }
  18803. else if (!(data instanceof Texture2D)) {
  18804. tex = new Texture2D(data.width, data.height, 1, false, false);
  18805. tex.wrapModeU = exports.WarpMode.Clamp;
  18806. tex.wrapModeV = exports.WarpMode.Clamp;
  18807. tex.loadImageSource(data, true);
  18808. tex._setCreateURL(data.src);
  18809. }
  18810. else {
  18811. tex = data;
  18812. }
  18813. var texture = new Texture(tex);
  18814. texture.url = this._url;
  18815. this.complete(texture);
  18816. }
  18817. else if (type === Loader.SOUND || type === "nativeimage") {
  18818. this.complete(data);
  18819. }
  18820. else if (type === "htmlimage") {
  18821. let tex = new Texture2D(data.width, data.height, 1, false, false);
  18822. tex.wrapModeU = exports.WarpMode.Clamp;
  18823. tex.wrapModeV = exports.WarpMode.Clamp;
  18824. tex.loadImageSource(data, true);
  18825. tex._setCreateURL(data.src);
  18826. this.complete(tex);
  18827. }
  18828. else if (type === Loader.ATLAS) {
  18829. if (data.frames) {
  18830. var toloadPics = [];
  18831. if (!this._data) {
  18832. this._data = data;
  18833. if (data.meta && data.meta.image) {
  18834. toloadPics = data.meta.image.split(",");
  18835. var split = this._url.indexOf("/") >= 0 ? "/" : "\\";
  18836. var idx = this._url.lastIndexOf(split);
  18837. var folderPath = idx >= 0 ? this._url.substr(0, idx + 1) : "";
  18838. var changeType = null;
  18839. if (Browser.onAndroid && data.meta.compressTextureAndroid) {
  18840. changeType = ".ktx";
  18841. }
  18842. if (Browser.onIOS && data.meta.compressTextureIOS) {
  18843. changeType = ".pvr";
  18844. }
  18845. for (var i = 0, len = toloadPics.length; i < len; i++) {
  18846. if (changeType) {
  18847. toloadPics[i] = folderPath + toloadPics[i].replace(".png", changeType);
  18848. }
  18849. else {
  18850. toloadPics[i] = folderPath + toloadPics[i];
  18851. }
  18852. }
  18853. }
  18854. else {
  18855. toloadPics = [this._url.replace(".json", ".png")];
  18856. }
  18857. toloadPics.reverse();
  18858. data.toLoads = toloadPics;
  18859. data.pics = [];
  18860. }
  18861. this.event(Event.PROGRESS, 0.3 + 1 / toloadPics.length * 0.6);
  18862. var url = URL.formatURL(toloadPics.pop());
  18863. var ext = Utils.getFileExtension(url);
  18864. var type = Loader.IMAGE;
  18865. if (ext == "pvr" || ext == "ktx") {
  18866. type = Loader.BUFFER;
  18867. }
  18868. return this._loadResourceFilter(type, url);
  18869. }
  18870. else {
  18871. if (!(data instanceof Texture2D)) {
  18872. if (data instanceof ArrayBuffer) {
  18873. let url = this._http.url;
  18874. var ext = Utils.getFileExtension(url);
  18875. let format;
  18876. switch (ext) {
  18877. case "ktx":
  18878. format = exports.TextureFormat.ETC1RGB;
  18879. break;
  18880. case "pvr":
  18881. format = exports.TextureFormat.PVRTCRGBA_4BPPV;
  18882. break;
  18883. default: {
  18884. console.error('unknown format', ext);
  18885. return;
  18886. }
  18887. }
  18888. let tex = new Texture2D(0, 0, format, false, false);
  18889. tex.wrapModeU = exports.WarpMode.Clamp;
  18890. tex.wrapModeV = exports.WarpMode.Clamp;
  18891. tex.setCompressData(data);
  18892. tex._setCreateURL(url);
  18893. data = tex;
  18894. }
  18895. else {
  18896. let tex = new Texture2D(data.width, data.height, 1, false, false);
  18897. tex.wrapModeU = exports.WarpMode.Clamp;
  18898. tex.wrapModeV = exports.WarpMode.Clamp;
  18899. tex.loadImageSource(data, true);
  18900. tex._setCreateURL(data.src);
  18901. data = tex;
  18902. }
  18903. }
  18904. this._data.pics.push(data);
  18905. if (this._data.toLoads.length > 0) {
  18906. this.event(Event.PROGRESS, 0.3 + 1 / this._data.toLoads.length * 0.6);
  18907. var url = URL.formatURL(this._data.toLoads.pop());
  18908. var ext = Utils.getFileExtension(url);
  18909. var type = Loader.IMAGE;
  18910. if (ext == "pvr" || ext == "ktx") {
  18911. type = Loader.BUFFER;
  18912. }
  18913. return this._loadResourceFilter(type, url);
  18914. }
  18915. var frames = this._data.frames;
  18916. var cleanUrl = this._url.split("?")[0];
  18917. var directory = (this._data.meta && this._data.meta.prefix) ? this._data.meta.prefix : cleanUrl.substring(0, cleanUrl.lastIndexOf(".")) + "/";
  18918. var pics = this._data.pics;
  18919. var atlasURL = URL.formatURL(this._url);
  18920. var map = Loader.atlasMap[atlasURL] || (Loader.atlasMap[atlasURL] = []);
  18921. map.dir = directory;
  18922. var scaleRate = 1;
  18923. if (this._data.meta && this._data.meta.scale && this._data.meta.scale != 1) {
  18924. scaleRate = parseFloat(this._data.meta.scale);
  18925. for (var name in frames) {
  18926. var obj = frames[name];
  18927. var tPic = pics[obj.frame.idx ? obj.frame.idx : 0];
  18928. var url = URL.formatURL(directory + name);
  18929. tPic.scaleRate = scaleRate;
  18930. var tTexture;
  18931. tTexture = Texture._create(tPic, obj.frame.x, obj.frame.y, obj.frame.w, obj.frame.h, obj.spriteSourceSize.x, obj.spriteSourceSize.y, obj.sourceSize.w, obj.sourceSize.h, Loader.getRes(url));
  18932. Loader.cacheTexture(url, tTexture);
  18933. tTexture.url = url;
  18934. map.push(url);
  18935. }
  18936. }
  18937. else {
  18938. for (name in frames) {
  18939. obj = frames[name];
  18940. tPic = pics[obj.frame.idx ? obj.frame.idx : 0];
  18941. url = URL.formatURL(directory + name);
  18942. tTexture = Texture._create(tPic, obj.frame.x, obj.frame.y, obj.frame.w, obj.frame.h, obj.spriteSourceSize.x, obj.spriteSourceSize.y, obj.sourceSize.w, obj.sourceSize.h, Loader.getRes(url));
  18943. Loader.cacheTexture(url, tTexture);
  18944. tTexture.url = url;
  18945. map.push(url);
  18946. }
  18947. }
  18948. delete this._data.pics;
  18949. this.complete(this._data);
  18950. }
  18951. }
  18952. else if (type === Loader.FONT) {
  18953. if (!data._source) {
  18954. this._data = data;
  18955. this.event(Event.PROGRESS, 0.5);
  18956. return this._loadResourceFilter(Loader.IMAGE, this._url.replace(".fnt", ".png"));
  18957. }
  18958. else {
  18959. var bFont = new BitmapFont();
  18960. bFont.parseFont(this._data, new Texture(data));
  18961. var tArr = this._url.split(".fnt")[0].split("/");
  18962. var fontName = tArr[tArr.length - 1];
  18963. Text.registerBitmapFont(fontName, bFont);
  18964. this._data = bFont;
  18965. this.complete(this._data);
  18966. }
  18967. }
  18968. else if (type === Loader.PREFAB) {
  18969. var prefab = new Prefab();
  18970. prefab.json = data;
  18971. this.complete(prefab);
  18972. }
  18973. else {
  18974. this.complete(data);
  18975. }
  18976. }
  18977. parsePLFData(plfData) {
  18978. var type;
  18979. var filePath;
  18980. var fileDic;
  18981. for (type in plfData) {
  18982. fileDic = plfData[type];
  18983. switch (type) {
  18984. case "json":
  18985. case "text":
  18986. for (filePath in fileDic) {
  18987. Loader.preLoadedMap[URL.formatURL(filePath)] = fileDic[filePath];
  18988. }
  18989. break;
  18990. default:
  18991. for (filePath in fileDic) {
  18992. Loader.preLoadedMap[URL.formatURL(filePath)] = fileDic[filePath];
  18993. }
  18994. }
  18995. }
  18996. }
  18997. parsePLFBData(plfData) {
  18998. var byte;
  18999. byte = new Byte(plfData);
  19000. var i, len;
  19001. len = byte.getInt32();
  19002. for (i = 0; i < len; i++) {
  19003. this.parseOnePLFBFile(byte);
  19004. }
  19005. }
  19006. parseOnePLFBFile(byte) {
  19007. var fileLen;
  19008. var fileName;
  19009. var fileData;
  19010. fileName = byte.getUTFString();
  19011. fileLen = byte.getInt32();
  19012. fileData = byte.readArrayBuffer(fileLen);
  19013. Loader.preLoadedMap[URL.formatURL(fileName)] = fileData;
  19014. }
  19015. complete(data) {
  19016. this._data = data;
  19017. if (this._customParse) {
  19018. this.event(Event.LOADED, data instanceof Array ? [data] : data);
  19019. }
  19020. else {
  19021. Loader._loaders.push(this);
  19022. if (!Loader._isWorking)
  19023. Loader.checkNext();
  19024. }
  19025. }
  19026. static checkNext() {
  19027. Loader._isWorking = true;
  19028. var startTimer = Browser.now();
  19029. while (Loader._startIndex < Loader._loaders.length) {
  19030. Loader._loaders[Loader._startIndex].endLoad();
  19031. Loader._startIndex++;
  19032. if (Browser.now() - startTimer > Loader.maxTimeOut) {
  19033. console.warn("loader callback cost a long time:" + (Browser.now() - startTimer) + " url=" + Loader._loaders[Loader._startIndex - 1].url);
  19034. ILaya.systemTimer.frameOnce(1, null, Loader.checkNext);
  19035. return;
  19036. }
  19037. }
  19038. Loader._loaders.length = 0;
  19039. Loader._startIndex = 0;
  19040. Loader._isWorking = false;
  19041. }
  19042. endLoad(content = null) {
  19043. content && (this._data = content);
  19044. if (this._cache)
  19045. Loader.cacheRes(this._url, this._data);
  19046. this.event(Event.PROGRESS, 1);
  19047. this.event(Event.COMPLETE, this.data instanceof Array ? [this.data] : this.data);
  19048. }
  19049. get url() {
  19050. return this._url;
  19051. }
  19052. get type() {
  19053. return this._type;
  19054. }
  19055. get cache() {
  19056. return this._cache;
  19057. }
  19058. get data() {
  19059. return this._data;
  19060. }
  19061. static clearRes(url) {
  19062. url = URL.formatURL(url);
  19063. var arr = Loader.getAtlas(url);
  19064. if (arr) {
  19065. for (var i = 0, n = arr.length; i < n; i++) {
  19066. var resUrl = arr[i];
  19067. var tex = Loader.getRes(resUrl);
  19068. delete Loader.textureMap[resUrl];
  19069. if (tex)
  19070. tex.destroy();
  19071. }
  19072. arr.length = 0;
  19073. delete Loader.atlasMap[url];
  19074. }
  19075. var texture = Loader.textureMap[url];
  19076. if (texture) {
  19077. texture.destroy();
  19078. delete Loader.textureMap[url];
  19079. }
  19080. var res = Loader.loadedMap[url];
  19081. (res) && (delete Loader.loadedMap[url]);
  19082. }
  19083. static clearTextureRes(url) {
  19084. url = URL.formatURL(url);
  19085. var arr = Loader.getAtlas(url);
  19086. if (arr && arr.length > 0) {
  19087. arr.forEach(function (t) {
  19088. var tex = Loader.getRes(t);
  19089. if (tex instanceof Texture) {
  19090. tex.disposeBitmap();
  19091. }
  19092. });
  19093. }
  19094. else {
  19095. var t = Loader.getRes(url);
  19096. if (t instanceof Texture) {
  19097. t.disposeBitmap();
  19098. }
  19099. }
  19100. }
  19101. static getRes(url) {
  19102. var res = Loader.textureMap[URL.formatURL(url)];
  19103. if (res)
  19104. return res;
  19105. else
  19106. return Loader.loadedMap[URL.formatURL(url)];
  19107. }
  19108. static getAtlas(url) {
  19109. return Loader.atlasMap[URL.formatURL(url)];
  19110. }
  19111. static cacheRes(url, data) {
  19112. url = URL.formatURL(url);
  19113. if (Loader.loadedMap[url] != null) {
  19114. console.warn("Resources already exist,is repeated loading:", url);
  19115. }
  19116. else {
  19117. if (data instanceof Texture) {
  19118. Loader.loadedMap[url] = data.bitmap;
  19119. Loader.textureMap[url] = data;
  19120. }
  19121. else {
  19122. Loader.loadedMap[url] = data;
  19123. }
  19124. }
  19125. }
  19126. static cacheResForce(url, data) {
  19127. Loader.loadedMap[url] = data;
  19128. }
  19129. static cacheTexture(url, data) {
  19130. url = URL.formatURL(url);
  19131. if (Loader.textureMap[url] != null) {
  19132. console.warn("Resources already exist,is repeated loading:", url);
  19133. }
  19134. else {
  19135. Loader.textureMap[url] = data;
  19136. }
  19137. }
  19138. static setGroup(url, group) {
  19139. if (!Loader.groupMap[group])
  19140. Loader.groupMap[group] = [];
  19141. Loader.groupMap[group].push(url);
  19142. }
  19143. static clearResByGroup(group) {
  19144. if (!Loader.groupMap[group])
  19145. return;
  19146. var arr = Loader.groupMap[group], i, len = arr.length;
  19147. for (i = 0; i < len; i++) {
  19148. Loader.clearRes(arr[i]);
  19149. }
  19150. arr.length = 0;
  19151. }
  19152. }
  19153. Loader.TEXT = "text";
  19154. Loader.JSON = "json";
  19155. Loader.PREFAB = "prefab";
  19156. Loader.XML = "xml";
  19157. Loader.BUFFER = "arraybuffer";
  19158. Loader.IMAGE = "image";
  19159. Loader.SOUND = "sound";
  19160. Loader.ATLAS = "atlas";
  19161. Loader.FONT = "font";
  19162. Loader.TTF = "ttf";
  19163. Loader.PLF = "plf";
  19164. Loader.PLFB = "plfb";
  19165. Loader.HIERARCHY = "HIERARCHY";
  19166. Loader.MESH = "MESH";
  19167. Loader.MATERIAL = "MATERIAL";
  19168. Loader.TEXTURE2D = "TEXTURE2D";
  19169. Loader.TEXTURECUBE = "TEXTURECUBE";
  19170. Loader.ANIMATIONCLIP = "ANIMATIONCLIP";
  19171. Loader.AVATAR = "AVATAR";
  19172. Loader.TERRAINHEIGHTDATA = "TERRAINHEIGHTDATA";
  19173. Loader.TERRAINRES = "TERRAIN";
  19174. Loader.typeMap = { "ttf": "ttf", "png": "image", "jpg": "image", "jpeg": "image", "ktx": "image", "pvr": "image", "txt": "text", "json": "json", "prefab": "prefab", "xml": "xml", "als": "atlas", "atlas": "atlas", "mp3": "sound", "ogg": "sound", "wav": "sound", "part": "json", "fnt": "font", "plf": "plf", "plfb": "plfb", "scene": "json", "ani": "json", "sk": "arraybuffer", "wasm": "arraybuffer" };
  19175. Loader.parserMap = {};
  19176. Loader.maxTimeOut = 100;
  19177. Loader.groupMap = {};
  19178. Loader.loadedMap = {};
  19179. Loader.atlasMap = {};
  19180. Loader.textureMap = {};
  19181. Loader.preLoadedMap = {};
  19182. Loader._imgCache = {};
  19183. Loader._loaders = [];
  19184. Loader._isWorking = false;
  19185. Loader._startIndex = 0;
  19186. class AtlasInfoManager {
  19187. static enable(infoFile, callback = null) {
  19188. ILaya.loader.load(infoFile, Handler.create(null, AtlasInfoManager._onInfoLoaded, [callback]), null, Loader.JSON);
  19189. }
  19190. static _onInfoLoaded(callback, data) {
  19191. var tKey;
  19192. var tPrefix;
  19193. var tArr;
  19194. var i, len;
  19195. for (tKey in data) {
  19196. tArr = data[tKey];
  19197. tPrefix = tArr[0];
  19198. tArr = tArr[1];
  19199. len = tArr.length;
  19200. for (i = 0; i < len; i++) {
  19201. AtlasInfoManager._fileLoadDic[tPrefix + tArr[i]] = tKey;
  19202. }
  19203. }
  19204. callback && callback.run();
  19205. }
  19206. static getFileLoadPath(file) {
  19207. return AtlasInfoManager._fileLoadDic[file] || file;
  19208. }
  19209. }
  19210. AtlasInfoManager._fileLoadDic = {};
  19211. class LoaderManager extends EventDispatcher {
  19212. constructor() {
  19213. super();
  19214. this.retryNum = 1;
  19215. this.retryDelay = 0;
  19216. this.maxLoader = 5;
  19217. this._loaders = [];
  19218. this._loaderCount = 0;
  19219. this._resInfos = [];
  19220. this._infoPool = [];
  19221. this._maxPriority = 5;
  19222. this._failRes = {};
  19223. this._statInfo = { count: 1, loaded: 1 };
  19224. for (var i = 0; i < this._maxPriority; i++)
  19225. this._resInfos[i] = [];
  19226. }
  19227. getProgress() {
  19228. return this._statInfo.loaded / this._statInfo.count;
  19229. }
  19230. resetProgress() {
  19231. this._statInfo.count = this._statInfo.loaded = 1;
  19232. }
  19233. create(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  19234. this._create(url, true, complete, progress, type, constructParams, propertyParams, priority, cache);
  19235. }
  19236. _create(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  19237. if (url instanceof Array) {
  19238. var allScuess = true;
  19239. var items = url;
  19240. var itemCount = items.length;
  19241. var loadedCount = 0;
  19242. if (progress) {
  19243. var progress2 = Handler.create(progress.caller, progress ? progress.method : null, progress.args, false);
  19244. }
  19245. for (var i = 0; i < itemCount; i++) {
  19246. var item = items[i];
  19247. if (typeof (item) == 'string')
  19248. item = items[i] = { url: item };
  19249. item.progress = 0;
  19250. }
  19251. for (i = 0; i < itemCount; i++) {
  19252. item = items[i];
  19253. var progressHandler = progress ? Handler.create(null, function (item, value) {
  19254. item.progress = value;
  19255. var num = 0;
  19256. for (var j = 0; j < itemCount; j++) {
  19257. var item1 = items[j];
  19258. num += item1.progress;
  19259. }
  19260. var v = num / itemCount;
  19261. progress2.runWith(v);
  19262. }, [item], false) : null;
  19263. var completeHandler = (progress || complete) ? Handler.create(null, function (item, content = null) {
  19264. loadedCount++;
  19265. item.progress = 1;
  19266. content || (allScuess = false);
  19267. if (loadedCount === itemCount && complete) {
  19268. complete.runWith(allScuess);
  19269. }
  19270. }, [item]) : null;
  19271. this._createOne(item.url, mainResou, completeHandler, progressHandler, item.type || type, item.constructParams || constructParams, item.propertyParams || propertyParams, item.priority || priority, cache);
  19272. }
  19273. }
  19274. else {
  19275. this._createOne(url, mainResou, complete, progress, type, constructParams, propertyParams, priority, cache);
  19276. }
  19277. }
  19278. _createOne(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  19279. var item = this.getRes(url);
  19280. if (!item) {
  19281. var extension = (LoaderManager.createMap[Utils.getFilecompatibleExtension(url)]) ? Utils.getFilecompatibleExtension(url) : Utils.getFileExtension(url);
  19282. (type) || (type = LoaderManager.createMap[extension] ? LoaderManager.createMap[extension][0] : null);
  19283. if (!type) {
  19284. this.load(url, complete, progress, type, priority, cache);
  19285. return;
  19286. }
  19287. var parserMap = Loader.parserMap;
  19288. if (!parserMap[type]) {
  19289. this.load(url, complete, progress, type, priority, cache);
  19290. return;
  19291. }
  19292. this._createLoad(url, Handler.create(null, function (createRes) {
  19293. if (createRes) {
  19294. if (!mainResou && createRes instanceof Resource)
  19295. createRes._addReference();
  19296. createRes._setCreateURL(url);
  19297. }
  19298. complete && complete.runWith(createRes);
  19299. ILaya.loader.event(url);
  19300. }), progress, type, constructParams, propertyParams, priority, cache, true);
  19301. }
  19302. else {
  19303. if (!mainResou && item instanceof Resource)
  19304. item._addReference();
  19305. progress && progress.runWith(1);
  19306. complete && complete.runWith(item);
  19307. }
  19308. }
  19309. load(url, complete = null, progress = null, type = null, priority = 1, cache = true, group = null, ignoreCache = false, useWorkerLoader = ILaya.WorkerLoader.enable) {
  19310. if (url instanceof Array) {
  19311. return this._loadAssets(url, complete, progress, type, priority, cache, group);
  19312. }
  19313. if (!type) {
  19314. if (url.indexOf("data:image") === 0)
  19315. type = Loader.IMAGE;
  19316. else
  19317. type = Loader.getTypeFromUrl(url);
  19318. }
  19319. var content;
  19320. if (type === Loader.IMAGE)
  19321. content = Loader.textureMap[URL.formatURL(url)];
  19322. else
  19323. content = Loader.loadedMap[URL.formatURL(url)];
  19324. if (!ignoreCache && content != null) {
  19325. ILaya.systemTimer.callLater(this, function () {
  19326. progress && progress.runWith(1);
  19327. complete && complete.runWith(content instanceof Array ? [content] : content);
  19328. this._loaderCount || this.event(Event.COMPLETE);
  19329. });
  19330. }
  19331. else {
  19332. var original;
  19333. original = url;
  19334. url = AtlasInfoManager.getFileLoadPath(url);
  19335. if (url != original && type !== "nativeimage") {
  19336. type = Loader.ATLAS;
  19337. }
  19338. else {
  19339. original = null;
  19340. }
  19341. var info = LoaderManager._resMap[url];
  19342. if (!info) {
  19343. info = this._infoPool.length ? this._infoPool.pop() : new ResInfo();
  19344. info.url = url;
  19345. info.type = type;
  19346. info.cache = cache;
  19347. info.group = group;
  19348. info.ignoreCache = ignoreCache;
  19349. info.useWorkerLoader = useWorkerLoader;
  19350. info.originalUrl = original;
  19351. complete && info.on(Event.COMPLETE, complete.caller, complete.method, complete.args);
  19352. progress && info.on(Event.PROGRESS, progress.caller, progress.method, progress.args);
  19353. LoaderManager._resMap[url] = info;
  19354. priority = priority < this._maxPriority ? priority : this._maxPriority - 1;
  19355. this._resInfos[priority].push(info);
  19356. this._statInfo.count++;
  19357. this.event(Event.PROGRESS, this.getProgress());
  19358. this._next();
  19359. }
  19360. else {
  19361. if (complete) {
  19362. if (original) {
  19363. complete && info._createListener(Event.COMPLETE, this, this._resInfoLoaded, [original, complete], false, false);
  19364. }
  19365. else {
  19366. complete && info._createListener(Event.COMPLETE, complete.caller, complete.method, complete.args, false, false);
  19367. }
  19368. }
  19369. progress && info._createListener(Event.PROGRESS, progress.caller, progress.method, progress.args, false, false);
  19370. }
  19371. }
  19372. return this;
  19373. }
  19374. _resInfoLoaded(original, complete) {
  19375. complete.runWith(Loader.getRes(original));
  19376. }
  19377. _createLoad(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true, ignoreCache = false) {
  19378. if (url instanceof Array)
  19379. return this._loadAssets(url, complete, progress, type, priority, cache);
  19380. var content = Loader.getRes(url);
  19381. if (content != null) {
  19382. ILaya.systemTimer.frameOnce(1, this, function () {
  19383. progress && progress.runWith(1);
  19384. complete && complete.runWith(content);
  19385. this._loaderCount || this.event(Event.COMPLETE);
  19386. });
  19387. }
  19388. else {
  19389. var info = LoaderManager._resMap[url];
  19390. if (!info) {
  19391. info = this._infoPool.length ? this._infoPool.pop() : new ResInfo();
  19392. info.url = url;
  19393. info.type = type;
  19394. info.cache = false;
  19395. info.ignoreCache = ignoreCache;
  19396. info.originalUrl = null;
  19397. info.group = null;
  19398. info.createCache = cache;
  19399. info.createConstructParams = constructParams;
  19400. info.createPropertyParams = propertyParams;
  19401. complete && info.on(Event.COMPLETE, complete.caller, complete.method, complete.args);
  19402. progress && info.on(Event.PROGRESS, progress.caller, progress.method, progress.args);
  19403. LoaderManager._resMap[url] = info;
  19404. priority = priority < this._maxPriority ? priority : this._maxPriority - 1;
  19405. this._resInfos[priority].push(info);
  19406. this._statInfo.count++;
  19407. this.event(Event.PROGRESS, this.getProgress());
  19408. this._next();
  19409. }
  19410. else {
  19411. complete && info._createListener(Event.COMPLETE, complete.caller, complete.method, complete.args, false, false);
  19412. progress && info._createListener(Event.PROGRESS, progress.caller, progress.method, progress.args, false, false);
  19413. }
  19414. }
  19415. return this;
  19416. }
  19417. _next() {
  19418. if (this._loaderCount >= this.maxLoader)
  19419. return;
  19420. for (var i = 0; i < this._maxPriority; i++) {
  19421. var infos = this._resInfos[i];
  19422. while (infos.length > 0) {
  19423. var info = infos.shift();
  19424. if (info)
  19425. return this._doLoad(info);
  19426. }
  19427. }
  19428. this._loaderCount || this.event(Event.COMPLETE);
  19429. }
  19430. _doLoad(resInfo) {
  19431. this._loaderCount++;
  19432. var loader = this._loaders.length ? this._loaders.pop() : new Loader();
  19433. loader.on(Event.COMPLETE, null, onLoaded);
  19434. loader.on(Event.PROGRESS, null, function (num) {
  19435. resInfo.event(Event.PROGRESS, num);
  19436. });
  19437. loader.on(Event.ERROR, null, function (msg) {
  19438. onLoaded(null);
  19439. });
  19440. var _me = this;
  19441. function onLoaded(data = null) {
  19442. loader.offAll();
  19443. loader._data = null;
  19444. loader._customParse = false;
  19445. _me._loaders.push(loader);
  19446. _me._endLoad(resInfo, data instanceof Array ? [data] : data);
  19447. _me._loaderCount--;
  19448. _me._next();
  19449. }
  19450. loader._constructParams = resInfo.createConstructParams;
  19451. loader._propertyParams = resInfo.createPropertyParams;
  19452. loader._createCache = resInfo.createCache;
  19453. loader.load(resInfo.url, resInfo.type, resInfo.cache, resInfo.group, resInfo.ignoreCache, resInfo.useWorkerLoader);
  19454. }
  19455. _endLoad(resInfo, content) {
  19456. var url = resInfo.url;
  19457. if (content == null) {
  19458. var errorCount = this._failRes[url] || 0;
  19459. if (errorCount < this.retryNum) {
  19460. console.warn("[warn]Retry to load:", url);
  19461. this._failRes[url] = errorCount + 1;
  19462. ILaya.systemTimer.once(this.retryDelay, this, this._addReTry, [resInfo], false);
  19463. return;
  19464. }
  19465. else {
  19466. Loader.clearRes(url);
  19467. console.warn("[error]Failed to load:", url);
  19468. this.event(Event.ERROR, url);
  19469. }
  19470. }
  19471. if (this._failRes[url])
  19472. this._failRes[url] = 0;
  19473. delete LoaderManager._resMap[url];
  19474. if (resInfo.originalUrl) {
  19475. content = Loader.getRes(resInfo.originalUrl);
  19476. }
  19477. resInfo.event(Event.COMPLETE, content);
  19478. resInfo.offAll();
  19479. this._infoPool.push(resInfo);
  19480. this._statInfo.loaded++;
  19481. this.event(Event.PROGRESS, this.getProgress());
  19482. }
  19483. _addReTry(resInfo) {
  19484. this._resInfos[this._maxPriority - 1].push(resInfo);
  19485. this._next();
  19486. }
  19487. clearRes(url) {
  19488. Loader.clearRes(url);
  19489. }
  19490. clearTextureRes(url) {
  19491. Loader.clearTextureRes(url);
  19492. }
  19493. getRes(url) {
  19494. return Loader.getRes(url);
  19495. }
  19496. cacheRes(url, data) {
  19497. Loader.cacheRes(url, data);
  19498. }
  19499. setGroup(url, group) {
  19500. Loader.setGroup(url, group);
  19501. }
  19502. clearResByGroup(group) {
  19503. Loader.clearResByGroup(group);
  19504. }
  19505. static cacheRes(url, data) {
  19506. Loader.cacheRes(url, data);
  19507. }
  19508. clearUnLoaded() {
  19509. for (var i = 0; i < this._maxPriority; i++) {
  19510. var infos = this._resInfos[i];
  19511. for (var j = infos.length - 1; j > -1; j--) {
  19512. var info = infos[j];
  19513. if (info) {
  19514. info.offAll();
  19515. this._infoPool.push(info);
  19516. }
  19517. }
  19518. infos.length = 0;
  19519. }
  19520. this._loaderCount = 0;
  19521. LoaderManager._resMap = {};
  19522. }
  19523. cancelLoadByUrls(urls) {
  19524. if (!urls)
  19525. return;
  19526. for (var i = 0, n = urls.length; i < n; i++) {
  19527. this.cancelLoadByUrl(urls[i]);
  19528. }
  19529. }
  19530. cancelLoadByUrl(url) {
  19531. for (var i = 0; i < this._maxPriority; i++) {
  19532. var infos = this._resInfos[i];
  19533. for (var j = infos.length - 1; j > -1; j--) {
  19534. var info = infos[j];
  19535. if (info && info.url === url) {
  19536. infos[j] = null;
  19537. info.offAll();
  19538. this._infoPool.push(info);
  19539. }
  19540. }
  19541. }
  19542. if (LoaderManager._resMap[url])
  19543. delete LoaderManager._resMap[url];
  19544. }
  19545. _loadAssets(arr, complete = null, progress = null, type = null, priority = 1, cache = true, group = null) {
  19546. var itemCount = arr.length;
  19547. var loadedCount = 0;
  19548. var totalSize = 0;
  19549. var items = [];
  19550. var success = true;
  19551. for (var i = 0; i < itemCount; i++) {
  19552. let url = arr[i];
  19553. let item;
  19554. if (typeof (url) == 'string')
  19555. item = { url: url, type: type, size: 1, priority: priority };
  19556. else
  19557. item = url;
  19558. if (!item.size)
  19559. item.size = 1;
  19560. item.progress = 0;
  19561. totalSize += item.size;
  19562. items.push(item);
  19563. var progressHandler = progress ? Handler.create(null, loadProgress, [item], false) : null;
  19564. var completeHandler = (complete || progress) ? Handler.create(null, loadComplete, [item]) : null;
  19565. this.load(item.url, completeHandler, progressHandler, item.type, item.priority || 1, cache, item.group || group, false, item.useWorkerLoader);
  19566. }
  19567. function loadComplete(item, content = null) {
  19568. loadedCount++;
  19569. item.progress = 1;
  19570. if (!content)
  19571. success = false;
  19572. if (loadedCount === itemCount && complete) {
  19573. complete.runWith(success);
  19574. }
  19575. }
  19576. function loadProgress(item, value) {
  19577. if (progress != null) {
  19578. item.progress = value;
  19579. var num = 0;
  19580. for (var j = 0; j < items.length; j++) {
  19581. var item1 = items[j];
  19582. if (item1) {
  19583. let prog = item1.progress == undefined ? 0 : item1.progress;
  19584. num += item1.size == undefined ? 0 : item1.size * prog;
  19585. }
  19586. }
  19587. var v = num / totalSize;
  19588. progress.runWith(v);
  19589. }
  19590. }
  19591. return this;
  19592. }
  19593. decodeBitmaps(urls) {
  19594. var i, len = urls.length;
  19595. var ctx;
  19596. ctx = ILaya.Render._context;
  19597. for (i = 0; i < len; i++) {
  19598. var atlas;
  19599. atlas = Loader.getAtlas(urls[i]);
  19600. if (atlas) {
  19601. this._decodeTexture(atlas[0], ctx);
  19602. }
  19603. else {
  19604. var tex;
  19605. tex = this.getRes(urls[i]);
  19606. if (tex && tex instanceof Texture) {
  19607. this._decodeTexture(tex, ctx);
  19608. }
  19609. }
  19610. }
  19611. }
  19612. _decodeTexture(tex, ctx) {
  19613. var bitmap = tex.bitmap;
  19614. if (!tex || !bitmap)
  19615. return;
  19616. var tImg = bitmap.source || bitmap.image;
  19617. if (!tImg)
  19618. return;
  19619. if (tImg instanceof HTMLImageElement) {
  19620. ctx.drawImage(tImg, 0, 0, 1, 1);
  19621. var info = ctx.getImageData(0, 0, 1, 1);
  19622. }
  19623. }
  19624. }
  19625. LoaderManager._resMap = {};
  19626. LoaderManager.createMap = { atlas: [null, Loader.ATLAS] };
  19627. class ResInfo extends EventDispatcher {
  19628. }
  19629. class LocalStorage {
  19630. static __init__() {
  19631. if (!LocalStorage._baseClass) {
  19632. LocalStorage._baseClass = Storage;
  19633. Storage.init();
  19634. }
  19635. LocalStorage.items = LocalStorage._baseClass.items;
  19636. LocalStorage.support = LocalStorage._baseClass.support;
  19637. return LocalStorage.support;
  19638. }
  19639. static setItem(key, value) {
  19640. LocalStorage._baseClass.setItem(key, value);
  19641. }
  19642. static getItem(key) {
  19643. return LocalStorage._baseClass.getItem(key);
  19644. }
  19645. static setJSON(key, value) {
  19646. LocalStorage._baseClass.setJSON(key, value);
  19647. }
  19648. static getJSON(key) {
  19649. return LocalStorage._baseClass.getJSON(key);
  19650. }
  19651. static removeItem(key) {
  19652. LocalStorage._baseClass.removeItem(key);
  19653. }
  19654. static clear() {
  19655. LocalStorage._baseClass.clear();
  19656. }
  19657. }
  19658. LocalStorage.support = false;
  19659. class Storage {
  19660. static init() {
  19661. try {
  19662. Storage.support = true;
  19663. Storage.items = window.localStorage;
  19664. Storage.setItem('laya', '1');
  19665. Storage.removeItem('laya');
  19666. }
  19667. catch (e) {
  19668. Storage.support = false;
  19669. }
  19670. if (!Storage.support)
  19671. console.log('LocalStorage is not supprot or browser is private mode.');
  19672. }
  19673. static setItem(key, value) {
  19674. try {
  19675. Storage.support && Storage.items.setItem(key, value);
  19676. }
  19677. catch (e) {
  19678. console.warn("set localStorage failed", e);
  19679. }
  19680. }
  19681. static getItem(key) {
  19682. return Storage.support ? Storage.items.getItem(key) : null;
  19683. }
  19684. static setJSON(key, value) {
  19685. try {
  19686. Storage.support && Storage.items.setItem(key, JSON.stringify(value));
  19687. }
  19688. catch (e) {
  19689. console.warn("set localStorage failed", e);
  19690. }
  19691. }
  19692. static getJSON(key) {
  19693. try {
  19694. let obj = JSON.parse(Storage.support ? Storage.items.getItem(key) : null);
  19695. return obj;
  19696. }
  19697. catch (err) {
  19698. return Storage.items.getItem(key);
  19699. }
  19700. }
  19701. static removeItem(key) {
  19702. Storage.support && Storage.items.removeItem(key);
  19703. }
  19704. static clear() {
  19705. Storage.support && Storage.items.clear();
  19706. }
  19707. }
  19708. Storage.support = false;
  19709. class TTFLoader {
  19710. load(fontPath) {
  19711. this._url = fontPath;
  19712. var tArr = fontPath.toLowerCase().split(".ttf")[0].split("/");
  19713. this.fontName = tArr[tArr.length - 1];
  19714. if (ILaya.Render.isConchApp) {
  19715. this._loadConch();
  19716. }
  19717. else if (window.FontFace) {
  19718. this._loadWithFontFace();
  19719. }
  19720. else {
  19721. this._loadWithCSS();
  19722. }
  19723. }
  19724. _loadConch() {
  19725. this._http = new HttpRequest();
  19726. this._http.on(Event.ERROR, this, this._onErr);
  19727. this._http.on(Event.COMPLETE, this, this._onHttpLoaded);
  19728. this._http.send(this._url, null, "get", Loader.BUFFER);
  19729. }
  19730. _onHttpLoaded(data = null) {
  19731. window["conchTextCanvas"].setFontFaceFromBuffer(this.fontName, data);
  19732. this._clearHttp();
  19733. this._complete();
  19734. }
  19735. _clearHttp() {
  19736. if (this._http) {
  19737. this._http.off(Event.ERROR, this, this._onErr);
  19738. this._http.off(Event.COMPLETE, this, this._onHttpLoaded);
  19739. this._http = null;
  19740. }
  19741. }
  19742. _onErr() {
  19743. this._clearHttp();
  19744. if (this.err) {
  19745. this.err.runWith("fail:" + this._url);
  19746. this.err = null;
  19747. }
  19748. }
  19749. _complete() {
  19750. ILaya.systemTimer.clear(this, this._complete);
  19751. ILaya.systemTimer.clear(this, this._checkComplete);
  19752. if (this._div && this._div.parentNode) {
  19753. this._div.parentNode.removeChild(this._div);
  19754. this._div = null;
  19755. }
  19756. if (this.complete) {
  19757. this.complete.runWith(this);
  19758. this.complete = null;
  19759. }
  19760. }
  19761. _checkComplete() {
  19762. if (ILaya.Browser.measureText(TTFLoader._testString, this._fontTxt).width != this._txtWidth) {
  19763. this._complete();
  19764. }
  19765. }
  19766. _loadWithFontFace() {
  19767. var fontFace = new window.FontFace(this.fontName, "url('" + this._url + "')");
  19768. document.fonts.add(fontFace);
  19769. var self = this;
  19770. fontFace.loaded.then((function () {
  19771. self._complete();
  19772. }));
  19773. fontFace.load();
  19774. }
  19775. _createDiv() {
  19776. this._div = Browser.createElement("div");
  19777. this._div.innerHTML = "laya";
  19778. var _style = this._div.style;
  19779. _style.fontFamily = this.fontName;
  19780. _style.position = "absolute";
  19781. _style.left = "-100px";
  19782. _style.top = "-100px";
  19783. document.body.appendChild(this._div);
  19784. }
  19785. _loadWithCSS() {
  19786. var fontStyle = Browser.createElement("style");
  19787. fontStyle.type = "text/css";
  19788. document.body.appendChild(fontStyle);
  19789. fontStyle.textContent = "@font-face { font-family:'" + this.fontName + "'; src:url('" + this._url + "');}";
  19790. this._fontTxt = "40px " + this.fontName;
  19791. this._txtWidth = Browser.measureText(TTFLoader._testString, this._fontTxt).width;
  19792. var self = this;
  19793. fontStyle.onload = function () {
  19794. ILaya.systemTimer.once(10000, self, self._complete);
  19795. };
  19796. ILaya.systemTimer.loop(20, this, this._checkComplete);
  19797. this._createDiv();
  19798. }
  19799. }
  19800. TTFLoader._testString = "LayaTTFFont";
  19801. class Ease {
  19802. static linearNone(t, b, c, d) {
  19803. return c * t / d + b;
  19804. }
  19805. static linearIn(t, b, c, d) {
  19806. return c * t / d + b;
  19807. }
  19808. static linearInOut(t, b, c, d) {
  19809. return c * t / d + b;
  19810. }
  19811. static linearOut(t, b, c, d) {
  19812. return c * t / d + b;
  19813. }
  19814. static bounceIn(t, b, c, d) {
  19815. return c - Ease.bounceOut(d - t, 0, c, d) + b;
  19816. }
  19817. static bounceInOut(t, b, c, d) {
  19818. if (t < d * 0.5)
  19819. return Ease.bounceIn(t * 2, 0, c, d) * .5 + b;
  19820. else
  19821. return Ease.bounceOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
  19822. }
  19823. static bounceOut(t, b, c, d) {
  19824. if ((t /= d) < (1 / 2.75))
  19825. return c * (7.5625 * t * t) + b;
  19826. else if (t < (2 / 2.75))
  19827. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
  19828. else if (t < (2.5 / 2.75))
  19829. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
  19830. else
  19831. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
  19832. }
  19833. static backIn(t, b, c, d, s = 1.70158) {
  19834. return c * (t /= d) * t * ((s + 1) * t - s) + b;
  19835. }
  19836. static backInOut(t, b, c, d, s = 1.70158) {
  19837. if ((t /= d * 0.5) < 1)
  19838. return c * 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
  19839. return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
  19840. }
  19841. static backOut(t, b, c, d, s = 1.70158) {
  19842. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  19843. }
  19844. static elasticIn(t, b, c, d, a = 0, p = 0) {
  19845. var s;
  19846. if (t == 0)
  19847. return b;
  19848. if ((t /= d) == 1)
  19849. return b + c;
  19850. if (!p)
  19851. p = d * .3;
  19852. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19853. a = c;
  19854. s = p / 4;
  19855. }
  19856. else
  19857. s = p / Ease.PI2 * Math.asin(c / a);
  19858. return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p)) + b;
  19859. }
  19860. static elasticInOut(t, b, c, d, a = 0, p = 0) {
  19861. var s;
  19862. if (t == 0)
  19863. return b;
  19864. if ((t /= d * 0.5) == 2)
  19865. return b + c;
  19866. if (!p)
  19867. p = d * (.3 * 1.5);
  19868. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19869. a = c;
  19870. s = p / 4;
  19871. }
  19872. else
  19873. s = p / Ease.PI2 * Math.asin(c / a);
  19874. if (t < 1)
  19875. return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p)) + b;
  19876. return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p) * .5 + c + b;
  19877. }
  19878. static elasticOut(t, b, c, d, a = 0, p = 0) {
  19879. var s;
  19880. if (t == 0)
  19881. return b;
  19882. if ((t /= d) == 1)
  19883. return b + c;
  19884. if (!p)
  19885. p = d * .3;
  19886. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19887. a = c;
  19888. s = p / 4;
  19889. }
  19890. else
  19891. s = p / Ease.PI2 * Math.asin(c / a);
  19892. return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * Ease.PI2 / p) + c + b);
  19893. }
  19894. static strongIn(t, b, c, d) {
  19895. return c * (t /= d) * t * t * t * t + b;
  19896. }
  19897. static strongInOut(t, b, c, d) {
  19898. if ((t /= d * 0.5) < 1)
  19899. return c * 0.5 * t * t * t * t * t + b;
  19900. return c * 0.5 * ((t -= 2) * t * t * t * t + 2) + b;
  19901. }
  19902. static strongOut(t, b, c, d) {
  19903. return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  19904. }
  19905. static sineInOut(t, b, c, d) {
  19906. return -c * 0.5 * (Math.cos(Math.PI * t / d) - 1) + b;
  19907. }
  19908. static sineIn(t, b, c, d) {
  19909. return -c * Math.cos(t / d * Ease.HALF_PI) + c + b;
  19910. }
  19911. static sineOut(t, b, c, d) {
  19912. return c * Math.sin(t / d * Ease.HALF_PI) + b;
  19913. }
  19914. static quintIn(t, b, c, d) {
  19915. return c * (t /= d) * t * t * t * t + b;
  19916. }
  19917. static quintInOut(t, b, c, d) {
  19918. if ((t /= d * 0.5) < 1)
  19919. return c * 0.5 * t * t * t * t * t + b;
  19920. return c * 0.5 * ((t -= 2) * t * t * t * t + 2) + b;
  19921. }
  19922. static quintOut(t, b, c, d) {
  19923. return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  19924. }
  19925. static quartIn(t, b, c, d) {
  19926. return c * (t /= d) * t * t * t + b;
  19927. }
  19928. static quartInOut(t, b, c, d) {
  19929. if ((t /= d * 0.5) < 1)
  19930. return c * 0.5 * t * t * t * t + b;
  19931. return -c * 0.5 * ((t -= 2) * t * t * t - 2) + b;
  19932. }
  19933. static quartOut(t, b, c, d) {
  19934. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  19935. }
  19936. static cubicIn(t, b, c, d) {
  19937. return c * (t /= d) * t * t + b;
  19938. }
  19939. static cubicInOut(t, b, c, d) {
  19940. if ((t /= d * 0.5) < 1)
  19941. return c * 0.5 * t * t * t + b;
  19942. return c * 0.5 * ((t -= 2) * t * t + 2) + b;
  19943. }
  19944. static cubicOut(t, b, c, d) {
  19945. return c * ((t = t / d - 1) * t * t + 1) + b;
  19946. }
  19947. static quadIn(t, b, c, d) {
  19948. return c * (t /= d) * t + b;
  19949. }
  19950. static quadInOut(t, b, c, d) {
  19951. if ((t /= d * 0.5) < 1)
  19952. return c * 0.5 * t * t + b;
  19953. return -c * 0.5 * ((--t) * (t - 2) - 1) + b;
  19954. }
  19955. static quadOut(t, b, c, d) {
  19956. return -c * (t /= d) * (t - 2) + b;
  19957. }
  19958. static expoIn(t, b, c, d) {
  19959. return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b - c * 0.001;
  19960. }
  19961. static expoInOut(t, b, c, d) {
  19962. if (t == 0)
  19963. return b;
  19964. if (t == d)
  19965. return b + c;
  19966. if ((t /= d * 0.5) < 1)
  19967. return c * 0.5 * Math.pow(2, 10 * (t - 1)) + b;
  19968. return c * 0.5 * (-Math.pow(2, -10 * --t) + 2) + b;
  19969. }
  19970. static expoOut(t, b, c, d) {
  19971. return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
  19972. }
  19973. static circIn(t, b, c, d) {
  19974. return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
  19975. }
  19976. static circInOut(t, b, c, d) {
  19977. if ((t /= d * 0.5) < 1)
  19978. return -c * 0.5 * (Math.sqrt(1 - t * t) - 1) + b;
  19979. return c * 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  19980. }
  19981. static circOut(t, b, c, d) {
  19982. return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
  19983. }
  19984. }
  19985. Ease.HALF_PI = Math.PI * 0.5;
  19986. Ease.PI2 = Math.PI * 2;
  19987. class Tween {
  19988. constructor() {
  19989. this.gid = 0;
  19990. this.repeat = 1;
  19991. this._count = 0;
  19992. }
  19993. static to(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false, autoRecover = true) {
  19994. return Pool.getItemByClass("tween", Tween)._create(target, props, duration, ease, complete, delay, coverBefore, true, autoRecover, true);
  19995. }
  19996. static from(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false, autoRecover = true) {
  19997. return Pool.getItemByClass("tween", Tween)._create(target, props, duration, ease, complete, delay, coverBefore, false, autoRecover, true);
  19998. }
  19999. to(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false) {
  20000. return this._create(target, props, duration, ease, complete, delay, coverBefore, true, false, true);
  20001. }
  20002. from(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false) {
  20003. return this._create(target, props, duration, ease, complete, delay, coverBefore, false, false, true);
  20004. }
  20005. _create(target, props, duration, ease, complete, delay, coverBefore, isTo, usePool, runNow) {
  20006. if (!target)
  20007. throw new Error("Tween:target is null");
  20008. this._target = target;
  20009. this._duration = duration;
  20010. this._ease = ease || props.ease || Tween.easeNone;
  20011. this._complete = complete || props.complete;
  20012. this._delay = delay;
  20013. this._props = [];
  20014. this._usedTimer = 0;
  20015. this._startTimer = Browser.now();
  20016. this._usedPool = usePool;
  20017. this._delayParam = null;
  20018. this.update = props.update;
  20019. var gid = (target.$_GID || (target.$_GID = Utils.getGID()));
  20020. if (!Tween.tweenMap[gid]) {
  20021. Tween.tweenMap[gid] = [this];
  20022. }
  20023. else {
  20024. if (coverBefore)
  20025. Tween.clearTween(target);
  20026. Tween.tweenMap[gid].push(this);
  20027. }
  20028. if (runNow) {
  20029. if (delay <= 0)
  20030. this.firstStart(target, props, isTo);
  20031. else {
  20032. this._delayParam = [target, props, isTo];
  20033. ILaya.timer.once(delay, this, this.firstStart, this._delayParam);
  20034. }
  20035. }
  20036. else {
  20037. this._initProps(target, props, isTo);
  20038. }
  20039. return this;
  20040. }
  20041. firstStart(target, props, isTo) {
  20042. this._delayParam = null;
  20043. if (target.destroyed) {
  20044. this.clear();
  20045. return;
  20046. }
  20047. this._initProps(target, props, isTo);
  20048. this._beginLoop();
  20049. }
  20050. _initProps(target, props, isTo) {
  20051. for (var p in props) {
  20052. if (typeof (target[p]) == 'number') {
  20053. var start = isTo ? target[p] : props[p];
  20054. var end = isTo ? props[p] : target[p];
  20055. this._props.push([p, start, end - start]);
  20056. if (!isTo)
  20057. target[p] = start;
  20058. }
  20059. }
  20060. }
  20061. _beginLoop() {
  20062. ILaya.timer.frameLoop(1, this, this._doEase);
  20063. }
  20064. _doEase() {
  20065. this._updateEase(Browser.now());
  20066. }
  20067. _updateEase(time) {
  20068. var target = this._target;
  20069. if (!target)
  20070. return;
  20071. if (target.destroyed)
  20072. return Tween.clearTween(target);
  20073. var usedTimer = this._usedTimer = time - this._startTimer - this._delay;
  20074. if (usedTimer < 0)
  20075. return;
  20076. if (usedTimer >= this._duration)
  20077. return this.complete();
  20078. var ratio = usedTimer > 0 ? this._ease(usedTimer, 0, 1, this._duration) : 0;
  20079. var props = this._props;
  20080. for (var i = 0, n = props.length; i < n; i++) {
  20081. var prop = props[i];
  20082. target[prop[0]] = prop[1] + (ratio * prop[2]);
  20083. }
  20084. if (this.update)
  20085. this.update.run();
  20086. }
  20087. set progress(v) {
  20088. var uTime = v * this._duration;
  20089. this._startTimer = Browser.now() - this._delay - uTime;
  20090. }
  20091. complete() {
  20092. if (!this._target)
  20093. return;
  20094. ILaya.timer.runTimer(this, this.firstStart);
  20095. var target = this._target;
  20096. var props = this._props;
  20097. var handler = this._complete;
  20098. for (var i = 0, n = props.length; i < n; i++) {
  20099. var prop = props[i];
  20100. target[prop[0]] = prop[1] + prop[2];
  20101. }
  20102. if (this.update)
  20103. this.update.run();
  20104. this._count++;
  20105. if (this.repeat != 0 && this._count >= this.repeat) {
  20106. this.clear();
  20107. handler && handler.run();
  20108. }
  20109. else {
  20110. this.restart();
  20111. }
  20112. }
  20113. pause() {
  20114. ILaya.timer.clear(this, this._beginLoop);
  20115. ILaya.timer.clear(this, this._doEase);
  20116. ILaya.timer.clear(this, this.firstStart);
  20117. var time = Browser.now();
  20118. var dTime;
  20119. dTime = time - this._startTimer - this._delay;
  20120. if (dTime < 0) {
  20121. this._usedTimer = dTime;
  20122. }
  20123. }
  20124. setStartTime(startTime) {
  20125. this._startTimer = startTime;
  20126. }
  20127. static clearAll(target) {
  20128. if (!target || !target.$_GID)
  20129. return;
  20130. var tweens = Tween.tweenMap[target.$_GID];
  20131. if (tweens) {
  20132. for (var i = 0, n = tweens.length; i < n; i++) {
  20133. tweens[i]._clear();
  20134. }
  20135. tweens.length = 0;
  20136. }
  20137. }
  20138. static clear(tween) {
  20139. tween.clear();
  20140. }
  20141. static clearTween(target) {
  20142. Tween.clearAll(target);
  20143. }
  20144. clear() {
  20145. if (this._target) {
  20146. this._remove();
  20147. this._clear();
  20148. }
  20149. }
  20150. _clear() {
  20151. this.pause();
  20152. ILaya.timer.clear(this, this.firstStart);
  20153. this._complete = null;
  20154. this._target = null;
  20155. this._ease = null;
  20156. this._props = null;
  20157. this._delayParam = null;
  20158. this.repeat = 1;
  20159. if (this._usedPool) {
  20160. this.update = null;
  20161. Pool.recover("tween", this);
  20162. }
  20163. }
  20164. recover() {
  20165. this._usedPool = true;
  20166. this._clear();
  20167. }
  20168. _remove() {
  20169. var tweens = Tween.tweenMap[this._target.$_GID];
  20170. if (tweens) {
  20171. for (var i = 0, n = tweens.length; i < n; i++) {
  20172. if (tweens[i] === this) {
  20173. tweens.splice(i, 1);
  20174. break;
  20175. }
  20176. }
  20177. }
  20178. }
  20179. restart() {
  20180. this.pause();
  20181. this._usedTimer = 0;
  20182. this._startTimer = Browser.now();
  20183. if (this._delayParam) {
  20184. ILaya.timer.once(this._delay, this, this.firstStart, this._delayParam);
  20185. return;
  20186. }
  20187. var props = this._props;
  20188. for (var i = 0, n = props.length; i < n; i++) {
  20189. var prop = props[i];
  20190. this._target[prop[0]] = prop[1];
  20191. }
  20192. ILaya.timer.once(this._delay, this, this._beginLoop);
  20193. }
  20194. resume() {
  20195. if (this._usedTimer >= this._duration)
  20196. return;
  20197. this._startTimer = Browser.now() - this._usedTimer - this._delay;
  20198. if (this._delayParam) {
  20199. if (this._usedTimer < 0) {
  20200. ILaya.timer.once(-this._usedTimer, this, this.firstStart, this._delayParam);
  20201. }
  20202. else {
  20203. this.firstStart.apply(this, this._delayParam);
  20204. }
  20205. }
  20206. else {
  20207. this._beginLoop();
  20208. }
  20209. }
  20210. static easeNone(t, b, c, d) {
  20211. return c * t / d + b;
  20212. }
  20213. }
  20214. Tween.tweenMap = [];
  20215. class Dragging {
  20216. constructor() {
  20217. this.ratio = 0.92;
  20218. this.maxOffset = 60;
  20219. this._dragging = false;
  20220. this._clickOnly = true;
  20221. }
  20222. start(target, area, hasInertia, elasticDistance, elasticBackTime, data, disableMouseEvent, ratio = 0.92) {
  20223. this.clearTimer();
  20224. this.target = target;
  20225. this.area = area;
  20226. this.hasInertia = hasInertia;
  20227. this.elasticDistance = area ? elasticDistance : 0;
  20228. this.elasticBackTime = elasticBackTime;
  20229. this.data = data;
  20230. this._disableMouseEvent = disableMouseEvent;
  20231. this.ratio = ratio;
  20232. this._parent = target.parent;
  20233. this._clickOnly = true;
  20234. this._dragging = true;
  20235. this._elasticRateX = this._elasticRateY = 1;
  20236. this._lastX = this._parent.mouseX;
  20237. this._lastY = this._parent.mouseY;
  20238. ILaya.stage.on(Event.MOUSE_UP, this, this.onStageMouseUp);
  20239. ILaya.stage.on(Event.MOUSE_OUT, this, this.onStageMouseUp);
  20240. ILaya.systemTimer.frameLoop(1, this, this.loop);
  20241. }
  20242. clearTimer() {
  20243. ILaya.systemTimer.clear(this, this.loop);
  20244. ILaya.systemTimer.clear(this, this.tweenMove);
  20245. if (this._tween) {
  20246. this._tween.recover();
  20247. this._tween = null;
  20248. }
  20249. }
  20250. stop() {
  20251. if (this._dragging) {
  20252. MouseManager.instance.disableMouseEvent = false;
  20253. ILaya.stage.off(Event.MOUSE_UP, this, this.onStageMouseUp);
  20254. ILaya.stage.off(Event.MOUSE_OUT, this, this.onStageMouseUp);
  20255. this._dragging = false;
  20256. this.target && this.area && this.backToArea();
  20257. this.clear();
  20258. }
  20259. }
  20260. loop() {
  20261. var point = this._parent.getMousePoint();
  20262. var mouseX = point.x;
  20263. var mouseY = point.y;
  20264. var offsetX = mouseX - this._lastX;
  20265. var offsetY = mouseY - this._lastY;
  20266. if (this._clickOnly) {
  20267. if (Math.abs(offsetX * ILaya.stage._canvasTransform.getScaleX()) > 1 || Math.abs(offsetY * ILaya.stage._canvasTransform.getScaleY()) > 1) {
  20268. this._clickOnly = false;
  20269. this._offsets || (this._offsets = []);
  20270. this._offsets.length = 0;
  20271. this.target.event(Event.DRAG_START, this.data);
  20272. MouseManager.instance.disableMouseEvent = this._disableMouseEvent;
  20273. }
  20274. else
  20275. return;
  20276. }
  20277. else {
  20278. this._offsets.push(offsetX, offsetY);
  20279. }
  20280. if (offsetX === 0 && offsetY === 0)
  20281. return;
  20282. this._lastX = mouseX;
  20283. this._lastY = mouseY;
  20284. this.target.x += offsetX * this._elasticRateX;
  20285. this.target.y += offsetY * this._elasticRateY;
  20286. this.area && this.checkArea();
  20287. this.target.event(Event.DRAG_MOVE, this.data);
  20288. }
  20289. checkArea() {
  20290. if (this.elasticDistance <= 0) {
  20291. this.backToArea();
  20292. }
  20293. else {
  20294. if (this.target._x < this.area.x) {
  20295. var offsetX = this.area.x - this.target._x;
  20296. }
  20297. else if (this.target._x > this.area.x + this.area.width) {
  20298. offsetX = this.target._x - this.area.x - this.area.width;
  20299. }
  20300. else {
  20301. offsetX = 0;
  20302. }
  20303. this._elasticRateX = Math.max(0, 1 - (offsetX / this.elasticDistance));
  20304. if (this.target._y < this.area.y) {
  20305. var offsetY = this.area.y - this.target.y;
  20306. }
  20307. else if (this.target._y > this.area.y + this.area.height) {
  20308. offsetY = this.target._y - this.area.y - this.area.height;
  20309. }
  20310. else {
  20311. offsetY = 0;
  20312. }
  20313. this._elasticRateY = Math.max(0, 1 - (offsetY / this.elasticDistance));
  20314. }
  20315. }
  20316. backToArea() {
  20317. this.target.x = Math.min(Math.max(this.target._x, this.area.x), this.area.x + this.area.width);
  20318. this.target.y = Math.min(Math.max(this.target._y, this.area.y), this.area.y + this.area.height);
  20319. }
  20320. onStageMouseUp(e) {
  20321. MouseManager.instance.disableMouseEvent = false;
  20322. ILaya.stage.off(Event.MOUSE_UP, this, this.onStageMouseUp);
  20323. ILaya.stage.off(Event.MOUSE_OUT, this, this.onStageMouseUp);
  20324. ILaya.systemTimer.clear(this, this.loop);
  20325. if (this._clickOnly || !this.target)
  20326. return;
  20327. if (this.hasInertia) {
  20328. if (this._offsets.length < 1) {
  20329. this._offsets.push(this._parent.mouseX - this._lastX, this._parent.mouseY - this._lastY);
  20330. }
  20331. this._offsetX = this._offsetY = 0;
  20332. var len = this._offsets.length;
  20333. var n = Math.min(len, 6);
  20334. var m = this._offsets.length - n;
  20335. for (var i = len - 1; i > m; i--) {
  20336. this._offsetY += this._offsets[i--];
  20337. this._offsetX += this._offsets[i];
  20338. }
  20339. this._offsetX = this._offsetX / n * 2;
  20340. this._offsetY = this._offsetY / n * 2;
  20341. if (Math.abs(this._offsetX) > this.maxOffset)
  20342. this._offsetX = this._offsetX > 0 ? this.maxOffset : -this.maxOffset;
  20343. if (Math.abs(this._offsetY) > this.maxOffset)
  20344. this._offsetY = this._offsetY > 0 ? this.maxOffset : -this.maxOffset;
  20345. ILaya.systemTimer.frameLoop(1, this, this.tweenMove);
  20346. }
  20347. else if (this.elasticDistance > 0) {
  20348. this.checkElastic();
  20349. }
  20350. else {
  20351. this.clear();
  20352. }
  20353. }
  20354. checkElastic() {
  20355. var tx = NaN;
  20356. var ty = NaN;
  20357. if (this.target.x < this.area.x)
  20358. tx = this.area.x;
  20359. else if (this.target._x > this.area.x + this.area.width)
  20360. tx = this.area.x + this.area.width;
  20361. if (this.target.y < this.area.y)
  20362. ty = this.area.y;
  20363. else if (this.target._y > this.area.y + this.area.height)
  20364. ty = this.area.y + this.area.height;
  20365. if (!isNaN(tx) || !isNaN(ty)) {
  20366. var obj = {};
  20367. if (!isNaN(tx))
  20368. obj.x = tx;
  20369. if (!isNaN(ty))
  20370. obj.y = ty;
  20371. this._tween = Tween.to(this.target, obj, this.elasticBackTime, Ease.sineOut, Handler.create(this, this.clear), 0, false, false);
  20372. }
  20373. else {
  20374. this.clear();
  20375. }
  20376. }
  20377. tweenMove() {
  20378. this._offsetX *= this.ratio * this._elasticRateX;
  20379. this._offsetY *= this.ratio * this._elasticRateY;
  20380. this.target.x += this._offsetX;
  20381. this.target.y += this._offsetY;
  20382. this.area && this.checkArea();
  20383. this.target.event(Event.DRAG_MOVE, this.data);
  20384. if ((Math.abs(this._offsetX) < 1 && Math.abs(this._offsetY) < 1) || this._elasticRateX < 0.5 || this._elasticRateY < 0.5) {
  20385. ILaya.systemTimer.clear(this, this.tweenMove);
  20386. if (this.elasticDistance > 0)
  20387. this.checkElastic();
  20388. else
  20389. this.clear();
  20390. }
  20391. }
  20392. clear() {
  20393. if (this.target) {
  20394. this.clearTimer();
  20395. var sp = this.target;
  20396. this.target = null;
  20397. this._parent = null;
  20398. sp.event(Event.DRAG_END, this.data);
  20399. }
  20400. }
  20401. }
  20402. class Component {
  20403. constructor() {
  20404. this._id = Utils.getGID();
  20405. this._resetComp();
  20406. }
  20407. get id() {
  20408. return this._id;
  20409. }
  20410. get enabled() {
  20411. return this._enabled;
  20412. }
  20413. set enabled(value) {
  20414. if (this._enabled != value) {
  20415. this._enabled = value;
  20416. if (this.owner) {
  20417. if (value)
  20418. this.owner.activeInHierarchy && this._onEnable();
  20419. else
  20420. this.owner.activeInHierarchy && this._onDisable();
  20421. }
  20422. }
  20423. }
  20424. get isSingleton() {
  20425. return true;
  20426. }
  20427. get destroyed() {
  20428. return this._destroyed;
  20429. }
  20430. _isScript() {
  20431. return false;
  20432. }
  20433. _resetComp() {
  20434. this._indexInList = -1;
  20435. this._enabled = true;
  20436. this._awaked = false;
  20437. this.owner = null;
  20438. }
  20439. _getIndexInList() {
  20440. return this._indexInList;
  20441. }
  20442. _setIndexInList(index) {
  20443. this._indexInList = index;
  20444. }
  20445. _onAdded() {
  20446. }
  20447. _onAwake() {
  20448. }
  20449. _onEnable() {
  20450. }
  20451. _onDisable() {
  20452. }
  20453. _onDestroy() {
  20454. }
  20455. onReset() {
  20456. }
  20457. _parse(data, interactMap = null) {
  20458. }
  20459. _parseInteractive(data = null, spriteMap = null) {
  20460. }
  20461. _cloneTo(dest) {
  20462. }
  20463. _setActive(value) {
  20464. if (value) {
  20465. if (!this._awaked) {
  20466. this._awaked = true;
  20467. this._onAwake();
  20468. }
  20469. this._enabled && this._onEnable();
  20470. }
  20471. else {
  20472. this._enabled && this._onDisable();
  20473. }
  20474. }
  20475. destroy() {
  20476. if (this.owner)
  20477. this.owner._destroyComponent(this);
  20478. }
  20479. _destroy() {
  20480. if (this.owner.activeInHierarchy && this._enabled)
  20481. this._setActive(false);
  20482. this._onDestroy();
  20483. this._destroyed = true;
  20484. if (this.onReset !== Component.prototype.onReset) {
  20485. this.onReset();
  20486. this._resetComp();
  20487. Pool.recoverByClass(this);
  20488. }
  20489. else {
  20490. this._resetComp();
  20491. }
  20492. }
  20493. }
  20494. class AnimationBase extends Sprite {
  20495. constructor() {
  20496. super();
  20497. this.wrapMode = 0;
  20498. this._interval = Config.animationInterval;
  20499. this._isReverse = false;
  20500. this._frameRateChanged = false;
  20501. this._setBitUp(Const.DISPLAY);
  20502. }
  20503. play(start = 0, loop = true, name = "") {
  20504. this._isPlaying = true;
  20505. this._actionName = name;
  20506. this.index = (typeof (start) == 'string') ? this._getFrameByLabel(start) : start;
  20507. this.loop = loop;
  20508. this._isReverse = this.wrapMode === AnimationBase.WRAP_REVERSE;
  20509. if (this.index == 0 && this._isReverse) {
  20510. this.index = this.count - 1;
  20511. }
  20512. if (this.interval > 0)
  20513. this.timerLoop(this.interval, this, this._frameLoop, null, true, true);
  20514. }
  20515. get interval() {
  20516. return this._interval;
  20517. }
  20518. set interval(value) {
  20519. if (this._interval != value) {
  20520. this._frameRateChanged = true;
  20521. this._interval = value;
  20522. if (this._isPlaying && value > 0) {
  20523. this.timerLoop(value, this, this._frameLoop, null, true, true);
  20524. }
  20525. }
  20526. }
  20527. _getFrameByLabel(label) {
  20528. for (var i = 0; i < this._count; i++) {
  20529. var item = this._labels[i];
  20530. if (item && item.indexOf(label) > -1)
  20531. return i;
  20532. }
  20533. return 0;
  20534. }
  20535. _frameLoop() {
  20536. if (!this._controlNode || this._controlNode.destroyed) {
  20537. this.clearTimer(this, this._frameLoop);
  20538. return;
  20539. }
  20540. if (this._isReverse) {
  20541. this._index--;
  20542. if (this._index < 0) {
  20543. if (this.loop) {
  20544. if (this.wrapMode == AnimationBase.WRAP_PINGPONG) {
  20545. this._index = this._count > 0 ? 1 : 0;
  20546. this._isReverse = false;
  20547. }
  20548. else {
  20549. this._index = this._count - 1;
  20550. }
  20551. this.event(Event.COMPLETE);
  20552. }
  20553. else {
  20554. this._index = 0;
  20555. this.stop();
  20556. this.event(Event.COMPLETE);
  20557. return;
  20558. }
  20559. }
  20560. }
  20561. else {
  20562. this._index++;
  20563. if (this._index >= this._count) {
  20564. if (this.loop) {
  20565. if (this.wrapMode == AnimationBase.WRAP_PINGPONG) {
  20566. this._index = this._count - 2 >= 0 ? this._count - 2 : 0;
  20567. this._isReverse = true;
  20568. }
  20569. else {
  20570. this._index = 0;
  20571. }
  20572. this.event(Event.COMPLETE);
  20573. }
  20574. else {
  20575. this._index--;
  20576. this.stop();
  20577. this.event(Event.COMPLETE);
  20578. return;
  20579. }
  20580. }
  20581. }
  20582. this.index = this._index;
  20583. }
  20584. _setControlNode(node) {
  20585. if (this._controlNode) {
  20586. this._controlNode.off(Event.DISPLAY, this, this._resumePlay);
  20587. this._controlNode.off(Event.UNDISPLAY, this, this._resumePlay);
  20588. }
  20589. this._controlNode = node;
  20590. if (node && node != this) {
  20591. node.on(Event.DISPLAY, this, this._resumePlay);
  20592. node.on(Event.UNDISPLAY, this, this._resumePlay);
  20593. }
  20594. }
  20595. _setDisplay(value) {
  20596. super._setDisplay(value);
  20597. this._resumePlay();
  20598. }
  20599. _resumePlay() {
  20600. if (this._isPlaying) {
  20601. if (this._controlNode.displayedInStage)
  20602. this.play(this._index, this.loop, this._actionName);
  20603. else
  20604. this.clearTimer(this, this._frameLoop);
  20605. }
  20606. }
  20607. stop() {
  20608. this._isPlaying = false;
  20609. this.clearTimer(this, this._frameLoop);
  20610. }
  20611. get isPlaying() {
  20612. return this._isPlaying;
  20613. }
  20614. addLabel(label, index) {
  20615. if (!this._labels)
  20616. this._labels = {};
  20617. if (!this._labels[index])
  20618. this._labels[index] = [];
  20619. this._labels[index].push(label);
  20620. }
  20621. removeLabel(label) {
  20622. if (!label)
  20623. this._labels = null;
  20624. else if (this._labels) {
  20625. for (var name in this._labels) {
  20626. this._removeLabelFromList(this._labels[name], label);
  20627. }
  20628. }
  20629. }
  20630. _removeLabelFromList(list, label) {
  20631. if (!list)
  20632. return;
  20633. for (var i = list.length - 1; i >= 0; i--) {
  20634. if (list[i] == label) {
  20635. list.splice(i, 1);
  20636. }
  20637. }
  20638. }
  20639. gotoAndStop(position) {
  20640. this.index = (typeof (position) == 'string') ? this._getFrameByLabel(position) : position;
  20641. this.stop();
  20642. }
  20643. get index() {
  20644. return this._index;
  20645. }
  20646. set index(value) {
  20647. this._index = value;
  20648. this._displayToIndex(value);
  20649. if (this._labels && this._labels[value]) {
  20650. var tArr = this._labels[value];
  20651. for (var i = 0, len = tArr.length; i < len; i++) {
  20652. this.event(Event.LABEL, tArr[i]);
  20653. }
  20654. }
  20655. }
  20656. _displayToIndex(value) {
  20657. }
  20658. get count() {
  20659. return this._count;
  20660. }
  20661. clear() {
  20662. this.stop();
  20663. this._labels = null;
  20664. return this;
  20665. }
  20666. }
  20667. AnimationBase.WRAP_POSITIVE = 0;
  20668. AnimationBase.WRAP_REVERSE = 1;
  20669. AnimationBase.WRAP_PINGPONG = 2;
  20670. ClassUtils.regClass("laya.display.AnimationBase", AnimationBase);
  20671. ClassUtils.regClass("Laya.AnimationBase", AnimationBase);
  20672. class MathUtil {
  20673. static subtractVector3(l, r, o) {
  20674. o[0] = l[0] - r[0];
  20675. o[1] = l[1] - r[1];
  20676. o[2] = l[2] - r[2];
  20677. }
  20678. static lerp(left, right, amount) {
  20679. return left * (1 - amount) + right * amount;
  20680. }
  20681. static scaleVector3(f, b, e) {
  20682. e[0] = f[0] * b;
  20683. e[1] = f[1] * b;
  20684. e[2] = f[2] * b;
  20685. }
  20686. static lerpVector3(l, r, t, o) {
  20687. var ax = l[0], ay = l[1], az = l[2];
  20688. o[0] = ax + t * (r[0] - ax);
  20689. o[1] = ay + t * (r[1] - ay);
  20690. o[2] = az + t * (r[2] - az);
  20691. }
  20692. static lerpVector4(l, r, t, o) {
  20693. var ax = l[0], ay = l[1], az = l[2], aw = l[3];
  20694. o[0] = ax + t * (r[0] - ax);
  20695. o[1] = ay + t * (r[1] - ay);
  20696. o[2] = az + t * (r[2] - az);
  20697. o[3] = aw + t * (r[3] - aw);
  20698. }
  20699. static slerpQuaternionArray(a, Offset1, b, Offset2, t, out, Offset3) {
  20700. var ax = a[Offset1 + 0], ay = a[Offset1 + 1], az = a[Offset1 + 2], aw = a[Offset1 + 3], bx = b[Offset2 + 0], by = b[Offset2 + 1], bz = b[Offset2 + 2], bw = b[Offset2 + 3];
  20701. var omega, cosom, sinom, scale0, scale1;
  20702. cosom = ax * bx + ay * by + az * bz + aw * bw;
  20703. if (cosom < 0.0) {
  20704. cosom = -cosom;
  20705. bx = -bx;
  20706. by = -by;
  20707. bz = -bz;
  20708. bw = -bw;
  20709. }
  20710. if ((1.0 - cosom) > 0.000001) {
  20711. omega = Math.acos(cosom);
  20712. sinom = Math.sin(omega);
  20713. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  20714. scale1 = Math.sin(t * omega) / sinom;
  20715. }
  20716. else {
  20717. scale0 = 1.0 - t;
  20718. scale1 = t;
  20719. }
  20720. out[Offset3 + 0] = scale0 * ax + scale1 * bx;
  20721. out[Offset3 + 1] = scale0 * ay + scale1 * by;
  20722. out[Offset3 + 2] = scale0 * az + scale1 * bz;
  20723. out[Offset3 + 3] = scale0 * aw + scale1 * bw;
  20724. return out;
  20725. }
  20726. static getRotation(x0, y0, x1, y1) {
  20727. return Math.atan2(y1 - y0, x1 - x0) / Math.PI * 180;
  20728. }
  20729. static sortBigFirst(a, b) {
  20730. if (a == b)
  20731. return 0;
  20732. return b > a ? 1 : -1;
  20733. }
  20734. static sortSmallFirst(a, b) {
  20735. if (a == b)
  20736. return 0;
  20737. return b > a ? -1 : 1;
  20738. }
  20739. static sortNumBigFirst(a, b) {
  20740. return parseFloat(b) - parseFloat(a);
  20741. }
  20742. static sortNumSmallFirst(a, b) {
  20743. return parseFloat(a) - parseFloat(b);
  20744. }
  20745. static sortByKey(key, bigFirst = false, forceNum = true) {
  20746. var _sortFun;
  20747. if (bigFirst) {
  20748. _sortFun = forceNum ? MathUtil.sortNumBigFirst : MathUtil.sortBigFirst;
  20749. }
  20750. else {
  20751. _sortFun = forceNum ? MathUtil.sortNumSmallFirst : MathUtil.sortSmallFirst;
  20752. }
  20753. return function (a, b) {
  20754. return _sortFun(a[key], b[key]);
  20755. };
  20756. }
  20757. }
  20758. class FrameAnimation extends AnimationBase {
  20759. constructor() {
  20760. super();
  20761. if (FrameAnimation._sortIndexFun === undefined) {
  20762. FrameAnimation._sortIndexFun = MathUtil.sortByKey("index", false, true);
  20763. }
  20764. }
  20765. static _sortIndexFun(objpre, objnext) {
  20766. return objpre.index - objnext.index;
  20767. }
  20768. _setUp(targetDic, animationData) {
  20769. this._targetDic = targetDic;
  20770. this._animationData = animationData;
  20771. this.interval = 1000 / animationData.frameRate;
  20772. if (animationData.parsed) {
  20773. this._count = animationData.count;
  20774. this._labels = animationData.labels;
  20775. this._usedFrames = animationData.animationNewFrames;
  20776. }
  20777. else {
  20778. this._usedFrames = [];
  20779. this._calculateDatas();
  20780. animationData.parsed = true;
  20781. animationData.labels = this._labels;
  20782. animationData.count = this._count;
  20783. animationData.animationNewFrames = this._usedFrames;
  20784. }
  20785. }
  20786. clear() {
  20787. super.clear();
  20788. this._targetDic = null;
  20789. this._animationData = null;
  20790. return this;
  20791. }
  20792. _displayToIndex(value) {
  20793. if (!this._animationData)
  20794. return;
  20795. if (value < 0)
  20796. value = 0;
  20797. if (value > this._count)
  20798. value = this._count;
  20799. var nodes = this._animationData.nodes, i, len = nodes.length;
  20800. for (i = 0; i < len; i++) {
  20801. this._displayNodeToFrame(nodes[i], value);
  20802. }
  20803. }
  20804. _displayNodeToFrame(node, frame, targetDic = null) {
  20805. if (!targetDic)
  20806. targetDic = this._targetDic;
  20807. var target = targetDic[node.target];
  20808. if (!target) {
  20809. return;
  20810. }
  20811. var frames = node.frames, key, propFrames, value;
  20812. var keys = node.keys, i, len = keys.length;
  20813. for (i = 0; i < len; i++) {
  20814. key = keys[i];
  20815. propFrames = frames[key];
  20816. if (propFrames.length > frame) {
  20817. value = propFrames[frame];
  20818. }
  20819. else {
  20820. value = propFrames[propFrames.length - 1];
  20821. }
  20822. target[key] = value;
  20823. }
  20824. var funkeys = node.funkeys;
  20825. len = funkeys.length;
  20826. var funFrames;
  20827. if (len == 0)
  20828. return;
  20829. for (i = 0; i < len; i++) {
  20830. key = funkeys[i];
  20831. funFrames = frames[key];
  20832. if (funFrames[frame] !== undefined) {
  20833. target[key] && target[key].apply(target, funFrames[frame]);
  20834. }
  20835. }
  20836. }
  20837. _calculateDatas() {
  20838. if (!this._animationData)
  20839. return;
  20840. var nodes = this._animationData.nodes, i, len = nodes.length, tNode;
  20841. this._count = 0;
  20842. for (i = 0; i < len; i++) {
  20843. tNode = nodes[i];
  20844. this._calculateKeyFrames(tNode);
  20845. }
  20846. this._count += 1;
  20847. }
  20848. _calculateKeyFrames(node) {
  20849. var keyFrames = node.keyframes, key, tKeyFrames, target = node.target;
  20850. if (!node.frames)
  20851. node.frames = {};
  20852. if (!node.keys)
  20853. node.keys = [];
  20854. else
  20855. node.keys.length = 0;
  20856. if (!node.funkeys)
  20857. node.funkeys = [];
  20858. else
  20859. node.funkeys.length = 0;
  20860. if (!node.initValues)
  20861. node.initValues = {};
  20862. for (key in keyFrames) {
  20863. var isFun = key.indexOf("()") != -1;
  20864. tKeyFrames = keyFrames[key];
  20865. if (isFun)
  20866. key = key.substr(0, key.length - 2);
  20867. if (!node.frames[key]) {
  20868. node.frames[key] = [];
  20869. }
  20870. if (!isFun) {
  20871. if (this._targetDic && this._targetDic[target]) {
  20872. node.initValues[key] = this._targetDic[target][key];
  20873. }
  20874. tKeyFrames.sort(FrameAnimation._sortIndexFun);
  20875. node.keys.push(key);
  20876. this._calculateNodePropFrames(tKeyFrames, node.frames[key], key, target);
  20877. }
  20878. else {
  20879. node.funkeys.push(key);
  20880. var map = node.frames[key];
  20881. for (var i = 0; i < tKeyFrames.length; i++) {
  20882. var temp = tKeyFrames[i];
  20883. map[temp.index] = temp.value;
  20884. if (temp.index > this._count)
  20885. this._count = temp.index;
  20886. }
  20887. }
  20888. }
  20889. }
  20890. resetNodes() {
  20891. if (!this._targetDic)
  20892. return;
  20893. if (!this._animationData)
  20894. return;
  20895. var nodes = this._animationData.nodes, i, len = nodes.length;
  20896. var tNode;
  20897. var initValues;
  20898. for (i = 0; i < len; i++) {
  20899. tNode = nodes[i];
  20900. initValues = tNode.initValues;
  20901. if (!initValues)
  20902. continue;
  20903. var target = this._targetDic[tNode.target];
  20904. if (!target)
  20905. continue;
  20906. var key;
  20907. for (key in initValues) {
  20908. target[key] = initValues[key];
  20909. }
  20910. }
  20911. }
  20912. _calculateNodePropFrames(keyframes, frames, key, target) {
  20913. var i, len = keyframes.length - 1;
  20914. frames.length = keyframes[len].index + 1;
  20915. for (i = 0; i < len; i++) {
  20916. this._dealKeyFrame(keyframes[i]);
  20917. this._calculateFrameValues(keyframes[i], keyframes[i + 1], frames);
  20918. }
  20919. if (len == 0) {
  20920. frames[0] = keyframes[0].value;
  20921. if (this._usedFrames)
  20922. this._usedFrames[keyframes[0].index] = true;
  20923. }
  20924. this._dealKeyFrame(keyframes[i]);
  20925. }
  20926. _dealKeyFrame(keyFrame) {
  20927. if (keyFrame.label && keyFrame.label != "")
  20928. this.addLabel(keyFrame.label, keyFrame.index);
  20929. }
  20930. _calculateFrameValues(startFrame, endFrame, result) {
  20931. var i, easeFun;
  20932. var start = startFrame.index, end = endFrame.index;
  20933. var startValue = startFrame.value;
  20934. var dValue = endFrame.value - startFrame.value;
  20935. var dLen = end - start;
  20936. var frames = this._usedFrames;
  20937. if (end > this._count)
  20938. this._count = end;
  20939. if (startFrame.tween) {
  20940. easeFun = Ease[startFrame.tweenMethod];
  20941. if (easeFun == null)
  20942. easeFun = Ease.linearNone;
  20943. for (i = start; i < end; i++) {
  20944. result[i] = easeFun(i - start, startValue, dValue, dLen);
  20945. if (frames)
  20946. frames[i] = true;
  20947. }
  20948. }
  20949. else {
  20950. for (i = start; i < end; i++) {
  20951. result[i] = startValue;
  20952. }
  20953. }
  20954. if (frames) {
  20955. frames[startFrame.index] = true;
  20956. frames[endFrame.index] = true;
  20957. }
  20958. result[endFrame.index] = endFrame.value;
  20959. }
  20960. }
  20961. ClassUtils.regClass("laya.display.FrameAnimation", FrameAnimation);
  20962. ClassUtils.regClass("Laya.FrameAnimation", FrameAnimation);
  20963. class WeakObject {
  20964. constructor() {
  20965. this._obj = {};
  20966. WeakObject._maps.push(this);
  20967. }
  20968. static __init__() {
  20969. WeakObject.I = new WeakObject();
  20970. if (!WeakObject.supportWeakMap)
  20971. ILaya.systemTimer.loop(WeakObject.delInterval, null, WeakObject.clearCache);
  20972. }
  20973. static clearCache() {
  20974. for (var i = 0, n = WeakObject._maps.length; i < n; i++) {
  20975. var obj = WeakObject._maps[i];
  20976. obj._obj = {};
  20977. }
  20978. }
  20979. set(key, value) {
  20980. if (key == null)
  20981. return;
  20982. if (WeakObject.supportWeakMap) ;
  20983. else {
  20984. if (typeof (key) == 'string' || typeof (key) == 'number') {
  20985. this._obj[key] = value;
  20986. }
  20987. else {
  20988. key.$_GID || (key.$_GID = Utils.getGID());
  20989. this._obj[key.$_GID] = value;
  20990. }
  20991. }
  20992. }
  20993. get(key) {
  20994. if (key == null)
  20995. return null;
  20996. if (WeakObject.supportWeakMap) ;
  20997. else {
  20998. if (typeof (key) == 'string' || typeof (key) == 'number')
  20999. return this._obj[key];
  21000. return this._obj[key.$_GID];
  21001. }
  21002. }
  21003. del(key) {
  21004. if (key == null)
  21005. return;
  21006. if (WeakObject.supportWeakMap) ;
  21007. else {
  21008. if (typeof (key) == 'string' || typeof (key) == 'number')
  21009. delete this._obj[key];
  21010. else
  21011. delete this._obj[this._obj.$_GID];
  21012. }
  21013. }
  21014. has(key) {
  21015. if (key == null)
  21016. return false;
  21017. if (WeakObject.supportWeakMap) ;
  21018. else {
  21019. if (typeof (key) == 'string' || typeof (key) == 'number')
  21020. return this._obj[key] != null;
  21021. return this._obj[this._obj.$_GID] != null;
  21022. }
  21023. }
  21024. }
  21025. WeakObject.supportWeakMap = false;
  21026. WeakObject.delInterval = 10 * 60 * 1000;
  21027. WeakObject._maps = [];
  21028. class SceneUtils {
  21029. static __init() {
  21030. SceneUtils._funMap = new WeakObject();
  21031. }
  21032. static getBindFun(value) {
  21033. var fun = SceneUtils._funMap.get(value);
  21034. if (fun == null) {
  21035. var temp = "\"" + value + "\"";
  21036. temp = temp.replace(/^"\${|}"$/g, "").replace(/\${/g, "\"+").replace(/}/g, "+\"");
  21037. var str = "(function(data){if(data==null)return;with(data){try{\nreturn " + temp + "\n}catch(e){}}})";
  21038. fun = window.Laya._runScript(str);
  21039. SceneUtils._funMap.set(value, fun);
  21040. }
  21041. return fun;
  21042. }
  21043. static createByData(root, uiView) {
  21044. var tInitTool = InitTool.create();
  21045. root = SceneUtils.createComp(uiView, root, root, null, tInitTool);
  21046. root._setBit(Const.NOT_READY, true);
  21047. if ("_idMap" in root) {
  21048. root["_idMap"] = tInitTool._idMap;
  21049. }
  21050. if (uiView.animations) {
  21051. var anilist = [];
  21052. var animations = uiView.animations;
  21053. var i, len = animations.length;
  21054. var tAni;
  21055. var tAniO;
  21056. for (i = 0; i < len; i++) {
  21057. tAni = new FrameAnimation();
  21058. tAniO = animations[i];
  21059. tAni._setUp(tInitTool._idMap, tAniO);
  21060. root[tAniO.name] = tAni;
  21061. tAni._setControlNode(root);
  21062. switch (tAniO.action) {
  21063. case 1:
  21064. tAni.play(0, false);
  21065. break;
  21066. case 2:
  21067. tAni.play(0, true);
  21068. break;
  21069. }
  21070. anilist.push(tAni);
  21071. }
  21072. root._aniList = anilist;
  21073. }
  21074. if (root._$componentType === "Scene" && root._width > 0 && uiView.props.hitTestPrior == null && !root.mouseThrough)
  21075. root.hitTestPrior = true;
  21076. tInitTool.beginLoad(root);
  21077. return root;
  21078. }
  21079. static createInitTool() {
  21080. return InitTool.create();
  21081. }
  21082. static createComp(uiView, comp = null, view = null, dataMap = null, initTool = null) {
  21083. if (uiView.type == "Scene3D" || uiView.type == "Sprite3D") {
  21084. var outBatchSprits = [];
  21085. var scene3D = ILaya.Laya["Utils3D"]._createSceneByJsonForMaker(uiView, outBatchSprits, initTool);
  21086. if (uiView.type == "Sprite3D")
  21087. ILaya.Laya["StaticBatchManager"].combine(scene3D, outBatchSprits);
  21088. else
  21089. ILaya.Laya["StaticBatchManager"].combine(null, outBatchSprits);
  21090. return scene3D;
  21091. }
  21092. comp = comp || SceneUtils.getCompInstance(uiView);
  21093. if (!comp) {
  21094. if (uiView.props && uiView.props.runtime)
  21095. console.warn("runtime not found:" + uiView.props.runtime);
  21096. else
  21097. console.warn("can not create:" + uiView.type);
  21098. return null;
  21099. }
  21100. var child = uiView.child;
  21101. if (child) {
  21102. var isList = comp["_$componentType"] == "List";
  21103. for (var i = 0, n = child.length; i < n; i++) {
  21104. var node = child[i];
  21105. if ('itemRender' in comp && (node.props.name == "render" || node.props.renderType === "render")) {
  21106. comp["itemRender"] = node;
  21107. }
  21108. else if (node.type == "Graphic") {
  21109. ILaya.ClassUtils._addGraphicsToSprite(node, comp);
  21110. }
  21111. else if (ILaya.ClassUtils._isDrawType(node.type)) {
  21112. ILaya.ClassUtils._addGraphicToSprite(node, comp, true);
  21113. }
  21114. else {
  21115. if (isList) {
  21116. var arr = [];
  21117. var tChild = SceneUtils.createComp(node, null, view, arr, initTool);
  21118. if (arr.length)
  21119. tChild["_$bindData"] = arr;
  21120. }
  21121. else {
  21122. tChild = SceneUtils.createComp(node, null, view, dataMap, initTool);
  21123. }
  21124. if (node.type == "Script") {
  21125. if (tChild instanceof Component) {
  21126. comp._addComponentInstance(tChild);
  21127. }
  21128. else {
  21129. if ("owner" in tChild) {
  21130. tChild["owner"] = comp;
  21131. }
  21132. else if ("target" in tChild) {
  21133. tChild["target"] = comp;
  21134. }
  21135. }
  21136. }
  21137. else if (node.props.renderType == "mask" || node.props.name == "mask") {
  21138. comp.mask = tChild;
  21139. }
  21140. else {
  21141. tChild instanceof Node && comp.addChild(tChild);
  21142. }
  21143. }
  21144. }
  21145. }
  21146. var props = uiView.props;
  21147. for (var prop in props) {
  21148. var value = props[prop];
  21149. if (typeof (value) == 'string' && (value.indexOf("@node:") >= 0 || value.indexOf("@Prefab:") >= 0)) {
  21150. if (initTool) {
  21151. initTool.addNodeRef(comp, prop, value);
  21152. }
  21153. }
  21154. else
  21155. SceneUtils.setCompValue(comp, prop, value, view, dataMap);
  21156. }
  21157. if (comp._afterInited) {
  21158. comp._afterInited();
  21159. }
  21160. if (uiView.compId && initTool && initTool._idMap) {
  21161. initTool._idMap[uiView.compId] = comp;
  21162. }
  21163. return comp;
  21164. }
  21165. static setCompValue(comp, prop, value, view = null, dataMap = null) {
  21166. if (typeof (value) == 'string' && value.indexOf("${") > -1) {
  21167. SceneUtils._sheet || (SceneUtils._sheet = ILaya.ClassUtils.getClass("laya.data.Table"));
  21168. if (!SceneUtils._sheet) {
  21169. console.warn("Can not find class Sheet");
  21170. return;
  21171. }
  21172. if (dataMap) {
  21173. dataMap.push(comp, prop, value);
  21174. }
  21175. else if (view) {
  21176. if (value.indexOf("].") == -1) {
  21177. value = value.replace(".", "[0].");
  21178. }
  21179. var watcher = new DataWatcher(comp, prop, value);
  21180. watcher.exe(view);
  21181. var one, temp;
  21182. var str = value.replace(/\[.*?\]\./g, ".");
  21183. while ((one = SceneUtils._parseWatchData.exec(str)) != null) {
  21184. var key1 = one[1];
  21185. while ((temp = SceneUtils._parseKeyWord.exec(key1)) != null) {
  21186. var key2 = temp[0];
  21187. var arr = (view._watchMap[key2] || (view._watchMap[key2] = []));
  21188. arr.push(watcher);
  21189. SceneUtils._sheet.I.notifer.on(key2, view, view.changeData, [key2]);
  21190. }
  21191. arr = (view._watchMap[key1] || (view._watchMap[key1] = []));
  21192. arr.push(watcher);
  21193. SceneUtils._sheet.I.notifer.on(key1, view, view.changeData, [key1]);
  21194. }
  21195. }
  21196. return;
  21197. }
  21198. if (prop === "var" && view) {
  21199. view[value] = comp;
  21200. }
  21201. else {
  21202. comp[prop] = (value === "true" ? true : (value === "false" ? false : value));
  21203. }
  21204. }
  21205. static getCompInstance(json) {
  21206. if (json.type == "UIView") {
  21207. if (json.props && json.props.pageData) {
  21208. return SceneUtils.createByData(null, json.props.pageData);
  21209. }
  21210. }
  21211. var runtime = (json.props && json.props.runtime) || json.type;
  21212. var compClass = ILaya.ClassUtils.getClass(runtime);
  21213. if (!compClass)
  21214. throw "Can not find class " + runtime;
  21215. if (json.type === "Script" && compClass.prototype._doAwake) {
  21216. var comp = Pool.createByClass(compClass);
  21217. comp._destroyed = false;
  21218. return comp;
  21219. }
  21220. if (json.props && "renderType" in json.props && json.props["renderType"] == "instance") {
  21221. if (!compClass["instance"])
  21222. compClass["instance"] = new compClass();
  21223. return compClass["instance"];
  21224. }
  21225. return new compClass();
  21226. }
  21227. }
  21228. SceneUtils._parseWatchData = /\${(.*?)}/g;
  21229. SceneUtils._parseKeyWord = /[a-zA-Z_][a-zA-Z0-9_]*(?:(?:\.[a-zA-Z_][a-zA-Z0-9_]*)+)/g;
  21230. class DataWatcher {
  21231. constructor(comp, prop, value) {
  21232. this.comp = comp;
  21233. this.prop = prop;
  21234. this.value = value;
  21235. }
  21236. exe(view) {
  21237. var fun = SceneUtils.getBindFun(this.value);
  21238. this.comp[this.prop] = fun.call(this, view);
  21239. }
  21240. }
  21241. class InitTool {
  21242. reset() {
  21243. this._nodeRefList = null;
  21244. this._initList = null;
  21245. this._idMap = null;
  21246. this._loadList = null;
  21247. this._scene = null;
  21248. }
  21249. recover() {
  21250. this.reset();
  21251. Pool.recover("InitTool", this);
  21252. }
  21253. static create() {
  21254. var tool = Pool.getItemByClass("InitTool", InitTool);
  21255. tool._idMap = [];
  21256. return tool;
  21257. }
  21258. addLoadRes(url, type = null) {
  21259. if (!this._loadList)
  21260. this._loadList = [];
  21261. if (ILaya.loader.getRes(url)) {
  21262. return;
  21263. }
  21264. if (!type) {
  21265. this._loadList.push(url);
  21266. }
  21267. else {
  21268. this._loadList.push({ url: url, type: type });
  21269. }
  21270. }
  21271. addNodeRef(node, prop, referStr) {
  21272. if (!this._nodeRefList)
  21273. this._nodeRefList = [];
  21274. this._nodeRefList.push([node, prop, referStr]);
  21275. if (referStr.indexOf("@Prefab:") >= 0) {
  21276. this.addLoadRes(referStr.replace("@Prefab:", ""), Loader.PREFAB);
  21277. }
  21278. }
  21279. setNodeRef() {
  21280. if (!this._nodeRefList)
  21281. return;
  21282. if (!this._idMap) {
  21283. this._nodeRefList = null;
  21284. return;
  21285. }
  21286. var i, len;
  21287. len = this._nodeRefList.length;
  21288. var tRefInfo;
  21289. for (i = 0; i < len; i++) {
  21290. tRefInfo = this._nodeRefList[i];
  21291. tRefInfo[0][tRefInfo[1]] = this.getReferData(tRefInfo[2]);
  21292. }
  21293. this._nodeRefList = null;
  21294. }
  21295. getReferData(referStr) {
  21296. if (referStr.indexOf("@Prefab:") >= 0) {
  21297. var prefab;
  21298. prefab = Loader.getRes(referStr.replace("@Prefab:", ""));
  21299. return prefab;
  21300. }
  21301. else if (referStr.indexOf("@arr:") >= 0) {
  21302. referStr = referStr.replace("@arr:", "");
  21303. var list;
  21304. list = referStr.split(",");
  21305. var i, len;
  21306. var tStr;
  21307. len = list.length;
  21308. for (i = 0; i < len; i++) {
  21309. tStr = list[i];
  21310. if (tStr) {
  21311. list[i] = this._idMap[tStr.replace("@node:", "")];
  21312. }
  21313. else {
  21314. list[i] = null;
  21315. }
  21316. }
  21317. return list;
  21318. }
  21319. else {
  21320. return this._idMap[referStr.replace("@node:", "")];
  21321. }
  21322. }
  21323. addInitItem(item) {
  21324. if (!this._initList)
  21325. this._initList = [];
  21326. this._initList.push(item);
  21327. }
  21328. doInits() {
  21329. if (!this._initList)
  21330. return;
  21331. this._initList = null;
  21332. }
  21333. finish() {
  21334. this.setNodeRef();
  21335. this.doInits();
  21336. this._scene._setBit(Const.NOT_READY, false);
  21337. if (this._scene.parent && this._scene.parent.activeInHierarchy && this._scene.active)
  21338. this._scene._processActive();
  21339. this._scene.event("onViewCreated");
  21340. this.recover();
  21341. }
  21342. beginLoad(scene) {
  21343. this._scene = scene;
  21344. if (!this._loadList || this._loadList.length < 1) {
  21345. this.finish();
  21346. }
  21347. else {
  21348. ILaya.loader.load(this._loadList, Handler.create(this, this.finish));
  21349. }
  21350. }
  21351. }
  21352. class IStatRender {
  21353. show(x = 0, y = 0) {
  21354. }
  21355. enable() {
  21356. }
  21357. hide() {
  21358. }
  21359. set_onclick(fn) {
  21360. }
  21361. isCanvasRender() {
  21362. return true;
  21363. }
  21364. renderNotCanvas(ctx, x, y) { }
  21365. }
  21366. class StatUI extends IStatRender {
  21367. constructor() {
  21368. super(...arguments);
  21369. this._show = false;
  21370. this._useCanvas = false;
  21371. this._height = 100;
  21372. this._view = [];
  21373. }
  21374. show(x = 0, y = 0) {
  21375. if (!Browser.onMiniGame && !ILaya.Render.isConchApp && !Browser.onBDMiniGame && !Browser.onKGMiniGame && !Browser.onQGMiniGame && !Browser.onQQMiniGame && !Browser.onAlipayMiniGame && !Browser.onBLMiniGame && !Browser.onTTMiniGame && !Browser.onHWMiniGame && !Browser.onTBMiniGame)
  21376. this._useCanvas = true;
  21377. this._show = true;
  21378. Stat._fpsData.length = 60;
  21379. this._view[0] = { title: "FPS(WebGL)", value: "_fpsStr", color: "yellow", units: "int" };
  21380. this._view[1] = { title: "Sprite", value: "_spriteStr", color: "white", units: "int" };
  21381. this._view[2] = { title: "RenderBatches", value: "renderBatches", color: "white", units: "int" };
  21382. this._view[3] = { title: "SavedRenderBatches", value: "savedRenderBatches", color: "white", units: "int" };
  21383. this._view[4] = { title: "CPUMemory", value: "cpuMemory", color: "yellow", units: "M" };
  21384. this._view[5] = { title: "GPUMemory", value: "gpuMemory", color: "yellow", units: "M" };
  21385. this._view[6] = { title: "Shader", value: "shaderCall", color: "white", units: "int" };
  21386. this._view[7] = { title: "Canvas", value: "_canvasStr", color: "white", units: "int" };
  21387. if (Render.is3DMode) {
  21388. this._view[0].title = "FPS(3D)";
  21389. this._view[8] = { title: "TriFaces", value: "trianglesFaces", color: "white", units: "int" };
  21390. this._view[9] = { title: "FrustumCulling", value: "frustumCulling", color: "white", units: "int" };
  21391. this._view[10] = { title: "OctreeNodeCulling", value: "octreeNodeCulling", color: "white", units: "int" };
  21392. }
  21393. if (this._useCanvas) {
  21394. this.createUIPre(x, y);
  21395. }
  21396. else
  21397. this.createUI(x, y);
  21398. this.enable();
  21399. }
  21400. createUIPre(x, y) {
  21401. var pixel = Browser.pixelRatio;
  21402. this._width = pixel * 180;
  21403. this._vx = pixel * 120;
  21404. this._height = pixel * (this._view.length * 12 + 3 * pixel) + 4;
  21405. StatUI._fontSize = 12 * pixel;
  21406. for (var i = 0; i < this._view.length; i++) {
  21407. this._view[i].x = 4;
  21408. this._view[i].y = i * StatUI._fontSize + 2 * pixel;
  21409. }
  21410. if (!this._canvas) {
  21411. this._canvas = new HTMLCanvas(true);
  21412. this._canvas.size(this._width, this._height);
  21413. this._ctx = this._canvas.getContext('2d');
  21414. this._ctx.textBaseline = "top";
  21415. this._ctx.font = StatUI._fontSize + "px Arial";
  21416. this._canvas.source.style.cssText = "pointer-events:none;background:rgba(150,150,150,0.8);z-index:100000;position: absolute;direction:ltr;left:" + x + "px;top:" + y + "px;width:" + (this._width / pixel) + "px;height:" + (this._height / pixel) + "px;";
  21417. }
  21418. if (!Browser.onKGMiniGame) {
  21419. Browser.container.appendChild(this._canvas.source);
  21420. }
  21421. this._first = true;
  21422. this.loop();
  21423. this._first = false;
  21424. }
  21425. createUI(x, y) {
  21426. var stat = this._sp;
  21427. var pixel = Browser.pixelRatio;
  21428. if (!stat) {
  21429. stat = new Sprite();
  21430. this._leftText = new Text();
  21431. this._leftText.pos(5, 5);
  21432. this._leftText.color = "#ffffff";
  21433. stat.addChild(this._leftText);
  21434. this._txt = new Text();
  21435. this._txt.pos(130 * pixel, 5);
  21436. this._txt.color = "#ffffff";
  21437. stat.addChild(this._txt);
  21438. this._sp = stat;
  21439. }
  21440. stat.pos(x, y);
  21441. var text = "";
  21442. for (var i = 0; i < this._view.length; i++) {
  21443. var one = this._view[i];
  21444. text += one.title + "\n";
  21445. }
  21446. this._leftText.text = text;
  21447. var width = pixel * 138;
  21448. var height = pixel * (this._view.length * 12 + 3 * pixel) + 4;
  21449. this._txt.fontSize = StatUI._fontSize * pixel;
  21450. this._leftText.fontSize = StatUI._fontSize * pixel;
  21451. stat.size(width, height);
  21452. stat.graphics.clear();
  21453. stat.graphics.alpha(0.5);
  21454. stat.graphics.drawRect(0, 0, width + 110, height + 30, "#999999");
  21455. stat.graphics.alpha(2);
  21456. this.loop();
  21457. }
  21458. enable() {
  21459. ILaya.systemTimer.frameLoop(1, this, this.loop);
  21460. }
  21461. hide() {
  21462. this._show = false;
  21463. ILaya.systemTimer.clear(this, this.loop);
  21464. if (this._canvas) {
  21465. Browser.removeElement(this._canvas.source);
  21466. }
  21467. }
  21468. set_onclick(fn) {
  21469. if (this._sp) {
  21470. this._sp.on("click", this._sp, fn);
  21471. }
  21472. if (this._canvas) {
  21473. this._canvas.source.onclick = fn;
  21474. this._canvas.source.style.pointerEvents = '';
  21475. }
  21476. }
  21477. loop() {
  21478. Stat._count++;
  21479. var timer = Browser.now();
  21480. if (timer - Stat._timer < 1000)
  21481. return;
  21482. var count = Stat._count;
  21483. Stat.FPS = Math.round((count * 1000) / (timer - Stat._timer));
  21484. if (this._show) {
  21485. Stat.trianglesFaces = Math.round(Stat.trianglesFaces / count);
  21486. if (!this._useCanvas) {
  21487. Stat.renderBatches = Math.round(Stat.renderBatches / count) - 1;
  21488. }
  21489. else {
  21490. Stat.renderBatches = Math.round(Stat.renderBatches / count);
  21491. }
  21492. Stat.savedRenderBatches = Math.round(Stat.savedRenderBatches / count);
  21493. Stat.shaderCall = Math.round(Stat.shaderCall / count);
  21494. Stat.spriteRenderUseCacheCount = Math.round(Stat.spriteRenderUseCacheCount / count);
  21495. Stat.canvasNormal = Math.round(Stat.canvasNormal / count);
  21496. Stat.canvasBitmap = Math.round(Stat.canvasBitmap / count);
  21497. Stat.canvasReCache = Math.ceil(Stat.canvasReCache / count);
  21498. Stat.frustumCulling = Math.round(Stat.frustumCulling / count);
  21499. Stat.octreeNodeCulling = Math.round(Stat.octreeNodeCulling / count);
  21500. var delay = Stat.FPS > 0 ? Math.floor(1000 / Stat.FPS).toString() : " ";
  21501. Stat._fpsStr = Stat.FPS + (Stat.renderSlow ? " slow" : "") + " " + delay;
  21502. Stat._spriteStr = Stat.spriteCount + (Stat.spriteRenderUseCacheCount ? ("/" + Stat.spriteRenderUseCacheCount) : '');
  21503. Stat._canvasStr = Stat.canvasReCache + "/" + Stat.canvasNormal + "/" + Stat.canvasBitmap;
  21504. Stat.cpuMemory = Resource.cpuMemory;
  21505. Stat.gpuMemory = Resource.gpuMemory;
  21506. if (this._useCanvas) {
  21507. this.renderInfoPre();
  21508. }
  21509. else
  21510. this.renderInfo();
  21511. Stat.clear();
  21512. }
  21513. Stat._count = 0;
  21514. Stat._timer = timer;
  21515. }
  21516. renderInfoPre() {
  21517. var i = 0;
  21518. var one;
  21519. var value;
  21520. if (this._canvas) {
  21521. var ctx = this._ctx;
  21522. ctx.clearRect(this._first ? 0 : this._vx, 0, this._width, this._height);
  21523. for (i = 0; i < this._view.length; i++) {
  21524. one = this._view[i];
  21525. if (this._first) {
  21526. ctx.fillStyle = "white";
  21527. ctx.fillText(one.title, one.x, one.y);
  21528. }
  21529. ctx.fillStyle = one.color;
  21530. value = Stat[one.value];
  21531. (one.units == "M") && (value = Math.floor(value / (1024 * 1024) * 100) / 100 + " M");
  21532. ctx.fillText(value + "", one.x + this._vx, one.y);
  21533. }
  21534. }
  21535. }
  21536. renderInfo() {
  21537. var text = "";
  21538. for (var i = 0; i < this._view.length; i++) {
  21539. var one = this._view[i];
  21540. var value = Stat[one.value];
  21541. (one.units == "M") && (value = Math.floor(value / (1024 * 1024) * 100) / 100 + " M");
  21542. (one.units == "K") && (value = Math.floor(value / (1024) * 100) / 100 + " K");
  21543. text += value + "\n";
  21544. }
  21545. this._txt.text = text;
  21546. }
  21547. isCanvasRender() {
  21548. return this._useCanvas;
  21549. }
  21550. renderNotCanvas(ctx, x, y) {
  21551. this._show && this._sp && this._sp.render(ctx, 0, 0);
  21552. }
  21553. }
  21554. StatUI._fontSize = 12;
  21555. class Timer {
  21556. constructor(autoActive = true) {
  21557. this.scale = 1;
  21558. this.currTimer = Date.now();
  21559. this.currFrame = 0;
  21560. this._delta = 0;
  21561. this._lastTimer = Date.now();
  21562. this._map = [];
  21563. this._handlers = [];
  21564. this._temp = [];
  21565. this._count = 0;
  21566. autoActive && Timer.gSysTimer && Timer.gSysTimer.frameLoop(1, this, this._update);
  21567. }
  21568. get delta() {
  21569. return this._delta;
  21570. }
  21571. _update() {
  21572. if (this.scale <= 0) {
  21573. this._lastTimer = Date.now();
  21574. this._delta = 0;
  21575. return;
  21576. }
  21577. var frame = this.currFrame = this.currFrame + this.scale;
  21578. var now = Date.now();
  21579. var awake = (now - this._lastTimer) > 30000;
  21580. this._delta = (now - this._lastTimer) * this.scale;
  21581. var timer = this.currTimer = this.currTimer + this._delta;
  21582. this._lastTimer = now;
  21583. var handlers = this._handlers;
  21584. this._count = 0;
  21585. for (var i = 0, n = handlers.length; i < n; i++) {
  21586. var handler = handlers[i];
  21587. if (handler.method !== null) {
  21588. var t = handler.userFrame ? frame : timer;
  21589. if (t >= handler.exeTime) {
  21590. if (handler.repeat) {
  21591. if (!handler.jumpFrame || awake) {
  21592. handler.exeTime += handler.delay;
  21593. handler.run(false);
  21594. if (t > handler.exeTime) {
  21595. handler.exeTime += Math.ceil((t - handler.exeTime) / handler.delay) * handler.delay;
  21596. }
  21597. }
  21598. else {
  21599. while (t >= handler.exeTime) {
  21600. handler.exeTime += handler.delay;
  21601. handler.run(false);
  21602. }
  21603. }
  21604. }
  21605. else {
  21606. handler.run(true);
  21607. }
  21608. }
  21609. }
  21610. else {
  21611. this._count++;
  21612. }
  21613. }
  21614. if (this._count > 30 || frame % 200 === 0)
  21615. this._clearHandlers();
  21616. }
  21617. _clearHandlers() {
  21618. var handlers = this._handlers;
  21619. for (var i = 0, n = handlers.length; i < n; i++) {
  21620. var handler = handlers[i];
  21621. if (handler.method !== null)
  21622. this._temp.push(handler);
  21623. else
  21624. this._recoverHandler(handler);
  21625. }
  21626. this._handlers = this._temp;
  21627. handlers.length = 0;
  21628. this._temp = handlers;
  21629. }
  21630. _recoverHandler(handler) {
  21631. if (this._map[handler.key] == handler)
  21632. this._map[handler.key] = null;
  21633. handler.clear();
  21634. Timer._pool.push(handler);
  21635. }
  21636. _create(useFrame, repeat, delay, caller, method, args, coverBefore) {
  21637. if (!delay) {
  21638. method.apply(caller, args);
  21639. return null;
  21640. }
  21641. if (coverBefore) {
  21642. var handler = this._getHandler(caller, method);
  21643. if (handler) {
  21644. handler.repeat = repeat;
  21645. handler.userFrame = useFrame;
  21646. handler.delay = delay;
  21647. handler.caller = caller;
  21648. handler.method = method;
  21649. handler.args = args;
  21650. handler.exeTime = delay + (useFrame ? this.currFrame : this.currTimer + Date.now() - this._lastTimer);
  21651. return handler;
  21652. }
  21653. }
  21654. handler = Timer._pool.length > 0 ? Timer._pool.pop() : new TimerHandler();
  21655. handler.repeat = repeat;
  21656. handler.userFrame = useFrame;
  21657. handler.delay = delay;
  21658. handler.caller = caller;
  21659. handler.method = method;
  21660. handler.args = args;
  21661. handler.exeTime = delay + (useFrame ? this.currFrame : this.currTimer + Date.now() - this._lastTimer);
  21662. this._indexHandler(handler);
  21663. this._handlers.push(handler);
  21664. return handler;
  21665. }
  21666. _indexHandler(handler) {
  21667. var caller = handler.caller;
  21668. var method = handler.method;
  21669. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  21670. var mid = method.$_TID || (method.$_TID = (Timer._mid++) * 100000);
  21671. handler.key = cid + mid;
  21672. this._map[handler.key] = handler;
  21673. }
  21674. once(delay, caller, method, args = null, coverBefore = true) {
  21675. this._create(false, false, delay, caller, method, args, coverBefore);
  21676. }
  21677. loop(delay, caller, method, args = null, coverBefore = true, jumpFrame = false) {
  21678. var handler = this._create(false, true, delay, caller, method, args, coverBefore);
  21679. if (handler)
  21680. handler.jumpFrame = jumpFrame;
  21681. }
  21682. frameOnce(delay, caller, method, args = null, coverBefore = true) {
  21683. this._create(true, false, delay, caller, method, args, coverBefore);
  21684. }
  21685. frameLoop(delay, caller, method, args = null, coverBefore = true) {
  21686. this._create(true, true, delay, caller, method, args, coverBefore);
  21687. }
  21688. toString() {
  21689. return " handlers:" + this._handlers.length + " pool:" + Timer._pool.length;
  21690. }
  21691. clear(caller, method) {
  21692. var handler = this._getHandler(caller, method);
  21693. if (handler) {
  21694. this._map[handler.key] = null;
  21695. handler.key = 0;
  21696. handler.clear();
  21697. }
  21698. }
  21699. clearAll(caller) {
  21700. if (!caller)
  21701. return;
  21702. for (var i = 0, n = this._handlers.length; i < n; i++) {
  21703. var handler = this._handlers[i];
  21704. if (handler.caller === caller) {
  21705. this._map[handler.key] = null;
  21706. handler.key = 0;
  21707. handler.clear();
  21708. }
  21709. }
  21710. }
  21711. _getHandler(caller, method) {
  21712. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  21713. var mid = method.$_TID || (method.$_TID = (Timer._mid++) * 100000);
  21714. return this._map[cid + mid];
  21715. }
  21716. callLater(caller, method, args = null) {
  21717. CallLater.I.callLater(caller, method, args);
  21718. }
  21719. runCallLater(caller, method) {
  21720. CallLater.I.runCallLater(caller, method);
  21721. }
  21722. runTimer(caller, method) {
  21723. var handler = this._getHandler(caller, method);
  21724. if (handler && handler.method != null) {
  21725. this._map[handler.key] = null;
  21726. handler.run(true);
  21727. }
  21728. }
  21729. pause() {
  21730. this.scale = 0;
  21731. }
  21732. resume() {
  21733. this.scale = 1;
  21734. }
  21735. }
  21736. Timer.gSysTimer = null;
  21737. Timer._pool = [];
  21738. Timer._mid = 1;
  21739. class TimerHandler {
  21740. clear() {
  21741. this.caller = null;
  21742. this.method = null;
  21743. this.args = null;
  21744. }
  21745. run(withClear) {
  21746. var caller = this.caller;
  21747. if (caller && caller.destroyed)
  21748. return this.clear();
  21749. var method = this.method;
  21750. var args = this.args;
  21751. withClear && this.clear();
  21752. if (method == null)
  21753. return;
  21754. args ? method.apply(caller, args) : method.call(caller);
  21755. }
  21756. }
  21757. class SkinSV extends Value2D {
  21758. constructor(type) {
  21759. super(ShaderDefines2D.SKINMESH, 0);
  21760. this.offsetX = 300;
  21761. this.offsetY = 0;
  21762. var gl = WebGLContext.mainContext;
  21763. var _vlen = 8 * CONST3D2D.BYTES_PE;
  21764. this.position = [2, gl.FLOAT, false, _vlen, 0];
  21765. this.texcoord = [2, gl.FLOAT, false, _vlen, 2 * CONST3D2D.BYTES_PE];
  21766. this.color = [4, gl.FLOAT, false, _vlen, 4 * CONST3D2D.BYTES_PE];
  21767. }
  21768. }
  21769. class PrimitiveSV extends Value2D {
  21770. constructor(args) {
  21771. super(ShaderDefines2D.PRIMITIVE, 0);
  21772. this._attribLocation = ['position', 0, 'attribColor', 1];
  21773. }
  21774. }
  21775. class TextureSV extends Value2D {
  21776. constructor(subID = 0) {
  21777. super(ShaderDefines2D.TEXTURE2D, subID);
  21778. this.strength = 0;
  21779. this.blurInfo = null;
  21780. this.colorMat = null;
  21781. this.colorAlpha = null;
  21782. this._attribLocation = ['posuv', 0, 'attribColor', 1, 'attribFlags', 2];
  21783. }
  21784. clear() {
  21785. this.texture = null;
  21786. this.shader = null;
  21787. this.defines._value = this.subID;
  21788. }
  21789. }
  21790. class InlcudeFile {
  21791. constructor(txt) {
  21792. this.codes = {};
  21793. this.funs = {};
  21794. this.curUseID = -1;
  21795. this.funnames = "";
  21796. this.script = txt;
  21797. var begin = 0, ofs, end;
  21798. while (true) {
  21799. begin = txt.indexOf("#begin", begin);
  21800. if (begin < 0)
  21801. break;
  21802. end = begin + 5;
  21803. while (true) {
  21804. end = txt.indexOf("#end", end);
  21805. if (end < 0)
  21806. break;
  21807. if (txt.charAt(end + 4) === 'i')
  21808. end += 5;
  21809. else
  21810. break;
  21811. }
  21812. if (end < 0) {
  21813. throw "add include err,no #end:" + txt;
  21814. }
  21815. ofs = txt.indexOf('\n', begin);
  21816. var words = ILaya.ShaderCompile.splitToWords(txt.substr(begin, ofs - begin), null);
  21817. if (words[1] == 'code') {
  21818. this.codes[words[2]] = txt.substr(ofs + 1, end - ofs - 1);
  21819. }
  21820. else if (words[1] == 'function') {
  21821. ofs = txt.indexOf("function", begin);
  21822. ofs += "function".length;
  21823. this.funs[words[3]] = txt.substr(ofs + 1, end - ofs - 1);
  21824. this.funnames += words[3] + ";";
  21825. }
  21826. begin = end + 1;
  21827. }
  21828. }
  21829. getWith(name = null) {
  21830. var r = name ? this.codes[name] : this.script;
  21831. if (!r) {
  21832. throw "get with error:" + name;
  21833. }
  21834. return r;
  21835. }
  21836. getFunsScript(funsdef) {
  21837. var r = "";
  21838. for (var i in this.funs) {
  21839. if (funsdef.indexOf(i + ";") >= 0) {
  21840. r += this.funs[i];
  21841. }
  21842. }
  21843. return r;
  21844. }
  21845. }
  21846. class ShaderNode {
  21847. constructor(includefiles) {
  21848. this.childs = [];
  21849. this.text = "";
  21850. this.useFuns = "";
  21851. this.z = 0;
  21852. this.includefiles = includefiles;
  21853. }
  21854. setParent(parent) {
  21855. parent.childs.push(this);
  21856. this.z = parent.z + 1;
  21857. this.parent = parent;
  21858. }
  21859. setCondition(condition, type) {
  21860. if (condition) {
  21861. this.conditionType = type;
  21862. condition = condition.replace(/(\s*$)/g, "");
  21863. this.condition = function () {
  21864. return this[condition];
  21865. };
  21866. this.condition.__condition = condition;
  21867. }
  21868. }
  21869. toscript(def, out) {
  21870. return this._toscript(def, out, ++ShaderNode.__id);
  21871. }
  21872. _toscript(def, out, id) {
  21873. if (this.childs.length < 1 && !this.text)
  21874. return out;
  21875. var outIndex = out.length;
  21876. if (this.condition) {
  21877. var ifdef = !!this.condition.call(def);
  21878. this.conditionType === ILaya.ShaderCompile.IFDEF_ELSE && (ifdef = !ifdef);
  21879. if (!ifdef)
  21880. return out;
  21881. }
  21882. if (this.noCompile)
  21883. this.text && out.push(this.text);
  21884. this.childs.length > 0 && this.childs.forEach(function (o, index, arr) {
  21885. o._toscript(def, out, id);
  21886. });
  21887. if (this.includefiles.length > 0 && this.useFuns.length > 0) {
  21888. var funsCode;
  21889. for (var i = 0, n = this.includefiles.length; i < n; i++) {
  21890. if (this.includefiles[i].curUseID == id) {
  21891. continue;
  21892. }
  21893. funsCode = this.includefiles[i].file.getFunsScript(this.useFuns);
  21894. if (funsCode.length > 0) {
  21895. this.includefiles[i].curUseID = id;
  21896. out[0] = funsCode + out[0];
  21897. }
  21898. }
  21899. }
  21900. return out;
  21901. }
  21902. }
  21903. ShaderNode.__id = 1;
  21904. class ShaderCompile {
  21905. constructor(vs, ps, nameMap) {
  21906. this.defs = {};
  21907. let _this = this;
  21908. function _compile(script) {
  21909. script = script.replace(ShaderCompile._clearCR, "");
  21910. var includefiles = [];
  21911. var top = new ShaderNode(includefiles);
  21912. _this._compileToTree(top, script.split('\n'), 0, includefiles, _this.defs);
  21913. return top;
  21914. }
  21915. var startTime = Date.now();
  21916. this._VS = _compile(vs);
  21917. this._PS = _compile(ps);
  21918. this._nameMap = nameMap;
  21919. if ((Date.now() - startTime) > 2)
  21920. console.log("ShaderCompile use time:" + (Date.now() - startTime) + " size:" + vs.length + "/" + ps.length);
  21921. }
  21922. static __init__() {
  21923. var gl = LayaGL.instance;
  21924. ShaderCompile.shaderParamsMap = { "float": gl.FLOAT, "int": gl.INT, "bool": gl.BOOL, "vec2": gl.FLOAT_VEC2, "vec3": gl.FLOAT_VEC3, "vec4": gl.FLOAT_VEC4, "ivec2": gl.INT_VEC2, "ivec3": gl.INT_VEC3, "ivec4": gl.INT_VEC4, "bvec2": gl.BOOL_VEC2, "bvec3": gl.BOOL_VEC3, "bvec4": gl.BOOL_VEC4, "mat2": gl.FLOAT_MAT2, "mat3": gl.FLOAT_MAT3, "mat4": gl.FLOAT_MAT4, "sampler2D": gl.SAMPLER_2D, "samplerCube": gl.SAMPLER_CUBE };
  21925. }
  21926. static _parseOne(attributes, uniforms, words, i, word, b) {
  21927. var one = { type: ShaderCompile.shaderParamsMap[words[i + 1]], name: words[i + 2], size: isNaN(parseInt(words[i + 3])) ? 1 : parseInt(words[i + 3]) };
  21928. if (b) {
  21929. if (word == "attribute") {
  21930. attributes.push(one);
  21931. }
  21932. else {
  21933. uniforms.push(one);
  21934. }
  21935. }
  21936. if (words[i + 3] == ':') {
  21937. one.type = words[i + 4];
  21938. i += 2;
  21939. }
  21940. i += 2;
  21941. return i;
  21942. }
  21943. static addInclude(fileName, txt) {
  21944. if (!txt || txt.length === 0)
  21945. throw new Error("add shader include file err:" + fileName);
  21946. if (ShaderCompile.includes[fileName])
  21947. throw new Error("add shader include file err, has add:" + fileName);
  21948. ShaderCompile.includes[fileName] = new InlcudeFile(txt);
  21949. }
  21950. static preGetParams(vs, ps) {
  21951. var text = [vs, ps];
  21952. var result = {};
  21953. var attributes = [];
  21954. var uniforms = [];
  21955. var definesInfo = {};
  21956. var definesName = [];
  21957. result.attributes = attributes;
  21958. result.uniforms = uniforms;
  21959. result.defines = definesInfo;
  21960. var i, n;
  21961. for (var s = 0; s < 2; s++) {
  21962. text[s] = text[s].replace(ShaderCompile._removeAnnotation, "");
  21963. var words = text[s].match(ShaderCompile._reg);
  21964. var tempelse;
  21965. for (i = 0, n = words.length; i < n; i++) {
  21966. var word = words[i];
  21967. if (word != "attribute" && word != "uniform") {
  21968. if (word == "#define") {
  21969. word = words[++i];
  21970. definesName[word] = 1;
  21971. continue;
  21972. }
  21973. else if (word == "#ifdef") {
  21974. tempelse = words[++i];
  21975. var def = definesInfo[tempelse] = definesInfo[tempelse] || [];
  21976. for (i++; i < n; i++) {
  21977. word = words[i];
  21978. if (word != "attribute" && word != "uniform") {
  21979. if (word == "#else") {
  21980. for (i++; i < n; i++) {
  21981. word = words[i];
  21982. if (word != "attribute" && word != "uniform") {
  21983. if (word == "#endif") {
  21984. break;
  21985. }
  21986. continue;
  21987. }
  21988. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, !definesName[tempelse]);
  21989. }
  21990. }
  21991. continue;
  21992. }
  21993. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, definesName[tempelse]);
  21994. }
  21995. }
  21996. continue;
  21997. }
  21998. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, true);
  21999. }
  22000. }
  22001. return result;
  22002. }
  22003. static splitToWords(str, block) {
  22004. var out = [];
  22005. var c;
  22006. var ofs = -1;
  22007. var word;
  22008. for (var i = 0, n = str.length; i < n; i++) {
  22009. c = str.charAt(i);
  22010. if (" \t=+-*/&%!<>()'\",;".indexOf(c) >= 0) {
  22011. if (ofs >= 0 && (i - ofs) > 1) {
  22012. word = str.substr(ofs, i - ofs);
  22013. out.push(word);
  22014. }
  22015. if (c == '"' || c == "'") {
  22016. var ofs2 = str.indexOf(c, i + 1);
  22017. if (ofs2 < 0) {
  22018. throw "Sharder err:" + str;
  22019. }
  22020. out.push(str.substr(i + 1, ofs2 - i - 1));
  22021. i = ofs2;
  22022. ofs = -1;
  22023. continue;
  22024. }
  22025. if (c == '(' && block && out.length > 0) {
  22026. word = out[out.length - 1] + ";";
  22027. if ("vec4;main;".indexOf(word) < 0)
  22028. block.useFuns += word;
  22029. }
  22030. ofs = -1;
  22031. continue;
  22032. }
  22033. if (ofs < 0)
  22034. ofs = i;
  22035. }
  22036. if (ofs < n && (n - ofs) > 1) {
  22037. word = str.substr(ofs, n - ofs);
  22038. out.push(word);
  22039. }
  22040. return out;
  22041. }
  22042. _compileToTree(parent, lines, start, includefiles, defs) {
  22043. var node, preNode;
  22044. var text, name, fname;
  22045. var ofs, words, noUseNode;
  22046. var i, n, j;
  22047. for (i = start; i < lines.length; i++) {
  22048. text = lines[i];
  22049. if (text.length < 1)
  22050. continue;
  22051. ofs = text.indexOf("//");
  22052. if (ofs === 0)
  22053. continue;
  22054. if (ofs >= 0)
  22055. text = text.substr(0, ofs);
  22056. node = noUseNode || new ShaderNode(includefiles);
  22057. noUseNode = null;
  22058. node.text = text;
  22059. node.noCompile = true;
  22060. if ((ofs = text.indexOf("#")) >= 0) {
  22061. name = "#";
  22062. for (j = ofs + 1, n = text.length; j < n; j++) {
  22063. var c = text.charAt(j);
  22064. if (c === ' ' || c === '\t' || c === '?')
  22065. break;
  22066. name += c;
  22067. }
  22068. node.name = name;
  22069. switch (name) {
  22070. case "#ifdef":
  22071. case "#ifndef":
  22072. node.src = text;
  22073. node.noCompile = text.match(/[!&|()=<>]/) != null;
  22074. if (!node.noCompile) {
  22075. words = text.replace(/^\s*/, '').split(/\s+/);
  22076. node.setCondition(words[1], name === "#ifdef" ? ShaderCompile.IFDEF_YES : ShaderCompile.IFDEF_ELSE);
  22077. node.text = "//" + node.text;
  22078. }
  22079. else {
  22080. console.log("function():Boolean{return " + text.substr(ofs + node.name.length) + "}");
  22081. }
  22082. node.setParent(parent);
  22083. parent = node;
  22084. if (defs) {
  22085. words = text.substr(j).split(ShaderCompile._splitToWordExps3);
  22086. for (j = 0; j < words.length; j++) {
  22087. text = words[j];
  22088. text.length && (defs[text] = true);
  22089. }
  22090. }
  22091. continue;
  22092. case "#if":
  22093. node.src = text;
  22094. node.noCompile = true;
  22095. node.setParent(parent);
  22096. parent = node;
  22097. if (defs) {
  22098. words = text.substr(j).split(ShaderCompile._splitToWordExps3);
  22099. for (j = 0; j < words.length; j++) {
  22100. text = words[j];
  22101. text.length && text != "defined" && (defs[text] = true);
  22102. }
  22103. }
  22104. continue;
  22105. case "#else":
  22106. node.src = text;
  22107. parent = parent.parent;
  22108. preNode = parent.childs[parent.childs.length - 1];
  22109. node.noCompile = preNode.noCompile;
  22110. if (!node.noCompile) {
  22111. node.condition = preNode.condition;
  22112. node.conditionType = preNode.conditionType == ShaderCompile.IFDEF_YES ? ShaderCompile.IFDEF_ELSE : ShaderCompile.IFDEF_YES;
  22113. node.text = "//" + node.text + " " + preNode.text + " " + node.conditionType;
  22114. }
  22115. node.setParent(parent);
  22116. parent = node;
  22117. continue;
  22118. case "#endif":
  22119. parent = parent.parent;
  22120. preNode = parent.childs[parent.childs.length - 1];
  22121. node.noCompile = preNode.noCompile;
  22122. if (!node.noCompile) {
  22123. node.text = "//" + node.text;
  22124. }
  22125. node.setParent(parent);
  22126. continue;
  22127. case "#include":
  22128. words = ShaderCompile.splitToWords(text, null);
  22129. var inlcudeFile = ShaderCompile.includes[words[1]];
  22130. if (!inlcudeFile) {
  22131. throw "ShaderCompile error no this include file:" + words[1];
  22132. }
  22133. if ((ofs = words[0].indexOf("?")) < 0) {
  22134. node.setParent(parent);
  22135. text = inlcudeFile.getWith(words[2] == 'with' ? words[3] : null);
  22136. this._compileToTree(node, text.split('\n'), 0, includefiles, defs);
  22137. node.text = "";
  22138. continue;
  22139. }
  22140. node.setCondition(words[0].substr(ofs + 1), ShaderCompile.IFDEF_YES);
  22141. node.text = inlcudeFile.getWith(words[2] == 'with' ? words[3] : null);
  22142. break;
  22143. case "#import":
  22144. words = ShaderCompile.splitToWords(text, null);
  22145. fname = words[1];
  22146. includefiles.push({ node: node, file: ShaderCompile.includes[fname], ofs: node.text.length });
  22147. continue;
  22148. }
  22149. }
  22150. else {
  22151. preNode = parent.childs[parent.childs.length - 1];
  22152. if (preNode && !preNode.name) {
  22153. includefiles.length > 0 && ShaderCompile.splitToWords(text, preNode);
  22154. noUseNode = node;
  22155. preNode.text += "\n" + text;
  22156. continue;
  22157. }
  22158. includefiles.length > 0 && ShaderCompile.splitToWords(text, node);
  22159. }
  22160. node.setParent(parent);
  22161. }
  22162. }
  22163. createShader(define, shaderName, createShader, bindAttrib) {
  22164. var defMap = {};
  22165. var defineStr = "";
  22166. if (define) {
  22167. for (var i in define) {
  22168. defineStr += "#define " + i + "\n";
  22169. defMap[i] = true;
  22170. }
  22171. }
  22172. var vs = this._VS.toscript(defMap, []);
  22173. var ps = this._PS.toscript(defMap, []);
  22174. return (createShader || Shader.create)(defineStr + vs.join('\n'), defineStr + ps.join('\n'), shaderName, this._nameMap, bindAttrib);
  22175. }
  22176. }
  22177. ShaderCompile.IFDEF_NO = 0;
  22178. ShaderCompile.IFDEF_YES = 1;
  22179. ShaderCompile.IFDEF_ELSE = 2;
  22180. ShaderCompile.IFDEF_PARENT = 3;
  22181. ShaderCompile._removeAnnotation = new RegExp("(/\\*([^*]|[\\r\\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)", "g");
  22182. ShaderCompile._reg = new RegExp("(\".*\")|('.*')|([#\\w\\*-\\.+/()=<>{}\\\\]+)|([,;:\\\\])", "g");
  22183. ShaderCompile._splitToWordExps = new RegExp("[(\".*\")]+|[('.*')]+|([ \\t=\\+\\-*/&%!<>!%\(\),;])", "g");
  22184. ShaderCompile.includes = {};
  22185. ShaderCompile._clearCR = new RegExp("\r", "g");
  22186. ShaderCompile._splitToWordExps3 = new RegExp("[ \\t=\\+\\-*/&%!<>!%\(\),;\\|]", "g");
  22187. class WorkerLoader extends EventDispatcher {
  22188. constructor() {
  22189. super();
  22190. this.worker = new Worker(WorkerLoader.workerPath);
  22191. let me = this;
  22192. this.worker.onmessage = function (evt) {
  22193. me.workerMessage(evt.data);
  22194. };
  22195. }
  22196. static __init__() {
  22197. if (WorkerLoader._preLoadFun != null)
  22198. return false;
  22199. if (!Worker)
  22200. return false;
  22201. WorkerLoader._preLoadFun = Loader["prototype"]["_loadImage"];
  22202. Loader["prototype"]["_loadImage"] = WorkerLoader["prototype"]["_loadImage"];
  22203. if (!WorkerLoader.I)
  22204. WorkerLoader.I = new WorkerLoader();
  22205. return true;
  22206. }
  22207. static workerSupported() {
  22208. return Worker ? true : false;
  22209. }
  22210. static enableWorkerLoader() {
  22211. if (!WorkerLoader._tryEnabled) {
  22212. WorkerLoader.enable = true;
  22213. WorkerLoader._tryEnabled = true;
  22214. }
  22215. }
  22216. static set enable(value) {
  22217. if (WorkerLoader._enable != value) {
  22218. WorkerLoader._enable = value;
  22219. if (value && WorkerLoader._preLoadFun == null)
  22220. WorkerLoader._enable = WorkerLoader.__init__();
  22221. }
  22222. }
  22223. static get enable() {
  22224. return WorkerLoader._enable;
  22225. }
  22226. workerMessage(data) {
  22227. if (data) {
  22228. switch (data.type) {
  22229. case "Image":
  22230. this.imageLoaded(data);
  22231. break;
  22232. case "Disable":
  22233. WorkerLoader.enable = false;
  22234. break;
  22235. }
  22236. }
  22237. }
  22238. imageLoaded(data) {
  22239. if (!data.dataType || data.dataType != "imageBitmap") {
  22240. this.event(data.url, null);
  22241. return;
  22242. }
  22243. var imageData = data.imageBitmap;
  22244. console.log("load:", data.url);
  22245. this.event(data.url, imageData);
  22246. }
  22247. loadImage(url) {
  22248. this.worker.postMessage(url);
  22249. }
  22250. _loadImage(url) {
  22251. var _this = this;
  22252. let type = _this.type;
  22253. if (!this._useWorkerLoader || !WorkerLoader._enable) {
  22254. WorkerLoader._preLoadFun.call(_this, url);
  22255. return;
  22256. }
  22257. url = URL.formatURL(url);
  22258. function clear() {
  22259. WorkerLoader.I.off(url, _this, onload);
  22260. }
  22261. var onload = function (imageData) {
  22262. clear();
  22263. if (imageData) {
  22264. var image = imageData;
  22265. if (type !== "nativeimage") {
  22266. image = new Texture2D();
  22267. image.loadImageSource(imageData);
  22268. }
  22269. _this["onLoaded"](image);
  22270. }
  22271. else {
  22272. WorkerLoader._preLoadFun.call(_this, url);
  22273. }
  22274. };
  22275. WorkerLoader.I.on(url, _this, onload);
  22276. WorkerLoader.I.loadImage(url);
  22277. }
  22278. }
  22279. WorkerLoader.workerPath = "libs/workerloader.js";
  22280. WorkerLoader._enable = false;
  22281. WorkerLoader._tryEnabled = false;
  22282. class Mouse {
  22283. static set cursor(cursorStr) {
  22284. Mouse._style.cursor = cursorStr;
  22285. }
  22286. static get cursor() {
  22287. return Mouse._style.cursor;
  22288. }
  22289. static __init__() {
  22290. Mouse._style = Browser.document.body.style;
  22291. }
  22292. static hide() {
  22293. if (Mouse.cursor != "none") {
  22294. Mouse._preCursor = Mouse.cursor;
  22295. Mouse.cursor = "none";
  22296. }
  22297. }
  22298. static show() {
  22299. if (Mouse.cursor == "none") {
  22300. if (Mouse._preCursor) {
  22301. Mouse.cursor = Mouse._preCursor;
  22302. }
  22303. else {
  22304. Mouse.cursor = "auto";
  22305. }
  22306. }
  22307. }
  22308. }
  22309. class MeshParticle2D extends Mesh2D {
  22310. constructor(maxNum) {
  22311. super(MeshParticle2D.const_stride, maxNum * 4 * MeshParticle2D.const_stride, 4);
  22312. this.canReuse = true;
  22313. this.setAttributes(MeshParticle2D._fixattriInfo);
  22314. this.createQuadIB(maxNum);
  22315. this._quadNum = maxNum;
  22316. }
  22317. static __init__() {
  22318. var gl = LayaGL.instance;
  22319. MeshParticle2D._fixattriInfo = [gl.FLOAT, 4, 0,
  22320. gl.FLOAT, 3, 16,
  22321. gl.FLOAT, 3, 28,
  22322. gl.FLOAT, 4, 40,
  22323. gl.FLOAT, 4, 56,
  22324. gl.FLOAT, 3, 72,
  22325. gl.FLOAT, 2, 84,
  22326. gl.FLOAT, 4, 92,
  22327. gl.FLOAT, 1, 108,
  22328. gl.FLOAT, 1, 112];
  22329. }
  22330. setMaxParticleNum(maxNum) {
  22331. this._vb._resizeBuffer(maxNum * 4 * MeshParticle2D.const_stride, false);
  22332. this.createQuadIB(maxNum);
  22333. }
  22334. static getAMesh(maxNum) {
  22335. if (MeshParticle2D._POOL.length) {
  22336. var ret = MeshParticle2D._POOL.pop();
  22337. ret.setMaxParticleNum(maxNum);
  22338. return ret;
  22339. }
  22340. return new MeshParticle2D(maxNum);
  22341. }
  22342. releaseMesh() {
  22343. this._vb.setByteLength(0);
  22344. this.vertNum = 0;
  22345. this.indexNum = 0;
  22346. MeshParticle2D._POOL.push(this);
  22347. }
  22348. destroy() {
  22349. this._ib.destroy();
  22350. this._vb.destroy();
  22351. this._vb.deleteBuffer();
  22352. }
  22353. }
  22354. MeshParticle2D.const_stride = 116;
  22355. MeshParticle2D._POOL = [];
  22356. class HTMLImage extends Bitmap {
  22357. }
  22358. HTMLImage.create = function (width, height, format) {
  22359. var tex = new Texture2D(width, height, format, false, false);
  22360. tex.wrapModeU = exports.WarpMode.Clamp;
  22361. tex.wrapModeV = exports.WarpMode.Clamp;
  22362. return tex;
  22363. };
  22364. class Laya {
  22365. static __init(_classs) {
  22366. _classs.forEach(function (o) { o.__init$ && o.__init$(); });
  22367. }
  22368. static init(width, height, ...plugins) {
  22369. if (Laya._isinit)
  22370. return;
  22371. Laya._isinit = true;
  22372. ArrayBuffer.prototype.slice || (ArrayBuffer.prototype.slice = Laya._arrayBufferSlice);
  22373. Browser.__init__();
  22374. var mainCanv = Browser.mainCanvas = new HTMLCanvas(true);
  22375. var style = mainCanv.source.style;
  22376. style.position = 'absolute';
  22377. style.top = style.left = "0px";
  22378. style.background = "#000000";
  22379. if (!Browser.onKGMiniGame && !Browser.onAlipayMiniGame) {
  22380. Browser.container.appendChild(mainCanv.source);
  22381. }
  22382. Browser.canvas = new HTMLCanvas(true);
  22383. Browser.context = Browser.canvas.getContext('2d');
  22384. Browser.supportWebAudio = SoundManager.__init__();
  22385. Browser.supportLocalStorage = LocalStorage.__init__();
  22386. Laya.systemTimer = new Timer(false);
  22387. exports.systemTimer = Timer.gSysTimer = Laya.systemTimer;
  22388. Laya.startTimer = new Timer(false);
  22389. Laya.physicsTimer = new Timer(false);
  22390. Laya.updateTimer = new Timer(false);
  22391. Laya.lateTimer = new Timer(false);
  22392. Laya.timer = new Timer(false);
  22393. exports.startTimer = ILaya.startTimer = Laya.startTimer;
  22394. exports.lateTimer = ILaya.lateTimer = Laya.lateTimer;
  22395. exports.updateTimer = ILaya.updateTimer = Laya.updateTimer;
  22396. ILaya.systemTimer = Laya.systemTimer;
  22397. exports.timer = ILaya.timer = Laya.timer;
  22398. exports.physicsTimer = ILaya.physicsTimer = Laya.physicsTimer;
  22399. Laya.loader = new LoaderManager();
  22400. ILaya.Laya = Laya;
  22401. exports.loader = ILaya.loader = Laya.loader;
  22402. WeakObject.__init__();
  22403. SceneUtils.__init();
  22404. Mouse.__init__();
  22405. WebGL.inner_enable();
  22406. if (plugins) {
  22407. for (var i = 0, n = plugins.length; i < n; i++) {
  22408. if (plugins[i] && plugins[i].enable) {
  22409. plugins[i].enable();
  22410. }
  22411. }
  22412. }
  22413. if (ILaya.Render.isConchApp) {
  22414. Laya.enableNative();
  22415. }
  22416. Laya.enableWebGLPlus();
  22417. CacheManger.beginCheck();
  22418. exports.stage = Laya.stage = new Stage();
  22419. ILaya.stage = Laya.stage;
  22420. Utils.gStage = Laya.stage;
  22421. URL.rootPath = URL._basePath = Laya._getUrlPath();
  22422. MeshQuadTexture.__int__();
  22423. MeshVG.__init__();
  22424. MeshTexture.__init__();
  22425. Laya.render = new Render(0, 0, Browser.mainCanvas);
  22426. exports.render = Laya.render;
  22427. Laya.stage.size(width, height);
  22428. window.stage = Laya.stage;
  22429. WebGLContext.__init__();
  22430. MeshParticle2D.__init__();
  22431. ShaderCompile.__init__();
  22432. RenderSprite.__init__();
  22433. KeyBoardManager.__init__();
  22434. MouseManager.instance.__init__(Laya.stage, Render.canvas);
  22435. Input.__init__();
  22436. SoundManager.autoStopMusic = true;
  22437. Stat._StatRender = new StatUI();
  22438. Value2D._initone(ShaderDefines2D.TEXTURE2D, TextureSV);
  22439. Value2D._initone(ShaderDefines2D.TEXTURE2D | ShaderDefines2D.FILTERGLOW, TextureSV);
  22440. Value2D._initone(ShaderDefines2D.PRIMITIVE, PrimitiveSV);
  22441. Value2D._initone(ShaderDefines2D.SKINMESH, SkinSV);
  22442. return Render.canvas;
  22443. }
  22444. static _getUrlPath() {
  22445. return URL.getPath(location.protocol + "//" + location.host + location.pathname);
  22446. }
  22447. static _arrayBufferSlice(start, end) {
  22448. var arr = this;
  22449. var arrU8List = new Uint8Array(arr, start, end - start);
  22450. var newU8List = new Uint8Array(arrU8List.length);
  22451. newU8List.set(arrU8List);
  22452. return newU8List.buffer;
  22453. }
  22454. static alertGlobalError(value) {
  22455. var erralert = 0;
  22456. if (value) {
  22457. Browser.window.onerror = function (msg, url, line, column, detail) {
  22458. if (erralert++ < 5 && detail)
  22459. this.alert("出错啦,请把此信息截图给研发商\n" + msg + "\n" + detail.stack);
  22460. };
  22461. }
  22462. else {
  22463. Browser.window.onerror = null;
  22464. }
  22465. }
  22466. static _runScript(script) {
  22467. return Browser.window[Laya._evcode](script);
  22468. }
  22469. static enableDebugPanel(debugJsPath = "libs/laya.debugtool.js") {
  22470. if (!window['Laya']["DebugPanel"]) {
  22471. var script = Browser.createElement("script");
  22472. script.onload = function () {
  22473. window['Laya']["DebugPanel"].enable();
  22474. };
  22475. script.src = debugJsPath;
  22476. Browser.document.body.appendChild(script);
  22477. }
  22478. else {
  22479. window['Laya']["DebugPanel"].enable();
  22480. }
  22481. }
  22482. static enableWebGLPlus() {
  22483. WebGLContext.__init_native();
  22484. }
  22485. static enableNative() {
  22486. if (Laya.isNativeRender_enable)
  22487. return;
  22488. Laya.isNativeRender_enable = true;
  22489. if (Render.supportWebGLPlusRendering) {
  22490. Shader.prototype.uploadTexture2D = function (value) {
  22491. var gl = LayaGL.instance;
  22492. gl.bindTexture(gl.TEXTURE_2D, value);
  22493. };
  22494. }
  22495. RenderState2D.width = Browser.window.innerWidth;
  22496. RenderState2D.height = Browser.window.innerHeight;
  22497. Browser.measureText = function (txt, font) {
  22498. window["conchTextCanvas"].font = font;
  22499. return window["conchTextCanvas"].measureText(txt);
  22500. };
  22501. Stage.clear = function (color) {
  22502. Context.set2DRenderConfig();
  22503. var c = ColorUtils.create(color).arrColor;
  22504. var gl = LayaGL.instance;
  22505. if (c)
  22506. gl.clearColor(c[0], c[1], c[2], c[3]);
  22507. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
  22508. RenderState2D.clear();
  22509. };
  22510. Sprite.drawToCanvas = Sprite.drawToTexture = function (sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY) {
  22511. offsetX -= sprite.x;
  22512. offsetY -= sprite.y;
  22513. offsetX |= 0;
  22514. offsetY |= 0;
  22515. canvasWidth |= 0;
  22516. canvasHeight |= 0;
  22517. var canv = new HTMLCanvas(false);
  22518. var ctx = canv.getContext('2d');
  22519. canv.size(canvasWidth, canvasHeight);
  22520. ctx.asBitmap = true;
  22521. ctx._targets.start();
  22522. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  22523. ctx.flush();
  22524. ctx._targets.end();
  22525. ctx._targets.restore();
  22526. return canv;
  22527. };
  22528. Object["defineProperty"](RenderTexture2D.prototype, "uv", {
  22529. "get": function () {
  22530. return this._uv;
  22531. },
  22532. "set": function (v) {
  22533. this._uv = v;
  22534. }
  22535. });
  22536. HTMLCanvas.prototype.getTexture = function () {
  22537. if (!this._texture) {
  22538. this._texture = this.context._targets;
  22539. this._texture.uv = RenderTexture2D.flipyuv;
  22540. this._texture.bitmap = this._texture;
  22541. }
  22542. return this._texture;
  22543. };
  22544. }
  22545. }
  22546. Laya.stage = null;
  22547. Laya.systemTimer = null;
  22548. Laya.startTimer = null;
  22549. Laya.physicsTimer = null;
  22550. Laya.updateTimer = null;
  22551. Laya.lateTimer = null;
  22552. Laya.timer = null;
  22553. Laya.loader = null;
  22554. Laya.version = "2.10.0";
  22555. Laya._isinit = false;
  22556. Laya.isWXOpenDataContext = false;
  22557. Laya.isWXPosMsg = false;
  22558. Laya.__classmap = null;
  22559. Laya.Config = Config;
  22560. Laya.TextRender = TextRender;
  22561. Laya.EventDispatcher = EventDispatcher;
  22562. Laya.SoundChannel = SoundChannel;
  22563. Laya.Stage = Stage;
  22564. Laya.Render = Render;
  22565. Laya.Browser = Browser;
  22566. Laya.Sprite = Sprite;
  22567. Laya.Node = Node;
  22568. Laya.Context = Context;
  22569. Laya.WebGL = WebGL;
  22570. Laya.Handler = Handler;
  22571. Laya.RunDriver = RunDriver;
  22572. Laya.Utils = Utils;
  22573. Laya.Input = Input;
  22574. Laya.Loader = Loader;
  22575. Laya.LocalStorage = LocalStorage;
  22576. Laya.SoundManager = SoundManager;
  22577. Laya.URL = URL;
  22578. Laya.Event = Event;
  22579. Laya.Matrix = Matrix;
  22580. Laya.HTMLImage = HTMLImage;
  22581. Laya.Laya = Laya;
  22582. Laya._evcode = "eva" + "l";
  22583. Laya.isNativeRender_enable = false;
  22584. Laya.__classmap = ILaya.__classMap;
  22585. ILaya.Timer = Timer;
  22586. ILaya.Dragging = Dragging;
  22587. ILaya.GraphicsBounds = GraphicsBounds;
  22588. ILaya.Sprite = Sprite;
  22589. ILaya.TextRender = TextRender;
  22590. ILaya.Loader = Loader;
  22591. ILaya.TTFLoader = TTFLoader;
  22592. ILaya.WebAudioSound = WebAudioSound;
  22593. ILaya.SoundManager = SoundManager;
  22594. ILaya.ShaderCompile = ShaderCompile;
  22595. ILaya.ClassUtils = ClassUtils;
  22596. ILaya.SceneUtils = SceneUtils;
  22597. ILaya.Context = Context;
  22598. ILaya.Render = Render;
  22599. ILaya.MouseManager = MouseManager;
  22600. ILaya.Text = Text;
  22601. ILaya.Browser = Browser;
  22602. ILaya.WebGL = WebGL;
  22603. ILaya.AudioSound = AudioSound;
  22604. ILaya.Pool = Pool;
  22605. ILaya.Utils = Utils;
  22606. ILaya.Graphics = Graphics;
  22607. ILaya.Submit = Submit;
  22608. ILaya.Stage = Stage;
  22609. ILaya.Resource = Resource;
  22610. ILaya.WorkerLoader = WorkerLoader;
  22611. var libs = window._layalibs;
  22612. if (libs) {
  22613. libs.sort(function (a, b) {
  22614. return a.i - b.i;
  22615. });
  22616. for (var j = 0; j < libs.length; j++) {
  22617. libs[j].f(window, window.document, Laya);
  22618. }
  22619. }
  22620. let win = window;
  22621. if (win.Laya) {
  22622. win.Laya.Laya = Laya;
  22623. Object.assign(win.Laya, Laya);
  22624. }
  22625. else
  22626. win.Laya = Laya;
  22627. var __init = Laya.__init;
  22628. var init = Laya.init;
  22629. var version = Laya.version;
  22630. var isWXOpenDataContext;
  22631. var isWXPosMsg;
  22632. var alertGlobalError = Laya.alertGlobalError;
  22633. var enableDebugPanel = Laya.enableDebugPanel;
  22634. function _static(_class, def) {
  22635. for (var i = 0, sz = def.length; i < sz; i += 2) {
  22636. if (def[i] == 'length')
  22637. _class.length = def[i + 1].call(_class);
  22638. else {
  22639. function tmp() {
  22640. var name = def[i];
  22641. var getfn = def[i + 1];
  22642. Object.defineProperty(_class, name, {
  22643. get: function () { delete this[name]; return this[name] = getfn.call(this); },
  22644. set: function (v) { delete this[name]; this[name] = v; }, enumerable: true, configurable: true
  22645. });
  22646. }
  22647. tmp();
  22648. }
  22649. }
  22650. }
  22651. class CommonScript extends Component {
  22652. get isSingleton() {
  22653. return false;
  22654. }
  22655. constructor() {
  22656. super();
  22657. }
  22658. onAwake() {
  22659. }
  22660. onEnable() {
  22661. }
  22662. onStart() {
  22663. }
  22664. onUpdate() {
  22665. }
  22666. onLateUpdate() {
  22667. }
  22668. onDisable() {
  22669. }
  22670. onDestroy() {
  22671. }
  22672. }
  22673. class Script extends Component {
  22674. get isSingleton() {
  22675. return false;
  22676. }
  22677. _onAwake() {
  22678. this.onAwake();
  22679. if (this.onStart !== Script.prototype.onStart) {
  22680. ILaya.startTimer.callLater(this, this.onStart);
  22681. }
  22682. }
  22683. _onEnable() {
  22684. var proto = Script.prototype;
  22685. if (this.onTriggerEnter !== proto.onTriggerEnter) {
  22686. this.owner.on(Event.TRIGGER_ENTER, this, this.onTriggerEnter);
  22687. }
  22688. if (this.onTriggerStay !== proto.onTriggerStay) {
  22689. this.owner.on(Event.TRIGGER_STAY, this, this.onTriggerStay);
  22690. }
  22691. if (this.onTriggerExit !== proto.onTriggerExit) {
  22692. this.owner.on(Event.TRIGGER_EXIT, this, this.onTriggerExit);
  22693. }
  22694. if (this.onMouseDown !== proto.onMouseDown) {
  22695. this.owner.on(Event.MOUSE_DOWN, this, this.onMouseDown);
  22696. }
  22697. if (this.onMouseUp !== proto.onMouseUp) {
  22698. this.owner.on(Event.MOUSE_UP, this, this.onMouseUp);
  22699. }
  22700. if (this.onClick !== proto.onClick) {
  22701. this.owner.on(Event.CLICK, this, this.onClick);
  22702. }
  22703. if (this.onStageMouseDown !== proto.onStageMouseDown) {
  22704. ILaya.stage.on(Event.MOUSE_DOWN, this, this.onStageMouseDown);
  22705. }
  22706. if (this.onStageMouseUp !== proto.onStageMouseUp) {
  22707. ILaya.stage.on(Event.MOUSE_UP, this, this.onStageMouseUp);
  22708. }
  22709. if (this.onStageClick !== proto.onStageClick) {
  22710. ILaya.stage.on(Event.CLICK, this, this.onStageClick);
  22711. }
  22712. if (this.onStageMouseMove !== proto.onStageMouseMove) {
  22713. ILaya.stage.on(Event.MOUSE_MOVE, this, this.onStageMouseMove);
  22714. }
  22715. if (this.onDoubleClick !== proto.onDoubleClick) {
  22716. this.owner.on(Event.DOUBLE_CLICK, this, this.onDoubleClick);
  22717. }
  22718. if (this.onRightClick !== proto.onRightClick) {
  22719. this.owner.on(Event.RIGHT_CLICK, this, this.onRightClick);
  22720. }
  22721. if (this.onMouseMove !== proto.onMouseMove) {
  22722. this.owner.on(Event.MOUSE_MOVE, this, this.onMouseMove);
  22723. }
  22724. if (this.onMouseOver !== proto.onMouseOver) {
  22725. this.owner.on(Event.MOUSE_OVER, this, this.onMouseOver);
  22726. }
  22727. if (this.onMouseOut !== proto.onMouseOut) {
  22728. this.owner.on(Event.MOUSE_OUT, this, this.onMouseOut);
  22729. }
  22730. if (this.onKeyDown !== proto.onKeyDown) {
  22731. ILaya.stage.on(Event.KEY_DOWN, this, this.onKeyDown);
  22732. }
  22733. if (this.onKeyPress !== proto.onKeyPress) {
  22734. ILaya.stage.on(Event.KEY_PRESS, this, this.onKeyPress);
  22735. }
  22736. if (this.onKeyUp !== proto.onKeyUp) {
  22737. ILaya.stage.on(Event.KEY_UP, this, this.onKeyUp);
  22738. }
  22739. if (this.onUpdate !== proto.onUpdate) {
  22740. ILaya.updateTimer.frameLoop(1, this, this.onUpdate);
  22741. }
  22742. if (this.onLateUpdate !== proto.onLateUpdate) {
  22743. ILaya.lateTimer.frameLoop(1, this, this.onLateUpdate);
  22744. }
  22745. if (this.onPreRender !== proto.onPreRender) {
  22746. ILaya.lateTimer.frameLoop(1, this, this.onPreRender);
  22747. }
  22748. this.onEnable();
  22749. }
  22750. _onDisable() {
  22751. this.owner.offAllCaller(this);
  22752. ILaya.stage.offAllCaller(this);
  22753. ILaya.startTimer.clearAll(this);
  22754. ILaya.updateTimer.clearAll(this);
  22755. ILaya.lateTimer.clearAll(this);
  22756. }
  22757. _isScript() {
  22758. return true;
  22759. }
  22760. _onDestroy() {
  22761. this.onDestroy();
  22762. }
  22763. onAwake() {
  22764. }
  22765. onEnable() {
  22766. }
  22767. onStart() {
  22768. }
  22769. onTriggerEnter(other, self, contact) {
  22770. }
  22771. onTriggerStay(other, self, contact) {
  22772. }
  22773. onTriggerExit(other, self, contact) {
  22774. }
  22775. onMouseDown(e) {
  22776. }
  22777. onMouseUp(e) {
  22778. }
  22779. onClick(e) {
  22780. }
  22781. onStageMouseDown(e) {
  22782. }
  22783. onStageMouseUp(e) {
  22784. }
  22785. onStageClick(e) {
  22786. }
  22787. onStageMouseMove(e) {
  22788. }
  22789. onDoubleClick(e) {
  22790. }
  22791. onRightClick(e) {
  22792. }
  22793. onMouseMove(e) {
  22794. }
  22795. onMouseOver(e) {
  22796. }
  22797. onMouseOut(e) {
  22798. }
  22799. onKeyDown(e) {
  22800. }
  22801. onKeyPress(e) {
  22802. }
  22803. onKeyUp(e) {
  22804. }
  22805. onUpdate() {
  22806. }
  22807. onLateUpdate() {
  22808. }
  22809. onPreRender() {
  22810. }
  22811. onPostRender() {
  22812. }
  22813. onDisable() {
  22814. }
  22815. onDestroy() {
  22816. }
  22817. }
  22818. class GraphicAnimation extends FrameAnimation {
  22819. constructor() {
  22820. super(...arguments);
  22821. this._nodeIDAniDic = {};
  22822. }
  22823. _parseNodeList(uiView) {
  22824. if (!this._nodeList)
  22825. this._nodeList = [];
  22826. this._nodeDefaultProps[uiView.compId] = uiView.props;
  22827. if (uiView.compId)
  22828. this._nodeList.push(uiView.compId);
  22829. var childs = uiView.child;
  22830. if (childs) {
  22831. var i, len = childs.length;
  22832. for (i = 0; i < len; i++) {
  22833. this._parseNodeList(childs[i]);
  22834. }
  22835. }
  22836. }
  22837. _calGraphicData(aniData) {
  22838. this._setUp(null, aniData);
  22839. this._createGraphicData();
  22840. if (this._nodeIDAniDic) {
  22841. var key;
  22842. for (key in this._nodeIDAniDic) {
  22843. this._nodeIDAniDic[key] = null;
  22844. }
  22845. }
  22846. }
  22847. _createGraphicData() {
  22848. var gList = [];
  22849. var i, len = this.count;
  22850. var animationDataNew = this._usedFrames;
  22851. if (!animationDataNew)
  22852. animationDataNew = [];
  22853. var preGraphic;
  22854. for (i = 0; i < len; i++) {
  22855. if (animationDataNew[i] || !preGraphic) {
  22856. preGraphic = this._createFrameGraphic(i);
  22857. }
  22858. gList.push(preGraphic);
  22859. }
  22860. this._gList = gList;
  22861. }
  22862. _createFrameGraphic(frame) {
  22863. var g = new Graphics();
  22864. if (!GraphicAnimation._rootMatrix)
  22865. GraphicAnimation._rootMatrix = new Matrix();
  22866. this._updateNodeGraphic(this._rootNode, frame, GraphicAnimation._rootMatrix, g);
  22867. return g;
  22868. }
  22869. _updateNodeGraphic(node, frame, parentTransfrom, g, alpha = 1) {
  22870. var tNodeG;
  22871. tNodeG = this._nodeGDic[node.compId] = this._getNodeGraphicData(node.compId, frame, this._nodeGDic[node.compId]);
  22872. if (!tNodeG.resultTransform)
  22873. tNodeG.resultTransform = new Matrix();
  22874. var tResultTransform;
  22875. tResultTransform = tNodeG.resultTransform;
  22876. Matrix.mul(tNodeG.transform, parentTransfrom, tResultTransform);
  22877. var tTex;
  22878. var tGraphicAlpha = tNodeG.alpha * alpha;
  22879. if (tGraphicAlpha < 0.01)
  22880. return;
  22881. if (tNodeG.skin) {
  22882. tTex = this._getTextureByUrl(tNodeG.skin);
  22883. if (tTex) {
  22884. if (tResultTransform._checkTransform()) {
  22885. g.drawTexture(tTex, 0, 0, tNodeG.width, tNodeG.height, tResultTransform, tGraphicAlpha);
  22886. tNodeG.resultTransform = null;
  22887. }
  22888. else {
  22889. g.drawTexture(tTex, tResultTransform.tx, tResultTransform.ty, tNodeG.width, tNodeG.height, null, tGraphicAlpha);
  22890. }
  22891. }
  22892. }
  22893. var childs = node.child;
  22894. if (!childs)
  22895. return;
  22896. var i, len;
  22897. len = childs.length;
  22898. for (i = 0; i < len; i++) {
  22899. this._updateNodeGraphic(childs[i], frame, tResultTransform, g, tGraphicAlpha);
  22900. }
  22901. }
  22902. _updateNoChilds(tNodeG, g) {
  22903. if (!tNodeG.skin)
  22904. return;
  22905. var tTex = this._getTextureByUrl(tNodeG.skin);
  22906. if (!tTex)
  22907. return;
  22908. var tTransform = tNodeG.transform;
  22909. tTransform._checkTransform();
  22910. var onlyTranslate;
  22911. onlyTranslate = !tTransform._bTransform;
  22912. if (!onlyTranslate) {
  22913. g.drawTexture(tTex, 0, 0, tNodeG.width, tNodeG.height, tTransform.clone(), tNodeG.alpha);
  22914. }
  22915. else {
  22916. g.drawTexture(tTex, tTransform.tx, tTransform.ty, tNodeG.width, tNodeG.height, null, tNodeG.alpha);
  22917. }
  22918. }
  22919. _updateNodeGraphic2(node, frame, g) {
  22920. var tNodeG;
  22921. tNodeG = this._nodeGDic[node.compId] = this._getNodeGraphicData(node.compId, frame, this._nodeGDic[node.compId]);
  22922. if (!node.child) {
  22923. this._updateNoChilds(tNodeG, g);
  22924. return;
  22925. }
  22926. var tTransform = tNodeG.transform;
  22927. tTransform._checkTransform();
  22928. var onlyTranslate;
  22929. onlyTranslate = !tTransform._bTransform;
  22930. var hasTrans;
  22931. hasTrans = onlyTranslate && (tTransform.tx != 0 || tTransform.ty != 0);
  22932. var ifSave;
  22933. ifSave = (tTransform._bTransform) || tNodeG.alpha != 1;
  22934. if (ifSave)
  22935. g.save();
  22936. if (tNodeG.alpha != 1)
  22937. g.alpha(tNodeG.alpha);
  22938. if (!onlyTranslate)
  22939. g.transform(tTransform.clone());
  22940. else if (hasTrans)
  22941. g.translate(tTransform.tx, tTransform.ty);
  22942. var childs = node.child;
  22943. var tTex;
  22944. if (tNodeG.skin) {
  22945. tTex = this._getTextureByUrl(tNodeG.skin);
  22946. if (tTex) {
  22947. g.drawImage(tTex, 0, 0, tNodeG.width, tNodeG.height);
  22948. }
  22949. }
  22950. if (childs) {
  22951. var i, len;
  22952. len = childs.length;
  22953. for (i = 0; i < len; i++) {
  22954. this._updateNodeGraphic2(childs[i], frame, g);
  22955. }
  22956. }
  22957. if (ifSave) {
  22958. g.restore();
  22959. }
  22960. else {
  22961. if (!onlyTranslate) {
  22962. g.transform(tTransform.clone().invert());
  22963. }
  22964. else if (hasTrans) {
  22965. g.translate(-tTransform.tx, -tTransform.ty);
  22966. }
  22967. }
  22968. }
  22969. _calculateKeyFrames(node) {
  22970. super._calculateKeyFrames(node);
  22971. this._nodeIDAniDic[node.target] = node;
  22972. }
  22973. getNodeDataByID(nodeID) {
  22974. return this._nodeIDAniDic[nodeID];
  22975. }
  22976. _getParams(obj, params, frame, obj2) {
  22977. var rst = GraphicAnimation._temParam;
  22978. rst.length = params.length;
  22979. var i, len = params.length;
  22980. for (i = 0; i < len; i++) {
  22981. rst[i] = this._getObjVar(obj, params[i][0], frame, params[i][1], obj2);
  22982. }
  22983. return rst;
  22984. }
  22985. _getObjVar(obj, key, frame, noValue, obj2) {
  22986. if (key in obj) {
  22987. var vArr = obj[key];
  22988. if (frame >= vArr.length)
  22989. frame = vArr.length - 1;
  22990. return obj[key][frame];
  22991. }
  22992. if (key in obj2) {
  22993. return obj2[key];
  22994. }
  22995. return noValue;
  22996. }
  22997. _getNodeGraphicData(nodeID, frame, rst) {
  22998. if (!rst)
  22999. rst = new GraphicNode();
  23000. if (!rst.transform) {
  23001. rst.transform = new Matrix();
  23002. }
  23003. else {
  23004. rst.transform.identity();
  23005. }
  23006. var node = this.getNodeDataByID(nodeID);
  23007. if (!node)
  23008. return rst;
  23009. var frameData = node.frames;
  23010. var params = this._getParams(frameData, GraphicAnimation._drawTextureCmd, frame, this._nodeDefaultProps[nodeID]);
  23011. var url = params[0];
  23012. var width, height;
  23013. var px = params[5], py = params[6];
  23014. var aX = params[13], aY = params[14];
  23015. var sx = params[7], sy = params[8];
  23016. var rotate = params[9];
  23017. var skewX = params[11], skewY = params[12];
  23018. width = params[3];
  23019. height = params[4];
  23020. if (width == 0 || height == 0)
  23021. url = null;
  23022. if (width == -1)
  23023. width = 0;
  23024. if (height == -1)
  23025. height = 0;
  23026. var tex;
  23027. rst.skin = url;
  23028. rst.width = width;
  23029. rst.height = height;
  23030. if (url) {
  23031. tex = this._getTextureByUrl(url);
  23032. if (tex) {
  23033. if (!width)
  23034. width = tex.sourceWidth;
  23035. if (!height)
  23036. height = tex.sourceHeight;
  23037. }
  23038. else {
  23039. console.warn("lost skin:", url, ",you may load pics first");
  23040. }
  23041. }
  23042. rst.alpha = params[10];
  23043. var m = rst.transform;
  23044. if (aX != 0) {
  23045. px = aX * width;
  23046. }
  23047. if (aY != 0) {
  23048. py = aY * height;
  23049. }
  23050. if (px != 0 || py != 0) {
  23051. m.translate(-px, -py);
  23052. }
  23053. var tm = null;
  23054. if (rotate || sx !== 1 || sy !== 1 || skewX || skewY) {
  23055. tm = GraphicAnimation._tempMt;
  23056. tm.identity();
  23057. tm._bTransform = true;
  23058. var skx = (rotate - skewX) * 0.0174532922222222;
  23059. var sky = (rotate + skewY) * 0.0174532922222222;
  23060. var cx = Math.cos(sky);
  23061. var ssx = Math.sin(sky);
  23062. var cy = Math.sin(skx);
  23063. var ssy = Math.cos(skx);
  23064. tm.a = sx * cx;
  23065. tm.b = sx * ssx;
  23066. tm.c = -sy * cy;
  23067. tm.d = sy * ssy;
  23068. tm.tx = tm.ty = 0;
  23069. }
  23070. if (tm) {
  23071. m = Matrix.mul(m, tm, m);
  23072. }
  23073. m.translate(params[1], params[2]);
  23074. return rst;
  23075. }
  23076. _getTextureByUrl(url) {
  23077. return Loader.getRes(url);
  23078. }
  23079. setAniData(uiView, aniName = null) {
  23080. if (uiView.animations) {
  23081. this._nodeDefaultProps = {};
  23082. this._nodeGDic = {};
  23083. if (this._nodeList)
  23084. this._nodeList.length = 0;
  23085. this._rootNode = uiView;
  23086. this._parseNodeList(uiView);
  23087. var aniDic = {};
  23088. var anilist = [];
  23089. var animations = uiView.animations;
  23090. var i, len = animations.length;
  23091. var tAniO;
  23092. for (i = 0; i < len; i++) {
  23093. tAniO = animations[i];
  23094. this._labels = null;
  23095. if (aniName && aniName != tAniO.name) {
  23096. continue;
  23097. }
  23098. if (!tAniO)
  23099. continue;
  23100. try {
  23101. this._calGraphicData(tAniO);
  23102. }
  23103. catch (e) {
  23104. console.warn("parse animation fail:" + tAniO.name + ",empty animation created");
  23105. this._gList = [];
  23106. }
  23107. var frameO = {};
  23108. frameO.interval = 1000 / tAniO["frameRate"];
  23109. frameO.frames = this._gList;
  23110. frameO.labels = this._labels;
  23111. frameO.name = tAniO.name;
  23112. anilist.push(frameO);
  23113. aniDic[tAniO.name] = frameO;
  23114. }
  23115. this.animationList = anilist;
  23116. this.animationDic = aniDic;
  23117. }
  23118. GraphicAnimation._temParam.length = 0;
  23119. }
  23120. parseByData(aniData) {
  23121. var rootNode, aniO;
  23122. rootNode = aniData.nodeRoot;
  23123. aniO = aniData.aniO;
  23124. delete aniData.nodeRoot;
  23125. delete aniData.aniO;
  23126. this._nodeDefaultProps = {};
  23127. this._nodeGDic = {};
  23128. if (this._nodeList)
  23129. this._nodeList.length = 0;
  23130. this._rootNode = rootNode;
  23131. this._parseNodeList(rootNode);
  23132. this._labels = null;
  23133. try {
  23134. this._calGraphicData(aniO);
  23135. }
  23136. catch (e) {
  23137. console.warn("parse animation fail:" + aniO.name + ",empty animation created");
  23138. this._gList = [];
  23139. }
  23140. var frameO = aniData;
  23141. frameO.interval = 1000 / aniO["frameRate"];
  23142. frameO.frames = this._gList;
  23143. frameO.labels = this._labels;
  23144. frameO.name = aniO.name;
  23145. return frameO;
  23146. }
  23147. setUpAniData(uiView) {
  23148. if (uiView.animations) {
  23149. var aniDic = {};
  23150. var anilist = [];
  23151. var animations = uiView.animations;
  23152. var i, len = animations.length;
  23153. var tAniO;
  23154. for (i = 0; i < len; i++) {
  23155. tAniO = animations[i];
  23156. if (!tAniO)
  23157. continue;
  23158. var frameO = {};
  23159. frameO.name = tAniO.name;
  23160. frameO.aniO = tAniO;
  23161. frameO.nodeRoot = uiView;
  23162. anilist.push(frameO);
  23163. aniDic[tAniO.name] = frameO;
  23164. }
  23165. this.animationList = anilist;
  23166. this.animationDic = aniDic;
  23167. }
  23168. }
  23169. _clear() {
  23170. this.animationList = null;
  23171. this.animationDic = null;
  23172. this._gList = null;
  23173. this._nodeGDic = null;
  23174. }
  23175. static parseAnimationByData(animationObject) {
  23176. if (!GraphicAnimation._I)
  23177. GraphicAnimation._I = new GraphicAnimation();
  23178. var rst;
  23179. rst = GraphicAnimation._I.parseByData(animationObject);
  23180. GraphicAnimation._I._clear();
  23181. return rst;
  23182. }
  23183. static parseAnimationData(aniData) {
  23184. if (!GraphicAnimation._I)
  23185. GraphicAnimation._I = new GraphicAnimation();
  23186. GraphicAnimation._I.setUpAniData(aniData);
  23187. var rst;
  23188. rst = {};
  23189. rst.animationList = GraphicAnimation._I.animationList;
  23190. rst.animationDic = GraphicAnimation._I.animationDic;
  23191. GraphicAnimation._I._clear();
  23192. return rst;
  23193. }
  23194. }
  23195. GraphicAnimation._drawTextureCmd = [["skin", null], ["x", 0], ["y", 0], ["width", -1], ["height", -1], ["pivotX", 0], ["pivotY", 0], ["scaleX", 1], ["scaleY", 1], ["rotation", 0], ["alpha", 1], ["skewX", 0], ["skewY", 0], ["anchorX", 0], ["anchorY", 0]];
  23196. GraphicAnimation._temParam = [];
  23197. GraphicAnimation._tempMt = new Matrix();
  23198. class GraphicNode {
  23199. constructor() {
  23200. this.alpha = 1;
  23201. }
  23202. }
  23203. class Animation extends AnimationBase {
  23204. constructor() {
  23205. super();
  23206. this._setControlNode(this);
  23207. }
  23208. destroy(destroyChild = true) {
  23209. this.stop();
  23210. super.destroy(destroyChild);
  23211. this._frames = null;
  23212. this._labels = null;
  23213. }
  23214. play(start = 0, loop = true, name = "") {
  23215. if (name)
  23216. this._setFramesFromCache(name, true);
  23217. super.play(start, loop, name);
  23218. }
  23219. _setFramesFromCache(name, showWarn = false) {
  23220. if (this._url)
  23221. name = this._url + "#" + name;
  23222. if (name && Animation.framesMap[name]) {
  23223. var tAniO = Animation.framesMap[name];
  23224. if (tAniO instanceof Array) {
  23225. this._frames = Animation.framesMap[name];
  23226. this._count = this._frames.length;
  23227. }
  23228. else {
  23229. if (tAniO.nodeRoot) {
  23230. Animation.framesMap[name] = GraphicAnimation.parseAnimationByData(tAniO);
  23231. tAniO = Animation.framesMap[name];
  23232. }
  23233. this._frames = tAniO.frames;
  23234. this._count = this._frames.length;
  23235. if (!this._frameRateChanged)
  23236. this._interval = tAniO.interval;
  23237. this._labels = this._copyLabels(tAniO.labels);
  23238. }
  23239. return true;
  23240. }
  23241. else {
  23242. if (showWarn)
  23243. console.log("ani not found:", name);
  23244. }
  23245. return false;
  23246. }
  23247. _copyLabels(labels) {
  23248. if (!labels)
  23249. return null;
  23250. var rst;
  23251. rst = {};
  23252. var key;
  23253. for (key in labels) {
  23254. rst[key] = Utils.copyArray([], labels[key]);
  23255. }
  23256. return rst;
  23257. }
  23258. _frameLoop() {
  23259. if (this._visible && this._style.alpha > 0.01 && this._frames) {
  23260. super._frameLoop();
  23261. }
  23262. }
  23263. _displayToIndex(value) {
  23264. if (this._frames)
  23265. this.graphics = this._frames[value];
  23266. }
  23267. get frames() {
  23268. return this._frames;
  23269. }
  23270. set frames(value) {
  23271. this._frames = value;
  23272. if (value) {
  23273. this._count = value.length;
  23274. if (this._actionName)
  23275. this._setFramesFromCache(this._actionName, true);
  23276. this.index = this._index;
  23277. }
  23278. }
  23279. set source(value) {
  23280. if (value.indexOf(".ani") > -1)
  23281. this.loadAnimation(value);
  23282. else if (value.indexOf(".json") > -1 || value.indexOf("als") > -1 || value.indexOf("atlas") > -1)
  23283. this.loadAtlas(value);
  23284. else
  23285. this.loadImages(value.split(","));
  23286. }
  23287. set autoAnimation(value) {
  23288. this.play(0, true, value);
  23289. }
  23290. set autoPlay(value) {
  23291. if (value)
  23292. this.play();
  23293. else
  23294. this.stop();
  23295. }
  23296. clear() {
  23297. super.clear();
  23298. this.stop();
  23299. this.graphics = null;
  23300. this._frames = null;
  23301. this._labels = null;
  23302. return this;
  23303. }
  23304. loadImages(urls, cacheName = "") {
  23305. this._url = "";
  23306. if (!this._setFramesFromCache(cacheName)) {
  23307. this.frames = Animation.framesMap[cacheName] ? Animation.framesMap[cacheName] : Animation.createFrames(urls, cacheName);
  23308. }
  23309. return this;
  23310. }
  23311. loadAtlas(url, loaded = null, cacheName = "") {
  23312. this._url = "";
  23313. var _this = this;
  23314. if (!_this._setFramesFromCache(cacheName)) {
  23315. function onLoaded(loadUrl) {
  23316. if (url === loadUrl) {
  23317. _this.frames = Animation.framesMap[cacheName] ? Animation.framesMap[cacheName] : Animation.createFrames(url, cacheName);
  23318. if (loaded)
  23319. loaded.run();
  23320. }
  23321. }
  23322. if (Loader.getAtlas(url))
  23323. onLoaded(url);
  23324. else
  23325. ILaya.loader.load(url, Handler.create(null, onLoaded, [url]), null, Loader.ATLAS);
  23326. }
  23327. return this;
  23328. }
  23329. loadAnimation(url, loaded = null, atlas = null) {
  23330. this._url = url;
  23331. var _this = this;
  23332. if (!this._actionName)
  23333. this._actionName = "";
  23334. if (!_this._setFramesFromCache(this._actionName)) {
  23335. if (!atlas || Loader.getAtlas(atlas)) {
  23336. this._loadAnimationData(url, loaded, atlas);
  23337. }
  23338. else {
  23339. ILaya.loader.load(atlas, Handler.create(this, this._loadAnimationData, [url, loaded, atlas]), null, Loader.ATLAS);
  23340. }
  23341. }
  23342. else {
  23343. _this._setFramesFromCache(this._actionName, true);
  23344. this.index = 0;
  23345. if (loaded)
  23346. loaded.run();
  23347. }
  23348. return this;
  23349. }
  23350. _loadAnimationData(url, loaded = null, atlas = null) {
  23351. if (atlas && !Loader.getAtlas(atlas)) {
  23352. console.warn("atlas load fail:" + atlas);
  23353. return;
  23354. }
  23355. var _this = this;
  23356. function onLoaded(loadUrl) {
  23357. if (!Loader.getRes(loadUrl)) {
  23358. if (Animation.framesMap[url + "#"]) {
  23359. _this._setFramesFromCache(_this._actionName, true);
  23360. _this.index = 0;
  23361. _this._resumePlay();
  23362. if (loaded)
  23363. loaded.run();
  23364. }
  23365. return;
  23366. }
  23367. if (url === loadUrl) {
  23368. var tAniO;
  23369. if (!Animation.framesMap[url + "#"]) {
  23370. var aniData = GraphicAnimation.parseAnimationData(Loader.getRes(url));
  23371. if (!aniData)
  23372. return;
  23373. var aniList = aniData.animationList;
  23374. var i, len = aniList.length;
  23375. var defaultO;
  23376. for (i = 0; i < len; i++) {
  23377. tAniO = aniList[i];
  23378. Animation.framesMap[url + "#" + tAniO.name] = tAniO;
  23379. if (!defaultO)
  23380. defaultO = tAniO;
  23381. }
  23382. if (defaultO) {
  23383. Animation.framesMap[url + "#"] = defaultO;
  23384. _this._setFramesFromCache(_this._actionName, true);
  23385. _this.index = 0;
  23386. }
  23387. _this._resumePlay();
  23388. }
  23389. else {
  23390. _this._setFramesFromCache(_this._actionName, true);
  23391. _this.index = 0;
  23392. _this._resumePlay();
  23393. }
  23394. if (loaded)
  23395. loaded.run();
  23396. }
  23397. Loader.clearRes(url);
  23398. }
  23399. if (Loader.getRes(url))
  23400. onLoaded(url);
  23401. else
  23402. ILaya.loader.load(url, Handler.create(null, onLoaded, [url]), null, Loader.JSON);
  23403. }
  23404. static createFrames(url, name) {
  23405. var arr;
  23406. if (typeof (url) == 'string') {
  23407. var atlas = Loader.getAtlas(url);
  23408. if (atlas && atlas.length) {
  23409. arr = [];
  23410. for (var i = 0, n = atlas.length; i < n; i++) {
  23411. var g = new Graphics();
  23412. g.drawImage(Loader.getRes(atlas[i]), 0, 0);
  23413. arr.push(g);
  23414. }
  23415. }
  23416. }
  23417. else if (url instanceof Array) {
  23418. arr = [];
  23419. for (i = 0, n = url.length; i < n; i++) {
  23420. g = new Graphics();
  23421. g.loadImage(url[i], 0, 0);
  23422. arr.push(g);
  23423. }
  23424. }
  23425. if (name)
  23426. Animation.framesMap[name] = arr;
  23427. return arr;
  23428. }
  23429. static clearCache(key) {
  23430. var cache = Animation.framesMap;
  23431. var val;
  23432. var key2 = key + "#";
  23433. for (val in cache) {
  23434. if (val === key || val.indexOf(key2) === 0) {
  23435. delete Animation.framesMap[val];
  23436. }
  23437. }
  23438. }
  23439. }
  23440. Animation.framesMap = {};
  23441. ILaya.regClass(Animation);
  23442. ClassUtils.regClass("laya.display.Animation", Animation);
  23443. ClassUtils.regClass("Laya.Animation", Animation);
  23444. class EffectAnimation extends FrameAnimation {
  23445. constructor() {
  23446. super(...arguments);
  23447. this._initData = {};
  23448. }
  23449. set target(v) {
  23450. if (this._target)
  23451. this._target.off(EffectAnimation.EFFECT_BEGIN, this, this._onOtherBegin);
  23452. this._target = v;
  23453. if (this._target)
  23454. this._target.on(EffectAnimation.EFFECT_BEGIN, this, this._onOtherBegin);
  23455. this._addEvent();
  23456. }
  23457. get target() {
  23458. return this._target;
  23459. }
  23460. _onOtherBegin(effect) {
  23461. if (effect === this)
  23462. return;
  23463. this.stop();
  23464. }
  23465. set playEvent(event) {
  23466. this._playEvent = event;
  23467. if (!event)
  23468. return;
  23469. this._addEvent();
  23470. }
  23471. _addEvent() {
  23472. if (!this._target || !this._playEvent)
  23473. return;
  23474. this._setControlNode(this._target);
  23475. this._target.on(this._playEvent, this, this._onPlayAction);
  23476. }
  23477. _onPlayAction() {
  23478. this.play(0, false);
  23479. }
  23480. play(start = 0, loop = true, name = "") {
  23481. if (!this._target)
  23482. return;
  23483. this._target.event(EffectAnimation.EFFECT_BEGIN, [this]);
  23484. this._recordInitData();
  23485. super.play(start, loop, name);
  23486. }
  23487. _recordInitData() {
  23488. if (!this._aniKeys)
  23489. return;
  23490. var i, len;
  23491. len = this._aniKeys.length;
  23492. var key;
  23493. for (i = 0; i < len; i++) {
  23494. key = this._aniKeys[i];
  23495. this._initData[key] = this._target[key];
  23496. }
  23497. }
  23498. set effectClass(classStr) {
  23499. this._effectClass = ClassUtils.getClass(classStr);
  23500. if (this._effectClass) {
  23501. var uiData = this._effectClass["uiView"];
  23502. if (uiData) {
  23503. var aniData = uiData["animations"];
  23504. if (aniData && aniData[0]) {
  23505. var data = aniData[0];
  23506. this._setUp({}, data);
  23507. if (data.nodes && data.nodes[0]) {
  23508. this._aniKeys = data.nodes[0].keys;
  23509. }
  23510. }
  23511. }
  23512. }
  23513. }
  23514. set effectData(uiData) {
  23515. if (uiData) {
  23516. var aniData = uiData["animations"];
  23517. if (aniData && aniData[0]) {
  23518. var data = aniData[0];
  23519. this._setUp({}, data);
  23520. if (data.nodes && data.nodes[0]) {
  23521. this._aniKeys = data.nodes[0].keys;
  23522. }
  23523. }
  23524. }
  23525. }
  23526. _displayToIndex(value) {
  23527. if (!this._animationData)
  23528. return;
  23529. if (value < 0)
  23530. value = 0;
  23531. if (value > this._count)
  23532. value = this._count;
  23533. var nodes = this._animationData.nodes, i, len = nodes.length;
  23534. len = len > 1 ? 1 : len;
  23535. for (i = 0; i < len; i++) {
  23536. this._displayNodeToFrame(nodes[i], value);
  23537. }
  23538. }
  23539. _displayNodeToFrame(node, frame, targetDic = null) {
  23540. if (!this._target)
  23541. return;
  23542. var target = this._target;
  23543. var frames = node.frames, key, propFrames, value;
  23544. var keys = node.keys, i, len = keys.length;
  23545. var secondFrames = node.secondFrames;
  23546. var tSecondFrame;
  23547. var easeFun;
  23548. var tKeyFrames;
  23549. var startFrame;
  23550. var endFrame;
  23551. for (i = 0; i < len; i++) {
  23552. key = keys[i];
  23553. propFrames = frames[key];
  23554. tSecondFrame = secondFrames[key];
  23555. if (tSecondFrame == -1) {
  23556. value = this._initData[key];
  23557. }
  23558. else {
  23559. if (frame < tSecondFrame) {
  23560. tKeyFrames = node.keyframes[key];
  23561. startFrame = tKeyFrames[0];
  23562. if (startFrame.tween) {
  23563. easeFun = Ease[startFrame.tweenMethod];
  23564. if (easeFun == null)
  23565. easeFun = Ease.linearNone;
  23566. endFrame = tKeyFrames[1];
  23567. value = easeFun(frame, this._initData[key], endFrame.value - this._initData[key], endFrame.index);
  23568. }
  23569. else {
  23570. value = this._initData[key];
  23571. }
  23572. }
  23573. else {
  23574. if (propFrames.length > frame)
  23575. value = propFrames[frame];
  23576. else
  23577. value = propFrames[propFrames.length - 1];
  23578. }
  23579. }
  23580. target[key] = value;
  23581. }
  23582. }
  23583. _calculateKeyFrames(node) {
  23584. super._calculateKeyFrames(node);
  23585. var keyFrames = node.keyframes, key, tKeyFrames, target = node.target;
  23586. var secondFrames = {};
  23587. node.secondFrames = secondFrames;
  23588. for (key in keyFrames) {
  23589. tKeyFrames = keyFrames[key];
  23590. if (tKeyFrames.length <= 1)
  23591. secondFrames[key] = -1;
  23592. else
  23593. secondFrames[key] = tKeyFrames[1].index;
  23594. }
  23595. }
  23596. }
  23597. EffectAnimation.EFFECT_BEGIN = "effectbegin";
  23598. ClassUtils.regClass("laya.display.EffectAnimation", EffectAnimation);
  23599. ClassUtils.regClass("Laya.EffectAnimation", EffectAnimation);
  23600. class SceneLoader extends EventDispatcher {
  23601. constructor() {
  23602. super();
  23603. this._completeHandler = new Handler(this, this.onOneLoadComplete);
  23604. this.reset();
  23605. }
  23606. reset() {
  23607. this._toLoadList = [];
  23608. this._isLoading = false;
  23609. this.totalCount = 0;
  23610. }
  23611. get leftCount() {
  23612. if (this._isLoading)
  23613. return this._toLoadList.length + 1;
  23614. return this._toLoadList.length;
  23615. }
  23616. get loadedCount() {
  23617. return this.totalCount - this.leftCount;
  23618. }
  23619. load(url, is3D = false, ifCheck = true) {
  23620. if (url instanceof Array) {
  23621. var i, len;
  23622. len = url.length;
  23623. for (i = 0; i < len; i++) {
  23624. this._addToLoadList(url[i], is3D);
  23625. }
  23626. }
  23627. else {
  23628. this._addToLoadList(url, is3D);
  23629. }
  23630. if (ifCheck)
  23631. this._checkNext();
  23632. }
  23633. _addToLoadList(url, is3D = false) {
  23634. if (this._toLoadList.indexOf(url) >= 0)
  23635. return;
  23636. if (Loader.getRes(url))
  23637. return;
  23638. if (is3D) {
  23639. this._toLoadList.push({ url: url });
  23640. }
  23641. else
  23642. this._toLoadList.push(url);
  23643. this.totalCount++;
  23644. }
  23645. _checkNext() {
  23646. if (!this._isLoading) {
  23647. if (this._toLoadList.length == 0) {
  23648. this.event(Event.COMPLETE);
  23649. return;
  23650. }
  23651. var tItem;
  23652. tItem = this._toLoadList.pop();
  23653. if (typeof (tItem) == 'string') {
  23654. this.loadOne(tItem);
  23655. }
  23656. else {
  23657. this.loadOne(tItem.url, true);
  23658. }
  23659. }
  23660. }
  23661. loadOne(url, is3D = false) {
  23662. this._curUrl = url;
  23663. var type = Utils.getFileExtension(this._curUrl);
  23664. if (is3D) {
  23665. ILaya.loader.create(url, this._completeHandler);
  23666. }
  23667. else if (SceneLoader.LoadableExtensions[type]) {
  23668. ILaya.loader.load(url, this._completeHandler, null, SceneLoader.LoadableExtensions[type]);
  23669. }
  23670. else if (url != AtlasInfoManager.getFileLoadPath(url) || SceneLoader.No3dLoadTypes[type] || !LoaderManager.createMap[type]) {
  23671. ILaya.loader.load(url, this._completeHandler);
  23672. }
  23673. else {
  23674. ILaya.loader.create(url, this._completeHandler);
  23675. }
  23676. }
  23677. onOneLoadComplete() {
  23678. this._isLoading = false;
  23679. if (!Loader.getRes(this._curUrl)) {
  23680. console.log("Fail to load:", this._curUrl);
  23681. }
  23682. var type = Utils.getFileExtension(this._curUrl);
  23683. if (SceneLoader.LoadableExtensions[type]) {
  23684. var dataO;
  23685. dataO = Loader.getRes(this._curUrl);
  23686. if (dataO && (dataO instanceof Prefab)) {
  23687. dataO = dataO.json;
  23688. }
  23689. if (dataO) {
  23690. if (dataO.loadList) {
  23691. this.load(dataO.loadList, false, false);
  23692. }
  23693. if (dataO.loadList3D) {
  23694. this.load(dataO.loadList3D, true, false);
  23695. }
  23696. }
  23697. }
  23698. if (type == "sk") {
  23699. this.load(this._curUrl.replace(".sk", ".png"), false, false);
  23700. }
  23701. this.event(Event.PROGRESS, this.getProgress());
  23702. this._checkNext();
  23703. }
  23704. getProgress() {
  23705. return this.loadedCount / this.totalCount;
  23706. }
  23707. }
  23708. SceneLoader.LoadableExtensions = { "scene": Loader.JSON, "scene3d": Loader.JSON, "ani": Loader.JSON, "ui": Loader.JSON, "prefab": Loader.PREFAB };
  23709. SceneLoader.No3dLoadTypes = { "png": true, "jpg": true, "txt": true };
  23710. class Scene extends Sprite {
  23711. constructor(createChildren = true) {
  23712. super();
  23713. this.autoDestroyAtClosed = false;
  23714. this.url = null;
  23715. this._viewCreated = false;
  23716. this._idMap = null;
  23717. this._$componentType = "Scene";
  23718. Scene.unDestroyedScenes.push(this);
  23719. this._scene = this;
  23720. if (createChildren)
  23721. this.createChildren();
  23722. }
  23723. createChildren() {
  23724. }
  23725. static setUIMap(url) {
  23726. let uimap = ILaya.loader.getRes(url);
  23727. if (uimap) {
  23728. for (let key in uimap) {
  23729. ILaya.Loader.loadedMap[URL.formatURL(key + ".scene")] = uimap[key];
  23730. }
  23731. }
  23732. else {
  23733. throw "请提前加载uimap的json,再使用该接口设置!";
  23734. }
  23735. }
  23736. loadScene(path) {
  23737. var url = path.indexOf(".") > -1 ? path : path + ".scene";
  23738. var view = ILaya.loader.getRes(url);
  23739. if (view) {
  23740. this.createView(view);
  23741. }
  23742. else {
  23743. this._setBit(Const.NOT_READY, true);
  23744. ILaya.loader.resetProgress();
  23745. var loader = new SceneLoader();
  23746. loader.on(Event.COMPLETE, this, this._onSceneLoaded, [url]);
  23747. loader.load(url);
  23748. }
  23749. }
  23750. _onSceneLoaded(url) {
  23751. this.createView(ILaya.Loader.getRes(url));
  23752. }
  23753. createView(view) {
  23754. if (view && !this._viewCreated) {
  23755. this._viewCreated = true;
  23756. SceneUtils.createByData(this, view);
  23757. }
  23758. }
  23759. getNodeByID(id) {
  23760. if (this._idMap)
  23761. return this._idMap[id];
  23762. return null;
  23763. }
  23764. open(closeOther = true, param = null) {
  23765. if (closeOther)
  23766. Scene.closeAll();
  23767. Scene.root.addChild(this);
  23768. this.onOpened(param);
  23769. }
  23770. onOpened(param) {
  23771. }
  23772. close(type = null) {
  23773. this.onClosed(type);
  23774. if (this.autoDestroyAtClosed)
  23775. this.destroy();
  23776. else
  23777. this.removeSelf();
  23778. }
  23779. onClosed(type = null) {
  23780. }
  23781. destroy(destroyChild = true) {
  23782. this._idMap = null;
  23783. super.destroy(destroyChild);
  23784. var list = Scene.unDestroyedScenes;
  23785. for (var i = list.length - 1; i > -1; i--) {
  23786. if (list[i] === this) {
  23787. list.splice(i, 1);
  23788. return;
  23789. }
  23790. }
  23791. }
  23792. set scaleX(value) {
  23793. if (super.get_scaleX() == value)
  23794. return;
  23795. super.set_scaleX(value);
  23796. this.event(Event.RESIZE);
  23797. }
  23798. get scaleX() {
  23799. return super.scaleX;
  23800. }
  23801. set scaleY(value) {
  23802. if (super.get_scaleY() == value)
  23803. return;
  23804. super.set_scaleY(value);
  23805. this.event(Event.RESIZE);
  23806. }
  23807. get scaleY() {
  23808. return super.scaleY;
  23809. }
  23810. get width() {
  23811. if (this._width)
  23812. return this._width;
  23813. var max = 0;
  23814. for (var i = this.numChildren - 1; i > -1; i--) {
  23815. var comp = this.getChildAt(i);
  23816. if (comp._visible) {
  23817. max = Math.max(comp._x + comp.width * comp.scaleX, max);
  23818. }
  23819. }
  23820. return max;
  23821. }
  23822. set width(value) {
  23823. if (super.get_width() == value)
  23824. return;
  23825. super.set_width(value);
  23826. this.callLater(this._sizeChanged);
  23827. }
  23828. get height() {
  23829. if (this._height)
  23830. return this._height;
  23831. var max = 0;
  23832. for (var i = this.numChildren - 1; i > -1; i--) {
  23833. var comp = this.getChildAt(i);
  23834. if (comp._visible) {
  23835. max = Math.max(comp._y + comp.height * comp.scaleY, max);
  23836. }
  23837. }
  23838. return max;
  23839. }
  23840. set height(value) {
  23841. if (super.get_height() == value)
  23842. return;
  23843. super.set_height(value);
  23844. this.callLater(this._sizeChanged);
  23845. }
  23846. _sizeChanged() {
  23847. this.event(Event.RESIZE);
  23848. }
  23849. static get root() {
  23850. if (!Scene._root) {
  23851. Scene._root = ILaya.stage.addChild(new Sprite());
  23852. Scene._root.name = "root";
  23853. ILaya.stage.on("resize", null, () => {
  23854. Scene._root.size(ILaya.stage.width, ILaya.stage.height);
  23855. Scene._root.event(Event.RESIZE);
  23856. });
  23857. Scene._root.size(ILaya.stage.width, ILaya.stage.height);
  23858. Scene._root.event(Event.RESIZE);
  23859. }
  23860. return Scene._root;
  23861. }
  23862. get timer() {
  23863. return this._timer || ILaya.timer;
  23864. }
  23865. set timer(value) {
  23866. this._timer = value;
  23867. }
  23868. static load(url, complete = null, progress = null) {
  23869. ILaya.loader.resetProgress();
  23870. var loader = new SceneLoader();
  23871. loader.on(Event.PROGRESS, null, onProgress);
  23872. loader.once(Event.COMPLETE, null, create);
  23873. loader.load(url);
  23874. function onProgress(value) {
  23875. if (Scene._loadPage)
  23876. Scene._loadPage.event("progress", value);
  23877. progress && progress.runWith(value);
  23878. }
  23879. function create() {
  23880. loader.off(Event.PROGRESS, null, onProgress);
  23881. var obj = ILaya.Loader.getRes(url);
  23882. if (!obj)
  23883. throw "Can not find scene:" + url;
  23884. if (!obj.props)
  23885. throw "Scene data is error:" + url;
  23886. var runtime = obj.props.runtime ? obj.props.runtime : obj.type;
  23887. var clas = ILaya.ClassUtils.getClass(runtime);
  23888. if (obj.props.renderType == "instance") {
  23889. var scene = clas.instance || (clas.instance = new clas());
  23890. }
  23891. else {
  23892. scene = new clas();
  23893. }
  23894. if (scene && scene instanceof Node) {
  23895. scene.url = url;
  23896. if (scene._viewCreated) {
  23897. complete && complete.runWith(scene);
  23898. }
  23899. else {
  23900. scene.on("onViewCreated", null, function () {
  23901. complete && complete.runWith(scene);
  23902. });
  23903. scene.createView(obj);
  23904. }
  23905. Scene.hideLoadingPage();
  23906. }
  23907. else {
  23908. throw "Can not find scene:" + runtime;
  23909. }
  23910. }
  23911. }
  23912. static open(url, closeOther = true, param = null, complete = null, progress = null) {
  23913. if (param instanceof Handler) {
  23914. var temp = complete;
  23915. complete = param;
  23916. param = temp;
  23917. }
  23918. Scene.showLoadingPage();
  23919. Scene.load(url, Handler.create(null, this._onSceneLoaded, [closeOther, complete, param]), progress);
  23920. }
  23921. static _onSceneLoaded(closeOther, complete, param, scene) {
  23922. scene.open(closeOther, param);
  23923. if (complete)
  23924. complete.runWith(scene);
  23925. }
  23926. static close(url, name = "") {
  23927. var flag = false;
  23928. var list = Scene.unDestroyedScenes;
  23929. for (var i = 0, n = list.length; i < n; i++) {
  23930. var scene = list[i];
  23931. if (scene && scene.parent && scene.url === url && scene.name == name) {
  23932. scene.close();
  23933. flag = true;
  23934. }
  23935. }
  23936. return flag;
  23937. }
  23938. static closeAll() {
  23939. var root = Scene.root;
  23940. for (var i = 0, n = root.numChildren; i < n; i++) {
  23941. var scene = root.getChildAt(0);
  23942. if (scene instanceof Scene)
  23943. scene.close();
  23944. else
  23945. scene.removeSelf();
  23946. }
  23947. }
  23948. static destroy(url, name = "") {
  23949. var flag = false;
  23950. var list = [].concat(Scene.unDestroyedScenes);
  23951. for (var i = 0, n = list.length; i < n; i++) {
  23952. var scene = list[i];
  23953. if (scene.url === url && scene.name == name && !scene.destroyed) {
  23954. scene.destroy();
  23955. flag = true;
  23956. }
  23957. }
  23958. return flag;
  23959. }
  23960. static gc() {
  23961. Resource.destroyUnusedResources();
  23962. }
  23963. static setLoadingPage(loadPage) {
  23964. if (Scene._loadPage != loadPage) {
  23965. Scene._loadPage = loadPage;
  23966. }
  23967. }
  23968. static showLoadingPage(param = null, delay = 500) {
  23969. if (Scene._loadPage) {
  23970. ILaya.systemTimer.clear(null, Scene._showLoading);
  23971. ILaya.systemTimer.clear(null, Scene._hideLoading);
  23972. ILaya.systemTimer.once(delay, null, Scene._showLoading, [param], false);
  23973. }
  23974. }
  23975. static _showLoading(param) {
  23976. ILaya.stage.addChild(Scene._loadPage);
  23977. Scene._loadPage.onOpened(param);
  23978. }
  23979. static _hideLoading() {
  23980. Scene._loadPage.close();
  23981. }
  23982. static hideLoadingPage(delay = 500) {
  23983. if (Scene._loadPage) {
  23984. ILaya.systemTimer.clear(null, Scene._showLoading);
  23985. ILaya.systemTimer.clear(null, Scene._hideLoading);
  23986. ILaya.systemTimer.once(delay, null, Scene._hideLoading);
  23987. }
  23988. }
  23989. }
  23990. Scene.unDestroyedScenes = [];
  23991. ILaya.regClass(Scene);
  23992. ClassUtils.regClass("laya.display.Scene", Scene);
  23993. ClassUtils.regClass("Laya.Scene", Scene);
  23994. class DrawParticleCmd {
  23995. static create(_temp) {
  23996. var cmd = Pool.getItemByClass("DrawParticleCmd", DrawParticleCmd);
  23997. cmd._templ = _temp;
  23998. return cmd;
  23999. }
  24000. recover() {
  24001. this._templ = null;
  24002. Pool.recover("DrawParticleCmd", this);
  24003. }
  24004. run(context, gx, gy) {
  24005. context.drawParticle(gx, gy, this._templ);
  24006. }
  24007. get cmdID() {
  24008. return DrawParticleCmd.ID;
  24009. }
  24010. }
  24011. DrawParticleCmd.ID = "DrawParticleCmd";
  24012. class FilterSetterBase {
  24013. constructor() {
  24014. }
  24015. paramChanged() {
  24016. Laya.systemTimer.callLater(this, this.buildFilter);
  24017. }
  24018. buildFilter() {
  24019. if (this._target) {
  24020. this.addFilter(this._target);
  24021. }
  24022. }
  24023. addFilter(sprite) {
  24024. if (!sprite)
  24025. return;
  24026. if (!sprite.filters) {
  24027. sprite.filters = [this._filter];
  24028. }
  24029. else {
  24030. var preFilters;
  24031. preFilters = sprite.filters;
  24032. if (preFilters.indexOf(this._filter) < 0) {
  24033. preFilters.push(this._filter);
  24034. sprite.filters = Utils.copyArray([], preFilters);
  24035. }
  24036. }
  24037. }
  24038. removeFilter(sprite) {
  24039. if (!sprite)
  24040. return;
  24041. sprite.filters = null;
  24042. }
  24043. set target(value) {
  24044. if (this._target != value) {
  24045. this._target = value;
  24046. this.paramChanged();
  24047. }
  24048. }
  24049. }
  24050. class BlurFilterGLRender {
  24051. render(rt, ctx, width, height, filter) {
  24052. var shaderValue = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  24053. this.setShaderInfo(shaderValue, filter, rt.width, rt.height);
  24054. ctx.drawTarget(rt, 0, 0, width, height, Matrix.EMPTY.identity(), shaderValue);
  24055. }
  24056. setShaderInfo(shader, filter, w, h) {
  24057. shader.defines.add(Filter.BLUR);
  24058. var sv = shader;
  24059. BlurFilterGLRender.blurinfo[0] = w;
  24060. BlurFilterGLRender.blurinfo[1] = h;
  24061. sv.blurInfo = BlurFilterGLRender.blurinfo;
  24062. var sigma = filter.strength / 3.0;
  24063. var sigma2 = sigma * sigma;
  24064. filter.strength_sig2_2sig2_gauss1[0] = filter.strength;
  24065. filter.strength_sig2_2sig2_gauss1[1] = sigma2;
  24066. filter.strength_sig2_2sig2_gauss1[2] = 2.0 * sigma2;
  24067. filter.strength_sig2_2sig2_gauss1[3] = 1.0 / (2.0 * Math.PI * sigma2);
  24068. sv.strength_sig2_2sig2_gauss1 = filter.strength_sig2_2sig2_gauss1;
  24069. }
  24070. }
  24071. BlurFilterGLRender.blurinfo = new Array(2);
  24072. class BlurFilter extends Filter {
  24073. constructor(strength = 4) {
  24074. super();
  24075. this.strength_sig2_2sig2_gauss1 = [];
  24076. this.strength = strength;
  24077. this._glRender = new BlurFilterGLRender();
  24078. }
  24079. get type() {
  24080. return Filter.BLUR;
  24081. }
  24082. getStrenth_sig2_2sig2_native() {
  24083. if (!this.strength_sig2_native) {
  24084. this.strength_sig2_native = new Float32Array(4);
  24085. }
  24086. var sigma = this.strength / 3.0;
  24087. var sigma2 = sigma * sigma;
  24088. this.strength_sig2_native[0] = this.strength;
  24089. this.strength_sig2_native[1] = sigma2;
  24090. this.strength_sig2_native[2] = 2.0 * sigma2;
  24091. this.strength_sig2_native[3] = 1.0 / (2.0 * Math.PI * sigma2);
  24092. return this.strength_sig2_native;
  24093. }
  24094. }
  24095. class BlurFilterSetter extends FilterSetterBase {
  24096. constructor() {
  24097. super();
  24098. this._strength = 4;
  24099. this._filter = new BlurFilter(this.strength);
  24100. }
  24101. buildFilter() {
  24102. this._filter = new BlurFilter(this.strength);
  24103. super.buildFilter();
  24104. }
  24105. get strength() {
  24106. return this._strength;
  24107. }
  24108. set strength(value) {
  24109. this._strength = value;
  24110. }
  24111. }
  24112. class ButtonEffect {
  24113. constructor() {
  24114. this._curState = 0;
  24115. this.effectScale = 1.5;
  24116. this.tweenTime = 300;
  24117. }
  24118. set target(tar) {
  24119. this._tar = tar;
  24120. tar.on(Event.MOUSE_DOWN, this, this.toChangedState);
  24121. tar.on(Event.MOUSE_UP, this, this.toInitState);
  24122. tar.on(Event.MOUSE_OUT, this, this.toInitState);
  24123. }
  24124. toChangedState() {
  24125. this._curState = 1;
  24126. if (this._curTween)
  24127. Tween.clear(this._curTween);
  24128. this._curTween = Tween.to(this._tar, { scaleX: this.effectScale, scaleY: this.effectScale }, this.tweenTime, Ease[this.effectEase], Handler.create(this, this.tweenComplete));
  24129. }
  24130. toInitState() {
  24131. if (this._curState == 2)
  24132. return;
  24133. if (this._curTween)
  24134. Tween.clear(this._curTween);
  24135. this._curState = 2;
  24136. this._curTween = Tween.to(this._tar, { scaleX: 1, scaleY: 1 }, this.tweenTime, Ease[this.backEase], Handler.create(this, this.tweenComplete));
  24137. }
  24138. tweenComplete() {
  24139. this._curState = 0;
  24140. this._curTween = null;
  24141. }
  24142. }
  24143. class ColorFilterSetter extends FilterSetterBase {
  24144. constructor() {
  24145. super();
  24146. this._brightness = 0;
  24147. this._contrast = 0;
  24148. this._saturation = 0;
  24149. this._hue = 0;
  24150. this._red = 0;
  24151. this._green = 0;
  24152. this._blue = 0;
  24153. this._alpha = 0;
  24154. this._filter = new ColorFilter();
  24155. }
  24156. buildFilter() {
  24157. this._filter.reset();
  24158. this._filter.color(this.red, this.green, this.blue, this.alpha);
  24159. this._filter.adjustHue(this.hue);
  24160. this._filter.adjustContrast(this.contrast);
  24161. this._filter.adjustBrightness(this.brightness);
  24162. this._filter.adjustSaturation(this.saturation);
  24163. super.buildFilter();
  24164. }
  24165. get brightness() {
  24166. return this._brightness;
  24167. }
  24168. set brightness(value) {
  24169. this._brightness = value;
  24170. this.paramChanged();
  24171. }
  24172. get contrast() {
  24173. return this._contrast;
  24174. }
  24175. set contrast(value) {
  24176. this._contrast = value;
  24177. this.paramChanged();
  24178. }
  24179. get saturation() {
  24180. return this._saturation;
  24181. }
  24182. set saturation(value) {
  24183. this._saturation = value;
  24184. this.paramChanged();
  24185. }
  24186. get hue() {
  24187. return this._hue;
  24188. }
  24189. set hue(value) {
  24190. this._hue = value;
  24191. this.paramChanged();
  24192. }
  24193. get red() {
  24194. return this._red;
  24195. }
  24196. set red(value) {
  24197. this._red = value;
  24198. this.paramChanged();
  24199. }
  24200. get green() {
  24201. return this._green;
  24202. }
  24203. set green(value) {
  24204. this._green = value;
  24205. this.paramChanged();
  24206. }
  24207. get blue() {
  24208. return this._blue;
  24209. }
  24210. set blue(value) {
  24211. this._blue = value;
  24212. this.paramChanged();
  24213. }
  24214. get color() {
  24215. return this._color;
  24216. }
  24217. set color(value) {
  24218. this._color = value;
  24219. var colorO;
  24220. colorO = ColorUtils.create(value);
  24221. this._red = colorO.arrColor[0] * 255;
  24222. this._green = colorO.arrColor[1] * 255;
  24223. this._blue = colorO.arrColor[2] * 255;
  24224. this.paramChanged();
  24225. }
  24226. get alpha() {
  24227. return this._alpha;
  24228. }
  24229. set alpha(value) {
  24230. this._alpha = value;
  24231. this.paramChanged();
  24232. }
  24233. }
  24234. class EffectBase extends Component {
  24235. constructor() {
  24236. super(...arguments);
  24237. this.duration = 1000;
  24238. this.delay = 0;
  24239. this.repeat = 0;
  24240. this.autoDestroyAtComplete = true;
  24241. }
  24242. _onAwake() {
  24243. this.target = this.target || this.owner;
  24244. if (this.autoDestroyAtComplete)
  24245. this._comlete = Handler.create(this.target, this.target.destroy, null, false);
  24246. if (this.eventName)
  24247. this.owner.on(this.eventName, this, this._exeTween);
  24248. else
  24249. this._exeTween();
  24250. }
  24251. _exeTween() {
  24252. this._tween = this._doTween();
  24253. this._tween.repeat = this.repeat;
  24254. }
  24255. _doTween() {
  24256. return null;
  24257. }
  24258. onReset() {
  24259. this.duration = 1000;
  24260. this.delay = 0;
  24261. this.repeat = 0;
  24262. this.ease = null;
  24263. this.target = null;
  24264. if (this.eventName) {
  24265. this.owner.off(this.eventName, this, this._exeTween);
  24266. this.eventName = null;
  24267. }
  24268. if (this._comlete) {
  24269. this._comlete.recover();
  24270. this._comlete = null;
  24271. }
  24272. if (this._tween) {
  24273. this._tween.clear();
  24274. this._tween = null;
  24275. }
  24276. }
  24277. }
  24278. class FadeIn extends EffectBase {
  24279. _doTween() {
  24280. this.target.alpha = 0;
  24281. return Tween.to(this.target, { alpha: 1 }, this.duration, Ease[this.ease], this._comlete, this.delay);
  24282. }
  24283. }
  24284. class FadeOut extends EffectBase {
  24285. _doTween() {
  24286. this.target.alpha = 1;
  24287. return Tween.to(this.target, { alpha: 0 }, this.duration, Ease[this.ease], this._comlete, this.delay);
  24288. }
  24289. }
  24290. class GlowFilterGLRender {
  24291. setShaderInfo(shader, w, h, data) {
  24292. shader.defines.add(data.type);
  24293. var sv = shader;
  24294. sv.u_blurInfo1 = data._sv_blurInfo1;
  24295. var info2 = data._sv_blurInfo2;
  24296. info2[0] = w;
  24297. info2[1] = h;
  24298. sv.u_blurInfo2 = info2;
  24299. sv.u_color = data.getColor();
  24300. }
  24301. render(rt, ctx, width, height, filter) {
  24302. var w = width, h = height;
  24303. var svBlur = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  24304. this.setShaderInfo(svBlur, w, h, filter);
  24305. var svCP = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  24306. var matI = Matrix.TEMP.identity();
  24307. ctx.drawTarget(rt, 0, 0, w, h, matI, svBlur);
  24308. ctx.drawTarget(rt, 0, 0, w, h, matI, svCP);
  24309. }
  24310. }
  24311. class GlowFilter extends Filter {
  24312. constructor(color, blur = 4, offX = 6, offY = 6) {
  24313. super();
  24314. this._elements = new Float32Array(9);
  24315. this._sv_blurInfo1 = new Array(4);
  24316. this._sv_blurInfo2 = [0, 0, 1, 0];
  24317. this._color = new ColorUtils(color);
  24318. this.blur = Math.min(blur, 20);
  24319. this.offX = offX;
  24320. this.offY = offY;
  24321. this._sv_blurInfo1[0] = this._sv_blurInfo1[1] = this.blur;
  24322. this._sv_blurInfo1[2] = offX;
  24323. this._sv_blurInfo1[3] = -offY;
  24324. this._glRender = new GlowFilterGLRender();
  24325. }
  24326. get type() {
  24327. return BlurFilter.GLOW;
  24328. }
  24329. get offY() {
  24330. return this._elements[6];
  24331. }
  24332. set offY(value) {
  24333. this._elements[6] = value;
  24334. this._sv_blurInfo1[3] = -value;
  24335. }
  24336. get offX() {
  24337. return this._elements[5];
  24338. }
  24339. set offX(value) {
  24340. this._elements[5] = value;
  24341. this._sv_blurInfo1[2] = value;
  24342. }
  24343. getColor() {
  24344. return this._color.arrColor;
  24345. }
  24346. get blur() {
  24347. return this._elements[4];
  24348. }
  24349. set blur(value) {
  24350. this._elements[4] = value;
  24351. this._sv_blurInfo1[0] = this._sv_blurInfo1[1] = value;
  24352. }
  24353. getColorNative() {
  24354. if (!this._color_native) {
  24355. this._color_native = new Float32Array(4);
  24356. }
  24357. var color = this.getColor();
  24358. this._color_native[0] = color[0];
  24359. this._color_native[1] = color[1];
  24360. this._color_native[2] = color[2];
  24361. this._color_native[3] = color[3];
  24362. return this._color_native;
  24363. }
  24364. getBlurInfo1Native() {
  24365. if (!this._blurInof1_native) {
  24366. this._blurInof1_native = new Float32Array(4);
  24367. }
  24368. this._blurInof1_native[0] = this._blurInof1_native[1] = this.blur;
  24369. this._blurInof1_native[2] = this.offX;
  24370. this._blurInof1_native[3] = this.offY;
  24371. return this._blurInof1_native;
  24372. }
  24373. getBlurInfo2Native() {
  24374. if (!this._blurInof2_native) {
  24375. this._blurInof2_native = new Float32Array(4);
  24376. }
  24377. this._blurInof2_native[2] = 1;
  24378. return this._blurInof2_native;
  24379. }
  24380. }
  24381. class GlowFilterSetter extends FilterSetterBase {
  24382. constructor() {
  24383. super();
  24384. this._color = "#ff0000";
  24385. this._blur = 4;
  24386. this._offX = 6;
  24387. this._offY = 6;
  24388. this._filter = new GlowFilter(this._color);
  24389. }
  24390. buildFilter() {
  24391. this._filter = new GlowFilter(this.color, this.blur, this.offX, this.offY);
  24392. super.buildFilter();
  24393. }
  24394. get color() {
  24395. return this._color;
  24396. }
  24397. set color(value) {
  24398. this._color = value;
  24399. this.paramChanged();
  24400. }
  24401. get blur() {
  24402. return this._blur;
  24403. }
  24404. set blur(value) {
  24405. this._blur = value;
  24406. this.paramChanged();
  24407. }
  24408. get offX() {
  24409. return this._offX;
  24410. }
  24411. set offX(value) {
  24412. this._offX = value;
  24413. this.paramChanged();
  24414. }
  24415. get offY() {
  24416. return this._offY;
  24417. }
  24418. set offY(value) {
  24419. this._offY = value;
  24420. this.paramChanged();
  24421. }
  24422. }
  24423. class KeyLocation {
  24424. }
  24425. KeyLocation.STANDARD = 0;
  24426. KeyLocation.LEFT = 1;
  24427. KeyLocation.RIGHT = 2;
  24428. KeyLocation.NUM_PAD = 3;
  24429. class Keyboard {
  24430. }
  24431. Keyboard.NUMBER_0 = 48;
  24432. Keyboard.NUMBER_1 = 49;
  24433. Keyboard.NUMBER_2 = 50;
  24434. Keyboard.NUMBER_3 = 51;
  24435. Keyboard.NUMBER_4 = 52;
  24436. Keyboard.NUMBER_5 = 53;
  24437. Keyboard.NUMBER_6 = 54;
  24438. Keyboard.NUMBER_7 = 55;
  24439. Keyboard.NUMBER_8 = 56;
  24440. Keyboard.NUMBER_9 = 57;
  24441. Keyboard.A = 65;
  24442. Keyboard.B = 66;
  24443. Keyboard.C = 67;
  24444. Keyboard.D = 68;
  24445. Keyboard.E = 69;
  24446. Keyboard.F = 70;
  24447. Keyboard.G = 71;
  24448. Keyboard.H = 72;
  24449. Keyboard.I = 73;
  24450. Keyboard.J = 74;
  24451. Keyboard.K = 75;
  24452. Keyboard.L = 76;
  24453. Keyboard.M = 77;
  24454. Keyboard.N = 78;
  24455. Keyboard.O = 79;
  24456. Keyboard.P = 80;
  24457. Keyboard.Q = 81;
  24458. Keyboard.R = 82;
  24459. Keyboard.S = 83;
  24460. Keyboard.T = 84;
  24461. Keyboard.U = 85;
  24462. Keyboard.V = 86;
  24463. Keyboard.W = 87;
  24464. Keyboard.X = 88;
  24465. Keyboard.Y = 89;
  24466. Keyboard.Z = 90;
  24467. Keyboard.F1 = 112;
  24468. Keyboard.F2 = 113;
  24469. Keyboard.F3 = 114;
  24470. Keyboard.F4 = 115;
  24471. Keyboard.F5 = 116;
  24472. Keyboard.F6 = 117;
  24473. Keyboard.F7 = 118;
  24474. Keyboard.F8 = 119;
  24475. Keyboard.F9 = 120;
  24476. Keyboard.F10 = 121;
  24477. Keyboard.F11 = 122;
  24478. Keyboard.F12 = 123;
  24479. Keyboard.F13 = 124;
  24480. Keyboard.F14 = 125;
  24481. Keyboard.F15 = 126;
  24482. Keyboard.NUMPAD = 21;
  24483. Keyboard.NUMPAD_0 = 96;
  24484. Keyboard.NUMPAD_1 = 97;
  24485. Keyboard.NUMPAD_2 = 98;
  24486. Keyboard.NUMPAD_3 = 99;
  24487. Keyboard.NUMPAD_4 = 100;
  24488. Keyboard.NUMPAD_5 = 101;
  24489. Keyboard.NUMPAD_6 = 102;
  24490. Keyboard.NUMPAD_7 = 103;
  24491. Keyboard.NUMPAD_8 = 104;
  24492. Keyboard.NUMPAD_9 = 105;
  24493. Keyboard.NUMPAD_ADD = 107;
  24494. Keyboard.NUMPAD_DECIMAL = 110;
  24495. Keyboard.NUMPAD_DIVIDE = 111;
  24496. Keyboard.NUMPAD_ENTER = 108;
  24497. Keyboard.NUMPAD_MULTIPLY = 106;
  24498. Keyboard.NUMPAD_SUBTRACT = 109;
  24499. Keyboard.SEMICOLON = 186;
  24500. Keyboard.EQUAL = 187;
  24501. Keyboard.COMMA = 188;
  24502. Keyboard.MINUS = 189;
  24503. Keyboard.PERIOD = 190;
  24504. Keyboard.SLASH = 191;
  24505. Keyboard.BACKQUOTE = 192;
  24506. Keyboard.LEFTBRACKET = 219;
  24507. Keyboard.BACKSLASH = 220;
  24508. Keyboard.RIGHTBRACKET = 221;
  24509. Keyboard.QUOTE = 222;
  24510. Keyboard.ALTERNATE = 18;
  24511. Keyboard.BACKSPACE = 8;
  24512. Keyboard.CAPS_LOCK = 20;
  24513. Keyboard.COMMAND = 15;
  24514. Keyboard.CONTROL = 17;
  24515. Keyboard.DELETE = 46;
  24516. Keyboard.ENTER = 13;
  24517. Keyboard.ESCAPE = 27;
  24518. Keyboard.PAGE_UP = 33;
  24519. Keyboard.PAGE_DOWN = 34;
  24520. Keyboard.END = 35;
  24521. Keyboard.HOME = 36;
  24522. Keyboard.LEFT = 37;
  24523. Keyboard.UP = 38;
  24524. Keyboard.RIGHT = 39;
  24525. Keyboard.DOWN = 40;
  24526. Keyboard.SHIFT = 16;
  24527. Keyboard.SPACE = 32;
  24528. Keyboard.TAB = 9;
  24529. Keyboard.INSERT = 45;
  24530. class CommandEncoder {
  24531. constructor(layagl, reserveSize, adjustSize, isSyncToRenderThread) {
  24532. this._idata = [];
  24533. }
  24534. getArrayData() {
  24535. return this._idata;
  24536. }
  24537. getPtrID() {
  24538. return 0;
  24539. }
  24540. beginEncoding() {
  24541. }
  24542. endEncoding() {
  24543. }
  24544. clearEncoding() {
  24545. this._idata.length = 0;
  24546. }
  24547. getCount() {
  24548. return this._idata.length;
  24549. }
  24550. add_ShaderValue(o) {
  24551. this._idata.push(o);
  24552. }
  24553. addShaderUniform(one) {
  24554. this.add_ShaderValue(one);
  24555. }
  24556. }
  24557. class LayaGLRunner {
  24558. static uploadShaderUniforms(layaGL, commandEncoder, shaderData, uploadUnTexture) {
  24559. var data = shaderData._data;
  24560. var shaderUniform = commandEncoder.getArrayData();
  24561. var shaderCall = 0;
  24562. for (var i = 0, n = shaderUniform.length; i < n; i++) {
  24563. var one = shaderUniform[i];
  24564. if (uploadUnTexture || one.textureID !== -1) {
  24565. var value = data[one.dataOffset];
  24566. if (value != null)
  24567. shaderCall += one.fun.call(one.caller, one, value);
  24568. }
  24569. }
  24570. return shaderCall;
  24571. }
  24572. static uploadCustomUniform(layaGL, custom, index, data) {
  24573. var shaderCall = 0;
  24574. var one = custom[index];
  24575. if (one && data != null)
  24576. shaderCall += one.fun.call(one.caller, one, data);
  24577. return shaderCall;
  24578. }
  24579. static uploadShaderUniformsForNative(layaGL, commandEncoder, shaderData) {
  24580. var nType = LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_ID;
  24581. if (shaderData._runtimeCopyValues.length > 0) {
  24582. nType = LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_DATA;
  24583. }
  24584. var data = shaderData._data;
  24585. return LayaGL.instance.uploadShaderUniforms(commandEncoder, data, nType);
  24586. }
  24587. }
  24588. class QuickTestTool {
  24589. constructor() {
  24590. }
  24591. static getMCDName(type) {
  24592. return QuickTestTool._typeToNameDic[type];
  24593. }
  24594. static showRenderTypeInfo(type, force = false) {
  24595. if (!force && QuickTestTool.showedDic[type])
  24596. return;
  24597. QuickTestTool.showedDic[type] = true;
  24598. if (!QuickTestTool._rendertypeToStrDic[type]) {
  24599. var arr = [];
  24600. var tType;
  24601. tType = 1;
  24602. while (tType <= type) {
  24603. if (tType & type) {
  24604. arr.push(QuickTestTool.getMCDName(tType & type));
  24605. }
  24606. tType = tType << 1;
  24607. }
  24608. QuickTestTool._rendertypeToStrDic[type] = arr.join(",");
  24609. }
  24610. console.log("cmd:", QuickTestTool._rendertypeToStrDic[type]);
  24611. }
  24612. static __init__() {
  24613. QuickTestTool._typeToNameDic[SpriteConst.ALPHA] = "ALPHA";
  24614. QuickTestTool._typeToNameDic[SpriteConst.TRANSFORM] = "TRANSFORM";
  24615. QuickTestTool._typeToNameDic[SpriteConst.TEXTURE] = "TEXTURE";
  24616. QuickTestTool._typeToNameDic[SpriteConst.GRAPHICS] = "GRAPHICS";
  24617. QuickTestTool._typeToNameDic[SpriteConst.ONECHILD] = "ONECHILD";
  24618. QuickTestTool._typeToNameDic[SpriteConst.CHILDS] = "CHILDS";
  24619. QuickTestTool._typeToNameDic[SpriteConst.TRANSFORM | SpriteConst.ALPHA] = "TRANSFORM|ALPHA";
  24620. QuickTestTool._typeToNameDic[SpriteConst.CANVAS] = "CANVAS";
  24621. QuickTestTool._typeToNameDic[SpriteConst.BLEND] = "BLEND";
  24622. QuickTestTool._typeToNameDic[SpriteConst.FILTERS] = "FILTERS";
  24623. QuickTestTool._typeToNameDic[SpriteConst.MASK] = "MASK";
  24624. QuickTestTool._typeToNameDic[SpriteConst.CLIP] = "CLIP";
  24625. QuickTestTool._typeToNameDic[SpriteConst.LAYAGL3D] = "LAYAGL3D";
  24626. }
  24627. render(context, x, y) {
  24628. QuickTestTool._addType(this._renderType);
  24629. QuickTestTool.showRenderTypeInfo(this._renderType);
  24630. RenderSprite.renders[this._renderType]._fun(this, context, x + this._x, y + this._y);
  24631. this._repaint = 0;
  24632. }
  24633. _stageRender(context, x, y) {
  24634. QuickTestTool._countStart();
  24635. QuickTestTool._PreStageRender.call(ILaya.stage, context, x, y);
  24636. QuickTestTool._countEnd();
  24637. }
  24638. static _countStart() {
  24639. var key;
  24640. for (key in QuickTestTool._countDic) {
  24641. QuickTestTool._countDic[key] = 0;
  24642. }
  24643. }
  24644. static _countEnd() {
  24645. QuickTestTool._i++;
  24646. if (QuickTestTool._i > 60) {
  24647. QuickTestTool.showCountInfo();
  24648. QuickTestTool._i = 0;
  24649. }
  24650. }
  24651. static _addType(type) {
  24652. if (!QuickTestTool._countDic[type]) {
  24653. QuickTestTool._countDic[type] = 1;
  24654. }
  24655. else {
  24656. QuickTestTool._countDic[type] += 1;
  24657. }
  24658. }
  24659. static showCountInfo() {
  24660. console.log("===================");
  24661. var key;
  24662. for (key in QuickTestTool._countDic) {
  24663. console.log("count:" + QuickTestTool._countDic[key]);
  24664. QuickTestTool.showRenderTypeInfo(key, true);
  24665. }
  24666. }
  24667. static enableQuickTest() {
  24668. QuickTestTool.__init__();
  24669. Sprite["prototype"]["render"] = QuickTestTool["prototype"]["render"];
  24670. QuickTestTool._PreStageRender = Stage["prototype"]["render"];
  24671. Stage["prototype"]["render"] = QuickTestTool["prototype"]["_stageRender"];
  24672. }
  24673. }
  24674. QuickTestTool.showedDic = {};
  24675. QuickTestTool._rendertypeToStrDic = {};
  24676. QuickTestTool._typeToNameDic = {};
  24677. QuickTestTool._countDic = {};
  24678. QuickTestTool._i = 0;
  24679. class Sound extends EventDispatcher {
  24680. load(url) {
  24681. }
  24682. play(startTime = 0, loops = 0) {
  24683. return null;
  24684. }
  24685. get duration() {
  24686. return 0;
  24687. }
  24688. dispose() {
  24689. }
  24690. }
  24691. class SoundNode extends Sprite {
  24692. constructor() {
  24693. super();
  24694. this.visible = false;
  24695. this.on(Event.ADDED, this, this._onParentChange);
  24696. this.on(Event.REMOVED, this, this._onParentChange);
  24697. }
  24698. _onParentChange() {
  24699. this.target = this.parent;
  24700. }
  24701. play(loops = 1, complete = null) {
  24702. if (isNaN(loops)) {
  24703. loops = 1;
  24704. }
  24705. if (!this.url)
  24706. return;
  24707. this.stop();
  24708. this._channel = SoundManager.playSound(this.url, loops, complete);
  24709. }
  24710. stop() {
  24711. if (this._channel && !this._channel.isStopped) {
  24712. this._channel.stop();
  24713. }
  24714. this._channel = null;
  24715. }
  24716. _setPlayAction(tar, event, action, add = true) {
  24717. if (!this[action])
  24718. return;
  24719. if (!tar)
  24720. return;
  24721. if (add) {
  24722. tar.on(event, this, this[action]);
  24723. }
  24724. else {
  24725. tar.off(event, this, this[action]);
  24726. }
  24727. }
  24728. _setPlayActions(tar, events, action, add = true) {
  24729. if (!tar)
  24730. return;
  24731. if (!events)
  24732. return;
  24733. var eventArr = events.split(",");
  24734. var i, len;
  24735. len = eventArr.length;
  24736. for (i = 0; i < len; i++) {
  24737. this._setPlayAction(tar, eventArr[i], action, add);
  24738. }
  24739. }
  24740. set playEvent(events) {
  24741. this._playEvents = events;
  24742. if (!events)
  24743. return;
  24744. if (this._tar) {
  24745. this._setPlayActions(this._tar, events, "play");
  24746. }
  24747. }
  24748. set target(tar) {
  24749. if (this._tar) {
  24750. this._setPlayActions(this._tar, this._playEvents, "play", false);
  24751. this._setPlayActions(this._tar, this._stopEvents, "stop", false);
  24752. }
  24753. this._tar = tar;
  24754. if (this._tar) {
  24755. this._setPlayActions(this._tar, this._playEvents, "play", true);
  24756. this._setPlayActions(this._tar, this._stopEvents, "stop", true);
  24757. }
  24758. }
  24759. set stopEvent(events) {
  24760. this._stopEvents = events;
  24761. if (!events)
  24762. return;
  24763. if (this._tar) {
  24764. this._setPlayActions(this._tar, events, "stop");
  24765. }
  24766. }
  24767. }
  24768. ClassUtils.regClass("laya.media.SoundNode", SoundNode);
  24769. ClassUtils.regClass("Laya.SoundNode", SoundNode);
  24770. class ResourceVersion {
  24771. static enable(manifestFile, callback, type = 2) {
  24772. ResourceVersion.type = type;
  24773. ILaya.loader.load(manifestFile, Handler.create(null, ResourceVersion.onManifestLoaded, [callback]), null, Loader.JSON);
  24774. }
  24775. static onManifestLoaded(callback, data) {
  24776. ResourceVersion.manifest = data;
  24777. URL.customFormat = ResourceVersion.addVersionPrefix;
  24778. callback.run();
  24779. if (!data) {
  24780. console.warn("资源版本清单文件不存在,不使用资源版本管理。忽略ERR_FILE_NOT_FOUND错误。");
  24781. }
  24782. }
  24783. static addVersionPrefix(originURL) {
  24784. originURL = URL.getAdptedFilePath(originURL);
  24785. if (ResourceVersion.manifest && ResourceVersion.manifest[originURL]) {
  24786. if (ResourceVersion.type == ResourceVersion.FILENAME_VERSION)
  24787. return ResourceVersion.manifest[originURL];
  24788. return ResourceVersion.manifest[originURL] + "/" + originURL;
  24789. }
  24790. return originURL;
  24791. }
  24792. }
  24793. ResourceVersion.FOLDER_VERSION = 1;
  24794. ResourceVersion.FILENAME_VERSION = 2;
  24795. ResourceVersion.type = ResourceVersion.FOLDER_VERSION;
  24796. class Socket extends EventDispatcher {
  24797. constructor(host = null, port = 0, byteClass = null, protocols = null) {
  24798. super();
  24799. this.disableInput = false;
  24800. this.protocols = [];
  24801. this._byteClass = byteClass ? byteClass : Byte;
  24802. this.protocols = protocols;
  24803. this.endian = Socket.BIG_ENDIAN;
  24804. if (host && port > 0 && port < 65535)
  24805. this.connect(host, port);
  24806. }
  24807. get input() {
  24808. return this._input;
  24809. }
  24810. get output() {
  24811. return this._output;
  24812. }
  24813. get connected() {
  24814. return this._connected;
  24815. }
  24816. get endian() {
  24817. return this._endian;
  24818. }
  24819. set endian(value) {
  24820. this._endian = value;
  24821. if (this._input != null)
  24822. this._input.endian = value;
  24823. if (this._output != null)
  24824. this._output.endian = value;
  24825. }
  24826. connect(host, port) {
  24827. var url = "ws://" + host + ":" + port;
  24828. this.connectByUrl(url);
  24829. }
  24830. connectByUrl(url) {
  24831. if (this._socket != null)
  24832. this.close();
  24833. this._socket && this.cleanSocket();
  24834. if (!this.protocols || this.protocols.length == 0) {
  24835. this._socket = new Browser.window.WebSocket(url);
  24836. }
  24837. else {
  24838. this._socket = new Browser.window.WebSocket(url, this.protocols);
  24839. }
  24840. this._socket.binaryType = "arraybuffer";
  24841. this._output = new this._byteClass();
  24842. this._output.endian = this.endian;
  24843. this._input = new this._byteClass();
  24844. this._input.endian = this.endian;
  24845. this._addInputPosition = 0;
  24846. this._socket.onopen = (e) => {
  24847. this._onOpen(e);
  24848. };
  24849. this._socket.onmessage = (msg) => {
  24850. this._onMessage(msg);
  24851. };
  24852. this._socket.onclose = (e) => {
  24853. this._onClose(e);
  24854. };
  24855. this._socket.onerror = (e) => {
  24856. this._onError(e);
  24857. };
  24858. }
  24859. cleanSocket() {
  24860. this.close();
  24861. this._connected = false;
  24862. this._socket.onopen = null;
  24863. this._socket.onmessage = null;
  24864. this._socket.onclose = null;
  24865. this._socket.onerror = null;
  24866. this._socket = null;
  24867. }
  24868. close() {
  24869. if (this._socket != null) {
  24870. try {
  24871. this._socket.close();
  24872. }
  24873. catch (e) {
  24874. }
  24875. }
  24876. }
  24877. _onOpen(e) {
  24878. this._connected = true;
  24879. this.event(Event.OPEN, e);
  24880. }
  24881. _onMessage(msg) {
  24882. if (!msg || !msg.data)
  24883. return;
  24884. var data = msg.data;
  24885. if (this.disableInput && data) {
  24886. this.event(Event.MESSAGE, data);
  24887. return;
  24888. }
  24889. if (this._input.length > 0 && this._input.bytesAvailable < 1) {
  24890. this._input.clear();
  24891. this._addInputPosition = 0;
  24892. }
  24893. var pre = this._input.pos;
  24894. !this._addInputPosition && (this._addInputPosition = 0);
  24895. this._input.pos = this._addInputPosition;
  24896. if (data) {
  24897. if (typeof (data) == 'string') {
  24898. this._input.writeUTFBytes(data);
  24899. }
  24900. else {
  24901. this._input.writeArrayBuffer(data);
  24902. }
  24903. this._addInputPosition = this._input.pos;
  24904. this._input.pos = pre;
  24905. }
  24906. this.event(Event.MESSAGE, data);
  24907. }
  24908. _onClose(e) {
  24909. this._connected = false;
  24910. this.event(Event.CLOSE, e);
  24911. }
  24912. _onError(e) {
  24913. this.event(Event.ERROR, e);
  24914. }
  24915. send(data) {
  24916. this._socket.send(data);
  24917. }
  24918. flush() {
  24919. if (this._output && this._output.length > 0) {
  24920. var evt;
  24921. try {
  24922. this._socket && this._socket.send(this._output.__getBuffer().slice(0, this._output.length));
  24923. }
  24924. catch (e) {
  24925. evt = e;
  24926. }
  24927. this._output.endian = this.endian;
  24928. this._output.clear();
  24929. if (evt)
  24930. this.event(Event.ERROR, evt);
  24931. }
  24932. }
  24933. }
  24934. Socket.LITTLE_ENDIAN = "littleEndian";
  24935. Socket.BIG_ENDIAN = "bigEndian";
  24936. (function (TextureDecodeFormat) {
  24937. TextureDecodeFormat[TextureDecodeFormat["Normal"] = 0] = "Normal";
  24938. TextureDecodeFormat[TextureDecodeFormat["RGBM"] = 1] = "RGBM";
  24939. })(exports.TextureDecodeFormat || (exports.TextureDecodeFormat = {}));
  24940. class VideoTexture extends BaseTexture {
  24941. constructor() {
  24942. var gl = LayaGL.instance;
  24943. super(gl.RGB, false);
  24944. this._glTextureType = gl.TEXTURE_2D;
  24945. this._width = 1;
  24946. this._height = 1;
  24947. this._wrapModeU = this._wrapModeV = exports.WarpMode.Clamp;
  24948. this._filterMode = exports.FilterMode.Bilinear;
  24949. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  24950. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  24951. this._setFilterMode(this._filterMode);
  24952. this._needUpdate = false;
  24953. this._readyed = true;
  24954. VideoTexture._videoTexturePool.push(this);
  24955. }
  24956. static _update() {
  24957. var pool = VideoTexture._videoTexturePool;
  24958. for (var i = 0, n = pool.length; i < n; i++) {
  24959. var videoElement = pool[i];
  24960. (videoElement) && videoElement._updateVideoData();
  24961. }
  24962. }
  24963. get video() {
  24964. return this._video;
  24965. }
  24966. set video(value) {
  24967. if (!value || !(value instanceof HTMLVideoElement))
  24968. return;
  24969. this._video = value;
  24970. if (Laya.Browser.onMobile) {
  24971. this._video["x5-playsInline"] = true;
  24972. this._video["x5-playsinline"] = true;
  24973. this._video.x5PlaysInline = true;
  24974. this._video.playsInline = true;
  24975. this._video["webkit-playsInline"] = true;
  24976. this._video["webkit-playsinline"] = true;
  24977. this._video.webkitPlaysInline = true;
  24978. this._video.playsinline = true;
  24979. this._video.style.playsInline = true;
  24980. this._video.crossOrigin = "anonymous";
  24981. this._video.setAttribute('crossorigin', "anonymous");
  24982. this._video.setAttribute('playsinline', 'true');
  24983. this._video.setAttribute('x5-playsinline', 'true');
  24984. this._video.setAttribute('webkit-playsinline', 'true');
  24985. this._video.autoplay = true;
  24986. }
  24987. }
  24988. _updateVideoData() {
  24989. if (!this._video || !this._needUpdate)
  24990. return;
  24991. var gl = LayaGL.instance;
  24992. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  24993. gl.texImage2D(this._glTextureType, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, this._video);
  24994. }
  24995. videoPlay() {
  24996. this._video.play();
  24997. this._needUpdate = true;
  24998. }
  24999. videoPause() {
  25000. this._video.pause();
  25001. this._needUpdate = false;
  25002. }
  25003. destroy() {
  25004. super.destroy();
  25005. this._video = null;
  25006. }
  25007. }
  25008. VideoTexture._videoTexturePool = new Array();
  25009. class System {
  25010. static changeDefinition(name, classObj) {
  25011. window.Laya[name] = classObj;
  25012. var str = name + "=classObj";
  25013. window['eval'](str);
  25014. }
  25015. }
  25016. class HTMLChar {
  25017. constructor() {
  25018. this.reset();
  25019. }
  25020. setData(char, w, h, style) {
  25021. this.char = char;
  25022. this.charNum = char.charCodeAt(0);
  25023. this.x = this.y = 0;
  25024. this.width = w;
  25025. this.height = h;
  25026. this.style = style;
  25027. this.isWord = !HTMLChar._isWordRegExp.test(char);
  25028. return this;
  25029. }
  25030. reset() {
  25031. this.x = this.y = this.width = this.height = 0;
  25032. this.isWord = false;
  25033. this.char = null;
  25034. this.charNum = 0;
  25035. this.style = null;
  25036. return this;
  25037. }
  25038. recover() {
  25039. Pool.recover("HTMLChar", this.reset());
  25040. }
  25041. static create() {
  25042. return Pool.getItemByClass("HTMLChar", HTMLChar);
  25043. }
  25044. _isChar() {
  25045. return true;
  25046. }
  25047. _getCSSStyle() {
  25048. return this.style;
  25049. }
  25050. }
  25051. HTMLChar._isWordRegExp = new RegExp("[\\w\.]", "");
  25052. class Log {
  25053. static enable() {
  25054. if (!Log._logdiv) {
  25055. Log._logdiv = Browser.createElement('div');
  25056. Log._logdiv.style.cssText = "border:white;padding:4px;overflow-y:auto;z-index:1000000;background:rgba(100,100,100,0.6);color:white;position: absolute;left:0px;top:0px;width:50%;height:50%;";
  25057. Browser.document.body.appendChild(Log._logdiv);
  25058. Log._btn = Browser.createElement("button");
  25059. Log._btn.innerText = "Hide";
  25060. Log._btn.style.cssText = "z-index:1000001;position: absolute;left:10px;top:10px;";
  25061. Log._btn.onclick = Log.toggle;
  25062. Browser.document.body.appendChild(Log._btn);
  25063. }
  25064. }
  25065. static toggle() {
  25066. var style = Log._logdiv.style;
  25067. if (style.display === "") {
  25068. Log._btn.innerText = "Show";
  25069. style.display = "none";
  25070. }
  25071. else {
  25072. Log._btn.innerText = "Hide";
  25073. style.display = "";
  25074. }
  25075. }
  25076. static print(value) {
  25077. if (Log._logdiv) {
  25078. if (Log._count >= Log.maxCount)
  25079. Log.clear();
  25080. Log._count++;
  25081. Log._logdiv.innerText += value + "\n";
  25082. if (Log.autoScrollToBottom) {
  25083. if (Log._logdiv.scrollHeight - Log._logdiv.scrollTop - Log._logdiv.clientHeight < 50) {
  25084. Log._logdiv.scrollTop = Log._logdiv.scrollHeight;
  25085. }
  25086. }
  25087. }
  25088. }
  25089. static clear() {
  25090. Log._logdiv.innerText = "";
  25091. Log._count = 0;
  25092. }
  25093. }
  25094. Log._count = 0;
  25095. Log.maxCount = 50;
  25096. Log.autoScrollToBottom = true;
  25097. let DATANUM = 300;
  25098. class PerfData {
  25099. constructor(id, color, name, scale) {
  25100. this.scale = 1.0;
  25101. this.datas = new Array(DATANUM);
  25102. this.datapos = 0;
  25103. this.id = id;
  25104. this.color = color;
  25105. this.name = name;
  25106. this.scale = scale;
  25107. }
  25108. addData(v) {
  25109. this.datas[this.datapos] = v;
  25110. this.datapos++;
  25111. this.datapos %= DATANUM;
  25112. }
  25113. }
  25114. class PerfHUD extends Sprite {
  25115. constructor() {
  25116. super();
  25117. this.datas = [];
  25118. this.xdata = new Array(PerfHUD.DATANUM);
  25119. this.ydata = new Array(PerfHUD.DATANUM);
  25120. this.hud_width = 800;
  25121. this.hud_height = 200;
  25122. this.gMinV = 0;
  25123. this.gMaxV = 100;
  25124. this.textSpace = 40;
  25125. this.sttm = 0;
  25126. PerfHUD.inst = this;
  25127. this._renderType |= SpriteConst.CUSTOM;
  25128. this._setRenderType(this._renderType);
  25129. this._setCustomRender();
  25130. this.addDataDef(0, 0xffffff, 'frame', 1.0);
  25131. this.addDataDef(1, 0x00ff00, 'update', 1.0);
  25132. this.addDataDef(2, 0xff0000, 'flush', 1.0);
  25133. PerfHUD._now = performance ? performance.now.bind(performance) : Date.now;
  25134. }
  25135. now() {
  25136. return PerfHUD._now();
  25137. }
  25138. start() {
  25139. this.sttm = PerfHUD._now();
  25140. }
  25141. end(i) {
  25142. var dt = PerfHUD._now() - this.sttm;
  25143. this.updateValue(i, dt);
  25144. }
  25145. config(w, h) {
  25146. this.hud_width = w;
  25147. this.hud_height = h;
  25148. }
  25149. addDataDef(id, color, name, scale) {
  25150. this.datas[id] = new PerfData(id, color, name, scale);
  25151. }
  25152. updateValue(id, v) {
  25153. this.datas[id].addData(v);
  25154. }
  25155. v2y(v) {
  25156. var bb = this._y + this.hud_height * (1 - (v - this.gMinV) / this.gMaxV);
  25157. return this._y + this.hud_height * (1 - (v - this.gMinV) / this.gMaxV);
  25158. }
  25159. drawHLine(ctx, v, color, text) {
  25160. var sx = this._x;
  25161. var ex = this._x + this.hud_width;
  25162. var sy = this.v2y(v);
  25163. ctx.fillText(text, sx, sy - 6, null, 'green', null);
  25164. sx += this.textSpace;
  25165. ctx.fillStyle = color;
  25166. ctx.fillRect(sx, sy, this._x + this.hud_width, 1, null);
  25167. }
  25168. customRender(ctx, x, y) {
  25169. var now = performance.now();
  25170. if (PerfHUD._lastTm <= 0)
  25171. PerfHUD._lastTm = now;
  25172. this.updateValue(0, now - PerfHUD._lastTm);
  25173. PerfHUD._lastTm = now;
  25174. ctx.save();
  25175. ctx.fillRect(this._x, this._y, this.hud_width, this.hud_height + 4, '#000000cc');
  25176. ctx.globalAlpha = 0.9;
  25177. this.drawHLine(ctx, 0, 'green', ' 0');
  25178. this.drawHLine(ctx, 10, 'green', ' 10');
  25179. this.drawHLine(ctx, 16.667, 'red', ' ');
  25180. this.drawHLine(ctx, 20, 'green', '50|20');
  25181. this.drawHLine(ctx, 16.667 * 2, 'yellow', '');
  25182. this.drawHLine(ctx, 16.667 * 3, 'yellow', '');
  25183. this.drawHLine(ctx, 16.667 * 4, 'yellow', '');
  25184. this.drawHLine(ctx, 50, 'green', '20|50');
  25185. this.drawHLine(ctx, 100, 'green', '10|100');
  25186. for (var di = 0, sz = this.datas.length; di < sz; di++) {
  25187. var cd = this.datas[di];
  25188. if (!cd)
  25189. continue;
  25190. var dtlen = cd.datas.length;
  25191. var dx = (this.hud_width - this.textSpace) / dtlen;
  25192. var cx = cd.datapos;
  25193. var _cx = this._x + this.textSpace;
  25194. ctx.fillStyle = cd.color;
  25195. for (var dtsz = dtlen; cx < dtsz; cx++) {
  25196. var sty = this.v2y(cd.datas[cx] * cd.scale);
  25197. ctx.fillRect(_cx, sty, dx, this.hud_height + this._y - sty, null);
  25198. _cx += dx;
  25199. }
  25200. for (cx = 0; cx < cd.datapos; cx++) {
  25201. sty = this.v2y(cd.datas[cx] * cd.scale);
  25202. ctx.fillRect(_cx, sty, dx, this.hud_height + this._y - sty, null);
  25203. _cx += dx;
  25204. }
  25205. }
  25206. ctx.restore();
  25207. }
  25208. }
  25209. PerfHUD._lastTm = 0;
  25210. PerfHUD._now = null;
  25211. PerfHUD.DATANUM = 300;
  25212. PerfHUD.drawTexTm = 0;
  25213. class PoolCache {
  25214. constructor() {
  25215. this.maxCount = 1000;
  25216. }
  25217. getCacheList() {
  25218. return Pool.getPoolBySign(this.sign);
  25219. }
  25220. tryDispose(force) {
  25221. var list;
  25222. list = Pool.getPoolBySign(this.sign);
  25223. if (list.length > this.maxCount) {
  25224. list.splice(this.maxCount, list.length - this.maxCount);
  25225. }
  25226. }
  25227. static addPoolCacheManager(sign, maxCount = 100) {
  25228. var cache;
  25229. cache = new PoolCache();
  25230. cache.sign = sign;
  25231. cache.maxCount = maxCount;
  25232. CacheManger.regCacheByFunction(Utils.bind(cache.tryDispose, cache), Utils.bind(cache.getCacheList, cache));
  25233. }
  25234. }
  25235. class TimeLine extends EventDispatcher {
  25236. constructor() {
  25237. super(...arguments);
  25238. this._tweenDic = {};
  25239. this._tweenDataList = [];
  25240. this._currTime = 0;
  25241. this._lastTime = 0;
  25242. this._startTime = 0;
  25243. this._index = 0;
  25244. this._gidIndex = 0;
  25245. this._firstTweenDic = {};
  25246. this._startTimeSort = false;
  25247. this._endTimeSort = false;
  25248. this._loopKey = false;
  25249. this.scale = 1;
  25250. this._frameRate = 60;
  25251. this._frameIndex = 0;
  25252. this._total = 0;
  25253. }
  25254. static to(target, props, duration, ease = null, offset = 0) {
  25255. return (new TimeLine()).to(target, props, duration, ease, offset);
  25256. }
  25257. static from(target, props, duration, ease = null, offset = 0) {
  25258. return (new TimeLine()).from(target, props, duration, ease, offset);
  25259. }
  25260. to(target, props, duration, ease = null, offset = 0) {
  25261. return this._create(target, props, duration, ease, offset, true);
  25262. }
  25263. from(target, props, duration, ease = null, offset = 0) {
  25264. return this._create(target, props, duration, ease, offset, false);
  25265. }
  25266. _create(target, props, duration, ease, offset, isTo) {
  25267. var tTweenData = Pool.getItemByClass("tweenData", tweenData);
  25268. tTweenData.isTo = isTo;
  25269. tTweenData.type = 0;
  25270. tTweenData.target = target;
  25271. tTweenData.duration = duration;
  25272. tTweenData.data = props;
  25273. tTweenData.startTime = this._startTime + offset;
  25274. tTweenData.endTime = tTweenData.startTime + tTweenData.duration;
  25275. tTweenData.ease = ease;
  25276. this._startTime = Math.max(tTweenData.endTime, this._startTime);
  25277. this._tweenDataList.push(tTweenData);
  25278. this._startTimeSort = true;
  25279. this._endTimeSort = true;
  25280. return this;
  25281. }
  25282. addLabel(label, offset) {
  25283. var tTweenData = Pool.getItemByClass("tweenData", tweenData);
  25284. tTweenData.type = 1;
  25285. tTweenData.data = label;
  25286. tTweenData.endTime = tTweenData.startTime = this._startTime + offset;
  25287. this._labelDic || (this._labelDic = {});
  25288. this._labelDic[label] = tTweenData;
  25289. this._tweenDataList.push(tTweenData);
  25290. return this;
  25291. }
  25292. removeLabel(label) {
  25293. if (this._labelDic && this._labelDic[label]) {
  25294. var tTweenData = this._labelDic[label];
  25295. if (tTweenData) {
  25296. var tIndex = this._tweenDataList.indexOf(tTweenData);
  25297. if (tIndex > -1) {
  25298. this._tweenDataList.splice(tIndex, 1);
  25299. }
  25300. }
  25301. delete this._labelDic[label];
  25302. }
  25303. }
  25304. gotoTime(time) {
  25305. if (this._tweenDataList == null || this._tweenDataList.length == 0)
  25306. return;
  25307. var tTween;
  25308. var tObject;
  25309. for (var p in this._firstTweenDic) {
  25310. tObject = this._firstTweenDic[p];
  25311. if (tObject) {
  25312. for (var tDataP in tObject) {
  25313. if (tDataP in tObject.diyTarget) {
  25314. tObject.diyTarget[tDataP] = tObject[tDataP];
  25315. }
  25316. }
  25317. }
  25318. }
  25319. for (p in this._tweenDic) {
  25320. tTween = this._tweenDic[p];
  25321. tTween.clear();
  25322. delete this._tweenDic[p];
  25323. }
  25324. this._index = 0;
  25325. this._gidIndex = 0;
  25326. this._currTime = time;
  25327. this._lastTime = Browser.now();
  25328. var tTweenDataCopyList;
  25329. if (this._endTweenDataList == null || this._endTimeSort) {
  25330. this._endTimeSort = false;
  25331. this._endTweenDataList = tTweenDataCopyList = this._tweenDataList.concat();
  25332. function Compare(paraA, paraB) {
  25333. if (paraA.endTime > paraB.endTime) {
  25334. return 1;
  25335. }
  25336. else if (paraA.endTime < paraB.endTime) {
  25337. return -1;
  25338. }
  25339. else {
  25340. return 0;
  25341. }
  25342. }
  25343. tTweenDataCopyList.sort(Compare);
  25344. }
  25345. else {
  25346. tTweenDataCopyList = this._endTweenDataList;
  25347. }
  25348. var tTweenData;
  25349. for (var i = 0, n = tTweenDataCopyList.length; i < n; i++) {
  25350. tTweenData = tTweenDataCopyList[i];
  25351. if (tTweenData.type == 0) {
  25352. if (time >= tTweenData.endTime) {
  25353. this._index = Math.max(this._index, i + 1);
  25354. var props = tTweenData.data;
  25355. if (tTweenData.isTo) {
  25356. for (var tP in props) {
  25357. tTweenData.target[tP] = props[tP];
  25358. }
  25359. }
  25360. }
  25361. else {
  25362. break;
  25363. }
  25364. }
  25365. }
  25366. for (i = 0, n = this._tweenDataList.length; i < n; i++) {
  25367. tTweenData = this._tweenDataList[i];
  25368. if (tTweenData.type == 0) {
  25369. if (time >= tTweenData.startTime && time < tTweenData.endTime) {
  25370. this._index = Math.max(this._index, i + 1);
  25371. this._gidIndex++;
  25372. tTween = Pool.getItemByClass("tween", Tween);
  25373. tTween._create(tTweenData.target, tTweenData.data, tTweenData.duration, tTweenData.ease, Handler.create(this, this._animComplete, [this._gidIndex]), 0, false, tTweenData.isTo, true, false);
  25374. tTween.setStartTime(this._currTime - (time - tTweenData.startTime));
  25375. tTween._updateEase(this._currTime);
  25376. tTween.gid = this._gidIndex;
  25377. this._tweenDic[this._gidIndex] = tTween;
  25378. }
  25379. }
  25380. }
  25381. }
  25382. gotoLabel(Label) {
  25383. if (this._labelDic == null)
  25384. return;
  25385. var tLabelData = this._labelDic[Label];
  25386. if (tLabelData)
  25387. this.gotoTime(tLabelData.startTime);
  25388. }
  25389. pause() {
  25390. ILaya.timer.clear(this, this._update);
  25391. }
  25392. resume() {
  25393. this.play(this._currTime, this._loopKey);
  25394. }
  25395. play(timeOrLabel = 0, loop = false) {
  25396. if (!this._tweenDataList)
  25397. return;
  25398. if (this._startTimeSort) {
  25399. this._startTimeSort = false;
  25400. function Compare(paraA, paraB) {
  25401. if (paraA.startTime > paraB.startTime) {
  25402. return 1;
  25403. }
  25404. else if (paraA.startTime < paraB.startTime) {
  25405. return -1;
  25406. }
  25407. else {
  25408. return 0;
  25409. }
  25410. }
  25411. this._tweenDataList.sort(Compare);
  25412. for (var i = 0, n = this._tweenDataList.length; i < n; i++) {
  25413. var tTweenData = this._tweenDataList[i];
  25414. if (tTweenData != null && tTweenData.type == 0) {
  25415. var tTarget = tTweenData.target;
  25416. var gid = (tTarget.$_GID || (tTarget.$_GID = Utils.getGID()));
  25417. var tSrcData = null;
  25418. if (this._firstTweenDic[gid] == null) {
  25419. tSrcData = {};
  25420. tSrcData.diyTarget = tTarget;
  25421. this._firstTweenDic[gid] = tSrcData;
  25422. }
  25423. else {
  25424. tSrcData = this._firstTweenDic[gid];
  25425. }
  25426. for (var p in tTweenData.data) {
  25427. if (tSrcData[p] == null) {
  25428. tSrcData[p] = tTarget[p];
  25429. }
  25430. }
  25431. }
  25432. }
  25433. }
  25434. if (typeof (timeOrLabel) == 'string') {
  25435. this.gotoLabel(timeOrLabel);
  25436. }
  25437. else {
  25438. this.gotoTime(timeOrLabel);
  25439. }
  25440. this._loopKey = loop;
  25441. this._lastTime = Browser.now();
  25442. ILaya.timer.frameLoop(1, this, this._update);
  25443. }
  25444. _update() {
  25445. if (this._currTime >= this._startTime) {
  25446. if (this._loopKey) {
  25447. this._complete();
  25448. if (!this._tweenDataList)
  25449. return;
  25450. this.gotoTime(0);
  25451. }
  25452. else {
  25453. for (var p in this._tweenDic) {
  25454. tTween = this._tweenDic[p];
  25455. tTween.complete();
  25456. }
  25457. this.pause();
  25458. this._complete();
  25459. return;
  25460. }
  25461. }
  25462. var tNow = Browser.now();
  25463. var tFrameTime = tNow - this._lastTime;
  25464. var tCurrTime = this._currTime += tFrameTime * this.scale;
  25465. this._lastTime = tNow;
  25466. for (p in this._tweenDic) {
  25467. tTween = this._tweenDic[p];
  25468. tTween._updateEase(tCurrTime);
  25469. }
  25470. var tTween;
  25471. if (this._tweenDataList.length != 0 && this._index < this._tweenDataList.length) {
  25472. var tTweenData = this._tweenDataList[this._index];
  25473. if (tCurrTime >= tTweenData.startTime) {
  25474. this._index++;
  25475. if (tTweenData.type == 0) {
  25476. this._gidIndex++;
  25477. tTween = Pool.getItemByClass("tween", Tween);
  25478. tTween._create(tTweenData.target, tTweenData.data, tTweenData.duration, tTweenData.ease, Handler.create(this, this._animComplete, [this._gidIndex]), 0, false, tTweenData.isTo, true, false);
  25479. tTween.setStartTime(tCurrTime);
  25480. tTween.gid = this._gidIndex;
  25481. this._tweenDic[this._gidIndex] = tTween;
  25482. tTween._updateEase(tCurrTime);
  25483. }
  25484. else {
  25485. this.event(Event.LABEL, tTweenData.data);
  25486. }
  25487. }
  25488. }
  25489. }
  25490. _animComplete(index) {
  25491. var tTween = this._tweenDic[index];
  25492. if (tTween)
  25493. delete this._tweenDic[index];
  25494. }
  25495. _complete() {
  25496. this.event(Event.COMPLETE);
  25497. }
  25498. get index() {
  25499. return this._frameIndex;
  25500. }
  25501. set index(value) {
  25502. this._frameIndex = value;
  25503. this.gotoTime(this._frameIndex / this._frameRate * 1000);
  25504. }
  25505. get total() {
  25506. this._total = Math.floor(this._startTime / 1000 * this._frameRate);
  25507. return this._total;
  25508. }
  25509. reset() {
  25510. var p;
  25511. if (this._labelDic) {
  25512. for (p in this._labelDic) {
  25513. delete this._labelDic[p];
  25514. }
  25515. }
  25516. var tTween;
  25517. for (p in this._tweenDic) {
  25518. tTween = this._tweenDic[p];
  25519. tTween.clear();
  25520. delete this._tweenDic[p];
  25521. }
  25522. for (p in this._firstTweenDic) {
  25523. delete this._firstTweenDic[p];
  25524. }
  25525. this._endTweenDataList = null;
  25526. if (this._tweenDataList && this._tweenDataList.length) {
  25527. var i, len;
  25528. len = this._tweenDataList.length;
  25529. for (i = 0; i < len; i++) {
  25530. if (this._tweenDataList[i])
  25531. this._tweenDataList[i].destroy();
  25532. }
  25533. }
  25534. this._tweenDataList.length = 0;
  25535. this._currTime = 0;
  25536. this._lastTime = 0;
  25537. this._startTime = 0;
  25538. this._index = 0;
  25539. this._gidIndex = 0;
  25540. this.scale = 1;
  25541. ILaya.timer.clear(this, this._update);
  25542. }
  25543. destroy() {
  25544. this.reset();
  25545. this._labelDic = null;
  25546. this._tweenDic = null;
  25547. this._tweenDataList = null;
  25548. this._firstTweenDic = null;
  25549. }
  25550. }
  25551. class tweenData {
  25552. constructor() {
  25553. this.type = 0;
  25554. this.isTo = true;
  25555. }
  25556. destroy() {
  25557. this.target = null;
  25558. this.ease = null;
  25559. this.data = null;
  25560. this.isTo = true;
  25561. this.type = 0;
  25562. Pool.recover("tweenData", this);
  25563. }
  25564. }
  25565. class ShaderValue {
  25566. constructor() {
  25567. }
  25568. }
  25569. class ArabicReshaper {
  25570. characterMapContains(c) {
  25571. for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
  25572. if (ArabicReshaper.charsMap[i][0] === c) {
  25573. return true;
  25574. }
  25575. }
  25576. return false;
  25577. }
  25578. getCharRep(c) {
  25579. for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
  25580. if (ArabicReshaper.charsMap[i][0] === c) {
  25581. return ArabicReshaper.charsMap[i];
  25582. }
  25583. }
  25584. return false;
  25585. }
  25586. getCombCharRep(c1, c2) {
  25587. for (var i = 0; i < ArabicReshaper.combCharsMap.length; ++i) {
  25588. if (ArabicReshaper.combCharsMap[i][0][0] === c1 && ArabicReshaper.combCharsMap[i][0][1] === c2) {
  25589. return ArabicReshaper.combCharsMap[i];
  25590. }
  25591. }
  25592. return false;
  25593. }
  25594. isTransparent(c) {
  25595. for (var i = 0; i < ArabicReshaper.transChars.length; ++i) {
  25596. if (ArabicReshaper.transChars[i] === c) {
  25597. return true;
  25598. }
  25599. }
  25600. return false;
  25601. }
  25602. getOriginalCharsFromCode(code) {
  25603. var j;
  25604. for (j = 0; j < ArabicReshaper.charsMap.length; ++j) {
  25605. if (ArabicReshaper.charsMap[j].indexOf(code) > -1) {
  25606. return String.fromCharCode(ArabicReshaper.charsMap[j][0]);
  25607. }
  25608. }
  25609. for (j = 0; j < ArabicReshaper.combCharsMap.length; ++j) {
  25610. if (ArabicReshaper.combCharsMap[j].indexOf(code) > -1) {
  25611. return String.fromCharCode(ArabicReshaper.combCharsMap[j][0][0]) +
  25612. String.fromCharCode(ArabicReshaper.combCharsMap[j][0][1]);
  25613. }
  25614. }
  25615. return String.fromCharCode(code);
  25616. }
  25617. convertArabic(normal) {
  25618. var crep, combcrep, shaped = '';
  25619. for (var i = 0; i < normal.length; ++i) {
  25620. var current = normal.charCodeAt(i);
  25621. if (this.characterMapContains(current)) {
  25622. var prev = null, next = null, prevID = i - 1, nextID = i + 1;
  25623. for (; prevID >= 0; --prevID) {
  25624. if (!this.isTransparent(normal.charCodeAt(prevID))) {
  25625. break;
  25626. }
  25627. }
  25628. prev = (prevID >= 0) ? normal.charCodeAt(prevID) : null;
  25629. crep = prev ? this.getCharRep(prev) : false;
  25630. if (!crep || crep[2] == null && crep[3] == null) {
  25631. prev = null;
  25632. }
  25633. for (; nextID < normal.length; ++nextID) {
  25634. if (!this.isTransparent(normal.charCodeAt(nextID))) {
  25635. break;
  25636. }
  25637. }
  25638. next = (nextID < normal.length) ? normal.charCodeAt(nextID) : null;
  25639. crep = next ? this.getCharRep(next) : false;
  25640. if (!crep || crep[3] == null && crep[4] == null) {
  25641. next = null;
  25642. }
  25643. if (current === 0x0644 && next != null &&
  25644. (next === 0x0622 || next === 0x0623 || next === 0x0625 || next === 0x0627)) {
  25645. combcrep = this.getCombCharRep(current, next);
  25646. if (prev != null) {
  25647. shaped += String.fromCharCode(combcrep[4]);
  25648. }
  25649. else {
  25650. shaped += String.fromCharCode(combcrep[1]);
  25651. }
  25652. ++i;
  25653. continue;
  25654. }
  25655. crep = this.getCharRep(current);
  25656. if (prev != null && next != null && crep[3] != null) {
  25657. shaped += String.fromCharCode(crep[3]);
  25658. continue;
  25659. }
  25660. else if (prev != null && crep[4] != null) {
  25661. shaped += String.fromCharCode(crep[4]);
  25662. continue;
  25663. }
  25664. else if (next != null && crep[2] != null) {
  25665. shaped += String.fromCharCode(crep[2]);
  25666. continue;
  25667. }
  25668. else {
  25669. shaped += String.fromCharCode(crep[1]);
  25670. }
  25671. }
  25672. else {
  25673. shaped += String.fromCharCode(current);
  25674. }
  25675. }
  25676. return shaped;
  25677. }
  25678. convertArabicBack(apfb) {
  25679. var toReturn = '', selectedChar;
  25680. var i;
  25681. for (i = 0; i < apfb.length; ++i) {
  25682. selectedChar = apfb.charCodeAt(i);
  25683. toReturn += this.getOriginalCharsFromCode(selectedChar);
  25684. }
  25685. return toReturn;
  25686. }
  25687. }
  25688. ArabicReshaper.charsMap = [[0x0621, 0xFE80, null, null, null],
  25689. [0x0622, 0xFE81, null, null, 0xFE82],
  25690. [0x0623, 0xFE83, null, null, 0xFE84],
  25691. [0x0624, 0xFE85, null, null, 0xFE86],
  25692. [0x0625, 0xFE87, null, null, 0xFE88],
  25693. [0x0626, 0xFE89, 0xFE8B, 0xFE8C, 0xFE8A],
  25694. [0x0627, 0xFE8D, null, null, 0xFE8E],
  25695. [0x0628, 0xFE8F, 0xFE91, 0xFE92, 0xFE90],
  25696. [0x0629, 0xFE93, null, null, 0xFE94],
  25697. [0x062A, 0xFE95, 0xFE97, 0xFE98, 0xFE96],
  25698. [0x062B, 0xFE99, 0xFE9B, 0xFE9C, 0xFE9A],
  25699. [0x062C, 0xFE9D, 0xFE9F, 0xFEA0, 0xFE9E],
  25700. [0x062D, 0xFEA1, 0xFEA3, 0xFEA4, 0xFEA2],
  25701. [0x062E, 0xFEA5, 0xFEA7, 0xFEA8, 0xFEA6],
  25702. [0x062F, 0xFEA9, null, null, 0xFEAA],
  25703. [0x0630, 0xFEAB, null, null, 0xFEAC],
  25704. [0x0631, 0xFEAD, null, null, 0xFEAE],
  25705. [0x0632, 0xFEAF, null, null, 0xFEB0],
  25706. [0x0633, 0xFEB1, 0xFEB3, 0xFEB4, 0xFEB2],
  25707. [0x0634, 0xFEB5, 0xFEB7, 0xFEB8, 0xFEB6],
  25708. [0x0635, 0xFEB9, 0xFEBB, 0xFEBC, 0xFEBA],
  25709. [0x0636, 0xFEBD, 0xFEBF, 0xFEC0, 0xFEBE],
  25710. [0x0637, 0xFEC1, 0xFEC3, 0xFEC4, 0xFEC2],
  25711. [0x0638, 0xFEC5, 0xFEC7, 0xFEC8, 0xFEC6],
  25712. [0x0639, 0xFEC9, 0xFECB, 0xFECC, 0xFECA],
  25713. [0x063A, 0xFECD, 0xFECF, 0xFED0, 0xFECE],
  25714. [0x0640, 0x0640, 0x0640, 0x0640, 0x0640],
  25715. [0x0641, 0xFED1, 0xFED3, 0xFED4, 0xFED2],
  25716. [0x0642, 0xFED5, 0xFED7, 0xFED8, 0xFED6],
  25717. [0x0643, 0xFED9, 0xFEDB, 0xFEDC, 0xFEDA],
  25718. [0x0644, 0xFEDD, 0xFEDF, 0xFEE0, 0xFEDE],
  25719. [0x0645, 0xFEE1, 0xFEE3, 0xFEE4, 0xFEE2],
  25720. [0x0646, 0xFEE5, 0xFEE7, 0xFEE8, 0xFEE6],
  25721. [0x0647, 0xFEE9, 0xFEEB, 0xFEEC, 0xFEEA],
  25722. [0x0648, 0xFEED, null, null, 0xFEEE],
  25723. [0x0649, 0xFEEF, null, null, 0xFEF0],
  25724. [0x064A, 0xFEF1, 0xFEF3, 0xFEF4, 0xFEF2],
  25725. [0x067E, 0xFB56, 0xFB58, 0xFB59, 0xFB57],
  25726. [0x06CC, 0xFBFC, 0xFBFE, 0xFBFF, 0xFBFD],
  25727. [0x0686, 0xFB7A, 0xFB7C, 0xFB7D, 0xFB7B],
  25728. [0x06A9, 0xFB8E, 0xFB90, 0xFB91, 0xFB8F],
  25729. [0x06AF, 0xFB92, 0xFB94, 0xFB95, 0xFB93],
  25730. [0x0698, 0xFB8A, null, null, 0xFB8B]];
  25731. ArabicReshaper.combCharsMap = [[[0x0644, 0x0622], 0xFEF5, null, null, 0xFEF6],
  25732. [[0x0644, 0x0623], 0xFEF7, null, null, 0xFEF8],
  25733. [[0x0644, 0x0625], 0xFEF9, null, null, 0xFEFA],
  25734. [[0x0644, 0x0627], 0xFEFB, null, null, 0xFEFC]];
  25735. ArabicReshaper.transChars = [0x0610,
  25736. 0x0612,
  25737. 0x0613,
  25738. 0x0614,
  25739. 0x0615,
  25740. 0x064B,
  25741. 0x064C,
  25742. 0x064D,
  25743. 0x064E,
  25744. 0x064F,
  25745. 0x0650,
  25746. 0x0651,
  25747. 0x0652,
  25748. 0x0653,
  25749. 0x0654,
  25750. 0x0655,
  25751. 0x0656,
  25752. 0x0657,
  25753. 0x0658,
  25754. 0x0670,
  25755. 0x06D6,
  25756. 0x06D7,
  25757. 0x06D8,
  25758. 0x06D9,
  25759. 0x06DA,
  25760. 0x06DB,
  25761. 0x06DC,
  25762. 0x06DF,
  25763. 0x06E0,
  25764. 0x06E1,
  25765. 0x06E2,
  25766. 0x06E3,
  25767. 0x06E4,
  25768. 0x06E7,
  25769. 0x06E8,
  25770. 0x06EA,
  25771. 0x06EB,
  25772. 0x06EC,
  25773. 0x06ED];
  25774. class MatirxArray {
  25775. static ArrayMul(a, b, o) {
  25776. if (!a) {
  25777. MatirxArray.copyArray(b, o);
  25778. return;
  25779. }
  25780. if (!b) {
  25781. MatirxArray.copyArray(a, o);
  25782. return;
  25783. }
  25784. var ai0, ai1, ai2, ai3;
  25785. for (var i = 0; i < 4; i++) {
  25786. ai0 = a[i];
  25787. ai1 = a[i + 4];
  25788. ai2 = a[i + 8];
  25789. ai3 = a[i + 12];
  25790. o[i] = ai0 * b[0] + ai1 * b[1] + ai2 * b[2] + ai3 * b[3];
  25791. o[i + 4] = ai0 * b[4] + ai1 * b[5] + ai2 * b[6] + ai3 * b[7];
  25792. o[i + 8] = ai0 * b[8] + ai1 * b[9] + ai2 * b[10] + ai3 * b[11];
  25793. o[i + 12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15];
  25794. }
  25795. }
  25796. static copyArray(f, t) {
  25797. if (!f)
  25798. return;
  25799. if (!t)
  25800. return;
  25801. for (var i = 0; i < f.length; i++) {
  25802. t[i] = f[i];
  25803. }
  25804. }
  25805. }
  25806. exports.AlphaCmd = AlphaCmd;
  25807. exports.Animation = Animation;
  25808. exports.AnimationBase = AnimationBase;
  25809. exports.ArabicReshaper = ArabicReshaper;
  25810. exports.AtlasGrid = AtlasGrid;
  25811. exports.AtlasInfoManager = AtlasInfoManager;
  25812. exports.AudioSound = AudioSound;
  25813. exports.AudioSoundChannel = AudioSoundChannel;
  25814. exports.BasePoly = BasePoly;
  25815. exports.BaseShader = BaseShader;
  25816. exports.BaseTexture = BaseTexture;
  25817. exports.Bezier = Bezier;
  25818. exports.Bitmap = Bitmap;
  25819. exports.BitmapFont = BitmapFont;
  25820. exports.BlendMode = BlendMode;
  25821. exports.BlurFilter = BlurFilter;
  25822. exports.BlurFilterGLRender = BlurFilterGLRender;
  25823. exports.BlurFilterSetter = BlurFilterSetter;
  25824. exports.BoundsStyle = BoundsStyle;
  25825. exports.Browser = Browser;
  25826. exports.Buffer = Buffer;
  25827. exports.Buffer2D = Buffer2D;
  25828. exports.BufferState2D = BufferState2D;
  25829. exports.BufferStateBase = BufferStateBase;
  25830. exports.ButtonEffect = ButtonEffect;
  25831. exports.Byte = Byte;
  25832. exports.CONST3D2D = CONST3D2D;
  25833. exports.CacheManger = CacheManger;
  25834. exports.CacheStyle = CacheStyle;
  25835. exports.CallLater = CallLater;
  25836. exports.CharRenderInfo = CharRenderInfo;
  25837. exports.CharRender_Canvas = CharRender_Canvas;
  25838. exports.CharRender_Native = CharRender_Native;
  25839. exports.CharSubmitCache = CharSubmitCache;
  25840. exports.ClassUtils = ClassUtils;
  25841. exports.ClipRectCmd = ClipRectCmd;
  25842. exports.ColorFilter = ColorFilter;
  25843. exports.ColorFilterSetter = ColorFilterSetter;
  25844. exports.ColorUtils = ColorUtils;
  25845. exports.CommandEncoder = CommandEncoder;
  25846. exports.CommonScript = CommonScript;
  25847. exports.Component = Component;
  25848. exports.Config = Config;
  25849. exports.Const = Const;
  25850. exports.Context = Context;
  25851. exports.Dragging = Dragging;
  25852. exports.Draw9GridTexture = Draw9GridTexture;
  25853. exports.DrawCircleCmd = DrawCircleCmd;
  25854. exports.DrawCurvesCmd = DrawCurvesCmd;
  25855. exports.DrawImageCmd = DrawImageCmd;
  25856. exports.DrawLineCmd = DrawLineCmd;
  25857. exports.DrawLinesCmd = DrawLinesCmd;
  25858. exports.DrawParticleCmd = DrawParticleCmd;
  25859. exports.DrawPathCmd = DrawPathCmd;
  25860. exports.DrawPieCmd = DrawPieCmd;
  25861. exports.DrawPolyCmd = DrawPolyCmd;
  25862. exports.DrawRectCmd = DrawRectCmd;
  25863. exports.DrawStyle = DrawStyle;
  25864. exports.DrawTextureCmd = DrawTextureCmd;
  25865. exports.DrawTexturesCmd = DrawTexturesCmd;
  25866. exports.DrawTrianglesCmd = DrawTrianglesCmd;
  25867. exports.Earcut = Earcut;
  25868. exports.EarcutNode = EarcutNode;
  25869. exports.Ease = Ease;
  25870. exports.EffectAnimation = EffectAnimation;
  25871. exports.EffectBase = EffectBase;
  25872. exports.Event = Event;
  25873. exports.EventDispatcher = EventDispatcher;
  25874. exports.FadeIn = FadeIn;
  25875. exports.FadeOut = FadeOut;
  25876. exports.FillTextCmd = FillTextCmd;
  25877. exports.FillTextureCmd = FillTextureCmd;
  25878. exports.Filter = Filter;
  25879. exports.FilterSetterBase = FilterSetterBase;
  25880. exports.FontInfo = FontInfo;
  25881. exports.FrameAnimation = FrameAnimation;
  25882. exports.GlowFilter = GlowFilter;
  25883. exports.GlowFilterGLRender = GlowFilterGLRender;
  25884. exports.GlowFilterSetter = GlowFilterSetter;
  25885. exports.GrahamScan = GrahamScan;
  25886. exports.GraphicAnimation = GraphicAnimation;
  25887. exports.Graphics = Graphics;
  25888. exports.GraphicsBounds = GraphicsBounds;
  25889. exports.HTMLCanvas = HTMLCanvas;
  25890. exports.HTMLChar = HTMLChar;
  25891. exports.HTMLImage = HTMLImage;
  25892. exports.HalfFloatUtils = HalfFloatUtils;
  25893. exports.Handler = Handler;
  25894. exports.HitArea = HitArea;
  25895. exports.HttpRequest = HttpRequest;
  25896. exports.ICharRender = ICharRender;
  25897. exports.ILaya = ILaya;
  25898. exports.IStatRender = IStatRender;
  25899. exports.IndexBuffer2D = IndexBuffer2D;
  25900. exports.InlcudeFile = InlcudeFile;
  25901. exports.Input = Input;
  25902. exports.KeyBoardManager = KeyBoardManager;
  25903. exports.KeyLocation = KeyLocation;
  25904. exports.Keyboard = Keyboard;
  25905. exports.Laya = Laya;
  25906. exports.LayaGL = LayaGL;
  25907. exports.LayaGLQuickRunner = LayaGLQuickRunner;
  25908. exports.LayaGLRunner = LayaGLRunner;
  25909. exports.LayaGPU = LayaGPU;
  25910. exports.Loader = Loader;
  25911. exports.LoaderManager = LoaderManager;
  25912. exports.LocalStorage = LocalStorage;
  25913. exports.Log = Log;
  25914. exports.MathUtil = MathUtil;
  25915. exports.MatirxArray = MatirxArray;
  25916. exports.Matrix = Matrix;
  25917. exports.Mesh2D = Mesh2D;
  25918. exports.MeshParticle2D = MeshParticle2D;
  25919. exports.MeshQuadTexture = MeshQuadTexture;
  25920. exports.MeshTexture = MeshTexture;
  25921. exports.MeshVG = MeshVG;
  25922. exports.Mouse = Mouse;
  25923. exports.MouseManager = MouseManager;
  25924. exports.Node = Node;
  25925. exports.Path = Path;
  25926. exports.PerfData = PerfData;
  25927. exports.PerfHUD = PerfHUD;
  25928. exports.Point = Point;
  25929. exports.Pool = Pool;
  25930. exports.PoolCache = PoolCache;
  25931. exports.Prefab = Prefab;
  25932. exports.PrimitiveSV = PrimitiveSV;
  25933. exports.QuickTestTool = QuickTestTool;
  25934. exports.Rectangle = Rectangle;
  25935. exports.Render = Render;
  25936. exports.RenderInfo = RenderInfo;
  25937. exports.RenderSprite = RenderSprite;
  25938. exports.RenderState2D = RenderState2D;
  25939. exports.RenderTexture2D = RenderTexture2D;
  25940. exports.Resource = Resource;
  25941. exports.ResourceVersion = ResourceVersion;
  25942. exports.RestoreCmd = RestoreCmd;
  25943. exports.RotateCmd = RotateCmd;
  25944. exports.RunDriver = RunDriver;
  25945. exports.SaveBase = SaveBase;
  25946. exports.SaveClipRect = SaveClipRect;
  25947. exports.SaveCmd = SaveCmd;
  25948. exports.SaveMark = SaveMark;
  25949. exports.SaveTransform = SaveTransform;
  25950. exports.SaveTranslate = SaveTranslate;
  25951. exports.ScaleCmd = ScaleCmd;
  25952. exports.Scene = Scene;
  25953. exports.SceneLoader = SceneLoader;
  25954. exports.SceneUtils = SceneUtils;
  25955. exports.Script = Script;
  25956. exports.Shader = Shader;
  25957. exports.Shader2D = Shader2D;
  25958. exports.Shader2X = Shader2X;
  25959. exports.ShaderCompile = ShaderCompile;
  25960. exports.ShaderDefines2D = ShaderDefines2D;
  25961. exports.ShaderDefinesBase = ShaderDefinesBase;
  25962. exports.ShaderNode = ShaderNode;
  25963. exports.ShaderValue = ShaderValue;
  25964. exports.SkinMeshBuffer = SkinMeshBuffer;
  25965. exports.SkinSV = SkinSV;
  25966. exports.Socket = Socket;
  25967. exports.Sound = Sound;
  25968. exports.SoundChannel = SoundChannel;
  25969. exports.SoundManager = SoundManager;
  25970. exports.SoundNode = SoundNode;
  25971. exports.Sprite = Sprite;
  25972. exports.SpriteConst = SpriteConst;
  25973. exports.SpriteStyle = SpriteStyle;
  25974. exports.Stage = Stage;
  25975. exports.Stat = Stat;
  25976. exports.StatUI = StatUI;
  25977. exports.StringKey = StringKey;
  25978. exports.Submit = Submit;
  25979. exports.SubmitBase = SubmitBase;
  25980. exports.SubmitCMD = SubmitCMD;
  25981. exports.SubmitCanvas = SubmitCanvas;
  25982. exports.SubmitKey = SubmitKey;
  25983. exports.SubmitTarget = SubmitTarget;
  25984. exports.SubmitTexture = SubmitTexture;
  25985. exports.System = System;
  25986. exports.SystemUtils = SystemUtils;
  25987. exports.TTFLoader = TTFLoader;
  25988. exports.Text = Text;
  25989. exports.TextAtlas = TextAtlas;
  25990. exports.TextRender = TextRender;
  25991. exports.TextStyle = TextStyle;
  25992. exports.TextTexture = TextTexture;
  25993. exports.Texture = Texture;
  25994. exports.Texture2D = Texture2D;
  25995. exports.TextureSV = TextureSV;
  25996. exports.TimeLine = TimeLine;
  25997. exports.Timer = Timer;
  25998. exports.TouchManager = TouchManager;
  25999. exports.TransformCmd = TransformCmd;
  26000. exports.TranslateCmd = TranslateCmd;
  26001. exports.Tween = Tween;
  26002. exports.URL = URL;
  26003. exports.Utils = Utils;
  26004. exports.Value2D = Value2D;
  26005. exports.VectorGraphManager = VectorGraphManager;
  26006. exports.VertexArrayObject = VertexArrayObject;
  26007. exports.VertexBuffer2D = VertexBuffer2D;
  26008. exports.VideoTexture = VideoTexture;
  26009. exports.WeakObject = WeakObject;
  26010. exports.WebAudioSound = WebAudioSound;
  26011. exports.WebAudioSoundChannel = WebAudioSoundChannel;
  26012. exports.WebGL = WebGL;
  26013. exports.WebGLCacheAsNormalCanvas = WebGLCacheAsNormalCanvas;
  26014. exports.WebGLContext = WebGLContext;
  26015. exports.WebGLRTMgr = WebGLRTMgr;
  26016. exports.WordText = WordText;
  26017. exports.WorkerLoader = WorkerLoader;
  26018. exports.__init = __init;
  26019. exports._static = _static;
  26020. exports.alertGlobalError = alertGlobalError;
  26021. exports.enableDebugPanel = enableDebugPanel;
  26022. exports.init = init;
  26023. exports.isWXOpenDataContext = isWXOpenDataContext;
  26024. exports.isWXPosMsg = isWXPosMsg;
  26025. exports.version = version;
  26026. exports.static=_static;
  26027. return exports;
  26028. }({}));