laya.core.js 980 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701
  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 = false;
  11. Config.webGL2D_MeshAllocMaxMem = true;
  12. Config.is2DPixelArtGame = false;
  13. Config.useWebGL2 = true;
  14. Config.useRetinalCanvas = false;
  15. window.Config = Config;
  16. class ILaya {
  17. static regClass(c) {
  18. ILaya.__classMap[c.name] = c;
  19. }
  20. }
  21. ILaya.Laya = null;
  22. ILaya.Timer = null;
  23. ILaya.WorkerLoader = null;
  24. ILaya.Dragging = null;
  25. ILaya.GraphicsBounds = null;
  26. ILaya.Sprite = null;
  27. ILaya.TextRender = null;
  28. ILaya.TextAtlas = null;
  29. ILaya.timer = null;
  30. ILaya.systemTimer = null;
  31. ILaya.startTimer = null;
  32. ILaya.updateTimer = null;
  33. ILaya.lateTimer = null;
  34. ILaya.physicsTimer = null;
  35. ILaya.stage = null;
  36. ILaya.Loader = null;
  37. ILaya.loader = null;
  38. ILaya.TTFLoader = null;
  39. ILaya.SoundManager = null;
  40. ILaya.WebAudioSound = null;
  41. ILaya.AudioSound = null;
  42. ILaya.ShaderCompile = null;
  43. ILaya.ClassUtils = null;
  44. ILaya.SceneUtils = null;
  45. ILaya.Context = null;
  46. ILaya.Render = null;
  47. ILaya.MouseManager = null;
  48. ILaya.Text = null;
  49. ILaya.Browser = null;
  50. ILaya.WebGL = null;
  51. ILaya.Pool = null;
  52. ILaya.Utils = null;
  53. ILaya.Graphics = null;
  54. ILaya.Submit = null;
  55. ILaya.Stage = null;
  56. ILaya.Resource = null;
  57. ILaya.__classMap = {};
  58. class Pool {
  59. static getPoolBySign(sign) {
  60. return Pool._poolDic[sign] || (Pool._poolDic[sign] = []);
  61. }
  62. static clearBySign(sign) {
  63. if (Pool._poolDic[sign])
  64. Pool._poolDic[sign].length = 0;
  65. }
  66. static recover(sign, item) {
  67. if (item[Pool.POOLSIGN])
  68. return;
  69. item[Pool.POOLSIGN] = true;
  70. Pool.getPoolBySign(sign).push(item);
  71. }
  72. static recoverByClass(instance) {
  73. if (instance) {
  74. var className = instance["__className"] || instance.constructor._$gid;
  75. if (className)
  76. Pool.recover(className, instance);
  77. }
  78. }
  79. static _getClassSign(cla) {
  80. var className = cla["__className"] || cla["_$gid"];
  81. if (!className) {
  82. cla["_$gid"] = className = Pool._CLSID + "";
  83. Pool._CLSID++;
  84. }
  85. return className;
  86. }
  87. static createByClass(cls) {
  88. return Pool.getItemByClass(Pool._getClassSign(cls), cls);
  89. }
  90. static getItemByClass(sign, cls) {
  91. if (!Pool._poolDic[sign])
  92. return new cls();
  93. var pool = Pool.getPoolBySign(sign);
  94. if (pool.length) {
  95. var rst = pool.pop();
  96. rst[Pool.POOLSIGN] = false;
  97. }
  98. else {
  99. rst = new cls();
  100. }
  101. return rst;
  102. }
  103. static getItemByCreateFun(sign, createFun, caller = null) {
  104. var pool = Pool.getPoolBySign(sign);
  105. var rst = pool.length ? pool.pop() : createFun.call(caller);
  106. rst[Pool.POOLSIGN] = false;
  107. return rst;
  108. }
  109. static getItem(sign) {
  110. var pool = Pool.getPoolBySign(sign);
  111. var rst = pool.length ? pool.pop() : null;
  112. if (rst) {
  113. rst[Pool.POOLSIGN] = false;
  114. }
  115. return rst;
  116. }
  117. }
  118. Pool._CLSID = 0;
  119. Pool.POOLSIGN = "__InPool";
  120. Pool._poolDic = {};
  121. class AlphaCmd {
  122. static create(alpha) {
  123. var cmd = Pool.getItemByClass("AlphaCmd", AlphaCmd);
  124. cmd.alpha = alpha;
  125. return cmd;
  126. }
  127. recover() {
  128. Pool.recover("AlphaCmd", this);
  129. }
  130. run(context, gx, gy) {
  131. context.alpha(this.alpha);
  132. }
  133. get cmdID() {
  134. return AlphaCmd.ID;
  135. }
  136. }
  137. AlphaCmd.ID = "Alpha";
  138. class DrawCircleCmd {
  139. static create(x, y, radius, fillColor, lineColor, lineWidth, vid) {
  140. var cmd = Pool.getItemByClass("DrawCircleCmd", DrawCircleCmd);
  141. cmd.x = x;
  142. cmd.y = y;
  143. cmd.radius = radius;
  144. cmd.fillColor = fillColor;
  145. cmd.lineColor = lineColor;
  146. cmd.lineWidth = lineWidth;
  147. cmd.vid = vid;
  148. return cmd;
  149. }
  150. recover() {
  151. this.fillColor = null;
  152. this.lineColor = null;
  153. Pool.recover("DrawCircleCmd", this);
  154. }
  155. run(context, gx, gy) {
  156. context._drawCircle(this.x + gx, this.y + gy, this.radius, this.fillColor, this.lineColor, this.lineWidth, this.vid);
  157. }
  158. get cmdID() {
  159. return DrawCircleCmd.ID;
  160. }
  161. }
  162. DrawCircleCmd.ID = "DrawCircle";
  163. class DrawCurvesCmd {
  164. static create(x, y, points, lineColor, lineWidth) {
  165. var cmd = Pool.getItemByClass("DrawCurvesCmd", DrawCurvesCmd);
  166. cmd.x = x;
  167. cmd.y = y;
  168. cmd.points = points;
  169. cmd.lineColor = lineColor;
  170. cmd.lineWidth = lineWidth;
  171. return cmd;
  172. }
  173. recover() {
  174. this.points = null;
  175. this.lineColor = null;
  176. Pool.recover("DrawCurvesCmd", this);
  177. }
  178. run(context, gx, gy) {
  179. if (this.points)
  180. context.drawCurves(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth);
  181. }
  182. get cmdID() {
  183. return DrawCurvesCmd.ID;
  184. }
  185. }
  186. DrawCurvesCmd.ID = "DrawCurves";
  187. class DrawImageCmd {
  188. static create(texture, x, y, width, height) {
  189. var cmd = Pool.getItemByClass("DrawImageCmd", DrawImageCmd);
  190. cmd.texture = texture;
  191. texture._addReference();
  192. cmd.x = x;
  193. cmd.y = y;
  194. cmd.width = width;
  195. cmd.height = height;
  196. return cmd;
  197. }
  198. recover() {
  199. this.texture && this.texture._removeReference();
  200. this.texture = null;
  201. Pool.recover("DrawImageCmd", this);
  202. }
  203. run(context, gx, gy) {
  204. if (this.texture)
  205. context.drawTexture(this.texture, this.x + gx, this.y + gy, this.width, this.height);
  206. }
  207. get cmdID() {
  208. return DrawImageCmd.ID;
  209. }
  210. }
  211. DrawImageCmd.ID = "DrawImage";
  212. class DrawLineCmd {
  213. static create(fromX, fromY, toX, toY, lineColor, lineWidth, vid) {
  214. var cmd = Pool.getItemByClass("DrawLineCmd", DrawLineCmd);
  215. cmd.fromX = fromX;
  216. cmd.fromY = fromY;
  217. cmd.toX = toX;
  218. cmd.toY = toY;
  219. cmd.lineColor = lineColor;
  220. cmd.lineWidth = lineWidth;
  221. cmd.vid = vid;
  222. return cmd;
  223. }
  224. recover() {
  225. Pool.recover("DrawLineCmd", this);
  226. }
  227. run(context, gx, gy) {
  228. context._drawLine(gx, gy, this.fromX, this.fromY, this.toX, this.toY, this.lineColor, this.lineWidth, this.vid);
  229. }
  230. get cmdID() {
  231. return DrawLineCmd.ID;
  232. }
  233. }
  234. DrawLineCmd.ID = "DrawLine";
  235. class DrawLinesCmd {
  236. static create(x, y, points, lineColor, lineWidth, vid) {
  237. var cmd = Pool.getItemByClass("DrawLinesCmd", DrawLinesCmd);
  238. cmd.x = x;
  239. cmd.y = y;
  240. cmd.points = points;
  241. cmd.lineColor = lineColor;
  242. cmd.lineWidth = lineWidth;
  243. cmd.vid = vid;
  244. return cmd;
  245. }
  246. recover() {
  247. this.points = null;
  248. this.lineColor = null;
  249. Pool.recover("DrawLinesCmd", this);
  250. }
  251. run(context, gx, gy) {
  252. this.points && context._drawLines(this.x + gx, this.y + gy, this.points, this.lineColor, this.lineWidth, this.vid);
  253. }
  254. get cmdID() {
  255. return DrawLinesCmd.ID;
  256. }
  257. }
  258. DrawLinesCmd.ID = "DrawLines";
  259. class DrawPathCmd {
  260. static create(x, y, paths, brush, pen) {
  261. var cmd = Pool.getItemByClass("DrawPathCmd", DrawPathCmd);
  262. cmd.x = x;
  263. cmd.y = y;
  264. cmd.paths = paths;
  265. cmd.brush = brush;
  266. cmd.pen = pen;
  267. return cmd;
  268. }
  269. recover() {
  270. this.paths = null;
  271. this.brush = null;
  272. this.pen = null;
  273. Pool.recover("DrawPathCmd", this);
  274. }
  275. run(context, gx, gy) {
  276. this.paths && context._drawPath(this.x + gx, this.y + gy, this.paths, this.brush, this.pen);
  277. }
  278. get cmdID() {
  279. return DrawPathCmd.ID;
  280. }
  281. }
  282. DrawPathCmd.ID = "DrawPath";
  283. class DrawPieCmd {
  284. static create(x, y, radius, startAngle, endAngle, fillColor, lineColor, lineWidth, vid) {
  285. var cmd = Pool.getItemByClass("DrawPieCmd", DrawPieCmd);
  286. cmd.x = x;
  287. cmd.y = y;
  288. cmd.radius = radius;
  289. cmd._startAngle = startAngle;
  290. cmd._endAngle = endAngle;
  291. cmd.fillColor = fillColor;
  292. cmd.lineColor = lineColor;
  293. cmd.lineWidth = lineWidth;
  294. cmd.vid = vid;
  295. return cmd;
  296. }
  297. recover() {
  298. this.fillColor = null;
  299. this.lineColor = null;
  300. Pool.recover("DrawPieCmd", this);
  301. }
  302. run(context, gx, gy) {
  303. context._drawPie(this.x + gx, this.y + gy, this.radius, this._startAngle, this._endAngle, this.fillColor, this.lineColor, this.lineWidth, this.vid);
  304. }
  305. get cmdID() {
  306. return DrawPieCmd.ID;
  307. }
  308. get startAngle() {
  309. return this._startAngle * 180 / Math.PI;
  310. }
  311. set startAngle(value) {
  312. this._startAngle = value * Math.PI / 180;
  313. }
  314. get endAngle() {
  315. return this._endAngle * 180 / Math.PI;
  316. }
  317. set endAngle(value) {
  318. this._endAngle = value * Math.PI / 180;
  319. }
  320. }
  321. DrawPieCmd.ID = "DrawPie";
  322. class DrawPolyCmd {
  323. static create(x, y, points, fillColor, lineColor, lineWidth, isConvexPolygon, vid) {
  324. var cmd = Pool.getItemByClass("DrawPolyCmd", DrawPolyCmd);
  325. cmd.x = x;
  326. cmd.y = y;
  327. cmd.points = points;
  328. cmd.fillColor = fillColor;
  329. cmd.lineColor = lineColor;
  330. cmd.lineWidth = lineWidth;
  331. cmd.isConvexPolygon = isConvexPolygon;
  332. cmd.vid = vid;
  333. return cmd;
  334. }
  335. recover() {
  336. this.points = null;
  337. this.fillColor = null;
  338. this.lineColor = null;
  339. Pool.recover("DrawPolyCmd", this);
  340. }
  341. run(context, gx, gy) {
  342. this.points && context._drawPoly(this.x + gx, this.y + gy, this.points, this.fillColor, this.lineColor, this.lineWidth, this.isConvexPolygon, this.vid);
  343. }
  344. get cmdID() {
  345. return DrawPolyCmd.ID;
  346. }
  347. }
  348. DrawPolyCmd.ID = "DrawPoly";
  349. class DrawRectCmd {
  350. static create(x, y, width, height, fillColor, lineColor, lineWidth) {
  351. var cmd = Pool.getItemByClass("DrawRectCmd", DrawRectCmd);
  352. cmd.x = x;
  353. cmd.y = y;
  354. cmd.width = width;
  355. cmd.height = height;
  356. cmd.fillColor = fillColor;
  357. cmd.lineColor = lineColor;
  358. cmd.lineWidth = lineWidth;
  359. return cmd;
  360. }
  361. recover() {
  362. this.fillColor = null;
  363. this.lineColor = null;
  364. Pool.recover("DrawRectCmd", this);
  365. }
  366. run(context, gx, gy) {
  367. context.drawRect(this.x + gx, this.y + gy, this.width, this.height, this.fillColor, this.lineColor, this.lineWidth);
  368. }
  369. get cmdID() {
  370. return DrawRectCmd.ID;
  371. }
  372. }
  373. DrawRectCmd.ID = "DrawRect";
  374. class Matrix {
  375. constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0, nums = 0) {
  376. this._bTransform = false;
  377. if (Matrix._createFun != null) {
  378. return Matrix._createFun(a, b, c, d, tx, ty, nums);
  379. }
  380. this.a = a;
  381. this.b = b;
  382. this.c = c;
  383. this.d = d;
  384. this.tx = tx;
  385. this.ty = ty;
  386. this._checkTransform();
  387. }
  388. identity() {
  389. this.a = this.d = 1;
  390. this.b = this.tx = this.ty = this.c = 0;
  391. this._bTransform = false;
  392. return this;
  393. }
  394. _checkTransform() {
  395. return this._bTransform = (this.a !== 1 || this.b !== 0 || this.c !== 0 || this.d !== 1);
  396. }
  397. setTranslate(x, y) {
  398. this.tx = x;
  399. this.ty = y;
  400. return this;
  401. }
  402. translate(x, y) {
  403. this.tx += x;
  404. this.ty += y;
  405. return this;
  406. }
  407. scale(x, y) {
  408. this.a *= x;
  409. this.d *= y;
  410. this.c *= x;
  411. this.b *= y;
  412. this.tx *= x;
  413. this.ty *= y;
  414. this._bTransform = true;
  415. return this;
  416. }
  417. rotate(angle) {
  418. var cos = Math.cos(angle);
  419. var sin = Math.sin(angle);
  420. var a1 = this.a;
  421. var c1 = this.c;
  422. var tx1 = this.tx;
  423. this.a = a1 * cos - this.b * sin;
  424. this.b = a1 * sin + this.b * cos;
  425. this.c = c1 * cos - this.d * sin;
  426. this.d = c1 * sin + this.d * cos;
  427. this.tx = tx1 * cos - this.ty * sin;
  428. this.ty = tx1 * sin + this.ty * cos;
  429. this._bTransform = true;
  430. return this;
  431. }
  432. skew(x, y) {
  433. var tanX = Math.tan(x);
  434. var tanY = Math.tan(y);
  435. var a1 = this.a;
  436. var b1 = this.b;
  437. this.a += tanY * this.c;
  438. this.b += tanY * this.d;
  439. this.c += tanX * a1;
  440. this.d += tanX * b1;
  441. return this;
  442. }
  443. invertTransformPoint(out) {
  444. var a1 = this.a;
  445. var b1 = this.b;
  446. var c1 = this.c;
  447. var d1 = this.d;
  448. var tx1 = this.tx;
  449. var n = a1 * d1 - b1 * c1;
  450. var a2 = d1 / n;
  451. var b2 = -b1 / n;
  452. var c2 = -c1 / n;
  453. var d2 = a1 / n;
  454. var tx2 = (c1 * this.ty - d1 * tx1) / n;
  455. var ty2 = -(a1 * this.ty - b1 * tx1) / n;
  456. return out.setTo(a2 * out.x + c2 * out.y + tx2, b2 * out.x + d2 * out.y + ty2);
  457. }
  458. transformPoint(out) {
  459. return out.setTo(this.a * out.x + this.c * out.y + this.tx, this.b * out.x + this.d * out.y + this.ty);
  460. }
  461. transformPointN(out) {
  462. return out.setTo(this.a * out.x + this.c * out.y, this.b * out.x + this.d * out.y);
  463. }
  464. getScaleX() {
  465. return this.b === 0 ? this.a : Math.sqrt(this.a * this.a + this.b * this.b);
  466. }
  467. getScaleY() {
  468. return this.c === 0 ? this.d : Math.sqrt(this.c * this.c + this.d * this.d);
  469. }
  470. invert() {
  471. var a1 = this.a;
  472. var b1 = this.b;
  473. var c1 = this.c;
  474. var d1 = this.d;
  475. var tx1 = this.tx;
  476. var n = a1 * d1 - b1 * c1;
  477. this.a = d1 / n;
  478. this.b = -b1 / n;
  479. this.c = -c1 / n;
  480. this.d = a1 / n;
  481. this.tx = (c1 * this.ty - d1 * tx1) / n;
  482. this.ty = -(a1 * this.ty - b1 * tx1) / n;
  483. return this;
  484. }
  485. setTo(a, b, c, d, tx, ty) {
  486. this.a = a, this.b = b, this.c = c, this.d = d, this.tx = tx, this.ty = ty;
  487. return this;
  488. }
  489. concat(matrix) {
  490. var a = this.a;
  491. var c = this.c;
  492. var tx = this.tx;
  493. this.a = a * matrix.a + this.b * matrix.c;
  494. this.b = a * matrix.b + this.b * matrix.d;
  495. this.c = c * matrix.a + this.d * matrix.c;
  496. this.d = c * matrix.b + this.d * matrix.d;
  497. this.tx = tx * matrix.a + this.ty * matrix.c + matrix.tx;
  498. this.ty = tx * matrix.b + this.ty * matrix.d + matrix.ty;
  499. return this;
  500. }
  501. static mul(m1, m2, out) {
  502. var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
  503. var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
  504. if (bb !== 0 || bc !== 0) {
  505. out.a = aa * ba + ab * bc;
  506. out.b = aa * bb + ab * bd;
  507. out.c = ac * ba + ad * bc;
  508. out.d = ac * bb + ad * bd;
  509. out.tx = ba * atx + bc * aty + btx;
  510. out.ty = bb * atx + bd * aty + bty;
  511. }
  512. else {
  513. out.a = aa * ba;
  514. out.b = ab * bd;
  515. out.c = ac * ba;
  516. out.d = ad * bd;
  517. out.tx = ba * atx + btx;
  518. out.ty = bd * aty + bty;
  519. }
  520. return out;
  521. }
  522. static mul16(m1, m2, out) {
  523. var aa = m1.a, ab = m1.b, ac = m1.c, ad = m1.d, atx = m1.tx, aty = m1.ty;
  524. var ba = m2.a, bb = m2.b, bc = m2.c, bd = m2.d, btx = m2.tx, bty = m2.ty;
  525. if (bb !== 0 || bc !== 0) {
  526. out[0] = aa * ba + ab * bc;
  527. out[1] = aa * bb + ab * bd;
  528. out[4] = ac * ba + ad * bc;
  529. out[5] = ac * bb + ad * bd;
  530. out[12] = ba * atx + bc * aty + btx;
  531. out[13] = bb * atx + bd * aty + bty;
  532. }
  533. else {
  534. out[0] = aa * ba;
  535. out[1] = ab * bd;
  536. out[4] = ac * ba;
  537. out[5] = ad * bd;
  538. out[12] = ba * atx + btx;
  539. out[13] = bd * aty + bty;
  540. }
  541. return out;
  542. }
  543. scaleEx(x, y) {
  544. var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
  545. if (bb !== 0 || bc !== 0) {
  546. this.a = x * ba;
  547. this.b = x * bb;
  548. this.c = y * bc;
  549. this.d = y * bd;
  550. }
  551. else {
  552. this.a = x * ba;
  553. this.b = 0 * bd;
  554. this.c = 0 * ba;
  555. this.d = y * bd;
  556. }
  557. this._bTransform = true;
  558. }
  559. rotateEx(angle) {
  560. var cos = Math.cos(angle);
  561. var sin = Math.sin(angle);
  562. var ba = this.a, bb = this.b, bc = this.c, bd = this.d;
  563. if (bb !== 0 || bc !== 0) {
  564. this.a = cos * ba + sin * bc;
  565. this.b = cos * bb + sin * bd;
  566. this.c = -sin * ba + cos * bc;
  567. this.d = -sin * bb + cos * bd;
  568. }
  569. else {
  570. this.a = cos * ba;
  571. this.b = sin * bd;
  572. this.c = -sin * ba;
  573. this.d = cos * bd;
  574. }
  575. this._bTransform = true;
  576. }
  577. clone() {
  578. var dec = Matrix.create();
  579. dec.a = this.a;
  580. dec.b = this.b;
  581. dec.c = this.c;
  582. dec.d = this.d;
  583. dec.tx = this.tx;
  584. dec.ty = this.ty;
  585. dec._bTransform = this._bTransform;
  586. return dec;
  587. }
  588. copyTo(dec) {
  589. dec.a = this.a;
  590. dec.b = this.b;
  591. dec.c = this.c;
  592. dec.d = this.d;
  593. dec.tx = this.tx;
  594. dec.ty = this.ty;
  595. dec._bTransform = this._bTransform;
  596. return dec;
  597. }
  598. toString() {
  599. return this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.tx + "," + this.ty;
  600. }
  601. destroy() {
  602. this.recover();
  603. }
  604. recover() {
  605. Pool.recover("Matrix", this.identity());
  606. }
  607. static create() {
  608. return Pool.getItemByClass("Matrix", Matrix);
  609. }
  610. }
  611. Matrix.EMPTY = new Matrix();
  612. Matrix.TEMP = new Matrix();
  613. Matrix._createFun = null;
  614. class Point {
  615. constructor(x = 0, y = 0) {
  616. this.x = x;
  617. this.y = y;
  618. }
  619. static create() {
  620. return Pool.getItemByClass("Point", Point);
  621. }
  622. setTo(x, y) {
  623. this.x = x;
  624. this.y = y;
  625. return this;
  626. }
  627. reset() {
  628. this.x = this.y = 0;
  629. return this;
  630. }
  631. recover() {
  632. Pool.recover("Point", this.reset());
  633. }
  634. distance(x, y) {
  635. return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * (this.y - y));
  636. }
  637. toString() {
  638. return this.x + "," + this.y;
  639. }
  640. normalize() {
  641. var d = Math.sqrt(this.x * this.x + this.y * this.y);
  642. if (d > 0) {
  643. var id = 1.0 / d;
  644. this.x *= id;
  645. this.y *= id;
  646. }
  647. }
  648. copy(point) {
  649. return this.setTo(point.x, point.y);
  650. }
  651. }
  652. Point.TEMP = new Point();
  653. Point.EMPTY = new Point();
  654. class Rectangle {
  655. constructor(x = 0, y = 0, width = 0, height = 0) {
  656. this.x = x;
  657. this.y = y;
  658. this.width = width;
  659. this.height = height;
  660. }
  661. get right() {
  662. return this.x + this.width;
  663. }
  664. get bottom() {
  665. return this.y + this.height;
  666. }
  667. setTo(x, y, width, height) {
  668. this.x = x;
  669. this.y = y;
  670. this.width = width;
  671. this.height = height;
  672. return this;
  673. }
  674. reset() {
  675. this.x = this.y = this.width = this.height = 0;
  676. return this;
  677. }
  678. recover() {
  679. if (this == Rectangle.TEMP || this == Rectangle.EMPTY) {
  680. console.log("recover Temp or Empty:", this);
  681. return;
  682. }
  683. Pool.recover("Rectangle", this.reset());
  684. }
  685. static create() {
  686. return Pool.getItemByClass("Rectangle", Rectangle);
  687. }
  688. copyFrom(source) {
  689. this.x = source.x;
  690. this.y = source.y;
  691. this.width = source.width;
  692. this.height = source.height;
  693. return this;
  694. }
  695. contains(x, y) {
  696. if (this.width <= 0 || this.height <= 0)
  697. return false;
  698. if (x >= this.x && x < this.right) {
  699. if (y >= this.y && y < this.bottom) {
  700. return true;
  701. }
  702. }
  703. return false;
  704. }
  705. intersects(rect) {
  706. 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);
  707. }
  708. intersection(rect, out = null) {
  709. if (!this.intersects(rect))
  710. return null;
  711. out || (out = new Rectangle());
  712. out.x = Math.max(this.x, rect.x);
  713. out.y = Math.max(this.y, rect.y);
  714. out.width = Math.min(this.right, rect.right) - out.x;
  715. out.height = Math.min(this.bottom, rect.bottom) - out.y;
  716. return out;
  717. }
  718. union(source, out = null) {
  719. out || (out = new Rectangle());
  720. this.clone(out);
  721. if (source.width <= 0 || source.height <= 0)
  722. return out;
  723. out.addPoint(source.x, source.y);
  724. out.addPoint(source.right, source.bottom);
  725. return this;
  726. }
  727. clone(out = null) {
  728. out || (out = new Rectangle());
  729. out.x = this.x;
  730. out.y = this.y;
  731. out.width = this.width;
  732. out.height = this.height;
  733. return out;
  734. }
  735. toString() {
  736. return this.x + "," + this.y + "," + this.width + "," + this.height;
  737. }
  738. equals(rect) {
  739. if (!rect || rect.x !== this.x || rect.y !== this.y || rect.width !== this.width || rect.height !== this.height)
  740. return false;
  741. return true;
  742. }
  743. addPoint(x, y) {
  744. this.x > x && (this.width += this.x - x, this.x = x);
  745. this.y > y && (this.height += this.y - y, this.y = y);
  746. if (this.width < x - this.x)
  747. this.width = x - this.x;
  748. if (this.height < y - this.y)
  749. this.height = y - this.y;
  750. return this;
  751. }
  752. _getBoundPoints() {
  753. var rst = Rectangle._temB;
  754. rst.length = 0;
  755. if (this.width == 0 || this.height == 0)
  756. return rst;
  757. 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);
  758. return rst;
  759. }
  760. static _getBoundPointS(x, y, width, height) {
  761. var rst = Rectangle._temA;
  762. rst.length = 0;
  763. if (width == 0 || height == 0)
  764. return rst;
  765. rst.push(x, y, x + width, y, x, y + height, x + width, y + height);
  766. return rst;
  767. }
  768. static _getWrapRec(pointList, rst = null) {
  769. if (!pointList || pointList.length < 1)
  770. return rst ? rst.setTo(0, 0, 0, 0) : Rectangle.TEMP.setTo(0, 0, 0, 0);
  771. rst = rst ? rst : Rectangle.create();
  772. var i, len = pointList.length, minX, maxX, minY, maxY, tPoint = Point.TEMP;
  773. minX = minY = 99999;
  774. maxX = maxY = -minX;
  775. for (i = 0; i < len; i += 2) {
  776. tPoint.x = pointList[i];
  777. tPoint.y = pointList[i + 1];
  778. minX = minX < tPoint.x ? minX : tPoint.x;
  779. minY = minY < tPoint.y ? minY : tPoint.y;
  780. maxX = maxX > tPoint.x ? maxX : tPoint.x;
  781. maxY = maxY > tPoint.y ? maxY : tPoint.y;
  782. }
  783. return rst.setTo(minX, minY, maxX - minX, maxY - minY);
  784. }
  785. isEmpty() {
  786. if (this.width <= 0 || this.height <= 0)
  787. return true;
  788. return false;
  789. }
  790. }
  791. Rectangle.EMPTY = new Rectangle();
  792. Rectangle.TEMP = new Rectangle();
  793. Rectangle._temB = [];
  794. Rectangle._temA = [];
  795. class LayaGL {
  796. }
  797. LayaGL.ARRAY_BUFFER_TYPE_DATA = 0;
  798. LayaGL.ARRAY_BUFFER_TYPE_CMD = 1;
  799. LayaGL.ARRAY_BUFFER_REF_REFERENCE = 0;
  800. LayaGL.ARRAY_BUFFER_REF_COPY = 1;
  801. LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_ID = 0;
  802. LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_DATA = 1;
  803. class WebGLContext {
  804. static __init__() {
  805. var gl = LayaGL.instance;
  806. WebGLContext._depthFunc = gl.LESS;
  807. WebGLContext._blendEquation = gl.FUNC_ADD;
  808. WebGLContext._blendEquationRGB = gl.FUNC_ADD;
  809. WebGLContext._blendEquationAlpha = gl.FUNC_ADD;
  810. WebGLContext._sFactor = gl.ONE;
  811. WebGLContext._dFactor = gl.ZERO;
  812. WebGLContext._sFactorAlpha = gl.ONE;
  813. WebGLContext._dFactorAlpha = gl.ZERO;
  814. WebGLContext._activedTextureID = gl.TEXTURE0;
  815. WebGLContext._glTextureIDs = [gl.TEXTURE0, gl.TEXTURE1, gl.TEXTURE2, gl.TEXTURE3, gl.TEXTURE4, gl.TEXTURE5, gl.TEXTURE6, gl.TEXTURE7];
  816. }
  817. static useProgram(gl, program) {
  818. if (WebGLContext._useProgram === program)
  819. return false;
  820. gl.useProgram(program);
  821. WebGLContext._useProgram = program;
  822. return true;
  823. }
  824. static setDepthTest(gl, value) {
  825. value !== WebGLContext._depthTest && (WebGLContext._depthTest = value, value ? gl.enable(gl.DEPTH_TEST) : gl.disable(gl.DEPTH_TEST));
  826. }
  827. static setDepthMask(gl, value) {
  828. value !== WebGLContext._depthMask && (WebGLContext._depthMask = value, gl.depthMask(value));
  829. }
  830. static setDepthFunc(gl, value) {
  831. value !== WebGLContext._depthFunc && (WebGLContext._depthFunc = value, gl.depthFunc(value));
  832. }
  833. static setBlend(gl, value) {
  834. value !== WebGLContext._blend && (WebGLContext._blend = value, value ? gl.enable(gl.BLEND) : gl.disable(gl.BLEND));
  835. }
  836. static setBlendEquation(gl, blendEquation) {
  837. if (blendEquation !== WebGLContext._blendEquation) {
  838. WebGLContext._blendEquation = blendEquation;
  839. WebGLContext._blendEquationRGB = WebGLContext._blendEquationAlpha = null;
  840. gl.blendEquation(blendEquation);
  841. }
  842. }
  843. static setBlendEquationSeparate(gl, blendEquationRGB, blendEquationAlpha) {
  844. if (blendEquationRGB !== WebGLContext._blendEquationRGB || blendEquationAlpha !== WebGLContext._blendEquationAlpha) {
  845. WebGLContext._blendEquationRGB = blendEquationRGB;
  846. WebGLContext._blendEquationAlpha = blendEquationAlpha;
  847. WebGLContext._blendEquation = null;
  848. gl.blendEquationSeparate(blendEquationRGB, blendEquationAlpha);
  849. }
  850. }
  851. static setBlendFunc(gl, sFactor, dFactor) {
  852. if (sFactor !== WebGLContext._sFactor || dFactor !== WebGLContext._dFactor) {
  853. WebGLContext._sFactor = sFactor;
  854. WebGLContext._dFactor = dFactor;
  855. WebGLContext._sFactorRGB = null;
  856. WebGLContext._dFactorRGB = null;
  857. WebGLContext._sFactorAlpha = null;
  858. WebGLContext._dFactorAlpha = null;
  859. gl.blendFunc(sFactor, dFactor);
  860. }
  861. }
  862. static setBlendFuncSeperate(gl, srcRGB, dstRGB, srcAlpha, dstAlpha) {
  863. if (srcRGB !== WebGLContext._sFactorRGB || dstRGB !== WebGLContext._dFactorRGB || srcAlpha !== WebGLContext._sFactorAlpha || dstAlpha !== WebGLContext._dFactorAlpha) {
  864. WebGLContext._sFactorRGB = srcRGB;
  865. WebGLContext._dFactorRGB = dstRGB;
  866. WebGLContext._sFactorAlpha = srcAlpha;
  867. WebGLContext._dFactorAlpha = dstAlpha;
  868. WebGLContext._sFactor = null;
  869. WebGLContext._dFactor = null;
  870. gl.blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
  871. }
  872. }
  873. static setCullFace(gl, value) {
  874. value !== WebGLContext._cullFace && (WebGLContext._cullFace = value, value ? gl.enable(gl.CULL_FACE) : gl.disable(gl.CULL_FACE));
  875. }
  876. static setFrontFace(gl, value) {
  877. value !== WebGLContext._frontFace && (WebGLContext._frontFace = value, gl.frontFace(value));
  878. }
  879. static activeTexture(gl, textureID) {
  880. if (WebGLContext._activedTextureID !== textureID) {
  881. gl.activeTexture(textureID);
  882. WebGLContext._activedTextureID = textureID;
  883. }
  884. }
  885. static bindTexture(gl, target, texture) {
  886. if (WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] !== texture) {
  887. gl.bindTexture(target, texture);
  888. WebGLContext._activeTextures[WebGLContext._activedTextureID - gl.TEXTURE0] = texture;
  889. }
  890. }
  891. static __init_native() {
  892. if (!ILaya.Render.supportWebGLPlusRendering)
  893. return;
  894. var webGLContext = WebGLContext;
  895. webGLContext.activeTexture = webGLContext.activeTextureForNative;
  896. webGLContext.bindTexture = webGLContext.bindTextureForNative;
  897. }
  898. static useProgramForNative(gl, program) {
  899. gl.useProgram(program);
  900. return true;
  901. }
  902. static setDepthTestForNative(gl, value) {
  903. if (value)
  904. gl.enable(gl.DEPTH_TEST);
  905. else
  906. gl.disable(gl.DEPTH_TEST);
  907. }
  908. static setDepthMaskForNative(gl, value) {
  909. gl.depthMask(value);
  910. }
  911. static setDepthFuncForNative(gl, value) {
  912. gl.depthFunc(value);
  913. }
  914. static setBlendForNative(gl, value) {
  915. if (value)
  916. gl.enable(gl.BLEND);
  917. else
  918. gl.disable(gl.BLEND);
  919. }
  920. static setBlendFuncForNative(gl, sFactor, dFactor) {
  921. gl.blendFunc(sFactor, dFactor);
  922. }
  923. static setCullFaceForNative(gl, value) {
  924. if (value)
  925. gl.enable(gl.CULL_FACE);
  926. else
  927. gl.disable(gl.CULL_FACE);
  928. }
  929. static setFrontFaceForNative(gl, value) {
  930. gl.frontFace(value);
  931. }
  932. static activeTextureForNative(gl, textureID) {
  933. gl.activeTexture(textureID);
  934. }
  935. static bindTextureForNative(gl, target, texture) {
  936. gl.bindTexture(target, texture);
  937. }
  938. static bindVertexArrayForNative(gl, vertexArray) {
  939. gl.bindVertexArray(vertexArray);
  940. }
  941. }
  942. WebGLContext._activeTextures = new Array(8);
  943. WebGLContext._useProgram = null;
  944. WebGLContext._depthTest = true;
  945. WebGLContext._depthMask = true;
  946. WebGLContext._blend = false;
  947. WebGLContext._cullFace = false;
  948. WebGLContext.mainContext = null;
  949. class Handler {
  950. constructor(caller = null, method = null, args = null, once = false) {
  951. this.once = false;
  952. this._id = 0;
  953. this.setTo(caller, method, args, once);
  954. }
  955. setTo(caller, method, args, once = false) {
  956. this._id = Handler._gid++;
  957. this.caller = caller;
  958. this.method = method;
  959. this.args = args;
  960. this.once = once;
  961. return this;
  962. }
  963. run() {
  964. if (this.method == null)
  965. return null;
  966. var id = this._id;
  967. var result = this.method.apply(this.caller, this.args);
  968. this._id === id && this.once && this.recover();
  969. return result;
  970. }
  971. runWith(data) {
  972. if (this.method == null)
  973. return null;
  974. var id = this._id;
  975. if (data == null)
  976. var result = this.method.apply(this.caller, this.args);
  977. else if (!this.args && !data.unshift)
  978. result = this.method.call(this.caller, data);
  979. else if (this.args)
  980. result = this.method.apply(this.caller, this.args.concat(data));
  981. else
  982. result = this.method.apply(this.caller, data);
  983. this._id === id && this.once && this.recover();
  984. return result;
  985. }
  986. clear() {
  987. this.caller = null;
  988. this.method = null;
  989. this.args = null;
  990. return this;
  991. }
  992. recover() {
  993. if (this._id > 0) {
  994. this._id = 0;
  995. Handler._pool.push(this.clear());
  996. }
  997. }
  998. static create(caller, method, args = null, once = true) {
  999. if (Handler._pool.length)
  1000. return Handler._pool.pop().setTo(caller, method, args, once);
  1001. return new Handler(caller, method, args, once);
  1002. }
  1003. }
  1004. Handler._pool = [];
  1005. Handler._gid = 1;
  1006. class EventDispatcher {
  1007. hasListener(type) {
  1008. var listener = this._events && this._events[type];
  1009. return !!listener;
  1010. }
  1011. event(type, data = null) {
  1012. if (!this._events || !this._events[type])
  1013. return false;
  1014. var listeners = this._events[type];
  1015. if (listeners.run) {
  1016. if (listeners.once)
  1017. delete this._events[type];
  1018. data != null ? listeners.runWith(data) : listeners.run();
  1019. }
  1020. else {
  1021. for (var i = 0, n = listeners.length; i < n; i++) {
  1022. var listener = listeners[i];
  1023. if (listener) {
  1024. (data != null) ? listener.runWith(data) : listener.run();
  1025. }
  1026. if (!listener || listener.once) {
  1027. listeners.splice(i, 1);
  1028. i--;
  1029. n--;
  1030. }
  1031. }
  1032. if (listeners.length === 0 && this._events)
  1033. delete this._events[type];
  1034. }
  1035. return true;
  1036. }
  1037. on(type, caller, listener, args = null) {
  1038. return this._createListener(type, caller, listener, args, false);
  1039. }
  1040. once(type, caller, listener, args = null) {
  1041. return this._createListener(type, caller, listener, args, true);
  1042. }
  1043. _createListener(type, caller, listener, args, once, offBefore = true) {
  1044. offBefore && this.off(type, caller, listener, once);
  1045. var handler = EventHandler.create(caller || this, listener, args, once);
  1046. this._events || (this._events = {});
  1047. var events = this._events;
  1048. if (!events[type])
  1049. events[type] = handler;
  1050. else {
  1051. if (!events[type].run)
  1052. events[type].push(handler);
  1053. else
  1054. events[type] = [events[type], handler];
  1055. }
  1056. return this;
  1057. }
  1058. off(type, caller, listener, onceOnly = false) {
  1059. if (!this._events || !this._events[type])
  1060. return this;
  1061. var listeners = this._events[type];
  1062. if (listeners != null) {
  1063. if (listeners.run) {
  1064. if ((!caller || listeners.caller === caller) && (listener == null || listeners.method === listener) && (!onceOnly || listeners.once)) {
  1065. delete this._events[type];
  1066. listeners.recover();
  1067. }
  1068. }
  1069. else {
  1070. var count = 0;
  1071. for (var i = 0, n = listeners.length; i < n; i++) {
  1072. var item = listeners[i];
  1073. if (!item) {
  1074. count++;
  1075. continue;
  1076. }
  1077. if (item && (!caller || item.caller === caller) && (listener == null || item.method === listener) && (!onceOnly || item.once)) {
  1078. count++;
  1079. listeners[i] = null;
  1080. item.recover();
  1081. }
  1082. }
  1083. if (count === n)
  1084. delete this._events[type];
  1085. }
  1086. }
  1087. return this;
  1088. }
  1089. offAll(type = null) {
  1090. var events = this._events;
  1091. if (!events)
  1092. return this;
  1093. if (type) {
  1094. this._recoverHandlers(events[type]);
  1095. delete events[type];
  1096. }
  1097. else {
  1098. for (var name in events) {
  1099. this._recoverHandlers(events[name]);
  1100. }
  1101. this._events = null;
  1102. }
  1103. return this;
  1104. }
  1105. offAllCaller(caller) {
  1106. if (caller && this._events) {
  1107. for (var name in this._events) {
  1108. this.off(name, caller, null);
  1109. }
  1110. }
  1111. return this;
  1112. }
  1113. _recoverHandlers(arr) {
  1114. if (!arr)
  1115. return;
  1116. if (arr.run) {
  1117. arr.recover();
  1118. }
  1119. else {
  1120. for (var i = arr.length - 1; i > -1; i--) {
  1121. if (arr[i]) {
  1122. arr[i].recover();
  1123. arr[i] = null;
  1124. }
  1125. }
  1126. }
  1127. }
  1128. isMouseEvent(type) {
  1129. return EventDispatcher.MOUSE_EVENTS[type] || false;
  1130. }
  1131. }
  1132. EventDispatcher.MOUSE_EVENTS = { "rightmousedown": true, "rightmouseup": true, "rightclick": true, "mousedown": true, "mouseup": true, "mousemove": true, "mouseover": true, "mouseout": true, "click": true, "doubleclick": true };
  1133. class EventHandler extends Handler {
  1134. constructor(caller, method, args, once) {
  1135. super(caller, method, args, once);
  1136. }
  1137. recover() {
  1138. if (this._id > 0) {
  1139. this._id = 0;
  1140. EventHandler._pool.push(this.clear());
  1141. }
  1142. }
  1143. static create(caller, method, args = null, once = true) {
  1144. if (EventHandler._pool.length)
  1145. return EventHandler._pool.pop().setTo(caller, method, args, once);
  1146. return new EventHandler(caller, method, args, once);
  1147. }
  1148. }
  1149. EventHandler._pool = [];
  1150. class URL {
  1151. constructor(url) {
  1152. this._url = URL.formatURL(url);
  1153. this._path = URL.getPath(url);
  1154. }
  1155. get url() {
  1156. return this._url;
  1157. }
  1158. get path() {
  1159. return this._path;
  1160. }
  1161. static set basePath(value) {
  1162. URL._basePath = ILaya.Laya._getUrlPath();
  1163. URL._basePath = URL.formatURL(value);
  1164. }
  1165. static get basePath() {
  1166. return URL._basePath;
  1167. }
  1168. static formatURL(url) {
  1169. if (!url)
  1170. return "null path";
  1171. if (url.indexOf(":") > 0)
  1172. return url;
  1173. if (URL.customFormat != null)
  1174. url = URL.customFormat(url);
  1175. if (url.indexOf(":") > 0)
  1176. return url;
  1177. var char1 = url.charAt(0);
  1178. if (char1 === ".") {
  1179. return URL._formatRelativePath(URL._basePath + url);
  1180. }
  1181. else if (char1 === '~') {
  1182. return URL.rootPath + url.substring(1);
  1183. }
  1184. else if (char1 === "d") {
  1185. if (url.indexOf("data:image") === 0)
  1186. return url;
  1187. }
  1188. else if (char1 === "/") {
  1189. return url;
  1190. }
  1191. return URL._basePath + url;
  1192. }
  1193. static _formatRelativePath(value) {
  1194. var parts = value.split("/");
  1195. for (var i = 0, len = parts.length; i < len; i++) {
  1196. if (parts[i] == '..') {
  1197. parts.splice(i - 1, 2);
  1198. i -= 2;
  1199. }
  1200. }
  1201. return parts.join('/');
  1202. }
  1203. static getPath(url) {
  1204. var ofs = url.lastIndexOf('/');
  1205. return ofs > 0 ? url.substr(0, ofs + 1) : "";
  1206. }
  1207. static getFileName(url) {
  1208. var ofs = url.lastIndexOf('/');
  1209. return ofs > 0 ? url.substr(ofs + 1) : url;
  1210. }
  1211. static getAdptedFilePath(url) {
  1212. if (!URL.exportSceneToJson || !url)
  1213. return url;
  1214. var i, len;
  1215. len = URL._adpteTypeList.length;
  1216. var tArr;
  1217. for (i = 0; i < len; i++) {
  1218. tArr = URL._adpteTypeList[i];
  1219. url = url.replace(tArr[0], tArr[1]);
  1220. }
  1221. return url;
  1222. }
  1223. }
  1224. URL.version = {};
  1225. URL.exportSceneToJson = false;
  1226. URL._basePath = "";
  1227. URL.rootPath = "";
  1228. URL.customFormat = function (url) {
  1229. var newUrl = URL.version[url];
  1230. if (!window.conch && newUrl)
  1231. url += "?v=" + newUrl;
  1232. return url;
  1233. };
  1234. URL._adpteTypeList = [[".scene3d", ".json"], [".scene", ".json"], [".taa", ".json"], [".prefab", ".json"]];
  1235. class Resource extends EventDispatcher {
  1236. constructor() {
  1237. super();
  1238. this._id = 0;
  1239. this._url = null;
  1240. this._cpuMemory = 0;
  1241. this._gpuMemory = 0;
  1242. this._destroyed = false;
  1243. this._referenceCount = 0;
  1244. this.lock = false;
  1245. this.name = null;
  1246. this._id = ++Resource._uniqueIDCounter;
  1247. this._destroyed = false;
  1248. this._referenceCount = 0;
  1249. Resource._idResourcesMap[this.id] = this;
  1250. this.lock = false;
  1251. }
  1252. static get cpuMemory() {
  1253. return Resource._cpuMemory;
  1254. }
  1255. static get gpuMemory() {
  1256. return Resource._gpuMemory;
  1257. }
  1258. static _addCPUMemory(size) {
  1259. Resource._cpuMemory += size;
  1260. }
  1261. static _addGPUMemory(size) {
  1262. Resource._gpuMemory += size;
  1263. }
  1264. static _addMemory(cpuSize, gpuSize) {
  1265. Resource._cpuMemory += cpuSize;
  1266. Resource._gpuMemory += gpuSize;
  1267. }
  1268. static getResourceByID(id) {
  1269. return Resource._idResourcesMap[id];
  1270. }
  1271. static getResourceByURL(url, index = 0) {
  1272. return Resource._urlResourcesMap[url][index];
  1273. }
  1274. static destroyUnusedResources() {
  1275. for (var k in Resource._idResourcesMap) {
  1276. var res = Resource._idResourcesMap[k];
  1277. if (!res.lock && res._referenceCount === 0)
  1278. res.destroy();
  1279. }
  1280. }
  1281. get id() {
  1282. return this._id;
  1283. }
  1284. get url() {
  1285. return this._url;
  1286. }
  1287. get cpuMemory() {
  1288. return this._cpuMemory;
  1289. }
  1290. get gpuMemory() {
  1291. return this._gpuMemory;
  1292. }
  1293. get destroyed() {
  1294. return this._destroyed;
  1295. }
  1296. get referenceCount() {
  1297. return this._referenceCount;
  1298. }
  1299. _setCPUMemory(value) {
  1300. var offsetValue = value - this._cpuMemory;
  1301. this._cpuMemory = value;
  1302. Resource._addCPUMemory(offsetValue);
  1303. }
  1304. _setGPUMemory(value) {
  1305. var offsetValue = value - this._gpuMemory;
  1306. this._gpuMemory = value;
  1307. Resource._addGPUMemory(offsetValue);
  1308. }
  1309. _setCreateURL(url) {
  1310. url = URL.formatURL(url);
  1311. if (this._url !== url) {
  1312. var resList;
  1313. if (this._url) {
  1314. resList = Resource._urlResourcesMap[this._url];
  1315. resList.splice(resList.indexOf(this), 1);
  1316. (resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
  1317. }
  1318. if (url) {
  1319. resList = Resource._urlResourcesMap[url];
  1320. (resList) || (Resource._urlResourcesMap[url] = resList = []);
  1321. resList.push(this);
  1322. }
  1323. this._url = url;
  1324. }
  1325. }
  1326. _addReference(count = 1) {
  1327. this._referenceCount += count;
  1328. }
  1329. _removeReference(count = 1) {
  1330. this._referenceCount -= count;
  1331. }
  1332. _clearReference() {
  1333. this._referenceCount = 0;
  1334. }
  1335. _recoverResource() {
  1336. }
  1337. _disposeResource() {
  1338. }
  1339. _activeResource() {
  1340. }
  1341. destroy() {
  1342. if (this._destroyed)
  1343. return;
  1344. this._destroyed = true;
  1345. this.lock = false;
  1346. this._disposeResource();
  1347. delete Resource._idResourcesMap[this.id];
  1348. var resList;
  1349. if (this._url) {
  1350. resList = Resource._urlResourcesMap[this._url];
  1351. if (resList) {
  1352. resList.splice(resList.indexOf(this), 1);
  1353. (resList.length === 0) && (delete Resource._urlResourcesMap[this._url]);
  1354. }
  1355. var resou = ILaya.Loader.loadedMap[this._url];
  1356. (resou == this) && (delete ILaya.Loader.loadedMap[this._url]);
  1357. }
  1358. }
  1359. }
  1360. Resource._uniqueIDCounter = 0;
  1361. Resource._idResourcesMap = {};
  1362. Resource._urlResourcesMap = {};
  1363. Resource._cpuMemory = 0;
  1364. Resource._gpuMemory = 0;
  1365. class Bitmap extends Resource {
  1366. get width() {
  1367. return this._width;
  1368. }
  1369. set width(width) {
  1370. this._width = width;
  1371. }
  1372. get height() {
  1373. return this._height;
  1374. }
  1375. set height(height) {
  1376. this._height = height;
  1377. }
  1378. constructor() {
  1379. super();
  1380. this._width = -1;
  1381. this._height = -1;
  1382. }
  1383. _getSource() {
  1384. throw "Bitmap: must override it.";
  1385. }
  1386. }
  1387. (function (TextureFormat) {
  1388. TextureFormat[TextureFormat["R8G8B8"] = 0] = "R8G8B8";
  1389. TextureFormat[TextureFormat["R8G8B8A8"] = 1] = "R8G8B8A8";
  1390. TextureFormat[TextureFormat["Alpha8"] = 2] = "Alpha8";
  1391. TextureFormat[TextureFormat["DXT1"] = 3] = "DXT1";
  1392. TextureFormat[TextureFormat["DXT5"] = 4] = "DXT5";
  1393. TextureFormat[TextureFormat["ETC1RGB"] = 5] = "ETC1RGB";
  1394. TextureFormat[TextureFormat["PVRTCRGB_2BPPV"] = 9] = "PVRTCRGB_2BPPV";
  1395. TextureFormat[TextureFormat["PVRTCRGBA_2BPPV"] = 10] = "PVRTCRGBA_2BPPV";
  1396. TextureFormat[TextureFormat["PVRTCRGB_4BPPV"] = 11] = "PVRTCRGB_4BPPV";
  1397. TextureFormat[TextureFormat["PVRTCRGBA_4BPPV"] = 12] = "PVRTCRGBA_4BPPV";
  1398. TextureFormat[TextureFormat["R32G32B32A32"] = 15] = "R32G32B32A32";
  1399. })(exports.TextureFormat || (exports.TextureFormat = {}));
  1400. class BaseTexture extends Bitmap {
  1401. constructor(format, mipMap) {
  1402. super();
  1403. this._wrapModeU = BaseTexture.WARPMODE_REPEAT;
  1404. this._wrapModeV = BaseTexture.WARPMODE_REPEAT;
  1405. this._filterMode = BaseTexture.FILTERMODE_BILINEAR;
  1406. this._readyed = false;
  1407. this._width = -1;
  1408. this._height = -1;
  1409. this._format = format;
  1410. this._mipmap = mipMap;
  1411. this._anisoLevel = 1;
  1412. this._glTexture = LayaGL.instance.createTexture();
  1413. }
  1414. get mipmap() {
  1415. return this._mipmap;
  1416. }
  1417. get format() {
  1418. return this._format;
  1419. }
  1420. get wrapModeU() {
  1421. return this._wrapModeU;
  1422. }
  1423. set wrapModeU(value) {
  1424. if (this._wrapModeU !== value) {
  1425. this._wrapModeU = value;
  1426. (this._width !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_S, value));
  1427. }
  1428. }
  1429. get wrapModeV() {
  1430. return this._wrapModeV;
  1431. }
  1432. set wrapModeV(value) {
  1433. if (this._wrapModeV !== value) {
  1434. this._wrapModeV = value;
  1435. (this._height !== -1) && (this._setWarpMode(LayaGL.instance.TEXTURE_WRAP_T, value));
  1436. }
  1437. }
  1438. get filterMode() {
  1439. return this._filterMode;
  1440. }
  1441. set filterMode(value) {
  1442. if (value !== this._filterMode) {
  1443. this._filterMode = value;
  1444. ((this._width !== -1) && (this._height !== -1)) && (this._setFilterMode(value));
  1445. }
  1446. }
  1447. get anisoLevel() {
  1448. return this._anisoLevel;
  1449. }
  1450. set anisoLevel(value) {
  1451. if (value !== this._anisoLevel) {
  1452. this._anisoLevel = Math.max(1, Math.min(16, value));
  1453. ((this._width !== -1) && (this._height !== -1)) && (this._setAnisotropy(value));
  1454. }
  1455. }
  1456. get mipmapCount() {
  1457. return this._mipmapCount;
  1458. }
  1459. get defaulteTexture() {
  1460. throw "BaseTexture:must override it.";
  1461. }
  1462. _getFormatByteCount() {
  1463. switch (this._format) {
  1464. case exports.TextureFormat.R8G8B8:
  1465. return 3;
  1466. case exports.TextureFormat.R8G8B8A8:
  1467. return 4;
  1468. case exports.TextureFormat.Alpha8:
  1469. return 1;
  1470. case exports.TextureFormat.R32G32B32A32:
  1471. return 4;
  1472. default:
  1473. throw "Texture2D: unknown format.";
  1474. }
  1475. }
  1476. _isPot(size) {
  1477. return (size & (size - 1)) === 0;
  1478. }
  1479. _getGLFormat() {
  1480. var glFormat;
  1481. var gl = LayaGL.instance;
  1482. var gpu = LayaGL.layaGPUInstance;
  1483. switch (this._format) {
  1484. case exports.TextureFormat.R8G8B8:
  1485. glFormat = gl.RGB;
  1486. break;
  1487. case exports.TextureFormat.R8G8B8A8:
  1488. glFormat = gl.RGBA;
  1489. break;
  1490. case exports.TextureFormat.Alpha8:
  1491. glFormat = gl.ALPHA;
  1492. break;
  1493. case exports.TextureFormat.R32G32B32A32:
  1494. glFormat = gl.RGBA;
  1495. break;
  1496. case exports.TextureFormat.DXT1:
  1497. if (gpu._compressedTextureS3tc)
  1498. glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGB_S3TC_DXT1_EXT;
  1499. else
  1500. throw "BaseTexture: not support DXT1 format.";
  1501. break;
  1502. case exports.TextureFormat.DXT5:
  1503. if (gpu._compressedTextureS3tc)
  1504. glFormat = gpu._compressedTextureS3tc.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  1505. else
  1506. throw "BaseTexture: not support DXT5 format.";
  1507. break;
  1508. case exports.TextureFormat.ETC1RGB:
  1509. if (gpu._compressedTextureEtc1)
  1510. glFormat = gpu._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL;
  1511. else
  1512. throw "BaseTexture: not support ETC1RGB format.";
  1513. break;
  1514. case exports.TextureFormat.PVRTCRGB_2BPPV:
  1515. if (gpu._compressedTexturePvrtc)
  1516. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  1517. else
  1518. throw "BaseTexture: not support PVRTCRGB_2BPPV format.";
  1519. break;
  1520. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  1521. if (gpu._compressedTexturePvrtc)
  1522. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  1523. else
  1524. throw "BaseTexture: not support PVRTCRGBA_2BPPV format.";
  1525. break;
  1526. case exports.TextureFormat.PVRTCRGB_4BPPV:
  1527. if (gpu._compressedTexturePvrtc)
  1528. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  1529. else
  1530. throw "BaseTexture: not support PVRTCRGB_4BPPV format.";
  1531. break;
  1532. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  1533. if (gpu._compressedTexturePvrtc)
  1534. glFormat = gpu._compressedTexturePvrtc.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  1535. else
  1536. throw "BaseTexture: not support PVRTCRGBA_4BPPV format.";
  1537. break;
  1538. default:
  1539. throw "BaseTexture: unknown texture format.";
  1540. }
  1541. return glFormat;
  1542. }
  1543. _setFilterMode(value) {
  1544. var gl = LayaGL.instance;
  1545. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1546. switch (value) {
  1547. case BaseTexture.FILTERMODE_POINT:
  1548. if (this._mipmap)
  1549. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_NEAREST);
  1550. else
  1551. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  1552. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  1553. break;
  1554. case BaseTexture.FILTERMODE_BILINEAR:
  1555. if (this._mipmap)
  1556. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
  1557. else
  1558. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  1559. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1560. break;
  1561. case BaseTexture.FILTERMODE_TRILINEAR:
  1562. if (this._mipmap)
  1563. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
  1564. else
  1565. gl.texParameteri(this._glTextureType, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  1566. gl.texParameteri(this._glTextureType, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  1567. break;
  1568. default:
  1569. throw new Error("BaseTexture:unknown filterMode value.");
  1570. }
  1571. }
  1572. _setWarpMode(orientation, mode) {
  1573. var gl = LayaGL.instance;
  1574. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1575. if (this._isPot(this._width) && this._isPot(this._height)) {
  1576. switch (mode) {
  1577. case BaseTexture.WARPMODE_REPEAT:
  1578. gl.texParameteri(this._glTextureType, orientation, gl.REPEAT);
  1579. break;
  1580. case BaseTexture.WARPMODE_CLAMP:
  1581. gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
  1582. break;
  1583. }
  1584. }
  1585. else {
  1586. gl.texParameteri(this._glTextureType, orientation, gl.CLAMP_TO_EDGE);
  1587. }
  1588. }
  1589. _setAnisotropy(value) {
  1590. var anisotropic = LayaGL.layaGPUInstance._extTextureFilterAnisotropic;
  1591. if (anisotropic) {
  1592. value = Math.max(value, 1);
  1593. var gl = LayaGL.instance;
  1594. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1595. value = Math.min(gl.getParameter(anisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT), value);
  1596. gl.texParameterf(this._glTextureType, anisotropic.TEXTURE_MAX_ANISOTROPY_EXT, value);
  1597. }
  1598. }
  1599. _disposeResource() {
  1600. if (this._glTexture) {
  1601. LayaGL.instance.deleteTexture(this._glTexture);
  1602. this._glTexture = null;
  1603. this._setGPUMemory(0);
  1604. }
  1605. }
  1606. _getSource() {
  1607. if (this._readyed)
  1608. return this._glTexture;
  1609. else
  1610. return null;
  1611. }
  1612. generateMipmap() {
  1613. if (this._isPot(this.width) && this._isPot(this.height))
  1614. LayaGL.instance.generateMipmap(this._glTextureType);
  1615. }
  1616. }
  1617. BaseTexture.WARPMODE_REPEAT = 0;
  1618. BaseTexture.WARPMODE_CLAMP = 1;
  1619. BaseTexture.FILTERMODE_POINT = 0;
  1620. BaseTexture.FILTERMODE_BILINEAR = 1;
  1621. BaseTexture.FILTERMODE_TRILINEAR = 2;
  1622. BaseTexture.FORMAT_R8G8B8 = 0;
  1623. BaseTexture.FORMAT_R8G8B8A8 = 1;
  1624. BaseTexture.FORMAT_ALPHA8 = 2;
  1625. BaseTexture.FORMAT_DXT1 = 3;
  1626. BaseTexture.FORMAT_DXT5 = 4;
  1627. BaseTexture.FORMAT_ETC1RGB = 5;
  1628. BaseTexture.FORMAT_PVRTCRGB_2BPPV = 9;
  1629. BaseTexture.FORMAT_PVRTCRGBA_2BPPV = 10;
  1630. BaseTexture.FORMAT_PVRTCRGB_4BPPV = 11;
  1631. BaseTexture.FORMAT_PVRTCRGBA_4BPPV = 12;
  1632. BaseTexture.RENDERTEXTURE_FORMAT_RGBA_HALF_FLOAT = 14;
  1633. BaseTexture.FORMAT_R32G32B32A32 = 15;
  1634. BaseTexture.FORMAT_DEPTH_16 = 0;
  1635. BaseTexture.FORMAT_STENCIL_8 = 1;
  1636. BaseTexture.FORMAT_DEPTHSTENCIL_16_8 = 2;
  1637. BaseTexture.FORMAT_DEPTHSTENCIL_NONE = 3;
  1638. class Texture2D extends BaseTexture {
  1639. constructor(width = 0, height = 0, format = exports.TextureFormat.R8G8B8A8, mipmap = true, canRead = false) {
  1640. super(format, mipmap);
  1641. var gl = LayaGL.instance;
  1642. this._glTextureType = gl.TEXTURE_2D;
  1643. this._width = width;
  1644. this._height = height;
  1645. this._canRead = canRead;
  1646. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  1647. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  1648. this._setFilterMode(this._filterMode);
  1649. this._setAnisotropy(this._anisoLevel);
  1650. var compress = this._gpuCompressFormat();
  1651. if (mipmap) {
  1652. var mipCount = Math.max(Math.ceil(Math.log2(width)) + 1, Math.ceil(Math.log2(height)) + 1);
  1653. if (!compress) {
  1654. for (var i = 0; i < mipCount; i++)
  1655. this._setPixels(null, i, Math.max(width >> i, 1), Math.max(height >> i, 1));
  1656. }
  1657. this._mipmapCount = mipCount;
  1658. this._setGPUMemory(width * height * 4 * (1 + 1 / 3));
  1659. }
  1660. else {
  1661. if (!compress)
  1662. this._setPixels(null, 0, width, height);
  1663. this._mipmapCount = 1;
  1664. this._setGPUMemory(width * height * 4);
  1665. }
  1666. }
  1667. static __init__() {
  1668. var pixels = new Uint8Array(3);
  1669. pixels[0] = 128;
  1670. pixels[1] = 128;
  1671. pixels[2] = 128;
  1672. Texture2D.grayTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  1673. Texture2D.grayTexture.setPixels(pixels);
  1674. Texture2D.grayTexture.lock = true;
  1675. pixels[0] = 255;
  1676. pixels[1] = 255;
  1677. pixels[2] = 255;
  1678. Texture2D.whiteTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  1679. Texture2D.whiteTexture.setPixels(pixels);
  1680. Texture2D.whiteTexture.lock = true;
  1681. pixels[0] = 0;
  1682. pixels[1] = 0;
  1683. pixels[2] = 0;
  1684. Texture2D.blackTexture = new Texture2D(1, 1, exports.TextureFormat.R8G8B8, false, false);
  1685. Texture2D.blackTexture.setPixels(pixels);
  1686. Texture2D.blackTexture.lock = true;
  1687. }
  1688. static _parse(data, propertyParams = null, constructParams = null) {
  1689. var texture = constructParams ? new Texture2D(constructParams[0], constructParams[1], constructParams[2], constructParams[3], constructParams[4]) : new Texture2D(0, 0);
  1690. if (propertyParams) {
  1691. texture.wrapModeU = propertyParams.wrapModeU;
  1692. texture.wrapModeV = propertyParams.wrapModeV;
  1693. texture.filterMode = propertyParams.filterMode;
  1694. texture.anisoLevel = propertyParams.anisoLevel;
  1695. }
  1696. switch (texture._format) {
  1697. case exports.TextureFormat.R8G8B8:
  1698. case exports.TextureFormat.R8G8B8A8:
  1699. texture.loadImageSource(data);
  1700. break;
  1701. case exports.TextureFormat.DXT1:
  1702. case exports.TextureFormat.DXT5:
  1703. case exports.TextureFormat.ETC1RGB:
  1704. case exports.TextureFormat.PVRTCRGB_2BPPV:
  1705. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  1706. case exports.TextureFormat.PVRTCRGB_4BPPV:
  1707. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  1708. texture.setCompressData(data);
  1709. break;
  1710. default:
  1711. throw "Texture2D:unkonwn format.";
  1712. }
  1713. return texture;
  1714. }
  1715. static load(url, complete) {
  1716. ILaya.loader.create(url, complete, null, ILaya.Loader.TEXTURE2D);
  1717. }
  1718. get defaulteTexture() {
  1719. return Texture2D.grayTexture;
  1720. }
  1721. _gpuCompressFormat() {
  1722. return this._format == exports.TextureFormat.DXT1 || this._format == exports.TextureFormat.DXT5 ||
  1723. this._format == exports.TextureFormat.ETC1RGB ||
  1724. this._format == exports.TextureFormat.PVRTCRGB_2BPPV || this._format == exports.TextureFormat.PVRTCRGBA_2BPPV ||
  1725. this._format == exports.TextureFormat.PVRTCRGB_4BPPV || this._format == exports.TextureFormat.PVRTCRGBA_4BPPV;
  1726. }
  1727. _setPixels(pixels, miplevel, width, height) {
  1728. var gl = LayaGL.instance;
  1729. var textureType = this._glTextureType;
  1730. var glFormat = this._getGLFormat();
  1731. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  1732. switch (this.format) {
  1733. case exports.TextureFormat.R8G8B8:
  1734. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  1735. gl.texImage2D(textureType, miplevel, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, pixels);
  1736. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  1737. break;
  1738. case exports.TextureFormat.R32G32B32A32:
  1739. if (LayaGL.layaGPUInstance._isWebGL2)
  1740. gl.texImage2D(textureType, miplevel, gl.RGBA32F, width, height, 0, glFormat, gl.FLOAT, pixels);
  1741. else
  1742. gl.texImage2D(textureType, miplevel, gl.RGBA, width, height, 0, glFormat, gl.FLOAT, pixels);
  1743. break;
  1744. default:
  1745. gl.texImage2D(textureType, miplevel, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, pixels);
  1746. }
  1747. }
  1748. _calcualatesCompressedDataSize(format, width, height) {
  1749. switch (format) {
  1750. case exports.TextureFormat.DXT1:
  1751. case exports.TextureFormat.ETC1RGB:
  1752. return ((width + 3) >> 2) * ((height + 3) >> 2) * 8;
  1753. case exports.TextureFormat.DXT5:
  1754. return ((width + 3) >> 2) * ((height + 3) >> 2) * 16;
  1755. case exports.TextureFormat.PVRTCRGB_4BPPV:
  1756. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  1757. return Math.floor((Math.max(width, 8) * Math.max(height, 8) * 4 + 7) / 8);
  1758. case exports.TextureFormat.PVRTCRGB_2BPPV:
  1759. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  1760. return Math.floor((Math.max(width, 16) * Math.max(height, 8) * 2 + 7) / 8);
  1761. default:
  1762. return 0;
  1763. }
  1764. }
  1765. _pharseDDS(arrayBuffer) {
  1766. const FOURCC_DXT1 = 827611204;
  1767. const FOURCC_DXT5 = 894720068;
  1768. const DDPF_FOURCC = 0x4;
  1769. const DDSD_MIPMAPCOUNT = 0x20000;
  1770. const DDS_MAGIC = 0x20534444;
  1771. const DDS_HEADER_LENGTH = 31;
  1772. const DDS_HEADER_MAGIC = 0;
  1773. const DDS_HEADER_SIZE = 1;
  1774. const DDS_HEADER_FLAGS = 2;
  1775. const DDS_HEADER_HEIGHT = 3;
  1776. const DDS_HEADER_WIDTH = 4;
  1777. const DDS_HEADER_MIPMAPCOUNT = 7;
  1778. const DDS_HEADER_PF_FLAGS = 20;
  1779. const DDS_HEADER_PF_FOURCC = 21;
  1780. var header = new Int32Array(arrayBuffer, 0, DDS_HEADER_LENGTH);
  1781. if (header[DDS_HEADER_MAGIC] != DDS_MAGIC)
  1782. throw "Invalid magic number in DDS header";
  1783. if (!(header[DDS_HEADER_PF_FLAGS] & DDPF_FOURCC))
  1784. throw "Unsupported format, must contain a FourCC code";
  1785. var compressedFormat = header[DDS_HEADER_PF_FOURCC];
  1786. switch (this._format) {
  1787. case exports.TextureFormat.DXT1:
  1788. if (compressedFormat !== FOURCC_DXT1)
  1789. throw "the FourCC code is not same with texture format.";
  1790. break;
  1791. case exports.TextureFormat.DXT5:
  1792. if (compressedFormat !== FOURCC_DXT5)
  1793. throw "the FourCC code is not same with texture format.";
  1794. break;
  1795. default:
  1796. throw "unknown texture format.";
  1797. }
  1798. var mipLevels = 1;
  1799. if (header[DDS_HEADER_FLAGS] & DDSD_MIPMAPCOUNT) {
  1800. mipLevels = Math.max(1, header[DDS_HEADER_MIPMAPCOUNT]);
  1801. if (!this._mipmap)
  1802. throw "the mipmap is not same with Texture2D.";
  1803. }
  1804. else {
  1805. if (this._mipmap)
  1806. throw "the mipmap is not same with Texture2D.";
  1807. }
  1808. var width = header[DDS_HEADER_WIDTH];
  1809. var height = header[DDS_HEADER_HEIGHT];
  1810. this._width = width;
  1811. this._height = height;
  1812. var dataOffset = header[DDS_HEADER_SIZE] + 4;
  1813. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 0);
  1814. }
  1815. _pharseKTX(arrayBuffer) {
  1816. const ETC_HEADER_LENGTH = 13;
  1817. const ETC_HEADER_FORMAT = 4;
  1818. const ETC_HEADER_HEIGHT = 7;
  1819. const ETC_HEADER_WIDTH = 6;
  1820. const ETC_HEADER_MIPMAPCOUNT = 11;
  1821. const ETC_HEADER_METADATA = 12;
  1822. var id = new Uint8Array(arrayBuffer, 0, 12);
  1823. 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)
  1824. throw ("Invalid fileIdentifier in KTX header");
  1825. var header = new Int32Array(id.buffer, id.length, ETC_HEADER_LENGTH);
  1826. var compressedFormat = header[ETC_HEADER_FORMAT];
  1827. switch (compressedFormat) {
  1828. case LayaGL.layaGPUInstance._compressedTextureEtc1.COMPRESSED_RGB_ETC1_WEBGL:
  1829. this._format = exports.TextureFormat.ETC1RGB;
  1830. break;
  1831. default:
  1832. throw "unknown texture format.";
  1833. }
  1834. var mipLevels = header[ETC_HEADER_MIPMAPCOUNT];
  1835. var width = header[ETC_HEADER_WIDTH];
  1836. var height = header[ETC_HEADER_HEIGHT];
  1837. this._width = width;
  1838. this._height = height;
  1839. var dataOffset = 64 + header[ETC_HEADER_METADATA];
  1840. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 4);
  1841. }
  1842. _pharsePVR(arrayBuffer) {
  1843. const PVR_FORMAT_2BPP_RGB = 0;
  1844. const PVR_FORMAT_2BPP_RGBA = 1;
  1845. const PVR_FORMAT_4BPP_RGB = 2;
  1846. const PVR_FORMAT_4BPP_RGBA = 3;
  1847. const PVR_MAGIC = 0x03525650;
  1848. const PVR_HEADER_LENGTH = 13;
  1849. const PVR_HEADER_MAGIC = 0;
  1850. const PVR_HEADER_FORMAT = 2;
  1851. const PVR_HEADER_HEIGHT = 6;
  1852. const PVR_HEADER_WIDTH = 7;
  1853. const PVR_HEADER_MIPMAPCOUNT = 11;
  1854. const PVR_HEADER_METADATA = 12;
  1855. var header = new Int32Array(arrayBuffer, 0, PVR_HEADER_LENGTH);
  1856. if (header[PVR_HEADER_MAGIC] != PVR_MAGIC)
  1857. throw ("Invalid magic number in PVR header");
  1858. var compressedFormat = header[PVR_HEADER_FORMAT];
  1859. switch (compressedFormat) {
  1860. case PVR_FORMAT_2BPP_RGB:
  1861. this._format = exports.TextureFormat.PVRTCRGB_2BPPV;
  1862. break;
  1863. case PVR_FORMAT_4BPP_RGB:
  1864. this._format = exports.TextureFormat.PVRTCRGB_4BPPV;
  1865. break;
  1866. case PVR_FORMAT_2BPP_RGBA:
  1867. this._format = exports.TextureFormat.PVRTCRGBA_2BPPV;
  1868. break;
  1869. case PVR_FORMAT_4BPP_RGBA:
  1870. this._format = exports.TextureFormat.PVRTCRGBA_4BPPV;
  1871. break;
  1872. default:
  1873. throw "Texture2D:unknown PVR format.";
  1874. }
  1875. var mipLevels = header[PVR_HEADER_MIPMAPCOUNT];
  1876. var width = header[PVR_HEADER_WIDTH];
  1877. var height = header[PVR_HEADER_HEIGHT];
  1878. this._width = width;
  1879. this._height = height;
  1880. var dataOffset = header[PVR_HEADER_METADATA] + 52;
  1881. this._upLoadCompressedTexImage2D(arrayBuffer, width, height, mipLevels, dataOffset, 0);
  1882. }
  1883. _upLoadCompressedTexImage2D(data, width, height, miplevelCount, dataOffset, imageSizeOffset) {
  1884. var gl = LayaGL.instance;
  1885. var textureType = this._glTextureType;
  1886. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  1887. var glFormat = this._getGLFormat();
  1888. var offset = dataOffset;
  1889. for (var i = 0; i < miplevelCount; i++) {
  1890. offset += imageSizeOffset;
  1891. var mipDataSize = this._calcualatesCompressedDataSize(this._format, width, height);
  1892. var mipData = new Uint8Array(data, offset, mipDataSize);
  1893. gl.compressedTexImage2D(textureType, i, glFormat, width, height, 0, mipData);
  1894. width = Math.max(width >> 1, 1.0);
  1895. height = Math.max(height >> 1, 1.0);
  1896. offset += mipDataSize;
  1897. }
  1898. var memory = offset;
  1899. this._setGPUMemory(memory);
  1900. this._readyed = true;
  1901. this._activeResource();
  1902. }
  1903. loadImageSource(source, premultiplyAlpha = false) {
  1904. var gl = LayaGL.instance;
  1905. var width = source.width;
  1906. var height = source.height;
  1907. this._width = width;
  1908. this._height = height;
  1909. if (!(this._isPot(width) && this._isPot(height)))
  1910. this._mipmap = false;
  1911. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  1912. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  1913. this._setFilterMode(this._filterMode);
  1914. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  1915. var glFormat = this._getGLFormat();
  1916. if (ILaya.Render.isConchApp) {
  1917. if (source.setPremultiplyAlpha) {
  1918. source.setPremultiplyAlpha(premultiplyAlpha);
  1919. }
  1920. gl.texImage2D(this._glTextureType, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
  1921. }
  1922. else {
  1923. (premultiplyAlpha) && (gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true));
  1924. gl.texImage2D(this._glTextureType, 0, glFormat, glFormat, gl.UNSIGNED_BYTE, source);
  1925. (premultiplyAlpha) && (gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false));
  1926. }
  1927. if (this._mipmap) {
  1928. gl.generateMipmap(this._glTextureType);
  1929. this._setGPUMemory(width * height * 4 * (1 + 1 / 3));
  1930. }
  1931. else {
  1932. this._setGPUMemory(width * height * 4);
  1933. }
  1934. if (this._canRead) {
  1935. if (ILaya.Render.isConchApp) {
  1936. this._pixels = new Uint8Array(source._nativeObj.getImageData(0, 0, width, height));
  1937. }
  1938. else {
  1939. ILaya.Browser.canvas.size(width, height);
  1940. ILaya.Browser.canvas.clear();
  1941. ILaya.Browser.context.drawImage(source, 0, 0, width, height);
  1942. this._pixels = new Uint8Array(ILaya.Browser.context.getImageData(0, 0, width, height).data.buffer);
  1943. }
  1944. }
  1945. this._readyed = true;
  1946. this._activeResource();
  1947. }
  1948. setPixels(pixels, miplevel = 0) {
  1949. if (this._gpuCompressFormat())
  1950. throw "Texture2D:the format is GPU compression format.";
  1951. if (!pixels)
  1952. throw "Texture2D:pixels can't be null.";
  1953. var width = Math.max(this._width >> miplevel, 1);
  1954. var height = Math.max(this._height >> miplevel, 1);
  1955. var pixelsCount = width * height * this._getFormatByteCount();
  1956. if (pixels.length < pixelsCount)
  1957. throw "Texture2D:pixels length should at least " + pixelsCount + ".";
  1958. this._setPixels(pixels, miplevel, width, height);
  1959. if (this._canRead)
  1960. this._pixels = pixels;
  1961. this._readyed = true;
  1962. this._activeResource();
  1963. }
  1964. setSubPixels(x, y, width, height, pixels, miplevel = 0) {
  1965. if (this._gpuCompressFormat())
  1966. throw "Texture2D:the format is GPU compression format.";
  1967. if (!pixels)
  1968. throw "Texture2D:pixels can't be null.";
  1969. var gl = LayaGL.instance;
  1970. var textureType = this._glTextureType;
  1971. WebGLContext.bindTexture(gl, textureType, this._glTexture);
  1972. var glFormat = this._getGLFormat();
  1973. switch (this.format) {
  1974. case exports.TextureFormat.R8G8B8:
  1975. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
  1976. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  1977. gl.pixelStorei(gl.UNPACK_ALIGNMENT, 4);
  1978. break;
  1979. case exports.TextureFormat.R32G32B32A32:
  1980. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.FLOAT, pixels);
  1981. break;
  1982. default:
  1983. gl.texSubImage2D(textureType, miplevel, x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  1984. }
  1985. this._readyed = true;
  1986. this._activeResource();
  1987. }
  1988. setCompressData(data) {
  1989. switch (this._format) {
  1990. case exports.TextureFormat.DXT1:
  1991. case exports.TextureFormat.DXT5:
  1992. this._pharseDDS(data);
  1993. break;
  1994. case exports.TextureFormat.ETC1RGB:
  1995. this._pharseKTX(data);
  1996. break;
  1997. case exports.TextureFormat.PVRTCRGB_2BPPV:
  1998. case exports.TextureFormat.PVRTCRGBA_2BPPV:
  1999. case exports.TextureFormat.PVRTCRGB_4BPPV:
  2000. case exports.TextureFormat.PVRTCRGBA_4BPPV:
  2001. this._pharsePVR(data);
  2002. break;
  2003. default:
  2004. throw "Texture2D:unkonwn format.";
  2005. }
  2006. }
  2007. _recoverResource() {
  2008. }
  2009. getPixels() {
  2010. if (this._canRead)
  2011. return this._pixels;
  2012. else
  2013. throw new Error("Texture2D: must set texture canRead is true.");
  2014. }
  2015. }
  2016. Texture2D.TEXTURE2D = "TEXTURE2D";
  2017. Texture2D.grayTexture = null;
  2018. Texture2D.whiteTexture = null;
  2019. Texture2D.blackTexture = null;
  2020. class BaseShader extends Resource {
  2021. constructor() {
  2022. super();
  2023. }
  2024. }
  2025. class RenderState2D {
  2026. static mat2MatArray(mat, matArray) {
  2027. var m = mat;
  2028. var m4 = matArray;
  2029. m4[0] = m.a;
  2030. m4[1] = m.b;
  2031. m4[2] = RenderState2D.EMPTYMAT4_ARRAY[2];
  2032. m4[3] = RenderState2D.EMPTYMAT4_ARRAY[3];
  2033. m4[4] = m.c;
  2034. m4[5] = m.d;
  2035. m4[6] = RenderState2D.EMPTYMAT4_ARRAY[6];
  2036. m4[7] = RenderState2D.EMPTYMAT4_ARRAY[7];
  2037. m4[8] = RenderState2D.EMPTYMAT4_ARRAY[8];
  2038. m4[9] = RenderState2D.EMPTYMAT4_ARRAY[9];
  2039. m4[10] = RenderState2D.EMPTYMAT4_ARRAY[10];
  2040. m4[11] = RenderState2D.EMPTYMAT4_ARRAY[11];
  2041. m4[12] = m.tx;
  2042. m4[13] = m.ty;
  2043. m4[14] = RenderState2D.EMPTYMAT4_ARRAY[14];
  2044. m4[15] = RenderState2D.EMPTYMAT4_ARRAY[15];
  2045. return matArray;
  2046. }
  2047. static restoreTempArray() {
  2048. RenderState2D.TEMPMAT4_ARRAY[0] = 1;
  2049. RenderState2D.TEMPMAT4_ARRAY[1] = 0;
  2050. RenderState2D.TEMPMAT4_ARRAY[4] = 0;
  2051. RenderState2D.TEMPMAT4_ARRAY[5] = 1;
  2052. RenderState2D.TEMPMAT4_ARRAY[12] = 0;
  2053. RenderState2D.TEMPMAT4_ARRAY[13] = 0;
  2054. }
  2055. static clear() {
  2056. RenderState2D.worldScissorTest = false;
  2057. RenderState2D.worldAlpha = 1;
  2058. }
  2059. }
  2060. RenderState2D._MAXSIZE = 99999999;
  2061. RenderState2D.EMPTYMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  2062. RenderState2D.TEMPMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  2063. RenderState2D.worldMatrix4 = RenderState2D.TEMPMAT4_ARRAY;
  2064. RenderState2D.worldMatrix = new Matrix();
  2065. RenderState2D.matWVP = null;
  2066. RenderState2D.worldAlpha = 1.0;
  2067. RenderState2D.worldScissorTest = false;
  2068. RenderState2D.width = 0;
  2069. RenderState2D.height = 0;
  2070. (function (RenderTextureFormat) {
  2071. RenderTextureFormat[RenderTextureFormat["R8G8B8"] = 0] = "R8G8B8";
  2072. RenderTextureFormat[RenderTextureFormat["R8G8B8A8"] = 1] = "R8G8B8A8";
  2073. RenderTextureFormat[RenderTextureFormat["Alpha8"] = 2] = "Alpha8";
  2074. RenderTextureFormat[RenderTextureFormat["R16G16B16A16"] = 14] = "R16G16B16A16";
  2075. })(exports.RenderTextureFormat || (exports.RenderTextureFormat = {}));
  2076. (function (RenderTextureDepthFormat) {
  2077. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTH_16"] = 0] = "DEPTH_16";
  2078. RenderTextureDepthFormat[RenderTextureDepthFormat["STENCIL_8"] = 1] = "STENCIL_8";
  2079. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTHSTENCIL_16_8"] = 2] = "DEPTHSTENCIL_16_8";
  2080. RenderTextureDepthFormat[RenderTextureDepthFormat["DEPTHSTENCIL_NONE"] = 3] = "DEPTHSTENCIL_NONE";
  2081. })(exports.RenderTextureDepthFormat || (exports.RenderTextureDepthFormat = {}));
  2082. class RenderTexture2D extends BaseTexture {
  2083. constructor(width, height, format = exports.RenderTextureFormat.R8G8B8, depthStencilFormat = exports.RenderTextureDepthFormat.DEPTH_16) {
  2084. super(format, false);
  2085. this._mgrKey = 0;
  2086. this._glTextureType = LayaGL.instance.TEXTURE_2D;
  2087. this._width = width;
  2088. this._height = height;
  2089. this._depthStencilFormat = depthStencilFormat;
  2090. this._create(width, height);
  2091. this.lock = true;
  2092. }
  2093. static get currentActive() {
  2094. return RenderTexture2D._currentActive;
  2095. }
  2096. get depthStencilFormat() {
  2097. return this._depthStencilFormat;
  2098. }
  2099. get defaulteTexture() {
  2100. return Texture2D.grayTexture;
  2101. }
  2102. getIsReady() {
  2103. return true;
  2104. }
  2105. get sourceWidth() {
  2106. return this._width;
  2107. }
  2108. get sourceHeight() {
  2109. return this._height;
  2110. }
  2111. get offsetX() {
  2112. return 0;
  2113. }
  2114. get offsetY() {
  2115. return 0;
  2116. }
  2117. _create(width, height) {
  2118. var gl = LayaGL.instance;
  2119. this._frameBuffer = gl.createFramebuffer();
  2120. WebGLContext.bindTexture(gl, this._glTextureType, this._glTexture);
  2121. var glFormat = this._getGLFormat();
  2122. gl.texImage2D(this._glTextureType, 0, glFormat, width, height, 0, glFormat, gl.UNSIGNED_BYTE, null);
  2123. this._setGPUMemory(width * height * 4);
  2124. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2125. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._glTexture, 0);
  2126. if (this._depthStencilFormat !== exports.RenderTextureDepthFormat.DEPTHSTENCIL_NONE) {
  2127. this._depthStencilBuffer = gl.createRenderbuffer();
  2128. gl.bindRenderbuffer(gl.RENDERBUFFER, this._depthStencilBuffer);
  2129. switch (this._depthStencilFormat) {
  2130. case exports.RenderTextureDepthFormat.DEPTH_16:
  2131. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  2132. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2133. break;
  2134. case exports.RenderTextureDepthFormat.STENCIL_8:
  2135. gl.renderbufferStorage(gl.RENDERBUFFER, gl.STENCIL_INDEX8, width, height);
  2136. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2137. break;
  2138. case exports.RenderTextureDepthFormat.DEPTHSTENCIL_16_8:
  2139. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
  2140. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, this._depthStencilBuffer);
  2141. break;
  2142. default:
  2143. }
  2144. }
  2145. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2146. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  2147. this._setWarpMode(gl.TEXTURE_WRAP_S, this._wrapModeU);
  2148. this._setWarpMode(gl.TEXTURE_WRAP_T, this._wrapModeV);
  2149. this._setFilterMode(this._filterMode);
  2150. this._setAnisotropy(this._anisoLevel);
  2151. this._readyed = true;
  2152. this._activeResource();
  2153. }
  2154. generateMipmap() {
  2155. if (this._isPot(this.width) && this._isPot(this.height)) {
  2156. this._mipmap = true;
  2157. LayaGL.instance.generateMipmap(this._glTextureType);
  2158. this._setFilterMode(this._filterMode);
  2159. this._setGPUMemory(this.width * this.height * 4 * (1 + 1 / 3));
  2160. }
  2161. else {
  2162. this._mipmap = false;
  2163. this._setGPUMemory(this.width * this.height * 4);
  2164. }
  2165. }
  2166. static pushRT() {
  2167. RenderTexture2D.rtStack.push({ rt: RenderTexture2D._currentActive, w: RenderState2D.width, h: RenderState2D.height });
  2168. }
  2169. static popRT() {
  2170. var gl = LayaGL.instance;
  2171. var top = RenderTexture2D.rtStack.pop();
  2172. if (top) {
  2173. if (RenderTexture2D._currentActive != top.rt) {
  2174. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, top.rt ? top.rt._frameBuffer : null);
  2175. RenderTexture2D._currentActive = top.rt;
  2176. }
  2177. gl.viewport(0, 0, top.w, top.h);
  2178. RenderState2D.width = top.w;
  2179. RenderState2D.height = top.h;
  2180. }
  2181. }
  2182. start() {
  2183. var gl = LayaGL.instance;
  2184. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2185. this._lastRT = RenderTexture2D._currentActive;
  2186. RenderTexture2D._currentActive = this;
  2187. this._readyed = true;
  2188. gl.viewport(0, 0, this._width, this._height);
  2189. this._lastWidth = RenderState2D.width;
  2190. this._lastHeight = RenderState2D.height;
  2191. RenderState2D.width = this._width;
  2192. RenderState2D.height = this._height;
  2193. BaseShader.activeShader = null;
  2194. }
  2195. end() {
  2196. var gl = LayaGL.instance;
  2197. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2198. RenderTexture2D._currentActive = null;
  2199. this._readyed = true;
  2200. }
  2201. restore() {
  2202. var gl = LayaGL.instance;
  2203. if (this._lastRT != RenderTexture2D._currentActive) {
  2204. LayaGL.instance.bindFramebuffer(gl.FRAMEBUFFER, this._lastRT ? this._lastRT._frameBuffer : null);
  2205. RenderTexture2D._currentActive = this._lastRT;
  2206. }
  2207. this._readyed = true;
  2208. gl.viewport(0, 0, this._lastWidth, this._lastHeight);
  2209. RenderState2D.width = this._lastWidth;
  2210. RenderState2D.height = this._lastHeight;
  2211. BaseShader.activeShader = null;
  2212. }
  2213. clear(r = 0.0, g = 0.0, b = 0.0, a = 1.0) {
  2214. var gl = LayaGL.instance;
  2215. gl.clearColor(r, g, b, a);
  2216. var clearFlag = gl.COLOR_BUFFER_BIT;
  2217. switch (this._depthStencilFormat) {
  2218. case gl.DEPTH_COMPONENT16:
  2219. clearFlag |= gl.DEPTH_BUFFER_BIT;
  2220. break;
  2221. case gl.STENCIL_INDEX8:
  2222. clearFlag |= gl.STENCIL_BUFFER_BIT;
  2223. break;
  2224. case gl.DEPTH_STENCIL:
  2225. clearFlag |= gl.DEPTH_BUFFER_BIT;
  2226. clearFlag |= gl.STENCIL_BUFFER_BIT;
  2227. break;
  2228. }
  2229. gl.clear(clearFlag);
  2230. }
  2231. getData(x, y, width, height) {
  2232. if (ILaya.Render.isConchApp && window.conchConfig.threadMode == 2) {
  2233. throw "native 2 thread mode use getDataAsync";
  2234. }
  2235. var gl = LayaGL.instance;
  2236. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2237. var canRead = (gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE);
  2238. if (!canRead) {
  2239. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2240. return null;
  2241. }
  2242. var pixels = new Uint8Array(this._width * this._height * 4);
  2243. var glFormat = this._getGLFormat();
  2244. gl.readPixels(x, y, width, height, glFormat, gl.UNSIGNED_BYTE, pixels);
  2245. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2246. return pixels;
  2247. }
  2248. getDataAsync(x, y, width, height, callBack) {
  2249. var gl = LayaGL.instance;
  2250. gl.bindFramebuffer(gl.FRAMEBUFFER, this._frameBuffer);
  2251. gl.readPixelsAsync(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, function (data) {
  2252. callBack(new Uint8Array(data));
  2253. });
  2254. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  2255. }
  2256. recycle() {
  2257. }
  2258. _disposeResource() {
  2259. if (this._frameBuffer) {
  2260. var gl = LayaGL.instance;
  2261. gl.deleteTexture(this._glTexture);
  2262. gl.deleteFramebuffer(this._frameBuffer);
  2263. gl.deleteRenderbuffer(this._depthStencilBuffer);
  2264. this._glTexture = null;
  2265. this._frameBuffer = null;
  2266. this._depthStencilBuffer = null;
  2267. this._setGPUMemory(0);
  2268. }
  2269. }
  2270. }
  2271. RenderTexture2D.rtStack = [];
  2272. RenderTexture2D.defuv = [0, 0, 1, 0, 1, 1, 0, 1];
  2273. RenderTexture2D.flipyuv = [0, 1, 1, 1, 1, 0, 0, 0];
  2274. class WebGLRTMgr {
  2275. static getRT(w, h) {
  2276. w = w | 0;
  2277. h = h | 0;
  2278. if (w >= 10000) {
  2279. console.error('getRT error! w too big');
  2280. }
  2281. var ret;
  2282. ret = new RenderTexture2D(w, h, exports.RenderTextureFormat.R8G8B8A8, -1);
  2283. return ret;
  2284. }
  2285. static releaseRT(rt) {
  2286. rt._disposeResource();
  2287. return;
  2288. }
  2289. }
  2290. WebGLRTMgr.dict = {};
  2291. class BlendMode {
  2292. static _init_(gl) {
  2293. BlendMode.fns = [BlendMode.BlendNormal, BlendMode.BlendAdd, BlendMode.BlendMultiply, BlendMode.BlendScreen, BlendMode.BlendOverlay, BlendMode.BlendLight, BlendMode.BlendMask, BlendMode.BlendDestinationOut];
  2294. BlendMode.targetFns = [BlendMode.BlendNormalTarget, BlendMode.BlendAddTarget, BlendMode.BlendMultiplyTarget, BlendMode.BlendScreenTarget, BlendMode.BlendOverlayTarget, BlendMode.BlendLightTarget, BlendMode.BlendMask, BlendMode.BlendDestinationOut];
  2295. }
  2296. static BlendNormal(gl) {
  2297. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2298. }
  2299. static BlendAdd(gl) {
  2300. WebGLContext.setBlendFunc(gl, gl.ONE, gl.DST_ALPHA);
  2301. }
  2302. static BlendMultiply(gl) {
  2303. WebGLContext.setBlendFunc(gl, gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA);
  2304. }
  2305. static BlendScreen(gl) {
  2306. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE);
  2307. }
  2308. static BlendOverlay(gl) {
  2309. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_COLOR);
  2310. }
  2311. static BlendLight(gl) {
  2312. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE);
  2313. }
  2314. static BlendNormalTarget(gl) {
  2315. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  2316. }
  2317. static BlendAddTarget(gl) {
  2318. WebGLContext.setBlendFunc(gl, gl.ONE, gl.DST_ALPHA);
  2319. }
  2320. static BlendMultiplyTarget(gl) {
  2321. WebGLContext.setBlendFunc(gl, gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA);
  2322. }
  2323. static BlendScreenTarget(gl) {
  2324. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE);
  2325. }
  2326. static BlendOverlayTarget(gl) {
  2327. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_COLOR);
  2328. }
  2329. static BlendLightTarget(gl) {
  2330. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE);
  2331. }
  2332. static BlendMask(gl) {
  2333. WebGLContext.setBlendFunc(gl, gl.ZERO, gl.SRC_ALPHA);
  2334. }
  2335. static BlendDestinationOut(gl) {
  2336. WebGLContext.setBlendFunc(gl, gl.ZERO, gl.ZERO);
  2337. }
  2338. }
  2339. BlendMode.activeBlendFunction = null;
  2340. BlendMode.NAMES = ["normal", "add", "multiply", "screen", "overlay", "light", "mask", "destination-out"];
  2341. BlendMode.TOINT = { "normal": 0, "add": 1, "multiply": 2, "screen": 3, "overlay": 4, "light": 5, "mask": 6, "destination-out": 7, "lighter": 1 };
  2342. BlendMode.NORMAL = "normal";
  2343. BlendMode.ADD = "add";
  2344. BlendMode.MULTIPLY = "multiply";
  2345. BlendMode.SCREEN = "screen";
  2346. BlendMode.OVERLAY = "overlay";
  2347. BlendMode.LIGHT = "light";
  2348. BlendMode.MASK = "mask";
  2349. BlendMode.DESTINATIONOUT = "destination-out";
  2350. BlendMode.LIGHTER = "lighter";
  2351. BlendMode.fns = [];
  2352. BlendMode.targetFns = [];
  2353. class ShaderDefinesBase {
  2354. constructor(name2int, int2name, int2nameMap) {
  2355. this._value = 0;
  2356. this._name2int = name2int;
  2357. this._int2name = int2name;
  2358. this._int2nameMap = int2nameMap;
  2359. }
  2360. add(value) {
  2361. if (typeof (value) == 'string') {
  2362. this._value |= this._name2int[value];
  2363. }
  2364. else {
  2365. this._value |= value;
  2366. }
  2367. return this._value;
  2368. }
  2369. addInt(value) {
  2370. this._value |= value;
  2371. return this._value;
  2372. }
  2373. remove(value) {
  2374. if (typeof (value) == 'string') {
  2375. this._value &= ~(this._name2int[value]);
  2376. }
  2377. else {
  2378. this._value &= (~value);
  2379. }
  2380. return this._value;
  2381. }
  2382. isDefine(def) {
  2383. return (this._value & def) === def;
  2384. }
  2385. getValue() {
  2386. return this._value;
  2387. }
  2388. setValue(value) {
  2389. this._value = value;
  2390. }
  2391. toNameDic() {
  2392. var r = this._int2nameMap[this._value];
  2393. return r ? r : ShaderDefinesBase._toText(this._value, this._int2name, this._int2nameMap);
  2394. }
  2395. static _reg(name, value, _name2int, _int2name) {
  2396. _name2int[name] = value;
  2397. _int2name[value] = name;
  2398. }
  2399. static _toText(value, _int2name, _int2nameMap) {
  2400. var r = _int2nameMap[value];
  2401. if (r)
  2402. return r;
  2403. var o = {};
  2404. var d = 1;
  2405. for (var i = 0; i < 32; i++) {
  2406. d = 1 << i;
  2407. if (d > value)
  2408. break;
  2409. if (value & d) {
  2410. var name = _int2name[d];
  2411. name && (o[name] = "");
  2412. }
  2413. }
  2414. _int2nameMap[value] = o;
  2415. return o;
  2416. }
  2417. static _toInt(names, _name2int) {
  2418. var words = names.split('.');
  2419. var num = 0;
  2420. for (var i = 0, n = words.length; i < n; i++) {
  2421. var value = _name2int[words[i]];
  2422. if (!value)
  2423. throw new Error("Defines to int err:" + names + "/" + words[i]);
  2424. num |= value;
  2425. }
  2426. return num;
  2427. }
  2428. }
  2429. class ShaderDefines2D extends ShaderDefinesBase {
  2430. constructor() {
  2431. super(ShaderDefines2D.__name2int, ShaderDefines2D.__int2name, ShaderDefines2D.__int2nameMap);
  2432. }
  2433. static __init__() {
  2434. ShaderDefines2D.reg("TEXTURE2D", ShaderDefines2D.TEXTURE2D);
  2435. ShaderDefines2D.reg("PRIMITIVE", ShaderDefines2D.PRIMITIVE);
  2436. ShaderDefines2D.reg("GLOW_FILTER", ShaderDefines2D.FILTERGLOW);
  2437. ShaderDefines2D.reg("BLUR_FILTER", ShaderDefines2D.FILTERBLUR);
  2438. ShaderDefines2D.reg("COLOR_FILTER", ShaderDefines2D.FILTERCOLOR);
  2439. ShaderDefines2D.reg("COLOR_ADD", ShaderDefines2D.COLORADD);
  2440. ShaderDefines2D.reg("WORLDMAT", ShaderDefines2D.WORLDMAT);
  2441. ShaderDefines2D.reg("FILLTEXTURE", ShaderDefines2D.FILLTEXTURE);
  2442. ShaderDefines2D.reg('MVP3D', ShaderDefines2D.MVP3D);
  2443. }
  2444. static reg(name, value) {
  2445. this._reg(name, value, ShaderDefines2D.__name2int, ShaderDefines2D.__int2name);
  2446. }
  2447. static toText(value, int2name, int2nameMap) {
  2448. return this._toText(value, int2name, int2nameMap);
  2449. }
  2450. static toInt(names) {
  2451. return this._toInt(names, ShaderDefines2D.__name2int);
  2452. }
  2453. }
  2454. ShaderDefines2D.TEXTURE2D = 0x01;
  2455. ShaderDefines2D.PRIMITIVE = 0x04;
  2456. ShaderDefines2D.FILTERGLOW = 0x08;
  2457. ShaderDefines2D.FILTERBLUR = 0x10;
  2458. ShaderDefines2D.FILTERCOLOR = 0x20;
  2459. ShaderDefines2D.COLORADD = 0x40;
  2460. ShaderDefines2D.WORLDMAT = 0x80;
  2461. ShaderDefines2D.FILLTEXTURE = 0x100;
  2462. ShaderDefines2D.SKINMESH = 0x200;
  2463. ShaderDefines2D.MVP3D = 0x800;
  2464. ShaderDefines2D.NOOPTMASK = ShaderDefines2D.FILTERGLOW | ShaderDefines2D.FILTERBLUR | ShaderDefines2D.FILTERCOLOR | ShaderDefines2D.FILLTEXTURE;
  2465. ShaderDefines2D.__name2int = {};
  2466. ShaderDefines2D.__int2name = [];
  2467. ShaderDefines2D.__int2nameMap = [];
  2468. class Stat {
  2469. static show(x = 0, y = 0) {
  2470. Stat._StatRender.show(x, y);
  2471. }
  2472. static enable() {
  2473. Stat._StatRender.enable();
  2474. }
  2475. static hide() {
  2476. Stat._StatRender.hide();
  2477. }
  2478. static clear() {
  2479. Stat.trianglesFaces = Stat.renderBatches = Stat.savedRenderBatches = Stat.shaderCall = Stat.spriteRenderUseCacheCount = Stat.frustumCulling = Stat.octreeNodeCulling = Stat.canvasNormal = Stat.canvasBitmap = Stat.canvasReCache = 0;
  2480. }
  2481. static set onclick(fn) {
  2482. Stat._StatRender.set_onclick(fn);
  2483. }
  2484. }
  2485. Stat.FPS = 0;
  2486. Stat.loopCount = 0;
  2487. Stat.shaderCall = 0;
  2488. Stat.renderBatches = 0;
  2489. Stat.savedRenderBatches = 0;
  2490. Stat.trianglesFaces = 0;
  2491. Stat.spriteCount = 0;
  2492. Stat.spriteRenderUseCacheCount = 0;
  2493. Stat.frustumCulling = 0;
  2494. Stat.octreeNodeCulling = 0;
  2495. Stat.canvasNormal = 0;
  2496. Stat.canvasBitmap = 0;
  2497. Stat.canvasReCache = 0;
  2498. Stat.renderSlow = false;
  2499. Stat._fpsData = [];
  2500. Stat._timer = 0;
  2501. Stat._count = 0;
  2502. Stat._StatRender = null;
  2503. class StringKey {
  2504. constructor() {
  2505. this._strsToID = {};
  2506. this._idToStrs = [];
  2507. this._length = 0;
  2508. }
  2509. add(str) {
  2510. var index = this._strsToID[str];
  2511. if (index != null)
  2512. return index;
  2513. this._idToStrs[this._length] = str;
  2514. return this._strsToID[str] = this._length++;
  2515. }
  2516. getID(str) {
  2517. var index = this._strsToID[str];
  2518. return index == null ? -1 : index;
  2519. }
  2520. getName(id) {
  2521. var str = this._idToStrs[id];
  2522. return str == null ? undefined : str;
  2523. }
  2524. }
  2525. class Shader extends BaseShader {
  2526. constructor(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  2527. super();
  2528. this._attribInfo = null;
  2529. this.customCompile = false;
  2530. this._curActTexIndex = 0;
  2531. this.tag = {};
  2532. this._program = null;
  2533. this._params = null;
  2534. this._paramsMap = {};
  2535. if ((!vs) || (!ps))
  2536. throw "Shader Error";
  2537. this._attribInfo = bindAttrib;
  2538. this._id = ++Shader._count;
  2539. this._vs = vs;
  2540. this._ps = ps;
  2541. this._nameMap = nameMap ? nameMap : {};
  2542. saveName != null && (Shader.sharders[saveName] = this);
  2543. this.recreateResource();
  2544. this.lock = true;
  2545. }
  2546. static getShader(name) {
  2547. return Shader.sharders[name];
  2548. }
  2549. static create(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  2550. return new Shader(vs, ps, saveName, nameMap, bindAttrib);
  2551. }
  2552. static withCompile(nameID, define, shaderName, createShader) {
  2553. if (shaderName && Shader.sharders[shaderName])
  2554. return Shader.sharders[shaderName];
  2555. var pre = Shader._preCompileShader[Shader.SHADERNAME2ID * nameID];
  2556. if (!pre)
  2557. throw new Error("withCompile shader err!" + nameID);
  2558. return pre.createShader(define, shaderName, createShader, null);
  2559. }
  2560. static withCompile2D(nameID, mainID, define, shaderName, createShader, bindAttrib = null) {
  2561. if (shaderName && Shader.sharders[shaderName])
  2562. return Shader.sharders[shaderName];
  2563. var pre = Shader._preCompileShader[Shader.SHADERNAME2ID * nameID + mainID];
  2564. if (!pre)
  2565. throw new Error("withCompile shader err!" + nameID + " " + mainID);
  2566. return pre.createShader(define, shaderName, createShader, bindAttrib);
  2567. }
  2568. static addInclude(fileName, txt) {
  2569. ILaya.ShaderCompile.addInclude(fileName, txt);
  2570. }
  2571. static preCompile(nameID, vs, ps, nameMap) {
  2572. var id = Shader.SHADERNAME2ID * nameID;
  2573. Shader._preCompileShader[id] = new ILaya.ShaderCompile(vs, ps, nameMap);
  2574. }
  2575. static preCompile2D(nameID, mainID, vs, ps, nameMap) {
  2576. var id = Shader.SHADERNAME2ID * nameID + mainID;
  2577. Shader._preCompileShader[id] = new ILaya.ShaderCompile(vs, ps, nameMap);
  2578. }
  2579. recreateResource() {
  2580. this._compile();
  2581. this._setGPUMemory(0);
  2582. }
  2583. _disposeResource() {
  2584. WebGLContext.mainContext.deleteShader(this._vshader);
  2585. WebGLContext.mainContext.deleteShader(this._pshader);
  2586. WebGLContext.mainContext.deleteProgram(this._program);
  2587. this._vshader = this._pshader = this._program = null;
  2588. this._params = null;
  2589. this._paramsMap = {};
  2590. this._setGPUMemory(0);
  2591. this._curActTexIndex = 0;
  2592. }
  2593. _compile() {
  2594. if (!this._vs || !this._ps || this._params)
  2595. return;
  2596. this._reCompile = true;
  2597. this._params = [];
  2598. var result;
  2599. if (this.customCompile)
  2600. result = ILaya.ShaderCompile.preGetParams(this._vs, this._ps);
  2601. var gl = WebGLContext.mainContext;
  2602. this._program = gl.createProgram();
  2603. this._vshader = Shader._createShader(gl, this._vs, gl.VERTEX_SHADER);
  2604. this._pshader = Shader._createShader(gl, this._ps, gl.FRAGMENT_SHADER);
  2605. gl.attachShader(this._program, this._vshader);
  2606. gl.attachShader(this._program, this._pshader);
  2607. var one, i, n, location;
  2608. var attribDescNum = this._attribInfo ? this._attribInfo.length : 0;
  2609. for (i = 0; i < attribDescNum; i += 2) {
  2610. gl.bindAttribLocation(this._program, this._attribInfo[i + 1], this._attribInfo[i]);
  2611. }
  2612. gl.linkProgram(this._program);
  2613. if (!this.customCompile && !gl.getProgramParameter(this._program, gl.LINK_STATUS)) {
  2614. throw gl.getProgramInfoLog(this._program);
  2615. }
  2616. var nUniformNum = this.customCompile ? result.uniforms.length : gl.getProgramParameter(this._program, gl.ACTIVE_UNIFORMS);
  2617. for (i = 0; i < nUniformNum; i++) {
  2618. var uniform = this.customCompile ? result.uniforms[i] : gl.getActiveUniform(this._program, i);
  2619. location = gl.getUniformLocation(this._program, uniform.name);
  2620. one = { vartype: "uniform", glfun: null, ivartype: 1, location: location, name: uniform.name, type: uniform.type, isArray: false, isSame: false, preValue: null, indexOfParams: 0 };
  2621. if (one.name.indexOf('[0]') > 0) {
  2622. one.name = one.name.substr(0, one.name.length - 3);
  2623. one.isArray = true;
  2624. one.location = gl.getUniformLocation(this._program, one.name);
  2625. }
  2626. this._params.push(one);
  2627. }
  2628. for (i = 0, n = this._params.length; i < n; i++) {
  2629. one = this._params[i];
  2630. one.indexOfParams = i;
  2631. one.index = 1;
  2632. one.value = [one.location, null];
  2633. one.codename = one.name;
  2634. one.name = this._nameMap[one.codename] ? this._nameMap[one.codename] : one.codename;
  2635. this._paramsMap[one.name] = one;
  2636. one._this = this;
  2637. one.uploadedValue = [];
  2638. switch (one.type) {
  2639. case gl.INT:
  2640. one.fun = one.isArray ? this._uniform1iv : this._uniform1i;
  2641. break;
  2642. case gl.FLOAT:
  2643. one.fun = one.isArray ? this._uniform1fv : this._uniform1f;
  2644. break;
  2645. case gl.FLOAT_VEC2:
  2646. one.fun = one.isArray ? this._uniform_vec2v : this._uniform_vec2;
  2647. break;
  2648. case gl.FLOAT_VEC3:
  2649. one.fun = one.isArray ? this._uniform_vec3v : this._uniform_vec3;
  2650. break;
  2651. case gl.FLOAT_VEC4:
  2652. one.fun = one.isArray ? this._uniform_vec4v : this._uniform_vec4;
  2653. break;
  2654. case gl.SAMPLER_2D:
  2655. one.fun = this._uniform_sampler2D;
  2656. break;
  2657. case gl.SAMPLER_CUBE:
  2658. one.fun = this._uniform_samplerCube;
  2659. break;
  2660. case gl.FLOAT_MAT4:
  2661. one.glfun = gl.uniformMatrix4fv;
  2662. one.fun = this._uniformMatrix4fv;
  2663. break;
  2664. case gl.BOOL:
  2665. one.fun = this._uniform1i;
  2666. break;
  2667. case gl.FLOAT_MAT2:
  2668. case gl.FLOAT_MAT3:
  2669. throw new Error("compile shader err!");
  2670. default:
  2671. throw new Error("compile shader err!");
  2672. }
  2673. }
  2674. }
  2675. static _createShader(gl, str, type) {
  2676. var shader = gl.createShader(type);
  2677. gl.shaderSource(shader, str);
  2678. gl.compileShader(shader);
  2679. if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  2680. return shader;
  2681. }
  2682. else {
  2683. console.log(gl.getShaderInfoLog(shader));
  2684. return null;
  2685. }
  2686. }
  2687. getUniform(name) {
  2688. return this._paramsMap[name];
  2689. }
  2690. _uniform1f(one, value) {
  2691. var uploadedValue = one.uploadedValue;
  2692. if (uploadedValue[0] !== value) {
  2693. WebGLContext.mainContext.uniform1f(one.location, uploadedValue[0] = value);
  2694. return 1;
  2695. }
  2696. return 0;
  2697. }
  2698. _uniform1fv(one, value) {
  2699. if (value.length < 4) {
  2700. var uploadedValue = one.uploadedValue;
  2701. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  2702. WebGLContext.mainContext.uniform1fv(one.location, value);
  2703. uploadedValue[0] = value[0];
  2704. uploadedValue[1] = value[1];
  2705. uploadedValue[2] = value[2];
  2706. uploadedValue[3] = value[3];
  2707. return 1;
  2708. }
  2709. return 0;
  2710. }
  2711. else {
  2712. WebGLContext.mainContext.uniform1fv(one.location, value);
  2713. return 1;
  2714. }
  2715. }
  2716. _uniform_vec2(one, value) {
  2717. var uploadedValue = one.uploadedValue;
  2718. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1]) {
  2719. WebGLContext.mainContext.uniform2f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1]);
  2720. return 1;
  2721. }
  2722. return 0;
  2723. }
  2724. _uniform_vec2v(one, value) {
  2725. if (value.length < 2) {
  2726. var uploadedValue = one.uploadedValue;
  2727. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  2728. WebGLContext.mainContext.uniform2fv(one.location, value);
  2729. uploadedValue[0] = value[0];
  2730. uploadedValue[1] = value[1];
  2731. uploadedValue[2] = value[2];
  2732. uploadedValue[3] = value[3];
  2733. return 1;
  2734. }
  2735. return 0;
  2736. }
  2737. else {
  2738. WebGLContext.mainContext.uniform2fv(one.location, value);
  2739. return 1;
  2740. }
  2741. }
  2742. _uniform_vec3(one, value) {
  2743. var uploadedValue = one.uploadedValue;
  2744. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2]) {
  2745. WebGLContext.mainContext.uniform3f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2]);
  2746. return 1;
  2747. }
  2748. return 0;
  2749. }
  2750. _uniform_vec3v(one, value) {
  2751. WebGLContext.mainContext.uniform3fv(one.location, value);
  2752. return 1;
  2753. }
  2754. _uniform_vec4(one, value) {
  2755. var uploadedValue = one.uploadedValue;
  2756. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  2757. WebGLContext.mainContext.uniform4f(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2], uploadedValue[3] = value[3]);
  2758. return 1;
  2759. }
  2760. return 0;
  2761. }
  2762. _uniform_vec4v(one, value) {
  2763. WebGLContext.mainContext.uniform4fv(one.location, value);
  2764. return 1;
  2765. }
  2766. _uniformMatrix2fv(one, value) {
  2767. WebGLContext.mainContext.uniformMatrix2fv(one.location, false, value);
  2768. return 1;
  2769. }
  2770. _uniformMatrix3fv(one, value) {
  2771. WebGLContext.mainContext.uniformMatrix3fv(one.location, false, value);
  2772. return 1;
  2773. }
  2774. _uniformMatrix4fv(one, value) {
  2775. WebGLContext.mainContext.uniformMatrix4fv(one.location, false, value);
  2776. return 1;
  2777. }
  2778. _uniform1i(one, value) {
  2779. var uploadedValue = one.uploadedValue;
  2780. if (uploadedValue[0] !== value) {
  2781. WebGLContext.mainContext.uniform1i(one.location, uploadedValue[0] = value);
  2782. return 1;
  2783. }
  2784. return 0;
  2785. }
  2786. _uniform1iv(one, value) {
  2787. WebGLContext.mainContext.uniform1iv(one.location, value);
  2788. return 1;
  2789. }
  2790. _uniform_ivec2(one, value) {
  2791. var uploadedValue = one.uploadedValue;
  2792. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1]) {
  2793. WebGLContext.mainContext.uniform2i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1]);
  2794. return 1;
  2795. }
  2796. return 0;
  2797. }
  2798. _uniform_ivec2v(one, value) {
  2799. WebGLContext.mainContext.uniform2iv(one.location, value);
  2800. return 1;
  2801. }
  2802. _uniform_vec3i(one, value) {
  2803. var uploadedValue = one.uploadedValue;
  2804. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2]) {
  2805. WebGLContext.mainContext.uniform3i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2]);
  2806. return 1;
  2807. }
  2808. return 0;
  2809. }
  2810. _uniform_vec3vi(one, value) {
  2811. WebGLContext.mainContext.uniform3iv(one.location, value);
  2812. return 1;
  2813. }
  2814. _uniform_vec4i(one, value) {
  2815. var uploadedValue = one.uploadedValue;
  2816. if (uploadedValue[0] !== value[0] || uploadedValue[1] !== value[1] || uploadedValue[2] !== value[2] || uploadedValue[3] !== value[3]) {
  2817. WebGLContext.mainContext.uniform4i(one.location, uploadedValue[0] = value[0], uploadedValue[1] = value[1], uploadedValue[2] = value[2], uploadedValue[3] = value[3]);
  2818. return 1;
  2819. }
  2820. return 0;
  2821. }
  2822. _uniform_vec4vi(one, value) {
  2823. WebGLContext.mainContext.uniform4iv(one.location, value);
  2824. return 1;
  2825. }
  2826. _uniform_sampler2D(one, value) {
  2827. var gl = WebGLContext.mainContext;
  2828. var uploadedValue = one.uploadedValue;
  2829. if (uploadedValue[0] == null) {
  2830. uploadedValue[0] = this._curActTexIndex;
  2831. gl.uniform1i(one.location, this._curActTexIndex);
  2832. WebGLContext.activeTexture(gl, gl.TEXTURE0 + this._curActTexIndex);
  2833. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, value);
  2834. this._curActTexIndex++;
  2835. return 1;
  2836. }
  2837. else {
  2838. WebGLContext.activeTexture(gl, gl.TEXTURE0 + uploadedValue[0]);
  2839. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, value);
  2840. return 0;
  2841. }
  2842. }
  2843. _uniform_samplerCube(one, value) {
  2844. var gl = WebGLContext.mainContext;
  2845. var uploadedValue = one.uploadedValue;
  2846. if (uploadedValue[0] == null) {
  2847. uploadedValue[0] = this._curActTexIndex;
  2848. gl.uniform1i(one.location, this._curActTexIndex);
  2849. WebGLContext.activeTexture(gl, gl.TEXTURE0 + this._curActTexIndex);
  2850. WebGLContext.bindTexture(gl, gl.TEXTURE_CUBE_MAP, value);
  2851. this._curActTexIndex++;
  2852. return 1;
  2853. }
  2854. else {
  2855. WebGLContext.activeTexture(gl, gl.TEXTURE0 + uploadedValue[0]);
  2856. WebGLContext.bindTexture(gl, gl.TEXTURE_CUBE_MAP, value);
  2857. return 0;
  2858. }
  2859. }
  2860. _noSetValue(one) {
  2861. console.log("no....:" + one.name);
  2862. }
  2863. uploadOne(name, value) {
  2864. WebGLContext.useProgram(WebGLContext.mainContext, this._program);
  2865. var one = this._paramsMap[name];
  2866. one.fun.call(this, one, value);
  2867. }
  2868. uploadTexture2D(value) {
  2869. var CTX = WebGLContext;
  2870. if (CTX._activeTextures[0] !== value) {
  2871. CTX.bindTexture(WebGLContext.mainContext, LayaGL.instance.TEXTURE_2D, value);
  2872. CTX._activeTextures[0] = value;
  2873. }
  2874. }
  2875. upload(shaderValue, params = null) {
  2876. BaseShader.activeShader = BaseShader.bindShader = this;
  2877. var gl = WebGLContext.mainContext;
  2878. WebGLContext.useProgram(gl, this._program);
  2879. if (this._reCompile) {
  2880. params = this._params;
  2881. this._reCompile = false;
  2882. }
  2883. else {
  2884. params = params || this._params;
  2885. }
  2886. var one, value, n = params.length, shaderCall = 0;
  2887. for (var i = 0; i < n; i++) {
  2888. one = params[i];
  2889. if ((value = shaderValue[one.name]) !== null)
  2890. shaderCall += one.fun.call(this, one, value);
  2891. }
  2892. Stat.shaderCall += shaderCall;
  2893. }
  2894. uploadArray(shaderValue, length, _bufferUsage) {
  2895. BaseShader.activeShader = this;
  2896. BaseShader.bindShader = this;
  2897. WebGLContext.useProgram(WebGLContext.mainContext, this._program);
  2898. var params = this._params, value;
  2899. var one, shaderCall = 0;
  2900. for (var i = length - 2; i >= 0; i -= 2) {
  2901. one = this._paramsMap[shaderValue[i]];
  2902. if (!one)
  2903. continue;
  2904. value = shaderValue[i + 1];
  2905. if (value != null) {
  2906. _bufferUsage && _bufferUsage[one.name] && _bufferUsage[one.name].bind();
  2907. shaderCall += one.fun.call(this, one, value);
  2908. }
  2909. }
  2910. Stat.shaderCall += shaderCall;
  2911. }
  2912. getParams() {
  2913. return this._params;
  2914. }
  2915. setAttributesLocation(attribDesc) {
  2916. this._attribInfo = attribDesc;
  2917. }
  2918. }
  2919. Shader._count = 0;
  2920. Shader._preCompileShader = {};
  2921. Shader.SHADERNAME2ID = 0.0002;
  2922. Shader.nameKey = new StringKey();
  2923. Shader.sharders = new Array(0x20);
  2924. class Shader2X extends Shader {
  2925. constructor(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  2926. super(vs, ps, saveName, nameMap, bindAttrib);
  2927. this._params2dQuick2 = null;
  2928. this._shaderValueWidth = 0;
  2929. this._shaderValueHeight = 0;
  2930. }
  2931. _disposeResource() {
  2932. super._disposeResource();
  2933. this._params2dQuick2 = null;
  2934. }
  2935. upload2dQuick2(shaderValue) {
  2936. this.upload(shaderValue, this._params2dQuick2 || this._make2dQuick2());
  2937. }
  2938. _make2dQuick2() {
  2939. if (!this._params2dQuick2) {
  2940. this._params2dQuick2 = [];
  2941. var params = this._params, one;
  2942. for (var i = 0, n = params.length; i < n; i++) {
  2943. one = params[i];
  2944. if (one.name !== "size")
  2945. this._params2dQuick2.push(one);
  2946. }
  2947. }
  2948. return this._params2dQuick2;
  2949. }
  2950. static create(vs, ps, saveName = null, nameMap = null, bindAttrib = null) {
  2951. return new Shader2X(vs, ps, saveName, nameMap, bindAttrib);
  2952. }
  2953. }
  2954. class Value2D {
  2955. constructor(mainID, subID) {
  2956. this.defines = new ShaderDefines2D();
  2957. this.size = [0, 0];
  2958. this.alpha = 1.0;
  2959. this.ALPHA = 1.0;
  2960. this.subID = 0;
  2961. this.ref = 1;
  2962. this._cacheID = 0;
  2963. this.clipMatDir = [ILaya.Context._MAXSIZE, 0, 0, ILaya.Context._MAXSIZE];
  2964. this.clipMatPos = [0, 0];
  2965. this.clipOff = [0, 0];
  2966. this.mainID = mainID;
  2967. this.subID = subID;
  2968. this.textureHost = null;
  2969. this.texture = null;
  2970. this.color = null;
  2971. this.colorAdd = null;
  2972. this.u_mmat2 = null;
  2973. this._cacheID = mainID | subID;
  2974. this._inClassCache = Value2D._cache[this._cacheID];
  2975. if (mainID > 0 && !this._inClassCache) {
  2976. this._inClassCache = Value2D._cache[this._cacheID] = [];
  2977. this._inClassCache._length = 0;
  2978. }
  2979. this.clear();
  2980. }
  2981. static _initone(type, classT) {
  2982. Value2D._typeClass[type] = classT;
  2983. Value2D._cache[type] = [];
  2984. Value2D._cache[type]._length = 0;
  2985. }
  2986. static __init__() {
  2987. }
  2988. setValue(value) { }
  2989. _ShaderWithCompile() {
  2990. var ret = Shader.withCompile2D(0, this.mainID, this.defines.toNameDic(), this.mainID | this.defines._value, Shader2X.create, this._attribLocation);
  2991. return ret;
  2992. }
  2993. upload() {
  2994. var renderstate2d = RenderState2D;
  2995. RenderState2D.worldMatrix4 === RenderState2D.TEMPMAT4_ARRAY || this.defines.addInt(ShaderDefines2D.WORLDMAT);
  2996. this.mmat = renderstate2d.worldMatrix4;
  2997. if (RenderState2D.matWVP) {
  2998. this.defines.addInt(ShaderDefines2D.MVP3D);
  2999. this.u_MvpMatrix = RenderState2D.matWVP.elements;
  3000. }
  3001. var sd = Shader.sharders[this.mainID | this.defines._value] || this._ShaderWithCompile();
  3002. if (sd._shaderValueWidth !== renderstate2d.width || sd._shaderValueHeight !== renderstate2d.height) {
  3003. this.size[0] = renderstate2d.width;
  3004. this.size[1] = renderstate2d.height;
  3005. sd._shaderValueWidth = renderstate2d.width;
  3006. sd._shaderValueHeight = renderstate2d.height;
  3007. sd.upload(this, null);
  3008. }
  3009. else {
  3010. sd.upload(this, sd._params2dQuick2 || sd._make2dQuick2());
  3011. }
  3012. }
  3013. setFilters(value) {
  3014. this.filters = value;
  3015. if (!value)
  3016. return;
  3017. var n = value.length, f;
  3018. for (var i = 0; i < n; i++) {
  3019. f = value[i];
  3020. if (f) {
  3021. this.defines.add(f.type);
  3022. f.action.setValue(this);
  3023. }
  3024. }
  3025. }
  3026. clear() {
  3027. this.defines._value = this.subID;
  3028. this.clipOff[0] = 0;
  3029. }
  3030. release() {
  3031. if ((--this.ref) < 1) {
  3032. this._inClassCache && (this._inClassCache[this._inClassCache._length++] = this);
  3033. this.clear();
  3034. this.filters = null;
  3035. this.ref = 1;
  3036. this.clipOff[0] = 0;
  3037. }
  3038. }
  3039. static create(mainType, subType) {
  3040. var types = Value2D._cache[mainType | subType];
  3041. if (types._length)
  3042. return types[--types._length];
  3043. else
  3044. return new Value2D._typeClass[mainType | subType](subType);
  3045. }
  3046. }
  3047. Value2D._cache = [];
  3048. Value2D._typeClass = [];
  3049. Value2D.TEMPMAT4_ARRAY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  3050. class SubmitKey {
  3051. constructor() {
  3052. this.clear();
  3053. }
  3054. clear() {
  3055. this.submitType = -1;
  3056. this.blendShader = this.other = 0;
  3057. }
  3058. copyFrom(src) {
  3059. this.other = src.other;
  3060. this.blendShader = src.blendShader;
  3061. this.submitType = src.submitType;
  3062. }
  3063. copyFrom2(src, submitType, other) {
  3064. this.other = other;
  3065. this.submitType = submitType;
  3066. }
  3067. equal3_2(next, submitType, other) {
  3068. return this.submitType === submitType && this.other === other && this.blendShader === next.blendShader;
  3069. }
  3070. equal4_2(next, submitType, other) {
  3071. return this.submitType === submitType && this.other === other && this.blendShader === next.blendShader;
  3072. }
  3073. equal_3(next) {
  3074. return this.submitType === next.submitType && this.blendShader === next.blendShader;
  3075. }
  3076. equal(next) {
  3077. return this.other === next.other && this.submitType === next.submitType && this.blendShader === next.blendShader;
  3078. }
  3079. }
  3080. class SubmitCMD {
  3081. constructor() {
  3082. this._ref = 1;
  3083. this._key = new SubmitKey();
  3084. }
  3085. renderSubmit() {
  3086. this.fun.apply(this._this, this.args);
  3087. return 1;
  3088. }
  3089. getRenderType() {
  3090. return 0;
  3091. }
  3092. releaseRender() {
  3093. if ((--this._ref) < 1) {
  3094. var pool = SubmitCMD.POOL;
  3095. pool[pool._length++] = this;
  3096. }
  3097. }
  3098. static create(args, fun, thisobj) {
  3099. var o = SubmitCMD.POOL._length ? SubmitCMD.POOL[--SubmitCMD.POOL._length] : new SubmitCMD();
  3100. o.fun = fun;
  3101. o.args = args;
  3102. o._this = thisobj;
  3103. o._ref = 1;
  3104. o._key.clear();
  3105. return o;
  3106. }
  3107. }
  3108. SubmitCMD.POOL = [];
  3109. {
  3110. SubmitCMD.POOL._length = 0;
  3111. }
  3112. class Filter {
  3113. constructor() { }
  3114. get type() { return -1; }
  3115. }
  3116. Filter.BLUR = 0x10;
  3117. Filter.COLOR = 0x20;
  3118. Filter.GLOW = 0x08;
  3119. Filter._filter = function (sprite, context, x, y) {
  3120. var webglctx = context;
  3121. var next = this._next;
  3122. if (next) {
  3123. var filters = sprite.filters, len = filters.length;
  3124. if (len == 1 && (filters[0].type == Filter.COLOR)) {
  3125. context.save();
  3126. context.setColorFilter(filters[0]);
  3127. next._fun.call(next, sprite, context, x, y);
  3128. context.restore();
  3129. return;
  3130. }
  3131. var svCP = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  3132. var b;
  3133. var p = Point.TEMP;
  3134. var tMatrix = webglctx._curMat;
  3135. var mat = Matrix.create();
  3136. tMatrix.copyTo(mat);
  3137. var tPadding = 0;
  3138. var tHalfPadding = 0;
  3139. var tIsHaveGlowFilter = false;
  3140. var source = null;
  3141. var out = sprite._cacheStyle.filterCache || null;
  3142. if (!out || sprite.getRepaint() != 0) {
  3143. tIsHaveGlowFilter = sprite._isHaveGlowFilter();
  3144. if (tIsHaveGlowFilter) {
  3145. tPadding = 50;
  3146. tHalfPadding = 25;
  3147. }
  3148. b = new Rectangle();
  3149. b.copyFrom(sprite.getSelfBounds());
  3150. b.x += sprite.x;
  3151. b.y += sprite.y;
  3152. b.x -= sprite.pivotX + 4;
  3153. b.y -= sprite.pivotY + 4;
  3154. var tSX = b.x;
  3155. var tSY = b.y;
  3156. b.width += (tPadding + 8);
  3157. b.height += (tPadding + 8);
  3158. p.x = b.x * mat.a + b.y * mat.c;
  3159. p.y = b.y * mat.d + b.x * mat.b;
  3160. b.x = p.x;
  3161. b.y = p.y;
  3162. p.x = b.width * mat.a + b.height * mat.c;
  3163. p.y = b.height * mat.d + b.width * mat.b;
  3164. b.width = p.x;
  3165. b.height = p.y;
  3166. if (b.width <= 0 || b.height <= 0) {
  3167. return;
  3168. }
  3169. out && WebGLRTMgr.releaseRT(out);
  3170. source = WebGLRTMgr.getRT(b.width, b.height);
  3171. var outRT = out = WebGLRTMgr.getRT(b.width, b.height);
  3172. sprite._getCacheStyle().filterCache = out;
  3173. webglctx.pushRT();
  3174. webglctx.useRT(source);
  3175. var tX = sprite.x - tSX + tHalfPadding;
  3176. var tY = sprite.y - tSY + tHalfPadding;
  3177. next._fun.call(next, sprite, context, tX, tY);
  3178. webglctx.useRT(outRT);
  3179. for (var i = 0; i < len; i++) {
  3180. if (i != 0) {
  3181. webglctx.useRT(source);
  3182. webglctx.drawTarget(outRT, 0, 0, b.width, b.height, Matrix.TEMP.identity(), svCP, null, BlendMode.TOINT.overlay);
  3183. webglctx.useRT(outRT);
  3184. }
  3185. var fil = filters[i];
  3186. switch (fil.type) {
  3187. case Filter.BLUR:
  3188. fil._glRender && fil._glRender.render(source, context, b.width, b.height, fil);
  3189. break;
  3190. case Filter.GLOW:
  3191. fil._glRender && fil._glRender.render(source, context, b.width, b.height, fil);
  3192. break;
  3193. case Filter.COLOR:
  3194. webglctx.setColorFilter(fil);
  3195. webglctx.drawTarget(source, 0, 0, b.width, b.height, Matrix.EMPTY.identity(), Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  3196. webglctx.setColorFilter(null);
  3197. break;
  3198. }
  3199. }
  3200. webglctx.popRT();
  3201. }
  3202. else {
  3203. tIsHaveGlowFilter = sprite._cacheStyle.hasGlowFilter || false;
  3204. if (tIsHaveGlowFilter) {
  3205. tPadding = 50;
  3206. tHalfPadding = 25;
  3207. }
  3208. b = sprite.getBounds();
  3209. if (b.width <= 0 || b.height <= 0) {
  3210. return;
  3211. }
  3212. b.width += tPadding;
  3213. b.height += tPadding;
  3214. p.x = b.x * mat.a + b.y * mat.c;
  3215. p.y = b.y * mat.d + b.x * mat.b;
  3216. b.x = p.x;
  3217. b.y = p.y;
  3218. p.x = b.width * mat.a + b.height * mat.c;
  3219. p.y = b.height * mat.d + b.width * mat.b;
  3220. b.width = p.x;
  3221. b.height = p.y;
  3222. }
  3223. x = x - tHalfPadding - sprite.x;
  3224. y = y - tHalfPadding - sprite.y;
  3225. p.setTo(x, y);
  3226. mat.transformPoint(p);
  3227. x = p.x + b.x;
  3228. y = p.y + b.y;
  3229. webglctx._drawRenderTexture(out, x, y, b.width, b.height, Matrix.TEMP.identity(), 1.0, RenderTexture2D.defuv);
  3230. if (source) {
  3231. var submit = SubmitCMD.create([source], function (s) {
  3232. s.destroy();
  3233. }, this);
  3234. source = null;
  3235. context.addRenderObject(submit);
  3236. }
  3237. mat.destroy();
  3238. }
  3239. };
  3240. class Utils {
  3241. static toRadian(angle) {
  3242. return angle * Utils._pi2;
  3243. }
  3244. static toAngle(radian) {
  3245. return radian * Utils._pi;
  3246. }
  3247. static toHexColor(color) {
  3248. if (color < 0 || isNaN(color))
  3249. return null;
  3250. var str = color.toString(16);
  3251. while (str.length < 6)
  3252. str = "0" + str;
  3253. return "#" + str;
  3254. }
  3255. static getGID() {
  3256. return Utils._gid++;
  3257. }
  3258. static concatArray(source, array) {
  3259. if (!array)
  3260. return source;
  3261. if (!source)
  3262. return array;
  3263. var i, len = array.length;
  3264. for (i = 0; i < len; i++) {
  3265. source.push(array[i]);
  3266. }
  3267. return source;
  3268. }
  3269. static clearArray(array) {
  3270. if (!array)
  3271. return array;
  3272. array.length = 0;
  3273. return array;
  3274. }
  3275. static copyArray(source, array) {
  3276. source || (source = []);
  3277. if (!array)
  3278. return source;
  3279. source.length = array.length;
  3280. var i, len = array.length;
  3281. for (i = 0; i < len; i++) {
  3282. source[i] = array[i];
  3283. }
  3284. return source;
  3285. }
  3286. static getGlobalRecByPoints(sprite, x0, y0, x1, y1) {
  3287. var newLTPoint;
  3288. newLTPoint = Point.create().setTo(x0, y0);
  3289. newLTPoint = sprite.localToGlobal(newLTPoint);
  3290. var newRBPoint;
  3291. newRBPoint = Point.create().setTo(x1, y1);
  3292. newRBPoint = sprite.localToGlobal(newRBPoint);
  3293. var rst = Rectangle._getWrapRec([newLTPoint.x, newLTPoint.y, newRBPoint.x, newRBPoint.y]);
  3294. newLTPoint.recover();
  3295. newRBPoint.recover();
  3296. return rst;
  3297. }
  3298. static getGlobalPosAndScale(sprite) {
  3299. return Utils.getGlobalRecByPoints(sprite, 0, 0, 1, 1);
  3300. }
  3301. static bind(fun, scope) {
  3302. var rst = fun;
  3303. rst = fun.bind(scope);
  3304. return rst;
  3305. }
  3306. static updateOrder(array) {
  3307. if (!array || array.length < 2)
  3308. return false;
  3309. var i = 1, j, len = array.length, key, c;
  3310. while (i < len) {
  3311. j = i;
  3312. c = array[j];
  3313. key = array[j]._zOrder;
  3314. while (--j > -1) {
  3315. if (array[j]._zOrder > key)
  3316. array[j + 1] = array[j];
  3317. else
  3318. break;
  3319. }
  3320. array[j + 1] = c;
  3321. i++;
  3322. }
  3323. return true;
  3324. }
  3325. static transPointList(points, x, y) {
  3326. var i, len = points.length;
  3327. for (i = 0; i < len; i += 2) {
  3328. points[i] += x;
  3329. points[i + 1] += y;
  3330. }
  3331. }
  3332. static parseInt(str, radix = 0) {
  3333. var result = parseInt(str, radix);
  3334. if (isNaN(result))
  3335. return 0;
  3336. return result;
  3337. }
  3338. static getFileExtension(path) {
  3339. Utils._extReg.lastIndex = path.lastIndexOf(".");
  3340. var result = Utils._extReg.exec(path);
  3341. if (result && result.length > 1) {
  3342. return result[1].toLowerCase();
  3343. }
  3344. return null;
  3345. }
  3346. static getTransformRelativeToWindow(coordinateSpace, x, y) {
  3347. var stage = Utils.gStage;
  3348. var globalTransform = Utils.getGlobalPosAndScale(coordinateSpace);
  3349. var canvasMatrix = stage._canvasTransform.clone();
  3350. var canvasLeft = canvasMatrix.tx;
  3351. var canvasTop = canvasMatrix.ty;
  3352. canvasMatrix.rotate(-Math.PI / 180 * stage.canvasDegree);
  3353. canvasMatrix.scale(stage.clientScaleX, stage.clientScaleY);
  3354. var perpendicular = (stage.canvasDegree % 180 != 0);
  3355. var tx, ty;
  3356. if (perpendicular) {
  3357. tx = y + globalTransform.y;
  3358. ty = x + globalTransform.x;
  3359. tx *= canvasMatrix.d;
  3360. ty *= canvasMatrix.a;
  3361. if (stage.canvasDegree == 90) {
  3362. tx = canvasLeft - tx;
  3363. ty += canvasTop;
  3364. }
  3365. else {
  3366. tx += canvasLeft;
  3367. ty = canvasTop - ty;
  3368. }
  3369. }
  3370. else {
  3371. tx = x + globalTransform.x;
  3372. ty = y + globalTransform.y;
  3373. tx *= canvasMatrix.a;
  3374. ty *= canvasMatrix.d;
  3375. tx += canvasLeft;
  3376. ty += canvasTop;
  3377. }
  3378. ty += stage['_safariOffsetY'];
  3379. var domScaleX, domScaleY;
  3380. if (perpendicular) {
  3381. domScaleX = canvasMatrix.d * globalTransform.height;
  3382. domScaleY = canvasMatrix.a * globalTransform.width;
  3383. }
  3384. else {
  3385. domScaleX = canvasMatrix.a * globalTransform.width;
  3386. domScaleY = canvasMatrix.d * globalTransform.height;
  3387. }
  3388. return { x: tx, y: ty, scaleX: domScaleX, scaleY: domScaleY };
  3389. }
  3390. static fitDOMElementInArea(dom, coordinateSpace, x, y, width, height) {
  3391. if (!dom._fitLayaAirInitialized) {
  3392. dom._fitLayaAirInitialized = true;
  3393. dom.style.transformOrigin = dom.style.webKittransformOrigin = "left top";
  3394. dom.style.position = "absolute";
  3395. }
  3396. var transform = Utils.getTransformRelativeToWindow(coordinateSpace, x, y);
  3397. dom.style.transform = dom.style.webkitTransform = "scale(" + transform.scaleX + "," + transform.scaleY + ") rotate(" + (Utils.gStage.canvasDegree) + "deg)";
  3398. dom.style.width = width + 'px';
  3399. dom.style.height = height + 'px';
  3400. dom.style.left = transform.x + 'px';
  3401. dom.style.top = transform.y + 'px';
  3402. }
  3403. static isOkTextureList(textureList) {
  3404. if (!textureList)
  3405. return false;
  3406. var i, len = textureList.length;
  3407. var tTexture;
  3408. for (i = 0; i < len; i++) {
  3409. tTexture = textureList[i];
  3410. if (!tTexture || !tTexture._getSource())
  3411. return false;
  3412. }
  3413. return true;
  3414. }
  3415. static isOKCmdList(cmds) {
  3416. if (!cmds)
  3417. return false;
  3418. var i, len = cmds.length;
  3419. var cmd;
  3420. for (i = 0; i < len; i++) {
  3421. cmd = cmds[i];
  3422. }
  3423. return true;
  3424. }
  3425. static getQueryString(name) {
  3426. if (ILaya.Browser.onMiniGame)
  3427. return null;
  3428. if (!window.location || !window.location.search)
  3429. return null;
  3430. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  3431. var r = window.location.search.substr(1).match(reg);
  3432. if (r != null)
  3433. return unescape(r[2]);
  3434. return null;
  3435. }
  3436. }
  3437. Utils.gStage = null;
  3438. Utils._gid = 1;
  3439. Utils._pi = 180 / Math.PI;
  3440. Utils._pi2 = Math.PI / 180;
  3441. Utils._extReg = /\.(\w+)\??/g;
  3442. Utils.parseXMLFromString = function (value) {
  3443. var rst;
  3444. value = value.replace(/>\s+</g, '><');
  3445. rst = (new DOMParser()).parseFromString(value, 'text/xml');
  3446. if (rst.firstChild.textContent.indexOf("This page contains the following errors") > -1) {
  3447. throw new Error(rst.firstChild.firstChild.textContent);
  3448. }
  3449. return rst;
  3450. };
  3451. class ColorUtils {
  3452. constructor(value) {
  3453. this.arrColor = [];
  3454. if (value == null) {
  3455. this.strColor = "#00000000";
  3456. this.numColor = 0;
  3457. this.arrColor = [0, 0, 0, 0];
  3458. return;
  3459. }
  3460. var i, len;
  3461. var color;
  3462. if (typeof (value) == 'string') {
  3463. if (value.indexOf("rgba(") >= 0 || value.indexOf("rgb(") >= 0) {
  3464. var tStr = value;
  3465. var beginI, endI;
  3466. beginI = tStr.indexOf("(");
  3467. endI = tStr.indexOf(")");
  3468. tStr = tStr.substring(beginI + 1, endI);
  3469. this.arrColor = tStr.split(",");
  3470. len = this.arrColor.length;
  3471. for (i = 0; i < len; i++) {
  3472. this.arrColor[i] = parseFloat(this.arrColor[i]);
  3473. if (i < 3) {
  3474. this.arrColor[i] = Math.round(this.arrColor[i]);
  3475. }
  3476. }
  3477. if (this.arrColor.length == 4) {
  3478. color = ((this.arrColor[0] * 256 + this.arrColor[1]) * 256 + this.arrColor[2]) * 256 + Math.round(this.arrColor[3] * 255);
  3479. }
  3480. else {
  3481. color = ((this.arrColor[0] * 256 + this.arrColor[1]) * 256 + this.arrColor[2]);
  3482. }
  3483. this.strColor = value;
  3484. }
  3485. else {
  3486. this.strColor = value;
  3487. value.charAt(0) === '#' && (value = value.substr(1));
  3488. len = value.length;
  3489. if (len === 3 || len === 4) {
  3490. var temp = "";
  3491. for (i = 0; i < len; i++) {
  3492. temp += (value[i] + value[i]);
  3493. }
  3494. value = temp;
  3495. }
  3496. color = parseInt(value, 16);
  3497. }
  3498. }
  3499. else {
  3500. color = value;
  3501. this.strColor = Utils.toHexColor(color);
  3502. }
  3503. if (this.strColor.indexOf("rgba") >= 0 || this.strColor.length === 9) {
  3504. this.arrColor = [((0xFF000000 & color) >>> 24) / 255, ((0xFF0000 & color) >> 16) / 255, ((0xFF00 & color) >> 8) / 255, (0xFF & color) / 255];
  3505. this.numColor = (0xff000000 & color) >>> 24 | (color & 0xff0000) >> 8 | (color & 0x00ff00) << 8 | ((color & 0xff) << 24);
  3506. }
  3507. else {
  3508. this.arrColor = [((0xFF0000 & color) >> 16) / 255, ((0xFF00 & color) >> 8) / 255, (0xFF & color) / 255, 1];
  3509. this.numColor = 0xff000000 | (color & 0xff0000) >> 16 | (color & 0x00ff00) | (color & 0xff) << 16;
  3510. }
  3511. this.arrColor.__id = ++ColorUtils._COLODID;
  3512. }
  3513. static _initDefault() {
  3514. ColorUtils._DEFAULT = {};
  3515. for (var i in ColorUtils._COLOR_MAP)
  3516. ColorUtils._SAVE[i] = ColorUtils._DEFAULT[i] = new ColorUtils(ColorUtils._COLOR_MAP[i]);
  3517. return ColorUtils._DEFAULT;
  3518. }
  3519. static _initSaveMap() {
  3520. ColorUtils._SAVE_SIZE = 0;
  3521. ColorUtils._SAVE = {};
  3522. for (var i in ColorUtils._DEFAULT)
  3523. ColorUtils._SAVE[i] = ColorUtils._DEFAULT[i];
  3524. }
  3525. static create(value) {
  3526. var key = value + "";
  3527. var color = ColorUtils._SAVE[key];
  3528. if (color != null)
  3529. return color;
  3530. if (ColorUtils._SAVE_SIZE < 1000)
  3531. ColorUtils._initSaveMap();
  3532. return ColorUtils._SAVE[key] = new ColorUtils(value);
  3533. }
  3534. }
  3535. ColorUtils._SAVE = {};
  3536. ColorUtils._SAVE_SIZE = 0;
  3537. ColorUtils._COLOR_MAP = { "purple": "#800080", "orange": "#ffa500", "white": '#FFFFFF', "red": '#FF0000', "green": '#00FF00', "blue": '#0000FF', "black": '#000000', "yellow": '#FFFF00', 'gray': '#808080' };
  3538. ColorUtils._DEFAULT = ColorUtils._initDefault();
  3539. ColorUtils._COLODID = 1;
  3540. class ColorFilter extends Filter {
  3541. constructor(mat = null) {
  3542. super();
  3543. if (!mat)
  3544. mat = this._copyMatrix(ColorFilter.IDENTITY_MATRIX);
  3545. this._mat = new Float32Array(16);
  3546. this._alpha = new Float32Array(4);
  3547. this.setByMatrix(mat);
  3548. }
  3549. gray() {
  3550. return this.setByMatrix(ColorFilter.GRAY_MATRIX);
  3551. }
  3552. color(red = 0, green = 0, blue = 0, alpha = 1) {
  3553. return this.setByMatrix([1, 0, 0, 0, red, 0, 1, 0, 0, green, 0, 0, 1, 0, blue, 0, 0, 0, 1, alpha]);
  3554. }
  3555. setColor(color) {
  3556. var arr = ColorUtils.create(color).arrColor;
  3557. 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];
  3558. return this.setByMatrix(mt);
  3559. }
  3560. setByMatrix(matrix) {
  3561. if (this._matrix != matrix)
  3562. this._copyMatrix(matrix);
  3563. var j = 0;
  3564. var z = 0;
  3565. for (var i = 0; i < 20; i++) {
  3566. if (i % 5 != 4) {
  3567. this._mat[j++] = matrix[i];
  3568. }
  3569. else {
  3570. this._alpha[z++] = matrix[i];
  3571. }
  3572. }
  3573. return this;
  3574. }
  3575. get type() {
  3576. return Filter.COLOR;
  3577. }
  3578. adjustColor(brightness, contrast, saturation, hue) {
  3579. this.adjustHue(hue);
  3580. this.adjustContrast(contrast);
  3581. this.adjustBrightness(brightness);
  3582. this.adjustSaturation(saturation);
  3583. return this;
  3584. }
  3585. adjustBrightness(brightness) {
  3586. brightness = this._clampValue(brightness, 100);
  3587. if (brightness == 0 || isNaN(brightness))
  3588. return this;
  3589. 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]);
  3590. }
  3591. adjustContrast(contrast) {
  3592. contrast = this._clampValue(contrast, 100);
  3593. if (contrast == 0 || isNaN(contrast))
  3594. return this;
  3595. var x;
  3596. if (contrast < 0) {
  3597. x = 127 + contrast / 100 * 127;
  3598. }
  3599. else {
  3600. x = contrast % 1;
  3601. if (x == 0) {
  3602. x = ColorFilter.DELTA_INDEX[contrast];
  3603. }
  3604. else {
  3605. x = ColorFilter.DELTA_INDEX[(contrast << 0)] * (1 - x) + ColorFilter.DELTA_INDEX[(contrast << 0) + 1] * x;
  3606. }
  3607. x = x * 127 + 127;
  3608. }
  3609. var x1 = x / 127;
  3610. var x2 = (127 - x) * 0.5;
  3611. 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]);
  3612. }
  3613. adjustSaturation(saturation) {
  3614. saturation = this._clampValue(saturation, 100);
  3615. if (saturation == 0 || isNaN(saturation))
  3616. return this;
  3617. var x = 1 + ((saturation > 0) ? 3 * saturation / 100 : saturation / 100);
  3618. var dx = 1 - x;
  3619. var r = 0.3086 * dx;
  3620. var g = 0.6094 * dx;
  3621. var b = 0.0820 * dx;
  3622. 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]);
  3623. }
  3624. adjustHue(hue) {
  3625. hue = this._clampValue(hue, 180) / 180 * Math.PI;
  3626. if (hue == 0 || isNaN(hue))
  3627. return this;
  3628. var cos = Math.cos(hue);
  3629. var sin = Math.sin(hue);
  3630. var r = 0.213;
  3631. var g = 0.715;
  3632. var b = 0.072;
  3633. 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]);
  3634. }
  3635. reset() {
  3636. return this.setByMatrix(this._copyMatrix(ColorFilter.IDENTITY_MATRIX));
  3637. }
  3638. _multiplyMatrix(matrix) {
  3639. var col = [];
  3640. this._matrix = this._fixMatrix(this._matrix);
  3641. for (var i = 0; i < 5; i++) {
  3642. for (var j = 0; j < 5; j++) {
  3643. col[j] = this._matrix[j + i * 5];
  3644. }
  3645. for (j = 0; j < 5; j++) {
  3646. var val = 0;
  3647. for (var k = 0; k < 5; k++) {
  3648. val += matrix[j + k * 5] * col[k];
  3649. }
  3650. this._matrix[j + i * 5] = val;
  3651. }
  3652. }
  3653. return this.setByMatrix(this._matrix);
  3654. }
  3655. _clampValue(val, limit) {
  3656. return Math.min(limit, Math.max(-limit, val));
  3657. }
  3658. _fixMatrix(matrix = null) {
  3659. if (matrix == null)
  3660. return ColorFilter.IDENTITY_MATRIX;
  3661. if (matrix.length < ColorFilter.LENGTH)
  3662. matrix = matrix.slice(0, matrix.length).concat(ColorFilter.IDENTITY_MATRIX.slice(matrix.length, ColorFilter.LENGTH));
  3663. else if (matrix.length > ColorFilter.LENGTH)
  3664. matrix = matrix.slice(0, ColorFilter.LENGTH);
  3665. return matrix;
  3666. }
  3667. _copyMatrix(matrix) {
  3668. var len = ColorFilter.LENGTH;
  3669. if (!this._matrix)
  3670. this._matrix = [];
  3671. for (var i = 0; i < len; i++) {
  3672. this._matrix[i] = matrix[i];
  3673. }
  3674. return this._matrix;
  3675. }
  3676. }
  3677. 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];
  3678. 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];
  3679. 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];
  3680. ColorFilter.LENGTH = 25;
  3681. class DrawTextureCmd {
  3682. constructor() {
  3683. this.colorFlt = null;
  3684. this.uv = null;
  3685. }
  3686. static create(texture, x, y, width, height, matrix, alpha, color, blendMode, uv) {
  3687. var cmd = Pool.getItemByClass("DrawTextureCmd", DrawTextureCmd);
  3688. cmd.texture = texture;
  3689. texture._addReference();
  3690. cmd.x = x;
  3691. cmd.y = y;
  3692. cmd.width = width;
  3693. cmd.height = height;
  3694. cmd.matrix = matrix;
  3695. cmd.alpha = alpha;
  3696. cmd.color = color;
  3697. cmd.blendMode = blendMode;
  3698. cmd.uv = uv == undefined ? null : uv;
  3699. if (color) {
  3700. cmd.colorFlt = new ColorFilter();
  3701. cmd.colorFlt.setColor(color);
  3702. }
  3703. return cmd;
  3704. }
  3705. recover() {
  3706. this.texture && this.texture._removeReference();
  3707. this.texture = null;
  3708. this.matrix = null;
  3709. Pool.recover("DrawTextureCmd", this);
  3710. }
  3711. run(context, gx, gy) {
  3712. 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);
  3713. }
  3714. get cmdID() {
  3715. return DrawTextureCmd.ID;
  3716. }
  3717. }
  3718. DrawTextureCmd.ID = "DrawTexture";
  3719. class FillTextureCmd {
  3720. static create(texture, x, y, width, height, type, offset, other) {
  3721. var cmd = Pool.getItemByClass("FillTextureCmd", FillTextureCmd);
  3722. cmd.texture = texture;
  3723. cmd.x = x;
  3724. cmd.y = y;
  3725. cmd.width = width;
  3726. cmd.height = height;
  3727. cmd.type = type;
  3728. cmd.offset = offset;
  3729. cmd.other = other;
  3730. return cmd;
  3731. }
  3732. recover() {
  3733. this.texture = null;
  3734. this.offset = null;
  3735. this.other = null;
  3736. Pool.recover("FillTextureCmd", this);
  3737. }
  3738. run(context, gx, gy) {
  3739. context.fillTexture(this.texture, this.x + gx, this.y + gy, this.width, this.height, this.type, this.offset, this.other);
  3740. }
  3741. get cmdID() {
  3742. return FillTextureCmd.ID;
  3743. }
  3744. }
  3745. FillTextureCmd.ID = "FillTexture";
  3746. class RestoreCmd {
  3747. static create() {
  3748. var cmd = Pool.getItemByClass("RestoreCmd", RestoreCmd);
  3749. return cmd;
  3750. }
  3751. recover() {
  3752. Pool.recover("RestoreCmd", this);
  3753. }
  3754. run(context, gx, gy) {
  3755. context.restore();
  3756. }
  3757. get cmdID() {
  3758. return RestoreCmd.ID;
  3759. }
  3760. }
  3761. RestoreCmd.ID = "Restore";
  3762. class RotateCmd {
  3763. static create(angle, pivotX, pivotY) {
  3764. var cmd = Pool.getItemByClass("RotateCmd", RotateCmd);
  3765. cmd.angle = angle;
  3766. cmd.pivotX = pivotX;
  3767. cmd.pivotY = pivotY;
  3768. return cmd;
  3769. }
  3770. recover() {
  3771. Pool.recover("RotateCmd", this);
  3772. }
  3773. run(context, gx, gy) {
  3774. context._rotate(this.angle, this.pivotX + gx, this.pivotY + gy);
  3775. }
  3776. get cmdID() {
  3777. return RotateCmd.ID;
  3778. }
  3779. }
  3780. RotateCmd.ID = "Rotate";
  3781. class ScaleCmd {
  3782. static create(scaleX, scaleY, pivotX, pivotY) {
  3783. var cmd = Pool.getItemByClass("ScaleCmd", ScaleCmd);
  3784. cmd.scaleX = scaleX;
  3785. cmd.scaleY = scaleY;
  3786. cmd.pivotX = pivotX;
  3787. cmd.pivotY = pivotY;
  3788. return cmd;
  3789. }
  3790. recover() {
  3791. Pool.recover("ScaleCmd", this);
  3792. }
  3793. run(context, gx, gy) {
  3794. context._scale(this.scaleX, this.scaleY, this.pivotX + gx, this.pivotY + gy);
  3795. }
  3796. get cmdID() {
  3797. return ScaleCmd.ID;
  3798. }
  3799. }
  3800. ScaleCmd.ID = "Scale";
  3801. class TransformCmd {
  3802. static create(matrix, pivotX, pivotY) {
  3803. var cmd = Pool.getItemByClass("TransformCmd", TransformCmd);
  3804. cmd.matrix = matrix;
  3805. cmd.pivotX = pivotX;
  3806. cmd.pivotY = pivotY;
  3807. return cmd;
  3808. }
  3809. recover() {
  3810. this.matrix = null;
  3811. Pool.recover("TransformCmd", this);
  3812. }
  3813. run(context, gx, gy) {
  3814. context._transform(this.matrix, this.pivotX + gx, this.pivotY + gy);
  3815. }
  3816. get cmdID() {
  3817. return TransformCmd.ID;
  3818. }
  3819. }
  3820. TransformCmd.ID = "Transform";
  3821. class TranslateCmd {
  3822. static create(tx, ty) {
  3823. var cmd = Pool.getItemByClass("TranslateCmd", TranslateCmd);
  3824. cmd.tx = tx;
  3825. cmd.ty = ty;
  3826. return cmd;
  3827. }
  3828. recover() {
  3829. Pool.recover("TranslateCmd", this);
  3830. }
  3831. run(context, gx, gy) {
  3832. context.translate(this.tx, this.ty);
  3833. }
  3834. get cmdID() {
  3835. return TranslateCmd.ID;
  3836. }
  3837. }
  3838. TranslateCmd.ID = "Translate";
  3839. class Bezier {
  3840. constructor() {
  3841. this._controlPoints = [new Point(), new Point(), new Point()];
  3842. this._calFun = this.getPoint2;
  3843. }
  3844. _switchPoint(x, y) {
  3845. var tPoint = this._controlPoints.shift();
  3846. tPoint.setTo(x, y);
  3847. this._controlPoints.push(tPoint);
  3848. }
  3849. getPoint2(t, rst) {
  3850. var p1 = this._controlPoints[0];
  3851. var p2 = this._controlPoints[1];
  3852. var p3 = this._controlPoints[2];
  3853. var lineX = Math.pow((1 - t), 2) * p1.x + 2 * t * (1 - t) * p2.x + Math.pow(t, 2) * p3.x;
  3854. var lineY = Math.pow((1 - t), 2) * p1.y + 2 * t * (1 - t) * p2.y + Math.pow(t, 2) * p3.y;
  3855. rst.push(lineX, lineY);
  3856. }
  3857. getPoint3(t, rst) {
  3858. var p1 = this._controlPoints[0];
  3859. var p2 = this._controlPoints[1];
  3860. var p3 = this._controlPoints[2];
  3861. var p4 = this._controlPoints[3];
  3862. 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);
  3863. 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);
  3864. rst.push(lineX, lineY);
  3865. }
  3866. insertPoints(count, rst) {
  3867. var i;
  3868. count = count > 0 ? count : 5;
  3869. var dLen;
  3870. dLen = 1 / count;
  3871. for (i = 0; i <= 1; i += dLen) {
  3872. this._calFun(i, rst);
  3873. }
  3874. }
  3875. getBezierPoints(pList, inSertCount = 5, count = 2) {
  3876. var i, len;
  3877. len = pList.length;
  3878. if (len < (count + 1) * 2)
  3879. return [];
  3880. var rst = [];
  3881. switch (count) {
  3882. case 2:
  3883. this._calFun = this.getPoint2;
  3884. break;
  3885. case 3:
  3886. this._calFun = this.getPoint3;
  3887. break;
  3888. default:
  3889. return [];
  3890. }
  3891. while (this._controlPoints.length <= count) {
  3892. this._controlPoints.push(Point.create());
  3893. }
  3894. for (i = 0; i < count * 2; i += 2) {
  3895. this._switchPoint(pList[i], pList[i + 1]);
  3896. }
  3897. for (i = count * 2; i < len; i += 2) {
  3898. this._switchPoint(pList[i], pList[i + 1]);
  3899. if ((i / 2) % count == 0)
  3900. this.insertPoints(inSertCount, rst);
  3901. }
  3902. return rst;
  3903. }
  3904. }
  3905. Bezier.I = new Bezier();
  3906. class GrahamScan {
  3907. static multiply(p1, p2, p0) {
  3908. return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
  3909. }
  3910. static dis(p1, p2) {
  3911. return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
  3912. }
  3913. static _getPoints(count, tempUse = false, rst = null) {
  3914. if (!GrahamScan._mPointList)
  3915. GrahamScan._mPointList = [];
  3916. while (GrahamScan._mPointList.length < count)
  3917. GrahamScan._mPointList.push(new Point());
  3918. if (!rst)
  3919. rst = [];
  3920. rst.length = 0;
  3921. if (tempUse) {
  3922. GrahamScan.getFrom(rst, GrahamScan._mPointList, count);
  3923. }
  3924. else {
  3925. GrahamScan.getFromR(rst, GrahamScan._mPointList, count);
  3926. }
  3927. return rst;
  3928. }
  3929. static getFrom(rst, src, count) {
  3930. var i;
  3931. for (i = 0; i < count; i++) {
  3932. rst.push(src[i]);
  3933. }
  3934. return rst;
  3935. }
  3936. static getFromR(rst, src, count) {
  3937. var i;
  3938. for (i = 0; i < count; i++) {
  3939. rst.push(src.pop());
  3940. }
  3941. return rst;
  3942. }
  3943. static pListToPointList(pList, tempUse = false) {
  3944. var i, len = pList.length / 2, rst = GrahamScan._getPoints(len, tempUse, GrahamScan._tempPointList);
  3945. for (i = 0; i < len; i++) {
  3946. rst[i].setTo(pList[i + i], pList[i + i + 1]);
  3947. }
  3948. return rst;
  3949. }
  3950. static pointListToPlist(pointList) {
  3951. var i, len = pointList.length, rst = GrahamScan._temPList, tPoint;
  3952. rst.length = 0;
  3953. for (i = 0; i < len; i++) {
  3954. tPoint = pointList[i];
  3955. rst.push(tPoint.x, tPoint.y);
  3956. }
  3957. return rst;
  3958. }
  3959. static scanPList(pList) {
  3960. return Utils.copyArray(pList, GrahamScan.pointListToPlist(GrahamScan.scan(GrahamScan.pListToPointList(pList, true))));
  3961. }
  3962. static scan(PointSet) {
  3963. var i, j, k = 0, tmp, n = PointSet.length, ch;
  3964. var _tmpDic = {};
  3965. var key;
  3966. ch = GrahamScan._temArr;
  3967. ch.length = 0;
  3968. n = PointSet.length;
  3969. for (i = n - 1; i >= 0; i--) {
  3970. tmp = PointSet[i];
  3971. key = tmp.x + "_" + tmp.y;
  3972. if (!(key in _tmpDic)) {
  3973. _tmpDic[key] = true;
  3974. ch.push(tmp);
  3975. }
  3976. }
  3977. n = ch.length;
  3978. Utils.copyArray(PointSet, ch);
  3979. for (i = 1; i < n; i++)
  3980. if ((PointSet[i].y < PointSet[k].y) || ((PointSet[i].y == PointSet[k].y) && (PointSet[i].x < PointSet[k].x)))
  3981. k = i;
  3982. tmp = PointSet[0];
  3983. PointSet[0] = PointSet[k];
  3984. PointSet[k] = tmp;
  3985. for (i = 1; i < n - 1; i++) {
  3986. k = i;
  3987. for (j = i + 1; j < n; j++)
  3988. 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]))))
  3989. k = j;
  3990. tmp = PointSet[i];
  3991. PointSet[i] = PointSet[k];
  3992. PointSet[k] = tmp;
  3993. }
  3994. ch = GrahamScan._temArr;
  3995. ch.length = 0;
  3996. if (PointSet.length < 3) {
  3997. return Utils.copyArray(ch, PointSet);
  3998. }
  3999. ch.push(PointSet[0], PointSet[1], PointSet[2]);
  4000. for (i = 3; i < n; i++) {
  4001. while (ch.length >= 2 && GrahamScan.multiply(PointSet[i], ch[ch.length - 1], ch[ch.length - 2]) >= 0)
  4002. ch.pop();
  4003. PointSet[i] && ch.push(PointSet[i]);
  4004. }
  4005. return ch;
  4006. }
  4007. }
  4008. GrahamScan._tempPointList = [];
  4009. GrahamScan._temPList = [];
  4010. GrahamScan._temArr = [];
  4011. class DrawStyle {
  4012. constructor(value) {
  4013. this.setValue(value);
  4014. }
  4015. static create(value) {
  4016. if (value) {
  4017. var color = (value instanceof ColorUtils) ? value : ColorUtils.create(value);
  4018. return color._drawStyle || (color._drawStyle = new DrawStyle(value));
  4019. }
  4020. return DrawStyle.DEFAULT;
  4021. }
  4022. setValue(value) {
  4023. if (value) {
  4024. this._color = (value instanceof ColorUtils) ? value : ColorUtils.create(value);
  4025. }
  4026. else
  4027. this._color = ColorUtils.create("#000000");
  4028. }
  4029. reset() {
  4030. this._color = ColorUtils.create("#000000");
  4031. }
  4032. toInt() {
  4033. return this._color.numColor;
  4034. }
  4035. equal(value) {
  4036. if (typeof (value) == 'string')
  4037. return this._color.strColor === value;
  4038. if (value instanceof ColorUtils)
  4039. return this._color.numColor === value.numColor;
  4040. return false;
  4041. }
  4042. toColorStr() {
  4043. return this._color.strColor;
  4044. }
  4045. }
  4046. DrawStyle.DEFAULT = new DrawStyle("#000000");
  4047. class Path {
  4048. constructor() {
  4049. this._lastOriX = 0;
  4050. this._lastOriY = 0;
  4051. this.paths = [];
  4052. this._curPath = null;
  4053. }
  4054. beginPath(convex) {
  4055. this.paths.length = 1;
  4056. this._curPath = this.paths[0] = new renderPath();
  4057. this._curPath.convex = convex;
  4058. }
  4059. closePath() {
  4060. this._curPath.loop = true;
  4061. }
  4062. newPath() {
  4063. this._curPath = new renderPath();
  4064. this.paths.push(this._curPath);
  4065. }
  4066. addPoint(pointX, pointY) {
  4067. this._curPath.path.push(pointX, pointY);
  4068. }
  4069. push(points, convex) {
  4070. if (!this._curPath) {
  4071. this._curPath = new renderPath();
  4072. this.paths.push(this._curPath);
  4073. }
  4074. else if (this._curPath.path.length > 0) {
  4075. this._curPath = new renderPath();
  4076. this.paths.push(this._curPath);
  4077. }
  4078. var rp = this._curPath;
  4079. rp.path = points.slice();
  4080. rp.convex = convex;
  4081. }
  4082. reset() {
  4083. this.paths.length = 0;
  4084. }
  4085. }
  4086. class renderPath {
  4087. constructor() {
  4088. this.path = [];
  4089. this.loop = false;
  4090. this.convex = false;
  4091. }
  4092. }
  4093. class SubmitBase {
  4094. constructor(renderType = SubmitBase.TYPE_2D) {
  4095. this.clipInfoID = -1;
  4096. this._mesh = null;
  4097. this._blendFn = null;
  4098. this._id = 0;
  4099. this._renderType = 0;
  4100. this._parent = null;
  4101. this._key = new SubmitKey();
  4102. this._startIdx = 0;
  4103. this._numEle = 0;
  4104. this._ref = 1;
  4105. this.shaderValue = null;
  4106. this._renderType = renderType;
  4107. this._id = ++SubmitBase.ID;
  4108. }
  4109. static __init__() {
  4110. var s = SubmitBase.RENDERBASE = new SubmitBase(-1);
  4111. s.shaderValue = new Value2D(0, 0);
  4112. s.shaderValue.ALPHA = 1;
  4113. s._ref = 0xFFFFFFFF;
  4114. }
  4115. getID() {
  4116. return this._id;
  4117. }
  4118. getRenderType() {
  4119. return this._renderType;
  4120. }
  4121. toString() {
  4122. return "ibindex:" + this._startIdx + " num:" + this._numEle + " key=" + this._key;
  4123. }
  4124. renderSubmit() { return 1; }
  4125. releaseRender() { }
  4126. }
  4127. SubmitBase.TYPE_2D = 10000;
  4128. SubmitBase.TYPE_CANVAS = 10003;
  4129. SubmitBase.TYPE_CMDSETRT = 10004;
  4130. SubmitBase.TYPE_CUSTOM = 10005;
  4131. SubmitBase.TYPE_BLURRT = 10006;
  4132. SubmitBase.TYPE_CMDDESTORYPRERT = 10007;
  4133. SubmitBase.TYPE_DISABLESTENCIL = 10008;
  4134. SubmitBase.TYPE_OTHERIBVB = 10009;
  4135. SubmitBase.TYPE_PRIMITIVE = 10010;
  4136. SubmitBase.TYPE_RT = 10011;
  4137. SubmitBase.TYPE_BLUR_RT = 10012;
  4138. SubmitBase.TYPE_TARGET = 10013;
  4139. SubmitBase.TYPE_CHANGE_VALUE = 10014;
  4140. SubmitBase.TYPE_SHAPE = 10015;
  4141. SubmitBase.TYPE_TEXTURE = 10016;
  4142. SubmitBase.TYPE_FILLTEXTURE = 10017;
  4143. SubmitBase.KEY_ONCE = -1;
  4144. SubmitBase.KEY_FILLRECT = 1;
  4145. SubmitBase.KEY_DRAWTEXTURE = 2;
  4146. SubmitBase.KEY_VG = 3;
  4147. SubmitBase.KEY_TRIANGLES = 4;
  4148. SubmitBase.ID = 1;
  4149. SubmitBase.preRender = null;
  4150. class SaveBase {
  4151. constructor() {
  4152. }
  4153. static _createArray() {
  4154. var value = [];
  4155. value._length = 0;
  4156. return value;
  4157. }
  4158. static _init() {
  4159. var namemap = SaveBase._namemap = {};
  4160. namemap[SaveBase.TYPE_ALPHA] = "ALPHA";
  4161. namemap[SaveBase.TYPE_FILESTYLE] = "fillStyle";
  4162. namemap[SaveBase.TYPE_FONT] = "font";
  4163. namemap[SaveBase.TYPE_LINEWIDTH] = "lineWidth";
  4164. namemap[SaveBase.TYPE_STROKESTYLE] = "strokeStyle";
  4165. namemap[SaveBase.TYPE_ENABLEMERGE] = "_mergeID";
  4166. namemap[SaveBase.TYPE_MARK] = namemap[SaveBase.TYPE_TRANSFORM] = namemap[SaveBase.TYPE_TRANSLATE] = [];
  4167. namemap[SaveBase.TYPE_TEXTBASELINE] = "textBaseline";
  4168. namemap[SaveBase.TYPE_TEXTALIGN] = "textAlign";
  4169. namemap[SaveBase.TYPE_GLOBALCOMPOSITEOPERATION] = "_nBlendType";
  4170. namemap[SaveBase.TYPE_SHADER] = "shader";
  4171. namemap[SaveBase.TYPE_FILTERS] = "filters";
  4172. namemap[SaveBase.TYPE_COLORFILTER] = '_colorFiler';
  4173. return namemap;
  4174. }
  4175. isSaveMark() { return false; }
  4176. restore(context) {
  4177. this._dataObj[this._valueName] = this._value;
  4178. SaveBase.POOL[SaveBase.POOL._length++] = this;
  4179. this._newSubmit && (context._curSubmit = SubmitBase.RENDERBASE);
  4180. }
  4181. static save(context, type, dataObj, newSubmit) {
  4182. if ((context._saveMark._saveuse & type) !== type) {
  4183. context._saveMark._saveuse |= type;
  4184. var cache = SaveBase.POOL;
  4185. var o = cache._length > 0 ? cache[--cache._length] : (new SaveBase());
  4186. o._value = dataObj[o._valueName = SaveBase._namemap[type]];
  4187. o._dataObj = dataObj;
  4188. o._newSubmit = newSubmit;
  4189. var _save = context._save;
  4190. _save[_save._length++] = o;
  4191. }
  4192. }
  4193. }
  4194. SaveBase.TYPE_ALPHA = 0x1;
  4195. SaveBase.TYPE_FILESTYLE = 0x2;
  4196. SaveBase.TYPE_FONT = 0x8;
  4197. SaveBase.TYPE_LINEWIDTH = 0x100;
  4198. SaveBase.TYPE_STROKESTYLE = 0x200;
  4199. SaveBase.TYPE_MARK = 0x400;
  4200. SaveBase.TYPE_TRANSFORM = 0x800;
  4201. SaveBase.TYPE_TRANSLATE = 0x1000;
  4202. SaveBase.TYPE_ENABLEMERGE = 0x2000;
  4203. SaveBase.TYPE_TEXTBASELINE = 0x4000;
  4204. SaveBase.TYPE_TEXTALIGN = 0x8000;
  4205. SaveBase.TYPE_GLOBALCOMPOSITEOPERATION = 0x10000;
  4206. SaveBase.TYPE_CLIPRECT = 0x20000;
  4207. SaveBase.TYPE_CLIPRECT_STENCIL = 0x40000;
  4208. SaveBase.TYPE_IBVB = 0x80000;
  4209. SaveBase.TYPE_SHADER = 0x100000;
  4210. SaveBase.TYPE_FILTERS = 0x200000;
  4211. SaveBase.TYPE_FILTERS_TYPE = 0x400000;
  4212. SaveBase.TYPE_COLORFILTER = 0x800000;
  4213. SaveBase.POOL = SaveBase._createArray();
  4214. SaveBase._namemap = SaveBase._init();
  4215. class SaveClipRect {
  4216. constructor() {
  4217. this._globalClipMatrix = new Matrix();
  4218. this._clipInfoID = -1;
  4219. this._clipRect = new Rectangle();
  4220. this.incache = false;
  4221. }
  4222. isSaveMark() { return false; }
  4223. restore(context) {
  4224. this._globalClipMatrix.copyTo(context._globalClipMatrix);
  4225. this._clipRect.clone(context._clipRect);
  4226. context._clipInfoID = this._clipInfoID;
  4227. SaveClipRect.POOL[SaveClipRect.POOL._length++] = this;
  4228. context._clipInCache = this.incache;
  4229. }
  4230. static save(context) {
  4231. if ((context._saveMark._saveuse & SaveBase.TYPE_CLIPRECT) == SaveBase.TYPE_CLIPRECT)
  4232. return;
  4233. context._saveMark._saveuse |= SaveBase.TYPE_CLIPRECT;
  4234. var cache = SaveClipRect.POOL;
  4235. var o = cache._length > 0 ? cache[--cache._length] : (new SaveClipRect());
  4236. context._globalClipMatrix.copyTo(o._globalClipMatrix);
  4237. context._clipRect.clone(o._clipRect);
  4238. o._clipInfoID = context._clipInfoID;
  4239. o.incache = context._clipInCache;
  4240. var _save = context._save;
  4241. _save[_save._length++] = o;
  4242. }
  4243. }
  4244. SaveClipRect.POOL = SaveBase._createArray();
  4245. class SaveMark {
  4246. constructor() {
  4247. this._saveuse = 0;
  4248. }
  4249. isSaveMark() {
  4250. return true;
  4251. }
  4252. restore(context) {
  4253. context._saveMark = this._preSaveMark;
  4254. SaveMark.POOL[SaveMark.POOL._length++] = this;
  4255. }
  4256. static Create(context) {
  4257. var no = SaveMark.POOL;
  4258. var o = no._length > 0 ? no[--no._length] : (new SaveMark());
  4259. o._saveuse = 0;
  4260. o._preSaveMark = context._saveMark;
  4261. context._saveMark = o;
  4262. return o;
  4263. }
  4264. }
  4265. SaveMark.POOL = SaveBase._createArray();
  4266. class SaveTransform {
  4267. constructor() {
  4268. this._matrix = new Matrix();
  4269. }
  4270. isSaveMark() { return false; }
  4271. restore(context) {
  4272. context._curMat = this._savematrix;
  4273. SaveTransform.POOL[SaveTransform.POOL._length++] = this;
  4274. }
  4275. static save(context) {
  4276. var _saveMark = context._saveMark;
  4277. if ((_saveMark._saveuse & SaveBase.TYPE_TRANSFORM) === SaveBase.TYPE_TRANSFORM)
  4278. return;
  4279. _saveMark._saveuse |= SaveBase.TYPE_TRANSFORM;
  4280. var no = SaveTransform.POOL;
  4281. var o = no._length > 0 ? no[--no._length] : (new SaveTransform());
  4282. o._savematrix = context._curMat;
  4283. context._curMat = context._curMat.copyTo(o._matrix);
  4284. var _save = context._save;
  4285. _save[_save._length++] = o;
  4286. }
  4287. }
  4288. SaveTransform.POOL = SaveBase._createArray();
  4289. class SaveTranslate {
  4290. constructor() {
  4291. this._mat = new Matrix();
  4292. }
  4293. isSaveMark() { return false; }
  4294. restore(context) {
  4295. this._mat.copyTo(context._curMat);
  4296. SaveTranslate.POOL[SaveTranslate.POOL._length++] = this;
  4297. }
  4298. static save(context) {
  4299. var no = SaveTranslate.POOL;
  4300. var o = no._length > 0 ? no[--no._length] : (new SaveTranslate());
  4301. context._curMat.copyTo(o._mat);
  4302. var _save = context._save;
  4303. _save[_save._length++] = o;
  4304. }
  4305. }
  4306. SaveTranslate.POOL = SaveBase._createArray();
  4307. class BufferStateBase {
  4308. constructor() {
  4309. this._nativeVertexArrayObject = LayaGL.layaGPUInstance.createVertexArray();
  4310. }
  4311. bind() {
  4312. if (BufferStateBase._curBindedBufferState !== this) {
  4313. LayaGL.layaGPUInstance.bindVertexArray(this._nativeVertexArrayObject);
  4314. BufferStateBase._curBindedBufferState = this;
  4315. }
  4316. }
  4317. unBind() {
  4318. if (BufferStateBase._curBindedBufferState === this) {
  4319. LayaGL.layaGPUInstance.bindVertexArray(null);
  4320. BufferStateBase._curBindedBufferState = null;
  4321. }
  4322. else {
  4323. throw "BufferState: must call bind() function first.";
  4324. }
  4325. }
  4326. destroy() {
  4327. LayaGL.layaGPUInstance.deleteVertexArray(this._nativeVertexArrayObject);
  4328. }
  4329. bindForNative() {
  4330. LayaGL.instance.bindVertexArray(this._nativeVertexArrayObject);
  4331. BufferStateBase._curBindedBufferState = this;
  4332. }
  4333. unBindForNative() {
  4334. LayaGL.instance.bindVertexArray(null);
  4335. BufferStateBase._curBindedBufferState = null;
  4336. }
  4337. }
  4338. class BufferState2D extends BufferStateBase {
  4339. constructor() {
  4340. super();
  4341. }
  4342. }
  4343. class Buffer {
  4344. constructor() {
  4345. this._byteLength = 0;
  4346. this._glBuffer = LayaGL.instance.createBuffer();
  4347. }
  4348. get bufferUsage() {
  4349. return this._bufferUsage;
  4350. }
  4351. _bindForVAO() {
  4352. }
  4353. bind() {
  4354. return false;
  4355. }
  4356. destroy() {
  4357. if (this._glBuffer) {
  4358. LayaGL.instance.deleteBuffer(this._glBuffer);
  4359. this._glBuffer = null;
  4360. }
  4361. }
  4362. }
  4363. class RenderInfo {
  4364. }
  4365. RenderInfo.loopStTm = 0;
  4366. RenderInfo.loopCount = 0;
  4367. class Buffer2D extends Buffer {
  4368. constructor() {
  4369. super();
  4370. this._maxsize = 0;
  4371. this._upload = true;
  4372. this._uploadSize = 0;
  4373. this._bufferSize = 0;
  4374. this._u8Array = null;
  4375. }
  4376. static __int__(gl) {
  4377. }
  4378. get bufferLength() {
  4379. return this._buffer.byteLength;
  4380. }
  4381. set byteLength(value) {
  4382. this.setByteLength(value);
  4383. }
  4384. setByteLength(value) {
  4385. if (this._byteLength !== value) {
  4386. value <= this._bufferSize || (this._resizeBuffer(value * 2 + 256, true));
  4387. this._byteLength = value;
  4388. }
  4389. }
  4390. needSize(sz) {
  4391. var old = this._byteLength;
  4392. if (sz) {
  4393. var needsz = this._byteLength + sz;
  4394. needsz <= this._bufferSize || (this._resizeBuffer(needsz << 1, true));
  4395. this._byteLength = needsz;
  4396. }
  4397. return old;
  4398. }
  4399. _bufferData() {
  4400. this._maxsize = Math.max(this._maxsize, this._byteLength);
  4401. if (RenderInfo.loopCount % 30 == 0) {
  4402. if (this._buffer.byteLength > (this._maxsize + 64)) {
  4403. this._buffer = this._buffer.slice(0, this._maxsize + 64);
  4404. this._bufferSize = this._buffer.byteLength;
  4405. this._checkArrayUse();
  4406. }
  4407. this._maxsize = this._byteLength;
  4408. }
  4409. if (this._uploadSize < this._buffer.byteLength) {
  4410. this._uploadSize = this._buffer.byteLength;
  4411. LayaGL.instance.bufferData(this._bufferType, this._uploadSize, this._bufferUsage);
  4412. }
  4413. LayaGL.instance.bufferSubData(this._bufferType, 0, new Uint8Array(this._buffer, 0, this._byteLength));
  4414. }
  4415. _bufferSubData(offset = 0, dataStart = 0, dataLength = 0) {
  4416. this._maxsize = Math.max(this._maxsize, this._byteLength);
  4417. if (RenderInfo.loopCount % 30 == 0) {
  4418. if (this._buffer.byteLength > (this._maxsize + 64)) {
  4419. this._buffer = this._buffer.slice(0, this._maxsize + 64);
  4420. this._bufferSize = this._buffer.byteLength;
  4421. this._checkArrayUse();
  4422. }
  4423. this._maxsize = this._byteLength;
  4424. }
  4425. if (this._uploadSize < this._buffer.byteLength) {
  4426. this._uploadSize = this._buffer.byteLength;
  4427. LayaGL.instance.bufferData(this._bufferType, this._uploadSize, this._bufferUsage);
  4428. }
  4429. if (dataStart || dataLength) {
  4430. var subBuffer = this._buffer.slice(dataStart, dataLength);
  4431. LayaGL.instance.bufferSubData(this._bufferType, offset, subBuffer);
  4432. }
  4433. else {
  4434. LayaGL.instance.bufferSubData(this._bufferType, offset, this._buffer);
  4435. }
  4436. }
  4437. _checkArrayUse() {
  4438. }
  4439. _bind_uploadForVAO() {
  4440. if (!this._upload)
  4441. return false;
  4442. this._upload = false;
  4443. this._bindForVAO();
  4444. this._bufferData();
  4445. return true;
  4446. }
  4447. _bind_upload() {
  4448. if (!this._upload)
  4449. return false;
  4450. this._upload = false;
  4451. this.bind();
  4452. this._bufferData();
  4453. return true;
  4454. }
  4455. _bind_subUpload(offset = 0, dataStart = 0, dataLength = 0) {
  4456. if (!this._upload)
  4457. return false;
  4458. this._upload = false;
  4459. this.bind();
  4460. this._bufferSubData(offset, dataStart, dataLength);
  4461. return true;
  4462. }
  4463. _resizeBuffer(nsz, copy) {
  4464. var buff = this._buffer;
  4465. if (nsz <= buff.byteLength)
  4466. return this;
  4467. var u8buf = this._u8Array;
  4468. if (copy && buff && buff.byteLength > 0) {
  4469. var newbuffer = new ArrayBuffer(nsz);
  4470. var oldU8Arr = (u8buf && u8buf.buffer == buff) ? u8buf : new Uint8Array(buff);
  4471. u8buf = this._u8Array = new Uint8Array(newbuffer);
  4472. u8buf.set(oldU8Arr, 0);
  4473. buff = this._buffer = newbuffer;
  4474. }
  4475. else {
  4476. buff = this._buffer = new ArrayBuffer(nsz);
  4477. this._u8Array = null;
  4478. }
  4479. this._checkArrayUse();
  4480. this._upload = true;
  4481. this._bufferSize = buff.byteLength;
  4482. return this;
  4483. }
  4484. append(data) {
  4485. this._upload = true;
  4486. var byteLen, n;
  4487. byteLen = data.byteLength;
  4488. if (data instanceof Uint8Array) {
  4489. this._resizeBuffer(this._byteLength + byteLen, true);
  4490. n = new Uint8Array(this._buffer, this._byteLength);
  4491. }
  4492. else if (data instanceof Uint16Array) {
  4493. this._resizeBuffer(this._byteLength + byteLen, true);
  4494. n = new Uint16Array(this._buffer, this._byteLength);
  4495. }
  4496. else if (data instanceof Float32Array) {
  4497. this._resizeBuffer(this._byteLength + byteLen, true);
  4498. n = new Float32Array(this._buffer, this._byteLength);
  4499. }
  4500. n.set(data, 0);
  4501. this._byteLength += byteLen;
  4502. this._checkArrayUse();
  4503. }
  4504. appendU16Array(data, len) {
  4505. this._resizeBuffer(this._byteLength + len * 2, true);
  4506. var u = new Uint16Array(this._buffer, this._byteLength, len);
  4507. if (len == 6) {
  4508. u[0] = data[0];
  4509. u[1] = data[1];
  4510. u[2] = data[2];
  4511. u[3] = data[3];
  4512. u[4] = data[4];
  4513. u[5] = data[5];
  4514. }
  4515. else if (len >= 100) {
  4516. u.set(new Uint16Array(data.buffer, 0, len));
  4517. }
  4518. else {
  4519. for (var i = 0; i < len; i++) {
  4520. u[i] = data[i];
  4521. }
  4522. }
  4523. this._byteLength += len * 2;
  4524. this._checkArrayUse();
  4525. }
  4526. appendEx(data, type) {
  4527. this._upload = true;
  4528. var byteLen, n;
  4529. byteLen = data.byteLength;
  4530. this._resizeBuffer(this._byteLength + byteLen, true);
  4531. n = new type(this._buffer, this._byteLength);
  4532. n.set(data, 0);
  4533. this._byteLength += byteLen;
  4534. this._checkArrayUse();
  4535. }
  4536. appendEx2(data, type, dataLen, perDataLen = 1) {
  4537. this._upload = true;
  4538. var byteLen, n;
  4539. byteLen = dataLen * perDataLen;
  4540. this._resizeBuffer(this._byteLength + byteLen, true);
  4541. n = new type(this._buffer, this._byteLength);
  4542. var i;
  4543. for (i = 0; i < dataLen; i++) {
  4544. n[i] = data[i];
  4545. }
  4546. this._byteLength += byteLen;
  4547. this._checkArrayUse();
  4548. }
  4549. getBuffer() {
  4550. return this._buffer;
  4551. }
  4552. setNeedUpload() {
  4553. this._upload = true;
  4554. }
  4555. getNeedUpload() {
  4556. return this._upload;
  4557. }
  4558. upload() {
  4559. var gl = LayaGL.instance;
  4560. var scuess = this._bind_upload();
  4561. gl.bindBuffer(this._bufferType, null);
  4562. if (this._bufferType == gl.ARRAY_BUFFER)
  4563. Buffer._bindedVertexBuffer = null;
  4564. if (this._bufferType == gl.ELEMENT_ARRAY_BUFFER)
  4565. Buffer._bindedIndexBuffer = null;
  4566. BaseShader.activeShader = null;
  4567. return scuess;
  4568. }
  4569. subUpload(offset = 0, dataStart = 0, dataLength = 0) {
  4570. var gl = LayaGL.instance;
  4571. var scuess = this._bind_subUpload();
  4572. gl.bindBuffer(this._bufferType, null);
  4573. if (this._bufferType == gl.ARRAY_BUFFER)
  4574. Buffer._bindedVertexBuffer = null;
  4575. if (this._bufferType == gl.ELEMENT_ARRAY_BUFFER)
  4576. Buffer._bindedIndexBuffer = null;
  4577. BaseShader.activeShader = null;
  4578. return scuess;
  4579. }
  4580. _disposeResource() {
  4581. this._upload = true;
  4582. this._uploadSize = 0;
  4583. }
  4584. clear() {
  4585. this._byteLength = 0;
  4586. this._upload = true;
  4587. }
  4588. }
  4589. Buffer2D.FLOAT32 = 4;
  4590. Buffer2D.SHORT = 2;
  4591. class IndexBuffer2D extends Buffer2D {
  4592. constructor(bufferUsage = 0x88e4) {
  4593. super();
  4594. this._bufferUsage = bufferUsage;
  4595. this._bufferType = LayaGL.instance.ELEMENT_ARRAY_BUFFER;
  4596. this._buffer = new ArrayBuffer(8);
  4597. }
  4598. _checkArrayUse() {
  4599. this._uint16Array && (this._uint16Array = new Uint16Array(this._buffer));
  4600. }
  4601. getUint16Array() {
  4602. return this._uint16Array || (this._uint16Array = new Uint16Array(this._buffer));
  4603. }
  4604. _bindForVAO() {
  4605. var gl = LayaGL.instance;
  4606. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._glBuffer);
  4607. }
  4608. bind() {
  4609. if (Buffer._bindedIndexBuffer !== this._glBuffer) {
  4610. var gl = LayaGL.instance;
  4611. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._glBuffer);
  4612. Buffer._bindedIndexBuffer = this._glBuffer;
  4613. return true;
  4614. }
  4615. return false;
  4616. }
  4617. destory() {
  4618. this._uint16Array = null;
  4619. this._buffer = null;
  4620. }
  4621. disposeResource() {
  4622. this._disposeResource();
  4623. }
  4624. }
  4625. IndexBuffer2D.create = function (bufferUsage = 0x88e4) {
  4626. return new IndexBuffer2D(bufferUsage);
  4627. };
  4628. class VertexBuffer2D extends Buffer2D {
  4629. constructor(vertexStride, bufferUsage) {
  4630. super();
  4631. this._vertexStride = vertexStride;
  4632. this._bufferUsage = bufferUsage;
  4633. this._bufferType = LayaGL.instance.ARRAY_BUFFER;
  4634. this._buffer = new ArrayBuffer(8);
  4635. this._floatArray32 = new Float32Array(this._buffer);
  4636. this._uint32Array = new Uint32Array(this._buffer);
  4637. }
  4638. get vertexStride() {
  4639. return this._vertexStride;
  4640. }
  4641. getFloat32Array() {
  4642. return this._floatArray32;
  4643. }
  4644. appendArray(data) {
  4645. var oldoff = this._byteLength >> 2;
  4646. this.setByteLength(this._byteLength + data.length * 4);
  4647. var vbdata = this.getFloat32Array();
  4648. vbdata.set(data, oldoff);
  4649. this._upload = true;
  4650. }
  4651. _checkArrayUse() {
  4652. this._floatArray32 && (this._floatArray32 = new Float32Array(this._buffer));
  4653. this._uint32Array && (this._uint32Array = new Uint32Array(this._buffer));
  4654. }
  4655. deleteBuffer() {
  4656. super._disposeResource();
  4657. }
  4658. _bindForVAO() {
  4659. var gl = LayaGL.instance;
  4660. gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
  4661. }
  4662. bind() {
  4663. if (Buffer._bindedVertexBuffer !== this._glBuffer) {
  4664. var gl = LayaGL.instance;
  4665. gl.bindBuffer(gl.ARRAY_BUFFER, this._glBuffer);
  4666. Buffer._bindedVertexBuffer = this._glBuffer;
  4667. return true;
  4668. }
  4669. return false;
  4670. }
  4671. destroy() {
  4672. super.destroy();
  4673. this._byteLength = 0;
  4674. this._upload = true;
  4675. this._buffer = null;
  4676. this._floatArray32 = null;
  4677. }
  4678. }
  4679. VertexBuffer2D.create = function (vertexStride, bufferUsage = 0x88e8) {
  4680. return new VertexBuffer2D(vertexStride, bufferUsage);
  4681. };
  4682. class Mesh2D {
  4683. constructor(stride, vballoc, iballoc) {
  4684. this._stride = 0;
  4685. this.vertNum = 0;
  4686. this.indexNum = 0;
  4687. this._applied = false;
  4688. this._quadNum = 0;
  4689. this.canReuse = false;
  4690. this._stride = stride;
  4691. this._vb = new VertexBuffer2D(stride, LayaGL.instance.DYNAMIC_DRAW);
  4692. if (vballoc) {
  4693. this._vb._resizeBuffer(vballoc, false);
  4694. }
  4695. else {
  4696. Config.webGL2D_MeshAllocMaxMem && this._vb._resizeBuffer(64 * 1024 * stride, false);
  4697. }
  4698. this._ib = new IndexBuffer2D();
  4699. if (iballoc) {
  4700. this._ib._resizeBuffer(iballoc, false);
  4701. }
  4702. }
  4703. cloneWithNewVB() {
  4704. var mesh = new Mesh2D(this._stride, 0, 0);
  4705. mesh._ib = this._ib;
  4706. mesh._quadNum = this._quadNum;
  4707. mesh._attribInfo = this._attribInfo;
  4708. return mesh;
  4709. }
  4710. cloneWithNewVBIB() {
  4711. var mesh = new Mesh2D(this._stride, 0, 0);
  4712. mesh._attribInfo = this._attribInfo;
  4713. return mesh;
  4714. }
  4715. getVBW() {
  4716. this._vb.setNeedUpload();
  4717. return this._vb;
  4718. }
  4719. getVBR() {
  4720. return this._vb;
  4721. }
  4722. getIBR() {
  4723. return this._ib;
  4724. }
  4725. getIBW() {
  4726. this._ib.setNeedUpload();
  4727. return this._ib;
  4728. }
  4729. createQuadIB(QuadNum) {
  4730. this._quadNum = QuadNum;
  4731. this._ib._resizeBuffer(QuadNum * 6 * 2, false);
  4732. this._ib.byteLength = this._ib.bufferLength;
  4733. var bd = this._ib.getUint16Array();
  4734. var idx = 0;
  4735. var curvert = 0;
  4736. for (var i = 0; i < QuadNum; i++) {
  4737. bd[idx++] = curvert;
  4738. bd[idx++] = curvert + 2;
  4739. bd[idx++] = curvert + 1;
  4740. bd[idx++] = curvert;
  4741. bd[idx++] = curvert + 3;
  4742. bd[idx++] = curvert + 2;
  4743. curvert += 4;
  4744. }
  4745. this._ib.setNeedUpload();
  4746. }
  4747. setAttributes(attribs) {
  4748. this._attribInfo = attribs;
  4749. if (this._attribInfo.length % 3 != 0) {
  4750. throw 'Mesh2D setAttributes error!';
  4751. }
  4752. }
  4753. configVAO(gl) {
  4754. if (this._applied)
  4755. return;
  4756. this._applied = true;
  4757. if (!this._vao) {
  4758. this._vao = new BufferState2D();
  4759. }
  4760. this._vao.bind();
  4761. this._vb._bindForVAO();
  4762. this._ib.setNeedUpload();
  4763. this._ib._bind_uploadForVAO();
  4764. var attribNum = this._attribInfo.length / 3;
  4765. var idx = 0;
  4766. for (var i = 0; i < attribNum; i++) {
  4767. var _size = this._attribInfo[idx + 1];
  4768. var _type = this._attribInfo[idx];
  4769. var _off = this._attribInfo[idx + 2];
  4770. gl.enableVertexAttribArray(i);
  4771. gl.vertexAttribPointer(i, _size, _type, false, this._stride, _off);
  4772. idx += 3;
  4773. }
  4774. this._vao.unBind();
  4775. }
  4776. useMesh(gl) {
  4777. this._applied || this.configVAO(gl);
  4778. this._vao.bind();
  4779. this._vb.bind();
  4780. this._ib._bind_upload() || this._ib.bind();
  4781. this._vb._bind_upload() || this._vb.bind();
  4782. }
  4783. getEleNum() {
  4784. return this._ib.getBuffer().byteLength / 2;
  4785. }
  4786. releaseMesh() { }
  4787. destroy() {
  4788. }
  4789. clearVB() {
  4790. this._vb.clear();
  4791. }
  4792. }
  4793. Mesh2D._gvaoid = 0;
  4794. class MeshQuadTexture extends Mesh2D {
  4795. constructor() {
  4796. super(MeshQuadTexture.const_stride, 4, 4);
  4797. this.canReuse = true;
  4798. this.setAttributes(MeshQuadTexture._fixattriInfo);
  4799. if (!MeshQuadTexture._fixib) {
  4800. this.createQuadIB(MeshQuadTexture._maxIB);
  4801. MeshQuadTexture._fixib = this._ib;
  4802. }
  4803. else {
  4804. this._ib = MeshQuadTexture._fixib;
  4805. this._quadNum = MeshQuadTexture._maxIB;
  4806. }
  4807. }
  4808. static __int__() {
  4809. MeshQuadTexture._fixattriInfo = [5126, 4, 0,
  4810. 5121, 4, 16,
  4811. 5121, 4, 20];
  4812. }
  4813. static getAMesh(mainctx) {
  4814. var ret = null;
  4815. if (MeshQuadTexture._POOL.length) {
  4816. ret = MeshQuadTexture._POOL.pop();
  4817. }
  4818. else
  4819. ret = new MeshQuadTexture();
  4820. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshQuadTexture.const_stride, false);
  4821. return ret;
  4822. }
  4823. releaseMesh() {
  4824. this._vb.setByteLength(0);
  4825. this.vertNum = 0;
  4826. this.indexNum = 0;
  4827. MeshQuadTexture._POOL.push(this);
  4828. }
  4829. destroy() {
  4830. this._vb.destroy();
  4831. this._vb.deleteBuffer();
  4832. }
  4833. addQuad(pos, uv, color, useTex) {
  4834. var vb = this._vb;
  4835. var vpos = (vb._byteLength >> 2);
  4836. vb.setByteLength((vpos + MeshQuadTexture.const_stride) << 2);
  4837. var vbdata = vb._floatArray32 || vb.getFloat32Array();
  4838. var vbu32Arr = vb._uint32Array;
  4839. var cpos = vpos;
  4840. var useTexVal = useTex ? 0xff : 0;
  4841. vbdata[cpos++] = pos[0];
  4842. vbdata[cpos++] = pos[1];
  4843. vbdata[cpos++] = uv[0];
  4844. vbdata[cpos++] = uv[1];
  4845. vbu32Arr[cpos++] = color;
  4846. vbu32Arr[cpos++] = useTexVal;
  4847. vbdata[cpos++] = pos[2];
  4848. vbdata[cpos++] = pos[3];
  4849. vbdata[cpos++] = uv[2];
  4850. vbdata[cpos++] = uv[3];
  4851. vbu32Arr[cpos++] = color;
  4852. vbu32Arr[cpos++] = useTexVal;
  4853. vbdata[cpos++] = pos[4];
  4854. vbdata[cpos++] = pos[5];
  4855. vbdata[cpos++] = uv[4];
  4856. vbdata[cpos++] = uv[5];
  4857. vbu32Arr[cpos++] = color;
  4858. vbu32Arr[cpos++] = useTexVal;
  4859. vbdata[cpos++] = pos[6];
  4860. vbdata[cpos++] = pos[7];
  4861. vbdata[cpos++] = uv[6];
  4862. vbdata[cpos++] = uv[7];
  4863. vbu32Arr[cpos++] = color;
  4864. vbu32Arr[cpos++] = useTexVal;
  4865. vb._upload = true;
  4866. }
  4867. }
  4868. MeshQuadTexture.const_stride = 24;
  4869. MeshQuadTexture._maxIB = 16 * 1024;
  4870. MeshQuadTexture._POOL = [];
  4871. class MeshTexture extends Mesh2D {
  4872. constructor() {
  4873. super(MeshTexture.const_stride, 4, 4);
  4874. this.canReuse = true;
  4875. this.setAttributes(MeshTexture._fixattriInfo);
  4876. }
  4877. static __init__() {
  4878. MeshTexture._fixattriInfo = [5126, 4, 0,
  4879. 5121, 4, 16,
  4880. 5121, 4, 20];
  4881. }
  4882. static getAMesh(mainctx) {
  4883. var ret;
  4884. if (MeshTexture._POOL.length) {
  4885. ret = MeshTexture._POOL.pop();
  4886. }
  4887. else
  4888. ret = new MeshTexture();
  4889. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshTexture.const_stride, false);
  4890. return ret;
  4891. }
  4892. addData(vertices, uvs, idx, matrix, rgba) {
  4893. var vb = this._vb;
  4894. var ib = this._ib;
  4895. var vertsz = vertices.length >> 1;
  4896. var startpos = vb.needSize(vertsz * MeshTexture.const_stride);
  4897. var f32pos = startpos >> 2;
  4898. var vbdata = vb._floatArray32 || vb.getFloat32Array();
  4899. var vbu32Arr = vb._uint32Array;
  4900. var ci = 0;
  4901. var m00 = matrix.a;
  4902. var m01 = matrix.b;
  4903. var m10 = matrix.c;
  4904. var m11 = matrix.d;
  4905. var tx = matrix.tx;
  4906. var ty = matrix.ty;
  4907. var i = 0;
  4908. for (i = 0; i < vertsz; i++) {
  4909. var x = vertices[ci], y = vertices[ci + 1];
  4910. vbdata[f32pos] = x * m00 + y * m10 + tx;
  4911. vbdata[f32pos + 1] = x * m01 + y * m11 + ty;
  4912. vbdata[f32pos + 2] = uvs[ci];
  4913. vbdata[f32pos + 3] = uvs[ci + 1];
  4914. vbu32Arr[f32pos + 4] = rgba;
  4915. vbu32Arr[f32pos + 5] = 0xff;
  4916. f32pos += 6;
  4917. ci += 2;
  4918. }
  4919. vb.setNeedUpload();
  4920. var vertN = this.vertNum;
  4921. var sz = idx.length;
  4922. var stib = ib.needSize(idx.byteLength);
  4923. var cidx = ib.getUint16Array();
  4924. var stibid = stib >> 1;
  4925. if (vertN > 0) {
  4926. var end = stibid + sz;
  4927. var si = 0;
  4928. for (i = stibid; i < end; i++, si++) {
  4929. cidx[i] = idx[si] + vertN;
  4930. }
  4931. }
  4932. else {
  4933. cidx.set(idx, stibid);
  4934. }
  4935. ib.setNeedUpload();
  4936. this.vertNum += vertsz;
  4937. this.indexNum += idx.length;
  4938. }
  4939. releaseMesh() {
  4940. this._vb.setByteLength(0);
  4941. this._ib.setByteLength(0);
  4942. this.vertNum = 0;
  4943. this.indexNum = 0;
  4944. MeshTexture._POOL.push(this);
  4945. }
  4946. destroy() {
  4947. this._ib.destroy();
  4948. this._vb.destroy();
  4949. this._ib.disposeResource();
  4950. this._vb.deleteBuffer();
  4951. }
  4952. }
  4953. MeshTexture.const_stride = 24;
  4954. MeshTexture._POOL = [];
  4955. class MeshVG extends Mesh2D {
  4956. constructor() {
  4957. super(MeshVG.const_stride, 4, 4);
  4958. this.canReuse = true;
  4959. this.setAttributes(MeshVG._fixattriInfo);
  4960. }
  4961. static __init__() {
  4962. MeshVG._fixattriInfo = [5126, 2, 0,
  4963. 5121, 4, 8];
  4964. }
  4965. static getAMesh(mainctx) {
  4966. var ret;
  4967. if (MeshVG._POOL.length) {
  4968. ret = MeshVG._POOL.pop();
  4969. }
  4970. else
  4971. ret = new MeshVG();
  4972. mainctx && ret._vb._resizeBuffer(64 * 1024 * MeshVG.const_stride, false);
  4973. return ret;
  4974. }
  4975. addVertAndIBToMesh(ctx, points, rgba, ib) {
  4976. var startpos = this._vb.needSize(points.length / 2 * MeshVG.const_stride);
  4977. var f32pos = startpos >> 2;
  4978. var vbdata = this._vb._floatArray32 || this._vb.getFloat32Array();
  4979. var vbu32Arr = this._vb._uint32Array;
  4980. var ci = 0;
  4981. var sz = points.length / 2;
  4982. for (var i = 0; i < sz; i++) {
  4983. vbdata[f32pos++] = points[ci];
  4984. vbdata[f32pos++] = points[ci + 1];
  4985. ci += 2;
  4986. vbu32Arr[f32pos++] = rgba;
  4987. }
  4988. this._vb.setNeedUpload();
  4989. this._ib.append(new Uint16Array(ib));
  4990. this._ib.setNeedUpload();
  4991. this.vertNum += sz;
  4992. this.indexNum += ib.length;
  4993. }
  4994. releaseMesh() {
  4995. this._vb.setByteLength(0);
  4996. this._ib.setByteLength(0);
  4997. this.vertNum = 0;
  4998. this.indexNum = 0;
  4999. MeshVG._POOL.push(this);
  5000. }
  5001. destroy() {
  5002. this._ib.destroy();
  5003. this._vb.destroy();
  5004. this._ib.disposeResource();
  5005. this._vb.deleteBuffer();
  5006. }
  5007. }
  5008. MeshVG.const_stride = 12;
  5009. MeshVG._POOL = [];
  5010. class WebGLCacheAsNormalCanvas {
  5011. constructor(ctx, sp) {
  5012. this.submitStartPos = 0;
  5013. this.submitEndPos = 0;
  5014. this.context = null;
  5015. this.touches = [];
  5016. this.submits = [];
  5017. this.sprite = null;
  5018. this.meshlist = [];
  5019. this.cachedClipInfo = new Matrix();
  5020. this.oldTx = 0;
  5021. this.oldTy = 0;
  5022. this.invMat = new Matrix();
  5023. this.context = ctx;
  5024. this.sprite = sp;
  5025. ctx._globalClipMatrix.copyTo(this.cachedClipInfo);
  5026. }
  5027. startRec() {
  5028. if (this.context._charSubmitCache._enable) {
  5029. this.context._charSubmitCache.enable(false, this.context);
  5030. this.context._charSubmitCache.enable(true, this.context);
  5031. }
  5032. this.context._incache = true;
  5033. this.touches.length = 0;
  5034. this.context.touches = this.touches;
  5035. this.context._globalClipMatrix.copyTo(this.cachedClipInfo);
  5036. this.submits.length = 0;
  5037. this.submitStartPos = this.context._submits._length;
  5038. for (var i = 0, sz = this.meshlist.length; i < sz; i++) {
  5039. var curm = this.meshlist[i];
  5040. curm.canReuse ? (curm.releaseMesh()) : (curm.destroy());
  5041. }
  5042. this.meshlist.length = 0;
  5043. this._mesh = MeshQuadTexture.getAMesh(false);
  5044. this._pathMesh = MeshVG.getAMesh(false);
  5045. this._triangleMesh = MeshTexture.getAMesh(false);
  5046. this.meshlist.push(this._mesh);
  5047. this.meshlist.push(this._pathMesh);
  5048. this.meshlist.push(this._triangleMesh);
  5049. this.context._curSubmit = SubmitBase.RENDERBASE;
  5050. this._oldMesh = this.context._mesh;
  5051. this._oldPathMesh = this.context._pathMesh;
  5052. this._oldTriMesh = this.context._triangleMesh;
  5053. this._oldMeshList = this.context.meshlist;
  5054. this.context._mesh = this._mesh;
  5055. this.context._pathMesh = this._pathMesh;
  5056. this.context._triangleMesh = this._triangleMesh;
  5057. this.context.meshlist = this.meshlist;
  5058. this.oldTx = this.context._curMat.tx;
  5059. this.oldTy = this.context._curMat.ty;
  5060. this.context._curMat.tx = 0;
  5061. this.context._curMat.ty = 0;
  5062. this.context._curMat.copyTo(this.invMat);
  5063. this.invMat.invert();
  5064. }
  5065. endRec() {
  5066. if (this.context._charSubmitCache._enable) {
  5067. this.context._charSubmitCache.enable(false, this.context);
  5068. this.context._charSubmitCache.enable(true, this.context);
  5069. }
  5070. var parsubmits = this.context._submits;
  5071. this.submitEndPos = parsubmits._length;
  5072. var num = this.submitEndPos - this.submitStartPos;
  5073. for (var i = 0; i < num; i++) {
  5074. this.submits.push(parsubmits[this.submitStartPos + i]);
  5075. }
  5076. parsubmits._length -= num;
  5077. this.context._mesh = this._oldMesh;
  5078. this.context._pathMesh = this._oldPathMesh;
  5079. this.context._triangleMesh = this._oldTriMesh;
  5080. this.context.meshlist = this._oldMeshList;
  5081. this.context._curSubmit = SubmitBase.RENDERBASE;
  5082. this.context._curMat.tx = this.oldTx;
  5083. this.context._curMat.ty = this.oldTy;
  5084. this.context.touches = null;
  5085. this.context._incache = false;
  5086. }
  5087. isCacheValid() {
  5088. var curclip = this.context._globalClipMatrix;
  5089. if (curclip.a != this.cachedClipInfo.a || curclip.b != this.cachedClipInfo.b || curclip.c != this.cachedClipInfo.c
  5090. || curclip.d != this.cachedClipInfo.d || curclip.tx != this.cachedClipInfo.tx || curclip.ty != this.cachedClipInfo.ty)
  5091. return false;
  5092. return true;
  5093. }
  5094. flushsubmit() {
  5095. var curSubmit = SubmitBase.RENDERBASE;
  5096. this.submits.forEach(function (subm) {
  5097. if (subm == SubmitBase.RENDERBASE)
  5098. return;
  5099. SubmitBase.preRender = curSubmit;
  5100. curSubmit = subm;
  5101. subm.renderSubmit();
  5102. });
  5103. }
  5104. releaseMem() {
  5105. }
  5106. }
  5107. WebGLCacheAsNormalCanvas.matI = new Matrix();
  5108. 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}";
  5109. var texture_ps = "/*\r\n\ttexture和fillrect使用的。\r\n*/\r\n#ifdef GL_FRAGMENT_PRECISION_HIGH\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}";
  5110. 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}";
  5111. 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}";
  5112. 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}";
  5113. 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}";
  5114. class Shader2D {
  5115. constructor() {
  5116. this.ALPHA = 1;
  5117. this.defines = new ShaderDefines2D();
  5118. this.shaderType = 0;
  5119. this.fillStyle = DrawStyle.DEFAULT;
  5120. this.strokeStyle = DrawStyle.DEFAULT;
  5121. }
  5122. destroy() {
  5123. this.defines = null;
  5124. this.filters = null;
  5125. }
  5126. static __init__() {
  5127. Shader.preCompile2D(0, ShaderDefines2D.TEXTURE2D, texture_vs, texture_ps, null);
  5128. Shader.preCompile2D(0, ShaderDefines2D.PRIMITIVE, prime_vs, prime_ps, null);
  5129. Shader.preCompile2D(0, ShaderDefines2D.SKINMESH, skin_vs, skin_ps, null);
  5130. }
  5131. }
  5132. class SkinMeshBuffer {
  5133. constructor() {
  5134. var gl = LayaGL.instance;
  5135. this.ib = IndexBuffer2D.create(gl.DYNAMIC_DRAW);
  5136. this.vb = VertexBuffer2D.create(8);
  5137. }
  5138. static getInstance() {
  5139. return SkinMeshBuffer.instance = SkinMeshBuffer.instance || new SkinMeshBuffer();
  5140. }
  5141. addSkinMesh(skinMesh) {
  5142. skinMesh.getData2(this.vb, this.ib, this.vb._byteLength / 32);
  5143. }
  5144. reset() {
  5145. this.vb.clear();
  5146. this.ib.clear();
  5147. }
  5148. }
  5149. class BasePoly {
  5150. static createLine2(p, indices, lineWidth, indexBase, outVertex, loop) {
  5151. if (p.length < 4)
  5152. return null;
  5153. var points = BasePoly.tempData.length > (p.length + 2) ? BasePoly.tempData : new Array(p.length + 2);
  5154. points[0] = p[0];
  5155. points[1] = p[1];
  5156. var newlen = 2;
  5157. var i = 0;
  5158. var length = p.length;
  5159. for (i = 2; i < length; i += 2) {
  5160. if (Math.abs(p[i] - p[i - 2]) + Math.abs(p[i + 1] - p[i - 1]) > 0.01) {
  5161. points[newlen++] = p[i];
  5162. points[newlen++] = p[i + 1];
  5163. }
  5164. }
  5165. if (loop && Math.abs(p[0] - points[newlen - 2]) + Math.abs(p[1] - points[newlen - 1]) > 0.01) {
  5166. points[newlen++] = p[0];
  5167. points[newlen++] = p[1];
  5168. }
  5169. var result = outVertex;
  5170. length = newlen / 2;
  5171. var w = lineWidth / 2;
  5172. var px, py, p1x, p1y, p2x, p2y, p3x, p3y;
  5173. var perpx, perpy, perp2x, perp2y;
  5174. var a1, b1, c1, a2, b2, c2;
  5175. var denom, dist;
  5176. p1x = points[0];
  5177. p1y = points[1];
  5178. p2x = points[2];
  5179. p2y = points[3];
  5180. perpx = -(p1y - p2y);
  5181. perpy = p1x - p2x;
  5182. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5183. perpx = perpx / dist * w;
  5184. perpy = perpy / dist * w;
  5185. result.push(p1x - perpx, p1y - perpy, p1x + perpx, p1y + perpy);
  5186. for (i = 1; i < length - 1; i++) {
  5187. p1x = points[(i - 1) * 2];
  5188. p1y = points[(i - 1) * 2 + 1];
  5189. p2x = points[(i) * 2];
  5190. p2y = points[(i) * 2 + 1];
  5191. p3x = points[(i + 1) * 2];
  5192. p3y = points[(i + 1) * 2 + 1];
  5193. perpx = -(p1y - p2y);
  5194. perpy = p1x - p2x;
  5195. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5196. perpx = perpx / dist * w;
  5197. perpy = perpy / dist * w;
  5198. perp2x = -(p2y - p3y);
  5199. perp2y = p2x - p3x;
  5200. dist = Math.sqrt(perp2x * perp2x + perp2y * perp2y);
  5201. perp2x = perp2x / dist * w;
  5202. perp2y = perp2y / dist * w;
  5203. a1 = (-perpy + p1y) - (-perpy + p2y);
  5204. b1 = (-perpx + p2x) - (-perpx + p1x);
  5205. c1 = (-perpx + p1x) * (-perpy + p2y) - (-perpx + p2x) * (-perpy + p1y);
  5206. a2 = (-perp2y + p3y) - (-perp2y + p2y);
  5207. b2 = (-perp2x + p2x) - (-perp2x + p3x);
  5208. c2 = (-perp2x + p3x) * (-perp2y + p2y) - (-perp2x + p2x) * (-perp2y + p3y);
  5209. denom = a1 * b2 - a2 * b1;
  5210. if (Math.abs(denom) < 0.1) {
  5211. denom += 10.1;
  5212. result.push(p2x - perpx, p2y - perpy, p2x + perpx, p2y + perpy);
  5213. continue;
  5214. }
  5215. px = (b1 * c2 - b2 * c1) / denom;
  5216. py = (a2 * c1 - a1 * c2) / denom;
  5217. result.push(px, py, p2x - (px - p2x), p2y - (py - p2y));
  5218. }
  5219. p1x = points[newlen - 4];
  5220. p1y = points[newlen - 3];
  5221. p2x = points[newlen - 2];
  5222. p2y = points[newlen - 1];
  5223. perpx = -(p1y - p2y);
  5224. perpy = p1x - p2x;
  5225. dist = Math.sqrt(perpx * perpx + perpy * perpy);
  5226. perpx = perpx / dist * w;
  5227. perpy = perpy / dist * w;
  5228. result.push(p2x - perpx, p2y - perpy, p2x + perpx, p2y + perpy);
  5229. for (i = 1; i < length; i++) {
  5230. 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);
  5231. }
  5232. return result;
  5233. }
  5234. static createLineTriangle(path, color, width, loop, outvb, vbstride, outib) {
  5235. var points = path.slice();
  5236. var ptlen = points.length;
  5237. var p1x = points[0], p1y = points[1];
  5238. var p2x = points[2], p2y = points[2];
  5239. var len = 0;
  5240. var rp = 0;
  5241. var dx = 0, dy = 0;
  5242. var pointnum = ptlen / 2;
  5243. if (pointnum <= 1)
  5244. return;
  5245. if (pointnum == 2) {
  5246. return;
  5247. }
  5248. var tmpData = new Array(pointnum * 4);
  5249. var realPtNum = 0;
  5250. var ci = 0;
  5251. for (var i = 0; i < pointnum - 1; i++) {
  5252. p1x = points[ci++], p1y = points[ci++];
  5253. p2x = points[ci++], p2y = points[ci++];
  5254. dx = p2x - p1x, dy = p2y - p1y;
  5255. if (dx != 0 && dy != 0) {
  5256. len = Math.sqrt(dx * dx + dy * dy);
  5257. if (len > 1e-3) {
  5258. rp = realPtNum * 4;
  5259. tmpData[rp] = p1x;
  5260. tmpData[rp + 1] = p1y;
  5261. tmpData[rp + 2] = dx / len;
  5262. tmpData[rp + 3] = dy / len;
  5263. realPtNum++;
  5264. }
  5265. }
  5266. }
  5267. if (loop) {
  5268. p1x = points[ptlen - 2], p1y = points[ptlen - 1];
  5269. p2x = points[0], p2y = points[1];
  5270. dx = p2x - p1x, dy = p2y - p1y;
  5271. if (dx != 0 && dy != 0) {
  5272. len = Math.sqrt(dx * dx + dy * dy);
  5273. if (len > 1e-3) {
  5274. rp = realPtNum * 4;
  5275. tmpData[rp] = p1x;
  5276. tmpData[rp + 1] = p1y;
  5277. tmpData[rp + 2] = dx / len;
  5278. tmpData[rp + 3] = dy / len;
  5279. realPtNum++;
  5280. }
  5281. }
  5282. }
  5283. else {
  5284. rp = realPtNum * 4;
  5285. tmpData[rp] = p1x;
  5286. tmpData[rp + 1] = p1y;
  5287. tmpData[rp + 2] = dx / len;
  5288. tmpData[rp + 3] = dy / len;
  5289. realPtNum++;
  5290. }
  5291. ci = 0;
  5292. for (i = 0; i < pointnum; i++) {
  5293. p1x = points[ci], p1y = points[ci + 1];
  5294. p2x = points[ci + 2], p2y = points[ci + 3];
  5295. }
  5296. }
  5297. }
  5298. BasePoly.tempData = new Array(256);
  5299. class EarcutNode {
  5300. constructor(i, x, y) {
  5301. this.i = i;
  5302. this.x = x;
  5303. this.y = y;
  5304. this.prev = null;
  5305. this.next = null;
  5306. this.z = null;
  5307. this.prevZ = null;
  5308. this.nextZ = null;
  5309. this.steiner = false;
  5310. }
  5311. }
  5312. class Earcut {
  5313. static earcut(data, holeIndices, dim) {
  5314. dim = dim || 2;
  5315. var hasHoles = holeIndices && holeIndices.length, outerLen = hasHoles ? holeIndices[0] * dim : data.length, outerNode = Earcut.linkedList(data, 0, outerLen, dim, true), triangles = [];
  5316. if (!outerNode)
  5317. return triangles;
  5318. var minX, minY, maxX, maxY, x, y, invSize;
  5319. if (hasHoles)
  5320. outerNode = Earcut.eliminateHoles(data, holeIndices, outerNode, dim);
  5321. if (data.length > 80 * dim) {
  5322. minX = maxX = data[0];
  5323. minY = maxY = data[1];
  5324. for (var i = dim; i < outerLen; i += dim) {
  5325. x = data[i];
  5326. y = data[i + 1];
  5327. if (x < minX)
  5328. minX = x;
  5329. if (y < minY)
  5330. minY = y;
  5331. if (x > maxX)
  5332. maxX = x;
  5333. if (y > maxY)
  5334. maxY = y;
  5335. }
  5336. invSize = Math.max(maxX - minX, maxY - minY);
  5337. invSize = invSize !== 0 ? 1 / invSize : 0;
  5338. }
  5339. Earcut.earcutLinked(outerNode, triangles, dim, minX, minY, invSize);
  5340. return triangles;
  5341. }
  5342. static linkedList(data, start, end, dim, clockwise) {
  5343. var i, last;
  5344. if (clockwise === (Earcut.signedArea(data, start, end, dim) > 0)) {
  5345. for (i = start; i < end; i += dim)
  5346. last = Earcut.insertNode(i, data[i], data[i + 1], last);
  5347. }
  5348. else {
  5349. for (i = end - dim; i >= start; i -= dim)
  5350. last = Earcut.insertNode(i, data[i], data[i + 1], last);
  5351. }
  5352. if (last && Earcut.equals(last, last.next)) {
  5353. Earcut.removeNode(last);
  5354. last = last.next;
  5355. }
  5356. return last;
  5357. }
  5358. static filterPoints(start, end) {
  5359. if (!start)
  5360. return start;
  5361. if (!end)
  5362. end = start;
  5363. var p = start, again;
  5364. do {
  5365. again = false;
  5366. if (!p.steiner && (Earcut.equals(p, p.next) || Earcut.area(p.prev, p, p.next) === 0)) {
  5367. Earcut.removeNode(p);
  5368. p = end = p.prev;
  5369. if (p === p.next)
  5370. break;
  5371. again = true;
  5372. }
  5373. else {
  5374. p = p.next;
  5375. }
  5376. } while (again || p !== end);
  5377. return end;
  5378. }
  5379. static earcutLinked(ear, triangles, dim, minX, minY, invSize, pass = null) {
  5380. if (!ear)
  5381. return;
  5382. if (!pass && invSize)
  5383. Earcut.indexCurve(ear, minX, minY, invSize);
  5384. var stop = ear, prev, next;
  5385. while (ear.prev !== ear.next) {
  5386. prev = ear.prev;
  5387. next = ear.next;
  5388. if (invSize ? Earcut.isEarHashed(ear, minX, minY, invSize) : Earcut.isEar(ear)) {
  5389. triangles.push(prev.i / dim);
  5390. triangles.push(ear.i / dim);
  5391. triangles.push(next.i / dim);
  5392. Earcut.removeNode(ear);
  5393. ear = next.next;
  5394. stop = next.next;
  5395. continue;
  5396. }
  5397. ear = next;
  5398. if (ear === stop) {
  5399. if (!pass) {
  5400. Earcut.earcutLinked(Earcut.filterPoints(ear, null), triangles, dim, minX, minY, invSize, 1);
  5401. }
  5402. else if (pass === 1) {
  5403. ear = Earcut.cureLocalIntersections(ear, triangles, dim);
  5404. Earcut.earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
  5405. }
  5406. else if (pass === 2) {
  5407. Earcut.splitEarcut(ear, triangles, dim, minX, minY, invSize);
  5408. }
  5409. break;
  5410. }
  5411. }
  5412. }
  5413. static isEar(ear) {
  5414. var a = ear.prev, b = ear, c = ear.next;
  5415. if (Earcut.area(a, b, c) >= 0)
  5416. return false;
  5417. var p = ear.next.next;
  5418. while (p !== ear.prev) {
  5419. if (Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  5420. Earcut.area(p.prev, p, p.next) >= 0)
  5421. return false;
  5422. p = p.next;
  5423. }
  5424. return true;
  5425. }
  5426. static isEarHashed(ear, minX, minY, invSize) {
  5427. var a = ear.prev, b = ear, c = ear.next;
  5428. if (Earcut.area(a, b, c) >= 0)
  5429. return false;
  5430. 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);
  5431. var minZ = Earcut.zOrder(minTX, minTY, minX, minY, invSize), maxZ = Earcut.zOrder(maxTX, maxTY, minX, minY, invSize);
  5432. var p = ear.nextZ;
  5433. while (p && p.z <= maxZ) {
  5434. if (p !== ear.prev && p !== ear.next &&
  5435. Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  5436. Earcut.area(p.prev, p, p.next) >= 0)
  5437. return false;
  5438. p = p.nextZ;
  5439. }
  5440. p = ear.prevZ;
  5441. while (p && p.z >= minZ) {
  5442. if (p !== ear.prev && p !== ear.next &&
  5443. Earcut.pointInTriangle(a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y) &&
  5444. Earcut.area(p.prev, p, p.next) >= 0)
  5445. return false;
  5446. p = p.prevZ;
  5447. }
  5448. return true;
  5449. }
  5450. static cureLocalIntersections(start, triangles, dim) {
  5451. var p = start;
  5452. do {
  5453. var a = p.prev, b = p.next.next;
  5454. if (!Earcut.equals(a, b) && Earcut.intersects(a, p, p.next, b) && Earcut.locallyInside(a, b) && Earcut.locallyInside(b, a)) {
  5455. triangles.push(a.i / dim);
  5456. triangles.push(p.i / dim);
  5457. triangles.push(b.i / dim);
  5458. Earcut.removeNode(p);
  5459. Earcut.removeNode(p.next);
  5460. p = start = b;
  5461. }
  5462. p = p.next;
  5463. } while (p !== start);
  5464. return p;
  5465. }
  5466. static splitEarcut(start, triangles, dim, minX, minY, invSize) {
  5467. var a = start;
  5468. do {
  5469. var b = a.next.next;
  5470. while (b !== a.prev) {
  5471. if (a.i !== b.i && Earcut.isValidDiagonal(a, b)) {
  5472. var c = Earcut.splitPolygon(a, b);
  5473. a = Earcut.filterPoints(a, a.next);
  5474. c = Earcut.filterPoints(c, c.next);
  5475. Earcut.earcutLinked(a, triangles, dim, minX, minY, invSize);
  5476. Earcut.earcutLinked(c, triangles, dim, minX, minY, invSize);
  5477. return;
  5478. }
  5479. b = b.next;
  5480. }
  5481. a = a.next;
  5482. } while (a !== start);
  5483. }
  5484. static eliminateHoles(data, holeIndices, outerNode, dim) {
  5485. var queue = [], i, len, start, end, list;
  5486. for (i = 0, len = holeIndices.length; i < len; i++) {
  5487. start = holeIndices[i] * dim;
  5488. end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
  5489. list = Earcut.linkedList(data, start, end, dim, false);
  5490. if (list === list.next)
  5491. list.steiner = true;
  5492. queue.push(Earcut.getLeftmost(list));
  5493. }
  5494. queue.sort(Earcut.compareX);
  5495. for (i = 0; i < queue.length; i++) {
  5496. Earcut.eliminateHole(queue[i], outerNode);
  5497. outerNode = Earcut.filterPoints(outerNode, outerNode.next);
  5498. }
  5499. return outerNode;
  5500. }
  5501. static compareX(a, b) {
  5502. return a.x - b.x;
  5503. }
  5504. static eliminateHole(hole, outerNode) {
  5505. outerNode = Earcut.findHoleBridge(hole, outerNode);
  5506. if (outerNode) {
  5507. var b = Earcut.splitPolygon(outerNode, hole);
  5508. Earcut.filterPoints(b, b.next);
  5509. }
  5510. }
  5511. static findHoleBridge(hole, outerNode) {
  5512. var p = outerNode, hx = hole.x, hy = hole.y, qx = -Infinity, m;
  5513. do {
  5514. if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
  5515. var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
  5516. if (x <= hx && x > qx) {
  5517. qx = x;
  5518. if (x === hx) {
  5519. if (hy === p.y)
  5520. return p;
  5521. if (hy === p.next.y)
  5522. return p.next;
  5523. }
  5524. m = p.x < p.next.x ? p : p.next;
  5525. }
  5526. }
  5527. p = p.next;
  5528. } while (p !== outerNode);
  5529. if (!m)
  5530. return null;
  5531. if (hx === qx)
  5532. return m.prev;
  5533. var stop = m, mx = m.x, my = m.y, tanMin = Infinity, tan;
  5534. p = m.next;
  5535. while (p !== stop) {
  5536. if (hx >= p.x && p.x >= mx && hx !== p.x &&
  5537. Earcut.pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
  5538. tan = Math.abs(hy - p.y) / (hx - p.x);
  5539. if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && Earcut.locallyInside(p, hole)) {
  5540. m = p;
  5541. tanMin = tan;
  5542. }
  5543. }
  5544. p = p.next;
  5545. }
  5546. return m;
  5547. }
  5548. static indexCurve(start, minX, minY, invSize) {
  5549. var p = start;
  5550. do {
  5551. if (p.z === null)
  5552. p.z = Earcut.zOrder(p.x, p.y, minX, minY, invSize);
  5553. p.prevZ = p.prev;
  5554. p.nextZ = p.next;
  5555. p = p.next;
  5556. } while (p !== start);
  5557. p.prevZ.nextZ = null;
  5558. p.prevZ = null;
  5559. Earcut.sortLinked(p);
  5560. }
  5561. static sortLinked(list) {
  5562. var i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1;
  5563. do {
  5564. p = list;
  5565. list = null;
  5566. tail = null;
  5567. numMerges = 0;
  5568. while (p) {
  5569. numMerges++;
  5570. q = p;
  5571. pSize = 0;
  5572. for (i = 0; i < inSize; i++) {
  5573. pSize++;
  5574. q = q.nextZ;
  5575. if (!q)
  5576. break;
  5577. }
  5578. qSize = inSize;
  5579. while (pSize > 0 || (qSize > 0 && q)) {
  5580. if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
  5581. e = p;
  5582. p = p.nextZ;
  5583. pSize--;
  5584. }
  5585. else {
  5586. e = q;
  5587. q = q.nextZ;
  5588. qSize--;
  5589. }
  5590. if (tail)
  5591. tail.nextZ = e;
  5592. else
  5593. list = e;
  5594. e.prevZ = tail;
  5595. tail = e;
  5596. }
  5597. p = q;
  5598. }
  5599. tail.nextZ = null;
  5600. inSize *= 2;
  5601. } while (numMerges > 1);
  5602. return list;
  5603. }
  5604. static zOrder(x, y, minX, minY, invSize) {
  5605. x = 32767 * (x - minX) * invSize;
  5606. y = 32767 * (y - minY) * invSize;
  5607. x = (x | (x << 8)) & 0x00FF00FF;
  5608. x = (x | (x << 4)) & 0x0F0F0F0F;
  5609. x = (x | (x << 2)) & 0x33333333;
  5610. x = (x | (x << 1)) & 0x55555555;
  5611. y = (y | (y << 8)) & 0x00FF00FF;
  5612. y = (y | (y << 4)) & 0x0F0F0F0F;
  5613. y = (y | (y << 2)) & 0x33333333;
  5614. y = (y | (y << 1)) & 0x55555555;
  5615. return x | (y << 1);
  5616. }
  5617. static getLeftmost(start) {
  5618. var p = start, leftmost = start;
  5619. do {
  5620. if (p.x < leftmost.x)
  5621. leftmost = p;
  5622. p = p.next;
  5623. } while (p !== start);
  5624. return leftmost;
  5625. }
  5626. static pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
  5627. return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
  5628. (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
  5629. (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0;
  5630. }
  5631. static isValidDiagonal(a, b) {
  5632. return a.next.i !== b.i && a.prev.i !== b.i && !Earcut.intersectsPolygon(a, b) &&
  5633. Earcut.locallyInside(a, b) && Earcut.locallyInside(b, a) && Earcut.middleInside(a, b);
  5634. }
  5635. static area(p, q, r) {
  5636. return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
  5637. }
  5638. static equals(p1, p2) {
  5639. return p1.x === p2.x && p1.y === p2.y;
  5640. }
  5641. static intersects(p1, q1, p2, q2) {
  5642. if ((Earcut.equals(p1, q1) && Earcut.equals(p2, q2)) ||
  5643. (Earcut.equals(p1, q2) && Earcut.equals(p2, q1)))
  5644. return true;
  5645. return Earcut.area(p1, q1, p2) > 0 !== Earcut.area(p1, q1, q2) > 0 &&
  5646. Earcut.area(p2, q2, p1) > 0 !== Earcut.area(p2, q2, q1) > 0;
  5647. }
  5648. static intersectsPolygon(a, b) {
  5649. var p = a;
  5650. do {
  5651. if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  5652. Earcut.intersects(p, p.next, a, b))
  5653. return true;
  5654. p = p.next;
  5655. } while (p !== a);
  5656. return false;
  5657. }
  5658. static locallyInside(a, b) {
  5659. return Earcut.area(a.prev, a, a.next) < 0 ?
  5660. Earcut.area(a, b, a.next) >= 0 && Earcut.area(a, a.prev, b) >= 0 :
  5661. Earcut.area(a, b, a.prev) < 0 || Earcut.area(a, a.next, b) < 0;
  5662. }
  5663. static middleInside(a, b) {
  5664. var p = a, inside = false, px = (a.x + b.x) / 2, py = (a.y + b.y) / 2;
  5665. do {
  5666. if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
  5667. (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
  5668. inside = !inside;
  5669. p = p.next;
  5670. } while (p !== a);
  5671. return inside;
  5672. }
  5673. static splitPolygon(a, b) {
  5674. 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;
  5675. a.next = b;
  5676. b.prev = a;
  5677. a2.next = an;
  5678. an.prev = a2;
  5679. b2.next = a2;
  5680. a2.prev = b2;
  5681. bp.next = b2;
  5682. b2.prev = bp;
  5683. return b2;
  5684. }
  5685. static insertNode(i, x, y, last) {
  5686. var p = new EarcutNode(i, x, y);
  5687. if (!last) {
  5688. p.prev = p;
  5689. p.next = p;
  5690. }
  5691. else {
  5692. p.next = last.next;
  5693. p.prev = last;
  5694. last.next.prev = p;
  5695. last.next = p;
  5696. }
  5697. return p;
  5698. }
  5699. static removeNode(p) {
  5700. p.next.prev = p.prev;
  5701. p.prev.next = p.next;
  5702. if (p.prevZ)
  5703. p.prevZ.nextZ = p.nextZ;
  5704. if (p.nextZ)
  5705. p.nextZ.prevZ = p.prevZ;
  5706. }
  5707. static signedArea(data, start, end, dim) {
  5708. var sum = 0;
  5709. for (var i = start, j = end - dim; i < end; i += dim) {
  5710. sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
  5711. j = i;
  5712. }
  5713. return sum;
  5714. }
  5715. }
  5716. class CONST3D2D {
  5717. }
  5718. CONST3D2D.BYTES_PE = 4;
  5719. CONST3D2D.BYTES_PIDX = 2;
  5720. CONST3D2D.defaultMatrix4 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  5721. CONST3D2D.defaultMinusYMatrix4 = [1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
  5722. CONST3D2D.uniformMatrix3 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
  5723. CONST3D2D._TMPARRAY = [];
  5724. CONST3D2D._OFFSETX = 0;
  5725. CONST3D2D._OFFSETY = 0;
  5726. class Submit extends SubmitBase {
  5727. constructor(renderType = SubmitBase.TYPE_2D) {
  5728. super(renderType);
  5729. }
  5730. renderSubmit() {
  5731. if (this._numEle === 0 || !this._mesh || this._numEle == 0)
  5732. return 1;
  5733. var _tex = this.shaderValue.textureHost;
  5734. if (_tex) {
  5735. var source = _tex._getSource();
  5736. if (!source)
  5737. return 1;
  5738. this.shaderValue.texture = source;
  5739. }
  5740. var gl = WebGLContext.mainContext;
  5741. this._mesh.useMesh(gl);
  5742. this.shaderValue.upload();
  5743. if (BlendMode.activeBlendFunction !== this._blendFn) {
  5744. WebGLContext.setBlend(gl, true);
  5745. this._blendFn(gl);
  5746. BlendMode.activeBlendFunction = this._blendFn;
  5747. }
  5748. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  5749. Stat.renderBatches++;
  5750. Stat.trianglesFaces += this._numEle / 3;
  5751. return 1;
  5752. }
  5753. releaseRender() {
  5754. if (SubmitBase.RENDERBASE == this)
  5755. return;
  5756. if ((--this._ref) < 1) {
  5757. Submit.POOL[Submit._poolSize++] = this;
  5758. this.shaderValue.release();
  5759. this.shaderValue = null;
  5760. this._mesh = null;
  5761. this._parent && (this._parent.releaseRender(), this._parent = null);
  5762. }
  5763. }
  5764. static create(context, mesh, sv) {
  5765. var o = Submit._poolSize ? Submit.POOL[--Submit._poolSize] : new Submit();
  5766. o._ref = 1;
  5767. o._mesh = mesh;
  5768. o._key.clear();
  5769. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  5770. o._numEle = 0;
  5771. var blendType = context._nBlendType;
  5772. o._blendFn = context._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  5773. o.shaderValue = sv;
  5774. o.shaderValue.setValue(context._shader2D);
  5775. var filters = context._shader2D.filters;
  5776. filters && o.shaderValue.setFilters(filters);
  5777. return o;
  5778. }
  5779. static createShape(ctx, mesh, numEle, sv) {
  5780. var o = Submit._poolSize ? Submit.POOL[--Submit._poolSize] : (new Submit());
  5781. o._mesh = mesh;
  5782. o._numEle = numEle;
  5783. o._startIdx = mesh.indexNum * 2;
  5784. o._ref = 1;
  5785. o.shaderValue = sv;
  5786. o.shaderValue.setValue(ctx._shader2D);
  5787. var blendType = ctx._nBlendType;
  5788. o._key.blendShader = blendType;
  5789. o._blendFn = ctx._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  5790. return o;
  5791. }
  5792. }
  5793. Submit._poolSize = 0;
  5794. Submit.POOL = [];
  5795. class SubmitCanvas extends SubmitBase {
  5796. constructor() {
  5797. super(SubmitBase.TYPE_2D);
  5798. this._matrix = new Matrix();
  5799. this._matrix4 = CONST3D2D.defaultMatrix4.concat();
  5800. this.shaderValue = new Value2D(0, 0);
  5801. }
  5802. static create(canvas, alpha, filters) {
  5803. var o = (!SubmitCanvas.POOL._length) ? (new SubmitCanvas()) : SubmitCanvas.POOL[--SubmitCanvas.POOL._length];
  5804. o.canv = canvas;
  5805. o._ref = 1;
  5806. o._numEle = 0;
  5807. var v = o.shaderValue;
  5808. v.alpha = alpha;
  5809. v.defines.setValue(0);
  5810. filters && filters.length && v.setFilters(filters);
  5811. return o;
  5812. }
  5813. renderSubmit() {
  5814. var preAlpha = RenderState2D.worldAlpha;
  5815. var preMatrix4 = RenderState2D.worldMatrix4;
  5816. var preMatrix = RenderState2D.worldMatrix;
  5817. var preFilters = RenderState2D.worldFilters;
  5818. var preWorldShaderDefines = RenderState2D.worldShaderDefines;
  5819. var v = this.shaderValue;
  5820. var m = this._matrix;
  5821. var m4 = this._matrix4;
  5822. var mout = Matrix.TEMP;
  5823. Matrix.mul(m, preMatrix, mout);
  5824. m4[0] = mout.a;
  5825. m4[1] = mout.b;
  5826. m4[4] = mout.c;
  5827. m4[5] = mout.d;
  5828. m4[12] = mout.tx;
  5829. m4[13] = mout.ty;
  5830. RenderState2D.worldMatrix = mout.clone();
  5831. RenderState2D.worldMatrix4 = m4;
  5832. RenderState2D.worldAlpha = RenderState2D.worldAlpha * v.alpha;
  5833. if (v.filters && v.filters.length) {
  5834. RenderState2D.worldFilters = v.filters;
  5835. RenderState2D.worldShaderDefines = v.defines;
  5836. }
  5837. this.canv['flushsubmit']();
  5838. RenderState2D.worldAlpha = preAlpha;
  5839. RenderState2D.worldMatrix4 = preMatrix4;
  5840. RenderState2D.worldMatrix.destroy();
  5841. RenderState2D.worldMatrix = preMatrix;
  5842. RenderState2D.worldFilters = preFilters;
  5843. RenderState2D.worldShaderDefines = preWorldShaderDefines;
  5844. return 1;
  5845. }
  5846. releaseRender() {
  5847. if ((--this._ref) < 1) {
  5848. var cache = SubmitCanvas.POOL;
  5849. this._mesh = null;
  5850. cache[cache._length++] = this;
  5851. }
  5852. }
  5853. getRenderType() {
  5854. return SubmitBase.TYPE_CANVAS;
  5855. }
  5856. ;
  5857. }
  5858. SubmitCanvas.POOL = [];
  5859. {
  5860. SubmitCanvas.POOL._length = 0;
  5861. }
  5862. class SubmitTarget {
  5863. constructor() {
  5864. this.blendType = 0;
  5865. this._ref = 1;
  5866. this._key = new SubmitKey();
  5867. }
  5868. renderSubmit() {
  5869. var gl = WebGLContext.mainContext;
  5870. this._mesh.useMesh(gl);
  5871. var target = this.srcRT;
  5872. if (target) {
  5873. this.shaderValue.texture = target._getSource();
  5874. this.shaderValue.upload();
  5875. this.blend();
  5876. Stat.renderBatches++;
  5877. Stat.trianglesFaces += this._numEle / 3;
  5878. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  5879. }
  5880. return 1;
  5881. }
  5882. blend() {
  5883. if (BlendMode.activeBlendFunction !== BlendMode.fns[this.blendType]) {
  5884. var gl = WebGLContext.mainContext;
  5885. gl.enable(gl.BLEND);
  5886. BlendMode.fns[this.blendType](gl);
  5887. BlendMode.activeBlendFunction = BlendMode.fns[this.blendType];
  5888. }
  5889. }
  5890. getRenderType() {
  5891. return 0;
  5892. }
  5893. releaseRender() {
  5894. if ((--this._ref) < 1) {
  5895. var pool = SubmitTarget.POOL;
  5896. pool[pool._length++] = this;
  5897. }
  5898. }
  5899. static create(context, mesh, sv, rt) {
  5900. var o = SubmitTarget.POOL._length ? SubmitTarget.POOL[--SubmitTarget.POOL._length] : new SubmitTarget();
  5901. o._mesh = mesh;
  5902. o.srcRT = rt;
  5903. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  5904. o._ref = 1;
  5905. o._key.clear();
  5906. o._numEle = 0;
  5907. o.blendType = context._nBlendType;
  5908. o._key.blendShader = o.blendType;
  5909. o.shaderValue = sv;
  5910. o.shaderValue.setValue(context._shader2D);
  5911. if (context._colorFiler) {
  5912. var ft = context._colorFiler;
  5913. sv.defines.add(ft.type);
  5914. sv.colorMat = ft._mat;
  5915. sv.colorAlpha = ft._alpha;
  5916. }
  5917. return o;
  5918. }
  5919. }
  5920. SubmitTarget.POOL = [];
  5921. {
  5922. SubmitTarget.POOL._length = 0;
  5923. }
  5924. class SubmitTexture extends SubmitBase {
  5925. constructor(renderType = SubmitBase.TYPE_2D) {
  5926. super(renderType);
  5927. }
  5928. releaseRender() {
  5929. if ((--this._ref) < 1) {
  5930. SubmitTexture.POOL[SubmitTexture._poolSize++] = this;
  5931. this.shaderValue.release();
  5932. this._mesh = null;
  5933. this._parent && (this._parent.releaseRender(), this._parent = null);
  5934. }
  5935. }
  5936. renderSubmit() {
  5937. if (this._numEle === 0)
  5938. return 1;
  5939. var tex = this.shaderValue.textureHost;
  5940. if (tex) {
  5941. var source = tex ? tex._getSource() : null;
  5942. if (!source)
  5943. return 1;
  5944. }
  5945. var gl = WebGLContext.mainContext;
  5946. this._mesh.useMesh(gl);
  5947. var lastSubmit = SubmitBase.preRender;
  5948. var prekey = SubmitBase.preRender._key;
  5949. if (this._key.blendShader === 0 && (this._key.submitType === prekey.submitType && this._key.blendShader === prekey.blendShader) && BaseShader.activeShader &&
  5950. SubmitBase.preRender.clipInfoID == this.clipInfoID &&
  5951. lastSubmit.shaderValue.defines._value === this.shaderValue.defines._value &&
  5952. (this.shaderValue.defines._value & ShaderDefines2D.NOOPTMASK) == 0) {
  5953. BaseShader.activeShader.uploadTexture2D(source);
  5954. }
  5955. else {
  5956. if (BlendMode.activeBlendFunction !== this._blendFn) {
  5957. WebGLContext.setBlend(gl, true);
  5958. this._blendFn(gl);
  5959. BlendMode.activeBlendFunction = this._blendFn;
  5960. }
  5961. this.shaderValue.texture = source;
  5962. this.shaderValue.upload();
  5963. }
  5964. gl.drawElements(gl.TRIANGLES, this._numEle, gl.UNSIGNED_SHORT, this._startIdx);
  5965. Stat.renderBatches++;
  5966. Stat.trianglesFaces += this._numEle / 3;
  5967. return 1;
  5968. }
  5969. static create(context, mesh, sv) {
  5970. var o = SubmitTexture._poolSize ? SubmitTexture.POOL[--SubmitTexture._poolSize] : new SubmitTexture(SubmitBase.TYPE_TEXTURE);
  5971. o._mesh = mesh;
  5972. o._key.clear();
  5973. o._key.submitType = SubmitBase.KEY_DRAWTEXTURE;
  5974. o._ref = 1;
  5975. o._startIdx = mesh.indexNum * CONST3D2D.BYTES_PIDX;
  5976. o._numEle = 0;
  5977. var blendType = context._nBlendType;
  5978. o._key.blendShader = blendType;
  5979. o._blendFn = context._targets ? BlendMode.targetFns[blendType] : BlendMode.fns[blendType];
  5980. o.shaderValue = sv;
  5981. if (context._colorFiler) {
  5982. var ft = context._colorFiler;
  5983. sv.defines.add(ft.type);
  5984. sv.colorMat = ft._mat;
  5985. sv.colorAlpha = ft._alpha;
  5986. }
  5987. return o;
  5988. }
  5989. }
  5990. SubmitTexture._poolSize = 0;
  5991. SubmitTexture.POOL = [];
  5992. class CharSubmitCache {
  5993. constructor() {
  5994. this._data = [];
  5995. this._ndata = 0;
  5996. this._clipid = -1;
  5997. this._clipMatrix = new Matrix();
  5998. this._enable = false;
  5999. }
  6000. clear() {
  6001. this._tex = null;
  6002. this._imgId = -1;
  6003. this._ndata = 0;
  6004. this._enable = false;
  6005. this._colorFiler = null;
  6006. }
  6007. destroy() {
  6008. this.clear();
  6009. this._data.length = 0;
  6010. this._data = null;
  6011. }
  6012. add(ctx, tex, imgid, pos, uv, color) {
  6013. if (this._ndata > 0 && (this._tex != tex || this._imgId != imgid ||
  6014. (this._clipid >= 0 && this._clipid != ctx._clipInfoID))) {
  6015. this.submit(ctx);
  6016. }
  6017. this._clipid = ctx._clipInfoID;
  6018. ctx._globalClipMatrix.copyTo(this._clipMatrix);
  6019. this._tex = tex;
  6020. this._imgId = imgid;
  6021. this._colorFiler = ctx._colorFiler;
  6022. this._data[this._ndata] = pos;
  6023. this._data[this._ndata + 1] = uv;
  6024. this._data[this._ndata + 2] = color;
  6025. this._ndata += 3;
  6026. }
  6027. getPos() {
  6028. if (CharSubmitCache.__nPosPool == 0)
  6029. return new Array(8);
  6030. return CharSubmitCache.__posPool[--CharSubmitCache.__nPosPool];
  6031. }
  6032. enable(value, ctx) {
  6033. if (value === this._enable)
  6034. return;
  6035. this._enable = value;
  6036. this._enable || this.submit(ctx);
  6037. }
  6038. submit(ctx) {
  6039. var n = this._ndata;
  6040. if (!n)
  6041. return;
  6042. var _mesh = ctx._mesh;
  6043. var colorFiler = ctx._colorFiler;
  6044. ctx._colorFiler = this._colorFiler;
  6045. var submit = SubmitTexture.create(ctx, _mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  6046. ctx._submits[ctx._submits._length++] = ctx._curSubmit = submit;
  6047. submit.shaderValue.textureHost = this._tex;
  6048. submit._key.other = this._imgId;
  6049. ctx._colorFiler = colorFiler;
  6050. ctx._copyClipInfo(submit, this._clipMatrix);
  6051. submit.clipInfoID = this._clipid;
  6052. for (var i = 0; i < n; i += 3) {
  6053. _mesh.addQuad(this._data[i], this._data[i + 1], this._data[i + 2], true);
  6054. CharSubmitCache.__posPool[CharSubmitCache.__nPosPool++] = this._data[i];
  6055. }
  6056. n /= 3;
  6057. submit._numEle += n * 6;
  6058. _mesh.indexNum += n * 6;
  6059. _mesh.vertNum += n * 4;
  6060. ctx._drawCount += n;
  6061. this._ndata = 0;
  6062. if (RenderInfo.loopCount % 100 == 0)
  6063. this._data.length = 0;
  6064. }
  6065. }
  6066. CharSubmitCache.__posPool = [];
  6067. CharSubmitCache.__nPosPool = 0;
  6068. class AtlasGrid {
  6069. constructor(width = 0, height = 0, id = 0) {
  6070. this.atlasID = 0;
  6071. this._width = 0;
  6072. this._height = 0;
  6073. this._texCount = 0;
  6074. this._rowInfo = null;
  6075. this._cells = null;
  6076. this._used = 0;
  6077. this._cells = null;
  6078. this._rowInfo = null;
  6079. this.atlasID = id;
  6080. this._init(width, height);
  6081. }
  6082. addRect(type, width, height, pt) {
  6083. if (!this._get(width, height, pt))
  6084. return false;
  6085. this._fill(pt.x, pt.y, width, height, type);
  6086. this._texCount++;
  6087. return true;
  6088. }
  6089. _release() {
  6090. this._cells = null;
  6091. this._rowInfo = null;
  6092. }
  6093. _init(width, height) {
  6094. this._width = width;
  6095. this._height = height;
  6096. this._release();
  6097. if (this._width == 0)
  6098. return false;
  6099. this._cells = new Uint8Array(this._width * this._height * 3);
  6100. this._rowInfo = new Uint8Array(this._height);
  6101. this._used = 0;
  6102. this._clear();
  6103. return true;
  6104. }
  6105. _get(width, height, pt) {
  6106. if (width > this._width || height > this._height) {
  6107. return false;
  6108. }
  6109. var rx = -1;
  6110. var ry = -1;
  6111. var nWidth = this._width;
  6112. var nHeight = this._height;
  6113. var pCellBox = this._cells;
  6114. for (var y = 0; y < nHeight; y++) {
  6115. if (this._rowInfo[y] < width)
  6116. continue;
  6117. for (var x = 0; x < nWidth;) {
  6118. var tm = (y * nWidth + x) * 3;
  6119. if (pCellBox[tm] != 0 || pCellBox[tm + 1] < width || pCellBox[tm + 2] < height) {
  6120. x += pCellBox[tm + 1];
  6121. continue;
  6122. }
  6123. rx = x;
  6124. ry = y;
  6125. for (var xx = 0; xx < width; xx++) {
  6126. if (pCellBox[3 * xx + tm + 2] < height) {
  6127. rx = -1;
  6128. break;
  6129. }
  6130. }
  6131. if (rx < 0) {
  6132. x += pCellBox[tm + 1];
  6133. continue;
  6134. }
  6135. pt.x = rx;
  6136. pt.y = ry;
  6137. return true;
  6138. }
  6139. }
  6140. return false;
  6141. }
  6142. _fill(x, y, w, h, type) {
  6143. var nWidth = this._width;
  6144. var nHeghit = this._height;
  6145. this._check((x + w) <= nWidth && (y + h) <= nHeghit);
  6146. for (var yy = y; yy < (h + y); ++yy) {
  6147. this._check(this._rowInfo[yy] >= w);
  6148. this._rowInfo[yy] -= w;
  6149. for (var xx = 0; xx < w; xx++) {
  6150. var tm = (x + yy * nWidth + xx) * 3;
  6151. this._check(this._cells[tm] == 0);
  6152. this._cells[tm] = type;
  6153. this._cells[tm + 1] = w;
  6154. this._cells[tm + 2] = h;
  6155. }
  6156. }
  6157. if (x > 0) {
  6158. for (yy = 0; yy < h; ++yy) {
  6159. var s = 0;
  6160. for (xx = x - 1; xx >= 0; --xx, ++s) {
  6161. if (this._cells[((y + yy) * nWidth + xx) * 3] != 0)
  6162. break;
  6163. }
  6164. for (xx = s; xx > 0; --xx) {
  6165. this._cells[((y + yy) * nWidth + x - xx) * 3 + 1] = xx;
  6166. this._check(xx > 0);
  6167. }
  6168. }
  6169. }
  6170. if (y > 0) {
  6171. for (xx = x; xx < (x + w); ++xx) {
  6172. s = 0;
  6173. for (yy = y - 1; yy >= 0; --yy, s++) {
  6174. if (this._cells[(xx + yy * nWidth) * 3] != 0)
  6175. break;
  6176. }
  6177. for (yy = s; yy > 0; --yy) {
  6178. this._cells[(xx + (y - yy) * nWidth) * 3 + 2] = yy;
  6179. this._check(yy > 0);
  6180. }
  6181. }
  6182. }
  6183. this._used += (w * h) / (this._width * this._height);
  6184. }
  6185. _check(ret) {
  6186. if (ret == false) {
  6187. console.log("xtexMerger 错误啦");
  6188. }
  6189. }
  6190. _clear() {
  6191. this._texCount = 0;
  6192. for (var y = 0; y < this._height; y++) {
  6193. this._rowInfo[y] = this._width;
  6194. }
  6195. for (var i = 0; i < this._height; i++) {
  6196. for (var j = 0; j < this._width; j++) {
  6197. var tm = (i * this._width + j) * 3;
  6198. this._cells[tm] = 0;
  6199. this._cells[tm + 1] = this._width - j;
  6200. this._cells[tm + 2] = this._width - i;
  6201. }
  6202. }
  6203. }
  6204. }
  6205. class TextTexture extends Resource {
  6206. constructor(textureW, textureH) {
  6207. super();
  6208. this._texW = 0;
  6209. this._texH = 0;
  6210. this.__destroyed = false;
  6211. this._discardTm = 0;
  6212. this.genID = 0;
  6213. this.bitmap = { id: 0, _glTexture: null };
  6214. this.curUsedCovRate = 0;
  6215. this.curUsedCovRateAtlas = 0;
  6216. this.lastTouchTm = 0;
  6217. this.ri = null;
  6218. this._texW = textureW || TextTexture.gTextRender.atlasWidth;
  6219. this._texH = textureH || TextTexture.gTextRender.atlasWidth;
  6220. this.bitmap.id = this.id;
  6221. this.lock = true;
  6222. }
  6223. recreateResource() {
  6224. if (this._source)
  6225. return;
  6226. var gl = LayaGL.instance;
  6227. var glTex = this._source = gl.createTexture();
  6228. this.bitmap._glTexture = glTex;
  6229. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, glTex);
  6230. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this._texW, this._texH, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  6231. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  6232. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  6233. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  6234. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  6235. if (TextTexture.gTextRender.debugUV) {
  6236. this.fillWhite();
  6237. }
  6238. }
  6239. addChar(data, x, y, uv = null) {
  6240. if (TextTexture.gTextRender.isWan1Wan) {
  6241. return this.addCharCanvas(data, x, y, uv);
  6242. }
  6243. !this._source && this.recreateResource();
  6244. var gl = LayaGL.instance;
  6245. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, this._source);
  6246. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  6247. var dt = data.data;
  6248. if (data.data instanceof Uint8ClampedArray)
  6249. dt = new Uint8Array(dt.buffer);
  6250. gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, data.width, data.height, gl.RGBA, gl.UNSIGNED_BYTE, dt);
  6251. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  6252. var u0;
  6253. var v0;
  6254. var u1;
  6255. var v1;
  6256. u0 = x / this._texW;
  6257. v0 = y / this._texH;
  6258. u1 = (x + data.width) / this._texW;
  6259. v1 = (y + data.height) / this._texH;
  6260. uv = uv || new Array(8);
  6261. uv[0] = u0, uv[1] = v0;
  6262. uv[2] = u1, uv[3] = v0;
  6263. uv[4] = u1, uv[5] = v1;
  6264. uv[6] = u0, uv[7] = v1;
  6265. return uv;
  6266. }
  6267. addCharCanvas(canv, x, y, uv = null) {
  6268. !this._source && this.recreateResource();
  6269. var gl = LayaGL.instance;
  6270. WebGLContext.bindTexture(gl, gl.TEXTURE_2D, this._source);
  6271. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
  6272. gl.texSubImage2D(gl.TEXTURE_2D, 0, x, y, gl.RGBA, gl.UNSIGNED_BYTE, canv);
  6273. !ILaya.Render.isConchApp && gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
  6274. var u0;
  6275. var v0;
  6276. var u1;
  6277. var v1;
  6278. if (ILaya.Render.isConchApp) {
  6279. u0 = x / this._texW;
  6280. v0 = y / this._texH;
  6281. u1 = (x + canv.width) / this._texW;
  6282. v1 = (y + canv.height) / this._texH;
  6283. }
  6284. else {
  6285. u0 = (x + 1) / this._texW;
  6286. v0 = (y + 1) / this._texH;
  6287. u1 = (x + canv.width - 1) / this._texW;
  6288. v1 = (y + canv.height - 1) / this._texH;
  6289. }
  6290. uv = uv || new Array(8);
  6291. uv[0] = u0, uv[1] = v0;
  6292. uv[2] = u1, uv[3] = v0;
  6293. uv[4] = u1, uv[5] = v1;
  6294. uv[6] = u0, uv[7] = v1;
  6295. return uv;
  6296. }
  6297. fillWhite() {
  6298. !this._source && this.recreateResource();
  6299. var gl = LayaGL.instance;
  6300. var dt = new Uint8Array(this._texW * this._texH * 4);
  6301. dt.fill(0xff);
  6302. gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, this._texW, this._texH, gl.RGBA, gl.UNSIGNED_BYTE, dt);
  6303. }
  6304. discard() {
  6305. ILaya.stage.setGlobalRepaint();
  6306. this.destroy();
  6307. return;
  6308. if (this._texW != TextTexture.gTextRender.atlasWidth || this._texH != TextTexture.gTextRender.atlasWidth) {
  6309. this.destroy();
  6310. return;
  6311. }
  6312. this.genID++;
  6313. if (TextTexture.poolLen >= TextTexture.pool.length) {
  6314. TextTexture.pool = TextTexture.pool.concat(new Array(10));
  6315. }
  6316. this._discardTm = RenderInfo.loopStTm;
  6317. TextTexture.pool[TextTexture.poolLen++] = this;
  6318. }
  6319. static getTextTexture(w, h) {
  6320. return new TextTexture(w, h);
  6321. if (w != TextTexture.gTextRender.atlasWidth || w != TextTexture.gTextRender.atlasWidth)
  6322. return new TextTexture(w, h);
  6323. if (TextTexture.poolLen > 0) {
  6324. var ret = TextTexture.pool[--TextTexture.poolLen];
  6325. if (TextTexture.poolLen > 0)
  6326. TextTexture.clean();
  6327. return ret;
  6328. }
  6329. return new TextTexture(w, h);
  6330. }
  6331. destroy() {
  6332. this.__destroyed = true;
  6333. var gl = LayaGL.instance;
  6334. this._source && gl.deleteTexture(this._source);
  6335. this._source = null;
  6336. }
  6337. static clean() {
  6338. var curtm = RenderInfo.loopStTm;
  6339. if (TextTexture.cleanTm === 0)
  6340. TextTexture.cleanTm = curtm;
  6341. if (curtm - TextTexture.cleanTm >= TextTexture.gTextRender.checkCleanTextureDt) {
  6342. for (var i = 0; i < TextTexture.poolLen; i++) {
  6343. var p = TextTexture.pool[i];
  6344. if (curtm - p._discardTm >= TextTexture.gTextRender.destroyUnusedTextureDt) {
  6345. p.destroy();
  6346. TextTexture.pool[i] = TextTexture.pool[TextTexture.poolLen - 1];
  6347. TextTexture.poolLen--;
  6348. i--;
  6349. }
  6350. }
  6351. TextTexture.cleanTm = curtm;
  6352. }
  6353. }
  6354. touchRect(ri, curloop) {
  6355. if (this.lastTouchTm != curloop) {
  6356. this.curUsedCovRate = 0;
  6357. this.curUsedCovRateAtlas = 0;
  6358. this.lastTouchTm = curloop;
  6359. }
  6360. var texw2 = TextTexture.gTextRender.atlasWidth * TextTexture.gTextRender.atlasWidth;
  6361. var gridw2 = ILaya.TextAtlas.atlasGridW * ILaya.TextAtlas.atlasGridW;
  6362. this.curUsedCovRate += (ri.bmpWidth * ri.bmpHeight) / texw2;
  6363. this.curUsedCovRateAtlas += (Math.ceil(ri.bmpWidth / ILaya.TextAtlas.atlasGridW) * Math.ceil(ri.bmpHeight / ILaya.TextAtlas.atlasGridW)) / (texw2 / gridw2);
  6364. }
  6365. get texture() {
  6366. return this;
  6367. }
  6368. _getSource() {
  6369. return this._source;
  6370. }
  6371. drawOnScreen(x, y) {
  6372. }
  6373. }
  6374. TextTexture.gTextRender = null;
  6375. TextTexture.pool = new Array(10);
  6376. TextTexture.poolLen = 0;
  6377. TextTexture.cleanTm = 0;
  6378. class TextAtlas {
  6379. constructor() {
  6380. this.texWidth = 1024;
  6381. this.texHeight = 1024;
  6382. this.texture = null;
  6383. this.charMaps = {};
  6384. this.texHeight = this.texWidth = ILaya.TextRender.atlasWidth;
  6385. this.texture = TextTexture.getTextTexture(this.texWidth, this.texHeight);
  6386. if (this.texWidth / TextAtlas.atlasGridW > 256) {
  6387. TextAtlas.atlasGridW = Math.ceil(this.texWidth / 256);
  6388. }
  6389. this.atlasgrid = new AtlasGrid(this.texWidth / TextAtlas.atlasGridW, this.texHeight / TextAtlas.atlasGridW, this.texture.id);
  6390. }
  6391. setProtecteDist(d) {
  6392. }
  6393. getAEmpty(w, h, pt) {
  6394. var find = this.atlasgrid.addRect(1, Math.ceil(w / TextAtlas.atlasGridW), Math.ceil(h / TextAtlas.atlasGridW), pt);
  6395. if (find) {
  6396. pt.x *= TextAtlas.atlasGridW;
  6397. pt.y *= TextAtlas.atlasGridW;
  6398. }
  6399. return find;
  6400. }
  6401. get usedRate() {
  6402. return this.atlasgrid._used;
  6403. }
  6404. destroy() {
  6405. for (var k in this.charMaps) {
  6406. var ri = this.charMaps[k];
  6407. ri.deleted = true;
  6408. }
  6409. this.texture.discard();
  6410. }
  6411. printDebugInfo() {
  6412. }
  6413. }
  6414. TextAtlas.atlasGridW = 16;
  6415. class Event {
  6416. setTo(type, currentTarget, target) {
  6417. this.type = type;
  6418. this.currentTarget = currentTarget;
  6419. this.target = target;
  6420. return this;
  6421. }
  6422. stopPropagation() {
  6423. this._stoped = true;
  6424. }
  6425. get touches() {
  6426. if (!this.nativeEvent)
  6427. return null;
  6428. var arr = this.nativeEvent.touches;
  6429. if (arr) {
  6430. var stage = ILaya.stage;
  6431. for (var i = 0, n = arr.length; i < n; i++) {
  6432. var e = arr[i];
  6433. var point = Point.TEMP;
  6434. point.setTo(e.clientX, e.clientY);
  6435. stage._canvasTransform.invertTransformPoint(point);
  6436. stage.transform.invertTransformPoint(point);
  6437. e.stageX = point.x;
  6438. e.stageY = point.y;
  6439. }
  6440. }
  6441. return arr;
  6442. }
  6443. get altKey() {
  6444. return this.nativeEvent.altKey;
  6445. }
  6446. get ctrlKey() {
  6447. return this.nativeEvent.ctrlKey;
  6448. }
  6449. get shiftKey() {
  6450. return this.nativeEvent.shiftKey;
  6451. }
  6452. get charCode() {
  6453. return this.nativeEvent.charCode;
  6454. }
  6455. get keyLocation() {
  6456. return this.nativeEvent.location || this.nativeEvent.keyLocation;
  6457. }
  6458. get stageX() {
  6459. return ILaya.stage.mouseX;
  6460. }
  6461. get stageY() {
  6462. return ILaya.stage.mouseY;
  6463. }
  6464. }
  6465. Event.EMPTY = new Event();
  6466. Event.MOUSE_DOWN = "mousedown";
  6467. Event.MOUSE_UP = "mouseup";
  6468. Event.CLICK = "click";
  6469. Event.RIGHT_MOUSE_DOWN = "rightmousedown";
  6470. Event.RIGHT_MOUSE_UP = "rightmouseup";
  6471. Event.RIGHT_CLICK = "rightclick";
  6472. Event.MOUSE_MOVE = "mousemove";
  6473. Event.MOUSE_OVER = "mouseover";
  6474. Event.MOUSE_OUT = "mouseout";
  6475. Event.MOUSE_WHEEL = "mousewheel";
  6476. Event.ROLL_OVER = "mouseover";
  6477. Event.ROLL_OUT = "mouseout";
  6478. Event.DOUBLE_CLICK = "doubleclick";
  6479. Event.CHANGE = "change";
  6480. Event.CHANGED = "changed";
  6481. Event.RESIZE = "resize";
  6482. Event.ADDED = "added";
  6483. Event.REMOVED = "removed";
  6484. Event.DISPLAY = "display";
  6485. Event.UNDISPLAY = "undisplay";
  6486. Event.ERROR = "error";
  6487. Event.COMPLETE = "complete";
  6488. Event.LOADED = "loaded";
  6489. Event.READY = "ready";
  6490. Event.PROGRESS = "progress";
  6491. Event.INPUT = "input";
  6492. Event.RENDER = "render";
  6493. Event.OPEN = "open";
  6494. Event.MESSAGE = "message";
  6495. Event.CLOSE = "close";
  6496. Event.KEY_DOWN = "keydown";
  6497. Event.KEY_PRESS = "keypress";
  6498. Event.KEY_UP = "keyup";
  6499. Event.FRAME = "enterframe";
  6500. Event.DRAG_START = "dragstart";
  6501. Event.DRAG_MOVE = "dragmove";
  6502. Event.DRAG_END = "dragend";
  6503. Event.ENTER = "enter";
  6504. Event.SELECT = "select";
  6505. Event.BLUR = "blur";
  6506. Event.FOCUS = "focus";
  6507. Event.VISIBILITY_CHANGE = "visibilitychange";
  6508. Event.FOCUS_CHANGE = "focuschange";
  6509. Event.PLAYED = "played";
  6510. Event.PAUSED = "paused";
  6511. Event.STOPPED = "stopped";
  6512. Event.START = "start";
  6513. Event.END = "end";
  6514. Event.COMPONENT_ADDED = "componentadded";
  6515. Event.COMPONENT_REMOVED = "componentremoved";
  6516. Event.RELEASED = "released";
  6517. Event.LINK = "link";
  6518. Event.LABEL = "label";
  6519. Event.FULL_SCREEN_CHANGE = "fullscreenchange";
  6520. Event.DEVICE_LOST = "devicelost";
  6521. Event.TRANSFORM_CHANGED = "transformchanged";
  6522. Event.ANIMATION_CHANGED = "animationchanged";
  6523. Event.TRAIL_FILTER_CHANGE = "trailfilterchange";
  6524. Event.TRIGGER_ENTER = "triggerenter";
  6525. Event.TRIGGER_STAY = "triggerstay";
  6526. Event.TRIGGER_EXIT = "triggerexit";
  6527. class Texture extends EventDispatcher {
  6528. constructor(bitmap = null, uv = null, sourceWidth = 0, sourceHeight = 0) {
  6529. super();
  6530. this.uvrect = [0, 0, 1, 1];
  6531. this._destroyed = false;
  6532. this._referenceCount = 0;
  6533. this.$_GID = 0;
  6534. this.offsetX = 0;
  6535. this.offsetY = 0;
  6536. this._w = 0;
  6537. this._h = 0;
  6538. this.sourceWidth = 0;
  6539. this.sourceHeight = 0;
  6540. this.url = null;
  6541. this.scaleRate = 1;
  6542. this.setTo(bitmap, uv, sourceWidth, sourceHeight);
  6543. }
  6544. static moveUV(offsetX, offsetY, uv) {
  6545. for (var i = 0; i < 8; i += 2) {
  6546. uv[i] += offsetX;
  6547. uv[i + 1] += offsetY;
  6548. }
  6549. return uv;
  6550. }
  6551. static create(source, x, y, width, height, offsetX = 0, offsetY = 0, sourceWidth = 0, sourceHeight = 0) {
  6552. return Texture._create(source, x, y, width, height, offsetX, offsetY, sourceWidth, sourceHeight);
  6553. }
  6554. static _create(source, x, y, width, height, offsetX = 0, offsetY = 0, sourceWidth = 0, sourceHeight = 0, outTexture = null) {
  6555. var btex = source instanceof Texture;
  6556. var uv = btex ? source.uv : Texture.DEF_UV;
  6557. var bitmap = btex ? source.bitmap : source;
  6558. if (bitmap.width && (x + width) > bitmap.width)
  6559. width = bitmap.width - x;
  6560. if (bitmap.height && (y + height) > bitmap.height)
  6561. height = bitmap.height - y;
  6562. var tex;
  6563. if (outTexture) {
  6564. tex = outTexture;
  6565. tex.setTo(bitmap, null, sourceWidth || width, sourceHeight || height);
  6566. }
  6567. else {
  6568. tex = new Texture(bitmap, null, sourceWidth || width, sourceHeight || height);
  6569. }
  6570. tex.width = width;
  6571. tex.height = height;
  6572. tex.offsetX = offsetX;
  6573. tex.offsetY = offsetY;
  6574. var dwidth = 1 / bitmap.width;
  6575. var dheight = 1 / bitmap.height;
  6576. x *= dwidth;
  6577. y *= dheight;
  6578. width *= dwidth;
  6579. height *= dheight;
  6580. var u1 = tex.uv[0], v1 = tex.uv[1], u2 = tex.uv[4], v2 = tex.uv[5];
  6581. var inAltasUVWidth = (u2 - u1), inAltasUVHeight = (v2 - v1);
  6582. var oriUV = Texture.moveUV(uv[0], uv[1], [x, y, x + width, y, x + width, y + height, x, y + height]);
  6583. tex.uv = new Float32Array([u1 + oriUV[0] * inAltasUVWidth, v1 + oriUV[1] * inAltasUVHeight,
  6584. u2 - (1 - oriUV[2]) * inAltasUVWidth, v1 + oriUV[3] * inAltasUVHeight,
  6585. u2 - (1 - oriUV[4]) * inAltasUVWidth, v2 - (1 - oriUV[5]) * inAltasUVHeight,
  6586. u1 + oriUV[6] * inAltasUVWidth, v2 - (1 - oriUV[7]) * inAltasUVHeight]);
  6587. var bitmapScale = bitmap.scaleRate;
  6588. if (bitmapScale && bitmapScale != 1) {
  6589. tex.sourceWidth /= bitmapScale;
  6590. tex.sourceHeight /= bitmapScale;
  6591. tex.width /= bitmapScale;
  6592. tex.height /= bitmapScale;
  6593. tex.scaleRate = bitmapScale;
  6594. }
  6595. else {
  6596. tex.scaleRate = 1;
  6597. }
  6598. return tex;
  6599. }
  6600. static createFromTexture(texture, x, y, width, height) {
  6601. var texScaleRate = texture.scaleRate;
  6602. if (texScaleRate != 1) {
  6603. x *= texScaleRate;
  6604. y *= texScaleRate;
  6605. width *= texScaleRate;
  6606. height *= texScaleRate;
  6607. }
  6608. var rect = Rectangle.TEMP.setTo(x - texture.offsetX, y - texture.offsetY, width, height);
  6609. var result = rect.intersection(Texture._rect1.setTo(0, 0, texture.width, texture.height), Texture._rect2);
  6610. if (result)
  6611. var tex = Texture.create(texture, result.x, result.y, result.width, result.height, result.x - rect.x, result.y - rect.y, width, height);
  6612. else
  6613. return null;
  6614. return tex;
  6615. }
  6616. get uv() {
  6617. return this._uv;
  6618. }
  6619. set uv(value) {
  6620. this.uvrect[0] = Math.min(value[0], value[2], value[4], value[6]);
  6621. this.uvrect[1] = Math.min(value[1], value[3], value[5], value[7]);
  6622. this.uvrect[2] = Math.max(value[0], value[2], value[4], value[6]) - this.uvrect[0];
  6623. this.uvrect[3] = Math.max(value[1], value[3], value[5], value[7]) - this.uvrect[1];
  6624. this._uv = value;
  6625. }
  6626. get width() {
  6627. if (this._w)
  6628. return this._w;
  6629. if (!this.bitmap)
  6630. return 0;
  6631. return (this.uv && this.uv !== Texture.DEF_UV) ? (this.uv[2] - this.uv[0]) * this.bitmap.width : this.bitmap.width;
  6632. }
  6633. set width(value) {
  6634. this._w = value;
  6635. this.sourceWidth || (this.sourceWidth = value);
  6636. }
  6637. get height() {
  6638. if (this._h)
  6639. return this._h;
  6640. if (!this.bitmap)
  6641. return 0;
  6642. return (this.uv && this.uv !== Texture.DEF_UV) ? (this.uv[5] - this.uv[1]) * this.bitmap.height : this.bitmap.height;
  6643. }
  6644. set height(value) {
  6645. this._h = value;
  6646. this.sourceHeight || (this.sourceHeight = value);
  6647. }
  6648. get bitmap() {
  6649. return this._bitmap;
  6650. }
  6651. set bitmap(value) {
  6652. this._bitmap && this._bitmap._removeReference(this._referenceCount);
  6653. this._bitmap = value;
  6654. value && (value._addReference(this._referenceCount));
  6655. }
  6656. get destroyed() {
  6657. return this._destroyed;
  6658. }
  6659. _addReference() {
  6660. this._bitmap && this._bitmap._addReference();
  6661. this._referenceCount++;
  6662. }
  6663. _removeReference() {
  6664. this._bitmap && this._bitmap._removeReference();
  6665. this._referenceCount--;
  6666. }
  6667. _getSource(cb = null) {
  6668. if (this._destroyed || !this._bitmap)
  6669. return null;
  6670. this.recoverBitmap(cb);
  6671. return this._bitmap.destroyed ? null : this.bitmap._getSource();
  6672. }
  6673. _onLoaded(complete, context) {
  6674. if (!context) ;
  6675. else if (context == this) ;
  6676. else if (context instanceof Texture) {
  6677. var tex = context;
  6678. Texture._create(context, 0, 0, tex.width, tex.height, 0, 0, tex.sourceWidth, tex.sourceHeight, this);
  6679. }
  6680. else {
  6681. this.bitmap = context;
  6682. this.sourceWidth = this._w = context.width;
  6683. this.sourceHeight = this._h = context.height;
  6684. }
  6685. complete && complete.run();
  6686. this.event(Event.READY, this);
  6687. }
  6688. getIsReady() {
  6689. return this._destroyed ? false : (this._bitmap ? true : false);
  6690. }
  6691. setTo(bitmap = null, uv = null, sourceWidth = 0, sourceHeight = 0) {
  6692. this.bitmap = bitmap;
  6693. this.sourceWidth = sourceWidth;
  6694. this.sourceHeight = sourceHeight;
  6695. if (bitmap) {
  6696. this._w = bitmap.width;
  6697. this._h = bitmap.height;
  6698. this.sourceWidth = this.sourceWidth || bitmap.width;
  6699. this.sourceHeight = this.sourceHeight || bitmap.height;
  6700. }
  6701. this.uv = uv || Texture.DEF_UV;
  6702. }
  6703. load(url, complete = null) {
  6704. if (!this._destroyed)
  6705. ILaya.loader.load(url, Handler.create(this, this._onLoaded, [complete]), null, "htmlimage", 1, true);
  6706. }
  6707. getTexturePixels(x, y, width, height) {
  6708. var st, dst, i;
  6709. var tex2d = this.bitmap;
  6710. var texw = tex2d.width;
  6711. var texh = tex2d.height;
  6712. if (x + width > texw)
  6713. width -= (x + width) - texw;
  6714. if (y + height > texh)
  6715. height -= (y + height) - texh;
  6716. if (width <= 0 || height <= 0)
  6717. return null;
  6718. var wstride = width * 4;
  6719. var pix = null;
  6720. try {
  6721. pix = tex2d.getPixels();
  6722. }
  6723. catch (e) {
  6724. }
  6725. if (pix) {
  6726. if (x == 0 && y == 0 && width == texw && height == texh)
  6727. return pix;
  6728. var ret = new Uint8Array(width * height * 4);
  6729. wstride = texw * 4;
  6730. st = x * 4;
  6731. dst = (y + height - 1) * wstride + x * 4;
  6732. for (i = height - 1; i >= 0; i--) {
  6733. ret.set(dt.slice(dst, dst + width * 4), st);
  6734. st += wstride;
  6735. dst -= wstride;
  6736. }
  6737. return ret;
  6738. }
  6739. var ctx = new ILaya.Context();
  6740. ctx.size(width, height);
  6741. ctx.asBitmap = true;
  6742. var uv = null;
  6743. if (x != 0 || y != 0 || width != texw || height != texh) {
  6744. uv = this._uv.slice();
  6745. var stu = uv[0];
  6746. var stv = uv[1];
  6747. var uvw = uv[2] - stu;
  6748. var uvh = uv[7] - stv;
  6749. var uk = uvw / texw;
  6750. var vk = uvh / texh;
  6751. uv = [stu + x * uk, stv + y * vk,
  6752. stu + (x + width) * uk, stv + y * vk,
  6753. stu + (x + width) * uk, stv + (y + height) * vk,
  6754. stu + x * uk, stv + (y + height) * vk];
  6755. }
  6756. ctx._drawTextureM(this, 0, 0, width, height, null, 1.0, uv);
  6757. ctx._targets.start();
  6758. ctx.flush();
  6759. ctx._targets.end();
  6760. ctx._targets.restore();
  6761. var dt = ctx._targets.getData(0, 0, width, height);
  6762. ctx.destroy();
  6763. ret = new Uint8Array(width * height * 4);
  6764. st = 0;
  6765. dst = (height - 1) * wstride;
  6766. for (i = height - 1; i >= 0; i--) {
  6767. ret.set(dt.slice(dst, dst + wstride), st);
  6768. st += wstride;
  6769. dst -= wstride;
  6770. }
  6771. return ret;
  6772. }
  6773. getPixels(x, y, width, height) {
  6774. if (window.conch) {
  6775. return this._nativeObj.getImageData(x, y, width, height);
  6776. }
  6777. else {
  6778. return this.getTexturePixels(x, y, width, height);
  6779. }
  6780. }
  6781. recoverBitmap(onok = null) {
  6782. var url = this._bitmap.url;
  6783. if (!this._destroyed && (!this._bitmap || this._bitmap.destroyed) && url) {
  6784. ILaya.loader.load(url, Handler.create(this, function (bit) {
  6785. this.bitmap = bit;
  6786. onok && onok();
  6787. }), null, "htmlimage", 1, true);
  6788. }
  6789. }
  6790. disposeBitmap() {
  6791. if (!this._destroyed && this._bitmap) {
  6792. this._bitmap.destroy();
  6793. }
  6794. }
  6795. destroy(force = false) {
  6796. if (!this._destroyed) {
  6797. this._destroyed = true;
  6798. var bit = this._bitmap;
  6799. if (bit) {
  6800. bit._removeReference(this._referenceCount);
  6801. if (bit.referenceCount === 0 || force)
  6802. bit.destroy();
  6803. bit = null;
  6804. }
  6805. if (this.url && this === ILaya.loader.getRes(this.url))
  6806. ILaya.Loader.clearRes(this.url);
  6807. }
  6808. }
  6809. }
  6810. Texture.DEF_UV = new Float32Array([0, 0, 1.0, 0, 1.0, 1.0, 0, 1.0]);
  6811. Texture.NO_UV = new Float32Array([0, 0, 0, 0, 0, 0, 0, 0]);
  6812. Texture.INV_UV = new Float32Array([0, 1, 1.0, 1, 1.0, 0.0, 0, 0.0]);
  6813. Texture._rect1 = new Rectangle();
  6814. Texture._rect2 = new Rectangle();
  6815. class FontInfo {
  6816. constructor(font) {
  6817. this._font = "14px Arial";
  6818. this._family = "Arial";
  6819. this._size = 14;
  6820. this._italic = false;
  6821. this._bold = false;
  6822. this._id = FontInfo._gfontID++;
  6823. this.setFont(font || this._font);
  6824. }
  6825. static Parse(font) {
  6826. if (font === FontInfo._lastFont) {
  6827. return FontInfo._lastFontInfo;
  6828. }
  6829. var r = FontInfo._cache[font];
  6830. if (!r) {
  6831. r = FontInfo._cache[font] = new FontInfo(font);
  6832. }
  6833. FontInfo._lastFont = font;
  6834. FontInfo._lastFontInfo = r;
  6835. return r;
  6836. }
  6837. setFont(value) {
  6838. this._font = value;
  6839. var _words = value.split(' ');
  6840. var l = _words.length;
  6841. if (l < 2) {
  6842. if (l == 1) {
  6843. if (_words[0].indexOf('px') > 0) {
  6844. this._size = parseInt(_words[0]);
  6845. }
  6846. }
  6847. return;
  6848. }
  6849. var szpos = -1;
  6850. for (var i = 0; i < l; i++) {
  6851. if (_words[i].indexOf('px') > 0 || _words[i].indexOf('pt') > 0) {
  6852. szpos = i;
  6853. this._size = parseInt(_words[i]);
  6854. if (this._size <= 0) {
  6855. console.error('font parse error:' + value);
  6856. this._size = 14;
  6857. }
  6858. break;
  6859. }
  6860. }
  6861. var fpos = szpos + 1;
  6862. var familys = _words[fpos];
  6863. fpos++;
  6864. for (; fpos < l; fpos++) {
  6865. familys += ' ' + _words[fpos];
  6866. }
  6867. this._family = (familys.split(','))[0];
  6868. this._italic = _words.indexOf('italic') >= 0;
  6869. this._bold = _words.indexOf('bold') >= 0;
  6870. }
  6871. }
  6872. FontInfo.EMPTY = new FontInfo(null);
  6873. FontInfo._cache = {};
  6874. FontInfo._gfontID = 0;
  6875. FontInfo._lastFont = '';
  6876. class WordText {
  6877. constructor() {
  6878. this.save = [];
  6879. this.toUpperCase = null;
  6880. this.width = -1;
  6881. this.pageChars = [];
  6882. this.startID = 0;
  6883. this.startIDStroke = 0;
  6884. this.lastGCCnt = 0;
  6885. this.splitRender = false;
  6886. }
  6887. setText(txt) {
  6888. this.changed = true;
  6889. this._text = txt;
  6890. this.width = -1;
  6891. this.cleanCache();
  6892. }
  6893. toString() {
  6894. return this._text;
  6895. }
  6896. get length() {
  6897. return this._text ? this._text.length : 0;
  6898. }
  6899. charCodeAt(i) {
  6900. return this._text ? this._text.charCodeAt(i) : NaN;
  6901. }
  6902. charAt(i) {
  6903. return this._text ? this._text.charAt(i) : null;
  6904. }
  6905. cleanCache() {
  6906. this.pageChars.forEach(function (p) {
  6907. var tex = p.tex;
  6908. var words = p.words;
  6909. if (p.words.length == 1 && tex && tex.ri) {
  6910. tex.destroy();
  6911. }
  6912. });
  6913. this.pageChars = [];
  6914. this.startID = 0;
  6915. }
  6916. }
  6917. class CharRenderInfo {
  6918. constructor() {
  6919. this.char = '';
  6920. this.deleted = false;
  6921. this.uv = new Array(8);
  6922. this.pos = 0;
  6923. this.orix = 0;
  6924. this.oriy = 0;
  6925. this.touchTick = 0;
  6926. this.isSpace = false;
  6927. }
  6928. touch() {
  6929. var curLoop = RenderInfo.loopCount;
  6930. if (this.touchTick != curLoop) {
  6931. this.tex.touchRect(this, curLoop);
  6932. }
  6933. this.touchTick = curLoop;
  6934. }
  6935. }
  6936. class ICharRender {
  6937. constructor() {
  6938. this.fontsz = 16;
  6939. }
  6940. getWidth(font, str) { return 0; }
  6941. scale(sx, sy) {
  6942. }
  6943. get canvasWidth() {
  6944. return 0;
  6945. }
  6946. set canvasWidth(w) {
  6947. }
  6948. getCharBmp(char, font, lineWidth, colStr, strokeColStr, size, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  6949. return null;
  6950. }
  6951. }
  6952. class Browser {
  6953. static __init__() {
  6954. var Laya = window.Laya || ILaya.Laya;
  6955. if (Browser._window)
  6956. return Browser._window;
  6957. var win = Browser._window = window;
  6958. var doc = Browser._document = win.document;
  6959. var u = Browser.userAgent = win.navigator.userAgent;
  6960. var maxTouchPoints = win.navigator.maxTouchPoints || 0;
  6961. var platform = win.navigator.platform;
  6962. if (u.indexOf('AlipayMiniGame') > -1 && "my" in Browser.window) {
  6963. window.aliPayMiniGame(Laya, Laya);
  6964. if (!Laya["ALIMiniAdapter"]) {
  6965. console.error("请先添加阿里小游戏适配库");
  6966. }
  6967. else {
  6968. Laya["ALIMiniAdapter"].enable();
  6969. }
  6970. }
  6971. if (u.indexOf('OPPO') == -1 && u.indexOf("MiniGame") > -1 && "wx" in Browser.window) {
  6972. if ("qq" in Browser.window) {
  6973. window.qqMiniGame(Laya, Laya);
  6974. if (!Laya["QQMiniAdapter"]) {
  6975. console.error("请引入手机QQ小游戏的适配库:https://ldc2.layabox.com/doc/?nav=zh-ts-5-0-0");
  6976. }
  6977. else {
  6978. Laya["QQMiniAdapter"].enable();
  6979. }
  6980. }
  6981. else {
  6982. window.wxMiniGame(Laya, Laya);
  6983. if (!Laya["MiniAdpter"]) {
  6984. console.error("请先添加小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?nav=zh-ts-5-0-0");
  6985. }
  6986. else {
  6987. Laya["MiniAdpter"].enable();
  6988. }
  6989. }
  6990. }
  6991. if (u.indexOf("MiniGame") > -1 && "qq" in Browser.window) {
  6992. window.qqMiniGame(Laya, Laya);
  6993. if (!Laya["QQMiniAdapter"]) {
  6994. console.error("请先添加小游戏适配库,详细教程");
  6995. }
  6996. else {
  6997. Laya["QQMiniAdapter"].enable();
  6998. }
  6999. }
  7000. if (u.indexOf("SwanGame") > -1) {
  7001. window.bdMiniGame(Laya, Laya);
  7002. if (!Laya["BMiniAdapter"]) {
  7003. console.error("请先添加百度小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?nav=zh-ts-5-0-0");
  7004. }
  7005. else {
  7006. Laya["BMiniAdapter"].enable();
  7007. }
  7008. }
  7009. if (u.indexOf('QuickGame') > -1) {
  7010. window.miMiniGame(Laya, Laya);
  7011. if (!Laya["KGMiniAdapter"]) {
  7012. console.error("请先添加小米小游戏适配库,详细教程:https://ldc2.layabox.com/doc/?nav=zh-ts-5-0-0");
  7013. }
  7014. else {
  7015. Laya["KGMiniAdapter"].enable();
  7016. }
  7017. }
  7018. if (u.indexOf('OPPO') > -1 && u.indexOf('MiniGame') > -1) {
  7019. window.qgMiniGame(Laya, Laya);
  7020. if (!Laya["QGMiniAdapter"]) {
  7021. console.error("请先添加OPPO小游戏适配库");
  7022. }
  7023. else {
  7024. Laya["QGMiniAdapter"].enable();
  7025. }
  7026. }
  7027. if (u.indexOf('VVGame') > -1) {
  7028. window.vvMiniGame(Laya, Laya);
  7029. if (!Laya["VVMiniAdapter"]) {
  7030. console.error("请先添加VIVO小游戏适配库");
  7031. }
  7032. else {
  7033. Laya["VVMiniAdapter"].enable();
  7034. }
  7035. }
  7036. win.trace = console.log;
  7037. win.requestAnimationFrame = win.requestAnimationFrame || win.webkitRequestAnimationFrame || win.mozRequestAnimationFrame || win.oRequestAnimationFrame || win.msRequestAnimationFrame || function (fun) {
  7038. return win.setTimeout(fun, 1000 / 60);
  7039. };
  7040. var bodyStyle = doc.body.style;
  7041. bodyStyle.margin = 0;
  7042. bodyStyle.overflow = 'hidden';
  7043. bodyStyle['-webkit-user-select'] = 'none';
  7044. bodyStyle['-webkit-tap-highlight-color'] = 'rgba(200,200,200,0)';
  7045. var metas = doc.getElementsByTagName('meta');
  7046. var i = 0, flag = false, content = 'width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no';
  7047. while (i < metas.length) {
  7048. var meta = metas[i];
  7049. if (meta.name == 'viewport') {
  7050. meta.content = content;
  7051. flag = true;
  7052. break;
  7053. }
  7054. i++;
  7055. }
  7056. if (!flag) {
  7057. meta = doc.createElement('meta');
  7058. meta.name = 'viewport', meta.content = content;
  7059. doc.getElementsByTagName('head')[0].appendChild(meta);
  7060. }
  7061. Browser.onMobile = window.isConchApp ? true : u.indexOf("Mobile") > -1;
  7062. Browser.onIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  7063. Browser.onIPhone = u.indexOf("iPhone") > -1;
  7064. Browser.onMac = u.indexOf("Mac OS X") > -1;
  7065. Browser.onIPad = u.indexOf("iPad") > -1 || (platform === 'MacIntel' && maxTouchPoints > 1);
  7066. Browser.onAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  7067. Browser.onWP = u.indexOf("Windows Phone") > -1;
  7068. Browser.onQQBrowser = u.indexOf("QQBrowser") > -1;
  7069. Browser.onMQQBrowser = u.indexOf("MQQBrowser") > -1 || (u.indexOf("Mobile") > -1 && u.indexOf("QQ") > -1);
  7070. Browser.onIE = !!win.ActiveXObject || "ActiveXObject" in win;
  7071. Browser.onWeiXin = u.indexOf('MicroMessenger') > -1;
  7072. Browser.onSafari = u.indexOf("Safari") > -1;
  7073. Browser.onPC = !Browser.onMobile;
  7074. Browser.onMiniGame = u.indexOf('MiniGame') > -1;
  7075. Browser.onBDMiniGame = u.indexOf('SwanGame') > -1;
  7076. Browser.onLayaRuntime = !!Browser.window.conch;
  7077. if (u.indexOf('OPPO') > -1 && u.indexOf('MiniGame') > -1) {
  7078. Browser.onQGMiniGame = true;
  7079. Browser.onMiniGame = false;
  7080. }
  7081. else if ("qq" in Browser.window && u.indexOf('MiniGame') > -1) {
  7082. Browser.onQQMiniGame = true;
  7083. Browser.onMiniGame = false;
  7084. }
  7085. Browser.onVVMiniGame = u.indexOf('VVGame') > -1;
  7086. Browser.onKGMiniGame = u.indexOf('QuickGame') > -1;
  7087. if (u.indexOf('AlipayMiniGame') > -1) {
  7088. Browser.onAlipayMiniGame = true;
  7089. Browser.onMiniGame = false;
  7090. }
  7091. return win;
  7092. }
  7093. static createElement(type) {
  7094. Browser.__init__();
  7095. return Browser._document.createElement(type);
  7096. }
  7097. static getElementById(type) {
  7098. Browser.__init__();
  7099. return Browser._document.getElementById(type);
  7100. }
  7101. static removeElement(ele) {
  7102. if (ele && ele.parentNode)
  7103. ele.parentNode.removeChild(ele);
  7104. }
  7105. static now() {
  7106. return Date.now();
  7107. }
  7108. static get clientWidth() {
  7109. Browser.__init__();
  7110. return Browser._window.innerWidth || Browser._document.body.clientWidth;
  7111. }
  7112. static get clientHeight() {
  7113. Browser.__init__();
  7114. return Browser._window.innerHeight || Browser._document.body.clientHeight || Browser._document.documentElement.clientHeight;
  7115. }
  7116. static get width() {
  7117. Browser.__init__();
  7118. return ((ILaya.stage && ILaya.stage.canvasRotation) ? Browser.clientHeight : Browser.clientWidth) * Browser.pixelRatio;
  7119. }
  7120. static get height() {
  7121. Browser.__init__();
  7122. return ((ILaya.stage && ILaya.stage.canvasRotation) ? Browser.clientWidth : Browser.clientHeight) * Browser.pixelRatio;
  7123. }
  7124. static get pixelRatio() {
  7125. if (Browser._pixelRatio < 0) {
  7126. Browser.__init__();
  7127. if (Browser.userAgent.indexOf("Mozilla/6.0(Linux; Android 6.0; HUAWEI NXT-AL10 Build/HUAWEINXT-AL10)") > -1)
  7128. Browser._pixelRatio = 2;
  7129. else {
  7130. Browser._pixelRatio = (Browser._window.devicePixelRatio || 1);
  7131. if (Browser._pixelRatio < 1)
  7132. Browser._pixelRatio = 1;
  7133. }
  7134. }
  7135. return Browser._pixelRatio;
  7136. }
  7137. static get container() {
  7138. if (!Browser._container) {
  7139. Browser.__init__();
  7140. Browser._container = Browser.createElement("div");
  7141. Browser._container.id = "layaContainer";
  7142. Browser._document.body.appendChild(Browser._container);
  7143. }
  7144. return Browser._container;
  7145. }
  7146. static set container(value) {
  7147. Browser._container = value;
  7148. }
  7149. static get window() {
  7150. return Browser._window || Browser.__init__();
  7151. }
  7152. static get document() {
  7153. Browser.__init__();
  7154. return Browser._document;
  7155. }
  7156. }
  7157. Browser._pixelRatio = -1;
  7158. Browser.mainCanvas = null;
  7159. Browser.hanzi = new RegExp("^[\u4E00-\u9FA5]$");
  7160. Browser.fontMap = [];
  7161. Browser.measureText = function (txt, font) {
  7162. var isChinese = Browser.hanzi.test(txt);
  7163. if (isChinese && Browser.fontMap[font]) {
  7164. return Browser.fontMap[font];
  7165. }
  7166. var ctx = Browser.context;
  7167. ctx.font = font;
  7168. var r = ctx.measureText(txt);
  7169. if (isChinese)
  7170. Browser.fontMap[font] = r;
  7171. return r;
  7172. };
  7173. class CharRender_Canvas extends ICharRender {
  7174. constructor(maxw, maxh, scalefont = true, useImageData = true, showdbg = false) {
  7175. super();
  7176. this.ctx = null;
  7177. this.lastScaleX = 1.0;
  7178. this.lastScaleY = 1.0;
  7179. this.maxTexW = 0;
  7180. this.maxTexH = 0;
  7181. this.scaleFontSize = true;
  7182. this.showDbgInfo = false;
  7183. this.supportImageData = true;
  7184. this.maxTexW = maxw;
  7185. this.maxTexH = maxh;
  7186. this.scaleFontSize = scalefont;
  7187. this.supportImageData = useImageData;
  7188. this.showDbgInfo = showdbg;
  7189. if (!CharRender_Canvas.canvas) {
  7190. CharRender_Canvas.canvas = Browser.createElement('canvas');
  7191. CharRender_Canvas.canvas.width = 1024;
  7192. CharRender_Canvas.canvas.height = 512;
  7193. CharRender_Canvas.canvas.style.left = "-10000px";
  7194. CharRender_Canvas.canvas.style.position = "absolute";
  7195. document.body.appendChild(CharRender_Canvas.canvas);
  7196. this.ctx = CharRender_Canvas.canvas.getContext('2d');
  7197. }
  7198. }
  7199. get canvasWidth() {
  7200. return CharRender_Canvas.canvas.width;
  7201. }
  7202. set canvasWidth(w) {
  7203. if (CharRender_Canvas.canvas.width == w)
  7204. return;
  7205. CharRender_Canvas.canvas.width = w;
  7206. if (w > 2048) {
  7207. console.warn("画文字设置的宽度太大,超过2048了");
  7208. }
  7209. this.ctx.setTransform(1, 0, 0, 1, 0, 0);
  7210. this.ctx.scale(this.lastScaleX, this.lastScaleY);
  7211. }
  7212. getWidth(font, str) {
  7213. if (!this.ctx)
  7214. return 0;
  7215. if (this.ctx._lastFont != font) {
  7216. this.ctx.font = font;
  7217. this.ctx._lastFont = font;
  7218. }
  7219. return this.ctx.measureText(str).width;
  7220. }
  7221. scale(sx, sy) {
  7222. if (!this.supportImageData) {
  7223. this.lastScaleX = sx;
  7224. this.lastScaleY = sy;
  7225. return;
  7226. }
  7227. if (this.lastScaleX != sx || this.lastScaleY != sy) {
  7228. this.ctx.setTransform(sx, 0, 0, sy, 0, 0);
  7229. this.lastScaleX = sx;
  7230. this.lastScaleY = sy;
  7231. }
  7232. }
  7233. getCharBmp(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  7234. if (!this.supportImageData)
  7235. return this.getCharCanvas(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom);
  7236. var ctx = this.ctx;
  7237. var sz = this.fontsz;
  7238. if (ctx.font != font) {
  7239. ctx.font = font;
  7240. ctx._lastFont = font;
  7241. }
  7242. cri.width = ctx.measureText(char).width;
  7243. var w = cri.width * this.lastScaleX;
  7244. var h = cri.height * this.lastScaleY;
  7245. w += (margin_left + margin_right) * this.lastScaleX;
  7246. h += (margin_top + margin_bottom) * this.lastScaleY;
  7247. w = Math.ceil(w);
  7248. h = Math.ceil(h);
  7249. w = Math.min(w, CharRender_Canvas.canvas.width);
  7250. h = Math.min(h, CharRender_Canvas.canvas.height);
  7251. var clearW = w + lineWidth * 2 + 1;
  7252. var clearH = h + lineWidth * 2 + 1;
  7253. if (rect) {
  7254. clearW = Math.max(clearW, rect[0] + rect[2] + 1);
  7255. clearH = Math.max(clearH, rect[1] + rect[3] + 1);
  7256. }
  7257. ctx.clearRect(0, 0, clearW / this.lastScaleX + 1, clearH / this.lastScaleY + 1);
  7258. ctx.save();
  7259. ctx.textBaseline = "middle";
  7260. if (lineWidth > 0) {
  7261. ctx.strokeStyle = strokeColStr;
  7262. ctx.lineWidth = lineWidth;
  7263. ctx.strokeText(char, margin_left, margin_top + sz / 2);
  7264. }
  7265. if (colStr) {
  7266. ctx.fillStyle = colStr;
  7267. ctx.fillText(char, margin_left, margin_top + sz / 2);
  7268. }
  7269. if (this.showDbgInfo) {
  7270. ctx.strokeStyle = '#ff0000';
  7271. ctx.strokeRect(1, 1, w - 2, h - 2);
  7272. ctx.strokeStyle = '#00ff00';
  7273. ctx.strokeRect(margin_left, margin_top, cri.width, cri.height);
  7274. }
  7275. if (rect) {
  7276. if (rect[2] == -1)
  7277. rect[2] = Math.ceil((cri.width + lineWidth * 2) * this.lastScaleX);
  7278. }
  7279. var imgdt = rect ? (ctx.getImageData(rect[0], rect[1], rect[2], rect[3] + 1)) : (ctx.getImageData(0, 0, w, h + 1));
  7280. ctx.restore();
  7281. cri.bmpWidth = imgdt.width;
  7282. cri.bmpHeight = imgdt.height;
  7283. return imgdt;
  7284. }
  7285. getCharCanvas(char, font, lineWidth, colStr, strokeColStr, cri, margin_left, margin_top, margin_right, margin_bottom) {
  7286. var ctx = this.ctx;
  7287. if (ctx.font != font) {
  7288. ctx.font = font;
  7289. ctx._lastFont = font;
  7290. }
  7291. cri.width = ctx.measureText(char).width;
  7292. var w = cri.width * this.lastScaleX;
  7293. var h = cri.height * this.lastScaleY;
  7294. w += (margin_left + margin_right) * this.lastScaleX;
  7295. h += ((margin_top + margin_bottom) * this.lastScaleY + 1);
  7296. w = Math.min(w, this.maxTexW);
  7297. h = Math.min(h, this.maxTexH);
  7298. CharRender_Canvas.canvas.width = Math.min(w + 1, this.maxTexW);
  7299. CharRender_Canvas.canvas.height = Math.min(h + 1, this.maxTexH);
  7300. ctx.font = font;
  7301. ctx.clearRect(0, 0, w + 1 + lineWidth, h + 1 + lineWidth);
  7302. ctx.setTransform(1, 0, 0, 1, 0, 0);
  7303. ctx.save();
  7304. if (this.scaleFontSize) {
  7305. ctx.scale(this.lastScaleX, this.lastScaleY);
  7306. }
  7307. ctx.translate(margin_left, margin_top);
  7308. ctx.textAlign = "left";
  7309. var sz = this.fontsz;
  7310. ctx.textBaseline = "middle";
  7311. if (lineWidth > 0) {
  7312. ctx.strokeStyle = strokeColStr;
  7313. ctx.fillStyle = colStr;
  7314. ctx.lineWidth = lineWidth;
  7315. if (ctx.fillAndStrokeText) {
  7316. ctx.fillAndStrokeText(char, 0, sz / 2);
  7317. }
  7318. else {
  7319. ctx.strokeText(char, 0, sz / 2);
  7320. ctx.fillText(char, 0, sz / 2);
  7321. }
  7322. }
  7323. else if (colStr) {
  7324. ctx.fillStyle = colStr;
  7325. ctx.fillText(char, 0, sz / 2);
  7326. }
  7327. if (this.showDbgInfo) {
  7328. ctx.strokeStyle = '#ff0000';
  7329. ctx.strokeRect(0, 0, w, h);
  7330. ctx.strokeStyle = '#00ff00';
  7331. ctx.strokeRect(0, 0, cri.width, cri.height);
  7332. }
  7333. ctx.restore();
  7334. cri.bmpWidth = CharRender_Canvas.canvas.width;
  7335. cri.bmpHeight = CharRender_Canvas.canvas.height;
  7336. return CharRender_Canvas.canvas;
  7337. }
  7338. }
  7339. CharRender_Canvas.canvas = null;
  7340. class CharRender_Native extends ICharRender {
  7341. constructor() {
  7342. super();
  7343. this.lastFont = '';
  7344. this.lastScaleX = 1.0;
  7345. this.lastScaleY = 1.0;
  7346. }
  7347. getWidth(font, str) {
  7348. if (!window.conchTextCanvas)
  7349. return 0;
  7350. window.conchTextCanvas.font = font;
  7351. this.lastFont = font;
  7352. return window.conchTextCanvas.measureText(str).width;
  7353. }
  7354. scale(sx, sy) {
  7355. this.lastScaleX = sx;
  7356. this.lastScaleY = sy;
  7357. }
  7358. getCharBmp(char, font, lineWidth, colStr, strokeColStr, size, margin_left, margin_top, margin_right, margin_bottom, rect = null) {
  7359. if (!window.conchTextCanvas)
  7360. return null;
  7361. window.conchTextCanvas.font = font;
  7362. this.lastFont = font;
  7363. var w = size.width = window.conchTextCanvas.measureText(char).width;
  7364. var h = size.height;
  7365. w += (margin_left + margin_right);
  7366. h += (margin_top + margin_bottom);
  7367. window.conchTextCanvas.scale && window.conchTextCanvas.scale(this.lastScaleX, this.lastScaleY);
  7368. var c1 = ColorUtils.create(strokeColStr);
  7369. var nStrokeColor = c1.numColor;
  7370. var c2 = ColorUtils.create(colStr);
  7371. var nTextColor = c2.numColor;
  7372. var textInfo = window.conchTextCanvas.getTextBitmapData(char, nTextColor, lineWidth > 2 ? 2 : lineWidth, nStrokeColor);
  7373. size.bmpWidth = textInfo.width;
  7374. size.bmpHeight = textInfo.height;
  7375. return textInfo;
  7376. }
  7377. }
  7378. class TextRender {
  7379. constructor() {
  7380. this.fontSizeInfo = {};
  7381. this.mapFont = {};
  7382. this.fontID = 0;
  7383. this.mapColor = [];
  7384. this.colorID = 0;
  7385. this.fontScaleX = 1.0;
  7386. this.fontScaleY = 1.0;
  7387. this._curStrPos = 0;
  7388. this.textAtlases = [];
  7389. this.isoTextures = [];
  7390. this.lastFont = null;
  7391. this.fontSizeW = 0;
  7392. this.fontSizeH = 0;
  7393. this.fontSizeOffX = 0;
  7394. this.fontSizeOffY = 0;
  7395. this.renderPerChar = true;
  7396. this.tmpAtlasPos = new Point();
  7397. this.textureMem = 0;
  7398. ILaya.TextAtlas = TextAtlas;
  7399. var bugIOS = false;
  7400. var miniadp = ILaya.Laya['MiniAdpter'];
  7401. if (miniadp && miniadp.systemInfo && miniadp.systemInfo.system) {
  7402. bugIOS = miniadp.systemInfo.system.toLowerCase() === 'ios 10.1.1';
  7403. }
  7404. if (ILaya.Browser.onMiniGame && !bugIOS)
  7405. TextRender.isWan1Wan = true;
  7406. this.charRender = ILaya.Render.isConchApp ? (new CharRender_Native()) : (new CharRender_Canvas(TextRender.atlasWidth, TextRender.atlasWidth, TextRender.scaleFontWithCtx, !TextRender.isWan1Wan, false));
  7407. TextRender.textRenderInst = this;
  7408. ILaya.Laya['textRender'] = this;
  7409. TextRender.atlasWidth2 = TextRender.atlasWidth * TextRender.atlasWidth;
  7410. }
  7411. setFont(font) {
  7412. if (this.lastFont == font)
  7413. return;
  7414. this.lastFont = font;
  7415. var fontsz = this.getFontSizeInfo(font._family);
  7416. var offx = fontsz >> 24;
  7417. var offy = (fontsz >> 16) & 0xff;
  7418. var fw = (fontsz >> 8) & 0xff;
  7419. var fh = fontsz & 0xff;
  7420. var k = font._size / TextRender.standardFontSize;
  7421. this.fontSizeOffX = Math.ceil(offx * k);
  7422. this.fontSizeOffY = Math.ceil(offy * k);
  7423. this.fontSizeW = Math.ceil(fw * k);
  7424. this.fontSizeH = Math.ceil(fh * k);
  7425. if (font._font.indexOf('italic') >= 0) {
  7426. this.fontStr = font._font.replace('italic', '');
  7427. }
  7428. else {
  7429. this.fontStr = font._font;
  7430. }
  7431. }
  7432. getNextChar(str) {
  7433. var len = str.length;
  7434. var start = this._curStrPos;
  7435. if (start >= len)
  7436. return null;
  7437. var i = start;
  7438. var state = 0;
  7439. for (; i < len; i++) {
  7440. var c = str.charCodeAt(i);
  7441. if ((c >>> 11) == 0x1b) {
  7442. if (state == 1)
  7443. break;
  7444. state = 1;
  7445. i++;
  7446. }
  7447. else if (c === 0xfe0e || c === 0xfe0f) ;
  7448. else if (c == 0x200d) {
  7449. state = 2;
  7450. }
  7451. else {
  7452. if (state == 0)
  7453. state = 1;
  7454. else if (state == 1)
  7455. break;
  7456. }
  7457. }
  7458. this._curStrPos = i;
  7459. return str.substring(start, i);
  7460. }
  7461. filltext(ctx, data, x, y, fontStr, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  7462. if (data.length <= 0)
  7463. return;
  7464. var font = FontInfo.Parse(fontStr);
  7465. var nTextAlign = 0;
  7466. switch (textAlign) {
  7467. case 'center':
  7468. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  7469. break;
  7470. case 'right':
  7471. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  7472. break;
  7473. }
  7474. this._fast_filltext(ctx, data, null, x, y, font, color, strokeColor, lineWidth, nTextAlign, underLine);
  7475. }
  7476. fillWords(ctx, data, x, y, fontStr, color, strokeColor, lineWidth) {
  7477. if (!data)
  7478. return;
  7479. if (data.length <= 0)
  7480. return;
  7481. var font = typeof (fontStr) === 'string' ? FontInfo.Parse(fontStr) : fontStr;
  7482. this._fast_filltext(ctx, null, data, x, y, font, color, strokeColor, lineWidth, 0, 0);
  7483. }
  7484. _fast_filltext(ctx, data, htmlchars, x, y, font, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  7485. if (data && data.length < 1)
  7486. return;
  7487. if (htmlchars && htmlchars.length < 1)
  7488. return;
  7489. if (lineWidth < 0)
  7490. lineWidth = 0;
  7491. this.setFont(font);
  7492. this.fontScaleX = this.fontScaleY = 1.0;
  7493. if (TextRender.scaleFontWithCtx) {
  7494. var sx = 1;
  7495. var sy = 1;
  7496. if (!ILaya.Render.isConchApp || (window.conchTextCanvas.scale)) {
  7497. sx = ctx.getMatScaleX();
  7498. sy = ctx.getMatScaleY();
  7499. }
  7500. if (sx < 1e-4 || sy < 1e-1)
  7501. return;
  7502. if (sx > 1)
  7503. this.fontScaleX = sx;
  7504. if (sy > 1)
  7505. this.fontScaleY = sy;
  7506. }
  7507. font._italic && (ctx._italicDeg = 13);
  7508. var wt = data;
  7509. var isWT = !htmlchars && (data instanceof WordText);
  7510. var str = data;
  7511. var isHtmlChar = !!htmlchars;
  7512. var sameTexData = isWT ? wt.pageChars : [];
  7513. var strWidth = 0;
  7514. if (isWT) {
  7515. str = wt._text;
  7516. strWidth = wt.width;
  7517. if (strWidth < 0) {
  7518. strWidth = wt.width = this.charRender.getWidth(this.fontStr, str);
  7519. }
  7520. }
  7521. else {
  7522. strWidth = str ? this.charRender.getWidth(this.fontStr, str) : 0;
  7523. }
  7524. switch (textAlign) {
  7525. case ILaya.Context.ENUM_TEXTALIGN_CENTER:
  7526. x -= strWidth / 2;
  7527. break;
  7528. case ILaya.Context.ENUM_TEXTALIGN_RIGHT:
  7529. x -= strWidth;
  7530. break;
  7531. }
  7532. if (wt && sameTexData) {
  7533. if (this.hasFreedText(sameTexData)) {
  7534. sameTexData = wt.pageChars = [];
  7535. }
  7536. }
  7537. var ri = null;
  7538. var splitTex = this.renderPerChar = (!isWT) || TextRender.forceSplitRender || isHtmlChar || (isWT && wt.splitRender);
  7539. if (!sameTexData || sameTexData.length < 1) {
  7540. if (splitTex) {
  7541. var stx = 0;
  7542. var sty = 0;
  7543. this._curStrPos = 0;
  7544. var curstr;
  7545. while (true) {
  7546. if (htmlchars) {
  7547. var chc = htmlchars[this._curStrPos++];
  7548. if (chc) {
  7549. curstr = chc.char;
  7550. stx = chc.x;
  7551. sty = chc.y;
  7552. }
  7553. else {
  7554. curstr = null;
  7555. }
  7556. }
  7557. else {
  7558. curstr = this.getNextChar(str);
  7559. }
  7560. if (!curstr)
  7561. break;
  7562. ri = this.getCharRenderInfo(curstr, font, color, strokeColor, lineWidth, false);
  7563. if (!ri) {
  7564. break;
  7565. }
  7566. if (ri.isSpace) ;
  7567. else {
  7568. var add = sameTexData[ri.tex.id];
  7569. if (!add) {
  7570. var o1 = { texgen: ri.tex.genID, tex: ri.tex, words: [] };
  7571. sameTexData[ri.tex.id] = o1;
  7572. add = o1.words;
  7573. }
  7574. else {
  7575. add = add.words;
  7576. }
  7577. add.push({ ri: ri, x: stx, y: sty, w: ri.bmpWidth / this.fontScaleX, h: ri.bmpHeight / this.fontScaleY });
  7578. stx += ri.width;
  7579. }
  7580. }
  7581. }
  7582. else {
  7583. var isotex = TextRender.noAtlas || strWidth * this.fontScaleX > TextRender.atlasWidth;
  7584. ri = this.getCharRenderInfo(str, font, color, strokeColor, lineWidth, isotex);
  7585. 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 }] };
  7586. }
  7587. }
  7588. this._drawResortedWords(ctx, x, y, sameTexData);
  7589. ctx._italicDeg = 0;
  7590. }
  7591. _drawResortedWords(ctx, startx, starty, samePagesData) {
  7592. var isLastRender = ctx._charSubmitCache ? ctx._charSubmitCache._enable : false;
  7593. var mat = ctx._curMat;
  7594. var slen = samePagesData.length;
  7595. for (var id in samePagesData) {
  7596. var dt = samePagesData[id];
  7597. if (!dt)
  7598. continue;
  7599. var pri = dt.words;
  7600. var pisz = pri.length;
  7601. if (pisz <= 0)
  7602. continue;
  7603. var tex = samePagesData[id].tex;
  7604. for (var j = 0; j < pisz; j++) {
  7605. var riSaved = pri[j];
  7606. var ri = riSaved.ri;
  7607. if (ri.isSpace)
  7608. continue;
  7609. ri.touch();
  7610. ctx.drawTexAlign = true;
  7611. if (ILaya.Render.isConchApp) {
  7612. ctx._drawTextureM(tex.texture, startx + riSaved.x - ri.orix, starty + riSaved.y - ri.oriy, riSaved.w, riSaved.h, null, 1.0, ri.uv);
  7613. }
  7614. else {
  7615. let t = tex;
  7616. 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);
  7617. }
  7618. if (ctx.touches) {
  7619. ctx.touches.push(ri);
  7620. }
  7621. }
  7622. }
  7623. }
  7624. hasFreedText(txts) {
  7625. var sz = txts.length;
  7626. for (var i = 0; i < sz; i++) {
  7627. var pri = txts[i];
  7628. if (!pri)
  7629. continue;
  7630. var tex = pri.tex;
  7631. if (tex.__destroyed || tex.genID != pri.texgen) {
  7632. return true;
  7633. }
  7634. }
  7635. return false;
  7636. }
  7637. getCharRenderInfo(str, font, color, strokeColor, lineWidth, isoTexture = false) {
  7638. var fid = this.mapFont[font._family];
  7639. if (fid == undefined) {
  7640. this.mapFont[font._family] = fid = this.fontID++;
  7641. }
  7642. var key = str + '_' + fid + '_' + font._size + '_' + color;
  7643. if (lineWidth > 0)
  7644. key += '_' + strokeColor + lineWidth;
  7645. if (font._bold)
  7646. key += 'P';
  7647. if (this.fontScaleX != 1 || this.fontScaleY != 1) {
  7648. key += (this.fontScaleX * 20 | 0) + '_' + (this.fontScaleY * 20 | 0);
  7649. }
  7650. var i = 0;
  7651. var sz = this.textAtlases.length;
  7652. var ri;
  7653. var atlas;
  7654. if (!isoTexture) {
  7655. for (i = 0; i < sz; i++) {
  7656. atlas = this.textAtlases[i];
  7657. ri = atlas.charMaps[key];
  7658. if (ri) {
  7659. ri.touch();
  7660. return ri;
  7661. }
  7662. }
  7663. }
  7664. ri = new CharRenderInfo();
  7665. this.charRender.scale(this.fontScaleX, this.fontScaleY);
  7666. ri.char = str;
  7667. ri.height = font._size;
  7668. var margin = ILaya.Render.isConchApp ? 0 : (font._size / 3 | 0);
  7669. var imgdt = null;
  7670. if (!lineWidth) {
  7671. lineWidth = 0;
  7672. }
  7673. var w1 = Math.ceil((this.charRender.getWidth(this.fontStr, str) + 2 * lineWidth) * this.fontScaleX);
  7674. if (w1 > this.charRender.canvasWidth) {
  7675. this.charRender.canvasWidth = Math.min(2048, w1 + margin * 2);
  7676. }
  7677. if (isoTexture) {
  7678. this.charRender.fontsz = font._size;
  7679. imgdt = this.charRender.getCharBmp(str, this.fontStr, lineWidth, color, strokeColor, ri, margin, margin, margin, margin, null);
  7680. if (imgdt) {
  7681. var tex = TextTexture.getTextTexture(imgdt.width, imgdt.height);
  7682. tex.addChar(imgdt, 0, 0, ri.uv);
  7683. ri.tex = tex;
  7684. ri.orix = margin;
  7685. ri.oriy = margin;
  7686. tex.ri = ri;
  7687. this.isoTextures.push(tex);
  7688. }
  7689. }
  7690. else {
  7691. var len = str.length;
  7692. var lineExt = lineWidth * 1;
  7693. var fw = Math.ceil((this.fontSizeW + lineExt * 2) * this.fontScaleX);
  7694. var fh = Math.ceil((this.fontSizeH + lineExt * 2) * this.fontScaleY);
  7695. TextRender.imgdtRect[0] = ((margin - this.fontSizeOffX - lineExt) * this.fontScaleX) | 0;
  7696. TextRender.imgdtRect[1] = ((margin - this.fontSizeOffY - lineExt) * this.fontScaleY) | 0;
  7697. if (this.renderPerChar || len == 1) {
  7698. TextRender.imgdtRect[2] = Math.max(w1, fw);
  7699. TextRender.imgdtRect[3] = Math.max(w1, fh);
  7700. }
  7701. else {
  7702. TextRender.imgdtRect[2] = -1;
  7703. TextRender.imgdtRect[3] = fh;
  7704. }
  7705. this.charRender.fontsz = font._size;
  7706. imgdt = this.charRender.getCharBmp(str, this.fontStr, lineWidth, color, strokeColor, ri, margin, margin, margin, margin, TextRender.imgdtRect);
  7707. if (imgdt) {
  7708. atlas = this.addBmpData(imgdt, ri);
  7709. if (TextRender.isWan1Wan) {
  7710. ri.orix = margin;
  7711. ri.oriy = margin;
  7712. }
  7713. else {
  7714. ri.orix = (this.fontSizeOffX + lineExt);
  7715. ri.oriy = (this.fontSizeOffY + lineExt);
  7716. }
  7717. atlas.charMaps[key] = ri;
  7718. }
  7719. }
  7720. return ri;
  7721. }
  7722. addBmpData(data, ri) {
  7723. var w = data.width;
  7724. var h = data.height;
  7725. var sz = this.textAtlases.length;
  7726. var atlas;
  7727. var find = false;
  7728. for (var i = 0; i < sz; i++) {
  7729. atlas = this.textAtlases[i];
  7730. find = atlas.getAEmpty(w, h, this.tmpAtlasPos);
  7731. if (find) {
  7732. break;
  7733. }
  7734. }
  7735. if (!find) {
  7736. atlas = new TextAtlas();
  7737. this.textAtlases.push(atlas);
  7738. find = atlas.getAEmpty(w, h, this.tmpAtlasPos);
  7739. if (!find) {
  7740. throw 'err1';
  7741. }
  7742. this.cleanAtlases();
  7743. }
  7744. if (find) {
  7745. atlas.texture.addChar(data, this.tmpAtlasPos.x, this.tmpAtlasPos.y, ri.uv);
  7746. ri.tex = atlas.texture;
  7747. }
  7748. return atlas;
  7749. }
  7750. GC() {
  7751. var i = 0;
  7752. var sz = this.textAtlases.length;
  7753. var dt = 0;
  7754. var destroyDt = TextRender.destroyAtlasDt;
  7755. var totalUsedRate = 0;
  7756. var totalUsedRateAtlas = 0;
  7757. var curloop = RenderInfo.loopCount;
  7758. var maxWasteRateID = -1;
  7759. var maxWasteRate = 0;
  7760. var tex = null;
  7761. var curatlas = null;
  7762. for (; i < sz; i++) {
  7763. curatlas = this.textAtlases[i];
  7764. tex = curatlas.texture;
  7765. if (tex) {
  7766. totalUsedRate += tex.curUsedCovRate;
  7767. totalUsedRateAtlas += tex.curUsedCovRateAtlas;
  7768. var waste = curatlas.usedRate - tex.curUsedCovRateAtlas;
  7769. if (maxWasteRate < waste) {
  7770. maxWasteRate = waste;
  7771. maxWasteRateID = i;
  7772. }
  7773. }
  7774. dt = curloop - curatlas.texture.lastTouchTm;
  7775. if (dt > destroyDt) {
  7776. TextRender.showLog && console.log('TextRender GC delete atlas ' + tex ? curatlas.texture.id : 'unk');
  7777. curatlas.destroy();
  7778. this.textAtlases[i] = this.textAtlases[sz - 1];
  7779. sz--;
  7780. i--;
  7781. maxWasteRateID = -1;
  7782. }
  7783. }
  7784. this.textAtlases.length = sz;
  7785. sz = this.isoTextures.length;
  7786. for (i = 0; i < sz; i++) {
  7787. tex = this.isoTextures[i];
  7788. dt = curloop - tex.lastTouchTm;
  7789. if (dt > TextRender.destroyUnusedTextureDt) {
  7790. tex.ri.deleted = true;
  7791. tex.ri.tex = null;
  7792. tex.destroy();
  7793. this.isoTextures[i] = this.isoTextures[sz - 1];
  7794. sz--;
  7795. i--;
  7796. }
  7797. }
  7798. this.isoTextures.length = sz;
  7799. var needGC = this.textAtlases.length > 1 && this.textAtlases.length - totalUsedRateAtlas >= 2;
  7800. if (TextRender.atlasWidth * TextRender.atlasWidth * 4 * this.textAtlases.length > TextRender.cleanMem || needGC || TextRender.simClean) {
  7801. TextRender.simClean = false;
  7802. TextRender.showLog && console.log('清理使用率低的贴图。总使用率:', totalUsedRateAtlas, ':', this.textAtlases.length, '最差贴图:' + maxWasteRateID);
  7803. if (maxWasteRateID >= 0) {
  7804. curatlas = this.textAtlases[maxWasteRateID];
  7805. curatlas.destroy();
  7806. this.textAtlases[maxWasteRateID] = this.textAtlases[this.textAtlases.length - 1];
  7807. this.textAtlases.length = this.textAtlases.length - 1;
  7808. }
  7809. }
  7810. TextTexture.clean();
  7811. }
  7812. cleanAtlases() {
  7813. }
  7814. getCharBmp(c) {
  7815. }
  7816. checkBmpLine(data, l, sx, ex) {
  7817. if (this.bmpData32.buffer != data.data.buffer) {
  7818. this.bmpData32 = new Uint32Array(data.data.buffer);
  7819. }
  7820. var stpos = data.width * l + sx;
  7821. for (var x = sx; x < ex; x++) {
  7822. if (this.bmpData32[stpos++] != 0)
  7823. return true;
  7824. }
  7825. return false;
  7826. }
  7827. updateBbx(data, curbbx, onlyH = false) {
  7828. var w = data.width;
  7829. var h = data.height;
  7830. var x = 0;
  7831. var sy = curbbx[1];
  7832. var ey = 0;
  7833. var y = sy;
  7834. if (this.checkBmpLine(data, sy, 0, w)) {
  7835. while (true) {
  7836. y = (sy + ey) / 2 | 0;
  7837. if (y + 1 >= sy) {
  7838. curbbx[1] = y;
  7839. break;
  7840. }
  7841. if (this.checkBmpLine(data, y, 0, w)) {
  7842. sy = y;
  7843. }
  7844. else {
  7845. ey = y;
  7846. }
  7847. }
  7848. }
  7849. if (curbbx[3] > h)
  7850. curbbx[3] = h;
  7851. else {
  7852. y = sy = curbbx[3];
  7853. ey = h;
  7854. if (this.checkBmpLine(data, sy, 0, w)) {
  7855. while (true) {
  7856. y = (sy + ey) / 2 | 0;
  7857. if (y - 1 <= sy) {
  7858. curbbx[3] = y;
  7859. break;
  7860. }
  7861. if (this.checkBmpLine(data, y, 0, w)) {
  7862. sy = y;
  7863. }
  7864. else {
  7865. ey = y;
  7866. }
  7867. }
  7868. }
  7869. }
  7870. if (onlyH)
  7871. return;
  7872. var minx = curbbx[0];
  7873. var stpos = w * curbbx[1];
  7874. for (y = curbbx[1]; y < curbbx[3]; y++) {
  7875. for (x = 0; x < minx; x++) {
  7876. if (this.bmpData32[stpos + x] != 0) {
  7877. minx = x;
  7878. break;
  7879. }
  7880. }
  7881. stpos += w;
  7882. }
  7883. curbbx[0] = minx;
  7884. var maxx = curbbx[2];
  7885. stpos = w * curbbx[1];
  7886. for (y = curbbx[1]; y < curbbx[3]; y++) {
  7887. for (x = maxx; x < w; x++) {
  7888. if (this.bmpData32[stpos + x] != 0) {
  7889. maxx = x;
  7890. break;
  7891. }
  7892. }
  7893. stpos += w;
  7894. }
  7895. curbbx[2] = maxx;
  7896. }
  7897. getFontSizeInfo(font) {
  7898. var finfo = this.fontSizeInfo[font];
  7899. if (finfo != undefined)
  7900. return finfo;
  7901. var fontstr = 'bold ' + TextRender.standardFontSize + 'px ' + font;
  7902. if (TextRender.isWan1Wan) {
  7903. this.fontSizeW = this.charRender.getWidth(fontstr, '有') * 1.5;
  7904. this.fontSizeH = TextRender.standardFontSize * 1.5;
  7905. var szinfo = this.fontSizeW << 8 | this.fontSizeH;
  7906. this.fontSizeInfo[font] = szinfo;
  7907. return szinfo;
  7908. }
  7909. TextRender.pixelBBX[0] = TextRender.standardFontSize / 2;
  7910. TextRender.pixelBBX[1] = TextRender.standardFontSize / 2;
  7911. TextRender.pixelBBX[2] = TextRender.standardFontSize;
  7912. TextRender.pixelBBX[3] = TextRender.standardFontSize;
  7913. var orix = 16;
  7914. var oriy = 16;
  7915. var marginr = 16;
  7916. var marginb = 16;
  7917. this.charRender.scale(1, 1);
  7918. TextRender.tmpRI.height = TextRender.standardFontSize;
  7919. this.charRender.fontsz = TextRender.standardFontSize;
  7920. var bmpdt = this.charRender.getCharBmp('g', fontstr, 0, 'red', null, TextRender.tmpRI, orix, oriy, marginr, marginb);
  7921. if (ILaya.Render.isConchApp) {
  7922. bmpdt.data = new Uint8ClampedArray(bmpdt.data);
  7923. }
  7924. this.bmpData32 = new Uint32Array(bmpdt.data.buffer);
  7925. this.updateBbx(bmpdt, TextRender.pixelBBX, false);
  7926. bmpdt = this.charRender.getCharBmp('有', fontstr, 0, 'red', null, TextRender.tmpRI, oriy, oriy, marginr, marginb);
  7927. if (ILaya.Render.isConchApp) {
  7928. bmpdt.data = new Uint8ClampedArray(bmpdt.data);
  7929. }
  7930. this.bmpData32 = new Uint32Array(bmpdt.data.buffer);
  7931. if (TextRender.pixelBBX[2] < orix + TextRender.tmpRI.width)
  7932. TextRender.pixelBBX[2] = orix + TextRender.tmpRI.width;
  7933. this.updateBbx(bmpdt, TextRender.pixelBBX, false);
  7934. if (ILaya.Render.isConchApp) {
  7935. orix = 0;
  7936. oriy = 0;
  7937. }
  7938. var xoff = Math.max(orix - TextRender.pixelBBX[0], 0);
  7939. var yoff = Math.max(oriy - TextRender.pixelBBX[1], 0);
  7940. var bbxw = TextRender.pixelBBX[2] - TextRender.pixelBBX[0];
  7941. var bbxh = TextRender.pixelBBX[3] - TextRender.pixelBBX[1];
  7942. var sizeinfo = xoff << 24 | yoff << 16 | bbxw << 8 | bbxh;
  7943. this.fontSizeInfo[font] = sizeinfo;
  7944. return sizeinfo;
  7945. }
  7946. printDbgInfo() {
  7947. console.log('图集个数:' + this.textAtlases.length + ',每个图集大小:' + TextRender.atlasWidth + 'x' + TextRender.atlasWidth, ' 用canvas:', TextRender.isWan1Wan);
  7948. console.log('图集占用空间:' + (TextRender.atlasWidth * TextRender.atlasWidth * 4 / 1024 / 1024 * this.textAtlases.length) + 'M');
  7949. console.log('缓存用到的字体:');
  7950. for (var f in this.mapFont) {
  7951. var fontsz = this.getFontSizeInfo(f);
  7952. var offx = fontsz >> 24;
  7953. var offy = (fontsz >> 16) & 0xff;
  7954. var fw = (fontsz >> 8) & 0xff;
  7955. var fh = fontsz & 0xff;
  7956. console.log(' ' + f, ' off:', offx, offy, ' size:', fw, fh);
  7957. }
  7958. var num = 0;
  7959. console.log('缓存数据:');
  7960. var totalUsedRate = 0;
  7961. var totalUsedRateAtlas = 0;
  7962. this.textAtlases.forEach(function (a) {
  7963. var id = a.texture.id;
  7964. var dt = RenderInfo.loopCount - a.texture.lastTouchTm;
  7965. var dtstr = dt > 0 ? ('' + dt + '帧以前') : '当前帧';
  7966. totalUsedRate += a.texture.curUsedCovRate;
  7967. totalUsedRateAtlas += a.texture.curUsedCovRateAtlas;
  7968. console.log('--图集(id:' + id + ',当前使用率:' + (a.texture.curUsedCovRate * 1000 | 0) + '‰', '当前图集使用率:', (a.texture.curUsedCovRateAtlas * 100 | 0) + '%', '图集使用率:', (a.usedRate * 100 | 0), '%, 使用于:' + dtstr + ')--:');
  7969. for (var k in a.charMaps) {
  7970. var ri = a.charMaps[k];
  7971. 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);
  7972. num++;
  7973. }
  7974. });
  7975. console.log('独立贴图文字(' + this.isoTextures.length + '个):');
  7976. this.isoTextures.forEach(function (tex) {
  7977. console.log(' size:', tex._texW, tex._texH, 'touch间隔:', (RenderInfo.loopCount - tex.lastTouchTm), 'char:', tex.ri.char);
  7978. });
  7979. console.log('总缓存:', num, '总使用率:', totalUsedRate, '总当前图集使用率:', totalUsedRateAtlas);
  7980. }
  7981. showAtlas(n, bgcolor, x, y, w, h) {
  7982. if (!this.textAtlases[n]) {
  7983. console.log('没有这个图集');
  7984. return null;
  7985. }
  7986. var sp = new ILaya.Sprite();
  7987. var texttex = this.textAtlases[n].texture;
  7988. var texture = {
  7989. width: TextRender.atlasWidth,
  7990. height: TextRender.atlasWidth,
  7991. sourceWidth: TextRender.atlasWidth,
  7992. sourceHeight: TextRender.atlasWidth,
  7993. offsetX: 0,
  7994. offsetY: 0,
  7995. getIsReady: function () { return true; },
  7996. _addReference: function () { },
  7997. _removeReference: function () { },
  7998. _getSource: function () { return texttex._getSource(); },
  7999. bitmap: { id: texttex.id },
  8000. _uv: Texture.DEF_UV
  8001. };
  8002. sp.size = function (w, h) {
  8003. this.width = w;
  8004. this.height = h;
  8005. sp.graphics.clear();
  8006. sp.graphics.drawRect(0, 0, sp.width, sp.height, bgcolor);
  8007. sp.graphics.drawTexture(texture, 0, 0, sp.width, sp.height);
  8008. return this;
  8009. };
  8010. sp.graphics.drawRect(0, 0, w, h, bgcolor);
  8011. sp.graphics.drawTexture(texture, 0, 0, w, h);
  8012. sp.pos(x, y);
  8013. ILaya.stage.addChild(sp);
  8014. return sp;
  8015. }
  8016. filltext_native(ctx, data, htmlchars, x, y, fontStr, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  8017. if (data && data.length <= 0)
  8018. return;
  8019. if (htmlchars && htmlchars.length < 1)
  8020. return;
  8021. var font = FontInfo.Parse(fontStr);
  8022. var nTextAlign = 0;
  8023. switch (textAlign) {
  8024. case 'center':
  8025. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  8026. break;
  8027. case 'right':
  8028. nTextAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  8029. break;
  8030. }
  8031. return this._fast_filltext(ctx, data, htmlchars, x, y, font, color, strokeColor, lineWidth, nTextAlign, underLine);
  8032. }
  8033. }
  8034. TextRender.useOldCharBook = false;
  8035. TextRender.atlasWidth = 1024;
  8036. TextRender.noAtlas = false;
  8037. TextRender.forceSplitRender = false;
  8038. TextRender.forceWholeRender = false;
  8039. TextRender.scaleFontWithCtx = true;
  8040. TextRender.standardFontSize = 32;
  8041. TextRender.destroyAtlasDt = 10;
  8042. TextRender.checkCleanTextureDt = 2000;
  8043. TextRender.destroyUnusedTextureDt = 3000;
  8044. TextRender.cleanMem = 100 * 1024 * 1024;
  8045. TextRender.isWan1Wan = false;
  8046. TextRender.showLog = false;
  8047. TextRender.debugUV = false;
  8048. TextRender.tmpRI = new CharRenderInfo();
  8049. TextRender.pixelBBX = [0, 0, 0, 0];
  8050. TextRender.imgdtRect = [0, 0, 0, 0];
  8051. TextRender.simClean = false;
  8052. TextTexture.gTextRender = TextRender;
  8053. class Context {
  8054. constructor() {
  8055. this._tmpMatrix = new Matrix();
  8056. this._drawTexToDrawTri_Vert = new Float32Array(8);
  8057. this._drawTexToDrawTri_Index = new Uint16Array([0, 1, 2, 0, 2, 3]);
  8058. this._tempUV = new Float32Array(8);
  8059. this._drawTriUseAbsMatrix = false;
  8060. this._id = ++Context._COUNT;
  8061. this._other = null;
  8062. this._renderNextSubmitIndex = 0;
  8063. this._path = null;
  8064. this._drawCount = 1;
  8065. this._width = Context._MAXSIZE;
  8066. this._height = Context._MAXSIZE;
  8067. this._renderCount = 0;
  8068. this._submits = null;
  8069. this._curSubmit = null;
  8070. this._submitKey = new SubmitKey();
  8071. this._mesh = null;
  8072. this._pathMesh = null;
  8073. this._triangleMesh = null;
  8074. this.meshlist = [];
  8075. this._transedPoints = new Array(8);
  8076. this._temp4Points = new Array(8);
  8077. this._clipRect = Context.MAXCLIPRECT;
  8078. this._globalClipMatrix = new Matrix(Context._MAXSIZE, 0, 0, Context._MAXSIZE, 0, 0);
  8079. this._clipInCache = false;
  8080. this._clipInfoID = 0;
  8081. this._clipID_Gen = 0;
  8082. this._lastMatScaleX = 1.0;
  8083. this._lastMatScaleY = 1.0;
  8084. this._lastMat_a = 1.0;
  8085. this._lastMat_b = 0.0;
  8086. this._lastMat_c = 0.0;
  8087. this._lastMat_d = 1.0;
  8088. this._nBlendType = 0;
  8089. this._save = null;
  8090. this._targets = null;
  8091. this._charSubmitCache = null;
  8092. this._saveMark = null;
  8093. this._shader2D = new Shader2D();
  8094. this.sprite = null;
  8095. this._italicDeg = 0;
  8096. this._lastTex = null;
  8097. this._fillColor = 0;
  8098. this._flushCnt = 0;
  8099. this.defTexture = null;
  8100. this._colorFiler = null;
  8101. this.drawTexAlign = false;
  8102. this._incache = false;
  8103. this.isMain = false;
  8104. Context._contextcount++;
  8105. Context._textRender = Context._textRender || new TextRender();
  8106. if (!this.defTexture) {
  8107. var defTex2d = new Texture2D(2, 2);
  8108. defTex2d.setPixels(new Uint8Array(16));
  8109. defTex2d.lock = true;
  8110. this.defTexture = new Texture(defTex2d);
  8111. }
  8112. this._lastTex = this.defTexture;
  8113. this.clear();
  8114. }
  8115. static __init__() {
  8116. Context.MAXCLIPRECT = new Rectangle(0, 0, Context._MAXSIZE, Context._MAXSIZE);
  8117. ContextParams.DEFAULT = new ContextParams();
  8118. }
  8119. drawImage(...args) {
  8120. }
  8121. getImageData(...args) {
  8122. }
  8123. measureText(text) {
  8124. return null;
  8125. }
  8126. setTransform(...args) {
  8127. }
  8128. $transform(a, b, c, d, tx, ty) {
  8129. }
  8130. get lineJoin() {
  8131. return '';
  8132. }
  8133. set lineJoin(value) {
  8134. }
  8135. get lineCap() {
  8136. return '';
  8137. }
  8138. set lineCap(value) {
  8139. }
  8140. get miterLimit() {
  8141. return '';
  8142. }
  8143. set miterLimit(value) {
  8144. }
  8145. clearRect(x, y, width, height) {
  8146. }
  8147. _drawRect(x, y, width, height, style) {
  8148. Stat.renderBatches++;
  8149. style && (this.fillStyle = style);
  8150. this.fillRect(x, y, width, height, null);
  8151. }
  8152. drawTexture2(x, y, pivotX, pivotY, m, args2) {
  8153. }
  8154. transformByMatrix(matrix, tx, ty) {
  8155. this.transform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx + tx, matrix.ty + ty);
  8156. }
  8157. saveTransform(matrix) {
  8158. this.save();
  8159. }
  8160. restoreTransform(matrix) {
  8161. this.restore();
  8162. }
  8163. drawRect(x, y, width, height, fillColor, lineColor, lineWidth) {
  8164. var ctx = this;
  8165. if (fillColor != null) {
  8166. ctx.fillStyle = fillColor;
  8167. ctx.fillRect(x, y, width, height);
  8168. }
  8169. if (lineColor != null) {
  8170. ctx.strokeStyle = lineColor;
  8171. ctx.lineWidth = lineWidth;
  8172. ctx.strokeRect(x, y, width, height);
  8173. }
  8174. }
  8175. alpha(value) {
  8176. this.globalAlpha *= value;
  8177. }
  8178. _transform(mat, pivotX, pivotY) {
  8179. this.translate(pivotX, pivotY);
  8180. this.transform(mat.a, mat.b, mat.c, mat.d, mat.tx, mat.ty);
  8181. this.translate(-pivotX, -pivotY);
  8182. }
  8183. _rotate(angle, pivotX, pivotY) {
  8184. this.translate(pivotX, pivotY);
  8185. this.rotate(angle);
  8186. this.translate(-pivotX, -pivotY);
  8187. }
  8188. _scale(scaleX, scaleY, pivotX, pivotY) {
  8189. this.translate(pivotX, pivotY);
  8190. this.scale(scaleX, scaleY);
  8191. this.translate(-pivotX, -pivotY);
  8192. }
  8193. _drawLine(x, y, fromX, fromY, toX, toY, lineColor, lineWidth, vid) {
  8194. this.beginPath();
  8195. this.strokeStyle = lineColor;
  8196. this.lineWidth = lineWidth;
  8197. this.moveTo(x + fromX, y + fromY);
  8198. this.lineTo(x + toX, y + toY);
  8199. this.stroke();
  8200. }
  8201. _drawLines(x, y, points, lineColor, lineWidth, vid) {
  8202. this.beginPath();
  8203. this.strokeStyle = lineColor;
  8204. this.lineWidth = lineWidth;
  8205. this.addPath(points.slice(), false, false, x, y);
  8206. this.stroke();
  8207. }
  8208. drawCurves(x, y, points, lineColor, lineWidth) {
  8209. this.beginPath();
  8210. this.strokeStyle = lineColor;
  8211. this.lineWidth = lineWidth;
  8212. this.moveTo(x + points[0], y + points[1]);
  8213. var i = 2, n = points.length;
  8214. while (i < n) {
  8215. this.quadraticCurveTo(x + points[i++], y + points[i++], x + points[i++], y + points[i++]);
  8216. }
  8217. this.stroke();
  8218. }
  8219. _fillAndStroke(fillColor, strokeColor, lineWidth, isConvexPolygon = false) {
  8220. if (fillColor != null) {
  8221. this.fillStyle = fillColor;
  8222. this.fill();
  8223. }
  8224. if (strokeColor != null && lineWidth > 0) {
  8225. this.strokeStyle = strokeColor;
  8226. this.lineWidth = lineWidth;
  8227. this.stroke();
  8228. }
  8229. }
  8230. _drawCircle(x, y, radius, fillColor, lineColor, lineWidth, vid) {
  8231. Stat.renderBatches++;
  8232. this.beginPath(true);
  8233. this.arc(x, y, radius, 0, Context.PI2);
  8234. this.closePath();
  8235. this._fillAndStroke(fillColor, lineColor, lineWidth);
  8236. }
  8237. _drawPie(x, y, radius, startAngle, endAngle, fillColor, lineColor, lineWidth, vid) {
  8238. this.beginPath();
  8239. this.moveTo(x, y);
  8240. this.arc(x, y, radius, startAngle, endAngle);
  8241. this.closePath();
  8242. this._fillAndStroke(fillColor, lineColor, lineWidth);
  8243. }
  8244. _drawPoly(x, y, points, fillColor, lineColor, lineWidth, isConvexPolygon, vid) {
  8245. this.beginPath();
  8246. this.addPath(points.slice(), true, isConvexPolygon, x, y);
  8247. this.closePath();
  8248. this._fillAndStroke(fillColor, lineColor, lineWidth, isConvexPolygon);
  8249. }
  8250. _drawPath(x, y, paths, brush, pen) {
  8251. this.beginPath();
  8252. for (var i = 0, n = paths.length; i < n; i++) {
  8253. var path = paths[i];
  8254. switch (path[0]) {
  8255. case "moveTo":
  8256. this.moveTo(x + path[1], y + path[2]);
  8257. break;
  8258. case "lineTo":
  8259. this.lineTo(x + path[1], y + path[2]);
  8260. break;
  8261. case "arcTo":
  8262. this.arcTo(x + path[1], y + path[2], x + path[3], y + path[4], path[5]);
  8263. break;
  8264. case "closePath":
  8265. this.closePath();
  8266. break;
  8267. }
  8268. }
  8269. if (brush != null) {
  8270. this.fillStyle = brush.fillStyle;
  8271. this.fill();
  8272. }
  8273. if (pen != null) {
  8274. this.strokeStyle = pen.strokeStyle;
  8275. this.lineWidth = pen.lineWidth || 1;
  8276. this.lineJoin = pen.lineJoin;
  8277. this.lineCap = pen.lineCap;
  8278. this.miterLimit = pen.miterLimit;
  8279. this.stroke();
  8280. }
  8281. }
  8282. static set2DRenderConfig() {
  8283. var gl = LayaGL.instance;
  8284. WebGLContext.setBlend(gl, true);
  8285. WebGLContext.setBlendEquation(gl, gl.FUNC_ADD);
  8286. BlendMode.activeBlendFunction = null;
  8287. WebGLContext.setBlendFunc(gl, gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
  8288. WebGLContext.setDepthTest(gl, false);
  8289. WebGLContext.setCullFace(gl, false);
  8290. WebGLContext.setDepthMask(gl, true);
  8291. WebGLContext.setFrontFace(gl, gl.CCW);
  8292. gl.viewport(0, 0, RenderState2D.width, RenderState2D.height);
  8293. }
  8294. clearBG(r, g, b, a) {
  8295. var gl = WebGLContext.mainContext;
  8296. gl.clearColor(r, g, b, a);
  8297. gl.clear(gl.COLOR_BUFFER_BIT);
  8298. }
  8299. _getSubmits() {
  8300. return this._submits;
  8301. }
  8302. _releaseMem(keepRT = false) {
  8303. if (!this._submits)
  8304. return;
  8305. this._curMat && this._curMat.destroy();
  8306. this._curMat = null;
  8307. this._shader2D.destroy();
  8308. this._shader2D = null;
  8309. this._charSubmitCache.clear();
  8310. for (var i = 0, n = this._submits._length; i < n; i++) {
  8311. this._submits[i].releaseRender();
  8312. }
  8313. this._submits.length = 0;
  8314. this._submits._length = 0;
  8315. this._submits = null;
  8316. this._curSubmit = null;
  8317. this._path = null;
  8318. this._save = null;
  8319. var sz;
  8320. for (i = 0, sz = this.meshlist.length; i < sz; i++) {
  8321. var curm = this.meshlist[i];
  8322. curm.destroy();
  8323. }
  8324. this.meshlist.length = 0;
  8325. this.sprite = null;
  8326. if (!keepRT) {
  8327. this._targets && (this._targets.destroy());
  8328. this._targets = null;
  8329. }
  8330. }
  8331. destroy(keepRT = false) {
  8332. --Context._contextcount;
  8333. this.sprite = null;
  8334. this._releaseMem(keepRT);
  8335. this._charSubmitCache.destroy();
  8336. this._mesh.destroy();
  8337. if (!keepRT) {
  8338. this._targets && this._targets.destroy();
  8339. this._targets = null;
  8340. }
  8341. }
  8342. clear() {
  8343. if (!this._submits) {
  8344. this._other = ContextParams.DEFAULT;
  8345. this._curMat = Matrix.create();
  8346. this._charSubmitCache = new CharSubmitCache();
  8347. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  8348. this.meshlist.push(this._mesh);
  8349. this._pathMesh = MeshVG.getAMesh(this.isMain);
  8350. this.meshlist.push(this._pathMesh);
  8351. this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  8352. this.meshlist.push(this._triangleMesh);
  8353. this._submits = [];
  8354. this._save = [SaveMark.Create(this)];
  8355. this._save.length = 10;
  8356. this._shader2D = new Shader2D();
  8357. }
  8358. this._submitKey.clear();
  8359. this._mesh.clearVB();
  8360. this._drawCount = 1;
  8361. this._other = ContextParams.DEFAULT;
  8362. this._other.lineWidth = this._shader2D.ALPHA = 1.0;
  8363. this._nBlendType = 0;
  8364. this._clipRect = Context.MAXCLIPRECT;
  8365. this._curSubmit = SubmitBase.RENDERBASE;
  8366. SubmitBase.RENDERBASE._ref = 0xFFFFFF;
  8367. SubmitBase.RENDERBASE._numEle = 0;
  8368. this._shader2D.fillStyle = this._shader2D.strokeStyle = DrawStyle.DEFAULT;
  8369. for (var i = 0, n = this._submits._length; i < n; i++)
  8370. this._submits[i].releaseRender();
  8371. this._submits._length = 0;
  8372. this._curMat.identity();
  8373. this._other.clear();
  8374. this._saveMark = this._save[0];
  8375. this._save._length = 1;
  8376. }
  8377. size(w, h) {
  8378. if (this._width != w || this._height != h) {
  8379. this._width = w;
  8380. this._height = h;
  8381. if (this._targets) {
  8382. this._targets.destroy();
  8383. this._targets = new RenderTexture2D(w, h, exports.RenderTextureFormat.R8G8B8A8, -1);
  8384. }
  8385. if (this.isMain) {
  8386. WebGLContext.mainContext.viewport(0, 0, w, h);
  8387. RenderState2D.width = w;
  8388. RenderState2D.height = h;
  8389. }
  8390. }
  8391. if (w === 0 && h === 0)
  8392. this._releaseMem();
  8393. }
  8394. set asBitmap(value) {
  8395. if (value) {
  8396. let rt = this._targets;
  8397. if (!this._width || !this._height)
  8398. throw Error("asBitmap no size!");
  8399. if (!rt || rt.width != this._width || rt.height != this._height) {
  8400. this._targets = new RenderTexture2D(this._width, this._height, exports.RenderTextureFormat.R8G8B8A8, -1);
  8401. }
  8402. }
  8403. else {
  8404. this._targets && this._targets.destroy();
  8405. this._targets = null;
  8406. }
  8407. }
  8408. getMatScaleX() {
  8409. if (this._lastMat_a == this._curMat.a && this._lastMat_b == this._curMat.b)
  8410. return this._lastMatScaleX;
  8411. this._lastMatScaleX = this._curMat.getScaleX();
  8412. this._lastMat_a = this._curMat.a;
  8413. this._lastMat_b = this._curMat.b;
  8414. return this._lastMatScaleX;
  8415. }
  8416. getMatScaleY() {
  8417. if (this._lastMat_c == this._curMat.c && this._lastMat_d == this._curMat.d)
  8418. return this._lastMatScaleY;
  8419. this._lastMatScaleY = this._curMat.getScaleY();
  8420. this._lastMat_c = this._curMat.c;
  8421. this._lastMat_d = this._curMat.d;
  8422. return this._lastMatScaleY;
  8423. }
  8424. setFillColor(color) {
  8425. this._fillColor = color;
  8426. }
  8427. getFillColor() {
  8428. return this._fillColor;
  8429. }
  8430. set fillStyle(value) {
  8431. if (!this._shader2D.fillStyle.equal(value)) {
  8432. SaveBase.save(this, SaveBase.TYPE_FILESTYLE, this._shader2D, false);
  8433. this._shader2D.fillStyle = DrawStyle.create(value);
  8434. this._submitKey.other = -this._shader2D.fillStyle.toInt();
  8435. }
  8436. }
  8437. get fillStyle() {
  8438. return this._shader2D.fillStyle;
  8439. }
  8440. set globalAlpha(value) {
  8441. value = Math.floor(value * 1000) / 1000;
  8442. if (value != this._shader2D.ALPHA) {
  8443. SaveBase.save(this, SaveBase.TYPE_ALPHA, this._shader2D, false);
  8444. this._shader2D.ALPHA = value;
  8445. }
  8446. }
  8447. get globalAlpha() {
  8448. return this._shader2D.ALPHA;
  8449. }
  8450. set textAlign(value) {
  8451. (this._other.textAlign === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_TEXTALIGN, this._other, false), this._other.textAlign = value);
  8452. }
  8453. get textAlign() {
  8454. return this._other.textAlign;
  8455. }
  8456. set textBaseline(value) {
  8457. (this._other.textBaseline === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_TEXTBASELINE, this._other, false), this._other.textBaseline = value);
  8458. }
  8459. get textBaseline() {
  8460. return this._other.textBaseline;
  8461. }
  8462. set globalCompositeOperation(value) {
  8463. var n = BlendMode.TOINT[value];
  8464. n == null || (this._nBlendType === n) || (SaveBase.save(this, SaveBase.TYPE_GLOBALCOMPOSITEOPERATION, this, true), this._curSubmit = SubmitBase.RENDERBASE, this._nBlendType = n);
  8465. }
  8466. get globalCompositeOperation() {
  8467. return BlendMode.NAMES[this._nBlendType];
  8468. }
  8469. set strokeStyle(value) {
  8470. 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());
  8471. }
  8472. get strokeStyle() {
  8473. return this._shader2D.strokeStyle;
  8474. }
  8475. translate(x, y) {
  8476. if (x !== 0 || y !== 0) {
  8477. SaveTranslate.save(this);
  8478. if (this._curMat._bTransform) {
  8479. SaveTransform.save(this);
  8480. this._curMat.tx += (x * this._curMat.a + y * this._curMat.c);
  8481. this._curMat.ty += (x * this._curMat.b + y * this._curMat.d);
  8482. }
  8483. else {
  8484. this._curMat.tx = x;
  8485. this._curMat.ty = y;
  8486. }
  8487. }
  8488. }
  8489. set lineWidth(value) {
  8490. (this._other.lineWidth === value) || (this._other = this._other.make(), SaveBase.save(this, SaveBase.TYPE_LINEWIDTH, this._other, false), this._other.lineWidth = value);
  8491. }
  8492. get lineWidth() {
  8493. return this._other.lineWidth;
  8494. }
  8495. save() {
  8496. this._save[this._save._length++] = SaveMark.Create(this);
  8497. }
  8498. restore() {
  8499. var sz = this._save._length;
  8500. var lastBlend = this._nBlendType;
  8501. if (sz < 1)
  8502. return;
  8503. for (var i = sz - 1; i >= 0; i--) {
  8504. var o = this._save[i];
  8505. o.restore(this);
  8506. if (o.isSaveMark()) {
  8507. this._save._length = i;
  8508. return;
  8509. }
  8510. }
  8511. if (lastBlend != this._nBlendType) {
  8512. this._curSubmit = SubmitBase.RENDERBASE;
  8513. }
  8514. }
  8515. set font(str) {
  8516. this._other = this._other.make();
  8517. SaveBase.save(this, SaveBase.TYPE_FONT, this._other, false);
  8518. }
  8519. fillText(txt, x, y, fontStr, color, align, lineWidth = 0, borderColor = "") {
  8520. Context._textRender.filltext(this, txt, x, y, fontStr, color, borderColor, lineWidth, align);
  8521. }
  8522. drawText(text, x, y, font, color, textAlign) {
  8523. Context._textRender.filltext(this, text, x, y, font, color, null, 0, textAlign);
  8524. }
  8525. fillWords(words, x, y, fontStr, color) {
  8526. Context._textRender.fillWords(this, words, x, y, fontStr, color, null, 0);
  8527. }
  8528. strokeWord(text, x, y, font, color, lineWidth, textAlign) {
  8529. Context._textRender.filltext(this, text, x, y, font, null, color, lineWidth, textAlign);
  8530. }
  8531. fillBorderText(txt, x, y, font, color, borderColor, lineWidth, textAlign) {
  8532. Context._textRender.filltext(this, txt, x, y, font, color, borderColor, lineWidth, textAlign);
  8533. }
  8534. fillBorderWords(words, x, y, font, color, borderColor, lineWidth) {
  8535. Context._textRender.fillWords(this, words, x, y, font, color, borderColor, lineWidth);
  8536. }
  8537. _fast_filltext(data, x, y, fontObj, color, strokeColor, lineWidth, textAlign, underLine = 0) {
  8538. Context._textRender._fast_filltext(this, data, null, x, y, fontObj, color, strokeColor, lineWidth, textAlign, underLine);
  8539. }
  8540. _fillRect(x, y, width, height, rgba) {
  8541. var submit = this._curSubmit;
  8542. var sameKey = submit && (submit._key.submitType === SubmitBase.KEY_DRAWTEXTURE && submit._key.blendShader === this._nBlendType);
  8543. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  8544. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  8545. this.meshlist.push(this._mesh);
  8546. sameKey = false;
  8547. }
  8548. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  8549. this.transformQuad(x, y, width, height, 0, this._curMat, this._transedPoints);
  8550. if (!this.clipedOff(this._transedPoints)) {
  8551. this._mesh.addQuad(this._transedPoints, Texture.NO_UV, rgba, false);
  8552. if (!sameKey) {
  8553. submit = this._curSubmit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  8554. this._submits[this._submits._length++] = submit;
  8555. this._copyClipInfo(submit, this._globalClipMatrix);
  8556. submit.shaderValue.textureHost = this._lastTex;
  8557. submit._key.other = (this._lastTex && this._lastTex.bitmap) ? this._lastTex.bitmap.id : -1;
  8558. submit._renderType = SubmitBase.TYPE_TEXTURE;
  8559. }
  8560. this._curSubmit._numEle += 6;
  8561. this._mesh.indexNum += 6;
  8562. this._mesh.vertNum += 4;
  8563. }
  8564. }
  8565. fillRect(x, y, width, height, fillStyle) {
  8566. var drawstyle = fillStyle ? DrawStyle.create(fillStyle) : this._shader2D.fillStyle;
  8567. var rgba = this.mixRGBandAlpha(drawstyle.toInt());
  8568. this._fillRect(x, y, width, height, rgba);
  8569. }
  8570. fillTexture(texture, x, y, width, height, type, offset, other) {
  8571. if (!texture._getSource()) {
  8572. this.sprite && ILaya.systemTimer.callLater(this, this._repaintSprite);
  8573. return;
  8574. }
  8575. this._fillTexture(texture, texture.width, texture.height, texture.uvrect, x, y, width, height, type, offset.x, offset.y);
  8576. }
  8577. _fillTexture(texture, texw, texh, texuvRect, x, y, width, height, type, offsetx, offsety) {
  8578. var submit = this._curSubmit;
  8579. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  8580. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  8581. this.meshlist.push(this._mesh);
  8582. }
  8583. var repeatx = true;
  8584. var repeaty = true;
  8585. switch (type) {
  8586. case "repeat": break;
  8587. case "repeat-x":
  8588. repeaty = false;
  8589. break;
  8590. case "repeat-y":
  8591. repeatx = false;
  8592. break;
  8593. case "no-repeat":
  8594. repeatx = repeaty = false;
  8595. break;
  8596. default: break;
  8597. }
  8598. var uv = this._temp4Points;
  8599. var stu = 0;
  8600. var stv = 0;
  8601. var stx = 0, sty = 0, edx = 0, edy = 0;
  8602. if (offsetx < 0) {
  8603. stx = x;
  8604. stu = (-offsetx % texw) / texw;
  8605. }
  8606. else {
  8607. stx = x + offsetx;
  8608. }
  8609. if (offsety < 0) {
  8610. sty = y;
  8611. stv = (-offsety % texh) / texh;
  8612. }
  8613. else {
  8614. sty = y + offsety;
  8615. }
  8616. edx = x + width;
  8617. edy = y + height;
  8618. (!repeatx) && (edx = Math.min(edx, x + offsetx + texw));
  8619. (!repeaty) && (edy = Math.min(edy, y + offsety + texh));
  8620. if (edx < x || edy < y)
  8621. return;
  8622. if (stx > edx || sty > edy)
  8623. return;
  8624. var edu = (edx - x - offsetx) / texw;
  8625. var edv = (edy - y - offsety) / texh;
  8626. this.transformQuad(stx, sty, edx - stx, edy - sty, 0, this._curMat, this._transedPoints);
  8627. uv[0] = stu;
  8628. uv[1] = stv;
  8629. uv[2] = edu;
  8630. uv[3] = stv;
  8631. uv[4] = edu;
  8632. uv[5] = edv;
  8633. uv[6] = stu;
  8634. uv[7] = edv;
  8635. if (!this.clipedOff(this._transedPoints)) {
  8636. var rgba = this._mixRGBandAlpha(0xffffffff, this._shader2D.ALPHA);
  8637. this._mesh.addQuad(this._transedPoints, uv, rgba, true);
  8638. var sv = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  8639. sv.defines.add(ShaderDefines2D.FILLTEXTURE);
  8640. sv.u_TexRange = texuvRect.concat();
  8641. submit = this._curSubmit = SubmitTexture.create(this, this._mesh, sv);
  8642. this._submits[this._submits._length++] = submit;
  8643. this._copyClipInfo(submit, this._globalClipMatrix);
  8644. submit.shaderValue.textureHost = texture;
  8645. submit._renderType = SubmitBase.TYPE_TEXTURE;
  8646. this._curSubmit._numEle += 6;
  8647. this._mesh.indexNum += 6;
  8648. this._mesh.vertNum += 4;
  8649. }
  8650. this.breakNextMerge();
  8651. }
  8652. setColorFilter(filter) {
  8653. SaveBase.save(this, SaveBase.TYPE_COLORFILTER, this, true);
  8654. this._colorFiler = filter;
  8655. this._curSubmit = SubmitBase.RENDERBASE;
  8656. }
  8657. drawTexture(tex, x, y, width, height) {
  8658. this._drawTextureM(tex, x, y, width, height, null, 1, null);
  8659. }
  8660. drawTextures(tex, pos, tx, ty) {
  8661. if (!tex._getSource()) {
  8662. this.sprite && ILaya.systemTimer.callLater(this, this._repaintSprite);
  8663. return;
  8664. }
  8665. var n = pos.length / 2;
  8666. var ipos = 0;
  8667. var bmpid = tex.bitmap.id;
  8668. for (var i = 0; i < n; i++) {
  8669. this._inner_drawTexture(tex, bmpid, pos[ipos++] + tx, pos[ipos++] + ty, 0, 0, null, null, 1.0, false);
  8670. }
  8671. }
  8672. _drawTextureAddSubmit(imgid, tex) {
  8673. var submit = null;
  8674. submit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  8675. this._submits[this._submits._length++] = submit;
  8676. submit.shaderValue.textureHost = tex;
  8677. submit._key.other = imgid;
  8678. submit._renderType = SubmitBase.TYPE_TEXTURE;
  8679. this._curSubmit = submit;
  8680. }
  8681. _drawTextureM(tex, x, y, width, height, m, alpha, uv) {
  8682. var cs = this.sprite;
  8683. if (!tex._getSource(function () {
  8684. if (cs) {
  8685. cs.repaint();
  8686. }
  8687. })) {
  8688. return false;
  8689. }
  8690. return this._inner_drawTexture(tex, tex.bitmap.id, x, y, width, height, m, uv, alpha, false);
  8691. }
  8692. _drawRenderTexture(tex, x, y, width, height, m, alpha, uv) {
  8693. return this._inner_drawTexture(tex, -1, x, y, width, height, m, uv, 1.0, false);
  8694. }
  8695. submitDebugger() {
  8696. this._submits[this._submits._length++] = SubmitCMD.create([], function () { debugger; }, this);
  8697. }
  8698. _copyClipInfo(submit, clipInfo) {
  8699. var cm = submit.shaderValue.clipMatDir;
  8700. cm[0] = clipInfo.a;
  8701. cm[1] = clipInfo.b;
  8702. cm[2] = clipInfo.c;
  8703. cm[3] = clipInfo.d;
  8704. var cmp = submit.shaderValue.clipMatPos;
  8705. cmp[0] = clipInfo.tx;
  8706. cmp[1] = clipInfo.ty;
  8707. submit.clipInfoID = this._clipInfoID;
  8708. if (this._clipInCache) {
  8709. submit.shaderValue.clipOff[0] = 1;
  8710. }
  8711. }
  8712. isSameClipInfo(submit) {
  8713. return (submit.clipInfoID === this._clipInfoID);
  8714. }
  8715. _useNewTex2DSubmit(tex, minVertNum) {
  8716. if (this._mesh.vertNum + minVertNum > Context._MAXVERTNUM) {
  8717. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  8718. this.meshlist.push(this._mesh);
  8719. }
  8720. var submit = SubmitTexture.create(this, this._mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  8721. this._submits[this._submits._length++] = this._curSubmit = submit;
  8722. submit.shaderValue.textureHost = tex;
  8723. this._copyClipInfo(submit, this._globalClipMatrix);
  8724. }
  8725. _drawTexRect(x, y, w, h, uv) {
  8726. this.transformQuad(x, y, w, h, this._italicDeg, this._curMat, this._transedPoints);
  8727. var ops = this._transedPoints;
  8728. ops[0] = (ops[0] + 0.5) | 0;
  8729. ops[1] = (ops[1] + 0.5) | 0;
  8730. ops[2] = (ops[2] + 0.5) | 0;
  8731. ops[3] = (ops[3] + 0.5) | 0;
  8732. ops[4] = (ops[4] + 0.5) | 0;
  8733. ops[5] = (ops[5] + 0.5) | 0;
  8734. ops[6] = (ops[6] + 0.5) | 0;
  8735. ops[7] = (ops[7] + 0.5) | 0;
  8736. if (!this.clipedOff(this._transedPoints)) {
  8737. this._mesh.addQuad(this._transedPoints, uv, this._fillColor, true);
  8738. this._curSubmit._numEle += 6;
  8739. this._mesh.indexNum += 6;
  8740. this._mesh.vertNum += 4;
  8741. }
  8742. }
  8743. drawCallOptimize(enable) {
  8744. this._charSubmitCache.enable(enable, this);
  8745. return enable;
  8746. }
  8747. _inner_drawTexture(tex, imgid, x, y, width, height, m, uv, alpha, lastRender) {
  8748. var preKey = this._curSubmit._key;
  8749. uv = uv || tex._uv;
  8750. if (preKey.submitType === SubmitBase.KEY_TRIANGLES && preKey.other === imgid) {
  8751. var tv = this._drawTexToDrawTri_Vert;
  8752. tv[0] = x;
  8753. tv[1] = y;
  8754. tv[2] = x + width, tv[3] = y, tv[4] = x + width, tv[5] = y + height, tv[6] = x, tv[7] = y + height;
  8755. this._drawTriUseAbsMatrix = true;
  8756. var tuv = this._tempUV;
  8757. tuv[0] = uv[0];
  8758. tuv[1] = uv[1];
  8759. tuv[2] = uv[2];
  8760. tuv[3] = uv[3];
  8761. tuv[4] = uv[4];
  8762. tuv[5] = uv[5];
  8763. tuv[6] = uv[6];
  8764. tuv[7] = uv[7];
  8765. this.drawTriangles(tex, 0, 0, tv, tuv, this._drawTexToDrawTri_Index, m, alpha, null, null);
  8766. this._drawTriUseAbsMatrix = false;
  8767. return true;
  8768. }
  8769. var mesh = this._mesh;
  8770. var submit = this._curSubmit;
  8771. var ops = lastRender ? this._charSubmitCache.getPos() : this._transedPoints;
  8772. this.transformQuad(x, y, width || tex.width, height || tex.height, this._italicDeg, m || this._curMat, ops);
  8773. if (this.drawTexAlign) {
  8774. var round = Math.round;
  8775. ops[0] = round(ops[0]);
  8776. ops[1] = round(ops[1]);
  8777. ops[2] = round(ops[2]);
  8778. ops[3] = round(ops[3]);
  8779. ops[4] = round(ops[4]);
  8780. ops[5] = round(ops[5]);
  8781. ops[6] = round(ops[6]);
  8782. ops[7] = round(ops[7]);
  8783. this.drawTexAlign = false;
  8784. }
  8785. var rgba = this._mixRGBandAlpha(0xffffffff, this._shader2D.ALPHA * alpha);
  8786. if (lastRender) {
  8787. this._charSubmitCache.add(this, tex, imgid, ops, uv, rgba);
  8788. return true;
  8789. }
  8790. this._drawCount++;
  8791. var sameKey = imgid >= 0 && preKey.submitType === SubmitBase.KEY_DRAWTEXTURE && preKey.other === imgid;
  8792. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  8793. this._lastTex = tex;
  8794. if (mesh.vertNum + 4 > Context._MAXVERTNUM) {
  8795. mesh = this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  8796. this.meshlist.push(mesh);
  8797. sameKey = false;
  8798. }
  8799. {
  8800. mesh.addQuad(ops, uv, rgba, true);
  8801. if (!sameKey) {
  8802. this._submits[this._submits._length++] = this._curSubmit = submit = SubmitTexture.create(this, mesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  8803. submit.shaderValue.textureHost = tex;
  8804. submit._key.other = imgid;
  8805. this._copyClipInfo(submit, this._globalClipMatrix);
  8806. }
  8807. submit._numEle += 6;
  8808. mesh.indexNum += 6;
  8809. mesh.vertNum += 4;
  8810. return true;
  8811. }
  8812. return false;
  8813. }
  8814. transform4Points(a, m, out) {
  8815. var tx = m.tx;
  8816. var ty = m.ty;
  8817. var ma = m.a;
  8818. var mb = m.b;
  8819. var mc = m.c;
  8820. var md = m.d;
  8821. var a0 = a[0];
  8822. var a1 = a[1];
  8823. var a2 = a[2];
  8824. var a3 = a[3];
  8825. var a4 = a[4];
  8826. var a5 = a[5];
  8827. var a6 = a[6];
  8828. var a7 = a[7];
  8829. if (m._bTransform) {
  8830. out[0] = a0 * ma + a1 * mc + tx;
  8831. out[1] = a0 * mb + a1 * md + ty;
  8832. out[2] = a2 * ma + a3 * mc + tx;
  8833. out[3] = a2 * mb + a3 * md + ty;
  8834. out[4] = a4 * ma + a5 * mc + tx;
  8835. out[5] = a4 * mb + a5 * md + ty;
  8836. out[6] = a6 * ma + a7 * mc + tx;
  8837. out[7] = a6 * mb + a7 * md + ty;
  8838. }
  8839. else {
  8840. out[0] = a0 + tx;
  8841. out[1] = a1 + ty;
  8842. out[2] = a2 + tx;
  8843. out[3] = a3 + ty;
  8844. out[4] = a4 + tx;
  8845. out[5] = a5 + ty;
  8846. out[6] = a6 + tx;
  8847. out[7] = a7 + ty;
  8848. }
  8849. }
  8850. clipedOff(pt) {
  8851. if (this._clipRect.width <= 0 || this._clipRect.height <= 0)
  8852. return true;
  8853. return false;
  8854. }
  8855. transformQuad(x, y, w, h, italicDeg, m, out) {
  8856. var xoff = 0;
  8857. if (italicDeg != 0) {
  8858. xoff = Math.tan(italicDeg * Math.PI / 180) * h;
  8859. }
  8860. var maxx = x + w;
  8861. var maxy = y + h;
  8862. var tx = m.tx;
  8863. var ty = m.ty;
  8864. var ma = m.a;
  8865. var mb = m.b;
  8866. var mc = m.c;
  8867. var md = m.d;
  8868. var a0 = x + xoff;
  8869. var a1 = y;
  8870. var a2 = maxx + xoff;
  8871. var a3 = y;
  8872. var a4 = maxx;
  8873. var a5 = maxy;
  8874. var a6 = x;
  8875. var a7 = maxy;
  8876. if (m._bTransform) {
  8877. out[0] = a0 * ma + a1 * mc + tx;
  8878. out[1] = a0 * mb + a1 * md + ty;
  8879. out[2] = a2 * ma + a3 * mc + tx;
  8880. out[3] = a2 * mb + a3 * md + ty;
  8881. out[4] = a4 * ma + a5 * mc + tx;
  8882. out[5] = a4 * mb + a5 * md + ty;
  8883. out[6] = a6 * ma + a7 * mc + tx;
  8884. out[7] = a6 * mb + a7 * md + ty;
  8885. }
  8886. else {
  8887. out[0] = a0 + tx;
  8888. out[1] = a1 + ty;
  8889. out[2] = a2 + tx;
  8890. out[3] = a3 + ty;
  8891. out[4] = a4 + tx;
  8892. out[5] = a5 + ty;
  8893. out[6] = a6 + tx;
  8894. out[7] = a7 + ty;
  8895. }
  8896. }
  8897. pushRT() {
  8898. this.addRenderObject(SubmitCMD.create(null, RenderTexture2D.pushRT, this));
  8899. }
  8900. popRT() {
  8901. this.addRenderObject(SubmitCMD.create(null, RenderTexture2D.popRT, this));
  8902. this.breakNextMerge();
  8903. }
  8904. useRT(rt) {
  8905. function _use(rt) {
  8906. if (!rt) {
  8907. throw 'error useRT';
  8908. }
  8909. else {
  8910. rt.start();
  8911. rt.clear(0, 0, 0, 0);
  8912. }
  8913. }
  8914. this.addRenderObject(SubmitCMD.create([rt], _use, this));
  8915. this.breakNextMerge();
  8916. }
  8917. RTRestore(rt) {
  8918. function _restore(rt) {
  8919. rt.restore();
  8920. }
  8921. this.addRenderObject(SubmitCMD.create([rt], _restore, this));
  8922. this.breakNextMerge();
  8923. }
  8924. breakNextMerge() {
  8925. this._curSubmit = SubmitBase.RENDERBASE;
  8926. }
  8927. _repaintSprite() {
  8928. this.sprite && this.sprite.repaint();
  8929. }
  8930. drawTextureWithTransform(tex, x, y, width, height, transform, tx, ty, alpha, blendMode, colorfilter = null, uv) {
  8931. var oldcomp;
  8932. var curMat = this._curMat;
  8933. if (blendMode) {
  8934. oldcomp = this.globalCompositeOperation;
  8935. this.globalCompositeOperation = blendMode;
  8936. }
  8937. var oldColorFilter = this._colorFiler;
  8938. if (colorfilter) {
  8939. this.setColorFilter(colorfilter);
  8940. }
  8941. if (!transform) {
  8942. this._drawTextureM(tex, x + tx, y + ty, width, height, curMat, alpha, uv);
  8943. if (blendMode) {
  8944. this.globalCompositeOperation = oldcomp;
  8945. }
  8946. if (colorfilter) {
  8947. this.setColorFilter(oldColorFilter);
  8948. }
  8949. return;
  8950. }
  8951. var tmpMat = this._tmpMatrix;
  8952. tmpMat.a = transform.a;
  8953. tmpMat.b = transform.b;
  8954. tmpMat.c = transform.c;
  8955. tmpMat.d = transform.d;
  8956. tmpMat.tx = transform.tx + tx;
  8957. tmpMat.ty = transform.ty + ty;
  8958. tmpMat._bTransform = transform._bTransform;
  8959. if (transform && curMat._bTransform) {
  8960. Matrix.mul(tmpMat, curMat, tmpMat);
  8961. transform = tmpMat;
  8962. transform._bTransform = true;
  8963. }
  8964. else {
  8965. tmpMat.tx += curMat.tx;
  8966. tmpMat.ty += curMat.ty;
  8967. transform = tmpMat;
  8968. }
  8969. this._drawTextureM(tex, x, y, width, height, transform, alpha, uv);
  8970. if (blendMode) {
  8971. this.globalCompositeOperation = oldcomp;
  8972. }
  8973. if (colorfilter) {
  8974. this.setColorFilter(oldColorFilter);
  8975. }
  8976. }
  8977. _flushToTarget(context, target) {
  8978. RenderState2D.worldScissorTest = false;
  8979. var gl = LayaGL.instance;
  8980. gl.disable(gl.SCISSOR_TEST);
  8981. var preAlpha = RenderState2D.worldAlpha;
  8982. var preMatrix4 = RenderState2D.worldMatrix4;
  8983. var preMatrix = RenderState2D.worldMatrix;
  8984. RenderState2D.worldMatrix = Matrix.EMPTY;
  8985. RenderState2D.restoreTempArray();
  8986. RenderState2D.worldMatrix4 = RenderState2D.TEMPMAT4_ARRAY;
  8987. RenderState2D.worldAlpha = 1;
  8988. BaseShader.activeShader = null;
  8989. target.start();
  8990. if (context._submits._length > 0)
  8991. target.clear(0, 0, 0, 0);
  8992. context._curSubmit = SubmitBase.RENDERBASE;
  8993. context.flush();
  8994. context.clear();
  8995. target.restore();
  8996. context._curSubmit = SubmitBase.RENDERBASE;
  8997. BaseShader.activeShader = null;
  8998. RenderState2D.worldAlpha = preAlpha;
  8999. RenderState2D.worldMatrix4 = preMatrix4;
  9000. RenderState2D.worldMatrix = preMatrix;
  9001. }
  9002. drawCanvas(canvas, x, y, width, height) {
  9003. if (!canvas)
  9004. return;
  9005. var src = canvas.context;
  9006. var submit;
  9007. if (src._targets) {
  9008. if (src._submits._length > 0) {
  9009. submit = SubmitCMD.create([src, src._targets], this._flushToTarget, this);
  9010. this._submits[this._submits._length++] = submit;
  9011. }
  9012. this._drawRenderTexture(src._targets, x, y, width, height, null, 1.0, RenderTexture2D.flipyuv);
  9013. this._curSubmit = SubmitBase.RENDERBASE;
  9014. }
  9015. else {
  9016. var canv = canvas;
  9017. if (canv.touches) {
  9018. canv.touches.forEach(function (v) { v.touch(); });
  9019. }
  9020. submit = SubmitCanvas.create(canvas, this._shader2D.ALPHA, this._shader2D.filters);
  9021. this._submits[this._submits._length++] = submit;
  9022. submit._key.clear();
  9023. var mat = submit._matrix;
  9024. this._curMat.copyTo(mat);
  9025. var tx = mat.tx, ty = mat.ty;
  9026. mat.tx = mat.ty = 0;
  9027. mat.transformPoint(Point.TEMP.setTo(x, y));
  9028. mat.translate(Point.TEMP.x + tx, Point.TEMP.y + ty);
  9029. Matrix.mul(canv.invMat, mat, mat);
  9030. this._curSubmit = SubmitBase.RENDERBASE;
  9031. }
  9032. }
  9033. drawTarget(rt, x, y, width, height, m, shaderValue, uv = null, blend = -1) {
  9034. this._drawCount++;
  9035. if (this._mesh.vertNum + 4 > Context._MAXVERTNUM) {
  9036. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9037. this.meshlist.push(this._mesh);
  9038. }
  9039. this.transformQuad(x, y, width, height, 0, m || this._curMat, this._transedPoints);
  9040. if (!this.clipedOff(this._transedPoints)) {
  9041. this._mesh.addQuad(this._transedPoints, uv || Texture.DEF_UV, 0xffffffff, true);
  9042. var submit = this._curSubmit = SubmitTarget.create(this, this._mesh, shaderValue, rt);
  9043. submit.blendType = (blend == -1) ? this._nBlendType : blend;
  9044. this._copyClipInfo(submit, this._globalClipMatrix);
  9045. submit._numEle = 6;
  9046. this._mesh.indexNum += 6;
  9047. this._mesh.vertNum += 4;
  9048. this._submits[this._submits._length++] = submit;
  9049. this._curSubmit = SubmitBase.RENDERBASE;
  9050. return true;
  9051. }
  9052. this._curSubmit = SubmitBase.RENDERBASE;
  9053. return false;
  9054. }
  9055. drawTriangles(tex, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum = 0xffffffff) {
  9056. var oldcomp = null;
  9057. if (blendMode) {
  9058. oldcomp = this.globalCompositeOperation;
  9059. this.globalCompositeOperation = blendMode;
  9060. }
  9061. if (!tex._getSource()) {
  9062. if (this.sprite) {
  9063. ILaya.systemTimer.callLater(this, this._repaintSprite);
  9064. }
  9065. return;
  9066. }
  9067. this._drawCount++;
  9068. var tmpMat = this._tmpMatrix;
  9069. var triMesh = this._triangleMesh;
  9070. var oldColorFilter = null;
  9071. var needRestorFilter = false;
  9072. if (color) {
  9073. oldColorFilter = this._colorFiler;
  9074. this._colorFiler = color;
  9075. this._curSubmit = SubmitBase.RENDERBASE;
  9076. needRestorFilter = oldColorFilter != color;
  9077. }
  9078. var webGLImg = tex.bitmap;
  9079. var preKey = this._curSubmit._key;
  9080. var sameKey = preKey.submitType === SubmitBase.KEY_TRIANGLES && preKey.other === webGLImg.id && preKey.blendShader == this._nBlendType;
  9081. if (triMesh.vertNum + vertices.length / 2 > Context._MAXVERTNUM) {
  9082. triMesh = this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  9083. this.meshlist.push(triMesh);
  9084. sameKey = false;
  9085. }
  9086. if (!sameKey) {
  9087. var submit = this._curSubmit = SubmitTexture.create(this, triMesh, Value2D.create(ShaderDefines2D.TEXTURE2D, 0));
  9088. submit.shaderValue.textureHost = tex;
  9089. submit._renderType = SubmitBase.TYPE_TEXTURE;
  9090. submit._key.submitType = SubmitBase.KEY_TRIANGLES;
  9091. submit._key.other = webGLImg.id;
  9092. this._copyClipInfo(submit, this._globalClipMatrix);
  9093. this._submits[this._submits._length++] = submit;
  9094. }
  9095. var rgba = this._mixRGBandAlpha(colorNum, this._shader2D.ALPHA * alpha);
  9096. if (!this._drawTriUseAbsMatrix) {
  9097. if (!matrix) {
  9098. tmpMat.a = 1;
  9099. tmpMat.b = 0;
  9100. tmpMat.c = 0;
  9101. tmpMat.d = 1;
  9102. tmpMat.tx = x;
  9103. tmpMat.ty = y;
  9104. }
  9105. else {
  9106. tmpMat.a = matrix.a;
  9107. tmpMat.b = matrix.b;
  9108. tmpMat.c = matrix.c;
  9109. tmpMat.d = matrix.d;
  9110. tmpMat.tx = matrix.tx + x;
  9111. tmpMat.ty = matrix.ty + y;
  9112. }
  9113. Matrix.mul(tmpMat, this._curMat, tmpMat);
  9114. triMesh.addData(vertices, uvs, indices, tmpMat || this._curMat, rgba);
  9115. }
  9116. else {
  9117. triMesh.addData(vertices, uvs, indices, matrix, rgba);
  9118. }
  9119. this._curSubmit._numEle += indices.length;
  9120. if (needRestorFilter) {
  9121. this._colorFiler = oldColorFilter;
  9122. this._curSubmit = SubmitBase.RENDERBASE;
  9123. }
  9124. if (blendMode) {
  9125. this.globalCompositeOperation = oldcomp;
  9126. }
  9127. }
  9128. transform(a, b, c, d, tx, ty) {
  9129. SaveTransform.save(this);
  9130. Matrix.mul(Matrix.TEMP.setTo(a, b, c, d, tx, ty), this._curMat, this._curMat);
  9131. this._curMat._checkTransform();
  9132. }
  9133. _transformByMatrix(matrix, tx, ty) {
  9134. matrix.setTranslate(tx, ty);
  9135. Matrix.mul(matrix, this._curMat, this._curMat);
  9136. matrix.setTranslate(0, 0);
  9137. this._curMat._bTransform = true;
  9138. }
  9139. setTransformByMatrix(value) {
  9140. value.copyTo(this._curMat);
  9141. }
  9142. rotate(angle) {
  9143. SaveTransform.save(this);
  9144. this._curMat.rotateEx(angle);
  9145. }
  9146. scale(scaleX, scaleY) {
  9147. SaveTransform.save(this);
  9148. this._curMat.scaleEx(scaleX, scaleY);
  9149. }
  9150. clipRect(x, y, width, height) {
  9151. SaveClipRect.save(this);
  9152. if (this._clipRect == Context.MAXCLIPRECT) {
  9153. this._clipRect = new Rectangle(x, y, width, height);
  9154. }
  9155. else {
  9156. this._clipRect.width = width;
  9157. this._clipRect.height = height;
  9158. this._clipRect.x = x;
  9159. this._clipRect.y = y;
  9160. }
  9161. this._clipID_Gen++;
  9162. this._clipID_Gen %= 10000;
  9163. this._clipInfoID = this._clipID_Gen;
  9164. var cm = this._globalClipMatrix;
  9165. var minx = cm.tx;
  9166. var miny = cm.ty;
  9167. var maxx = minx + cm.a;
  9168. var maxy = miny + cm.d;
  9169. if (this._clipRect.width >= Context._MAXSIZE) {
  9170. cm.a = cm.d = Context._MAXSIZE;
  9171. cm.b = cm.c = cm.tx = cm.ty = 0;
  9172. }
  9173. else {
  9174. if (this._curMat._bTransform) {
  9175. cm.tx = this._clipRect.x * this._curMat.a + this._clipRect.y * this._curMat.c + this._curMat.tx;
  9176. cm.ty = this._clipRect.x * this._curMat.b + this._clipRect.y * this._curMat.d + this._curMat.ty;
  9177. cm.a = this._clipRect.width * this._curMat.a;
  9178. cm.b = this._clipRect.width * this._curMat.b;
  9179. cm.c = this._clipRect.height * this._curMat.c;
  9180. cm.d = this._clipRect.height * this._curMat.d;
  9181. }
  9182. else {
  9183. cm.tx = this._clipRect.x + this._curMat.tx;
  9184. cm.ty = this._clipRect.y + this._curMat.ty;
  9185. cm.a = this._clipRect.width;
  9186. cm.b = cm.c = 0;
  9187. cm.d = this._clipRect.height;
  9188. }
  9189. if (this._incache) {
  9190. this._clipInCache = true;
  9191. }
  9192. }
  9193. if (cm.a > 0 && cm.d > 0) {
  9194. var cmaxx = cm.tx + cm.a;
  9195. var cmaxy = cm.ty + cm.d;
  9196. if (cmaxx <= minx || cmaxy <= miny || cm.tx >= maxx || cm.ty >= maxy) {
  9197. cm.a = -0.1;
  9198. cm.d = -0.1;
  9199. }
  9200. else {
  9201. if (cm.tx < minx) {
  9202. cm.a -= (minx - cm.tx);
  9203. cm.tx = minx;
  9204. }
  9205. if (cmaxx > maxx) {
  9206. cm.a -= (cmaxx - maxx);
  9207. }
  9208. if (cm.ty < miny) {
  9209. cm.d -= (miny - cm.ty);
  9210. cm.ty = miny;
  9211. }
  9212. if (cmaxy > maxy) {
  9213. cm.d -= (cmaxy - maxy);
  9214. }
  9215. if (cm.a <= 0)
  9216. cm.a = -0.1;
  9217. if (cm.d <= 0)
  9218. cm.d = -0.1;
  9219. }
  9220. }
  9221. }
  9222. drawMesh(x, y, ib, vb, numElement, mat, shader, shaderValues, startIndex = 0) {
  9223. }
  9224. addRenderObject(o) {
  9225. this._submits[this._submits._length++] = o;
  9226. }
  9227. submitElement(start, end) {
  9228. var mainCtx = this.isMain;
  9229. var renderList = this._submits;
  9230. var ret = renderList._length;
  9231. end < 0 && (end = renderList._length);
  9232. var submit = SubmitBase.RENDERBASE;
  9233. while (start < end) {
  9234. this._renderNextSubmitIndex = start + 1;
  9235. if (renderList[start] === SubmitBase.RENDERBASE) {
  9236. start++;
  9237. continue;
  9238. }
  9239. SubmitBase.preRender = submit;
  9240. submit = renderList[start];
  9241. start += submit.renderSubmit();
  9242. }
  9243. return ret;
  9244. }
  9245. flush() {
  9246. this._clipID_Gen = 0;
  9247. var ret = this.submitElement(0, this._submits._length);
  9248. this._path && this._path.reset();
  9249. SkinMeshBuffer.instance && SkinMeshBuffer.getInstance().reset();
  9250. this._curSubmit = SubmitBase.RENDERBASE;
  9251. for (var i = 0, sz = this.meshlist.length; i < sz; i++) {
  9252. var curm = this.meshlist[i];
  9253. curm.canReuse ? (curm.releaseMesh()) : (curm.destroy());
  9254. }
  9255. this.meshlist.length = 0;
  9256. this._mesh = MeshQuadTexture.getAMesh(this.isMain);
  9257. this._pathMesh = MeshVG.getAMesh(this.isMain);
  9258. this._triangleMesh = MeshTexture.getAMesh(this.isMain);
  9259. this.meshlist.push(this._mesh, this._pathMesh, this._triangleMesh);
  9260. this._flushCnt++;
  9261. if (this._flushCnt % 60 == 0 && this.isMain) {
  9262. if (TextRender.textRenderInst) {
  9263. TextRender.textRenderInst.GC();
  9264. }
  9265. }
  9266. return ret;
  9267. }
  9268. beginPath(convex = false) {
  9269. var tPath = this._getPath();
  9270. tPath.beginPath(convex);
  9271. }
  9272. closePath() {
  9273. this._path.closePath();
  9274. }
  9275. addPath(points, close, convex, dx, dy) {
  9276. var ci = 0;
  9277. for (var i = 0, sz = points.length / 2; i < sz; i++) {
  9278. var x1 = points[ci] + dx, y1 = points[ci + 1] + dy;
  9279. points[ci] = x1;
  9280. points[ci + 1] = y1;
  9281. ci += 2;
  9282. }
  9283. this._getPath().push(points, convex);
  9284. }
  9285. fill() {
  9286. var m = this._curMat;
  9287. var tPath = this._getPath();
  9288. var submit = this._curSubmit;
  9289. var sameKey = (submit._key.submitType === SubmitBase.KEY_VG && submit._key.blendShader === this._nBlendType);
  9290. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  9291. if (!sameKey) {
  9292. this._curSubmit = this.addVGSubmit(this._pathMesh);
  9293. }
  9294. var rgba = this.mixRGBandAlpha(this.fillStyle.toInt());
  9295. var curEleNum = 0;
  9296. var idx;
  9297. for (var i = 0, sz = tPath.paths.length; i < sz; i++) {
  9298. var p = tPath.paths[i];
  9299. var vertNum = p.path.length / 2;
  9300. if (vertNum < 3 || (vertNum == 3 && !p.convex))
  9301. continue;
  9302. var cpath = p.path.concat();
  9303. var pi = 0;
  9304. var xp, yp;
  9305. var _x, _y;
  9306. if (m._bTransform) {
  9307. for (pi = 0; pi < vertNum; pi++) {
  9308. xp = pi << 1;
  9309. yp = xp + 1;
  9310. _x = cpath[xp];
  9311. _y = cpath[yp];
  9312. cpath[xp] = m.a * _x + m.c * _y + m.tx;
  9313. cpath[yp] = m.b * _x + m.d * _y + m.ty;
  9314. }
  9315. }
  9316. else {
  9317. for (pi = 0; pi < vertNum; pi++) {
  9318. xp = pi << 1;
  9319. yp = xp + 1;
  9320. _x = cpath[xp];
  9321. _y = cpath[yp];
  9322. cpath[xp] = _x + m.tx;
  9323. cpath[yp] = _y + m.ty;
  9324. }
  9325. }
  9326. if (this._pathMesh.vertNum + vertNum > Context._MAXVERTNUM) {
  9327. this._curSubmit._numEle += curEleNum;
  9328. curEleNum = 0;
  9329. this._pathMesh = MeshVG.getAMesh(this.isMain);
  9330. this._curSubmit = this.addVGSubmit(this._pathMesh);
  9331. }
  9332. var curvert = this._pathMesh.vertNum;
  9333. if (p.convex) {
  9334. var faceNum = vertNum - 2;
  9335. idx = new Array(faceNum * 3);
  9336. var idxpos = 0;
  9337. for (var fi = 0; fi < faceNum; fi++) {
  9338. idx[idxpos++] = curvert;
  9339. idx[idxpos++] = fi + 1 + curvert;
  9340. idx[idxpos++] = fi + 2 + curvert;
  9341. }
  9342. }
  9343. else {
  9344. idx = Earcut.earcut(cpath, null, 2);
  9345. if (curvert > 0) {
  9346. for (var ii = 0; ii < idx.length; ii++) {
  9347. idx[ii] += curvert;
  9348. }
  9349. }
  9350. }
  9351. this._pathMesh.addVertAndIBToMesh(this, cpath, rgba, idx);
  9352. curEleNum += idx.length;
  9353. }
  9354. this._curSubmit._numEle += curEleNum;
  9355. }
  9356. addVGSubmit(mesh) {
  9357. var submit = Submit.createShape(this, mesh, 0, Value2D.create(ShaderDefines2D.PRIMITIVE, 0));
  9358. submit._key.submitType = SubmitBase.KEY_VG;
  9359. this._submits[this._submits._length++] = submit;
  9360. this._copyClipInfo(submit, this._globalClipMatrix);
  9361. return submit;
  9362. }
  9363. stroke() {
  9364. if (this.lineWidth > 0) {
  9365. var rgba = this.mixRGBandAlpha(this.strokeStyle._color.numColor);
  9366. var tPath = this._getPath();
  9367. var submit = this._curSubmit;
  9368. var sameKey = (submit._key.submitType === SubmitBase.KEY_VG && submit._key.blendShader === this._nBlendType);
  9369. sameKey && (sameKey = sameKey && this.isSameClipInfo(submit));
  9370. if (!sameKey) {
  9371. this._curSubmit = this.addVGSubmit(this._pathMesh);
  9372. }
  9373. var curEleNum = 0;
  9374. for (var i = 0, sz = tPath.paths.length; i < sz; i++) {
  9375. var p = tPath.paths[i];
  9376. if (p.path.length <= 0)
  9377. continue;
  9378. var idx = [];
  9379. var vertex = [];
  9380. var maxVertexNum = p.path.length * 2;
  9381. if (maxVertexNum < 2)
  9382. continue;
  9383. if (this._pathMesh.vertNum + maxVertexNum > Context._MAXVERTNUM) {
  9384. this._curSubmit._numEle += curEleNum;
  9385. curEleNum = 0;
  9386. this._pathMesh = MeshVG.getAMesh(this.isMain);
  9387. this.meshlist.push(this._pathMesh);
  9388. this._curSubmit = this.addVGSubmit(this._pathMesh);
  9389. }
  9390. BasePoly.createLine2(p.path, idx, this.lineWidth, this._pathMesh.vertNum, vertex, p.loop);
  9391. var ptnum = vertex.length / 2;
  9392. var m = this._curMat;
  9393. var pi = 0;
  9394. var xp, yp;
  9395. var _x, _y;
  9396. if (m._bTransform) {
  9397. for (pi = 0; pi < ptnum; pi++) {
  9398. xp = pi << 1;
  9399. yp = xp + 1;
  9400. _x = vertex[xp];
  9401. _y = vertex[yp];
  9402. vertex[xp] = m.a * _x + m.c * _y + m.tx;
  9403. vertex[yp] = m.b * _x + m.d * _y + m.ty;
  9404. }
  9405. }
  9406. else {
  9407. for (pi = 0; pi < ptnum; pi++) {
  9408. xp = pi << 1;
  9409. yp = xp + 1;
  9410. _x = vertex[xp];
  9411. _y = vertex[yp];
  9412. vertex[xp] = _x + m.tx;
  9413. vertex[yp] = _y + m.ty;
  9414. }
  9415. }
  9416. this._pathMesh.addVertAndIBToMesh(this, vertex, rgba, idx);
  9417. curEleNum += idx.length;
  9418. }
  9419. this._curSubmit._numEle += curEleNum;
  9420. }
  9421. }
  9422. moveTo(x, y) {
  9423. var tPath = this._getPath();
  9424. tPath.newPath();
  9425. tPath._lastOriX = x;
  9426. tPath._lastOriY = y;
  9427. tPath.addPoint(x, y);
  9428. }
  9429. lineTo(x, y) {
  9430. var tPath = this._getPath();
  9431. if (Math.abs(x - tPath._lastOriX) < 1e-3 && Math.abs(y - tPath._lastOriY) < 1e-3)
  9432. return;
  9433. tPath._lastOriX = x;
  9434. tPath._lastOriY = y;
  9435. tPath.addPoint(x, y);
  9436. }
  9437. arcTo(x1, y1, x2, y2, r) {
  9438. var i = 0;
  9439. var x = 0, y = 0;
  9440. var dx = this._path._lastOriX - x1;
  9441. var dy = this._path._lastOriY - y1;
  9442. var len1 = Math.sqrt(dx * dx + dy * dy);
  9443. if (len1 <= 0.000001) {
  9444. return;
  9445. }
  9446. var ndx = dx / len1;
  9447. var ndy = dy / len1;
  9448. var dx2 = x2 - x1;
  9449. var dy2 = y2 - y1;
  9450. var len22 = dx2 * dx2 + dy2 * dy2;
  9451. var len2 = Math.sqrt(len22);
  9452. if (len2 <= 0.000001) {
  9453. return;
  9454. }
  9455. var ndx2 = dx2 / len2;
  9456. var ndy2 = dy2 / len2;
  9457. var odx = ndx + ndx2;
  9458. var ody = ndy + ndy2;
  9459. var olen = Math.sqrt(odx * odx + ody * ody);
  9460. if (olen <= 0.000001) {
  9461. return;
  9462. }
  9463. var nOdx = odx / olen;
  9464. var nOdy = ody / olen;
  9465. var alpha = Math.acos(nOdx * ndx + nOdy * ndy);
  9466. var halfAng = Math.PI / 2 - alpha;
  9467. len1 = r / Math.tan(halfAng);
  9468. var ptx1 = len1 * ndx + x1;
  9469. var pty1 = len1 * ndy + y1;
  9470. var orilen = Math.sqrt(len1 * len1 + r * r);
  9471. var orix = x1 + nOdx * orilen;
  9472. var oriy = y1 + nOdy * orilen;
  9473. var dir = ndx * ndy2 - ndy * ndx2;
  9474. var fChgAng = 0;
  9475. var sinx = 0.0;
  9476. var cosx = 0.0;
  9477. if (dir >= 0) {
  9478. fChgAng = halfAng * 2;
  9479. var fda = fChgAng / Context.SEGNUM;
  9480. sinx = Math.sin(fda);
  9481. cosx = Math.cos(fda);
  9482. }
  9483. else {
  9484. fChgAng = -halfAng * 2;
  9485. fda = fChgAng / Context.SEGNUM;
  9486. sinx = Math.sin(fda);
  9487. cosx = Math.cos(fda);
  9488. }
  9489. var lastx = this._path._lastOriX, lasty = this._path._lastOriY;
  9490. var _x1 = ptx1, _y1 = pty1;
  9491. if (Math.abs(_x1 - this._path._lastOriX) > 0.1 || Math.abs(_y1 - this._path._lastOriY) > 0.1) {
  9492. x = _x1;
  9493. y = _y1;
  9494. lastx = _x1;
  9495. lasty = _y1;
  9496. this._path._lastOriX = x;
  9497. this._path._lastOriY = y;
  9498. this._path.addPoint(x, y);
  9499. }
  9500. var cvx = ptx1 - orix;
  9501. var cvy = pty1 - oriy;
  9502. for (i = 0; i < Context.SEGNUM; i++) {
  9503. var cx = cvx * cosx + cvy * sinx;
  9504. var cy = -cvx * sinx + cvy * cosx;
  9505. x = cx + orix;
  9506. y = cy + oriy;
  9507. if (Math.abs(lastx - x) > 0.1 || Math.abs(lasty - y) > 0.1) {
  9508. this._path._lastOriX = x;
  9509. this._path._lastOriY = y;
  9510. this._path.addPoint(x, y);
  9511. lastx = x;
  9512. lasty = y;
  9513. }
  9514. cvx = cx;
  9515. cvy = cy;
  9516. }
  9517. }
  9518. arc(cx, cy, r, startAngle, endAngle, counterclockwise = false, b = true) {
  9519. var a = 0, da = 0;
  9520. var dx = 0, dy = 0, x = 0, y = 0;
  9521. var i, ndivs;
  9522. da = endAngle - startAngle;
  9523. if (!counterclockwise) {
  9524. if (Math.abs(da) >= Math.PI * 2) {
  9525. da = Math.PI * 2;
  9526. }
  9527. else {
  9528. while (da < 0.0) {
  9529. da += Math.PI * 2;
  9530. }
  9531. }
  9532. }
  9533. else {
  9534. if (Math.abs(da) >= Math.PI * 2) {
  9535. da = -Math.PI * 2;
  9536. }
  9537. else {
  9538. while (da > 0.0) {
  9539. da -= Math.PI * 2;
  9540. }
  9541. }
  9542. }
  9543. var sx = this.getMatScaleX();
  9544. var sy = this.getMatScaleY();
  9545. var sr = r * (sx > sy ? sx : sy);
  9546. var cl = 2 * Math.PI * sr;
  9547. ndivs = (Math.max(cl / 10, 10)) | 0;
  9548. var tPath = this._getPath();
  9549. for (i = 0; i <= ndivs; i++) {
  9550. a = startAngle + da * (i / ndivs);
  9551. dx = Math.cos(a);
  9552. dy = Math.sin(a);
  9553. x = cx + dx * r;
  9554. y = cy + dy * r;
  9555. if (x != this._path._lastOriX || y != this._path._lastOriY) {
  9556. tPath.addPoint(x, y);
  9557. }
  9558. }
  9559. dx = Math.cos(endAngle);
  9560. dy = Math.sin(endAngle);
  9561. x = cx + dx * r;
  9562. y = cy + dy * r;
  9563. if (x != this._path._lastOriX || y != this._path._lastOriY) {
  9564. tPath.addPoint(x, y);
  9565. }
  9566. }
  9567. quadraticCurveTo(cpx, cpy, x, y) {
  9568. var tBezier = Bezier.I;
  9569. var tArray = tBezier.getBezierPoints([this._path._lastOriX, this._path._lastOriY, cpx, cpy, x, y], 30, 2);
  9570. for (var i = 0, n = tArray.length / 2; i < n; i++) {
  9571. this.lineTo(tArray[i * 2], tArray[i * 2 + 1]);
  9572. }
  9573. this.lineTo(x, y);
  9574. }
  9575. mixRGBandAlpha(color) {
  9576. return this._mixRGBandAlpha(color, this._shader2D.ALPHA);
  9577. }
  9578. _mixRGBandAlpha(color, alpha) {
  9579. if (alpha >= 1) {
  9580. return color;
  9581. }
  9582. var a = ((color & 0xff000000) >>> 24);
  9583. if (a != 0) {
  9584. a *= alpha;
  9585. }
  9586. else {
  9587. a = alpha * 255;
  9588. }
  9589. return (color & 0x00ffffff) | (a << 24);
  9590. }
  9591. strokeRect(x, y, width, height, parameterLineWidth) {
  9592. if (this.lineWidth > 0) {
  9593. var rgba = this.mixRGBandAlpha(this.strokeStyle._color.numColor);
  9594. var hw = this.lineWidth / 2;
  9595. this._fillRect(x - hw, y - hw, width + this.lineWidth, this.lineWidth, rgba);
  9596. this._fillRect(x - hw, y - hw + height, width + this.lineWidth, this.lineWidth, rgba);
  9597. this._fillRect(x - hw, y + hw, this.lineWidth, height - this.lineWidth, rgba);
  9598. this._fillRect(x - hw + width, y + hw, this.lineWidth, height - this.lineWidth, rgba);
  9599. }
  9600. }
  9601. clip() {
  9602. }
  9603. drawParticle(x, y, pt) {
  9604. pt.x = x;
  9605. pt.y = y;
  9606. this._submits[this._submits._length++] = pt;
  9607. }
  9608. _getPath() {
  9609. return this._path || (this._path = new Path());
  9610. }
  9611. get canvas() {
  9612. return this._canvas;
  9613. }
  9614. _fillTexture_h(tex, imgid, uv, oriw, orih, x, y, w) {
  9615. if (oriw <= 0)
  9616. console.error('_fillTexture_h error: oriw must>0');
  9617. var stx = x;
  9618. var num = Math.floor(w / oriw);
  9619. var left = w % oriw;
  9620. for (var i = 0; i < num; i++) {
  9621. this._inner_drawTexture(tex, imgid, stx, y, oriw, orih, this._curMat, uv, 1, false);
  9622. stx += oriw;
  9623. }
  9624. if (left > 0) {
  9625. var du = uv[2] - uv[0];
  9626. var uvr = uv[0] + du * (left / oriw);
  9627. var tuv = Context.tmpuv1;
  9628. tuv[0] = uv[0];
  9629. tuv[1] = uv[1];
  9630. tuv[2] = uvr;
  9631. tuv[3] = uv[3];
  9632. tuv[4] = uvr;
  9633. tuv[5] = uv[5];
  9634. tuv[6] = uv[6];
  9635. tuv[7] = uv[7];
  9636. this._inner_drawTexture(tex, imgid, stx, y, left, orih, this._curMat, tuv, 1, false);
  9637. }
  9638. }
  9639. _fillTexture_v(tex, imgid, uv, oriw, orih, x, y, h) {
  9640. if (orih <= 0)
  9641. console.error('_fillTexture_v error: orih must>0');
  9642. var sty = y;
  9643. var num = Math.floor(h / orih);
  9644. var left = h % orih;
  9645. for (var i = 0; i < num; i++) {
  9646. this._inner_drawTexture(tex, imgid, x, sty, oriw, orih, this._curMat, uv, 1, false);
  9647. sty += orih;
  9648. }
  9649. if (left > 0) {
  9650. var dv = uv[7] - uv[1];
  9651. var uvb = uv[1] + dv * (left / orih);
  9652. var tuv = Context.tmpuv1;
  9653. tuv[0] = uv[0];
  9654. tuv[1] = uv[1];
  9655. tuv[2] = uv[2];
  9656. tuv[3] = uv[3];
  9657. tuv[4] = uv[4];
  9658. tuv[5] = uvb;
  9659. tuv[6] = uv[6];
  9660. tuv[7] = uvb;
  9661. this._inner_drawTexture(tex, imgid, x, sty, oriw, left, this._curMat, tuv, 1, false);
  9662. }
  9663. }
  9664. drawTextureWithSizeGrid(tex, tx, ty, width, height, sizeGrid, gx, gy) {
  9665. if (!tex._getSource())
  9666. return;
  9667. tx += gx;
  9668. ty += gy;
  9669. var uv = tex.uv, w = tex.bitmap.width, h = tex.bitmap.height;
  9670. var top = sizeGrid[0];
  9671. var left = sizeGrid[3];
  9672. var d_top = top / h;
  9673. var d_left = left / w;
  9674. var right = sizeGrid[1];
  9675. var bottom = sizeGrid[2];
  9676. var d_right = right / w;
  9677. var d_bottom = bottom / h;
  9678. var repeat = sizeGrid[4];
  9679. var needClip = false;
  9680. if (width == w) {
  9681. left = right = 0;
  9682. }
  9683. if (height == h) {
  9684. top = bottom = 0;
  9685. }
  9686. if (left + right > width) {
  9687. var clipWidth = width;
  9688. needClip = true;
  9689. width = left + right;
  9690. this.save();
  9691. this.clipRect(0 + tx, 0 + ty, clipWidth, height);
  9692. }
  9693. var imgid = tex.bitmap.id;
  9694. var mat = this._curMat;
  9695. var tuv = this._tempUV;
  9696. var uvl = uv[0];
  9697. var uvt = uv[1];
  9698. var uvr = uv[4];
  9699. var uvb = uv[5];
  9700. var uvl_ = uvl;
  9701. var uvt_ = uvt;
  9702. var uvr_ = uvr;
  9703. var uvb_ = uvb;
  9704. if (left && top) {
  9705. uvr_ = uvl + d_left;
  9706. uvb_ = uvt + d_top;
  9707. tuv[0] = uvl, tuv[1] = uvt, tuv[2] = uvr_, tuv[3] = uvt,
  9708. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl, tuv[7] = uvb_;
  9709. this._inner_drawTexture(tex, imgid, tx, ty, left, top, mat, tuv, 1, false);
  9710. }
  9711. if (right && top) {
  9712. uvl_ = uvr - d_right;
  9713. uvt_ = uvt;
  9714. uvr_ = uvr;
  9715. uvb_ = uvt + d_top;
  9716. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9717. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9718. this._inner_drawTexture(tex, imgid, width - right + tx, 0 + ty, right, top, mat, tuv, 1, false);
  9719. }
  9720. if (left && bottom) {
  9721. uvl_ = uvl;
  9722. uvt_ = uvb - d_bottom;
  9723. uvr_ = uvl + d_left;
  9724. uvb_ = uvb;
  9725. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9726. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9727. this._inner_drawTexture(tex, imgid, 0 + tx, height - bottom + ty, left, bottom, mat, tuv, 1, false);
  9728. }
  9729. if (right && bottom) {
  9730. uvl_ = uvr - d_right;
  9731. uvt_ = uvb - d_bottom;
  9732. uvr_ = uvr;
  9733. uvb_ = uvb;
  9734. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9735. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9736. this._inner_drawTexture(tex, imgid, width - right + tx, height - bottom + ty, right, bottom, mat, tuv, 1, false);
  9737. }
  9738. if (top) {
  9739. uvl_ = uvl + d_left;
  9740. uvt_ = uvt;
  9741. uvr_ = uvr - d_right;
  9742. uvb_ = uvt + d_top;
  9743. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9744. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9745. if (repeat) {
  9746. this._fillTexture_h(tex, imgid, tuv, tex.width - left - right, top, left + tx, ty, width - left - right);
  9747. }
  9748. else {
  9749. this._inner_drawTexture(tex, imgid, left + tx, ty, width - left - right, top, mat, tuv, 1, false);
  9750. }
  9751. }
  9752. if (bottom) {
  9753. uvl_ = uvl + d_left;
  9754. uvt_ = uvb - d_bottom;
  9755. uvr_ = uvr - d_right;
  9756. uvb_ = uvb;
  9757. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9758. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9759. if (repeat) {
  9760. this._fillTexture_h(tex, imgid, tuv, tex.width - left - right, bottom, left + tx, height - bottom + ty, width - left - right);
  9761. }
  9762. else {
  9763. this._inner_drawTexture(tex, imgid, left + tx, height - bottom + ty, width - left - right, bottom, mat, tuv, 1, false);
  9764. }
  9765. }
  9766. if (left) {
  9767. uvl_ = uvl;
  9768. uvt_ = uvt + d_top;
  9769. uvr_ = uvl + d_left;
  9770. uvb_ = uvb - d_bottom;
  9771. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9772. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9773. if (repeat) {
  9774. this._fillTexture_v(tex, imgid, tuv, left, tex.height - top - bottom, tx, top + ty, height - top - bottom);
  9775. }
  9776. else {
  9777. this._inner_drawTexture(tex, imgid, tx, top + ty, left, height - top - bottom, mat, tuv, 1, false);
  9778. }
  9779. }
  9780. if (right) {
  9781. uvl_ = uvr - d_right;
  9782. uvt_ = uvt + d_top;
  9783. uvr_ = uvr;
  9784. uvb_ = uvb - d_bottom;
  9785. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9786. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9787. if (repeat) {
  9788. this._fillTexture_v(tex, imgid, tuv, right, tex.height - top - bottom, width - right + tx, top + ty, height - top - bottom);
  9789. }
  9790. else {
  9791. this._inner_drawTexture(tex, imgid, width - right + tx, top + ty, right, height - top - bottom, mat, tuv, 1, false);
  9792. }
  9793. }
  9794. uvl_ = uvl + d_left;
  9795. uvt_ = uvt + d_top;
  9796. uvr_ = uvr - d_right;
  9797. uvb_ = uvb - d_bottom;
  9798. tuv[0] = uvl_, tuv[1] = uvt_, tuv[2] = uvr_, tuv[3] = uvt_,
  9799. tuv[4] = uvr_, tuv[5] = uvb_, tuv[6] = uvl_, tuv[7] = uvb_;
  9800. if (repeat) {
  9801. var tuvr = Context.tmpUVRect;
  9802. tuvr[0] = uvl_;
  9803. tuvr[1] = uvt_;
  9804. tuvr[2] = uvr_ - uvl_;
  9805. tuvr[3] = uvb_ - uvt_;
  9806. 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);
  9807. }
  9808. else {
  9809. this._inner_drawTexture(tex, imgid, left + tx, top + ty, width - left - right, height - top - bottom, mat, tuv, 1, false);
  9810. }
  9811. if (needClip)
  9812. this.restore();
  9813. }
  9814. }
  9815. Context.ENUM_TEXTALIGN_DEFAULT = 0;
  9816. Context.ENUM_TEXTALIGN_CENTER = 1;
  9817. Context.ENUM_TEXTALIGN_RIGHT = 2;
  9818. Context._SUBMITVBSIZE = 32000;
  9819. Context._MAXSIZE = 99999999;
  9820. Context._MAXVERTNUM = 65535;
  9821. Context.MAXCLIPRECT = null;
  9822. Context._COUNT = 0;
  9823. Context.SEGNUM = 32;
  9824. Context._contextcount = 0;
  9825. Context.PI2 = 2 * Math.PI;
  9826. Context._textRender = null;
  9827. Context.tmpuv1 = [0, 0, 0, 0, 0, 0, 0, 0];
  9828. Context.tmpUV = [0, 0, 0, 0, 0, 0, 0, 0];
  9829. Context.tmpUVRect = [0, 0, 0, 0];
  9830. class ContextParams {
  9831. constructor() {
  9832. this.lineWidth = 1;
  9833. }
  9834. clear() {
  9835. this.lineWidth = 1;
  9836. this.textAlign = this.textBaseline = null;
  9837. }
  9838. make() {
  9839. return this === ContextParams.DEFAULT ? new ContextParams() : this;
  9840. }
  9841. }
  9842. class WebGL {
  9843. static _uint8ArraySlice() {
  9844. var _this = this;
  9845. var sz = _this.length;
  9846. var dec = new Uint8Array(_this.length);
  9847. for (var i = 0; i < sz; i++)
  9848. dec[i] = _this[i];
  9849. return dec;
  9850. }
  9851. static _float32ArraySlice() {
  9852. var _this = this;
  9853. var sz = _this.length;
  9854. var dec = new Float32Array(_this.length);
  9855. for (var i = 0; i < sz; i++)
  9856. dec[i] = _this[i];
  9857. return dec;
  9858. }
  9859. static _uint16ArraySlice(...arg) {
  9860. var _this = this;
  9861. var sz;
  9862. var dec;
  9863. var i;
  9864. if (arg.length === 0) {
  9865. sz = _this.length;
  9866. dec = new Uint16Array(sz);
  9867. for (i = 0; i < sz; i++)
  9868. dec[i] = _this[i];
  9869. }
  9870. else if (arg.length === 2) {
  9871. var start = arg[0];
  9872. var end = arg[1];
  9873. if (end > start) {
  9874. sz = end - start;
  9875. dec = new Uint16Array(sz);
  9876. for (i = start; i < end; i++)
  9877. dec[i - start] = _this[i];
  9878. }
  9879. else {
  9880. dec = new Uint16Array(0);
  9881. }
  9882. }
  9883. return dec;
  9884. }
  9885. static _nativeRender_enable() {
  9886. }
  9887. static enable() {
  9888. return true;
  9889. }
  9890. static inner_enable() {
  9891. Float32Array.prototype.slice || (Float32Array.prototype.slice = WebGL._float32ArraySlice);
  9892. Uint16Array.prototype.slice || (Uint16Array.prototype.slice = WebGL._uint16ArraySlice);
  9893. Uint8Array.prototype.slice || (Uint8Array.prototype.slice = WebGL._uint8ArraySlice);
  9894. return true;
  9895. }
  9896. static onStageResize(width, height) {
  9897. if (WebGLContext.mainContext == null)
  9898. return;
  9899. WebGLContext.mainContext.viewport(0, 0, width, height);
  9900. RenderState2D.width = width;
  9901. RenderState2D.height = height;
  9902. }
  9903. }
  9904. WebGL._isWebGL2 = false;
  9905. WebGL.isNativeRender_enable = false;
  9906. class VertexArrayObject {
  9907. constructor() {
  9908. }
  9909. }
  9910. (function () {
  9911. var glErrorShadow = {};
  9912. function error(msg) {
  9913. if (window.console && window.console.error) {
  9914. window.console.error(msg);
  9915. }
  9916. }
  9917. function log(msg) {
  9918. if (window.console && window.console.log) {
  9919. window.console.log(msg);
  9920. }
  9921. }
  9922. function synthesizeGLError(err, opt_msg) {
  9923. glErrorShadow[err] = true;
  9924. if (opt_msg !== undefined) {
  9925. error(opt_msg);
  9926. }
  9927. }
  9928. function wrapGLError(gl) {
  9929. var f = gl.getError;
  9930. gl.getError = function () {
  9931. var err;
  9932. do {
  9933. err = f.apply(gl);
  9934. if (err != gl.NO_ERROR) {
  9935. glErrorShadow[err] = true;
  9936. }
  9937. } while (err != gl.NO_ERROR);
  9938. for (var err1 in glErrorShadow) {
  9939. if (glErrorShadow[err1]) {
  9940. delete glErrorShadow[err1];
  9941. return parseInt(err1);
  9942. }
  9943. }
  9944. return gl.NO_ERROR;
  9945. };
  9946. }
  9947. var WebGLVertexArrayObjectOES = function WebGLVertexArrayObjectOES(ext) {
  9948. var gl = ext.gl;
  9949. this.ext = ext;
  9950. this.isAlive = true;
  9951. this.hasBeenBound = false;
  9952. this.elementArrayBuffer = null;
  9953. this.attribs = new Array(ext.maxVertexAttribs);
  9954. for (var n = 0; n < this.attribs.length; n++) {
  9955. var attrib = new WebGLVertexArrayObjectOES.VertexAttrib(gl);
  9956. this.attribs[n] = attrib;
  9957. }
  9958. this.maxAttrib = 0;
  9959. };
  9960. WebGLVertexArrayObjectOES.VertexAttrib = function VertexAttrib(gl) {
  9961. this.enabled = false;
  9962. this.buffer = null;
  9963. this.size = 4;
  9964. this.type = gl.FLOAT;
  9965. this.normalized = false;
  9966. this.stride = 16;
  9967. this.offset = 0;
  9968. this.cached = "";
  9969. this.recache();
  9970. };
  9971. WebGLVertexArrayObjectOES.VertexAttrib.prototype.recache = function recache() {
  9972. this.cached = [this.size, this.type, this.normalized, this.stride, this.offset].join(":");
  9973. };
  9974. var OESVertexArrayObject = function OESVertexArrayObject(gl) {
  9975. var self = this;
  9976. this.gl = gl;
  9977. wrapGLError(gl);
  9978. var original = this.original = {
  9979. getParameter: gl.getParameter,
  9980. enableVertexAttribArray: gl.enableVertexAttribArray,
  9981. disableVertexAttribArray: gl.disableVertexAttribArray,
  9982. bindBuffer: gl.bindBuffer,
  9983. getVertexAttrib: gl.getVertexAttrib,
  9984. vertexAttribPointer: gl.vertexAttribPointer
  9985. };
  9986. gl.getParameter = function getParameter(pname) {
  9987. if (pname == self.VERTEX_ARRAY_BINDING_OES) {
  9988. if (self.currentVertexArrayObject == self.defaultVertexArrayObject) {
  9989. return null;
  9990. }
  9991. else {
  9992. return self.currentVertexArrayObject;
  9993. }
  9994. }
  9995. return original.getParameter.apply(this, arguments);
  9996. };
  9997. gl.enableVertexAttribArray = function enableVertexAttribArray(index) {
  9998. var vao = self.currentVertexArrayObject;
  9999. vao.maxAttrib = Math.max(vao.maxAttrib, index);
  10000. var attrib = vao.attribs[index];
  10001. attrib.enabled = true;
  10002. return original.enableVertexAttribArray.apply(this, arguments);
  10003. };
  10004. gl.disableVertexAttribArray = function disableVertexAttribArray(index) {
  10005. var vao = self.currentVertexArrayObject;
  10006. vao.maxAttrib = Math.max(vao.maxAttrib, index);
  10007. var attrib = vao.attribs[index];
  10008. attrib.enabled = false;
  10009. return original.disableVertexAttribArray.apply(this, arguments);
  10010. };
  10011. gl.bindBuffer = function bindBuffer(target, buffer) {
  10012. switch (target) {
  10013. case gl.ARRAY_BUFFER:
  10014. self.currentArrayBuffer = buffer;
  10015. break;
  10016. case gl.ELEMENT_ARRAY_BUFFER:
  10017. self.currentVertexArrayObject.elementArrayBuffer = buffer;
  10018. break;
  10019. }
  10020. return original.bindBuffer.apply(this, arguments);
  10021. };
  10022. gl.getVertexAttrib = function getVertexAttrib(index, pname) {
  10023. var vao = self.currentVertexArrayObject;
  10024. var attrib = vao.attribs[index];
  10025. switch (pname) {
  10026. case gl.VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
  10027. return attrib.buffer;
  10028. case gl.VERTEX_ATTRIB_ARRAY_ENABLED:
  10029. return attrib.enabled;
  10030. case gl.VERTEX_ATTRIB_ARRAY_SIZE:
  10031. return attrib.size;
  10032. case gl.VERTEX_ATTRIB_ARRAY_STRIDE:
  10033. return attrib.stride;
  10034. case gl.VERTEX_ATTRIB_ARRAY_TYPE:
  10035. return attrib.type;
  10036. case gl.VERTEX_ATTRIB_ARRAY_NORMALIZED:
  10037. return attrib.normalized;
  10038. default:
  10039. return original.getVertexAttrib.apply(this, arguments);
  10040. }
  10041. };
  10042. gl.vertexAttribPointer = function vertexAttribPointer(indx, size, type, normalized, stride, offset) {
  10043. var vao = self.currentVertexArrayObject;
  10044. vao.maxAttrib = Math.max(vao.maxAttrib, indx);
  10045. var attrib = vao.attribs[indx];
  10046. attrib.buffer = self.currentArrayBuffer;
  10047. attrib.size = size;
  10048. attrib.type = type;
  10049. attrib.normalized = normalized;
  10050. attrib.stride = stride;
  10051. attrib.offset = offset;
  10052. attrib.recache();
  10053. return original.vertexAttribPointer.apply(this, arguments);
  10054. };
  10055. if (gl.instrumentExtension) {
  10056. gl.instrumentExtension(this, "OES_vertex_array_object");
  10057. }
  10058. gl.canvas.addEventListener('webglcontextrestored', function () {
  10059. log("OESVertexArrayObject emulation library context restored");
  10060. self.reset_();
  10061. }, true);
  10062. this.reset_();
  10063. };
  10064. OESVertexArrayObject.prototype.VERTEX_ARRAY_BINDING_OES = 0x85B5;
  10065. OESVertexArrayObject.prototype.reset_ = function reset_() {
  10066. var contextWasLost = this.vertexArrayObjects !== undefined;
  10067. if (contextWasLost) {
  10068. for (var ii = 0; ii < this.vertexArrayObjects.length; ++ii) {
  10069. this.vertexArrayObjects.isAlive = false;
  10070. }
  10071. }
  10072. var gl = this.gl;
  10073. this.maxVertexAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS);
  10074. this.defaultVertexArrayObject = new WebGLVertexArrayObjectOES(this);
  10075. this.currentVertexArrayObject = null;
  10076. this.currentArrayBuffer = null;
  10077. this.vertexArrayObjects = [this.defaultVertexArrayObject];
  10078. this.bindVertexArrayOES(null);
  10079. };
  10080. OESVertexArrayObject.prototype.createVertexArrayOES = function createVertexArrayOES() {
  10081. var arrayObject = new WebGLVertexArrayObjectOES(this);
  10082. this.vertexArrayObjects.push(arrayObject);
  10083. return arrayObject;
  10084. };
  10085. OESVertexArrayObject.prototype.deleteVertexArrayOES = function deleteVertexArrayOES(arrayObject) {
  10086. arrayObject.isAlive = false;
  10087. this.vertexArrayObjects.splice(this.vertexArrayObjects.indexOf(arrayObject), 1);
  10088. if (this.currentVertexArrayObject == arrayObject) {
  10089. this.bindVertexArrayOES(null);
  10090. }
  10091. };
  10092. OESVertexArrayObject.prototype.isVertexArrayOES = function isVertexArrayOES(arrayObject) {
  10093. if (arrayObject && arrayObject instanceof WebGLVertexArrayObjectOES) {
  10094. if (arrayObject.hasBeenBound && arrayObject.ext == this) {
  10095. return true;
  10096. }
  10097. }
  10098. return false;
  10099. };
  10100. OESVertexArrayObject.prototype.bindVertexArrayOES = function bindVertexArrayOES(arrayObject) {
  10101. var gl = this.gl;
  10102. if (arrayObject && !arrayObject.isAlive) {
  10103. synthesizeGLError(gl.INVALID_OPERATION, "bindVertexArrayOES: attempt to bind deleted arrayObject");
  10104. return;
  10105. }
  10106. var original = this.original;
  10107. var oldVAO = this.currentVertexArrayObject;
  10108. this.currentVertexArrayObject = arrayObject || this.defaultVertexArrayObject;
  10109. this.currentVertexArrayObject.hasBeenBound = true;
  10110. var newVAO = this.currentVertexArrayObject;
  10111. if (oldVAO == newVAO) {
  10112. return;
  10113. }
  10114. if (!oldVAO || newVAO.elementArrayBuffer != oldVAO.elementArrayBuffer) {
  10115. original.bindBuffer.call(gl, gl.ELEMENT_ARRAY_BUFFER, newVAO.elementArrayBuffer);
  10116. }
  10117. var currentBinding = this.currentArrayBuffer;
  10118. var maxAttrib = Math.max(oldVAO ? oldVAO.maxAttrib : 0, newVAO.maxAttrib);
  10119. for (var n = 0; n <= maxAttrib; n++) {
  10120. var attrib = newVAO.attribs[n];
  10121. var oldAttrib = oldVAO ? oldVAO.attribs[n] : null;
  10122. if (!oldVAO || attrib.enabled != oldAttrib.enabled) {
  10123. if (attrib.enabled) {
  10124. original.enableVertexAttribArray.call(gl, n);
  10125. }
  10126. else {
  10127. original.disableVertexAttribArray.call(gl, n);
  10128. }
  10129. }
  10130. if (attrib.enabled) {
  10131. var bufferChanged = false;
  10132. if (!oldVAO || attrib.buffer != oldAttrib.buffer) {
  10133. if (currentBinding != attrib.buffer) {
  10134. original.bindBuffer.call(gl, gl.ARRAY_BUFFER, attrib.buffer);
  10135. currentBinding = attrib.buffer;
  10136. }
  10137. bufferChanged = true;
  10138. }
  10139. if (bufferChanged || attrib.cached != oldAttrib.cached) {
  10140. original.vertexAttribPointer.call(gl, n, attrib.size, attrib.type, attrib.normalized, attrib.stride, attrib.offset);
  10141. }
  10142. }
  10143. }
  10144. if (this.currentArrayBuffer != currentBinding) {
  10145. original.bindBuffer.call(gl, gl.ARRAY_BUFFER, this.currentArrayBuffer);
  10146. }
  10147. };
  10148. window._setupVertexArrayObject = function (gl) {
  10149. var original_getSupportedExtensions = gl.getSupportedExtensions;
  10150. gl.getSupportedExtensions = function getSupportedExtensions() {
  10151. var list = original_getSupportedExtensions.call(this) || [];
  10152. if (list.indexOf("OES_vertex_array_object") < 0) {
  10153. list.push("OES_vertex_array_object");
  10154. }
  10155. return list;
  10156. };
  10157. var original_getExtension = gl.getExtension;
  10158. gl.getExtension = function getExtension(name) {
  10159. var ext = original_getExtension.call(this, name);
  10160. if (ext) {
  10161. return ext;
  10162. }
  10163. if (name !== "OES_vertex_array_object") {
  10164. return null;
  10165. }
  10166. if (!this.__OESVertexArrayObject) {
  10167. console.log("Setup OES_vertex_array_object polyfill");
  10168. this.__OESVertexArrayObject = new OESVertexArrayObject(this);
  10169. }
  10170. return this.__OESVertexArrayObject;
  10171. };
  10172. };
  10173. window._forceSetupVertexArrayObject = function (gl) {
  10174. var original_getSupportedExtensions = gl.getSupportedExtensions;
  10175. gl.getSupportedExtensions = function getSupportedExtensions() {
  10176. var list = original_getSupportedExtensions.call(this) || [];
  10177. if (list.indexOf("OES_vertex_array_object") < 0) {
  10178. list.push("OES_vertex_array_object");
  10179. }
  10180. return list;
  10181. };
  10182. var original_getExtension = gl.getExtension;
  10183. gl.getExtension = function getExtension(name) {
  10184. if (name === "OES_vertex_array_object") {
  10185. if (!this.__OESVertexArrayObject) {
  10186. console.log("Setup OES_vertex_array_object polyfill");
  10187. this.__OESVertexArrayObject = new OESVertexArrayObject(this);
  10188. }
  10189. return this.__OESVertexArrayObject;
  10190. }
  10191. else {
  10192. var ext = original_getExtension.call(this, name);
  10193. if (ext) {
  10194. return ext;
  10195. }
  10196. else {
  10197. return null;
  10198. }
  10199. }
  10200. };
  10201. };
  10202. }());
  10203. class LayaGPU {
  10204. constructor(gl, isWebGL2) {
  10205. this._gl = null;
  10206. this._vaoExt = null;
  10207. this._angleInstancedArrays = null;
  10208. this._isWebGL2 = false;
  10209. this._oesTextureHalfFloat = null;
  10210. this._oes_element_index_uint = null;
  10211. this._oesTextureHalfFloatLinear = null;
  10212. this._oesTextureFloat = null;
  10213. this._extTextureFilterAnisotropic = null;
  10214. this._compressedTextureS3tc = null;
  10215. this._compressedTexturePvrtc = null;
  10216. this._compressedTextureEtc1 = null;
  10217. this._gl = gl;
  10218. this._isWebGL2 = isWebGL2;
  10219. if (!isWebGL2) {
  10220. var forceVAO = LayaGPU._forceSupportVAOPlatform();
  10221. if (!ILaya.Render.isConchApp) {
  10222. if (window._setupVertexArrayObject) {
  10223. if (forceVAO)
  10224. window._forceSetupVertexArrayObject(gl);
  10225. else
  10226. window._setupVertexArrayObject(gl);
  10227. }
  10228. }
  10229. this._vaoExt = this._getExtension("OES_vertex_array_object");
  10230. if (!forceVAO)
  10231. this._angleInstancedArrays = this._getExtension("ANGLE_instanced_arrays");
  10232. this._oesTextureHalfFloat = this._getExtension("OES_texture_half_float");
  10233. this._oesTextureHalfFloatLinear = this._getExtension("OES_texture_half_float_linear");
  10234. this._oesTextureFloat = this._getExtension("OES_texture_float");
  10235. this._oes_element_index_uint = this._getExtension("OES_element_index_uint");
  10236. }
  10237. else {
  10238. this._getExtension("EXT_color_buffer_float");
  10239. }
  10240. this._extTextureFilterAnisotropic = this._getExtension("EXT_texture_filter_anisotropic");
  10241. this._compressedTextureS3tc = this._getExtension("WEBGL_compressed_texture_s3tc");
  10242. this._compressedTexturePvrtc = this._getExtension("WEBGL_compressed_texture_pvrtc");
  10243. this._compressedTextureEtc1 = this._getExtension("WEBGL_compressed_texture_etc1");
  10244. }
  10245. static _forceSupportVAOPlatform() {
  10246. let Browser = ILaya.Browser;
  10247. return Browser.onBDMiniGame || Browser.onQGMiniGame;
  10248. }
  10249. _getExtension(name) {
  10250. var prefixes = LayaGPU._extentionVendorPrefixes;
  10251. for (var k in prefixes) {
  10252. var ext = this._gl.getExtension(prefixes[k] + name);
  10253. if (ext)
  10254. return ext;
  10255. }
  10256. return null;
  10257. }
  10258. createVertexArray() {
  10259. if (this._isWebGL2)
  10260. return this._gl.createVertexArray();
  10261. else
  10262. return this._vaoExt.createVertexArrayOES();
  10263. }
  10264. bindVertexArray(vertexArray) {
  10265. if (this._isWebGL2)
  10266. this._gl.bindVertexArray(vertexArray);
  10267. else
  10268. this._vaoExt.bindVertexArrayOES(vertexArray);
  10269. }
  10270. deleteVertexArray(vertexArray) {
  10271. if (this._isWebGL2)
  10272. this._gl.deleteVertexArray(vertexArray);
  10273. else
  10274. this._vaoExt.deleteVertexArrayOES(vertexArray);
  10275. }
  10276. isVertexArray(vertexArray) {
  10277. if (this._isWebGL2)
  10278. this._gl.isVertexArray(vertexArray);
  10279. else
  10280. this._vaoExt.isVertexArrayOES(vertexArray);
  10281. }
  10282. drawElementsInstanced(mode, count, type, offset, instanceCount) {
  10283. if (this._isWebGL2)
  10284. this._gl.drawElementsInstanced(mode, count, type, offset, instanceCount);
  10285. else
  10286. this._angleInstancedArrays.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);
  10287. }
  10288. drawArraysInstanced(mode, first, count, instanceCount) {
  10289. if (this._isWebGL2)
  10290. this._gl.drawArraysInstanced(mode, first, count, instanceCount);
  10291. else
  10292. this._angleInstancedArrays.drawArraysInstancedANGLE(mode, first, count, instanceCount);
  10293. }
  10294. vertexAttribDivisor(index, divisor) {
  10295. if (this._isWebGL2)
  10296. this._gl.vertexAttribDivisor(index, divisor);
  10297. else
  10298. this._angleInstancedArrays.vertexAttribDivisorANGLE(index, divisor);
  10299. }
  10300. supportInstance() {
  10301. if (this._isWebGL2 || this._angleInstancedArrays)
  10302. return true;
  10303. else
  10304. return false;
  10305. }
  10306. supportElementIndexUint32() {
  10307. return this._isWebGL2 || this._oes_element_index_uint ? true : false;
  10308. }
  10309. }
  10310. LayaGPU._extentionVendorPrefixes = ["", "WEBKIT_", "MOZ_"];
  10311. class Render {
  10312. constructor(width, height, mainCanv) {
  10313. this._timeId = 0;
  10314. Render._mainCanvas = mainCanv;
  10315. let source = Render._mainCanvas.source;
  10316. source.id = "layaCanvas";
  10317. source.width = width;
  10318. source.height = height;
  10319. if (Render.isConchApp) {
  10320. document.body.appendChild(source);
  10321. }
  10322. this.initRender(Render._mainCanvas, width, height);
  10323. window.requestAnimationFrame(loop);
  10324. function loop(stamp) {
  10325. ILaya.stage._loop();
  10326. window.requestAnimationFrame(loop);
  10327. }
  10328. ILaya.stage.on("visibilitychange", this, this._onVisibilitychange);
  10329. }
  10330. _onVisibilitychange() {
  10331. if (!ILaya.stage.isVisibility) {
  10332. this._timeId = window.setInterval(this._enterFrame, 1000);
  10333. }
  10334. else if (this._timeId != 0) {
  10335. window.clearInterval(this._timeId);
  10336. }
  10337. }
  10338. initRender(canvas, w, h) {
  10339. function getWebGLContext(canvas) {
  10340. var gl;
  10341. var names = ["webgl2", "webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
  10342. if (!Config.useWebGL2 || Browser.onBDMiniGame) {
  10343. names.shift();
  10344. }
  10345. for (var i = 0; i < names.length; i++) {
  10346. try {
  10347. gl = canvas.getContext(names[i], { stencil: Config.isStencil, alpha: Config.isAlpha, antialias: Config.isAntialias, premultipliedAlpha: Config.premultipliedAlpha, preserveDrawingBuffer: Config.preserveDrawingBuffer });
  10348. }
  10349. catch (e) {
  10350. }
  10351. if (gl) {
  10352. (names[i] === 'webgl2') && (WebGL._isWebGL2 = true);
  10353. return gl;
  10354. }
  10355. }
  10356. return null;
  10357. }
  10358. var gl = LayaGL.instance = WebGLContext.mainContext = getWebGLContext(Render._mainCanvas.source);
  10359. if (!gl)
  10360. return false;
  10361. LayaGL.instance = gl;
  10362. LayaGL.layaGPUInstance = new LayaGPU(gl, WebGL._isWebGL2);
  10363. canvas.size(w, h);
  10364. Context.__init__();
  10365. SubmitBase.__init__();
  10366. var ctx = new Context();
  10367. ctx.isMain = true;
  10368. Render._context = ctx;
  10369. canvas._setContext(ctx);
  10370. ShaderDefines2D.__init__();
  10371. Value2D.__init__();
  10372. Shader2D.__init__();
  10373. Buffer2D.__int__(gl);
  10374. BlendMode._init_(gl);
  10375. return true;
  10376. }
  10377. _enterFrame(e = null) {
  10378. ILaya.stage._loop();
  10379. }
  10380. static get context() {
  10381. return Render._context;
  10382. }
  10383. static get canvas() {
  10384. return Render._mainCanvas.source;
  10385. }
  10386. }
  10387. Render.supportWebGLPlusCulling = false;
  10388. Render.supportWebGLPlusAnimation = false;
  10389. Render.supportWebGLPlusRendering = false;
  10390. Render.isConchApp = false;
  10391. {
  10392. Render.isConchApp = (window.conch != null);
  10393. if (Render.isConchApp) {
  10394. Render.supportWebGLPlusCulling = false;
  10395. Render.supportWebGLPlusAnimation = false;
  10396. Render.supportWebGLPlusRendering = false;
  10397. }
  10398. else if (window.qq != null && window.qq.webglPlus != null) {
  10399. Render.supportWebGLPlusCulling = false;
  10400. Render.supportWebGLPlusAnimation = false;
  10401. Render.supportWebGLPlusRendering = false;
  10402. }
  10403. }
  10404. class DrawTrianglesCmd {
  10405. static create(texture, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum) {
  10406. var cmd = Pool.getItemByClass("DrawTrianglesCmd", DrawTrianglesCmd);
  10407. cmd.texture = texture;
  10408. cmd.x = x;
  10409. cmd.y = y;
  10410. cmd.vertices = vertices;
  10411. cmd.uvs = uvs;
  10412. cmd.indices = indices;
  10413. cmd.matrix = matrix;
  10414. cmd.alpha = alpha;
  10415. if (color) {
  10416. cmd.color = new ColorFilter();
  10417. var c = ColorUtils.create(color).arrColor;
  10418. cmd.color.color(c[0] * 255, c[1] * 255, c[2] * 255, c[3] * 255);
  10419. }
  10420. cmd.blendMode = blendMode;
  10421. cmd.colorNum = colorNum;
  10422. return cmd;
  10423. }
  10424. recover() {
  10425. this.texture = null;
  10426. this.vertices = null;
  10427. this.uvs = null;
  10428. this.indices = null;
  10429. this.matrix = null;
  10430. Pool.recover("DrawTrianglesCmd", this);
  10431. }
  10432. run(context, gx, gy) {
  10433. 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);
  10434. }
  10435. get cmdID() {
  10436. return DrawTrianglesCmd.ID;
  10437. }
  10438. }
  10439. DrawTrianglesCmd.ID = "DrawTriangles";
  10440. class Draw9GridTexture {
  10441. constructor() {
  10442. }
  10443. static create(texture, x, y, width, height, sizeGrid) {
  10444. var cmd = Pool.getItemByClass("Draw9GridTexture", Draw9GridTexture);
  10445. cmd.texture = texture;
  10446. texture._addReference();
  10447. cmd.x = x;
  10448. cmd.y = y;
  10449. cmd.width = width;
  10450. cmd.height = height;
  10451. cmd.sizeGrid = sizeGrid;
  10452. return cmd;
  10453. }
  10454. recover() {
  10455. this.texture._removeReference();
  10456. Pool.recover("Draw9GridTexture", this);
  10457. }
  10458. run(context, gx, gy) {
  10459. context.drawTextureWithSizeGrid(this.texture, this.x, this.y, this.width, this.height, this.sizeGrid, gx, gy);
  10460. }
  10461. get cmdID() {
  10462. return Draw9GridTexture.ID;
  10463. }
  10464. }
  10465. Draw9GridTexture.ID = "Draw9GridTexture";
  10466. class GraphicsBounds {
  10467. constructor() {
  10468. this._cacheBoundsType = false;
  10469. }
  10470. destroy() {
  10471. this._graphics = null;
  10472. this._cacheBoundsType = false;
  10473. if (this._temp)
  10474. this._temp.length = 0;
  10475. if (this._rstBoundPoints)
  10476. this._rstBoundPoints.length = 0;
  10477. if (this._bounds)
  10478. this._bounds.recover();
  10479. this._bounds = null;
  10480. Pool.recover("GraphicsBounds", this);
  10481. }
  10482. static create() {
  10483. return Pool.getItemByClass("GraphicsBounds", GraphicsBounds);
  10484. }
  10485. reset() {
  10486. this._temp && (this._temp.length = 0);
  10487. }
  10488. getBounds(realSize = false) {
  10489. if (!this._bounds || !this._temp || this._temp.length < 1 || realSize != this._cacheBoundsType) {
  10490. this._bounds = Rectangle._getWrapRec(this.getBoundPoints(realSize), this._bounds);
  10491. }
  10492. this._cacheBoundsType = realSize;
  10493. return this._bounds;
  10494. }
  10495. getBoundPoints(realSize = false) {
  10496. if (!this._temp || this._temp.length < 1 || realSize != this._cacheBoundsType)
  10497. this._temp = this._getCmdPoints(realSize);
  10498. this._cacheBoundsType = realSize;
  10499. return this._rstBoundPoints = Utils.copyArray(this._rstBoundPoints, this._temp);
  10500. }
  10501. _getCmdPoints(realSize = false) {
  10502. var cmds = this._graphics.cmds;
  10503. var rst;
  10504. rst = this._temp || (this._temp = []);
  10505. rst.length = 0;
  10506. if (!cmds && this._graphics._one != null) {
  10507. GraphicsBounds._tempCmds.length = 0;
  10508. GraphicsBounds._tempCmds.push(this._graphics._one);
  10509. cmds = GraphicsBounds._tempCmds;
  10510. }
  10511. if (!cmds)
  10512. return rst;
  10513. var matrixs = GraphicsBounds._tempMatrixArrays;
  10514. matrixs.length = 0;
  10515. var tMatrix = GraphicsBounds._initMatrix;
  10516. tMatrix.identity();
  10517. var tempMatrix = GraphicsBounds._tempMatrix;
  10518. var cmd;
  10519. var tex;
  10520. for (var i = 0, n = cmds.length; i < n; i++) {
  10521. cmd = cmds[i];
  10522. switch (cmd.cmdID) {
  10523. case AlphaCmd.ID:
  10524. matrixs.push(tMatrix);
  10525. tMatrix = tMatrix.clone();
  10526. break;
  10527. case RestoreCmd.ID:
  10528. tMatrix = matrixs.pop();
  10529. break;
  10530. case ScaleCmd.ID:
  10531. tempMatrix.identity();
  10532. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  10533. tempMatrix.scale(cmd.scaleX, cmd.scaleY);
  10534. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  10535. this._switchMatrix(tMatrix, tempMatrix);
  10536. break;
  10537. case RotateCmd.ID:
  10538. tempMatrix.identity();
  10539. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  10540. tempMatrix.rotate(cmd.angle);
  10541. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  10542. this._switchMatrix(tMatrix, tempMatrix);
  10543. break;
  10544. case TranslateCmd.ID:
  10545. tempMatrix.identity();
  10546. tempMatrix.translate(cmd.tx, cmd.ty);
  10547. this._switchMatrix(tMatrix, tempMatrix);
  10548. break;
  10549. case TransformCmd.ID:
  10550. tempMatrix.identity();
  10551. tempMatrix.translate(-cmd.pivotX, -cmd.pivotY);
  10552. tempMatrix.concat(cmd.matrix);
  10553. tempMatrix.translate(cmd.pivotX, cmd.pivotY);
  10554. this._switchMatrix(tMatrix, tempMatrix);
  10555. break;
  10556. case DrawImageCmd.ID:
  10557. case FillTextureCmd.ID:
  10558. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  10559. break;
  10560. case DrawTextureCmd.ID:
  10561. tMatrix.copyTo(tempMatrix);
  10562. if (cmd.matrix)
  10563. tempMatrix.concat(cmd.matrix);
  10564. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tempMatrix);
  10565. break;
  10566. case DrawImageCmd.ID:
  10567. tex = cmd.texture;
  10568. if (realSize) {
  10569. if (cmd.width && cmd.height) {
  10570. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  10571. }
  10572. else {
  10573. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), tMatrix);
  10574. }
  10575. }
  10576. else {
  10577. var wRate = (cmd.width || tex.sourceWidth) / tex.width;
  10578. var hRate = (cmd.height || tex.sourceHeight) / tex.height;
  10579. var oWidth = wRate * tex.sourceWidth;
  10580. var oHeight = hRate * tex.sourceHeight;
  10581. var offX = tex.offsetX > 0 ? tex.offsetX : 0;
  10582. var offY = tex.offsetY > 0 ? tex.offsetY : 0;
  10583. offX *= wRate;
  10584. offY *= hRate;
  10585. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - offX, cmd.y - offY, oWidth, oHeight), tMatrix);
  10586. }
  10587. break;
  10588. case FillTextureCmd.ID:
  10589. if (cmd.width && cmd.height) {
  10590. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  10591. }
  10592. else {
  10593. tex = cmd.texture;
  10594. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), tMatrix);
  10595. }
  10596. break;
  10597. case DrawTextureCmd.ID:
  10598. var drawMatrix;
  10599. if (cmd.matrix) {
  10600. tMatrix.copyTo(tempMatrix);
  10601. tempMatrix.concat(cmd.matrix);
  10602. drawMatrix = tempMatrix;
  10603. }
  10604. else {
  10605. drawMatrix = tMatrix;
  10606. }
  10607. if (realSize) {
  10608. if (cmd.width && cmd.height) {
  10609. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), drawMatrix);
  10610. }
  10611. else {
  10612. tex = cmd.texture;
  10613. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, tex.width, tex.height), drawMatrix);
  10614. }
  10615. }
  10616. else {
  10617. tex = cmd.texture;
  10618. wRate = (cmd.width || tex.sourceWidth) / tex.width;
  10619. hRate = (cmd.height || tex.sourceHeight) / tex.height;
  10620. oWidth = wRate * tex.sourceWidth;
  10621. oHeight = hRate * tex.sourceHeight;
  10622. offX = tex.offsetX > 0 ? tex.offsetX : 0;
  10623. offY = tex.offsetY > 0 ? tex.offsetY : 0;
  10624. offX *= wRate;
  10625. offY *= hRate;
  10626. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - offX, cmd.y - offY, oWidth, oHeight), drawMatrix);
  10627. }
  10628. break;
  10629. case DrawRectCmd.ID:
  10630. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x, cmd.y, cmd.width, cmd.height), tMatrix);
  10631. break;
  10632. case DrawCircleCmd.ID:
  10633. GraphicsBounds._addPointArrToRst(rst, Rectangle._getBoundPointS(cmd.x - cmd.radius, cmd.y - cmd.radius, cmd.radius + cmd.radius, cmd.radius + cmd.radius), tMatrix);
  10634. break;
  10635. case DrawLineCmd.ID:
  10636. GraphicsBounds._tempPoints.length = 0;
  10637. var lineWidth;
  10638. lineWidth = cmd.lineWidth * 0.5;
  10639. if (cmd.fromX == cmd.toX) {
  10640. GraphicsBounds._tempPoints.push(cmd.fromX + lineWidth, cmd.fromY, cmd.toX + lineWidth, cmd.toY, cmd.fromX - lineWidth, cmd.fromY, cmd.toX - lineWidth, cmd.toY);
  10641. }
  10642. else if (cmd.fromY == cmd.toY) {
  10643. GraphicsBounds._tempPoints.push(cmd.fromX, cmd.fromY + lineWidth, cmd.toX, cmd.toY + lineWidth, cmd.fromX, cmd.fromY - lineWidth, cmd.toX, cmd.toY - lineWidth);
  10644. }
  10645. else {
  10646. GraphicsBounds._tempPoints.push(cmd.fromX, cmd.fromY, cmd.toX, cmd.toY);
  10647. }
  10648. GraphicsBounds._addPointArrToRst(rst, GraphicsBounds._tempPoints, tMatrix);
  10649. break;
  10650. case DrawCurvesCmd.ID:
  10651. GraphicsBounds._addPointArrToRst(rst, Bezier.I.getBezierPoints(cmd.points), tMatrix, cmd.x, cmd.y);
  10652. break;
  10653. case DrawLinesCmd.ID:
  10654. case DrawPolyCmd.ID:
  10655. GraphicsBounds._addPointArrToRst(rst, cmd.points, tMatrix, cmd.x, cmd.y);
  10656. break;
  10657. case DrawPathCmd.ID:
  10658. GraphicsBounds._addPointArrToRst(rst, this._getPathPoints(cmd.paths), tMatrix, cmd.x, cmd.y);
  10659. break;
  10660. case DrawPieCmd.ID:
  10661. GraphicsBounds._addPointArrToRst(rst, this._getPiePoints(cmd.x, cmd.y, cmd.radius, cmd.startAngle, cmd.endAngle), tMatrix);
  10662. break;
  10663. case DrawTrianglesCmd.ID:
  10664. GraphicsBounds._addPointArrToRst(rst, this._getTriAngBBXPoints(cmd.vertices), tMatrix);
  10665. break;
  10666. case Draw9GridTexture.ID:
  10667. GraphicsBounds._addPointArrToRst(rst, this._getDraw9GridBBXPoints(cmd), tMatrix);
  10668. break;
  10669. }
  10670. }
  10671. if (rst.length > 200) {
  10672. rst = Utils.copyArray(rst, Rectangle._getWrapRec(rst)._getBoundPoints());
  10673. }
  10674. else if (rst.length > 8)
  10675. rst = GrahamScan.scanPList(rst);
  10676. return rst;
  10677. }
  10678. _switchMatrix(tMatix, tempMatrix) {
  10679. tempMatrix.concat(tMatix);
  10680. tempMatrix.copyTo(tMatix);
  10681. }
  10682. static _addPointArrToRst(rst, points, matrix, dx = 0, dy = 0) {
  10683. var i, len;
  10684. len = points.length;
  10685. for (i = 0; i < len; i += 2) {
  10686. GraphicsBounds._addPointToRst(rst, points[i] + dx, points[i + 1] + dy, matrix);
  10687. }
  10688. }
  10689. static _addPointToRst(rst, x, y, matrix) {
  10690. var _tempPoint = Point.TEMP;
  10691. _tempPoint.setTo(x ? x : 0, y ? y : 0);
  10692. matrix.transformPoint(_tempPoint);
  10693. rst.push(_tempPoint.x, _tempPoint.y);
  10694. }
  10695. _getPiePoints(x, y, radius, startAngle, endAngle) {
  10696. var rst = GraphicsBounds._tempPoints;
  10697. GraphicsBounds._tempPoints.length = 0;
  10698. var k = Math.PI / 180;
  10699. var d1 = endAngle - startAngle;
  10700. if (d1 >= 360 || d1 <= -360) {
  10701. rst.push(x - radius, y - radius);
  10702. rst.push(x + radius, y - radius);
  10703. rst.push(x + radius, y + radius);
  10704. rst.push(x - radius, y + radius);
  10705. return rst;
  10706. }
  10707. rst.push(x, y);
  10708. var delta = d1 % 360;
  10709. if (delta < 0)
  10710. delta += 360;
  10711. var end1 = startAngle + delta;
  10712. var st = startAngle * k;
  10713. var ed = end1 * k;
  10714. rst.push(x + radius * Math.cos(st), y + radius * Math.sin(st));
  10715. rst.push(x + radius * Math.cos(ed), y + radius * Math.sin(ed));
  10716. var s1 = Math.ceil(startAngle / 90) * 90;
  10717. var s2 = Math.floor(end1 / 90) * 90;
  10718. for (var cs = s1; cs <= s2; cs += 90) {
  10719. var csr = cs * k;
  10720. rst.push(x + radius * Math.cos(csr), y + radius * Math.sin(csr));
  10721. }
  10722. return rst;
  10723. }
  10724. _getTriAngBBXPoints(vert) {
  10725. var vnum = vert.length;
  10726. if (vnum < 2)
  10727. return [];
  10728. var minx = vert[0];
  10729. var miny = vert[1];
  10730. var maxx = minx;
  10731. var maxy = miny;
  10732. for (var i = 2; i < vnum;) {
  10733. var cx = vert[i++];
  10734. var cy = vert[i++];
  10735. if (minx > cx)
  10736. minx = cx;
  10737. if (miny > cy)
  10738. miny = cy;
  10739. if (maxx < cx)
  10740. maxx = cx;
  10741. if (maxy < cy)
  10742. maxy = cy;
  10743. }
  10744. return [minx, miny, maxx, miny, maxx, maxy, minx, maxy];
  10745. }
  10746. _getDraw9GridBBXPoints(cmd) {
  10747. var minx = 0;
  10748. var miny = 0;
  10749. var maxx = cmd.width;
  10750. var maxy = cmd.height;
  10751. return [minx, miny, maxx, miny, maxx, maxy, minx, maxy];
  10752. }
  10753. _getPathPoints(paths) {
  10754. var i, len;
  10755. var rst = GraphicsBounds._tempPoints;
  10756. rst.length = 0;
  10757. len = paths.length;
  10758. var tCMD;
  10759. for (i = 0; i < len; i++) {
  10760. tCMD = paths[i];
  10761. if (tCMD.length > 1) {
  10762. rst.push(tCMD[1], tCMD[2]);
  10763. if (tCMD.length > 3) {
  10764. rst.push(tCMD[3], tCMD[4]);
  10765. }
  10766. }
  10767. }
  10768. return rst;
  10769. }
  10770. }
  10771. GraphicsBounds._tempMatrix = new Matrix();
  10772. GraphicsBounds._initMatrix = new Matrix();
  10773. GraphicsBounds._tempPoints = [];
  10774. GraphicsBounds._tempMatrixArrays = [];
  10775. GraphicsBounds._tempCmds = [];
  10776. class SpriteConst {
  10777. }
  10778. SpriteConst.ALPHA = 0x01;
  10779. SpriteConst.TRANSFORM = 0x02;
  10780. SpriteConst.BLEND = 0x04;
  10781. SpriteConst.CANVAS = 0x08;
  10782. SpriteConst.FILTERS = 0x10;
  10783. SpriteConst.MASK = 0x20;
  10784. SpriteConst.CLIP = 0x40;
  10785. SpriteConst.STYLE = 0x80;
  10786. SpriteConst.TEXTURE = 0x100;
  10787. SpriteConst.GRAPHICS = 0x200;
  10788. SpriteConst.LAYAGL3D = 0x400;
  10789. SpriteConst.CUSTOM = 0x800;
  10790. SpriteConst.ONECHILD = 0x1000;
  10791. SpriteConst.CHILDS = 0x2000;
  10792. SpriteConst.REPAINT_NONE = 0;
  10793. SpriteConst.REPAINT_NODE = 0x01;
  10794. SpriteConst.REPAINT_CACHE = 0x02;
  10795. SpriteConst.REPAINT_ALL = 0x03;
  10796. class ClipRectCmd {
  10797. static create(x, y, width, height) {
  10798. var cmd = Pool.getItemByClass("ClipRectCmd", ClipRectCmd);
  10799. cmd.x = x;
  10800. cmd.y = y;
  10801. cmd.width = width;
  10802. cmd.height = height;
  10803. return cmd;
  10804. }
  10805. recover() {
  10806. Pool.recover("ClipRectCmd", this);
  10807. }
  10808. run(context, gx, gy) {
  10809. context.clipRect(this.x + gx, this.y + gy, this.width, this.height);
  10810. }
  10811. get cmdID() {
  10812. return ClipRectCmd.ID;
  10813. }
  10814. }
  10815. ClipRectCmd.ID = "ClipRect";
  10816. class DrawTexturesCmd {
  10817. static create(texture, pos) {
  10818. var cmd = Pool.getItemByClass("DrawTexturesCmd", DrawTexturesCmd);
  10819. cmd.texture = texture;
  10820. texture._addReference();
  10821. cmd.pos = pos;
  10822. return cmd;
  10823. }
  10824. recover() {
  10825. this.texture._removeReference();
  10826. this.texture = null;
  10827. this.pos = null;
  10828. Pool.recover("DrawTexturesCmd", this);
  10829. }
  10830. run(context, gx, gy) {
  10831. context.drawTextures(this.texture, this.pos, gx, gy);
  10832. }
  10833. get cmdID() {
  10834. return DrawTexturesCmd.ID;
  10835. }
  10836. }
  10837. DrawTexturesCmd.ID = "DrawTextures";
  10838. class FillTextCmd {
  10839. constructor() {
  10840. this._textIsWorldText = false;
  10841. this._fontColor = 0xffffffff;
  10842. this._strokeColor = 0;
  10843. this._fontObj = FillTextCmd._defFontObj;
  10844. this._nTexAlign = 0;
  10845. }
  10846. static create(text, words, x, y, font, color, textAlign, lineWidth, borderColor) {
  10847. var cmd = Pool.getItemByClass("FillTextCmd", FillTextCmd);
  10848. cmd.text = text;
  10849. cmd._textIsWorldText = text instanceof WordText;
  10850. cmd._words = words;
  10851. cmd.x = x;
  10852. cmd.y = y;
  10853. cmd.font = font;
  10854. cmd.color = color;
  10855. cmd.textAlign = textAlign;
  10856. cmd._lineWidth = lineWidth;
  10857. cmd._borderColor = borderColor;
  10858. return cmd;
  10859. }
  10860. recover() {
  10861. Pool.recover("FillTextCmd", this);
  10862. }
  10863. run(context, gx, gy) {
  10864. if (ILaya.stage.isGlobalRepaint()) {
  10865. this._textIsWorldText && this._text.cleanCache();
  10866. }
  10867. if (this._words) {
  10868. Context._textRender.fillWords(context, this._words, this.x + gx, this.y + gy, this._fontObj, this._color, this._borderColor, this._lineWidth);
  10869. }
  10870. else {
  10871. if (this._textIsWorldText) {
  10872. context._fast_filltext(this._text, this.x + gx, this.y + gy, this._fontObj, this._color, this._borderColor, this._lineWidth, this._nTexAlign, 0);
  10873. }
  10874. else {
  10875. Context._textRender.filltext(context, this._text, this.x + gx, this.y + gy, this.font, this.color, this._borderColor, this._lineWidth, this._textAlign);
  10876. }
  10877. }
  10878. }
  10879. get cmdID() {
  10880. return FillTextCmd.ID;
  10881. }
  10882. get text() {
  10883. return this._text;
  10884. }
  10885. set text(value) {
  10886. this._text = value;
  10887. this._textIsWorldText = value instanceof WordText;
  10888. this._textIsWorldText && this._text.cleanCache();
  10889. }
  10890. get font() {
  10891. return this._font;
  10892. }
  10893. set font(value) {
  10894. this._font = value;
  10895. this._fontObj = FontInfo.Parse(value);
  10896. this._textIsWorldText && this._text.cleanCache();
  10897. }
  10898. get color() {
  10899. return this._color;
  10900. }
  10901. set color(value) {
  10902. this._color = value;
  10903. this._fontColor = ColorUtils.create(value).numColor;
  10904. this._textIsWorldText && this._text.cleanCache();
  10905. }
  10906. get textAlign() {
  10907. return this._textAlign;
  10908. }
  10909. set textAlign(value) {
  10910. this._textAlign = value;
  10911. switch (value) {
  10912. case 'center':
  10913. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_CENTER;
  10914. break;
  10915. case 'right':
  10916. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_RIGHT;
  10917. break;
  10918. default:
  10919. this._nTexAlign = ILaya.Context.ENUM_TEXTALIGN_DEFAULT;
  10920. }
  10921. this._textIsWorldText && this._text.cleanCache();
  10922. }
  10923. }
  10924. FillTextCmd.ID = "FillText";
  10925. FillTextCmd._defFontObj = new FontInfo(null);
  10926. class SaveCmd {
  10927. static create() {
  10928. var cmd = Pool.getItemByClass("SaveCmd", SaveCmd);
  10929. return cmd;
  10930. }
  10931. recover() {
  10932. Pool.recover("SaveCmd", this);
  10933. }
  10934. run(context, gx, gy) {
  10935. context.save();
  10936. }
  10937. get cmdID() {
  10938. return SaveCmd.ID;
  10939. }
  10940. }
  10941. SaveCmd.ID = "Save";
  10942. class CacheManger {
  10943. constructor() {
  10944. }
  10945. static regCacheByFunction(disposeFunction, getCacheListFunction) {
  10946. CacheManger.unRegCacheByFunction(disposeFunction, getCacheListFunction);
  10947. var cache;
  10948. cache = { tryDispose: disposeFunction, getCacheList: getCacheListFunction };
  10949. CacheManger._cacheList.push(cache);
  10950. }
  10951. static unRegCacheByFunction(disposeFunction, getCacheListFunction) {
  10952. var i, len;
  10953. len = CacheManger._cacheList.length;
  10954. for (i = 0; i < len; i++) {
  10955. if (CacheManger._cacheList[i].tryDispose == disposeFunction && CacheManger._cacheList[i].getCacheList == getCacheListFunction) {
  10956. CacheManger._cacheList.splice(i, 1);
  10957. return;
  10958. }
  10959. }
  10960. }
  10961. static forceDispose() {
  10962. var i, len = CacheManger._cacheList.length;
  10963. for (i = 0; i < len; i++) {
  10964. CacheManger._cacheList[i].tryDispose(true);
  10965. }
  10966. }
  10967. static beginCheck(waitTime = 15000) {
  10968. ILaya.systemTimer.loop(waitTime, null, CacheManger._checkLoop);
  10969. }
  10970. static stopCheck() {
  10971. ILaya.systemTimer.clear(null, CacheManger._checkLoop);
  10972. }
  10973. static _checkLoop() {
  10974. var cacheList = CacheManger._cacheList;
  10975. if (cacheList.length < 1)
  10976. return;
  10977. var tTime = ILaya.Browser.now();
  10978. var count;
  10979. var len;
  10980. len = count = cacheList.length;
  10981. while (count > 0) {
  10982. CacheManger._index++;
  10983. CacheManger._index = CacheManger._index % len;
  10984. cacheList[CacheManger._index].tryDispose(false);
  10985. if (ILaya.Browser.now() - tTime > CacheManger.loopTimeLimit)
  10986. break;
  10987. count--;
  10988. }
  10989. }
  10990. }
  10991. CacheManger.loopTimeLimit = 2;
  10992. CacheManger._cacheList = [];
  10993. CacheManger._index = 0;
  10994. class VectorGraphManager {
  10995. constructor() {
  10996. this.useDic = {};
  10997. this.shapeDic = {};
  10998. this.shapeLineDic = {};
  10999. this._id = 0;
  11000. this._checkKey = false;
  11001. this._freeIdArray = [];
  11002. CacheManger.regCacheByFunction(this.startDispose.bind(this), this.getCacheList.bind(this));
  11003. }
  11004. static getInstance() {
  11005. return VectorGraphManager.instance = VectorGraphManager.instance || new VectorGraphManager();
  11006. }
  11007. getId() {
  11008. return this._id++;
  11009. }
  11010. addShape(id, shape) {
  11011. this.shapeDic[id] = shape;
  11012. if (!this.useDic[id]) {
  11013. this.useDic[id] = true;
  11014. }
  11015. }
  11016. addLine(id, Line) {
  11017. this.shapeLineDic[id] = Line;
  11018. if (!this.shapeLineDic[id]) {
  11019. this.shapeLineDic[id] = true;
  11020. }
  11021. }
  11022. getShape(id) {
  11023. if (this._checkKey) {
  11024. if (this.useDic[id] != null) {
  11025. this.useDic[id] = true;
  11026. }
  11027. }
  11028. }
  11029. deleteShape(id) {
  11030. if (this.shapeDic[id]) {
  11031. this.shapeDic[id] = null;
  11032. delete this.shapeDic[id];
  11033. }
  11034. if (this.shapeLineDic[id]) {
  11035. this.shapeLineDic[id] = null;
  11036. delete this.shapeLineDic[id];
  11037. }
  11038. if (this.useDic[id] != null) {
  11039. delete this.useDic[id];
  11040. }
  11041. }
  11042. getCacheList() {
  11043. var str;
  11044. var list = [];
  11045. for (str in this.shapeDic) {
  11046. list.push(this.shapeDic[str]);
  11047. }
  11048. for (str in this.shapeLineDic) {
  11049. list.push(this.shapeLineDic[str]);
  11050. }
  11051. return list;
  11052. }
  11053. startDispose(key) {
  11054. var str;
  11055. for (str in this.useDic) {
  11056. this.useDic[str] = false;
  11057. }
  11058. this._checkKey = true;
  11059. }
  11060. endDispose() {
  11061. if (this._checkKey) {
  11062. var str;
  11063. for (str in this.useDic) {
  11064. if (!this.useDic[str]) {
  11065. this.deleteShape(str);
  11066. }
  11067. }
  11068. this._checkKey = false;
  11069. }
  11070. }
  11071. }
  11072. class Graphics {
  11073. constructor() {
  11074. this._sp = null;
  11075. this._one = null;
  11076. this._render = this._renderEmpty;
  11077. this._cmds = null;
  11078. this._vectorgraphArray = null;
  11079. this._graphicBounds = null;
  11080. this.autoDestroy = false;
  11081. this._createData();
  11082. }
  11083. _createData() {
  11084. }
  11085. _clearData() {
  11086. }
  11087. _destroyData() {
  11088. }
  11089. destroy() {
  11090. this.clear(true);
  11091. if (this._graphicBounds)
  11092. this._graphicBounds.destroy();
  11093. this._graphicBounds = null;
  11094. this._vectorgraphArray = null;
  11095. if (this._sp) {
  11096. this._sp._renderType = 0;
  11097. this._sp._setRenderType(0);
  11098. this._sp = null;
  11099. }
  11100. this._destroyData();
  11101. }
  11102. clear(recoverCmds = true) {
  11103. if (recoverCmds) {
  11104. var tCmd = this._one;
  11105. if (this._cmds) {
  11106. var i, len = this._cmds.length;
  11107. for (i = 0; i < len; i++) {
  11108. tCmd = this._cmds[i];
  11109. tCmd.recover();
  11110. }
  11111. this._cmds.length = 0;
  11112. }
  11113. else if (tCmd) {
  11114. tCmd.recover();
  11115. }
  11116. }
  11117. else {
  11118. this._cmds = null;
  11119. }
  11120. this._one = null;
  11121. this._render = this._renderEmpty;
  11122. this._clearData();
  11123. if (this._sp) {
  11124. this._sp._renderType &= ~SpriteConst.GRAPHICS;
  11125. this._sp._setRenderType(this._sp._renderType);
  11126. }
  11127. this._repaint();
  11128. if (this._vectorgraphArray) {
  11129. for (i = 0, len = this._vectorgraphArray.length; i < len; i++) {
  11130. VectorGraphManager.getInstance().deleteShape(this._vectorgraphArray[i]);
  11131. }
  11132. this._vectorgraphArray.length = 0;
  11133. }
  11134. }
  11135. _clearBoundsCache() {
  11136. if (this._graphicBounds)
  11137. this._graphicBounds.reset();
  11138. }
  11139. _initGraphicBounds() {
  11140. if (!this._graphicBounds) {
  11141. this._graphicBounds = GraphicsBounds.create();
  11142. this._graphicBounds._graphics = this;
  11143. }
  11144. }
  11145. _repaint() {
  11146. this._clearBoundsCache();
  11147. this._sp && this._sp.repaint();
  11148. }
  11149. _isOnlyOne() {
  11150. return !this._cmds || this._cmds.length === 0;
  11151. }
  11152. get cmds() {
  11153. return this._cmds;
  11154. }
  11155. set cmds(value) {
  11156. if (this._sp) {
  11157. this._sp._renderType |= SpriteConst.GRAPHICS;
  11158. this._sp._setRenderType(this._sp._renderType);
  11159. }
  11160. this._cmds = value;
  11161. this._render = this._renderAll;
  11162. this._repaint();
  11163. }
  11164. getBounds(realSize = false) {
  11165. this._initGraphicBounds();
  11166. return this._graphicBounds.getBounds(realSize);
  11167. }
  11168. getBoundPoints(realSize = false) {
  11169. this._initGraphicBounds();
  11170. return this._graphicBounds.getBoundPoints(realSize);
  11171. }
  11172. drawImage(texture, x = 0, y = 0, width = 0, height = 0) {
  11173. if (!texture)
  11174. return null;
  11175. if (!width)
  11176. width = texture.sourceWidth;
  11177. if (!height)
  11178. height = texture.sourceHeight;
  11179. if (texture.getIsReady()) {
  11180. var wRate = width / texture.sourceWidth;
  11181. var hRate = height / texture.sourceHeight;
  11182. width = texture.width * wRate;
  11183. height = texture.height * hRate;
  11184. if (width <= 0 || height <= 0)
  11185. return null;
  11186. x += texture.offsetX * wRate;
  11187. y += texture.offsetY * hRate;
  11188. }
  11189. if (this._sp) {
  11190. this._sp._renderType |= SpriteConst.GRAPHICS;
  11191. this._sp._setRenderType(this._sp._renderType);
  11192. }
  11193. var args = DrawImageCmd.create.call(this, texture, x, y, width, height);
  11194. if (this._one == null) {
  11195. this._one = args;
  11196. this._render = this._renderOneImg;
  11197. }
  11198. else {
  11199. this._saveToCmd(null, args);
  11200. }
  11201. this._repaint();
  11202. return args;
  11203. }
  11204. drawTexture(texture, x = 0, y = 0, width = 0, height = 0, matrix = null, alpha = 1, color = null, blendMode = null, uv) {
  11205. if (!texture || alpha < 0.01)
  11206. return null;
  11207. if (!texture.getIsReady())
  11208. return null;
  11209. if (!width)
  11210. width = texture.sourceWidth;
  11211. if (!height)
  11212. height = texture.sourceHeight;
  11213. if (texture.getIsReady()) {
  11214. var wRate = width / texture.sourceWidth;
  11215. var hRate = height / texture.sourceHeight;
  11216. width = texture.width * wRate;
  11217. height = texture.height * hRate;
  11218. if (width <= 0 || height <= 0)
  11219. return null;
  11220. x += texture.offsetX * wRate;
  11221. y += texture.offsetY * hRate;
  11222. }
  11223. if (this._sp) {
  11224. this._sp._renderType |= SpriteConst.GRAPHICS;
  11225. this._sp._setRenderType(this._sp._renderType);
  11226. }
  11227. var args = DrawTextureCmd.create.call(this, texture, x, y, width, height, matrix, alpha, color, blendMode, uv);
  11228. this._repaint();
  11229. return this._saveToCmd(null, args);
  11230. }
  11231. drawTextures(texture, pos) {
  11232. if (!texture)
  11233. return null;
  11234. return this._saveToCmd(Render._context.drawTextures, DrawTexturesCmd.create.call(this, texture, pos));
  11235. }
  11236. drawTriangles(texture, x, y, vertices, uvs, indices, matrix = null, alpha = 1, color = null, blendMode = null, colorNum = undefined) {
  11237. return this._saveToCmd(Render._context.drawTriangles, DrawTrianglesCmd.create.call(this, texture, x, y, vertices, uvs, indices, matrix, alpha, color, blendMode, colorNum));
  11238. }
  11239. fillTexture(texture, x, y, width = 0, height = 0, type = "repeat", offset = null) {
  11240. if (texture && texture.getIsReady())
  11241. return this._saveToCmd(Render._context._fillTexture, FillTextureCmd.create.call(this, texture, x, y, width, height, type, offset || Point.EMPTY, {}));
  11242. else
  11243. return null;
  11244. }
  11245. _saveToCmd(fun, args) {
  11246. if (this._sp) {
  11247. this._sp._renderType |= SpriteConst.GRAPHICS;
  11248. this._sp._setRenderType(this._sp._renderType);
  11249. }
  11250. if (this._one == null) {
  11251. this._one = args;
  11252. this._render = this._renderOne;
  11253. }
  11254. else {
  11255. this._render = this._renderAll;
  11256. (this._cmds || (this._cmds = [])).length === 0 && this._cmds.push(this._one);
  11257. this._cmds.push(args);
  11258. }
  11259. this._repaint();
  11260. return args;
  11261. }
  11262. clipRect(x, y, width, height) {
  11263. return this._saveToCmd(Render._context.clipRect, ClipRectCmd.create.call(this, x, y, width, height));
  11264. }
  11265. fillText(text, x, y, font, color, textAlign) {
  11266. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), color, textAlign, 0, ""));
  11267. }
  11268. fillBorderText(text, x, y, font, fillColor, textAlign, lineWidth, borderColor) {
  11269. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), fillColor, borderColor, lineWidth, textAlign));
  11270. }
  11271. fillWords(words, x, y, font, color) {
  11272. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, null, words, x, y, font || ILaya.Text.defaultFontStr(), color));
  11273. }
  11274. fillBorderWords(words, x, y, font, fillColor, borderColor, lineWidth) {
  11275. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, null, words, x, y, font || ILaya.Text.defaultFontStr(), fillColor, "", lineWidth, borderColor));
  11276. }
  11277. strokeText(text, x, y, font, color, lineWidth, textAlign) {
  11278. return this._saveToCmd(Render._context.fillText, FillTextCmd.create.call(this, text, null, x, y, font || ILaya.Text.defaultFontStr(), null, textAlign, lineWidth, color));
  11279. }
  11280. alpha(alpha) {
  11281. return this._saveToCmd(Render._context.alpha, AlphaCmd.create.call(this, alpha));
  11282. }
  11283. transform(matrix, pivotX = 0, pivotY = 0) {
  11284. return this._saveToCmd(Render._context._transform, TransformCmd.create.call(this, matrix, pivotX, pivotY));
  11285. }
  11286. rotate(angle, pivotX = 0, pivotY = 0) {
  11287. return this._saveToCmd(Render._context._rotate, RotateCmd.create.call(this, angle, pivotX, pivotY));
  11288. }
  11289. scale(scaleX, scaleY, pivotX = 0, pivotY = 0) {
  11290. return this._saveToCmd(Render._context._scale, ScaleCmd.create.call(this, scaleX, scaleY, pivotX, pivotY));
  11291. }
  11292. translate(tx, ty) {
  11293. return this._saveToCmd(Render._context.translate, TranslateCmd.create.call(this, tx, ty));
  11294. }
  11295. save() {
  11296. return this._saveToCmd(Render._context._save, SaveCmd.create.call(this));
  11297. }
  11298. restore() {
  11299. return this._saveToCmd(Render._context.restore, RestoreCmd.create.call(this));
  11300. }
  11301. replaceText(text) {
  11302. this._repaint();
  11303. var cmds = this._cmds;
  11304. if (!cmds) {
  11305. if (this._one && this._isTextCmd(this._one)) {
  11306. this._one.text = text;
  11307. return true;
  11308. }
  11309. }
  11310. else {
  11311. for (var i = cmds.length - 1; i > -1; i--) {
  11312. if (this._isTextCmd(cmds[i])) {
  11313. cmds[i].text = text;
  11314. return true;
  11315. }
  11316. }
  11317. }
  11318. return false;
  11319. }
  11320. _isTextCmd(cmd) {
  11321. var cmdID = cmd.cmdID;
  11322. return cmdID == FillTextCmd.ID;
  11323. }
  11324. replaceTextColor(color) {
  11325. this._repaint();
  11326. var cmds = this._cmds;
  11327. if (!cmds) {
  11328. if (this._one && this._isTextCmd(this._one)) {
  11329. this._setTextCmdColor(this._one, color);
  11330. }
  11331. }
  11332. else {
  11333. for (var i = cmds.length - 1; i > -1; i--) {
  11334. if (this._isTextCmd(cmds[i])) {
  11335. this._setTextCmdColor(cmds[i], color);
  11336. }
  11337. }
  11338. }
  11339. }
  11340. _setTextCmdColor(cmdO, color) {
  11341. var cmdID = cmdO.cmdID;
  11342. switch (cmdID) {
  11343. case FillTextCmd.ID:
  11344. cmdO.color = color;
  11345. break;
  11346. }
  11347. }
  11348. loadImage(url, x = 0, y = 0, width = 0, height = 0, complete = null) {
  11349. var tex = ILaya.Loader.getRes(url);
  11350. if (!tex) {
  11351. tex = new Texture();
  11352. tex.load(url);
  11353. ILaya.Loader.cacheTexture(url, tex);
  11354. tex.once(Event.READY, this, this.drawImage, [tex, x, y, width, height]);
  11355. }
  11356. else {
  11357. if (!tex.getIsReady()) {
  11358. tex.once(Event.READY, this, this.drawImage, [tex, x, y, width, height]);
  11359. }
  11360. else
  11361. this.drawImage(tex, x, y, width, height);
  11362. }
  11363. if (complete != null) {
  11364. tex.getIsReady() ? complete.call(this._sp) : tex.on(Event.READY, this._sp, complete);
  11365. }
  11366. }
  11367. _renderEmpty(sprite, context, x, y) {
  11368. }
  11369. _renderAll(sprite, context, x, y) {
  11370. var cmds = this._cmds;
  11371. for (var i = 0, n = cmds.length; i < n; i++) {
  11372. cmds[i].run(context, x, y);
  11373. }
  11374. }
  11375. _renderOne(sprite, context, x, y) {
  11376. context.sprite = sprite;
  11377. this._one.run(context, x, y);
  11378. }
  11379. _renderOneImg(sprite, context, x, y) {
  11380. context.sprite = sprite;
  11381. this._one.run(context, x, y);
  11382. }
  11383. drawLine(fromX, fromY, toX, toY, lineColor, lineWidth = 1) {
  11384. var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
  11385. return this._saveToCmd(Render._context._drawLine, DrawLineCmd.create.call(this, fromX + offset, fromY + offset, toX + offset, toY + offset, lineColor, lineWidth, 0));
  11386. }
  11387. drawLines(x, y, points, lineColor, lineWidth = 1) {
  11388. if (!points || points.length < 4)
  11389. return null;
  11390. var offset = (lineWidth < 1 || lineWidth % 2 === 0) ? 0 : 0.5;
  11391. return this._saveToCmd(Render._context._drawLines, DrawLinesCmd.create.call(this, x + offset, y + offset, points, lineColor, lineWidth, 0));
  11392. }
  11393. drawCurves(x, y, points, lineColor, lineWidth = 1) {
  11394. return this._saveToCmd(Render._context.drawCurves, DrawCurvesCmd.create.call(this, x, y, points, lineColor, lineWidth));
  11395. }
  11396. drawRect(x, y, width, height, fillColor, lineColor = null, lineWidth = 1) {
  11397. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  11398. var lineOffset = lineColor ? lineWidth : 0;
  11399. return this._saveToCmd(Render._context.drawRect, DrawRectCmd.create.call(this, x + offset, y + offset, width - lineOffset, height - lineOffset, fillColor, lineColor, lineWidth));
  11400. }
  11401. drawCircle(x, y, radius, fillColor, lineColor = null, lineWidth = 1) {
  11402. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  11403. return this._saveToCmd(Render._context._drawCircle, DrawCircleCmd.create.call(this, x, y, radius - offset, fillColor, lineColor, lineWidth, 0));
  11404. }
  11405. drawPie(x, y, radius, startAngle, endAngle, fillColor, lineColor = null, lineWidth = 1) {
  11406. var offset = (lineWidth >= 1 && lineColor) ? lineWidth / 2 : 0;
  11407. var lineOffset = lineColor ? lineWidth : 0;
  11408. 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));
  11409. }
  11410. drawPoly(x, y, points, fillColor, lineColor = null, lineWidth = 1) {
  11411. var tIsConvexPolygon = false;
  11412. if (points.length > 6) {
  11413. tIsConvexPolygon = false;
  11414. }
  11415. else {
  11416. tIsConvexPolygon = true;
  11417. }
  11418. var offset = (lineWidth >= 1 && lineColor) ? (lineWidth % 2 === 0 ? 0 : 0.5) : 0;
  11419. return this._saveToCmd(Render._context._drawPoly, DrawPolyCmd.create.call(this, x + offset, y + offset, points, fillColor, lineColor, lineWidth, tIsConvexPolygon, 0));
  11420. }
  11421. drawPath(x, y, paths, brush = null, pen = null) {
  11422. return this._saveToCmd(Render._context._drawPath, DrawPathCmd.create.call(this, x, y, paths, brush, pen));
  11423. }
  11424. draw9Grid(texture, x = 0, y = 0, width = 0, height = 0, sizeGrid = null) {
  11425. this._saveToCmd(null, Draw9GridTexture.create(texture, x, y, width, height, sizeGrid));
  11426. }
  11427. }
  11428. class Const {
  11429. }
  11430. Const.NOT_ACTIVE = 0x01;
  11431. Const.ACTIVE_INHIERARCHY = 0x02;
  11432. Const.AWAKED = 0x04;
  11433. Const.NOT_READY = 0x08;
  11434. Const.DISPLAY = 0x10;
  11435. Const.HAS_ZORDER = 0x20;
  11436. Const.HAS_MOUSE = 0x40;
  11437. Const.DISPLAYED_INSTAGE = 0x80;
  11438. Const.DRAWCALL_OPTIMIZE = 0x100;
  11439. class LayaGLQuickRunner {
  11440. static __init__() {
  11441. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TRANSFORM | SpriteConst.GRAPHICS] = LayaGLQuickRunner.alpha_transform_drawLayaGL;
  11442. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.GRAPHICS] = LayaGLQuickRunner.alpha_drawLayaGL;
  11443. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.GRAPHICS] = LayaGLQuickRunner.transform_drawLayaGL;
  11444. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.CHILDS] = LayaGLQuickRunner.transform_drawNodes;
  11445. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TRANSFORM | SpriteConst.TEXTURE] = LayaGLQuickRunner.alpha_transform_drawTexture;
  11446. LayaGLQuickRunner.map[SpriteConst.ALPHA | SpriteConst.TEXTURE] = LayaGLQuickRunner.alpha_drawTexture;
  11447. LayaGLQuickRunner.map[SpriteConst.TRANSFORM | SpriteConst.TEXTURE] = LayaGLQuickRunner.transform_drawTexture;
  11448. LayaGLQuickRunner.map[SpriteConst.GRAPHICS | SpriteConst.CHILDS] = LayaGLQuickRunner.drawLayaGL_drawNodes;
  11449. }
  11450. static transform_drawTexture(sprite, context, x, y) {
  11451. var style = sprite._style;
  11452. var tex = sprite.texture;
  11453. context.saveTransform(LayaGLQuickRunner.curMat);
  11454. context.transformByMatrix(sprite.transform, x, y);
  11455. context.drawTexture(tex, -sprite.pivotX, -sprite.pivotY, sprite._width || tex.width, sprite._height || tex.height);
  11456. context.restoreTransform(LayaGLQuickRunner.curMat);
  11457. }
  11458. static alpha_drawTexture(sprite, context, x, y) {
  11459. var style = sprite._style;
  11460. var alpha;
  11461. var tex = sprite.texture;
  11462. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  11463. var temp = context.globalAlpha;
  11464. context.globalAlpha *= alpha;
  11465. context.drawTexture(tex, x - style.pivotX + tex.offsetX, y - style.pivotY + tex.offsetY, sprite._width || tex.width, sprite._height || tex.height);
  11466. context.globalAlpha = temp;
  11467. }
  11468. }
  11469. static alpha_transform_drawTexture(sprite, context, x, y) {
  11470. var style = sprite._style;
  11471. var alpha;
  11472. var tex = sprite.texture;
  11473. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  11474. var temp = context.globalAlpha;
  11475. context.globalAlpha *= alpha;
  11476. context.saveTransform(LayaGLQuickRunner.curMat);
  11477. context.transformByMatrix(sprite.transform, x, y);
  11478. context.drawTexture(tex, -style.pivotX + tex.offsetX, -style.pivotY + tex.offsetY, sprite._width || tex.width, sprite._height || tex.height);
  11479. context.restoreTransform(LayaGLQuickRunner.curMat);
  11480. context.globalAlpha = temp;
  11481. }
  11482. }
  11483. static alpha_transform_drawLayaGL(sprite, context, x, y) {
  11484. var style = sprite._style;
  11485. var alpha;
  11486. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  11487. var temp = context.globalAlpha;
  11488. context.globalAlpha *= alpha;
  11489. context.saveTransform(LayaGLQuickRunner.curMat);
  11490. context.transformByMatrix(sprite.transform, x, y);
  11491. sprite._graphics && sprite._graphics._render(sprite, context, -style.pivotX, -style.pivotY);
  11492. context.restoreTransform(LayaGLQuickRunner.curMat);
  11493. context.globalAlpha = temp;
  11494. }
  11495. }
  11496. static alpha_drawLayaGL(sprite, context, x, y) {
  11497. var style = sprite._style;
  11498. var alpha;
  11499. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  11500. var temp = context.globalAlpha;
  11501. context.globalAlpha *= alpha;
  11502. sprite._graphics && sprite._graphics._render(sprite, context, x - style.pivotX, y - style.pivotY);
  11503. context.globalAlpha = temp;
  11504. }
  11505. }
  11506. static transform_drawLayaGL(sprite, context, x, y) {
  11507. var style = sprite._style;
  11508. context.saveTransform(LayaGLQuickRunner.curMat);
  11509. context.transformByMatrix(sprite.transform, x, y);
  11510. sprite._graphics && sprite._graphics._render(sprite, context, -style.pivotX, -style.pivotY);
  11511. context.restoreTransform(LayaGLQuickRunner.curMat);
  11512. }
  11513. static transform_drawNodes(sprite, context, x, y) {
  11514. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  11515. var style = sprite._style;
  11516. context.saveTransform(LayaGLQuickRunner.curMat);
  11517. context.transformByMatrix(sprite.transform, x, y);
  11518. x = -style.pivotX;
  11519. y = -style.pivotY;
  11520. var childs = sprite._children, n = childs.length, ele;
  11521. if (style.viewport) {
  11522. var rect = style.viewport;
  11523. var left = rect.x;
  11524. var top = rect.y;
  11525. var right = rect.right;
  11526. var bottom = rect.bottom;
  11527. var _x, _y;
  11528. for (i = 0; i < n; ++i) {
  11529. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  11530. ele.render(context, x, y);
  11531. }
  11532. }
  11533. }
  11534. else {
  11535. for (var i = 0; i < n; ++i)
  11536. (ele = childs[i])._visible && ele.render(context, x, y);
  11537. }
  11538. context.restoreTransform(LayaGLQuickRunner.curMat);
  11539. textLastRender && context.drawCallOptimize(false);
  11540. }
  11541. static drawLayaGL_drawNodes(sprite, context, x, y) {
  11542. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  11543. var style = sprite._style;
  11544. x = x - style.pivotX;
  11545. y = y - style.pivotY;
  11546. sprite._graphics && sprite._graphics._render(sprite, context, x, y);
  11547. var childs = sprite._children, n = childs.length, ele;
  11548. if (style.viewport) {
  11549. var rect = style.viewport;
  11550. var left = rect.x;
  11551. var top = rect.y;
  11552. var right = rect.right;
  11553. var bottom = rect.bottom;
  11554. var _x, _y;
  11555. for (i = 0; i < n; ++i) {
  11556. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  11557. ele.render(context, x, y);
  11558. }
  11559. }
  11560. }
  11561. else {
  11562. for (var i = 0; i < n; ++i)
  11563. (ele = childs[i])._visible && ele.render(context, x, y);
  11564. }
  11565. textLastRender && context.drawCallOptimize(false);
  11566. }
  11567. }
  11568. LayaGLQuickRunner.map = {};
  11569. LayaGLQuickRunner.curMat = new Matrix();
  11570. class RenderSprite {
  11571. constructor(type, next) {
  11572. if (LayaGLQuickRunner.map[type]) {
  11573. this._fun = LayaGLQuickRunner.map[type];
  11574. this._next = RenderSprite.NORENDER;
  11575. return;
  11576. }
  11577. this._next = next || RenderSprite.NORENDER;
  11578. switch (type) {
  11579. case 0:
  11580. this._fun = this._no;
  11581. return;
  11582. case SpriteConst.ALPHA:
  11583. this._fun = this._alpha;
  11584. return;
  11585. case SpriteConst.TRANSFORM:
  11586. this._fun = this._transform;
  11587. return;
  11588. case SpriteConst.BLEND:
  11589. this._fun = this._blend;
  11590. return;
  11591. case SpriteConst.CANVAS:
  11592. this._fun = this._canvas;
  11593. return;
  11594. case SpriteConst.MASK:
  11595. this._fun = this._mask;
  11596. return;
  11597. case SpriteConst.CLIP:
  11598. this._fun = this._clip;
  11599. return;
  11600. case SpriteConst.STYLE:
  11601. this._fun = this._style;
  11602. return;
  11603. case SpriteConst.GRAPHICS:
  11604. this._fun = this._graphics;
  11605. return;
  11606. case SpriteConst.CHILDS:
  11607. this._fun = this._children;
  11608. return;
  11609. case SpriteConst.CUSTOM:
  11610. this._fun = this._custom;
  11611. return;
  11612. case SpriteConst.TEXTURE:
  11613. this._fun = this._texture;
  11614. return;
  11615. case SpriteConst.FILTERS:
  11616. this._fun = Filter._filter;
  11617. return;
  11618. case RenderSprite.INIT:
  11619. this._fun = RenderSprite._initRenderFun;
  11620. return;
  11621. }
  11622. this.onCreate(type);
  11623. }
  11624. static __init__() {
  11625. LayaGLQuickRunner.__init__();
  11626. var i, len;
  11627. var initRender;
  11628. initRender = new RenderSprite(RenderSprite.INIT, null);
  11629. len = RenderSprite.renders.length = SpriteConst.CHILDS * 2;
  11630. for (i = 0; i < len; i++)
  11631. RenderSprite.renders[i] = initRender;
  11632. RenderSprite.renders[0] = new RenderSprite(0, null);
  11633. }
  11634. static _initRenderFun(sprite, context, x, y) {
  11635. var type = sprite._renderType;
  11636. var r = RenderSprite.renders[type] = RenderSprite._getTypeRender(type);
  11637. r._fun(sprite, context, x, y);
  11638. }
  11639. static _getTypeRender(type) {
  11640. if (LayaGLQuickRunner.map[type])
  11641. return new RenderSprite(type, null);
  11642. var rst = null;
  11643. var tType = SpriteConst.CHILDS;
  11644. while (tType > 0) {
  11645. if (tType & type)
  11646. rst = new RenderSprite(tType, rst);
  11647. tType = tType >> 1;
  11648. }
  11649. return rst;
  11650. }
  11651. onCreate(type) {
  11652. }
  11653. _style(sprite, context, x, y) {
  11654. var style = sprite._style;
  11655. if (style.render != null)
  11656. style.render(sprite, context, x, y);
  11657. var next = this._next;
  11658. next._fun.call(next, sprite, context, x, y);
  11659. }
  11660. _no(sprite, context, x, y) {
  11661. }
  11662. _custom(sprite, context, x, y) {
  11663. sprite.customRender(context, x, y);
  11664. this._next._fun.call(this._next, sprite, context, x - sprite.pivotX, y - sprite.pivotY);
  11665. }
  11666. _clip(sprite, context, x, y) {
  11667. var next = this._next;
  11668. if (next == RenderSprite.NORENDER)
  11669. return;
  11670. var r = sprite._style.scrollRect;
  11671. context.save();
  11672. context.clipRect(x, y, r.width, r.height);
  11673. next._fun.call(next, sprite, context, x - r.x, y - r.y);
  11674. context.restore();
  11675. }
  11676. _texture(sprite, context, x, y) {
  11677. var tex = sprite.texture;
  11678. if (tex._getSource())
  11679. context.drawTexture(tex, x - sprite.pivotX + tex.offsetX, y - sprite.pivotY + tex.offsetY, sprite._width || tex.width, sprite._height || tex.height);
  11680. var next = this._next;
  11681. if (next != RenderSprite.NORENDER)
  11682. next._fun.call(next, sprite, context, x, y);
  11683. }
  11684. _graphics(sprite, context, x, y) {
  11685. var style = sprite._style;
  11686. var g = sprite._graphics;
  11687. g && g._render(sprite, context, x - style.pivotX, y - style.pivotY);
  11688. var next = this._next;
  11689. if (next != RenderSprite.NORENDER)
  11690. next._fun.call(next, sprite, context, x, y);
  11691. }
  11692. _image(sprite, context, x, y) {
  11693. var style = sprite._style;
  11694. context.drawTexture2(x, y, style.pivotX, style.pivotY, sprite.transform, sprite._graphics._one);
  11695. }
  11696. _image2(sprite, context, x, y) {
  11697. var style = sprite._style;
  11698. context.drawTexture2(x, y, style.pivotX, style.pivotY, sprite.transform, sprite._graphics._one);
  11699. }
  11700. _alpha(sprite, context, x, y) {
  11701. var style = sprite._style;
  11702. var alpha;
  11703. if ((alpha = style.alpha) > 0.01 || sprite._needRepaint()) {
  11704. var temp = context.globalAlpha;
  11705. context.globalAlpha *= alpha;
  11706. var next = this._next;
  11707. next._fun.call(next, sprite, context, x, y);
  11708. context.globalAlpha = temp;
  11709. }
  11710. }
  11711. _transform(sprite, context, x, y) {
  11712. var transform = sprite.transform, _next = this._next;
  11713. var style = sprite._style;
  11714. if (transform && _next != RenderSprite.NORENDER) {
  11715. context.save();
  11716. context.transform(transform.a, transform.b, transform.c, transform.d, transform.tx + x, transform.ty + y);
  11717. _next._fun.call(_next, sprite, context, 0, 0);
  11718. context.restore();
  11719. }
  11720. else {
  11721. if (_next != RenderSprite.NORENDER)
  11722. _next._fun.call(_next, sprite, context, x, y);
  11723. }
  11724. }
  11725. _children(sprite, context, x, y) {
  11726. var style = sprite._style;
  11727. var childs = sprite._children, n = childs.length, ele;
  11728. x = x - sprite.pivotX;
  11729. y = y - sprite.pivotY;
  11730. var textLastRender = sprite._getBit(Const.DRAWCALL_OPTIMIZE) && context.drawCallOptimize(true);
  11731. if (style.viewport) {
  11732. var rect = style.viewport;
  11733. var left = rect.x;
  11734. var top = rect.y;
  11735. var right = rect.right;
  11736. var bottom = rect.bottom;
  11737. var _x, _y;
  11738. for (i = 0; i < n; ++i) {
  11739. if ((ele = childs[i])._visible && ((_x = ele._x) < right && (_x + ele.width) > left && (_y = ele._y) < bottom && (_y + ele.height) > top)) {
  11740. ele.render(context, x, y);
  11741. }
  11742. }
  11743. }
  11744. else {
  11745. for (var i = 0; i < n; ++i)
  11746. (ele = childs[i])._visible && ele.render(context, x, y);
  11747. }
  11748. textLastRender && context.drawCallOptimize(false);
  11749. }
  11750. _canvas(sprite, context, x, y) {
  11751. var _cacheStyle = sprite._cacheStyle;
  11752. var _next = this._next;
  11753. if (!_cacheStyle.enableCanvasRender) {
  11754. _next._fun.call(_next, sprite, context, x, y);
  11755. return;
  11756. }
  11757. _cacheStyle.cacheAs === 'bitmap' ? (Stat.canvasBitmap++) : (Stat.canvasNormal++);
  11758. var cacheNeedRebuild = false;
  11759. var textNeedRestore = false;
  11760. if (_cacheStyle.canvas) {
  11761. var canv = _cacheStyle.canvas;
  11762. var ctx = canv.context;
  11763. var charRIs = canv.touches;
  11764. if (charRIs) {
  11765. for (var ci = 0; ci < charRIs.length; ci++) {
  11766. if (charRIs[ci].deleted) {
  11767. textNeedRestore = true;
  11768. break;
  11769. }
  11770. }
  11771. }
  11772. cacheNeedRebuild = canv.isCacheValid && !canv.isCacheValid();
  11773. }
  11774. if (sprite._needRepaint() || (!_cacheStyle.canvas) || textNeedRestore || cacheNeedRebuild || ILaya.stage.isGlobalRepaint()) {
  11775. if (_cacheStyle.cacheAs === 'normal') {
  11776. if (context._targets) {
  11777. _next._fun.call(_next, sprite, context, x, y);
  11778. return;
  11779. }
  11780. else {
  11781. this._canvas_webgl_normal_repaint(sprite, context);
  11782. }
  11783. }
  11784. else {
  11785. this._canvas_repaint(sprite, context, x, y);
  11786. }
  11787. }
  11788. var tRec = _cacheStyle.cacheRect;
  11789. context.drawCanvas(_cacheStyle.canvas, x + tRec.x, y + tRec.y, tRec.width, tRec.height);
  11790. }
  11791. _canvas_repaint(sprite, context, x, y) {
  11792. var _cacheStyle = sprite._cacheStyle;
  11793. var _next = this._next;
  11794. var tx;
  11795. var canvas = _cacheStyle.canvas;
  11796. var left;
  11797. var top;
  11798. var tRec;
  11799. var tCacheType = _cacheStyle.cacheAs;
  11800. var w, h;
  11801. var scaleX, scaleY;
  11802. var scaleInfo;
  11803. scaleInfo = _cacheStyle._calculateCacheRect(sprite, tCacheType, x, y);
  11804. scaleX = scaleInfo.x;
  11805. scaleY = scaleInfo.y;
  11806. tRec = _cacheStyle.cacheRect;
  11807. w = tRec.width * scaleX;
  11808. h = tRec.height * scaleY;
  11809. left = tRec.x;
  11810. top = tRec.y;
  11811. if (tCacheType === 'bitmap' && (w > 2048 || h > 2048)) {
  11812. console.warn("cache bitmap size larger than 2048,cache ignored");
  11813. _cacheStyle.releaseContext();
  11814. _next._fun.call(_next, sprite, context, x, y);
  11815. return;
  11816. }
  11817. if (!canvas) {
  11818. _cacheStyle.createContext();
  11819. canvas = _cacheStyle.canvas;
  11820. }
  11821. tx = canvas.context;
  11822. tx.sprite = sprite;
  11823. (canvas.width != w || canvas.height != h) && canvas.size(w, h);
  11824. if (tCacheType === 'bitmap')
  11825. tx.asBitmap = true;
  11826. else if (tCacheType === 'normal')
  11827. tx.asBitmap = false;
  11828. tx.clear();
  11829. if (scaleX != 1 || scaleY != 1) {
  11830. var ctx = tx;
  11831. ctx.save();
  11832. ctx.scale(scaleX, scaleY);
  11833. _next._fun.call(_next, sprite, tx, -left, -top);
  11834. ctx.restore();
  11835. sprite._applyFilters();
  11836. }
  11837. else {
  11838. ctx = tx;
  11839. _next._fun.call(_next, sprite, tx, -left, -top);
  11840. sprite._applyFilters();
  11841. }
  11842. if (_cacheStyle.staticCache)
  11843. _cacheStyle.reCache = false;
  11844. Stat.canvasReCache++;
  11845. }
  11846. _canvas_webgl_normal_repaint(sprite, context) {
  11847. var _cacheStyle = sprite._cacheStyle;
  11848. var _next = this._next;
  11849. var canvas = _cacheStyle.canvas;
  11850. var tCacheType = _cacheStyle.cacheAs;
  11851. _cacheStyle._calculateCacheRect(sprite, tCacheType, 0, 0);
  11852. if (!canvas) {
  11853. canvas = _cacheStyle.canvas = new WebGLCacheAsNormalCanvas(context, sprite);
  11854. }
  11855. var tx = canvas.context;
  11856. canvas['startRec']();
  11857. _next._fun.call(_next, sprite, tx, sprite.pivotX, sprite.pivotY);
  11858. sprite._applyFilters();
  11859. Stat.canvasReCache++;
  11860. canvas['endRec']();
  11861. }
  11862. _blend(sprite, context, x, y) {
  11863. var style = sprite._style;
  11864. var next = this._next;
  11865. if (style.blendMode) {
  11866. context.save();
  11867. context.globalCompositeOperation = style.blendMode;
  11868. next._fun.call(next, sprite, context, x, y);
  11869. context.restore();
  11870. }
  11871. else {
  11872. next._fun.call(next, sprite, context, x, y);
  11873. }
  11874. }
  11875. _mask(sprite, context, x, y) {
  11876. var next = this._next;
  11877. var mask = sprite.mask;
  11878. var ctx = context;
  11879. if (mask) {
  11880. ctx.save();
  11881. var preBlendMode = ctx.globalCompositeOperation;
  11882. var tRect = new Rectangle();
  11883. tRect.copyFrom(mask.getBounds());
  11884. tRect.width = Math.round(tRect.width);
  11885. tRect.height = Math.round(tRect.height);
  11886. tRect.x = Math.round(tRect.x);
  11887. tRect.y = Math.round(tRect.y);
  11888. if (tRect.width > 0 && tRect.height > 0) {
  11889. var w = tRect.width;
  11890. var h = tRect.height;
  11891. var tmpRT = WebGLRTMgr.getRT(w, h);
  11892. ctx.breakNextMerge();
  11893. ctx.pushRT();
  11894. ctx.addRenderObject(SubmitCMD.create([ctx, tmpRT, w, h], RenderSprite.tmpTarget, this));
  11895. mask.render(ctx, -tRect.x, -tRect.y);
  11896. ctx.breakNextMerge();
  11897. ctx.popRT();
  11898. ctx.save();
  11899. ctx.clipRect(x + tRect.x - sprite.getStyle().pivotX, y + tRect.y - sprite.getStyle().pivotY, w, h);
  11900. next._fun.call(next, sprite, ctx, x, y);
  11901. ctx.restore();
  11902. preBlendMode = ctx.globalCompositeOperation;
  11903. ctx.addRenderObject(SubmitCMD.create(["mask"], RenderSprite.setBlendMode, this));
  11904. var shaderValue = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  11905. var uv = Texture.INV_UV;
  11906. ctx.drawTarget(tmpRT, x + tRect.x - sprite.getStyle().pivotX, y + tRect.y - sprite.getStyle().pivotY, w, h, Matrix.TEMP.identity(), shaderValue, uv, 6);
  11907. ctx.addRenderObject(SubmitCMD.create([tmpRT], RenderSprite.recycleTarget, this));
  11908. ctx.addRenderObject(SubmitCMD.create([preBlendMode], RenderSprite.setBlendMode, this));
  11909. }
  11910. ctx.restore();
  11911. }
  11912. else {
  11913. next._fun.call(next, sprite, context, x, y);
  11914. }
  11915. }
  11916. static tmpTarget(ctx, rt, w, h) {
  11917. rt.start();
  11918. rt.clear(0, 0, 0, 0);
  11919. }
  11920. static recycleTarget(rt) {
  11921. WebGLRTMgr.releaseRT(rt);
  11922. }
  11923. static setBlendMode(blendMode) {
  11924. var gl = WebGLContext.mainContext;
  11925. BlendMode.targetFns[BlendMode.TOINT[blendMode]](gl);
  11926. }
  11927. }
  11928. RenderSprite.INIT = 0x11111;
  11929. RenderSprite.renders = [];
  11930. RenderSprite.NORENDER = new RenderSprite(0, null);
  11931. RenderSprite.tempUV = new Array(8);
  11932. class HTMLCanvas extends Bitmap {
  11933. get source() {
  11934. return this._source;
  11935. }
  11936. _getSource() {
  11937. return this._source;
  11938. }
  11939. constructor(createCanvas = false) {
  11940. super();
  11941. if (createCanvas)
  11942. this._source = Browser.createElement("canvas");
  11943. else {
  11944. this._source = this;
  11945. }
  11946. this.lock = true;
  11947. }
  11948. clear() {
  11949. this._ctx && this._ctx.clear && this._ctx.clear();
  11950. if (this._texture) {
  11951. this._texture.destroy();
  11952. this._texture = null;
  11953. }
  11954. }
  11955. destroy() {
  11956. super.destroy();
  11957. this._setCPUMemory(0);
  11958. this._ctx && this._ctx.destroy && this._ctx.destroy();
  11959. this._ctx = null;
  11960. }
  11961. release() {
  11962. }
  11963. get context() {
  11964. if (this._ctx)
  11965. return this._ctx;
  11966. if (this._source == this) {
  11967. this._ctx = new ILaya.Context();
  11968. }
  11969. else {
  11970. this._ctx = this._source.getContext(ILaya.Render.isConchApp ? 'layagl' : '2d');
  11971. }
  11972. this._ctx._canvas = this;
  11973. return this._ctx;
  11974. }
  11975. _setContext(context) {
  11976. this._ctx = context;
  11977. }
  11978. getContext(contextID, other = null) {
  11979. return this.context;
  11980. }
  11981. getMemSize() {
  11982. return 0;
  11983. }
  11984. size(w, h) {
  11985. if (this._width != w || this._height != h || (this._source && (this._source.width != w || this._source.height != h))) {
  11986. this._width = w;
  11987. this._height = h;
  11988. this._setCPUMemory(w * h * 4);
  11989. this._ctx && this._ctx.size && this._ctx.size(w, h);
  11990. if (this._source) {
  11991. this._source.height = h;
  11992. this._source.width = w;
  11993. }
  11994. if (this._texture) {
  11995. this._texture.destroy();
  11996. this._texture = null;
  11997. }
  11998. }
  11999. }
  12000. getTexture() {
  12001. if (!this._texture) {
  12002. var bitmap = new Texture2D();
  12003. bitmap.loadImageSource(this.source);
  12004. this._texture = new Texture(bitmap);
  12005. }
  12006. return this._texture;
  12007. }
  12008. toBase64(type, encoderOptions) {
  12009. if (this._source) {
  12010. if (ILaya.Render.isConchApp) {
  12011. var win = window;
  12012. if (win.conchConfig.threadMode == 2) {
  12013. throw "native 2 thread mode use toBase64Async";
  12014. }
  12015. var width = this._ctx._targets.sourceWidth;
  12016. var height = this._ctx._targets.sourceHeight;
  12017. var data = this._ctx._targets.getData(0, 0, width, height);
  12018. return win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
  12019. }
  12020. else {
  12021. return this._source.toDataURL(type, encoderOptions);
  12022. }
  12023. }
  12024. return null;
  12025. }
  12026. toBase64Async(type, encoderOptions, callBack) {
  12027. var width = this._ctx._targets.sourceWidth;
  12028. var height = this._ctx._targets.sourceHeight;
  12029. this._ctx._targets.getDataAsync(0, 0, width, height, function (data) {
  12030. let win = window;
  12031. var base64 = win.conchToBase64FlipY ? win.conchToBase64FlipY(type, encoderOptions, data.buffer, width, height) : win.conchToBase64(type, encoderOptions, data.buffer, width, height);
  12032. callBack(base64);
  12033. });
  12034. }
  12035. }
  12036. class HitArea {
  12037. contains(x, y) {
  12038. if (!HitArea._isHitGraphic(x, y, this.hit))
  12039. return false;
  12040. return !HitArea._isHitGraphic(x, y, this.unHit);
  12041. }
  12042. static _isHitGraphic(x, y, graphic) {
  12043. if (!graphic)
  12044. return false;
  12045. var cmds = graphic.cmds;
  12046. if (!cmds && graphic._one) {
  12047. cmds = HitArea._cmds;
  12048. cmds.length = 1;
  12049. cmds[0] = graphic._one;
  12050. }
  12051. if (!cmds)
  12052. return false;
  12053. var i, len;
  12054. len = cmds.length;
  12055. var cmd;
  12056. for (i = 0; i < len; i++) {
  12057. cmd = cmds[i];
  12058. if (!cmd)
  12059. continue;
  12060. switch (cmd.cmdID) {
  12061. case "Translate":
  12062. x -= cmd.tx;
  12063. y -= cmd.ty;
  12064. }
  12065. if (HitArea._isHitCmd(x, y, cmd))
  12066. return true;
  12067. }
  12068. return false;
  12069. }
  12070. static _isHitCmd(x, y, cmd) {
  12071. if (!cmd)
  12072. return false;
  12073. var rst = false;
  12074. switch (cmd.cmdID) {
  12075. case "DrawRect":
  12076. HitArea._rect.setTo(cmd.x, cmd.y, cmd.width, cmd.height);
  12077. rst = HitArea._rect.contains(x, y);
  12078. break;
  12079. case "DrawCircle":
  12080. var d;
  12081. x -= cmd.x;
  12082. y -= cmd.y;
  12083. d = x * x + y * y;
  12084. rst = d < cmd.radius * cmd.radius;
  12085. break;
  12086. case "DrawPoly":
  12087. x -= cmd.x;
  12088. y -= cmd.y;
  12089. rst = HitArea._ptInPolygon(x, y, cmd.points);
  12090. break;
  12091. }
  12092. return rst;
  12093. }
  12094. static _ptInPolygon(x, y, areaPoints) {
  12095. var p = HitArea._ptPoint;
  12096. p.setTo(x, y);
  12097. var nCross = 0;
  12098. var p1x, p1y, p2x, p2y;
  12099. var len;
  12100. len = areaPoints.length;
  12101. for (var i = 0; i < len; i += 2) {
  12102. p1x = areaPoints[i];
  12103. p1y = areaPoints[i + 1];
  12104. p2x = areaPoints[(i + 2) % len];
  12105. p2y = areaPoints[(i + 3) % len];
  12106. if (p1y == p2y)
  12107. continue;
  12108. if (p.y < Math.min(p1y, p2y))
  12109. continue;
  12110. if (p.y >= Math.max(p1y, p2y))
  12111. continue;
  12112. var tx = (p.y - p1y) * (p2x - p1x) / (p2y - p1y) + p1x;
  12113. if (tx > p.x)
  12114. nCross++;
  12115. }
  12116. return (nCross % 2 == 1);
  12117. }
  12118. get hit() {
  12119. if (!this._hit)
  12120. this._hit = new ILaya.Graphics();
  12121. return this._hit;
  12122. }
  12123. set hit(value) {
  12124. this._hit = value;
  12125. }
  12126. get unHit() {
  12127. if (!this._unHit)
  12128. this._unHit = new ILaya.Graphics();
  12129. return this._unHit;
  12130. }
  12131. set unHit(value) {
  12132. this._unHit = value;
  12133. }
  12134. }
  12135. HitArea._cmds = [];
  12136. HitArea._rect = new Rectangle();
  12137. HitArea._ptPoint = new Point();
  12138. class ClassUtils {
  12139. static regClass(className, classDef) {
  12140. ClassUtils._classMap[className] = classDef;
  12141. }
  12142. static regShortClassName(classes) {
  12143. for (var i = 0; i < classes.length; i++) {
  12144. var classDef = classes[i];
  12145. var className = classDef.name;
  12146. ClassUtils._classMap[className] = classDef;
  12147. }
  12148. }
  12149. static getRegClass(className) {
  12150. return ClassUtils._classMap[className];
  12151. }
  12152. static getClass(className) {
  12153. var classObject = ClassUtils._classMap[className] || ClassUtils._classMap['Laya.' + className] || className;
  12154. var glaya = ILaya.Laya;
  12155. if (typeof (classObject) == 'string')
  12156. return (ILaya.__classMap[classObject] || glaya[className]);
  12157. return classObject;
  12158. }
  12159. static getInstance(className) {
  12160. var compClass = ClassUtils.getClass(className);
  12161. if (compClass)
  12162. return new compClass();
  12163. else
  12164. console.warn("[error] Undefined class:", className);
  12165. return null;
  12166. }
  12167. static createByJson(json, node = null, root = null, customHandler = null, instanceHandler = null) {
  12168. if (typeof (json) == 'string')
  12169. json = JSON.parse(json);
  12170. var props = json.props;
  12171. if (!node) {
  12172. node = instanceHandler ? instanceHandler.runWith(json) : ClassUtils.getInstance(props.runtime || json.type);
  12173. if (!node)
  12174. return null;
  12175. }
  12176. var child = json.child;
  12177. if (child) {
  12178. for (var i = 0, n = child.length; i < n; i++) {
  12179. var data = child[i];
  12180. if ((data.props.name === "render" || data.props.renderType === "render") && node["_$set_itemRender"])
  12181. node.itemRender = data;
  12182. else {
  12183. if (data.type == "Graphic") {
  12184. ClassUtils._addGraphicsToSprite(data, node);
  12185. }
  12186. else if (ClassUtils._isDrawType(data.type)) {
  12187. ClassUtils._addGraphicToSprite(data, node, true);
  12188. }
  12189. else {
  12190. var tChild = ClassUtils.createByJson(data, null, root, customHandler, instanceHandler);
  12191. if (data.type === "Script") {
  12192. if ("owner" in tChild) {
  12193. tChild["owner"] = node;
  12194. }
  12195. else if ("target" in tChild) {
  12196. tChild["target"] = node;
  12197. }
  12198. }
  12199. else if (data.props.renderType == "mask") {
  12200. node.mask = tChild;
  12201. }
  12202. else {
  12203. node.addChild(tChild);
  12204. }
  12205. }
  12206. }
  12207. }
  12208. }
  12209. if (props) {
  12210. for (var prop in props) {
  12211. var value = props[prop];
  12212. if (prop === "var" && root) {
  12213. root[value] = node;
  12214. }
  12215. else if (value instanceof Array && node[prop] instanceof Function) {
  12216. node[prop].apply(node, value);
  12217. }
  12218. else {
  12219. node[prop] = value;
  12220. }
  12221. }
  12222. }
  12223. if (customHandler && json.customProps) {
  12224. customHandler.runWith([node, json]);
  12225. }
  12226. if (node["created"])
  12227. node.created();
  12228. return node;
  12229. }
  12230. static _addGraphicsToSprite(graphicO, sprite) {
  12231. var graphics = graphicO.child;
  12232. if (!graphics || graphics.length < 1)
  12233. return;
  12234. var g = ClassUtils._getGraphicsFromSprite(graphicO, sprite);
  12235. var ox = 0;
  12236. var oy = 0;
  12237. if (graphicO.props) {
  12238. ox = ClassUtils._getObjVar(graphicO.props, "x", 0);
  12239. oy = ClassUtils._getObjVar(graphicO.props, "y", 0);
  12240. }
  12241. if (ox != 0 && oy != 0) {
  12242. g.translate(ox, oy);
  12243. }
  12244. var i, len;
  12245. len = graphics.length;
  12246. for (i = 0; i < len; i++) {
  12247. ClassUtils._addGraphicToGraphics(graphics[i], g);
  12248. }
  12249. if (ox != 0 && oy != 0) {
  12250. g.translate(-ox, -oy);
  12251. }
  12252. }
  12253. static _addGraphicToSprite(graphicO, sprite, isChild = false) {
  12254. var g = isChild ? ClassUtils._getGraphicsFromSprite(graphicO, sprite) : sprite.graphics;
  12255. ClassUtils._addGraphicToGraphics(graphicO, g);
  12256. }
  12257. static _getGraphicsFromSprite(dataO, sprite) {
  12258. if (!dataO || !dataO.props)
  12259. return sprite.graphics;
  12260. var propsName = dataO.props.renderType;
  12261. if (propsName === "hit" || propsName === "unHit") {
  12262. var hitArea = sprite._style.hitArea || (sprite.hitArea = new HitArea());
  12263. if (!hitArea[propsName]) {
  12264. hitArea[propsName] = new Graphics();
  12265. }
  12266. var g = hitArea[propsName];
  12267. }
  12268. if (!g)
  12269. g = sprite.graphics;
  12270. return g;
  12271. }
  12272. static _getTransformData(propsO) {
  12273. var m;
  12274. if ("pivotX" in propsO || "pivotY" in propsO) {
  12275. m = m || new Matrix();
  12276. m.translate(-ClassUtils._getObjVar(propsO, "pivotX", 0), -ClassUtils._getObjVar(propsO, "pivotY", 0));
  12277. }
  12278. var sx = ClassUtils._getObjVar(propsO, "scaleX", 1), sy = ClassUtils._getObjVar(propsO, "scaleY", 1);
  12279. var rotate = ClassUtils._getObjVar(propsO, "rotation", 0);
  12280. var skewX = ClassUtils._getObjVar(propsO, "skewX", 0);
  12281. var skewY = ClassUtils._getObjVar(propsO, "skewY", 0);
  12282. if (sx != 1 || sy != 1 || rotate != 0) {
  12283. m = m || new Matrix();
  12284. m.scale(sx, sy);
  12285. m.rotate(rotate * 0.0174532922222222);
  12286. }
  12287. return m;
  12288. }
  12289. static _addGraphicToGraphics(graphicO, graphic) {
  12290. var propsO;
  12291. propsO = graphicO.props;
  12292. if (!propsO)
  12293. return;
  12294. var drawConfig;
  12295. drawConfig = ClassUtils.DrawTypeDic[graphicO.type];
  12296. if (!drawConfig)
  12297. return;
  12298. var g = graphic;
  12299. var params = ClassUtils._getParams(propsO, drawConfig[1], drawConfig[2], drawConfig[3]);
  12300. var m = ClassUtils._tM;
  12301. if (m || ClassUtils._alpha != 1) {
  12302. g.save();
  12303. if (m)
  12304. g.transform(m);
  12305. if (ClassUtils._alpha != 1)
  12306. g.alpha(ClassUtils._alpha);
  12307. }
  12308. g[drawConfig[0]].apply(g, params);
  12309. if (m || ClassUtils._alpha != 1) {
  12310. g.restore();
  12311. }
  12312. }
  12313. static _adptLineData(params) {
  12314. params[2] = parseFloat(params[0]) + parseFloat(params[2]);
  12315. params[3] = parseFloat(params[1]) + parseFloat(params[3]);
  12316. return params;
  12317. }
  12318. static _adptTextureData(params) {
  12319. params[0] = ILaya.Loader.getRes(params[0]);
  12320. return params;
  12321. }
  12322. static _adptLinesData(params) {
  12323. params[2] = ClassUtils._getPointListByStr(params[2]);
  12324. return params;
  12325. }
  12326. static _isDrawType(type) {
  12327. if (type === "Image")
  12328. return false;
  12329. return type in ClassUtils.DrawTypeDic;
  12330. }
  12331. static _getParams(obj, params, xPos = 0, adptFun = null) {
  12332. var rst = ClassUtils._temParam;
  12333. rst.length = params.length;
  12334. var i, len;
  12335. len = params.length;
  12336. for (i = 0; i < len; i++) {
  12337. rst[i] = ClassUtils._getObjVar(obj, params[i][0], params[i][1]);
  12338. }
  12339. ClassUtils._alpha = ClassUtils._getObjVar(obj, "alpha", 1);
  12340. var m;
  12341. m = ClassUtils._getTransformData(obj);
  12342. if (m) {
  12343. if (!xPos)
  12344. xPos = 0;
  12345. m.translate(rst[xPos], rst[xPos + 1]);
  12346. rst[xPos] = rst[xPos + 1] = 0;
  12347. ClassUtils._tM = m;
  12348. }
  12349. else {
  12350. ClassUtils._tM = null;
  12351. }
  12352. if (adptFun && ClassUtils[adptFun]) {
  12353. rst = ClassUtils[adptFun](rst);
  12354. }
  12355. return rst;
  12356. }
  12357. static _getPointListByStr(str) {
  12358. var pointArr = str.split(",");
  12359. var i, len;
  12360. len = pointArr.length;
  12361. for (i = 0; i < len; i++) {
  12362. pointArr[i] = parseFloat(pointArr[i]);
  12363. }
  12364. return pointArr;
  12365. }
  12366. static _getObjVar(obj, key, noValue) {
  12367. if (key in obj) {
  12368. return obj[key];
  12369. }
  12370. return noValue;
  12371. }
  12372. }
  12373. 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"] };
  12374. ClassUtils._temParam = [];
  12375. ClassUtils._classMap = {};
  12376. class BoundsStyle {
  12377. reset() {
  12378. if (this.bounds)
  12379. this.bounds.recover();
  12380. if (this.userBounds)
  12381. this.userBounds.recover();
  12382. this.bounds = null;
  12383. this.userBounds = null;
  12384. this.temBM = null;
  12385. return this;
  12386. }
  12387. recover() {
  12388. Pool.recover("BoundsStyle", this.reset());
  12389. }
  12390. static create() {
  12391. return Pool.getItemByClass("BoundsStyle", BoundsStyle);
  12392. }
  12393. }
  12394. class CacheStyle {
  12395. constructor() {
  12396. this.reset();
  12397. }
  12398. needBitmapCache() {
  12399. return this.cacheForFilters || !!this.mask;
  12400. }
  12401. needEnableCanvasRender() {
  12402. return this.userSetCache != "none" || this.cacheForFilters || !!this.mask;
  12403. }
  12404. releaseContext() {
  12405. if (this.canvas && this.canvas.size) {
  12406. Pool.recover("CacheCanvas", this.canvas);
  12407. this.canvas.size(0, 0);
  12408. try {
  12409. this.canvas.width = 0;
  12410. this.canvas.height = 0;
  12411. }
  12412. catch (e) {
  12413. }
  12414. }
  12415. this.canvas = null;
  12416. }
  12417. createContext() {
  12418. if (!this.canvas) {
  12419. this.canvas = Pool.getItem("CacheCanvas") || new HTMLCanvas(false);
  12420. var tx = this.canvas.context;
  12421. if (!tx) {
  12422. tx = this.canvas.getContext('2d');
  12423. }
  12424. }
  12425. }
  12426. releaseFilterCache() {
  12427. var fc = this.filterCache;
  12428. if (fc) {
  12429. fc.destroy();
  12430. fc.recycle();
  12431. this.filterCache = null;
  12432. }
  12433. }
  12434. recover() {
  12435. if (this === CacheStyle.EMPTY)
  12436. return;
  12437. Pool.recover("SpriteCache", this.reset());
  12438. }
  12439. reset() {
  12440. this.releaseContext();
  12441. this.releaseFilterCache();
  12442. this.cacheAs = "none";
  12443. this.enableCanvasRender = false;
  12444. this.userSetCache = "none";
  12445. this.cacheForFilters = false;
  12446. this.staticCache = false;
  12447. this.reCache = true;
  12448. this.mask = null;
  12449. this.maskParent = null;
  12450. this.filterCache = null;
  12451. this.filters = null;
  12452. this.hasGlowFilter = false;
  12453. if (this.cacheRect)
  12454. this.cacheRect.recover();
  12455. this.cacheRect = null;
  12456. return this;
  12457. }
  12458. static create() {
  12459. return Pool.getItemByClass("SpriteCache", CacheStyle);
  12460. }
  12461. _calculateCacheRect(sprite, tCacheType, x, y) {
  12462. var _cacheStyle = sprite._cacheStyle;
  12463. if (!_cacheStyle.cacheRect)
  12464. _cacheStyle.cacheRect = Rectangle.create();
  12465. var tRec;
  12466. if (tCacheType === "bitmap") {
  12467. tRec = sprite.getSelfBounds();
  12468. tRec.width = tRec.width + CacheStyle.CANVAS_EXTEND_EDGE * 2;
  12469. tRec.height = tRec.height + CacheStyle.CANVAS_EXTEND_EDGE * 2;
  12470. tRec.x = tRec.x - sprite.pivotX;
  12471. tRec.y = tRec.y - sprite.pivotY;
  12472. tRec.x = tRec.x - CacheStyle.CANVAS_EXTEND_EDGE;
  12473. tRec.y = tRec.y - CacheStyle.CANVAS_EXTEND_EDGE;
  12474. tRec.x = Math.floor(tRec.x + x) - x;
  12475. tRec.y = Math.floor(tRec.y + y) - y;
  12476. tRec.width = Math.floor(tRec.width);
  12477. tRec.height = Math.floor(tRec.height);
  12478. _cacheStyle.cacheRect.copyFrom(tRec);
  12479. }
  12480. else {
  12481. _cacheStyle.cacheRect.setTo(-sprite._style.pivotX, -sprite._style.pivotY, 1, 1);
  12482. }
  12483. tRec = _cacheStyle.cacheRect;
  12484. if (sprite._style.scrollRect) {
  12485. var scrollRect = sprite._style.scrollRect;
  12486. tRec.x -= scrollRect.x;
  12487. tRec.y -= scrollRect.y;
  12488. }
  12489. CacheStyle._scaleInfo.setTo(1, 1);
  12490. return CacheStyle._scaleInfo;
  12491. }
  12492. }
  12493. CacheStyle.EMPTY = new CacheStyle();
  12494. CacheStyle._scaleInfo = new Point();
  12495. CacheStyle.CANVAS_EXTEND_EDGE = 16;
  12496. class SpriteStyle {
  12497. constructor() {
  12498. this.reset();
  12499. }
  12500. reset() {
  12501. this.scaleX = this.scaleY = 1;
  12502. this.skewX = this.skewY = 0;
  12503. this.pivotX = this.pivotY = this.rotation = 0;
  12504. this.alpha = 1;
  12505. if (this.scrollRect)
  12506. this.scrollRect.recover();
  12507. this.scrollRect = null;
  12508. if (this.viewport)
  12509. this.viewport.recover();
  12510. this.viewport = null;
  12511. this.hitArea = null;
  12512. this.dragging = null;
  12513. this.blendMode = null;
  12514. return this;
  12515. }
  12516. recover() {
  12517. if (this === SpriteStyle.EMPTY)
  12518. return;
  12519. Pool.recover("SpriteStyle", this.reset());
  12520. }
  12521. static create() {
  12522. return Pool.getItemByClass("SpriteStyle", SpriteStyle);
  12523. }
  12524. }
  12525. SpriteStyle.EMPTY = new SpriteStyle();
  12526. class Node extends EventDispatcher {
  12527. constructor() {
  12528. super();
  12529. this._bits = 0;
  12530. this._children = Node.ARRAY_EMPTY;
  12531. this._extUIChild = Node.ARRAY_EMPTY;
  12532. this._parent = null;
  12533. this.name = "";
  12534. this.destroyed = false;
  12535. this.createGLBuffer();
  12536. }
  12537. createGLBuffer() {
  12538. }
  12539. _setBit(type, value) {
  12540. if (type === Const.DISPLAY) {
  12541. var preValue = this._getBit(type);
  12542. if (preValue != value)
  12543. this._updateDisplayedInstage();
  12544. }
  12545. if (value)
  12546. this._bits |= type;
  12547. else
  12548. this._bits &= ~type;
  12549. }
  12550. _getBit(type) {
  12551. return (this._bits & type) != 0;
  12552. }
  12553. _setUpNoticeChain() {
  12554. if (this._getBit(Const.DISPLAY))
  12555. this._setBitUp(Const.DISPLAY);
  12556. }
  12557. _setBitUp(type) {
  12558. var ele = this;
  12559. ele._setBit(type, true);
  12560. ele = ele._parent;
  12561. while (ele) {
  12562. if (ele._getBit(type))
  12563. return;
  12564. ele._setBit(type, true);
  12565. ele = ele._parent;
  12566. }
  12567. }
  12568. on(type, caller, listener, args = null) {
  12569. if (type === Event.DISPLAY || type === Event.UNDISPLAY) {
  12570. if (!this._getBit(Const.DISPLAY))
  12571. this._setBitUp(Const.DISPLAY);
  12572. }
  12573. return this._createListener(type, caller, listener, args, false);
  12574. }
  12575. once(type, caller, listener, args = null) {
  12576. if (type === Event.DISPLAY || type === Event.UNDISPLAY) {
  12577. if (!this._getBit(Const.DISPLAY))
  12578. this._setBitUp(Const.DISPLAY);
  12579. }
  12580. return this._createListener(type, caller, listener, args, true);
  12581. }
  12582. destroy(destroyChild = true) {
  12583. this.destroyed = true;
  12584. this._destroyAllComponent();
  12585. this._parent && this._parent.removeChild(this);
  12586. if (this._children) {
  12587. if (destroyChild)
  12588. this.destroyChildren();
  12589. else
  12590. this.removeChildren();
  12591. }
  12592. this.onDestroy();
  12593. this._children = null;
  12594. this.offAll();
  12595. }
  12596. onDestroy() {
  12597. }
  12598. destroyChildren() {
  12599. if (this._children) {
  12600. for (var i = 0, n = this._children.length; i < n; i++) {
  12601. this._children[0].destroy(true);
  12602. }
  12603. }
  12604. }
  12605. addChild(node) {
  12606. if (!node || this.destroyed || node === this)
  12607. return node;
  12608. if (node._zOrder)
  12609. this._setBit(Const.HAS_ZORDER, true);
  12610. if (node._parent === this) {
  12611. var index = this.getChildIndex(node);
  12612. if (index !== this._children.length - 1) {
  12613. this._children.splice(index, 1);
  12614. this._children.push(node);
  12615. this._childChanged();
  12616. }
  12617. }
  12618. else {
  12619. node._parent && node._parent.removeChild(node);
  12620. this._children === Node.ARRAY_EMPTY && (this._children = []);
  12621. this._children.push(node);
  12622. node._setParent(this);
  12623. this._childChanged();
  12624. }
  12625. return node;
  12626. }
  12627. addInputChild(node) {
  12628. if (this._extUIChild == Node.ARRAY_EMPTY) {
  12629. this._extUIChild = [node];
  12630. }
  12631. else {
  12632. if (this._extUIChild.indexOf(node) >= 0) {
  12633. return null;
  12634. }
  12635. this._extUIChild.push(node);
  12636. }
  12637. return null;
  12638. }
  12639. removeInputChild(node) {
  12640. var idx = this._extUIChild.indexOf(node);
  12641. if (idx >= 0) {
  12642. this._extUIChild.splice(idx, 1);
  12643. }
  12644. }
  12645. addChildren(...args) {
  12646. var i = 0, n = args.length;
  12647. while (i < n) {
  12648. this.addChild(args[i++]);
  12649. }
  12650. }
  12651. addChildAt(node, index) {
  12652. if (!node || this.destroyed || node === this)
  12653. return node;
  12654. if (node._zOrder)
  12655. this._setBit(Const.HAS_ZORDER, true);
  12656. if (index >= 0 && index <= this._children.length) {
  12657. if (node._parent === this) {
  12658. var oldIndex = this.getChildIndex(node);
  12659. this._children.splice(oldIndex, 1);
  12660. this._children.splice(index, 0, node);
  12661. this._childChanged();
  12662. }
  12663. else {
  12664. node._parent && node._parent.removeChild(node);
  12665. this._children === Node.ARRAY_EMPTY && (this._children = []);
  12666. this._children.splice(index, 0, node);
  12667. node._setParent(this);
  12668. }
  12669. return node;
  12670. }
  12671. else {
  12672. throw new Error("appendChildAt:The index is out of bounds");
  12673. }
  12674. }
  12675. getChildIndex(node) {
  12676. return this._children.indexOf(node);
  12677. }
  12678. getChildByName(name) {
  12679. var nodes = this._children;
  12680. if (nodes) {
  12681. for (var i = 0, n = nodes.length; i < n; i++) {
  12682. var node = nodes[i];
  12683. if (node.name === name)
  12684. return node;
  12685. }
  12686. }
  12687. return null;
  12688. }
  12689. getChildAt(index) {
  12690. return this._children[index] || null;
  12691. }
  12692. setChildIndex(node, index) {
  12693. var childs = this._children;
  12694. if (index < 0 || index >= childs.length) {
  12695. throw new Error("setChildIndex:The index is out of bounds.");
  12696. }
  12697. var oldIndex = this.getChildIndex(node);
  12698. if (oldIndex < 0)
  12699. throw new Error("setChildIndex:node is must child of this object.");
  12700. childs.splice(oldIndex, 1);
  12701. childs.splice(index, 0, node);
  12702. this._childChanged();
  12703. return node;
  12704. }
  12705. _childChanged(child = null) {
  12706. }
  12707. removeChild(node) {
  12708. if (!this._children)
  12709. return node;
  12710. var index = this._children.indexOf(node);
  12711. return this.removeChildAt(index);
  12712. }
  12713. removeSelf() {
  12714. this._parent && this._parent.removeChild(this);
  12715. return this;
  12716. }
  12717. removeChildByName(name) {
  12718. var node = this.getChildByName(name);
  12719. node && this.removeChild(node);
  12720. return node;
  12721. }
  12722. removeChildAt(index) {
  12723. var node = this.getChildAt(index);
  12724. if (node) {
  12725. this._children.splice(index, 1);
  12726. node._setParent(null);
  12727. }
  12728. return node;
  12729. }
  12730. removeChildren(beginIndex = 0, endIndex = 0x7fffffff) {
  12731. if (this._children && this._children.length > 0) {
  12732. var childs = this._children;
  12733. if (beginIndex === 0 && endIndex >= childs.length - 1) {
  12734. var arr = childs;
  12735. this._children = Node.ARRAY_EMPTY;
  12736. }
  12737. else {
  12738. arr = childs.splice(beginIndex, endIndex - beginIndex);
  12739. }
  12740. for (var i = 0, n = arr.length; i < n; i++) {
  12741. arr[i]._setParent(null);
  12742. }
  12743. }
  12744. return this;
  12745. }
  12746. replaceChild(newNode, oldNode) {
  12747. var index = this._children.indexOf(oldNode);
  12748. if (index > -1) {
  12749. this._children.splice(index, 1, newNode);
  12750. oldNode._setParent(null);
  12751. newNode._setParent(this);
  12752. return newNode;
  12753. }
  12754. return null;
  12755. }
  12756. get numChildren() {
  12757. return this._children.length;
  12758. }
  12759. get parent() {
  12760. return this._parent;
  12761. }
  12762. _setParent(value) {
  12763. if (this._parent !== value) {
  12764. if (value) {
  12765. this._parent = value;
  12766. this._onAdded();
  12767. this.event(Event.ADDED);
  12768. if (this._getBit(Const.DISPLAY)) {
  12769. this._setUpNoticeChain();
  12770. value.displayedInStage && this._displayChild(this, true);
  12771. }
  12772. value._childChanged(this);
  12773. }
  12774. else {
  12775. this._onRemoved();
  12776. this.event(Event.REMOVED);
  12777. this._parent._childChanged();
  12778. if (this._getBit(Const.DISPLAY))
  12779. this._displayChild(this, false);
  12780. this._parent = value;
  12781. }
  12782. }
  12783. }
  12784. get displayedInStage() {
  12785. if (this._getBit(Const.DISPLAY))
  12786. return this._getBit(Const.DISPLAYED_INSTAGE);
  12787. this._setBitUp(Const.DISPLAY);
  12788. return this._getBit(Const.DISPLAYED_INSTAGE);
  12789. }
  12790. _updateDisplayedInstage() {
  12791. var ele;
  12792. ele = this;
  12793. var stage = ILaya.stage;
  12794. var displayedInStage = false;
  12795. while (ele) {
  12796. if (ele._getBit(Const.DISPLAY)) {
  12797. displayedInStage = ele._getBit(Const.DISPLAYED_INSTAGE);
  12798. break;
  12799. }
  12800. if (ele === stage || ele._getBit(Const.DISPLAYED_INSTAGE)) {
  12801. displayedInStage = true;
  12802. break;
  12803. }
  12804. ele = ele._parent;
  12805. }
  12806. this._setBit(Const.DISPLAYED_INSTAGE, displayedInStage);
  12807. }
  12808. _setDisplay(value) {
  12809. if (this._getBit(Const.DISPLAYED_INSTAGE) !== value) {
  12810. this._setBit(Const.DISPLAYED_INSTAGE, value);
  12811. if (value)
  12812. this.event(Event.DISPLAY);
  12813. else
  12814. this.event(Event.UNDISPLAY);
  12815. }
  12816. }
  12817. _displayChild(node, display) {
  12818. var childs = node._children;
  12819. if (childs) {
  12820. for (var i = 0, n = childs.length; i < n; i++) {
  12821. var child = childs[i];
  12822. if (!child._getBit(Const.DISPLAY))
  12823. continue;
  12824. if (child._children.length > 0) {
  12825. this._displayChild(child, display);
  12826. }
  12827. else {
  12828. child._setDisplay(display);
  12829. }
  12830. }
  12831. }
  12832. node._setDisplay(display);
  12833. }
  12834. contains(node) {
  12835. if (node === this)
  12836. return true;
  12837. while (node) {
  12838. if (node._parent === this)
  12839. return true;
  12840. node = node._parent;
  12841. }
  12842. return false;
  12843. }
  12844. timerLoop(delay, caller, method, args = null, coverBefore = true, jumpFrame = false) {
  12845. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12846. timer.loop(delay, caller, method, args, coverBefore, jumpFrame);
  12847. }
  12848. timerOnce(delay, caller, method, args = null, coverBefore = true) {
  12849. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12850. timer._create(false, false, delay, caller, method, args, coverBefore);
  12851. }
  12852. frameLoop(delay, caller, method, args = null, coverBefore = true) {
  12853. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12854. timer._create(true, true, delay, caller, method, args, coverBefore);
  12855. }
  12856. frameOnce(delay, caller, method, args = null, coverBefore = true) {
  12857. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12858. timer._create(true, false, delay, caller, method, args, coverBefore);
  12859. }
  12860. clearTimer(caller, method) {
  12861. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12862. timer.clear(caller, method);
  12863. }
  12864. callLater(method, args = null) {
  12865. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12866. timer.callLater(this, method, args);
  12867. }
  12868. runCallLater(method) {
  12869. var timer = this.scene ? this.scene.timer : ILaya.timer;
  12870. timer.runCallLater(this, method);
  12871. }
  12872. get scene() {
  12873. return this._scene;
  12874. }
  12875. get active() {
  12876. return !this._getBit(Const.NOT_READY) && !this._getBit(Const.NOT_ACTIVE);
  12877. }
  12878. set active(value) {
  12879. value = !!value;
  12880. if (!this._getBit(Const.NOT_ACTIVE) !== value) {
  12881. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  12882. if (value)
  12883. 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.";
  12884. else
  12885. 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.";
  12886. }
  12887. else {
  12888. this._setBit(Const.NOT_ACTIVE, !value);
  12889. if (this._parent) {
  12890. if (this._parent.activeInHierarchy) {
  12891. if (value)
  12892. this._processActive();
  12893. else
  12894. this._processInActive();
  12895. }
  12896. }
  12897. }
  12898. }
  12899. }
  12900. get activeInHierarchy() {
  12901. return this._getBit(Const.ACTIVE_INHIERARCHY);
  12902. }
  12903. _onActive() {
  12904. Stat.spriteCount++;
  12905. }
  12906. _onInActive() {
  12907. Stat.spriteCount--;
  12908. }
  12909. _onActiveInScene() {
  12910. }
  12911. _onInActiveInScene() {
  12912. }
  12913. _parse(data, spriteMap) {
  12914. }
  12915. _setBelongScene(scene) {
  12916. if (!this._scene) {
  12917. this._scene = scene;
  12918. this._onActiveInScene();
  12919. for (var i = 0, n = this._children.length; i < n; i++)
  12920. this._children[i]._setBelongScene(scene);
  12921. }
  12922. }
  12923. _setUnBelongScene() {
  12924. if (this._scene !== this) {
  12925. this._onInActiveInScene();
  12926. this._scene = null;
  12927. for (var i = 0, n = this._children.length; i < n; i++)
  12928. this._children[i]._setUnBelongScene();
  12929. }
  12930. }
  12931. onAwake() {
  12932. }
  12933. onEnable() {
  12934. }
  12935. _processActive() {
  12936. (this._activeChangeScripts) || (this._activeChangeScripts = []);
  12937. this._activeHierarchy(this._activeChangeScripts);
  12938. this._activeScripts();
  12939. }
  12940. _activeHierarchy(activeChangeScripts) {
  12941. this._setBit(Const.ACTIVE_INHIERARCHY, true);
  12942. if (this._components) {
  12943. for (var i = 0, n = this._components.length; i < n; i++) {
  12944. var comp = this._components[i];
  12945. comp._setActive(true);
  12946. (comp._isScript() && comp._enabled) && (activeChangeScripts.push(comp));
  12947. }
  12948. }
  12949. this._onActive();
  12950. for (i = 0, n = this._children.length; i < n; i++) {
  12951. var child = this._children[i];
  12952. (!child._getBit(Const.NOT_ACTIVE)) && (child._activeHierarchy(activeChangeScripts));
  12953. }
  12954. if (!this._getBit(Const.AWAKED)) {
  12955. this._setBit(Const.AWAKED, true);
  12956. this.onAwake();
  12957. }
  12958. this.onEnable();
  12959. }
  12960. _activeScripts() {
  12961. for (var i = 0, n = this._activeChangeScripts.length; i < n; i++)
  12962. this._activeChangeScripts[i].onEnable();
  12963. this._activeChangeScripts.length = 0;
  12964. }
  12965. _processInActive() {
  12966. (this._activeChangeScripts) || (this._activeChangeScripts = []);
  12967. this._inActiveHierarchy(this._activeChangeScripts);
  12968. this._inActiveScripts();
  12969. }
  12970. _inActiveHierarchy(activeChangeScripts) {
  12971. this._onInActive();
  12972. if (this._components) {
  12973. for (var i = 0, n = this._components.length; i < n; i++) {
  12974. var comp = this._components[i];
  12975. comp._setActive(false);
  12976. (comp._isScript() && comp._enabled) && (activeChangeScripts.push(comp));
  12977. }
  12978. }
  12979. this._setBit(Const.ACTIVE_INHIERARCHY, false);
  12980. for (i = 0, n = this._children.length; i < n; i++) {
  12981. var child = this._children[i];
  12982. (child && !child._getBit(Const.NOT_ACTIVE)) && (child._inActiveHierarchy(activeChangeScripts));
  12983. }
  12984. this.onDisable();
  12985. }
  12986. _inActiveScripts() {
  12987. for (var i = 0, n = this._activeChangeScripts.length; i < n; i++)
  12988. this._activeChangeScripts[i].onDisable();
  12989. this._activeChangeScripts.length = 0;
  12990. }
  12991. onDisable() {
  12992. }
  12993. _onAdded() {
  12994. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  12995. 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.";
  12996. }
  12997. else {
  12998. var parentScene = this._parent.scene;
  12999. parentScene && this._setBelongScene(parentScene);
  13000. (this._parent.activeInHierarchy && this.active) && this._processActive();
  13001. }
  13002. }
  13003. _onRemoved() {
  13004. if (this._activeChangeScripts && this._activeChangeScripts.length !== 0) {
  13005. 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.";
  13006. }
  13007. else {
  13008. (this._parent.activeInHierarchy && this.active) && this._processInActive();
  13009. this._parent.scene && this._setUnBelongScene();
  13010. }
  13011. }
  13012. _addComponentInstance(comp) {
  13013. this._components = this._components || [];
  13014. this._components.push(comp);
  13015. comp.owner = this;
  13016. comp._onAdded();
  13017. if (this.activeInHierarchy)
  13018. comp._setActive(true);
  13019. }
  13020. _destroyComponent(comp) {
  13021. if (this._components) {
  13022. for (var i = 0, n = this._components.length; i < n; i++) {
  13023. var item = this._components[i];
  13024. if (item === comp) {
  13025. item._destroy();
  13026. this._components.splice(i, 1);
  13027. break;
  13028. }
  13029. }
  13030. }
  13031. }
  13032. _destroyAllComponent() {
  13033. if (this._components) {
  13034. for (var i = 0, n = this._components.length; i < n; i++) {
  13035. var item = this._components[i];
  13036. item._destroy();
  13037. }
  13038. this._components.length = 0;
  13039. }
  13040. }
  13041. _cloneTo(destObject, srcRoot, dstRoot) {
  13042. var destNode = destObject;
  13043. if (this._components) {
  13044. for (var i = 0, n = this._components.length; i < n; i++) {
  13045. var destComponent = destNode.addComponent(this._components[i].constructor);
  13046. this._components[i]._cloneTo(destComponent);
  13047. }
  13048. }
  13049. }
  13050. addComponentIntance(component) {
  13051. if (component.owner)
  13052. throw "Node:the component has belong to other node.";
  13053. if (component.isSingleton && this.getComponent(component.constructor))
  13054. throw "Node:the component is singleton,can't add the second one.";
  13055. this._addComponentInstance(component);
  13056. return component;
  13057. }
  13058. addComponent(componentType) {
  13059. var comp = Pool.createByClass(componentType);
  13060. comp._destroyed = false;
  13061. if (comp.isSingleton && this.getComponent(componentType))
  13062. throw "无法实例" + componentType + "组件" + "," + componentType + "组件已存在!";
  13063. this._addComponentInstance(comp);
  13064. return comp;
  13065. }
  13066. getComponent(componentType) {
  13067. if (this._components) {
  13068. for (var i = 0, n = this._components.length; i < n; i++) {
  13069. var comp = this._components[i];
  13070. if (comp instanceof componentType)
  13071. return comp;
  13072. }
  13073. }
  13074. return null;
  13075. }
  13076. getComponents(componentType) {
  13077. var arr;
  13078. if (this._components) {
  13079. for (var i = 0, n = this._components.length; i < n; i++) {
  13080. var comp = this._components[i];
  13081. if (comp instanceof componentType) {
  13082. arr = arr || [];
  13083. arr.push(comp);
  13084. }
  13085. }
  13086. }
  13087. return arr;
  13088. }
  13089. get timer() {
  13090. return this.scene ? this.scene.timer : ILaya.timer;
  13091. }
  13092. }
  13093. Node.ARRAY_EMPTY = [];
  13094. ClassUtils.regClass("laya.display.Node", Node);
  13095. ClassUtils.regClass("Laya.Node", Node);
  13096. class Sprite extends Node {
  13097. constructor() {
  13098. super();
  13099. this._x = 0;
  13100. this._y = 0;
  13101. this._width = 0;
  13102. this._height = 0;
  13103. this._visible = true;
  13104. this._mouseState = 0;
  13105. this._zOrder = 0;
  13106. this._renderType = 0;
  13107. this._transform = null;
  13108. this._tfChanged = false;
  13109. this._repaint = SpriteConst.REPAINT_NONE;
  13110. this._texture = null;
  13111. this._style = SpriteStyle.EMPTY;
  13112. this._cacheStyle = CacheStyle.EMPTY;
  13113. this._boundStyle = null;
  13114. this._graphics = null;
  13115. this.mouseThrough = false;
  13116. this.autoSize = false;
  13117. this.hitTestPrior = false;
  13118. }
  13119. destroy(destroyChild = true) {
  13120. super.destroy(destroyChild);
  13121. this._style && this._style.recover();
  13122. this._cacheStyle && this._cacheStyle.recover();
  13123. this._boundStyle && this._boundStyle.recover();
  13124. this._style = null;
  13125. this._cacheStyle = null;
  13126. this._boundStyle = null;
  13127. this._transform = null;
  13128. if (this._graphics && this._graphics.autoDestroy) {
  13129. this._graphics.destroy();
  13130. }
  13131. this._graphics = null;
  13132. this.texture = null;
  13133. }
  13134. updateZOrder() {
  13135. Utils.updateOrder(this._children) && this.repaint();
  13136. }
  13137. _getBoundsStyle() {
  13138. if (!this._boundStyle)
  13139. this._boundStyle = BoundsStyle.create();
  13140. return this._boundStyle;
  13141. }
  13142. _setCustomRender() {
  13143. }
  13144. set customRenderEnable(b) {
  13145. if (b) {
  13146. this._renderType |= SpriteConst.CUSTOM;
  13147. this._setRenderType(this._renderType);
  13148. this._setCustomRender();
  13149. }
  13150. }
  13151. get cacheAs() {
  13152. return this._cacheStyle.cacheAs;
  13153. }
  13154. _setCacheAs(value) {
  13155. }
  13156. set cacheAs(value) {
  13157. if (value === this._cacheStyle.userSetCache)
  13158. return;
  13159. if (this.mask && value === 'normal')
  13160. return;
  13161. this._setCacheAs(value);
  13162. this._getCacheStyle().userSetCache = value;
  13163. this._checkCanvasEnable();
  13164. this.repaint();
  13165. }
  13166. _checkCanvasEnable() {
  13167. var tEnable = this._cacheStyle.needEnableCanvasRender();
  13168. this._getCacheStyle().enableCanvasRender = tEnable;
  13169. if (tEnable) {
  13170. if (this._cacheStyle.needBitmapCache()) {
  13171. this._cacheStyle.cacheAs = "bitmap";
  13172. }
  13173. else {
  13174. this._cacheStyle.cacheAs = this._cacheStyle.userSetCache;
  13175. }
  13176. this._cacheStyle.reCache = true;
  13177. this._renderType |= SpriteConst.CANVAS;
  13178. }
  13179. else {
  13180. this._cacheStyle.cacheAs = "none";
  13181. this._cacheStyle.releaseContext();
  13182. this._renderType &= ~SpriteConst.CANVAS;
  13183. }
  13184. this._setCacheAs(this._cacheStyle.cacheAs);
  13185. this._setRenderType(this._renderType);
  13186. }
  13187. get staticCache() {
  13188. return this._cacheStyle.staticCache;
  13189. }
  13190. set staticCache(value) {
  13191. this._getCacheStyle().staticCache = value;
  13192. if (!value)
  13193. this.reCache();
  13194. }
  13195. reCache() {
  13196. this._cacheStyle.reCache = true;
  13197. this._repaint |= SpriteConst.REPAINT_CACHE;
  13198. }
  13199. getRepaint() {
  13200. return this._repaint;
  13201. }
  13202. _setX(value) {
  13203. this._x = value;
  13204. }
  13205. _setY(value) {
  13206. this._y = value;
  13207. }
  13208. _setWidth(texture, value) {
  13209. }
  13210. _setHeight(texture, value) {
  13211. }
  13212. get x() {
  13213. return this._x;
  13214. }
  13215. set x(value) {
  13216. if (this.destroyed)
  13217. return;
  13218. if (this._x !== value) {
  13219. this._setX(value);
  13220. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  13221. var p = this._cacheStyle.maskParent;
  13222. if (p) {
  13223. p.repaint(SpriteConst.REPAINT_CACHE);
  13224. }
  13225. }
  13226. }
  13227. get y() {
  13228. return this._y;
  13229. }
  13230. set y(value) {
  13231. if (this.destroyed)
  13232. return;
  13233. if (this._y !== value) {
  13234. this._setY(value);
  13235. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  13236. var p = this._cacheStyle.maskParent;
  13237. if (p) {
  13238. p.repaint(SpriteConst.REPAINT_CACHE);
  13239. }
  13240. }
  13241. }
  13242. get width() {
  13243. return this.get_width();
  13244. }
  13245. set width(value) {
  13246. this.set_width(value);
  13247. }
  13248. set_width(value) {
  13249. if (this._width !== value) {
  13250. this._width = value;
  13251. this._setWidth(this.texture, value);
  13252. this._setTranformChange();
  13253. }
  13254. }
  13255. get_width() {
  13256. if (!this.autoSize)
  13257. return this._width || (this.texture ? this.texture.width : 0);
  13258. if (this.texture)
  13259. return this.texture.width;
  13260. if (!this._graphics && this._children.length === 0)
  13261. return 0;
  13262. return this.getSelfBounds().width;
  13263. }
  13264. get height() {
  13265. return this.get_height();
  13266. }
  13267. set height(value) {
  13268. this.set_height(value);
  13269. }
  13270. set_height(value) {
  13271. if (this._height !== value) {
  13272. this._height = value;
  13273. this._setHeight(this.texture, value);
  13274. this._setTranformChange();
  13275. }
  13276. }
  13277. get_height() {
  13278. if (!this.autoSize)
  13279. return this._height || (this.texture ? this.texture.height : 0);
  13280. if (this.texture)
  13281. return this.texture.height;
  13282. if (!this._graphics && this._children.length === 0)
  13283. return 0;
  13284. return this.getSelfBounds().height;
  13285. }
  13286. get displayWidth() {
  13287. return this.width * this.scaleX;
  13288. }
  13289. get displayHeight() {
  13290. return this.height * this.scaleY;
  13291. }
  13292. setSelfBounds(bound) {
  13293. this._getBoundsStyle().userBounds = bound;
  13294. }
  13295. getBounds() {
  13296. return this._getBoundsStyle().bounds = Rectangle._getWrapRec(this._boundPointsToParent());
  13297. }
  13298. getSelfBounds() {
  13299. if (this._boundStyle && this._boundStyle.userBounds)
  13300. return this._boundStyle.userBounds;
  13301. if (!this._graphics && this._children.length === 0 && !this._texture)
  13302. return Rectangle.TEMP.setTo(0, 0, this.width, this.height);
  13303. return this._getBoundsStyle().bounds = Rectangle._getWrapRec(this._getBoundPointsM(false));
  13304. }
  13305. _boundPointsToParent(ifRotate = false) {
  13306. var pX = 0, pY = 0;
  13307. if (this._style) {
  13308. pX = this.pivotX;
  13309. pY = this.pivotY;
  13310. ifRotate = ifRotate || (this._style.rotation !== 0);
  13311. if (this._style.scrollRect) {
  13312. pX += this._style.scrollRect.x;
  13313. pY += this._style.scrollRect.y;
  13314. }
  13315. }
  13316. var pList = this._getBoundPointsM(ifRotate);
  13317. if (!pList || pList.length < 1)
  13318. return pList;
  13319. if (pList.length != 8) {
  13320. pList = ifRotate ? GrahamScan.scanPList(pList) : Rectangle._getWrapRec(pList, Rectangle.TEMP)._getBoundPoints();
  13321. }
  13322. if (!this.transform) {
  13323. Utils.transPointList(pList, this._x - pX, this._y - pY);
  13324. return pList;
  13325. }
  13326. var tPoint = Point.TEMP;
  13327. var i, len = pList.length;
  13328. for (i = 0; i < len; i += 2) {
  13329. tPoint.x = pList[i];
  13330. tPoint.y = pList[i + 1];
  13331. this.toParentPoint(tPoint);
  13332. pList[i] = tPoint.x;
  13333. pList[i + 1] = tPoint.y;
  13334. }
  13335. return pList;
  13336. }
  13337. getGraphicBounds(realSize = false) {
  13338. if (!this._graphics)
  13339. return Rectangle.TEMP.setTo(0, 0, 0, 0);
  13340. return this._graphics.getBounds(realSize);
  13341. }
  13342. _getBoundPointsM(ifRotate = false) {
  13343. if (this._boundStyle && this._boundStyle.userBounds)
  13344. return this._boundStyle.userBounds._getBoundPoints();
  13345. if (!this._boundStyle)
  13346. this._getBoundsStyle();
  13347. if (!this._boundStyle.temBM)
  13348. this._boundStyle.temBM = [];
  13349. if (this._style.scrollRect) {
  13350. var rst = Utils.clearArray(this._boundStyle.temBM);
  13351. var rec = Rectangle.TEMP;
  13352. rec.copyFrom(this._style.scrollRect);
  13353. Utils.concatArray(rst, rec._getBoundPoints());
  13354. return rst;
  13355. }
  13356. var pList;
  13357. if (this._graphics) {
  13358. pList = this._graphics.getBoundPoints();
  13359. }
  13360. else {
  13361. pList = Utils.clearArray(this._boundStyle.temBM);
  13362. if (this._texture) {
  13363. rec = Rectangle.TEMP;
  13364. rec.setTo(0, 0, this.width || this._texture.width, this.height || this._texture.height);
  13365. Utils.concatArray(pList, rec._getBoundPoints());
  13366. }
  13367. }
  13368. var child;
  13369. var cList;
  13370. var __childs;
  13371. __childs = this._children;
  13372. for (var i = 0, n = __childs.length; i < n; i++) {
  13373. child = __childs[i];
  13374. if (child instanceof Sprite && child._visible === true) {
  13375. cList = child._boundPointsToParent(ifRotate);
  13376. if (cList)
  13377. pList = pList ? Utils.concatArray(pList, cList) : cList;
  13378. }
  13379. }
  13380. return pList;
  13381. }
  13382. _getCacheStyle() {
  13383. this._cacheStyle === CacheStyle.EMPTY && (this._cacheStyle = CacheStyle.create());
  13384. return this._cacheStyle;
  13385. }
  13386. getStyle() {
  13387. this._style === SpriteStyle.EMPTY && (this._style = SpriteStyle.create());
  13388. return this._style;
  13389. }
  13390. setStyle(value) {
  13391. this._style = value;
  13392. }
  13393. get scaleX() {
  13394. return this._style.scaleX;
  13395. }
  13396. set scaleX(value) {
  13397. this.set_scaleX(value);
  13398. }
  13399. _setScaleX(value) {
  13400. this._style.scaleX = value;
  13401. }
  13402. get scaleY() {
  13403. return this._style.scaleY;
  13404. }
  13405. set scaleY(value) {
  13406. this.set_scaleY(value);
  13407. }
  13408. _setScaleY(value) {
  13409. this._style.scaleY = value;
  13410. }
  13411. set_scaleX(value) {
  13412. var style = this.getStyle();
  13413. if (style.scaleX !== value) {
  13414. this._setScaleX(value);
  13415. this._setTranformChange();
  13416. }
  13417. }
  13418. get_scaleX() {
  13419. return this._style.scaleX;
  13420. }
  13421. set_scaleY(value) {
  13422. var style = this.getStyle();
  13423. if (style.scaleY !== value) {
  13424. this._setScaleY(value);
  13425. this._setTranformChange();
  13426. }
  13427. }
  13428. get_scaleY() {
  13429. return this._style.scaleY;
  13430. }
  13431. get rotation() {
  13432. return this._style.rotation;
  13433. }
  13434. set rotation(value) {
  13435. var style = this.getStyle();
  13436. if (style.rotation !== value) {
  13437. this._setRotation(value);
  13438. this._setTranformChange();
  13439. }
  13440. }
  13441. _setRotation(value) {
  13442. this._style.rotation = value;
  13443. }
  13444. get skewX() {
  13445. return this._style.skewX;
  13446. }
  13447. set skewX(value) {
  13448. var style = this.getStyle();
  13449. if (style.skewX !== value) {
  13450. this._setSkewX(value);
  13451. this._setTranformChange();
  13452. }
  13453. }
  13454. _setSkewX(value) {
  13455. this._style.skewX = value;
  13456. }
  13457. get skewY() {
  13458. return this._style.skewY;
  13459. }
  13460. set skewY(value) {
  13461. var style = this.getStyle();
  13462. if (style.skewY !== value) {
  13463. this._setSkewY(value);
  13464. this._setTranformChange();
  13465. }
  13466. }
  13467. _setSkewY(value) {
  13468. this._style.skewY = value;
  13469. }
  13470. _createTransform() {
  13471. return Matrix.create();
  13472. }
  13473. _adjustTransform() {
  13474. this._tfChanged = false;
  13475. var style = this._style;
  13476. var sx = style.scaleX, sy = style.scaleY;
  13477. var sskx = style.skewX;
  13478. var ssky = style.skewY;
  13479. var rot = style.rotation;
  13480. var m = this._transform || (this._transform = this._createTransform());
  13481. if (rot || sx !== 1 || sy !== 1 || sskx !== 0 || ssky !== 0) {
  13482. m._bTransform = true;
  13483. var skx = (rot - sskx) * 0.0174532922222222;
  13484. var sky = (rot + ssky) * 0.0174532922222222;
  13485. var cx = Math.cos(sky);
  13486. var ssx = Math.sin(sky);
  13487. var cy = Math.sin(skx);
  13488. var ssy = Math.cos(skx);
  13489. m.a = sx * cx;
  13490. m.b = sx * ssx;
  13491. m.c = -sy * cy;
  13492. m.d = sy * ssy;
  13493. m.tx = m.ty = 0;
  13494. }
  13495. else {
  13496. m.identity();
  13497. this._renderType &= ~SpriteConst.TRANSFORM;
  13498. this._setRenderType(this._renderType);
  13499. }
  13500. return m;
  13501. }
  13502. _setTransform(value) {
  13503. }
  13504. get transform() {
  13505. return this._tfChanged ? this._adjustTransform() : this._transform;
  13506. }
  13507. set transform(value) {
  13508. this.set_transform(value);
  13509. }
  13510. get_transform() {
  13511. return this._tfChanged ? this._adjustTransform() : this._transform;
  13512. }
  13513. set_transform(value) {
  13514. this._tfChanged = false;
  13515. var m = this._transform || (this._transform = this._createTransform());
  13516. value.copyTo(m);
  13517. this._setTransform(m);
  13518. if (value) {
  13519. this._x = m.tx;
  13520. this._y = m.ty;
  13521. m.tx = m.ty = 0;
  13522. }
  13523. if (value)
  13524. this._renderType |= SpriteConst.TRANSFORM;
  13525. else {
  13526. this._renderType &= ~SpriteConst.TRANSFORM;
  13527. }
  13528. this._setRenderType(this._renderType);
  13529. this.parentRepaint();
  13530. }
  13531. _setPivotX(value) {
  13532. var style = this.getStyle();
  13533. style.pivotX = value;
  13534. }
  13535. _getPivotX() {
  13536. return this._style.pivotX;
  13537. }
  13538. _setPivotY(value) {
  13539. var style = this.getStyle();
  13540. style.pivotY = value;
  13541. }
  13542. _getPivotY() {
  13543. return this._style.pivotY;
  13544. }
  13545. get pivotX() {
  13546. return this._getPivotX();
  13547. }
  13548. set pivotX(value) {
  13549. this._setPivotX(value);
  13550. this.repaint();
  13551. }
  13552. get pivotY() {
  13553. return this._getPivotY();
  13554. }
  13555. set pivotY(value) {
  13556. this._setPivotY(value);
  13557. this.repaint();
  13558. }
  13559. _setAlpha(value) {
  13560. if (this._style.alpha !== value) {
  13561. var style = this.getStyle();
  13562. style.alpha = value;
  13563. if (value !== 1)
  13564. this._renderType |= SpriteConst.ALPHA;
  13565. else
  13566. this._renderType &= ~SpriteConst.ALPHA;
  13567. this._setRenderType(this._renderType);
  13568. this.parentRepaint();
  13569. }
  13570. }
  13571. _getAlpha() {
  13572. return this._style.alpha;
  13573. }
  13574. get alpha() {
  13575. return this._getAlpha();
  13576. }
  13577. set alpha(value) {
  13578. value = value < 0 ? 0 : (value > 1 ? 1 : value);
  13579. this._setAlpha(value);
  13580. }
  13581. get visible() {
  13582. return this.get_visible();
  13583. }
  13584. set visible(value) {
  13585. this.set_visible(value);
  13586. }
  13587. get_visible() {
  13588. return this._visible;
  13589. }
  13590. set_visible(value) {
  13591. if (this._visible !== value) {
  13592. this._visible = value;
  13593. this.parentRepaint(SpriteConst.REPAINT_ALL);
  13594. }
  13595. }
  13596. _setBlendMode(value) {
  13597. }
  13598. get blendMode() {
  13599. return this._style.blendMode;
  13600. }
  13601. set blendMode(value) {
  13602. this._setBlendMode(value);
  13603. this.getStyle().blendMode = value;
  13604. if (value && value != "source-over")
  13605. this._renderType |= SpriteConst.BLEND;
  13606. else
  13607. this._renderType &= ~SpriteConst.BLEND;
  13608. this._setRenderType(this._renderType);
  13609. this.parentRepaint();
  13610. }
  13611. get graphics() {
  13612. if (!this._graphics) {
  13613. this.graphics = new Graphics();
  13614. this._graphics.autoDestroy = true;
  13615. }
  13616. return this._graphics;
  13617. }
  13618. _setGraphics(value) {
  13619. }
  13620. _setGraphicsCallBack() {
  13621. }
  13622. set graphics(value) {
  13623. if (this._graphics)
  13624. this._graphics._sp = null;
  13625. this._graphics = value;
  13626. if (value) {
  13627. this._setGraphics(value);
  13628. this._renderType |= SpriteConst.GRAPHICS;
  13629. value._sp = this;
  13630. }
  13631. else {
  13632. this._renderType &= ~SpriteConst.GRAPHICS;
  13633. }
  13634. this._setRenderType(this._renderType);
  13635. this.repaint();
  13636. }
  13637. get scrollRect() {
  13638. return this._style.scrollRect;
  13639. }
  13640. _setScrollRect(value) {
  13641. }
  13642. set scrollRect(value) {
  13643. this.getStyle().scrollRect = value;
  13644. this._setScrollRect(value);
  13645. this.repaint();
  13646. if (value) {
  13647. this._renderType |= SpriteConst.CLIP;
  13648. }
  13649. else {
  13650. this._renderType &= ~SpriteConst.CLIP;
  13651. }
  13652. this._setRenderType(this._renderType);
  13653. }
  13654. pos(x, y, speedMode = false) {
  13655. if (this._x !== x || this._y !== y) {
  13656. if (this.destroyed)
  13657. return this;
  13658. if (speedMode) {
  13659. this._setX(x);
  13660. this._setY(y);
  13661. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  13662. var p = this._cacheStyle.maskParent;
  13663. if (p) {
  13664. p.repaint(SpriteConst.REPAINT_CACHE);
  13665. }
  13666. }
  13667. else {
  13668. this.x = x;
  13669. this.y = y;
  13670. }
  13671. }
  13672. return this;
  13673. }
  13674. pivot(x, y) {
  13675. this.pivotX = x;
  13676. this.pivotY = y;
  13677. return this;
  13678. }
  13679. size(width, height) {
  13680. this.width = width;
  13681. this.height = height;
  13682. return this;
  13683. }
  13684. scale(scaleX, scaleY, speedMode = false) {
  13685. var style = this.getStyle();
  13686. if (style.scaleX != scaleX || style.scaleY != scaleY) {
  13687. if (this.destroyed)
  13688. return this;
  13689. if (speedMode) {
  13690. this._setScaleX(scaleX);
  13691. this._setScaleY(scaleY);
  13692. this._setTranformChange();
  13693. }
  13694. else {
  13695. this.scaleX = scaleX;
  13696. this.scaleY = scaleY;
  13697. }
  13698. }
  13699. return this;
  13700. }
  13701. skew(skewX, skewY) {
  13702. this.skewX = skewX;
  13703. this.skewY = skewY;
  13704. return this;
  13705. }
  13706. render(ctx, x, y) {
  13707. RenderSprite.renders[this._renderType]._fun(this, ctx, x + this._x, y + this._y);
  13708. this._repaint = 0;
  13709. }
  13710. drawToCanvas(canvasWidth, canvasHeight, offsetX, offsetY) {
  13711. return Sprite.drawToCanvas(this, this._renderType, canvasWidth, canvasHeight, offsetX, offsetY);
  13712. }
  13713. drawToTexture(canvasWidth, canvasHeight, offsetX, offsetY, rt = null) {
  13714. return Sprite.drawToTexture(this, this._renderType, canvasWidth, canvasHeight, offsetX, offsetY, rt);
  13715. }
  13716. drawToTexture3D(offx, offy, tex) {
  13717. throw 'not implement';
  13718. }
  13719. static drawToCanvas(sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY) {
  13720. offsetX -= sprite.x;
  13721. offsetY -= sprite.y;
  13722. offsetX |= 0;
  13723. offsetY |= 0;
  13724. canvasWidth |= 0;
  13725. canvasHeight |= 0;
  13726. var ctx = new Context();
  13727. ctx.size(canvasWidth, canvasHeight);
  13728. ctx.asBitmap = true;
  13729. ctx._targets.start();
  13730. ctx._targets.clear(0, 0, 0, 0);
  13731. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  13732. ctx.flush();
  13733. ctx._targets.end();
  13734. ctx._targets.restore();
  13735. var dt = ctx._targets.getData(0, 0, canvasWidth, canvasHeight);
  13736. ctx.destroy();
  13737. var imgdata = new ImageData(canvasWidth, canvasHeight);
  13738. var lineLen = canvasWidth * 4;
  13739. var dst = imgdata.data;
  13740. var y = canvasHeight - 1;
  13741. var off = y * lineLen;
  13742. var srcoff = 0;
  13743. for (; y >= 0; y--) {
  13744. dst.set(dt.subarray(srcoff, srcoff + lineLen), off);
  13745. off -= lineLen;
  13746. srcoff += lineLen;
  13747. }
  13748. var canv = new HTMLCanvas(true);
  13749. canv.size(canvasWidth, canvasHeight);
  13750. var ctx2d = canv.getContext('2d');
  13751. ctx2d.putImageData(imgdata, 0, 0);
  13752. return canv;
  13753. }
  13754. static drawToTexture(sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY, rt = null) {
  13755. if (!Sprite.drawtocanvCtx) {
  13756. Sprite.drawtocanvCtx = new Context();
  13757. }
  13758. offsetX -= sprite.x;
  13759. offsetY -= sprite.y;
  13760. offsetX |= 0;
  13761. offsetY |= 0;
  13762. canvasWidth |= 0;
  13763. canvasHeight |= 0;
  13764. var ctx = rt ? Sprite.drawtocanvCtx : new Context();
  13765. ctx.clear();
  13766. ctx.size(canvasWidth, canvasHeight);
  13767. if (rt) {
  13768. ctx._targets = rt;
  13769. }
  13770. else {
  13771. ctx.asBitmap = true;
  13772. }
  13773. ctx._targets.start();
  13774. ctx._targets.clear(0, 0, 0, 0);
  13775. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  13776. ctx.flush();
  13777. ctx._targets.end();
  13778. ctx._targets.restore();
  13779. if (!rt) {
  13780. var rtex = new Texture(ctx._targets, Texture.INV_UV);
  13781. ctx.destroy(true);
  13782. return rtex;
  13783. }
  13784. return rt;
  13785. }
  13786. customRender(context, x, y) {
  13787. this._repaint = SpriteConst.REPAINT_ALL;
  13788. }
  13789. _applyFilters() {
  13790. }
  13791. get filters() {
  13792. return this._cacheStyle.filters;
  13793. }
  13794. _setColorFilter(value) { }
  13795. set filters(value) {
  13796. value && value.length === 0 && (value = null);
  13797. if (this._cacheStyle.filters == value)
  13798. return;
  13799. this._getCacheStyle().filters = value ? value.slice() : null;
  13800. if (value && value.length) {
  13801. this._setColorFilter(value[0]);
  13802. this._renderType |= SpriteConst.FILTERS;
  13803. }
  13804. else {
  13805. this._setColorFilter(null);
  13806. this._renderType &= ~SpriteConst.FILTERS;
  13807. }
  13808. this._setRenderType(this._renderType);
  13809. if (value && value.length > 0) {
  13810. if (!this._getBit(Const.DISPLAY))
  13811. this._setBitUp(Const.DISPLAY);
  13812. if (!(value.length == 1 && (value[0] instanceof ColorFilter))) {
  13813. this._getCacheStyle().cacheForFilters = true;
  13814. this._checkCanvasEnable();
  13815. }
  13816. }
  13817. else {
  13818. if (this._cacheStyle.cacheForFilters) {
  13819. this._cacheStyle.cacheForFilters = false;
  13820. this._checkCanvasEnable();
  13821. }
  13822. }
  13823. this._getCacheStyle().hasGlowFilter = this._isHaveGlowFilter();
  13824. this.repaint();
  13825. }
  13826. _isHaveGlowFilter() {
  13827. var i, len;
  13828. if (this.filters) {
  13829. for (i = 0; i < this.filters.length; i++) {
  13830. if (this.filters[i].type == Filter.GLOW) {
  13831. return true;
  13832. }
  13833. }
  13834. }
  13835. for (i = 0, len = this._children.length; i < len; i++) {
  13836. if (this._children[i]._isHaveGlowFilter()) {
  13837. return true;
  13838. }
  13839. }
  13840. return false;
  13841. }
  13842. localToGlobal(point, createNewPoint = false, globalNode = null) {
  13843. if (createNewPoint === true) {
  13844. point = new Point(point.x, point.y);
  13845. }
  13846. var ele = this;
  13847. globalNode = globalNode || ILaya.stage;
  13848. while (ele && !ele.destroyed) {
  13849. if (ele == globalNode)
  13850. break;
  13851. point = ele.toParentPoint(point);
  13852. ele = ele.parent;
  13853. }
  13854. return point;
  13855. }
  13856. globalToLocal(point, createNewPoint = false, globalNode = null) {
  13857. if (createNewPoint) {
  13858. point = new Point(point.x, point.y);
  13859. }
  13860. var ele = this;
  13861. var list = [];
  13862. globalNode = globalNode || ILaya.stage;
  13863. while (ele && !ele.destroyed) {
  13864. if (ele == globalNode)
  13865. break;
  13866. list.push(ele);
  13867. ele = ele.parent;
  13868. }
  13869. var i = list.length - 1;
  13870. while (i >= 0) {
  13871. ele = list[i];
  13872. point = ele.fromParentPoint(point);
  13873. i--;
  13874. }
  13875. return point;
  13876. }
  13877. toParentPoint(point) {
  13878. if (!point)
  13879. return point;
  13880. point.x -= this.pivotX;
  13881. point.y -= this.pivotY;
  13882. if (this.transform) {
  13883. this._transform.transformPoint(point);
  13884. }
  13885. point.x += this._x;
  13886. point.y += this._y;
  13887. var scroll = this._style.scrollRect;
  13888. if (scroll) {
  13889. point.x -= scroll.x;
  13890. point.y -= scroll.y;
  13891. }
  13892. return point;
  13893. }
  13894. fromParentPoint(point) {
  13895. if (!point)
  13896. return point;
  13897. point.x -= this._x;
  13898. point.y -= this._y;
  13899. var scroll = this._style.scrollRect;
  13900. if (scroll) {
  13901. point.x += scroll.x;
  13902. point.y += scroll.y;
  13903. }
  13904. if (this.transform) {
  13905. this._transform.invertTransformPoint(point);
  13906. }
  13907. point.x += this.pivotX;
  13908. point.y += this.pivotY;
  13909. return point;
  13910. }
  13911. fromStagePoint(point) {
  13912. return point;
  13913. }
  13914. on(type, caller, listener, args = null) {
  13915. if (this._mouseState !== 1 && this.isMouseEvent(type)) {
  13916. this.mouseEnabled = true;
  13917. this._setBit(Const.HAS_MOUSE, true);
  13918. if (this._parent) {
  13919. this._onDisplay();
  13920. }
  13921. return this._createListener(type, caller, listener, args, false);
  13922. }
  13923. return super.on(type, caller, listener, args);
  13924. }
  13925. once(type, caller, listener, args = null) {
  13926. if (this._mouseState !== 1 && this.isMouseEvent(type)) {
  13927. this.mouseEnabled = true;
  13928. this._setBit(Const.HAS_MOUSE, true);
  13929. if (this._parent) {
  13930. this._onDisplay();
  13931. }
  13932. return this._createListener(type, caller, listener, args, true);
  13933. }
  13934. return super.once(type, caller, listener, args);
  13935. }
  13936. _onDisplay(v) {
  13937. if (this._mouseState !== 1) {
  13938. var ele = this;
  13939. ele = ele.parent;
  13940. while (ele && ele._mouseState !== 1) {
  13941. if (ele._getBit(Const.HAS_MOUSE))
  13942. break;
  13943. ele.mouseEnabled = true;
  13944. ele._setBit(Const.HAS_MOUSE, true);
  13945. ele = ele.parent;
  13946. }
  13947. }
  13948. }
  13949. _setParent(value) {
  13950. super._setParent(value);
  13951. if (value && this._getBit(Const.HAS_MOUSE)) {
  13952. this._onDisplay();
  13953. }
  13954. }
  13955. loadImage(url, complete = null) {
  13956. if (!url) {
  13957. this.texture = null;
  13958. loaded.call(this);
  13959. }
  13960. else {
  13961. var tex = ILaya.Loader.textureMap[URL.formatURL(url)];
  13962. if (!tex) {
  13963. tex = new Texture();
  13964. tex.load(url);
  13965. ILaya.Loader.cacheTexture(url, tex);
  13966. }
  13967. this.texture = tex;
  13968. if (!tex.getIsReady())
  13969. tex.once(Event.READY, this, loaded);
  13970. else
  13971. loaded.call(this);
  13972. }
  13973. function loaded() {
  13974. this.repaint(SpriteConst.REPAINT_ALL);
  13975. complete && complete.run();
  13976. }
  13977. return this;
  13978. }
  13979. static fromImage(url) {
  13980. return new Sprite().loadImage(url);
  13981. }
  13982. repaint(type = SpriteConst.REPAINT_CACHE) {
  13983. if (!(this._repaint & type)) {
  13984. this._repaint |= type;
  13985. this.parentRepaint(type);
  13986. }
  13987. if (this._cacheStyle && this._cacheStyle.maskParent) {
  13988. this._cacheStyle.maskParent.repaint(type);
  13989. }
  13990. }
  13991. _needRepaint() {
  13992. return (this._repaint & SpriteConst.REPAINT_CACHE) && this._cacheStyle.enableCanvasRender && this._cacheStyle.reCache;
  13993. }
  13994. _childChanged(child = null) {
  13995. if (this._children.length)
  13996. this._renderType |= SpriteConst.CHILDS;
  13997. else
  13998. this._renderType &= ~SpriteConst.CHILDS;
  13999. this._setRenderType(this._renderType);
  14000. if (child && this._getBit(Const.HAS_ZORDER))
  14001. ILaya.systemTimer.callLater(this, this.updateZOrder);
  14002. this.repaint(SpriteConst.REPAINT_ALL);
  14003. }
  14004. parentRepaint(type = SpriteConst.REPAINT_CACHE) {
  14005. var p = this._parent;
  14006. if (p && !(p._repaint & type)) {
  14007. p._repaint |= type;
  14008. p.parentRepaint(type);
  14009. }
  14010. }
  14011. get stage() {
  14012. return ILaya.stage;
  14013. }
  14014. get hitArea() {
  14015. return this._style.hitArea;
  14016. }
  14017. set hitArea(value) {
  14018. this.getStyle().hitArea = value;
  14019. }
  14020. _setMask(value) {
  14021. }
  14022. get mask() {
  14023. return this._cacheStyle.mask;
  14024. }
  14025. set mask(value) {
  14026. if (value && this.mask && this.mask._cacheStyle.maskParent)
  14027. return;
  14028. this._getCacheStyle().mask = value;
  14029. this._setMask(value);
  14030. this._checkCanvasEnable();
  14031. if (value) {
  14032. value._getCacheStyle().maskParent = this;
  14033. }
  14034. else {
  14035. if (this.mask) {
  14036. this.mask._getCacheStyle().maskParent = null;
  14037. }
  14038. }
  14039. this._renderType |= SpriteConst.MASK;
  14040. this._setRenderType(this._renderType);
  14041. this.parentRepaint(SpriteConst.REPAINT_ALL);
  14042. }
  14043. get mouseEnabled() {
  14044. return this._mouseState > 1;
  14045. }
  14046. set mouseEnabled(value) {
  14047. this._mouseState = value ? 2 : 1;
  14048. }
  14049. startDrag(area = null, hasInertia = false, elasticDistance = 0, elasticBackTime = 300, data = null, disableMouseEvent = false, ratio = 0.92) {
  14050. this._style.dragging || (this.getStyle().dragging = new ILaya.Dragging());
  14051. this._style.dragging.start(this, area, hasInertia, elasticDistance, elasticBackTime, data, disableMouseEvent, ratio);
  14052. }
  14053. stopDrag() {
  14054. this._style.dragging && this._style.dragging.stop();
  14055. }
  14056. _setDisplay(value) {
  14057. if (!value) {
  14058. if (this._cacheStyle) {
  14059. this._cacheStyle.releaseContext();
  14060. this._cacheStyle.releaseFilterCache();
  14061. if (this._cacheStyle.hasGlowFilter) {
  14062. this._cacheStyle.hasGlowFilter = false;
  14063. }
  14064. }
  14065. }
  14066. super._setDisplay(value);
  14067. }
  14068. hitTestPoint(x, y) {
  14069. var point = this.globalToLocal(Point.TEMP.setTo(x, y));
  14070. x = point.x;
  14071. y = point.y;
  14072. 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();
  14073. return rect.contains(x, y);
  14074. }
  14075. getMousePoint() {
  14076. return this.globalToLocal(Point.TEMP.setTo(ILaya.stage.mouseX, ILaya.stage.mouseY));
  14077. }
  14078. get globalScaleX() {
  14079. var scale = 1;
  14080. var ele = this;
  14081. while (ele) {
  14082. if (ele === ILaya.stage)
  14083. break;
  14084. scale *= ele.scaleX;
  14085. ele = ele.parent;
  14086. }
  14087. return scale;
  14088. }
  14089. get globalRotation() {
  14090. var angle = 0;
  14091. var ele = this;
  14092. while (ele) {
  14093. if (ele === ILaya.stage)
  14094. break;
  14095. angle += ele.rotation;
  14096. ele = ele.parent;
  14097. }
  14098. return angle;
  14099. }
  14100. get globalScaleY() {
  14101. var scale = 1;
  14102. var ele = this;
  14103. while (ele) {
  14104. if (ele === ILaya.stage)
  14105. break;
  14106. scale *= ele.scaleY;
  14107. ele = ele.parent;
  14108. }
  14109. return scale;
  14110. }
  14111. get mouseX() {
  14112. return this.getMousePoint().x;
  14113. }
  14114. get mouseY() {
  14115. return this.getMousePoint().y;
  14116. }
  14117. get zOrder() {
  14118. return this._zOrder;
  14119. }
  14120. set zOrder(value) {
  14121. if (this._zOrder != value) {
  14122. this._zOrder = value;
  14123. if (this._parent) {
  14124. value && this._parent._setBit(Const.HAS_ZORDER, true);
  14125. ILaya.systemTimer.callLater(this._parent, this.updateZOrder);
  14126. }
  14127. }
  14128. }
  14129. get texture() {
  14130. return this._texture;
  14131. }
  14132. _setTexture(value) {
  14133. }
  14134. set texture(value) {
  14135. if (typeof (value) == 'string') {
  14136. this.loadImage(value);
  14137. }
  14138. else if (this._texture != value) {
  14139. this._texture && this._texture._removeReference();
  14140. this._texture = value;
  14141. value && value._addReference();
  14142. this._setTexture(value);
  14143. this._setWidth(this._texture, this.width);
  14144. this._setHeight(this._texture, this.height);
  14145. if (value)
  14146. this._renderType |= SpriteConst.TEXTURE;
  14147. else
  14148. this._renderType &= ~SpriteConst.TEXTURE;
  14149. this._setRenderType(this._renderType);
  14150. this.repaint();
  14151. }
  14152. }
  14153. get viewport() {
  14154. return this._style.viewport;
  14155. }
  14156. set viewport(value) {
  14157. if (typeof (value) == 'string') {
  14158. var recArr;
  14159. recArr = value.split(",");
  14160. if (recArr.length > 3) {
  14161. value = new Rectangle(parseFloat(recArr[0]), parseFloat(recArr[1]), parseFloat(recArr[2]), parseFloat(recArr[3]));
  14162. }
  14163. }
  14164. this.getStyle().viewport = value;
  14165. }
  14166. _setRenderType(type) {
  14167. }
  14168. _setTranformChange() {
  14169. this._tfChanged = true;
  14170. this._renderType |= SpriteConst.TRANSFORM;
  14171. this.parentRepaint(SpriteConst.REPAINT_CACHE);
  14172. }
  14173. _setBgStyleColor(x, y, width, height, fillColor) {
  14174. }
  14175. _setBorderStyleColor(x, y, width, height, fillColor, borderWidth) {
  14176. }
  14177. captureMouseEvent(exclusive) {
  14178. ILaya.MouseManager.instance.setCapture(this, exclusive);
  14179. }
  14180. releaseMouseEvent() {
  14181. ILaya.MouseManager.instance.releaseCapture();
  14182. }
  14183. set drawCallOptimize(value) {
  14184. this._setBit(Const.DRAWCALL_OPTIMIZE, value);
  14185. }
  14186. get drawCallOptimize() {
  14187. return this._getBit(Const.DRAWCALL_OPTIMIZE);
  14188. }
  14189. }
  14190. ClassUtils.regClass("laya.display.Sprite", Sprite);
  14191. ClassUtils.regClass("Laya.Sprite", Sprite);
  14192. class TextStyle extends SpriteStyle {
  14193. constructor() {
  14194. super(...arguments);
  14195. this.italic = false;
  14196. }
  14197. reset() {
  14198. super.reset();
  14199. this.italic = false;
  14200. this.align = "left";
  14201. this.wordWrap = false;
  14202. this.leading = 0;
  14203. this.padding = [0, 0, 0, 0];
  14204. this.bgColor = null;
  14205. this.borderColor = null;
  14206. this.asPassword = false;
  14207. this.stroke = 0;
  14208. this.strokeColor = "#000000";
  14209. this.bold = false;
  14210. this.underline = false;
  14211. this.underlineColor = null;
  14212. this.currBitmapFont = null;
  14213. return this;
  14214. }
  14215. recover() {
  14216. if (this === TextStyle.EMPTY)
  14217. return;
  14218. Pool.recover("TextStyle", this.reset());
  14219. }
  14220. static create() {
  14221. return Pool.getItemByClass("TextStyle", TextStyle);
  14222. }
  14223. render(sprite, context, x, y) {
  14224. (this.bgColor || this.borderColor) && context.drawRect(x, y, sprite.width, sprite.height, this.bgColor, this.borderColor, 1);
  14225. }
  14226. }
  14227. TextStyle.EMPTY = new TextStyle();
  14228. class Text extends Sprite {
  14229. constructor() {
  14230. super();
  14231. this._textWidth = 0;
  14232. this._textHeight = 0;
  14233. this._lines = [];
  14234. this._lineWidths = [];
  14235. this._startX = 0;
  14236. this._startY = 0;
  14237. this._charSize = {};
  14238. this._valign = "top";
  14239. this._fontSize = Text.defaultFontSize;
  14240. this._font = Text.defaultFont;
  14241. this._color = "#000000";
  14242. this._singleCharRender = false;
  14243. this.overflow = Text.VISIBLE;
  14244. this._style = TextStyle.EMPTY;
  14245. }
  14246. static defaultFontStr() {
  14247. return Text.defaultFontSize + "px " + Text.defaultFont;
  14248. }
  14249. getStyle() {
  14250. this._style === TextStyle.EMPTY && (this._style = TextStyle.create());
  14251. return this._style;
  14252. }
  14253. _getTextStyle() {
  14254. if (this._style === TextStyle.EMPTY) {
  14255. this._style = TextStyle.create();
  14256. }
  14257. return this._style;
  14258. }
  14259. static registerBitmapFont(name, bitmapFont) {
  14260. Text._bitmapFonts || (Text._bitmapFonts = {});
  14261. Text._bitmapFonts[name] = bitmapFont;
  14262. }
  14263. static unregisterBitmapFont(name, destroy = true) {
  14264. if (Text._bitmapFonts && Text._bitmapFonts[name]) {
  14265. var tBitmapFont = Text._bitmapFonts[name];
  14266. if (destroy)
  14267. tBitmapFont.destroy();
  14268. delete Text._bitmapFonts[name];
  14269. }
  14270. }
  14271. destroy(destroyChild = true) {
  14272. super.destroy(destroyChild);
  14273. this._clipPoint = null;
  14274. this._lines = null;
  14275. this._lineWidths = null;
  14276. this._words && this._words.forEach(function (w) {
  14277. w.cleanCache();
  14278. });
  14279. this._words = null;
  14280. this._charSize = null;
  14281. }
  14282. _getBoundPointsM(ifRotate = false) {
  14283. var rec = Rectangle.TEMP;
  14284. rec.setTo(0, 0, this.width, this.height);
  14285. return rec._getBoundPoints();
  14286. }
  14287. getGraphicBounds(realSize = false) {
  14288. var rec = Rectangle.TEMP;
  14289. rec.setTo(0, 0, this.width, this.height);
  14290. return rec;
  14291. }
  14292. get width() {
  14293. if (this._width)
  14294. return this._width;
  14295. return this.textWidth + this.padding[1] + this.padding[3];
  14296. }
  14297. set width(value) {
  14298. if (value != this._width) {
  14299. super.set_width(value);
  14300. this.isChanged = true;
  14301. if (this.borderColor) {
  14302. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  14303. }
  14304. }
  14305. }
  14306. _getCSSStyle() {
  14307. return this._style;
  14308. }
  14309. get height() {
  14310. if (this._height)
  14311. return this._height;
  14312. return this.textHeight;
  14313. }
  14314. set height(value) {
  14315. if (value != this._height) {
  14316. super.set_height(value);
  14317. this.isChanged = true;
  14318. if (this.borderColor) {
  14319. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  14320. }
  14321. }
  14322. }
  14323. get textWidth() {
  14324. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  14325. return this._textWidth;
  14326. }
  14327. get textHeight() {
  14328. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  14329. return this._textHeight;
  14330. }
  14331. get text() {
  14332. return this._text || "";
  14333. }
  14334. get_text() {
  14335. return this._text || "";
  14336. }
  14337. set_text(value) {
  14338. if (this._text !== value) {
  14339. this.lang(value + "");
  14340. this.isChanged = true;
  14341. this.event(Event.CHANGE);
  14342. if (this.borderColor) {
  14343. this._setBorderStyleColor(0, 0, this.width, this.height, this.borderColor, 1);
  14344. }
  14345. }
  14346. }
  14347. set text(value) {
  14348. this.set_text(value);
  14349. }
  14350. lang(text, arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null, arg8 = null, arg9 = null, arg10 = null) {
  14351. text = Text.langPacks && Text.langPacks[text] ? Text.langPacks[text] : text;
  14352. if (arguments.length < 2) {
  14353. this._text = text;
  14354. }
  14355. else {
  14356. for (var i = 0, n = arguments.length; i < n; i++) {
  14357. text = text.replace("{" + i + "}", arguments[i + 1]);
  14358. }
  14359. this._text = text;
  14360. }
  14361. }
  14362. get font() {
  14363. return this._font;
  14364. }
  14365. set font(value) {
  14366. if (this._style.currBitmapFont) {
  14367. this._getTextStyle().currBitmapFont = null;
  14368. this.scale(1, 1);
  14369. }
  14370. if (Text._bitmapFonts && Text._bitmapFonts[value]) {
  14371. this._getTextStyle().currBitmapFont = Text._bitmapFonts[value];
  14372. }
  14373. this._font = value;
  14374. this.isChanged = true;
  14375. }
  14376. get fontSize() {
  14377. return this._fontSize;
  14378. }
  14379. set fontSize(value) {
  14380. if (this._fontSize != value) {
  14381. this._fontSize = value;
  14382. this.isChanged = true;
  14383. }
  14384. }
  14385. get bold() {
  14386. return this._style.bold;
  14387. }
  14388. set bold(value) {
  14389. this._getTextStyle().bold = value;
  14390. this.isChanged = true;
  14391. }
  14392. get color() {
  14393. return this._color;
  14394. }
  14395. set color(value) {
  14396. this.set_color(value);
  14397. }
  14398. get_color() {
  14399. return this._color;
  14400. }
  14401. set_color(value) {
  14402. if (this._color != value) {
  14403. this._color = value;
  14404. if (!this._isChanged && this._graphics) {
  14405. this._graphics.replaceTextColor(this.color);
  14406. }
  14407. else {
  14408. this.isChanged = true;
  14409. }
  14410. }
  14411. }
  14412. get italic() {
  14413. return this._style.italic;
  14414. }
  14415. set italic(value) {
  14416. this._getTextStyle().italic = value;
  14417. this.isChanged = true;
  14418. }
  14419. get align() {
  14420. return this._style.align;
  14421. }
  14422. set align(value) {
  14423. this._getTextStyle().align = value;
  14424. this.isChanged = true;
  14425. }
  14426. get valign() {
  14427. return this._valign;
  14428. }
  14429. set valign(value) {
  14430. this._valign = value;
  14431. this.isChanged = true;
  14432. }
  14433. get wordWrap() {
  14434. return this._style.wordWrap;
  14435. }
  14436. set wordWrap(value) {
  14437. this._getTextStyle().wordWrap = value;
  14438. this.isChanged = true;
  14439. }
  14440. get leading() {
  14441. return this._style.leading;
  14442. }
  14443. set leading(value) {
  14444. this._getTextStyle().leading = value;
  14445. this.isChanged = true;
  14446. }
  14447. get padding() {
  14448. return this._style.padding;
  14449. }
  14450. set padding(value) {
  14451. if (typeof (value) == 'string') {
  14452. var arr;
  14453. arr = value.split(",");
  14454. var i, len;
  14455. len = arr.length;
  14456. while (arr.length < 4) {
  14457. arr.push(0);
  14458. }
  14459. for (i = 0; i < len; i++) {
  14460. arr[i] = parseFloat(arr[i]) || 0;
  14461. }
  14462. value = arr;
  14463. }
  14464. this._getTextStyle().padding = value;
  14465. this.isChanged = true;
  14466. }
  14467. get bgColor() {
  14468. return this._style.bgColor;
  14469. }
  14470. set bgColor(value) {
  14471. this.set_bgColor(value);
  14472. }
  14473. set_bgColor(value) {
  14474. this._getTextStyle().bgColor = value;
  14475. this._renderType |= SpriteConst.STYLE;
  14476. this._setBgStyleColor(0, 0, this.width, this.height, value);
  14477. this._setRenderType(this._renderType);
  14478. this.isChanged = true;
  14479. }
  14480. get_bgColor() {
  14481. return this._style.bgColor;
  14482. }
  14483. get borderColor() {
  14484. return this._style.borderColor;
  14485. }
  14486. set borderColor(value) {
  14487. this._getTextStyle().borderColor = value;
  14488. this._renderType |= SpriteConst.STYLE;
  14489. this._setBorderStyleColor(0, 0, this.width, this.height, value, 1);
  14490. this._setRenderType(this._renderType);
  14491. this.isChanged = true;
  14492. }
  14493. get stroke() {
  14494. return this._style.stroke;
  14495. }
  14496. set stroke(value) {
  14497. this._getTextStyle().stroke = value;
  14498. this.isChanged = true;
  14499. }
  14500. get strokeColor() {
  14501. return this._style.strokeColor;
  14502. }
  14503. set strokeColor(value) {
  14504. this._getTextStyle().strokeColor = value;
  14505. this.isChanged = true;
  14506. }
  14507. set isChanged(value) {
  14508. if (this._isChanged !== value) {
  14509. this._isChanged = value;
  14510. value && ILaya.systemTimer.callLater(this, this.typeset);
  14511. }
  14512. }
  14513. _getContextFont() {
  14514. return (this.italic ? "italic " : "") + (this.bold ? "bold " : "") + this.fontSize + "px " + (ILaya.Browser.onIPhone ? (Text.fontFamilyMap[this.font] || this.font) : this.font);
  14515. }
  14516. _isPassWordMode() {
  14517. var style = this._style;
  14518. var password = style.asPassword;
  14519. if (("prompt" in this) && this['prompt'] == this._text)
  14520. password = false;
  14521. return password;
  14522. }
  14523. _getPassWordTxt(txt) {
  14524. var len = txt.length;
  14525. var word;
  14526. word = "";
  14527. for (var j = len; j > 0; j--) {
  14528. word += "●";
  14529. }
  14530. return word;
  14531. }
  14532. _renderText() {
  14533. var padding = this.padding;
  14534. var visibleLineCount = this._lines.length;
  14535. if (this.overflow != Text.VISIBLE) {
  14536. visibleLineCount = Math.min(visibleLineCount, Math.floor((this.height - padding[0] - padding[2]) / (this.leading + this._charSize.height)) + 1);
  14537. }
  14538. var beginLine = this.scrollY / (this._charSize.height + this.leading) | 0;
  14539. var graphics = this.graphics;
  14540. graphics.clear(true);
  14541. var ctxFont = this._getContextFont();
  14542. ILaya.Browser.context.font = ctxFont;
  14543. var startX = padding[3];
  14544. var textAlgin = "left";
  14545. var lines = this._lines;
  14546. var lineHeight = this.leading + this._charSize.height;
  14547. var tCurrBitmapFont = this._style.currBitmapFont;
  14548. if (tCurrBitmapFont) {
  14549. lineHeight = this.leading + tCurrBitmapFont.getMaxHeight();
  14550. }
  14551. var startY = padding[0];
  14552. if ((!tCurrBitmapFont) && this._width > 0 && this._textWidth <= this._width) {
  14553. if (this.align == "right") {
  14554. textAlgin = "right";
  14555. startX = this._width - padding[1];
  14556. }
  14557. else if (this.align == "center") {
  14558. textAlgin = "center";
  14559. startX = this._width * 0.5 + padding[3] - padding[1];
  14560. }
  14561. }
  14562. if (this._height > 0) {
  14563. var tempVAlign = (this._textHeight > this._height) ? "top" : this.valign;
  14564. if (tempVAlign === "middle")
  14565. startY = (this._height - visibleLineCount * lineHeight) * 0.5 + padding[0] - padding[2];
  14566. else if (tempVAlign === "bottom")
  14567. startY = this._height - visibleLineCount * lineHeight - padding[2];
  14568. }
  14569. var style = this._style;
  14570. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  14571. var bitmapScale = tCurrBitmapFont.fontSize / this.fontSize;
  14572. }
  14573. if (this._clipPoint) {
  14574. graphics.save();
  14575. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  14576. var tClipWidth;
  14577. var tClipHeight;
  14578. this._width ? tClipWidth = (this._width - padding[3] - padding[1]) : tClipWidth = this._textWidth;
  14579. this._height ? tClipHeight = (this._height - padding[0] - padding[2]) : tClipHeight = this._textHeight;
  14580. tClipWidth *= bitmapScale;
  14581. tClipHeight *= bitmapScale;
  14582. graphics.clipRect(padding[3], padding[0], tClipWidth, tClipHeight);
  14583. }
  14584. else {
  14585. 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);
  14586. }
  14587. this.repaint();
  14588. }
  14589. var password = style.asPassword;
  14590. if (("prompt" in this) && this['prompt'] == this._text)
  14591. password = false;
  14592. var x = 0, y = 0;
  14593. var end = Math.min(this._lines.length, visibleLineCount + beginLine) || 1;
  14594. for (var i = beginLine; i < end; i++) {
  14595. var word = lines[i];
  14596. var _word;
  14597. if (password) {
  14598. var len = word.length;
  14599. word = "";
  14600. for (var j = len; j > 0; j--) {
  14601. word += "●";
  14602. }
  14603. }
  14604. if (word == null)
  14605. word = "";
  14606. x = startX - (this._clipPoint ? this._clipPoint.x : 0);
  14607. y = startY + lineHeight * i - (this._clipPoint ? this._clipPoint.y : 0);
  14608. this.underline && this._drawUnderline(textAlgin, x, y, i);
  14609. if (tCurrBitmapFont) {
  14610. var tWidth = this.width;
  14611. if (tCurrBitmapFont.autoScaleSize) {
  14612. tWidth = this.width * bitmapScale;
  14613. }
  14614. tCurrBitmapFont._drawText(word, this, x, y, this.align, tWidth);
  14615. }
  14616. else {
  14617. this._words || (this._words = []);
  14618. if (this._words.length > (i - beginLine)) {
  14619. _word = this._words[i - beginLine];
  14620. }
  14621. else {
  14622. _word = new WordText();
  14623. this._words.push(_word);
  14624. }
  14625. _word.setText(word);
  14626. _word.splitRender = this._singleCharRender;
  14627. style.stroke ? graphics.fillBorderText(_word, x, y, ctxFont, this.color, style.strokeColor, style.stroke, textAlgin) : graphics.fillText(_word, x, y, ctxFont, this.color, textAlgin);
  14628. }
  14629. }
  14630. if (tCurrBitmapFont && tCurrBitmapFont.autoScaleSize) {
  14631. var tScale = 1 / bitmapScale;
  14632. this.scale(tScale, tScale);
  14633. }
  14634. if (this._clipPoint)
  14635. graphics.restore();
  14636. this._startX = startX;
  14637. this._startY = startY;
  14638. }
  14639. _drawUnderline(align, x, y, lineIndex) {
  14640. var lineWidth = this._lineWidths[lineIndex];
  14641. switch (align) {
  14642. case 'center':
  14643. x -= lineWidth / 2;
  14644. break;
  14645. case 'right':
  14646. x -= lineWidth;
  14647. break;
  14648. case 'left':
  14649. default:
  14650. break;
  14651. }
  14652. y += this._charSize.height;
  14653. this._graphics.drawLine(x, y, x + lineWidth, y, this.underlineColor || this.color, 1);
  14654. }
  14655. typeset() {
  14656. this._isChanged = false;
  14657. if (!this._text) {
  14658. this._clipPoint = null;
  14659. this._textWidth = this._textHeight = 0;
  14660. this.graphics.clear(true);
  14661. return;
  14662. }
  14663. if (ILaya.Render.isConchApp) {
  14664. window.conchTextCanvas.font = this._getContextFont();
  14665. }
  14666. else {
  14667. ILaya.Browser.context.font = this._getContextFont();
  14668. }
  14669. this._lines.length = 0;
  14670. this._lineWidths.length = 0;
  14671. if (this._isPassWordMode()) {
  14672. this._parseLines(this._getPassWordTxt(this._text));
  14673. }
  14674. else
  14675. this._parseLines(this._text);
  14676. this._evalTextSize();
  14677. if (this._checkEnabledViewportOrNot())
  14678. this._clipPoint || (this._clipPoint = new Point(0, 0));
  14679. else
  14680. this._clipPoint = null;
  14681. this._renderText();
  14682. }
  14683. _evalTextSize() {
  14684. var nw, nh;
  14685. nw = Math.max.apply(this, this._lineWidths);
  14686. if (this._style.currBitmapFont)
  14687. nh = this._lines.length * (this._style.currBitmapFont.getMaxHeight() + this.leading) + this.padding[0] + this.padding[2];
  14688. else
  14689. nh = this._lines.length * (this._charSize.height + this.leading) + this.padding[0] + this.padding[2];
  14690. if (nw != this._textWidth || nh != this._textHeight) {
  14691. this._textWidth = nw;
  14692. this._textHeight = nh;
  14693. }
  14694. }
  14695. _checkEnabledViewportOrNot() {
  14696. return this.overflow == Text.SCROLL && ((this._width > 0 && this._textWidth > this._width) || (this._height > 0 && this._textHeight > this._height));
  14697. }
  14698. changeText(text) {
  14699. if (this._text !== text) {
  14700. this.lang(text + "");
  14701. if (this._graphics && this._graphics.replaceText(this._text)) ;
  14702. else {
  14703. this.typeset();
  14704. }
  14705. }
  14706. }
  14707. _parseLines(text) {
  14708. var needWordWrapOrTruncate = this.wordWrap || this.overflow == Text.HIDDEN;
  14709. if (needWordWrapOrTruncate) {
  14710. var wordWrapWidth = this._getWordWrapWidth();
  14711. }
  14712. var bitmapFont = this._style.currBitmapFont;
  14713. if (bitmapFont) {
  14714. this._charSize.width = bitmapFont.getMaxWidth();
  14715. this._charSize.height = bitmapFont.getMaxHeight();
  14716. }
  14717. else {
  14718. var measureResult = null;
  14719. if (ILaya.Render.isConchApp) {
  14720. measureResult = window.conchTextCanvas.measureText(Text._testWord);
  14721. }
  14722. else {
  14723. measureResult = ILaya.Browser.context.measureText(Text._testWord);
  14724. }
  14725. if (!measureResult)
  14726. measureResult = { width: 100 };
  14727. this._charSize.width = measureResult.width;
  14728. this._charSize.height = (measureResult.height || this.fontSize);
  14729. }
  14730. var lines = text.replace(/\r\n/g, "\n").split("\n");
  14731. for (var i = 0, n = lines.length; i < n; i++) {
  14732. var line = lines[i];
  14733. if (needWordWrapOrTruncate)
  14734. this._parseLine(line, wordWrapWidth);
  14735. else {
  14736. this._lineWidths.push(this._getTextWidth(line));
  14737. this._lines.push(line);
  14738. }
  14739. }
  14740. }
  14741. _parseLine(line, wordWrapWidth) {
  14742. var lines = this._lines;
  14743. var maybeIndex = 0;
  14744. var charsWidth = 0;
  14745. var wordWidth = 0;
  14746. var startIndex = 0;
  14747. charsWidth = this._getTextWidth(line);
  14748. if (charsWidth <= wordWrapWidth) {
  14749. lines.push(line);
  14750. this._lineWidths.push(charsWidth);
  14751. return;
  14752. }
  14753. charsWidth = this._charSize.width;
  14754. maybeIndex = Math.floor(wordWrapWidth / charsWidth);
  14755. (maybeIndex == 0) && (maybeIndex = 1);
  14756. charsWidth = this._getTextWidth(line.substring(0, maybeIndex));
  14757. wordWidth = charsWidth;
  14758. for (var j = maybeIndex, m = line.length; j < m; j++) {
  14759. charsWidth = this._getTextWidth(line.charAt(j));
  14760. wordWidth += charsWidth;
  14761. if (wordWidth > wordWrapWidth) {
  14762. if (this.wordWrap) {
  14763. var newLine = line.substring(startIndex, j);
  14764. if (newLine.charCodeAt(newLine.length - 1) < 255) {
  14765. var execResult = /(?:\w|-)+$/.exec(newLine);
  14766. if (execResult) {
  14767. j = execResult.index + startIndex;
  14768. if (execResult.index == 0)
  14769. j += newLine.length;
  14770. else
  14771. newLine = line.substring(startIndex, j);
  14772. }
  14773. }
  14774. lines.push(newLine);
  14775. this._lineWidths.push(wordWidth - charsWidth);
  14776. startIndex = j;
  14777. if (j + maybeIndex < m) {
  14778. j += maybeIndex;
  14779. charsWidth = this._getTextWidth(line.substring(startIndex, j));
  14780. wordWidth = charsWidth;
  14781. j--;
  14782. }
  14783. else {
  14784. lines.push(line.substring(startIndex, m));
  14785. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  14786. startIndex = -1;
  14787. break;
  14788. }
  14789. }
  14790. else if (this.overflow == Text.HIDDEN) {
  14791. lines.push(line.substring(0, j));
  14792. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  14793. return;
  14794. }
  14795. }
  14796. }
  14797. if (this.wordWrap && startIndex != -1) {
  14798. lines.push(line.substring(startIndex, m));
  14799. this._lineWidths.push(this._getTextWidth(lines[lines.length - 1]));
  14800. }
  14801. }
  14802. _getTextWidth(text) {
  14803. var bitmapFont = this._style.currBitmapFont;
  14804. if (bitmapFont)
  14805. return bitmapFont.getTextWidth(text);
  14806. else {
  14807. if (ILaya.Render.isConchApp) {
  14808. return window.conchTextCanvas.measureText(text).width;
  14809. }
  14810. else {
  14811. let ret = ILaya.Browser.context.measureText(text) || { width: 100 };
  14812. return ret.width;
  14813. }
  14814. }
  14815. }
  14816. _getWordWrapWidth() {
  14817. var p = this.padding;
  14818. var w;
  14819. var bitmapFont = this._style.currBitmapFont;
  14820. if (bitmapFont && bitmapFont.autoScaleSize)
  14821. w = this._width * (bitmapFont.fontSize / this.fontSize);
  14822. else
  14823. w = this._width;
  14824. if (w <= 0) {
  14825. w = this.wordWrap ? 100 : ILaya.Browser.width;
  14826. }
  14827. w <= 0 && (w = 100);
  14828. return w - p[3] - p[1];
  14829. }
  14830. getCharPoint(charIndex, out = null) {
  14831. this._isChanged && ILaya.systemTimer.runCallLater(this, this.typeset);
  14832. var len = 0, lines = this._lines, startIndex = 0;
  14833. for (var i = 0, n = lines.length; i < n; i++) {
  14834. len += lines[i].length;
  14835. if (charIndex < len) {
  14836. var line = i;
  14837. break;
  14838. }
  14839. startIndex = len;
  14840. }
  14841. var ctxFont = (this.italic ? "italic " : "") + (this.bold ? "bold " : "") + this.fontSize + "px " + this.font;
  14842. ILaya.Browser.context.font = ctxFont;
  14843. var width = this._getTextWidth(this._text.substring(startIndex, charIndex));
  14844. var point = out || new Point();
  14845. 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));
  14846. }
  14847. set scrollX(value) {
  14848. if (this.overflow != Text.SCROLL || (this.textWidth < this._width || !this._clipPoint))
  14849. return;
  14850. value = value < this.padding[3] ? this.padding[3] : value;
  14851. var maxScrollX = this._textWidth - this._width;
  14852. value = value > maxScrollX ? maxScrollX : value;
  14853. this._clipPoint.x = value;
  14854. this._renderText();
  14855. }
  14856. get scrollX() {
  14857. if (!this._clipPoint)
  14858. return 0;
  14859. return this._clipPoint.x;
  14860. }
  14861. set scrollY(value) {
  14862. if (this.overflow != Text.SCROLL || (this.textHeight < this._height || !this._clipPoint))
  14863. return;
  14864. value = value < this.padding[0] ? this.padding[0] : value;
  14865. var maxScrollY = this._textHeight - this._height;
  14866. value = value > maxScrollY ? maxScrollY : value;
  14867. this._clipPoint.y = value;
  14868. this._renderText();
  14869. }
  14870. get scrollY() {
  14871. if (!this._clipPoint)
  14872. return 0;
  14873. return this._clipPoint.y;
  14874. }
  14875. get maxScrollX() {
  14876. return (this.textWidth < this._width) ? 0 : this._textWidth - this._width;
  14877. }
  14878. get maxScrollY() {
  14879. return (this.textHeight < this._height) ? 0 : this._textHeight - this._height;
  14880. }
  14881. get lines() {
  14882. if (this._isChanged)
  14883. this.typeset();
  14884. return this._lines;
  14885. }
  14886. get underlineColor() {
  14887. return this._style.underlineColor;
  14888. }
  14889. set underlineColor(value) {
  14890. this._getTextStyle().underlineColor = value;
  14891. if (!this._isChanged)
  14892. this._renderText();
  14893. }
  14894. get underline() {
  14895. return this._style.underline;
  14896. }
  14897. set underline(value) {
  14898. this._getTextStyle().underline = value;
  14899. }
  14900. set singleCharRender(value) {
  14901. this._singleCharRender = value;
  14902. }
  14903. get singleCharRender() {
  14904. return this._singleCharRender;
  14905. }
  14906. }
  14907. Text.VISIBLE = "visible";
  14908. Text.SCROLL = "scroll";
  14909. Text.HIDDEN = "hidden";
  14910. Text.defaultFontSize = 12;
  14911. Text.defaultFont = "Arial";
  14912. Text.isComplexText = false;
  14913. Text.fontFamilyMap = { "报隶": "报隶-简", "黑体": "黑体-简", "楷体": "楷体-简", "兰亭黑": "兰亭黑-简", "隶变": "隶变-简", "凌慧体": "凌慧体-简", "翩翩体": "翩翩体-简", "苹方": "苹方-简", "手札体": "手札体-简", "宋体": "宋体-简", "娃娃体": "娃娃体-简", "魏碑": "魏碑-简", "行楷": "行楷-简", "雅痞": "雅痞-简", "圆体": "圆体-简" };
  14914. Text._testWord = "游";
  14915. Text.CharacterCache = true;
  14916. Text.RightToLeft = false;
  14917. ILaya.regClass(Text);
  14918. ClassUtils.regClass("laya.display.Text", Text);
  14919. ClassUtils.regClass("Laya.Text", Text);
  14920. class Input extends Text {
  14921. constructor() {
  14922. super();
  14923. this._multiline = false;
  14924. this._editable = true;
  14925. this._maxChars = 1E5;
  14926. this._type = "text";
  14927. this._prompt = '';
  14928. this._promptColor = "#A9A9A9";
  14929. this._originColor = "#000000";
  14930. this._content = '';
  14931. Input.IOS_IFRAME = (ILaya.Browser.onIOS && ILaya.Browser.window.top != ILaya.Browser.window.self);
  14932. this._width = 100;
  14933. this._height = 20;
  14934. this.multiline = false;
  14935. this.overflow = Text.SCROLL;
  14936. this.on(Event.MOUSE_DOWN, this, this._onMouseDown);
  14937. this.on(Event.UNDISPLAY, this, this._onUnDisplay);
  14938. }
  14939. static __init__() {
  14940. Input._createInputElement();
  14941. if (ILaya.Browser.onMobile) {
  14942. var isTrue = false;
  14943. if (ILaya.Browser.onMiniGame || ILaya.Browser.onBDMiniGame || ILaya.Browser.onQGMiniGame || ILaya.Browser.onKGMiniGame || ILaya.Browser.onVVMiniGame || ILaya.Browser.onAlipayMiniGame || ILaya.Browser.onQQMiniGame) {
  14944. isTrue = true;
  14945. }
  14946. ILaya.Render.canvas.addEventListener(Input.IOS_IFRAME ? (isTrue ? "touchend" : "click") : "touchend", Input._popupInputMethod);
  14947. }
  14948. }
  14949. static _popupInputMethod(e) {
  14950. if (!Input.isInputting)
  14951. return;
  14952. var input = Input.inputElement;
  14953. input.focus();
  14954. }
  14955. static _createInputElement() {
  14956. Input._initInput(Input.area = ILaya.Browser.createElement("textarea"));
  14957. Input._initInput(Input.input = ILaya.Browser.createElement("input"));
  14958. Input.inputContainer = ILaya.Browser.createElement("div");
  14959. Input.inputContainer.style.position = "absolute";
  14960. Input.inputContainer.style.zIndex = 1E5;
  14961. ILaya.Browser.container.appendChild(Input.inputContainer);
  14962. Input.inputContainer.setPos = function (x, y) {
  14963. Input.inputContainer.style.left = x + 'px';
  14964. Input.inputContainer.style.top = y + 'px';
  14965. };
  14966. }
  14967. static _initInput(input) {
  14968. var style = input.style;
  14969. 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;";
  14970. style.resize = 'none';
  14971. style.backgroundColor = 'transparent';
  14972. style.border = 'none';
  14973. style.outline = 'none';
  14974. style.zIndex = 1;
  14975. input.addEventListener('input', Input._processInputting);
  14976. input.addEventListener('mousemove', Input._stopEvent);
  14977. input.addEventListener('mousedown', Input._stopEvent);
  14978. input.addEventListener('touchmove', Input._stopEvent);
  14979. input.setFontFace = function (fontFace) { input.style.fontFamily = fontFace; };
  14980. if (!ILaya.Render.isConchApp) {
  14981. input.setColor = function (color) { input.style.color = color; };
  14982. input.setFontSize = function (fontSize) { input.style.fontSize = fontSize + 'px'; };
  14983. }
  14984. }
  14985. static _processInputting(e) {
  14986. var input = Input.inputElement.target;
  14987. if (!input)
  14988. return;
  14989. var value = Input.inputElement.value;
  14990. if (input._restrictPattern) {
  14991. value = value.replace(/\u2006|\x27/g, "");
  14992. if (input._restrictPattern.test(value)) {
  14993. value = value.replace(input._restrictPattern, "");
  14994. Input.inputElement.value = value;
  14995. }
  14996. }
  14997. input._text = value;
  14998. input.event(Event.INPUT);
  14999. }
  15000. static _stopEvent(e) {
  15001. if (e.type == 'touchmove')
  15002. e.preventDefault();
  15003. e.stopPropagation && e.stopPropagation();
  15004. }
  15005. setSelection(startIndex, endIndex) {
  15006. this.focus = true;
  15007. Input.inputElement.selectionStart = startIndex;
  15008. Input.inputElement.selectionEnd = endIndex;
  15009. }
  15010. get multiline() {
  15011. return this._multiline;
  15012. }
  15013. set multiline(value) {
  15014. this._multiline = value;
  15015. this.valign = value ? "top" : "middle";
  15016. }
  15017. get nativeInput() {
  15018. return this._multiline ? Input.area : Input.input;
  15019. }
  15020. _onUnDisplay(e = null) {
  15021. this.focus = false;
  15022. }
  15023. _onMouseDown(e) {
  15024. this.focus = true;
  15025. }
  15026. _syncInputTransform() {
  15027. var inputElement = this.nativeInput;
  15028. var transform = Utils.getTransformRelativeToWindow(this, this.padding[3], this.padding[0]);
  15029. var inputWid = this._width - this.padding[1] - this.padding[3];
  15030. var inputHei = this._height - this.padding[0] - this.padding[2];
  15031. if (ILaya.Render.isConchApp) {
  15032. inputElement.setScale(transform.scaleX, transform.scaleY);
  15033. inputElement.setSize(inputWid, inputHei);
  15034. inputElement.setPos(transform.x, transform.y);
  15035. }
  15036. else {
  15037. Input.inputContainer.style.transform = Input.inputContainer.style.webkitTransform = "scale(" + transform.scaleX + "," + transform.scaleY + ") rotate(" + (ILaya.stage.canvasDegree) + "deg)";
  15038. inputElement.style.width = inputWid + 'px';
  15039. inputElement.style.height = inputHei + 'px';
  15040. Input.inputContainer.style.left = transform.x + 'px';
  15041. Input.inputContainer.style.top = transform.y + 'px';
  15042. }
  15043. }
  15044. select() {
  15045. this.nativeInput.select();
  15046. }
  15047. get focus() {
  15048. return this._focus;
  15049. }
  15050. set focus(value) {
  15051. var input = this.nativeInput;
  15052. if (this._focus !== value) {
  15053. if (value) {
  15054. if (input.target) {
  15055. input.target._focusOut();
  15056. }
  15057. else {
  15058. this._setInputMethod();
  15059. }
  15060. input.target = this;
  15061. this._focusIn();
  15062. }
  15063. else {
  15064. input.target = null;
  15065. this._focusOut();
  15066. ILaya.Browser.document.body.scrollTop = 0;
  15067. input.blur();
  15068. if (ILaya.Render.isConchApp)
  15069. input.setPos(-10000, -10000);
  15070. else if (Input.inputContainer.contains(input))
  15071. Input.inputContainer.removeChild(input);
  15072. }
  15073. }
  15074. }
  15075. _setInputMethod() {
  15076. Input.input.parentElement && (Input.inputContainer.removeChild(Input.input));
  15077. Input.area.parentElement && (Input.inputContainer.removeChild(Input.area));
  15078. Input.inputElement = (this._multiline ? Input.area : Input.input);
  15079. Input.inputContainer.appendChild(Input.inputElement);
  15080. if (Text.RightToLeft) {
  15081. Input.inputElement.style.direction = "rtl";
  15082. }
  15083. }
  15084. _focusIn() {
  15085. Input.isInputting = true;
  15086. var input = this.nativeInput;
  15087. this._focus = true;
  15088. var cssStyle = input.style;
  15089. cssStyle.whiteSpace = (this.wordWrap ? "pre-wrap" : "nowrap");
  15090. this._setPromptColor();
  15091. input.readOnly = !this._editable;
  15092. if (ILaya.Render.isConchApp) {
  15093. input.setType(this._type);
  15094. input.setForbidEdit(!this._editable);
  15095. }
  15096. input.maxLength = this._maxChars;
  15097. var padding = this.padding;
  15098. input.value = this._content;
  15099. input.placeholder = this._prompt;
  15100. ILaya.stage.off(Event.KEY_DOWN, this, this._onKeyDown);
  15101. ILaya.stage.on(Event.KEY_DOWN, this, this._onKeyDown);
  15102. ILaya.stage.focus = this;
  15103. this.event(Event.FOCUS);
  15104. if (ILaya.Browser.onPC)
  15105. input.focus();
  15106. if (!ILaya.Browser.onMiniGame && !ILaya.Browser.onBDMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame) {
  15107. var temp = this._text;
  15108. this._text = null;
  15109. }
  15110. this.typeset();
  15111. input.setColor(this._originColor);
  15112. input.setFontSize(this.fontSize);
  15113. input.setFontFace(ILaya.Browser.onIPhone ? (Text.fontFamilyMap[this.font] || this.font) : this.font);
  15114. if (ILaya.Render.isConchApp) {
  15115. input.setMultiAble && input.setMultiAble(this._multiline);
  15116. }
  15117. cssStyle.lineHeight = (this.leading + this.fontSize) + "px";
  15118. cssStyle.fontStyle = (this.italic ? "italic" : "normal");
  15119. cssStyle.fontWeight = (this.bold ? "bold" : "normal");
  15120. cssStyle.textAlign = this.align;
  15121. cssStyle.padding = "0 0";
  15122. this._syncInputTransform();
  15123. if (!ILaya.Render.isConchApp && ILaya.Browser.onPC)
  15124. ILaya.systemTimer.frameLoop(1, this, this._syncInputTransform);
  15125. }
  15126. _setPromptColor() {
  15127. Input.promptStyleDOM = ILaya.Browser.getElementById("promptStyle");
  15128. if (!Input.promptStyleDOM) {
  15129. Input.promptStyleDOM = ILaya.Browser.createElement("style");
  15130. Input.promptStyleDOM.setAttribute("id", "promptStyle");
  15131. ILaya.Browser.document.head.appendChild(Input.promptStyleDOM);
  15132. }
  15133. 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 + "}";
  15134. }
  15135. _focusOut() {
  15136. if (!Input.isInputting)
  15137. return;
  15138. Input.isInputting = false;
  15139. this._focus = false;
  15140. this._text = null;
  15141. this._content = this.nativeInput.value;
  15142. if (!this._content) {
  15143. super.set_text(this._prompt);
  15144. super.set_color(this._promptColor);
  15145. }
  15146. else {
  15147. super.set_text(this._content);
  15148. super.set_color(this._originColor);
  15149. }
  15150. ILaya.stage.off(Event.KEY_DOWN, this, this._onKeyDown);
  15151. ILaya.stage.focus = null;
  15152. this.event(Event.BLUR);
  15153. this.event(Event.CHANGE);
  15154. if (ILaya.Render.isConchApp)
  15155. this.nativeInput.blur();
  15156. ILaya.Browser.onPC && ILaya.systemTimer.clear(this, this._syncInputTransform);
  15157. }
  15158. _onKeyDown(e) {
  15159. if (e.keyCode === 13) {
  15160. if (ILaya.Browser.onMobile && !this._multiline)
  15161. this.focus = false;
  15162. this.event(Event.ENTER);
  15163. }
  15164. }
  15165. set text(value) {
  15166. super.set_color(this._originColor);
  15167. value += '';
  15168. if (this._focus) {
  15169. this.nativeInput.value = value || '';
  15170. this.event(Event.CHANGE);
  15171. }
  15172. else {
  15173. if (!this._multiline)
  15174. value = value.replace(/\r?\n/g, '');
  15175. this._content = value;
  15176. if (value)
  15177. super.set_text(value);
  15178. else {
  15179. super.set_text(this._prompt);
  15180. super.set_color(this.promptColor);
  15181. }
  15182. }
  15183. }
  15184. get text() {
  15185. if (this._focus)
  15186. return this.nativeInput.value;
  15187. else
  15188. return this._content || "";
  15189. }
  15190. changeText(text) {
  15191. this._content = text;
  15192. if (this._focus) {
  15193. this.nativeInput.value = text || '';
  15194. this.event(Event.CHANGE);
  15195. }
  15196. else
  15197. super.changeText(text);
  15198. }
  15199. set color(value) {
  15200. if (this._focus)
  15201. this.nativeInput.setColor(value);
  15202. super.set_color(this._content ? value : this._promptColor);
  15203. this._originColor = value;
  15204. }
  15205. get color() {
  15206. return super.color;
  15207. }
  15208. set bgColor(value) {
  15209. super.set_bgColor(value);
  15210. if (ILaya.Render.isConchApp)
  15211. this.nativeInput.setBgColor(value);
  15212. }
  15213. get bgColor() {
  15214. return super.bgColor;
  15215. }
  15216. get restrict() {
  15217. if (this._restrictPattern) {
  15218. return this._restrictPattern.source;
  15219. }
  15220. return "";
  15221. }
  15222. set restrict(pattern) {
  15223. if (pattern) {
  15224. pattern = "[^" + pattern + "]";
  15225. if (pattern.indexOf("^^") > -1)
  15226. pattern = pattern.replace("^^", "");
  15227. this._restrictPattern = new RegExp(pattern, "g");
  15228. }
  15229. else
  15230. this._restrictPattern = null;
  15231. }
  15232. set editable(value) {
  15233. this._editable = value;
  15234. if (ILaya.Render.isConchApp) {
  15235. Input.input.setForbidEdit(!value);
  15236. }
  15237. }
  15238. get editable() {
  15239. return this._editable;
  15240. }
  15241. get maxChars() {
  15242. return this._maxChars;
  15243. }
  15244. set maxChars(value) {
  15245. if (value <= 0)
  15246. value = 1E5;
  15247. this._maxChars = value;
  15248. }
  15249. get prompt() {
  15250. return this._prompt;
  15251. }
  15252. set prompt(value) {
  15253. if (!this._text && value)
  15254. super.set_color(this._promptColor);
  15255. this.promptColor = this._promptColor;
  15256. if (this._text)
  15257. super.set_text((this._text == this._prompt) ? value : this._text);
  15258. else
  15259. super.set_text(value);
  15260. this._prompt = Text.langPacks && Text.langPacks[value] ? Text.langPacks[value] : value;
  15261. }
  15262. get promptColor() {
  15263. return this._promptColor;
  15264. }
  15265. set promptColor(value) {
  15266. this._promptColor = value;
  15267. if (!this._content)
  15268. super.set_color(value);
  15269. }
  15270. get type() {
  15271. return this._type;
  15272. }
  15273. set type(value) {
  15274. if (value === "password")
  15275. this._getTextStyle().asPassword = true;
  15276. else
  15277. this._getTextStyle().asPassword = false;
  15278. this._type = value;
  15279. }
  15280. }
  15281. Input.TYPE_TEXT = "text";
  15282. Input.TYPE_PASSWORD = "password";
  15283. Input.TYPE_EMAIL = "email";
  15284. Input.TYPE_URL = "url";
  15285. Input.TYPE_NUMBER = "number";
  15286. Input.TYPE_RANGE = "range";
  15287. Input.TYPE_DATE = "date";
  15288. Input.TYPE_MONTH = "month";
  15289. Input.TYPE_WEEK = "week";
  15290. Input.TYPE_TIME = "time";
  15291. Input.TYPE_DATE_TIME = "datetime";
  15292. Input.TYPE_DATE_TIME_LOCAL = "datetime-local";
  15293. Input.TYPE_SEARCH = "search";
  15294. Input.IOS_IFRAME = false;
  15295. Input.inputHeight = 45;
  15296. Input.isInputting = false;
  15297. ClassUtils.regClass("laya.display.Input", Input);
  15298. ClassUtils.regClass("Laya.Input", Input);
  15299. class TouchManager {
  15300. constructor() {
  15301. this.preOvers = [];
  15302. this.preDowns = [];
  15303. this.preRightDowns = [];
  15304. this.enable = true;
  15305. this._event = new Event();
  15306. this._lastClickTime = 0;
  15307. }
  15308. _clearTempArrs() {
  15309. TouchManager._oldArr.length = 0;
  15310. TouchManager._newArr.length = 0;
  15311. TouchManager._tEleArr.length = 0;
  15312. }
  15313. getTouchFromArr(touchID, arr) {
  15314. var i, len;
  15315. len = arr.length;
  15316. var tTouchO;
  15317. for (i = 0; i < len; i++) {
  15318. tTouchO = arr[i];
  15319. if (tTouchO.id == touchID) {
  15320. return tTouchO;
  15321. }
  15322. }
  15323. return null;
  15324. }
  15325. removeTouchFromArr(touchID, arr) {
  15326. var i;
  15327. for (i = arr.length - 1; i >= 0; i--) {
  15328. if (arr[i].id == touchID) {
  15329. arr.splice(i, 1);
  15330. }
  15331. }
  15332. }
  15333. createTouchO(ele, touchID) {
  15334. var rst;
  15335. rst = Pool.getItem("TouchData") || {};
  15336. rst.id = touchID;
  15337. rst.tar = ele;
  15338. return rst;
  15339. }
  15340. onMouseDown(ele, touchID, isLeft = false) {
  15341. if (!this.enable)
  15342. return;
  15343. var preO;
  15344. var tO;
  15345. var arrs;
  15346. preO = this.getTouchFromArr(touchID, this.preOvers);
  15347. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  15348. if (!preO) {
  15349. tO = this.createTouchO(ele, touchID);
  15350. this.preOvers.push(tO);
  15351. }
  15352. else {
  15353. preO.tar = ele;
  15354. }
  15355. if (Browser.onMobile)
  15356. this.sendEvents(arrs, Event.MOUSE_OVER);
  15357. var preDowns;
  15358. preDowns = isLeft ? this.preDowns : this.preRightDowns;
  15359. preO = this.getTouchFromArr(touchID, preDowns);
  15360. if (!preO) {
  15361. tO = this.createTouchO(ele, touchID);
  15362. preDowns.push(tO);
  15363. }
  15364. else {
  15365. preO.tar = ele;
  15366. }
  15367. this.sendEvents(arrs, isLeft ? Event.MOUSE_DOWN : Event.RIGHT_MOUSE_DOWN);
  15368. this._clearTempArrs();
  15369. }
  15370. sendEvents(eles, type) {
  15371. var i, len;
  15372. len = eles.length;
  15373. this._event._stoped = false;
  15374. var _target;
  15375. _target = eles[0];
  15376. for (i = 0; i < len; i++) {
  15377. var tE = eles[i];
  15378. if (tE.destroyed)
  15379. return;
  15380. tE.event(type, this._event.setTo(type, tE, _target));
  15381. if (this._event._stoped)
  15382. break;
  15383. }
  15384. }
  15385. getEles(start, end = null, rst = null) {
  15386. if (!rst) {
  15387. rst = [];
  15388. }
  15389. else {
  15390. rst.length = 0;
  15391. }
  15392. while (start && start != end) {
  15393. rst.push(start);
  15394. start = start.parent;
  15395. }
  15396. return rst;
  15397. }
  15398. checkMouseOutAndOverOfMove(eleNew, elePre, touchID = 0) {
  15399. if (elePre == eleNew)
  15400. return;
  15401. var tar;
  15402. var arrs;
  15403. var i, len;
  15404. if (elePre.contains(eleNew)) {
  15405. arrs = this.getEles(eleNew, elePre, TouchManager._tEleArr);
  15406. this.sendEvents(arrs, Event.MOUSE_OVER);
  15407. }
  15408. else if (eleNew.contains(elePre)) {
  15409. arrs = this.getEles(elePre, eleNew, TouchManager._tEleArr);
  15410. this.sendEvents(arrs, Event.MOUSE_OUT);
  15411. }
  15412. else {
  15413. arrs = TouchManager._tEleArr;
  15414. arrs.length = 0;
  15415. var oldArr;
  15416. oldArr = this.getEles(elePre, null, TouchManager._oldArr);
  15417. var newArr;
  15418. newArr = this.getEles(eleNew, null, TouchManager._newArr);
  15419. len = oldArr.length;
  15420. var tIndex;
  15421. for (i = 0; i < len; i++) {
  15422. tar = oldArr[i];
  15423. tIndex = newArr.indexOf(tar);
  15424. if (tIndex >= 0) {
  15425. newArr.splice(tIndex, newArr.length - tIndex);
  15426. break;
  15427. }
  15428. else {
  15429. arrs.push(tar);
  15430. }
  15431. }
  15432. if (arrs.length > 0) {
  15433. this.sendEvents(arrs, Event.MOUSE_OUT);
  15434. }
  15435. if (newArr.length > 0) {
  15436. this.sendEvents(newArr, Event.MOUSE_OVER);
  15437. }
  15438. }
  15439. }
  15440. onMouseMove(ele, touchID) {
  15441. if (!this.enable)
  15442. return;
  15443. var preO;
  15444. preO = this.getTouchFromArr(touchID, this.preOvers);
  15445. var arrs;
  15446. if (!preO) {
  15447. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  15448. this.sendEvents(arrs, Event.MOUSE_OVER);
  15449. this.preOvers.push(this.createTouchO(ele, touchID));
  15450. }
  15451. else {
  15452. this.checkMouseOutAndOverOfMove(ele, preO.tar);
  15453. preO.tar = ele;
  15454. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  15455. }
  15456. this.sendEvents(arrs, Event.MOUSE_MOVE);
  15457. this._clearTempArrs();
  15458. }
  15459. getLastOvers() {
  15460. TouchManager._tEleArr.length = 0;
  15461. if (this.preOvers.length > 0 && this.preOvers[0].tar) {
  15462. return this.getEles(this.preOvers[0].tar, null, TouchManager._tEleArr);
  15463. }
  15464. TouchManager._tEleArr.push(ILaya.stage);
  15465. return TouchManager._tEleArr;
  15466. }
  15467. stageMouseOut() {
  15468. var lastOvers;
  15469. lastOvers = this.getLastOvers();
  15470. this.preOvers.length = 0;
  15471. this.sendEvents(lastOvers, Event.MOUSE_OUT);
  15472. }
  15473. onMouseUp(ele, touchID, isLeft = false) {
  15474. if (!this.enable)
  15475. return;
  15476. var preO;
  15477. var arrs;
  15478. var oldArr;
  15479. var i, len;
  15480. var tar;
  15481. var sendArr;
  15482. var onMobile = Browser.onMobile;
  15483. arrs = this.getEles(ele, null, TouchManager._tEleArr);
  15484. this.sendEvents(arrs, isLeft ? Event.MOUSE_UP : Event.RIGHT_MOUSE_UP);
  15485. var preDowns;
  15486. preDowns = isLeft ? this.preDowns : this.preRightDowns;
  15487. preO = this.getTouchFromArr(touchID, preDowns);
  15488. if (!preO) ;
  15489. else {
  15490. var isDouble;
  15491. var now = Browser.now();
  15492. isDouble = now - this._lastClickTime < 300;
  15493. this._lastClickTime = now;
  15494. if (ele == preO.tar) {
  15495. sendArr = arrs;
  15496. }
  15497. else {
  15498. oldArr = this.getEles(preO.tar, null, TouchManager._oldArr);
  15499. sendArr = TouchManager._newArr;
  15500. sendArr.length = 0;
  15501. len = oldArr.length;
  15502. for (i = 0; i < len; i++) {
  15503. tar = oldArr[i];
  15504. if (arrs.indexOf(tar) >= 0) {
  15505. sendArr.push(tar);
  15506. }
  15507. }
  15508. }
  15509. if (sendArr.length > 0) {
  15510. this.sendEvents(sendArr, isLeft ? Event.CLICK : Event.RIGHT_CLICK);
  15511. }
  15512. if (isLeft && isDouble) {
  15513. this.sendEvents(sendArr, Event.DOUBLE_CLICK);
  15514. }
  15515. this.removeTouchFromArr(touchID, preDowns);
  15516. preO.tar = null;
  15517. Pool.recover("TouchData", preO);
  15518. }
  15519. preO = this.getTouchFromArr(touchID, this.preOvers);
  15520. if (!preO) ;
  15521. else {
  15522. if (onMobile) {
  15523. sendArr = this.getEles(preO.tar, null, sendArr);
  15524. if (sendArr && sendArr.length > 0) {
  15525. this.sendEvents(sendArr, Event.MOUSE_OUT);
  15526. }
  15527. this.removeTouchFromArr(touchID, this.preOvers);
  15528. preO.tar = null;
  15529. Pool.recover("TouchData", preO);
  15530. }
  15531. }
  15532. this._clearTempArrs();
  15533. }
  15534. }
  15535. TouchManager.I = new TouchManager();
  15536. TouchManager._oldArr = [];
  15537. TouchManager._newArr = [];
  15538. TouchManager._tEleArr = [];
  15539. class MouseManager {
  15540. constructor() {
  15541. this.mouseX = 0;
  15542. this.mouseY = 0;
  15543. this.disableMouseEvent = false;
  15544. this.mouseDownTime = 0;
  15545. this.mouseMoveAccuracy = 2;
  15546. this._event = new Event();
  15547. this._captureSp = null;
  15548. this._captureChain = [];
  15549. this._captureExlusiveMode = false;
  15550. this._hitCaputreSp = false;
  15551. this._point = new Point();
  15552. this._rect = new Rectangle();
  15553. this._lastMoveTimer = 0;
  15554. this._prePoint = new Point();
  15555. this._touchIDs = {};
  15556. this._curTouchID = NaN;
  15557. this._id = 1;
  15558. }
  15559. __init__(stage, canvas) {
  15560. this._stage = stage;
  15561. var _this = this;
  15562. canvas.oncontextmenu = function (e) {
  15563. if (MouseManager.enabled)
  15564. return false;
  15565. };
  15566. canvas.addEventListener('mousedown', function (e) {
  15567. if (MouseManager.enabled) {
  15568. if (!Browser.onIE)
  15569. (e.cancelable) && (e.preventDefault());
  15570. _this.mouseDownTime = Browser.now();
  15571. _this.runEvent(e);
  15572. }
  15573. });
  15574. canvas.addEventListener('mouseup', function (e) {
  15575. if (MouseManager.enabled) {
  15576. (e.cancelable) && (e.preventDefault());
  15577. _this.mouseDownTime = -Browser.now();
  15578. _this.runEvent(e);
  15579. }
  15580. }, true);
  15581. canvas.addEventListener('mousemove', function (e) {
  15582. if (MouseManager.enabled) {
  15583. (e.cancelable) && (e.preventDefault());
  15584. var now = Browser.now();
  15585. if (now - _this._lastMoveTimer < 10)
  15586. return;
  15587. _this._lastMoveTimer = now;
  15588. _this.runEvent(e);
  15589. }
  15590. }, true);
  15591. canvas.addEventListener("mouseout", function (e) {
  15592. if (MouseManager.enabled)
  15593. _this.runEvent(e);
  15594. });
  15595. canvas.addEventListener("mouseover", function (e) {
  15596. if (MouseManager.enabled)
  15597. _this.runEvent(e);
  15598. });
  15599. canvas.addEventListener("touchstart", function (e) {
  15600. if (MouseManager.enabled) {
  15601. if (!MouseManager._isFirstTouch && !Input.isInputting)
  15602. (e.cancelable) && (e.preventDefault());
  15603. _this.mouseDownTime = Browser.now();
  15604. _this.runEvent(e);
  15605. }
  15606. });
  15607. canvas.addEventListener("touchend", function (e) {
  15608. if (MouseManager.enabled) {
  15609. if (!MouseManager._isFirstTouch && !Input.isInputting)
  15610. (e.cancelable) && (e.preventDefault());
  15611. MouseManager._isFirstTouch = false;
  15612. _this.mouseDownTime = -Browser.now();
  15613. _this.runEvent(e);
  15614. }
  15615. else {
  15616. _this._curTouchID = NaN;
  15617. }
  15618. }, true);
  15619. canvas.addEventListener("touchmove", function (e) {
  15620. if (MouseManager.enabled) {
  15621. (e.cancelable) && (e.preventDefault());
  15622. _this.runEvent(e);
  15623. }
  15624. }, true);
  15625. canvas.addEventListener("touchcancel", function (e) {
  15626. if (MouseManager.enabled) {
  15627. (e.cancelable) && (e.preventDefault());
  15628. _this.runEvent(e);
  15629. }
  15630. else {
  15631. _this._curTouchID = NaN;
  15632. }
  15633. }, true);
  15634. canvas.addEventListener('mousewheel', function (e) {
  15635. if (MouseManager.enabled)
  15636. _this.runEvent(e);
  15637. });
  15638. canvas.addEventListener('DOMMouseScroll', function (e) {
  15639. if (MouseManager.enabled)
  15640. _this.runEvent(e);
  15641. });
  15642. }
  15643. initEvent(e, nativeEvent = null) {
  15644. var _this = this;
  15645. _this._event._stoped = false;
  15646. _this._event.nativeEvent = nativeEvent || e;
  15647. _this._target = null;
  15648. this._point.setTo(e.pageX || e.clientX, e.pageY || e.clientY);
  15649. if (this._stage._canvasTransform) {
  15650. this._stage._canvasTransform.invertTransformPoint(this._point);
  15651. _this.mouseX = this._point.x;
  15652. _this.mouseY = this._point.y;
  15653. }
  15654. _this._event.touchId = e.identifier || 0;
  15655. this._tTouchID = _this._event.touchId;
  15656. var evt;
  15657. evt = TouchManager.I._event;
  15658. evt._stoped = false;
  15659. evt.nativeEvent = _this._event.nativeEvent;
  15660. evt.touchId = _this._event.touchId;
  15661. }
  15662. checkMouseWheel(e) {
  15663. this._event.delta = e.wheelDelta ? e.wheelDelta * 0.025 : -e.detail;
  15664. var _lastOvers = TouchManager.I.getLastOvers();
  15665. for (var i = 0, n = _lastOvers.length; i < n; i++) {
  15666. var ele = _lastOvers[i];
  15667. ele.event(Event.MOUSE_WHEEL, this._event.setTo(Event.MOUSE_WHEEL, ele, this._target));
  15668. }
  15669. }
  15670. onMouseMove(ele) {
  15671. TouchManager.I.onMouseMove(ele, this._tTouchID);
  15672. }
  15673. onMouseDown(ele) {
  15674. if (Input.isInputting && ILaya.stage.focus && ILaya.stage.focus["focus"] && !ILaya.stage.focus.contains(this._target)) {
  15675. var pre_input = ILaya.stage.focus['_tf'] || ILaya.stage.focus;
  15676. var new_input = ele['_tf'] || ele;
  15677. if (new_input instanceof Input && new_input.multiline == pre_input.multiline)
  15678. pre_input['_focusOut']();
  15679. else
  15680. pre_input.focus = false;
  15681. }
  15682. TouchManager.I.onMouseDown(ele, this._tTouchID, this._isLeftMouse);
  15683. }
  15684. onMouseUp(ele) {
  15685. TouchManager.I.onMouseUp(ele, this._tTouchID, this._isLeftMouse);
  15686. }
  15687. check(sp, mouseX, mouseY, callBack) {
  15688. this._point.setTo(mouseX, mouseY);
  15689. sp.fromParentPoint(this._point);
  15690. mouseX = this._point.x;
  15691. mouseY = this._point.y;
  15692. var scrollRect = sp._style.scrollRect;
  15693. if (scrollRect) {
  15694. this._rect.setTo(scrollRect.x, scrollRect.y, scrollRect.width, scrollRect.height);
  15695. if (!this._rect.contains(mouseX, mouseY))
  15696. return false;
  15697. }
  15698. if (!this.disableMouseEvent) {
  15699. if (sp.hitTestPrior && !sp.mouseThrough && !this.hitTest(sp, mouseX, mouseY)) {
  15700. return false;
  15701. }
  15702. for (var i = sp._children.length - 1; i > -1; i--) {
  15703. var child = sp._children[i];
  15704. if (!child.destroyed && child._mouseState > 1 && child._visible) {
  15705. if (this.check(child, mouseX, mouseY, callBack))
  15706. return true;
  15707. }
  15708. }
  15709. for (i = sp._extUIChild.length - 1; i >= 0; i--) {
  15710. var c = sp._extUIChild[i];
  15711. if (!c.destroyed && c._mouseState > 1 && c._visible) {
  15712. if (this.check(c, mouseX, mouseY, callBack))
  15713. return true;
  15714. }
  15715. }
  15716. }
  15717. var isHit = (sp.hitTestPrior && !sp.mouseThrough && !this.disableMouseEvent) ? true : this.hitTest(sp, mouseX, mouseY);
  15718. if (isHit) {
  15719. this._target = sp;
  15720. callBack.call(this, sp);
  15721. if (this._target == this._hitCaputreSp) {
  15722. this._hitCaputreSp = true;
  15723. }
  15724. }
  15725. else if (callBack === this.onMouseUp && sp === this._stage) {
  15726. this._target = this._stage;
  15727. callBack.call(this, this._target);
  15728. }
  15729. return isHit;
  15730. }
  15731. hitTest(sp, mouseX, mouseY) {
  15732. var isHit = false;
  15733. if (sp.scrollRect) {
  15734. mouseX -= sp._style.scrollRect.x;
  15735. mouseY -= sp._style.scrollRect.y;
  15736. }
  15737. var hitArea = sp._style.hitArea;
  15738. if (hitArea && hitArea._hit) {
  15739. return hitArea.contains(mouseX, mouseY);
  15740. }
  15741. if (sp.width > 0 && sp.height > 0 || sp.mouseThrough || hitArea) {
  15742. if (!sp.mouseThrough) {
  15743. isHit = (hitArea ? hitArea : this._rect.setTo(0, 0, sp.width, sp.height)).contains(mouseX, mouseY);
  15744. }
  15745. else {
  15746. isHit = sp.getGraphicBounds().contains(mouseX, mouseY);
  15747. }
  15748. }
  15749. return isHit;
  15750. }
  15751. _checkAllBaseUI(mousex, mousey, callback) {
  15752. var ret = this.handleExclusiveCapture(this.mouseX, this.mouseY, callback);
  15753. if (ret)
  15754. return true;
  15755. ret = this.check(this._stage, this.mouseX, this.mouseY, callback);
  15756. return this.handleCapture(this.mouseX, this.mouseY, callback) || ret;
  15757. }
  15758. check3DUI(mousex, mousey, callback) {
  15759. var uis = this._stage._3dUI;
  15760. var i = 0;
  15761. var ret = false;
  15762. for (; i < uis.length; i++) {
  15763. var curui = uis[i];
  15764. this._stage._curUIBase = curui;
  15765. if (!curui.destroyed && curui._mouseState > 1 && curui._visible) {
  15766. ret = ret || this.check(curui, this.mouseX, this.mouseY, callback);
  15767. }
  15768. }
  15769. this._stage._curUIBase = this._stage;
  15770. return ret;
  15771. }
  15772. handleExclusiveCapture(mousex, mousey, callback) {
  15773. if (this._captureExlusiveMode && this._captureSp && this._captureChain.length > 0) {
  15774. var cursp;
  15775. this._point.setTo(mousex, mousey);
  15776. for (var i = 0; i < this._captureChain.length; i++) {
  15777. cursp = this._captureChain[i];
  15778. cursp.fromParentPoint(this._point);
  15779. }
  15780. this._target = cursp;
  15781. callback.call(this, cursp);
  15782. return true;
  15783. }
  15784. return false;
  15785. }
  15786. handleCapture(mousex, mousey, callback) {
  15787. if (!this._hitCaputreSp && this._captureSp && this._captureChain.length > 0) {
  15788. var cursp;
  15789. this._point.setTo(mousex, mousey);
  15790. for (var i = 0; i < this._captureChain.length; i++) {
  15791. cursp = this._captureChain[i];
  15792. cursp.fromParentPoint(this._point);
  15793. }
  15794. this._target = cursp;
  15795. callback.call(this, cursp);
  15796. return true;
  15797. }
  15798. return false;
  15799. }
  15800. runEvent(evt) {
  15801. var i, n, touch;
  15802. if (evt.type !== 'mousemove')
  15803. this._prePoint.x = this._prePoint.y = -1000000;
  15804. switch (evt.type) {
  15805. case 'mousedown':
  15806. this._touchIDs[0] = this._id++;
  15807. if (!MouseManager._isTouchRespond) {
  15808. this._isLeftMouse = evt.button === 0;
  15809. this.initEvent(evt);
  15810. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseDown);
  15811. }
  15812. else
  15813. MouseManager._isTouchRespond = false;
  15814. break;
  15815. case 'mouseup':
  15816. this._isLeftMouse = evt.button === 0;
  15817. this.initEvent(evt);
  15818. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseUp);
  15819. break;
  15820. case 'mousemove':
  15821. if ((Math.abs(this._prePoint.x - evt.clientX) + Math.abs(this._prePoint.y - evt.clientY)) >= this.mouseMoveAccuracy) {
  15822. this._prePoint.x = evt.clientX;
  15823. this._prePoint.y = evt.clientY;
  15824. this.initEvent(evt);
  15825. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseMove);
  15826. }
  15827. break;
  15828. case "touchstart":
  15829. MouseManager._isTouchRespond = true;
  15830. this._isLeftMouse = true;
  15831. var touches = evt.changedTouches;
  15832. for (i = 0, n = touches.length; i < n; i++) {
  15833. touch = touches[i];
  15834. if (MouseManager.multiTouchEnabled || isNaN(this._curTouchID)) {
  15835. this._curTouchID = touch.identifier;
  15836. if (this._id % 200 === 0)
  15837. this._touchIDs = {};
  15838. this._touchIDs[touch.identifier] = this._id++;
  15839. this.initEvent(touch, evt);
  15840. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseDown);
  15841. }
  15842. }
  15843. break;
  15844. case "touchend":
  15845. case "touchcancel":
  15846. MouseManager._isTouchRespond = true;
  15847. this._isLeftMouse = true;
  15848. var touchends = evt.changedTouches;
  15849. for (i = 0, n = touchends.length; i < n; i++) {
  15850. touch = touchends[i];
  15851. if (MouseManager.multiTouchEnabled || touch.identifier == this._curTouchID) {
  15852. this._curTouchID = NaN;
  15853. this.initEvent(touch, evt);
  15854. var isChecked;
  15855. isChecked = this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseUp);
  15856. if (!isChecked) {
  15857. this.onMouseUp(null);
  15858. }
  15859. }
  15860. }
  15861. break;
  15862. case "touchmove":
  15863. var touchemoves = evt.changedTouches;
  15864. for (i = 0, n = touchemoves.length; i < n; i++) {
  15865. touch = touchemoves[i];
  15866. if (MouseManager.multiTouchEnabled || touch.identifier == this._curTouchID) {
  15867. this.initEvent(touch, evt);
  15868. this._checkAllBaseUI(this.mouseX, this.mouseY, this.onMouseMove);
  15869. }
  15870. }
  15871. break;
  15872. case "wheel":
  15873. case "mousewheel":
  15874. case "DOMMouseScroll":
  15875. this.checkMouseWheel(evt);
  15876. break;
  15877. case "mouseout":
  15878. TouchManager.I.stageMouseOut();
  15879. break;
  15880. case "mouseover":
  15881. this._stage.event(Event.MOUSE_OVER, this._event.setTo(Event.MOUSE_OVER, this._stage, this._stage));
  15882. break;
  15883. }
  15884. }
  15885. setCapture(sp, exclusive = false) {
  15886. this._captureSp = sp;
  15887. this._captureExlusiveMode = exclusive;
  15888. this._captureChain.length = 0;
  15889. this._captureChain.push(sp);
  15890. var cursp = sp;
  15891. while (true) {
  15892. if (cursp == ILaya.stage)
  15893. break;
  15894. if (cursp == ILaya.stage._curUIBase)
  15895. break;
  15896. cursp = cursp.parent;
  15897. if (!cursp)
  15898. break;
  15899. this._captureChain.splice(0, 0, cursp);
  15900. }
  15901. }
  15902. releaseCapture() {
  15903. console.log('release capture');
  15904. this._captureSp = null;
  15905. }
  15906. }
  15907. MouseManager.instance = new MouseManager();
  15908. MouseManager.enabled = true;
  15909. MouseManager.multiTouchEnabled = true;
  15910. MouseManager._isFirstTouch = true;
  15911. class CallLater {
  15912. constructor() {
  15913. this._pool = [];
  15914. this._map = {};
  15915. this._laters = [];
  15916. }
  15917. _update() {
  15918. let laters = this._laters;
  15919. let len = laters.length;
  15920. if (len > 0) {
  15921. for (let i = 0, n = len - 1; i <= n; i++) {
  15922. let handler = laters[i];
  15923. this._map[handler.key] = null;
  15924. if (handler.method !== null) {
  15925. handler.run();
  15926. handler.clear();
  15927. }
  15928. this._pool.push(handler);
  15929. i === n && (n = laters.length - 1);
  15930. }
  15931. laters.length = 0;
  15932. }
  15933. }
  15934. _getHandler(caller, method) {
  15935. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  15936. var mid = method.$_TID || (method.$_TID = (ILaya.Timer._mid++));
  15937. return this._map[cid + '.' + mid];
  15938. }
  15939. callLater(caller, method, args = null) {
  15940. if (this._getHandler(caller, method) == null) {
  15941. let handler;
  15942. if (this._pool.length)
  15943. handler = this._pool.pop();
  15944. else
  15945. handler = new LaterHandler();
  15946. handler.caller = caller;
  15947. handler.method = method;
  15948. handler.args = args;
  15949. var cid = caller ? caller.$_GID : 0;
  15950. var mid = method["$_TID"];
  15951. handler.key = cid + '.' + mid;
  15952. this._map[handler.key] = handler;
  15953. this._laters.push(handler);
  15954. }
  15955. }
  15956. runCallLater(caller, method) {
  15957. var handler = this._getHandler(caller, method);
  15958. if (handler && handler.method != null) {
  15959. this._map[handler.key] = null;
  15960. handler.run();
  15961. handler.clear();
  15962. }
  15963. }
  15964. }
  15965. CallLater.I = new CallLater();
  15966. class LaterHandler {
  15967. clear() {
  15968. this.caller = null;
  15969. this.method = null;
  15970. this.args = null;
  15971. }
  15972. run() {
  15973. var caller = this.caller;
  15974. if (caller && caller.destroyed)
  15975. return this.clear();
  15976. var method = this.method;
  15977. var args = this.args;
  15978. if (method == null)
  15979. return;
  15980. args ? method.apply(caller, args) : method.call(caller);
  15981. }
  15982. }
  15983. class RunDriver {
  15984. }
  15985. RunDriver.createShaderCondition = function (conditionScript) {
  15986. var fn = "(function() {return " + conditionScript + ";})";
  15987. return window.Laya._runScript(fn);
  15988. };
  15989. RunDriver.changeWebGLSize = function (w, h) {
  15990. WebGL.onStageResize(w, h);
  15991. };
  15992. class Stage extends Sprite {
  15993. constructor() {
  15994. super();
  15995. this.offset = new Point();
  15996. this._frameRate = "fast";
  15997. this.designWidth = 0;
  15998. this.designHeight = 0;
  15999. this.canvasRotation = false;
  16000. this.canvasDegree = 0;
  16001. this.renderingEnabled = true;
  16002. this.screenAdaptationEnabled = true;
  16003. this._canvasTransform = new Matrix();
  16004. this._screenMode = "none";
  16005. this._scaleMode = "noscale";
  16006. this._alignV = "top";
  16007. this._alignH = "left";
  16008. this._bgColor = "black";
  16009. this._mouseMoveTime = 0;
  16010. this._renderCount = 0;
  16011. this._safariOffsetY = 0;
  16012. this._frameStartTime = 0;
  16013. this._previousOrientation = Browser.window.orientation;
  16014. this._wgColor = [0, 0, 0, 1];
  16015. this._scene3Ds = [];
  16016. this._globalRepaintSet = false;
  16017. this._globalRepaintGet = false;
  16018. this._3dUI = [];
  16019. this._curUIBase = null;
  16020. this.useRetinalCanvas = false;
  16021. super.set_transform(this._createTransform());
  16022. this.mouseEnabled = true;
  16023. this.hitTestPrior = true;
  16024. this.autoSize = false;
  16025. this._setBit(Const.DISPLAYED_INSTAGE, true);
  16026. this._setBit(Const.ACTIVE_INHIERARCHY, true);
  16027. this._isFocused = true;
  16028. this._isVisibility = true;
  16029. this.useRetinalCanvas = Config.useRetinalCanvas;
  16030. var window = Browser.window;
  16031. window.addEventListener("focus", () => {
  16032. this._isFocused = true;
  16033. this.event(Event.FOCUS);
  16034. this.event(Event.FOCUS_CHANGE);
  16035. });
  16036. window.addEventListener("blur", () => {
  16037. this._isFocused = false;
  16038. this.event(Event.BLUR);
  16039. this.event(Event.FOCUS_CHANGE);
  16040. if (this._isInputting())
  16041. Input["inputElement"].target.focus = false;
  16042. });
  16043. var state = "visibilityState", visibilityChange = "visibilitychange";
  16044. var document = window.document;
  16045. if (typeof document.hidden !== "undefined") {
  16046. visibilityChange = "visibilitychange";
  16047. state = "visibilityState";
  16048. }
  16049. else if (typeof document.mozHidden !== "undefined") {
  16050. visibilityChange = "mozvisibilitychange";
  16051. state = "mozVisibilityState";
  16052. }
  16053. else if (typeof document.msHidden !== "undefined") {
  16054. visibilityChange = "msvisibilitychange";
  16055. state = "msVisibilityState";
  16056. }
  16057. else if (typeof document.webkitHidden !== "undefined") {
  16058. visibilityChange = "webkitvisibilitychange";
  16059. state = "webkitVisibilityState";
  16060. }
  16061. window.document.addEventListener(visibilityChange, () => {
  16062. if (Browser.document[state] == "hidden") {
  16063. this._isVisibility = false;
  16064. if (this._isInputting())
  16065. Input["inputElement"].target.focus = false;
  16066. }
  16067. else {
  16068. this._isVisibility = true;
  16069. }
  16070. this.renderingEnabled = this._isVisibility;
  16071. this.event(Event.VISIBILITY_CHANGE);
  16072. });
  16073. window.addEventListener("resize", () => {
  16074. var orientation = Browser.window.orientation;
  16075. if (orientation != null && orientation != this._previousOrientation && this._isInputting()) {
  16076. Input["inputElement"].target.focus = false;
  16077. }
  16078. this._previousOrientation = orientation;
  16079. if (this._isInputting())
  16080. return;
  16081. if (Browser.onSafari)
  16082. this._safariOffsetY = (Browser.window.__innerHeight || Browser.document.body.clientHeight || Browser.document.documentElement.clientHeight) - Browser.window.innerHeight;
  16083. this._resetCanvas();
  16084. });
  16085. window.addEventListener("orientationchange", (e) => {
  16086. this._resetCanvas();
  16087. });
  16088. this.on(Event.MOUSE_MOVE, this, this._onmouseMove);
  16089. if (Browser.onMobile)
  16090. this.on(Event.MOUSE_DOWN, this, this._onmouseMove);
  16091. }
  16092. _isInputting() {
  16093. return (Browser.onMobile && Input.isInputting);
  16094. }
  16095. set width(value) {
  16096. this.designWidth = value;
  16097. super.set_width(value);
  16098. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16099. }
  16100. get width() {
  16101. return super.get_width();
  16102. }
  16103. set height(value) {
  16104. this.designHeight = value;
  16105. super.set_height(value);
  16106. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16107. }
  16108. get height() {
  16109. return super.get_height();
  16110. }
  16111. set transform(value) {
  16112. super.set_transform(value);
  16113. }
  16114. get transform() {
  16115. if (this._tfChanged)
  16116. this._adjustTransform();
  16117. return (this._transform = this._transform || this._createTransform());
  16118. }
  16119. get isFocused() {
  16120. return this._isFocused;
  16121. }
  16122. get isVisibility() {
  16123. return this._isVisibility;
  16124. }
  16125. _changeCanvasSize() {
  16126. this.setScreenSize(Browser.clientWidth * Browser.pixelRatio, Browser.clientHeight * Browser.pixelRatio);
  16127. }
  16128. _resetCanvas() {
  16129. if (!this.screenAdaptationEnabled)
  16130. return;
  16131. this._changeCanvasSize();
  16132. }
  16133. setScreenSize(screenWidth, screenHeight) {
  16134. var rotation = false;
  16135. if (this._screenMode !== Stage.SCREEN_NONE) {
  16136. var screenType = screenWidth / screenHeight < 1 ? Stage.SCREEN_VERTICAL : Stage.SCREEN_HORIZONTAL;
  16137. rotation = screenType !== this._screenMode;
  16138. if (rotation) {
  16139. var temp = screenHeight;
  16140. screenHeight = screenWidth;
  16141. screenWidth = temp;
  16142. }
  16143. }
  16144. this.canvasRotation = rotation;
  16145. var canvas = Render._mainCanvas;
  16146. var canvasStyle = canvas.source.style;
  16147. var mat = this._canvasTransform.identity();
  16148. var scaleMode = this._scaleMode;
  16149. var scaleX = screenWidth / this.designWidth;
  16150. var scaleY = screenHeight / this.designHeight;
  16151. var canvasWidth = this.useRetinalCanvas ? screenWidth : this.designWidth;
  16152. var canvasHeight = this.useRetinalCanvas ? screenHeight : this.designHeight;
  16153. var realWidth = screenWidth;
  16154. var realHeight = screenHeight;
  16155. var pixelRatio = Browser.pixelRatio;
  16156. this._width = this.designWidth;
  16157. this._height = this.designHeight;
  16158. switch (scaleMode) {
  16159. case Stage.SCALE_NOSCALE:
  16160. scaleX = scaleY = 1;
  16161. realWidth = this.designWidth;
  16162. realHeight = this.designHeight;
  16163. break;
  16164. case Stage.SCALE_SHOWALL:
  16165. scaleX = scaleY = Math.min(scaleX, scaleY);
  16166. canvasWidth = realWidth = Math.round(this.designWidth * scaleX);
  16167. canvasHeight = realHeight = Math.round(this.designHeight * scaleY);
  16168. break;
  16169. case Stage.SCALE_NOBORDER:
  16170. scaleX = scaleY = Math.max(scaleX, scaleY);
  16171. realWidth = Math.round(this.designWidth * scaleX);
  16172. realHeight = Math.round(this.designHeight * scaleY);
  16173. break;
  16174. case Stage.SCALE_FULL:
  16175. scaleX = scaleY = 1;
  16176. this._width = canvasWidth = screenWidth;
  16177. this._height = canvasHeight = screenHeight;
  16178. break;
  16179. case Stage.SCALE_FIXED_WIDTH:
  16180. scaleY = scaleX;
  16181. this._height = canvasHeight = Math.round(screenHeight / scaleX);
  16182. break;
  16183. case Stage.SCALE_FIXED_HEIGHT:
  16184. scaleX = scaleY;
  16185. this._width = canvasWidth = Math.round(screenWidth / scaleY);
  16186. break;
  16187. case Stage.SCALE_FIXED_AUTO:
  16188. if ((screenWidth / screenHeight) < (this.designWidth / this.designHeight)) {
  16189. scaleY = scaleX;
  16190. this._height = canvasHeight = Math.round(screenHeight / scaleX);
  16191. }
  16192. else {
  16193. scaleX = scaleY;
  16194. this._width = canvasWidth = Math.round(screenWidth / scaleY);
  16195. }
  16196. break;
  16197. }
  16198. if (this.useRetinalCanvas) {
  16199. canvasWidth = screenWidth;
  16200. canvasHeight = screenHeight;
  16201. }
  16202. scaleX *= this.scaleX;
  16203. scaleY *= this.scaleY;
  16204. if (scaleX === 1 && scaleY === 1) {
  16205. this.transform.identity();
  16206. }
  16207. else {
  16208. this.transform.a = this._formatData(scaleX / (realWidth / canvasWidth));
  16209. this.transform.d = this._formatData(scaleY / (realHeight / canvasHeight));
  16210. }
  16211. canvas.size(canvasWidth, canvasHeight);
  16212. RunDriver.changeWebGLSize(canvasWidth, canvasHeight);
  16213. mat.scale(realWidth / canvasWidth / pixelRatio, realHeight / canvasHeight / pixelRatio);
  16214. if (this._alignH === Stage.ALIGN_LEFT)
  16215. this.offset.x = 0;
  16216. else if (this._alignH === Stage.ALIGN_RIGHT)
  16217. this.offset.x = screenWidth - realWidth;
  16218. else
  16219. this.offset.x = (screenWidth - realWidth) * 0.5 / pixelRatio;
  16220. if (this._alignV === Stage.ALIGN_TOP)
  16221. this.offset.y = 0;
  16222. else if (this._alignV === Stage.ALIGN_BOTTOM)
  16223. this.offset.y = screenHeight - realHeight;
  16224. else
  16225. this.offset.y = (screenHeight - realHeight) * 0.5 / pixelRatio;
  16226. this.offset.x = Math.round(this.offset.x);
  16227. this.offset.y = Math.round(this.offset.y);
  16228. mat.translate(this.offset.x, this.offset.y);
  16229. if (this._safariOffsetY)
  16230. mat.translate(0, this._safariOffsetY);
  16231. this.canvasDegree = 0;
  16232. if (rotation) {
  16233. if (this._screenMode === Stage.SCREEN_HORIZONTAL) {
  16234. mat.rotate(Math.PI / 2);
  16235. mat.translate(screenHeight / pixelRatio, 0);
  16236. this.canvasDegree = 90;
  16237. }
  16238. else {
  16239. mat.rotate(-Math.PI / 2);
  16240. mat.translate(0, screenWidth / pixelRatio);
  16241. this.canvasDegree = -90;
  16242. }
  16243. }
  16244. mat.a = this._formatData(mat.a);
  16245. mat.d = this._formatData(mat.d);
  16246. mat.tx = this._formatData(mat.tx);
  16247. mat.ty = this._formatData(mat.ty);
  16248. super.set_transform(this.transform);
  16249. canvasStyle.transformOrigin = canvasStyle.webkitTransformOrigin = canvasStyle.msTransformOrigin = canvasStyle.mozTransformOrigin = canvasStyle.oTransformOrigin = "0px 0px 0px";
  16250. canvasStyle.transform = canvasStyle.webkitTransform = canvasStyle.msTransform = canvasStyle.mozTransform = canvasStyle.oTransform = "matrix(" + mat.toString() + ")";
  16251. if (this._safariOffsetY)
  16252. mat.translate(0, -this._safariOffsetY);
  16253. mat.translate(parseInt(canvasStyle.left) || 0, parseInt(canvasStyle.top) || 0);
  16254. this.visible = true;
  16255. this._repaint |= SpriteConst.REPAINT_CACHE;
  16256. this.event(Event.RESIZE);
  16257. }
  16258. _formatData(value) {
  16259. if (Math.abs(value) < 0.000001)
  16260. return 0;
  16261. if (Math.abs(1 - value) < 0.001)
  16262. return value > 0 ? 1 : -1;
  16263. return value;
  16264. }
  16265. get scaleMode() {
  16266. return this._scaleMode;
  16267. }
  16268. set scaleMode(value) {
  16269. this._scaleMode = value;
  16270. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16271. }
  16272. get alignH() {
  16273. return this._alignH;
  16274. }
  16275. set alignH(value) {
  16276. this._alignH = value;
  16277. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16278. }
  16279. get alignV() {
  16280. return this._alignV;
  16281. }
  16282. set alignV(value) {
  16283. this._alignV = value;
  16284. ILaya.systemTimer.callLater(this, this._changeCanvasSize);
  16285. }
  16286. get bgColor() {
  16287. return this._bgColor;
  16288. }
  16289. set bgColor(value) {
  16290. this._bgColor = value;
  16291. if (value)
  16292. this._wgColor = ColorUtils.create(value).arrColor;
  16293. else
  16294. this._wgColor = null;
  16295. if (value) {
  16296. Render.canvas.style.background = value;
  16297. }
  16298. else {
  16299. Render.canvas.style.background = "none";
  16300. }
  16301. }
  16302. get mouseX() {
  16303. return Math.round(MouseManager.instance.mouseX / this.clientScaleX);
  16304. }
  16305. get mouseY() {
  16306. return Math.round(MouseManager.instance.mouseY / this.clientScaleY);
  16307. }
  16308. getMousePoint() {
  16309. return Point.TEMP.setTo(this.mouseX, this.mouseY);
  16310. }
  16311. get clientScaleX() {
  16312. return this._transform ? this._transform.getScaleX() : 1;
  16313. }
  16314. get clientScaleY() {
  16315. return this._transform ? this._transform.getScaleY() : 1;
  16316. }
  16317. get screenMode() {
  16318. return this._screenMode;
  16319. }
  16320. set screenMode(value) {
  16321. this._screenMode = value;
  16322. }
  16323. repaint(type = SpriteConst.REPAINT_CACHE) {
  16324. this._repaint |= type;
  16325. }
  16326. parentRepaint(type = SpriteConst.REPAINT_CACHE) {
  16327. }
  16328. _loop() {
  16329. this._globalRepaintGet = this._globalRepaintSet;
  16330. this._globalRepaintSet = false;
  16331. this.render(Render._context, 0, 0);
  16332. return true;
  16333. }
  16334. getFrameTm() {
  16335. return this._frameStartTime;
  16336. }
  16337. _onmouseMove(e) {
  16338. this._mouseMoveTime = Browser.now();
  16339. }
  16340. getTimeFromFrameStart() {
  16341. return Browser.now() - this._frameStartTime;
  16342. }
  16343. set visible(value) {
  16344. if (this.visible !== value) {
  16345. super.set_visible(value);
  16346. var style = Render._mainCanvas.source.style;
  16347. style.visibility = value ? "visible" : "hidden";
  16348. }
  16349. }
  16350. get visible() {
  16351. return super.visible;
  16352. }
  16353. render(context, x, y) {
  16354. if (window.conch) {
  16355. this.renderToNative(context, x, y);
  16356. return;
  16357. }
  16358. Stage._dbgSprite.graphics.clear();
  16359. if (this._frameRate === Stage.FRAME_SLEEP) {
  16360. var now = Browser.now();
  16361. if (now - this._frameStartTime >= 1000)
  16362. this._frameStartTime = now;
  16363. else
  16364. return;
  16365. }
  16366. else {
  16367. if (!this._visible) {
  16368. this._renderCount++;
  16369. if (this._renderCount % 5 === 0) {
  16370. CallLater.I._update();
  16371. Stat.loopCount++;
  16372. RenderInfo.loopCount = Stat.loopCount;
  16373. this._updateTimers();
  16374. }
  16375. return;
  16376. }
  16377. this._frameStartTime = Browser.now();
  16378. RenderInfo.loopStTm = this._frameStartTime;
  16379. }
  16380. this._renderCount++;
  16381. var frameMode = this._frameRate === Stage.FRAME_MOUSE ? (((this._frameStartTime - this._mouseMoveTime) < 2000) ? Stage.FRAME_FAST : Stage.FRAME_SLOW) : this._frameRate;
  16382. var isFastMode = (frameMode !== Stage.FRAME_SLOW);
  16383. var isDoubleLoop = (this._renderCount % 2 === 0);
  16384. Stat.renderSlow = !isFastMode;
  16385. if (!isFastMode && !isDoubleLoop)
  16386. return;
  16387. CallLater.I._update();
  16388. Stat.loopCount++;
  16389. RenderInfo.loopCount = Stat.loopCount;
  16390. if (this.renderingEnabled) {
  16391. for (var i = 0, n = this._scene3Ds.length; i < n; i++)
  16392. this._scene3Ds[i]._update();
  16393. context.clear();
  16394. super.render(context, x, y);
  16395. Stat._StatRender.renderNotCanvas(context, x, y);
  16396. }
  16397. Stage._dbgSprite.render(context, 0, 0);
  16398. if (this.renderingEnabled) {
  16399. Stage.clear(this._bgColor);
  16400. context.flush();
  16401. VectorGraphManager.instance && VectorGraphManager.getInstance().endDispose();
  16402. }
  16403. this._updateTimers();
  16404. }
  16405. renderToNative(context, x, y) {
  16406. this._renderCount++;
  16407. if (!this._visible) {
  16408. if (this._renderCount % 5 === 0) {
  16409. CallLater.I._update();
  16410. Stat.loopCount++;
  16411. RenderInfo.loopCount = Stat.loopCount;
  16412. this._updateTimers();
  16413. }
  16414. return;
  16415. }
  16416. CallLater.I._update();
  16417. Stat.loopCount++;
  16418. RenderInfo.loopCount = Stat.loopCount;
  16419. if (this.renderingEnabled) {
  16420. for (var i = 0, n = this._scene3Ds.length; i < n; i++)
  16421. this._scene3Ds[i]._update();
  16422. context.clear();
  16423. super.render(context, x, y);
  16424. Stat._StatRender.renderNotCanvas(context, x, y);
  16425. }
  16426. if (this.renderingEnabled) {
  16427. Stage.clear(this._bgColor);
  16428. context.flush();
  16429. VectorGraphManager.instance && VectorGraphManager.getInstance().endDispose();
  16430. }
  16431. this._updateTimers();
  16432. }
  16433. _updateTimers() {
  16434. ILaya.systemTimer._update();
  16435. ILaya.startTimer._update();
  16436. ILaya.physicsTimer._update();
  16437. ILaya.updateTimer._update();
  16438. ILaya.lateTimer._update();
  16439. ILaya.timer._update();
  16440. }
  16441. set fullScreenEnabled(value) {
  16442. var document = Browser.document;
  16443. var canvas = Render.canvas;
  16444. if (value) {
  16445. canvas.addEventListener('mousedown', this._requestFullscreen);
  16446. canvas.addEventListener('touchstart', this._requestFullscreen);
  16447. document.addEventListener("fullscreenchange", this._fullScreenChanged);
  16448. document.addEventListener("mozfullscreenchange", this._fullScreenChanged);
  16449. document.addEventListener("webkitfullscreenchange", this._fullScreenChanged);
  16450. document.addEventListener("msfullscreenchange", this._fullScreenChanged);
  16451. }
  16452. else {
  16453. canvas.removeEventListener('mousedown', this._requestFullscreen);
  16454. canvas.removeEventListener('touchstart', this._requestFullscreen);
  16455. document.removeEventListener("fullscreenchange", this._fullScreenChanged);
  16456. document.removeEventListener("mozfullscreenchange", this._fullScreenChanged);
  16457. document.removeEventListener("webkitfullscreenchange", this._fullScreenChanged);
  16458. document.removeEventListener("msfullscreenchange", this._fullScreenChanged);
  16459. }
  16460. }
  16461. get frameRate() {
  16462. if (!ILaya.Render.isConchApp) {
  16463. return this._frameRate;
  16464. }
  16465. else {
  16466. return this._frameRateNative;
  16467. }
  16468. }
  16469. set frameRate(value) {
  16470. if (!ILaya.Render.isConchApp) {
  16471. this._frameRate = value;
  16472. }
  16473. else {
  16474. var c = window.conch;
  16475. switch (value) {
  16476. case Stage.FRAME_FAST:
  16477. c.config.setLimitFPS(60);
  16478. break;
  16479. case Stage.FRAME_MOUSE:
  16480. c.config.setMouseFrame(2000);
  16481. break;
  16482. case Stage.FRAME_SLOW:
  16483. c.config.setSlowFrame(true);
  16484. break;
  16485. case Stage.FRAME_SLEEP:
  16486. c.config.setLimitFPS(1);
  16487. break;
  16488. }
  16489. this._frameRateNative = value;
  16490. }
  16491. }
  16492. _requestFullscreen() {
  16493. var element = Browser.document.documentElement;
  16494. if (element.requestFullscreen) {
  16495. element.requestFullscreen();
  16496. }
  16497. else if (element.mozRequestFullScreen) {
  16498. element.mozRequestFullScreen();
  16499. }
  16500. else if (element.webkitRequestFullscreen) {
  16501. element.webkitRequestFullscreen();
  16502. }
  16503. else if (element.msRequestFullscreen) {
  16504. element.msRequestFullscreen();
  16505. }
  16506. }
  16507. _fullScreenChanged() {
  16508. ILaya.stage.event(Event.FULL_SCREEN_CHANGE);
  16509. }
  16510. exitFullscreen() {
  16511. var document = Browser.document;
  16512. if (document.exitFullscreen) {
  16513. document.exitFullscreen();
  16514. }
  16515. else if (document.mozCancelFullScreen) {
  16516. document.mozCancelFullScreen();
  16517. }
  16518. else if (document.webkitExitFullscreen) {
  16519. document.webkitExitFullscreen();
  16520. }
  16521. }
  16522. isGlobalRepaint() {
  16523. return this._globalRepaintGet;
  16524. }
  16525. setGlobalRepaint() {
  16526. this._globalRepaintSet = true;
  16527. }
  16528. add3DUI(uibase) {
  16529. var uiroot = uibase.rootView;
  16530. if (this._3dUI.indexOf(uiroot) >= 0)
  16531. return;
  16532. this._3dUI.push(uiroot);
  16533. }
  16534. remove3DUI(uibase) {
  16535. var uiroot = uibase.rootView;
  16536. var p = this._3dUI.indexOf(uiroot);
  16537. if (p >= 0) {
  16538. this._3dUI.splice(p, 1);
  16539. return true;
  16540. }
  16541. return false;
  16542. }
  16543. }
  16544. Stage.SCALE_NOSCALE = "noscale";
  16545. Stage.SCALE_EXACTFIT = "exactfit";
  16546. Stage.SCALE_SHOWALL = "showall";
  16547. Stage.SCALE_NOBORDER = "noborder";
  16548. Stage.SCALE_FULL = "full";
  16549. Stage.SCALE_FIXED_WIDTH = "fixedwidth";
  16550. Stage.SCALE_FIXED_HEIGHT = "fixedheight";
  16551. Stage.SCALE_FIXED_AUTO = "fixedauto";
  16552. Stage.ALIGN_LEFT = "left";
  16553. Stage.ALIGN_RIGHT = "right";
  16554. Stage.ALIGN_CENTER = "center";
  16555. Stage.ALIGN_TOP = "top";
  16556. Stage.ALIGN_MIDDLE = "middle";
  16557. Stage.ALIGN_BOTTOM = "bottom";
  16558. Stage.SCREEN_NONE = "none";
  16559. Stage.SCREEN_HORIZONTAL = "horizontal";
  16560. Stage.SCREEN_VERTICAL = "vertical";
  16561. Stage.FRAME_FAST = "fast";
  16562. Stage.FRAME_SLOW = "slow";
  16563. Stage.FRAME_MOUSE = "mouse";
  16564. Stage.FRAME_SLEEP = "sleep";
  16565. Stage._dbgSprite = new Sprite();
  16566. Stage.clear = function (value) {
  16567. Context.set2DRenderConfig();
  16568. var gl = LayaGL.instance;
  16569. RenderState2D.worldScissorTest && gl.disable(gl.SCISSOR_TEST);
  16570. var ctx = Render.context;
  16571. var c = (ctx._submits._length == 0 || Config.preserveDrawingBuffer) ? ColorUtils.create(value).arrColor : ILaya.stage._wgColor;
  16572. if (c)
  16573. ctx.clearBG(c[0], c[1], c[2], c[3]);
  16574. else
  16575. ctx.clearBG(0, 0, 0, 0);
  16576. RenderState2D.clear();
  16577. };
  16578. ClassUtils.regClass("laya.display.Stage", Stage);
  16579. ClassUtils.regClass("Laya.Stage", Stage);
  16580. class KeyBoardManager {
  16581. static __init__() {
  16582. KeyBoardManager._addEvent("keydown");
  16583. KeyBoardManager._addEvent("keypress");
  16584. KeyBoardManager._addEvent("keyup");
  16585. }
  16586. static _addEvent(type) {
  16587. ILaya.Browser.document.addEventListener(type, function (e) {
  16588. KeyBoardManager._dispatch(e, type);
  16589. }, true);
  16590. }
  16591. static _dispatch(e, type) {
  16592. if (!KeyBoardManager.enabled)
  16593. return;
  16594. KeyBoardManager._event._stoped = false;
  16595. KeyBoardManager._event.nativeEvent = e;
  16596. KeyBoardManager._event.keyCode = e.keyCode || e.which || e.charCode;
  16597. if (type === "keydown")
  16598. KeyBoardManager._pressKeys[KeyBoardManager._event.keyCode] = true;
  16599. else if (type === "keyup")
  16600. KeyBoardManager._pressKeys[KeyBoardManager._event.keyCode] = null;
  16601. var target = (ILaya.stage.focus && (ILaya.stage.focus.event != null) && ILaya.stage.focus.displayedInStage) ? ILaya.stage.focus : ILaya.stage;
  16602. var ct = target;
  16603. while (ct) {
  16604. ct.event(type, KeyBoardManager._event.setTo(type, ct, target));
  16605. ct = ct.parent;
  16606. }
  16607. }
  16608. static hasKeyDown(key) {
  16609. return KeyBoardManager._pressKeys[key];
  16610. }
  16611. }
  16612. KeyBoardManager._pressKeys = {};
  16613. KeyBoardManager.enabled = true;
  16614. KeyBoardManager._event = new Event();
  16615. class SoundChannel extends EventDispatcher {
  16616. constructor() {
  16617. super(...arguments);
  16618. this.isStopped = false;
  16619. }
  16620. set volume(v) {
  16621. }
  16622. get volume() {
  16623. return 1;
  16624. }
  16625. get position() {
  16626. return 0;
  16627. }
  16628. get duration() {
  16629. return 0;
  16630. }
  16631. play() {
  16632. }
  16633. stop() {
  16634. if (this.completeHandler)
  16635. this.completeHandler.run();
  16636. }
  16637. pause() {
  16638. }
  16639. resume() {
  16640. }
  16641. __runComplete(handler) {
  16642. if (handler) {
  16643. handler.run();
  16644. }
  16645. }
  16646. }
  16647. class AudioSoundChannel extends SoundChannel {
  16648. constructor(audio) {
  16649. super();
  16650. this._audio = null;
  16651. this._onEnd = this.__onEnd.bind(this);
  16652. this._resumePlay = this.__resumePlay.bind(this);
  16653. audio.addEventListener("ended", this._onEnd);
  16654. this._audio = audio;
  16655. }
  16656. __onEnd(evt) {
  16657. if (this.loops == 1) {
  16658. if (this.completeHandler) {
  16659. ILaya.systemTimer.once(10, this, this.__runComplete, [this.completeHandler], false);
  16660. this.completeHandler = null;
  16661. }
  16662. this.stop();
  16663. this.event(Event.COMPLETE);
  16664. return;
  16665. }
  16666. if (this.loops > 0) {
  16667. this.loops--;
  16668. }
  16669. this.startTime = 0;
  16670. this.play();
  16671. }
  16672. __resumePlay() {
  16673. if (this._audio)
  16674. this._audio.removeEventListener("canplay", this._resumePlay);
  16675. if (this.isStopped)
  16676. return;
  16677. try {
  16678. this._audio.currentTime = this.startTime;
  16679. Browser.container.appendChild(this._audio);
  16680. this._audio.play();
  16681. }
  16682. catch (e) {
  16683. this.event(Event.ERROR);
  16684. }
  16685. }
  16686. play() {
  16687. this.isStopped = false;
  16688. try {
  16689. this._audio.playbackRate = ILaya.SoundManager.playbackRate;
  16690. this._audio.currentTime = this.startTime;
  16691. }
  16692. catch (e) {
  16693. this._audio.addEventListener("canplay", this._resumePlay);
  16694. return;
  16695. }
  16696. ILaya.SoundManager.addChannel(this);
  16697. Browser.container.appendChild(this._audio);
  16698. if ("play" in this._audio)
  16699. this._audio.play();
  16700. }
  16701. get position() {
  16702. if (!this._audio)
  16703. return 0;
  16704. return this._audio.currentTime;
  16705. }
  16706. get duration() {
  16707. if (!this._audio)
  16708. return 0;
  16709. return this._audio.duration;
  16710. }
  16711. stop() {
  16712. super.stop();
  16713. this.isStopped = true;
  16714. ILaya.SoundManager.removeChannel(this);
  16715. this.completeHandler = null;
  16716. if (!this._audio)
  16717. return;
  16718. if ("pause" in this._audio)
  16719. if (ILaya.Render.isConchApp) {
  16720. this._audio.stop();
  16721. }
  16722. this._audio.pause();
  16723. this._audio.removeEventListener("ended", this._onEnd);
  16724. this._audio.removeEventListener("canplay", this._resumePlay);
  16725. if (!ILaya.Browser.onIE) {
  16726. if (this._audio != ILaya.AudioSound._musicAudio) {
  16727. ILaya.Pool.recover("audio:" + this.url, this._audio);
  16728. }
  16729. }
  16730. Browser.removeElement(this._audio);
  16731. this._audio = null;
  16732. if (ILaya.SoundManager.autoReleaseSound)
  16733. ILaya.SoundManager.disposeSoundLater(this.url);
  16734. }
  16735. pause() {
  16736. this.isStopped = true;
  16737. ILaya.SoundManager.removeChannel(this);
  16738. if ("pause" in this._audio)
  16739. this._audio.pause();
  16740. if (ILaya.SoundManager.autoReleaseSound)
  16741. ILaya.SoundManager.disposeSoundLater(this.url);
  16742. }
  16743. resume() {
  16744. if (!this._audio)
  16745. return;
  16746. this.isStopped = false;
  16747. ILaya.SoundManager.addChannel(this);
  16748. if ("play" in this._audio)
  16749. this._audio.play();
  16750. }
  16751. set volume(v) {
  16752. if (!this._audio)
  16753. return;
  16754. this._audio.volume = v;
  16755. }
  16756. get volume() {
  16757. if (!this._audio)
  16758. return 1;
  16759. return this._audio.volume;
  16760. }
  16761. }
  16762. class AudioSound extends EventDispatcher {
  16763. constructor() {
  16764. super(...arguments);
  16765. this.loaded = false;
  16766. }
  16767. dispose() {
  16768. var ad = AudioSound._audioCache[this.url];
  16769. Pool.clearBySign("audio:" + this.url);
  16770. if (ad) {
  16771. if (!Render.isConchApp) {
  16772. ad.src = "";
  16773. }
  16774. delete AudioSound._audioCache[this.url];
  16775. }
  16776. }
  16777. static _initMusicAudio() {
  16778. if (AudioSound._musicAudio)
  16779. return;
  16780. if (!AudioSound._musicAudio)
  16781. AudioSound._musicAudio = Browser.createElement("audio");
  16782. if (!Render.isConchApp) {
  16783. Browser.document.addEventListener("mousedown", AudioSound._makeMusicOK);
  16784. }
  16785. }
  16786. static _makeMusicOK() {
  16787. Browser.document.removeEventListener("mousedown", AudioSound._makeMusicOK);
  16788. if (!AudioSound._musicAudio.src) {
  16789. AudioSound._musicAudio.src = "";
  16790. AudioSound._musicAudio.load();
  16791. }
  16792. else {
  16793. AudioSound._musicAudio.play();
  16794. }
  16795. }
  16796. load(url) {
  16797. url = URL.formatURL(url);
  16798. this.url = url;
  16799. var ad;
  16800. if (url == ILaya.SoundManager._bgMusic) {
  16801. AudioSound._initMusicAudio();
  16802. ad = AudioSound._musicAudio;
  16803. if (ad.src != url) {
  16804. AudioSound._audioCache[ad.src] = null;
  16805. ad = null;
  16806. }
  16807. }
  16808. else {
  16809. ad = AudioSound._audioCache[url];
  16810. }
  16811. if (ad && ad.readyState >= 2) {
  16812. this.event(Event.COMPLETE);
  16813. return;
  16814. }
  16815. if (!ad) {
  16816. if (url == ILaya.SoundManager._bgMusic) {
  16817. AudioSound._initMusicAudio();
  16818. ad = AudioSound._musicAudio;
  16819. }
  16820. else {
  16821. ad = Browser.createElement("audio");
  16822. }
  16823. AudioSound._audioCache[url] = ad;
  16824. ad.src = url;
  16825. }
  16826. ad.addEventListener("canplaythrough", onLoaded);
  16827. ad.addEventListener("error", onErr);
  16828. var me = this;
  16829. function onLoaded() {
  16830. offs();
  16831. me.loaded = true;
  16832. me.event(Event.COMPLETE);
  16833. }
  16834. function onErr() {
  16835. ad.load = null;
  16836. offs();
  16837. me.event(Event.ERROR);
  16838. }
  16839. function offs() {
  16840. ad.removeEventListener("canplaythrough", onLoaded);
  16841. ad.removeEventListener("error", onErr);
  16842. }
  16843. this.audio = ad;
  16844. if (ad.load) {
  16845. ad.load();
  16846. }
  16847. else {
  16848. onErr();
  16849. }
  16850. }
  16851. play(startTime = 0, loops = 0) {
  16852. if (!this.url)
  16853. return null;
  16854. var ad;
  16855. if (this.url == ILaya.SoundManager._bgMusic) {
  16856. ad = AudioSound._musicAudio;
  16857. }
  16858. else {
  16859. ad = AudioSound._audioCache[this.url];
  16860. }
  16861. if (!ad)
  16862. return null;
  16863. var tAd;
  16864. tAd = Pool.getItem("audio:" + this.url);
  16865. if (Render.isConchApp) {
  16866. if (!tAd) {
  16867. tAd = Browser.createElement("audio");
  16868. tAd.src = this.url;
  16869. }
  16870. }
  16871. else {
  16872. if (this.url == ILaya.SoundManager._bgMusic) {
  16873. AudioSound._initMusicAudio();
  16874. tAd = AudioSound._musicAudio;
  16875. tAd.src = this.url;
  16876. }
  16877. else {
  16878. tAd = tAd ? tAd : ad.cloneNode(true);
  16879. }
  16880. }
  16881. var channel = new AudioSoundChannel(tAd);
  16882. channel.url = this.url;
  16883. channel.loops = loops;
  16884. channel.startTime = startTime;
  16885. channel.play();
  16886. ILaya.SoundManager.addChannel(channel);
  16887. return channel;
  16888. }
  16889. get duration() {
  16890. var ad;
  16891. ad = AudioSound._audioCache[this.url];
  16892. if (!ad)
  16893. return 0;
  16894. return ad.duration;
  16895. }
  16896. }
  16897. AudioSound._audioCache = {};
  16898. class WebAudioSoundChannel extends SoundChannel {
  16899. constructor() {
  16900. super();
  16901. this.bufferSource = null;
  16902. this._currentTime = 0;
  16903. this._volume = 1;
  16904. this._startTime = 0;
  16905. this._pauseTime = 0;
  16906. this.context = ILaya.WebAudioSound.ctx;
  16907. this._onPlayEnd = Utils.bind(this.__onPlayEnd, this);
  16908. if (this.context["createGain"]) {
  16909. this.gain = this.context["createGain"]();
  16910. }
  16911. else {
  16912. this.gain = this.context["createGainNode"]();
  16913. }
  16914. }
  16915. play() {
  16916. ILaya.SoundManager.addChannel(this);
  16917. this.isStopped = false;
  16918. this._clearBufferSource();
  16919. if (!this.audioBuffer)
  16920. return;
  16921. if (this.startTime >= this.duration)
  16922. return stop();
  16923. var context = this.context;
  16924. var gain = this.gain;
  16925. var bufferSource = context.createBufferSource();
  16926. this.bufferSource = bufferSource;
  16927. bufferSource.buffer = this.audioBuffer;
  16928. bufferSource.connect(gain);
  16929. if (gain)
  16930. gain.disconnect();
  16931. gain.connect(context.destination);
  16932. bufferSource.onended = this._onPlayEnd;
  16933. this._startTime = Browser.now();
  16934. if (this.gain.gain.setTargetAtTime) {
  16935. this.gain.gain.setTargetAtTime(this._volume, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  16936. }
  16937. else
  16938. this.gain.gain.value = this._volume;
  16939. if (this.loops == 0) {
  16940. bufferSource.loop = true;
  16941. }
  16942. if (bufferSource.playbackRate.setTargetAtTime) {
  16943. bufferSource.playbackRate.setTargetAtTime(ILaya.SoundManager.playbackRate, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  16944. }
  16945. else
  16946. bufferSource.playbackRate.value = ILaya.SoundManager.playbackRate;
  16947. bufferSource.start(0, this.startTime);
  16948. this._currentTime = 0;
  16949. }
  16950. __onPlayEnd() {
  16951. if (this.loops == 1) {
  16952. if (this.completeHandler) {
  16953. ILaya.timer.once(10, this, this.__runComplete, [this.completeHandler], false);
  16954. this.completeHandler = null;
  16955. }
  16956. this.stop();
  16957. this.event(Event.COMPLETE);
  16958. return;
  16959. }
  16960. if (this.loops > 0) {
  16961. this.loops--;
  16962. }
  16963. this.startTime = 0;
  16964. this.play();
  16965. }
  16966. get position() {
  16967. if (this.bufferSource) {
  16968. return (Browser.now() - this._startTime) / 1000 + this.startTime;
  16969. }
  16970. return 0;
  16971. }
  16972. get duration() {
  16973. if (this.audioBuffer) {
  16974. return this.audioBuffer.duration;
  16975. }
  16976. return 0;
  16977. }
  16978. _clearBufferSource() {
  16979. if (this.bufferSource) {
  16980. var sourceNode = this.bufferSource;
  16981. if (sourceNode.stop) {
  16982. sourceNode.stop(0);
  16983. }
  16984. else {
  16985. sourceNode.noteOff(0);
  16986. }
  16987. sourceNode.disconnect(0);
  16988. sourceNode.onended = null;
  16989. if (!WebAudioSoundChannel._tryCleanFailed)
  16990. this._tryClearBuffer(sourceNode);
  16991. this.bufferSource = null;
  16992. }
  16993. }
  16994. _tryClearBuffer(sourceNode) {
  16995. if (!Browser.onMac) {
  16996. try {
  16997. sourceNode.buffer = null;
  16998. }
  16999. catch (e) {
  17000. WebAudioSoundChannel._tryCleanFailed = true;
  17001. }
  17002. return;
  17003. }
  17004. try {
  17005. sourceNode.buffer = ILaya.WebAudioSound._miniBuffer;
  17006. }
  17007. catch (e) {
  17008. WebAudioSoundChannel._tryCleanFailed = true;
  17009. }
  17010. }
  17011. stop() {
  17012. super.stop();
  17013. this._clearBufferSource();
  17014. this.audioBuffer = null;
  17015. if (this.gain)
  17016. this.gain.disconnect();
  17017. this.isStopped = true;
  17018. ILaya.SoundManager.removeChannel(this);
  17019. this.completeHandler = null;
  17020. if (ILaya.SoundManager.autoReleaseSound)
  17021. ILaya.SoundManager.disposeSoundLater(this.url);
  17022. }
  17023. pause() {
  17024. if (!this.isStopped) {
  17025. this._pauseTime = this.position;
  17026. }
  17027. this._clearBufferSource();
  17028. if (this.gain)
  17029. this.gain.disconnect();
  17030. this.isStopped = true;
  17031. ILaya.SoundManager.removeChannel(this);
  17032. if (ILaya.SoundManager.autoReleaseSound)
  17033. ILaya.SoundManager.disposeSoundLater(this.url);
  17034. }
  17035. resume() {
  17036. this.startTime = this._pauseTime;
  17037. this.play();
  17038. }
  17039. set volume(v) {
  17040. this._volume = v;
  17041. if (this.isStopped) {
  17042. return;
  17043. }
  17044. if (this.gain.gain.setTargetAtTime) {
  17045. this.gain.gain.setTargetAtTime(v, this.context.currentTime, WebAudioSoundChannel.SetTargetDelay);
  17046. }
  17047. else
  17048. this.gain.gain.value = v;
  17049. }
  17050. get volume() {
  17051. return this._volume;
  17052. }
  17053. }
  17054. WebAudioSoundChannel._tryCleanFailed = false;
  17055. WebAudioSoundChannel.SetTargetDelay = 0.001;
  17056. class WebAudioSound extends EventDispatcher {
  17057. constructor() {
  17058. super(...arguments);
  17059. this.loaded = false;
  17060. this._disposed = false;
  17061. }
  17062. static decode() {
  17063. if (WebAudioSound.buffs.length <= 0 || WebAudioSound.isDecoding) {
  17064. return;
  17065. }
  17066. WebAudioSound.isDecoding = true;
  17067. WebAudioSound.tInfo = WebAudioSound.buffs.shift();
  17068. WebAudioSound.ctx.decodeAudioData(WebAudioSound.tInfo["buffer"], WebAudioSound._done, WebAudioSound._fail);
  17069. }
  17070. static _done(audioBuffer) {
  17071. WebAudioSound.e.event("loaded:" + WebAudioSound.tInfo.url, audioBuffer);
  17072. WebAudioSound.isDecoding = false;
  17073. WebAudioSound.decode();
  17074. }
  17075. static _fail() {
  17076. WebAudioSound.e.event("err:" + WebAudioSound.tInfo.url, null);
  17077. WebAudioSound.isDecoding = false;
  17078. WebAudioSound.decode();
  17079. }
  17080. static _playEmptySound() {
  17081. if (WebAudioSound.ctx == null) {
  17082. return;
  17083. }
  17084. var source = WebAudioSound.ctx.createBufferSource();
  17085. source.buffer = WebAudioSound._miniBuffer;
  17086. source.connect(WebAudioSound.ctx.destination);
  17087. source.start(0, 0, 0);
  17088. }
  17089. static _unlock() {
  17090. if (WebAudioSound._unlocked) {
  17091. return;
  17092. }
  17093. WebAudioSound._playEmptySound();
  17094. if (WebAudioSound.ctx.state == "running") {
  17095. window.document.removeEventListener("mousedown", WebAudioSound._unlock, true);
  17096. window.document.removeEventListener("touchend", WebAudioSound._unlock, true);
  17097. window.document.removeEventListener("touchstart", WebAudioSound._unlock, true);
  17098. WebAudioSound._unlocked = true;
  17099. }
  17100. }
  17101. static initWebAudio() {
  17102. if (WebAudioSound.ctx.state != "running") {
  17103. WebAudioSound._unlock();
  17104. window.document.addEventListener("mousedown", WebAudioSound._unlock, true);
  17105. window.document.addEventListener("touchend", WebAudioSound._unlock, true);
  17106. window.document.addEventListener("touchstart", WebAudioSound._unlock, true);
  17107. }
  17108. }
  17109. load(url) {
  17110. var me = this;
  17111. url = URL.formatURL(url);
  17112. this.url = url;
  17113. this.audioBuffer = WebAudioSound._dataCache[url];
  17114. if (this.audioBuffer) {
  17115. this._loaded(this.audioBuffer);
  17116. return;
  17117. }
  17118. WebAudioSound.e.on("loaded:" + url, this, this._loaded);
  17119. WebAudioSound.e.on("err:" + url, this, this._err);
  17120. if (WebAudioSound.__loadingSound[url]) {
  17121. return;
  17122. }
  17123. WebAudioSound.__loadingSound[url] = true;
  17124. var request = new XMLHttpRequest();
  17125. request.open("GET", url, true);
  17126. request.responseType = "arraybuffer";
  17127. request.onload = function () {
  17128. if (me._disposed) {
  17129. me._removeLoadEvents();
  17130. return;
  17131. }
  17132. me.data = request.response;
  17133. WebAudioSound.buffs.push({ "buffer": me.data, "url": me.url });
  17134. WebAudioSound.decode();
  17135. };
  17136. request.onerror = function (e) {
  17137. me._err();
  17138. };
  17139. request.send();
  17140. }
  17141. _err() {
  17142. this._removeLoadEvents();
  17143. WebAudioSound.__loadingSound[this.url] = false;
  17144. this.event(Event.ERROR);
  17145. }
  17146. _loaded(audioBuffer) {
  17147. this._removeLoadEvents();
  17148. if (this._disposed) {
  17149. return;
  17150. }
  17151. this.audioBuffer = audioBuffer;
  17152. WebAudioSound._dataCache[this.url] = this.audioBuffer;
  17153. this.loaded = true;
  17154. this.event(Event.COMPLETE);
  17155. }
  17156. _removeLoadEvents() {
  17157. WebAudioSound.e.off("loaded:" + this.url, this, this._loaded);
  17158. WebAudioSound.e.off("err:" + this.url, this, this._err);
  17159. }
  17160. __playAfterLoaded() {
  17161. if (!this.__toPlays)
  17162. return;
  17163. var i, len;
  17164. var toPlays;
  17165. toPlays = this.__toPlays;
  17166. len = toPlays.length;
  17167. var tParams;
  17168. for (i = 0; i < len; i++) {
  17169. tParams = toPlays[i];
  17170. if (tParams[2] && !tParams[2].isStopped) {
  17171. this.play(tParams[0], tParams[1], tParams[2]);
  17172. }
  17173. }
  17174. this.__toPlays.length = 0;
  17175. }
  17176. play(startTime = 0, loops = 0, channel = null) {
  17177. channel = channel ? channel : new WebAudioSoundChannel();
  17178. if (!this.audioBuffer) {
  17179. if (this.url) {
  17180. if (!this.__toPlays)
  17181. this.__toPlays = [];
  17182. this.__toPlays.push([startTime, loops, channel]);
  17183. this.once(Event.COMPLETE, this, this.__playAfterLoaded);
  17184. this.load(this.url);
  17185. }
  17186. }
  17187. channel.url = this.url;
  17188. channel.loops = loops;
  17189. channel["audioBuffer"] = this.audioBuffer;
  17190. channel.startTime = startTime;
  17191. channel.play();
  17192. ILaya.SoundManager.addChannel(channel);
  17193. return channel;
  17194. }
  17195. get duration() {
  17196. if (this.audioBuffer) {
  17197. return this.audioBuffer.duration;
  17198. }
  17199. return 0;
  17200. }
  17201. dispose() {
  17202. this._disposed = true;
  17203. delete WebAudioSound._dataCache[this.url];
  17204. delete WebAudioSound.__loadingSound[this.url];
  17205. this.audioBuffer = null;
  17206. this.data = null;
  17207. this.__toPlays = [];
  17208. }
  17209. }
  17210. WebAudioSound._dataCache = {};
  17211. WebAudioSound.webAudioEnabled = window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"];
  17212. WebAudioSound.ctx = WebAudioSound.webAudioEnabled ? new (window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"])() : undefined;
  17213. WebAudioSound.buffs = [];
  17214. WebAudioSound.isDecoding = false;
  17215. WebAudioSound._miniBuffer = WebAudioSound.ctx ? WebAudioSound.ctx.createBuffer(1, 1, 22050) : undefined;
  17216. WebAudioSound.e = new EventDispatcher();
  17217. WebAudioSound._unlocked = false;
  17218. WebAudioSound.__loadingSound = {};
  17219. class SoundManager {
  17220. static __init__() {
  17221. var win = ILaya.Browser.window;
  17222. var supportWebAudio = win["AudioContext"] || win["webkitAudioContext"] || win["mozAudioContext"] ? true : false;
  17223. if (supportWebAudio)
  17224. WebAudioSound.initWebAudio();
  17225. SoundManager._soundClass = supportWebAudio ? WebAudioSound : AudioSound;
  17226. AudioSound._initMusicAudio();
  17227. SoundManager._musicClass = AudioSound;
  17228. return supportWebAudio;
  17229. }
  17230. static addChannel(channel) {
  17231. if (SoundManager._channels.indexOf(channel) >= 0)
  17232. return;
  17233. SoundManager._channels.push(channel);
  17234. }
  17235. static removeChannel(channel) {
  17236. var i;
  17237. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17238. if (SoundManager._channels[i] == channel) {
  17239. SoundManager._channels.splice(i, 1);
  17240. }
  17241. }
  17242. }
  17243. static disposeSoundLater(url) {
  17244. SoundManager._lastSoundUsedTimeDic[url] = ILaya.Browser.now();
  17245. if (!SoundManager._isCheckingDispose) {
  17246. SoundManager._isCheckingDispose = true;
  17247. ILaya.timer.loop(5000, null, SoundManager._checkDisposeSound);
  17248. }
  17249. }
  17250. static _checkDisposeSound() {
  17251. var key;
  17252. var tTime = ILaya.Browser.now();
  17253. var hasCheck = false;
  17254. for (key in SoundManager._lastSoundUsedTimeDic) {
  17255. if (tTime - SoundManager._lastSoundUsedTimeDic[key] > 30000) {
  17256. delete SoundManager._lastSoundUsedTimeDic[key];
  17257. SoundManager.disposeSoundIfNotUsed(key);
  17258. }
  17259. else {
  17260. hasCheck = true;
  17261. }
  17262. }
  17263. if (!hasCheck) {
  17264. SoundManager._isCheckingDispose = false;
  17265. ILaya.timer.clear(null, SoundManager._checkDisposeSound);
  17266. }
  17267. }
  17268. static disposeSoundIfNotUsed(url) {
  17269. var i;
  17270. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17271. if (SoundManager._channels[i].url == url) {
  17272. return;
  17273. }
  17274. }
  17275. SoundManager.destroySound(url);
  17276. }
  17277. static set autoStopMusic(v) {
  17278. ILaya.stage.off(Event.BLUR, null, SoundManager._stageOnBlur);
  17279. ILaya.stage.off(Event.FOCUS, null, SoundManager._stageOnFocus);
  17280. ILaya.stage.off(Event.VISIBILITY_CHANGE, null, SoundManager._visibilityChange);
  17281. SoundManager._autoStopMusic = v;
  17282. if (v) {
  17283. ILaya.stage.on(Event.BLUR, null, SoundManager._stageOnBlur);
  17284. ILaya.stage.on(Event.FOCUS, null, SoundManager._stageOnFocus);
  17285. ILaya.stage.on(Event.VISIBILITY_CHANGE, null, SoundManager._visibilityChange);
  17286. }
  17287. }
  17288. static get autoStopMusic() {
  17289. return SoundManager._autoStopMusic;
  17290. }
  17291. static _visibilityChange() {
  17292. if (ILaya.stage.isVisibility) {
  17293. SoundManager._stageOnFocus();
  17294. }
  17295. else {
  17296. SoundManager._stageOnBlur();
  17297. }
  17298. }
  17299. static _stageOnBlur() {
  17300. SoundManager._isActive = false;
  17301. if (SoundManager._musicChannel) {
  17302. if (!SoundManager._musicChannel.isStopped) {
  17303. SoundManager._blurPaused = true;
  17304. SoundManager._musicChannel.pause();
  17305. }
  17306. }
  17307. SoundManager.stopAllSound();
  17308. ILaya.stage.once(Event.MOUSE_DOWN, null, SoundManager._stageOnFocus);
  17309. }
  17310. static _recoverWebAudio() {
  17311. if (WebAudioSound.ctx && WebAudioSound.ctx.state != "running" && WebAudioSound.ctx.resume)
  17312. WebAudioSound.ctx.resume();
  17313. }
  17314. static _stageOnFocus() {
  17315. SoundManager._isActive = true;
  17316. SoundManager._recoverWebAudio();
  17317. ILaya.stage.off(Event.MOUSE_DOWN, null, SoundManager._stageOnFocus);
  17318. if (SoundManager._blurPaused) {
  17319. if (SoundManager._musicChannel && SoundManager._musicChannel.isStopped) {
  17320. SoundManager._blurPaused = false;
  17321. SoundManager._musicChannel.resume();
  17322. }
  17323. }
  17324. }
  17325. static set muted(value) {
  17326. if (value == SoundManager._muted)
  17327. return;
  17328. if (value) {
  17329. SoundManager.stopAllSound();
  17330. }
  17331. SoundManager.musicMuted = value;
  17332. SoundManager._muted = value;
  17333. }
  17334. static get muted() {
  17335. return SoundManager._muted;
  17336. }
  17337. static set soundMuted(value) {
  17338. SoundManager._soundMuted = value;
  17339. }
  17340. static get soundMuted() {
  17341. return SoundManager._soundMuted;
  17342. }
  17343. static set musicMuted(value) {
  17344. if (value == SoundManager._musicMuted)
  17345. return;
  17346. if (value) {
  17347. if (SoundManager._bgMusic) {
  17348. if (SoundManager._musicChannel && !SoundManager._musicChannel.isStopped) {
  17349. if (ILaya.Render.isConchApp) {
  17350. if (SoundManager._musicChannel._audio)
  17351. SoundManager._musicChannel._audio.muted = true;
  17352. }
  17353. else {
  17354. SoundManager._musicChannel.pause();
  17355. }
  17356. }
  17357. else {
  17358. SoundManager._musicChannel = null;
  17359. }
  17360. }
  17361. else {
  17362. SoundManager._musicChannel = null;
  17363. }
  17364. SoundManager._musicMuted = value;
  17365. }
  17366. else {
  17367. SoundManager._musicMuted = value;
  17368. if (SoundManager._bgMusic) {
  17369. if (SoundManager._musicChannel) {
  17370. if (ILaya.Render.isConchApp) {
  17371. if (SoundManager._musicChannel._audio)
  17372. SoundManager._musicChannel._audio.muted = false;
  17373. }
  17374. else {
  17375. SoundManager._musicChannel.resume();
  17376. }
  17377. }
  17378. }
  17379. }
  17380. }
  17381. static get musicMuted() {
  17382. return SoundManager._musicMuted;
  17383. }
  17384. static get useAudioMusic() {
  17385. return SoundManager._useAudioMusic;
  17386. }
  17387. static set useAudioMusic(value) {
  17388. SoundManager._useAudioMusic = value;
  17389. if (value) {
  17390. SoundManager._musicClass = AudioSound;
  17391. }
  17392. else {
  17393. SoundManager._musicClass = null;
  17394. }
  17395. }
  17396. static playSound(url, loops = 1, complete = null, soundClass = null, startTime = 0) {
  17397. if (!SoundManager._isActive || !url)
  17398. return null;
  17399. if (SoundManager._muted)
  17400. return null;
  17401. SoundManager._recoverWebAudio();
  17402. url = URL.formatURL(url);
  17403. if (url == SoundManager._bgMusic) {
  17404. if (SoundManager._musicMuted)
  17405. return null;
  17406. }
  17407. else {
  17408. if (ILaya.Render.isConchApp) {
  17409. var ext = Utils.getFileExtension(url);
  17410. if (ext != "wav" && ext != "ogg") {
  17411. alert("The sound only supports wav or ogg format,for optimal performance reason,please refer to the official website document.");
  17412. return null;
  17413. }
  17414. }
  17415. if (SoundManager._soundMuted)
  17416. return null;
  17417. }
  17418. var tSound;
  17419. if (!ILaya.Browser.onBDMiniGame && !ILaya.Browser.onMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame) {
  17420. tSound = ILaya.loader.getRes(url);
  17421. }
  17422. if (!soundClass)
  17423. soundClass = SoundManager._soundClass;
  17424. if (!tSound) {
  17425. tSound = new soundClass();
  17426. tSound.load(url);
  17427. if (!ILaya.Browser.onBDMiniGame && !ILaya.Browser.onMiniGame && !ILaya.Browser.onKGMiniGame && !ILaya.Browser.onQGMiniGame && !ILaya.Browser.onVVMiniGame && !ILaya.Browser.onAlipayMiniGame && !ILaya.Browser.onQQMiniGame) {
  17428. ILaya.Loader.cacheRes(url, tSound);
  17429. }
  17430. }
  17431. var channel;
  17432. channel = tSound.play(startTime, loops);
  17433. if (!channel)
  17434. return null;
  17435. channel.url = url;
  17436. channel.volume = (url == SoundManager._bgMusic) ? SoundManager.musicVolume : SoundManager.soundVolume;
  17437. channel.completeHandler = complete;
  17438. return channel;
  17439. }
  17440. static destroySound(url) {
  17441. var tSound = ILaya.loader.getRes(url);
  17442. if (tSound) {
  17443. ILaya.Loader.clearRes(url);
  17444. tSound.dispose();
  17445. }
  17446. }
  17447. static playMusic(url, loops = 0, complete = null, startTime = 0) {
  17448. url = URL.formatURL(url);
  17449. SoundManager._bgMusic = url;
  17450. if (SoundManager._musicChannel)
  17451. SoundManager._musicChannel.stop();
  17452. return SoundManager._musicChannel = SoundManager.playSound(url, loops, complete, SoundManager._musicClass, startTime);
  17453. }
  17454. static stopSound(url) {
  17455. url = URL.formatURL(url);
  17456. var i;
  17457. var channel;
  17458. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17459. channel = SoundManager._channels[i];
  17460. if (channel.url == url) {
  17461. channel.stop();
  17462. }
  17463. }
  17464. }
  17465. static stopAll() {
  17466. SoundManager._bgMusic = null;
  17467. var i;
  17468. var channel;
  17469. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17470. channel = SoundManager._channels[i];
  17471. channel.stop();
  17472. }
  17473. }
  17474. static stopAllSound() {
  17475. var i;
  17476. var channel;
  17477. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17478. channel = SoundManager._channels[i];
  17479. if (channel.url != SoundManager._bgMusic) {
  17480. channel.stop();
  17481. }
  17482. }
  17483. }
  17484. static stopMusic() {
  17485. if (SoundManager._musicChannel)
  17486. SoundManager._musicChannel.stop();
  17487. SoundManager._bgMusic = null;
  17488. }
  17489. static setSoundVolume(volume, url = null) {
  17490. if (url) {
  17491. url = URL.formatURL(url);
  17492. SoundManager._setVolume(url, volume);
  17493. }
  17494. else {
  17495. SoundManager.soundVolume = volume;
  17496. var i;
  17497. var channel;
  17498. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17499. channel = SoundManager._channels[i];
  17500. if (channel.url != SoundManager._bgMusic) {
  17501. channel.volume = volume;
  17502. }
  17503. }
  17504. }
  17505. }
  17506. static setMusicVolume(volume) {
  17507. SoundManager.musicVolume = volume;
  17508. SoundManager._setVolume(SoundManager._bgMusic, volume);
  17509. }
  17510. static _setVolume(url, volume) {
  17511. url = URL.formatURL(url);
  17512. var i;
  17513. var channel;
  17514. for (i = SoundManager._channels.length - 1; i >= 0; i--) {
  17515. channel = SoundManager._channels[i];
  17516. if (channel.url == url) {
  17517. channel.volume = volume;
  17518. }
  17519. }
  17520. }
  17521. }
  17522. SoundManager.musicVolume = 1;
  17523. SoundManager.soundVolume = 1;
  17524. SoundManager.playbackRate = 1;
  17525. SoundManager._useAudioMusic = true;
  17526. SoundManager._muted = false;
  17527. SoundManager._soundMuted = false;
  17528. SoundManager._musicMuted = false;
  17529. SoundManager._bgMusic = null;
  17530. SoundManager._musicChannel = null;
  17531. SoundManager._channels = [];
  17532. SoundManager._blurPaused = false;
  17533. SoundManager._isActive = true;
  17534. SoundManager._lastSoundUsedTimeDic = {};
  17535. SoundManager._isCheckingDispose = false;
  17536. SoundManager.autoReleaseSound = true;
  17537. class Prefab {
  17538. create() {
  17539. if (this.json)
  17540. return ILaya.SceneUtils.createByData(null, this.json);
  17541. return null;
  17542. }
  17543. }
  17544. class Byte {
  17545. constructor(data = null) {
  17546. this._xd_ = true;
  17547. this._allocated_ = 8;
  17548. this._pos_ = 0;
  17549. this._length = 0;
  17550. if (data) {
  17551. this._u8d_ = new Uint8Array(data);
  17552. this._d_ = new DataView(this._u8d_.buffer);
  17553. this._length = this._d_.byteLength;
  17554. }
  17555. else {
  17556. this._resizeBuffer(this._allocated_);
  17557. }
  17558. }
  17559. static getSystemEndian() {
  17560. if (!Byte._sysEndian) {
  17561. var buffer = new ArrayBuffer(2);
  17562. new DataView(buffer).setInt16(0, 256, true);
  17563. Byte._sysEndian = (new Int16Array(buffer))[0] === 256 ? Byte.LITTLE_ENDIAN : Byte.BIG_ENDIAN;
  17564. }
  17565. return Byte._sysEndian;
  17566. }
  17567. get buffer() {
  17568. var rstBuffer = this._d_.buffer;
  17569. if (rstBuffer.byteLength === this._length)
  17570. return rstBuffer;
  17571. return rstBuffer.slice(0, this._length);
  17572. }
  17573. get endian() {
  17574. return this._xd_ ? Byte.LITTLE_ENDIAN : Byte.BIG_ENDIAN;
  17575. }
  17576. set endian(value) {
  17577. this._xd_ = (value === Byte.LITTLE_ENDIAN);
  17578. }
  17579. set length(value) {
  17580. if (this._allocated_ < value)
  17581. this._resizeBuffer(this._allocated_ = Math.floor(Math.max(value, this._allocated_ * 2)));
  17582. else if (this._allocated_ > value)
  17583. this._resizeBuffer(this._allocated_ = value);
  17584. this._length = value;
  17585. }
  17586. get length() {
  17587. return this._length;
  17588. }
  17589. _resizeBuffer(len) {
  17590. try {
  17591. var newByteView = new Uint8Array(len);
  17592. if (this._u8d_ != null) {
  17593. if (this._u8d_.length <= len)
  17594. newByteView.set(this._u8d_);
  17595. else
  17596. newByteView.set(this._u8d_.subarray(0, len));
  17597. }
  17598. this._u8d_ = newByteView;
  17599. this._d_ = new DataView(newByteView.buffer);
  17600. }
  17601. catch (err) {
  17602. throw "Invalid typed array length:" + len;
  17603. }
  17604. }
  17605. getString() {
  17606. return this.readString();
  17607. }
  17608. readString() {
  17609. return this._rUTF(this.getUint16());
  17610. }
  17611. getFloat32Array(start, len) {
  17612. return this.readFloat32Array(start, len);
  17613. }
  17614. readFloat32Array(start, len) {
  17615. var end = start + len;
  17616. end = (end > this._length) ? this._length : end;
  17617. var v = new Float32Array(this._d_.buffer.slice(start, end));
  17618. this._pos_ = end;
  17619. return v;
  17620. }
  17621. getUint8Array(start, len) {
  17622. return this.readUint8Array(start, len);
  17623. }
  17624. readUint8Array(start, len) {
  17625. var end = start + len;
  17626. end = (end > this._length) ? this._length : end;
  17627. var v = new Uint8Array(this._d_.buffer.slice(start, end));
  17628. this._pos_ = end;
  17629. return v;
  17630. }
  17631. getInt16Array(start, len) {
  17632. return this.readInt16Array(start, len);
  17633. }
  17634. readInt16Array(start, len) {
  17635. var end = start + len;
  17636. end = (end > this._length) ? this._length : end;
  17637. var v = new Int16Array(this._d_.buffer.slice(start, end));
  17638. this._pos_ = end;
  17639. return v;
  17640. }
  17641. getFloat32() {
  17642. return this.readFloat32();
  17643. }
  17644. readFloat32() {
  17645. if (this._pos_ + 4 > this._length)
  17646. throw "getFloat32 error - Out of bounds";
  17647. var v = this._d_.getFloat32(this._pos_, this._xd_);
  17648. this._pos_ += 4;
  17649. return v;
  17650. }
  17651. getFloat64() {
  17652. return this.readFloat64();
  17653. }
  17654. readFloat64() {
  17655. if (this._pos_ + 8 > this._length)
  17656. throw "getFloat64 error - Out of bounds";
  17657. var v = this._d_.getFloat64(this._pos_, this._xd_);
  17658. this._pos_ += 8;
  17659. return v;
  17660. }
  17661. writeFloat32(value) {
  17662. this._ensureWrite(this._pos_ + 4);
  17663. this._d_.setFloat32(this._pos_, value, this._xd_);
  17664. this._pos_ += 4;
  17665. }
  17666. writeFloat64(value) {
  17667. this._ensureWrite(this._pos_ + 8);
  17668. this._d_.setFloat64(this._pos_, value, this._xd_);
  17669. this._pos_ += 8;
  17670. }
  17671. getInt32() {
  17672. return this.readInt32();
  17673. }
  17674. readInt32() {
  17675. if (this._pos_ + 4 > this._length)
  17676. throw "getInt32 error - Out of bounds";
  17677. var float = this._d_.getInt32(this._pos_, this._xd_);
  17678. this._pos_ += 4;
  17679. return float;
  17680. }
  17681. getUint32() {
  17682. return this.readUint32();
  17683. }
  17684. readUint32() {
  17685. if (this._pos_ + 4 > this._length)
  17686. throw "getUint32 error - Out of bounds";
  17687. var v = this._d_.getUint32(this._pos_, this._xd_);
  17688. this._pos_ += 4;
  17689. return v;
  17690. }
  17691. writeInt32(value) {
  17692. this._ensureWrite(this._pos_ + 4);
  17693. this._d_.setInt32(this._pos_, value, this._xd_);
  17694. this._pos_ += 4;
  17695. }
  17696. writeUint32(value) {
  17697. this._ensureWrite(this._pos_ + 4);
  17698. this._d_.setUint32(this._pos_, value, this._xd_);
  17699. this._pos_ += 4;
  17700. }
  17701. getInt16() {
  17702. return this.readInt16();
  17703. }
  17704. readInt16() {
  17705. if (this._pos_ + 2 > this._length)
  17706. throw "getInt16 error - Out of bounds";
  17707. var us = this._d_.getInt16(this._pos_, this._xd_);
  17708. this._pos_ += 2;
  17709. return us;
  17710. }
  17711. getUint16() {
  17712. return this.readUint16();
  17713. }
  17714. readUint16() {
  17715. if (this._pos_ + 2 > this._length)
  17716. throw "getUint16 error - Out of bounds";
  17717. var us = this._d_.getUint16(this._pos_, this._xd_);
  17718. this._pos_ += 2;
  17719. return us;
  17720. }
  17721. writeUint16(value) {
  17722. this._ensureWrite(this._pos_ + 2);
  17723. this._d_.setUint16(this._pos_, value, this._xd_);
  17724. this._pos_ += 2;
  17725. }
  17726. writeInt16(value) {
  17727. this._ensureWrite(this._pos_ + 2);
  17728. this._d_.setInt16(this._pos_, value, this._xd_);
  17729. this._pos_ += 2;
  17730. }
  17731. getUint8() {
  17732. return this.readUint8();
  17733. }
  17734. readUint8() {
  17735. if (this._pos_ + 1 > this._length)
  17736. throw "getUint8 error - Out of bounds";
  17737. return this._u8d_[this._pos_++];
  17738. }
  17739. writeUint8(value) {
  17740. this._ensureWrite(this._pos_ + 1);
  17741. this._d_.setUint8(this._pos_, value);
  17742. this._pos_++;
  17743. }
  17744. _getUInt8(pos) {
  17745. return this._readUInt8(pos);
  17746. }
  17747. _readUInt8(pos) {
  17748. return this._d_.getUint8(pos);
  17749. }
  17750. _getUint16(pos) {
  17751. return this._readUint16(pos);
  17752. }
  17753. _readUint16(pos) {
  17754. return this._d_.getUint16(pos, this._xd_);
  17755. }
  17756. _getMatrix() {
  17757. return this._readMatrix();
  17758. }
  17759. _readMatrix() {
  17760. var rst = new Matrix(this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32(), this.getFloat32());
  17761. return rst;
  17762. }
  17763. _rUTF(len) {
  17764. var max = this._pos_ + len, c, c2, c3, f = String.fromCharCode;
  17765. var u = this._u8d_;
  17766. var strs = [];
  17767. var n = 0;
  17768. strs.length = 1000;
  17769. while (this._pos_ < max) {
  17770. c = u[this._pos_++];
  17771. if (c < 0x80) {
  17772. if (c != 0)
  17773. strs[n++] = f(c);
  17774. }
  17775. else if (c < 0xE0) {
  17776. strs[n++] = f(((c & 0x3F) << 6) | (u[this._pos_++] & 0x7F));
  17777. }
  17778. else if (c < 0xF0) {
  17779. c2 = u[this._pos_++];
  17780. strs[n++] = f(((c & 0x1F) << 12) | ((c2 & 0x7F) << 6) | (u[this._pos_++] & 0x7F));
  17781. }
  17782. else {
  17783. c2 = u[this._pos_++];
  17784. c3 = u[this._pos_++];
  17785. const _code = ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 & 0x7F) << 6) | (u[this._pos_++] & 0x7F);
  17786. if (_code >= 0x10000) {
  17787. const _offset = _code - 0x10000;
  17788. const _lead = 0xd800 | (_offset >> 10);
  17789. const _trail = 0xdc00 | (_offset & 0x3ff);
  17790. strs[n++] = f(_lead);
  17791. strs[n++] = f(_trail);
  17792. }
  17793. else {
  17794. strs[n++] = f(_code);
  17795. }
  17796. }
  17797. }
  17798. strs.length = n;
  17799. return strs.join('');
  17800. }
  17801. getCustomString(len) {
  17802. return this.readCustomString(len);
  17803. }
  17804. readCustomString(len) {
  17805. var v = "", ulen = 0, c, c2, f = String.fromCharCode;
  17806. var u = this._u8d_;
  17807. while (len > 0) {
  17808. c = u[this._pos_];
  17809. if (c < 0x80) {
  17810. v += f(c);
  17811. this._pos_++;
  17812. len--;
  17813. }
  17814. else {
  17815. ulen = c - 0x80;
  17816. this._pos_++;
  17817. len -= ulen;
  17818. while (ulen > 0) {
  17819. c = u[this._pos_++];
  17820. c2 = u[this._pos_++];
  17821. v += f((c2 << 8) | c);
  17822. ulen--;
  17823. }
  17824. }
  17825. }
  17826. return v;
  17827. }
  17828. get pos() {
  17829. return this._pos_;
  17830. }
  17831. set pos(value) {
  17832. this._pos_ = value;
  17833. }
  17834. get bytesAvailable() {
  17835. return this._length - this._pos_;
  17836. }
  17837. clear() {
  17838. this._pos_ = 0;
  17839. this.length = 0;
  17840. }
  17841. __getBuffer() {
  17842. return this._d_.buffer;
  17843. }
  17844. writeUTFBytes(value) {
  17845. value = value + "";
  17846. for (var i = 0, sz = value.length; i < sz; i++) {
  17847. var c = value.charCodeAt(i);
  17848. if (c <= 0x7F) {
  17849. this.writeByte(c);
  17850. }
  17851. else if (c <= 0x7FF) {
  17852. this._ensureWrite(this._pos_ + 2);
  17853. this._u8d_.set([0xC0 | (c >> 6), 0x80 | (c & 0x3F)], this._pos_);
  17854. this._pos_ += 2;
  17855. }
  17856. else if (c >= 0xD800 && c <= 0xDBFF) {
  17857. i++;
  17858. const c2 = value.charCodeAt(i);
  17859. if (!Number.isNaN(c2) && c2 >= 0xDC00 && c2 <= 0xDFFF) {
  17860. const _p1 = (c & 0x3FF) + 0x40;
  17861. const _p2 = c2 & 0x3FF;
  17862. const _b1 = 0xF0 | ((_p1 >> 8) & 0x3F);
  17863. const _b2 = 0x80 | ((_p1 >> 2) & 0x3F);
  17864. const _b3 = 0x80 | ((_p1 & 0x3) << 4) | ((_p2 >> 6) & 0xF);
  17865. const _b4 = 0x80 | (_p2 & 0x3F);
  17866. this._ensureWrite(this._pos_ + 4);
  17867. this._u8d_.set([_b1, _b2, _b3, _b4], this._pos_);
  17868. this._pos_ += 4;
  17869. }
  17870. }
  17871. else if (c <= 0xFFFF) {
  17872. this._ensureWrite(this._pos_ + 3);
  17873. this._u8d_.set([0xE0 | (c >> 12), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)], this._pos_);
  17874. this._pos_ += 3;
  17875. }
  17876. else {
  17877. this._ensureWrite(this._pos_ + 4);
  17878. this._u8d_.set([0xF0 | (c >> 18), 0x80 | ((c >> 12) & 0x3F), 0x80 | ((c >> 6) & 0x3F), 0x80 | (c & 0x3F)], this._pos_);
  17879. this._pos_ += 4;
  17880. }
  17881. }
  17882. }
  17883. writeUTFString(value) {
  17884. var tPos = this.pos;
  17885. this.writeUint16(1);
  17886. this.writeUTFBytes(value);
  17887. var dPos = this.pos - tPos - 2;
  17888. this._d_.setUint16(tPos, dPos, this._xd_);
  17889. }
  17890. readUTFString() {
  17891. return this.readUTFBytes(this.getUint16());
  17892. }
  17893. getUTFString() {
  17894. return this.readUTFString();
  17895. }
  17896. readUTFBytes(len = -1) {
  17897. if (len === 0)
  17898. return "";
  17899. var lastBytes = this.bytesAvailable;
  17900. if (len > lastBytes)
  17901. throw "readUTFBytes error - Out of bounds";
  17902. len = len > 0 ? len : lastBytes;
  17903. return this._rUTF(len);
  17904. }
  17905. getUTFBytes(len = -1) {
  17906. return this.readUTFBytes(len);
  17907. }
  17908. writeByte(value) {
  17909. this._ensureWrite(this._pos_ + 1);
  17910. this._d_.setInt8(this._pos_, value);
  17911. this._pos_ += 1;
  17912. }
  17913. readByte() {
  17914. if (this._pos_ + 1 > this._length)
  17915. throw "readByte error - Out of bounds";
  17916. return this._d_.getInt8(this._pos_++);
  17917. }
  17918. getByte() {
  17919. return this.readByte();
  17920. }
  17921. _ensureWrite(lengthToEnsure) {
  17922. if (this._length < lengthToEnsure)
  17923. this._length = lengthToEnsure;
  17924. if (this._allocated_ < lengthToEnsure)
  17925. this.length = lengthToEnsure;
  17926. }
  17927. writeArrayBuffer(arraybuffer, offset = 0, length = 0) {
  17928. if (offset < 0 || length < 0)
  17929. throw "writeArrayBuffer error - Out of bounds";
  17930. if (length == 0)
  17931. length = arraybuffer.byteLength - offset;
  17932. this._ensureWrite(this._pos_ + length);
  17933. var uint8array = new Uint8Array(arraybuffer);
  17934. this._u8d_.set(uint8array.subarray(offset, offset + length), this._pos_);
  17935. this._pos_ += length;
  17936. }
  17937. readArrayBuffer(length) {
  17938. var rst;
  17939. rst = this._u8d_.buffer.slice(this._pos_, this._pos_ + length);
  17940. this._pos_ = this._pos_ + length;
  17941. return rst;
  17942. }
  17943. }
  17944. Byte.BIG_ENDIAN = "bigEndian";
  17945. Byte.LITTLE_ENDIAN = "littleEndian";
  17946. Byte._sysEndian = null;
  17947. class BitmapFont {
  17948. constructor() {
  17949. this._fontCharDic = {};
  17950. this._fontWidthMap = {};
  17951. this._maxWidth = 0;
  17952. this._spaceWidth = 10;
  17953. this.fontSize = 12;
  17954. this.autoScaleSize = false;
  17955. this.letterSpacing = 0;
  17956. }
  17957. loadFont(path, complete) {
  17958. this._path = path;
  17959. this._complete = complete;
  17960. if (!path || path.indexOf(".fnt") === -1) {
  17961. console.error('Bitmap font configuration information must be a ".fnt" file');
  17962. return;
  17963. }
  17964. ILaya.loader.load([{ url: path, type: ILaya.Loader.XML }, { url: path.replace(".fnt", ".png"), type: ILaya.Loader.IMAGE }], Handler.create(this, this._onLoaded));
  17965. }
  17966. _onLoaded() {
  17967. this.parseFont(ILaya.Loader.getRes(this._path), ILaya.Loader.getRes(this._path.replace(".fnt", ".png")));
  17968. this._complete && this._complete.run();
  17969. }
  17970. parseFont(xml, texture) {
  17971. if (xml == null || texture == null)
  17972. return;
  17973. this._texture = texture;
  17974. var tScale = 1;
  17975. var tInfo = xml.getElementsByTagName("info");
  17976. if (!tInfo[0].getAttributeNode) {
  17977. return this.parseFont2(xml, texture);
  17978. }
  17979. this.fontSize = parseInt(tInfo[0].getAttributeNode("size").nodeValue);
  17980. var tPadding = tInfo[0].getAttributeNode("padding").nodeValue;
  17981. var tPaddingArray = tPadding.split(",");
  17982. this._padding = [parseInt(tPaddingArray[0]), parseInt(tPaddingArray[1]), parseInt(tPaddingArray[2]), parseInt(tPaddingArray[3])];
  17983. var chars = xml.getElementsByTagName("char");
  17984. var i = 0;
  17985. for (i = 0; i < chars.length; i++) {
  17986. var tAttribute = chars[i];
  17987. var tId = parseInt(tAttribute.getAttributeNode("id").nodeValue);
  17988. var xOffset = parseInt(tAttribute.getAttributeNode("xoffset").nodeValue) / tScale;
  17989. var yOffset = parseInt(tAttribute.getAttributeNode("yoffset").nodeValue) / tScale;
  17990. var xAdvance = parseInt(tAttribute.getAttributeNode("xadvance").nodeValue) / tScale;
  17991. var region = new Rectangle();
  17992. region.x = parseInt(tAttribute.getAttributeNode("x").nodeValue);
  17993. region.y = parseInt(tAttribute.getAttributeNode("y").nodeValue);
  17994. region.width = parseInt(tAttribute.getAttributeNode("width").nodeValue);
  17995. region.height = parseInt(tAttribute.getAttributeNode("height").nodeValue);
  17996. var tTexture = Texture.create(texture, region.x, region.y, region.width, region.height, xOffset, yOffset);
  17997. this._maxWidth = Math.max(this._maxWidth, xAdvance + this.letterSpacing);
  17998. this._fontCharDic[tId] = tTexture;
  17999. this._fontWidthMap[tId] = xAdvance;
  18000. }
  18001. }
  18002. parseFont2(xml, texture) {
  18003. if (xml == null || texture == null)
  18004. return;
  18005. this._texture = texture;
  18006. var tScale = 1;
  18007. var tInfo = xml.getElementsByTagName("info");
  18008. this.fontSize = parseInt(tInfo[0].attributes["size"].nodeValue);
  18009. var tPadding = tInfo[0].attributes["padding"].nodeValue;
  18010. var tPaddingArray = tPadding.split(",");
  18011. this._padding = [parseInt(tPaddingArray[0]), parseInt(tPaddingArray[1]), parseInt(tPaddingArray[2]), parseInt(tPaddingArray[3])];
  18012. var chars = xml.getElementsByTagName("char");
  18013. var i = 0;
  18014. for (i = 0; i < chars.length; i++) {
  18015. var tAttribute = chars[i].attributes;
  18016. var tId = parseInt(tAttribute["id"].nodeValue);
  18017. var xOffset = parseInt(tAttribute["xoffset"].nodeValue) / tScale;
  18018. var yOffset = parseInt(tAttribute["yoffset"].nodeValue) / tScale;
  18019. var xAdvance = parseInt(tAttribute["xadvance"].nodeValue) / tScale;
  18020. var region = new Rectangle();
  18021. region.x = parseInt(tAttribute["x"].nodeValue);
  18022. region.y = parseInt(tAttribute["y"].nodeValue);
  18023. region.width = parseInt(tAttribute["width"].nodeValue);
  18024. region.height = parseInt(tAttribute["height"].nodeValue);
  18025. var tTexture = Texture.create(texture, region.x, region.y, region.width, region.height, xOffset, yOffset);
  18026. this._maxWidth = Math.max(this._maxWidth, xAdvance + this.letterSpacing);
  18027. this._fontCharDic[tId] = tTexture;
  18028. this._fontWidthMap[tId] = xAdvance;
  18029. }
  18030. }
  18031. getCharTexture(char) {
  18032. return this._fontCharDic[char.charCodeAt(0)];
  18033. }
  18034. destroy() {
  18035. if (this._texture) {
  18036. for (var p in this._fontCharDic) {
  18037. var tTexture = this._fontCharDic[p];
  18038. if (tTexture)
  18039. tTexture.destroy();
  18040. }
  18041. this._texture.destroy();
  18042. this._fontCharDic = null;
  18043. this._fontWidthMap = null;
  18044. this._texture = null;
  18045. this._complete = null;
  18046. this._padding = null;
  18047. }
  18048. }
  18049. setSpaceWidth(spaceWidth) {
  18050. this._spaceWidth = spaceWidth;
  18051. }
  18052. getCharWidth(char) {
  18053. var code = char.charCodeAt(0);
  18054. if (this._fontWidthMap[code])
  18055. return this._fontWidthMap[code] + this.letterSpacing;
  18056. if (char === " ")
  18057. return this._spaceWidth + this.letterSpacing;
  18058. return 0;
  18059. }
  18060. getTextWidth(text) {
  18061. var tWidth = 0;
  18062. for (var i = 0, n = text.length; i < n; i++) {
  18063. tWidth += this.getCharWidth(text.charAt(i));
  18064. }
  18065. return tWidth;
  18066. }
  18067. getMaxWidth() {
  18068. return this._maxWidth;
  18069. }
  18070. getMaxHeight() {
  18071. return this.fontSize;
  18072. }
  18073. _drawText(text, sprite, drawX, drawY, align, width) {
  18074. var tWidth = this.getTextWidth(text);
  18075. var tTexture;
  18076. var dx = 0;
  18077. align === "center" && (dx = (width - tWidth) / 2);
  18078. align === "right" && (dx = (width - tWidth));
  18079. var tx = 0;
  18080. for (var i = 0, n = text.length; i < n; i++) {
  18081. tTexture = this.getCharTexture(text.charAt(i));
  18082. if (tTexture) {
  18083. sprite.graphics.drawImage(tTexture, drawX + tx + dx, drawY);
  18084. tx += this.getCharWidth(text.charAt(i));
  18085. }
  18086. }
  18087. }
  18088. }
  18089. ClassUtils.regClass("laya.display.BitmapFont", BitmapFont);
  18090. ClassUtils.regClass("Laya.BitmapFont", BitmapFont);
  18091. class HttpRequest extends EventDispatcher {
  18092. constructor() {
  18093. super(...arguments);
  18094. this._http = new XMLHttpRequest();
  18095. }
  18096. send(url, data = null, method = "get", responseType = "text", headers = null) {
  18097. this._responseType = responseType;
  18098. this._data = null;
  18099. if (Browser.onVVMiniGame || Browser.onQGMiniGame || Browser.onQQMiniGame) {
  18100. url = encodeURI(url);
  18101. }
  18102. this._url = url;
  18103. var _this = this;
  18104. var http = this._http;
  18105. url = URL.getAdptedFilePath(url);
  18106. http.open(method, url, true);
  18107. if (headers) {
  18108. for (var i = 0; i < headers.length; i++) {
  18109. http.setRequestHeader(headers[i++], headers[i]);
  18110. }
  18111. }
  18112. else if (!(window.conch)) {
  18113. if (!data || typeof (data) == 'string')
  18114. http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  18115. else
  18116. http.setRequestHeader("Content-Type", "application/json");
  18117. }
  18118. let restype = responseType !== "arraybuffer" ? "text" : "arraybuffer";
  18119. http.responseType = restype;
  18120. if (http.dataType) {
  18121. http.dataType = restype;
  18122. }
  18123. http.onerror = function (e) {
  18124. _this._onError(e);
  18125. };
  18126. http.onabort = function (e) {
  18127. _this._onAbort(e);
  18128. };
  18129. http.onprogress = function (e) {
  18130. _this._onProgress(e);
  18131. };
  18132. http.onload = function (e) {
  18133. _this._onLoad(e);
  18134. };
  18135. http.send(data);
  18136. }
  18137. _onProgress(e) {
  18138. if (e && e.lengthComputable)
  18139. this.event(Event.PROGRESS, e.loaded / e.total);
  18140. }
  18141. _onAbort(e) {
  18142. this.error("Request was aborted by user");
  18143. }
  18144. _onError(e) {
  18145. this.error("Request failed Status:" + this._http.status + " text:" + this._http.statusText);
  18146. }
  18147. _onLoad(e) {
  18148. var http = this._http;
  18149. var status = http.status !== undefined ? http.status : 200;
  18150. if (status === 200 || status === 204 || status === 0) {
  18151. this.complete();
  18152. }
  18153. else {
  18154. this.error("[" + http.status + "]" + http.statusText + ":" + http.responseURL);
  18155. }
  18156. }
  18157. error(message) {
  18158. this.clear();
  18159. console.warn(this.url, message);
  18160. this.event(Event.ERROR, message);
  18161. }
  18162. complete() {
  18163. this.clear();
  18164. var flag = true;
  18165. try {
  18166. if (this._responseType === "json") {
  18167. this._data = JSON.parse(this._http.responseText);
  18168. }
  18169. else if (this._responseType === "xml") {
  18170. this._data = Utils.parseXMLFromString(this._http.responseText);
  18171. }
  18172. else {
  18173. this._data = this._http.response || this._http.responseText;
  18174. }
  18175. }
  18176. catch (e) {
  18177. flag = false;
  18178. this.error(e.message);
  18179. }
  18180. flag && this.event(Event.COMPLETE, this._data instanceof Array ? [this._data] : this._data);
  18181. }
  18182. clear() {
  18183. var http = this._http;
  18184. http.onerror = http.onabort = http.onprogress = http.onload = null;
  18185. }
  18186. get url() {
  18187. return this._url;
  18188. }
  18189. get data() {
  18190. return this._data;
  18191. }
  18192. get http() {
  18193. return this._http;
  18194. }
  18195. }
  18196. class Loader extends EventDispatcher {
  18197. constructor() {
  18198. super(...arguments);
  18199. this._customParse = false;
  18200. }
  18201. static getTypeFromUrl(url) {
  18202. var type = Utils.getFileExtension(url);
  18203. if (type)
  18204. return Loader.typeMap[type];
  18205. console.warn("Not recognize the resources suffix", url);
  18206. return "text";
  18207. }
  18208. load(url, type = null, cache = true, group = null, ignoreCache = false, useWorkerLoader = ILaya.WorkerLoader.enable) {
  18209. if (!url) {
  18210. this.onLoaded(null);
  18211. return;
  18212. }
  18213. Loader.setGroup(url, "666");
  18214. this._url = url;
  18215. if (url.indexOf("data:image") === 0)
  18216. type = Loader.IMAGE;
  18217. else
  18218. url = URL.formatURL(url);
  18219. this._type = type || (type = Loader.getTypeFromUrl(this._url));
  18220. this._cache = cache;
  18221. this._useWorkerLoader = useWorkerLoader;
  18222. this._data = null;
  18223. if (useWorkerLoader)
  18224. ILaya.WorkerLoader.enableWorkerLoader();
  18225. var cacheRes;
  18226. if (type == Loader.IMAGE)
  18227. cacheRes = Loader.textureMap[url];
  18228. else
  18229. cacheRes = Loader.loadedMap[url];
  18230. if (!ignoreCache && cacheRes) {
  18231. this._data = cacheRes;
  18232. this.event(Event.PROGRESS, 1);
  18233. this.event(Event.COMPLETE, this._data);
  18234. return;
  18235. }
  18236. if (group)
  18237. Loader.setGroup(url, group);
  18238. if (Loader.parserMap[type] != null) {
  18239. this._customParse = true;
  18240. if (Loader.parserMap[type] instanceof Handler)
  18241. Loader.parserMap[type].runWith(this);
  18242. else
  18243. Loader.parserMap[type].call(null, this);
  18244. return;
  18245. }
  18246. this._loadResourceFilter(type, url);
  18247. }
  18248. _loadResourceFilter(type, url) {
  18249. this._loadResource(type, url);
  18250. }
  18251. _loadResource(type, url) {
  18252. switch (type) {
  18253. case Loader.IMAGE:
  18254. case "htmlimage":
  18255. case "nativeimage":
  18256. this._loadImage(url);
  18257. break;
  18258. case Loader.SOUND:
  18259. this._loadSound(url);
  18260. break;
  18261. case Loader.TTF:
  18262. this._loadTTF(url);
  18263. break;
  18264. case Loader.ATLAS:
  18265. case Loader.PREFAB:
  18266. case Loader.PLF:
  18267. this._loadHttpRequestWhat(url, Loader.JSON);
  18268. break;
  18269. case Loader.FONT:
  18270. this._loadHttpRequestWhat(url, Loader.XML);
  18271. break;
  18272. case Loader.PLFB:
  18273. this._loadHttpRequestWhat(url, Loader.BUFFER);
  18274. break;
  18275. default:
  18276. this._loadHttpRequestWhat(url, type);
  18277. }
  18278. }
  18279. _loadHttpRequest(url, contentType, onLoadCaller, onLoad, onProcessCaller, onProcess, onErrorCaller, onError) {
  18280. if (Browser.onVVMiniGame) {
  18281. this._http = new HttpRequest();
  18282. }
  18283. else {
  18284. if (!this._http)
  18285. this._http = new HttpRequest();
  18286. }
  18287. onProcess && this._http.on(Event.PROGRESS, onProcessCaller, onProcess);
  18288. onLoad && this._http.on(Event.COMPLETE, onLoadCaller, onLoad);
  18289. this._http.on(Event.ERROR, onErrorCaller, onError);
  18290. this._http.send(url, null, "get", contentType);
  18291. }
  18292. _loadHtmlImage(url, onLoadCaller, onLoad, onErrorCaller, onError) {
  18293. var image;
  18294. function clear() {
  18295. var img = image;
  18296. img.onload = null;
  18297. img.onerror = null;
  18298. delete Loader._imgCache[url];
  18299. }
  18300. var onerror = function () {
  18301. clear();
  18302. onError.call(onErrorCaller);
  18303. };
  18304. var onload = function () {
  18305. clear();
  18306. onLoad.call(onLoadCaller, image);
  18307. };
  18308. image = new Browser.window.Image();
  18309. image.crossOrigin = "";
  18310. image.onload = onload;
  18311. image.onerror = onerror;
  18312. image.src = url;
  18313. Loader._imgCache[url] = image;
  18314. }
  18315. _loadHttpRequestWhat(url, contentType) {
  18316. if (Loader.preLoadedMap[url])
  18317. this.onLoaded(Loader.preLoadedMap[url]);
  18318. else
  18319. this._loadHttpRequest(url, contentType, this, this.onLoaded, this, this.onProgress, this, this.onError);
  18320. }
  18321. _loadTTF(url) {
  18322. url = URL.formatURL(url);
  18323. var ttfLoader = new ILaya.TTFLoader();
  18324. ttfLoader.complete = Handler.create(this, this.onLoaded);
  18325. ttfLoader.load(url);
  18326. }
  18327. _loadImage(url, isformatURL = true) {
  18328. var _this = this;
  18329. if (isformatURL)
  18330. url = URL.formatURL(url);
  18331. var onLoaded;
  18332. var onError = function () {
  18333. _this.event(Event.ERROR, "Load image failed");
  18334. };
  18335. if (this._type === "nativeimage") {
  18336. onLoaded = (image) => {
  18337. this.onLoaded(image);
  18338. };
  18339. this._loadHtmlImage(url, this, onLoaded, this, onError);
  18340. }
  18341. else {
  18342. var ext = Utils.getFileExtension(url);
  18343. if (ext === "ktx" || ext === "pvr") {
  18344. onLoaded = function (imageData) {
  18345. let format;
  18346. switch (ext) {
  18347. case "ktx":
  18348. format = exports.TextureFormat.ETC1RGB;
  18349. break;
  18350. case "pvr":
  18351. format = exports.TextureFormat.PVRTCRGBA_4BPPV;
  18352. break;
  18353. default: {
  18354. console.error('unknown format', ext);
  18355. return;
  18356. }
  18357. }
  18358. var tex = new Texture2D(0, 0, format, false, false);
  18359. tex.wrapModeU = BaseTexture.WARPMODE_CLAMP;
  18360. tex.wrapModeV = BaseTexture.WARPMODE_CLAMP;
  18361. tex.setCompressData(imageData);
  18362. tex._setCreateURL(url);
  18363. _this.onLoaded(tex);
  18364. };
  18365. this._loadHttpRequest(url, Loader.BUFFER, this, onLoaded, null, null, this, onError);
  18366. }
  18367. else {
  18368. onLoaded = function (image) {
  18369. var tex = new Texture2D(image.width, image.height, 1, false, false);
  18370. tex.wrapModeU = BaseTexture.WARPMODE_CLAMP;
  18371. tex.wrapModeV = BaseTexture.WARPMODE_CLAMP;
  18372. tex.loadImageSource(image, true);
  18373. tex._setCreateURL(url);
  18374. _this.onLoaded(tex);
  18375. };
  18376. this._loadHtmlImage(url, this, onLoaded, this, onError);
  18377. }
  18378. }
  18379. }
  18380. _loadSound(url) {
  18381. var sound = (new SoundManager._soundClass());
  18382. var _this = this;
  18383. sound.on(Event.COMPLETE, this, soundOnload);
  18384. sound.on(Event.ERROR, this, soundOnErr);
  18385. sound.load(url);
  18386. function soundOnload() {
  18387. clear();
  18388. _this.onLoaded(sound);
  18389. }
  18390. function soundOnErr() {
  18391. clear();
  18392. sound.dispose();
  18393. _this.event(Event.ERROR, "Load sound failed");
  18394. }
  18395. function clear() {
  18396. sound.offAll();
  18397. }
  18398. }
  18399. onProgress(value) {
  18400. if (this._type === Loader.ATLAS)
  18401. this.event(Event.PROGRESS, value * 0.3);
  18402. else
  18403. this.event(Event.PROGRESS, value);
  18404. }
  18405. onError(message) {
  18406. this.event(Event.ERROR, message);
  18407. }
  18408. onLoaded(data = null) {
  18409. var type = this._type;
  18410. if (type == Loader.PLFB) {
  18411. this.parsePLFBData(data);
  18412. this.complete(data);
  18413. }
  18414. else if (type == Loader.PLF) {
  18415. this.parsePLFData(data);
  18416. this.complete(data);
  18417. }
  18418. else if (type === Loader.IMAGE) {
  18419. var tex = new Texture(data);
  18420. tex.url = this._url;
  18421. this.complete(tex);
  18422. }
  18423. else if (type === Loader.SOUND || type === "htmlimage" || type === "nativeimage") {
  18424. this.complete(data);
  18425. }
  18426. else if (type === Loader.ATLAS) {
  18427. if (!(data instanceof Texture2D)) {
  18428. var toloadPics = [];
  18429. if (!this._data) {
  18430. this._data = data;
  18431. if (data.meta && data.meta.image) {
  18432. toloadPics = data.meta.image.split(",");
  18433. var split = this._url.indexOf("/") >= 0 ? "/" : "\\";
  18434. var idx = this._url.lastIndexOf(split);
  18435. var folderPath = idx >= 0 ? this._url.substr(0, idx + 1) : "";
  18436. var changeType = null;
  18437. if (Browser.onAndroid && data.meta.compressTextureAndroid) {
  18438. changeType = ".ktx";
  18439. }
  18440. if (Browser.onIOS && data.meta.compressTextureIOS) {
  18441. changeType = ".pvr";
  18442. }
  18443. for (var i = 0, len = toloadPics.length; i < len; i++) {
  18444. if (changeType) {
  18445. toloadPics[i] = folderPath + toloadPics[i].replace(".png", changeType);
  18446. }
  18447. else {
  18448. toloadPics[i] = folderPath + toloadPics[i];
  18449. }
  18450. }
  18451. }
  18452. else {
  18453. toloadPics = [this._url.replace(".json", ".png")];
  18454. }
  18455. toloadPics.reverse();
  18456. data.toLoads = toloadPics;
  18457. data.pics = [];
  18458. }
  18459. this.event(Event.PROGRESS, 0.3 + 1 / toloadPics.length * 0.6);
  18460. return this._loadResourceFilter(Loader.IMAGE, toloadPics.pop());
  18461. }
  18462. else {
  18463. this._data.pics.push(data);
  18464. if (this._data.toLoads.length > 0) {
  18465. this.event(Event.PROGRESS, 0.3 + 1 / this._data.toLoads.length * 0.6);
  18466. return this._loadResourceFilter(Loader.IMAGE, this._data.toLoads.pop());
  18467. }
  18468. var frames = this._data.frames;
  18469. var cleanUrl = this._url.split("?")[0];
  18470. var directory = (this._data.meta && this._data.meta.prefix) ? this._data.meta.prefix : cleanUrl.substring(0, cleanUrl.lastIndexOf(".")) + "/";
  18471. var pics = this._data.pics;
  18472. var atlasURL = URL.formatURL(this._url);
  18473. var map = Loader.atlasMap[atlasURL] || (Loader.atlasMap[atlasURL] = []);
  18474. map.dir = directory;
  18475. var scaleRate = 1;
  18476. if (this._data.meta && this._data.meta.scale && this._data.meta.scale != 1) {
  18477. scaleRate = parseFloat(this._data.meta.scale);
  18478. for (var name in frames) {
  18479. var obj = frames[name];
  18480. var tPic = pics[obj.frame.idx ? obj.frame.idx : 0];
  18481. var url = URL.formatURL(directory + name);
  18482. tPic.scaleRate = scaleRate;
  18483. var tTexture;
  18484. 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));
  18485. Loader.cacheTexture(url, tTexture);
  18486. tTexture.url = url;
  18487. map.push(url);
  18488. }
  18489. }
  18490. else {
  18491. for (name in frames) {
  18492. obj = frames[name];
  18493. tPic = pics[obj.frame.idx ? obj.frame.idx : 0];
  18494. url = URL.formatURL(directory + name);
  18495. 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));
  18496. Loader.cacheTexture(url, tTexture);
  18497. tTexture.url = url;
  18498. map.push(url);
  18499. }
  18500. }
  18501. delete this._data.pics;
  18502. this.complete(this._data);
  18503. }
  18504. }
  18505. else if (type === Loader.FONT) {
  18506. if (!data._source) {
  18507. this._data = data;
  18508. this.event(Event.PROGRESS, 0.5);
  18509. return this._loadResourceFilter(Loader.IMAGE, this._url.replace(".fnt", ".png"));
  18510. }
  18511. else {
  18512. var bFont = new BitmapFont();
  18513. bFont.parseFont(this._data, new Texture(data));
  18514. var tArr = this._url.split(".fnt")[0].split("/");
  18515. var fontName = tArr[tArr.length - 1];
  18516. Text.registerBitmapFont(fontName, bFont);
  18517. this._data = bFont;
  18518. this.complete(this._data);
  18519. }
  18520. }
  18521. else if (type === Loader.PREFAB) {
  18522. var prefab = new Prefab();
  18523. prefab.json = data;
  18524. this.complete(prefab);
  18525. }
  18526. else {
  18527. this.complete(data);
  18528. }
  18529. }
  18530. parsePLFData(plfData) {
  18531. var type;
  18532. var filePath;
  18533. var fileDic;
  18534. for (type in plfData) {
  18535. fileDic = plfData[type];
  18536. switch (type) {
  18537. case "json":
  18538. case "text":
  18539. for (filePath in fileDic) {
  18540. Loader.preLoadedMap[URL.formatURL(filePath)] = fileDic[filePath];
  18541. }
  18542. break;
  18543. default:
  18544. for (filePath in fileDic) {
  18545. Loader.preLoadedMap[URL.formatURL(filePath)] = fileDic[filePath];
  18546. }
  18547. }
  18548. }
  18549. }
  18550. parsePLFBData(plfData) {
  18551. var byte;
  18552. byte = new Byte(plfData);
  18553. var i, len;
  18554. len = byte.getInt32();
  18555. for (i = 0; i < len; i++) {
  18556. this.parseOnePLFBFile(byte);
  18557. }
  18558. }
  18559. parseOnePLFBFile(byte) {
  18560. var fileLen;
  18561. var fileName;
  18562. var fileData;
  18563. fileName = byte.getUTFString();
  18564. fileLen = byte.getInt32();
  18565. fileData = byte.readArrayBuffer(fileLen);
  18566. Loader.preLoadedMap[URL.formatURL(fileName)] = fileData;
  18567. }
  18568. complete(data) {
  18569. this._data = data;
  18570. if (this._customParse) {
  18571. this.event(Event.LOADED, data instanceof Array ? [data] : data);
  18572. }
  18573. else {
  18574. Loader._loaders.push(this);
  18575. if (!Loader._isWorking)
  18576. Loader.checkNext();
  18577. }
  18578. }
  18579. static checkNext() {
  18580. Loader._isWorking = true;
  18581. var startTimer = Browser.now();
  18582. while (Loader._startIndex < Loader._loaders.length) {
  18583. Loader._loaders[Loader._startIndex].endLoad();
  18584. Loader._startIndex++;
  18585. if (Browser.now() - startTimer > Loader.maxTimeOut) {
  18586. console.warn("loader callback cost a long time:" + (Browser.now() - startTimer) + " url=" + Loader._loaders[Loader._startIndex - 1].url);
  18587. ILaya.systemTimer.frameOnce(1, null, Loader.checkNext);
  18588. return;
  18589. }
  18590. }
  18591. Loader._loaders.length = 0;
  18592. Loader._startIndex = 0;
  18593. Loader._isWorking = false;
  18594. }
  18595. endLoad(content = null) {
  18596. content && (this._data = content);
  18597. if (this._cache)
  18598. Loader.cacheRes(this._url, this._data);
  18599. this.event(Event.PROGRESS, 1);
  18600. this.event(Event.COMPLETE, this.data instanceof Array ? [this.data] : this.data);
  18601. }
  18602. get url() {
  18603. return this._url;
  18604. }
  18605. get type() {
  18606. return this._type;
  18607. }
  18608. get cache() {
  18609. return this._cache;
  18610. }
  18611. get data() {
  18612. return this._data;
  18613. }
  18614. static clearRes(url) {
  18615. url = URL.formatURL(url);
  18616. var arr = Loader.getAtlas(url);
  18617. if (arr) {
  18618. for (var i = 0, n = arr.length; i < n; i++) {
  18619. var resUrl = arr[i];
  18620. var tex = Loader.getRes(resUrl);
  18621. delete Loader.textureMap[resUrl];
  18622. if (tex)
  18623. tex.destroy();
  18624. }
  18625. arr.length = 0;
  18626. delete Loader.atlasMap[url];
  18627. }
  18628. var texture = Loader.textureMap[url];
  18629. if (texture) {
  18630. texture.destroy();
  18631. delete Loader.textureMap[url];
  18632. }
  18633. var res = Loader.loadedMap[url];
  18634. (res) && (delete Loader.loadedMap[url]);
  18635. }
  18636. static clearTextureRes(url) {
  18637. url = URL.formatURL(url);
  18638. var arr = Loader.getAtlas(url);
  18639. if (arr && arr.length > 0) {
  18640. arr.forEach(function (t) {
  18641. var tex = Loader.getRes(t);
  18642. if (tex instanceof Texture) {
  18643. tex.disposeBitmap();
  18644. }
  18645. });
  18646. }
  18647. else {
  18648. var t = Loader.getRes(url);
  18649. if (t instanceof Texture) {
  18650. t.disposeBitmap();
  18651. }
  18652. }
  18653. }
  18654. static getRes(url) {
  18655. var res = Loader.textureMap[URL.formatURL(url)];
  18656. if (res)
  18657. return res;
  18658. else
  18659. return Loader.loadedMap[URL.formatURL(url)];
  18660. }
  18661. static getAtlas(url) {
  18662. return Loader.atlasMap[URL.formatURL(url)];
  18663. }
  18664. static cacheRes(url, data) {
  18665. url = URL.formatURL(url);
  18666. if (Loader.loadedMap[url] != null) {
  18667. console.warn("Resources already exist,is repeated loading:", url);
  18668. }
  18669. else {
  18670. if (data instanceof Texture) {
  18671. Loader.loadedMap[url] = data.bitmap;
  18672. Loader.textureMap[url] = data;
  18673. }
  18674. else {
  18675. Loader.loadedMap[url] = data;
  18676. }
  18677. }
  18678. }
  18679. static cacheTexture(url, data) {
  18680. url = URL.formatURL(url);
  18681. if (Loader.textureMap[url] != null) {
  18682. console.warn("Resources already exist,is repeated loading:", url);
  18683. }
  18684. else {
  18685. Loader.textureMap[url] = data;
  18686. }
  18687. }
  18688. static setGroup(url, group) {
  18689. if (!Loader.groupMap[group])
  18690. Loader.groupMap[group] = [];
  18691. Loader.groupMap[group].push(url);
  18692. }
  18693. static clearResByGroup(group) {
  18694. if (!Loader.groupMap[group])
  18695. return;
  18696. var arr = Loader.groupMap[group], i, len = arr.length;
  18697. for (i = 0; i < len; i++) {
  18698. Loader.clearRes(arr[i]);
  18699. }
  18700. arr.length = 0;
  18701. }
  18702. }
  18703. Loader.TEXT = "text";
  18704. Loader.JSON = "json";
  18705. Loader.PREFAB = "prefab";
  18706. Loader.XML = "xml";
  18707. Loader.BUFFER = "arraybuffer";
  18708. Loader.IMAGE = "image";
  18709. Loader.SOUND = "sound";
  18710. Loader.ATLAS = "atlas";
  18711. Loader.FONT = "font";
  18712. Loader.TTF = "ttf";
  18713. Loader.PLF = "plf";
  18714. Loader.PLFB = "plfb";
  18715. Loader.HIERARCHY = "HIERARCHY";
  18716. Loader.MESH = "MESH";
  18717. Loader.MATERIAL = "MATERIAL";
  18718. Loader.TEXTURE2D = "TEXTURE2D";
  18719. Loader.TEXTURECUBE = "TEXTURECUBE";
  18720. Loader.ANIMATIONCLIP = "ANIMATIONCLIP";
  18721. Loader.AVATAR = "AVATAR";
  18722. Loader.TERRAINHEIGHTDATA = "TERRAINHEIGHTDATA";
  18723. Loader.TERRAINRES = "TERRAIN";
  18724. 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" };
  18725. Loader.parserMap = {};
  18726. Loader.maxTimeOut = 100;
  18727. Loader.groupMap = {};
  18728. Loader.loadedMap = {};
  18729. Loader.atlasMap = {};
  18730. Loader.textureMap = {};
  18731. Loader.preLoadedMap = {};
  18732. Loader._imgCache = {};
  18733. Loader._loaders = [];
  18734. Loader._isWorking = false;
  18735. Loader._startIndex = 0;
  18736. class AtlasInfoManager {
  18737. static enable(infoFile, callback = null) {
  18738. ILaya.loader.load(infoFile, Handler.create(null, AtlasInfoManager._onInfoLoaded, [callback]), null, Loader.JSON);
  18739. }
  18740. static _onInfoLoaded(callback, data) {
  18741. var tKey;
  18742. var tPrefix;
  18743. var tArr;
  18744. var i, len;
  18745. for (tKey in data) {
  18746. tArr = data[tKey];
  18747. tPrefix = tArr[0];
  18748. tArr = tArr[1];
  18749. len = tArr.length;
  18750. for (i = 0; i < len; i++) {
  18751. AtlasInfoManager._fileLoadDic[tPrefix + tArr[i]] = tKey;
  18752. }
  18753. }
  18754. callback && callback.run();
  18755. }
  18756. static getFileLoadPath(file) {
  18757. return AtlasInfoManager._fileLoadDic[file] || file;
  18758. }
  18759. }
  18760. AtlasInfoManager._fileLoadDic = {};
  18761. class LoaderManager extends EventDispatcher {
  18762. constructor() {
  18763. super();
  18764. this.retryNum = 1;
  18765. this.retryDelay = 0;
  18766. this.maxLoader = 5;
  18767. this._loaders = [];
  18768. this._loaderCount = 0;
  18769. this._resInfos = [];
  18770. this._infoPool = [];
  18771. this._maxPriority = 5;
  18772. this._failRes = {};
  18773. this._statInfo = { count: 1, loaded: 1 };
  18774. for (var i = 0; i < this._maxPriority; i++)
  18775. this._resInfos[i] = [];
  18776. }
  18777. getProgress() {
  18778. return this._statInfo.loaded / this._statInfo.count;
  18779. }
  18780. resetProgress() {
  18781. this._statInfo.count = this._statInfo.loaded = 1;
  18782. }
  18783. create(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  18784. this._create(url, true, complete, progress, type, constructParams, propertyParams, priority, cache);
  18785. }
  18786. _create(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  18787. if (url instanceof Array) {
  18788. var allScuess = true;
  18789. var items = url;
  18790. var itemCount = items.length;
  18791. var loadedCount = 0;
  18792. if (progress) {
  18793. var progress2 = Handler.create(progress.caller, progress ? progress.method : null, progress.args, false);
  18794. }
  18795. for (var i = 0; i < itemCount; i++) {
  18796. var item = items[i];
  18797. if (typeof (item) == 'string')
  18798. item = items[i] = { url: item };
  18799. item.progress = 0;
  18800. }
  18801. for (i = 0; i < itemCount; i++) {
  18802. item = items[i];
  18803. var progressHandler = progress ? Handler.create(null, function (item, value) {
  18804. item.progress = value;
  18805. var num = 0;
  18806. for (var j = 0; j < itemCount; j++) {
  18807. var item1 = items[j];
  18808. num += item1.progress;
  18809. }
  18810. var v = num / itemCount;
  18811. progress2.runWith(v);
  18812. }, [item], false) : null;
  18813. var completeHandler = (progress || complete) ? Handler.create(null, function (item, content = null) {
  18814. loadedCount++;
  18815. item.progress = 1;
  18816. content || (allScuess = false);
  18817. if (loadedCount === itemCount && complete) {
  18818. complete.runWith(allScuess);
  18819. }
  18820. }, [item]) : null;
  18821. this._createOne(item.url, mainResou, completeHandler, progressHandler, item.type || type, item.constructParams || constructParams, item.propertyParams || propertyParams, item.priority || priority, cache);
  18822. }
  18823. }
  18824. else {
  18825. this._createOne(url, mainResou, complete, progress, type, constructParams, propertyParams, priority, cache);
  18826. }
  18827. }
  18828. _createOne(url, mainResou, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true) {
  18829. var item = this.getRes(url);
  18830. if (!item) {
  18831. var extension = Utils.getFileExtension(url);
  18832. (type) || (type = LoaderManager.createMap[extension] ? LoaderManager.createMap[extension][0] : null);
  18833. if (!type) {
  18834. this.load(url, complete, progress, type, priority, cache);
  18835. return;
  18836. }
  18837. var parserMap = Loader.parserMap;
  18838. if (!parserMap[type]) {
  18839. this.load(url, complete, progress, type, priority, cache);
  18840. return;
  18841. }
  18842. this._createLoad(url, Handler.create(null, function (createRes) {
  18843. if (createRes) {
  18844. if (!mainResou && createRes instanceof Resource)
  18845. createRes._addReference();
  18846. createRes._setCreateURL(url);
  18847. }
  18848. complete && complete.runWith(createRes);
  18849. ILaya.loader.event(url);
  18850. }), progress, type, constructParams, propertyParams, priority, cache, true);
  18851. }
  18852. else {
  18853. if (!mainResou && item instanceof Resource)
  18854. item._addReference();
  18855. progress && progress.runWith(1);
  18856. complete && complete.runWith(item);
  18857. }
  18858. }
  18859. load(url, complete = null, progress = null, type = null, priority = 1, cache = true, group = null, ignoreCache = false, useWorkerLoader = ILaya.WorkerLoader.enable) {
  18860. if (url instanceof Array) {
  18861. return this._loadAssets(url, complete, progress, type, priority, cache, group);
  18862. }
  18863. var content;
  18864. if (type === Loader.IMAGE)
  18865. content = Loader.textureMap[URL.formatURL(url)];
  18866. else
  18867. content = Loader.loadedMap[URL.formatURL(url)];
  18868. if (!ignoreCache && content != null) {
  18869. ILaya.systemTimer.frameOnce(1, this, function () {
  18870. progress && progress.runWith(1);
  18871. complete && complete.runWith(content instanceof Array ? [content] : content);
  18872. this._loaderCount || this.event(Event.COMPLETE);
  18873. });
  18874. }
  18875. else {
  18876. var original;
  18877. original = url;
  18878. url = AtlasInfoManager.getFileLoadPath(url);
  18879. if (url != original && type !== "nativeimage") {
  18880. type = Loader.ATLAS;
  18881. }
  18882. else {
  18883. original = null;
  18884. }
  18885. var info = LoaderManager._resMap[url];
  18886. if (!info) {
  18887. info = this._infoPool.length ? this._infoPool.pop() : new ResInfo();
  18888. info.url = url;
  18889. info.type = type;
  18890. info.cache = cache;
  18891. info.group = group;
  18892. info.ignoreCache = ignoreCache;
  18893. info.useWorkerLoader = useWorkerLoader;
  18894. info.originalUrl = original;
  18895. complete && info.on(Event.COMPLETE, complete.caller, complete.method, complete.args);
  18896. progress && info.on(Event.PROGRESS, progress.caller, progress.method, progress.args);
  18897. LoaderManager._resMap[url] = info;
  18898. priority = priority < this._maxPriority ? priority : this._maxPriority - 1;
  18899. this._resInfos[priority].push(info);
  18900. this._statInfo.count++;
  18901. this.event(Event.PROGRESS, this.getProgress());
  18902. this._next();
  18903. }
  18904. else {
  18905. if (complete) {
  18906. if (original) {
  18907. complete && info._createListener(Event.COMPLETE, this, this._resInfoLoaded, [original, complete], false, false);
  18908. }
  18909. else {
  18910. complete && info._createListener(Event.COMPLETE, complete.caller, complete.method, complete.args, false, false);
  18911. }
  18912. }
  18913. progress && info._createListener(Event.PROGRESS, progress.caller, progress.method, progress.args, false, false);
  18914. }
  18915. }
  18916. return this;
  18917. }
  18918. _resInfoLoaded(original, complete) {
  18919. complete.runWith(Loader.getRes(original));
  18920. }
  18921. _createLoad(url, complete = null, progress = null, type = null, constructParams = null, propertyParams = null, priority = 1, cache = true, ignoreCache = false) {
  18922. if (url instanceof Array)
  18923. return this._loadAssets(url, complete, progress, type, priority, cache);
  18924. var content = Loader.getRes(url);
  18925. if (content != null) {
  18926. ILaya.systemTimer.frameOnce(1, this, function () {
  18927. progress && progress.runWith(1);
  18928. complete && complete.runWith(content);
  18929. this._loaderCount || this.event(Event.COMPLETE);
  18930. });
  18931. }
  18932. else {
  18933. var info = LoaderManager._resMap[url];
  18934. if (!info) {
  18935. info = this._infoPool.length ? this._infoPool.pop() : new ResInfo();
  18936. info.url = url;
  18937. info.type = type;
  18938. info.cache = false;
  18939. info.ignoreCache = ignoreCache;
  18940. info.originalUrl = null;
  18941. info.group = null;
  18942. info.createCache = cache;
  18943. info.createConstructParams = constructParams;
  18944. info.createPropertyParams = propertyParams;
  18945. complete && info.on(Event.COMPLETE, complete.caller, complete.method, complete.args);
  18946. progress && info.on(Event.PROGRESS, progress.caller, progress.method, progress.args);
  18947. LoaderManager._resMap[url] = info;
  18948. priority = priority < this._maxPriority ? priority : this._maxPriority - 1;
  18949. this._resInfos[priority].push(info);
  18950. this._statInfo.count++;
  18951. this.event(Event.PROGRESS, this.getProgress());
  18952. this._next();
  18953. }
  18954. else {
  18955. complete && info._createListener(Event.COMPLETE, complete.caller, complete.method, complete.args, false, false);
  18956. progress && info._createListener(Event.PROGRESS, progress.caller, progress.method, progress.args, false, false);
  18957. }
  18958. }
  18959. return this;
  18960. }
  18961. _next() {
  18962. if (this._loaderCount >= this.maxLoader)
  18963. return;
  18964. for (var i = 0; i < this._maxPriority; i++) {
  18965. var infos = this._resInfos[i];
  18966. while (infos.length > 0) {
  18967. var info = infos.shift();
  18968. if (info)
  18969. return this._doLoad(info);
  18970. }
  18971. }
  18972. this._loaderCount || this.event(Event.COMPLETE);
  18973. }
  18974. _doLoad(resInfo) {
  18975. this._loaderCount++;
  18976. var loader = this._loaders.length ? this._loaders.pop() : new Loader();
  18977. loader.on(Event.COMPLETE, null, onLoaded);
  18978. loader.on(Event.PROGRESS, null, function (num) {
  18979. resInfo.event(Event.PROGRESS, num);
  18980. });
  18981. loader.on(Event.ERROR, null, function (msg) {
  18982. onLoaded(null);
  18983. });
  18984. var _me = this;
  18985. function onLoaded(data = null) {
  18986. loader.offAll();
  18987. loader._data = null;
  18988. loader._customParse = false;
  18989. _me._loaders.push(loader);
  18990. _me._endLoad(resInfo, data instanceof Array ? [data] : data);
  18991. _me._loaderCount--;
  18992. _me._next();
  18993. }
  18994. loader._constructParams = resInfo.createConstructParams;
  18995. loader._propertyParams = resInfo.createPropertyParams;
  18996. loader._createCache = resInfo.createCache;
  18997. loader.load(resInfo.url, resInfo.type, resInfo.cache, resInfo.group, resInfo.ignoreCache, resInfo.useWorkerLoader);
  18998. }
  18999. _endLoad(resInfo, content) {
  19000. var url = resInfo.url;
  19001. if (content == null) {
  19002. var errorCount = this._failRes[url] || 0;
  19003. if (errorCount < this.retryNum) {
  19004. console.warn("[warn]Retry to load:", url);
  19005. this._failRes[url] = errorCount + 1;
  19006. ILaya.systemTimer.once(this.retryDelay, this, this._addReTry, [resInfo], false);
  19007. return;
  19008. }
  19009. else {
  19010. Loader.clearRes(url);
  19011. console.warn("[error]Failed to load:", url);
  19012. this.event(Event.ERROR, url);
  19013. }
  19014. }
  19015. if (this._failRes[url])
  19016. this._failRes[url] = 0;
  19017. delete LoaderManager._resMap[url];
  19018. if (resInfo.originalUrl) {
  19019. content = Loader.getRes(resInfo.originalUrl);
  19020. }
  19021. resInfo.event(Event.COMPLETE, content);
  19022. resInfo.offAll();
  19023. this._infoPool.push(resInfo);
  19024. this._statInfo.loaded++;
  19025. this.event(Event.PROGRESS, this.getProgress());
  19026. }
  19027. _addReTry(resInfo) {
  19028. this._resInfos[this._maxPriority - 1].push(resInfo);
  19029. this._next();
  19030. }
  19031. clearRes(url) {
  19032. Loader.clearRes(url);
  19033. }
  19034. clearTextureRes(url) {
  19035. Loader.clearTextureRes(url);
  19036. }
  19037. getRes(url) {
  19038. return Loader.getRes(url);
  19039. }
  19040. cacheRes(url, data) {
  19041. Loader.cacheRes(url, data);
  19042. }
  19043. setGroup(url, group) {
  19044. Loader.setGroup(url, group);
  19045. }
  19046. clearResByGroup(group) {
  19047. Loader.clearResByGroup(group);
  19048. }
  19049. static cacheRes(url, data) {
  19050. Loader.cacheRes(url, data);
  19051. }
  19052. clearUnLoaded() {
  19053. for (var i = 0; i < this._maxPriority; i++) {
  19054. var infos = this._resInfos[i];
  19055. for (var j = infos.length - 1; j > -1; j--) {
  19056. var info = infos[j];
  19057. if (info) {
  19058. info.offAll();
  19059. this._infoPool.push(info);
  19060. }
  19061. }
  19062. infos.length = 0;
  19063. }
  19064. this._loaderCount = 0;
  19065. LoaderManager._resMap = {};
  19066. }
  19067. cancelLoadByUrls(urls) {
  19068. if (!urls)
  19069. return;
  19070. for (var i = 0, n = urls.length; i < n; i++) {
  19071. this.cancelLoadByUrl(urls[i]);
  19072. }
  19073. }
  19074. cancelLoadByUrl(url) {
  19075. for (var i = 0; i < this._maxPriority; i++) {
  19076. var infos = this._resInfos[i];
  19077. for (var j = infos.length - 1; j > -1; j--) {
  19078. var info = infos[j];
  19079. if (info && info.url === url) {
  19080. infos[j] = null;
  19081. info.offAll();
  19082. this._infoPool.push(info);
  19083. }
  19084. }
  19085. }
  19086. if (LoaderManager._resMap[url])
  19087. delete LoaderManager._resMap[url];
  19088. }
  19089. _loadAssets(arr, complete = null, progress = null, type = null, priority = 1, cache = true, group = null) {
  19090. var itemCount = arr.length;
  19091. var loadedCount = 0;
  19092. var totalSize = 0;
  19093. var items = [];
  19094. var success = true;
  19095. for (var i = 0; i < itemCount; i++) {
  19096. let url = arr[i];
  19097. let item;
  19098. if (typeof (url) == 'string')
  19099. item = { url: url, type: type, size: 1, priority: priority };
  19100. else
  19101. item = url;
  19102. if (!item.size)
  19103. item.size = 1;
  19104. item.progress = 0;
  19105. totalSize += item.size;
  19106. items.push(item);
  19107. var progressHandler = progress ? Handler.create(null, loadProgress, [item], false) : null;
  19108. var completeHandler = (complete || progress) ? Handler.create(null, loadComplete, [item]) : null;
  19109. this.load(item.url, completeHandler, progressHandler, item.type, item.priority || 1, cache, item.group || group, false, item.useWorkerLoader);
  19110. }
  19111. function loadComplete(item, content = null) {
  19112. loadedCount++;
  19113. item.progress = 1;
  19114. if (!content)
  19115. success = false;
  19116. if (loadedCount === itemCount && complete) {
  19117. complete.runWith(success);
  19118. }
  19119. }
  19120. function loadProgress(item, value) {
  19121. if (progress != null) {
  19122. item.progress = value;
  19123. var num = 0;
  19124. for (var j = 0; j < items.length; j++) {
  19125. var item1 = items[j];
  19126. if (item1) {
  19127. let prog = item1.progress == undefined ? 0 : item1.progress;
  19128. num += item1.size == undefined ? 0 : item1.size * prog;
  19129. }
  19130. }
  19131. var v = num / totalSize;
  19132. progress.runWith(v);
  19133. }
  19134. }
  19135. return this;
  19136. }
  19137. decodeBitmaps(urls) {
  19138. var i, len = urls.length;
  19139. var ctx;
  19140. ctx = ILaya.Render._context;
  19141. for (i = 0; i < len; i++) {
  19142. var atlas;
  19143. atlas = Loader.getAtlas(urls[i]);
  19144. if (atlas) {
  19145. this._decodeTexture(atlas[0], ctx);
  19146. }
  19147. else {
  19148. var tex;
  19149. tex = this.getRes(urls[i]);
  19150. if (tex && tex instanceof Texture) {
  19151. this._decodeTexture(tex, ctx);
  19152. }
  19153. }
  19154. }
  19155. }
  19156. _decodeTexture(tex, ctx) {
  19157. var bitmap = tex.bitmap;
  19158. if (!tex || !bitmap)
  19159. return;
  19160. var tImg = bitmap.source || bitmap.image;
  19161. if (!tImg)
  19162. return;
  19163. if (tImg instanceof HTMLImageElement) {
  19164. ctx.drawImage(tImg, 0, 0, 1, 1);
  19165. var info = ctx.getImageData(0, 0, 1, 1);
  19166. }
  19167. }
  19168. }
  19169. LoaderManager._resMap = {};
  19170. LoaderManager.createMap = { atlas: [null, Loader.ATLAS] };
  19171. class ResInfo extends EventDispatcher {
  19172. }
  19173. class LocalStorage {
  19174. static __init__() {
  19175. if (!LocalStorage._baseClass) {
  19176. LocalStorage._baseClass = Storage;
  19177. Storage.init();
  19178. }
  19179. LocalStorage.items = LocalStorage._baseClass.items;
  19180. LocalStorage.support = LocalStorage._baseClass.support;
  19181. return LocalStorage.support;
  19182. }
  19183. static setItem(key, value) {
  19184. LocalStorage._baseClass.setItem(key, value);
  19185. }
  19186. static getItem(key) {
  19187. return LocalStorage._baseClass.getItem(key);
  19188. }
  19189. static setJSON(key, value) {
  19190. LocalStorage._baseClass.setJSON(key, value);
  19191. }
  19192. static getJSON(key) {
  19193. return LocalStorage._baseClass.getJSON(key);
  19194. }
  19195. static removeItem(key) {
  19196. LocalStorage._baseClass.removeItem(key);
  19197. }
  19198. static clear() {
  19199. LocalStorage._baseClass.clear();
  19200. }
  19201. }
  19202. LocalStorage.support = false;
  19203. class Storage {
  19204. static init() {
  19205. try {
  19206. Storage.support = true;
  19207. Storage.items = window.localStorage;
  19208. Storage.setItem('laya', '1');
  19209. Storage.removeItem('laya');
  19210. }
  19211. catch (e) {
  19212. Storage.support = false;
  19213. }
  19214. if (!Storage.support)
  19215. console.log('LocalStorage is not supprot or browser is private mode.');
  19216. }
  19217. static setItem(key, value) {
  19218. try {
  19219. Storage.support && Storage.items.setItem(key, value);
  19220. }
  19221. catch (e) {
  19222. console.warn("set localStorage failed", e);
  19223. }
  19224. }
  19225. static getItem(key) {
  19226. return Storage.support ? Storage.items.getItem(key) : null;
  19227. }
  19228. static setJSON(key, value) {
  19229. try {
  19230. Storage.support && Storage.items.setItem(key, JSON.stringify(value));
  19231. }
  19232. catch (e) {
  19233. console.warn("set localStorage failed", e);
  19234. }
  19235. }
  19236. static getJSON(key) {
  19237. try {
  19238. let obj = JSON.parse(Storage.support ? Storage.items.getItem(key) : null);
  19239. return obj;
  19240. }
  19241. catch (err) {
  19242. return Storage.items.getItem(key);
  19243. }
  19244. }
  19245. static removeItem(key) {
  19246. Storage.support && Storage.items.removeItem(key);
  19247. }
  19248. static clear() {
  19249. Storage.support && Storage.items.clear();
  19250. }
  19251. }
  19252. Storage.support = false;
  19253. class TTFLoader {
  19254. load(fontPath) {
  19255. this._url = fontPath;
  19256. var tArr = fontPath.split(".ttf")[0].split("/");
  19257. this.fontName = tArr[tArr.length - 1];
  19258. if (ILaya.Render.isConchApp) {
  19259. this._loadConch();
  19260. }
  19261. else if (window.FontFace) {
  19262. this._loadWithFontFace();
  19263. }
  19264. else {
  19265. this._loadWithCSS();
  19266. }
  19267. }
  19268. _loadConch() {
  19269. this._http = new HttpRequest();
  19270. this._http.on(Event.ERROR, this, this._onErr);
  19271. this._http.on(Event.COMPLETE, this, this._onHttpLoaded);
  19272. this._http.send(this._url, null, "get", Loader.BUFFER);
  19273. }
  19274. _onHttpLoaded(data = null) {
  19275. window["conchTextCanvas"].setFontFaceFromBuffer(this.fontName, data);
  19276. this._clearHttp();
  19277. this._complete();
  19278. }
  19279. _clearHttp() {
  19280. if (this._http) {
  19281. this._http.off(Event.ERROR, this, this._onErr);
  19282. this._http.off(Event.COMPLETE, this, this._onHttpLoaded);
  19283. this._http = null;
  19284. }
  19285. }
  19286. _onErr() {
  19287. this._clearHttp();
  19288. if (this.err) {
  19289. this.err.runWith("fail:" + this._url);
  19290. this.err = null;
  19291. }
  19292. }
  19293. _complete() {
  19294. ILaya.systemTimer.clear(this, this._complete);
  19295. ILaya.systemTimer.clear(this, this._checkComplete);
  19296. if (this._div && this._div.parentNode) {
  19297. this._div.parentNode.removeChild(this._div);
  19298. this._div = null;
  19299. }
  19300. if (this.complete) {
  19301. this.complete.runWith(this);
  19302. this.complete = null;
  19303. }
  19304. }
  19305. _checkComplete() {
  19306. if (ILaya.Browser.measureText(TTFLoader._testString, this._fontTxt).width != this._txtWidth) {
  19307. this._complete();
  19308. }
  19309. }
  19310. _loadWithFontFace() {
  19311. var fontFace = new window.FontFace(this.fontName, "url('" + this._url + "')");
  19312. document.fonts.add(fontFace);
  19313. var self = this;
  19314. fontFace.loaded.then((function () {
  19315. self._complete();
  19316. }));
  19317. fontFace.load();
  19318. }
  19319. _createDiv() {
  19320. this._div = Browser.createElement("div");
  19321. this._div.innerHTML = "laya";
  19322. var _style = this._div.style;
  19323. _style.fontFamily = this.fontName;
  19324. _style.position = "absolute";
  19325. _style.left = "-100px";
  19326. _style.top = "-100px";
  19327. document.body.appendChild(this._div);
  19328. }
  19329. _loadWithCSS() {
  19330. var fontStyle = Browser.createElement("style");
  19331. fontStyle.type = "text/css";
  19332. document.body.appendChild(fontStyle);
  19333. fontStyle.textContent = "@font-face { font-family:'" + this.fontName + "'; src:url('" + this._url + "');}";
  19334. this._fontTxt = "40px " + this.fontName;
  19335. this._txtWidth = Browser.measureText(TTFLoader._testString, this._fontTxt).width;
  19336. var self = this;
  19337. fontStyle.onload = function () {
  19338. ILaya.systemTimer.once(10000, self, this._complete);
  19339. };
  19340. ILaya.systemTimer.loop(20, this, this._checkComplete);
  19341. this._createDiv();
  19342. }
  19343. }
  19344. TTFLoader._testString = "LayaTTFFont";
  19345. class Ease {
  19346. static linearNone(t, b, c, d) {
  19347. return c * t / d + b;
  19348. }
  19349. static linearIn(t, b, c, d) {
  19350. return c * t / d + b;
  19351. }
  19352. static linearInOut(t, b, c, d) {
  19353. return c * t / d + b;
  19354. }
  19355. static linearOut(t, b, c, d) {
  19356. return c * t / d + b;
  19357. }
  19358. static bounceIn(t, b, c, d) {
  19359. return c - Ease.bounceOut(d - t, 0, c, d) + b;
  19360. }
  19361. static bounceInOut(t, b, c, d) {
  19362. if (t < d * 0.5)
  19363. return Ease.bounceIn(t * 2, 0, c, d) * .5 + b;
  19364. else
  19365. return Ease.bounceOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
  19366. }
  19367. static bounceOut(t, b, c, d) {
  19368. if ((t /= d) < (1 / 2.75))
  19369. return c * (7.5625 * t * t) + b;
  19370. else if (t < (2 / 2.75))
  19371. return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
  19372. else if (t < (2.5 / 2.75))
  19373. return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
  19374. else
  19375. return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
  19376. }
  19377. static backIn(t, b, c, d, s = 1.70158) {
  19378. return c * (t /= d) * t * ((s + 1) * t - s) + b;
  19379. }
  19380. static backInOut(t, b, c, d, s = 1.70158) {
  19381. if ((t /= d * 0.5) < 1)
  19382. return c * 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
  19383. return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
  19384. }
  19385. static backOut(t, b, c, d, s = 1.70158) {
  19386. return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
  19387. }
  19388. static elasticIn(t, b, c, d, a = 0, p = 0) {
  19389. var s;
  19390. if (t == 0)
  19391. return b;
  19392. if ((t /= d) == 1)
  19393. return b + c;
  19394. if (!p)
  19395. p = d * .3;
  19396. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19397. a = c;
  19398. s = p / 4;
  19399. }
  19400. else
  19401. s = p / Ease.PI2 * Math.asin(c / a);
  19402. return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p)) + b;
  19403. }
  19404. static elasticInOut(t, b, c, d, a = 0, p = 0) {
  19405. var s;
  19406. if (t == 0)
  19407. return b;
  19408. if ((t /= d * 0.5) == 2)
  19409. return b + c;
  19410. if (!p)
  19411. p = d * (.3 * 1.5);
  19412. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19413. a = c;
  19414. s = p / 4;
  19415. }
  19416. else
  19417. s = p / Ease.PI2 * Math.asin(c / a);
  19418. if (t < 1)
  19419. return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p)) + b;
  19420. return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * Ease.PI2 / p) * .5 + c + b;
  19421. }
  19422. static elasticOut(t, b, c, d, a = 0, p = 0) {
  19423. var s;
  19424. if (t == 0)
  19425. return b;
  19426. if ((t /= d) == 1)
  19427. return b + c;
  19428. if (!p)
  19429. p = d * .3;
  19430. if (!a || (c > 0 && a < c) || (c < 0 && a < -c)) {
  19431. a = c;
  19432. s = p / 4;
  19433. }
  19434. else
  19435. s = p / Ease.PI2 * Math.asin(c / a);
  19436. return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * Ease.PI2 / p) + c + b);
  19437. }
  19438. static strongIn(t, b, c, d) {
  19439. return c * (t /= d) * t * t * t * t + b;
  19440. }
  19441. static strongInOut(t, b, c, d) {
  19442. if ((t /= d * 0.5) < 1)
  19443. return c * 0.5 * t * t * t * t * t + b;
  19444. return c * 0.5 * ((t -= 2) * t * t * t * t + 2) + b;
  19445. }
  19446. static strongOut(t, b, c, d) {
  19447. return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  19448. }
  19449. static sineInOut(t, b, c, d) {
  19450. return -c * 0.5 * (Math.cos(Math.PI * t / d) - 1) + b;
  19451. }
  19452. static sineIn(t, b, c, d) {
  19453. return -c * Math.cos(t / d * Ease.HALF_PI) + c + b;
  19454. }
  19455. static sineOut(t, b, c, d) {
  19456. return c * Math.sin(t / d * Ease.HALF_PI) + b;
  19457. }
  19458. static quintIn(t, b, c, d) {
  19459. return c * (t /= d) * t * t * t * t + b;
  19460. }
  19461. static quintInOut(t, b, c, d) {
  19462. if ((t /= d * 0.5) < 1)
  19463. return c * 0.5 * t * t * t * t * t + b;
  19464. return c * 0.5 * ((t -= 2) * t * t * t * t + 2) + b;
  19465. }
  19466. static quintOut(t, b, c, d) {
  19467. return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
  19468. }
  19469. static quartIn(t, b, c, d) {
  19470. return c * (t /= d) * t * t * t + b;
  19471. }
  19472. static quartInOut(t, b, c, d) {
  19473. if ((t /= d * 0.5) < 1)
  19474. return c * 0.5 * t * t * t * t + b;
  19475. return -c * 0.5 * ((t -= 2) * t * t * t - 2) + b;
  19476. }
  19477. static quartOut(t, b, c, d) {
  19478. return -c * ((t = t / d - 1) * t * t * t - 1) + b;
  19479. }
  19480. static cubicIn(t, b, c, d) {
  19481. return c * (t /= d) * t * t + b;
  19482. }
  19483. static cubicInOut(t, b, c, d) {
  19484. if ((t /= d * 0.5) < 1)
  19485. return c * 0.5 * t * t * t + b;
  19486. return c * 0.5 * ((t -= 2) * t * t + 2) + b;
  19487. }
  19488. static cubicOut(t, b, c, d) {
  19489. return c * ((t = t / d - 1) * t * t + 1) + b;
  19490. }
  19491. static quadIn(t, b, c, d) {
  19492. return c * (t /= d) * t + b;
  19493. }
  19494. static quadInOut(t, b, c, d) {
  19495. if ((t /= d * 0.5) < 1)
  19496. return c * 0.5 * t * t + b;
  19497. return -c * 0.5 * ((--t) * (t - 2) - 1) + b;
  19498. }
  19499. static quadOut(t, b, c, d) {
  19500. return -c * (t /= d) * (t - 2) + b;
  19501. }
  19502. static expoIn(t, b, c, d) {
  19503. return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b - c * 0.001;
  19504. }
  19505. static expoInOut(t, b, c, d) {
  19506. if (t == 0)
  19507. return b;
  19508. if (t == d)
  19509. return b + c;
  19510. if ((t /= d * 0.5) < 1)
  19511. return c * 0.5 * Math.pow(2, 10 * (t - 1)) + b;
  19512. return c * 0.5 * (-Math.pow(2, -10 * --t) + 2) + b;
  19513. }
  19514. static expoOut(t, b, c, d) {
  19515. return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
  19516. }
  19517. static circIn(t, b, c, d) {
  19518. return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
  19519. }
  19520. static circInOut(t, b, c, d) {
  19521. if ((t /= d * 0.5) < 1)
  19522. return -c * 0.5 * (Math.sqrt(1 - t * t) - 1) + b;
  19523. return c * 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
  19524. }
  19525. static circOut(t, b, c, d) {
  19526. return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
  19527. }
  19528. }
  19529. Ease.HALF_PI = Math.PI * 0.5;
  19530. Ease.PI2 = Math.PI * 2;
  19531. class Tween {
  19532. constructor() {
  19533. this.gid = 0;
  19534. this.repeat = 1;
  19535. this._count = 0;
  19536. }
  19537. static to(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false, autoRecover = true) {
  19538. return Pool.getItemByClass("tween", Tween)._create(target, props, duration, ease, complete, delay, coverBefore, true, autoRecover, true);
  19539. }
  19540. static from(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false, autoRecover = true) {
  19541. return Pool.getItemByClass("tween", Tween)._create(target, props, duration, ease, complete, delay, coverBefore, false, autoRecover, true);
  19542. }
  19543. to(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false) {
  19544. return this._create(target, props, duration, ease, complete, delay, coverBefore, true, false, true);
  19545. }
  19546. from(target, props, duration, ease = null, complete = null, delay = 0, coverBefore = false) {
  19547. return this._create(target, props, duration, ease, complete, delay, coverBefore, false, false, true);
  19548. }
  19549. _create(target, props, duration, ease, complete, delay, coverBefore, isTo, usePool, runNow) {
  19550. if (!target)
  19551. throw new Error("Tween:target is null");
  19552. this._target = target;
  19553. this._duration = duration;
  19554. this._ease = ease || props.ease || Tween.easeNone;
  19555. this._complete = complete || props.complete;
  19556. this._delay = delay;
  19557. this._props = [];
  19558. this._usedTimer = 0;
  19559. this._startTimer = Browser.now();
  19560. this._usedPool = usePool;
  19561. this._delayParam = null;
  19562. this.update = props.update;
  19563. var gid = (target.$_GID || (target.$_GID = Utils.getGID()));
  19564. if (!Tween.tweenMap[gid]) {
  19565. Tween.tweenMap[gid] = [this];
  19566. }
  19567. else {
  19568. if (coverBefore)
  19569. Tween.clearTween(target);
  19570. Tween.tweenMap[gid].push(this);
  19571. }
  19572. if (runNow) {
  19573. if (delay <= 0)
  19574. this.firstStart(target, props, isTo);
  19575. else {
  19576. this._delayParam = [target, props, isTo];
  19577. ILaya.timer.once(delay, this, this.firstStart, this._delayParam);
  19578. }
  19579. }
  19580. else {
  19581. this._initProps(target, props, isTo);
  19582. }
  19583. return this;
  19584. }
  19585. firstStart(target, props, isTo) {
  19586. this._delayParam = null;
  19587. if (target.destroyed) {
  19588. this.clear();
  19589. return;
  19590. }
  19591. this._initProps(target, props, isTo);
  19592. this._beginLoop();
  19593. }
  19594. _initProps(target, props, isTo) {
  19595. for (var p in props) {
  19596. if (typeof (target[p]) == 'number') {
  19597. var start = isTo ? target[p] : props[p];
  19598. var end = isTo ? props[p] : target[p];
  19599. this._props.push([p, start, end - start]);
  19600. if (!isTo)
  19601. target[p] = start;
  19602. }
  19603. }
  19604. }
  19605. _beginLoop() {
  19606. ILaya.timer.frameLoop(1, this, this._doEase);
  19607. }
  19608. _doEase() {
  19609. this._updateEase(Browser.now());
  19610. }
  19611. _updateEase(time) {
  19612. var target = this._target;
  19613. if (!target)
  19614. return;
  19615. if (target.destroyed)
  19616. return Tween.clearTween(target);
  19617. var usedTimer = this._usedTimer = time - this._startTimer - this._delay;
  19618. if (usedTimer < 0)
  19619. return;
  19620. if (usedTimer >= this._duration)
  19621. return this.complete();
  19622. var ratio = usedTimer > 0 ? this._ease(usedTimer, 0, 1, this._duration) : 0;
  19623. var props = this._props;
  19624. for (var i = 0, n = props.length; i < n; i++) {
  19625. var prop = props[i];
  19626. target[prop[0]] = prop[1] + (ratio * prop[2]);
  19627. }
  19628. if (this.update)
  19629. this.update.run();
  19630. }
  19631. set progress(v) {
  19632. var uTime = v * this._duration;
  19633. this._startTimer = Browser.now() - this._delay - uTime;
  19634. }
  19635. complete() {
  19636. if (!this._target)
  19637. return;
  19638. ILaya.timer.runTimer(this, this.firstStart);
  19639. var target = this._target;
  19640. var props = this._props;
  19641. var handler = this._complete;
  19642. for (var i = 0, n = props.length; i < n; i++) {
  19643. var prop = props[i];
  19644. target[prop[0]] = prop[1] + prop[2];
  19645. }
  19646. if (this.update)
  19647. this.update.run();
  19648. this._count++;
  19649. if (this.repeat != 0 && this._count >= this.repeat) {
  19650. this.clear();
  19651. handler && handler.run();
  19652. }
  19653. else {
  19654. this.restart();
  19655. }
  19656. }
  19657. pause() {
  19658. ILaya.timer.clear(this, this._beginLoop);
  19659. ILaya.timer.clear(this, this._doEase);
  19660. ILaya.timer.clear(this, this.firstStart);
  19661. var time = Browser.now();
  19662. var dTime;
  19663. dTime = time - this._startTimer - this._delay;
  19664. if (dTime < 0) {
  19665. this._usedTimer = dTime;
  19666. }
  19667. }
  19668. setStartTime(startTime) {
  19669. this._startTimer = startTime;
  19670. }
  19671. static clearAll(target) {
  19672. if (!target || !target.$_GID)
  19673. return;
  19674. var tweens = Tween.tweenMap[target.$_GID];
  19675. if (tweens) {
  19676. for (var i = 0, n = tweens.length; i < n; i++) {
  19677. tweens[i]._clear();
  19678. }
  19679. tweens.length = 0;
  19680. }
  19681. }
  19682. static clear(tween) {
  19683. tween.clear();
  19684. }
  19685. static clearTween(target) {
  19686. Tween.clearAll(target);
  19687. }
  19688. clear() {
  19689. if (this._target) {
  19690. this._remove();
  19691. this._clear();
  19692. }
  19693. }
  19694. _clear() {
  19695. this.pause();
  19696. ILaya.timer.clear(this, this.firstStart);
  19697. this._complete = null;
  19698. this._target = null;
  19699. this._ease = null;
  19700. this._props = null;
  19701. this._delayParam = null;
  19702. this.repeat = 1;
  19703. if (this._usedPool) {
  19704. this.update = null;
  19705. Pool.recover("tween", this);
  19706. }
  19707. }
  19708. recover() {
  19709. this._usedPool = true;
  19710. this._clear();
  19711. }
  19712. _remove() {
  19713. var tweens = Tween.tweenMap[this._target.$_GID];
  19714. if (tweens) {
  19715. for (var i = 0, n = tweens.length; i < n; i++) {
  19716. if (tweens[i] === this) {
  19717. tweens.splice(i, 1);
  19718. break;
  19719. }
  19720. }
  19721. }
  19722. }
  19723. restart() {
  19724. this.pause();
  19725. this._usedTimer = 0;
  19726. this._startTimer = Browser.now();
  19727. if (this._delayParam) {
  19728. ILaya.timer.once(this._delay, this, this.firstStart, this._delayParam);
  19729. return;
  19730. }
  19731. var props = this._props;
  19732. for (var i = 0, n = props.length; i < n; i++) {
  19733. var prop = props[i];
  19734. this._target[prop[0]] = prop[1];
  19735. }
  19736. ILaya.timer.once(this._delay, this, this._beginLoop);
  19737. }
  19738. resume() {
  19739. if (this._usedTimer >= this._duration)
  19740. return;
  19741. this._startTimer = Browser.now() - this._usedTimer - this._delay;
  19742. if (this._delayParam) {
  19743. if (this._usedTimer < 0) {
  19744. ILaya.timer.once(-this._usedTimer, this, this.firstStart, this._delayParam);
  19745. }
  19746. else {
  19747. this.firstStart.apply(this, this._delayParam);
  19748. }
  19749. }
  19750. else {
  19751. this._beginLoop();
  19752. }
  19753. }
  19754. static easeNone(t, b, c, d) {
  19755. return c * t / d + b;
  19756. }
  19757. }
  19758. Tween.tweenMap = [];
  19759. class Dragging {
  19760. constructor() {
  19761. this.ratio = 0.92;
  19762. this.maxOffset = 60;
  19763. this._dragging = false;
  19764. this._clickOnly = true;
  19765. }
  19766. start(target, area, hasInertia, elasticDistance, elasticBackTime, data, disableMouseEvent, ratio = 0.92) {
  19767. this.clearTimer();
  19768. this.target = target;
  19769. this.area = area;
  19770. this.hasInertia = hasInertia;
  19771. this.elasticDistance = area ? elasticDistance : 0;
  19772. this.elasticBackTime = elasticBackTime;
  19773. this.data = data;
  19774. this._disableMouseEvent = disableMouseEvent;
  19775. this.ratio = ratio;
  19776. this._parent = target.parent;
  19777. this._clickOnly = true;
  19778. this._dragging = true;
  19779. this._elasticRateX = this._elasticRateY = 1;
  19780. this._lastX = this._parent.mouseX;
  19781. this._lastY = this._parent.mouseY;
  19782. ILaya.stage.on(Event.MOUSE_UP, this, this.onStageMouseUp);
  19783. ILaya.stage.on(Event.MOUSE_OUT, this, this.onStageMouseUp);
  19784. ILaya.systemTimer.frameLoop(1, this, this.loop);
  19785. }
  19786. clearTimer() {
  19787. ILaya.systemTimer.clear(this, this.loop);
  19788. ILaya.systemTimer.clear(this, this.tweenMove);
  19789. if (this._tween) {
  19790. this._tween.recover();
  19791. this._tween = null;
  19792. }
  19793. }
  19794. stop() {
  19795. if (this._dragging) {
  19796. MouseManager.instance.disableMouseEvent = false;
  19797. ILaya.stage.off(Event.MOUSE_UP, this, this.onStageMouseUp);
  19798. ILaya.stage.off(Event.MOUSE_OUT, this, this.onStageMouseUp);
  19799. this._dragging = false;
  19800. this.target && this.area && this.backToArea();
  19801. this.clear();
  19802. }
  19803. }
  19804. loop() {
  19805. var point = this._parent.getMousePoint();
  19806. var mouseX = point.x;
  19807. var mouseY = point.y;
  19808. var offsetX = mouseX - this._lastX;
  19809. var offsetY = mouseY - this._lastY;
  19810. if (this._clickOnly) {
  19811. if (Math.abs(offsetX * ILaya.stage._canvasTransform.getScaleX()) > 1 || Math.abs(offsetY * ILaya.stage._canvasTransform.getScaleY()) > 1) {
  19812. this._clickOnly = false;
  19813. this._offsets || (this._offsets = []);
  19814. this._offsets.length = 0;
  19815. this.target.event(Event.DRAG_START, this.data);
  19816. MouseManager.instance.disableMouseEvent = this._disableMouseEvent;
  19817. }
  19818. else
  19819. return;
  19820. }
  19821. else {
  19822. this._offsets.push(offsetX, offsetY);
  19823. }
  19824. if (offsetX === 0 && offsetY === 0)
  19825. return;
  19826. this._lastX = mouseX;
  19827. this._lastY = mouseY;
  19828. this.target.x += offsetX * this._elasticRateX;
  19829. this.target.y += offsetY * this._elasticRateY;
  19830. this.area && this.checkArea();
  19831. this.target.event(Event.DRAG_MOVE, this.data);
  19832. }
  19833. checkArea() {
  19834. if (this.elasticDistance <= 0) {
  19835. this.backToArea();
  19836. }
  19837. else {
  19838. if (this.target._x < this.area.x) {
  19839. var offsetX = this.area.x - this.target._x;
  19840. }
  19841. else if (this.target._x > this.area.x + this.area.width) {
  19842. offsetX = this.target._x - this.area.x - this.area.width;
  19843. }
  19844. else {
  19845. offsetX = 0;
  19846. }
  19847. this._elasticRateX = Math.max(0, 1 - (offsetX / this.elasticDistance));
  19848. if (this.target._y < this.area.y) {
  19849. var offsetY = this.area.y - this.target.y;
  19850. }
  19851. else if (this.target._y > this.area.y + this.area.height) {
  19852. offsetY = this.target._y - this.area.y - this.area.height;
  19853. }
  19854. else {
  19855. offsetY = 0;
  19856. }
  19857. this._elasticRateY = Math.max(0, 1 - (offsetY / this.elasticDistance));
  19858. }
  19859. }
  19860. backToArea() {
  19861. this.target.x = Math.min(Math.max(this.target._x, this.area.x), this.area.x + this.area.width);
  19862. this.target.y = Math.min(Math.max(this.target._y, this.area.y), this.area.y + this.area.height);
  19863. }
  19864. onStageMouseUp(e) {
  19865. MouseManager.instance.disableMouseEvent = false;
  19866. ILaya.stage.off(Event.MOUSE_UP, this, this.onStageMouseUp);
  19867. ILaya.stage.off(Event.MOUSE_OUT, this, this.onStageMouseUp);
  19868. ILaya.systemTimer.clear(this, this.loop);
  19869. if (this._clickOnly || !this.target)
  19870. return;
  19871. if (this.hasInertia) {
  19872. if (this._offsets.length < 1) {
  19873. this._offsets.push(this._parent.mouseX - this._lastX, this._parent.mouseY - this._lastY);
  19874. }
  19875. this._offsetX = this._offsetY = 0;
  19876. var len = this._offsets.length;
  19877. var n = Math.min(len, 6);
  19878. var m = this._offsets.length - n;
  19879. for (var i = len - 1; i > m; i--) {
  19880. this._offsetY += this._offsets[i--];
  19881. this._offsetX += this._offsets[i];
  19882. }
  19883. this._offsetX = this._offsetX / n * 2;
  19884. this._offsetY = this._offsetY / n * 2;
  19885. if (Math.abs(this._offsetX) > this.maxOffset)
  19886. this._offsetX = this._offsetX > 0 ? this.maxOffset : -this.maxOffset;
  19887. if (Math.abs(this._offsetY) > this.maxOffset)
  19888. this._offsetY = this._offsetY > 0 ? this.maxOffset : -this.maxOffset;
  19889. ILaya.systemTimer.frameLoop(1, this, this.tweenMove);
  19890. }
  19891. else if (this.elasticDistance > 0) {
  19892. this.checkElastic();
  19893. }
  19894. else {
  19895. this.clear();
  19896. }
  19897. }
  19898. checkElastic() {
  19899. var tx = NaN;
  19900. var ty = NaN;
  19901. if (this.target.x < this.area.x)
  19902. tx = this.area.x;
  19903. else if (this.target._x > this.area.x + this.area.width)
  19904. tx = this.area.x + this.area.width;
  19905. if (this.target.y < this.area.y)
  19906. ty = this.area.y;
  19907. else if (this.target._y > this.area.y + this.area.height)
  19908. ty = this.area.y + this.area.height;
  19909. if (!isNaN(tx) || !isNaN(ty)) {
  19910. var obj = {};
  19911. if (!isNaN(tx))
  19912. obj.x = tx;
  19913. if (!isNaN(ty))
  19914. obj.y = ty;
  19915. this._tween = Tween.to(this.target, obj, this.elasticBackTime, Ease.sineOut, Handler.create(this, this.clear), 0, false, false);
  19916. }
  19917. else {
  19918. this.clear();
  19919. }
  19920. }
  19921. tweenMove() {
  19922. this._offsetX *= this.ratio * this._elasticRateX;
  19923. this._offsetY *= this.ratio * this._elasticRateY;
  19924. this.target.x += this._offsetX;
  19925. this.target.y += this._offsetY;
  19926. this.area && this.checkArea();
  19927. this.target.event(Event.DRAG_MOVE, this.data);
  19928. if ((Math.abs(this._offsetX) < 1 && Math.abs(this._offsetY) < 1) || this._elasticRateX < 0.5 || this._elasticRateY < 0.5) {
  19929. ILaya.systemTimer.clear(this, this.tweenMove);
  19930. if (this.elasticDistance > 0)
  19931. this.checkElastic();
  19932. else
  19933. this.clear();
  19934. }
  19935. }
  19936. clear() {
  19937. if (this.target) {
  19938. this.clearTimer();
  19939. var sp = this.target;
  19940. this.target = null;
  19941. this._parent = null;
  19942. sp.event(Event.DRAG_END, this.data);
  19943. }
  19944. }
  19945. }
  19946. class Component {
  19947. constructor() {
  19948. this._id = Utils.getGID();
  19949. this._resetComp();
  19950. }
  19951. get id() {
  19952. return this._id;
  19953. }
  19954. get enabled() {
  19955. return this._enabled;
  19956. }
  19957. set enabled(value) {
  19958. if (this._enabled != value) {
  19959. this._enabled = value;
  19960. if (this.owner) {
  19961. if (value)
  19962. this.owner.activeInHierarchy && this._onEnable();
  19963. else
  19964. this.owner.activeInHierarchy && this._onDisable();
  19965. }
  19966. }
  19967. }
  19968. get isSingleton() {
  19969. return true;
  19970. }
  19971. get destroyed() {
  19972. return this._destroyed;
  19973. }
  19974. _isScript() {
  19975. return false;
  19976. }
  19977. _resetComp() {
  19978. this._indexInList = -1;
  19979. this._enabled = true;
  19980. this._awaked = false;
  19981. this.owner = null;
  19982. }
  19983. _getIndexInList() {
  19984. return this._indexInList;
  19985. }
  19986. _setIndexInList(index) {
  19987. this._indexInList = index;
  19988. }
  19989. _onAdded() {
  19990. }
  19991. _onAwake() {
  19992. }
  19993. _onEnable() {
  19994. }
  19995. _onDisable() {
  19996. }
  19997. _onDestroy() {
  19998. }
  19999. onReset() {
  20000. }
  20001. _parse(data) {
  20002. }
  20003. _cloneTo(dest) {
  20004. }
  20005. _setActive(value) {
  20006. if (value) {
  20007. if (!this._awaked) {
  20008. this._awaked = true;
  20009. this._onAwake();
  20010. }
  20011. this._enabled && this._onEnable();
  20012. }
  20013. else {
  20014. this._enabled && this._onDisable();
  20015. }
  20016. }
  20017. destroy() {
  20018. if (this.owner)
  20019. this.owner._destroyComponent(this);
  20020. }
  20021. _destroy() {
  20022. if (this.owner.activeInHierarchy && this._enabled)
  20023. this._setActive(false);
  20024. this._onDestroy();
  20025. this._destroyed = true;
  20026. if (this.onReset !== Component.prototype.onReset) {
  20027. this.onReset();
  20028. this._resetComp();
  20029. Pool.recoverByClass(this);
  20030. }
  20031. else {
  20032. this._resetComp();
  20033. }
  20034. }
  20035. }
  20036. class AnimationBase extends Sprite {
  20037. constructor() {
  20038. super();
  20039. this.wrapMode = 0;
  20040. this._interval = Config.animationInterval;
  20041. this._isReverse = false;
  20042. this._frameRateChanged = false;
  20043. this._setBitUp(Const.DISPLAY);
  20044. }
  20045. play(start = 0, loop = true, name = "") {
  20046. this._isPlaying = true;
  20047. this._actionName = name;
  20048. this.index = (typeof (start) == 'string') ? this._getFrameByLabel(start) : start;
  20049. this.loop = loop;
  20050. this._isReverse = this.wrapMode === AnimationBase.WRAP_REVERSE;
  20051. if (this.index == 0 && this._isReverse) {
  20052. this.index = this.count - 1;
  20053. }
  20054. if (this.interval > 0)
  20055. this.timerLoop(this.interval, this, this._frameLoop, null, true, true);
  20056. }
  20057. get interval() {
  20058. return this._interval;
  20059. }
  20060. set interval(value) {
  20061. if (this._interval != value) {
  20062. this._frameRateChanged = true;
  20063. this._interval = value;
  20064. if (this._isPlaying && value > 0) {
  20065. this.timerLoop(value, this, this._frameLoop, null, true, true);
  20066. }
  20067. }
  20068. }
  20069. _getFrameByLabel(label) {
  20070. for (var i = 0; i < this._count; i++) {
  20071. var item = this._labels[i];
  20072. if (item && item.indexOf(label) > -1)
  20073. return i;
  20074. }
  20075. return 0;
  20076. }
  20077. _frameLoop() {
  20078. if (this._isReverse) {
  20079. this._index--;
  20080. if (this._index < 0) {
  20081. if (this.loop) {
  20082. if (this.wrapMode == AnimationBase.WRAP_PINGPONG) {
  20083. this._index = this._count > 0 ? 1 : 0;
  20084. this._isReverse = false;
  20085. }
  20086. else {
  20087. this._index = this._count - 1;
  20088. }
  20089. this.event(Event.COMPLETE);
  20090. }
  20091. else {
  20092. this._index = 0;
  20093. this.stop();
  20094. this.event(Event.COMPLETE);
  20095. return;
  20096. }
  20097. }
  20098. }
  20099. else {
  20100. this._index++;
  20101. if (this._index >= this._count) {
  20102. if (this.loop) {
  20103. if (this.wrapMode == AnimationBase.WRAP_PINGPONG) {
  20104. this._index = this._count - 2 >= 0 ? this._count - 2 : 0;
  20105. this._isReverse = true;
  20106. }
  20107. else {
  20108. this._index = 0;
  20109. }
  20110. this.event(Event.COMPLETE);
  20111. }
  20112. else {
  20113. this._index--;
  20114. this.stop();
  20115. this.event(Event.COMPLETE);
  20116. return;
  20117. }
  20118. }
  20119. }
  20120. this.index = this._index;
  20121. }
  20122. _setControlNode(node) {
  20123. if (this._controlNode) {
  20124. this._controlNode.off(Event.DISPLAY, this, this._resumePlay);
  20125. this._controlNode.off(Event.UNDISPLAY, this, this._resumePlay);
  20126. }
  20127. this._controlNode = node;
  20128. if (node && node != this) {
  20129. node.on(Event.DISPLAY, this, this._resumePlay);
  20130. node.on(Event.UNDISPLAY, this, this._resumePlay);
  20131. }
  20132. }
  20133. _setDisplay(value) {
  20134. super._setDisplay(value);
  20135. this._resumePlay();
  20136. }
  20137. _resumePlay() {
  20138. if (this._isPlaying) {
  20139. if (this._controlNode.displayedInStage)
  20140. this.play(this._index, this.loop, this._actionName);
  20141. else
  20142. this.clearTimer(this, this._frameLoop);
  20143. }
  20144. }
  20145. stop() {
  20146. this._isPlaying = false;
  20147. this.clearTimer(this, this._frameLoop);
  20148. }
  20149. get isPlaying() {
  20150. return this._isPlaying;
  20151. }
  20152. addLabel(label, index) {
  20153. if (!this._labels)
  20154. this._labels = {};
  20155. if (!this._labels[index])
  20156. this._labels[index] = [];
  20157. this._labels[index].push(label);
  20158. }
  20159. removeLabel(label) {
  20160. if (!label)
  20161. this._labels = null;
  20162. else if (this._labels) {
  20163. for (var name in this._labels) {
  20164. this._removeLabelFromList(this._labels[name], label);
  20165. }
  20166. }
  20167. }
  20168. _removeLabelFromList(list, label) {
  20169. if (!list)
  20170. return;
  20171. for (var i = list.length - 1; i >= 0; i--) {
  20172. if (list[i] == label) {
  20173. list.splice(i, 1);
  20174. }
  20175. }
  20176. }
  20177. gotoAndStop(position) {
  20178. this.index = (typeof (position) == 'string') ? this._getFrameByLabel(position) : position;
  20179. this.stop();
  20180. }
  20181. get index() {
  20182. return this._index;
  20183. }
  20184. set index(value) {
  20185. this._index = value;
  20186. this._displayToIndex(value);
  20187. if (this._labels && this._labels[value]) {
  20188. var tArr = this._labels[value];
  20189. for (var i = 0, len = tArr.length; i < len; i++) {
  20190. this.event(Event.LABEL, tArr[i]);
  20191. }
  20192. }
  20193. }
  20194. _displayToIndex(value) {
  20195. }
  20196. get count() {
  20197. return this._count;
  20198. }
  20199. clear() {
  20200. this.stop();
  20201. this._labels = null;
  20202. return this;
  20203. }
  20204. }
  20205. AnimationBase.WRAP_POSITIVE = 0;
  20206. AnimationBase.WRAP_REVERSE = 1;
  20207. AnimationBase.WRAP_PINGPONG = 2;
  20208. ClassUtils.regClass("laya.display.AnimationBase", AnimationBase);
  20209. ClassUtils.regClass("Laya.AnimationBase", AnimationBase);
  20210. class MathUtil {
  20211. static subtractVector3(l, r, o) {
  20212. o[0] = l[0] - r[0];
  20213. o[1] = l[1] - r[1];
  20214. o[2] = l[2] - r[2];
  20215. }
  20216. static lerp(left, right, amount) {
  20217. return left * (1 - amount) + right * amount;
  20218. }
  20219. static scaleVector3(f, b, e) {
  20220. e[0] = f[0] * b;
  20221. e[1] = f[1] * b;
  20222. e[2] = f[2] * b;
  20223. }
  20224. static lerpVector3(l, r, t, o) {
  20225. var ax = l[0], ay = l[1], az = l[2];
  20226. o[0] = ax + t * (r[0] - ax);
  20227. o[1] = ay + t * (r[1] - ay);
  20228. o[2] = az + t * (r[2] - az);
  20229. }
  20230. static lerpVector4(l, r, t, o) {
  20231. var ax = l[0], ay = l[1], az = l[2], aw = l[3];
  20232. o[0] = ax + t * (r[0] - ax);
  20233. o[1] = ay + t * (r[1] - ay);
  20234. o[2] = az + t * (r[2] - az);
  20235. o[3] = aw + t * (r[3] - aw);
  20236. }
  20237. static slerpQuaternionArray(a, Offset1, b, Offset2, t, out, Offset3) {
  20238. 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];
  20239. var omega, cosom, sinom, scale0, scale1;
  20240. cosom = ax * bx + ay * by + az * bz + aw * bw;
  20241. if (cosom < 0.0) {
  20242. cosom = -cosom;
  20243. bx = -bx;
  20244. by = -by;
  20245. bz = -bz;
  20246. bw = -bw;
  20247. }
  20248. if ((1.0 - cosom) > 0.000001) {
  20249. omega = Math.acos(cosom);
  20250. sinom = Math.sin(omega);
  20251. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  20252. scale1 = Math.sin(t * omega) / sinom;
  20253. }
  20254. else {
  20255. scale0 = 1.0 - t;
  20256. scale1 = t;
  20257. }
  20258. out[Offset3 + 0] = scale0 * ax + scale1 * bx;
  20259. out[Offset3 + 1] = scale0 * ay + scale1 * by;
  20260. out[Offset3 + 2] = scale0 * az + scale1 * bz;
  20261. out[Offset3 + 3] = scale0 * aw + scale1 * bw;
  20262. return out;
  20263. }
  20264. static getRotation(x0, y0, x1, y1) {
  20265. return Math.atan2(y1 - y0, x1 - x0) / Math.PI * 180;
  20266. }
  20267. static sortBigFirst(a, b) {
  20268. if (a == b)
  20269. return 0;
  20270. return b > a ? 1 : -1;
  20271. }
  20272. static sortSmallFirst(a, b) {
  20273. if (a == b)
  20274. return 0;
  20275. return b > a ? -1 : 1;
  20276. }
  20277. static sortNumBigFirst(a, b) {
  20278. return parseFloat(b) - parseFloat(a);
  20279. }
  20280. static sortNumSmallFirst(a, b) {
  20281. return parseFloat(a) - parseFloat(b);
  20282. }
  20283. static sortByKey(key, bigFirst = false, forceNum = true) {
  20284. var _sortFun;
  20285. if (bigFirst) {
  20286. _sortFun = forceNum ? MathUtil.sortNumBigFirst : MathUtil.sortBigFirst;
  20287. }
  20288. else {
  20289. _sortFun = forceNum ? MathUtil.sortNumSmallFirst : MathUtil.sortSmallFirst;
  20290. }
  20291. return function (a, b) {
  20292. return _sortFun(a[key], b[key]);
  20293. };
  20294. }
  20295. }
  20296. class FrameAnimation extends AnimationBase {
  20297. constructor() {
  20298. super();
  20299. if (FrameAnimation._sortIndexFun === null) {
  20300. FrameAnimation._sortIndexFun = MathUtil.sortByKey("index", false, true);
  20301. }
  20302. }
  20303. _setUp(targetDic, animationData) {
  20304. this._targetDic = targetDic;
  20305. this._animationData = animationData;
  20306. this.interval = 1000 / animationData.frameRate;
  20307. if (animationData.parsed) {
  20308. this._count = animationData.count;
  20309. this._labels = animationData.labels;
  20310. this._usedFrames = animationData.animationNewFrames;
  20311. }
  20312. else {
  20313. this._usedFrames = [];
  20314. this._calculateDatas();
  20315. animationData.parsed = true;
  20316. animationData.labels = this._labels;
  20317. animationData.count = this._count;
  20318. animationData.animationNewFrames = this._usedFrames;
  20319. }
  20320. }
  20321. clear() {
  20322. super.clear();
  20323. this._targetDic = null;
  20324. this._animationData = null;
  20325. return this;
  20326. }
  20327. _displayToIndex(value) {
  20328. if (!this._animationData)
  20329. return;
  20330. if (value < 0)
  20331. value = 0;
  20332. if (value > this._count)
  20333. value = this._count;
  20334. var nodes = this._animationData.nodes, i, len = nodes.length;
  20335. for (i = 0; i < len; i++) {
  20336. this._displayNodeToFrame(nodes[i], value);
  20337. }
  20338. }
  20339. _displayNodeToFrame(node, frame, targetDic = null) {
  20340. if (!targetDic)
  20341. targetDic = this._targetDic;
  20342. var target = targetDic[node.target];
  20343. if (!target) {
  20344. return;
  20345. }
  20346. var frames = node.frames, key, propFrames, value;
  20347. var keys = node.keys, i, len = keys.length;
  20348. for (i = 0; i < len; i++) {
  20349. key = keys[i];
  20350. propFrames = frames[key];
  20351. if (propFrames.length > frame) {
  20352. value = propFrames[frame];
  20353. }
  20354. else {
  20355. value = propFrames[propFrames.length - 1];
  20356. }
  20357. target[key] = value;
  20358. }
  20359. var funkeys = node.funkeys;
  20360. len = funkeys.length;
  20361. var funFrames;
  20362. if (len == 0)
  20363. return;
  20364. for (i = 0; i < len; i++) {
  20365. key = funkeys[i];
  20366. funFrames = frames[key];
  20367. if (funFrames[frame] !== undefined) {
  20368. target[key] && target[key].apply(target, funFrames[frame]);
  20369. }
  20370. }
  20371. }
  20372. _calculateDatas() {
  20373. if (!this._animationData)
  20374. return;
  20375. var nodes = this._animationData.nodes, i, len = nodes.length, tNode;
  20376. this._count = 0;
  20377. for (i = 0; i < len; i++) {
  20378. tNode = nodes[i];
  20379. this._calculateKeyFrames(tNode);
  20380. }
  20381. this._count += 1;
  20382. }
  20383. _calculateKeyFrames(node) {
  20384. var keyFrames = node.keyframes, key, tKeyFrames, target = node.target;
  20385. if (!node.frames)
  20386. node.frames = {};
  20387. if (!node.keys)
  20388. node.keys = [];
  20389. else
  20390. node.keys.length = 0;
  20391. if (!node.funkeys)
  20392. node.funkeys = [];
  20393. else
  20394. node.funkeys.length = 0;
  20395. if (!node.initValues)
  20396. node.initValues = {};
  20397. for (key in keyFrames) {
  20398. var isFun = key.indexOf("()") != -1;
  20399. tKeyFrames = keyFrames[key];
  20400. if (isFun)
  20401. key = key.substr(0, key.length - 2);
  20402. if (!node.frames[key]) {
  20403. node.frames[key] = [];
  20404. }
  20405. if (!isFun) {
  20406. if (this._targetDic && this._targetDic[target]) {
  20407. node.initValues[key] = this._targetDic[target][key];
  20408. }
  20409. tKeyFrames.sort(FrameAnimation._sortIndexFun);
  20410. node.keys.push(key);
  20411. this._calculateNodePropFrames(tKeyFrames, node.frames[key], key, target);
  20412. }
  20413. else {
  20414. node.funkeys.push(key);
  20415. var map = node.frames[key];
  20416. for (var i = 0; i < tKeyFrames.length; i++) {
  20417. var temp = tKeyFrames[i];
  20418. map[temp.index] = temp.value;
  20419. if (temp.index > this._count)
  20420. this._count = temp.index;
  20421. }
  20422. }
  20423. }
  20424. }
  20425. resetNodes() {
  20426. if (!this._targetDic)
  20427. return;
  20428. if (!this._animationData)
  20429. return;
  20430. var nodes = this._animationData.nodes, i, len = nodes.length;
  20431. var tNode;
  20432. var initValues;
  20433. for (i = 0; i < len; i++) {
  20434. tNode = nodes[i];
  20435. initValues = tNode.initValues;
  20436. if (!initValues)
  20437. continue;
  20438. var target = this._targetDic[tNode.target];
  20439. if (!target)
  20440. continue;
  20441. var key;
  20442. for (key in initValues) {
  20443. target[key] = initValues[key];
  20444. }
  20445. }
  20446. }
  20447. _calculateNodePropFrames(keyframes, frames, key, target) {
  20448. var i, len = keyframes.length - 1;
  20449. frames.length = keyframes[len].index + 1;
  20450. for (i = 0; i < len; i++) {
  20451. this._dealKeyFrame(keyframes[i]);
  20452. this._calculateFrameValues(keyframes[i], keyframes[i + 1], frames);
  20453. }
  20454. if (len == 0) {
  20455. frames[0] = keyframes[0].value;
  20456. if (this._usedFrames)
  20457. this._usedFrames[keyframes[0].index] = true;
  20458. }
  20459. this._dealKeyFrame(keyframes[i]);
  20460. }
  20461. _dealKeyFrame(keyFrame) {
  20462. if (keyFrame.label && keyFrame.label != "")
  20463. this.addLabel(keyFrame.label, keyFrame.index);
  20464. }
  20465. _calculateFrameValues(startFrame, endFrame, result) {
  20466. var i, easeFun;
  20467. var start = startFrame.index, end = endFrame.index;
  20468. var startValue = startFrame.value;
  20469. var dValue = endFrame.value - startFrame.value;
  20470. var dLen = end - start;
  20471. var frames = this._usedFrames;
  20472. if (end > this._count)
  20473. this._count = end;
  20474. if (startFrame.tween) {
  20475. easeFun = Ease[startFrame.tweenMethod];
  20476. if (easeFun == null)
  20477. easeFun = Ease.linearNone;
  20478. for (i = start; i < end; i++) {
  20479. result[i] = easeFun(i - start, startValue, dValue, dLen);
  20480. if (frames)
  20481. frames[i] = true;
  20482. }
  20483. }
  20484. else {
  20485. for (i = start; i < end; i++) {
  20486. result[i] = startValue;
  20487. }
  20488. }
  20489. if (frames) {
  20490. frames[startFrame.index] = true;
  20491. frames[endFrame.index] = true;
  20492. }
  20493. result[endFrame.index] = endFrame.value;
  20494. }
  20495. }
  20496. ClassUtils.regClass("laya.display.FrameAnimation", FrameAnimation);
  20497. ClassUtils.regClass("Laya.FrameAnimation", FrameAnimation);
  20498. class WeakObject {
  20499. constructor() {
  20500. this._obj = {};
  20501. WeakObject._maps.push(this);
  20502. }
  20503. static __init__() {
  20504. WeakObject.I = new WeakObject();
  20505. if (!WeakObject.supportWeakMap)
  20506. ILaya.systemTimer.loop(WeakObject.delInterval, null, WeakObject.clearCache);
  20507. }
  20508. static clearCache() {
  20509. for (var i = 0, n = WeakObject._maps.length; i < n; i++) {
  20510. var obj = WeakObject._maps[i];
  20511. obj._obj = {};
  20512. }
  20513. }
  20514. set(key, value) {
  20515. if (key == null)
  20516. return;
  20517. if (WeakObject.supportWeakMap) ;
  20518. else {
  20519. if (typeof (key) == 'string' || typeof (key) == 'number') {
  20520. this._obj[key] = value;
  20521. }
  20522. else {
  20523. key.$_GID || (key.$_GID = Utils.getGID());
  20524. this._obj[key.$_GID] = value;
  20525. }
  20526. }
  20527. }
  20528. get(key) {
  20529. if (key == null)
  20530. return null;
  20531. if (WeakObject.supportWeakMap) ;
  20532. else {
  20533. if (typeof (key) == 'string' || typeof (key) == 'number')
  20534. return this._obj[key];
  20535. return this._obj[key.$_GID];
  20536. }
  20537. }
  20538. del(key) {
  20539. if (key == null)
  20540. return;
  20541. if (WeakObject.supportWeakMap) ;
  20542. else {
  20543. if (typeof (key) == 'string' || typeof (key) == 'number')
  20544. delete this._obj[key];
  20545. else
  20546. delete this._obj[this._obj.$_GID];
  20547. }
  20548. }
  20549. has(key) {
  20550. if (key == null)
  20551. return false;
  20552. if (WeakObject.supportWeakMap) ;
  20553. else {
  20554. if (typeof (key) == 'string' || typeof (key) == 'number')
  20555. return this._obj[key] != null;
  20556. return this._obj[this._obj.$_GID] != null;
  20557. }
  20558. }
  20559. }
  20560. WeakObject.supportWeakMap = false;
  20561. WeakObject.delInterval = 10 * 60 * 1000;
  20562. WeakObject._maps = [];
  20563. class SceneUtils {
  20564. static __init() {
  20565. SceneUtils._funMap = new WeakObject();
  20566. }
  20567. static getBindFun(value) {
  20568. var fun = SceneUtils._funMap.get(value);
  20569. if (fun == null) {
  20570. var temp = "\"" + value + "\"";
  20571. temp = temp.replace(/^"\${|}"$/g, "").replace(/\${/g, "\"+").replace(/}/g, "+\"");
  20572. var str = "(function(data){if(data==null)return;with(data){try{\nreturn " + temp + "\n}catch(e){}}})";
  20573. fun = window.Laya._runScript(str);
  20574. SceneUtils._funMap.set(value, fun);
  20575. }
  20576. return fun;
  20577. }
  20578. static createByData(root, uiView) {
  20579. var tInitTool = InitTool.create();
  20580. root = SceneUtils.createComp(uiView, root, root, null, tInitTool);
  20581. root._setBit(Const.NOT_READY, true);
  20582. if ("_idMap" in root) {
  20583. root["_idMap"] = tInitTool._idMap;
  20584. }
  20585. if (uiView.animations) {
  20586. var anilist = [];
  20587. var animations = uiView.animations;
  20588. var i, len = animations.length;
  20589. var tAni;
  20590. var tAniO;
  20591. for (i = 0; i < len; i++) {
  20592. tAni = new FrameAnimation();
  20593. tAniO = animations[i];
  20594. tAni._setUp(tInitTool._idMap, tAniO);
  20595. root[tAniO.name] = tAni;
  20596. tAni._setControlNode(root);
  20597. switch (tAniO.action) {
  20598. case 1:
  20599. tAni.play(0, false);
  20600. break;
  20601. case 2:
  20602. tAni.play(0, true);
  20603. break;
  20604. }
  20605. anilist.push(tAni);
  20606. }
  20607. root._aniList = anilist;
  20608. }
  20609. if (root._$componentType === "Scene" && root._width > 0 && uiView.props.hitTestPrior == null && !root.mouseThrough)
  20610. root.hitTestPrior = true;
  20611. tInitTool.beginLoad(root);
  20612. return root;
  20613. }
  20614. static createInitTool() {
  20615. return InitTool.create();
  20616. }
  20617. static createComp(uiView, comp = null, view = null, dataMap = null, initTool = null) {
  20618. if (uiView.type == "Scene3D" || uiView.type == "Sprite3D") {
  20619. var outBatchSprits = [];
  20620. var scene3D = ILaya.Laya["Utils3D"]._createSceneByJsonForMaker(uiView, outBatchSprits, initTool);
  20621. if (uiView.type == "Sprite3D")
  20622. ILaya.Laya["StaticBatchManager"].combine(scene3D, outBatchSprits);
  20623. else
  20624. ILaya.Laya["StaticBatchManager"].combine(null, outBatchSprits);
  20625. return scene3D;
  20626. }
  20627. comp = comp || SceneUtils.getCompInstance(uiView);
  20628. if (!comp) {
  20629. if (uiView.props && uiView.props.runtime)
  20630. console.warn("runtime not found:" + uiView.props.runtime);
  20631. else
  20632. console.warn("can not create:" + uiView.type);
  20633. return null;
  20634. }
  20635. var child = uiView.child;
  20636. if (child) {
  20637. var isList = comp["_$componentType"] == "List";
  20638. for (var i = 0, n = child.length; i < n; i++) {
  20639. var node = child[i];
  20640. if ('itemRender' in comp && (node.props.name == "render" || node.props.renderType === "render")) {
  20641. comp["itemRender"] = node;
  20642. }
  20643. else if (node.type == "Graphic") {
  20644. ILaya.ClassUtils._addGraphicsToSprite(node, comp);
  20645. }
  20646. else if (ILaya.ClassUtils._isDrawType(node.type)) {
  20647. ILaya.ClassUtils._addGraphicToSprite(node, comp, true);
  20648. }
  20649. else {
  20650. if (isList) {
  20651. var arr = [];
  20652. var tChild = SceneUtils.createComp(node, null, view, arr, initTool);
  20653. if (arr.length)
  20654. tChild["_$bindData"] = arr;
  20655. }
  20656. else {
  20657. tChild = SceneUtils.createComp(node, null, view, dataMap, initTool);
  20658. }
  20659. if (node.type == "Script") {
  20660. if (tChild instanceof Component) {
  20661. comp._addComponentInstance(tChild);
  20662. }
  20663. else {
  20664. if ("owner" in tChild) {
  20665. tChild["owner"] = comp;
  20666. }
  20667. else if ("target" in tChild) {
  20668. tChild["target"] = comp;
  20669. }
  20670. }
  20671. }
  20672. else if (node.props.renderType == "mask" || node.props.name == "mask") {
  20673. comp.mask = tChild;
  20674. }
  20675. else {
  20676. tChild instanceof Node && comp.addChild(tChild);
  20677. }
  20678. }
  20679. }
  20680. }
  20681. var props = uiView.props;
  20682. for (var prop in props) {
  20683. var value = props[prop];
  20684. if (typeof (value) == 'string' && (value.indexOf("@node:") >= 0 || value.indexOf("@Prefab:") >= 0)) {
  20685. if (initTool) {
  20686. initTool.addNodeRef(comp, prop, value);
  20687. }
  20688. }
  20689. else
  20690. SceneUtils.setCompValue(comp, prop, value, view, dataMap);
  20691. }
  20692. if (comp._afterInited) {
  20693. comp._afterInited();
  20694. }
  20695. if (uiView.compId && initTool && initTool._idMap) {
  20696. initTool._idMap[uiView.compId] = comp;
  20697. }
  20698. return comp;
  20699. }
  20700. static setCompValue(comp, prop, value, view = null, dataMap = null) {
  20701. if (typeof (value) == 'string' && value.indexOf("${") > -1) {
  20702. SceneUtils._sheet || (SceneUtils._sheet = ILaya.ClassUtils.getClass("laya.data.Table"));
  20703. if (!SceneUtils._sheet) {
  20704. console.warn("Can not find class Sheet");
  20705. return;
  20706. }
  20707. if (dataMap) {
  20708. dataMap.push(comp, prop, value);
  20709. }
  20710. else if (view) {
  20711. if (value.indexOf("].") == -1) {
  20712. value = value.replace(".", "[0].");
  20713. }
  20714. var watcher = new DataWatcher(comp, prop, value);
  20715. watcher.exe(view);
  20716. var one, temp;
  20717. var str = value.replace(/\[.*?\]\./g, ".");
  20718. while ((one = SceneUtils._parseWatchData.exec(str)) != null) {
  20719. var key1 = one[1];
  20720. while ((temp = SceneUtils._parseKeyWord.exec(key1)) != null) {
  20721. var key2 = temp[0];
  20722. var arr = (view._watchMap[key2] || (view._watchMap[key2] = []));
  20723. arr.push(watcher);
  20724. SceneUtils._sheet.I.notifer.on(key2, view, view.changeData, [key2]);
  20725. }
  20726. arr = (view._watchMap[key1] || (view._watchMap[key1] = []));
  20727. arr.push(watcher);
  20728. SceneUtils._sheet.I.notifer.on(key1, view, view.changeData, [key1]);
  20729. }
  20730. }
  20731. return;
  20732. }
  20733. if (prop === "var" && view) {
  20734. view[value] = comp;
  20735. }
  20736. else {
  20737. comp[prop] = (value === "true" ? true : (value === "false" ? false : value));
  20738. }
  20739. }
  20740. static getCompInstance(json) {
  20741. if (json.type == "UIView") {
  20742. if (json.props && json.props.pageData) {
  20743. return SceneUtils.createByData(null, json.props.pageData);
  20744. }
  20745. }
  20746. var runtime = (json.props && json.props.runtime) || json.type;
  20747. var compClass = ILaya.ClassUtils.getClass(runtime);
  20748. if (!compClass)
  20749. throw "Can not find class " + runtime;
  20750. if (json.type === "Script" && compClass.prototype._doAwake) {
  20751. var comp = Pool.createByClass(compClass);
  20752. comp._destroyed = false;
  20753. return comp;
  20754. }
  20755. if (json.props && "renderType" in json.props && json.props["renderType"] == "instance") {
  20756. if (!compClass["instance"])
  20757. compClass["instance"] = new compClass();
  20758. return compClass["instance"];
  20759. }
  20760. return new compClass();
  20761. }
  20762. }
  20763. SceneUtils._parseWatchData = /\${(.*?)}/g;
  20764. SceneUtils._parseKeyWord = /[a-zA-Z_][a-zA-Z0-9_]*(?:(?:\.[a-zA-Z_][a-zA-Z0-9_]*)+)/g;
  20765. class DataWatcher {
  20766. constructor(comp, prop, value) {
  20767. this.comp = comp;
  20768. this.prop = prop;
  20769. this.value = value;
  20770. }
  20771. exe(view) {
  20772. var fun = SceneUtils.getBindFun(this.value);
  20773. this.comp[this.prop] = fun.call(this, view);
  20774. }
  20775. }
  20776. class InitTool {
  20777. reset() {
  20778. this._nodeRefList = null;
  20779. this._initList = null;
  20780. this._idMap = null;
  20781. this._loadList = null;
  20782. this._scene = null;
  20783. }
  20784. recover() {
  20785. this.reset();
  20786. Pool.recover("InitTool", this);
  20787. }
  20788. static create() {
  20789. var tool = Pool.getItemByClass("InitTool", InitTool);
  20790. tool._idMap = [];
  20791. return tool;
  20792. }
  20793. addLoadRes(url, type = null) {
  20794. if (!this._loadList)
  20795. this._loadList = [];
  20796. if (!type) {
  20797. this._loadList.push(url);
  20798. }
  20799. else {
  20800. this._loadList.push({ url: url, type: type });
  20801. }
  20802. }
  20803. addNodeRef(node, prop, referStr) {
  20804. if (!this._nodeRefList)
  20805. this._nodeRefList = [];
  20806. this._nodeRefList.push([node, prop, referStr]);
  20807. if (referStr.indexOf("@Prefab:") >= 0) {
  20808. this.addLoadRes(referStr.replace("@Prefab:", ""), Loader.PREFAB);
  20809. }
  20810. }
  20811. setNodeRef() {
  20812. if (!this._nodeRefList)
  20813. return;
  20814. if (!this._idMap) {
  20815. this._nodeRefList = null;
  20816. return;
  20817. }
  20818. var i, len;
  20819. len = this._nodeRefList.length;
  20820. var tRefInfo;
  20821. for (i = 0; i < len; i++) {
  20822. tRefInfo = this._nodeRefList[i];
  20823. tRefInfo[0][tRefInfo[1]] = this.getReferData(tRefInfo[2]);
  20824. }
  20825. this._nodeRefList = null;
  20826. }
  20827. getReferData(referStr) {
  20828. if (referStr.indexOf("@Prefab:") >= 0) {
  20829. var prefab;
  20830. prefab = Loader.getRes(referStr.replace("@Prefab:", ""));
  20831. return prefab;
  20832. }
  20833. else if (referStr.indexOf("@arr:") >= 0) {
  20834. referStr = referStr.replace("@arr:", "");
  20835. var list;
  20836. list = referStr.split(",");
  20837. var i, len;
  20838. var tStr;
  20839. len = list.length;
  20840. for (i = 0; i < len; i++) {
  20841. tStr = list[i];
  20842. if (tStr) {
  20843. list[i] = this._idMap[tStr.replace("@node:", "")];
  20844. }
  20845. else {
  20846. list[i] = null;
  20847. }
  20848. }
  20849. return list;
  20850. }
  20851. else {
  20852. return this._idMap[referStr.replace("@node:", "")];
  20853. }
  20854. }
  20855. addInitItem(item) {
  20856. if (!this._initList)
  20857. this._initList = [];
  20858. this._initList.push(item);
  20859. }
  20860. doInits() {
  20861. if (!this._initList)
  20862. return;
  20863. this._initList = null;
  20864. }
  20865. finish() {
  20866. this.setNodeRef();
  20867. this.doInits();
  20868. this._scene._setBit(Const.NOT_READY, false);
  20869. if (this._scene.parent && this._scene.parent.activeInHierarchy && this._scene.active)
  20870. this._scene._processActive();
  20871. this._scene.event("onViewCreated");
  20872. this.recover();
  20873. }
  20874. beginLoad(scene) {
  20875. this._scene = scene;
  20876. if (!this._loadList || this._loadList.length < 1) {
  20877. this.finish();
  20878. }
  20879. else {
  20880. ILaya.loader.load(this._loadList, Handler.create(this, this.finish));
  20881. }
  20882. }
  20883. }
  20884. class IStatRender {
  20885. show(x = 0, y = 0) {
  20886. }
  20887. enable() {
  20888. }
  20889. hide() {
  20890. }
  20891. set_onclick(fn) {
  20892. }
  20893. isCanvasRender() {
  20894. return true;
  20895. }
  20896. renderNotCanvas(ctx, x, y) { }
  20897. }
  20898. class StatUI extends IStatRender {
  20899. constructor() {
  20900. super(...arguments);
  20901. this._show = false;
  20902. this._useCanvas = false;
  20903. this._height = 100;
  20904. this._view = [];
  20905. }
  20906. show(x = 0, y = 0) {
  20907. if (!Browser.onMiniGame && !ILaya.Render.isConchApp && !Browser.onBDMiniGame && !Browser.onKGMiniGame && !Browser.onQGMiniGame && !Browser.onQQMiniGame && !Browser.onAlipayMiniGame)
  20908. this._useCanvas = true;
  20909. this._show = true;
  20910. Stat._fpsData.length = 60;
  20911. this._view[0] = { title: "FPS(Canvas)", value: "_fpsStr", color: "yellow", units: "int" };
  20912. this._view[1] = { title: "Sprite", value: "_spriteStr", color: "white", units: "int" };
  20913. this._view[2] = { title: "RenderBatches", value: "renderBatches", color: "white", units: "int" };
  20914. this._view[3] = { title: "SavedRenderBatches", value: "savedRenderBatches", color: "white", units: "int" };
  20915. this._view[4] = { title: "CPUMemory", value: "cpuMemory", color: "yellow", units: "M" };
  20916. this._view[5] = { title: "GPUMemory", value: "gpuMemory", color: "yellow", units: "M" };
  20917. this._view[6] = { title: "Shader", value: "shaderCall", color: "white", units: "int" };
  20918. if (!Render.is3DMode) {
  20919. this._view[0].title = "FPS(WebGL)";
  20920. this._view[7] = { title: "Canvas", value: "_canvasStr", color: "white", units: "int" };
  20921. }
  20922. else {
  20923. this._view[0].title = "FPS(3D)";
  20924. this._view[7] = { title: "TriFaces", value: "trianglesFaces", color: "white", units: "int" };
  20925. this._view[8] = { title: "FrustumCulling", value: "frustumCulling", color: "white", units: "int" };
  20926. this._view[9] = { title: "OctreeNodeCulling", value: "octreeNodeCulling", color: "white", units: "int" };
  20927. }
  20928. if (this._useCanvas) {
  20929. this.createUIPre(x, y);
  20930. }
  20931. else
  20932. this.createUI(x, y);
  20933. this.enable();
  20934. }
  20935. createUIPre(x, y) {
  20936. var pixel = Browser.pixelRatio;
  20937. this._width = pixel * 180;
  20938. this._vx = pixel * 120;
  20939. this._height = pixel * (this._view.length * 12 + 3 * pixel) + 4;
  20940. StatUI._fontSize = 12 * pixel;
  20941. for (var i = 0; i < this._view.length; i++) {
  20942. this._view[i].x = 4;
  20943. this._view[i].y = i * StatUI._fontSize + 2 * pixel;
  20944. }
  20945. if (!this._canvas) {
  20946. this._canvas = new HTMLCanvas(true);
  20947. this._canvas.size(this._width, this._height);
  20948. this._ctx = this._canvas.getContext('2d');
  20949. this._ctx.textBaseline = "top";
  20950. this._ctx.font = StatUI._fontSize + "px Arial";
  20951. 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;";
  20952. }
  20953. if (!Browser.onKGMiniGame) {
  20954. Browser.container.appendChild(this._canvas.source);
  20955. }
  20956. this._first = true;
  20957. this.loop();
  20958. this._first = false;
  20959. }
  20960. createUI(x, y) {
  20961. var stat = this._sp;
  20962. var pixel = Browser.pixelRatio;
  20963. if (!stat) {
  20964. stat = new Sprite();
  20965. this._leftText = new Text();
  20966. this._leftText.pos(5, 5);
  20967. this._leftText.color = "#ffffff";
  20968. stat.addChild(this._leftText);
  20969. this._txt = new Text();
  20970. this._txt.pos(130 * pixel, 5);
  20971. this._txt.color = "#ffffff";
  20972. stat.addChild(this._txt);
  20973. this._sp = stat;
  20974. }
  20975. stat.pos(x, y);
  20976. var text = "";
  20977. for (var i = 0; i < this._view.length; i++) {
  20978. var one = this._view[i];
  20979. text += one.title + "\n";
  20980. }
  20981. this._leftText.text = text;
  20982. var width = pixel * 138;
  20983. var height = pixel * (this._view.length * 12 + 3 * pixel) + 4;
  20984. this._txt.fontSize = StatUI._fontSize * pixel;
  20985. this._leftText.fontSize = StatUI._fontSize * pixel;
  20986. stat.size(width, height);
  20987. stat.graphics.clear();
  20988. stat.graphics.alpha(0.5);
  20989. stat.graphics.drawRect(0, 0, width + 110, height + 30, "#999999");
  20990. stat.graphics.alpha(2);
  20991. this.loop();
  20992. }
  20993. enable() {
  20994. ILaya.systemTimer.frameLoop(1, this, this.loop);
  20995. }
  20996. hide() {
  20997. this._show = false;
  20998. ILaya.systemTimer.clear(this, this.loop);
  20999. if (this._canvas) {
  21000. Browser.removeElement(this._canvas.source);
  21001. }
  21002. }
  21003. set_onclick(fn) {
  21004. if (this._sp) {
  21005. this._sp.on("click", this._sp, fn);
  21006. }
  21007. if (this._canvas) {
  21008. this._canvas.source.onclick = fn;
  21009. this._canvas.source.style.pointerEvents = '';
  21010. }
  21011. }
  21012. loop() {
  21013. Stat._count++;
  21014. var timer = Browser.now();
  21015. if (timer - Stat._timer < 1000)
  21016. return;
  21017. var count = Stat._count;
  21018. Stat.FPS = Math.round((count * 1000) / (timer - Stat._timer));
  21019. if (this._show) {
  21020. Stat.trianglesFaces = Math.round(Stat.trianglesFaces / count);
  21021. if (!this._useCanvas) {
  21022. Stat.renderBatches = Math.round(Stat.renderBatches / count) - 1;
  21023. }
  21024. else {
  21025. Stat.renderBatches = Math.round(Stat.renderBatches / count);
  21026. }
  21027. Stat.savedRenderBatches = Math.round(Stat.savedRenderBatches / count);
  21028. Stat.shaderCall = Math.round(Stat.shaderCall / count);
  21029. Stat.spriteRenderUseCacheCount = Math.round(Stat.spriteRenderUseCacheCount / count);
  21030. Stat.canvasNormal = Math.round(Stat.canvasNormal / count);
  21031. Stat.canvasBitmap = Math.round(Stat.canvasBitmap / count);
  21032. Stat.canvasReCache = Math.ceil(Stat.canvasReCache / count);
  21033. Stat.frustumCulling = Math.round(Stat.frustumCulling / count);
  21034. Stat.octreeNodeCulling = Math.round(Stat.octreeNodeCulling / count);
  21035. var delay = Stat.FPS > 0 ? Math.floor(1000 / Stat.FPS).toString() : " ";
  21036. Stat._fpsStr = Stat.FPS + (Stat.renderSlow ? " slow" : "") + " " + delay;
  21037. Stat._spriteStr = Stat.spriteCount + (Stat.spriteRenderUseCacheCount ? ("/" + Stat.spriteRenderUseCacheCount) : '');
  21038. Stat._canvasStr = Stat.canvasReCache + "/" + Stat.canvasNormal + "/" + Stat.canvasBitmap;
  21039. Stat.cpuMemory = Resource.cpuMemory;
  21040. Stat.gpuMemory = Resource.gpuMemory;
  21041. if (this._useCanvas) {
  21042. this.renderInfoPre();
  21043. }
  21044. else
  21045. this.renderInfo();
  21046. Stat.clear();
  21047. }
  21048. Stat._count = 0;
  21049. Stat._timer = timer;
  21050. }
  21051. renderInfoPre() {
  21052. var i = 0;
  21053. var one;
  21054. var value;
  21055. if (this._canvas) {
  21056. var ctx = this._ctx;
  21057. ctx.clearRect(this._first ? 0 : this._vx, 0, this._width, this._height);
  21058. for (i = 0; i < this._view.length; i++) {
  21059. one = this._view[i];
  21060. if (this._first) {
  21061. ctx.fillStyle = "white";
  21062. ctx.fillText(one.title, one.x, one.y);
  21063. }
  21064. ctx.fillStyle = one.color;
  21065. value = Stat[one.value];
  21066. (one.units == "M") && (value = Math.floor(value / (1024 * 1024) * 100) / 100 + " M");
  21067. ctx.fillText(value + "", one.x + this._vx, one.y);
  21068. }
  21069. }
  21070. }
  21071. renderInfo() {
  21072. var text = "";
  21073. for (var i = 0; i < this._view.length; i++) {
  21074. var one = this._view[i];
  21075. var value = Stat[one.value];
  21076. (one.units == "M") && (value = Math.floor(value / (1024 * 1024) * 100) / 100 + " M");
  21077. (one.units == "K") && (value = Math.floor(value / (1024) * 100) / 100 + " K");
  21078. text += value + "\n";
  21079. }
  21080. this._txt.text = text;
  21081. }
  21082. isCanvasRender() {
  21083. return this._useCanvas;
  21084. }
  21085. renderNotCanvas(ctx, x, y) {
  21086. this._show && this._sp && this._sp.render(ctx, 0, 0);
  21087. }
  21088. }
  21089. StatUI._fontSize = 12;
  21090. class Timer {
  21091. constructor(autoActive = true) {
  21092. this.scale = 1;
  21093. this.currTimer = Date.now();
  21094. this.currFrame = 0;
  21095. this._delta = 0;
  21096. this._lastTimer = Date.now();
  21097. this._map = [];
  21098. this._handlers = [];
  21099. this._temp = [];
  21100. this._count = 0;
  21101. autoActive && Timer.gSysTimer && Timer.gSysTimer.frameLoop(1, this, this._update);
  21102. }
  21103. get delta() {
  21104. return this._delta;
  21105. }
  21106. _update() {
  21107. if (this.scale <= 0) {
  21108. this._lastTimer = Date.now();
  21109. this._delta = 0;
  21110. return;
  21111. }
  21112. var frame = this.currFrame = this.currFrame + this.scale;
  21113. var now = Date.now();
  21114. var awake = (now - this._lastTimer) > 30000;
  21115. this._delta = (now - this._lastTimer) * this.scale;
  21116. var timer = this.currTimer = this.currTimer + this._delta;
  21117. this._lastTimer = now;
  21118. var handlers = this._handlers;
  21119. this._count = 0;
  21120. for (var i = 0, n = handlers.length; i < n; i++) {
  21121. var handler = handlers[i];
  21122. if (handler.method !== null) {
  21123. var t = handler.userFrame ? frame : timer;
  21124. if (t >= handler.exeTime) {
  21125. if (handler.repeat) {
  21126. if (!handler.jumpFrame || awake) {
  21127. handler.exeTime += handler.delay;
  21128. handler.run(false);
  21129. if (t > handler.exeTime) {
  21130. handler.exeTime += Math.ceil((t - handler.exeTime) / handler.delay) * handler.delay;
  21131. }
  21132. }
  21133. else {
  21134. while (t >= handler.exeTime) {
  21135. handler.exeTime += handler.delay;
  21136. handler.run(false);
  21137. }
  21138. }
  21139. }
  21140. else {
  21141. handler.run(true);
  21142. }
  21143. }
  21144. }
  21145. else {
  21146. this._count++;
  21147. }
  21148. }
  21149. if (this._count > 30 || frame % 200 === 0)
  21150. this._clearHandlers();
  21151. }
  21152. _clearHandlers() {
  21153. var handlers = this._handlers;
  21154. for (var i = 0, n = handlers.length; i < n; i++) {
  21155. var handler = handlers[i];
  21156. if (handler.method !== null)
  21157. this._temp.push(handler);
  21158. else
  21159. this._recoverHandler(handler);
  21160. }
  21161. this._handlers = this._temp;
  21162. handlers.length = 0;
  21163. this._temp = handlers;
  21164. }
  21165. _recoverHandler(handler) {
  21166. if (this._map[handler.key] == handler)
  21167. this._map[handler.key] = null;
  21168. handler.clear();
  21169. Timer._pool.push(handler);
  21170. }
  21171. _create(useFrame, repeat, delay, caller, method, args, coverBefore) {
  21172. if (!delay) {
  21173. method.apply(caller, args);
  21174. return null;
  21175. }
  21176. if (coverBefore) {
  21177. var handler = this._getHandler(caller, method);
  21178. if (handler) {
  21179. handler.repeat = repeat;
  21180. handler.userFrame = useFrame;
  21181. handler.delay = delay;
  21182. handler.caller = caller;
  21183. handler.method = method;
  21184. handler.args = args;
  21185. handler.exeTime = delay + (useFrame ? this.currFrame : this.currTimer + Date.now() - this._lastTimer);
  21186. return handler;
  21187. }
  21188. }
  21189. handler = Timer._pool.length > 0 ? Timer._pool.pop() : new TimerHandler();
  21190. handler.repeat = repeat;
  21191. handler.userFrame = useFrame;
  21192. handler.delay = delay;
  21193. handler.caller = caller;
  21194. handler.method = method;
  21195. handler.args = args;
  21196. handler.exeTime = delay + (useFrame ? this.currFrame : this.currTimer + Date.now() - this._lastTimer);
  21197. this._indexHandler(handler);
  21198. this._handlers.push(handler);
  21199. return handler;
  21200. }
  21201. _indexHandler(handler) {
  21202. var caller = handler.caller;
  21203. var method = handler.method;
  21204. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  21205. var mid = method.$_TID || (method.$_TID = (Timer._mid++) * 100000);
  21206. handler.key = cid + mid;
  21207. this._map[handler.key] = handler;
  21208. }
  21209. once(delay, caller, method, args = null, coverBefore = true) {
  21210. this._create(false, false, delay, caller, method, args, coverBefore);
  21211. }
  21212. loop(delay, caller, method, args = null, coverBefore = true, jumpFrame = false) {
  21213. var handler = this._create(false, true, delay, caller, method, args, coverBefore);
  21214. if (handler)
  21215. handler.jumpFrame = jumpFrame;
  21216. }
  21217. frameOnce(delay, caller, method, args = null, coverBefore = true) {
  21218. this._create(true, false, delay, caller, method, args, coverBefore);
  21219. }
  21220. frameLoop(delay, caller, method, args = null, coverBefore = true) {
  21221. this._create(true, true, delay, caller, method, args, coverBefore);
  21222. }
  21223. toString() {
  21224. return " handlers:" + this._handlers.length + " pool:" + Timer._pool.length;
  21225. }
  21226. clear(caller, method) {
  21227. var handler = this._getHandler(caller, method);
  21228. if (handler) {
  21229. this._map[handler.key] = null;
  21230. handler.key = 0;
  21231. handler.clear();
  21232. }
  21233. }
  21234. clearAll(caller) {
  21235. if (!caller)
  21236. return;
  21237. for (var i = 0, n = this._handlers.length; i < n; i++) {
  21238. var handler = this._handlers[i];
  21239. if (handler.caller === caller) {
  21240. this._map[handler.key] = null;
  21241. handler.key = 0;
  21242. handler.clear();
  21243. }
  21244. }
  21245. }
  21246. _getHandler(caller, method) {
  21247. var cid = caller ? caller.$_GID || (caller.$_GID = ILaya.Utils.getGID()) : 0;
  21248. var mid = method.$_TID || (method.$_TID = (Timer._mid++) * 100000);
  21249. return this._map[cid + mid];
  21250. }
  21251. callLater(caller, method, args = null) {
  21252. CallLater.I.callLater(caller, method, args);
  21253. }
  21254. runCallLater(caller, method) {
  21255. CallLater.I.runCallLater(caller, method);
  21256. }
  21257. runTimer(caller, method) {
  21258. var handler = this._getHandler(caller, method);
  21259. if (handler && handler.method != null) {
  21260. this._map[handler.key] = null;
  21261. handler.run(true);
  21262. }
  21263. }
  21264. pause() {
  21265. this.scale = 0;
  21266. }
  21267. resume() {
  21268. this.scale = 1;
  21269. }
  21270. }
  21271. Timer.gSysTimer = null;
  21272. Timer._pool = [];
  21273. Timer._mid = 1;
  21274. class TimerHandler {
  21275. clear() {
  21276. this.caller = null;
  21277. this.method = null;
  21278. this.args = null;
  21279. }
  21280. run(withClear) {
  21281. var caller = this.caller;
  21282. if (caller && caller.destroyed)
  21283. return this.clear();
  21284. var method = this.method;
  21285. var args = this.args;
  21286. withClear && this.clear();
  21287. if (method == null)
  21288. return;
  21289. args ? method.apply(caller, args) : method.call(caller);
  21290. }
  21291. }
  21292. class SkinSV extends Value2D {
  21293. constructor(type) {
  21294. super(ShaderDefines2D.SKINMESH, 0);
  21295. this.offsetX = 300;
  21296. this.offsetY = 0;
  21297. var gl = WebGLContext.mainContext;
  21298. var _vlen = 8 * CONST3D2D.BYTES_PE;
  21299. this.position = [2, gl.FLOAT, false, _vlen, 0];
  21300. this.texcoord = [2, gl.FLOAT, false, _vlen, 2 * CONST3D2D.BYTES_PE];
  21301. this.color = [4, gl.FLOAT, false, _vlen, 4 * CONST3D2D.BYTES_PE];
  21302. }
  21303. }
  21304. class PrimitiveSV extends Value2D {
  21305. constructor(args) {
  21306. super(ShaderDefines2D.PRIMITIVE, 0);
  21307. this._attribLocation = ['position', 0, 'attribColor', 1];
  21308. }
  21309. }
  21310. class TextureSV extends Value2D {
  21311. constructor(subID = 0) {
  21312. super(ShaderDefines2D.TEXTURE2D, subID);
  21313. this.strength = 0;
  21314. this.blurInfo = null;
  21315. this.colorMat = null;
  21316. this.colorAlpha = null;
  21317. this._attribLocation = ['posuv', 0, 'attribColor', 1, 'attribFlags', 2];
  21318. }
  21319. clear() {
  21320. this.texture = null;
  21321. this.shader = null;
  21322. this.defines._value = this.subID;
  21323. }
  21324. }
  21325. class InlcudeFile {
  21326. constructor(txt) {
  21327. this.codes = {};
  21328. this.funs = {};
  21329. this.curUseID = -1;
  21330. this.funnames = "";
  21331. this.script = txt;
  21332. var begin = 0, ofs, end;
  21333. while (true) {
  21334. begin = txt.indexOf("#begin", begin);
  21335. if (begin < 0)
  21336. break;
  21337. end = begin + 5;
  21338. while (true) {
  21339. end = txt.indexOf("#end", end);
  21340. if (end < 0)
  21341. break;
  21342. if (txt.charAt(end + 4) === 'i')
  21343. end += 5;
  21344. else
  21345. break;
  21346. }
  21347. if (end < 0) {
  21348. throw "add include err,no #end:" + txt;
  21349. }
  21350. ofs = txt.indexOf('\n', begin);
  21351. var words = ILaya.ShaderCompile.splitToWords(txt.substr(begin, ofs - begin), null);
  21352. if (words[1] == 'code') {
  21353. this.codes[words[2]] = txt.substr(ofs + 1, end - ofs - 1);
  21354. }
  21355. else if (words[1] == 'function') {
  21356. ofs = txt.indexOf("function", begin);
  21357. ofs += "function".length;
  21358. this.funs[words[3]] = txt.substr(ofs + 1, end - ofs - 1);
  21359. this.funnames += words[3] + ";";
  21360. }
  21361. begin = end + 1;
  21362. }
  21363. }
  21364. getWith(name = null) {
  21365. var r = name ? this.codes[name] : this.script;
  21366. if (!r) {
  21367. throw "get with error:" + name;
  21368. }
  21369. return r;
  21370. }
  21371. getFunsScript(funsdef) {
  21372. var r = "";
  21373. for (var i in this.funs) {
  21374. if (funsdef.indexOf(i + ";") >= 0) {
  21375. r += this.funs[i];
  21376. }
  21377. }
  21378. return r;
  21379. }
  21380. }
  21381. class ShaderNode {
  21382. constructor(includefiles) {
  21383. this.childs = [];
  21384. this.text = "";
  21385. this.useFuns = "";
  21386. this.z = 0;
  21387. this.includefiles = includefiles;
  21388. }
  21389. setParent(parent) {
  21390. parent.childs.push(this);
  21391. this.z = parent.z + 1;
  21392. this.parent = parent;
  21393. }
  21394. setCondition(condition, type) {
  21395. if (condition) {
  21396. this.conditionType = type;
  21397. condition = condition.replace(/(\s*$)/g, "");
  21398. this.condition = function () {
  21399. return this[condition];
  21400. };
  21401. this.condition.__condition = condition;
  21402. }
  21403. }
  21404. toscript(def, out) {
  21405. return this._toscript(def, out, ++ShaderNode.__id);
  21406. }
  21407. _toscript(def, out, id) {
  21408. if (this.childs.length < 1 && !this.text)
  21409. return out;
  21410. var outIndex = out.length;
  21411. if (this.condition) {
  21412. var ifdef = !!this.condition.call(def);
  21413. this.conditionType === ILaya.ShaderCompile.IFDEF_ELSE && (ifdef = !ifdef);
  21414. if (!ifdef)
  21415. return out;
  21416. }
  21417. this.text && out.push(this.text);
  21418. this.childs.length > 0 && this.childs.forEach(function (o, index, arr) {
  21419. o._toscript(def, out, id);
  21420. });
  21421. if (this.includefiles.length > 0 && this.useFuns.length > 0) {
  21422. var funsCode;
  21423. for (var i = 0, n = this.includefiles.length; i < n; i++) {
  21424. if (this.includefiles[i].curUseID == id) {
  21425. continue;
  21426. }
  21427. funsCode = this.includefiles[i].file.getFunsScript(this.useFuns);
  21428. if (funsCode.length > 0) {
  21429. this.includefiles[i].curUseID = id;
  21430. out[0] = funsCode + out[0];
  21431. }
  21432. }
  21433. }
  21434. return out;
  21435. }
  21436. }
  21437. ShaderNode.__id = 1;
  21438. class ShaderCompile {
  21439. constructor(vs, ps, nameMap) {
  21440. this.defs = {};
  21441. let _this = this;
  21442. function _compile(script) {
  21443. script = script.replace(ShaderCompile._clearCR, "");
  21444. var includefiles = [];
  21445. var top = new ShaderNode(includefiles);
  21446. _this._compileToTree(top, script.split('\n'), 0, includefiles, _this.defs);
  21447. return top;
  21448. }
  21449. var startTime = Date.now();
  21450. this._VS = _compile(vs);
  21451. this._PS = _compile(ps);
  21452. this._nameMap = nameMap;
  21453. if ((Date.now() - startTime) > 2)
  21454. console.log("ShaderCompile use time:" + (Date.now() - startTime) + " size:" + vs.length + "/" + ps.length);
  21455. }
  21456. static __init__() {
  21457. var gl = LayaGL.instance;
  21458. 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 };
  21459. }
  21460. static _parseOne(attributes, uniforms, words, i, word, b) {
  21461. var one = { type: ShaderCompile.shaderParamsMap[words[i + 1]], name: words[i + 2], size: isNaN(parseInt(words[i + 3])) ? 1 : parseInt(words[i + 3]) };
  21462. if (b) {
  21463. if (word == "attribute") {
  21464. attributes.push(one);
  21465. }
  21466. else {
  21467. uniforms.push(one);
  21468. }
  21469. }
  21470. if (words[i + 3] == ':') {
  21471. one.type = words[i + 4];
  21472. i += 2;
  21473. }
  21474. i += 2;
  21475. return i;
  21476. }
  21477. static addInclude(fileName, txt) {
  21478. if (!txt || txt.length === 0)
  21479. throw new Error("add shader include file err:" + fileName);
  21480. if (ShaderCompile.includes[fileName])
  21481. throw new Error("add shader include file err, has add:" + fileName);
  21482. ShaderCompile.includes[fileName] = new InlcudeFile(txt);
  21483. }
  21484. static preGetParams(vs, ps) {
  21485. var text = [vs, ps];
  21486. var result = {};
  21487. var attributes = [];
  21488. var uniforms = [];
  21489. var definesInfo = {};
  21490. var definesName = [];
  21491. result.attributes = attributes;
  21492. result.uniforms = uniforms;
  21493. result.defines = definesInfo;
  21494. var i, n;
  21495. for (var s = 0; s < 2; s++) {
  21496. text[s] = text[s].replace(ShaderCompile._removeAnnotation, "");
  21497. var words = text[s].match(ShaderCompile._reg);
  21498. var tempelse;
  21499. for (i = 0, n = words.length; i < n; i++) {
  21500. var word = words[i];
  21501. if (word != "attribute" && word != "uniform") {
  21502. if (word == "#define") {
  21503. word = words[++i];
  21504. definesName[word] = 1;
  21505. continue;
  21506. }
  21507. else if (word == "#ifdef") {
  21508. tempelse = words[++i];
  21509. var def = definesInfo[tempelse] = definesInfo[tempelse] || [];
  21510. for (i++; i < n; i++) {
  21511. word = words[i];
  21512. if (word != "attribute" && word != "uniform") {
  21513. if (word == "#else") {
  21514. for (i++; i < n; i++) {
  21515. word = words[i];
  21516. if (word != "attribute" && word != "uniform") {
  21517. if (word == "#endif") {
  21518. break;
  21519. }
  21520. continue;
  21521. }
  21522. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, !definesName[tempelse]);
  21523. }
  21524. }
  21525. continue;
  21526. }
  21527. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, definesName[tempelse]);
  21528. }
  21529. }
  21530. continue;
  21531. }
  21532. i = ShaderCompile._parseOne(attributes, uniforms, words, i, word, true);
  21533. }
  21534. }
  21535. return result;
  21536. }
  21537. static splitToWords(str, block) {
  21538. var out = [];
  21539. var c;
  21540. var ofs = -1;
  21541. var word;
  21542. for (var i = 0, n = str.length; i < n; i++) {
  21543. c = str.charAt(i);
  21544. if (" \t=+-*/&%!<>()'\",;".indexOf(c) >= 0) {
  21545. if (ofs >= 0 && (i - ofs) > 1) {
  21546. word = str.substr(ofs, i - ofs);
  21547. out.push(word);
  21548. }
  21549. if (c == '"' || c == "'") {
  21550. var ofs2 = str.indexOf(c, i + 1);
  21551. if (ofs2 < 0) {
  21552. throw "Sharder err:" + str;
  21553. }
  21554. out.push(str.substr(i + 1, ofs2 - i - 1));
  21555. i = ofs2;
  21556. ofs = -1;
  21557. continue;
  21558. }
  21559. if (c == '(' && block && out.length > 0) {
  21560. word = out[out.length - 1] + ";";
  21561. if ("vec4;main;".indexOf(word) < 0)
  21562. block.useFuns += word;
  21563. }
  21564. ofs = -1;
  21565. continue;
  21566. }
  21567. if (ofs < 0)
  21568. ofs = i;
  21569. }
  21570. if (ofs < n && (n - ofs) > 1) {
  21571. word = str.substr(ofs, n - ofs);
  21572. out.push(word);
  21573. }
  21574. return out;
  21575. }
  21576. _compileToTree(parent, lines, start, includefiles, defs) {
  21577. var node, preNode;
  21578. var text, name, fname;
  21579. var ofs, words, noUseNode;
  21580. var i, n, j;
  21581. for (i = start; i < lines.length; i++) {
  21582. text = lines[i];
  21583. if (text.length < 1)
  21584. continue;
  21585. ofs = text.indexOf("//");
  21586. if (ofs === 0)
  21587. continue;
  21588. if (ofs >= 0)
  21589. text = text.substr(0, ofs);
  21590. node = noUseNode || new ShaderNode(includefiles);
  21591. noUseNode = null;
  21592. node.text = text;
  21593. node.noCompile = true;
  21594. if ((ofs = text.indexOf("#")) >= 0) {
  21595. name = "#";
  21596. for (j = ofs + 1, n = text.length; j < n; j++) {
  21597. var c = text.charAt(j);
  21598. if (c === ' ' || c === '\t' || c === '?')
  21599. break;
  21600. name += c;
  21601. }
  21602. node.name = name;
  21603. switch (name) {
  21604. case "#ifdef":
  21605. case "#ifndef":
  21606. node.src = text;
  21607. node.noCompile = text.match(/[!&|()=<>]/) != null;
  21608. if (!node.noCompile) {
  21609. words = text.replace(/^\s*/, '').split(/\s+/);
  21610. node.setCondition(words[1], name === "#ifdef" ? ShaderCompile.IFDEF_YES : ShaderCompile.IFDEF_ELSE);
  21611. node.text = "//" + node.text;
  21612. }
  21613. else {
  21614. console.log("function():Boolean{return " + text.substr(ofs + node.name.length) + "}");
  21615. }
  21616. node.setParent(parent);
  21617. parent = node;
  21618. if (defs) {
  21619. words = text.substr(j).split(ShaderCompile._splitToWordExps3);
  21620. for (j = 0; j < words.length; j++) {
  21621. text = words[j];
  21622. text.length && (defs[text] = true);
  21623. }
  21624. }
  21625. continue;
  21626. case "#if":
  21627. node.src = text;
  21628. node.noCompile = true;
  21629. node.setParent(parent);
  21630. parent = node;
  21631. if (defs) {
  21632. words = text.substr(j).split(ShaderCompile._splitToWordExps3);
  21633. for (j = 0; j < words.length; j++) {
  21634. text = words[j];
  21635. text.length && text != "defined" && (defs[text] = true);
  21636. }
  21637. }
  21638. continue;
  21639. case "#else":
  21640. node.src = text;
  21641. parent = parent.parent;
  21642. preNode = parent.childs[parent.childs.length - 1];
  21643. node.noCompile = preNode.noCompile;
  21644. if (!node.noCompile) {
  21645. node.condition = preNode.condition;
  21646. node.conditionType = preNode.conditionType == ShaderCompile.IFDEF_YES ? ShaderCompile.IFDEF_ELSE : ShaderCompile.IFDEF_YES;
  21647. node.text = "//" + node.text + " " + preNode.text + " " + node.conditionType;
  21648. }
  21649. node.setParent(parent);
  21650. parent = node;
  21651. continue;
  21652. case "#endif":
  21653. parent = parent.parent;
  21654. preNode = parent.childs[parent.childs.length - 1];
  21655. node.noCompile = preNode.noCompile;
  21656. if (!node.noCompile) {
  21657. node.text = "//" + node.text;
  21658. }
  21659. node.setParent(parent);
  21660. continue;
  21661. case "#include":
  21662. words = ShaderCompile.splitToWords(text, null);
  21663. var inlcudeFile = ShaderCompile.includes[words[1]];
  21664. if (!inlcudeFile) {
  21665. throw "ShaderCompile error no this include file:" + words[1];
  21666. }
  21667. if ((ofs = words[0].indexOf("?")) < 0) {
  21668. node.setParent(parent);
  21669. text = inlcudeFile.getWith(words[2] == 'with' ? words[3] : null);
  21670. this._compileToTree(node, text.split('\n'), 0, includefiles, defs);
  21671. node.text = "";
  21672. continue;
  21673. }
  21674. node.setCondition(words[0].substr(ofs + 1), ShaderCompile.IFDEF_YES);
  21675. node.text = inlcudeFile.getWith(words[2] == 'with' ? words[3] : null);
  21676. break;
  21677. case "#import":
  21678. words = ShaderCompile.splitToWords(text, null);
  21679. fname = words[1];
  21680. includefiles.push({ node: node, file: ShaderCompile.includes[fname], ofs: node.text.length });
  21681. continue;
  21682. }
  21683. }
  21684. else {
  21685. preNode = parent.childs[parent.childs.length - 1];
  21686. if (preNode && !preNode.name) {
  21687. includefiles.length > 0 && ShaderCompile.splitToWords(text, preNode);
  21688. noUseNode = node;
  21689. preNode.text += "\n" + text;
  21690. continue;
  21691. }
  21692. includefiles.length > 0 && ShaderCompile.splitToWords(text, node);
  21693. }
  21694. node.setParent(parent);
  21695. }
  21696. }
  21697. createShader(define, shaderName, createShader, bindAttrib) {
  21698. var defMap = {};
  21699. var defineStr = "";
  21700. if (define) {
  21701. for (var i in define) {
  21702. defineStr += "#define " + i + "\n";
  21703. defMap[i] = true;
  21704. }
  21705. }
  21706. var vs = this._VS.toscript(defMap, []);
  21707. var ps = this._PS.toscript(defMap, []);
  21708. return (createShader || Shader.create)(defineStr + vs.join('\n'), defineStr + ps.join('\n'), shaderName, this._nameMap, bindAttrib);
  21709. }
  21710. }
  21711. ShaderCompile.IFDEF_NO = 0;
  21712. ShaderCompile.IFDEF_YES = 1;
  21713. ShaderCompile.IFDEF_ELSE = 2;
  21714. ShaderCompile.IFDEF_PARENT = 3;
  21715. ShaderCompile._removeAnnotation = new RegExp("(/\\*([^*]|[\\r\\\n]|(\\*+([^*/]|[\\r\\n])))*\\*+/)|(//.*)", "g");
  21716. ShaderCompile._reg = new RegExp("(\".*\")|('.*')|([#\\w\\*-\\.+/()=<>{}\\\\]+)|([,;:\\\\])", "g");
  21717. ShaderCompile._splitToWordExps = new RegExp("[(\".*\")]+|[('.*')]+|([ \\t=\\+\\-*/&%!<>!%\(\),;])", "g");
  21718. ShaderCompile.includes = {};
  21719. ShaderCompile._clearCR = new RegExp("\r", "g");
  21720. ShaderCompile._splitToWordExps3 = new RegExp("[ \\t=\\+\\-*/&%!<>!%\(\),;\\|]", "g");
  21721. class WorkerLoader extends EventDispatcher {
  21722. constructor() {
  21723. super();
  21724. this.worker = new Worker(WorkerLoader.workerPath);
  21725. let me = this;
  21726. this.worker.onmessage = function (evt) {
  21727. me.workerMessage(evt.data);
  21728. };
  21729. }
  21730. static __init__() {
  21731. if (WorkerLoader._preLoadFun != null)
  21732. return false;
  21733. if (!Worker)
  21734. return false;
  21735. WorkerLoader._preLoadFun = Loader["prototype"]["_loadImage"];
  21736. Loader["prototype"]["_loadImage"] = WorkerLoader["prototype"]["_loadImage"];
  21737. if (!WorkerLoader.I)
  21738. WorkerLoader.I = new WorkerLoader();
  21739. return true;
  21740. }
  21741. static workerSupported() {
  21742. return Worker ? true : false;
  21743. }
  21744. static enableWorkerLoader() {
  21745. if (!WorkerLoader._tryEnabled) {
  21746. WorkerLoader.enable = true;
  21747. WorkerLoader._tryEnabled = true;
  21748. }
  21749. }
  21750. static set enable(value) {
  21751. if (WorkerLoader._enable != value) {
  21752. WorkerLoader._enable = value;
  21753. if (value && WorkerLoader._preLoadFun == null)
  21754. WorkerLoader._enable = WorkerLoader.__init__();
  21755. }
  21756. }
  21757. static get enable() {
  21758. return WorkerLoader._enable;
  21759. }
  21760. workerMessage(data) {
  21761. if (data) {
  21762. switch (data.type) {
  21763. case "Image":
  21764. this.imageLoaded(data);
  21765. break;
  21766. case "Disable":
  21767. WorkerLoader.enable = false;
  21768. break;
  21769. }
  21770. }
  21771. }
  21772. imageLoaded(data) {
  21773. if (!data.dataType || data.dataType != "imageBitmap") {
  21774. this.event(data.url, null);
  21775. return;
  21776. }
  21777. var imageData = data.imageBitmap;
  21778. var tex = new Texture2D();
  21779. tex.loadImageSource(imageData);
  21780. console.log("load:", data.url);
  21781. this.event(data.url, tex);
  21782. }
  21783. loadImage(url) {
  21784. this.worker.postMessage(url);
  21785. }
  21786. _loadImage(url) {
  21787. var _this = this;
  21788. if (!this._useWorkerLoader || !WorkerLoader._enable) {
  21789. WorkerLoader._preLoadFun.call(_this, url);
  21790. return;
  21791. }
  21792. url = URL.formatURL(url);
  21793. function clear() {
  21794. WorkerLoader.I.off(url, _this, onload);
  21795. }
  21796. var onload = function (image) {
  21797. clear();
  21798. if (image) {
  21799. _this["onLoaded"](image);
  21800. }
  21801. else {
  21802. WorkerLoader._preLoadFun.call(_this, url);
  21803. }
  21804. };
  21805. WorkerLoader.I.on(url, _this, onload);
  21806. WorkerLoader.I.loadImage(url);
  21807. }
  21808. }
  21809. WorkerLoader.workerPath = "libs/workerloader.js";
  21810. WorkerLoader._enable = false;
  21811. WorkerLoader._tryEnabled = false;
  21812. class Mouse {
  21813. static set cursor(cursorStr) {
  21814. Mouse._style.cursor = cursorStr;
  21815. }
  21816. static get cursor() {
  21817. return Mouse._style.cursor;
  21818. }
  21819. static __init__() {
  21820. }
  21821. static hide() {
  21822. if (Mouse.cursor != "none") {
  21823. Mouse._preCursor = Mouse.cursor;
  21824. Mouse.cursor = "none";
  21825. }
  21826. }
  21827. static show() {
  21828. if (Mouse.cursor == "none") {
  21829. if (Mouse._preCursor) {
  21830. Mouse.cursor = Mouse._preCursor;
  21831. }
  21832. else {
  21833. Mouse.cursor = "auto";
  21834. }
  21835. }
  21836. }
  21837. }
  21838. class MeshParticle2D extends Mesh2D {
  21839. constructor(maxNum) {
  21840. super(MeshParticle2D.const_stride, maxNum * 4 * MeshParticle2D.const_stride, 4);
  21841. this.canReuse = true;
  21842. this.setAttributes(MeshParticle2D._fixattriInfo);
  21843. this.createQuadIB(maxNum);
  21844. this._quadNum = maxNum;
  21845. }
  21846. static __init__() {
  21847. var gl = LayaGL.instance;
  21848. MeshParticle2D._fixattriInfo = [gl.FLOAT, 4, 0,
  21849. gl.FLOAT, 3, 16,
  21850. gl.FLOAT, 3, 28,
  21851. gl.FLOAT, 4, 40,
  21852. gl.FLOAT, 4, 56,
  21853. gl.FLOAT, 3, 72,
  21854. gl.FLOAT, 2, 84,
  21855. gl.FLOAT, 4, 92,
  21856. gl.FLOAT, 1, 108,
  21857. gl.FLOAT, 1, 112];
  21858. }
  21859. setMaxParticleNum(maxNum) {
  21860. this._vb._resizeBuffer(maxNum * 4 * MeshParticle2D.const_stride, false);
  21861. this.createQuadIB(maxNum);
  21862. }
  21863. static getAMesh(maxNum) {
  21864. if (MeshParticle2D._POOL.length) {
  21865. var ret = MeshParticle2D._POOL.pop();
  21866. ret.setMaxParticleNum(maxNum);
  21867. return ret;
  21868. }
  21869. return new MeshParticle2D(maxNum);
  21870. }
  21871. releaseMesh() {
  21872. this._vb.setByteLength(0);
  21873. this.vertNum = 0;
  21874. this.indexNum = 0;
  21875. MeshParticle2D._POOL.push(this);
  21876. }
  21877. destroy() {
  21878. this._ib.destroy();
  21879. this._vb.destroy();
  21880. this._vb.deleteBuffer();
  21881. }
  21882. }
  21883. MeshParticle2D.const_stride = 116;
  21884. MeshParticle2D._POOL = [];
  21885. class HTMLImage extends Bitmap {
  21886. }
  21887. HTMLImage.create = function (width, height, format) {
  21888. var tex = new Texture2D(width, height, format, false, false);
  21889. tex.wrapModeU = BaseTexture.WARPMODE_CLAMP;
  21890. tex.wrapModeV = BaseTexture.WARPMODE_CLAMP;
  21891. return tex;
  21892. };
  21893. class Laya {
  21894. static __init(_classs) {
  21895. _classs.forEach(function (o) { o.__init$ && o.__init$(); });
  21896. }
  21897. static init(width, height, ...plugins) {
  21898. if (Laya._isinit)
  21899. return;
  21900. Laya._isinit = true;
  21901. ArrayBuffer.prototype.slice || (ArrayBuffer.prototype.slice = Laya._arrayBufferSlice);
  21902. Browser.__init__();
  21903. var mainCanv = Browser.mainCanvas = new HTMLCanvas(true);
  21904. var style = mainCanv.source.style;
  21905. style.position = 'absolute';
  21906. style.top = style.left = "0px";
  21907. style.background = "#000000";
  21908. if (!Browser.onKGMiniGame && !Browser.onAlipayMiniGame) {
  21909. Browser.container.appendChild(mainCanv.source);
  21910. }
  21911. Browser.canvas = new HTMLCanvas(true);
  21912. Browser.context = Browser.canvas.getContext('2d');
  21913. Browser.supportWebAudio = SoundManager.__init__();
  21914. Browser.supportLocalStorage = LocalStorage.__init__();
  21915. Laya.systemTimer = new Timer(false);
  21916. exports.systemTimer = Timer.gSysTimer = Laya.systemTimer;
  21917. Laya.startTimer = new Timer(false);
  21918. Laya.physicsTimer = new Timer(false);
  21919. Laya.updateTimer = new Timer(false);
  21920. Laya.lateTimer = new Timer(false);
  21921. Laya.timer = new Timer(false);
  21922. exports.startTimer = ILaya.startTimer = Laya.startTimer;
  21923. exports.lateTimer = ILaya.lateTimer = Laya.lateTimer;
  21924. exports.updateTimer = ILaya.updateTimer = Laya.updateTimer;
  21925. ILaya.systemTimer = Laya.systemTimer;
  21926. exports.timer = ILaya.timer = Laya.timer;
  21927. exports.physicsTimer = ILaya.physicsTimer = Laya.physicsTimer;
  21928. Laya.loader = new LoaderManager();
  21929. ILaya.Laya = Laya;
  21930. exports.loader = ILaya.loader = Laya.loader;
  21931. WeakObject.__init__();
  21932. SceneUtils.__init();
  21933. Mouse.__init__();
  21934. WebGL.inner_enable();
  21935. if (plugins) {
  21936. for (var i = 0, n = plugins.length; i < n; i++) {
  21937. if (plugins[i] && plugins[i].enable) {
  21938. plugins[i].enable();
  21939. }
  21940. }
  21941. }
  21942. if (ILaya.Render.isConchApp) {
  21943. Laya.enableNative();
  21944. }
  21945. Laya.enableWebGLPlus();
  21946. CacheManger.beginCheck();
  21947. exports.stage = Laya.stage = new Stage();
  21948. ILaya.stage = Laya.stage;
  21949. Utils.gStage = Laya.stage;
  21950. URL.rootPath = URL._basePath = Laya._getUrlPath();
  21951. MeshQuadTexture.__int__();
  21952. MeshVG.__init__();
  21953. MeshTexture.__init__();
  21954. Laya.render = new Render(0, 0, Browser.mainCanvas);
  21955. exports.render = Laya.render;
  21956. Laya.stage.size(width, height);
  21957. window.stage = Laya.stage;
  21958. WebGLContext.__init__();
  21959. MeshParticle2D.__init__();
  21960. ShaderCompile.__init__();
  21961. RenderSprite.__init__();
  21962. KeyBoardManager.__init__();
  21963. MouseManager.instance.__init__(Laya.stage, Render.canvas);
  21964. Input.__init__();
  21965. SoundManager.autoStopMusic = true;
  21966. Stat._StatRender = new StatUI();
  21967. Value2D._initone(ShaderDefines2D.TEXTURE2D, TextureSV);
  21968. Value2D._initone(ShaderDefines2D.TEXTURE2D | ShaderDefines2D.FILTERGLOW, TextureSV);
  21969. Value2D._initone(ShaderDefines2D.PRIMITIVE, PrimitiveSV);
  21970. Value2D._initone(ShaderDefines2D.SKINMESH, SkinSV);
  21971. return Render.canvas;
  21972. }
  21973. static _getUrlPath() {
  21974. var location = Browser.window.location;
  21975. var pathName = location.pathname;
  21976. pathName = pathName.charAt(2) == ':' ? pathName.substring(1) : pathName;
  21977. return URL.getPath(location.protocol == "file:" ? pathName : location.protocol + "//" + location.host + location.pathname);
  21978. }
  21979. static _arrayBufferSlice(start, end) {
  21980. var arr = this;
  21981. var arrU8List = new Uint8Array(arr, start, end - start);
  21982. var newU8List = new Uint8Array(arrU8List.length);
  21983. newU8List.set(arrU8List);
  21984. return newU8List.buffer;
  21985. }
  21986. static set alertGlobalError(value) {
  21987. var erralert = 0;
  21988. if (value) {
  21989. Browser.window.onerror = function (msg, url, line, column, detail) {
  21990. if (erralert++ < 5 && detail)
  21991. this.alert("出错啦,请把此信息截图给研发商\n" + msg + "\n" + detail.stack);
  21992. };
  21993. }
  21994. else {
  21995. Browser.window.onerror = null;
  21996. }
  21997. }
  21998. static _runScript(script) {
  21999. return Browser.window[Laya._evcode](script);
  22000. }
  22001. static enableDebugPanel(debugJsPath = "libs/laya.debugtool.js") {
  22002. if (!window['Laya']["DebugPanel"]) {
  22003. var script = Browser.createElement("script");
  22004. script.onload = function () {
  22005. window['Laya']["DebugPanel"].enable();
  22006. };
  22007. script.src = debugJsPath;
  22008. Browser.document.body.appendChild(script);
  22009. }
  22010. else {
  22011. window['Laya']["DebugPanel"].enable();
  22012. }
  22013. }
  22014. static enableWebGLPlus() {
  22015. WebGLContext.__init_native();
  22016. }
  22017. static enableNative() {
  22018. if (Laya.isNativeRender_enable)
  22019. return;
  22020. Laya.isNativeRender_enable = true;
  22021. if (Render.supportWebGLPlusRendering) {
  22022. Shader.prototype.uploadTexture2D = function (value) {
  22023. var gl = LayaGL.instance;
  22024. gl.bindTexture(gl.TEXTURE_2D, value);
  22025. };
  22026. }
  22027. RenderState2D.width = Browser.window.innerWidth;
  22028. RenderState2D.height = Browser.window.innerHeight;
  22029. Browser.measureText = function (txt, font) {
  22030. window["conchTextCanvas"].font = font;
  22031. return window["conchTextCanvas"].measureText(txt);
  22032. };
  22033. Stage.clear = function (color) {
  22034. Context.set2DRenderConfig();
  22035. var c = ColorUtils.create(color).arrColor;
  22036. var gl = LayaGL.instance;
  22037. if (c)
  22038. gl.clearColor(c[0], c[1], c[2], c[3]);
  22039. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
  22040. RenderState2D.clear();
  22041. };
  22042. Sprite.drawToCanvas = Sprite.drawToTexture = function (sprite, _renderType, canvasWidth, canvasHeight, offsetX, offsetY) {
  22043. offsetX -= sprite.x;
  22044. offsetY -= sprite.y;
  22045. offsetX |= 0;
  22046. offsetY |= 0;
  22047. canvasWidth |= 0;
  22048. canvasHeight |= 0;
  22049. var canv = new HTMLCanvas(false);
  22050. var ctx = canv.getContext('2d');
  22051. canv.size(canvasWidth, canvasHeight);
  22052. ctx.asBitmap = true;
  22053. ctx._targets.start();
  22054. RenderSprite.renders[_renderType]._fun(sprite, ctx, offsetX, offsetY);
  22055. ctx.flush();
  22056. ctx._targets.end();
  22057. ctx._targets.restore();
  22058. return canv;
  22059. };
  22060. Object["defineProperty"](RenderTexture2D.prototype, "uv", {
  22061. "get": function () {
  22062. return this._uv;
  22063. },
  22064. "set": function (v) {
  22065. this._uv = v;
  22066. }
  22067. });
  22068. HTMLCanvas.prototype.getTexture = function () {
  22069. if (!this._texture) {
  22070. this._texture = this.context._targets;
  22071. this._texture.uv = RenderTexture2D.flipyuv;
  22072. this._texture.bitmap = this._texture;
  22073. }
  22074. return this._texture;
  22075. };
  22076. }
  22077. }
  22078. Laya.stage = null;
  22079. Laya.systemTimer = null;
  22080. Laya.startTimer = null;
  22081. Laya.physicsTimer = null;
  22082. Laya.updateTimer = null;
  22083. Laya.lateTimer = null;
  22084. Laya.timer = null;
  22085. Laya.loader = null;
  22086. Laya.version = "2.4.0";
  22087. Laya._isinit = false;
  22088. Laya.isWXOpenDataContext = false;
  22089. Laya.isWXPosMsg = false;
  22090. Laya.__classmap = null;
  22091. Laya.Config = Config;
  22092. Laya.TextRender = TextRender;
  22093. Laya.EventDispatcher = EventDispatcher;
  22094. Laya.SoundChannel = SoundChannel;
  22095. Laya.Stage = Stage;
  22096. Laya.Render = Render;
  22097. Laya.Browser = Browser;
  22098. Laya.Sprite = Sprite;
  22099. Laya.Node = Node;
  22100. Laya.Context = Context;
  22101. Laya.WebGL = WebGL;
  22102. Laya.Handler = Handler;
  22103. Laya.RunDriver = RunDriver;
  22104. Laya.Utils = Utils;
  22105. Laya.Input = Input;
  22106. Laya.Loader = Loader;
  22107. Laya.LocalStorage = LocalStorage;
  22108. Laya.SoundManager = SoundManager;
  22109. Laya.URL = URL;
  22110. Laya.Event = Event;
  22111. Laya.Matrix = Matrix;
  22112. Laya.HTMLImage = HTMLImage;
  22113. Laya.Laya = Laya;
  22114. Laya._evcode = "eva" + "l";
  22115. Laya.isNativeRender_enable = false;
  22116. Laya.__classmap = ILaya.__classMap;
  22117. ILaya.Timer = Timer;
  22118. ILaya.Dragging = Dragging;
  22119. ILaya.GraphicsBounds = GraphicsBounds;
  22120. ILaya.Sprite = Sprite;
  22121. ILaya.TextRender = TextRender;
  22122. ILaya.Loader = Loader;
  22123. ILaya.TTFLoader = TTFLoader;
  22124. ILaya.WebAudioSound = WebAudioSound;
  22125. ILaya.SoundManager = SoundManager;
  22126. ILaya.ShaderCompile = ShaderCompile;
  22127. ILaya.ClassUtils = ClassUtils;
  22128. ILaya.SceneUtils = SceneUtils;
  22129. ILaya.Context = Context;
  22130. ILaya.Render = Render;
  22131. ILaya.MouseManager = MouseManager;
  22132. ILaya.Text = Text;
  22133. ILaya.Browser = Browser;
  22134. ILaya.WebGL = WebGL;
  22135. ILaya.AudioSound = AudioSound;
  22136. ILaya.Pool = Pool;
  22137. ILaya.Utils = Utils;
  22138. ILaya.Graphics = Graphics;
  22139. ILaya.Submit = Submit;
  22140. ILaya.Stage = Stage;
  22141. ILaya.Resource = Resource;
  22142. ILaya.WorkerLoader = WorkerLoader;
  22143. var libs = window._layalibs;
  22144. if (libs) {
  22145. libs.sort(function (a, b) {
  22146. return a.i - b.i;
  22147. });
  22148. for (var j = 0; j < libs.length; j++) {
  22149. libs[j].f(window, window.document, Laya);
  22150. }
  22151. }
  22152. let win = window;
  22153. if (win.Laya) {
  22154. win.Laya.Laya = Laya;
  22155. Object.assign(win.Laya, Laya);
  22156. }
  22157. else
  22158. win.Laya = Laya;
  22159. var __init = Laya.__init;
  22160. var init = Laya.init;
  22161. var version = Laya.version;
  22162. var isWXOpenDataContext;
  22163. var isWXPosMsg;
  22164. var alertGlobalError = Laya.alertGlobalError;
  22165. var enableDebugPanel = Laya.enableDebugPanel;
  22166. function _static(_class, def) {
  22167. for (var i = 0, sz = def.length; i < sz; i += 2) {
  22168. if (def[i] == 'length')
  22169. _class.length = def[i + 1].call(_class);
  22170. else {
  22171. function tmp() {
  22172. var name = def[i];
  22173. var getfn = def[i + 1];
  22174. Object.defineProperty(_class, name, {
  22175. get: function () { delete this[name]; return this[name] = getfn.call(this); },
  22176. set: function (v) { delete this[name]; this[name] = v; }, enumerable: true, configurable: true
  22177. });
  22178. }
  22179. tmp();
  22180. }
  22181. }
  22182. }
  22183. class CommonScript extends Component {
  22184. get isSingleton() {
  22185. return false;
  22186. }
  22187. constructor() {
  22188. super();
  22189. }
  22190. onAwake() {
  22191. }
  22192. onEnable() {
  22193. }
  22194. onStart() {
  22195. }
  22196. onUpdate() {
  22197. }
  22198. onLateUpdate() {
  22199. }
  22200. onDisable() {
  22201. }
  22202. onDestroy() {
  22203. }
  22204. }
  22205. class Script extends Component {
  22206. get isSingleton() {
  22207. return false;
  22208. }
  22209. _onAwake() {
  22210. this.onAwake();
  22211. if (this.onStart !== Script.prototype.onStart) {
  22212. ILaya.startTimer.callLater(this, this.onStart);
  22213. }
  22214. }
  22215. _onEnable() {
  22216. var proto = Script.prototype;
  22217. if (this.onTriggerEnter !== proto.onTriggerEnter) {
  22218. this.owner.on(Event.TRIGGER_ENTER, this, this.onTriggerEnter);
  22219. }
  22220. if (this.onTriggerStay !== proto.onTriggerStay) {
  22221. this.owner.on(Event.TRIGGER_STAY, this, this.onTriggerStay);
  22222. }
  22223. if (this.onTriggerExit !== proto.onTriggerExit) {
  22224. this.owner.on(Event.TRIGGER_EXIT, this, this.onTriggerExit);
  22225. }
  22226. if (this.onMouseDown !== proto.onMouseDown) {
  22227. this.owner.on(Event.MOUSE_DOWN, this, this.onMouseDown);
  22228. }
  22229. if (this.onMouseUp !== proto.onMouseUp) {
  22230. this.owner.on(Event.MOUSE_UP, this, this.onMouseUp);
  22231. }
  22232. if (this.onClick !== proto.onClick) {
  22233. this.owner.on(Event.CLICK, this, this.onClick);
  22234. }
  22235. if (this.onStageMouseDown !== proto.onStageMouseDown) {
  22236. ILaya.stage.on(Event.MOUSE_DOWN, this, this.onStageMouseDown);
  22237. }
  22238. if (this.onStageMouseUp !== proto.onStageMouseUp) {
  22239. ILaya.stage.on(Event.MOUSE_UP, this, this.onStageMouseUp);
  22240. }
  22241. if (this.onStageClick !== proto.onStageClick) {
  22242. ILaya.stage.on(Event.CLICK, this, this.onStageClick);
  22243. }
  22244. if (this.onStageMouseMove !== proto.onStageMouseMove) {
  22245. ILaya.stage.on(Event.MOUSE_MOVE, this, this.onStageMouseMove);
  22246. }
  22247. if (this.onDoubleClick !== proto.onDoubleClick) {
  22248. this.owner.on(Event.DOUBLE_CLICK, this, this.onDoubleClick);
  22249. }
  22250. if (this.onRightClick !== proto.onRightClick) {
  22251. this.owner.on(Event.RIGHT_CLICK, this, this.onRightClick);
  22252. }
  22253. if (this.onMouseMove !== proto.onMouseMove) {
  22254. this.owner.on(Event.MOUSE_MOVE, this, this.onMouseMove);
  22255. }
  22256. if (this.onMouseOver !== proto.onMouseOver) {
  22257. this.owner.on(Event.MOUSE_OVER, this, this.onMouseOver);
  22258. }
  22259. if (this.onMouseOut !== proto.onMouseOut) {
  22260. this.owner.on(Event.MOUSE_OUT, this, this.onMouseOut);
  22261. }
  22262. if (this.onKeyDown !== proto.onKeyDown) {
  22263. ILaya.stage.on(Event.KEY_DOWN, this, this.onKeyDown);
  22264. }
  22265. if (this.onKeyPress !== proto.onKeyPress) {
  22266. ILaya.stage.on(Event.KEY_PRESS, this, this.onKeyPress);
  22267. }
  22268. if (this.onKeyUp !== proto.onKeyUp) {
  22269. ILaya.stage.on(Event.KEY_UP, this, this.onKeyUp);
  22270. }
  22271. if (this.onUpdate !== proto.onUpdate) {
  22272. ILaya.updateTimer.frameLoop(1, this, this.onUpdate);
  22273. }
  22274. if (this.onLateUpdate !== proto.onLateUpdate) {
  22275. ILaya.lateTimer.frameLoop(1, this, this.onLateUpdate);
  22276. }
  22277. if (this.onPreRender !== proto.onPreRender) {
  22278. ILaya.lateTimer.frameLoop(1, this, this.onPreRender);
  22279. }
  22280. }
  22281. _onDisable() {
  22282. this.owner.offAllCaller(this);
  22283. ILaya.stage.offAllCaller(this);
  22284. ILaya.startTimer.clearAll(this);
  22285. ILaya.updateTimer.clearAll(this);
  22286. ILaya.lateTimer.clearAll(this);
  22287. }
  22288. _isScript() {
  22289. return true;
  22290. }
  22291. _onDestroy() {
  22292. this.onDestroy();
  22293. }
  22294. onAwake() {
  22295. }
  22296. onEnable() {
  22297. }
  22298. onStart() {
  22299. }
  22300. onTriggerEnter(other, self, contact) {
  22301. }
  22302. onTriggerStay(other, self, contact) {
  22303. }
  22304. onTriggerExit(other, self, contact) {
  22305. }
  22306. onMouseDown(e) {
  22307. }
  22308. onMouseUp(e) {
  22309. }
  22310. onClick(e) {
  22311. }
  22312. onStageMouseDown(e) {
  22313. }
  22314. onStageMouseUp(e) {
  22315. }
  22316. onStageClick(e) {
  22317. }
  22318. onStageMouseMove(e) {
  22319. }
  22320. onDoubleClick(e) {
  22321. }
  22322. onRightClick(e) {
  22323. }
  22324. onMouseMove(e) {
  22325. }
  22326. onMouseOver(e) {
  22327. }
  22328. onMouseOut(e) {
  22329. }
  22330. onKeyDown(e) {
  22331. }
  22332. onKeyPress(e) {
  22333. }
  22334. onKeyUp(e) {
  22335. }
  22336. onUpdate() {
  22337. }
  22338. onLateUpdate() {
  22339. }
  22340. onPreRender() {
  22341. }
  22342. onPostRender() {
  22343. }
  22344. onDisable() {
  22345. }
  22346. onDestroy() {
  22347. }
  22348. }
  22349. class GraphicAnimation extends FrameAnimation {
  22350. constructor() {
  22351. super(...arguments);
  22352. this._nodeIDAniDic = {};
  22353. }
  22354. _parseNodeList(uiView) {
  22355. if (!this._nodeList)
  22356. this._nodeList = [];
  22357. this._nodeDefaultProps[uiView.compId] = uiView.props;
  22358. if (uiView.compId)
  22359. this._nodeList.push(uiView.compId);
  22360. var childs = uiView.child;
  22361. if (childs) {
  22362. var i, len = childs.length;
  22363. for (i = 0; i < len; i++) {
  22364. this._parseNodeList(childs[i]);
  22365. }
  22366. }
  22367. }
  22368. _calGraphicData(aniData) {
  22369. this._setUp(null, aniData);
  22370. this._createGraphicData();
  22371. if (this._nodeIDAniDic) {
  22372. var key;
  22373. for (key in this._nodeIDAniDic) {
  22374. this._nodeIDAniDic[key] = null;
  22375. }
  22376. }
  22377. }
  22378. _createGraphicData() {
  22379. var gList = [];
  22380. var i, len = this.count;
  22381. var animationDataNew = this._usedFrames;
  22382. if (!animationDataNew)
  22383. animationDataNew = [];
  22384. var preGraphic;
  22385. for (i = 0; i < len; i++) {
  22386. if (animationDataNew[i] || !preGraphic) {
  22387. preGraphic = this._createFrameGraphic(i);
  22388. }
  22389. gList.push(preGraphic);
  22390. }
  22391. this._gList = gList;
  22392. }
  22393. _createFrameGraphic(frame) {
  22394. var g = new Graphics();
  22395. if (!GraphicAnimation._rootMatrix)
  22396. GraphicAnimation._rootMatrix = new Matrix();
  22397. this._updateNodeGraphic(this._rootNode, frame, GraphicAnimation._rootMatrix, g);
  22398. return g;
  22399. }
  22400. _updateNodeGraphic(node, frame, parentTransfrom, g, alpha = 1) {
  22401. var tNodeG;
  22402. tNodeG = this._nodeGDic[node.compId] = this._getNodeGraphicData(node.compId, frame, this._nodeGDic[node.compId]);
  22403. if (!tNodeG.resultTransform)
  22404. tNodeG.resultTransform = new Matrix();
  22405. var tResultTransform;
  22406. tResultTransform = tNodeG.resultTransform;
  22407. Matrix.mul(tNodeG.transform, parentTransfrom, tResultTransform);
  22408. var tTex;
  22409. var tGraphicAlpha = tNodeG.alpha * alpha;
  22410. if (tGraphicAlpha < 0.01)
  22411. return;
  22412. if (tNodeG.skin) {
  22413. tTex = this._getTextureByUrl(tNodeG.skin);
  22414. if (tTex) {
  22415. if (tResultTransform._checkTransform()) {
  22416. g.drawTexture(tTex, 0, 0, tNodeG.width, tNodeG.height, tResultTransform, tGraphicAlpha);
  22417. tNodeG.resultTransform = null;
  22418. }
  22419. else {
  22420. g.drawTexture(tTex, tResultTransform.tx, tResultTransform.ty, tNodeG.width, tNodeG.height, null, tGraphicAlpha);
  22421. }
  22422. }
  22423. }
  22424. var childs = node.child;
  22425. if (!childs)
  22426. return;
  22427. var i, len;
  22428. len = childs.length;
  22429. for (i = 0; i < len; i++) {
  22430. this._updateNodeGraphic(childs[i], frame, tResultTransform, g, tGraphicAlpha);
  22431. }
  22432. }
  22433. _updateNoChilds(tNodeG, g) {
  22434. if (!tNodeG.skin)
  22435. return;
  22436. var tTex = this._getTextureByUrl(tNodeG.skin);
  22437. if (!tTex)
  22438. return;
  22439. var tTransform = tNodeG.transform;
  22440. tTransform._checkTransform();
  22441. var onlyTranslate;
  22442. onlyTranslate = !tTransform._bTransform;
  22443. if (!onlyTranslate) {
  22444. g.drawTexture(tTex, 0, 0, tNodeG.width, tNodeG.height, tTransform.clone(), tNodeG.alpha);
  22445. }
  22446. else {
  22447. g.drawTexture(tTex, tTransform.tx, tTransform.ty, tNodeG.width, tNodeG.height, null, tNodeG.alpha);
  22448. }
  22449. }
  22450. _updateNodeGraphic2(node, frame, g) {
  22451. var tNodeG;
  22452. tNodeG = this._nodeGDic[node.compId] = this._getNodeGraphicData(node.compId, frame, this._nodeGDic[node.compId]);
  22453. if (!node.child) {
  22454. this._updateNoChilds(tNodeG, g);
  22455. return;
  22456. }
  22457. var tTransform = tNodeG.transform;
  22458. tTransform._checkTransform();
  22459. var onlyTranslate;
  22460. onlyTranslate = !tTransform._bTransform;
  22461. var hasTrans;
  22462. hasTrans = onlyTranslate && (tTransform.tx != 0 || tTransform.ty != 0);
  22463. var ifSave;
  22464. ifSave = (tTransform._bTransform) || tNodeG.alpha != 1;
  22465. if (ifSave)
  22466. g.save();
  22467. if (tNodeG.alpha != 1)
  22468. g.alpha(tNodeG.alpha);
  22469. if (!onlyTranslate)
  22470. g.transform(tTransform.clone());
  22471. else if (hasTrans)
  22472. g.translate(tTransform.tx, tTransform.ty);
  22473. var childs = node.child;
  22474. var tTex;
  22475. if (tNodeG.skin) {
  22476. tTex = this._getTextureByUrl(tNodeG.skin);
  22477. if (tTex) {
  22478. g.drawImage(tTex, 0, 0, tNodeG.width, tNodeG.height);
  22479. }
  22480. }
  22481. if (childs) {
  22482. var i, len;
  22483. len = childs.length;
  22484. for (i = 0; i < len; i++) {
  22485. this._updateNodeGraphic2(childs[i], frame, g);
  22486. }
  22487. }
  22488. if (ifSave) {
  22489. g.restore();
  22490. }
  22491. else {
  22492. if (!onlyTranslate) {
  22493. g.transform(tTransform.clone().invert());
  22494. }
  22495. else if (hasTrans) {
  22496. g.translate(-tTransform.tx, -tTransform.ty);
  22497. }
  22498. }
  22499. }
  22500. _calculateKeyFrames(node) {
  22501. super._calculateKeyFrames(node);
  22502. this._nodeIDAniDic[node.target] = node;
  22503. }
  22504. getNodeDataByID(nodeID) {
  22505. return this._nodeIDAniDic[nodeID];
  22506. }
  22507. _getParams(obj, params, frame, obj2) {
  22508. var rst = GraphicAnimation._temParam;
  22509. rst.length = params.length;
  22510. var i, len = params.length;
  22511. for (i = 0; i < len; i++) {
  22512. rst[i] = this._getObjVar(obj, params[i][0], frame, params[i][1], obj2);
  22513. }
  22514. return rst;
  22515. }
  22516. _getObjVar(obj, key, frame, noValue, obj2) {
  22517. if (key in obj) {
  22518. var vArr = obj[key];
  22519. if (frame >= vArr.length)
  22520. frame = vArr.length - 1;
  22521. return obj[key][frame];
  22522. }
  22523. if (key in obj2) {
  22524. return obj2[key];
  22525. }
  22526. return noValue;
  22527. }
  22528. _getNodeGraphicData(nodeID, frame, rst) {
  22529. if (!rst)
  22530. rst = new GraphicNode();
  22531. if (!rst.transform) {
  22532. rst.transform = new Matrix();
  22533. }
  22534. else {
  22535. rst.transform.identity();
  22536. }
  22537. var node = this.getNodeDataByID(nodeID);
  22538. if (!node)
  22539. return rst;
  22540. var frameData = node.frames;
  22541. var params = this._getParams(frameData, GraphicAnimation._drawTextureCmd, frame, this._nodeDefaultProps[nodeID]);
  22542. var url = params[0];
  22543. var width, height;
  22544. var px = params[5], py = params[6];
  22545. var aX = params[13], aY = params[14];
  22546. var sx = params[7], sy = params[8];
  22547. var rotate = params[9];
  22548. var skewX = params[11], skewY = params[12];
  22549. width = params[3];
  22550. height = params[4];
  22551. if (width == 0 || height == 0)
  22552. url = null;
  22553. if (width == -1)
  22554. width = 0;
  22555. if (height == -1)
  22556. height = 0;
  22557. var tex;
  22558. rst.skin = url;
  22559. rst.width = width;
  22560. rst.height = height;
  22561. if (url) {
  22562. tex = this._getTextureByUrl(url);
  22563. if (tex) {
  22564. if (!width)
  22565. width = tex.sourceWidth;
  22566. if (!height)
  22567. height = tex.sourceHeight;
  22568. }
  22569. else {
  22570. console.warn("lost skin:", url, ",you may load pics first");
  22571. }
  22572. }
  22573. rst.alpha = params[10];
  22574. var m = rst.transform;
  22575. if (aX != 0) {
  22576. px = aX * width;
  22577. }
  22578. if (aY != 0) {
  22579. py = aY * height;
  22580. }
  22581. if (px != 0 || py != 0) {
  22582. m.translate(-px, -py);
  22583. }
  22584. var tm = null;
  22585. if (rotate || sx !== 1 || sy !== 1 || skewX || skewY) {
  22586. tm = GraphicAnimation._tempMt;
  22587. tm.identity();
  22588. tm._bTransform = true;
  22589. var skx = (rotate - skewX) * 0.0174532922222222;
  22590. var sky = (rotate + skewY) * 0.0174532922222222;
  22591. var cx = Math.cos(sky);
  22592. var ssx = Math.sin(sky);
  22593. var cy = Math.sin(skx);
  22594. var ssy = Math.cos(skx);
  22595. tm.a = sx * cx;
  22596. tm.b = sx * ssx;
  22597. tm.c = -sy * cy;
  22598. tm.d = sy * ssy;
  22599. tm.tx = tm.ty = 0;
  22600. }
  22601. if (tm) {
  22602. m = Matrix.mul(m, tm, m);
  22603. }
  22604. m.translate(params[1], params[2]);
  22605. return rst;
  22606. }
  22607. _getTextureByUrl(url) {
  22608. return Loader.getRes(url);
  22609. }
  22610. setAniData(uiView, aniName = null) {
  22611. if (uiView.animations) {
  22612. this._nodeDefaultProps = {};
  22613. this._nodeGDic = {};
  22614. if (this._nodeList)
  22615. this._nodeList.length = 0;
  22616. this._rootNode = uiView;
  22617. this._parseNodeList(uiView);
  22618. var aniDic = {};
  22619. var anilist = [];
  22620. var animations = uiView.animations;
  22621. var i, len = animations.length;
  22622. var tAniO;
  22623. for (i = 0; i < len; i++) {
  22624. tAniO = animations[i];
  22625. this._labels = null;
  22626. if (aniName && aniName != tAniO.name) {
  22627. continue;
  22628. }
  22629. if (!tAniO)
  22630. continue;
  22631. try {
  22632. this._calGraphicData(tAniO);
  22633. }
  22634. catch (e) {
  22635. console.warn("parse animation fail:" + tAniO.name + ",empty animation created");
  22636. this._gList = [];
  22637. }
  22638. var frameO = {};
  22639. frameO.interval = 1000 / tAniO["frameRate"];
  22640. frameO.frames = this._gList;
  22641. frameO.labels = this._labels;
  22642. frameO.name = tAniO.name;
  22643. anilist.push(frameO);
  22644. aniDic[tAniO.name] = frameO;
  22645. }
  22646. this.animationList = anilist;
  22647. this.animationDic = aniDic;
  22648. }
  22649. GraphicAnimation._temParam.length = 0;
  22650. }
  22651. parseByData(aniData) {
  22652. var rootNode, aniO;
  22653. rootNode = aniData.nodeRoot;
  22654. aniO = aniData.aniO;
  22655. delete aniData.nodeRoot;
  22656. delete aniData.aniO;
  22657. this._nodeDefaultProps = {};
  22658. this._nodeGDic = {};
  22659. if (this._nodeList)
  22660. this._nodeList.length = 0;
  22661. this._rootNode = rootNode;
  22662. this._parseNodeList(rootNode);
  22663. this._labels = null;
  22664. try {
  22665. this._calGraphicData(aniO);
  22666. }
  22667. catch (e) {
  22668. console.warn("parse animation fail:" + aniO.name + ",empty animation created");
  22669. this._gList = [];
  22670. }
  22671. var frameO = aniData;
  22672. frameO.interval = 1000 / aniO["frameRate"];
  22673. frameO.frames = this._gList;
  22674. frameO.labels = this._labels;
  22675. frameO.name = aniO.name;
  22676. return frameO;
  22677. }
  22678. setUpAniData(uiView) {
  22679. if (uiView.animations) {
  22680. var aniDic = {};
  22681. var anilist = [];
  22682. var animations = uiView.animations;
  22683. var i, len = animations.length;
  22684. var tAniO;
  22685. for (i = 0; i < len; i++) {
  22686. tAniO = animations[i];
  22687. if (!tAniO)
  22688. continue;
  22689. var frameO = {};
  22690. frameO.name = tAniO.name;
  22691. frameO.aniO = tAniO;
  22692. frameO.nodeRoot = uiView;
  22693. anilist.push(frameO);
  22694. aniDic[tAniO.name] = frameO;
  22695. }
  22696. this.animationList = anilist;
  22697. this.animationDic = aniDic;
  22698. }
  22699. }
  22700. _clear() {
  22701. this.animationList = null;
  22702. this.animationDic = null;
  22703. this._gList = null;
  22704. this._nodeGDic = null;
  22705. }
  22706. static parseAnimationByData(animationObject) {
  22707. if (!GraphicAnimation._I)
  22708. GraphicAnimation._I = new GraphicAnimation();
  22709. var rst;
  22710. rst = GraphicAnimation._I.parseByData(animationObject);
  22711. GraphicAnimation._I._clear();
  22712. return rst;
  22713. }
  22714. static parseAnimationData(aniData) {
  22715. if (!GraphicAnimation._I)
  22716. GraphicAnimation._I = new GraphicAnimation();
  22717. GraphicAnimation._I.setUpAniData(aniData);
  22718. var rst;
  22719. rst = {};
  22720. rst.animationList = GraphicAnimation._I.animationList;
  22721. rst.animationDic = GraphicAnimation._I.animationDic;
  22722. GraphicAnimation._I._clear();
  22723. return rst;
  22724. }
  22725. }
  22726. 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]];
  22727. GraphicAnimation._temParam = [];
  22728. GraphicAnimation._tempMt = new Matrix();
  22729. class GraphicNode {
  22730. constructor() {
  22731. this.alpha = 1;
  22732. }
  22733. }
  22734. class Animation extends AnimationBase {
  22735. constructor() {
  22736. super();
  22737. this._setControlNode(this);
  22738. }
  22739. destroy(destroyChild = true) {
  22740. this.stop();
  22741. super.destroy(destroyChild);
  22742. this._frames = null;
  22743. this._labels = null;
  22744. }
  22745. play(start = 0, loop = true, name = "") {
  22746. if (name)
  22747. this._setFramesFromCache(name, true);
  22748. super.play(start, loop, name);
  22749. }
  22750. _setFramesFromCache(name, showWarn = false) {
  22751. if (this._url)
  22752. name = this._url + "#" + name;
  22753. if (name && Animation.framesMap[name]) {
  22754. var tAniO = Animation.framesMap[name];
  22755. if (tAniO instanceof Array) {
  22756. this._frames = Animation.framesMap[name];
  22757. this._count = this._frames.length;
  22758. }
  22759. else {
  22760. if (tAniO.nodeRoot) {
  22761. Animation.framesMap[name] = GraphicAnimation.parseAnimationByData(tAniO);
  22762. tAniO = Animation.framesMap[name];
  22763. }
  22764. this._frames = tAniO.frames;
  22765. this._count = this._frames.length;
  22766. if (!this._frameRateChanged)
  22767. this._interval = tAniO.interval;
  22768. this._labels = this._copyLabels(tAniO.labels);
  22769. }
  22770. return true;
  22771. }
  22772. else {
  22773. if (showWarn)
  22774. console.log("ani not found:", name);
  22775. }
  22776. return false;
  22777. }
  22778. _copyLabels(labels) {
  22779. if (!labels)
  22780. return null;
  22781. var rst;
  22782. rst = {};
  22783. var key;
  22784. for (key in labels) {
  22785. rst[key] = Utils.copyArray([], labels[key]);
  22786. }
  22787. return rst;
  22788. }
  22789. _frameLoop() {
  22790. if (this._visible && this._style.alpha > 0.01 && this._frames) {
  22791. super._frameLoop();
  22792. }
  22793. }
  22794. _displayToIndex(value) {
  22795. if (this._frames)
  22796. this.graphics = this._frames[value];
  22797. }
  22798. get frames() {
  22799. return this._frames;
  22800. }
  22801. set frames(value) {
  22802. this._frames = value;
  22803. if (value) {
  22804. this._count = value.length;
  22805. if (this._actionName)
  22806. this._setFramesFromCache(this._actionName, true);
  22807. this.index = this._index;
  22808. }
  22809. }
  22810. set source(value) {
  22811. if (value.indexOf(".ani") > -1)
  22812. this.loadAnimation(value);
  22813. else if (value.indexOf(".json") > -1 || value.indexOf("als") > -1 || value.indexOf("atlas") > -1)
  22814. this.loadAtlas(value);
  22815. else
  22816. this.loadImages(value.split(","));
  22817. }
  22818. set autoAnimation(value) {
  22819. this.play(0, true, value);
  22820. }
  22821. set autoPlay(value) {
  22822. if (value)
  22823. this.play();
  22824. else
  22825. this.stop();
  22826. }
  22827. clear() {
  22828. super.clear();
  22829. this.stop();
  22830. this.graphics = null;
  22831. this._frames = null;
  22832. this._labels = null;
  22833. return this;
  22834. }
  22835. loadImages(urls, cacheName = "") {
  22836. this._url = "";
  22837. if (!this._setFramesFromCache(cacheName)) {
  22838. this.frames = Animation.framesMap[cacheName] ? Animation.framesMap[cacheName] : Animation.createFrames(urls, cacheName);
  22839. }
  22840. return this;
  22841. }
  22842. loadAtlas(url, loaded = null, cacheName = "") {
  22843. this._url = "";
  22844. var _this = this;
  22845. if (!_this._setFramesFromCache(cacheName)) {
  22846. function onLoaded(loadUrl) {
  22847. if (url === loadUrl) {
  22848. _this.frames = Animation.framesMap[cacheName] ? Animation.framesMap[cacheName] : Animation.createFrames(url, cacheName);
  22849. if (loaded)
  22850. loaded.run();
  22851. }
  22852. }
  22853. if (Loader.getAtlas(url))
  22854. onLoaded(url);
  22855. else
  22856. ILaya.loader.load(url, Handler.create(null, onLoaded, [url]), null, Loader.ATLAS);
  22857. }
  22858. return this;
  22859. }
  22860. loadAnimation(url, loaded = null, atlas = null) {
  22861. this._url = url;
  22862. var _this = this;
  22863. if (!this._actionName)
  22864. this._actionName = "";
  22865. if (!_this._setFramesFromCache(this._actionName)) {
  22866. if (!atlas || Loader.getAtlas(atlas)) {
  22867. this._loadAnimationData(url, loaded, atlas);
  22868. }
  22869. else {
  22870. ILaya.loader.load(atlas, Handler.create(this, this._loadAnimationData, [url, loaded, atlas]), null, Loader.ATLAS);
  22871. }
  22872. }
  22873. else {
  22874. _this._setFramesFromCache(this._actionName, true);
  22875. this.index = 0;
  22876. if (loaded)
  22877. loaded.run();
  22878. }
  22879. return this;
  22880. }
  22881. _loadAnimationData(url, loaded = null, atlas = null) {
  22882. if (atlas && !Loader.getAtlas(atlas)) {
  22883. console.warn("atlas load fail:" + atlas);
  22884. return;
  22885. }
  22886. var _this = this;
  22887. function onLoaded(loadUrl) {
  22888. if (!Loader.getRes(loadUrl)) {
  22889. if (Animation.framesMap[url + "#"]) {
  22890. _this._setFramesFromCache(_this._actionName, true);
  22891. _this.index = 0;
  22892. _this._resumePlay();
  22893. if (loaded)
  22894. loaded.run();
  22895. }
  22896. return;
  22897. }
  22898. if (url === loadUrl) {
  22899. var tAniO;
  22900. if (!Animation.framesMap[url + "#"]) {
  22901. var aniData = GraphicAnimation.parseAnimationData(Loader.getRes(url));
  22902. if (!aniData)
  22903. return;
  22904. var aniList = aniData.animationList;
  22905. var i, len = aniList.length;
  22906. var defaultO;
  22907. for (i = 0; i < len; i++) {
  22908. tAniO = aniList[i];
  22909. Animation.framesMap[url + "#" + tAniO.name] = tAniO;
  22910. if (!defaultO)
  22911. defaultO = tAniO;
  22912. }
  22913. if (defaultO) {
  22914. Animation.framesMap[url + "#"] = defaultO;
  22915. _this._setFramesFromCache(_this._actionName, true);
  22916. _this.index = 0;
  22917. }
  22918. _this._resumePlay();
  22919. }
  22920. else {
  22921. _this._setFramesFromCache(_this._actionName, true);
  22922. _this.index = 0;
  22923. _this._resumePlay();
  22924. }
  22925. if (loaded)
  22926. loaded.run();
  22927. }
  22928. Loader.clearRes(url);
  22929. }
  22930. if (Loader.getRes(url))
  22931. onLoaded(url);
  22932. else
  22933. ILaya.loader.load(url, Handler.create(null, onLoaded, [url]), null, Loader.JSON);
  22934. }
  22935. static createFrames(url, name) {
  22936. var arr;
  22937. if (typeof (url) == 'string') {
  22938. var atlas = Loader.getAtlas(url);
  22939. if (atlas && atlas.length) {
  22940. arr = [];
  22941. for (var i = 0, n = atlas.length; i < n; i++) {
  22942. var g = new Graphics();
  22943. g.drawImage(Loader.getRes(atlas[i]), 0, 0);
  22944. arr.push(g);
  22945. }
  22946. }
  22947. }
  22948. else if (url instanceof Array) {
  22949. arr = [];
  22950. for (i = 0, n = url.length; i < n; i++) {
  22951. g = new Graphics();
  22952. g.loadImage(url[i], 0, 0);
  22953. arr.push(g);
  22954. }
  22955. }
  22956. if (name)
  22957. Animation.framesMap[name] = arr;
  22958. return arr;
  22959. }
  22960. static clearCache(key) {
  22961. var cache = Animation.framesMap;
  22962. var val;
  22963. var key2 = key + "#";
  22964. for (val in cache) {
  22965. if (val === key || val.indexOf(key2) === 0) {
  22966. delete Animation.framesMap[val];
  22967. }
  22968. }
  22969. }
  22970. }
  22971. Animation.framesMap = {};
  22972. ILaya.regClass(Animation);
  22973. ClassUtils.regClass("laya.display.Animation", Animation);
  22974. ClassUtils.regClass("Laya.Animation", Animation);
  22975. class EffectAnimation extends FrameAnimation {
  22976. constructor() {
  22977. super(...arguments);
  22978. this._initData = {};
  22979. }
  22980. set target(v) {
  22981. if (this._target)
  22982. this._target.off(EffectAnimation.EFFECT_BEGIN, this, this._onOtherBegin);
  22983. this._target = v;
  22984. if (this._target)
  22985. this._target.on(EffectAnimation.EFFECT_BEGIN, this, this._onOtherBegin);
  22986. this._addEvent();
  22987. }
  22988. get target() {
  22989. return this._target;
  22990. }
  22991. _onOtherBegin(effect) {
  22992. if (effect === this)
  22993. return;
  22994. this.stop();
  22995. }
  22996. set playEvent(event) {
  22997. this._playEvent = event;
  22998. if (!event)
  22999. return;
  23000. this._addEvent();
  23001. }
  23002. _addEvent() {
  23003. if (!this._target || !this._playEvent)
  23004. return;
  23005. this._setControlNode(this._target);
  23006. this._target.on(this._playEvent, this, this._onPlayAction);
  23007. }
  23008. _onPlayAction() {
  23009. this.play(0, false);
  23010. }
  23011. play(start = 0, loop = true, name = "") {
  23012. if (!this._target)
  23013. return;
  23014. this._target.event(EffectAnimation.EFFECT_BEGIN, [this]);
  23015. this._recordInitData();
  23016. super.play(start, loop, name);
  23017. }
  23018. _recordInitData() {
  23019. if (!this._aniKeys)
  23020. return;
  23021. var i, len;
  23022. len = this._aniKeys.length;
  23023. var key;
  23024. for (i = 0; i < len; i++) {
  23025. key = this._aniKeys[i];
  23026. this._initData[key] = this._target[key];
  23027. }
  23028. }
  23029. set effectClass(classStr) {
  23030. this._effectClass = ClassUtils.getClass(classStr);
  23031. if (this._effectClass) {
  23032. var uiData = this._effectClass["uiView"];
  23033. if (uiData) {
  23034. var aniData = uiData["animations"];
  23035. if (aniData && aniData[0]) {
  23036. var data = aniData[0];
  23037. this._setUp({}, data);
  23038. if (data.nodes && data.nodes[0]) {
  23039. this._aniKeys = data.nodes[0].keys;
  23040. }
  23041. }
  23042. }
  23043. }
  23044. }
  23045. set effectData(uiData) {
  23046. if (uiData) {
  23047. var aniData = uiData["animations"];
  23048. if (aniData && aniData[0]) {
  23049. var data = aniData[0];
  23050. this._setUp({}, data);
  23051. if (data.nodes && data.nodes[0]) {
  23052. this._aniKeys = data.nodes[0].keys;
  23053. }
  23054. }
  23055. }
  23056. }
  23057. _displayToIndex(value) {
  23058. if (!this._animationData)
  23059. return;
  23060. if (value < 0)
  23061. value = 0;
  23062. if (value > this._count)
  23063. value = this._count;
  23064. var nodes = this._animationData.nodes, i, len = nodes.length;
  23065. len = len > 1 ? 1 : len;
  23066. for (i = 0; i < len; i++) {
  23067. this._displayNodeToFrame(nodes[i], value);
  23068. }
  23069. }
  23070. _displayNodeToFrame(node, frame, targetDic = null) {
  23071. if (!this._target)
  23072. return;
  23073. var target = this._target;
  23074. var frames = node.frames, key, propFrames, value;
  23075. var keys = node.keys, i, len = keys.length;
  23076. var secondFrames = node.secondFrames;
  23077. var tSecondFrame;
  23078. var easeFun;
  23079. var tKeyFrames;
  23080. var startFrame;
  23081. var endFrame;
  23082. for (i = 0; i < len; i++) {
  23083. key = keys[i];
  23084. propFrames = frames[key];
  23085. tSecondFrame = secondFrames[key];
  23086. if (tSecondFrame == -1) {
  23087. value = this._initData[key];
  23088. }
  23089. else {
  23090. if (frame < tSecondFrame) {
  23091. tKeyFrames = node.keyframes[key];
  23092. startFrame = tKeyFrames[0];
  23093. if (startFrame.tween) {
  23094. easeFun = Ease[startFrame.tweenMethod];
  23095. if (easeFun == null)
  23096. easeFun = Ease.linearNone;
  23097. endFrame = tKeyFrames[1];
  23098. value = easeFun(frame, this._initData[key], endFrame.value - this._initData[key], endFrame.index);
  23099. }
  23100. else {
  23101. value = this._initData[key];
  23102. }
  23103. }
  23104. else {
  23105. if (propFrames.length > frame)
  23106. value = propFrames[frame];
  23107. else
  23108. value = propFrames[propFrames.length - 1];
  23109. }
  23110. }
  23111. target[key] = value;
  23112. }
  23113. }
  23114. _calculateKeyFrames(node) {
  23115. super._calculateKeyFrames(node);
  23116. var keyFrames = node.keyframes, key, tKeyFrames, target = node.target;
  23117. var secondFrames = {};
  23118. node.secondFrames = secondFrames;
  23119. for (key in keyFrames) {
  23120. tKeyFrames = keyFrames[key];
  23121. if (tKeyFrames.length <= 1)
  23122. secondFrames[key] = -1;
  23123. else
  23124. secondFrames[key] = tKeyFrames[1].index;
  23125. }
  23126. }
  23127. }
  23128. EffectAnimation.EFFECT_BEGIN = "effectbegin";
  23129. ClassUtils.regClass("laya.display.EffectAnimation", EffectAnimation);
  23130. ClassUtils.regClass("Laya.EffectAnimation", EffectAnimation);
  23131. class SceneLoader extends EventDispatcher {
  23132. constructor() {
  23133. super();
  23134. this._completeHandler = new Handler(this, this.onOneLoadComplete);
  23135. this.reset();
  23136. }
  23137. reset() {
  23138. this._toLoadList = [];
  23139. this._isLoading = false;
  23140. this.totalCount = 0;
  23141. }
  23142. get leftCount() {
  23143. if (this._isLoading)
  23144. return this._toLoadList.length + 1;
  23145. return this._toLoadList.length;
  23146. }
  23147. get loadedCount() {
  23148. return this.totalCount - this.leftCount;
  23149. }
  23150. load(url, is3D = false, ifCheck = true) {
  23151. if (url instanceof Array) {
  23152. var i, len;
  23153. len = url.length;
  23154. for (i = 0; i < len; i++) {
  23155. this._addToLoadList(url[i], is3D);
  23156. }
  23157. }
  23158. else {
  23159. this._addToLoadList(url, is3D);
  23160. }
  23161. if (ifCheck)
  23162. this._checkNext();
  23163. }
  23164. _addToLoadList(url, is3D = false) {
  23165. if (this._toLoadList.indexOf(url) >= 0)
  23166. return;
  23167. if (Loader.getRes(url))
  23168. return;
  23169. if (is3D) {
  23170. this._toLoadList.push({ url: url });
  23171. }
  23172. else
  23173. this._toLoadList.push(url);
  23174. this.totalCount++;
  23175. }
  23176. _checkNext() {
  23177. if (!this._isLoading) {
  23178. if (this._toLoadList.length == 0) {
  23179. this.event(Event.COMPLETE);
  23180. return;
  23181. }
  23182. var tItem;
  23183. tItem = this._toLoadList.pop();
  23184. if (typeof (tItem) == 'string') {
  23185. this.loadOne(tItem);
  23186. }
  23187. else {
  23188. this.loadOne(tItem.url, true);
  23189. }
  23190. }
  23191. }
  23192. loadOne(url, is3D = false) {
  23193. this._curUrl = url;
  23194. var type = Utils.getFileExtension(this._curUrl);
  23195. if (is3D) {
  23196. ILaya.loader.create(url, this._completeHandler);
  23197. }
  23198. else if (SceneLoader.LoadableExtensions[type]) {
  23199. ILaya.loader.load(url, this._completeHandler, null, SceneLoader.LoadableExtensions[type]);
  23200. }
  23201. else if (url != AtlasInfoManager.getFileLoadPath(url) || SceneLoader.No3dLoadTypes[type] || !LoaderManager.createMap[type]) {
  23202. ILaya.loader.load(url, this._completeHandler);
  23203. }
  23204. else {
  23205. ILaya.loader.create(url, this._completeHandler);
  23206. }
  23207. }
  23208. onOneLoadComplete() {
  23209. this._isLoading = false;
  23210. if (!Loader.getRes(this._curUrl)) {
  23211. console.log("Fail to load:", this._curUrl);
  23212. }
  23213. var type = Utils.getFileExtension(this._curUrl);
  23214. if (SceneLoader.LoadableExtensions[type]) {
  23215. var dataO;
  23216. dataO = Loader.getRes(this._curUrl);
  23217. if (dataO && (dataO instanceof Prefab)) {
  23218. dataO = dataO.json;
  23219. }
  23220. if (dataO) {
  23221. if (dataO.loadList) {
  23222. this.load(dataO.loadList, false, false);
  23223. }
  23224. if (dataO.loadList3D) {
  23225. this.load(dataO.loadList3D, true, false);
  23226. }
  23227. }
  23228. }
  23229. if (type == "sk") {
  23230. this.load(this._curUrl.replace(".sk", ".png"), false, false);
  23231. }
  23232. this.event(Event.PROGRESS, this.getProgress());
  23233. this._checkNext();
  23234. }
  23235. getProgress() {
  23236. return this.loadedCount / this.totalCount;
  23237. }
  23238. }
  23239. SceneLoader.LoadableExtensions = { "scene": Loader.JSON, "scene3d": Loader.JSON, "ani": Loader.JSON, "ui": Loader.JSON, "prefab": Loader.PREFAB };
  23240. SceneLoader.No3dLoadTypes = { "png": true, "jpg": true, "txt": true };
  23241. class Scene extends Sprite {
  23242. constructor(createChildren = true) {
  23243. super();
  23244. this.autoDestroyAtClosed = false;
  23245. this.url = null;
  23246. this._viewCreated = false;
  23247. this._$componentType = "Scene";
  23248. this._setBit(Const.NOT_READY, true);
  23249. Scene.unDestroyedScenes.push(this);
  23250. this._scene = this;
  23251. if (createChildren)
  23252. this.createChildren();
  23253. }
  23254. createChildren() {
  23255. }
  23256. static setUIMap(url) {
  23257. let uimap = ILaya.loader.getRes(url);
  23258. if (uimap) {
  23259. for (let key in uimap) {
  23260. ILaya.Loader.loadedMap[URL.formatURL(key + ".scene")] = uimap[key];
  23261. }
  23262. }
  23263. else {
  23264. throw "请提前加载uimap的json,再使用该接口设置!";
  23265. }
  23266. }
  23267. loadScene(path) {
  23268. var url = path.indexOf(".") > -1 ? path : path + ".scene";
  23269. var view = ILaya.loader.getRes(url);
  23270. if (view) {
  23271. this.createView(view);
  23272. }
  23273. else {
  23274. ILaya.loader.resetProgress();
  23275. var loader = new SceneLoader();
  23276. loader.on(Event.COMPLETE, this, this._onSceneLoaded, [url]);
  23277. loader.load(url);
  23278. }
  23279. }
  23280. _onSceneLoaded(url) {
  23281. this.createView(ILaya.Loader.getRes(url));
  23282. }
  23283. createView(view) {
  23284. if (view && !this._viewCreated) {
  23285. this._viewCreated = true;
  23286. SceneUtils.createByData(this, view);
  23287. }
  23288. }
  23289. getNodeByID(id) {
  23290. if (this._idMap)
  23291. return this._idMap[id];
  23292. return null;
  23293. }
  23294. open(closeOther = true, param = null) {
  23295. if (closeOther)
  23296. Scene.closeAll();
  23297. Scene.root.addChild(this);
  23298. this.onOpened(param);
  23299. }
  23300. onOpened(param) {
  23301. }
  23302. close(type = null) {
  23303. this.onClosed(type);
  23304. if (this.autoDestroyAtClosed)
  23305. this.destroy();
  23306. else
  23307. this.removeSelf();
  23308. }
  23309. onClosed(type = null) {
  23310. }
  23311. destroy(destroyChild = true) {
  23312. this._idMap = null;
  23313. super.destroy(destroyChild);
  23314. var list = Scene.unDestroyedScenes;
  23315. for (var i = list.length - 1; i > -1; i--) {
  23316. if (list[i] === this) {
  23317. list.splice(i, 1);
  23318. return;
  23319. }
  23320. }
  23321. }
  23322. set scaleX(value) {
  23323. if (super.get_scaleX() == value)
  23324. return;
  23325. super.set_scaleX(value);
  23326. this.event(Event.RESIZE);
  23327. }
  23328. get scaleX() {
  23329. return super.scaleX;
  23330. }
  23331. set scaleY(value) {
  23332. if (super.get_scaleY() == value)
  23333. return;
  23334. super.set_scaleY(value);
  23335. this.event(Event.RESIZE);
  23336. }
  23337. get scaleY() {
  23338. return super.scaleY;
  23339. }
  23340. get width() {
  23341. if (this._width)
  23342. return this._width;
  23343. var max = 0;
  23344. for (var i = this.numChildren - 1; i > -1; i--) {
  23345. var comp = this.getChildAt(i);
  23346. if (comp._visible) {
  23347. max = Math.max(comp._x + comp.width * comp.scaleX, max);
  23348. }
  23349. }
  23350. return max;
  23351. }
  23352. set width(value) {
  23353. if (super.get_width() == value)
  23354. return;
  23355. super.set_width(value);
  23356. this.callLater(this._sizeChanged);
  23357. }
  23358. get height() {
  23359. if (this._height)
  23360. return this._height;
  23361. var max = 0;
  23362. for (var i = this.numChildren - 1; i > -1; i--) {
  23363. var comp = this.getChildAt(i);
  23364. if (comp._visible) {
  23365. max = Math.max(comp._y + comp.height * comp.scaleY, max);
  23366. }
  23367. }
  23368. return max;
  23369. }
  23370. set height(value) {
  23371. if (super.get_height() == value)
  23372. return;
  23373. super.set_height(value);
  23374. this.callLater(this._sizeChanged);
  23375. }
  23376. _sizeChanged() {
  23377. this.event(Event.RESIZE);
  23378. }
  23379. static get root() {
  23380. if (!Scene._root) {
  23381. Scene._root = ILaya.stage.addChild(new Sprite());
  23382. Scene._root.name = "root";
  23383. ILaya.stage.on("resize", null, () => {
  23384. Scene._root.size(ILaya.stage.width, ILaya.stage.height);
  23385. Scene._root.event(Event.RESIZE);
  23386. });
  23387. Scene._root.size(ILaya.stage.width, ILaya.stage.height);
  23388. Scene._root.event(Event.RESIZE);
  23389. }
  23390. return Scene._root;
  23391. }
  23392. get timer() {
  23393. return this._timer || ILaya.timer;
  23394. }
  23395. set timer(value) {
  23396. this._timer = value;
  23397. }
  23398. static load(url, complete = null, progress = null) {
  23399. ILaya.loader.resetProgress();
  23400. var loader = new SceneLoader();
  23401. loader.on(Event.PROGRESS, null, onProgress);
  23402. loader.once(Event.COMPLETE, null, create);
  23403. loader.load(url);
  23404. function onProgress(value) {
  23405. if (Scene._loadPage)
  23406. Scene._loadPage.event("progress", value);
  23407. progress && progress.runWith(value);
  23408. }
  23409. function create() {
  23410. loader.off(Event.PROGRESS, null, onProgress);
  23411. var obj = ILaya.Loader.getRes(url);
  23412. if (!obj)
  23413. throw "Can not find scene:" + url;
  23414. if (!obj.props)
  23415. throw "Scene data is error:" + url;
  23416. var runtime = obj.props.runtime ? obj.props.runtime : obj.type;
  23417. var clas = ILaya.ClassUtils.getClass(runtime);
  23418. if (obj.props.renderType == "instance") {
  23419. var scene = clas.instance || (clas.instance = new clas());
  23420. }
  23421. else {
  23422. scene = new clas();
  23423. }
  23424. if (scene && scene instanceof Node) {
  23425. scene.url = url;
  23426. if (!scene._getBit(Const.NOT_READY)) {
  23427. complete && complete.runWith(scene);
  23428. }
  23429. else {
  23430. scene.on("onViewCreated", null, function () {
  23431. complete && complete.runWith(scene);
  23432. });
  23433. scene.createView(obj);
  23434. }
  23435. Scene.hideLoadingPage();
  23436. }
  23437. else {
  23438. throw "Can not find scene:" + runtime;
  23439. }
  23440. }
  23441. }
  23442. static open(url, closeOther = true, param = null, complete = null, progress = null) {
  23443. if (param instanceof Handler) {
  23444. var temp = complete;
  23445. complete = param;
  23446. param = temp;
  23447. }
  23448. Scene.showLoadingPage();
  23449. Scene.load(url, Handler.create(null, this._onSceneLoaded, [closeOther, complete, param]), progress);
  23450. }
  23451. static _onSceneLoaded(closeOther, complete, param, scene) {
  23452. scene.open(closeOther, param);
  23453. if (complete)
  23454. complete.runWith(scene);
  23455. }
  23456. static close(url, name = "") {
  23457. var flag = false;
  23458. var list = Scene.unDestroyedScenes;
  23459. for (var i = 0, n = list.length; i < n; i++) {
  23460. var scene = list[i];
  23461. if (scene && scene.parent && scene.url === url && scene.name == name) {
  23462. scene.close();
  23463. flag = true;
  23464. }
  23465. }
  23466. return flag;
  23467. }
  23468. static closeAll() {
  23469. var root = Scene.root;
  23470. for (var i = 0, n = root.numChildren; i < n; i++) {
  23471. var scene = root.getChildAt(0);
  23472. if (scene instanceof Scene)
  23473. scene.close();
  23474. else
  23475. scene.removeSelf();
  23476. }
  23477. }
  23478. static destroy(url, name = "") {
  23479. var flag = false;
  23480. var list = Scene.unDestroyedScenes;
  23481. for (var i = 0, n = list.length; i < n; i++) {
  23482. var scene = list[i];
  23483. if (scene.url === url && scene.name == name) {
  23484. scene.destroy();
  23485. flag = true;
  23486. }
  23487. }
  23488. return flag;
  23489. }
  23490. static gc() {
  23491. Resource.destroyUnusedResources();
  23492. }
  23493. static setLoadingPage(loadPage) {
  23494. if (Scene._loadPage != loadPage) {
  23495. Scene._loadPage = loadPage;
  23496. }
  23497. }
  23498. static showLoadingPage(param = null, delay = 500) {
  23499. if (Scene._loadPage) {
  23500. ILaya.systemTimer.clear(null, Scene._showLoading);
  23501. ILaya.systemTimer.clear(null, Scene._hideLoading);
  23502. ILaya.systemTimer.once(delay, null, Scene._showLoading, [param], false);
  23503. }
  23504. }
  23505. static _showLoading(param) {
  23506. ILaya.stage.addChild(Scene._loadPage);
  23507. Scene._loadPage.onOpened(param);
  23508. }
  23509. static _hideLoading() {
  23510. Scene._loadPage.close();
  23511. }
  23512. static hideLoadingPage(delay = 500) {
  23513. if (Scene._loadPage) {
  23514. ILaya.systemTimer.clear(null, Scene._showLoading);
  23515. ILaya.systemTimer.clear(null, Scene._hideLoading);
  23516. ILaya.systemTimer.once(delay, null, Scene._hideLoading);
  23517. }
  23518. }
  23519. }
  23520. Scene.unDestroyedScenes = [];
  23521. ILaya.regClass(Scene);
  23522. ClassUtils.regClass("laya.display.Scene", Scene);
  23523. ClassUtils.regClass("Laya.Scene", Scene);
  23524. class DrawParticleCmd {
  23525. static create(_temp) {
  23526. var cmd = Pool.getItemByClass("DrawParticleCmd", DrawParticleCmd);
  23527. cmd._templ = _temp;
  23528. return cmd;
  23529. }
  23530. recover() {
  23531. this._templ = null;
  23532. Pool.recover("DrawParticleCmd", this);
  23533. }
  23534. run(context, gx, gy) {
  23535. context.drawParticle(gx, gy, this._templ);
  23536. }
  23537. get cmdID() {
  23538. return DrawParticleCmd.ID;
  23539. }
  23540. }
  23541. DrawParticleCmd.ID = "DrawParticleCmd";
  23542. class FilterSetterBase {
  23543. constructor() {
  23544. }
  23545. paramChanged() {
  23546. Laya.systemTimer.callLater(this, this.buildFilter);
  23547. }
  23548. buildFilter() {
  23549. if (this._target) {
  23550. this.addFilter(this._target);
  23551. }
  23552. }
  23553. addFilter(sprite) {
  23554. if (!sprite)
  23555. return;
  23556. if (!sprite.filters) {
  23557. sprite.filters = [this._filter];
  23558. }
  23559. else {
  23560. var preFilters;
  23561. preFilters = sprite.filters;
  23562. if (preFilters.indexOf(this._filter) < 0) {
  23563. preFilters.push(this._filter);
  23564. sprite.filters = Utils.copyArray([], preFilters);
  23565. }
  23566. }
  23567. }
  23568. removeFilter(sprite) {
  23569. if (!sprite)
  23570. return;
  23571. sprite.filters = null;
  23572. }
  23573. set target(value) {
  23574. if (this._target != value) {
  23575. this._target = value;
  23576. this.paramChanged();
  23577. }
  23578. }
  23579. }
  23580. class BlurFilterGLRender {
  23581. render(rt, ctx, width, height, filter) {
  23582. var shaderValue = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  23583. this.setShaderInfo(shaderValue, filter, rt.width, rt.height);
  23584. ctx.drawTarget(rt, 0, 0, width, height, Matrix.EMPTY.identity(), shaderValue);
  23585. }
  23586. setShaderInfo(shader, filter, w, h) {
  23587. shader.defines.add(Filter.BLUR);
  23588. var sv = shader;
  23589. BlurFilterGLRender.blurinfo[0] = w;
  23590. BlurFilterGLRender.blurinfo[1] = h;
  23591. sv.blurInfo = BlurFilterGLRender.blurinfo;
  23592. var sigma = filter.strength / 3.0;
  23593. var sigma2 = sigma * sigma;
  23594. filter.strength_sig2_2sig2_gauss1[0] = filter.strength;
  23595. filter.strength_sig2_2sig2_gauss1[1] = sigma2;
  23596. filter.strength_sig2_2sig2_gauss1[2] = 2.0 * sigma2;
  23597. filter.strength_sig2_2sig2_gauss1[3] = 1.0 / (2.0 * Math.PI * sigma2);
  23598. sv.strength_sig2_2sig2_gauss1 = filter.strength_sig2_2sig2_gauss1;
  23599. }
  23600. }
  23601. BlurFilterGLRender.blurinfo = new Array(2);
  23602. class BlurFilter extends Filter {
  23603. constructor(strength = 4) {
  23604. super();
  23605. this.strength_sig2_2sig2_gauss1 = [];
  23606. this.strength = strength;
  23607. this._glRender = new BlurFilterGLRender();
  23608. }
  23609. get type() {
  23610. return Filter.BLUR;
  23611. }
  23612. getStrenth_sig2_2sig2_native() {
  23613. if (!this.strength_sig2_native) {
  23614. this.strength_sig2_native = new Float32Array(4);
  23615. }
  23616. var sigma = this.strength / 3.0;
  23617. var sigma2 = sigma * sigma;
  23618. this.strength_sig2_native[0] = this.strength;
  23619. this.strength_sig2_native[1] = sigma2;
  23620. this.strength_sig2_native[2] = 2.0 * sigma2;
  23621. this.strength_sig2_native[3] = 1.0 / (2.0 * Math.PI * sigma2);
  23622. return this.strength_sig2_native;
  23623. }
  23624. }
  23625. class BlurFilterSetter extends FilterSetterBase {
  23626. constructor() {
  23627. super();
  23628. this._strength = 4;
  23629. this._filter = new BlurFilter(this.strength);
  23630. }
  23631. buildFilter() {
  23632. this._filter = new BlurFilter(this.strength);
  23633. super.buildFilter();
  23634. }
  23635. get strength() {
  23636. return this._strength;
  23637. }
  23638. set strength(value) {
  23639. this._strength = value;
  23640. }
  23641. }
  23642. class ButtonEffect {
  23643. constructor() {
  23644. this._curState = 0;
  23645. this.effectScale = 1.5;
  23646. this.tweenTime = 300;
  23647. }
  23648. set target(tar) {
  23649. this._tar = tar;
  23650. tar.on(Event.MOUSE_DOWN, this, this.toChangedState);
  23651. tar.on(Event.MOUSE_UP, this, this.toInitState);
  23652. tar.on(Event.MOUSE_OUT, this, this.toInitState);
  23653. }
  23654. toChangedState() {
  23655. this._curState = 1;
  23656. if (this._curTween)
  23657. Tween.clear(this._curTween);
  23658. this._curTween = Tween.to(this._tar, { scaleX: this.effectScale, scaleY: this.effectScale }, this.tweenTime, Ease[this.effectEase], Handler.create(this, this.tweenComplete));
  23659. }
  23660. toInitState() {
  23661. if (this._curState == 2)
  23662. return;
  23663. if (this._curTween)
  23664. Tween.clear(this._curTween);
  23665. this._curState = 2;
  23666. this._curTween = Tween.to(this._tar, { scaleX: 1, scaleY: 1 }, this.tweenTime, Ease[this.backEase], Handler.create(this, this.tweenComplete));
  23667. }
  23668. tweenComplete() {
  23669. this._curState = 0;
  23670. this._curTween = null;
  23671. }
  23672. }
  23673. class ColorFilterSetter extends FilterSetterBase {
  23674. constructor() {
  23675. super();
  23676. this._brightness = 0;
  23677. this._contrast = 0;
  23678. this._saturation = 0;
  23679. this._hue = 0;
  23680. this._red = 0;
  23681. this._green = 0;
  23682. this._blue = 0;
  23683. this._alpha = 0;
  23684. this._filter = new ColorFilter();
  23685. }
  23686. buildFilter() {
  23687. this._filter.reset();
  23688. this._filter.color(this.red, this.green, this.blue, this.alpha);
  23689. this._filter.adjustHue(this.hue);
  23690. this._filter.adjustContrast(this.contrast);
  23691. this._filter.adjustBrightness(this.brightness);
  23692. this._filter.adjustSaturation(this.saturation);
  23693. super.buildFilter();
  23694. }
  23695. get brightness() {
  23696. return this._brightness;
  23697. }
  23698. set brightness(value) {
  23699. this._brightness = value;
  23700. this.paramChanged();
  23701. }
  23702. get contrast() {
  23703. return this._contrast;
  23704. }
  23705. set contrast(value) {
  23706. this._contrast = value;
  23707. this.paramChanged();
  23708. }
  23709. get saturation() {
  23710. return this._saturation;
  23711. }
  23712. set saturation(value) {
  23713. this._saturation = value;
  23714. this.paramChanged();
  23715. }
  23716. get hue() {
  23717. return this._hue;
  23718. }
  23719. set hue(value) {
  23720. this._hue = value;
  23721. this.paramChanged();
  23722. }
  23723. get red() {
  23724. return this._red;
  23725. }
  23726. set red(value) {
  23727. this._red = value;
  23728. this.paramChanged();
  23729. }
  23730. get green() {
  23731. return this._green;
  23732. }
  23733. set green(value) {
  23734. this._green = value;
  23735. this.paramChanged();
  23736. }
  23737. get blue() {
  23738. return this._blue;
  23739. }
  23740. set blue(value) {
  23741. this._blue = value;
  23742. this.paramChanged();
  23743. }
  23744. get color() {
  23745. return this._color;
  23746. }
  23747. set color(value) {
  23748. this._color = value;
  23749. var colorO;
  23750. colorO = ColorUtils.create(value);
  23751. this._red = colorO.arrColor[0] * 255;
  23752. this._green = colorO.arrColor[1] * 255;
  23753. this._blue = colorO.arrColor[2] * 255;
  23754. this.paramChanged();
  23755. }
  23756. get alpha() {
  23757. return this._alpha;
  23758. }
  23759. set alpha(value) {
  23760. this._alpha = value;
  23761. this.paramChanged();
  23762. }
  23763. }
  23764. class EffectBase extends Component {
  23765. constructor() {
  23766. super(...arguments);
  23767. this.duration = 1000;
  23768. this.delay = 0;
  23769. this.repeat = 0;
  23770. this.autoDestroyAtComplete = true;
  23771. }
  23772. _onAwake() {
  23773. this.target = this.target || this.owner;
  23774. if (this.autoDestroyAtComplete)
  23775. this._comlete = Handler.create(this.target, this.target.destroy, null, false);
  23776. if (this.eventName)
  23777. this.owner.on(this.eventName, this, this._exeTween);
  23778. else
  23779. this._exeTween();
  23780. }
  23781. _exeTween() {
  23782. this._tween = this._doTween();
  23783. this._tween.repeat = this.repeat;
  23784. }
  23785. _doTween() {
  23786. return null;
  23787. }
  23788. onReset() {
  23789. this.duration = 1000;
  23790. this.delay = 0;
  23791. this.repeat = 0;
  23792. this.ease = null;
  23793. this.target = null;
  23794. if (this.eventName) {
  23795. this.owner.off(this.eventName, this, this._exeTween);
  23796. this.eventName = null;
  23797. }
  23798. if (this._comlete) {
  23799. this._comlete.recover();
  23800. this._comlete = null;
  23801. }
  23802. if (this._tween) {
  23803. this._tween.clear();
  23804. this._tween = null;
  23805. }
  23806. }
  23807. }
  23808. class FadeIn extends EffectBase {
  23809. _doTween() {
  23810. this.target.alpha = 0;
  23811. return Tween.to(this.target, { alpha: 1 }, this.duration, Ease[this.ease], this._comlete, this.delay);
  23812. }
  23813. }
  23814. class FadeOut extends EffectBase {
  23815. _doTween() {
  23816. this.target.alpha = 1;
  23817. return Tween.to(this.target, { alpha: 0 }, this.duration, Ease[this.ease], this._comlete, this.delay);
  23818. }
  23819. }
  23820. class GlowFilterGLRender {
  23821. setShaderInfo(shader, w, h, data) {
  23822. shader.defines.add(data.type);
  23823. var sv = shader;
  23824. sv.u_blurInfo1 = data._sv_blurInfo1;
  23825. var info2 = data._sv_blurInfo2;
  23826. info2[0] = w;
  23827. info2[1] = h;
  23828. sv.u_blurInfo2 = info2;
  23829. sv.u_color = data.getColor();
  23830. }
  23831. render(rt, ctx, width, height, filter) {
  23832. var w = width, h = height;
  23833. var svBlur = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  23834. this.setShaderInfo(svBlur, w, h, filter);
  23835. var svCP = Value2D.create(ShaderDefines2D.TEXTURE2D, 0);
  23836. var matI = Matrix.TEMP.identity();
  23837. ctx.drawTarget(rt, 0, 0, w, h, matI, svBlur);
  23838. ctx.drawTarget(rt, 0, 0, w, h, matI, svCP);
  23839. }
  23840. }
  23841. class GlowFilter extends Filter {
  23842. constructor(color, blur = 4, offX = 6, offY = 6) {
  23843. super();
  23844. this._elements = new Float32Array(9);
  23845. this._sv_blurInfo1 = new Array(4);
  23846. this._sv_blurInfo2 = [0, 0, 1, 0];
  23847. this._color = new ColorUtils(color);
  23848. this.blur = Math.min(blur, 20);
  23849. this.offX = offX;
  23850. this.offY = offY;
  23851. this._sv_blurInfo1[0] = this._sv_blurInfo1[1] = this.blur;
  23852. this._sv_blurInfo1[2] = offX;
  23853. this._sv_blurInfo1[3] = -offY;
  23854. this._glRender = new GlowFilterGLRender();
  23855. }
  23856. get type() {
  23857. return BlurFilter.GLOW;
  23858. }
  23859. get offY() {
  23860. return this._elements[6];
  23861. }
  23862. set offY(value) {
  23863. this._elements[6] = value;
  23864. this._sv_blurInfo1[3] = -value;
  23865. }
  23866. get offX() {
  23867. return this._elements[5];
  23868. }
  23869. set offX(value) {
  23870. this._elements[5] = value;
  23871. this._sv_blurInfo1[2] = value;
  23872. }
  23873. getColor() {
  23874. return this._color.arrColor;
  23875. }
  23876. get blur() {
  23877. return this._elements[4];
  23878. }
  23879. set blur(value) {
  23880. this._elements[4] = value;
  23881. this._sv_blurInfo1[0] = this._sv_blurInfo1[1] = value;
  23882. }
  23883. getColorNative() {
  23884. if (!this._color_native) {
  23885. this._color_native = new Float32Array(4);
  23886. }
  23887. var color = this.getColor();
  23888. this._color_native[0] = color[0];
  23889. this._color_native[1] = color[1];
  23890. this._color_native[2] = color[2];
  23891. this._color_native[3] = color[3];
  23892. return this._color_native;
  23893. }
  23894. getBlurInfo1Native() {
  23895. if (!this._blurInof1_native) {
  23896. this._blurInof1_native = new Float32Array(4);
  23897. }
  23898. this._blurInof1_native[0] = this._blurInof1_native[1] = this.blur;
  23899. this._blurInof1_native[2] = this.offX;
  23900. this._blurInof1_native[3] = this.offY;
  23901. return this._blurInof1_native;
  23902. }
  23903. getBlurInfo2Native() {
  23904. if (!this._blurInof2_native) {
  23905. this._blurInof2_native = new Float32Array(4);
  23906. }
  23907. this._blurInof2_native[2] = 1;
  23908. return this._blurInof2_native;
  23909. }
  23910. }
  23911. class GlowFilterSetter extends FilterSetterBase {
  23912. constructor() {
  23913. super();
  23914. this._color = "#ff0000";
  23915. this._blur = 4;
  23916. this._offX = 6;
  23917. this._offY = 6;
  23918. this._filter = new GlowFilter(this._color);
  23919. }
  23920. buildFilter() {
  23921. this._filter = new GlowFilter(this.color, this.blur, this.offX, this.offY);
  23922. super.buildFilter();
  23923. }
  23924. get color() {
  23925. return this._color;
  23926. }
  23927. set color(value) {
  23928. this._color = value;
  23929. this.paramChanged();
  23930. }
  23931. get blur() {
  23932. return this._blur;
  23933. }
  23934. set blur(value) {
  23935. this._blur = value;
  23936. this.paramChanged();
  23937. }
  23938. get offX() {
  23939. return this._offX;
  23940. }
  23941. set offX(value) {
  23942. this._offX = value;
  23943. this.paramChanged();
  23944. }
  23945. get offY() {
  23946. return this._offY;
  23947. }
  23948. set offY(value) {
  23949. this._offY = value;
  23950. this.paramChanged();
  23951. }
  23952. }
  23953. class KeyLocation {
  23954. }
  23955. KeyLocation.STANDARD = 0;
  23956. KeyLocation.LEFT = 1;
  23957. KeyLocation.RIGHT = 2;
  23958. KeyLocation.NUM_PAD = 3;
  23959. class Keyboard {
  23960. }
  23961. Keyboard.NUMBER_0 = 48;
  23962. Keyboard.NUMBER_1 = 49;
  23963. Keyboard.NUMBER_2 = 50;
  23964. Keyboard.NUMBER_3 = 51;
  23965. Keyboard.NUMBER_4 = 52;
  23966. Keyboard.NUMBER_5 = 53;
  23967. Keyboard.NUMBER_6 = 54;
  23968. Keyboard.NUMBER_7 = 55;
  23969. Keyboard.NUMBER_8 = 56;
  23970. Keyboard.NUMBER_9 = 57;
  23971. Keyboard.A = 65;
  23972. Keyboard.B = 66;
  23973. Keyboard.C = 67;
  23974. Keyboard.D = 68;
  23975. Keyboard.E = 69;
  23976. Keyboard.F = 70;
  23977. Keyboard.G = 71;
  23978. Keyboard.H = 72;
  23979. Keyboard.I = 73;
  23980. Keyboard.J = 74;
  23981. Keyboard.K = 75;
  23982. Keyboard.L = 76;
  23983. Keyboard.M = 77;
  23984. Keyboard.N = 78;
  23985. Keyboard.O = 79;
  23986. Keyboard.P = 80;
  23987. Keyboard.Q = 81;
  23988. Keyboard.R = 82;
  23989. Keyboard.S = 83;
  23990. Keyboard.T = 84;
  23991. Keyboard.U = 85;
  23992. Keyboard.V = 86;
  23993. Keyboard.W = 87;
  23994. Keyboard.X = 88;
  23995. Keyboard.Y = 89;
  23996. Keyboard.Z = 90;
  23997. Keyboard.F1 = 112;
  23998. Keyboard.F2 = 113;
  23999. Keyboard.F3 = 114;
  24000. Keyboard.F4 = 115;
  24001. Keyboard.F5 = 116;
  24002. Keyboard.F6 = 117;
  24003. Keyboard.F7 = 118;
  24004. Keyboard.F8 = 119;
  24005. Keyboard.F9 = 120;
  24006. Keyboard.F10 = 121;
  24007. Keyboard.F11 = 122;
  24008. Keyboard.F12 = 123;
  24009. Keyboard.F13 = 124;
  24010. Keyboard.F14 = 125;
  24011. Keyboard.F15 = 126;
  24012. Keyboard.NUMPAD = 21;
  24013. Keyboard.NUMPAD_0 = 96;
  24014. Keyboard.NUMPAD_1 = 97;
  24015. Keyboard.NUMPAD_2 = 98;
  24016. Keyboard.NUMPAD_3 = 99;
  24017. Keyboard.NUMPAD_4 = 100;
  24018. Keyboard.NUMPAD_5 = 101;
  24019. Keyboard.NUMPAD_6 = 102;
  24020. Keyboard.NUMPAD_7 = 103;
  24021. Keyboard.NUMPAD_8 = 104;
  24022. Keyboard.NUMPAD_9 = 105;
  24023. Keyboard.NUMPAD_ADD = 107;
  24024. Keyboard.NUMPAD_DECIMAL = 110;
  24025. Keyboard.NUMPAD_DIVIDE = 111;
  24026. Keyboard.NUMPAD_ENTER = 108;
  24027. Keyboard.NUMPAD_MULTIPLY = 106;
  24028. Keyboard.NUMPAD_SUBTRACT = 109;
  24029. Keyboard.SEMICOLON = 186;
  24030. Keyboard.EQUAL = 187;
  24031. Keyboard.COMMA = 188;
  24032. Keyboard.MINUS = 189;
  24033. Keyboard.PERIOD = 190;
  24034. Keyboard.SLASH = 191;
  24035. Keyboard.BACKQUOTE = 192;
  24036. Keyboard.LEFTBRACKET = 219;
  24037. Keyboard.BACKSLASH = 220;
  24038. Keyboard.RIGHTBRACKET = 221;
  24039. Keyboard.QUOTE = 222;
  24040. Keyboard.ALTERNATE = 18;
  24041. Keyboard.BACKSPACE = 8;
  24042. Keyboard.CAPS_LOCK = 20;
  24043. Keyboard.COMMAND = 15;
  24044. Keyboard.CONTROL = 17;
  24045. Keyboard.DELETE = 46;
  24046. Keyboard.ENTER = 13;
  24047. Keyboard.ESCAPE = 27;
  24048. Keyboard.PAGE_UP = 33;
  24049. Keyboard.PAGE_DOWN = 34;
  24050. Keyboard.END = 35;
  24051. Keyboard.HOME = 36;
  24052. Keyboard.LEFT = 37;
  24053. Keyboard.UP = 38;
  24054. Keyboard.RIGHT = 39;
  24055. Keyboard.DOWN = 40;
  24056. Keyboard.SHIFT = 16;
  24057. Keyboard.SPACE = 32;
  24058. Keyboard.TAB = 9;
  24059. Keyboard.INSERT = 45;
  24060. class CommandEncoder {
  24061. constructor(layagl, reserveSize, adjustSize, isSyncToRenderThread) {
  24062. this._idata = [];
  24063. }
  24064. getArrayData() {
  24065. return this._idata;
  24066. }
  24067. getPtrID() {
  24068. return 0;
  24069. }
  24070. beginEncoding() {
  24071. }
  24072. endEncoding() {
  24073. }
  24074. clearEncoding() {
  24075. this._idata.length = 0;
  24076. }
  24077. getCount() {
  24078. return this._idata.length;
  24079. }
  24080. add_ShaderValue(o) {
  24081. this._idata.push(o);
  24082. }
  24083. addShaderUniform(one) {
  24084. this.add_ShaderValue(one);
  24085. }
  24086. }
  24087. class LayaGLRunner {
  24088. static uploadShaderUniforms(layaGL, commandEncoder, shaderData, uploadUnTexture) {
  24089. var data = shaderData._data;
  24090. var shaderUniform = commandEncoder.getArrayData();
  24091. var shaderCall = 0;
  24092. for (var i = 0, n = shaderUniform.length; i < n; i++) {
  24093. var one = shaderUniform[i];
  24094. if (uploadUnTexture || one.textureID !== -1) {
  24095. var value = data[one.dataOffset];
  24096. if (value != null)
  24097. shaderCall += one.fun.call(one.caller, one, value);
  24098. }
  24099. }
  24100. return shaderCall;
  24101. }
  24102. static uploadCustomUniform(layaGL, custom, index, data) {
  24103. var shaderCall = 0;
  24104. var one = custom[index];
  24105. if (one && data != null)
  24106. shaderCall += one.fun.call(one.caller, one, data);
  24107. return shaderCall;
  24108. }
  24109. static uploadShaderUniformsForNative(layaGL, commandEncoder, shaderData) {
  24110. var nType = LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_ID;
  24111. if (shaderData._runtimeCopyValues.length > 0) {
  24112. nType = LayaGL.UPLOAD_SHADER_UNIFORM_TYPE_DATA;
  24113. }
  24114. var data = shaderData._data;
  24115. return LayaGL.instance.uploadShaderUniforms(commandEncoder, data, nType);
  24116. }
  24117. }
  24118. class QuickTestTool {
  24119. constructor() {
  24120. }
  24121. static getMCDName(type) {
  24122. return QuickTestTool._typeToNameDic[type];
  24123. }
  24124. static showRenderTypeInfo(type, force = false) {
  24125. if (!force && QuickTestTool.showedDic[type])
  24126. return;
  24127. QuickTestTool.showedDic[type] = true;
  24128. if (!QuickTestTool._rendertypeToStrDic[type]) {
  24129. var arr = [];
  24130. var tType;
  24131. tType = 1;
  24132. while (tType <= type) {
  24133. if (tType & type) {
  24134. arr.push(QuickTestTool.getMCDName(tType & type));
  24135. }
  24136. tType = tType << 1;
  24137. }
  24138. QuickTestTool._rendertypeToStrDic[type] = arr.join(",");
  24139. }
  24140. console.log("cmd:", QuickTestTool._rendertypeToStrDic[type]);
  24141. }
  24142. static __init__() {
  24143. QuickTestTool._typeToNameDic[SpriteConst.ALPHA] = "ALPHA";
  24144. QuickTestTool._typeToNameDic[SpriteConst.TRANSFORM] = "TRANSFORM";
  24145. QuickTestTool._typeToNameDic[SpriteConst.TEXTURE] = "TEXTURE";
  24146. QuickTestTool._typeToNameDic[SpriteConst.GRAPHICS] = "GRAPHICS";
  24147. QuickTestTool._typeToNameDic[SpriteConst.ONECHILD] = "ONECHILD";
  24148. QuickTestTool._typeToNameDic[SpriteConst.CHILDS] = "CHILDS";
  24149. QuickTestTool._typeToNameDic[SpriteConst.TRANSFORM | SpriteConst.ALPHA] = "TRANSFORM|ALPHA";
  24150. QuickTestTool._typeToNameDic[SpriteConst.CANVAS] = "CANVAS";
  24151. QuickTestTool._typeToNameDic[SpriteConst.BLEND] = "BLEND";
  24152. QuickTestTool._typeToNameDic[SpriteConst.FILTERS] = "FILTERS";
  24153. QuickTestTool._typeToNameDic[SpriteConst.MASK] = "MASK";
  24154. QuickTestTool._typeToNameDic[SpriteConst.CLIP] = "CLIP";
  24155. QuickTestTool._typeToNameDic[SpriteConst.LAYAGL3D] = "LAYAGL3D";
  24156. }
  24157. render(context, x, y) {
  24158. QuickTestTool._addType(this._renderType);
  24159. QuickTestTool.showRenderTypeInfo(this._renderType);
  24160. RenderSprite.renders[this._renderType]._fun(this, context, x + this._x, y + this._y);
  24161. this._repaint = 0;
  24162. }
  24163. _stageRender(context, x, y) {
  24164. QuickTestTool._countStart();
  24165. QuickTestTool._PreStageRender.call(ILaya.stage, context, x, y);
  24166. QuickTestTool._countEnd();
  24167. }
  24168. static _countStart() {
  24169. var key;
  24170. for (key in QuickTestTool._countDic) {
  24171. QuickTestTool._countDic[key] = 0;
  24172. }
  24173. }
  24174. static _countEnd() {
  24175. QuickTestTool._i++;
  24176. if (QuickTestTool._i > 60) {
  24177. QuickTestTool.showCountInfo();
  24178. QuickTestTool._i = 0;
  24179. }
  24180. }
  24181. static _addType(type) {
  24182. if (!QuickTestTool._countDic[type]) {
  24183. QuickTestTool._countDic[type] = 1;
  24184. }
  24185. else {
  24186. QuickTestTool._countDic[type] += 1;
  24187. }
  24188. }
  24189. static showCountInfo() {
  24190. console.log("===================");
  24191. var key;
  24192. for (key in QuickTestTool._countDic) {
  24193. console.log("count:" + QuickTestTool._countDic[key]);
  24194. QuickTestTool.showRenderTypeInfo(key, true);
  24195. }
  24196. }
  24197. static enableQuickTest() {
  24198. QuickTestTool.__init__();
  24199. Sprite["prototype"]["render"] = QuickTestTool["prototype"]["render"];
  24200. QuickTestTool._PreStageRender = Stage["prototype"]["render"];
  24201. Stage["prototype"]["render"] = QuickTestTool["prototype"]["_stageRender"];
  24202. }
  24203. }
  24204. QuickTestTool.showedDic = {};
  24205. QuickTestTool._rendertypeToStrDic = {};
  24206. QuickTestTool._typeToNameDic = {};
  24207. QuickTestTool._countDic = {};
  24208. QuickTestTool._i = 0;
  24209. class Sound extends EventDispatcher {
  24210. load(url) {
  24211. }
  24212. play(startTime = 0, loops = 0) {
  24213. return null;
  24214. }
  24215. get duration() {
  24216. return 0;
  24217. }
  24218. dispose() {
  24219. }
  24220. }
  24221. class SoundNode extends Sprite {
  24222. constructor() {
  24223. super();
  24224. this.visible = false;
  24225. this.on(Event.ADDED, this, this._onParentChange);
  24226. this.on(Event.REMOVED, this, this._onParentChange);
  24227. }
  24228. _onParentChange() {
  24229. this.target = this.parent;
  24230. }
  24231. play(loops = 1, complete = null) {
  24232. if (isNaN(loops)) {
  24233. loops = 1;
  24234. }
  24235. if (!this.url)
  24236. return;
  24237. this.stop();
  24238. this._channel = SoundManager.playSound(this.url, loops, complete);
  24239. }
  24240. stop() {
  24241. if (this._channel && !this._channel.isStopped) {
  24242. this._channel.stop();
  24243. }
  24244. this._channel = null;
  24245. }
  24246. _setPlayAction(tar, event, action, add = true) {
  24247. if (!this[action])
  24248. return;
  24249. if (!tar)
  24250. return;
  24251. if (add) {
  24252. tar.on(event, this, this[action]);
  24253. }
  24254. else {
  24255. tar.off(event, this, this[action]);
  24256. }
  24257. }
  24258. _setPlayActions(tar, events, action, add = true) {
  24259. if (!tar)
  24260. return;
  24261. if (!events)
  24262. return;
  24263. var eventArr = events.split(",");
  24264. var i, len;
  24265. len = eventArr.length;
  24266. for (i = 0; i < len; i++) {
  24267. this._setPlayAction(tar, eventArr[i], action, add);
  24268. }
  24269. }
  24270. set playEvent(events) {
  24271. this._playEvents = events;
  24272. if (!events)
  24273. return;
  24274. if (this._tar) {
  24275. this._setPlayActions(this._tar, events, "play");
  24276. }
  24277. }
  24278. set target(tar) {
  24279. if (this._tar) {
  24280. this._setPlayActions(this._tar, this._playEvents, "play", false);
  24281. this._setPlayActions(this._tar, this._stopEvents, "stop", false);
  24282. }
  24283. this._tar = tar;
  24284. if (this._tar) {
  24285. this._setPlayActions(this._tar, this._playEvents, "play", true);
  24286. this._setPlayActions(this._tar, this._stopEvents, "stop", true);
  24287. }
  24288. }
  24289. set stopEvent(events) {
  24290. this._stopEvents = events;
  24291. if (!events)
  24292. return;
  24293. if (this._tar) {
  24294. this._setPlayActions(this._tar, events, "stop");
  24295. }
  24296. }
  24297. }
  24298. class ResourceVersion {
  24299. static enable(manifestFile, callback, type = 2) {
  24300. ResourceVersion.type = type;
  24301. ILaya.loader.load(manifestFile, Handler.create(null, ResourceVersion.onManifestLoaded, [callback]), null, Loader.JSON);
  24302. }
  24303. static onManifestLoaded(callback, data) {
  24304. ResourceVersion.manifest = data;
  24305. URL.customFormat = ResourceVersion.addVersionPrefix;
  24306. callback.run();
  24307. if (!data) {
  24308. console.warn("资源版本清单文件不存在,不使用资源版本管理。忽略ERR_FILE_NOT_FOUND错误。");
  24309. }
  24310. }
  24311. static addVersionPrefix(originURL) {
  24312. originURL = URL.getAdptedFilePath(originURL);
  24313. if (ResourceVersion.manifest && ResourceVersion.manifest[originURL]) {
  24314. if (ResourceVersion.type == ResourceVersion.FILENAME_VERSION)
  24315. return ResourceVersion.manifest[originURL];
  24316. return ResourceVersion.manifest[originURL] + "/" + originURL;
  24317. }
  24318. return originURL;
  24319. }
  24320. }
  24321. ResourceVersion.FOLDER_VERSION = 1;
  24322. ResourceVersion.FILENAME_VERSION = 2;
  24323. ResourceVersion.type = ResourceVersion.FOLDER_VERSION;
  24324. class Socket extends EventDispatcher {
  24325. constructor(host = null, port = 0, byteClass = null, protocols = null) {
  24326. super();
  24327. this.disableInput = false;
  24328. this.protocols = [];
  24329. this._byteClass = byteClass ? byteClass : Byte;
  24330. this.protocols = protocols;
  24331. this.endian = Socket.BIG_ENDIAN;
  24332. if (host && port > 0 && port < 65535)
  24333. this.connect(host, port);
  24334. }
  24335. get input() {
  24336. return this._input;
  24337. }
  24338. get output() {
  24339. return this._output;
  24340. }
  24341. get connected() {
  24342. return this._connected;
  24343. }
  24344. get endian() {
  24345. return this._endian;
  24346. }
  24347. set endian(value) {
  24348. this._endian = value;
  24349. if (this._input != null)
  24350. this._input.endian = value;
  24351. if (this._output != null)
  24352. this._output.endian = value;
  24353. }
  24354. connect(host, port) {
  24355. var url = "ws://" + host + ":" + port;
  24356. this.connectByUrl(url);
  24357. }
  24358. connectByUrl(url) {
  24359. if (this._socket != null)
  24360. this.close();
  24361. this._socket && this.cleanSocket();
  24362. if (!this.protocols || this.protocols.length == 0) {
  24363. this._socket = new Browser.window.WebSocket(url);
  24364. }
  24365. else {
  24366. this._socket = new Browser.window.WebSocket(url, this.protocols);
  24367. }
  24368. this._socket.binaryType = "arraybuffer";
  24369. this._output = new this._byteClass();
  24370. this._output.endian = this.endian;
  24371. this._input = new this._byteClass();
  24372. this._input.endian = this.endian;
  24373. this._addInputPosition = 0;
  24374. this._socket.onopen = (e) => {
  24375. this._onOpen(e);
  24376. };
  24377. this._socket.onmessage = (msg) => {
  24378. this._onMessage(msg);
  24379. };
  24380. this._socket.onclose = (e) => {
  24381. this._onClose(e);
  24382. };
  24383. this._socket.onerror = (e) => {
  24384. this._onError(e);
  24385. };
  24386. }
  24387. cleanSocket() {
  24388. this.close();
  24389. this._connected = false;
  24390. this._socket.onopen = null;
  24391. this._socket.onmessage = null;
  24392. this._socket.onclose = null;
  24393. this._socket.onerror = null;
  24394. this._socket = null;
  24395. }
  24396. close() {
  24397. if (this._socket != null) {
  24398. try {
  24399. this._socket.close();
  24400. }
  24401. catch (e) {
  24402. }
  24403. }
  24404. }
  24405. _onOpen(e) {
  24406. this._connected = true;
  24407. this.event(Event.OPEN, e);
  24408. }
  24409. _onMessage(msg) {
  24410. if (!msg || !msg.data)
  24411. return;
  24412. var data = msg.data;
  24413. if (this.disableInput && data) {
  24414. this.event(Event.MESSAGE, data);
  24415. return;
  24416. }
  24417. if (this._input.length > 0 && this._input.bytesAvailable < 1) {
  24418. this._input.clear();
  24419. this._addInputPosition = 0;
  24420. }
  24421. var pre = this._input.pos;
  24422. !this._addInputPosition && (this._addInputPosition = 0);
  24423. this._input.pos = this._addInputPosition;
  24424. if (data) {
  24425. if (typeof (data) == 'string') {
  24426. this._input.writeUTFBytes(data);
  24427. }
  24428. else {
  24429. this._input.writeArrayBuffer(data);
  24430. }
  24431. this._addInputPosition = this._input.pos;
  24432. this._input.pos = pre;
  24433. }
  24434. this.event(Event.MESSAGE, data);
  24435. }
  24436. _onClose(e) {
  24437. this._connected = false;
  24438. this.event(Event.CLOSE, e);
  24439. }
  24440. _onError(e) {
  24441. this.event(Event.ERROR, e);
  24442. }
  24443. send(data) {
  24444. this._socket.send(data);
  24445. }
  24446. flush() {
  24447. if (this._output && this._output.length > 0) {
  24448. var evt;
  24449. try {
  24450. this._socket && this._socket.send(this._output.__getBuffer().slice(0, this._output.length));
  24451. }
  24452. catch (e) {
  24453. evt = e;
  24454. }
  24455. this._output.endian = this.endian;
  24456. this._output.clear();
  24457. if (evt)
  24458. this.event(Event.ERROR, evt);
  24459. }
  24460. }
  24461. }
  24462. Socket.LITTLE_ENDIAN = "littleEndian";
  24463. Socket.BIG_ENDIAN = "bigEndian";
  24464. class System {
  24465. static changeDefinition(name, classObj) {
  24466. window.Laya[name] = classObj;
  24467. var str = name + "=classObj";
  24468. window['eval'](str);
  24469. }
  24470. }
  24471. class HTMLChar {
  24472. constructor() {
  24473. this.reset();
  24474. }
  24475. setData(char, w, h, style) {
  24476. this.char = char;
  24477. this.charNum = char.charCodeAt(0);
  24478. this.x = this.y = 0;
  24479. this.width = w;
  24480. this.height = h;
  24481. this.style = style;
  24482. this.isWord = !HTMLChar._isWordRegExp.test(char);
  24483. return this;
  24484. }
  24485. reset() {
  24486. this.x = this.y = this.width = this.height = 0;
  24487. this.isWord = false;
  24488. this.char = null;
  24489. this.charNum = 0;
  24490. this.style = null;
  24491. return this;
  24492. }
  24493. recover() {
  24494. Pool.recover("HTMLChar", this.reset());
  24495. }
  24496. static create() {
  24497. return Pool.getItemByClass("HTMLChar", HTMLChar);
  24498. }
  24499. _isChar() {
  24500. return true;
  24501. }
  24502. _getCSSStyle() {
  24503. return this.style;
  24504. }
  24505. }
  24506. HTMLChar._isWordRegExp = new RegExp("[\\w\.]", "");
  24507. class Log {
  24508. static enable() {
  24509. if (!Log._logdiv) {
  24510. Log._logdiv = Browser.createElement('div');
  24511. 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%;";
  24512. Browser.document.body.appendChild(Log._logdiv);
  24513. Log._btn = Browser.createElement("button");
  24514. Log._btn.innerText = "Hide";
  24515. Log._btn.style.cssText = "z-index:1000001;position: absolute;left:10px;top:10px;";
  24516. Log._btn.onclick = Log.toggle;
  24517. Browser.document.body.appendChild(Log._btn);
  24518. }
  24519. }
  24520. static toggle() {
  24521. var style = Log._logdiv.style;
  24522. if (style.display === "") {
  24523. Log._btn.innerText = "Show";
  24524. style.display = "none";
  24525. }
  24526. else {
  24527. Log._btn.innerText = "Hide";
  24528. style.display = "";
  24529. }
  24530. }
  24531. static print(value) {
  24532. if (Log._logdiv) {
  24533. if (Log._count >= Log.maxCount)
  24534. Log.clear();
  24535. Log._count++;
  24536. Log._logdiv.innerText += value + "\n";
  24537. if (Log.autoScrollToBottom) {
  24538. if (Log._logdiv.scrollHeight - Log._logdiv.scrollTop - Log._logdiv.clientHeight < 50) {
  24539. Log._logdiv.scrollTop = Log._logdiv.scrollHeight;
  24540. }
  24541. }
  24542. }
  24543. }
  24544. static clear() {
  24545. Log._logdiv.innerText = "";
  24546. Log._count = 0;
  24547. }
  24548. }
  24549. Log._count = 0;
  24550. Log.maxCount = 50;
  24551. Log.autoScrollToBottom = true;
  24552. let DATANUM = 300;
  24553. class PerfData {
  24554. constructor(id, color, name, scale) {
  24555. this.scale = 1.0;
  24556. this.datas = new Array(DATANUM);
  24557. this.datapos = 0;
  24558. this.id = id;
  24559. this.color = color;
  24560. this.name = name;
  24561. this.scale = scale;
  24562. }
  24563. addData(v) {
  24564. this.datas[this.datapos] = v;
  24565. this.datapos++;
  24566. this.datapos %= DATANUM;
  24567. }
  24568. }
  24569. class PerfHUD extends Sprite {
  24570. constructor() {
  24571. super();
  24572. this.datas = [];
  24573. this.xdata = new Array(PerfHUD.DATANUM);
  24574. this.ydata = new Array(PerfHUD.DATANUM);
  24575. this.hud_width = 800;
  24576. this.hud_height = 200;
  24577. this.gMinV = 0;
  24578. this.gMaxV = 100;
  24579. this.textSpace = 40;
  24580. this.sttm = 0;
  24581. PerfHUD.inst = this;
  24582. this._renderType |= SpriteConst.CUSTOM;
  24583. this._setRenderType(this._renderType);
  24584. this._setCustomRender();
  24585. this.addDataDef(0, 0xffffff, 'frame', 1.0);
  24586. this.addDataDef(1, 0x00ff00, 'update', 1.0);
  24587. this.addDataDef(2, 0xff0000, 'flush', 1.0);
  24588. PerfHUD._now = performance ? performance.now.bind(performance) : Date.now;
  24589. }
  24590. now() {
  24591. return PerfHUD._now();
  24592. }
  24593. start() {
  24594. this.sttm = PerfHUD._now();
  24595. }
  24596. end(i) {
  24597. var dt = PerfHUD._now() - this.sttm;
  24598. this.updateValue(i, dt);
  24599. }
  24600. config(w, h) {
  24601. this.hud_width = w;
  24602. this.hud_height = h;
  24603. }
  24604. addDataDef(id, color, name, scale) {
  24605. this.datas[id] = new PerfData(id, color, name, scale);
  24606. }
  24607. updateValue(id, v) {
  24608. this.datas[id].addData(v);
  24609. }
  24610. v2y(v) {
  24611. var bb = this._y + this.hud_height * (1 - (v - this.gMinV) / this.gMaxV);
  24612. return this._y + this.hud_height * (1 - (v - this.gMinV) / this.gMaxV);
  24613. }
  24614. drawHLine(ctx, v, color, text) {
  24615. var sx = this._x;
  24616. var ex = this._x + this.hud_width;
  24617. var sy = this.v2y(v);
  24618. ctx.fillText(text, sx, sy - 6, null, 'green', null);
  24619. sx += this.textSpace;
  24620. ctx.fillStyle = color;
  24621. ctx.fillRect(sx, sy, this._x + this.hud_width, 1, null);
  24622. }
  24623. customRender(ctx, x, y) {
  24624. var now = performance.now();
  24625. if (PerfHUD._lastTm <= 0)
  24626. PerfHUD._lastTm = now;
  24627. this.updateValue(0, now - PerfHUD._lastTm);
  24628. PerfHUD._lastTm = now;
  24629. ctx.save();
  24630. ctx.fillRect(this._x, this._y, this.hud_width, this.hud_height + 4, '#000000cc');
  24631. ctx.globalAlpha = 0.9;
  24632. this.drawHLine(ctx, 0, 'green', ' 0');
  24633. this.drawHLine(ctx, 10, 'green', ' 10');
  24634. this.drawHLine(ctx, 16.667, 'red', ' ');
  24635. this.drawHLine(ctx, 20, 'green', '50|20');
  24636. this.drawHLine(ctx, 16.667 * 2, 'yellow', '');
  24637. this.drawHLine(ctx, 16.667 * 3, 'yellow', '');
  24638. this.drawHLine(ctx, 16.667 * 4, 'yellow', '');
  24639. this.drawHLine(ctx, 50, 'green', '20|50');
  24640. this.drawHLine(ctx, 100, 'green', '10|100');
  24641. for (var di = 0, sz = this.datas.length; di < sz; di++) {
  24642. var cd = this.datas[di];
  24643. if (!cd)
  24644. continue;
  24645. var dtlen = cd.datas.length;
  24646. var dx = (this.hud_width - this.textSpace) / dtlen;
  24647. var cx = cd.datapos;
  24648. var _cx = this._x + this.textSpace;
  24649. ctx.fillStyle = cd.color;
  24650. for (var dtsz = dtlen; cx < dtsz; cx++) {
  24651. var sty = this.v2y(cd.datas[cx] * cd.scale);
  24652. ctx.fillRect(_cx, sty, dx, this.hud_height + this._y - sty, null);
  24653. _cx += dx;
  24654. }
  24655. for (cx = 0; cx < cd.datapos; cx++) {
  24656. sty = this.v2y(cd.datas[cx] * cd.scale);
  24657. ctx.fillRect(_cx, sty, dx, this.hud_height + this._y - sty, null);
  24658. _cx += dx;
  24659. }
  24660. }
  24661. ctx.restore();
  24662. }
  24663. }
  24664. PerfHUD._lastTm = 0;
  24665. PerfHUD._now = null;
  24666. PerfHUD.DATANUM = 300;
  24667. PerfHUD.drawTexTm = 0;
  24668. class PoolCache {
  24669. constructor() {
  24670. this.maxCount = 1000;
  24671. }
  24672. getCacheList() {
  24673. return Pool.getPoolBySign(this.sign);
  24674. }
  24675. tryDispose(force) {
  24676. var list;
  24677. list = Pool.getPoolBySign(this.sign);
  24678. if (list.length > this.maxCount) {
  24679. list.splice(this.maxCount, list.length - this.maxCount);
  24680. }
  24681. }
  24682. static addPoolCacheManager(sign, maxCount = 100) {
  24683. var cache;
  24684. cache = new PoolCache();
  24685. cache.sign = sign;
  24686. cache.maxCount = maxCount;
  24687. CacheManger.regCacheByFunction(Utils.bind(cache.tryDispose, cache), Utils.bind(cache.getCacheList, cache));
  24688. }
  24689. }
  24690. class TimeLine extends EventDispatcher {
  24691. constructor() {
  24692. super(...arguments);
  24693. this._tweenDic = {};
  24694. this._tweenDataList = [];
  24695. this._currTime = 0;
  24696. this._lastTime = 0;
  24697. this._startTime = 0;
  24698. this._index = 0;
  24699. this._gidIndex = 0;
  24700. this._firstTweenDic = {};
  24701. this._startTimeSort = false;
  24702. this._endTimeSort = false;
  24703. this._loopKey = false;
  24704. this.scale = 1;
  24705. this._frameRate = 60;
  24706. this._frameIndex = 0;
  24707. this._total = 0;
  24708. }
  24709. static to(target, props, duration, ease = null, offset = 0) {
  24710. return (new TimeLine()).to(target, props, duration, ease, offset);
  24711. }
  24712. static from(target, props, duration, ease = null, offset = 0) {
  24713. return (new TimeLine()).from(target, props, duration, ease, offset);
  24714. }
  24715. to(target, props, duration, ease = null, offset = 0) {
  24716. return this._create(target, props, duration, ease, offset, true);
  24717. }
  24718. from(target, props, duration, ease = null, offset = 0) {
  24719. return this._create(target, props, duration, ease, offset, false);
  24720. }
  24721. _create(target, props, duration, ease, offset, isTo) {
  24722. var tTweenData = Pool.getItemByClass("tweenData", tweenData);
  24723. tTweenData.isTo = isTo;
  24724. tTweenData.type = 0;
  24725. tTweenData.target = target;
  24726. tTweenData.duration = duration;
  24727. tTweenData.data = props;
  24728. tTweenData.startTime = this._startTime + offset;
  24729. tTweenData.endTime = tTweenData.startTime + tTweenData.duration;
  24730. tTweenData.ease = ease;
  24731. this._startTime = Math.max(tTweenData.endTime, this._startTime);
  24732. this._tweenDataList.push(tTweenData);
  24733. this._startTimeSort = true;
  24734. this._endTimeSort = true;
  24735. return this;
  24736. }
  24737. addLabel(label, offset) {
  24738. var tTweenData = Pool.getItemByClass("tweenData", tweenData);
  24739. tTweenData.type = 1;
  24740. tTweenData.data = label;
  24741. tTweenData.endTime = tTweenData.startTime = this._startTime + offset;
  24742. this._labelDic || (this._labelDic = {});
  24743. this._labelDic[label] = tTweenData;
  24744. this._tweenDataList.push(tTweenData);
  24745. return this;
  24746. }
  24747. removeLabel(label) {
  24748. if (this._labelDic && this._labelDic[label]) {
  24749. var tTweenData = this._labelDic[label];
  24750. if (tTweenData) {
  24751. var tIndex = this._tweenDataList.indexOf(tTweenData);
  24752. if (tIndex > -1) {
  24753. this._tweenDataList.splice(tIndex, 1);
  24754. }
  24755. }
  24756. delete this._labelDic[label];
  24757. }
  24758. }
  24759. gotoTime(time) {
  24760. if (this._tweenDataList == null || this._tweenDataList.length == 0)
  24761. return;
  24762. var tTween;
  24763. var tObject;
  24764. for (var p in this._firstTweenDic) {
  24765. tObject = this._firstTweenDic[p];
  24766. if (tObject) {
  24767. for (var tDataP in tObject) {
  24768. if (tDataP in tObject.diyTarget) {
  24769. tObject.diyTarget[tDataP] = tObject[tDataP];
  24770. }
  24771. }
  24772. }
  24773. }
  24774. for (p in this._tweenDic) {
  24775. tTween = this._tweenDic[p];
  24776. tTween.clear();
  24777. delete this._tweenDic[p];
  24778. }
  24779. this._index = 0;
  24780. this._gidIndex = 0;
  24781. this._currTime = time;
  24782. this._lastTime = Browser.now();
  24783. var tTweenDataCopyList;
  24784. if (this._endTweenDataList == null || this._endTimeSort) {
  24785. this._endTimeSort = false;
  24786. this._endTweenDataList = tTweenDataCopyList = this._tweenDataList.concat();
  24787. function Compare(paraA, paraB) {
  24788. if (paraA.endTime > paraB.endTime) {
  24789. return 1;
  24790. }
  24791. else if (paraA.endTime < paraB.endTime) {
  24792. return -1;
  24793. }
  24794. else {
  24795. return 0;
  24796. }
  24797. }
  24798. tTweenDataCopyList.sort(Compare);
  24799. }
  24800. else {
  24801. tTweenDataCopyList = this._endTweenDataList;
  24802. }
  24803. var tTweenData;
  24804. for (var i = 0, n = tTweenDataCopyList.length; i < n; i++) {
  24805. tTweenData = tTweenDataCopyList[i];
  24806. if (tTweenData.type == 0) {
  24807. if (time >= tTweenData.endTime) {
  24808. this._index = Math.max(this._index, i + 1);
  24809. var props = tTweenData.data;
  24810. if (tTweenData.isTo) {
  24811. for (var tP in props) {
  24812. tTweenData.target[tP] = props[tP];
  24813. }
  24814. }
  24815. }
  24816. else {
  24817. break;
  24818. }
  24819. }
  24820. }
  24821. for (i = 0, n = this._tweenDataList.length; i < n; i++) {
  24822. tTweenData = this._tweenDataList[i];
  24823. if (tTweenData.type == 0) {
  24824. if (time >= tTweenData.startTime && time < tTweenData.endTime) {
  24825. this._index = Math.max(this._index, i + 1);
  24826. this._gidIndex++;
  24827. tTween = Pool.getItemByClass("tween", Tween);
  24828. tTween._create(tTweenData.target, tTweenData.data, tTweenData.duration, tTweenData.ease, Handler.create(this, this._animComplete, [this._gidIndex]), 0, false, tTweenData.isTo, true, false);
  24829. tTween.setStartTime(this._currTime - (time - tTweenData.startTime));
  24830. tTween._updateEase(this._currTime);
  24831. tTween.gid = this._gidIndex;
  24832. this._tweenDic[this._gidIndex] = tTween;
  24833. }
  24834. }
  24835. }
  24836. }
  24837. gotoLabel(Label) {
  24838. if (this._labelDic == null)
  24839. return;
  24840. var tLabelData = this._labelDic[Label];
  24841. if (tLabelData)
  24842. this.gotoTime(tLabelData.startTime);
  24843. }
  24844. pause() {
  24845. ILaya.timer.clear(this, this._update);
  24846. }
  24847. resume() {
  24848. this.play(this._currTime, this._loopKey);
  24849. }
  24850. play(timeOrLabel = 0, loop = false) {
  24851. if (!this._tweenDataList)
  24852. return;
  24853. if (this._startTimeSort) {
  24854. this._startTimeSort = false;
  24855. function Compare(paraA, paraB) {
  24856. if (paraA.startTime > paraB.startTime) {
  24857. return 1;
  24858. }
  24859. else if (paraA.startTime < paraB.startTime) {
  24860. return -1;
  24861. }
  24862. else {
  24863. return 0;
  24864. }
  24865. }
  24866. this._tweenDataList.sort(Compare);
  24867. for (var i = 0, n = this._tweenDataList.length; i < n; i++) {
  24868. var tTweenData = this._tweenDataList[i];
  24869. if (tTweenData != null && tTweenData.type == 0) {
  24870. var tTarget = tTweenData.target;
  24871. var gid = (tTarget.$_GID || (tTarget.$_GID = Utils.getGID()));
  24872. var tSrcData = null;
  24873. if (this._firstTweenDic[gid] == null) {
  24874. tSrcData = {};
  24875. tSrcData.diyTarget = tTarget;
  24876. this._firstTweenDic[gid] = tSrcData;
  24877. }
  24878. else {
  24879. tSrcData = this._firstTweenDic[gid];
  24880. }
  24881. for (var p in tTweenData.data) {
  24882. if (tSrcData[p] == null) {
  24883. tSrcData[p] = tTarget[p];
  24884. }
  24885. }
  24886. }
  24887. }
  24888. }
  24889. if (typeof (timeOrLabel) == 'string') {
  24890. this.gotoLabel(timeOrLabel);
  24891. }
  24892. else {
  24893. this.gotoTime(timeOrLabel);
  24894. }
  24895. this._loopKey = loop;
  24896. this._lastTime = Browser.now();
  24897. ILaya.timer.frameLoop(1, this, this._update);
  24898. }
  24899. _update() {
  24900. if (this._currTime >= this._startTime) {
  24901. if (this._loopKey) {
  24902. this._complete();
  24903. if (!this._tweenDataList)
  24904. return;
  24905. this.gotoTime(0);
  24906. }
  24907. else {
  24908. for (var p in this._tweenDic) {
  24909. tTween = this._tweenDic[p];
  24910. tTween.complete();
  24911. }
  24912. this.pause();
  24913. this._complete();
  24914. return;
  24915. }
  24916. }
  24917. var tNow = Browser.now();
  24918. var tFrameTime = tNow - this._lastTime;
  24919. var tCurrTime = this._currTime += tFrameTime * this.scale;
  24920. this._lastTime = tNow;
  24921. for (p in this._tweenDic) {
  24922. tTween = this._tweenDic[p];
  24923. tTween._updateEase(tCurrTime);
  24924. }
  24925. var tTween;
  24926. if (this._tweenDataList.length != 0 && this._index < this._tweenDataList.length) {
  24927. var tTweenData = this._tweenDataList[this._index];
  24928. if (tCurrTime >= tTweenData.startTime) {
  24929. this._index++;
  24930. if (tTweenData.type == 0) {
  24931. this._gidIndex++;
  24932. tTween = Pool.getItemByClass("tween", Tween);
  24933. tTween._create(tTweenData.target, tTweenData.data, tTweenData.duration, tTweenData.ease, Handler.create(this, this._animComplete, [this._gidIndex]), 0, false, tTweenData.isTo, true, false);
  24934. tTween.setStartTime(tCurrTime);
  24935. tTween.gid = this._gidIndex;
  24936. this._tweenDic[this._gidIndex] = tTween;
  24937. tTween._updateEase(tCurrTime);
  24938. }
  24939. else {
  24940. this.event(Event.LABEL, tTweenData.data);
  24941. }
  24942. }
  24943. }
  24944. }
  24945. _animComplete(index) {
  24946. var tTween = this._tweenDic[index];
  24947. if (tTween)
  24948. delete this._tweenDic[index];
  24949. }
  24950. _complete() {
  24951. this.event(Event.COMPLETE);
  24952. }
  24953. get index() {
  24954. return this._frameIndex;
  24955. }
  24956. set index(value) {
  24957. this._frameIndex = value;
  24958. this.gotoTime(this._frameIndex / this._frameRate * 1000);
  24959. }
  24960. get total() {
  24961. this._total = Math.floor(this._startTime / 1000 * this._frameRate);
  24962. return this._total;
  24963. }
  24964. reset() {
  24965. var p;
  24966. if (this._labelDic) {
  24967. for (p in this._labelDic) {
  24968. delete this._labelDic[p];
  24969. }
  24970. }
  24971. var tTween;
  24972. for (p in this._tweenDic) {
  24973. tTween = this._tweenDic[p];
  24974. tTween.clear();
  24975. delete this._tweenDic[p];
  24976. }
  24977. for (p in this._firstTweenDic) {
  24978. delete this._firstTweenDic[p];
  24979. }
  24980. this._endTweenDataList = null;
  24981. if (this._tweenDataList && this._tweenDataList.length) {
  24982. var i, len;
  24983. len = this._tweenDataList.length;
  24984. for (i = 0; i < len; i++) {
  24985. if (this._tweenDataList[i])
  24986. this._tweenDataList[i].destroy();
  24987. }
  24988. }
  24989. this._tweenDataList.length = 0;
  24990. this._currTime = 0;
  24991. this._lastTime = 0;
  24992. this._startTime = 0;
  24993. this._index = 0;
  24994. this._gidIndex = 0;
  24995. this.scale = 1;
  24996. ILaya.timer.clear(this, this._update);
  24997. }
  24998. destroy() {
  24999. this.reset();
  25000. this._labelDic = null;
  25001. this._tweenDic = null;
  25002. this._tweenDataList = null;
  25003. this._firstTweenDic = null;
  25004. }
  25005. }
  25006. class tweenData {
  25007. constructor() {
  25008. this.type = 0;
  25009. this.isTo = true;
  25010. }
  25011. destroy() {
  25012. this.target = null;
  25013. this.ease = null;
  25014. this.data = null;
  25015. this.isTo = true;
  25016. this.type = 0;
  25017. Pool.recover("tweenData", this);
  25018. }
  25019. }
  25020. class ShaderValue {
  25021. constructor() {
  25022. }
  25023. }
  25024. class ArabicReshaper {
  25025. characterMapContains(c) {
  25026. for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
  25027. if (ArabicReshaper.charsMap[i][0] === c) {
  25028. return true;
  25029. }
  25030. }
  25031. return false;
  25032. }
  25033. getCharRep(c) {
  25034. for (var i = 0; i < ArabicReshaper.charsMap.length; ++i) {
  25035. if (ArabicReshaper.charsMap[i][0] === c) {
  25036. return ArabicReshaper.charsMap[i];
  25037. }
  25038. }
  25039. return false;
  25040. }
  25041. getCombCharRep(c1, c2) {
  25042. for (var i = 0; i < ArabicReshaper.combCharsMap.length; ++i) {
  25043. if (ArabicReshaper.combCharsMap[i][0][0] === c1 && ArabicReshaper.combCharsMap[i][0][1] === c2) {
  25044. return ArabicReshaper.combCharsMap[i];
  25045. }
  25046. }
  25047. return false;
  25048. }
  25049. isTransparent(c) {
  25050. for (var i = 0; i < ArabicReshaper.transChars.length; ++i) {
  25051. if (ArabicReshaper.transChars[i] === c) {
  25052. return true;
  25053. }
  25054. }
  25055. return false;
  25056. }
  25057. getOriginalCharsFromCode(code) {
  25058. var j;
  25059. for (j = 0; j < ArabicReshaper.charsMap.length; ++j) {
  25060. if (ArabicReshaper.charsMap[j].indexOf(code) > -1) {
  25061. return String.fromCharCode(ArabicReshaper.charsMap[j][0]);
  25062. }
  25063. }
  25064. for (j = 0; j < ArabicReshaper.combCharsMap.length; ++j) {
  25065. if (ArabicReshaper.combCharsMap[j].indexOf(code) > -1) {
  25066. return String.fromCharCode(ArabicReshaper.combCharsMap[j][0][0]) +
  25067. String.fromCharCode(ArabicReshaper.combCharsMap[j][0][1]);
  25068. }
  25069. }
  25070. return String.fromCharCode(code);
  25071. }
  25072. convertArabic(normal) {
  25073. var crep, combcrep, shaped = '';
  25074. for (var i = 0; i < normal.length; ++i) {
  25075. var current = normal.charCodeAt(i);
  25076. if (this.characterMapContains(current)) {
  25077. var prev = null, next = null, prevID = i - 1, nextID = i + 1;
  25078. for (; prevID >= 0; --prevID) {
  25079. if (!this.isTransparent(normal.charCodeAt(prevID))) {
  25080. break;
  25081. }
  25082. }
  25083. prev = (prevID >= 0) ? normal.charCodeAt(prevID) : null;
  25084. crep = prev ? this.getCharRep(prev) : false;
  25085. if (!crep || crep[2] == null && crep[3] == null) {
  25086. prev = null;
  25087. }
  25088. for (; nextID < normal.length; ++nextID) {
  25089. if (!this.isTransparent(normal.charCodeAt(nextID))) {
  25090. break;
  25091. }
  25092. }
  25093. next = (nextID < normal.length) ? normal.charCodeAt(nextID) : null;
  25094. crep = next ? this.getCharRep(next) : false;
  25095. if (!crep || crep[3] == null && crep[4] == null) {
  25096. next = null;
  25097. }
  25098. if (current === 0x0644 && next != null &&
  25099. (next === 0x0622 || next === 0x0623 || next === 0x0625 || next === 0x0627)) {
  25100. combcrep = this.getCombCharRep(current, next);
  25101. if (prev != null) {
  25102. shaped += String.fromCharCode(combcrep[4]);
  25103. }
  25104. else {
  25105. shaped += String.fromCharCode(combcrep[1]);
  25106. }
  25107. ++i;
  25108. continue;
  25109. }
  25110. crep = this.getCharRep(current);
  25111. if (prev != null && next != null && crep[3] != null) {
  25112. shaped += String.fromCharCode(crep[3]);
  25113. continue;
  25114. }
  25115. else if (prev != null && crep[4] != null) {
  25116. shaped += String.fromCharCode(crep[4]);
  25117. continue;
  25118. }
  25119. else if (next != null && crep[2] != null) {
  25120. shaped += String.fromCharCode(crep[2]);
  25121. continue;
  25122. }
  25123. else {
  25124. shaped += String.fromCharCode(crep[1]);
  25125. }
  25126. }
  25127. else {
  25128. shaped += String.fromCharCode(current);
  25129. }
  25130. }
  25131. return shaped;
  25132. }
  25133. convertArabicBack(apfb) {
  25134. var toReturn = '', selectedChar;
  25135. var i;
  25136. for (i = 0; i < apfb.length; ++i) {
  25137. selectedChar = apfb.charCodeAt(i);
  25138. toReturn += this.getOriginalCharsFromCode(selectedChar);
  25139. }
  25140. return toReturn;
  25141. }
  25142. }
  25143. ArabicReshaper.charsMap = [[0x0621, 0xFE80, null, null, null],
  25144. [0x0622, 0xFE81, null, null, 0xFE82],
  25145. [0x0623, 0xFE83, null, null, 0xFE84],
  25146. [0x0624, 0xFE85, null, null, 0xFE86],
  25147. [0x0625, 0xFE87, null, null, 0xFE88],
  25148. [0x0626, 0xFE89, 0xFE8B, 0xFE8C, 0xFE8A],
  25149. [0x0627, 0xFE8D, null, null, 0xFE8E],
  25150. [0x0628, 0xFE8F, 0xFE91, 0xFE92, 0xFE90],
  25151. [0x0629, 0xFE93, null, null, 0xFE94],
  25152. [0x062A, 0xFE95, 0xFE97, 0xFE98, 0xFE96],
  25153. [0x062B, 0xFE99, 0xFE9B, 0xFE9C, 0xFE9A],
  25154. [0x062C, 0xFE9D, 0xFE9F, 0xFEA0, 0xFE9E],
  25155. [0x062D, 0xFEA1, 0xFEA3, 0xFEA4, 0xFEA2],
  25156. [0x062E, 0xFEA5, 0xFEA7, 0xFEA8, 0xFEA6],
  25157. [0x062F, 0xFEA9, null, null, 0xFEAA],
  25158. [0x0630, 0xFEAB, null, null, 0xFEAC],
  25159. [0x0631, 0xFEAD, null, null, 0xFEAE],
  25160. [0x0632, 0xFEAF, null, null, 0xFEB0],
  25161. [0x0633, 0xFEB1, 0xFEB3, 0xFEB4, 0xFEB2],
  25162. [0x0634, 0xFEB5, 0xFEB7, 0xFEB8, 0xFEB6],
  25163. [0x0635, 0xFEB9, 0xFEBB, 0xFEBC, 0xFEBA],
  25164. [0x0636, 0xFEBD, 0xFEBF, 0xFEC0, 0xFEBE],
  25165. [0x0637, 0xFEC1, 0xFEC3, 0xFEC4, 0xFEC2],
  25166. [0x0638, 0xFEC5, 0xFEC7, 0xFEC8, 0xFEC6],
  25167. [0x0639, 0xFEC9, 0xFECB, 0xFECC, 0xFECA],
  25168. [0x063A, 0xFECD, 0xFECF, 0xFED0, 0xFECE],
  25169. [0x0640, 0x0640, 0x0640, 0x0640, 0x0640],
  25170. [0x0641, 0xFED1, 0xFED3, 0xFED4, 0xFED2],
  25171. [0x0642, 0xFED5, 0xFED7, 0xFED8, 0xFED6],
  25172. [0x0643, 0xFED9, 0xFEDB, 0xFEDC, 0xFEDA],
  25173. [0x0644, 0xFEDD, 0xFEDF, 0xFEE0, 0xFEDE],
  25174. [0x0645, 0xFEE1, 0xFEE3, 0xFEE4, 0xFEE2],
  25175. [0x0646, 0xFEE5, 0xFEE7, 0xFEE8, 0xFEE6],
  25176. [0x0647, 0xFEE9, 0xFEEB, 0xFEEC, 0xFEEA],
  25177. [0x0648, 0xFEED, null, null, 0xFEEE],
  25178. [0x0649, 0xFEEF, null, null, 0xFEF0],
  25179. [0x064A, 0xFEF1, 0xFEF3, 0xFEF4, 0xFEF2],
  25180. [0x067E, 0xFB56, 0xFB58, 0xFB59, 0xFB57],
  25181. [0x06CC, 0xFBFC, 0xFBFE, 0xFBFF, 0xFBFD],
  25182. [0x0686, 0xFB7A, 0xFB7C, 0xFB7D, 0xFB7B],
  25183. [0x06A9, 0xFB8E, 0xFB90, 0xFB91, 0xFB8F],
  25184. [0x06AF, 0xFB92, 0xFB94, 0xFB95, 0xFB93],
  25185. [0x0698, 0xFB8A, null, null, 0xFB8B]];
  25186. ArabicReshaper.combCharsMap = [[[0x0644, 0x0622], 0xFEF5, null, null, 0xFEF6],
  25187. [[0x0644, 0x0623], 0xFEF7, null, null, 0xFEF8],
  25188. [[0x0644, 0x0625], 0xFEF9, null, null, 0xFEFA],
  25189. [[0x0644, 0x0627], 0xFEFB, null, null, 0xFEFC]];
  25190. ArabicReshaper.transChars = [0x0610,
  25191. 0x0612,
  25192. 0x0613,
  25193. 0x0614,
  25194. 0x0615,
  25195. 0x064B,
  25196. 0x064C,
  25197. 0x064D,
  25198. 0x064E,
  25199. 0x064F,
  25200. 0x0650,
  25201. 0x0651,
  25202. 0x0652,
  25203. 0x0653,
  25204. 0x0654,
  25205. 0x0655,
  25206. 0x0656,
  25207. 0x0657,
  25208. 0x0658,
  25209. 0x0670,
  25210. 0x06D6,
  25211. 0x06D7,
  25212. 0x06D8,
  25213. 0x06D9,
  25214. 0x06DA,
  25215. 0x06DB,
  25216. 0x06DC,
  25217. 0x06DF,
  25218. 0x06E0,
  25219. 0x06E1,
  25220. 0x06E2,
  25221. 0x06E3,
  25222. 0x06E4,
  25223. 0x06E7,
  25224. 0x06E8,
  25225. 0x06EA,
  25226. 0x06EB,
  25227. 0x06EC,
  25228. 0x06ED];
  25229. class MatirxArray {
  25230. static ArrayMul(a, b, o) {
  25231. if (!a) {
  25232. MatirxArray.copyArray(b, o);
  25233. return;
  25234. }
  25235. if (!b) {
  25236. MatirxArray.copyArray(a, o);
  25237. return;
  25238. }
  25239. var ai0, ai1, ai2, ai3;
  25240. for (var i = 0; i < 4; i++) {
  25241. ai0 = a[i];
  25242. ai1 = a[i + 4];
  25243. ai2 = a[i + 8];
  25244. ai3 = a[i + 12];
  25245. o[i] = ai0 * b[0] + ai1 * b[1] + ai2 * b[2] + ai3 * b[3];
  25246. o[i + 4] = ai0 * b[4] + ai1 * b[5] + ai2 * b[6] + ai3 * b[7];
  25247. o[i + 8] = ai0 * b[8] + ai1 * b[9] + ai2 * b[10] + ai3 * b[11];
  25248. o[i + 12] = ai0 * b[12] + ai1 * b[13] + ai2 * b[14] + ai3 * b[15];
  25249. }
  25250. }
  25251. static copyArray(f, t) {
  25252. if (!f)
  25253. return;
  25254. if (!t)
  25255. return;
  25256. for (var i = 0; i < f.length; i++) {
  25257. t[i] = f[i];
  25258. }
  25259. }
  25260. }
  25261. exports.AlphaCmd = AlphaCmd;
  25262. exports.Animation = Animation;
  25263. exports.AnimationBase = AnimationBase;
  25264. exports.ArabicReshaper = ArabicReshaper;
  25265. exports.AtlasGrid = AtlasGrid;
  25266. exports.AtlasInfoManager = AtlasInfoManager;
  25267. exports.AudioSound = AudioSound;
  25268. exports.AudioSoundChannel = AudioSoundChannel;
  25269. exports.BasePoly = BasePoly;
  25270. exports.BaseShader = BaseShader;
  25271. exports.BaseTexture = BaseTexture;
  25272. exports.Bezier = Bezier;
  25273. exports.Bitmap = Bitmap;
  25274. exports.BitmapFont = BitmapFont;
  25275. exports.BlendMode = BlendMode;
  25276. exports.BlurFilter = BlurFilter;
  25277. exports.BlurFilterGLRender = BlurFilterGLRender;
  25278. exports.BlurFilterSetter = BlurFilterSetter;
  25279. exports.BoundsStyle = BoundsStyle;
  25280. exports.Browser = Browser;
  25281. exports.Buffer = Buffer;
  25282. exports.Buffer2D = Buffer2D;
  25283. exports.BufferState2D = BufferState2D;
  25284. exports.BufferStateBase = BufferStateBase;
  25285. exports.ButtonEffect = ButtonEffect;
  25286. exports.Byte = Byte;
  25287. exports.CONST3D2D = CONST3D2D;
  25288. exports.CacheManger = CacheManger;
  25289. exports.CacheStyle = CacheStyle;
  25290. exports.CallLater = CallLater;
  25291. exports.CharRenderInfo = CharRenderInfo;
  25292. exports.CharRender_Canvas = CharRender_Canvas;
  25293. exports.CharRender_Native = CharRender_Native;
  25294. exports.CharSubmitCache = CharSubmitCache;
  25295. exports.ClassUtils = ClassUtils;
  25296. exports.ClipRectCmd = ClipRectCmd;
  25297. exports.ColorFilter = ColorFilter;
  25298. exports.ColorFilterSetter = ColorFilterSetter;
  25299. exports.ColorUtils = ColorUtils;
  25300. exports.CommandEncoder = CommandEncoder;
  25301. exports.CommonScript = CommonScript;
  25302. exports.Component = Component;
  25303. exports.Config = Config;
  25304. exports.Const = Const;
  25305. exports.Context = Context;
  25306. exports.Dragging = Dragging;
  25307. exports.Draw9GridTexture = Draw9GridTexture;
  25308. exports.DrawCircleCmd = DrawCircleCmd;
  25309. exports.DrawCurvesCmd = DrawCurvesCmd;
  25310. exports.DrawImageCmd = DrawImageCmd;
  25311. exports.DrawLineCmd = DrawLineCmd;
  25312. exports.DrawLinesCmd = DrawLinesCmd;
  25313. exports.DrawParticleCmd = DrawParticleCmd;
  25314. exports.DrawPathCmd = DrawPathCmd;
  25315. exports.DrawPieCmd = DrawPieCmd;
  25316. exports.DrawPolyCmd = DrawPolyCmd;
  25317. exports.DrawRectCmd = DrawRectCmd;
  25318. exports.DrawStyle = DrawStyle;
  25319. exports.DrawTextureCmd = DrawTextureCmd;
  25320. exports.DrawTexturesCmd = DrawTexturesCmd;
  25321. exports.DrawTrianglesCmd = DrawTrianglesCmd;
  25322. exports.Earcut = Earcut;
  25323. exports.EarcutNode = EarcutNode;
  25324. exports.Ease = Ease;
  25325. exports.EffectAnimation = EffectAnimation;
  25326. exports.EffectBase = EffectBase;
  25327. exports.Event = Event;
  25328. exports.EventDispatcher = EventDispatcher;
  25329. exports.FadeIn = FadeIn;
  25330. exports.FadeOut = FadeOut;
  25331. exports.FillTextCmd = FillTextCmd;
  25332. exports.FillTextureCmd = FillTextureCmd;
  25333. exports.Filter = Filter;
  25334. exports.FilterSetterBase = FilterSetterBase;
  25335. exports.FontInfo = FontInfo;
  25336. exports.FrameAnimation = FrameAnimation;
  25337. exports.GlowFilter = GlowFilter;
  25338. exports.GlowFilterGLRender = GlowFilterGLRender;
  25339. exports.GlowFilterSetter = GlowFilterSetter;
  25340. exports.GrahamScan = GrahamScan;
  25341. exports.GraphicAnimation = GraphicAnimation;
  25342. exports.Graphics = Graphics;
  25343. exports.GraphicsBounds = GraphicsBounds;
  25344. exports.HTMLCanvas = HTMLCanvas;
  25345. exports.HTMLChar = HTMLChar;
  25346. exports.HTMLImage = HTMLImage;
  25347. exports.Handler = Handler;
  25348. exports.HitArea = HitArea;
  25349. exports.HttpRequest = HttpRequest;
  25350. exports.ICharRender = ICharRender;
  25351. exports.ILaya = ILaya;
  25352. exports.IStatRender = IStatRender;
  25353. exports.IndexBuffer2D = IndexBuffer2D;
  25354. exports.InlcudeFile = InlcudeFile;
  25355. exports.Input = Input;
  25356. exports.KeyBoardManager = KeyBoardManager;
  25357. exports.KeyLocation = KeyLocation;
  25358. exports.Keyboard = Keyboard;
  25359. exports.Laya = Laya;
  25360. exports.LayaGL = LayaGL;
  25361. exports.LayaGLQuickRunner = LayaGLQuickRunner;
  25362. exports.LayaGLRunner = LayaGLRunner;
  25363. exports.LayaGPU = LayaGPU;
  25364. exports.Loader = Loader;
  25365. exports.LoaderManager = LoaderManager;
  25366. exports.LocalStorage = LocalStorage;
  25367. exports.Log = Log;
  25368. exports.MathUtil = MathUtil;
  25369. exports.MatirxArray = MatirxArray;
  25370. exports.Matrix = Matrix;
  25371. exports.Mesh2D = Mesh2D;
  25372. exports.MeshParticle2D = MeshParticle2D;
  25373. exports.MeshQuadTexture = MeshQuadTexture;
  25374. exports.MeshTexture = MeshTexture;
  25375. exports.MeshVG = MeshVG;
  25376. exports.Mouse = Mouse;
  25377. exports.MouseManager = MouseManager;
  25378. exports.Node = Node;
  25379. exports.Path = Path;
  25380. exports.PerfData = PerfData;
  25381. exports.PerfHUD = PerfHUD;
  25382. exports.Point = Point;
  25383. exports.Pool = Pool;
  25384. exports.PoolCache = PoolCache;
  25385. exports.Prefab = Prefab;
  25386. exports.PrimitiveSV = PrimitiveSV;
  25387. exports.QuickTestTool = QuickTestTool;
  25388. exports.Rectangle = Rectangle;
  25389. exports.Render = Render;
  25390. exports.RenderInfo = RenderInfo;
  25391. exports.RenderSprite = RenderSprite;
  25392. exports.RenderState2D = RenderState2D;
  25393. exports.RenderTexture2D = RenderTexture2D;
  25394. exports.Resource = Resource;
  25395. exports.ResourceVersion = ResourceVersion;
  25396. exports.RestoreCmd = RestoreCmd;
  25397. exports.RotateCmd = RotateCmd;
  25398. exports.RunDriver = RunDriver;
  25399. exports.SaveBase = SaveBase;
  25400. exports.SaveClipRect = SaveClipRect;
  25401. exports.SaveCmd = SaveCmd;
  25402. exports.SaveMark = SaveMark;
  25403. exports.SaveTransform = SaveTransform;
  25404. exports.SaveTranslate = SaveTranslate;
  25405. exports.ScaleCmd = ScaleCmd;
  25406. exports.Scene = Scene;
  25407. exports.SceneLoader = SceneLoader;
  25408. exports.SceneUtils = SceneUtils;
  25409. exports.Script = Script;
  25410. exports.Shader = Shader;
  25411. exports.Shader2D = Shader2D;
  25412. exports.Shader2X = Shader2X;
  25413. exports.ShaderCompile = ShaderCompile;
  25414. exports.ShaderDefines2D = ShaderDefines2D;
  25415. exports.ShaderDefinesBase = ShaderDefinesBase;
  25416. exports.ShaderNode = ShaderNode;
  25417. exports.ShaderValue = ShaderValue;
  25418. exports.SkinMeshBuffer = SkinMeshBuffer;
  25419. exports.SkinSV = SkinSV;
  25420. exports.Socket = Socket;
  25421. exports.Sound = Sound;
  25422. exports.SoundChannel = SoundChannel;
  25423. exports.SoundManager = SoundManager;
  25424. exports.SoundNode = SoundNode;
  25425. exports.Sprite = Sprite;
  25426. exports.SpriteConst = SpriteConst;
  25427. exports.SpriteStyle = SpriteStyle;
  25428. exports.Stage = Stage;
  25429. exports.Stat = Stat;
  25430. exports.StatUI = StatUI;
  25431. exports.StringKey = StringKey;
  25432. exports.Submit = Submit;
  25433. exports.SubmitBase = SubmitBase;
  25434. exports.SubmitCMD = SubmitCMD;
  25435. exports.SubmitCanvas = SubmitCanvas;
  25436. exports.SubmitKey = SubmitKey;
  25437. exports.SubmitTarget = SubmitTarget;
  25438. exports.SubmitTexture = SubmitTexture;
  25439. exports.System = System;
  25440. exports.TTFLoader = TTFLoader;
  25441. exports.Text = Text;
  25442. exports.TextAtlas = TextAtlas;
  25443. exports.TextRender = TextRender;
  25444. exports.TextStyle = TextStyle;
  25445. exports.TextTexture = TextTexture;
  25446. exports.Texture = Texture;
  25447. exports.Texture2D = Texture2D;
  25448. exports.TextureSV = TextureSV;
  25449. exports.TimeLine = TimeLine;
  25450. exports.Timer = Timer;
  25451. exports.TouchManager = TouchManager;
  25452. exports.TransformCmd = TransformCmd;
  25453. exports.TranslateCmd = TranslateCmd;
  25454. exports.Tween = Tween;
  25455. exports.URL = URL;
  25456. exports.Utils = Utils;
  25457. exports.Value2D = Value2D;
  25458. exports.VectorGraphManager = VectorGraphManager;
  25459. exports.VertexArrayObject = VertexArrayObject;
  25460. exports.VertexBuffer2D = VertexBuffer2D;
  25461. exports.WeakObject = WeakObject;
  25462. exports.WebAudioSound = WebAudioSound;
  25463. exports.WebAudioSoundChannel = WebAudioSoundChannel;
  25464. exports.WebGL = WebGL;
  25465. exports.WebGLCacheAsNormalCanvas = WebGLCacheAsNormalCanvas;
  25466. exports.WebGLContext = WebGLContext;
  25467. exports.WebGLRTMgr = WebGLRTMgr;
  25468. exports.WordText = WordText;
  25469. exports.WorkerLoader = WorkerLoader;
  25470. exports.__init = __init;
  25471. exports._static = _static;
  25472. exports.alertGlobalError = alertGlobalError;
  25473. exports.enableDebugPanel = enableDebugPanel;
  25474. exports.init = init;
  25475. exports.isWXOpenDataContext = isWXOpenDataContext;
  25476. exports.isWXPosMsg = isWXPosMsg;
  25477. exports.version = version;
  25478. exports.static=_static;
  25479. return exports;
  25480. }({}));