laya.ui.js 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082
  1. (function (exports, Laya) {
  2. 'use strict';
  3. class UIConfig {
  4. }
  5. UIConfig.touchScrollEnable = true;
  6. UIConfig.mouseWheelEnable = true;
  7. UIConfig.showButtons = true;
  8. UIConfig.popupBgColor = "#000000";
  9. UIConfig.popupBgAlpha = 0.5;
  10. UIConfig.closeDialogOnSide = true;
  11. window.UIConfig = UIConfig;
  12. class Styles {
  13. }
  14. Styles.defaultSizeGrid = [4, 4, 4, 4, 0];
  15. Styles.labelColor = "#000000";
  16. Styles.labelPadding = [2, 2, 2, 2];
  17. Styles.inputLabelPadding = [1, 1, 1, 3];
  18. Styles.buttonStateNum = 3;
  19. Styles.buttonLabelColors = ["#32556b", "#32cc6b", "#ff0000", "#C0C0C0"];
  20. Styles.comboBoxItemColors = ["#5e95b6", "#ffffff", "#000000", "#8fa4b1", "#ffffff"];
  21. Styles.scrollBarMinNum = 15;
  22. Styles.scrollBarDelayTime = 500;
  23. class AutoBitmap extends Laya.Graphics {
  24. constructor() {
  25. super(...arguments);
  26. this.autoCacheCmd = true;
  27. this._width = 0;
  28. this._height = 0;
  29. this.uv = null;
  30. }
  31. destroy() {
  32. super.destroy();
  33. this._source = null;
  34. this._sizeGrid = null;
  35. this._offset = null;
  36. }
  37. get sizeGrid() {
  38. return this._sizeGrid;
  39. }
  40. set sizeGrid(value) {
  41. this._sizeGrid = value.map((v) => { return +v; });
  42. this._setChanged();
  43. }
  44. get width() {
  45. if (this._width)
  46. return this._width;
  47. if (this._source)
  48. return this._source.sourceWidth;
  49. return 0;
  50. }
  51. set width(value) {
  52. if (this._width != value) {
  53. this._width = value;
  54. this._setChanged();
  55. }
  56. }
  57. get height() {
  58. if (this._height)
  59. return this._height;
  60. if (this._source)
  61. return this._source.sourceHeight;
  62. return 0;
  63. }
  64. set height(value) {
  65. if (this._height != value) {
  66. this._height = value;
  67. this._setChanged();
  68. }
  69. }
  70. get source() {
  71. return this._source;
  72. }
  73. set source(value) {
  74. if (value) {
  75. this._source = value;
  76. this._setChanged();
  77. }
  78. else {
  79. this._source = null;
  80. this.clear();
  81. }
  82. }
  83. _setChanged() {
  84. if (!this._isChanged) {
  85. this._isChanged = true;
  86. Laya.ILaya.timer.callLater(this, this.changeSource);
  87. }
  88. }
  89. changeSource() {
  90. this._isChanged = false;
  91. var source = this._source;
  92. if (!source || !source.bitmap)
  93. return;
  94. var width = this.width;
  95. var height = this.height;
  96. var sizeGrid = this._sizeGrid;
  97. var sw = source.sourceWidth;
  98. var sh = source.sourceHeight;
  99. if (!sizeGrid || (sw === width && sh === height)) {
  100. this.clear();
  101. this.drawTexture(source, this._offset ? this._offset[0] : 0, this._offset ? this._offset[1] : 0, width, height, null, 1, null, null, this.uv);
  102. }
  103. else {
  104. this.clear();
  105. this.draw9Grid(source, 0, 0, width, height, sizeGrid);
  106. this._repaint();
  107. return;
  108. }
  109. this._repaint();
  110. }
  111. drawBitmap(repeat, tex, x, y, width = 0, height = 0) {
  112. if (width < 0.1 || height < 0.1)
  113. return;
  114. if (repeat && (tex.width != width || tex.height != height))
  115. this.fillTexture(tex, x, y, width, height);
  116. else
  117. this.drawImage(tex, x, y, width, height);
  118. }
  119. static getTexture(tex, x, y, width, height) {
  120. if (width <= 0)
  121. width = 1;
  122. if (height <= 0)
  123. height = 1;
  124. tex.$_GID || (tex.$_GID = Laya.Utils.getGID());
  125. var texture;
  126. if (!texture || !texture._getSource()) {
  127. texture = Laya.Texture.createFromTexture(tex, x, y, width, height);
  128. }
  129. return texture;
  130. }
  131. }
  132. Laya.ClassUtils.regClass("laya.ui.AutoBitmap", AutoBitmap);
  133. Laya.ClassUtils.regClass("Laya.AutoBitmap", AutoBitmap);
  134. class Widget extends Laya.Component {
  135. constructor() {
  136. super(...arguments);
  137. this._top = NaN;
  138. this._bottom = NaN;
  139. this._left = NaN;
  140. this._right = NaN;
  141. this._centerX = NaN;
  142. this._centerY = NaN;
  143. }
  144. onReset() {
  145. this._top = this._bottom = this._left = this._right = this._centerX = this._centerY = NaN;
  146. }
  147. _onEnable() {
  148. if (this.owner.parent)
  149. this._onAdded();
  150. else
  151. this.owner.once(Laya.Event.ADDED, this, this._onAdded);
  152. }
  153. _onDisable() {
  154. this.owner.off(Laya.Event.ADDED, this, this._onAdded);
  155. if (this.owner.parent)
  156. this.owner.parent.off(Laya.Event.RESIZE, this, this._onParentResize);
  157. }
  158. _onAdded() {
  159. if (this.owner.parent)
  160. this.owner.parent.on(Laya.Event.RESIZE, this, this._onParentResize);
  161. this.resetLayoutX();
  162. this.resetLayoutY();
  163. }
  164. _onParentResize() {
  165. var flagX = this.resetLayoutX();
  166. var flagY = this.resetLayoutY();
  167. if (flagX || flagY)
  168. this.owner.event(Laya.Event.RESIZE);
  169. }
  170. resetLayoutX() {
  171. var owner = this.owner;
  172. if (!owner)
  173. return false;
  174. var parent = owner.parent;
  175. if (parent) {
  176. if (!isNaN(this.centerX)) {
  177. owner.x = Math.round((parent.width - owner.displayWidth) * 0.5 + this.centerX + owner.pivotX * owner.scaleX);
  178. }
  179. else if (!isNaN(this.left)) {
  180. owner.x = Math.round(this.left + owner.pivotX * owner.scaleX);
  181. if (!isNaN(this.right)) {
  182. var temp = (parent._width - this.left - this.right) / (owner.scaleX || 0.01);
  183. if (temp != owner.width) {
  184. owner.width = temp;
  185. return true;
  186. }
  187. }
  188. }
  189. else if (!isNaN(this.right)) {
  190. owner.x = Math.round(parent.width - owner.displayWidth - this.right + owner.pivotX * owner.scaleX);
  191. }
  192. }
  193. return false;
  194. }
  195. resetLayoutY() {
  196. var owner = this.owner;
  197. if (!owner)
  198. return false;
  199. var parent = owner.parent;
  200. if (parent) {
  201. if (!isNaN(this.centerY)) {
  202. owner.y = Math.round((parent.height - owner.displayHeight) * 0.5 + this.centerY + owner.pivotY * owner.scaleY);
  203. }
  204. else if (!isNaN(this.top)) {
  205. owner.y = Math.round(this.top + owner.pivotY * owner.scaleY);
  206. if (!isNaN(this.bottom)) {
  207. var temp = (parent._height - this.top - this.bottom) / (owner.scaleY || 0.01);
  208. if (temp != owner.height) {
  209. owner.height = temp;
  210. return true;
  211. }
  212. }
  213. }
  214. else if (!isNaN(this.bottom)) {
  215. owner.y = Math.round(parent.height - owner.displayHeight - this.bottom + owner.pivotY * owner.scaleY);
  216. }
  217. }
  218. return false;
  219. }
  220. resetLayout() {
  221. if (this.owner) {
  222. this.resetLayoutX();
  223. this.resetLayoutY();
  224. }
  225. }
  226. get top() {
  227. return this._top;
  228. }
  229. set top(value) {
  230. if (this._top != value) {
  231. this._top = value;
  232. this.resetLayoutY();
  233. }
  234. }
  235. get bottom() {
  236. return this._bottom;
  237. }
  238. set bottom(value) {
  239. if (this._bottom != value) {
  240. this._bottom = value;
  241. this.resetLayoutY();
  242. }
  243. }
  244. get left() {
  245. return this._left;
  246. }
  247. set left(value) {
  248. if (this._left != value) {
  249. this._left = value;
  250. this.resetLayoutX();
  251. }
  252. }
  253. get right() {
  254. return this._right;
  255. }
  256. set right(value) {
  257. if (this._right != value) {
  258. this._right = value;
  259. this.resetLayoutX();
  260. }
  261. }
  262. get centerX() {
  263. return this._centerX;
  264. }
  265. set centerX(value) {
  266. if (this._centerX != value) {
  267. this._centerX = value;
  268. this.resetLayoutX();
  269. }
  270. }
  271. get centerY() {
  272. return this._centerY;
  273. }
  274. set centerY(value) {
  275. if (this._centerY != value) {
  276. this._centerY = value;
  277. this.resetLayoutY();
  278. }
  279. }
  280. }
  281. Widget.EMPTY = null;
  282. Laya.ILaya.regClass(Widget);
  283. Widget.EMPTY = new Widget();
  284. Laya.ClassUtils.regClass("laya.ui.Widget", Widget);
  285. Laya.ClassUtils.regClass("Laya.Widget", Widget);
  286. class UIEvent extends Laya.Event {
  287. }
  288. UIEvent.SHOW_TIP = "showtip";
  289. UIEvent.HIDE_TIP = "hidetip";
  290. Laya.ILaya.regClass(UIEvent);
  291. Laya.ClassUtils.regClass("laya.ui.UIEvent", UIEvent);
  292. Laya.ClassUtils.regClass("Laya.UIEvent", UIEvent);
  293. class UIUtils {
  294. static fillArray(arr, str, type = null) {
  295. var temp = arr.concat();
  296. if (str) {
  297. var a = str.split(",");
  298. for (var i = 0, n = Math.min(temp.length, a.length); i < n; i++) {
  299. var value = a[i];
  300. temp[i] = (value == "true" ? true : (value == "false" ? false : value));
  301. if (type != null)
  302. temp[i] = type(value);
  303. }
  304. }
  305. return temp;
  306. }
  307. static toColor(color) {
  308. return Laya.Utils.toHexColor(color);
  309. }
  310. static gray(traget, isGray = true) {
  311. if (isGray) {
  312. UIUtils.addFilter(traget, UIUtils.grayFilter);
  313. }
  314. else {
  315. UIUtils.clearFilter(traget, Laya.ColorFilter);
  316. }
  317. }
  318. static addFilter(target, filter) {
  319. var filters = target.filters || [];
  320. filters.push(filter);
  321. target.filters = filters;
  322. }
  323. static clearFilter(target, filterType) {
  324. var filters = target.filters;
  325. if (filters != null && filters.length > 0) {
  326. for (var i = filters.length - 1; i > -1; i--) {
  327. var filter = filters[i];
  328. if (filter instanceof filterType)
  329. filters.splice(i, 1);
  330. }
  331. target.filters = filters;
  332. }
  333. }
  334. static _getReplaceStr(word) {
  335. return UIUtils.escapeSequence[word];
  336. }
  337. static adptString(str) {
  338. return str.replace(/\\(\w)/g, UIUtils._getReplaceStr);
  339. }
  340. static getBindFun(value) {
  341. if (!UIUtils._funMap) {
  342. UIUtils._funMap = new Laya.WeakObject();
  343. }
  344. var fun = UIUtils._funMap.get(value);
  345. if (fun == null) {
  346. var temp = "\"" + value + "\"";
  347. temp = temp.replace(/^"\${|}"$/g, "").replace(/\${/g, "\"+").replace(/}/g, "+\"");
  348. var str = "(function(data){if(data==null)return;with(data){try{\nreturn " + temp + "\n}catch(e){}}})";
  349. fun = window.Laya._runScript(str);
  350. UIUtils._funMap.set(value, fun);
  351. }
  352. return fun;
  353. }
  354. }
  355. UIUtils.grayFilter = new Laya.ColorFilter([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]);
  356. UIUtils.escapeSequence = { "\\n": "\n", "\\t": "\t" };
  357. UIUtils._funMap = null;
  358. Laya.ClassUtils.regClass("laya.ui.UIUtils", UIUtils);
  359. Laya.ClassUtils.regClass("Laya.UIUtils", UIUtils);
  360. class UIComponent extends Laya.Sprite {
  361. constructor(createChildren = true) {
  362. super();
  363. this._anchorX = NaN;
  364. this._anchorY = NaN;
  365. this._widget = Widget.EMPTY;
  366. if (createChildren) {
  367. this.preinitialize();
  368. this.createChildren();
  369. this.initialize();
  370. }
  371. }
  372. destroy(destroyChild = true) {
  373. super.destroy(destroyChild);
  374. this._dataSource = null;
  375. this._tag = null;
  376. this._toolTip = null;
  377. }
  378. preinitialize() {
  379. }
  380. createChildren() {
  381. }
  382. initialize() {
  383. }
  384. get width() {
  385. return this.get_width();
  386. }
  387. get_width() {
  388. if (this._width)
  389. return this._width;
  390. return this.measureWidth();
  391. }
  392. measureWidth() {
  393. var max = 0;
  394. this.commitMeasure();
  395. for (var i = this.numChildren - 1; i > -1; i--) {
  396. var comp = this.getChildAt(i);
  397. if (comp._visible) {
  398. max = Math.max(comp._x + comp.width * comp.scaleX, max);
  399. }
  400. }
  401. return max;
  402. }
  403. commitMeasure() {
  404. }
  405. get height() {
  406. return this.get_height();
  407. }
  408. get_height() {
  409. if (this._height)
  410. return this._height;
  411. return this.measureHeight();
  412. }
  413. measureHeight() {
  414. var max = 0;
  415. this.commitMeasure();
  416. for (var i = this.numChildren - 1; i > -1; i--) {
  417. var comp = this.getChildAt(i);
  418. if (comp._visible) {
  419. max = Math.max(comp._y + comp.height * comp.scaleY, max);
  420. }
  421. }
  422. return max;
  423. }
  424. get dataSource() {
  425. return this.get_dataSource();
  426. }
  427. get_dataSource() {
  428. return this._dataSource;
  429. }
  430. set dataSource(value) {
  431. this.set_dataSource(value);
  432. }
  433. set_dataSource(value) {
  434. this._dataSource = value;
  435. for (var prop in this._dataSource) {
  436. if (prop in this && !(typeof (this[prop]) == 'function')) {
  437. this[prop] = this._dataSource[prop];
  438. }
  439. }
  440. }
  441. get top() {
  442. return this.get_top();
  443. }
  444. get_top() {
  445. return this._widget.top;
  446. }
  447. set top(value) {
  448. this.set_top(value);
  449. }
  450. set_top(value) {
  451. if (value != this._widget.top) {
  452. this._getWidget().top = value;
  453. }
  454. }
  455. get bottom() {
  456. return this.get_bottom();
  457. }
  458. get_bottom() {
  459. return this._widget.bottom;
  460. }
  461. set bottom(value) {
  462. this.set_bottom(value);
  463. }
  464. set_bottom(value) {
  465. if (value != this._widget.bottom) {
  466. this._getWidget().bottom = value;
  467. }
  468. }
  469. get left() {
  470. return this._widget.left;
  471. }
  472. set left(value) {
  473. if (value != this._widget.left) {
  474. this._getWidget().left = value;
  475. }
  476. }
  477. get right() {
  478. return this._widget.right;
  479. }
  480. set right(value) {
  481. if (value != this._widget.right) {
  482. this._getWidget().right = value;
  483. }
  484. }
  485. get centerX() {
  486. return this._widget.centerX;
  487. }
  488. set centerX(value) {
  489. if (value != this._widget.centerX) {
  490. this._getWidget().centerX = value;
  491. }
  492. }
  493. get centerY() {
  494. return this._widget.centerY;
  495. }
  496. set centerY(value) {
  497. if (value != this._widget.centerY) {
  498. this._getWidget().centerY = value;
  499. }
  500. }
  501. _sizeChanged() {
  502. if (!isNaN(this._anchorX))
  503. this.pivotX = this.anchorX * this.width;
  504. if (!isNaN(this._anchorY))
  505. this.pivotY = this.anchorY * this.height;
  506. this.event(Laya.Event.RESIZE);
  507. if (this._widget !== Widget.EMPTY)
  508. this._widget.resetLayout();
  509. }
  510. get tag() {
  511. return this._tag;
  512. }
  513. set tag(value) {
  514. this._tag = value;
  515. }
  516. get toolTip() {
  517. return this._toolTip;
  518. }
  519. set toolTip(value) {
  520. if (this._toolTip != value) {
  521. this._toolTip = value;
  522. if (value != null) {
  523. this.on(Laya.Event.MOUSE_OVER, this, this.onMouseOver);
  524. this.on(Laya.Event.MOUSE_OUT, this, this.onMouseOut);
  525. }
  526. else {
  527. this.off(Laya.Event.MOUSE_OVER, this, this.onMouseOver);
  528. this.off(Laya.Event.MOUSE_OUT, this, this.onMouseOut);
  529. }
  530. }
  531. }
  532. onMouseOver(e) {
  533. Laya.ILaya.stage.event(UIEvent.SHOW_TIP, this._toolTip);
  534. }
  535. onMouseOut(e) {
  536. Laya.ILaya.stage.event(UIEvent.HIDE_TIP, this._toolTip);
  537. }
  538. get gray() {
  539. return this._gray;
  540. }
  541. set gray(value) {
  542. if (value !== this._gray) {
  543. this._gray = value;
  544. UIUtils.gray(this, value);
  545. }
  546. }
  547. get disabled() {
  548. return this._disabled;
  549. }
  550. set disabled(value) {
  551. if (value !== this._disabled) {
  552. this.gray = this._disabled = value;
  553. this.mouseEnabled = !value;
  554. }
  555. }
  556. _getWidget() {
  557. this._widget === Widget.EMPTY && (this._widget = this.addComponent(Widget));
  558. return this._widget;
  559. }
  560. set scaleX(value) {
  561. this.set_scaleX(value);
  562. }
  563. set_scaleX(value) {
  564. if (super.get_scaleX() == value)
  565. return;
  566. super.set_scaleX(value);
  567. this.event(Laya.Event.RESIZE);
  568. }
  569. get scaleX() {
  570. return super.scaleX;
  571. }
  572. set scaleY(value) {
  573. this.set_scaleY(value);
  574. }
  575. set_scaleY(value) {
  576. if (super.get_scaleY() == value)
  577. return;
  578. super.set_scaleY(value);
  579. this.event(Laya.Event.RESIZE);
  580. }
  581. get scaleY() {
  582. return super.scaleY;
  583. }
  584. onCompResize() {
  585. this._sizeChanged();
  586. }
  587. set width(value) {
  588. this.set_width(value);
  589. }
  590. set_width(value) {
  591. if (super.get_width() == value)
  592. return;
  593. super.set_width(value);
  594. this.callLater(this._sizeChanged);
  595. }
  596. set height(value) {
  597. this.set_height(value);
  598. }
  599. set_height(value) {
  600. if (super.get_height() == value)
  601. return;
  602. super.set_height(value);
  603. this.callLater(this._sizeChanged);
  604. }
  605. get anchorX() {
  606. return this.get_anchorX();
  607. }
  608. get_anchorX() {
  609. return this._anchorX;
  610. }
  611. set anchorX(value) {
  612. this.set_anchorX(value);
  613. }
  614. set_anchorX(value) {
  615. if (this._anchorX != value) {
  616. this._anchorX = value;
  617. this.callLater(this._sizeChanged);
  618. }
  619. }
  620. get anchorY() {
  621. return this.get_anchorY();
  622. }
  623. get_anchorY() {
  624. return this._anchorY;
  625. }
  626. set anchorY(value) {
  627. this.set_anchorY(value);
  628. }
  629. set_anchorY(value) {
  630. if (this._anchorY != value) {
  631. this._anchorY = value;
  632. this.callLater(this._sizeChanged);
  633. }
  634. }
  635. _childChanged(child = null) {
  636. this.callLater(this._sizeChanged);
  637. super._childChanged(child);
  638. }
  639. }
  640. Laya.ILaya.regClass(UIComponent);
  641. Laya.ClassUtils.regClass("laya.ui.UIComponent", UIComponent);
  642. Laya.ClassUtils.regClass("Laya.UIComponent", UIComponent);
  643. class Image extends UIComponent {
  644. constructor(skin = null) {
  645. super();
  646. this.skin = skin;
  647. }
  648. destroy(destroyChild = true) {
  649. super.destroy(destroyChild);
  650. this._bitmap && this._bitmap.destroy();
  651. this._bitmap = null;
  652. }
  653. dispose() {
  654. this.destroy(true);
  655. Laya.ILaya.loader.clearRes(this._skin);
  656. }
  657. createChildren() {
  658. this.graphics = this._bitmap = new AutoBitmap();
  659. this._bitmap.autoCacheCmd = false;
  660. }
  661. get skin() {
  662. return this._skin;
  663. }
  664. set skin(value) {
  665. if (this._skin != value) {
  666. this._skin = value;
  667. if (value) {
  668. var source = Laya.Loader.getRes(value);
  669. if (source) {
  670. this.source = source;
  671. this.onCompResize();
  672. }
  673. else
  674. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this.setSource, [this._skin]), null, Laya.Loader.IMAGE, 1, true, this._group);
  675. }
  676. else {
  677. this.source = null;
  678. }
  679. }
  680. }
  681. get source() {
  682. return this._bitmap.source;
  683. }
  684. set source(value) {
  685. if (!this._bitmap)
  686. return;
  687. this._bitmap.source = value;
  688. this.event(Laya.Event.LOADED);
  689. this.repaint();
  690. }
  691. get group() {
  692. return this._group;
  693. }
  694. set group(value) {
  695. if (value && this._skin)
  696. Laya.Loader.setGroup(this._skin, value);
  697. this._group = value;
  698. }
  699. setSource(url, img = null) {
  700. if (url === this._skin && img) {
  701. this.source = img;
  702. this.onCompResize();
  703. }
  704. }
  705. measureWidth() {
  706. return this._bitmap.width;
  707. }
  708. measureHeight() {
  709. return this._bitmap.height;
  710. }
  711. set width(value) {
  712. super.width = value;
  713. this._bitmap.width = value == 0 ? 0.0000001 : value;
  714. }
  715. get width() {
  716. return super.width;
  717. }
  718. set height(value) {
  719. super.height = value;
  720. this._bitmap.height = value == 0 ? 0.0000001 : value;
  721. }
  722. get height() {
  723. return super.height;
  724. }
  725. get sizeGrid() {
  726. if (this._bitmap.sizeGrid)
  727. return this._bitmap.sizeGrid.join(",");
  728. return null;
  729. }
  730. set sizeGrid(value) {
  731. this._bitmap.sizeGrid = UIUtils.fillArray(Styles.defaultSizeGrid, value, Number);
  732. }
  733. set dataSource(value) {
  734. this._dataSource = value;
  735. if (typeof (value) == 'string')
  736. this.skin = value;
  737. else
  738. super.dataSource = value;
  739. }
  740. get dataSource() {
  741. return super.dataSource;
  742. }
  743. }
  744. Laya.ILaya.regClass(Image);
  745. Laya.ClassUtils.regClass("laya.ui.Image", Image);
  746. Laya.ClassUtils.regClass("Laya.Image", Image);
  747. class AdvImage extends Image {
  748. constructor(skin = null) {
  749. super();
  750. this.advsListArr = [];
  751. this.resUrl = "https://unioncdn.layabox.com/config/iconlist.json";
  752. this._http = new Laya.Browser.window.XMLHttpRequest();
  753. this._data = [];
  754. this._resquestTime = 360000;
  755. this._playIndex = 0;
  756. this._lunboTime = 5000;
  757. this.skin = skin;
  758. this.setLoadUrl();
  759. this.init();
  760. this.size(120, 120);
  761. }
  762. setLoadUrl() {
  763. }
  764. init() {
  765. if (this.isSupportJump()) {
  766. if (Laya.Browser.onMiniGame || Laya.Browser.onBDMiniGame) {
  767. Laya.ILaya.timer.loop(this._resquestTime, this, this.onGetAdvsListData);
  768. }
  769. this.onGetAdvsListData();
  770. this.initEvent();
  771. }
  772. else
  773. this.visible = false;
  774. }
  775. initEvent() {
  776. this.on(Laya.Event.CLICK, this, this.onAdvsImgClick);
  777. }
  778. onAdvsImgClick() {
  779. var currentJumpUrl = this.getCurrentAppidObj();
  780. if (currentJumpUrl)
  781. this.jumptoGame();
  782. }
  783. revertAdvsData() {
  784. if (this.advsListArr[this._playIndex]) {
  785. this.visible = true;
  786. this.skin = this.advsListArr[this._playIndex];
  787. }
  788. }
  789. isSupportJump() {
  790. if (Laya.Browser.onMiniGame) {
  791. var isSupperJump = window.wx.navigateToMiniProgram instanceof Function;
  792. return isSupperJump;
  793. }
  794. else if (Laya.Browser.onBDMiniGame)
  795. return true;
  796. return false;
  797. }
  798. jumptoGame() {
  799. var advsObj = this.advsListArr[this._playIndex];
  800. var desGameId = parseInt(advsObj.gameid);
  801. var extendInfo = advsObj.extendInfo;
  802. var path = advsObj.path;
  803. if (Laya.Browser.onMiniGame) {
  804. if (this.isSupportJump()) {
  805. window.wx.navigateToMiniProgram({
  806. appId: this._appid,
  807. path: "",
  808. extraData: "",
  809. envVersion: "release",
  810. success: function success() {
  811. console.log("-------------跳转成功--------------");
  812. },
  813. fail: function fail() {
  814. console.log("-------------跳转失败--------------");
  815. },
  816. complete: function complete() {
  817. console.log("-------------跳转接口调用成功--------------");
  818. this.updateAdvsInfo();
  819. }.bind(this)
  820. });
  821. }
  822. }
  823. else if (Laya.Browser.onBDMiniGame) ;
  824. else {
  825. this.visible = false;
  826. }
  827. }
  828. updateAdvsInfo() {
  829. this.visible = false;
  830. this.onLunbo();
  831. Laya.ILaya.timer.loop(this._lunboTime, this, this.onLunbo);
  832. }
  833. onLunbo() {
  834. if (this._playIndex >= this.advsListArr.length - 1)
  835. this._playIndex = 0;
  836. else
  837. this._playIndex += 1;
  838. this.visible = true;
  839. this.revertAdvsData();
  840. }
  841. getCurrentAppidObj() {
  842. return this.advsListArr[this._playIndex];
  843. }
  844. onGetAdvsListData() {
  845. var _this = this;
  846. var random = AdvImage.randRange(10000, 1000000);
  847. var url = this.resUrl + "?" + random;
  848. this._http.open("get", url, true);
  849. this._http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  850. this._http.responseType = "text";
  851. this._http.onerror = function (e) {
  852. _this._onError(e);
  853. };
  854. this._http.onload = function (e) {
  855. _this._onLoad(e);
  856. };
  857. this._http.send(null);
  858. }
  859. static randRange(minNum, maxNum) {
  860. return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
  861. }
  862. _onError(e) {
  863. this.error("Request failed Status:" + this._http.status + " text:" + this._http.statusText);
  864. }
  865. _onLoad(e) {
  866. var http = this._http;
  867. var status = http.status !== undefined ? http.status : 200;
  868. if (status === 200 || status === 204 || status === 0) {
  869. this.complete();
  870. }
  871. else {
  872. this.error("[" + http.status + "]" + http.statusText + ":" + http.responseURL);
  873. }
  874. }
  875. error(message) {
  876. this.event(Laya.Event.ERROR, message);
  877. }
  878. complete() {
  879. try {
  880. this._data = this._http.response || this._http.responseText;
  881. this._data = JSON.parse(this._data);
  882. this.advsListArr = this._data.list;
  883. this._appid = this._data.appid;
  884. this.updateAdvsInfo();
  885. this.revertAdvsData();
  886. }
  887. catch (e) {
  888. this.error(e.message);
  889. }
  890. }
  891. getAdvsQArr(data) {
  892. var tempArr = [];
  893. var gameAdvsObj = Laya.LocalStorage.getJSON("gameObj");
  894. for (var key in data) {
  895. var tempObj = data[key];
  896. if (gameAdvsObj && gameAdvsObj[tempObj.gameid] && !tempObj.isQiangZhi)
  897. continue;
  898. tempArr.push(tempObj);
  899. }
  900. return tempArr;
  901. }
  902. clear() {
  903. var http = this._http;
  904. http.onerror = http.onabort = http.onprogress = http.onload = null;
  905. }
  906. destroy(destroyChild = true) {
  907. Laya.ILaya.timer.clear(this, this.onLunbo);
  908. super.destroy(true);
  909. this.clear();
  910. Laya.ILaya.timer.clear(this, this.onGetAdvsListData);
  911. }
  912. }
  913. Laya.ClassUtils.regClass("laya.ui.AdvImage", AdvImage);
  914. Laya.ClassUtils.regClass("Laya.AdvImage", AdvImage);
  915. class Box extends UIComponent {
  916. set dataSource(value) {
  917. this._dataSource = value;
  918. for (var name in value) {
  919. var comp = this.getChildByName(name);
  920. if (comp)
  921. comp.dataSource = value[name];
  922. else if (name in this && !(this[name] instanceof Function))
  923. this[name] = value[name];
  924. }
  925. }
  926. get dataSource() {
  927. return super.dataSource;
  928. }
  929. get bgColor() {
  930. return this._bgColor;
  931. }
  932. set bgColor(value) {
  933. this._bgColor = value;
  934. if (value) {
  935. this._onResize(null);
  936. this.on(Laya.Event.RESIZE, this, this._onResize);
  937. }
  938. else {
  939. this.graphics.clear();
  940. this.off(Laya.Event.RESIZE, this, this._onResize);
  941. }
  942. }
  943. _onResize(e) {
  944. this.graphics.clear();
  945. this.graphics.drawRect(0, 0, this.width, this.height, this._bgColor);
  946. }
  947. }
  948. Laya.ILaya.regClass(Box);
  949. Laya.ClassUtils.regClass("laya.ui.Box", Box);
  950. Laya.ClassUtils.regClass("Laya.Box", Box);
  951. class Button extends UIComponent {
  952. constructor(skin = null, label = "") {
  953. super();
  954. this._labelColors = Styles.buttonLabelColors;
  955. this._state = 0;
  956. this._autoSize = true;
  957. this._stateNum = Styles.buttonStateNum;
  958. this._stateChanged = false;
  959. this.skin = skin;
  960. this.label = label;
  961. }
  962. destroy(destroyChild = true) {
  963. super.destroy(destroyChild);
  964. this._bitmap && this._bitmap.destroy();
  965. this._text && this._text.destroy(destroyChild);
  966. this._bitmap = null;
  967. this._text = null;
  968. this._clickHandler = null;
  969. this._labelColors = this._sources = this._strokeColors = null;
  970. }
  971. createChildren() {
  972. this.graphics = this._bitmap = new AutoBitmap();
  973. }
  974. createText() {
  975. if (!this._text) {
  976. this._text = new Laya.Text();
  977. this._text.overflow = Laya.Text.HIDDEN;
  978. this._text.align = "center";
  979. this._text.valign = "middle";
  980. this._text.width = this._width;
  981. this._text.height = this._height;
  982. }
  983. }
  984. initialize() {
  985. if (this._mouseState !== 1) {
  986. this.mouseEnabled = true;
  987. this._setBit(Laya.Const.HAS_MOUSE, true);
  988. }
  989. this._createListener(Laya.Event.MOUSE_OVER, this, this.onMouse, null, false, false);
  990. this._createListener(Laya.Event.MOUSE_OUT, this, this.onMouse, null, false, false);
  991. this._createListener(Laya.Event.MOUSE_DOWN, this, this.onMouse, null, false, false);
  992. this._createListener(Laya.Event.MOUSE_UP, this, this.onMouse, null, false, false);
  993. this._createListener(Laya.Event.CLICK, this, this.onMouse, null, false, false);
  994. }
  995. onMouse(e) {
  996. if (this.toggle === false && this._selected)
  997. return;
  998. if (e.type === Laya.Event.CLICK) {
  999. this.toggle && (this.selected = !this._selected);
  1000. this._clickHandler && this._clickHandler.run();
  1001. return;
  1002. }
  1003. !this._selected && (this.state = Button.stateMap[e.type]);
  1004. }
  1005. get skin() {
  1006. return this._skin;
  1007. }
  1008. set skin(value) {
  1009. if (this._skin != value) {
  1010. this._skin = value;
  1011. if (value) {
  1012. if (!Laya.Loader.getRes(value)) {
  1013. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  1014. }
  1015. else {
  1016. this._skinLoaded();
  1017. }
  1018. }
  1019. else {
  1020. this._skinLoaded();
  1021. }
  1022. }
  1023. }
  1024. _skinLoaded() {
  1025. this.callLater(this.changeClips);
  1026. this._setStateChanged();
  1027. this._sizeChanged();
  1028. this.event(Laya.Event.LOADED);
  1029. }
  1030. get stateNum() {
  1031. return this._stateNum;
  1032. }
  1033. set stateNum(value) {
  1034. if (typeof value == 'string') {
  1035. value = parseInt(value);
  1036. }
  1037. if (this._stateNum != value) {
  1038. this._stateNum = value < 1 ? 1 : value > 3 ? 3 : value;
  1039. this.callLater(this.changeClips);
  1040. }
  1041. }
  1042. changeClips() {
  1043. var img = Laya.Loader.getRes(this._skin);
  1044. if (!img) {
  1045. console.log("lose skin", this._skin);
  1046. return;
  1047. }
  1048. var width = img.sourceWidth;
  1049. var height = img.sourceHeight / this._stateNum;
  1050. img.$_GID || (img.$_GID = Laya.Utils.getGID());
  1051. var key = img.$_GID + "-" + this._stateNum;
  1052. var clips = Laya.WeakObject.I.get(key);
  1053. if (!Laya.Utils.isOkTextureList(clips)) {
  1054. clips = null;
  1055. }
  1056. if (clips)
  1057. this._sources = clips;
  1058. else {
  1059. this._sources = [];
  1060. if (this._stateNum === 1) {
  1061. this._sources.push(img);
  1062. }
  1063. else {
  1064. for (var i = 0; i < this._stateNum; i++) {
  1065. this._sources.push(Laya.Texture.createFromTexture(img, 0, height * i, width, height));
  1066. }
  1067. }
  1068. Laya.WeakObject.I.set(key, this._sources);
  1069. }
  1070. if (this._autoSize) {
  1071. this._bitmap.width = this._width || width;
  1072. this._bitmap.height = this._height || height;
  1073. if (this._text) {
  1074. this._text.width = this._bitmap.width;
  1075. this._text.height = this._bitmap.height;
  1076. }
  1077. }
  1078. else {
  1079. this._text && (this._text.x = width);
  1080. }
  1081. }
  1082. measureWidth() {
  1083. this.runCallLater(this.changeClips);
  1084. if (this._autoSize)
  1085. return this._bitmap.width;
  1086. this.runCallLater(this.changeState);
  1087. return this._bitmap.width + (this._text ? this._text.width : 0);
  1088. }
  1089. measureHeight() {
  1090. this.runCallLater(this.changeClips);
  1091. return this._text ? Math.max(this._bitmap.height, this._text.height) : this._bitmap.height;
  1092. }
  1093. get label() {
  1094. return this._text ? this._text.text : null;
  1095. }
  1096. set label(value) {
  1097. if (!this._text && !value)
  1098. return;
  1099. this.createText();
  1100. if (this._text.text != value) {
  1101. value && !this._text.parent && this.addChild(this._text);
  1102. this._text.text = (value + "").replace(/\\n/g, "\n");
  1103. this._setStateChanged();
  1104. }
  1105. }
  1106. get selected() {
  1107. return this._selected;
  1108. }
  1109. set selected(value) {
  1110. if (this._selected != value) {
  1111. this._selected = value;
  1112. this.state = this._selected ? 2 : 0;
  1113. this.event(Laya.Event.CHANGE);
  1114. }
  1115. }
  1116. get state() {
  1117. return this._state;
  1118. }
  1119. set state(value) {
  1120. if (this._state != value) {
  1121. this._state = value;
  1122. this._setStateChanged();
  1123. }
  1124. }
  1125. changeState() {
  1126. this._stateChanged = false;
  1127. this.runCallLater(this.changeClips);
  1128. var index = this._state < this._stateNum ? this._state : this._stateNum - 1;
  1129. this._sources && (this._bitmap.source = this._sources[index]);
  1130. if (this.label) {
  1131. this._text.color = this._labelColors[index];
  1132. if (this._strokeColors)
  1133. this._text.strokeColor = this._strokeColors[index];
  1134. }
  1135. }
  1136. get labelColors() {
  1137. return this._labelColors.join(",");
  1138. }
  1139. set labelColors(value) {
  1140. this._labelColors = UIUtils.fillArray(Styles.buttonLabelColors, value, String);
  1141. this._setStateChanged();
  1142. }
  1143. get strokeColors() {
  1144. return this._strokeColors ? this._strokeColors.join(",") : "";
  1145. }
  1146. set strokeColors(value) {
  1147. this._strokeColors = UIUtils.fillArray(Styles.buttonLabelColors, value, String);
  1148. this._setStateChanged();
  1149. }
  1150. get labelPadding() {
  1151. this.createText();
  1152. return this._text.padding.join(",");
  1153. }
  1154. set labelPadding(value) {
  1155. this.createText();
  1156. this._text.padding = UIUtils.fillArray(Styles.labelPadding, value, Number);
  1157. }
  1158. get labelSize() {
  1159. this.createText();
  1160. return this._text.fontSize;
  1161. }
  1162. set labelSize(value) {
  1163. this.createText();
  1164. this._text.fontSize = value;
  1165. }
  1166. get labelStroke() {
  1167. this.createText();
  1168. return this._text.stroke;
  1169. }
  1170. set labelStroke(value) {
  1171. this.createText();
  1172. this._text.stroke = value;
  1173. }
  1174. get labelStrokeColor() {
  1175. this.createText();
  1176. return this._text.strokeColor;
  1177. }
  1178. set labelStrokeColor(value) {
  1179. this.createText();
  1180. this._text.strokeColor = value;
  1181. }
  1182. get labelBold() {
  1183. this.createText();
  1184. return this._text.bold;
  1185. }
  1186. set labelBold(value) {
  1187. this.createText();
  1188. this._text.bold = value;
  1189. }
  1190. get labelFont() {
  1191. this.createText();
  1192. return this._text.font;
  1193. }
  1194. set labelFont(value) {
  1195. this.createText();
  1196. this._text.font = value;
  1197. }
  1198. get labelAlign() {
  1199. this.createText();
  1200. return this._text.align;
  1201. }
  1202. set labelAlign(value) {
  1203. this.createText();
  1204. this._text.align = value;
  1205. }
  1206. get clickHandler() {
  1207. return this._clickHandler;
  1208. }
  1209. set clickHandler(value) {
  1210. this._clickHandler = value;
  1211. }
  1212. get text() {
  1213. this.createText();
  1214. return this._text;
  1215. }
  1216. get sizeGrid() {
  1217. if (this._bitmap.sizeGrid)
  1218. return this._bitmap.sizeGrid.join(",");
  1219. return null;
  1220. }
  1221. set sizeGrid(value) {
  1222. this._bitmap.sizeGrid = UIUtils.fillArray(Styles.defaultSizeGrid, value, Number);
  1223. }
  1224. set width(value) {
  1225. super.set_width(value);
  1226. if (this._autoSize) {
  1227. this._bitmap.width = value;
  1228. this._text && (this._text.width = value);
  1229. }
  1230. }
  1231. get width() {
  1232. return super.get_width();
  1233. }
  1234. set height(value) {
  1235. super.set_height(value);
  1236. if (this._autoSize) {
  1237. this._bitmap.height = value;
  1238. this._text && (this._text.height = value);
  1239. }
  1240. }
  1241. get height() {
  1242. return super.get_height();
  1243. }
  1244. set dataSource(value) {
  1245. this._dataSource = value;
  1246. if (typeof (value) == 'number' || typeof (value) == 'string')
  1247. this.label = value + "";
  1248. else
  1249. super.set_dataSource(value);
  1250. }
  1251. get dataSource() {
  1252. return super.get_dataSource();
  1253. }
  1254. get iconOffset() {
  1255. return this._bitmap._offset ? this._bitmap._offset.join(",") : null;
  1256. }
  1257. set iconOffset(value) {
  1258. if (value)
  1259. this._bitmap._offset = UIUtils.fillArray([1, 1], value, Number);
  1260. else
  1261. this._bitmap._offset = [];
  1262. }
  1263. _setStateChanged() {
  1264. if (!this._stateChanged) {
  1265. this._stateChanged = true;
  1266. this.callLater(this.changeState);
  1267. }
  1268. }
  1269. }
  1270. Button.stateMap = { "mouseup": 0, "mouseover": 1, "mousedown": 2, "mouseout": 0 };
  1271. Laya.ILaya.regClass(Button);
  1272. Laya.ClassUtils.regClass("laya.ui.Button", Button);
  1273. Laya.ClassUtils.regClass("Laya.Button", Button);
  1274. class CheckBox extends Button {
  1275. constructor(skin = null, label = "") {
  1276. super(skin, label);
  1277. this.toggle = true;
  1278. this._autoSize = false;
  1279. }
  1280. preinitialize() {
  1281. super.preinitialize();
  1282. this.toggle = true;
  1283. this._autoSize = false;
  1284. }
  1285. initialize() {
  1286. super.initialize();
  1287. this.createText();
  1288. this._text.align = "left";
  1289. this._text.valign = "top";
  1290. this._text.width = 0;
  1291. }
  1292. set dataSource(value) {
  1293. this._dataSource = value;
  1294. if (value instanceof Boolean)
  1295. this.selected = value;
  1296. else if (typeof (value) == 'string')
  1297. this.selected = value === "true";
  1298. else
  1299. super.dataSource = value;
  1300. }
  1301. get dataSource() {
  1302. return super.dataSource;
  1303. }
  1304. }
  1305. Laya.ILaya.regClass(CheckBox);
  1306. Laya.ClassUtils.regClass("laya.ui.CheckBox", CheckBox);
  1307. Laya.ClassUtils.regClass("Laya.CheckBox", CheckBox);
  1308. class Clip extends UIComponent {
  1309. constructor(url = null, clipX = 1, clipY = 1) {
  1310. super();
  1311. this._clipX = 1;
  1312. this._clipY = 1;
  1313. this._clipWidth = 0;
  1314. this._clipHeight = 0;
  1315. this._interval = 50;
  1316. this._index = 0;
  1317. this._toIndex = -1;
  1318. this._clipX = clipX;
  1319. this._clipY = clipY;
  1320. this.skin = url;
  1321. }
  1322. destroy(destroyChild = true) {
  1323. super.destroy(true);
  1324. this._bitmap && this._bitmap.destroy();
  1325. this._bitmap = null;
  1326. this._sources = null;
  1327. }
  1328. dispose() {
  1329. this.destroy(true);
  1330. Laya.ILaya.loader.clearRes(this._skin);
  1331. }
  1332. createChildren() {
  1333. this.graphics = this._bitmap = new AutoBitmap();
  1334. }
  1335. _onDisplay(e) {
  1336. if (this._isPlaying) {
  1337. if (this._getBit(Laya.Const.DISPLAYED_INSTAGE))
  1338. this.play();
  1339. else
  1340. this.stop();
  1341. }
  1342. else if (this._autoPlay) {
  1343. this.play();
  1344. }
  1345. }
  1346. get skin() {
  1347. return this._skin;
  1348. }
  1349. set skin(value) {
  1350. if (this._skin != value) {
  1351. this._skin = value;
  1352. if (value) {
  1353. if (!Laya.Loader.getRes(value)) {
  1354. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  1355. }
  1356. else {
  1357. this._skinLoaded();
  1358. }
  1359. }
  1360. else {
  1361. this._bitmap.source = null;
  1362. }
  1363. }
  1364. }
  1365. _skinLoaded() {
  1366. this._setClipChanged();
  1367. this._sizeChanged();
  1368. this.event(Laya.Event.LOADED);
  1369. }
  1370. get clipX() {
  1371. return this._clipX;
  1372. }
  1373. set clipX(value) {
  1374. this._clipX = value || 1;
  1375. this._setClipChanged();
  1376. }
  1377. get clipY() {
  1378. return this._clipY;
  1379. }
  1380. set clipY(value) {
  1381. this._clipY = value || 1;
  1382. this._setClipChanged();
  1383. }
  1384. get clipWidth() {
  1385. return this._clipWidth;
  1386. }
  1387. set clipWidth(value) {
  1388. this._clipWidth = value;
  1389. this._setClipChanged();
  1390. }
  1391. get clipHeight() {
  1392. return this._clipHeight;
  1393. }
  1394. set clipHeight(value) {
  1395. this._clipHeight = value;
  1396. this._setClipChanged();
  1397. }
  1398. changeClip() {
  1399. this._clipChanged = false;
  1400. if (!this._skin)
  1401. return;
  1402. var img = Laya.Loader.getRes(this._skin);
  1403. if (img) {
  1404. this.loadComplete(this._skin, img);
  1405. }
  1406. else {
  1407. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this.loadComplete, [this._skin]));
  1408. }
  1409. }
  1410. loadComplete(url, img) {
  1411. if (url === this._skin && img) {
  1412. var w = this._clipWidth || Math.ceil(img.sourceWidth / this._clipX);
  1413. var h = this._clipHeight || Math.ceil(img.sourceHeight / this._clipY);
  1414. var key = this._skin + w + h;
  1415. var clips = Laya.WeakObject.I.get(key);
  1416. if (!Laya.Utils.isOkTextureList(clips)) {
  1417. clips = null;
  1418. }
  1419. if (clips)
  1420. this._sources = clips;
  1421. else {
  1422. this._sources = [];
  1423. for (var i = 0; i < this._clipY; i++) {
  1424. for (var j = 0; j < this._clipX; j++) {
  1425. this._sources.push(Laya.Texture.createFromTexture(img, w * j, h * i, w, h));
  1426. }
  1427. }
  1428. Laya.WeakObject.I.set(key, this._sources);
  1429. }
  1430. this.index = this._index;
  1431. this.event(Laya.Event.LOADED);
  1432. this.onCompResize();
  1433. }
  1434. }
  1435. get sources() {
  1436. return this._sources;
  1437. }
  1438. set sources(value) {
  1439. this._sources = value;
  1440. this.index = this._index;
  1441. this.event(Laya.Event.LOADED);
  1442. }
  1443. get group() {
  1444. return this._group;
  1445. }
  1446. set group(value) {
  1447. if (value && this._skin)
  1448. Laya.Loader.setGroup(this._skin, value);
  1449. this._group = value;
  1450. }
  1451. set width(value) {
  1452. super.width = value;
  1453. this._bitmap.width = value;
  1454. }
  1455. get width() {
  1456. return super.width;
  1457. }
  1458. set height(value) {
  1459. super.height = value;
  1460. this._bitmap.height = value;
  1461. }
  1462. get height() {
  1463. return super.height;
  1464. }
  1465. measureWidth() {
  1466. this.runCallLater(this.changeClip);
  1467. return this._bitmap.width;
  1468. }
  1469. measureHeight() {
  1470. this.runCallLater(this.changeClip);
  1471. return this._bitmap.height;
  1472. }
  1473. get sizeGrid() {
  1474. if (this._bitmap.sizeGrid)
  1475. return this._bitmap.sizeGrid.join(",");
  1476. return null;
  1477. }
  1478. set sizeGrid(value) {
  1479. this._bitmap.sizeGrid = UIUtils.fillArray(Styles.defaultSizeGrid, value, Number);
  1480. }
  1481. get index() {
  1482. return this._index;
  1483. }
  1484. set index(value) {
  1485. this._index = value;
  1486. this._bitmap && this._sources && (this._bitmap.source = this._sources[value]);
  1487. this.event(Laya.Event.CHANGE);
  1488. }
  1489. get total() {
  1490. this.runCallLater(this.changeClip);
  1491. return this._sources ? this._sources.length : 0;
  1492. }
  1493. get autoPlay() {
  1494. return this._autoPlay;
  1495. }
  1496. set autoPlay(value) {
  1497. if (this._autoPlay != value) {
  1498. this._autoPlay = value;
  1499. value ? this.play() : this.stop();
  1500. }
  1501. }
  1502. get interval() {
  1503. return this._interval;
  1504. }
  1505. set interval(value) {
  1506. if (this._interval != value) {
  1507. this._interval = value;
  1508. if (this._isPlaying)
  1509. this.play();
  1510. }
  1511. }
  1512. get isPlaying() {
  1513. return this._isPlaying;
  1514. }
  1515. set isPlaying(value) {
  1516. this._isPlaying = value;
  1517. }
  1518. play(from = 0, to = -1) {
  1519. this._isPlaying = true;
  1520. this.index = from;
  1521. this._toIndex = to;
  1522. this._index++;
  1523. Laya.ILaya.timer.loop(this.interval, this, this._loop);
  1524. this.on(Laya.Event.DISPLAY, this, this._onDisplay);
  1525. this.on(Laya.Event.UNDISPLAY, this, this._onDisplay);
  1526. }
  1527. _loop() {
  1528. if (this._visible && this._sources) {
  1529. this._index++;
  1530. if (this._toIndex > -1 && this._index >= this._toIndex)
  1531. this.stop();
  1532. else if (this._index >= this._sources.length)
  1533. this._index = 0;
  1534. this.index = this._index;
  1535. }
  1536. }
  1537. stop() {
  1538. this._isPlaying = false;
  1539. Laya.ILaya.timer.clear(this, this._loop);
  1540. this.event(Laya.Event.COMPLETE);
  1541. }
  1542. set dataSource(value) {
  1543. this._dataSource = value;
  1544. if (typeof (value) == 'number' || typeof (value) == 'string')
  1545. this.index = parseInt(value);
  1546. else
  1547. super.dataSource = value;
  1548. }
  1549. get dataSource() {
  1550. return super.dataSource;
  1551. }
  1552. get bitmap() {
  1553. return this._bitmap;
  1554. }
  1555. _setClipChanged() {
  1556. if (!this._clipChanged) {
  1557. this._clipChanged = true;
  1558. this.callLater(this.changeClip);
  1559. }
  1560. }
  1561. }
  1562. Laya.ILaya.regClass(Clip);
  1563. Laya.ClassUtils.regClass("laya.ui.Clip", Clip);
  1564. Laya.ClassUtils.regClass("Laya.Clip", Clip);
  1565. class ColorPicker extends UIComponent {
  1566. constructor(createChildren = true) {
  1567. super(false);
  1568. this._gridSize = 11;
  1569. this._bgColor = "#ffffff";
  1570. this._borderColor = "#000000";
  1571. this._inputColor = "#000000";
  1572. this._inputBgColor = "#efefef";
  1573. this._colors = [];
  1574. this._selectedColor = "#000000";
  1575. if (createChildren) {
  1576. this.preinitialize();
  1577. this.createChildren();
  1578. this.initialize();
  1579. }
  1580. }
  1581. destroy(destroyChild = true) {
  1582. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this.removeColorBox);
  1583. super.destroy(destroyChild);
  1584. this._colorPanel && this._colorPanel.destroy(destroyChild);
  1585. this._colorButton && this._colorButton.destroy(destroyChild);
  1586. this._colorPanel = null;
  1587. this._colorTiles = null;
  1588. this._colorBlock = null;
  1589. this._colorInput = null;
  1590. this._colorButton = null;
  1591. this._colors = null;
  1592. this.changeHandler = null;
  1593. }
  1594. createChildren() {
  1595. this.addChild(this._colorButton = new Button());
  1596. this._colorPanel = new Box();
  1597. this._colorPanel.size(230, 166);
  1598. this._colorPanel.addChild(this._colorTiles = new Laya.Sprite());
  1599. this._colorPanel.addChild(this._colorBlock = new Laya.Sprite());
  1600. this._colorPanel.addChild(this._colorInput = new Laya.Input());
  1601. }
  1602. initialize() {
  1603. this._colorButton.on(Laya.Event.CLICK, this, this.onColorButtonClick);
  1604. this._colorBlock.pos(5, 5);
  1605. this._colorInput.pos(60, 5);
  1606. this._colorInput.size(60, 20);
  1607. this._colorInput.on(Laya.Event.CHANGE, this, this.onColorInputChange);
  1608. this._colorInput.on(Laya.Event.KEY_DOWN, this, this.onColorFieldKeyDown);
  1609. this._colorTiles.pos(5, 30);
  1610. this._colorTiles.on(Laya.Event.MOUSE_MOVE, this, this.onColorTilesMouseMove);
  1611. this._colorTiles.on(Laya.Event.CLICK, this, this.onColorTilesClick);
  1612. this._colorTiles.size(20 * this._gridSize, 12 * this._gridSize);
  1613. this._colorPanel.on(Laya.Event.MOUSE_DOWN, this, this.onPanelMouseDown);
  1614. this.bgColor = this._bgColor;
  1615. }
  1616. onPanelMouseDown(e) {
  1617. e.stopPropagation();
  1618. }
  1619. changePanel() {
  1620. this._panelChanged = false;
  1621. var g = this._colorPanel.graphics;
  1622. g.clear(true);
  1623. g.drawRect(0, 0, 230, 166, this._bgColor, this._borderColor);
  1624. this.drawBlock(this._selectedColor);
  1625. this._colorInput.borderColor = this._borderColor;
  1626. this._colorInput.bgColor = this._inputBgColor;
  1627. this._colorInput.color = this._inputColor;
  1628. g = this._colorTiles.graphics;
  1629. g.clear(true);
  1630. var mainColors = [0x000000, 0x333333, 0x666666, 0x999999, 0xCCCCCC, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF];
  1631. for (var i = 0; i < 12; i++) {
  1632. for (var j = 0; j < 20; j++) {
  1633. var color;
  1634. if (j === 0)
  1635. color = mainColors[i];
  1636. else if (j === 1)
  1637. color = 0x000000;
  1638. else
  1639. color = (((i * 3 + j / 6) % 3 << 0) + ((i / 6) << 0) * 3) * 0x33 << 16 | j % 6 * 0x33 << 8 | (i << 0) % 6 * 0x33;
  1640. var strColor = UIUtils.toColor(color);
  1641. this._colors.push(strColor);
  1642. var x = j * this._gridSize;
  1643. var y = i * this._gridSize;
  1644. g.drawRect(x, y, this._gridSize, this._gridSize, strColor, "#000000");
  1645. }
  1646. }
  1647. }
  1648. onColorButtonClick(e) {
  1649. if (this._colorPanel.parent)
  1650. this.close();
  1651. else
  1652. this.open();
  1653. }
  1654. open() {
  1655. let stage = Laya.ILaya.stage;
  1656. var p = this.localToGlobal(new Laya.Point());
  1657. var px = p.x + this._colorPanel.width <= stage.width ? p.x : stage.width - this._colorPanel.width;
  1658. var py = p.y + this._colorButton.height;
  1659. py = py + this._colorPanel.height <= stage.height ? py : p.y - this._colorPanel.height;
  1660. this._colorPanel.pos(px, py);
  1661. this._colorPanel.zOrder = 1001;
  1662. stage.addChild(this._colorPanel);
  1663. stage.on(Laya.Event.MOUSE_DOWN, this, this.removeColorBox);
  1664. }
  1665. close() {
  1666. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this.removeColorBox);
  1667. this._colorPanel.removeSelf();
  1668. }
  1669. removeColorBox(e = null) {
  1670. this.close();
  1671. }
  1672. onColorFieldKeyDown(e) {
  1673. if (e.keyCode == 13) {
  1674. if (this._colorInput.text)
  1675. this.selectedColor = this._colorInput.text;
  1676. else
  1677. this.selectedColor = null;
  1678. this.close();
  1679. e.stopPropagation();
  1680. }
  1681. }
  1682. onColorInputChange(e = null) {
  1683. if (this._colorInput.text)
  1684. this.drawBlock(this._colorInput.text);
  1685. else
  1686. this.drawBlock("#FFFFFF");
  1687. }
  1688. onColorTilesClick(e) {
  1689. this.selectedColor = this.getColorByMouse();
  1690. this.close();
  1691. }
  1692. onColorTilesMouseMove(e) {
  1693. this._colorInput.focus = false;
  1694. var color = this.getColorByMouse();
  1695. this._colorInput.text = color;
  1696. this.drawBlock(color);
  1697. }
  1698. getColorByMouse() {
  1699. var point = this._colorTiles.getMousePoint();
  1700. var x = Math.floor(point.x / this._gridSize);
  1701. var y = Math.floor(point.y / this._gridSize);
  1702. return this._colors[y * 20 + x];
  1703. }
  1704. drawBlock(color) {
  1705. var g = this._colorBlock.graphics;
  1706. g.clear(true);
  1707. var showColor = color ? color : "#ffffff";
  1708. g.drawRect(0, 0, 50, 20, showColor, this._borderColor);
  1709. color || g.drawLine(0, 0, 50, 20, "#ff0000");
  1710. }
  1711. get selectedColor() {
  1712. return this._selectedColor;
  1713. }
  1714. set selectedColor(value) {
  1715. if (this._selectedColor != value) {
  1716. this._selectedColor = this._colorInput.text = value;
  1717. this.drawBlock(value);
  1718. this.changeColor();
  1719. this.changeHandler && this.changeHandler.runWith(this._selectedColor);
  1720. this.event(Laya.Event.CHANGE, Laya.Event.EMPTY.setTo(Laya.Event.CHANGE, this, this));
  1721. }
  1722. }
  1723. get skin() {
  1724. return this._colorButton.skin;
  1725. }
  1726. set skin(value) {
  1727. this._colorButton.once(Laya.Event.LOADED, this, this.changeColor);
  1728. this._colorButton.skin = value;
  1729. }
  1730. changeColor() {
  1731. var g = this.graphics;
  1732. g.clear(true);
  1733. var showColor = this._selectedColor || "#000000";
  1734. g.drawRect(0, 0, this._colorButton.width, this._colorButton.height, showColor);
  1735. }
  1736. get bgColor() {
  1737. return this._bgColor;
  1738. }
  1739. set bgColor(value) {
  1740. this._bgColor = value;
  1741. this._setPanelChanged();
  1742. }
  1743. get borderColor() {
  1744. return this._borderColor;
  1745. }
  1746. set borderColor(value) {
  1747. this._borderColor = value;
  1748. this._setPanelChanged();
  1749. }
  1750. get inputColor() {
  1751. return this._inputColor;
  1752. }
  1753. set inputColor(value) {
  1754. this._inputColor = value;
  1755. this._setPanelChanged();
  1756. }
  1757. get inputBgColor() {
  1758. return this._inputBgColor;
  1759. }
  1760. set inputBgColor(value) {
  1761. this._inputBgColor = value;
  1762. this._setPanelChanged();
  1763. }
  1764. _setPanelChanged() {
  1765. if (!this._panelChanged) {
  1766. this._panelChanged = true;
  1767. this.callLater(this.changePanel);
  1768. }
  1769. }
  1770. }
  1771. Laya.ILaya.regClass(ColorPicker);
  1772. Laya.ClassUtils.regClass("laya.ui.ColorPicker", ColorPicker);
  1773. Laya.ClassUtils.regClass("Laya.ColorPicker", ColorPicker);
  1774. class Label extends UIComponent {
  1775. constructor(text = "") {
  1776. super();
  1777. this.text = text;
  1778. }
  1779. destroy(destroyChild = true) {
  1780. super.destroy(destroyChild);
  1781. this._tf = null;
  1782. }
  1783. createChildren() {
  1784. this.addChild(this._tf = new Laya.Text());
  1785. }
  1786. get text() {
  1787. return this._tf.text;
  1788. }
  1789. set text(value) {
  1790. if (this._tf.text != value) {
  1791. if (value)
  1792. value = UIUtils.adptString(value + "");
  1793. this._tf.text = value;
  1794. this.event(Laya.Event.CHANGE);
  1795. if (!this._width || !this._height)
  1796. this.onCompResize();
  1797. }
  1798. }
  1799. changeText(text) {
  1800. this._tf.changeText(text);
  1801. }
  1802. get wordWrap() {
  1803. return this._tf.wordWrap;
  1804. }
  1805. set wordWrap(value) {
  1806. this._tf.wordWrap = value;
  1807. }
  1808. get color() {
  1809. return this._tf.color;
  1810. }
  1811. set color(value) {
  1812. this._tf.color = value;
  1813. }
  1814. get font() {
  1815. return this._tf.font;
  1816. }
  1817. set font(value) {
  1818. this._tf.font = value;
  1819. }
  1820. get align() {
  1821. return this._tf.align;
  1822. }
  1823. set align(value) {
  1824. this._tf.align = value;
  1825. }
  1826. get valign() {
  1827. return this._tf.valign;
  1828. }
  1829. set valign(value) {
  1830. this._tf.valign = value;
  1831. }
  1832. get bold() {
  1833. return this._tf.bold;
  1834. }
  1835. set bold(value) {
  1836. this._tf.bold = value;
  1837. }
  1838. get italic() {
  1839. return this._tf.italic;
  1840. }
  1841. set italic(value) {
  1842. this._tf.italic = value;
  1843. }
  1844. get leading() {
  1845. return this._tf.leading;
  1846. }
  1847. set leading(value) {
  1848. this._tf.leading = value;
  1849. }
  1850. get fontSize() {
  1851. return this._tf.fontSize;
  1852. }
  1853. set fontSize(value) {
  1854. this._tf.fontSize = value;
  1855. }
  1856. get padding() {
  1857. return this._tf.padding.join(",");
  1858. }
  1859. set padding(value) {
  1860. this._tf.padding = UIUtils.fillArray(Styles.labelPadding, value, Number);
  1861. }
  1862. get bgColor() {
  1863. return this._tf.bgColor;
  1864. }
  1865. set bgColor(value) {
  1866. this._tf.bgColor = value;
  1867. }
  1868. get borderColor() {
  1869. return this._tf.borderColor;
  1870. }
  1871. set borderColor(value) {
  1872. this._tf.borderColor = value;
  1873. }
  1874. get stroke() {
  1875. return this._tf.stroke;
  1876. }
  1877. set stroke(value) {
  1878. this._tf.stroke = value;
  1879. }
  1880. get strokeColor() {
  1881. return this._tf.strokeColor;
  1882. }
  1883. set strokeColor(value) {
  1884. this._tf.strokeColor = value;
  1885. }
  1886. get textField() {
  1887. return this._tf;
  1888. }
  1889. measureWidth() {
  1890. return this._tf.width;
  1891. }
  1892. measureHeight() {
  1893. return this._tf.height;
  1894. }
  1895. get width() {
  1896. if (this._width || this._tf.text)
  1897. return super.width;
  1898. return 0;
  1899. }
  1900. set width(value) {
  1901. super.width = value;
  1902. this._tf.width = value;
  1903. }
  1904. get height() {
  1905. if (this._height || this._tf.text)
  1906. return super.height;
  1907. return 0;
  1908. }
  1909. set height(value) {
  1910. super.height = value;
  1911. this._tf.height = value;
  1912. }
  1913. set dataSource(value) {
  1914. this._dataSource = value;
  1915. if (typeof (value) == 'number' || typeof (value) == 'string')
  1916. this.text = value + "";
  1917. else
  1918. super.dataSource = value;
  1919. }
  1920. get dataSource() {
  1921. return super.dataSource;
  1922. }
  1923. get overflow() {
  1924. return this._tf.overflow;
  1925. }
  1926. set overflow(value) {
  1927. this._tf.overflow = value;
  1928. }
  1929. get underline() {
  1930. return this._tf.underline;
  1931. }
  1932. set underline(value) {
  1933. this._tf.underline = value;
  1934. }
  1935. get underlineColor() {
  1936. return this._tf.underlineColor;
  1937. }
  1938. set underlineColor(value) {
  1939. this._tf.underlineColor = value;
  1940. }
  1941. }
  1942. Laya.ILaya.regClass(Label);
  1943. Laya.ClassUtils.regClass("laya.ui.Label", Label);
  1944. Laya.ClassUtils.regClass("Laya.Label", Label);
  1945. class Slider extends UIComponent {
  1946. constructor(skin = null) {
  1947. super();
  1948. this.isVertical = true;
  1949. this.showLabel = true;
  1950. this._max = 100;
  1951. this._min = 0;
  1952. this._tick = 1;
  1953. this._value = 0;
  1954. if (!Slider.label) {
  1955. Slider.label = new Label();
  1956. }
  1957. this.skin = skin;
  1958. }
  1959. destroy(destroyChild = true) {
  1960. super.destroy(destroyChild);
  1961. this._bg && this._bg.destroy(destroyChild);
  1962. this._bar && this._bar.destroy(destroyChild);
  1963. this._progress && this._progress.destroy(destroyChild);
  1964. this._bg = null;
  1965. this._bar = null;
  1966. this._progress = null;
  1967. this.changeHandler = null;
  1968. }
  1969. createChildren() {
  1970. this.addChild(this._bg = new Image());
  1971. this.addChild(this._bar = new Button());
  1972. }
  1973. initialize() {
  1974. this._bar.on(Laya.Event.MOUSE_DOWN, this, this.onBarMouseDown);
  1975. this._bg.sizeGrid = this._bar.sizeGrid = "4,4,4,4,0";
  1976. if (this._progress)
  1977. this._progress.sizeGrid = this._bar.sizeGrid;
  1978. this.allowClickBack = true;
  1979. }
  1980. onBarMouseDown(e) {
  1981. var Laya$1 = Laya.ILaya;
  1982. this._globalSacle || (this._globalSacle = new Laya.Point());
  1983. this._globalSacle.setTo(this.globalScaleX || 0.01, this.globalScaleY || 0.01);
  1984. this._maxMove = this.isVertical ? (this.height - this._bar.height) : (this.width - this._bar.width);
  1985. this._tx = Laya$1.stage.mouseX;
  1986. this._ty = Laya$1.stage.mouseY;
  1987. Laya$1.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
  1988. Laya$1.stage.once(Laya.Event.MOUSE_UP, this, this.mouseUp);
  1989. Laya$1.stage.once(Laya.Event.MOUSE_OUT, this, this.mouseUp);
  1990. this.showValueText();
  1991. }
  1992. showValueText() {
  1993. if (this.showLabel) {
  1994. var label = Slider.label;
  1995. this.addChild(label);
  1996. label.textField.changeText(this._value + "");
  1997. if (this.isVertical) {
  1998. label.x = this._bar._x + 20;
  1999. label.y = (this._bar.height - label.height) * 0.5 + this._bar._y;
  2000. }
  2001. else {
  2002. label.y = this._bar._y - 20;
  2003. label.x = (this._bar.width - label.width) * 0.5 + this._bar._x;
  2004. }
  2005. }
  2006. }
  2007. hideValueText() {
  2008. Slider.label && Slider.label.removeSelf();
  2009. }
  2010. mouseUp(e) {
  2011. let stage = Laya.ILaya.stage;
  2012. stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
  2013. stage.off(Laya.Event.MOUSE_UP, this, this.mouseUp);
  2014. stage.off(Laya.Event.MOUSE_OUT, this, this.mouseUp);
  2015. this.sendChangeEvent(Laya.Event.CHANGED);
  2016. this.hideValueText();
  2017. }
  2018. mouseMove(e) {
  2019. let stage = Laya.ILaya.stage;
  2020. var oldValue = this._value;
  2021. if (this.isVertical) {
  2022. this._bar.y += (stage.mouseY - this._ty) / this._globalSacle.y;
  2023. if (this._bar._y > this._maxMove)
  2024. this._bar.y = this._maxMove;
  2025. else if (this._bar._y < 0)
  2026. this._bar.y = 0;
  2027. this._value = this._bar._y / this._maxMove * (this._max - this._min) + this._min;
  2028. if (this._progress)
  2029. this._progress.height = this._bar._y + 0.5 * this._bar.height;
  2030. }
  2031. else {
  2032. this._bar.x += (stage.mouseX - this._tx) / this._globalSacle.x;
  2033. if (this._bar._x > this._maxMove)
  2034. this._bar.x = this._maxMove;
  2035. else if (this._bar._x < 0)
  2036. this._bar.x = 0;
  2037. this._value = this._bar._x / this._maxMove * (this._max - this._min) + this._min;
  2038. if (this._progress)
  2039. this._progress.width = this._bar._x + 0.5 * this._bar.width;
  2040. }
  2041. this._tx = stage.mouseX;
  2042. this._ty = stage.mouseY;
  2043. if (this._tick != 0) {
  2044. var pow = Math.pow(10, (this._tick + "").length - 1);
  2045. this._value = Math.round(Math.round(this._value / this._tick) * this._tick * pow) / pow;
  2046. }
  2047. if (this._value != oldValue) {
  2048. this.sendChangeEvent();
  2049. }
  2050. this.showValueText();
  2051. }
  2052. sendChangeEvent(type = Laya.Event.CHANGE) {
  2053. this.event(type);
  2054. this.changeHandler && this.changeHandler.runWith(this._value);
  2055. }
  2056. get skin() {
  2057. return this._skin;
  2058. }
  2059. set skin(value) {
  2060. if (this._skin != value) {
  2061. this._skin = value;
  2062. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  2063. Laya.ILaya.loader.load([this._skin, this._skin.replace(".png", "$bar.png")], Laya.Handler.create(this, this._skinLoaded));
  2064. }
  2065. else {
  2066. this._skinLoaded();
  2067. }
  2068. }
  2069. }
  2070. _skinLoaded() {
  2071. this._bg.skin = this._skin;
  2072. this._bar.skin = this._skin.replace(".png", "$bar.png");
  2073. var progressSkin = this._skin.replace(".png", "$progress.png");
  2074. if (Laya.Loader.getRes(progressSkin)) {
  2075. if (!this._progress) {
  2076. this.addChild(this._progress = new Image());
  2077. this._progress.sizeGrid = this._bar.sizeGrid;
  2078. this.setChildIndex(this._progress, 1);
  2079. }
  2080. this._progress.skin = progressSkin;
  2081. }
  2082. this.setBarPoint();
  2083. this.callLater(this.changeValue);
  2084. this._sizeChanged();
  2085. this.event(Laya.Event.LOADED);
  2086. }
  2087. setBarPoint() {
  2088. if (this.isVertical)
  2089. this._bar.x = Math.round((this._bg.width - this._bar.width) * 0.5);
  2090. else
  2091. this._bar.y = Math.round((this._bg.height - this._bar.height) * 0.5);
  2092. }
  2093. measureWidth() {
  2094. return Math.max(this._bg.width, this._bar.width);
  2095. }
  2096. measureHeight() {
  2097. return Math.max(this._bg.height, this._bar.height);
  2098. }
  2099. _sizeChanged() {
  2100. super._sizeChanged();
  2101. if (this.isVertical)
  2102. this._bg.height = this.height;
  2103. else
  2104. this._bg.width = this.width;
  2105. this.setBarPoint();
  2106. this.changeValue();
  2107. }
  2108. get sizeGrid() {
  2109. return this._bg.sizeGrid;
  2110. }
  2111. set sizeGrid(value) {
  2112. this._bg.sizeGrid = value;
  2113. this._bar.sizeGrid = value;
  2114. if (this._progress)
  2115. this._progress.sizeGrid = this._bar.sizeGrid;
  2116. }
  2117. setSlider(min, max, value) {
  2118. this._value = -1;
  2119. this._min = min;
  2120. this._max = max > min ? max : min;
  2121. this.value = value < min ? min : value > max ? max : value;
  2122. }
  2123. get tick() {
  2124. return this._tick;
  2125. }
  2126. set tick(value) {
  2127. if (this._tick != value) {
  2128. this._tick = value;
  2129. this.callLater(this.changeValue);
  2130. }
  2131. }
  2132. changeValue() {
  2133. if (this.tick != 0) {
  2134. var pow = Math.pow(10, (this._tick + "").length - 1);
  2135. this._value = Math.round(Math.round(this._value / this._tick) * this._tick * pow) / pow;
  2136. }
  2137. this._value = this._value > this._max ? this._max : this._value < this._min ? this._min : this._value;
  2138. var num = this._max - this._min;
  2139. if (num === 0)
  2140. num = 1;
  2141. if (this.isVertical) {
  2142. this._bar.y = (this._value - this._min) / num * (this.height - this._bar.height);
  2143. if (this._progress)
  2144. this._progress.height = this._bar._y + 0.5 * this._bar.height;
  2145. }
  2146. else {
  2147. this._bar.x = (this._value - this._min) / num * (this.width - this._bar.width);
  2148. if (this._progress)
  2149. this._progress.width = this._bar._x + 0.5 * this._bar.width;
  2150. }
  2151. }
  2152. get max() {
  2153. return this._max;
  2154. }
  2155. set max(value) {
  2156. if (this._max != value) {
  2157. this._max = value;
  2158. this.callLater(this.changeValue);
  2159. }
  2160. }
  2161. get min() {
  2162. return this._min;
  2163. }
  2164. set min(value) {
  2165. if (this._min != value) {
  2166. this._min = value;
  2167. this.callLater(this.changeValue);
  2168. }
  2169. }
  2170. get value() {
  2171. return this._value;
  2172. }
  2173. set value(num) {
  2174. if (this._value != num) {
  2175. var oldValue = this._value;
  2176. this._value = num;
  2177. this.changeValue();
  2178. if (this._value != oldValue) {
  2179. this.sendChangeEvent();
  2180. }
  2181. }
  2182. }
  2183. get allowClickBack() {
  2184. return this._allowClickBack;
  2185. }
  2186. set allowClickBack(value) {
  2187. if (this._allowClickBack != value) {
  2188. this._allowClickBack = value;
  2189. if (value)
  2190. this._bg.on(Laya.Event.MOUSE_DOWN, this, this.onBgMouseDown);
  2191. else
  2192. this._bg.off(Laya.Event.MOUSE_DOWN, this, this.onBgMouseDown);
  2193. }
  2194. }
  2195. onBgMouseDown(e) {
  2196. var point = this._bg.getMousePoint();
  2197. if (this.isVertical)
  2198. this.value = point.y / (this.height - this._bar.height) * (this._max - this._min) + this._min;
  2199. else
  2200. this.value = point.x / (this.width - this._bar.width) * (this._max - this._min) + this._min;
  2201. }
  2202. set dataSource(value) {
  2203. this._dataSource = value;
  2204. if (typeof (value) == 'number' || typeof (value) == 'string')
  2205. this.value = Number(value);
  2206. else
  2207. super.dataSource = value;
  2208. }
  2209. get dataSource() {
  2210. return super.dataSource;
  2211. }
  2212. get bar() {
  2213. return this._bar;
  2214. }
  2215. }
  2216. Slider.label = null;
  2217. Laya.ILaya.regClass(Slider);
  2218. Laya.ClassUtils.regClass("laya.ui.Slider", Slider);
  2219. Laya.ClassUtils.regClass("Laya.Slider", Slider);
  2220. class ScrollBar extends UIComponent {
  2221. constructor(skin = null) {
  2222. super();
  2223. this.rollRatio = 0.97;
  2224. this.scaleBar = true;
  2225. this.autoHide = false;
  2226. this.elasticDistance = 0;
  2227. this.elasticBackTime = 500;
  2228. this._showButtons = UIConfig.showButtons;
  2229. this._scrollSize = 1;
  2230. this._thumbPercent = 1;
  2231. this._lastOffset = 0;
  2232. this._checkElastic = false;
  2233. this._isElastic = false;
  2234. this._hide = false;
  2235. this._clickOnly = true;
  2236. this._touchScrollEnable = UIConfig.touchScrollEnable;
  2237. this._mouseWheelEnable = UIConfig.mouseWheelEnable;
  2238. this.skin = skin;
  2239. this.max = 1;
  2240. }
  2241. destroy(destroyChild = true) {
  2242. this.stopScroll();
  2243. this.target = null;
  2244. super.destroy(destroyChild);
  2245. this.upButton && this.upButton.destroy(destroyChild);
  2246. this.downButton && this.downButton.destroy(destroyChild);
  2247. this.slider && this.slider.destroy(destroyChild);
  2248. this.upButton = this.downButton = null;
  2249. this.slider = null;
  2250. this.changeHandler = null;
  2251. this._offsets = null;
  2252. }
  2253. createChildren() {
  2254. this.addChild(this.slider = new Slider());
  2255. this.addChild(this.upButton = new Button());
  2256. this.addChild(this.downButton = new Button());
  2257. }
  2258. initialize() {
  2259. this.slider.showLabel = false;
  2260. this.slider.tick = 0;
  2261. this.slider.on(Laya.Event.CHANGE, this, this.onSliderChange);
  2262. this.slider.setSlider(0, 0, 0);
  2263. this.upButton.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  2264. this.downButton.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  2265. }
  2266. onSliderChange() {
  2267. if (this._value != this.slider.value)
  2268. this.value = this.slider.value;
  2269. }
  2270. onButtonMouseDown(e) {
  2271. var isUp = e.currentTarget === this.upButton;
  2272. this.slide(isUp);
  2273. Laya.ILaya.timer.once(Styles.scrollBarDelayTime, this, this.startLoop, [isUp]);
  2274. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp);
  2275. }
  2276. startLoop(isUp) {
  2277. Laya.ILaya.timer.frameLoop(1, this, this.slide, [isUp]);
  2278. }
  2279. slide(isUp) {
  2280. if (isUp)
  2281. this.value -= this._scrollSize;
  2282. else
  2283. this.value += this._scrollSize;
  2284. }
  2285. onStageMouseUp(e) {
  2286. Laya.ILaya.timer.clear(this, this.startLoop);
  2287. Laya.ILaya.timer.clear(this, this.slide);
  2288. }
  2289. get skin() {
  2290. return this._skin;
  2291. }
  2292. set skin(value) {
  2293. if (value == " ")
  2294. return;
  2295. if (this._skin != value) {
  2296. this._skin = value;
  2297. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  2298. Laya.ILaya.loader.load([this._skin, this._skin.replace(".png", "$up.png"), this._skin.replace(".png", "$down.png"), this._skin.replace(".png", "$bar.png")], Laya.Handler.create(this, this._skinLoaded));
  2299. }
  2300. else {
  2301. this._skinLoaded();
  2302. }
  2303. }
  2304. }
  2305. _skinLoaded() {
  2306. this.slider.skin = this._skin;
  2307. this.callLater(this.changeScrollBar);
  2308. this._sizeChanged();
  2309. this.event(Laya.Event.LOADED);
  2310. }
  2311. changeScrollBar() {
  2312. this.upButton.visible = this._showButtons;
  2313. this.downButton.visible = this._showButtons;
  2314. if (this._showButtons) {
  2315. this.upButton.skin = this._skin.replace(".png", "$up.png");
  2316. this.downButton.skin = this._skin.replace(".png", "$down.png");
  2317. }
  2318. if (this.slider.isVertical)
  2319. this.slider.y = this._showButtons ? this.upButton.height : 0;
  2320. else
  2321. this.slider.x = this._showButtons ? this.upButton.width : 0;
  2322. this.resetPositions();
  2323. this.repaint();
  2324. }
  2325. _sizeChanged() {
  2326. super._sizeChanged();
  2327. this.repaint();
  2328. this.resetPositions();
  2329. this.event(Laya.Event.CHANGE);
  2330. this.changeHandler && this.changeHandler.runWith(this.value);
  2331. }
  2332. resetPositions() {
  2333. if (this.slider.isVertical)
  2334. this.slider.height = this.height - (this._showButtons ? (this.upButton.height + this.downButton.height) : 0);
  2335. else
  2336. this.slider.width = this.width - (this._showButtons ? (this.upButton.width + this.downButton.width) : 0);
  2337. this.resetButtonPosition();
  2338. }
  2339. resetButtonPosition() {
  2340. if (this.slider.isVertical)
  2341. this.downButton.y = this.slider._y + this.slider.height;
  2342. else
  2343. this.downButton.x = this.slider._x + this.slider.width;
  2344. }
  2345. measureWidth() {
  2346. if (this.slider.isVertical)
  2347. return this.slider.width;
  2348. return 100;
  2349. }
  2350. measureHeight() {
  2351. if (this.slider.isVertical)
  2352. return 100;
  2353. return this.slider.height;
  2354. }
  2355. setScroll(min, max, value) {
  2356. this.runCallLater(this._sizeChanged);
  2357. this.slider.setSlider(min, max, value);
  2358. this.slider.bar.visible = max > 0;
  2359. if (!this._hide && this.autoHide)
  2360. this.visible = false;
  2361. }
  2362. get max() {
  2363. return this.slider.max;
  2364. }
  2365. set max(value) {
  2366. this.slider.max = value;
  2367. }
  2368. get min() {
  2369. return this.slider.min;
  2370. }
  2371. set min(value) {
  2372. this.slider.min = value;
  2373. }
  2374. get value() {
  2375. return this._value;
  2376. }
  2377. set value(v) {
  2378. if (v !== this._value) {
  2379. this._value = v;
  2380. if (!this._isElastic) {
  2381. if (this.slider["_value"] != v) {
  2382. this.slider["_value"] = v;
  2383. this.slider.changeValue();
  2384. }
  2385. this._value = this.slider["_value"];
  2386. }
  2387. this.event(Laya.Event.CHANGE);
  2388. this.changeHandler && this.changeHandler.runWith(this._value);
  2389. }
  2390. }
  2391. get isVertical() {
  2392. return this.slider.isVertical;
  2393. }
  2394. set isVertical(value) {
  2395. this.slider.isVertical = value;
  2396. }
  2397. get sizeGrid() {
  2398. return this.slider.sizeGrid;
  2399. }
  2400. set sizeGrid(value) {
  2401. this.slider.sizeGrid = value;
  2402. }
  2403. get scrollSize() {
  2404. return this._scrollSize;
  2405. }
  2406. set scrollSize(value) {
  2407. this._scrollSize = value;
  2408. }
  2409. set dataSource(value) {
  2410. this._dataSource = value;
  2411. if (typeof (value) == 'number' || typeof (value) == 'string')
  2412. this.value = Number(value);
  2413. else
  2414. super.dataSource = value;
  2415. }
  2416. get dataSource() {
  2417. return super.dataSource;
  2418. }
  2419. get thumbPercent() {
  2420. return this._thumbPercent;
  2421. }
  2422. set thumbPercent(value) {
  2423. this.runCallLater(this.changeScrollBar);
  2424. this.runCallLater(this._sizeChanged);
  2425. value = value >= 1 ? 0.99 : value;
  2426. this._thumbPercent = value;
  2427. if (this.scaleBar) {
  2428. if (this.slider.isVertical)
  2429. this.slider.bar.height = Math.max(this.slider.height * value, Styles.scrollBarMinNum);
  2430. else
  2431. this.slider.bar.width = Math.max(this.slider.width * value, Styles.scrollBarMinNum);
  2432. }
  2433. }
  2434. get target() {
  2435. return this._target;
  2436. }
  2437. set target(value) {
  2438. if (this._target) {
  2439. this._target.off(Laya.Event.MOUSE_WHEEL, this, this.onTargetMouseWheel);
  2440. this._target.off(Laya.Event.MOUSE_DOWN, this, this.onTargetMouseDown);
  2441. }
  2442. this._target = value;
  2443. if (value) {
  2444. this._mouseWheelEnable && this._target.on(Laya.Event.MOUSE_WHEEL, this, this.onTargetMouseWheel);
  2445. this._touchScrollEnable && this._target.on(Laya.Event.MOUSE_DOWN, this, this.onTargetMouseDown);
  2446. }
  2447. }
  2448. get hide() {
  2449. return this._hide;
  2450. }
  2451. set hide(value) {
  2452. this._hide = value;
  2453. this.visible = !value;
  2454. }
  2455. get showButtons() {
  2456. return this._showButtons;
  2457. }
  2458. set showButtons(value) {
  2459. this._showButtons = value;
  2460. this.callLater(this.changeScrollBar);
  2461. }
  2462. get touchScrollEnable() {
  2463. return this._touchScrollEnable;
  2464. }
  2465. set touchScrollEnable(value) {
  2466. this._touchScrollEnable = value;
  2467. this.target = this._target;
  2468. }
  2469. get mouseWheelEnable() {
  2470. return this._mouseWheelEnable;
  2471. }
  2472. set mouseWheelEnable(value) {
  2473. this._mouseWheelEnable = value;
  2474. this.target = this._target;
  2475. }
  2476. onTargetMouseWheel(e) {
  2477. this.value -= e.delta * this._scrollSize;
  2478. this.target = this._target;
  2479. }
  2480. onTargetMouseDown(e) {
  2481. if ((this.isLockedFun) && !this.isLockedFun(e))
  2482. return;
  2483. this.event(Laya.Event.END);
  2484. this._clickOnly = true;
  2485. this._lastOffset = 0;
  2486. this._checkElastic = false;
  2487. this._lastPoint || (this._lastPoint = new Laya.Point());
  2488. this._lastPoint.setTo(Laya.ILaya.stage.mouseX, Laya.ILaya.stage.mouseY);
  2489. Laya.ILaya.timer.clear(this, this.tweenMove);
  2490. Laya.Tween.clearTween(this);
  2491. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2492. Laya.ILaya.stage.once(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2493. Laya.ILaya.timer.frameLoop(1, this, this.loop);
  2494. }
  2495. startDragForce() {
  2496. this._clickOnly = true;
  2497. this._lastOffset = 0;
  2498. this._checkElastic = false;
  2499. this._lastPoint || (this._lastPoint = new Laya.Point());
  2500. this._lastPoint.setTo(Laya.ILaya.stage.mouseX, Laya.ILaya.stage.mouseY);
  2501. Laya.ILaya.timer.clear(this, this.tweenMove);
  2502. Laya.Tween.clearTween(this);
  2503. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2504. Laya.ILaya.stage.once(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2505. Laya.ILaya.timer.frameLoop(1, this, this.loop);
  2506. }
  2507. cancelDragOp() {
  2508. Laya.ILaya.stage.off(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2509. Laya.ILaya.stage.off(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2510. Laya.ILaya.timer.clear(this, this.tweenMove);
  2511. Laya.ILaya.timer.clear(this, this.loop);
  2512. this._target.mouseEnabled = true;
  2513. }
  2514. checkTriggers(isTweenMove = false) {
  2515. if (this.value >= 0 && this.value - this._lastOffset <= 0) {
  2516. if ((this.triggerDownDragLimit) && this.triggerDownDragLimit(isTweenMove)) {
  2517. this.cancelDragOp();
  2518. this.value = 0;
  2519. return true;
  2520. }
  2521. }
  2522. if (this.value <= this.max && (this.value - this._lastOffset >= this.max)) {
  2523. if ((this.triggerUpDragLimit) && this.triggerUpDragLimit(isTweenMove)) {
  2524. this.cancelDragOp();
  2525. this.value = this.max;
  2526. return true;
  2527. }
  2528. }
  2529. return false;
  2530. }
  2531. get lastOffset() {
  2532. return this._lastOffset;
  2533. }
  2534. startTweenMoveForce(lastOffset) {
  2535. this._lastOffset = lastOffset;
  2536. Laya.ILaya.timer.frameLoop(1, this, this.tweenMove, [200]);
  2537. }
  2538. loop() {
  2539. var mouseY = Laya.ILaya.stage.mouseY;
  2540. var mouseX = Laya.ILaya.stage.mouseX;
  2541. this._lastOffset = this.isVertical ? (mouseY - this._lastPoint.y) : (mouseX - this._lastPoint.x);
  2542. if (this._clickOnly) {
  2543. if (Math.abs(this._lastOffset * (this.isVertical ? Laya.ILaya.stage._canvasTransform.getScaleY() : Laya.ILaya.stage._canvasTransform.getScaleX())) > 1) {
  2544. this._clickOnly = false;
  2545. if (this.checkTriggers())
  2546. return;
  2547. this._offsets || (this._offsets = []);
  2548. this._offsets.length = 0;
  2549. this._target.mouseEnabled = false;
  2550. if (!this.hide && this.autoHide) {
  2551. this.alpha = 1;
  2552. this.visible = true;
  2553. }
  2554. this.event(Laya.Event.START);
  2555. }
  2556. else
  2557. return;
  2558. }
  2559. else {
  2560. if (this.checkTriggers())
  2561. return;
  2562. }
  2563. this._offsets.push(this._lastOffset);
  2564. this._lastPoint.x = mouseX;
  2565. this._lastPoint.y = mouseY;
  2566. if (this._lastOffset === 0)
  2567. return;
  2568. if (!this._checkElastic) {
  2569. if (this.elasticDistance > 0) {
  2570. if (!this._checkElastic && this._lastOffset != 0) {
  2571. if ((this._lastOffset > 0 && this._value <= this.min) || (this._lastOffset < 0 && this._value >= this.max)) {
  2572. this._isElastic = true;
  2573. this._checkElastic = true;
  2574. }
  2575. else {
  2576. this._isElastic = false;
  2577. }
  2578. }
  2579. }
  2580. else {
  2581. this._checkElastic = true;
  2582. }
  2583. }
  2584. if (this._isElastic) {
  2585. if (this._value <= this.min) {
  2586. if (this._lastOffset > 0) {
  2587. this.value -= this._lastOffset * Math.max(0, (1 - ((this.min - this._value) / this.elasticDistance)));
  2588. }
  2589. else {
  2590. this.value -= this._lastOffset * 0.5;
  2591. if (this._value >= this.min)
  2592. this._checkElastic = false;
  2593. }
  2594. }
  2595. else if (this._value >= this.max) {
  2596. if (this._lastOffset < 0) {
  2597. this.value -= this._lastOffset * Math.max(0, (1 - ((this._value - this.max) / this.elasticDistance)));
  2598. }
  2599. else {
  2600. this.value -= this._lastOffset * 0.5;
  2601. if (this._value <= this.max)
  2602. this._checkElastic = false;
  2603. }
  2604. }
  2605. }
  2606. else {
  2607. this.value -= this._lastOffset;
  2608. }
  2609. }
  2610. onStageMouseUp2(e) {
  2611. Laya.ILaya.stage.off(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2612. Laya.ILaya.stage.off(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2613. Laya.ILaya.timer.clear(this, this.loop);
  2614. if (this._clickOnly) {
  2615. if (this._value >= this.min && this._value <= this.max)
  2616. return;
  2617. }
  2618. this._target.mouseEnabled = true;
  2619. if (this._isElastic) {
  2620. if (this._value < this.min) {
  2621. Laya.Tween.to(this, { value: this.min }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2622. }
  2623. else if (this._value > this.max) {
  2624. Laya.Tween.to(this, { value: this.max }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2625. }
  2626. }
  2627. else {
  2628. if (!this._offsets)
  2629. return;
  2630. if (this._offsets.length < 1) {
  2631. this._offsets[0] = this.isVertical ? Laya.ILaya.stage.mouseY - this._lastPoint.y : Laya.ILaya.stage.mouseX - this._lastPoint.x;
  2632. }
  2633. var offset = 0;
  2634. var n = Math.min(this._offsets.length, 3);
  2635. for (var i = 0; i < n; i++) {
  2636. offset += this._offsets[this._offsets.length - 1 - i];
  2637. }
  2638. this._lastOffset = offset / n;
  2639. offset = Math.abs(this._lastOffset);
  2640. if (offset < 2) {
  2641. this.event(Laya.Event.END);
  2642. return;
  2643. }
  2644. if (offset > 250)
  2645. this._lastOffset = this._lastOffset > 0 ? 250 : -250;
  2646. var dis = Math.round(Math.abs(this.elasticDistance * (this._lastOffset / 150)));
  2647. Laya.ILaya.timer.frameLoop(1, this, this.tweenMove, [dis]);
  2648. }
  2649. }
  2650. elasticOver() {
  2651. this._isElastic = false;
  2652. if (!this.hide && this.autoHide) {
  2653. Laya.Tween.to(this, { alpha: 0 }, 500);
  2654. }
  2655. this.event(Laya.Event.END);
  2656. }
  2657. tweenMove(maxDistance) {
  2658. this._lastOffset *= this.rollRatio;
  2659. if (this.checkTriggers(true)) {
  2660. return;
  2661. }
  2662. var tarSpeed;
  2663. if (maxDistance > 0) {
  2664. if (this._lastOffset > 0 && this.value <= this.min) {
  2665. this._isElastic = true;
  2666. tarSpeed = -(this.min - maxDistance - this.value) * 0.5;
  2667. if (this._lastOffset > tarSpeed)
  2668. this._lastOffset = tarSpeed;
  2669. }
  2670. else if (this._lastOffset < 0 && this.value >= this.max) {
  2671. this._isElastic = true;
  2672. tarSpeed = -(this.max + maxDistance - this.value) * 0.5;
  2673. if (this._lastOffset < tarSpeed)
  2674. this._lastOffset = tarSpeed;
  2675. }
  2676. }
  2677. this.value -= this._lastOffset;
  2678. if (Math.abs(this._lastOffset) < 0.1) {
  2679. Laya.ILaya.timer.clear(this, this.tweenMove);
  2680. if (this._isElastic) {
  2681. if (this._value < this.min) {
  2682. Laya.Tween.to(this, { value: this.min }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2683. }
  2684. else if (this._value > this.max) {
  2685. Laya.Tween.to(this, { value: this.max }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2686. }
  2687. else {
  2688. this.elasticOver();
  2689. }
  2690. return;
  2691. }
  2692. this.event(Laya.Event.END);
  2693. if (!this.hide && this.autoHide) {
  2694. Laya.Tween.to(this, { alpha: 0 }, 500);
  2695. }
  2696. }
  2697. }
  2698. stopScroll() {
  2699. this.onStageMouseUp2(null);
  2700. Laya.ILaya.timer.clear(this, this.tweenMove);
  2701. Laya.Tween.clearTween(this);
  2702. }
  2703. get tick() {
  2704. return this.slider.tick;
  2705. }
  2706. set tick(value) {
  2707. this.slider.tick = value;
  2708. }
  2709. }
  2710. Laya.ILaya.regClass(ScrollBar);
  2711. Laya.ClassUtils.regClass("laya.ui.ScrollBar", ScrollBar);
  2712. Laya.ClassUtils.regClass("Laya.ScrollBar", ScrollBar);
  2713. class VScrollBar extends ScrollBar {
  2714. }
  2715. Laya.ILaya.regClass(VScrollBar);
  2716. Laya.ClassUtils.regClass("laya.ui.VScrollBar", VScrollBar);
  2717. Laya.ClassUtils.regClass("Laya.VScrollBar", VScrollBar);
  2718. class HScrollBar extends ScrollBar {
  2719. initialize() {
  2720. super.initialize();
  2721. this.slider.isVertical = false;
  2722. }
  2723. }
  2724. Laya.ILaya.regClass(HScrollBar);
  2725. Laya.ClassUtils.regClass("laya.ui.HScrollBar", HScrollBar);
  2726. Laya.ClassUtils.regClass("Laya.HScrollBar", HScrollBar);
  2727. class List extends Box {
  2728. constructor() {
  2729. super(...arguments);
  2730. this.selectEnable = false;
  2731. this.totalPage = 0;
  2732. this._$componentType = "List";
  2733. this._repeatX = 0;
  2734. this._repeatY = 0;
  2735. this._repeatX2 = 0;
  2736. this._repeatY2 = 0;
  2737. this._spaceX = 0;
  2738. this._spaceY = 0;
  2739. this._cells = [];
  2740. this._startIndex = 0;
  2741. this._selectedIndex = -1;
  2742. this._page = 0;
  2743. this._isVertical = true;
  2744. this._cellSize = 20;
  2745. this._cellOffset = 0;
  2746. this._createdLine = 0;
  2747. this._offset = new Laya.Point();
  2748. this._usedCache = null;
  2749. this._elasticEnabled = false;
  2750. this._preLen = 0;
  2751. }
  2752. destroy(destroyChild = true) {
  2753. this._content && this._content.destroy(destroyChild);
  2754. this._scrollBar && this._scrollBar.destroy(destroyChild);
  2755. super.destroy(destroyChild);
  2756. this._content = null;
  2757. this._scrollBar = null;
  2758. this._itemRender = null;
  2759. this._cells = null;
  2760. this._array = null;
  2761. this.selectHandler = this.renderHandler = this.mouseHandler = null;
  2762. }
  2763. createChildren() {
  2764. this.addChild(this._content = new Box());
  2765. }
  2766. set cacheAs(value) {
  2767. super.cacheAs = value;
  2768. if (this._scrollBar) {
  2769. this._usedCache = null;
  2770. if (value !== "none")
  2771. this._scrollBar.on(Laya.Event.START, this, this.onScrollStart);
  2772. else
  2773. this._scrollBar.off(Laya.Event.START, this, this.onScrollStart);
  2774. }
  2775. }
  2776. get cacheAs() {
  2777. return super.cacheAs;
  2778. }
  2779. onScrollStart() {
  2780. this._usedCache || (this._usedCache = super.cacheAs);
  2781. super.cacheAs = "none";
  2782. this._scrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  2783. }
  2784. onScrollEnd() {
  2785. super.cacheAs = this._usedCache;
  2786. }
  2787. get content() {
  2788. return this._content;
  2789. }
  2790. get vScrollBarSkin() {
  2791. return this._scrollBar ? this._scrollBar.skin : null;
  2792. }
  2793. set vScrollBarSkin(value) {
  2794. this._removePreScrollBar();
  2795. var scrollBar = new VScrollBar();
  2796. scrollBar.name = "scrollBar";
  2797. scrollBar.right = 0;
  2798. scrollBar.skin = value;
  2799. scrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  2800. this.scrollBar = scrollBar;
  2801. this.addChild(scrollBar);
  2802. this._setCellChanged();
  2803. }
  2804. _removePreScrollBar() {
  2805. var preNode = this.removeChildByName("scrollBar");
  2806. if (preNode)
  2807. preNode.destroy(true);
  2808. }
  2809. get hScrollBarSkin() {
  2810. return this._scrollBar ? this._scrollBar.skin : null;
  2811. }
  2812. set hScrollBarSkin(value) {
  2813. this._removePreScrollBar();
  2814. var scrollBar = new HScrollBar();
  2815. scrollBar.name = "scrollBar";
  2816. scrollBar.bottom = 0;
  2817. scrollBar.skin = value;
  2818. scrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  2819. this.scrollBar = scrollBar;
  2820. this.addChild(scrollBar);
  2821. this._setCellChanged();
  2822. }
  2823. get scrollBar() {
  2824. return this._scrollBar;
  2825. }
  2826. set scrollBar(value) {
  2827. if (this._scrollBar != value) {
  2828. this._scrollBar = value;
  2829. if (value) {
  2830. this._isVertical = this._scrollBar.isVertical;
  2831. this.addChild(this._scrollBar);
  2832. this._scrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange);
  2833. }
  2834. }
  2835. }
  2836. get itemRender() {
  2837. return this._itemRender;
  2838. }
  2839. set itemRender(value) {
  2840. if (this._itemRender != value) {
  2841. this._itemRender = value;
  2842. for (var i = this._cells.length - 1; i > -1; i--) {
  2843. this._cells[i].destroy();
  2844. }
  2845. this._cells.length = 0;
  2846. this._setCellChanged();
  2847. }
  2848. }
  2849. set width(value) {
  2850. if (value != this._width) {
  2851. super.width = value;
  2852. this._setCellChanged();
  2853. }
  2854. }
  2855. get width() {
  2856. return super.width;
  2857. }
  2858. set height(value) {
  2859. if (value != this._height) {
  2860. super.height = value;
  2861. this._setCellChanged();
  2862. }
  2863. }
  2864. get height() {
  2865. return super.height;
  2866. }
  2867. get repeatX() {
  2868. return this._repeatX > 0 ? this._repeatX : this._repeatX2 > 0 ? this._repeatX2 : 1;
  2869. }
  2870. set repeatX(value) {
  2871. this._repeatX = value;
  2872. this._setCellChanged();
  2873. }
  2874. get repeatY() {
  2875. return this._repeatY > 0 ? this._repeatY : this._repeatY2 > 0 ? this._repeatY2 : 1;
  2876. }
  2877. set repeatY(value) {
  2878. this._repeatY = value;
  2879. this._setCellChanged();
  2880. }
  2881. get spaceX() {
  2882. return this._spaceX;
  2883. }
  2884. set spaceX(value) {
  2885. this._spaceX = value;
  2886. this._setCellChanged();
  2887. }
  2888. get spaceY() {
  2889. return this._spaceY;
  2890. }
  2891. set spaceY(value) {
  2892. this._spaceY = value;
  2893. this._setCellChanged();
  2894. }
  2895. changeCells() {
  2896. this._cellChanged = false;
  2897. if (this._itemRender) {
  2898. this.scrollBar = this.getChildByName("scrollBar");
  2899. var cell = this._getOneCell();
  2900. var cellWidth = (cell.width + this._spaceX) || 1;
  2901. var cellHeight = (cell.height + this._spaceY) || 1;
  2902. if (this._width > 0)
  2903. this._repeatX2 = this._isVertical ? Math.round(this._width / cellWidth) : Math.ceil(this._width / cellWidth);
  2904. if (this._height > 0)
  2905. this._repeatY2 = this._isVertical ? Math.ceil(this._height / cellHeight) : Math.round(this._height / cellHeight);
  2906. var listWidth = this._width ? this._width : (cellWidth * this.repeatX - this._spaceX);
  2907. var listHeight = this._height ? this._height : (cellHeight * this.repeatY - this._spaceY);
  2908. this._cellSize = this._isVertical ? cellHeight : cellWidth;
  2909. this._cellOffset = this._isVertical ? (cellHeight * Math.max(this._repeatY2, this._repeatY) - listHeight - this._spaceY) : (cellWidth * Math.max(this._repeatX2, this._repeatX) - listWidth - this._spaceX);
  2910. if (this._isVertical && this.vScrollBarSkin)
  2911. this._scrollBar.height = listHeight;
  2912. else if (!this._isVertical && this.hScrollBarSkin)
  2913. this._scrollBar.width = listWidth;
  2914. this.setContentSize(listWidth, listHeight);
  2915. var numX = this._isVertical ? this.repeatX : this.repeatY;
  2916. var numY = (this._isVertical ? this.repeatY : this.repeatX) + (this._scrollBar ? 1 : 0);
  2917. this._createItems(0, numX, numY);
  2918. this._createdLine = numY;
  2919. if (this._array) {
  2920. this.array = this._array;
  2921. this.runCallLater(this.renderItems);
  2922. }
  2923. }
  2924. }
  2925. _getOneCell() {
  2926. if (this._cells.length === 0) {
  2927. var item = this.createItem();
  2928. this._offset.setTo(item._x, item._y);
  2929. if (this.cacheContent)
  2930. return item;
  2931. this._cells.push(item);
  2932. }
  2933. return this._cells[0];
  2934. }
  2935. _createItems(startY, numX, numY) {
  2936. var box = this._content;
  2937. var cell = this._getOneCell();
  2938. var cellWidth = cell.width + this._spaceX;
  2939. var cellHeight = cell.height + this._spaceY;
  2940. if (this.cacheContent) {
  2941. var cacheBox = new Box();
  2942. cacheBox.cacheAs = "normal";
  2943. cacheBox.pos((this._isVertical ? 0 : startY) * cellWidth, (this._isVertical ? startY : 0) * cellHeight);
  2944. this._content.addChild(cacheBox);
  2945. box = cacheBox;
  2946. }
  2947. else {
  2948. var arr = [];
  2949. for (var i = this._cells.length - 1; i > -1; i--) {
  2950. var item = this._cells[i];
  2951. item.removeSelf();
  2952. arr.push(item);
  2953. }
  2954. this._cells.length = 0;
  2955. }
  2956. for (var k = startY; k < numY; k++) {
  2957. for (var l = 0; l < numX; l++) {
  2958. if (arr && arr.length) {
  2959. cell = arr.pop();
  2960. }
  2961. else {
  2962. cell = this.createItem();
  2963. }
  2964. cell.x = (this._isVertical ? l : k) * cellWidth - box._x;
  2965. cell.y = (this._isVertical ? k : l) * cellHeight - box._y;
  2966. cell.name = "item" + (k * numX + l);
  2967. box.addChild(cell);
  2968. this.addCell(cell);
  2969. }
  2970. }
  2971. }
  2972. createItem() {
  2973. var arr = [];
  2974. if (typeof (this._itemRender) == "function") {
  2975. var box = new this._itemRender();
  2976. }
  2977. else {
  2978. box = Laya.SceneUtils.createComp(this._itemRender, null, null, arr);
  2979. }
  2980. if (arr.length == 0 && box["_watchMap"]) {
  2981. var watchMap = box["_watchMap"];
  2982. for (var name in watchMap) {
  2983. var a = watchMap[name];
  2984. for (var i = 0; i < a.length; i++) {
  2985. var watcher = a[i];
  2986. arr.push(watcher.comp, watcher.prop, watcher.value);
  2987. }
  2988. }
  2989. }
  2990. if (arr.length)
  2991. box["_$bindData"] = arr;
  2992. return box;
  2993. }
  2994. addCell(cell) {
  2995. cell.on(Laya.Event.CLICK, this, this.onCellMouse);
  2996. cell.on(Laya.Event.RIGHT_CLICK, this, this.onCellMouse);
  2997. cell.on(Laya.Event.MOUSE_OVER, this, this.onCellMouse);
  2998. cell.on(Laya.Event.MOUSE_OUT, this, this.onCellMouse);
  2999. cell.on(Laya.Event.MOUSE_DOWN, this, this.onCellMouse);
  3000. cell.on(Laya.Event.MOUSE_UP, this, this.onCellMouse);
  3001. this._cells.push(cell);
  3002. }
  3003. _afterInited() {
  3004. this.initItems();
  3005. }
  3006. initItems() {
  3007. if (!this._itemRender && this.getChildByName("item0") != null) {
  3008. this.repeatX = 1;
  3009. var count;
  3010. count = 0;
  3011. for (var i = 0; i < 10000; i++) {
  3012. var cell = this.getChildByName("item" + i);
  3013. if (cell) {
  3014. this.addCell(cell);
  3015. count++;
  3016. continue;
  3017. }
  3018. break;
  3019. }
  3020. this.repeatY = count;
  3021. }
  3022. }
  3023. setContentSize(width, height) {
  3024. this._content.width = width;
  3025. this._content.height = height;
  3026. if (this._scrollBar || this._offset.x != 0 || this._offset.y != 0) {
  3027. this._content._style.scrollRect || (this._content.scrollRect = Laya.Rectangle.create());
  3028. this._content._style.scrollRect.setTo(-this._offset.x, -this._offset.y, width, height);
  3029. this._content.scrollRect = this._content.scrollRect;
  3030. }
  3031. this.event(Laya.Event.RESIZE);
  3032. }
  3033. onCellMouse(e) {
  3034. if (e.type === Laya.Event.MOUSE_DOWN)
  3035. this._isMoved = false;
  3036. var cell = e.currentTarget;
  3037. var index = this._startIndex + this._cells.indexOf(cell);
  3038. if (index < 0)
  3039. return;
  3040. if (e.type === Laya.Event.CLICK || e.type === Laya.Event.RIGHT_CLICK) {
  3041. if (this.selectEnable && !this._isMoved)
  3042. this.selectedIndex = index;
  3043. else
  3044. this.changeCellState(cell, true, 0);
  3045. }
  3046. else if ((e.type === Laya.Event.MOUSE_OVER || e.type === Laya.Event.MOUSE_OUT) && this._selectedIndex !== index) {
  3047. this.changeCellState(cell, e.type === Laya.Event.MOUSE_OVER, 0);
  3048. }
  3049. this.mouseHandler && this.mouseHandler.runWith([e, index]);
  3050. }
  3051. changeCellState(cell, visible, index) {
  3052. var selectBox = cell.getChildByName("selectBox");
  3053. if (selectBox) {
  3054. this.selectEnable = true;
  3055. selectBox.visible = visible;
  3056. selectBox.index = index;
  3057. }
  3058. }
  3059. _sizeChanged() {
  3060. super._sizeChanged();
  3061. this.setContentSize(this.width, this.height);
  3062. if (this._scrollBar)
  3063. this.callLater(this.onScrollBarChange);
  3064. }
  3065. onScrollBarChange(e = null) {
  3066. this.runCallLater(this.changeCells);
  3067. var scrollValue = this._scrollBar.value;
  3068. var lineX = (this._isVertical ? this.repeatX : this.repeatY);
  3069. var lineY = (this._isVertical ? this.repeatY : this.repeatX);
  3070. var scrollLine = Math.floor(scrollValue / this._cellSize);
  3071. if (!this.cacheContent) {
  3072. var index = scrollLine * lineX;
  3073. var num = 0;
  3074. if (index > this._startIndex) {
  3075. num = index - this._startIndex;
  3076. var down = true;
  3077. var toIndex = this._startIndex + lineX * (lineY + 1);
  3078. this._isMoved = true;
  3079. }
  3080. else if (index < this._startIndex) {
  3081. num = this._startIndex - index;
  3082. down = false;
  3083. toIndex = this._startIndex - 1;
  3084. this._isMoved = true;
  3085. }
  3086. for (var i = 0; i < num; i++) {
  3087. if (down) {
  3088. var cell = this._cells.shift();
  3089. this._cells[this._cells.length] = cell;
  3090. var cellIndex = toIndex + i;
  3091. }
  3092. else {
  3093. cell = this._cells.pop();
  3094. this._cells.unshift(cell);
  3095. cellIndex = toIndex - i;
  3096. }
  3097. var pos = Math.floor(cellIndex / lineX) * this._cellSize;
  3098. this._isVertical ? cell.y = pos : cell.x = pos;
  3099. this.renderItem(cell, cellIndex);
  3100. }
  3101. this._startIndex = index;
  3102. this.changeSelectStatus();
  3103. }
  3104. else {
  3105. num = (lineY + 1);
  3106. if (this._createdLine - scrollLine < num) {
  3107. this._createItems(this._createdLine, lineX, this._createdLine + num);
  3108. this.renderItems(this._createdLine * lineX, 0);
  3109. this._createdLine += num;
  3110. }
  3111. }
  3112. var r = this._content._style.scrollRect;
  3113. if (this._isVertical) {
  3114. r.y = scrollValue - this._offset.y;
  3115. r.x = -this._offset.x;
  3116. }
  3117. else {
  3118. r.y = -this._offset.y;
  3119. r.x = scrollValue - this._offset.x;
  3120. }
  3121. this._content.scrollRect = r;
  3122. }
  3123. posCell(cell, cellIndex) {
  3124. if (!this._scrollBar)
  3125. return;
  3126. var lineX = (this._isVertical ? this.repeatX : this.repeatY);
  3127. var lineY = (this._isVertical ? this.repeatY : this.repeatX);
  3128. var pos = Math.floor(cellIndex / lineX) * this._cellSize;
  3129. this._isVertical ? cell._y = pos : cell.x = pos;
  3130. }
  3131. get selectedIndex() {
  3132. return this._selectedIndex;
  3133. }
  3134. set selectedIndex(value) {
  3135. if (this._selectedIndex != value) {
  3136. this._selectedIndex = value;
  3137. this.changeSelectStatus();
  3138. this.event(Laya.Event.CHANGE);
  3139. this.selectHandler && this.selectHandler.runWith(value);
  3140. this.startIndex = this._startIndex;
  3141. }
  3142. }
  3143. changeSelectStatus() {
  3144. for (var i = 0, n = this._cells.length; i < n; i++) {
  3145. this.changeCellState(this._cells[i], this._selectedIndex === this._startIndex + i, 1);
  3146. }
  3147. }
  3148. get selectedItem() {
  3149. return this._selectedIndex != -1 ? this._array[this._selectedIndex] : null;
  3150. }
  3151. set selectedItem(value) {
  3152. this.selectedIndex = this._array.indexOf(value);
  3153. }
  3154. get selection() {
  3155. return this.getCell(this._selectedIndex);
  3156. }
  3157. set selection(value) {
  3158. this.selectedIndex = this._startIndex + this._cells.indexOf(value);
  3159. }
  3160. get startIndex() {
  3161. return this._startIndex;
  3162. }
  3163. set startIndex(value) {
  3164. this._startIndex = value > 0 ? value : 0;
  3165. this.callLater(this.renderItems);
  3166. }
  3167. renderItems(from = 0, to = 0) {
  3168. for (var i = from, n = to || this._cells.length; i < n; i++) {
  3169. this.renderItem(this._cells[i], this._startIndex + i);
  3170. }
  3171. this.changeSelectStatus();
  3172. }
  3173. renderItem(cell, index) {
  3174. if (this._array && index >= 0 && index < this._array.length) {
  3175. cell.visible = true;
  3176. if (cell["_$bindData"]) {
  3177. cell["_dataSource"] = this._array[index];
  3178. this._bindData(cell, this._array[index]);
  3179. }
  3180. else
  3181. cell.dataSource = this._array[index];
  3182. if (!this.cacheContent) {
  3183. this.posCell(cell, index);
  3184. }
  3185. if (this.hasListener(Laya.Event.RENDER))
  3186. this.event(Laya.Event.RENDER, [cell, index]);
  3187. if (this.renderHandler)
  3188. this.renderHandler.runWith([cell, index]);
  3189. }
  3190. else {
  3191. cell.visible = false;
  3192. cell.dataSource = null;
  3193. }
  3194. }
  3195. _bindData(cell, data) {
  3196. var arr = cell._$bindData;
  3197. for (var i = 0, n = arr.length; i < n; i++) {
  3198. var ele = arr[i++];
  3199. var prop = arr[i++];
  3200. var value = arr[i];
  3201. var fun = UIUtils.getBindFun(value);
  3202. ele[prop] = fun.call(this, data);
  3203. }
  3204. }
  3205. get array() {
  3206. return this._array;
  3207. }
  3208. set array(value) {
  3209. this.runCallLater(this.changeCells);
  3210. this._array = value || [];
  3211. this._preLen = this._array.length;
  3212. var length = this._array.length;
  3213. this.totalPage = Math.ceil(length / (this.repeatX * this.repeatY));
  3214. this._selectedIndex = this._selectedIndex < length ? this._selectedIndex : length - 1;
  3215. this.startIndex = this._startIndex;
  3216. if (this._scrollBar) {
  3217. this._scrollBar.stopScroll();
  3218. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3219. var numY = this._isVertical ? this.repeatY : this.repeatX;
  3220. var lineCount = Math.ceil(length / numX);
  3221. var total = this._cellOffset > 0 ? this.totalPage + 1 : this.totalPage;
  3222. if (total > 1 && lineCount >= numY) {
  3223. this._scrollBar.scrollSize = this._cellSize;
  3224. this._scrollBar.thumbPercent = numY / lineCount;
  3225. this._scrollBar.setScroll(0, (lineCount - numY) * this._cellSize + this._cellOffset, this._scrollBar.value);
  3226. this._scrollBar.target = this._content;
  3227. }
  3228. else {
  3229. this._scrollBar.setScroll(0, 0, 0);
  3230. this._scrollBar.target = this._content;
  3231. }
  3232. }
  3233. }
  3234. updateArray(array) {
  3235. this._array = array;
  3236. var freshStart;
  3237. if (this._array) {
  3238. freshStart = this._preLen - this._startIndex;
  3239. if (freshStart >= 0)
  3240. this.renderItems(freshStart);
  3241. this._preLen = this._array.length;
  3242. }
  3243. if (this._scrollBar) {
  3244. var length = array.length;
  3245. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3246. var numY = this._isVertical ? this.repeatY : this.repeatX;
  3247. var lineCount = Math.ceil(length / numX);
  3248. if (lineCount >= numY) {
  3249. this._scrollBar.thumbPercent = numY / lineCount;
  3250. this._scrollBar.slider["_max"] = (lineCount - numY) * this._cellSize + this._cellOffset;
  3251. }
  3252. }
  3253. }
  3254. get page() {
  3255. return this._page;
  3256. }
  3257. set page(value) {
  3258. this._page = value;
  3259. if (this._array) {
  3260. this._page = value > 0 ? value : 0;
  3261. this._page = this._page < this.totalPage ? this._page : this.totalPage - 1;
  3262. this.startIndex = this._page * this.repeatX * this.repeatY;
  3263. }
  3264. }
  3265. get length() {
  3266. return this._array ? this._array.length : 0;
  3267. }
  3268. set dataSource(value) {
  3269. this._dataSource = value;
  3270. if (typeof (value) == 'number' || typeof (value) == 'string')
  3271. this.selectedIndex = parseInt(value);
  3272. else if (value instanceof Array)
  3273. this.array = value;
  3274. else
  3275. super.dataSource = value;
  3276. }
  3277. get dataSource() {
  3278. return super.dataSource;
  3279. }
  3280. get cells() {
  3281. this.runCallLater(this.changeCells);
  3282. return this._cells;
  3283. }
  3284. get elasticEnabled() {
  3285. return this._elasticEnabled;
  3286. }
  3287. set elasticEnabled(value) {
  3288. this._elasticEnabled = value;
  3289. if (this._scrollBar) {
  3290. this._scrollBar.elasticDistance = value ? 200 : 0;
  3291. }
  3292. }
  3293. refresh() {
  3294. this.array = this._array;
  3295. }
  3296. getItem(index) {
  3297. if (index > -1 && index < this._array.length) {
  3298. return this._array[index];
  3299. }
  3300. return null;
  3301. }
  3302. changeItem(index, source) {
  3303. if (index > -1 && index < this._array.length) {
  3304. this._array[index] = source;
  3305. if (index >= this._startIndex && index < this._startIndex + this._cells.length) {
  3306. this.renderItem(this.getCell(index), index);
  3307. }
  3308. }
  3309. }
  3310. setItem(index, source) {
  3311. this.changeItem(index, source);
  3312. }
  3313. addItem(souce) {
  3314. this._array.push(souce);
  3315. this.array = this._array;
  3316. }
  3317. addItemAt(souce, index) {
  3318. this._array.splice(index, 0, souce);
  3319. this.array = this._array;
  3320. }
  3321. deleteItem(index) {
  3322. this._array.splice(index, 1);
  3323. this.array = this._array;
  3324. }
  3325. getCell(index) {
  3326. this.runCallLater(this.changeCells);
  3327. if (index > -1 && this._cells) {
  3328. return this._cells[(index - this._startIndex) % this._cells.length];
  3329. }
  3330. return null;
  3331. }
  3332. scrollTo(index) {
  3333. if (this._scrollBar) {
  3334. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3335. this._scrollBar.value = Math.floor(index / numX) * this._cellSize;
  3336. }
  3337. else {
  3338. this.startIndex = index;
  3339. }
  3340. }
  3341. tweenTo(index, time = 200, complete = null) {
  3342. if (this._scrollBar) {
  3343. this._scrollBar.stopScroll();
  3344. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3345. Laya.Tween.to(this._scrollBar, { value: Math.floor(index / numX) * this._cellSize }, time, null, complete, 0, true);
  3346. }
  3347. else {
  3348. this.startIndex = index;
  3349. if (complete)
  3350. complete.run();
  3351. }
  3352. }
  3353. _setCellChanged() {
  3354. if (!this._cellChanged) {
  3355. this._cellChanged = true;
  3356. this.callLater(this.changeCells);
  3357. }
  3358. }
  3359. commitMeasure() {
  3360. this.runCallLater(this.changeCells);
  3361. }
  3362. }
  3363. Laya.ILaya.regClass(List);
  3364. Laya.ClassUtils.regClass("laya.ui.List", List);
  3365. Laya.ClassUtils.regClass("Laya.List", List);
  3366. class ComboBox extends UIComponent {
  3367. constructor(skin = null, labels = null) {
  3368. super();
  3369. this._visibleNum = 6;
  3370. this._itemColors = Styles.comboBoxItemColors;
  3371. this._itemSize = 12;
  3372. this._labels = [];
  3373. this._selectedIndex = -1;
  3374. this.itemRender = null;
  3375. this.skin = skin;
  3376. this.labels = labels;
  3377. }
  3378. destroy(destroyChild = true) {
  3379. super.destroy(destroyChild);
  3380. this._button && this._button.destroy(destroyChild);
  3381. this._list && this._list.destroy(destroyChild);
  3382. this._button = null;
  3383. this._list = null;
  3384. this._itemColors = null;
  3385. this._labels = null;
  3386. this._selectHandler = null;
  3387. }
  3388. createChildren() {
  3389. this.addChild(this._button = new Button());
  3390. this._button.text.align = "left";
  3391. this._button.labelPadding = "0,0,0,5";
  3392. this._button.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  3393. }
  3394. _createList() {
  3395. this._list = new List();
  3396. if (this._scrollBarSkin)
  3397. this._list.vScrollBarSkin = this._scrollBarSkin;
  3398. this._setListEvent(this._list);
  3399. }
  3400. _setListEvent(list) {
  3401. this._list.selectEnable = true;
  3402. this._list.on(Laya.Event.MOUSE_DOWN, this, this.onListDown);
  3403. this._list.mouseHandler = Laya.Handler.create(this, this.onlistItemMouse, null, false);
  3404. if (this._list.scrollBar)
  3405. this._list.scrollBar.on(Laya.Event.MOUSE_DOWN, this, this.onScrollBarDown);
  3406. }
  3407. onListDown(e) {
  3408. e.stopPropagation();
  3409. }
  3410. onScrollBarDown(e) {
  3411. e.stopPropagation();
  3412. }
  3413. onButtonMouseDown(e) {
  3414. this.callLater(this.switchTo, [!this._isOpen]);
  3415. }
  3416. get skin() {
  3417. return this._button.skin;
  3418. }
  3419. set skin(value) {
  3420. if (this._button.skin != value) {
  3421. this._button.skin = value;
  3422. this._listChanged = true;
  3423. }
  3424. }
  3425. measureWidth() {
  3426. return this._button.width;
  3427. }
  3428. measureHeight() {
  3429. return this._button.height;
  3430. }
  3431. changeList() {
  3432. this._listChanged = false;
  3433. var labelWidth = this.width - 2;
  3434. var labelColor = this._itemColors[2];
  3435. this._itemHeight = this._itemSize + 6;
  3436. this._list.itemRender = this.itemRender || { type: "Box", child: [{ type: "Label", props: { name: "label", x: 1, padding: "3,3,3,3", width: labelWidth, height: this._itemHeight, fontSize: this._itemSize, color: labelColor } }] };
  3437. this._list.repeatY = this._visibleNum;
  3438. this._list.refresh();
  3439. }
  3440. onlistItemMouse(e, index) {
  3441. var type = e.type;
  3442. if (type === Laya.Event.MOUSE_OVER || type === Laya.Event.MOUSE_OUT) {
  3443. if (this._isCustomList)
  3444. return;
  3445. var box = this._list.getCell(index);
  3446. if (!box)
  3447. return;
  3448. var label = box.getChildByName("label");
  3449. if (label) {
  3450. if (type === Laya.Event.ROLL_OVER) {
  3451. label.bgColor = this._itemColors[0];
  3452. label.color = this._itemColors[1];
  3453. }
  3454. else {
  3455. label.bgColor = null;
  3456. label.color = this._itemColors[2];
  3457. }
  3458. }
  3459. }
  3460. else if (type === Laya.Event.CLICK) {
  3461. this.selectedIndex = index;
  3462. this.isOpen = false;
  3463. }
  3464. }
  3465. switchTo(value) {
  3466. this.isOpen = value;
  3467. }
  3468. changeOpen() {
  3469. this.isOpen = !this._isOpen;
  3470. }
  3471. set width(value) {
  3472. super.width = value;
  3473. this._button.width = this._width;
  3474. this._itemChanged = true;
  3475. this._listChanged = true;
  3476. }
  3477. get width() {
  3478. return super.width;
  3479. }
  3480. set height(value) {
  3481. super.height = value;
  3482. this._button.height = this._height;
  3483. }
  3484. get height() {
  3485. return super.height;
  3486. }
  3487. get labels() {
  3488. return this._labels.join(",");
  3489. }
  3490. set labels(value) {
  3491. if (this._labels.length > 0)
  3492. this.selectedIndex = -1;
  3493. if (value)
  3494. this._labels = value.split(",");
  3495. else
  3496. this._labels.length = 0;
  3497. this._itemChanged = true;
  3498. }
  3499. changeItem() {
  3500. this._itemChanged = false;
  3501. this._listHeight = this._labels.length > 0 ? Math.min(this._visibleNum, this._labels.length) * this._itemHeight : this._itemHeight;
  3502. if (!this._isCustomList) {
  3503. var g = this._list.graphics;
  3504. g.clear(true);
  3505. g.drawRect(0, 0, this.width - 1, this._listHeight, this._itemColors[4], this._itemColors[3]);
  3506. }
  3507. var a = this._list.array || [];
  3508. a.length = 0;
  3509. for (var i = 0, n = this._labels.length; i < n; i++) {
  3510. a.push({ label: this._labels[i] });
  3511. }
  3512. this._list.height = this._listHeight;
  3513. this._list.array = a;
  3514. }
  3515. get selectedIndex() {
  3516. return this._selectedIndex;
  3517. }
  3518. set selectedIndex(value) {
  3519. if (this._selectedIndex != value) {
  3520. this._selectedIndex = value;
  3521. if (this._labels.length > 0)
  3522. this.changeSelected();
  3523. else
  3524. this.callLater(this.changeSelected);
  3525. this.event(Laya.Event.CHANGE, [Laya.Event.EMPTY.setTo(Laya.Event.CHANGE, this, this)]);
  3526. this._selectHandler && this._selectHandler.runWith(this._selectedIndex);
  3527. }
  3528. }
  3529. changeSelected() {
  3530. this._button.label = this.selectedLabel;
  3531. }
  3532. get selectHandler() {
  3533. return this._selectHandler;
  3534. }
  3535. set selectHandler(value) {
  3536. this._selectHandler = value;
  3537. }
  3538. get selectedLabel() {
  3539. return this._selectedIndex > -1 && this._selectedIndex < this._labels.length ? this._labels[this._selectedIndex] : null;
  3540. }
  3541. set selectedLabel(value) {
  3542. this.selectedIndex = this._labels.indexOf(value);
  3543. }
  3544. get visibleNum() {
  3545. return this._visibleNum;
  3546. }
  3547. set visibleNum(value) {
  3548. this._visibleNum = value;
  3549. this._listChanged = true;
  3550. }
  3551. get itemColors() {
  3552. return String(this._itemColors);
  3553. }
  3554. set itemColors(value) {
  3555. this._itemColors = UIUtils.fillArray(this._itemColors, value, String);
  3556. this._listChanged = true;
  3557. }
  3558. get itemSize() {
  3559. return this._itemSize;
  3560. }
  3561. set itemSize(value) {
  3562. this._itemSize = value;
  3563. this._listChanged = true;
  3564. }
  3565. get isOpen() {
  3566. return this._isOpen;
  3567. }
  3568. set isOpen(value) {
  3569. if (this._isOpen != value) {
  3570. this._isOpen = value;
  3571. this._button.selected = this._isOpen;
  3572. if (this._isOpen) {
  3573. this._list || this._createList();
  3574. this._listChanged && !this._isCustomList && this.changeList();
  3575. this._itemChanged && this.changeItem();
  3576. var p = this.localToGlobal(Laya.Point.TEMP.setTo(0, 0));
  3577. var py = p.y + this._button.height;
  3578. py = py + this._listHeight <= Laya.ILaya.stage.height ? py : p.y - this._listHeight;
  3579. this._list.pos(p.x, py);
  3580. this._list.zOrder = 1001;
  3581. Laya.ILaya.stage.addChild(this._list);
  3582. Laya.ILaya.stage.once(Laya.Event.MOUSE_DOWN, this, this.removeList);
  3583. Laya.ILaya.stage.on(Laya.Event.MOUSE_WHEEL, this, this._onStageMouseWheel);
  3584. this._list.selectedIndex = this._selectedIndex;
  3585. }
  3586. else {
  3587. this._list && this._list.removeSelf();
  3588. }
  3589. }
  3590. }
  3591. _onStageMouseWheel(e) {
  3592. if (!this._list || this._list.contains(e.target))
  3593. return;
  3594. this.removeList(null);
  3595. }
  3596. removeList(e) {
  3597. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this.removeList);
  3598. Laya.ILaya.stage.off(Laya.Event.MOUSE_WHEEL, this, this._onStageMouseWheel);
  3599. this.isOpen = false;
  3600. }
  3601. get scrollBarSkin() {
  3602. return this._scrollBarSkin;
  3603. }
  3604. set scrollBarSkin(value) {
  3605. this._scrollBarSkin = value;
  3606. }
  3607. get sizeGrid() {
  3608. return this._button.sizeGrid;
  3609. }
  3610. set sizeGrid(value) {
  3611. this._button.sizeGrid = value;
  3612. }
  3613. get scrollBar() {
  3614. return this.list.scrollBar;
  3615. }
  3616. get button() {
  3617. return this._button;
  3618. }
  3619. get list() {
  3620. this._list || this._createList();
  3621. return this._list;
  3622. }
  3623. set list(value) {
  3624. if (value) {
  3625. value.removeSelf();
  3626. this._isCustomList = true;
  3627. this._list = value;
  3628. this._setListEvent(value);
  3629. this._itemHeight = value.getCell(0).height + value.spaceY;
  3630. }
  3631. }
  3632. set dataSource(value) {
  3633. this._dataSource = value;
  3634. if (typeof (value) == 'number' || typeof (value) == 'string')
  3635. this.selectedIndex = parseInt(value);
  3636. else if (value instanceof Array)
  3637. this.labels = value.join(",");
  3638. else
  3639. super.dataSource = value;
  3640. }
  3641. get dataSource() {
  3642. return super.dataSource;
  3643. }
  3644. get labelColors() {
  3645. return this._button.labelColors;
  3646. }
  3647. set labelColors(value) {
  3648. if (this._button.labelColors != value) {
  3649. this._button.labelColors = value;
  3650. }
  3651. }
  3652. get labelPadding() {
  3653. return this._button.text.padding.join(",");
  3654. }
  3655. set labelPadding(value) {
  3656. this._button.text.padding = UIUtils.fillArray(Styles.labelPadding, value, Number);
  3657. }
  3658. get labelSize() {
  3659. return this._button.text.fontSize;
  3660. }
  3661. set labelSize(value) {
  3662. this._button.text.fontSize = value;
  3663. }
  3664. get labelBold() {
  3665. return this._button.text.bold;
  3666. }
  3667. set labelBold(value) {
  3668. this._button.text.bold = value;
  3669. }
  3670. get labelFont() {
  3671. return this._button.text.font;
  3672. }
  3673. set labelFont(value) {
  3674. this._button.text.font = value;
  3675. }
  3676. get stateNum() {
  3677. return this._button.stateNum;
  3678. }
  3679. set stateNum(value) {
  3680. this._button.stateNum = value;
  3681. }
  3682. }
  3683. Laya.ILaya.regClass(ComboBox);
  3684. Laya.ClassUtils.regClass("laya.ui.ComboBox", ComboBox);
  3685. Laya.ClassUtils.regClass("Laya.ComboBox", ComboBox);
  3686. class ProgressBar extends UIComponent {
  3687. constructor(skin = null) {
  3688. super();
  3689. this._value = 0.5;
  3690. this.skin = skin;
  3691. }
  3692. destroy(destroyChild = true) {
  3693. super.destroy(destroyChild);
  3694. this._bg && this._bg.destroy(destroyChild);
  3695. this._bar && this._bar.destroy(destroyChild);
  3696. this._bg = this._bar = null;
  3697. this.changeHandler = null;
  3698. }
  3699. createChildren() {
  3700. this.addChild(this._bg = new Image());
  3701. this.addChild(this._bar = new Image());
  3702. this._bar._bitmap.autoCacheCmd = false;
  3703. }
  3704. get skin() {
  3705. return this._skin;
  3706. }
  3707. set skin(value) {
  3708. if (this._skin != value) {
  3709. this._skin = value;
  3710. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  3711. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  3712. }
  3713. else {
  3714. this._skinLoaded();
  3715. }
  3716. }
  3717. }
  3718. _skinLoaded() {
  3719. this._bg.skin = this._skin;
  3720. this._bar.skin = this._skin.replace(".png", "$bar.png");
  3721. this.callLater(this.changeValue);
  3722. this._sizeChanged();
  3723. this.event(Laya.Event.LOADED);
  3724. }
  3725. measureWidth() {
  3726. return this._bg.width;
  3727. }
  3728. measureHeight() {
  3729. return this._bg.height;
  3730. }
  3731. get value() {
  3732. return this._value;
  3733. }
  3734. set value(num) {
  3735. if (this._value != num) {
  3736. num = num > 1 ? 1 : num < 0 ? 0 : num;
  3737. this._value = num;
  3738. this.callLater(this.changeValue);
  3739. this.event(Laya.Event.CHANGE);
  3740. this.changeHandler && this.changeHandler.runWith(num);
  3741. }
  3742. }
  3743. changeValue() {
  3744. if (this.sizeGrid) {
  3745. var grid = this.sizeGrid.split(",");
  3746. var left = Number(grid[3]);
  3747. var right = Number(grid[1]);
  3748. var max = this.width - left - right;
  3749. var sw = max * this._value;
  3750. this._bar.width = left + right + sw;
  3751. this._bar.visible = this._bar.width > left + right;
  3752. }
  3753. else {
  3754. this._bar.width = this.width * this._value;
  3755. }
  3756. }
  3757. get bar() {
  3758. return this._bar;
  3759. }
  3760. get bg() {
  3761. return this._bg;
  3762. }
  3763. get sizeGrid() {
  3764. return this._bg.sizeGrid;
  3765. }
  3766. set sizeGrid(value) {
  3767. this._bg.sizeGrid = this._bar.sizeGrid = value;
  3768. }
  3769. set width(value) {
  3770. super.width = value;
  3771. this._bg.width = this._width;
  3772. this.callLater(this.changeValue);
  3773. }
  3774. get width() {
  3775. return super.width;
  3776. }
  3777. set height(value) {
  3778. super.height = value;
  3779. this._bg.height = this._height;
  3780. this._bar.height = this._height;
  3781. }
  3782. get height() {
  3783. return super.height;
  3784. }
  3785. set dataSource(value) {
  3786. this._dataSource = value;
  3787. if (typeof (value) == 'number' || typeof (value) == 'string')
  3788. this.value = Number(value);
  3789. else
  3790. super.dataSource = value;
  3791. }
  3792. get dataSource() {
  3793. return super.dataSource;
  3794. }
  3795. }
  3796. Laya.ILaya.regClass(ProgressBar);
  3797. Laya.ClassUtils.regClass("laya.ui.ProgressBar", ProgressBar);
  3798. Laya.ClassUtils.regClass("Laya.ProgressBar", ProgressBar);
  3799. class Radio extends Button {
  3800. constructor(skin = null, label = "") {
  3801. super(skin, label);
  3802. this.toggle = false;
  3803. this._autoSize = false;
  3804. }
  3805. destroy(destroyChild = true) {
  3806. super.destroy(destroyChild);
  3807. this._value = null;
  3808. }
  3809. preinitialize() {
  3810. super.preinitialize();
  3811. this.toggle = false;
  3812. this._autoSize = false;
  3813. }
  3814. initialize() {
  3815. super.initialize();
  3816. this.createText();
  3817. this._text.align = "left";
  3818. this._text.valign = "top";
  3819. this._text.width = 0;
  3820. this.on(Laya.Event.CLICK, this, this.onClick);
  3821. }
  3822. onClick(e) {
  3823. this.selected = true;
  3824. }
  3825. get value() {
  3826. return this._value != null ? this._value : this.label;
  3827. }
  3828. set value(obj) {
  3829. this._value = obj;
  3830. }
  3831. }
  3832. Laya.ILaya.regClass(Radio);
  3833. Laya.ClassUtils.regClass("laya.ui.Radio", Radio);
  3834. Laya.ClassUtils.regClass("Laya.Radio", Radio);
  3835. class UIGroup extends Box {
  3836. constructor(labels = null, skin = null) {
  3837. super();
  3838. this._selectedIndex = -1;
  3839. this._direction = "horizontal";
  3840. this._space = 0;
  3841. this.skin = skin;
  3842. this.labels = labels;
  3843. }
  3844. preinitialize() {
  3845. this.mouseEnabled = true;
  3846. }
  3847. destroy(destroyChild = true) {
  3848. super.destroy(destroyChild);
  3849. this._items && (this._items.length = 0);
  3850. this._items = null;
  3851. this.selectHandler = null;
  3852. }
  3853. addItem(item, autoLayOut = true) {
  3854. var display = item;
  3855. var index = this._items.length;
  3856. display.name = "item" + index;
  3857. this.addChild(display);
  3858. this.initItems();
  3859. if (autoLayOut && index > 0) {
  3860. var preItem = this._items[index - 1];
  3861. if (this._direction == "horizontal") {
  3862. display.x = preItem._x + preItem.width + this._space;
  3863. }
  3864. else {
  3865. display.y = preItem._y + preItem.height + this._space;
  3866. }
  3867. }
  3868. else {
  3869. if (autoLayOut) {
  3870. display.x = 0;
  3871. display.y = 0;
  3872. }
  3873. }
  3874. return index;
  3875. }
  3876. delItem(item, autoLayOut = true) {
  3877. var index = this._items.indexOf(item);
  3878. if (index != -1) {
  3879. var display = item;
  3880. this.removeChild(display);
  3881. for (var i = index + 1, n = this._items.length; i < n; i++) {
  3882. var child = this._items[i];
  3883. child.name = "item" + (i - 1);
  3884. if (autoLayOut) {
  3885. if (this._direction == "horizontal") {
  3886. child.x -= display.width + this._space;
  3887. }
  3888. else {
  3889. child.y -= display.height + this._space;
  3890. }
  3891. }
  3892. }
  3893. this.initItems();
  3894. if (this._selectedIndex > -1) {
  3895. var newIndex;
  3896. newIndex = this._selectedIndex < this._items.length ? this._selectedIndex : (this._selectedIndex - 1);
  3897. this._selectedIndex = -1;
  3898. this.selectedIndex = newIndex;
  3899. }
  3900. }
  3901. }
  3902. _afterInited() {
  3903. this.initItems();
  3904. }
  3905. initItems() {
  3906. this._items || (this._items = []);
  3907. this._items.length = 0;
  3908. for (var i = 0; i < 10000; i++) {
  3909. var item = this.getChildByName("item" + i);
  3910. if (item == null)
  3911. break;
  3912. this._items.push(item);
  3913. item.selected = (i === this._selectedIndex);
  3914. item.clickHandler = Laya.Handler.create(this, this.itemClick, [i], false);
  3915. }
  3916. }
  3917. itemClick(index) {
  3918. this.selectedIndex = index;
  3919. }
  3920. get selectedIndex() {
  3921. return this._selectedIndex;
  3922. }
  3923. set selectedIndex(value) {
  3924. if (this._selectedIndex != value) {
  3925. this.setSelect(this._selectedIndex, false);
  3926. this._selectedIndex = value;
  3927. this.setSelect(value, true);
  3928. this.event(Laya.Event.CHANGE);
  3929. this.selectHandler && this.selectHandler.runWith(this._selectedIndex);
  3930. }
  3931. }
  3932. setSelect(index, selected) {
  3933. if (this._items && index > -1 && index < this._items.length)
  3934. this._items[index].selected = selected;
  3935. }
  3936. get skin() {
  3937. return this._skin;
  3938. }
  3939. set skin(value) {
  3940. if (this._skin != value) {
  3941. this._skin = value;
  3942. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  3943. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  3944. }
  3945. else {
  3946. this._skinLoaded();
  3947. }
  3948. }
  3949. }
  3950. _skinLoaded() {
  3951. this._setLabelChanged();
  3952. this.event(Laya.Event.LOADED);
  3953. }
  3954. get labels() {
  3955. return this._labels;
  3956. }
  3957. set labels(value) {
  3958. if (this._labels != value) {
  3959. this._labels = value;
  3960. this.removeChildren();
  3961. this._setLabelChanged();
  3962. if (this._labels) {
  3963. var a = this._labels.split(",");
  3964. for (var i = 0, n = a.length; i < n; i++) {
  3965. var item = this.createItem(this._skin, a[i]);
  3966. item.name = "item" + i;
  3967. this.addChild(item);
  3968. }
  3969. }
  3970. this.initItems();
  3971. }
  3972. }
  3973. createItem(skin, label) {
  3974. return null;
  3975. }
  3976. get labelColors() {
  3977. return this._labelColors;
  3978. }
  3979. set labelColors(value) {
  3980. if (this._labelColors != value) {
  3981. this._labelColors = value;
  3982. this._setLabelChanged();
  3983. }
  3984. }
  3985. get labelStroke() {
  3986. return this._labelStroke;
  3987. }
  3988. set labelStroke(value) {
  3989. if (this._labelStroke != value) {
  3990. this._labelStroke = value;
  3991. this._setLabelChanged();
  3992. }
  3993. }
  3994. get labelStrokeColor() {
  3995. return this._labelStrokeColor;
  3996. }
  3997. set labelStrokeColor(value) {
  3998. if (this._labelStrokeColor != value) {
  3999. this._labelStrokeColor = value;
  4000. this._setLabelChanged();
  4001. }
  4002. }
  4003. get strokeColors() {
  4004. return this._strokeColors;
  4005. }
  4006. set strokeColors(value) {
  4007. if (this._strokeColors != value) {
  4008. this._strokeColors = value;
  4009. this._setLabelChanged();
  4010. }
  4011. }
  4012. get labelSize() {
  4013. return this._labelSize;
  4014. }
  4015. set labelSize(value) {
  4016. if (this._labelSize != value) {
  4017. this._labelSize = value;
  4018. this._setLabelChanged();
  4019. }
  4020. }
  4021. get stateNum() {
  4022. return this._stateNum;
  4023. }
  4024. set stateNum(value) {
  4025. if (this._stateNum != value) {
  4026. this._stateNum = value;
  4027. this._setLabelChanged();
  4028. }
  4029. }
  4030. get labelBold() {
  4031. return this._labelBold;
  4032. }
  4033. set labelBold(value) {
  4034. if (this._labelBold != value) {
  4035. this._labelBold = value;
  4036. this._setLabelChanged();
  4037. }
  4038. }
  4039. get labelFont() {
  4040. return this._labelFont;
  4041. }
  4042. set labelFont(value) {
  4043. if (this._labelFont != value) {
  4044. this._labelFont = value;
  4045. this._setLabelChanged();
  4046. }
  4047. }
  4048. get labelPadding() {
  4049. return this._labelPadding;
  4050. }
  4051. set labelPadding(value) {
  4052. if (this._labelPadding != value) {
  4053. this._labelPadding = value;
  4054. this._setLabelChanged();
  4055. }
  4056. }
  4057. get direction() {
  4058. return this._direction;
  4059. }
  4060. set direction(value) {
  4061. this._direction = value;
  4062. this._setLabelChanged();
  4063. }
  4064. get space() {
  4065. return this._space;
  4066. }
  4067. set space(value) {
  4068. this._space = value;
  4069. this._setLabelChanged();
  4070. }
  4071. changeLabels() {
  4072. this._labelChanged = false;
  4073. if (this._items) {
  4074. var left = 0;
  4075. for (var i = 0, n = this._items.length; i < n; i++) {
  4076. var btn = this._items[i];
  4077. this._skin && (btn.skin = this._skin);
  4078. this._labelColors && (btn.labelColors = this._labelColors);
  4079. this._labelSize && (btn.labelSize = this._labelSize);
  4080. this._labelStroke && (btn.labelStroke = this._labelStroke);
  4081. this._labelStrokeColor && (btn.labelStrokeColor = this._labelStrokeColor);
  4082. this._strokeColors && (btn.strokeColors = this._strokeColors);
  4083. this._labelBold && (btn.labelBold = this._labelBold);
  4084. this._labelPadding && (btn.labelPadding = this._labelPadding);
  4085. this._labelAlign && (btn.labelAlign = this._labelAlign);
  4086. this._stateNum && (btn.stateNum = this._stateNum);
  4087. this._labelFont && (btn.labelFont = this._labelFont);
  4088. if (this._direction === "horizontal") {
  4089. btn.y = 0;
  4090. btn.x = left;
  4091. left += btn.width + this._space;
  4092. }
  4093. else {
  4094. btn.x = 0;
  4095. btn.y = left;
  4096. left += btn.height + this._space;
  4097. }
  4098. }
  4099. }
  4100. this._sizeChanged();
  4101. }
  4102. commitMeasure() {
  4103. this.runCallLater(this.changeLabels);
  4104. }
  4105. get items() {
  4106. return this._items;
  4107. }
  4108. get selection() {
  4109. return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? this._items[this._selectedIndex] : null;
  4110. }
  4111. set selection(value) {
  4112. this.selectedIndex = this._items.indexOf(value);
  4113. }
  4114. set dataSource(value) {
  4115. this._dataSource = value;
  4116. if (typeof (value) == 'number' || typeof (value) == 'string')
  4117. this.selectedIndex = parseInt(value);
  4118. else if (value instanceof Array)
  4119. this.labels = value.join(",");
  4120. else
  4121. super.dataSource = value;
  4122. }
  4123. get dataSource() {
  4124. return super.dataSource;
  4125. }
  4126. _setLabelChanged() {
  4127. if (!this._labelChanged) {
  4128. this._labelChanged = true;
  4129. this.callLater(this.changeLabels);
  4130. }
  4131. }
  4132. }
  4133. Laya.ILaya.regClass(UIGroup);
  4134. Laya.ClassUtils.regClass("laya.ui.UIGroup", UIGroup);
  4135. Laya.ClassUtils.regClass("Laya.UIGroup", UIGroup);
  4136. class RadioGroup extends UIGroup {
  4137. createItem(skin, label) {
  4138. return new Radio(skin, label);
  4139. }
  4140. }
  4141. Laya.ILaya.regClass(RadioGroup);
  4142. Laya.ClassUtils.regClass("laya.ui.RadioGroup", RadioGroup);
  4143. Laya.ClassUtils.regClass("Laya.RadioGroup", RadioGroup);
  4144. class Tab extends UIGroup {
  4145. createItem(skin, label) {
  4146. return new Button(skin, label);
  4147. }
  4148. }
  4149. Laya.ILaya.regClass(Tab);
  4150. Laya.ClassUtils.regClass("laya.ui.Tab", Tab);
  4151. Laya.ClassUtils.regClass("Laya.Tab", Tab);
  4152. class ViewStack extends Box {
  4153. constructor() {
  4154. super(...arguments);
  4155. this._setIndexHandler = Laya.Handler.create(this, this.setIndex, null, false);
  4156. }
  4157. setItems(views) {
  4158. this.removeChildren();
  4159. var index = 0;
  4160. for (var i = 0, n = views.length; i < n; i++) {
  4161. var item = views[i];
  4162. if (item) {
  4163. item.name = "item" + index;
  4164. this.addChild(item);
  4165. index++;
  4166. }
  4167. }
  4168. this.initItems();
  4169. }
  4170. addItem(view) {
  4171. view.name = "item" + this._items.length;
  4172. this.addChild(view);
  4173. this.initItems();
  4174. }
  4175. _afterInited() {
  4176. this.initItems();
  4177. }
  4178. initItems() {
  4179. this._items = [];
  4180. for (var i = 0; i < 10000; i++) {
  4181. var item = this.getChildByName("item" + i);
  4182. if (item == null) {
  4183. break;
  4184. }
  4185. this._items.push(item);
  4186. item.visible = (i == this._selectedIndex);
  4187. }
  4188. }
  4189. get selectedIndex() {
  4190. return this._selectedIndex;
  4191. }
  4192. set selectedIndex(value) {
  4193. if (this._selectedIndex != value) {
  4194. this.setSelect(this._selectedIndex, false);
  4195. this._selectedIndex = value;
  4196. this.setSelect(this._selectedIndex, true);
  4197. }
  4198. }
  4199. setSelect(index, selected) {
  4200. if (this._items && index > -1 && index < this._items.length) {
  4201. this._items[index].visible = selected;
  4202. }
  4203. }
  4204. get selection() {
  4205. return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? this._items[this._selectedIndex] : null;
  4206. }
  4207. set selection(value) {
  4208. this.selectedIndex = this._items.indexOf(value);
  4209. }
  4210. get setIndexHandler() {
  4211. return this._setIndexHandler;
  4212. }
  4213. set setIndexHandler(value) {
  4214. this._setIndexHandler = value;
  4215. }
  4216. setIndex(index) {
  4217. this.selectedIndex = index;
  4218. }
  4219. get items() {
  4220. return this._items;
  4221. }
  4222. set dataSource(value) {
  4223. this._dataSource = value;
  4224. if (typeof (value) == 'number' || typeof (value) == 'string') {
  4225. this.selectedIndex = parseInt(value);
  4226. }
  4227. else {
  4228. for (var prop in this._dataSource) {
  4229. if (prop in this) {
  4230. this[prop] = this._dataSource[prop];
  4231. }
  4232. }
  4233. }
  4234. }
  4235. get dataSource() {
  4236. return super.dataSource;
  4237. }
  4238. }
  4239. Laya.ILaya.regClass(ViewStack);
  4240. Laya.ClassUtils.regClass("laya.ui.ViewStack", ViewStack);
  4241. Laya.ClassUtils.regClass("Laya.ViewStack", ViewStack);
  4242. class TextInput extends Label {
  4243. constructor(text = "") {
  4244. super();
  4245. this.text = text;
  4246. this.skin = this.skin;
  4247. }
  4248. preinitialize() {
  4249. this.mouseEnabled = true;
  4250. }
  4251. destroy(destroyChild = true) {
  4252. super.destroy(destroyChild);
  4253. this._bg && this._bg.destroy();
  4254. this._bg = null;
  4255. }
  4256. createChildren() {
  4257. this.addChild(this._tf = new Laya.Input());
  4258. this._tf.padding = Styles.inputLabelPadding;
  4259. this._tf.on(Laya.Event.INPUT, this, this._onInput);
  4260. this._tf.on(Laya.Event.ENTER, this, this._onEnter);
  4261. this._tf.on(Laya.Event.BLUR, this, this._onBlur);
  4262. this._tf.on(Laya.Event.FOCUS, this, this._onFocus);
  4263. }
  4264. _onFocus() {
  4265. this.event(Laya.Event.FOCUS, this);
  4266. }
  4267. _onBlur() {
  4268. this.event(Laya.Event.BLUR, this);
  4269. }
  4270. _onInput() {
  4271. this.event(Laya.Event.INPUT, this);
  4272. }
  4273. _onEnter() {
  4274. this.event(Laya.Event.ENTER, this);
  4275. }
  4276. initialize() {
  4277. this.width = 128;
  4278. this.height = 22;
  4279. }
  4280. get bg() {
  4281. return this._bg;
  4282. }
  4283. set bg(value) {
  4284. this.graphics = this._bg = value;
  4285. }
  4286. get skin() {
  4287. return this._skin;
  4288. }
  4289. set skin(value) {
  4290. if (this._skin != value) {
  4291. this._skin = value;
  4292. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  4293. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  4294. }
  4295. else {
  4296. this._skinLoaded();
  4297. }
  4298. }
  4299. }
  4300. _skinLoaded() {
  4301. this._bg || (this.graphics = this._bg = new AutoBitmap());
  4302. this._bg.source = Laya.Loader.getRes(this._skin);
  4303. this._width && (this._bg.width = this._width);
  4304. this._height && (this._bg.height = this._height);
  4305. this._sizeChanged();
  4306. this.event(Laya.Event.LOADED);
  4307. }
  4308. get sizeGrid() {
  4309. return this._bg && this._bg.sizeGrid ? this._bg.sizeGrid.join(",") : null;
  4310. }
  4311. set sizeGrid(value) {
  4312. this._bg || (this.graphics = this._bg = new AutoBitmap());
  4313. this._bg.sizeGrid = UIUtils.fillArray(Styles.defaultSizeGrid, value, Number);
  4314. }
  4315. set text(value) {
  4316. if (this._tf.text != value) {
  4317. value = value + "";
  4318. this._tf.text = value;
  4319. this.event(Laya.Event.CHANGE);
  4320. }
  4321. }
  4322. get text() {
  4323. return super.text;
  4324. }
  4325. set width(value) {
  4326. super.width = value;
  4327. this._bg && (this._bg.width = value);
  4328. }
  4329. get width() {
  4330. return super.width;
  4331. }
  4332. set height(value) {
  4333. super.height = value;
  4334. this._bg && (this._bg.height = value);
  4335. }
  4336. get height() {
  4337. return super.height;
  4338. }
  4339. get multiline() {
  4340. return this._tf.multiline;
  4341. }
  4342. set multiline(value) {
  4343. this._tf.multiline = value;
  4344. }
  4345. set editable(value) {
  4346. this._tf.editable = value;
  4347. }
  4348. get editable() {
  4349. return this._tf.editable;
  4350. }
  4351. select() {
  4352. this._tf.select();
  4353. }
  4354. get restrict() {
  4355. return this._tf.restrict;
  4356. }
  4357. set restrict(pattern) {
  4358. this._tf.restrict = pattern;
  4359. }
  4360. get prompt() {
  4361. return this._tf.prompt;
  4362. }
  4363. set prompt(value) {
  4364. this._tf.prompt = value;
  4365. }
  4366. get promptColor() {
  4367. return this._tf.promptColor;
  4368. }
  4369. set promptColor(value) {
  4370. this._tf.promptColor = value;
  4371. }
  4372. get maxChars() {
  4373. return this._tf.maxChars;
  4374. }
  4375. set maxChars(value) {
  4376. this._tf.maxChars = value;
  4377. }
  4378. get focus() {
  4379. return this._tf.focus;
  4380. }
  4381. set focus(value) {
  4382. this._tf.focus = value;
  4383. }
  4384. get type() {
  4385. return this._tf.type;
  4386. }
  4387. set type(value) {
  4388. this._tf.type = value;
  4389. }
  4390. setSelection(startIndex, endIndex) {
  4391. this._tf.setSelection(startIndex, endIndex);
  4392. }
  4393. }
  4394. Laya.ILaya.regClass(TextInput);
  4395. Laya.ClassUtils.regClass("laya.ui.TextInput", TextInput);
  4396. Laya.ClassUtils.regClass("Laya.TextInput", TextInput);
  4397. class TextArea extends TextInput {
  4398. constructor(text = "") {
  4399. super(text);
  4400. this.on(Laya.Event.CHANGE, this, this._onTextChange);
  4401. }
  4402. _onTextChange() {
  4403. this.callLater(this.changeScroll);
  4404. }
  4405. destroy(destroyChild = true) {
  4406. this._vScrollBar && this._vScrollBar.destroy();
  4407. this._hScrollBar && this._hScrollBar.destroy();
  4408. this._vScrollBar = null;
  4409. this._hScrollBar = null;
  4410. super.destroy(destroyChild);
  4411. }
  4412. initialize() {
  4413. this.width = 180;
  4414. this.height = 150;
  4415. this._tf.wordWrap = true;
  4416. this.multiline = true;
  4417. }
  4418. set width(value) {
  4419. super.width = value;
  4420. this.callLater(this.changeScroll);
  4421. }
  4422. get width() {
  4423. return super.width;
  4424. }
  4425. set height(value) {
  4426. super.height = value;
  4427. this.callLater(this.changeScroll);
  4428. }
  4429. get height() {
  4430. return super.height;
  4431. }
  4432. get vScrollBarSkin() {
  4433. return this._vScrollBar ? this._vScrollBar.skin : null;
  4434. }
  4435. set vScrollBarSkin(value) {
  4436. if (this._vScrollBar == null) {
  4437. this.addChild(this._vScrollBar = new VScrollBar());
  4438. this._vScrollBar.on(Laya.Event.CHANGE, this, this.onVBarChanged);
  4439. this._vScrollBar.target = this._tf;
  4440. this.callLater(this.changeScroll);
  4441. }
  4442. this._vScrollBar.skin = value;
  4443. }
  4444. get hScrollBarSkin() {
  4445. return this._hScrollBar ? this._hScrollBar.skin : null;
  4446. }
  4447. set hScrollBarSkin(value) {
  4448. if (this._hScrollBar == null) {
  4449. this.addChild(this._hScrollBar = new HScrollBar());
  4450. this._hScrollBar.on(Laya.Event.CHANGE, this, this.onHBarChanged);
  4451. this._hScrollBar.mouseWheelEnable = false;
  4452. this._hScrollBar.target = this._tf;
  4453. this.callLater(this.changeScroll);
  4454. }
  4455. this._hScrollBar.skin = value;
  4456. }
  4457. onVBarChanged(e) {
  4458. if (this._tf.scrollY != this._vScrollBar.value) {
  4459. this._tf.scrollY = this._vScrollBar.value;
  4460. }
  4461. }
  4462. onHBarChanged(e) {
  4463. if (this._tf.scrollX != this._hScrollBar.value) {
  4464. this._tf.scrollX = this._hScrollBar.value;
  4465. }
  4466. }
  4467. get vScrollBar() {
  4468. return this._vScrollBar;
  4469. }
  4470. get hScrollBar() {
  4471. return this._hScrollBar;
  4472. }
  4473. get maxScrollY() {
  4474. return this._tf.maxScrollY;
  4475. }
  4476. get scrollY() {
  4477. return this._tf.scrollY;
  4478. }
  4479. get maxScrollX() {
  4480. return this._tf.maxScrollX;
  4481. }
  4482. get scrollX() {
  4483. return this._tf.scrollX;
  4484. }
  4485. changeScroll() {
  4486. var vShow = this._vScrollBar && this._tf.maxScrollY > 0;
  4487. var hShow = this._hScrollBar && this._tf.maxScrollX > 0;
  4488. var showWidth = vShow ? this._width - this._vScrollBar.width : this._width;
  4489. var showHeight = hShow ? this._height - this._hScrollBar.height : this._height;
  4490. var padding = this._tf.padding || Styles.labelPadding;
  4491. this._tf.width = showWidth;
  4492. this._tf.height = showHeight;
  4493. if (this._vScrollBar) {
  4494. this._vScrollBar.x = this._width - this._vScrollBar.width - padding[2];
  4495. this._vScrollBar.y = padding[1];
  4496. this._vScrollBar.height = this._height - (hShow ? this._hScrollBar.height : 0) - padding[1] - padding[3];
  4497. this._vScrollBar.scrollSize = 1;
  4498. this._vScrollBar.thumbPercent = showHeight / Math.max(this._tf.textHeight, showHeight);
  4499. this._vScrollBar.setScroll(1, this._tf.maxScrollY, this._tf.scrollY);
  4500. this._vScrollBar.visible = vShow;
  4501. }
  4502. if (this._hScrollBar) {
  4503. this._hScrollBar.x = padding[0];
  4504. this._hScrollBar.y = this._height - this._hScrollBar.height - padding[3];
  4505. this._hScrollBar.width = this._width - (vShow ? this._vScrollBar.width : 0) - padding[0] - padding[2];
  4506. this._hScrollBar.scrollSize = Math.max(showWidth * 0.033, 1);
  4507. this._hScrollBar.thumbPercent = showWidth / Math.max(this._tf.textWidth, showWidth);
  4508. this._hScrollBar.setScroll(0, this.maxScrollX, this.scrollX);
  4509. this._hScrollBar.visible = hShow;
  4510. }
  4511. }
  4512. scrollTo(y) {
  4513. this.commitMeasure();
  4514. this._tf.scrollY = y;
  4515. }
  4516. }
  4517. Laya.ILaya.regClass(TextArea);
  4518. Laya.ClassUtils.regClass("laya.ui.TextArea", TextArea);
  4519. Laya.ClassUtils.regClass("Laya.TextArea", TextArea);
  4520. class ScaleBox extends Box {
  4521. constructor() {
  4522. super(...arguments);
  4523. this._oldW = 0;
  4524. this._oldH = 0;
  4525. }
  4526. onEnable() {
  4527. Laya.ILaya.stage.on("resize", this, this.onResize);
  4528. this.onResize();
  4529. }
  4530. onDisable() {
  4531. Laya.ILaya.stage.off("resize", this, this.onResize);
  4532. }
  4533. onResize() {
  4534. let stage = Laya.ILaya.stage;
  4535. if (this.width > 0 && this.height > 0) {
  4536. var scale = Math.min(stage.width / this._oldW, stage.height / this._oldH);
  4537. super.width = stage.width;
  4538. super.height = stage.height;
  4539. this.scale(scale, scale);
  4540. }
  4541. }
  4542. set width(value) {
  4543. super.width = value;
  4544. this._oldW = value;
  4545. }
  4546. get width() {
  4547. return super.width;
  4548. }
  4549. set height(value) {
  4550. super.height = value;
  4551. this._oldH = value;
  4552. }
  4553. get height() {
  4554. return super.height;
  4555. }
  4556. }
  4557. Laya.ILaya.regClass(ScaleBox);
  4558. Laya.ClassUtils.regClass("laya.ui.ScaleBox", ScaleBox);
  4559. Laya.ClassUtils.regClass("Laya.ScaleBox", ScaleBox);
  4560. class HSlider extends Slider {
  4561. constructor(skin = null) {
  4562. super(skin);
  4563. this.isVertical = false;
  4564. }
  4565. }
  4566. Laya.ILaya.regClass(HSlider);
  4567. Laya.ClassUtils.regClass("laya.ui.HSlider", HSlider);
  4568. Laya.ClassUtils.regClass("Laya.HSlider", HSlider);
  4569. class Panel extends Box {
  4570. constructor() {
  4571. super();
  4572. this._usedCache = null;
  4573. this._elasticEnabled = false;
  4574. this.width = this.height = 100;
  4575. }
  4576. destroy(destroyChild = true) {
  4577. super.destroy(destroyChild);
  4578. this._content && this._content.destroy(destroyChild);
  4579. this._vScrollBar && this._vScrollBar.destroy(destroyChild);
  4580. this._hScrollBar && this._hScrollBar.destroy(destroyChild);
  4581. this._vScrollBar = null;
  4582. this._hScrollBar = null;
  4583. this._content = null;
  4584. }
  4585. destroyChildren() {
  4586. this._content.destroyChildren();
  4587. }
  4588. createChildren() {
  4589. super.addChild(this._content = new Box());
  4590. }
  4591. addChild(child) {
  4592. child.on(Laya.Event.RESIZE, this, this.onResize);
  4593. this._setScrollChanged();
  4594. return this._content.addChild(child);
  4595. }
  4596. onResize() {
  4597. this._setScrollChanged();
  4598. }
  4599. addChildAt(child, index) {
  4600. child.on(Laya.Event.RESIZE, this, this.onResize);
  4601. this._setScrollChanged();
  4602. return this._content.addChildAt(child, index);
  4603. }
  4604. removeChild(child) {
  4605. child.off(Laya.Event.RESIZE, this, this.onResize);
  4606. this._setScrollChanged();
  4607. return this._content.removeChild(child);
  4608. }
  4609. removeChildAt(index) {
  4610. this.getChildAt(index).off(Laya.Event.RESIZE, this, this.onResize);
  4611. this._setScrollChanged();
  4612. return this._content.removeChildAt(index);
  4613. }
  4614. removeChildren(beginIndex = 0, endIndex = 0x7fffffff) {
  4615. this._content.removeChildren(beginIndex, endIndex);
  4616. this._setScrollChanged();
  4617. return this;
  4618. }
  4619. getChildAt(index) {
  4620. return this._content.getChildAt(index);
  4621. }
  4622. getChildByName(name) {
  4623. return this._content.getChildByName(name);
  4624. }
  4625. getChildIndex(child) {
  4626. return this._content.getChildIndex(child);
  4627. }
  4628. get numChildren() {
  4629. return this._content.numChildren;
  4630. }
  4631. changeScroll() {
  4632. this._scrollChanged = false;
  4633. var contentW = this.contentWidth || 1;
  4634. var contentH = this.contentHeight || 1;
  4635. var vscroll = this._vScrollBar;
  4636. var hscroll = this._hScrollBar;
  4637. var vShow = vscroll && contentH > this._height;
  4638. var hShow = hscroll && contentW > this._width;
  4639. var showWidth = vShow ? this._width - vscroll.width : this._width;
  4640. var showHeight = hShow ? this._height - hscroll.height : this._height;
  4641. if (vscroll) {
  4642. vscroll.x = this._width - vscroll.width;
  4643. vscroll.y = 0;
  4644. vscroll.height = this._height - (hShow ? hscroll.height : 0);
  4645. vscroll.scrollSize = Math.max(this._height * 0.033, 1);
  4646. vscroll.thumbPercent = showHeight / contentH;
  4647. vscroll.setScroll(0, contentH - showHeight, vscroll.value);
  4648. }
  4649. if (hscroll) {
  4650. hscroll.x = 0;
  4651. hscroll.y = this._height - hscroll.height;
  4652. hscroll.width = this._width - (vShow ? vscroll.width : 0);
  4653. hscroll.scrollSize = Math.max(this._width * 0.033, 1);
  4654. hscroll.thumbPercent = showWidth / contentW;
  4655. hscroll.setScroll(0, contentW - showWidth, hscroll.value);
  4656. }
  4657. }
  4658. _sizeChanged() {
  4659. super._sizeChanged();
  4660. this.setContentSize(this._width, this._height);
  4661. }
  4662. get contentWidth() {
  4663. var max = 0;
  4664. for (var i = this._content.numChildren - 1; i > -1; i--) {
  4665. var comp = this._content.getChildAt(i);
  4666. max = Math.max(comp._x + comp.width * comp.scaleX - comp.pivotX, max);
  4667. }
  4668. return max;
  4669. }
  4670. get contentHeight() {
  4671. var max = 0;
  4672. for (var i = this._content.numChildren - 1; i > -1; i--) {
  4673. var comp = this._content.getChildAt(i);
  4674. max = Math.max(comp._y + comp.height * comp.scaleY - comp.pivotY, max);
  4675. }
  4676. return max;
  4677. }
  4678. setContentSize(width, height) {
  4679. var content = this._content;
  4680. content.width = width;
  4681. content.height = height;
  4682. content._style.scrollRect || (content.scrollRect = Laya.Rectangle.create());
  4683. content._style.scrollRect.setTo(0, 0, width, height);
  4684. content.scrollRect = content.scrollRect;
  4685. }
  4686. set width(value) {
  4687. super.width = value;
  4688. this._setScrollChanged();
  4689. }
  4690. get width() {
  4691. return super.width;
  4692. }
  4693. set height(value) {
  4694. super.height = value;
  4695. this._setScrollChanged();
  4696. }
  4697. get height() {
  4698. return super.height;
  4699. }
  4700. get vScrollBarSkin() {
  4701. return this._vScrollBar ? this._vScrollBar.skin : null;
  4702. }
  4703. set vScrollBarSkin(value) {
  4704. if (this._vScrollBar == null) {
  4705. super.addChild(this._vScrollBar = new VScrollBar());
  4706. this._vScrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange, [this._vScrollBar]);
  4707. this._vScrollBar.target = this._content;
  4708. this._vScrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  4709. this._setScrollChanged();
  4710. }
  4711. this._vScrollBar.skin = value;
  4712. }
  4713. get hScrollBarSkin() {
  4714. return this._hScrollBar ? this._hScrollBar.skin : null;
  4715. }
  4716. set hScrollBarSkin(value) {
  4717. if (this._hScrollBar == null) {
  4718. super.addChild(this._hScrollBar = new HScrollBar());
  4719. this._hScrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange, [this._hScrollBar]);
  4720. this._hScrollBar.target = this._content;
  4721. this._hScrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  4722. this._setScrollChanged();
  4723. }
  4724. this._hScrollBar.skin = value;
  4725. }
  4726. get vScrollBar() {
  4727. return this._vScrollBar;
  4728. }
  4729. get hScrollBar() {
  4730. return this._hScrollBar;
  4731. }
  4732. get content() {
  4733. return this._content;
  4734. }
  4735. onScrollBarChange(scrollBar) {
  4736. var rect = this._content._style.scrollRect;
  4737. if (rect) {
  4738. var start = Math.round(scrollBar.value);
  4739. scrollBar.isVertical ? rect.y = start : rect.x = start;
  4740. this._content.scrollRect = rect;
  4741. }
  4742. }
  4743. scrollTo(x = 0, y = 0) {
  4744. if (this.vScrollBar)
  4745. this.vScrollBar.value = y;
  4746. if (this.hScrollBar)
  4747. this.hScrollBar.value = x;
  4748. }
  4749. refresh() {
  4750. this.changeScroll();
  4751. }
  4752. set cacheAs(value) {
  4753. super.cacheAs = value;
  4754. this._usedCache = null;
  4755. if (value !== "none") {
  4756. this._hScrollBar && this._hScrollBar.on(Laya.Event.START, this, this.onScrollStart);
  4757. this._vScrollBar && this._vScrollBar.on(Laya.Event.START, this, this.onScrollStart);
  4758. }
  4759. else {
  4760. this._hScrollBar && this._hScrollBar.off(Laya.Event.START, this, this.onScrollStart);
  4761. this._vScrollBar && this._vScrollBar.off(Laya.Event.START, this, this.onScrollStart);
  4762. }
  4763. }
  4764. get cacheAs() {
  4765. return super.cacheAs;
  4766. }
  4767. get elasticEnabled() {
  4768. return this._elasticEnabled;
  4769. }
  4770. set elasticEnabled(value) {
  4771. this._elasticEnabled = value;
  4772. if (this._vScrollBar) {
  4773. this._vScrollBar.elasticDistance = value ? 200 : 0;
  4774. }
  4775. if (this._hScrollBar) {
  4776. this._hScrollBar.elasticDistance = value ? 200 : 0;
  4777. }
  4778. }
  4779. onScrollStart() {
  4780. this._usedCache || (this._usedCache = super.cacheAs);
  4781. super.cacheAs = "none";
  4782. this._hScrollBar && this._hScrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  4783. this._vScrollBar && this._vScrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  4784. }
  4785. onScrollEnd() {
  4786. super.cacheAs = this._usedCache;
  4787. }
  4788. _setScrollChanged() {
  4789. if (!this._scrollChanged) {
  4790. this._scrollChanged = true;
  4791. this.callLater(this.changeScroll);
  4792. }
  4793. }
  4794. }
  4795. Laya.ILaya.regClass(Panel);
  4796. Laya.ClassUtils.regClass("laya.ui.Panel", Panel);
  4797. Laya.ClassUtils.regClass("Laya.Panel", Panel);
  4798. class VSlider extends Slider {
  4799. }
  4800. Laya.ILaya.regClass(VSlider);
  4801. Laya.ClassUtils.regClass("laya.ui.VSlider", VSlider);
  4802. Laya.ClassUtils.regClass("Laya.VSlider", VSlider);
  4803. class Tree extends Box {
  4804. constructor() {
  4805. super();
  4806. this._spaceLeft = 10;
  4807. this._spaceBottom = 0;
  4808. this._keepStatus = true;
  4809. this.width = this.height = 200;
  4810. }
  4811. destroy(destroyChild = true) {
  4812. super.destroy(destroyChild);
  4813. this._list && this._list.destroy(destroyChild);
  4814. this._list = null;
  4815. this._source = null;
  4816. this._renderHandler = null;
  4817. }
  4818. createChildren() {
  4819. this.addChild(this._list = new List());
  4820. this._list.renderHandler = Laya.Handler.create(this, this.renderItem, null, false);
  4821. this._list.repeatX = 1;
  4822. this._list.on(Laya.Event.CHANGE, this, this.onListChange);
  4823. }
  4824. onListChange(e = null) {
  4825. this.event(Laya.Event.CHANGE);
  4826. }
  4827. get keepStatus() {
  4828. return this._keepStatus;
  4829. }
  4830. set keepStatus(value) {
  4831. this._keepStatus = value;
  4832. }
  4833. get array() {
  4834. return this._list.array;
  4835. }
  4836. set array(value) {
  4837. if (this._keepStatus && this._list.array && value) {
  4838. this.parseOpenStatus(this._list.array, value);
  4839. }
  4840. this._source = value;
  4841. this._list.array = this.getArray();
  4842. }
  4843. get source() {
  4844. return this._source;
  4845. }
  4846. get list() {
  4847. return this._list;
  4848. }
  4849. get itemRender() {
  4850. return this._list.itemRender;
  4851. }
  4852. set itemRender(value) {
  4853. this._list.itemRender = value;
  4854. }
  4855. get scrollBarSkin() {
  4856. return this._list.vScrollBarSkin;
  4857. }
  4858. set scrollBarSkin(value) {
  4859. this._list.vScrollBarSkin = value;
  4860. }
  4861. get scrollBar() {
  4862. return this._list.scrollBar;
  4863. }
  4864. get mouseHandler() {
  4865. return this._list.mouseHandler;
  4866. }
  4867. set mouseHandler(value) {
  4868. this._list.mouseHandler = value;
  4869. }
  4870. get renderHandler() {
  4871. return this._renderHandler;
  4872. }
  4873. set renderHandler(value) {
  4874. this._renderHandler = value;
  4875. }
  4876. get spaceLeft() {
  4877. return this._spaceLeft;
  4878. }
  4879. set spaceLeft(value) {
  4880. this._spaceLeft = value;
  4881. }
  4882. get spaceBottom() {
  4883. return this._list.spaceY;
  4884. }
  4885. set spaceBottom(value) {
  4886. this._list.spaceY = value;
  4887. }
  4888. get selectedIndex() {
  4889. return this._list.selectedIndex;
  4890. }
  4891. set selectedIndex(value) {
  4892. this._list.selectedIndex = value;
  4893. }
  4894. get selectedItem() {
  4895. return this._list.selectedItem;
  4896. }
  4897. set selectedItem(value) {
  4898. this._list.selectedItem = value;
  4899. }
  4900. set width(value) {
  4901. super.width = value;
  4902. this._list.width = value;
  4903. }
  4904. get width() {
  4905. return super.width;
  4906. }
  4907. set height(value) {
  4908. super.height = value;
  4909. this._list.height = value;
  4910. }
  4911. get height() {
  4912. return super.height;
  4913. }
  4914. getArray() {
  4915. var arr = [];
  4916. for (let key in this._source) {
  4917. let item = this._source[key];
  4918. if (this.getParentOpenStatus(item)) {
  4919. item.x = this._spaceLeft * this.getDepth(item);
  4920. arr.push(item);
  4921. }
  4922. }
  4923. return arr;
  4924. }
  4925. getDepth(item, num = 0) {
  4926. if (item.nodeParent == null)
  4927. return num;
  4928. else
  4929. return this.getDepth(item.nodeParent, num + 1);
  4930. }
  4931. getParentOpenStatus(item) {
  4932. var parent = item.nodeParent;
  4933. if (parent == null) {
  4934. return true;
  4935. }
  4936. else {
  4937. if (parent.isOpen) {
  4938. if (parent.nodeParent != null)
  4939. return this.getParentOpenStatus(parent);
  4940. else
  4941. return true;
  4942. }
  4943. else {
  4944. return false;
  4945. }
  4946. }
  4947. }
  4948. renderItem(cell, index) {
  4949. var item = cell.dataSource;
  4950. if (item) {
  4951. cell.left = item.x;
  4952. var arrow = cell.getChildByName("arrow");
  4953. if (arrow) {
  4954. if (item.hasChild) {
  4955. arrow.visible = true;
  4956. arrow.index = item.isOpen ? 1 : 0;
  4957. arrow.tag = index;
  4958. arrow.off(Laya.Event.CLICK, this, this.onArrowClick);
  4959. arrow.on(Laya.Event.CLICK, this, this.onArrowClick);
  4960. }
  4961. else {
  4962. arrow.visible = false;
  4963. }
  4964. }
  4965. var folder = cell.getChildByName("folder");
  4966. if (folder) {
  4967. if (folder.clipY == 2) {
  4968. folder.index = item.isDirectory ? 0 : 1;
  4969. }
  4970. else {
  4971. folder.index = item.isDirectory ? item.isOpen ? 1 : 0 : 2;
  4972. }
  4973. }
  4974. this._renderHandler && this._renderHandler.runWith([cell, index]);
  4975. }
  4976. }
  4977. onArrowClick(e) {
  4978. var arrow = e.currentTarget;
  4979. var index = arrow.tag;
  4980. this._list.array[index].isOpen = !this._list.array[index].isOpen;
  4981. this.event(Laya.Event.OPEN);
  4982. this._list.array = this.getArray();
  4983. }
  4984. setItemState(index, isOpen) {
  4985. if (!this._list.array[index])
  4986. return;
  4987. this._list.array[index].isOpen = isOpen;
  4988. this._list.array = this.getArray();
  4989. }
  4990. fresh() {
  4991. this._list.array = this.getArray();
  4992. this.repaint();
  4993. }
  4994. set dataSource(value) {
  4995. this._dataSource = value;
  4996. super.dataSource = value;
  4997. }
  4998. get dataSource() {
  4999. return super.dataSource;
  5000. }
  5001. set xml(value) {
  5002. var arr = [];
  5003. this.parseXml(value.childNodes[0], arr, null, true);
  5004. this.array = arr;
  5005. }
  5006. parseXml(xml, source, nodeParent, isRoot) {
  5007. var obj;
  5008. var list = xml.childNodes;
  5009. var childCount = list.length;
  5010. if (!isRoot) {
  5011. obj = {};
  5012. var list2 = xml.attributes;
  5013. for (let key in list2) {
  5014. var attrs = list2[key];
  5015. var prop = attrs.nodeName;
  5016. var value = attrs.nodeValue;
  5017. obj[prop] = value == "true" ? true : value == "false" ? false : value;
  5018. }
  5019. obj.nodeParent = nodeParent;
  5020. if (childCount > 0)
  5021. obj.isDirectory = true;
  5022. obj.hasChild = childCount > 0;
  5023. source.push(obj);
  5024. }
  5025. for (var i = 0; i < childCount; i++) {
  5026. var node = list[i];
  5027. this.parseXml(node, source, obj, false);
  5028. }
  5029. }
  5030. parseOpenStatus(oldSource, newSource) {
  5031. for (var i = 0, n = newSource.length; i < n; i++) {
  5032. var newItem = newSource[i];
  5033. if (newItem.isDirectory) {
  5034. for (var j = 0, m = oldSource.length; j < m; j++) {
  5035. var oldItem = oldSource[j];
  5036. if (oldItem.isDirectory && this.isSameParent(oldItem, newItem) && newItem.label == oldItem.label) {
  5037. newItem.isOpen = oldItem.isOpen;
  5038. break;
  5039. }
  5040. }
  5041. }
  5042. }
  5043. }
  5044. isSameParent(item1, item2) {
  5045. if (item1.nodeParent == null && item2.nodeParent == null)
  5046. return true;
  5047. else if (item1.nodeParent == null || item2.nodeParent == null)
  5048. return false;
  5049. else {
  5050. if (item1.nodeParent.label == item2.nodeParent.label)
  5051. return this.isSameParent(item1.nodeParent, item2.nodeParent);
  5052. else
  5053. return false;
  5054. }
  5055. }
  5056. get selectedPath() {
  5057. if (this._list.selectedItem) {
  5058. return this._list.selectedItem.path;
  5059. }
  5060. return null;
  5061. }
  5062. filter(key) {
  5063. if (Boolean(key)) {
  5064. var result = [];
  5065. this.getFilterSource(this._source, result, key);
  5066. this._list.array = result;
  5067. }
  5068. else {
  5069. this._list.array = this.getArray();
  5070. }
  5071. }
  5072. getFilterSource(array, result, key) {
  5073. key = key.toLocaleLowerCase();
  5074. for (let item of array) {
  5075. if (!item.isDirectory && String(item.label).toLowerCase().indexOf(key) > -1) {
  5076. item.x = 0;
  5077. result.push(item);
  5078. }
  5079. if (item.child && item.child.length > 0) {
  5080. this.getFilterSource(item.child, result, key);
  5081. }
  5082. }
  5083. }
  5084. }
  5085. Laya.ILaya.regClass(Tree);
  5086. Laya.ClassUtils.regClass("laya.ui.Tree", Tree);
  5087. Laya.ClassUtils.regClass("Laya.Tree", Tree);
  5088. class LayoutBox extends Box {
  5089. constructor() {
  5090. super(...arguments);
  5091. this._space = 0;
  5092. this._align = "none";
  5093. this._itemChanged = false;
  5094. }
  5095. addChild(child) {
  5096. child.on(Laya.Event.RESIZE, this, this.onResize);
  5097. this._setItemChanged();
  5098. return super.addChild(child);
  5099. }
  5100. onResize(e) {
  5101. this._setItemChanged();
  5102. }
  5103. addChildAt(child, index) {
  5104. child.on(Laya.Event.RESIZE, this, this.onResize);
  5105. this._setItemChanged();
  5106. return super.addChildAt(child, index);
  5107. }
  5108. removeChildAt(index) {
  5109. this.getChildAt(index).off(Laya.Event.RESIZE, this, this.onResize);
  5110. this._setItemChanged();
  5111. return super.removeChildAt(index);
  5112. }
  5113. refresh() {
  5114. this._setItemChanged();
  5115. }
  5116. changeItems() {
  5117. this._itemChanged = false;
  5118. }
  5119. get space() {
  5120. return this._space;
  5121. }
  5122. set space(value) {
  5123. this._space = value;
  5124. this._setItemChanged();
  5125. }
  5126. get align() {
  5127. return this._align;
  5128. }
  5129. set align(value) {
  5130. this._align = value;
  5131. this._setItemChanged();
  5132. }
  5133. sortItem(items) {
  5134. if (items)
  5135. items.sort(function (a, b) { return a.y - b.y; });
  5136. }
  5137. _setItemChanged() {
  5138. if (!this._itemChanged) {
  5139. this._itemChanged = true;
  5140. this.callLater(this.changeItems);
  5141. }
  5142. }
  5143. }
  5144. Laya.ILaya.regClass(LayoutBox);
  5145. Laya.ClassUtils.regClass("laya.ui.LayoutBox", LayoutBox);
  5146. Laya.ClassUtils.regClass("Laya.LayoutBox", LayoutBox);
  5147. class HBox extends LayoutBox {
  5148. sortItem(items) {
  5149. if (items)
  5150. items.sort(function (a, b) { return a.x - b.x; });
  5151. }
  5152. set height(value) {
  5153. if (this._height != value) {
  5154. super.height = value;
  5155. this.callLater(this.changeItems);
  5156. }
  5157. }
  5158. get height() {
  5159. return super.height;
  5160. }
  5161. changeItems() {
  5162. this._itemChanged = false;
  5163. var items = [];
  5164. var maxHeight = 0;
  5165. for (var i = 0, n = this.numChildren; i < n; i++) {
  5166. var item = this.getChildAt(i);
  5167. if (item) {
  5168. items.push(item);
  5169. maxHeight = this._height ? this._height : Math.max(maxHeight, item.height * item.scaleY);
  5170. }
  5171. }
  5172. this.sortItem(items);
  5173. var left = 0;
  5174. for (i = 0, n = items.length; i < n; i++) {
  5175. item = items[i];
  5176. item.x = left;
  5177. left += item.width * item.scaleX + this._space;
  5178. if (this._align == HBox.TOP) {
  5179. item.y = 0;
  5180. }
  5181. else if (this._align == HBox.MIDDLE) {
  5182. item.y = (maxHeight - item.height * item.scaleY) * 0.5;
  5183. }
  5184. else if (this._align == HBox.BOTTOM) {
  5185. item.y = maxHeight - item.height * item.scaleY;
  5186. }
  5187. }
  5188. this._sizeChanged();
  5189. }
  5190. }
  5191. HBox.NONE = "none";
  5192. HBox.TOP = "top";
  5193. HBox.MIDDLE = "middle";
  5194. HBox.BOTTOM = "bottom";
  5195. Laya.ILaya.regClass(HBox);
  5196. Laya.ClassUtils.regClass("laya.ui.HBox", HBox);
  5197. Laya.ClassUtils.regClass("Laya.HBox", HBox);
  5198. class VBox extends LayoutBox {
  5199. set width(value) {
  5200. if (this._width != value) {
  5201. super.width = value;
  5202. this.callLater(this.changeItems);
  5203. }
  5204. }
  5205. get width() {
  5206. return super.width;
  5207. }
  5208. changeItems() {
  5209. this._itemChanged = false;
  5210. var items = [];
  5211. var maxWidth = 0;
  5212. for (var i = 0, n = this.numChildren; i < n; i++) {
  5213. var item = this.getChildAt(i);
  5214. if (item) {
  5215. items.push(item);
  5216. maxWidth = this._width ? this._width : Math.max(maxWidth, item.width * item.scaleX);
  5217. }
  5218. }
  5219. this.sortItem(items);
  5220. var top = 0;
  5221. for (i = 0, n = items.length; i < n; i++) {
  5222. item = items[i];
  5223. item.y = top;
  5224. top += item.height * item.scaleY + this._space;
  5225. if (this._align == VBox.LEFT) {
  5226. item.x = 0;
  5227. }
  5228. else if (this._align == VBox.CENTER) {
  5229. item.x = (maxWidth - item.width * item.scaleX) * 0.5;
  5230. }
  5231. else if (this._align == VBox.RIGHT) {
  5232. item.x = maxWidth - item.width * item.scaleX;
  5233. }
  5234. }
  5235. this._sizeChanged();
  5236. }
  5237. }
  5238. VBox.NONE = "none";
  5239. VBox.LEFT = "left";
  5240. VBox.CENTER = "center";
  5241. VBox.RIGHT = "right";
  5242. Laya.ILaya.regClass(VBox);
  5243. Laya.ClassUtils.regClass("laya.ui.VBox", VBox);
  5244. Laya.ClassUtils.regClass("Laya.VBox", VBox);
  5245. class FontClip extends Clip {
  5246. constructor(skin = null, sheet = null) {
  5247. super();
  5248. this._valueArr = '';
  5249. this._indexMap = null;
  5250. this._sheet = null;
  5251. this._direction = "horizontal";
  5252. this._spaceX = 0;
  5253. this._spaceY = 0;
  5254. this._align = "left";
  5255. this._wordsW = 0;
  5256. this._wordsH = 0;
  5257. if (skin)
  5258. this.skin = skin;
  5259. if (sheet)
  5260. this.sheet = sheet;
  5261. }
  5262. createChildren() {
  5263. this._bitmap = new AutoBitmap();
  5264. this.on(Laya.Event.LOADED, this, this._onClipLoaded);
  5265. }
  5266. _onClipLoaded() {
  5267. this.callLater(this.changeValue);
  5268. }
  5269. get sheet() {
  5270. return this._sheet;
  5271. }
  5272. set sheet(value) {
  5273. value += "";
  5274. this._sheet = value;
  5275. var arr = value.split(" ");
  5276. this._clipX = String(arr[0]).length;
  5277. this.clipY = arr.length;
  5278. this._indexMap = {};
  5279. for (var i = 0; i < this._clipY; i++) {
  5280. var line = arr[i].split("");
  5281. for (var j = 0, n = line.length; j < n; j++) {
  5282. this._indexMap[line[j]] = i * this._clipX + j;
  5283. }
  5284. }
  5285. }
  5286. get value() {
  5287. if (!this._valueArr)
  5288. return "";
  5289. return this._valueArr;
  5290. }
  5291. set value(value) {
  5292. value += "";
  5293. this._valueArr = value;
  5294. this.callLater(this.changeValue);
  5295. }
  5296. get direction() {
  5297. return this._direction;
  5298. }
  5299. set direction(value) {
  5300. this._direction = value;
  5301. this.callLater(this.changeValue);
  5302. }
  5303. get spaceX() {
  5304. return this._spaceX;
  5305. }
  5306. set spaceX(value) {
  5307. this._spaceX = value;
  5308. if (this._direction === "horizontal")
  5309. this.callLater(this.changeValue);
  5310. }
  5311. get spaceY() {
  5312. return this._spaceY;
  5313. }
  5314. set spaceY(value) {
  5315. this._spaceY = value;
  5316. if (!(this._direction === "horizontal"))
  5317. this.callLater(this.changeValue);
  5318. }
  5319. set align(v) {
  5320. this._align = v;
  5321. this.callLater(this.changeValue);
  5322. }
  5323. get align() {
  5324. return this._align;
  5325. }
  5326. changeValue() {
  5327. if (!this._sources)
  5328. return;
  5329. if (!this._valueArr)
  5330. return;
  5331. this.graphics.clear(true);
  5332. var texture;
  5333. texture = this._sources[0];
  5334. if (!texture)
  5335. return;
  5336. var isHorizontal = (this._direction === "horizontal");
  5337. if (isHorizontal) {
  5338. this._wordsW = this._valueArr.length * (texture.sourceWidth + this.spaceX);
  5339. this._wordsH = texture.sourceHeight;
  5340. }
  5341. else {
  5342. this._wordsW = texture.sourceWidth;
  5343. this._wordsH = (texture.sourceHeight + this.spaceY) * this._valueArr.length;
  5344. }
  5345. var dX = 0;
  5346. if (this._width) {
  5347. switch (this._align) {
  5348. case "center":
  5349. dX = 0.5 * (this._width - this._wordsW);
  5350. break;
  5351. case "right":
  5352. dX = this._width - this._wordsW;
  5353. break;
  5354. default:
  5355. dX = 0;
  5356. }
  5357. }
  5358. for (var i = 0, sz = this._valueArr.length; i < sz; i++) {
  5359. var index = this._indexMap[this._valueArr.charAt(i)];
  5360. if (!this.sources[index])
  5361. continue;
  5362. texture = this.sources[index];
  5363. if (isHorizontal)
  5364. this.graphics.drawImage(texture, dX + i * (texture.sourceWidth + this.spaceX), 0, texture.sourceWidth, texture.sourceHeight);
  5365. else
  5366. this.graphics.drawImage(texture, 0 + dX, i * (texture.sourceHeight + this.spaceY), texture.sourceWidth, texture.sourceHeight);
  5367. }
  5368. if (!this._width) {
  5369. this._widget.resetLayoutX();
  5370. this.callLater(this._sizeChanged);
  5371. }
  5372. if (!this._height) {
  5373. this._widget.resetLayoutY();
  5374. this.callLater(this._sizeChanged);
  5375. }
  5376. }
  5377. set width(value) {
  5378. super.width = value;
  5379. this.callLater(this.changeValue);
  5380. }
  5381. get width() {
  5382. return super.width;
  5383. }
  5384. set height(value) {
  5385. super.height = value;
  5386. this.callLater(this.changeValue);
  5387. }
  5388. get height() {
  5389. return super.height;
  5390. }
  5391. measureWidth() {
  5392. return this._wordsW;
  5393. }
  5394. measureHeight() {
  5395. return this._wordsH;
  5396. }
  5397. destroy(destroyChild = true) {
  5398. this._valueArr = null;
  5399. this._indexMap = null;
  5400. this.graphics.clear(true);
  5401. this.removeSelf();
  5402. this.off(Laya.Event.LOADED, this, this._onClipLoaded);
  5403. super.destroy(destroyChild);
  5404. }
  5405. }
  5406. Laya.ILaya.regClass(FontClip);
  5407. Laya.ClassUtils.regClass("laya.ui.FontClip", FontClip);
  5408. Laya.ClassUtils.regClass("Laya.FontClip", FontClip);
  5409. class View extends Laya.Scene {
  5410. constructor() {
  5411. super(false);
  5412. this._watchMap = {};
  5413. this._anchorX = NaN;
  5414. this._anchorY = NaN;
  5415. this._widget = Widget.EMPTY;
  5416. this.createChildren();
  5417. }
  5418. static __init__() {
  5419. Laya.ILaya.ClassUtils.regShortClassName([ViewStack, Button, TextArea, ColorPicker, Box, ScaleBox, CheckBox, Clip, ComboBox, UIComponent,
  5420. HScrollBar, HSlider, Image, Label, List, Panel, ProgressBar, Radio, RadioGroup, ScrollBar, Slider, Tab, TextInput, View,
  5421. VScrollBar, VSlider, Tree, HBox, VBox, Laya.Animation, Laya.Text, FontClip]);
  5422. }
  5423. static regComponent(key, compClass) {
  5424. Laya.ILaya.ClassUtils.regClass(key, compClass);
  5425. }
  5426. static regViewRuntime(key, compClass) {
  5427. Laya.ILaya.ClassUtils.regClass(key, compClass);
  5428. }
  5429. static regUI(url, json) {
  5430. Laya.ILaya.loader.cacheRes(url, json);
  5431. }
  5432. destroy(destroyChild = true) {
  5433. this._watchMap = null;
  5434. super.destroy(destroyChild);
  5435. }
  5436. changeData(key) {
  5437. var arr = this._watchMap[key];
  5438. if (!arr)
  5439. return;
  5440. for (var i = 0, n = arr.length; i < n; i++) {
  5441. var watcher = arr[i];
  5442. watcher.exe(this);
  5443. }
  5444. }
  5445. get top() {
  5446. return this._widget.top;
  5447. }
  5448. set top(value) {
  5449. if (value != this._widget.top) {
  5450. this._getWidget().top = value;
  5451. }
  5452. }
  5453. get bottom() {
  5454. return this._widget.bottom;
  5455. }
  5456. set bottom(value) {
  5457. if (value != this._widget.bottom) {
  5458. this._getWidget().bottom = value;
  5459. }
  5460. }
  5461. get left() {
  5462. return this._widget.left;
  5463. }
  5464. set left(value) {
  5465. if (value != this._widget.left) {
  5466. this._getWidget().left = value;
  5467. }
  5468. }
  5469. get right() {
  5470. return this._widget.right;
  5471. }
  5472. set right(value) {
  5473. if (value != this._widget.right) {
  5474. this._getWidget().right = value;
  5475. }
  5476. }
  5477. get centerX() {
  5478. return this._widget.centerX;
  5479. }
  5480. set centerX(value) {
  5481. if (value != this._widget.centerX) {
  5482. this._getWidget().centerX = value;
  5483. }
  5484. }
  5485. get centerY() {
  5486. return this._widget.centerY;
  5487. }
  5488. set centerY(value) {
  5489. if (value != this._widget.centerY) {
  5490. this._getWidget().centerY = value;
  5491. }
  5492. }
  5493. get anchorX() {
  5494. return this._anchorX;
  5495. }
  5496. set anchorX(value) {
  5497. if (this._anchorX != value) {
  5498. this._anchorX = value;
  5499. this.callLater(this._sizeChanged);
  5500. }
  5501. }
  5502. get anchorY() {
  5503. return this._anchorY;
  5504. }
  5505. set anchorY(value) {
  5506. if (this._anchorY != value) {
  5507. this._anchorY = value;
  5508. this.callLater(this._sizeChanged);
  5509. }
  5510. }
  5511. _sizeChanged() {
  5512. if (!isNaN(this._anchorX))
  5513. this.pivotX = this.anchorX * this.width;
  5514. if (!isNaN(this._anchorY))
  5515. this.pivotY = this.anchorY * this.height;
  5516. this.event(Laya.Event.RESIZE);
  5517. }
  5518. _getWidget() {
  5519. this._widget === Widget.EMPTY && (this._widget = this.addComponent(Widget));
  5520. return this._widget;
  5521. }
  5522. loadUI(path) {
  5523. var uiView = View.uiMap[path];
  5524. View.uiMap && this.createView(uiView);
  5525. }
  5526. get dataSource() {
  5527. return this._dataSource;
  5528. }
  5529. set dataSource(value) {
  5530. this._dataSource = value;
  5531. for (var name in value) {
  5532. var comp = this.getChildByName(name);
  5533. if (comp instanceof UIComponent)
  5534. comp.dataSource = value[name];
  5535. else if (name in this && !(this[name] instanceof Function))
  5536. this[name] = value[name];
  5537. }
  5538. }
  5539. }
  5540. View.uiMap = {};
  5541. Laya.ILaya.regClass(View);
  5542. Laya.ClassUtils.regClass("laya.ui.View", View);
  5543. Laya.ClassUtils.regClass("Laya.View", View);
  5544. class IUI {
  5545. }
  5546. IUI.Dialog = null;
  5547. class DialogManager extends Laya.Sprite {
  5548. constructor() {
  5549. super();
  5550. this.maskLayer = new Laya.Sprite();
  5551. this.popupEffect = (dialog) => {
  5552. dialog.scale(1, 1);
  5553. dialog._effectTween = Laya.Tween.from(dialog, { x: Laya.ILaya.stage.width / 2, y: Laya.ILaya.stage.height / 2, scaleX: 0, scaleY: 0 }, 300, Laya.Ease.backOut, Laya.Handler.create(this, this.doOpen, [dialog]), 0, false, false);
  5554. };
  5555. this.closeEffect = (dialog) => {
  5556. dialog._effectTween = Laya.Tween.to(dialog, { x: Laya.ILaya.stage.width / 2, y: Laya.ILaya.stage.height / 2, scaleX: 0, scaleY: 0 }, 300, Laya.Ease.strongOut, Laya.Handler.create(this, this.doClose, [dialog]), 0, false, false);
  5557. };
  5558. this.popupEffectHandler = new Laya.Handler(this, this.popupEffect);
  5559. this.closeEffectHandler = new Laya.Handler(this, this.closeEffect);
  5560. this.mouseEnabled = this.maskLayer.mouseEnabled = true;
  5561. this.zOrder = 1000;
  5562. Laya.ILaya.stage.addChild(this);
  5563. Laya.ILaya.stage.on(Laya.Event.RESIZE, this, this._onResize);
  5564. if (UIConfig.closeDialogOnSide)
  5565. this.maskLayer.on("click", this, this._closeOnSide);
  5566. this._onResize(null);
  5567. }
  5568. _closeOnSide() {
  5569. var dialog = this.getChildAt(this.numChildren - 1);
  5570. if (dialog instanceof IUI.Dialog)
  5571. dialog.close();
  5572. }
  5573. setLockView(value) {
  5574. if (!this.lockLayer) {
  5575. this.lockLayer = new Box();
  5576. this.lockLayer.mouseEnabled = true;
  5577. this.lockLayer.size(Laya.ILaya.stage.width, Laya.ILaya.stage.height);
  5578. }
  5579. this.lockLayer.removeChildren();
  5580. if (value) {
  5581. value.centerX = value.centerY = 0;
  5582. this.lockLayer.addChild(value);
  5583. }
  5584. }
  5585. _onResize(e = null) {
  5586. var width = this.maskLayer.width = Laya.ILaya.stage.width;
  5587. var height = this.maskLayer.height = Laya.ILaya.stage.height;
  5588. if (this.lockLayer)
  5589. this.lockLayer.size(width, height);
  5590. this.maskLayer.graphics.clear(true);
  5591. this.maskLayer.graphics.drawRect(0, 0, width, height, UIConfig.popupBgColor);
  5592. this.maskLayer.alpha = UIConfig.popupBgAlpha;
  5593. for (var i = this.numChildren - 1; i > -1; i--) {
  5594. var item = this.getChildAt(i);
  5595. if (item.isPopupCenter)
  5596. this._centerDialog(item);
  5597. }
  5598. }
  5599. _centerDialog(dialog) {
  5600. dialog.x = Math.round(((Laya.ILaya.stage.width - dialog.width) >> 1) + dialog.pivotX);
  5601. dialog.y = Math.round(((Laya.ILaya.stage.height - dialog.height) >> 1) + dialog.pivotY);
  5602. }
  5603. open(dialog, closeOther = false, showEffect = false) {
  5604. if (closeOther)
  5605. this._closeAll();
  5606. this._clearDialogEffect(dialog);
  5607. if (dialog.isPopupCenter)
  5608. this._centerDialog(dialog);
  5609. this.addChild(dialog);
  5610. if (dialog.isModal || this._getBit(Laya.Const.HAS_ZORDER))
  5611. Laya.ILaya.timer.callLater(this, this._checkMask);
  5612. if (showEffect && dialog.popupEffect != null)
  5613. dialog.popupEffect.runWith(dialog);
  5614. else
  5615. this.doOpen(dialog);
  5616. this.event(Laya.Event.OPEN);
  5617. }
  5618. _clearDialogEffect(dialog) {
  5619. if (dialog._effectTween) {
  5620. Laya.Tween.clear(dialog._effectTween);
  5621. dialog._effectTween = null;
  5622. }
  5623. }
  5624. doOpen(dialog) {
  5625. dialog.onOpened(dialog._param);
  5626. }
  5627. lock(value) {
  5628. if (this.lockLayer) {
  5629. if (value)
  5630. this.addChild(this.lockLayer);
  5631. else
  5632. this.lockLayer.removeSelf();
  5633. }
  5634. }
  5635. close(dialog) {
  5636. this._clearDialogEffect(dialog);
  5637. if (dialog.isShowEffect && dialog.closeEffect != null)
  5638. dialog.closeEffect.runWith([dialog]);
  5639. else
  5640. this.doClose(dialog);
  5641. this.event(Laya.Event.CLOSE);
  5642. }
  5643. doClose(dialog) {
  5644. dialog.removeSelf();
  5645. dialog.isModal && this._checkMask();
  5646. dialog.closeHandler && dialog.closeHandler.runWith(dialog.closeType);
  5647. dialog.onClosed(dialog.closeType);
  5648. if (dialog.autoDestroyAtClosed)
  5649. dialog.destroy();
  5650. }
  5651. closeAll() {
  5652. this._closeAll();
  5653. this.event(Laya.Event.CLOSE);
  5654. }
  5655. _closeAll() {
  5656. for (var i = this.numChildren - 1; i > -1; i--) {
  5657. var item = this.getChildAt(i);
  5658. if (item && item.close != null) {
  5659. this.doClose(item);
  5660. }
  5661. }
  5662. }
  5663. getDialogsByGroup(group) {
  5664. var arr = [];
  5665. for (var i = this.numChildren - 1; i > -1; i--) {
  5666. var item = this.getChildAt(i);
  5667. if (item && item.group === group) {
  5668. arr.push(item);
  5669. }
  5670. }
  5671. return arr;
  5672. }
  5673. closeByGroup(group) {
  5674. var arr = [];
  5675. for (var i = this.numChildren - 1; i > -1; i--) {
  5676. var item = this.getChildAt(i);
  5677. if (item && item.group === group) {
  5678. item.close();
  5679. arr.push(item);
  5680. }
  5681. }
  5682. return arr;
  5683. }
  5684. _checkMask() {
  5685. this.maskLayer.removeSelf();
  5686. for (var i = this.numChildren - 1; i > -1; i--) {
  5687. var dialog = this.getChildAt(i);
  5688. if (dialog && dialog.isModal) {
  5689. this.addChildAt(this.maskLayer, i);
  5690. return;
  5691. }
  5692. }
  5693. }
  5694. }
  5695. Laya.ClassUtils.regClass("laya.ui.DialogManager", DialogManager);
  5696. Laya.ClassUtils.regClass("Laya.DialogManager", DialogManager);
  5697. class Dialog extends View {
  5698. constructor() {
  5699. super();
  5700. this.isShowEffect = true;
  5701. this.isPopupCenter = true;
  5702. this.popupEffect = Dialog.manager.popupEffectHandler;
  5703. this.closeEffect = Dialog.manager.closeEffectHandler;
  5704. this._dealDragArea();
  5705. this.on(Laya.Event.CLICK, this, this._onClick);
  5706. }
  5707. static get manager() {
  5708. return Dialog._manager = Dialog._manager || new DialogManager();
  5709. }
  5710. static set manager(value) {
  5711. Dialog._manager = value;
  5712. }
  5713. _dealDragArea() {
  5714. var dragTarget = this.getChildByName("drag");
  5715. if (dragTarget) {
  5716. this.dragArea = dragTarget._x + "," + dragTarget._y + "," + dragTarget.width + "," + dragTarget.height;
  5717. dragTarget.removeSelf();
  5718. }
  5719. }
  5720. get dragArea() {
  5721. if (this._dragArea)
  5722. return this._dragArea.toString();
  5723. return null;
  5724. }
  5725. set dragArea(value) {
  5726. if (value) {
  5727. var a = UIUtils.fillArray([0, 0, 0, 0], value, Number);
  5728. this._dragArea = new Laya.Rectangle(a[0], a[1], a[2], a[3]);
  5729. this.on(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);
  5730. }
  5731. else {
  5732. this._dragArea = null;
  5733. this.off(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);
  5734. }
  5735. }
  5736. _onMouseDown(e) {
  5737. var point = this.getMousePoint();
  5738. if (this._dragArea.contains(point.x, point.y))
  5739. this.startDrag();
  5740. else
  5741. this.stopDrag();
  5742. }
  5743. _onClick(e) {
  5744. var btn = e.target;
  5745. if (btn) {
  5746. switch (btn.name) {
  5747. case Dialog.CLOSE:
  5748. case Dialog.CANCEL:
  5749. case Dialog.SURE:
  5750. case Dialog.NO:
  5751. case Dialog.OK:
  5752. case Dialog.YES:
  5753. this.close(btn.name);
  5754. return;
  5755. }
  5756. }
  5757. }
  5758. open(closeOther = true, param = null) {
  5759. this._dealDragArea();
  5760. this._param = param;
  5761. Dialog.manager.open(this, closeOther, this.isShowEffect);
  5762. Dialog.manager.lock(false);
  5763. }
  5764. close(type = null) {
  5765. this.closeType = type;
  5766. Dialog.manager.close(this);
  5767. }
  5768. destroy(destroyChild = true) {
  5769. this.closeHandler = null;
  5770. this.popupEffect = null;
  5771. this.closeEffect = null;
  5772. this._dragArea = null;
  5773. super.destroy(destroyChild);
  5774. }
  5775. show(closeOther = false, showEffect = true) {
  5776. this._open(false, closeOther, showEffect);
  5777. }
  5778. popup(closeOther = false, showEffect = true) {
  5779. this._open(true, closeOther, showEffect);
  5780. }
  5781. _open(modal, closeOther, showEffect) {
  5782. this.isModal = modal;
  5783. this.isShowEffect = showEffect;
  5784. Dialog.manager.lock(true);
  5785. this.open(closeOther);
  5786. }
  5787. get isPopup() {
  5788. return this.parent != null;
  5789. }
  5790. set zOrder(value) {
  5791. super.zOrder = value;
  5792. Dialog.manager._checkMask();
  5793. }
  5794. get zOrder() {
  5795. return super.zOrder;
  5796. }
  5797. static setLockView(view) {
  5798. Dialog.manager.setLockView(view);
  5799. }
  5800. static lock(value) {
  5801. Dialog.manager.lock(value);
  5802. }
  5803. static closeAll() {
  5804. Dialog.manager.closeAll();
  5805. }
  5806. static getDialogsByGroup(group) {
  5807. return Dialog.manager.getDialogsByGroup(group);
  5808. }
  5809. static closeByGroup(group) {
  5810. return Dialog.manager.closeByGroup(group);
  5811. }
  5812. }
  5813. Dialog.CLOSE = "close";
  5814. Dialog.CANCEL = "cancel";
  5815. Dialog.SURE = "sure";
  5816. Dialog.NO = "no";
  5817. Dialog.YES = "yes";
  5818. Dialog.OK = "ok";
  5819. IUI.Dialog = Dialog;
  5820. Laya.ILaya.regClass(Dialog);
  5821. Laya.ClassUtils.regClass("laya.ui.Dialog", Dialog);
  5822. Laya.ClassUtils.regClass("Laya.Dialog", Dialog);
  5823. class TipManager extends UIComponent {
  5824. constructor() {
  5825. super();
  5826. this._tipBox = new UIComponent();
  5827. this._tipBox.addChild(this._tipText = new Laya.Text());
  5828. this._tipText.x = this._tipText.y = 5;
  5829. this._tipText.color = TipManager.tipTextColor;
  5830. this._defaultTipHandler = this._showDefaultTip;
  5831. Laya.ILaya.stage.on(UIEvent.SHOW_TIP, this, this._onStageShowTip);
  5832. Laya.ILaya.stage.on(UIEvent.HIDE_TIP, this, this._onStageHideTip);
  5833. this.zOrder = 1100;
  5834. }
  5835. _onStageHideTip(e) {
  5836. Laya.ILaya.timer.clear(this, this._showTip);
  5837. this.closeAll();
  5838. this.removeSelf();
  5839. }
  5840. _onStageShowTip(data) {
  5841. Laya.ILaya.timer.once(TipManager.tipDelay, this, this._showTip, [data], true);
  5842. }
  5843. _showTip(tip) {
  5844. if (typeof (tip) == 'string') {
  5845. var text = String(tip);
  5846. if (Boolean(text)) {
  5847. this._defaultTipHandler(text);
  5848. }
  5849. }
  5850. else if (tip instanceof Laya.Handler) {
  5851. tip.run();
  5852. }
  5853. else if (tip instanceof Function) {
  5854. tip.apply();
  5855. }
  5856. {
  5857. Laya.ILaya.stage.on(Laya.Event.MOUSE_MOVE, this, this._onStageMouseMove);
  5858. Laya.ILaya.stage.on(Laya.Event.MOUSE_DOWN, this, this._onStageMouseDown);
  5859. }
  5860. this._onStageMouseMove(null);
  5861. }
  5862. _onStageMouseDown(e) {
  5863. this.closeAll();
  5864. }
  5865. _onStageMouseMove(e) {
  5866. this._showToStage(this, TipManager.offsetX, TipManager.offsetY);
  5867. }
  5868. _showToStage(dis, offX = 0, offY = 0) {
  5869. var rec = dis.getBounds();
  5870. dis.x = Laya.ILaya.stage.mouseX + offX;
  5871. dis.y = Laya.ILaya.stage.mouseY + offY;
  5872. if (dis._x + rec.width > Laya.ILaya.stage.width) {
  5873. dis.x -= rec.width + offX;
  5874. }
  5875. if (dis._y + rec.height > Laya.ILaya.stage.height) {
  5876. dis.y -= rec.height + offY;
  5877. }
  5878. }
  5879. closeAll() {
  5880. Laya.ILaya.timer.clear(this, this._showTip);
  5881. Laya.ILaya.stage.off(Laya.Event.MOUSE_MOVE, this, this._onStageMouseMove);
  5882. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this._onStageMouseDown);
  5883. this.removeChildren();
  5884. }
  5885. showDislayTip(tip) {
  5886. this.addChild(tip);
  5887. this._showToStage(this);
  5888. Laya.ILaya.stage.addChild(this);
  5889. }
  5890. _showDefaultTip(text) {
  5891. this._tipText.text = text;
  5892. var g = this._tipBox.graphics;
  5893. g.clear(true);
  5894. g.drawRect(0, 0, this._tipText.width + 10, this._tipText.height + 10, TipManager.tipBackColor);
  5895. this.addChild(this._tipBox);
  5896. this._showToStage(this);
  5897. Laya.ILaya.stage.addChild(this);
  5898. }
  5899. get defaultTipHandler() {
  5900. return this._defaultTipHandler;
  5901. }
  5902. set defaultTipHandler(value) {
  5903. this._defaultTipHandler = value;
  5904. }
  5905. }
  5906. TipManager.offsetX = 10;
  5907. TipManager.offsetY = 15;
  5908. TipManager.tipTextColor = "#ffffff";
  5909. TipManager.tipBackColor = "#111111";
  5910. TipManager.tipDelay = 200;
  5911. Laya.ILaya.regClass(TipManager);
  5912. Laya.ClassUtils.regClass("laya.ui.TipManager", TipManager);
  5913. Laya.ClassUtils.regClass("Laya.TipManager", TipManager);
  5914. class UILib {
  5915. static __init__() {
  5916. }
  5917. }
  5918. class WXOpenDataViewer extends UIComponent {
  5919. constructor() {
  5920. super();
  5921. this._width = this._height = 200;
  5922. var tex = new Laya.Texture();
  5923. tex.bitmap = new Laya.Texture2D();
  5924. this.texture = tex;
  5925. }
  5926. onEnable() {
  5927. this.postMsg({ type: "display", rate: Laya.Laya.stage.frameRate });
  5928. if (window.wx && window.sharedCanvas)
  5929. Laya.Laya.timer.frameLoop(1, this, this._onLoop);
  5930. }
  5931. onDisable() {
  5932. this.postMsg({ type: "undisplay" });
  5933. Laya.Laya.timer.clear(this, this._onLoop);
  5934. }
  5935. _onLoop() {
  5936. let _canvas = window.sharedCanvas;
  5937. this.texture.sourceWidth = _canvas.width;
  5938. this.texture.sourceHeight = _canvas.height;
  5939. this.texture.bitmap.loadImageSource(_canvas);
  5940. }
  5941. set width(value) {
  5942. super.width = value;
  5943. if (window.sharedCanvas)
  5944. window.sharedCanvas.width = value;
  5945. this.callLater(this._postMsg);
  5946. }
  5947. get width() {
  5948. return super.width;
  5949. }
  5950. set height(value) {
  5951. super.height = value;
  5952. if (window.sharedCanvas)
  5953. window.sharedCanvas.height = value;
  5954. this.callLater(this._postMsg);
  5955. }
  5956. get height() {
  5957. return super.height;
  5958. }
  5959. set x(value) {
  5960. super.x = value;
  5961. this.callLater(this._postMsg);
  5962. }
  5963. get x() {
  5964. return super.x;
  5965. }
  5966. set y(value) {
  5967. super.y = value;
  5968. this.callLater(this._postMsg);
  5969. }
  5970. get y() {
  5971. return super.y;
  5972. }
  5973. _postMsg() {
  5974. var mat = new Laya.Matrix();
  5975. mat.translate(this.x, this.y);
  5976. var stage = Laya.Laya.stage;
  5977. mat.scale(stage._canvasTransform.getScaleX() * this.globalScaleX * stage.transform.getScaleX(), stage._canvasTransform.getScaleY() * this.globalScaleY * stage.transform.getScaleY());
  5978. this.postMsg({ type: "changeMatrix", a: mat.a, b: mat.b, c: mat.c, d: mat.d, tx: mat.tx, ty: mat.ty, w: this.width, h: this.height });
  5979. }
  5980. postMsg(msg) {
  5981. if (window.wx && window.wx.getOpenDataContext) {
  5982. var openDataContext = window.wx.getOpenDataContext();
  5983. openDataContext.postMessage(msg);
  5984. }
  5985. }
  5986. }
  5987. Laya.ILaya.regClass(WXOpenDataViewer);
  5988. Laya.ClassUtils.regClass("laya.ui.WXOpenDataViewer", WXOpenDataViewer);
  5989. Laya.ClassUtils.regClass("Laya.WXOpenDataViewer", WXOpenDataViewer);
  5990. exports.AdvImage = AdvImage;
  5991. exports.AutoBitmap = AutoBitmap;
  5992. exports.Box = Box;
  5993. exports.Button = Button;
  5994. exports.CheckBox = CheckBox;
  5995. exports.Clip = Clip;
  5996. exports.ColorPicker = ColorPicker;
  5997. exports.ComboBox = ComboBox;
  5998. exports.Dialog = Dialog;
  5999. exports.DialogManager = DialogManager;
  6000. exports.FontClip = FontClip;
  6001. exports.HBox = HBox;
  6002. exports.HScrollBar = HScrollBar;
  6003. exports.HSlider = HSlider;
  6004. exports.IUI = IUI;
  6005. exports.Image = Image;
  6006. exports.Label = Label;
  6007. exports.LayoutBox = LayoutBox;
  6008. exports.List = List;
  6009. exports.Panel = Panel;
  6010. exports.ProgressBar = ProgressBar;
  6011. exports.Radio = Radio;
  6012. exports.RadioGroup = RadioGroup;
  6013. exports.ScaleBox = ScaleBox;
  6014. exports.ScrollBar = ScrollBar;
  6015. exports.Slider = Slider;
  6016. exports.Styles = Styles;
  6017. exports.Tab = Tab;
  6018. exports.TextArea = TextArea;
  6019. exports.TextInput = TextInput;
  6020. exports.TipManager = TipManager;
  6021. exports.Tree = Tree;
  6022. exports.UIComponent = UIComponent;
  6023. exports.UIConfig = UIConfig;
  6024. exports.UIEvent = UIEvent;
  6025. exports.UIGroup = UIGroup;
  6026. exports.UILib = UILib;
  6027. exports.UIUtils = UIUtils;
  6028. exports.VBox = VBox;
  6029. exports.VScrollBar = VScrollBar;
  6030. exports.VSlider = VSlider;
  6031. exports.View = View;
  6032. exports.ViewStack = ViewStack;
  6033. exports.WXOpenDataViewer = WXOpenDataViewer;
  6034. exports.Widget = Widget;
  6035. }(window.Laya = window.Laya || {}, Laya));