laya.ui.js 192 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073
  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(true);
  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() {
  1567. super(...arguments);
  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. }
  1576. destroy(destroyChild = true) {
  1577. super.destroy(destroyChild);
  1578. this._colorPanel && this._colorPanel.destroy(destroyChild);
  1579. this._colorButton && this._colorButton.destroy(destroyChild);
  1580. this._colorPanel = null;
  1581. this._colorTiles = null;
  1582. this._colorBlock = null;
  1583. this._colorInput = null;
  1584. this._colorButton = null;
  1585. this._colors = null;
  1586. this.changeHandler = null;
  1587. }
  1588. createChildren() {
  1589. this.addChild(this._colorButton = new Button());
  1590. this._colorPanel = new Box();
  1591. this._colorPanel.size(230, 166);
  1592. this._colorPanel.addChild(this._colorTiles = new Laya.Sprite());
  1593. this._colorPanel.addChild(this._colorBlock = new Laya.Sprite());
  1594. this._colorPanel.addChild(this._colorInput = new Laya.Input());
  1595. }
  1596. initialize() {
  1597. this._colorButton.on(Laya.Event.CLICK, this, this.onColorButtonClick);
  1598. this._colorBlock.pos(5, 5);
  1599. this._colorInput.pos(60, 5);
  1600. this._colorInput.size(60, 20);
  1601. this._colorInput.on(Laya.Event.CHANGE, this, this.onColorInputChange);
  1602. this._colorInput.on(Laya.Event.KEY_DOWN, this, this.onColorFieldKeyDown);
  1603. this._colorTiles.pos(5, 30);
  1604. this._colorTiles.on(Laya.Event.MOUSE_MOVE, this, this.onColorTilesMouseMove);
  1605. this._colorTiles.on(Laya.Event.CLICK, this, this.onColorTilesClick);
  1606. this._colorTiles.size(20 * this._gridSize, 12 * this._gridSize);
  1607. this._colorPanel.on(Laya.Event.MOUSE_DOWN, this, this.onPanelMouseDown);
  1608. this.bgColor = this._bgColor;
  1609. }
  1610. onPanelMouseDown(e) {
  1611. e.stopPropagation();
  1612. }
  1613. changePanel() {
  1614. this._panelChanged = false;
  1615. var g = this._colorPanel.graphics;
  1616. g.clear(true);
  1617. g.drawRect(0, 0, 230, 166, this._bgColor, this._borderColor);
  1618. this.drawBlock(this._selectedColor);
  1619. this._colorInput.borderColor = this._borderColor;
  1620. this._colorInput.bgColor = this._inputBgColor;
  1621. this._colorInput.color = this._inputColor;
  1622. g = this._colorTiles.graphics;
  1623. g.clear(true);
  1624. var mainColors = [0x000000, 0x333333, 0x666666, 0x999999, 0xCCCCCC, 0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0x00FFFF, 0xFF00FF];
  1625. for (var i = 0; i < 12; i++) {
  1626. for (var j = 0; j < 20; j++) {
  1627. var color;
  1628. if (j === 0)
  1629. color = mainColors[i];
  1630. else if (j === 1)
  1631. color = 0x000000;
  1632. else
  1633. color = (((i * 3 + j / 6) % 3 << 0) + ((i / 6) << 0) * 3) * 0x33 << 16 | j % 6 * 0x33 << 8 | (i << 0) % 6 * 0x33;
  1634. var strColor = UIUtils.toColor(color);
  1635. this._colors.push(strColor);
  1636. var x = j * this._gridSize;
  1637. var y = i * this._gridSize;
  1638. g.drawRect(x, y, this._gridSize, this._gridSize, strColor, "#000000");
  1639. }
  1640. }
  1641. }
  1642. onColorButtonClick(e) {
  1643. if (this._colorPanel.parent)
  1644. this.close();
  1645. else
  1646. this.open();
  1647. }
  1648. open() {
  1649. let stage = Laya.ILaya.stage;
  1650. var p = this.localToGlobal(new Laya.Point());
  1651. var px = p.x + this._colorPanel.width <= stage.width ? p.x : stage.width - this._colorPanel.width;
  1652. var py = p.y + this._colorButton.height;
  1653. py = py + this._colorPanel.height <= stage.height ? py : p.y - this._colorPanel.height;
  1654. this._colorPanel.pos(px, py);
  1655. this._colorPanel.zOrder = 1001;
  1656. stage.addChild(this._colorPanel);
  1657. stage.on(Laya.Event.MOUSE_DOWN, this, this.removeColorBox);
  1658. }
  1659. close() {
  1660. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this.removeColorBox);
  1661. this._colorPanel.removeSelf();
  1662. }
  1663. removeColorBox(e = null) {
  1664. this.close();
  1665. }
  1666. onColorFieldKeyDown(e) {
  1667. if (e.keyCode == 13) {
  1668. if (this._colorInput.text)
  1669. this.selectedColor = this._colorInput.text;
  1670. else
  1671. this.selectedColor = null;
  1672. this.close();
  1673. e.stopPropagation();
  1674. }
  1675. }
  1676. onColorInputChange(e = null) {
  1677. if (this._colorInput.text)
  1678. this.drawBlock(this._colorInput.text);
  1679. else
  1680. this.drawBlock("#FFFFFF");
  1681. }
  1682. onColorTilesClick(e) {
  1683. this.selectedColor = this.getColorByMouse();
  1684. this.close();
  1685. }
  1686. onColorTilesMouseMove(e) {
  1687. this._colorInput.focus = false;
  1688. var color = this.getColorByMouse();
  1689. this._colorInput.text = color;
  1690. this.drawBlock(color);
  1691. }
  1692. getColorByMouse() {
  1693. var point = this._colorTiles.getMousePoint();
  1694. var x = Math.floor(point.x / this._gridSize);
  1695. var y = Math.floor(point.y / this._gridSize);
  1696. return this._colors[y * 20 + x];
  1697. }
  1698. drawBlock(color) {
  1699. var g = this._colorBlock.graphics;
  1700. g.clear(true);
  1701. var showColor = color ? color : "#ffffff";
  1702. g.drawRect(0, 0, 50, 20, showColor, this._borderColor);
  1703. color || g.drawLine(0, 0, 50, 20, "#ff0000");
  1704. }
  1705. get selectedColor() {
  1706. return this._selectedColor;
  1707. }
  1708. set selectedColor(value) {
  1709. if (this._selectedColor != value) {
  1710. this._selectedColor = this._colorInput.text = value;
  1711. this.drawBlock(value);
  1712. this.changeColor();
  1713. this.changeHandler && this.changeHandler.runWith(this._selectedColor);
  1714. this.event(Laya.Event.CHANGE, Laya.Event.EMPTY.setTo(Laya.Event.CHANGE, this, this));
  1715. }
  1716. }
  1717. get skin() {
  1718. return this._colorButton.skin;
  1719. }
  1720. set skin(value) {
  1721. this._colorButton.once(Laya.Event.LOADED, this, this.changeColor);
  1722. this._colorButton.skin = value;
  1723. }
  1724. changeColor() {
  1725. var g = this.graphics;
  1726. g.clear(true);
  1727. var showColor = this._selectedColor || "#000000";
  1728. g.drawRect(0, 0, this._colorButton.width, this._colorButton.height, showColor);
  1729. }
  1730. get bgColor() {
  1731. return this._bgColor;
  1732. }
  1733. set bgColor(value) {
  1734. this._bgColor = value;
  1735. this._setPanelChanged();
  1736. }
  1737. get borderColor() {
  1738. return this._borderColor;
  1739. }
  1740. set borderColor(value) {
  1741. this._borderColor = value;
  1742. this._setPanelChanged();
  1743. }
  1744. get inputColor() {
  1745. return this._inputColor;
  1746. }
  1747. set inputColor(value) {
  1748. this._inputColor = value;
  1749. this._setPanelChanged();
  1750. }
  1751. get inputBgColor() {
  1752. return this._inputBgColor;
  1753. }
  1754. set inputBgColor(value) {
  1755. this._inputBgColor = value;
  1756. this._setPanelChanged();
  1757. }
  1758. _setPanelChanged() {
  1759. if (!this._panelChanged) {
  1760. this._panelChanged = true;
  1761. this.callLater(this.changePanel);
  1762. }
  1763. }
  1764. }
  1765. Laya.ILaya.regClass(ColorPicker);
  1766. Laya.ClassUtils.regClass("laya.ui.ColorPicker", ColorPicker);
  1767. Laya.ClassUtils.regClass("Laya.ColorPicker", ColorPicker);
  1768. class Label extends UIComponent {
  1769. constructor(text = "") {
  1770. super();
  1771. this.text = text;
  1772. }
  1773. destroy(destroyChild = true) {
  1774. super.destroy(destroyChild);
  1775. this._tf = null;
  1776. }
  1777. createChildren() {
  1778. this.addChild(this._tf = new Laya.Text());
  1779. }
  1780. get text() {
  1781. return this._tf.text;
  1782. }
  1783. set text(value) {
  1784. if (this._tf.text != value) {
  1785. if (value)
  1786. value = UIUtils.adptString(value + "");
  1787. this._tf.text = value;
  1788. this.event(Laya.Event.CHANGE);
  1789. if (!this._width || !this._height)
  1790. this.onCompResize();
  1791. }
  1792. }
  1793. changeText(text) {
  1794. this._tf.changeText(text);
  1795. }
  1796. get wordWrap() {
  1797. return this._tf.wordWrap;
  1798. }
  1799. set wordWrap(value) {
  1800. this._tf.wordWrap = value;
  1801. }
  1802. get color() {
  1803. return this._tf.color;
  1804. }
  1805. set color(value) {
  1806. this._tf.color = value;
  1807. }
  1808. get font() {
  1809. return this._tf.font;
  1810. }
  1811. set font(value) {
  1812. this._tf.font = value;
  1813. }
  1814. get align() {
  1815. return this._tf.align;
  1816. }
  1817. set align(value) {
  1818. this._tf.align = value;
  1819. }
  1820. get valign() {
  1821. return this._tf.valign;
  1822. }
  1823. set valign(value) {
  1824. this._tf.valign = value;
  1825. }
  1826. get bold() {
  1827. return this._tf.bold;
  1828. }
  1829. set bold(value) {
  1830. this._tf.bold = value;
  1831. }
  1832. get italic() {
  1833. return this._tf.italic;
  1834. }
  1835. set italic(value) {
  1836. this._tf.italic = value;
  1837. }
  1838. get leading() {
  1839. return this._tf.leading;
  1840. }
  1841. set leading(value) {
  1842. this._tf.leading = value;
  1843. }
  1844. get fontSize() {
  1845. return this._tf.fontSize;
  1846. }
  1847. set fontSize(value) {
  1848. this._tf.fontSize = value;
  1849. }
  1850. get padding() {
  1851. return this._tf.padding.join(",");
  1852. }
  1853. set padding(value) {
  1854. this._tf.padding = UIUtils.fillArray(Styles.labelPadding, value, Number);
  1855. }
  1856. get bgColor() {
  1857. return this._tf.bgColor;
  1858. }
  1859. set bgColor(value) {
  1860. this._tf.bgColor = value;
  1861. }
  1862. get borderColor() {
  1863. return this._tf.borderColor;
  1864. }
  1865. set borderColor(value) {
  1866. this._tf.borderColor = value;
  1867. }
  1868. get stroke() {
  1869. return this._tf.stroke;
  1870. }
  1871. set stroke(value) {
  1872. this._tf.stroke = value;
  1873. }
  1874. get strokeColor() {
  1875. return this._tf.strokeColor;
  1876. }
  1877. set strokeColor(value) {
  1878. this._tf.strokeColor = value;
  1879. }
  1880. get textField() {
  1881. return this._tf;
  1882. }
  1883. measureWidth() {
  1884. return this._tf.width;
  1885. }
  1886. measureHeight() {
  1887. return this._tf.height;
  1888. }
  1889. get width() {
  1890. if (this._width || this._tf.text)
  1891. return super.width;
  1892. return 0;
  1893. }
  1894. set width(value) {
  1895. super.width = value;
  1896. this._tf.width = value;
  1897. }
  1898. get height() {
  1899. if (this._height || this._tf.text)
  1900. return super.height;
  1901. return 0;
  1902. }
  1903. set height(value) {
  1904. super.height = value;
  1905. this._tf.height = value;
  1906. }
  1907. set dataSource(value) {
  1908. this._dataSource = value;
  1909. if (typeof (value) == 'number' || typeof (value) == 'string')
  1910. this.text = value + "";
  1911. else
  1912. super.dataSource = value;
  1913. }
  1914. get dataSource() {
  1915. return super.dataSource;
  1916. }
  1917. get overflow() {
  1918. return this._tf.overflow;
  1919. }
  1920. set overflow(value) {
  1921. this._tf.overflow = value;
  1922. }
  1923. get underline() {
  1924. return this._tf.underline;
  1925. }
  1926. set underline(value) {
  1927. this._tf.underline = value;
  1928. }
  1929. get underlineColor() {
  1930. return this._tf.underlineColor;
  1931. }
  1932. set underlineColor(value) {
  1933. this._tf.underlineColor = value;
  1934. }
  1935. }
  1936. Laya.ILaya.regClass(Label);
  1937. Laya.ClassUtils.regClass("laya.ui.Label", Label);
  1938. Laya.ClassUtils.regClass("Laya.Label", Label);
  1939. class Slider extends UIComponent {
  1940. constructor(skin = null) {
  1941. super();
  1942. this.isVertical = true;
  1943. this.showLabel = true;
  1944. this._max = 100;
  1945. this._min = 0;
  1946. this._tick = 1;
  1947. this._value = 0;
  1948. if (!Slider.label) {
  1949. Slider.label = new Label();
  1950. }
  1951. this.skin = skin;
  1952. }
  1953. destroy(destroyChild = true) {
  1954. super.destroy(destroyChild);
  1955. this._bg && this._bg.destroy(destroyChild);
  1956. this._bar && this._bar.destroy(destroyChild);
  1957. this._progress && this._progress.destroy(destroyChild);
  1958. this._bg = null;
  1959. this._bar = null;
  1960. this._progress = null;
  1961. this.changeHandler = null;
  1962. }
  1963. createChildren() {
  1964. this.addChild(this._bg = new Image());
  1965. this.addChild(this._bar = new Button());
  1966. }
  1967. initialize() {
  1968. this._bar.on(Laya.Event.MOUSE_DOWN, this, this.onBarMouseDown);
  1969. this._bg.sizeGrid = this._bar.sizeGrid = "4,4,4,4,0";
  1970. if (this._progress)
  1971. this._progress.sizeGrid = this._bar.sizeGrid;
  1972. this.allowClickBack = true;
  1973. }
  1974. onBarMouseDown(e) {
  1975. var Laya$1 = Laya.ILaya;
  1976. this._globalSacle || (this._globalSacle = new Laya.Point());
  1977. this._globalSacle.setTo(this.globalScaleX || 0.01, this.globalScaleY || 0.01);
  1978. this._maxMove = this.isVertical ? (this.height - this._bar.height) : (this.width - this._bar.width);
  1979. this._tx = Laya$1.stage.mouseX;
  1980. this._ty = Laya$1.stage.mouseY;
  1981. Laya$1.stage.on(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
  1982. Laya$1.stage.once(Laya.Event.MOUSE_UP, this, this.mouseUp);
  1983. Laya$1.stage.once(Laya.Event.MOUSE_OUT, this, this.mouseUp);
  1984. this.showValueText();
  1985. }
  1986. showValueText() {
  1987. if (this.showLabel) {
  1988. var label = Slider.label;
  1989. this.addChild(label);
  1990. label.textField.changeText(this._value + "");
  1991. if (this.isVertical) {
  1992. label.x = this._bar._x + 20;
  1993. label.y = (this._bar.height - label.height) * 0.5 + this._bar._y;
  1994. }
  1995. else {
  1996. label.y = this._bar._y - 20;
  1997. label.x = (this._bar.width - label.width) * 0.5 + this._bar._x;
  1998. }
  1999. }
  2000. }
  2001. hideValueText() {
  2002. Slider.label && Slider.label.removeSelf();
  2003. }
  2004. mouseUp(e) {
  2005. let stage = Laya.ILaya.stage;
  2006. stage.off(Laya.Event.MOUSE_MOVE, this, this.mouseMove);
  2007. stage.off(Laya.Event.MOUSE_UP, this, this.mouseUp);
  2008. stage.off(Laya.Event.MOUSE_OUT, this, this.mouseUp);
  2009. this.sendChangeEvent(Laya.Event.CHANGED);
  2010. this.hideValueText();
  2011. }
  2012. mouseMove(e) {
  2013. let stage = Laya.ILaya.stage;
  2014. var oldValue = this._value;
  2015. if (this.isVertical) {
  2016. this._bar.y += (stage.mouseY - this._ty) / this._globalSacle.y;
  2017. if (this._bar._y > this._maxMove)
  2018. this._bar.y = this._maxMove;
  2019. else if (this._bar._y < 0)
  2020. this._bar.y = 0;
  2021. this._value = this._bar._y / this._maxMove * (this._max - this._min) + this._min;
  2022. if (this._progress)
  2023. this._progress.height = this._bar._y + 0.5 * this._bar.height;
  2024. }
  2025. else {
  2026. this._bar.x += (stage.mouseX - this._tx) / this._globalSacle.x;
  2027. if (this._bar._x > this._maxMove)
  2028. this._bar.x = this._maxMove;
  2029. else if (this._bar._x < 0)
  2030. this._bar.x = 0;
  2031. this._value = this._bar._x / this._maxMove * (this._max - this._min) + this._min;
  2032. if (this._progress)
  2033. this._progress.width = this._bar._x + 0.5 * this._bar.width;
  2034. }
  2035. this._tx = stage.mouseX;
  2036. this._ty = stage.mouseY;
  2037. if (this._tick != 0) {
  2038. var pow = Math.pow(10, (this._tick + "").length - 1);
  2039. this._value = Math.round(Math.round(this._value / this._tick) * this._tick * pow) / pow;
  2040. }
  2041. if (this._value != oldValue) {
  2042. this.sendChangeEvent();
  2043. }
  2044. this.showValueText();
  2045. }
  2046. sendChangeEvent(type = Laya.Event.CHANGE) {
  2047. this.event(type);
  2048. this.changeHandler && this.changeHandler.runWith(this._value);
  2049. }
  2050. get skin() {
  2051. return this._skin;
  2052. }
  2053. set skin(value) {
  2054. if (this._skin != value) {
  2055. this._skin = value;
  2056. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  2057. Laya.ILaya.loader.load([this._skin, this._skin.replace(".png", "$bar.png")], Laya.Handler.create(this, this._skinLoaded));
  2058. }
  2059. else {
  2060. this._skinLoaded();
  2061. }
  2062. }
  2063. }
  2064. _skinLoaded() {
  2065. this._bg.skin = this._skin;
  2066. this._bar.skin = this._skin.replace(".png", "$bar.png");
  2067. var progressSkin = this._skin.replace(".png", "$progress.png");
  2068. if (Laya.Loader.getRes(progressSkin)) {
  2069. if (!this._progress) {
  2070. this.addChild(this._progress = new Image());
  2071. this._progress.sizeGrid = this._bar.sizeGrid;
  2072. this.setChildIndex(this._progress, 1);
  2073. }
  2074. this._progress.skin = progressSkin;
  2075. }
  2076. this.setBarPoint();
  2077. this.callLater(this.changeValue);
  2078. this._sizeChanged();
  2079. this.event(Laya.Event.LOADED);
  2080. }
  2081. setBarPoint() {
  2082. if (this.isVertical)
  2083. this._bar.x = Math.round((this._bg.width - this._bar.width) * 0.5);
  2084. else
  2085. this._bar.y = Math.round((this._bg.height - this._bar.height) * 0.5);
  2086. }
  2087. measureWidth() {
  2088. return Math.max(this._bg.width, this._bar.width);
  2089. }
  2090. measureHeight() {
  2091. return Math.max(this._bg.height, this._bar.height);
  2092. }
  2093. _sizeChanged() {
  2094. super._sizeChanged();
  2095. if (this.isVertical)
  2096. this._bg.height = this.height;
  2097. else
  2098. this._bg.width = this.width;
  2099. this.setBarPoint();
  2100. this.changeValue();
  2101. }
  2102. get sizeGrid() {
  2103. return this._bg.sizeGrid;
  2104. }
  2105. set sizeGrid(value) {
  2106. this._bg.sizeGrid = value;
  2107. this._bar.sizeGrid = value;
  2108. if (this._progress)
  2109. this._progress.sizeGrid = this._bar.sizeGrid;
  2110. }
  2111. setSlider(min, max, value) {
  2112. this._value = -1;
  2113. this._min = min;
  2114. this._max = max > min ? max : min;
  2115. this.value = value < min ? min : value > max ? max : value;
  2116. }
  2117. get tick() {
  2118. return this._tick;
  2119. }
  2120. set tick(value) {
  2121. if (this._tick != value) {
  2122. this._tick = value;
  2123. this.callLater(this.changeValue);
  2124. }
  2125. }
  2126. changeValue() {
  2127. if (this.tick != 0) {
  2128. var pow = Math.pow(10, (this._tick + "").length - 1);
  2129. this._value = Math.round(Math.round(this._value / this._tick) * this._tick * pow) / pow;
  2130. }
  2131. this._value = this._value > this._max ? this._max : this._value < this._min ? this._min : this._value;
  2132. var num = this._max - this._min;
  2133. if (num === 0)
  2134. num = 1;
  2135. if (this.isVertical) {
  2136. this._bar.y = (this._value - this._min) / num * (this.height - this._bar.height);
  2137. if (this._progress)
  2138. this._progress.height = this._bar._y + 0.5 * this._bar.height;
  2139. }
  2140. else {
  2141. this._bar.x = (this._value - this._min) / num * (this.width - this._bar.width);
  2142. if (this._progress)
  2143. this._progress.width = this._bar._x + 0.5 * this._bar.width;
  2144. }
  2145. }
  2146. get max() {
  2147. return this._max;
  2148. }
  2149. set max(value) {
  2150. if (this._max != value) {
  2151. this._max = value;
  2152. this.callLater(this.changeValue);
  2153. }
  2154. }
  2155. get min() {
  2156. return this._min;
  2157. }
  2158. set min(value) {
  2159. if (this._min != value) {
  2160. this._min = value;
  2161. this.callLater(this.changeValue);
  2162. }
  2163. }
  2164. get value() {
  2165. return this._value;
  2166. }
  2167. set value(num) {
  2168. if (this._value != num) {
  2169. var oldValue = this._value;
  2170. this._value = num;
  2171. this.changeValue();
  2172. if (this._value != oldValue) {
  2173. this.sendChangeEvent();
  2174. }
  2175. }
  2176. }
  2177. get allowClickBack() {
  2178. return this._allowClickBack;
  2179. }
  2180. set allowClickBack(value) {
  2181. if (this._allowClickBack != value) {
  2182. this._allowClickBack = value;
  2183. if (value)
  2184. this._bg.on(Laya.Event.MOUSE_DOWN, this, this.onBgMouseDown);
  2185. else
  2186. this._bg.off(Laya.Event.MOUSE_DOWN, this, this.onBgMouseDown);
  2187. }
  2188. }
  2189. onBgMouseDown(e) {
  2190. var point = this._bg.getMousePoint();
  2191. if (this.isVertical)
  2192. this.value = point.y / (this.height - this._bar.height) * (this._max - this._min) + this._min;
  2193. else
  2194. this.value = point.x / (this.width - this._bar.width) * (this._max - this._min) + this._min;
  2195. }
  2196. set dataSource(value) {
  2197. this._dataSource = value;
  2198. if (typeof (value) == 'number' || typeof (value) == 'string')
  2199. this.value = Number(value);
  2200. else
  2201. super.dataSource = value;
  2202. }
  2203. get dataSource() {
  2204. return super.dataSource;
  2205. }
  2206. get bar() {
  2207. return this._bar;
  2208. }
  2209. }
  2210. Slider.label = null;
  2211. Laya.ILaya.regClass(Slider);
  2212. Laya.ClassUtils.regClass("laya.ui.Slider", Slider);
  2213. Laya.ClassUtils.regClass("Laya.Slider", Slider);
  2214. class ScrollBar extends UIComponent {
  2215. constructor(skin = null) {
  2216. super();
  2217. this.rollRatio = 0.97;
  2218. this.scaleBar = true;
  2219. this.autoHide = false;
  2220. this.elasticDistance = 0;
  2221. this.elasticBackTime = 500;
  2222. this._showButtons = UIConfig.showButtons;
  2223. this._scrollSize = 1;
  2224. this._thumbPercent = 1;
  2225. this._lastOffset = 0;
  2226. this._checkElastic = false;
  2227. this._isElastic = false;
  2228. this._hide = false;
  2229. this._clickOnly = true;
  2230. this._touchScrollEnable = UIConfig.touchScrollEnable;
  2231. this._mouseWheelEnable = UIConfig.mouseWheelEnable;
  2232. this.skin = skin;
  2233. this.max = 1;
  2234. }
  2235. destroy(destroyChild = true) {
  2236. this.stopScroll();
  2237. this.target = null;
  2238. super.destroy(destroyChild);
  2239. this.upButton && this.upButton.destroy(destroyChild);
  2240. this.downButton && this.downButton.destroy(destroyChild);
  2241. this.slider && this.slider.destroy(destroyChild);
  2242. this.upButton = this.downButton = null;
  2243. this.slider = null;
  2244. this.changeHandler = null;
  2245. this._offsets = null;
  2246. }
  2247. createChildren() {
  2248. this.addChild(this.slider = new Slider());
  2249. this.addChild(this.upButton = new Button());
  2250. this.addChild(this.downButton = new Button());
  2251. }
  2252. initialize() {
  2253. this.slider.showLabel = false;
  2254. this.slider.tick = 0;
  2255. this.slider.on(Laya.Event.CHANGE, this, this.onSliderChange);
  2256. this.slider.setSlider(0, 0, 0);
  2257. this.upButton.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  2258. this.downButton.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  2259. }
  2260. onSliderChange() {
  2261. if (this._value != this.slider.value)
  2262. this.value = this.slider.value;
  2263. }
  2264. onButtonMouseDown(e) {
  2265. var isUp = e.currentTarget === this.upButton;
  2266. this.slide(isUp);
  2267. Laya.ILaya.timer.once(Styles.scrollBarDelayTime, this, this.startLoop, [isUp]);
  2268. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp);
  2269. }
  2270. startLoop(isUp) {
  2271. Laya.ILaya.timer.frameLoop(1, this, this.slide, [isUp]);
  2272. }
  2273. slide(isUp) {
  2274. if (isUp)
  2275. this.value -= this._scrollSize;
  2276. else
  2277. this.value += this._scrollSize;
  2278. }
  2279. onStageMouseUp(e) {
  2280. Laya.ILaya.timer.clear(this, this.startLoop);
  2281. Laya.ILaya.timer.clear(this, this.slide);
  2282. }
  2283. get skin() {
  2284. return this._skin;
  2285. }
  2286. set skin(value) {
  2287. if (value == " ")
  2288. return;
  2289. if (this._skin != value) {
  2290. this._skin = value;
  2291. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  2292. 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));
  2293. }
  2294. else {
  2295. this._skinLoaded();
  2296. }
  2297. }
  2298. }
  2299. _skinLoaded() {
  2300. this.slider.skin = this._skin;
  2301. this.callLater(this.changeScrollBar);
  2302. this._sizeChanged();
  2303. this.event(Laya.Event.LOADED);
  2304. }
  2305. changeScrollBar() {
  2306. this.upButton.visible = this._showButtons;
  2307. this.downButton.visible = this._showButtons;
  2308. if (this._showButtons) {
  2309. this.upButton.skin = this._skin.replace(".png", "$up.png");
  2310. this.downButton.skin = this._skin.replace(".png", "$down.png");
  2311. }
  2312. if (this.slider.isVertical)
  2313. this.slider.y = this._showButtons ? this.upButton.height : 0;
  2314. else
  2315. this.slider.x = this._showButtons ? this.upButton.width : 0;
  2316. this.resetPositions();
  2317. this.repaint();
  2318. }
  2319. _sizeChanged() {
  2320. super._sizeChanged();
  2321. this.repaint();
  2322. this.resetPositions();
  2323. this.event(Laya.Event.CHANGE);
  2324. this.changeHandler && this.changeHandler.runWith(this.value);
  2325. }
  2326. resetPositions() {
  2327. if (this.slider.isVertical)
  2328. this.slider.height = this.height - (this._showButtons ? (this.upButton.height + this.downButton.height) : 0);
  2329. else
  2330. this.slider.width = this.width - (this._showButtons ? (this.upButton.width + this.downButton.width) : 0);
  2331. this.resetButtonPosition();
  2332. }
  2333. resetButtonPosition() {
  2334. if (this.slider.isVertical)
  2335. this.downButton.y = this.slider._y + this.slider.height;
  2336. else
  2337. this.downButton.x = this.slider._x + this.slider.width;
  2338. }
  2339. measureWidth() {
  2340. if (this.slider.isVertical)
  2341. return this.slider.width;
  2342. return 100;
  2343. }
  2344. measureHeight() {
  2345. if (this.slider.isVertical)
  2346. return 100;
  2347. return this.slider.height;
  2348. }
  2349. setScroll(min, max, value) {
  2350. this.runCallLater(this._sizeChanged);
  2351. this.slider.setSlider(min, max, value);
  2352. this.slider.bar.visible = max > 0;
  2353. if (!this._hide && this.autoHide)
  2354. this.visible = false;
  2355. }
  2356. get max() {
  2357. return this.slider.max;
  2358. }
  2359. set max(value) {
  2360. this.slider.max = value;
  2361. }
  2362. get min() {
  2363. return this.slider.min;
  2364. }
  2365. set min(value) {
  2366. this.slider.min = value;
  2367. }
  2368. get value() {
  2369. return this._value;
  2370. }
  2371. set value(v) {
  2372. if (v !== this._value) {
  2373. this._value = v;
  2374. if (!this._isElastic) {
  2375. if (this.slider["_value"] != v) {
  2376. this.slider["_value"] = v;
  2377. this.slider.changeValue();
  2378. }
  2379. this._value = this.slider["_value"];
  2380. }
  2381. this.event(Laya.Event.CHANGE);
  2382. this.changeHandler && this.changeHandler.runWith(this._value);
  2383. }
  2384. }
  2385. get isVertical() {
  2386. return this.slider.isVertical;
  2387. }
  2388. set isVertical(value) {
  2389. this.slider.isVertical = value;
  2390. }
  2391. get sizeGrid() {
  2392. return this.slider.sizeGrid;
  2393. }
  2394. set sizeGrid(value) {
  2395. this.slider.sizeGrid = value;
  2396. }
  2397. get scrollSize() {
  2398. return this._scrollSize;
  2399. }
  2400. set scrollSize(value) {
  2401. this._scrollSize = value;
  2402. }
  2403. set dataSource(value) {
  2404. this._dataSource = value;
  2405. if (typeof (value) == 'number' || typeof (value) == 'string')
  2406. this.value = Number(value);
  2407. else
  2408. super.dataSource = value;
  2409. }
  2410. get dataSource() {
  2411. return super.dataSource;
  2412. }
  2413. get thumbPercent() {
  2414. return this._thumbPercent;
  2415. }
  2416. set thumbPercent(value) {
  2417. this.runCallLater(this.changeScrollBar);
  2418. this.runCallLater(this._sizeChanged);
  2419. value = value >= 1 ? 0.99 : value;
  2420. this._thumbPercent = value;
  2421. if (this.scaleBar) {
  2422. if (this.slider.isVertical)
  2423. this.slider.bar.height = Math.max(this.slider.height * value, Styles.scrollBarMinNum);
  2424. else
  2425. this.slider.bar.width = Math.max(this.slider.width * value, Styles.scrollBarMinNum);
  2426. }
  2427. }
  2428. get target() {
  2429. return this._target;
  2430. }
  2431. set target(value) {
  2432. if (this._target) {
  2433. this._target.off(Laya.Event.MOUSE_WHEEL, this, this.onTargetMouseWheel);
  2434. this._target.off(Laya.Event.MOUSE_DOWN, this, this.onTargetMouseDown);
  2435. }
  2436. this._target = value;
  2437. if (value) {
  2438. this._mouseWheelEnable && this._target.on(Laya.Event.MOUSE_WHEEL, this, this.onTargetMouseWheel);
  2439. this._touchScrollEnable && this._target.on(Laya.Event.MOUSE_DOWN, this, this.onTargetMouseDown);
  2440. }
  2441. }
  2442. get hide() {
  2443. return this._hide;
  2444. }
  2445. set hide(value) {
  2446. this._hide = value;
  2447. this.visible = !value;
  2448. }
  2449. get showButtons() {
  2450. return this._showButtons;
  2451. }
  2452. set showButtons(value) {
  2453. this._showButtons = value;
  2454. this.callLater(this.changeScrollBar);
  2455. }
  2456. get touchScrollEnable() {
  2457. return this._touchScrollEnable;
  2458. }
  2459. set touchScrollEnable(value) {
  2460. this._touchScrollEnable = value;
  2461. this.target = this._target;
  2462. }
  2463. get mouseWheelEnable() {
  2464. return this._mouseWheelEnable;
  2465. }
  2466. set mouseWheelEnable(value) {
  2467. this._mouseWheelEnable = value;
  2468. this.target = this._target;
  2469. }
  2470. onTargetMouseWheel(e) {
  2471. this.value -= e.delta * this._scrollSize;
  2472. this.target = this._target;
  2473. }
  2474. onTargetMouseDown(e) {
  2475. if ((this.isLockedFun) && !this.isLockedFun(e))
  2476. return;
  2477. this.event(Laya.Event.END);
  2478. this._clickOnly = true;
  2479. this._lastOffset = 0;
  2480. this._checkElastic = false;
  2481. this._lastPoint || (this._lastPoint = new Laya.Point());
  2482. this._lastPoint.setTo(Laya.ILaya.stage.mouseX, Laya.ILaya.stage.mouseY);
  2483. Laya.ILaya.timer.clear(this, this.tweenMove);
  2484. Laya.Tween.clearTween(this);
  2485. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2486. Laya.ILaya.stage.once(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2487. Laya.ILaya.timer.frameLoop(1, this, this.loop);
  2488. }
  2489. startDragForce() {
  2490. this._clickOnly = true;
  2491. this._lastOffset = 0;
  2492. this._checkElastic = false;
  2493. this._lastPoint || (this._lastPoint = new Laya.Point());
  2494. this._lastPoint.setTo(Laya.ILaya.stage.mouseX, Laya.ILaya.stage.mouseY);
  2495. Laya.ILaya.timer.clear(this, this.tweenMove);
  2496. Laya.Tween.clearTween(this);
  2497. Laya.ILaya.stage.once(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2498. Laya.ILaya.stage.once(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2499. Laya.ILaya.timer.frameLoop(1, this, this.loop);
  2500. }
  2501. cancelDragOp() {
  2502. Laya.ILaya.stage.off(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2503. Laya.ILaya.stage.off(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2504. Laya.ILaya.timer.clear(this, this.tweenMove);
  2505. Laya.ILaya.timer.clear(this, this.loop);
  2506. this._target.mouseEnabled = true;
  2507. }
  2508. checkTriggers(isTweenMove = false) {
  2509. if (this.value >= 0 && this.value - this._lastOffset <= 0) {
  2510. if ((this.triggerDownDragLimit) && this.triggerDownDragLimit(isTweenMove)) {
  2511. this.cancelDragOp();
  2512. this.value = 0;
  2513. return true;
  2514. }
  2515. }
  2516. if (this.value <= this.max && (this.value - this._lastOffset >= this.max)) {
  2517. if ((this.triggerUpDragLimit) && this.triggerUpDragLimit(isTweenMove)) {
  2518. this.cancelDragOp();
  2519. this.value = this.max;
  2520. return true;
  2521. }
  2522. }
  2523. return false;
  2524. }
  2525. get lastOffset() {
  2526. return this._lastOffset;
  2527. }
  2528. startTweenMoveForce(lastOffset) {
  2529. this._lastOffset = lastOffset;
  2530. Laya.ILaya.timer.frameLoop(1, this, this.tweenMove, [200]);
  2531. }
  2532. loop() {
  2533. var mouseY = Laya.ILaya.stage.mouseY;
  2534. var mouseX = Laya.ILaya.stage.mouseX;
  2535. this._lastOffset = this.isVertical ? (mouseY - this._lastPoint.y) : (mouseX - this._lastPoint.x);
  2536. if (this._clickOnly) {
  2537. if (Math.abs(this._lastOffset * (this.isVertical ? Laya.ILaya.stage._canvasTransform.getScaleY() : Laya.ILaya.stage._canvasTransform.getScaleX())) > 1) {
  2538. this._clickOnly = false;
  2539. if (this.checkTriggers())
  2540. return;
  2541. this._offsets || (this._offsets = []);
  2542. this._offsets.length = 0;
  2543. this._target.mouseEnabled = false;
  2544. if (!this.hide && this.autoHide) {
  2545. this.alpha = 1;
  2546. this.visible = true;
  2547. }
  2548. this.event(Laya.Event.START);
  2549. }
  2550. else
  2551. return;
  2552. }
  2553. else {
  2554. if (this.checkTriggers())
  2555. return;
  2556. }
  2557. this._offsets.push(this._lastOffset);
  2558. this._lastPoint.x = mouseX;
  2559. this._lastPoint.y = mouseY;
  2560. if (this._lastOffset === 0)
  2561. return;
  2562. if (!this._checkElastic) {
  2563. if (this.elasticDistance > 0) {
  2564. if (!this._checkElastic && this._lastOffset != 0) {
  2565. if ((this._lastOffset > 0 && this._value <= this.min) || (this._lastOffset < 0 && this._value >= this.max)) {
  2566. this._isElastic = true;
  2567. this._checkElastic = true;
  2568. }
  2569. else {
  2570. this._isElastic = false;
  2571. }
  2572. }
  2573. }
  2574. else {
  2575. this._checkElastic = true;
  2576. }
  2577. }
  2578. if (this._isElastic) {
  2579. if (this._value <= this.min) {
  2580. if (this._lastOffset > 0) {
  2581. this.value -= this._lastOffset * Math.max(0, (1 - ((this.min - this._value) / this.elasticDistance)));
  2582. }
  2583. else {
  2584. this.value -= this._lastOffset * 0.5;
  2585. if (this._value >= this.min)
  2586. this._checkElastic = false;
  2587. }
  2588. }
  2589. else if (this._value >= this.max) {
  2590. if (this._lastOffset < 0) {
  2591. this.value -= this._lastOffset * Math.max(0, (1 - ((this._value - this.max) / this.elasticDistance)));
  2592. }
  2593. else {
  2594. this.value -= this._lastOffset * 0.5;
  2595. if (this._value <= this.max)
  2596. this._checkElastic = false;
  2597. }
  2598. }
  2599. }
  2600. else {
  2601. this.value -= this._lastOffset;
  2602. }
  2603. }
  2604. onStageMouseUp2(e) {
  2605. Laya.ILaya.stage.off(Laya.Event.MOUSE_UP, this, this.onStageMouseUp2);
  2606. Laya.ILaya.stage.off(Laya.Event.MOUSE_OUT, this, this.onStageMouseUp2);
  2607. Laya.ILaya.timer.clear(this, this.loop);
  2608. if (this._clickOnly) {
  2609. if (this._value >= this.min && this._value <= this.max)
  2610. return;
  2611. }
  2612. this._target.mouseEnabled = true;
  2613. if (this._isElastic) {
  2614. if (this._value < this.min) {
  2615. Laya.Tween.to(this, { value: this.min }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2616. }
  2617. else if (this._value > this.max) {
  2618. Laya.Tween.to(this, { value: this.max }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2619. }
  2620. }
  2621. else {
  2622. if (!this._offsets)
  2623. return;
  2624. if (this._offsets.length < 1) {
  2625. this._offsets[0] = this.isVertical ? Laya.ILaya.stage.mouseY - this._lastPoint.y : Laya.ILaya.stage.mouseX - this._lastPoint.x;
  2626. }
  2627. var offset = 0;
  2628. var n = Math.min(this._offsets.length, 3);
  2629. for (var i = 0; i < n; i++) {
  2630. offset += this._offsets[this._offsets.length - 1 - i];
  2631. }
  2632. this._lastOffset = offset / n;
  2633. offset = Math.abs(this._lastOffset);
  2634. if (offset < 2) {
  2635. this.event(Laya.Event.END);
  2636. return;
  2637. }
  2638. if (offset > 250)
  2639. this._lastOffset = this._lastOffset > 0 ? 250 : -250;
  2640. var dis = Math.round(Math.abs(this.elasticDistance * (this._lastOffset / 150)));
  2641. Laya.ILaya.timer.frameLoop(1, this, this.tweenMove, [dis]);
  2642. }
  2643. }
  2644. elasticOver() {
  2645. this._isElastic = false;
  2646. if (!this.hide && this.autoHide) {
  2647. Laya.Tween.to(this, { alpha: 0 }, 500);
  2648. }
  2649. this.event(Laya.Event.END);
  2650. }
  2651. tweenMove(maxDistance) {
  2652. this._lastOffset *= this.rollRatio;
  2653. if (this.checkTriggers(true)) {
  2654. return;
  2655. }
  2656. var tarSpeed;
  2657. if (maxDistance > 0) {
  2658. if (this._lastOffset > 0 && this.value <= this.min) {
  2659. this._isElastic = true;
  2660. tarSpeed = -(this.min - maxDistance - this.value) * 0.5;
  2661. if (this._lastOffset > tarSpeed)
  2662. this._lastOffset = tarSpeed;
  2663. }
  2664. else if (this._lastOffset < 0 && this.value >= this.max) {
  2665. this._isElastic = true;
  2666. tarSpeed = -(this.max + maxDistance - this.value) * 0.5;
  2667. if (this._lastOffset < tarSpeed)
  2668. this._lastOffset = tarSpeed;
  2669. }
  2670. }
  2671. this.value -= this._lastOffset;
  2672. if (Math.abs(this._lastOffset) < 0.1) {
  2673. Laya.ILaya.timer.clear(this, this.tweenMove);
  2674. if (this._isElastic) {
  2675. if (this._value < this.min) {
  2676. Laya.Tween.to(this, { value: this.min }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2677. }
  2678. else if (this._value > this.max) {
  2679. Laya.Tween.to(this, { value: this.max }, this.elasticBackTime, Laya.Ease.sineOut, Laya.Handler.create(this, this.elasticOver));
  2680. }
  2681. else {
  2682. this.elasticOver();
  2683. }
  2684. return;
  2685. }
  2686. this.event(Laya.Event.END);
  2687. if (!this.hide && this.autoHide) {
  2688. Laya.Tween.to(this, { alpha: 0 }, 500);
  2689. }
  2690. }
  2691. }
  2692. stopScroll() {
  2693. this.onStageMouseUp2(null);
  2694. Laya.ILaya.timer.clear(this, this.tweenMove);
  2695. Laya.Tween.clearTween(this);
  2696. }
  2697. get tick() {
  2698. return this.slider.tick;
  2699. }
  2700. set tick(value) {
  2701. this.slider.tick = value;
  2702. }
  2703. }
  2704. Laya.ILaya.regClass(ScrollBar);
  2705. Laya.ClassUtils.regClass("laya.ui.ScrollBar", ScrollBar);
  2706. Laya.ClassUtils.regClass("Laya.ScrollBar", ScrollBar);
  2707. class VScrollBar extends ScrollBar {
  2708. }
  2709. Laya.ILaya.regClass(VScrollBar);
  2710. Laya.ClassUtils.regClass("laya.ui.VScrollBar", VScrollBar);
  2711. Laya.ClassUtils.regClass("Laya.VScrollBar", VScrollBar);
  2712. class HScrollBar extends ScrollBar {
  2713. initialize() {
  2714. super.initialize();
  2715. this.slider.isVertical = false;
  2716. }
  2717. }
  2718. Laya.ILaya.regClass(HScrollBar);
  2719. Laya.ClassUtils.regClass("laya.ui.HScrollBar", HScrollBar);
  2720. Laya.ClassUtils.regClass("Laya.HScrollBar", HScrollBar);
  2721. class List extends Box {
  2722. constructor() {
  2723. super(...arguments);
  2724. this.selectEnable = false;
  2725. this.totalPage = 0;
  2726. this._$componentType = "List";
  2727. this._repeatX = 0;
  2728. this._repeatY = 0;
  2729. this._repeatX2 = 0;
  2730. this._repeatY2 = 0;
  2731. this._spaceX = 0;
  2732. this._spaceY = 0;
  2733. this._cells = [];
  2734. this._startIndex = 0;
  2735. this._selectedIndex = -1;
  2736. this._page = 0;
  2737. this._isVertical = true;
  2738. this._cellSize = 20;
  2739. this._cellOffset = 0;
  2740. this._createdLine = 0;
  2741. this._offset = new Laya.Point();
  2742. this._usedCache = null;
  2743. this._elasticEnabled = false;
  2744. this._preLen = 0;
  2745. }
  2746. destroy(destroyChild = true) {
  2747. this._content && this._content.destroy(destroyChild);
  2748. this._scrollBar && this._scrollBar.destroy(destroyChild);
  2749. super.destroy(destroyChild);
  2750. this._content = null;
  2751. this._scrollBar = null;
  2752. this._itemRender = null;
  2753. this._cells = null;
  2754. this._array = null;
  2755. this.selectHandler = this.renderHandler = this.mouseHandler = null;
  2756. }
  2757. createChildren() {
  2758. this.addChild(this._content = new Box());
  2759. }
  2760. set cacheAs(value) {
  2761. super.cacheAs = value;
  2762. if (this._scrollBar) {
  2763. this._usedCache = null;
  2764. if (value !== "none")
  2765. this._scrollBar.on(Laya.Event.START, this, this.onScrollStart);
  2766. else
  2767. this._scrollBar.off(Laya.Event.START, this, this.onScrollStart);
  2768. }
  2769. }
  2770. get cacheAs() {
  2771. return super.cacheAs;
  2772. }
  2773. onScrollStart() {
  2774. this._usedCache || (this._usedCache = super.cacheAs);
  2775. super.cacheAs = "none";
  2776. this._scrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  2777. }
  2778. onScrollEnd() {
  2779. super.cacheAs = this._usedCache;
  2780. }
  2781. get content() {
  2782. return this._content;
  2783. }
  2784. get vScrollBarSkin() {
  2785. return this._scrollBar ? this._scrollBar.skin : null;
  2786. }
  2787. set vScrollBarSkin(value) {
  2788. this._removePreScrollBar();
  2789. var scrollBar = new VScrollBar();
  2790. scrollBar.name = "scrollBar";
  2791. scrollBar.right = 0;
  2792. scrollBar.skin = value;
  2793. scrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  2794. this.scrollBar = scrollBar;
  2795. this.addChild(scrollBar);
  2796. this._setCellChanged();
  2797. }
  2798. _removePreScrollBar() {
  2799. var preNode = this.removeChildByName("scrollBar");
  2800. if (preNode)
  2801. preNode.destroy(true);
  2802. }
  2803. get hScrollBarSkin() {
  2804. return this._scrollBar ? this._scrollBar.skin : null;
  2805. }
  2806. set hScrollBarSkin(value) {
  2807. this._removePreScrollBar();
  2808. var scrollBar = new HScrollBar();
  2809. scrollBar.name = "scrollBar";
  2810. scrollBar.bottom = 0;
  2811. scrollBar.skin = value;
  2812. scrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  2813. this.scrollBar = scrollBar;
  2814. this.addChild(scrollBar);
  2815. this._setCellChanged();
  2816. }
  2817. get scrollBar() {
  2818. return this._scrollBar;
  2819. }
  2820. set scrollBar(value) {
  2821. if (this._scrollBar != value) {
  2822. this._scrollBar = value;
  2823. if (value) {
  2824. this._isVertical = this._scrollBar.isVertical;
  2825. this.addChild(this._scrollBar);
  2826. this._scrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange);
  2827. }
  2828. }
  2829. }
  2830. get itemRender() {
  2831. return this._itemRender;
  2832. }
  2833. set itemRender(value) {
  2834. if (this._itemRender != value) {
  2835. this._itemRender = value;
  2836. for (var i = this._cells.length - 1; i > -1; i--) {
  2837. this._cells[i].destroy();
  2838. }
  2839. this._cells.length = 0;
  2840. this._setCellChanged();
  2841. }
  2842. }
  2843. set width(value) {
  2844. if (value != this._width) {
  2845. super.width = value;
  2846. this._setCellChanged();
  2847. }
  2848. }
  2849. get width() {
  2850. return super.width;
  2851. }
  2852. set height(value) {
  2853. if (value != this._height) {
  2854. super.height = value;
  2855. this._setCellChanged();
  2856. }
  2857. }
  2858. get height() {
  2859. return super.height;
  2860. }
  2861. get repeatX() {
  2862. return this._repeatX > 0 ? this._repeatX : this._repeatX2 > 0 ? this._repeatX2 : 1;
  2863. }
  2864. set repeatX(value) {
  2865. this._repeatX = value;
  2866. this._setCellChanged();
  2867. }
  2868. get repeatY() {
  2869. return this._repeatY > 0 ? this._repeatY : this._repeatY2 > 0 ? this._repeatY2 : 1;
  2870. }
  2871. set repeatY(value) {
  2872. this._repeatY = value;
  2873. this._setCellChanged();
  2874. }
  2875. get spaceX() {
  2876. return this._spaceX;
  2877. }
  2878. set spaceX(value) {
  2879. this._spaceX = value;
  2880. this._setCellChanged();
  2881. }
  2882. get spaceY() {
  2883. return this._spaceY;
  2884. }
  2885. set spaceY(value) {
  2886. this._spaceY = value;
  2887. this._setCellChanged();
  2888. }
  2889. changeCells() {
  2890. this._cellChanged = false;
  2891. if (this._itemRender) {
  2892. this.scrollBar = this.getChildByName("scrollBar");
  2893. var cell = this._getOneCell();
  2894. var cellWidth = (cell.width + this._spaceX) || 1;
  2895. var cellHeight = (cell.height + this._spaceY) || 1;
  2896. if (this._width > 0)
  2897. this._repeatX2 = this._isVertical ? Math.round(this._width / cellWidth) : Math.ceil(this._width / cellWidth);
  2898. if (this._height > 0)
  2899. this._repeatY2 = this._isVertical ? Math.ceil(this._height / cellHeight) : Math.round(this._height / cellHeight);
  2900. var listWidth = this._width ? this._width : (cellWidth * this.repeatX - this._spaceX);
  2901. var listHeight = this._height ? this._height : (cellHeight * this.repeatY - this._spaceY);
  2902. this._cellSize = this._isVertical ? cellHeight : cellWidth;
  2903. this._cellOffset = this._isVertical ? (cellHeight * Math.max(this._repeatY2, this._repeatY) - listHeight - this._spaceY) : (cellWidth * Math.max(this._repeatX2, this._repeatX) - listWidth - this._spaceX);
  2904. if (this._isVertical && this.vScrollBarSkin)
  2905. this._scrollBar.height = listHeight;
  2906. else if (!this._isVertical && this.hScrollBarSkin)
  2907. this._scrollBar.width = listWidth;
  2908. this.setContentSize(listWidth, listHeight);
  2909. var numX = this._isVertical ? this.repeatX : this.repeatY;
  2910. var numY = (this._isVertical ? this.repeatY : this.repeatX) + (this._scrollBar ? 1 : 0);
  2911. this._createItems(0, numX, numY);
  2912. this._createdLine = numY;
  2913. if (this._array) {
  2914. this.array = this._array;
  2915. this.runCallLater(this.renderItems);
  2916. }
  2917. }
  2918. }
  2919. _getOneCell() {
  2920. if (this._cells.length === 0) {
  2921. var item = this.createItem();
  2922. this._offset.setTo(item._x, item._y);
  2923. if (this.cacheContent)
  2924. return item;
  2925. this._cells.push(item);
  2926. }
  2927. return this._cells[0];
  2928. }
  2929. _createItems(startY, numX, numY) {
  2930. var box = this._content;
  2931. var cell = this._getOneCell();
  2932. var cellWidth = cell.width + this._spaceX;
  2933. var cellHeight = cell.height + this._spaceY;
  2934. if (this.cacheContent) {
  2935. var cacheBox = new Box();
  2936. cacheBox.cacheAs = "normal";
  2937. cacheBox.pos((this._isVertical ? 0 : startY) * cellWidth, (this._isVertical ? startY : 0) * cellHeight);
  2938. this._content.addChild(cacheBox);
  2939. box = cacheBox;
  2940. }
  2941. else {
  2942. var arr = [];
  2943. for (var i = this._cells.length - 1; i > -1; i--) {
  2944. var item = this._cells[i];
  2945. item.removeSelf();
  2946. arr.push(item);
  2947. }
  2948. this._cells.length = 0;
  2949. }
  2950. for (var k = startY; k < numY; k++) {
  2951. for (var l = 0; l < numX; l++) {
  2952. if (arr && arr.length) {
  2953. cell = arr.pop();
  2954. }
  2955. else {
  2956. cell = this.createItem();
  2957. }
  2958. cell.x = (this._isVertical ? l : k) * cellWidth - box._x;
  2959. cell.y = (this._isVertical ? k : l) * cellHeight - box._y;
  2960. cell.name = "item" + (k * numX + l);
  2961. box.addChild(cell);
  2962. this.addCell(cell);
  2963. }
  2964. }
  2965. }
  2966. createItem() {
  2967. var arr = [];
  2968. if (typeof (this._itemRender) == "function") {
  2969. var box = new this._itemRender();
  2970. }
  2971. else {
  2972. box = Laya.SceneUtils.createComp(this._itemRender, null, null, arr);
  2973. }
  2974. if (arr.length == 0 && box["_watchMap"]) {
  2975. var watchMap = box["_watchMap"];
  2976. for (var name in watchMap) {
  2977. var a = watchMap[name];
  2978. for (var i = 0; i < a.length; i++) {
  2979. var watcher = a[i];
  2980. arr.push(watcher.comp, watcher.prop, watcher.value);
  2981. }
  2982. }
  2983. }
  2984. if (arr.length)
  2985. box["_$bindData"] = arr;
  2986. return box;
  2987. }
  2988. addCell(cell) {
  2989. cell.on(Laya.Event.CLICK, this, this.onCellMouse);
  2990. cell.on(Laya.Event.RIGHT_CLICK, this, this.onCellMouse);
  2991. cell.on(Laya.Event.MOUSE_OVER, this, this.onCellMouse);
  2992. cell.on(Laya.Event.MOUSE_OUT, this, this.onCellMouse);
  2993. cell.on(Laya.Event.MOUSE_DOWN, this, this.onCellMouse);
  2994. cell.on(Laya.Event.MOUSE_UP, this, this.onCellMouse);
  2995. this._cells.push(cell);
  2996. }
  2997. _afterInited() {
  2998. this.initItems();
  2999. }
  3000. initItems() {
  3001. if (!this._itemRender && this.getChildByName("item0") != null) {
  3002. this.repeatX = 1;
  3003. var count;
  3004. count = 0;
  3005. for (var i = 0; i < 10000; i++) {
  3006. var cell = this.getChildByName("item" + i);
  3007. if (cell) {
  3008. this.addCell(cell);
  3009. count++;
  3010. continue;
  3011. }
  3012. break;
  3013. }
  3014. this.repeatY = count;
  3015. }
  3016. }
  3017. setContentSize(width, height) {
  3018. this._content.width = width;
  3019. this._content.height = height;
  3020. if (this._scrollBar || this._offset.x != 0 || this._offset.y != 0) {
  3021. this._content._style.scrollRect || (this._content.scrollRect = Laya.Rectangle.create());
  3022. this._content._style.scrollRect.setTo(-this._offset.x, -this._offset.y, width, height);
  3023. this._content.scrollRect = this._content.scrollRect;
  3024. }
  3025. this.event(Laya.Event.RESIZE);
  3026. }
  3027. onCellMouse(e) {
  3028. if (e.type === Laya.Event.MOUSE_DOWN)
  3029. this._isMoved = false;
  3030. var cell = e.currentTarget;
  3031. var index = this._startIndex + this._cells.indexOf(cell);
  3032. if (index < 0)
  3033. return;
  3034. if (e.type === Laya.Event.CLICK || e.type === Laya.Event.RIGHT_CLICK) {
  3035. if (this.selectEnable && !this._isMoved)
  3036. this.selectedIndex = index;
  3037. else
  3038. this.changeCellState(cell, true, 0);
  3039. }
  3040. else if ((e.type === Laya.Event.MOUSE_OVER || e.type === Laya.Event.MOUSE_OUT) && this._selectedIndex !== index) {
  3041. this.changeCellState(cell, e.type === Laya.Event.MOUSE_OVER, 0);
  3042. }
  3043. this.mouseHandler && this.mouseHandler.runWith([e, index]);
  3044. }
  3045. changeCellState(cell, visible, index) {
  3046. var selectBox = cell.getChildByName("selectBox");
  3047. if (selectBox) {
  3048. this.selectEnable = true;
  3049. selectBox.visible = visible;
  3050. selectBox.index = index;
  3051. }
  3052. }
  3053. _sizeChanged() {
  3054. super._sizeChanged();
  3055. this.setContentSize(this.width, this.height);
  3056. if (this._scrollBar)
  3057. this.callLater(this.onScrollBarChange);
  3058. }
  3059. onScrollBarChange(e = null) {
  3060. this.runCallLater(this.changeCells);
  3061. var scrollValue = this._scrollBar.value;
  3062. var lineX = (this._isVertical ? this.repeatX : this.repeatY);
  3063. var lineY = (this._isVertical ? this.repeatY : this.repeatX);
  3064. var scrollLine = Math.floor(scrollValue / this._cellSize);
  3065. if (!this.cacheContent) {
  3066. var index = scrollLine * lineX;
  3067. var num = 0;
  3068. if (index > this._startIndex) {
  3069. num = index - this._startIndex;
  3070. var down = true;
  3071. var toIndex = this._startIndex + lineX * (lineY + 1);
  3072. this._isMoved = true;
  3073. }
  3074. else if (index < this._startIndex) {
  3075. num = this._startIndex - index;
  3076. down = false;
  3077. toIndex = this._startIndex - 1;
  3078. this._isMoved = true;
  3079. }
  3080. for (var i = 0; i < num; i++) {
  3081. if (down) {
  3082. var cell = this._cells.shift();
  3083. this._cells[this._cells.length] = cell;
  3084. var cellIndex = toIndex + i;
  3085. }
  3086. else {
  3087. cell = this._cells.pop();
  3088. this._cells.unshift(cell);
  3089. cellIndex = toIndex - i;
  3090. }
  3091. var pos = Math.floor(cellIndex / lineX) * this._cellSize;
  3092. this._isVertical ? cell.y = pos : cell.x = pos;
  3093. this.renderItem(cell, cellIndex);
  3094. }
  3095. this._startIndex = index;
  3096. this.changeSelectStatus();
  3097. }
  3098. else {
  3099. num = (lineY + 1);
  3100. if (this._createdLine - scrollLine < num) {
  3101. this._createItems(this._createdLine, lineX, this._createdLine + num);
  3102. this.renderItems(this._createdLine * lineX, 0);
  3103. this._createdLine += num;
  3104. }
  3105. }
  3106. var r = this._content._style.scrollRect;
  3107. if (this._isVertical) {
  3108. r.y = scrollValue - this._offset.y;
  3109. r.x = -this._offset.x;
  3110. }
  3111. else {
  3112. r.y = -this._offset.y;
  3113. r.x = scrollValue - this._offset.x;
  3114. }
  3115. this._content.scrollRect = r;
  3116. }
  3117. posCell(cell, cellIndex) {
  3118. if (!this._scrollBar)
  3119. return;
  3120. var lineX = (this._isVertical ? this.repeatX : this.repeatY);
  3121. var lineY = (this._isVertical ? this.repeatY : this.repeatX);
  3122. var pos = Math.floor(cellIndex / lineX) * this._cellSize;
  3123. this._isVertical ? cell._y = pos : cell.x = pos;
  3124. }
  3125. get selectedIndex() {
  3126. return this._selectedIndex;
  3127. }
  3128. set selectedIndex(value) {
  3129. if (this._selectedIndex != value) {
  3130. this._selectedIndex = value;
  3131. this.changeSelectStatus();
  3132. this.event(Laya.Event.CHANGE);
  3133. this.selectHandler && this.selectHandler.runWith(value);
  3134. this.startIndex = this._startIndex;
  3135. }
  3136. }
  3137. changeSelectStatus() {
  3138. for (var i = 0, n = this._cells.length; i < n; i++) {
  3139. this.changeCellState(this._cells[i], this._selectedIndex === this._startIndex + i, 1);
  3140. }
  3141. }
  3142. get selectedItem() {
  3143. return this._selectedIndex != -1 ? this._array[this._selectedIndex] : null;
  3144. }
  3145. set selectedItem(value) {
  3146. this.selectedIndex = this._array.indexOf(value);
  3147. }
  3148. get selection() {
  3149. return this.getCell(this._selectedIndex);
  3150. }
  3151. set selection(value) {
  3152. this.selectedIndex = this._startIndex + this._cells.indexOf(value);
  3153. }
  3154. get startIndex() {
  3155. return this._startIndex;
  3156. }
  3157. set startIndex(value) {
  3158. this._startIndex = value > 0 ? value : 0;
  3159. this.callLater(this.renderItems);
  3160. }
  3161. renderItems(from = 0, to = 0) {
  3162. for (var i = from, n = to || this._cells.length; i < n; i++) {
  3163. this.renderItem(this._cells[i], this._startIndex + i);
  3164. }
  3165. this.changeSelectStatus();
  3166. }
  3167. renderItem(cell, index) {
  3168. if (this._array && index >= 0 && index < this._array.length) {
  3169. cell.visible = true;
  3170. if (cell["_$bindData"]) {
  3171. cell["_dataSource"] = this._array[index];
  3172. this._bindData(cell, this._array[index]);
  3173. }
  3174. else
  3175. cell.dataSource = this._array[index];
  3176. if (!this.cacheContent) {
  3177. this.posCell(cell, index);
  3178. }
  3179. if (this.hasListener(Laya.Event.RENDER))
  3180. this.event(Laya.Event.RENDER, [cell, index]);
  3181. if (this.renderHandler)
  3182. this.renderHandler.runWith([cell, index]);
  3183. }
  3184. else {
  3185. cell.visible = false;
  3186. cell.dataSource = null;
  3187. }
  3188. }
  3189. _bindData(cell, data) {
  3190. var arr = cell._$bindData;
  3191. for (var i = 0, n = arr.length; i < n; i++) {
  3192. var ele = arr[i++];
  3193. var prop = arr[i++];
  3194. var value = arr[i];
  3195. var fun = UIUtils.getBindFun(value);
  3196. ele[prop] = fun.call(this, data);
  3197. }
  3198. }
  3199. get array() {
  3200. return this._array;
  3201. }
  3202. set array(value) {
  3203. this.runCallLater(this.changeCells);
  3204. this._array = value || [];
  3205. this._preLen = this._array.length;
  3206. var length = this._array.length;
  3207. this.totalPage = Math.ceil(length / (this.repeatX * this.repeatY));
  3208. this._selectedIndex = this._selectedIndex < length ? this._selectedIndex : length - 1;
  3209. this.startIndex = this._startIndex;
  3210. if (this._scrollBar) {
  3211. this._scrollBar.stopScroll();
  3212. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3213. var numY = this._isVertical ? this.repeatY : this.repeatX;
  3214. var lineCount = Math.ceil(length / numX);
  3215. var total = this._cellOffset > 0 ? this.totalPage + 1 : this.totalPage;
  3216. if (total > 1 && lineCount >= numY) {
  3217. this._scrollBar.scrollSize = this._cellSize;
  3218. this._scrollBar.thumbPercent = numY / lineCount;
  3219. this._scrollBar.setScroll(0, (lineCount - numY) * this._cellSize + this._cellOffset, this._scrollBar.value);
  3220. this._scrollBar.target = this._content;
  3221. }
  3222. else {
  3223. this._scrollBar.setScroll(0, 0, 0);
  3224. this._scrollBar.target = this._content;
  3225. }
  3226. }
  3227. }
  3228. updateArray(array) {
  3229. this._array = array;
  3230. var freshStart;
  3231. if (this._array) {
  3232. freshStart = this._preLen - this._startIndex;
  3233. if (freshStart >= 0)
  3234. this.renderItems(freshStart);
  3235. this._preLen = this._array.length;
  3236. }
  3237. if (this._scrollBar) {
  3238. var length = array.length;
  3239. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3240. var numY = this._isVertical ? this.repeatY : this.repeatX;
  3241. var lineCount = Math.ceil(length / numX);
  3242. if (lineCount >= numY) {
  3243. this._scrollBar.thumbPercent = numY / lineCount;
  3244. this._scrollBar.slider["_max"] = (lineCount - numY) * this._cellSize + this._cellOffset;
  3245. }
  3246. }
  3247. }
  3248. get page() {
  3249. return this._page;
  3250. }
  3251. set page(value) {
  3252. this._page = value;
  3253. if (this._array) {
  3254. this._page = value > 0 ? value : 0;
  3255. this._page = this._page < this.totalPage ? this._page : this.totalPage - 1;
  3256. this.startIndex = this._page * this.repeatX * this.repeatY;
  3257. }
  3258. }
  3259. get length() {
  3260. return this._array ? this._array.length : 0;
  3261. }
  3262. set dataSource(value) {
  3263. this._dataSource = value;
  3264. if (typeof (value) == 'number' || typeof (value) == 'string')
  3265. this.selectedIndex = parseInt(value);
  3266. else if (value instanceof Array)
  3267. this.array = value;
  3268. else
  3269. super.dataSource = value;
  3270. }
  3271. get dataSource() {
  3272. return super.dataSource;
  3273. }
  3274. get cells() {
  3275. this.runCallLater(this.changeCells);
  3276. return this._cells;
  3277. }
  3278. get elasticEnabled() {
  3279. return this._elasticEnabled;
  3280. }
  3281. set elasticEnabled(value) {
  3282. this._elasticEnabled = value;
  3283. if (this._scrollBar) {
  3284. this._scrollBar.elasticDistance = value ? 200 : 0;
  3285. }
  3286. }
  3287. refresh() {
  3288. this.array = this._array;
  3289. }
  3290. getItem(index) {
  3291. if (index > -1 && index < this._array.length) {
  3292. return this._array[index];
  3293. }
  3294. return null;
  3295. }
  3296. changeItem(index, source) {
  3297. if (index > -1 && index < this._array.length) {
  3298. this._array[index] = source;
  3299. if (index >= this._startIndex && index < this._startIndex + this._cells.length) {
  3300. this.renderItem(this.getCell(index), index);
  3301. }
  3302. }
  3303. }
  3304. setItem(index, source) {
  3305. this.changeItem(index, source);
  3306. }
  3307. addItem(souce) {
  3308. this._array.push(souce);
  3309. this.array = this._array;
  3310. }
  3311. addItemAt(souce, index) {
  3312. this._array.splice(index, 0, souce);
  3313. this.array = this._array;
  3314. }
  3315. deleteItem(index) {
  3316. this._array.splice(index, 1);
  3317. this.array = this._array;
  3318. }
  3319. getCell(index) {
  3320. this.runCallLater(this.changeCells);
  3321. if (index > -1 && this._cells) {
  3322. return this._cells[(index - this._startIndex) % this._cells.length];
  3323. }
  3324. return null;
  3325. }
  3326. scrollTo(index) {
  3327. if (this._scrollBar) {
  3328. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3329. this._scrollBar.value = Math.floor(index / numX) * this._cellSize;
  3330. }
  3331. else {
  3332. this.startIndex = index;
  3333. }
  3334. }
  3335. tweenTo(index, time = 200, complete = null) {
  3336. if (this._scrollBar) {
  3337. this._scrollBar.stopScroll();
  3338. var numX = this._isVertical ? this.repeatX : this.repeatY;
  3339. Laya.Tween.to(this._scrollBar, { value: Math.floor(index / numX) * this._cellSize }, time, null, complete, 0, true);
  3340. }
  3341. else {
  3342. this.startIndex = index;
  3343. if (complete)
  3344. complete.run();
  3345. }
  3346. }
  3347. _setCellChanged() {
  3348. if (!this._cellChanged) {
  3349. this._cellChanged = true;
  3350. this.callLater(this.changeCells);
  3351. }
  3352. }
  3353. commitMeasure() {
  3354. this.runCallLater(this.changeCells);
  3355. }
  3356. }
  3357. Laya.ILaya.regClass(List);
  3358. Laya.ClassUtils.regClass("laya.ui.List", List);
  3359. Laya.ClassUtils.regClass("Laya.List", List);
  3360. class ComboBox extends UIComponent {
  3361. constructor(skin = null, labels = null) {
  3362. super();
  3363. this._visibleNum = 6;
  3364. this._itemColors = Styles.comboBoxItemColors;
  3365. this._itemSize = 12;
  3366. this._labels = [];
  3367. this._selectedIndex = -1;
  3368. this.itemRender = null;
  3369. this.skin = skin;
  3370. this.labels = labels;
  3371. }
  3372. destroy(destroyChild = true) {
  3373. super.destroy(destroyChild);
  3374. this._button && this._button.destroy(destroyChild);
  3375. this._list && this._list.destroy(destroyChild);
  3376. this._button = null;
  3377. this._list = null;
  3378. this._itemColors = null;
  3379. this._labels = null;
  3380. this._selectHandler = null;
  3381. }
  3382. createChildren() {
  3383. this.addChild(this._button = new Button());
  3384. this._button.text.align = "left";
  3385. this._button.labelPadding = "0,0,0,5";
  3386. this._button.on(Laya.Event.MOUSE_DOWN, this, this.onButtonMouseDown);
  3387. }
  3388. _createList() {
  3389. this._list = new List();
  3390. if (this._scrollBarSkin)
  3391. this._list.vScrollBarSkin = this._scrollBarSkin;
  3392. this._setListEvent(this._list);
  3393. }
  3394. _setListEvent(list) {
  3395. this._list.selectEnable = true;
  3396. this._list.on(Laya.Event.MOUSE_DOWN, this, this.onListDown);
  3397. this._list.mouseHandler = Laya.Handler.create(this, this.onlistItemMouse, null, false);
  3398. if (this._list.scrollBar)
  3399. this._list.scrollBar.on(Laya.Event.MOUSE_DOWN, this, this.onScrollBarDown);
  3400. }
  3401. onListDown(e) {
  3402. e.stopPropagation();
  3403. }
  3404. onScrollBarDown(e) {
  3405. e.stopPropagation();
  3406. }
  3407. onButtonMouseDown(e) {
  3408. this.callLater(this.switchTo, [!this._isOpen]);
  3409. }
  3410. get skin() {
  3411. return this._button.skin;
  3412. }
  3413. set skin(value) {
  3414. if (this._button.skin != value) {
  3415. this._button.skin = value;
  3416. this._listChanged = true;
  3417. }
  3418. }
  3419. measureWidth() {
  3420. return this._button.width;
  3421. }
  3422. measureHeight() {
  3423. return this._button.height;
  3424. }
  3425. changeList() {
  3426. this._listChanged = false;
  3427. var labelWidth = this.width - 2;
  3428. var labelColor = this._itemColors[2];
  3429. this._itemHeight = this._itemSize + 6;
  3430. 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 } }] };
  3431. this._list.repeatY = this._visibleNum;
  3432. this._list.refresh();
  3433. }
  3434. onlistItemMouse(e, index) {
  3435. var type = e.type;
  3436. if (type === Laya.Event.MOUSE_OVER || type === Laya.Event.MOUSE_OUT) {
  3437. if (this._isCustomList)
  3438. return;
  3439. var box = this._list.getCell(index);
  3440. if (!box)
  3441. return;
  3442. var label = box.getChildByName("label");
  3443. if (label) {
  3444. if (type === Laya.Event.ROLL_OVER) {
  3445. label.bgColor = this._itemColors[0];
  3446. label.color = this._itemColors[1];
  3447. }
  3448. else {
  3449. label.bgColor = null;
  3450. label.color = this._itemColors[2];
  3451. }
  3452. }
  3453. }
  3454. else if (type === Laya.Event.CLICK) {
  3455. this.selectedIndex = index;
  3456. this.isOpen = false;
  3457. }
  3458. }
  3459. switchTo(value) {
  3460. this.isOpen = value;
  3461. }
  3462. changeOpen() {
  3463. this.isOpen = !this._isOpen;
  3464. }
  3465. set width(value) {
  3466. super.width = value;
  3467. this._button.width = this._width;
  3468. this._itemChanged = true;
  3469. this._listChanged = true;
  3470. }
  3471. get width() {
  3472. return super.width;
  3473. }
  3474. set height(value) {
  3475. super.height = value;
  3476. this._button.height = this._height;
  3477. }
  3478. get height() {
  3479. return super.height;
  3480. }
  3481. get labels() {
  3482. return this._labels.join(",");
  3483. }
  3484. set labels(value) {
  3485. if (this._labels.length > 0)
  3486. this.selectedIndex = -1;
  3487. if (value)
  3488. this._labels = value.split(",");
  3489. else
  3490. this._labels.length = 0;
  3491. this._itemChanged = true;
  3492. }
  3493. changeItem() {
  3494. this._itemChanged = false;
  3495. this._listHeight = this._labels.length > 0 ? Math.min(this._visibleNum, this._labels.length) * this._itemHeight : this._itemHeight;
  3496. if (!this._isCustomList) {
  3497. var g = this._list.graphics;
  3498. g.clear(true);
  3499. g.drawRect(0, 0, this.width - 1, this._listHeight, this._itemColors[4], this._itemColors[3]);
  3500. }
  3501. var a = this._list.array || [];
  3502. a.length = 0;
  3503. for (var i = 0, n = this._labels.length; i < n; i++) {
  3504. a.push({ label: this._labels[i] });
  3505. }
  3506. this._list.height = this._listHeight;
  3507. this._list.array = a;
  3508. }
  3509. get selectedIndex() {
  3510. return this._selectedIndex;
  3511. }
  3512. set selectedIndex(value) {
  3513. if (this._selectedIndex != value) {
  3514. this._selectedIndex = value;
  3515. if (this._labels.length > 0)
  3516. this.changeSelected();
  3517. else
  3518. this.callLater(this.changeSelected);
  3519. this.event(Laya.Event.CHANGE, [Laya.Event.EMPTY.setTo(Laya.Event.CHANGE, this, this)]);
  3520. this._selectHandler && this._selectHandler.runWith(this._selectedIndex);
  3521. }
  3522. }
  3523. changeSelected() {
  3524. this._button.label = this.selectedLabel;
  3525. }
  3526. get selectHandler() {
  3527. return this._selectHandler;
  3528. }
  3529. set selectHandler(value) {
  3530. this._selectHandler = value;
  3531. }
  3532. get selectedLabel() {
  3533. return this._selectedIndex > -1 && this._selectedIndex < this._labels.length ? this._labels[this._selectedIndex] : null;
  3534. }
  3535. set selectedLabel(value) {
  3536. this.selectedIndex = this._labels.indexOf(value);
  3537. }
  3538. get visibleNum() {
  3539. return this._visibleNum;
  3540. }
  3541. set visibleNum(value) {
  3542. this._visibleNum = value;
  3543. this._listChanged = true;
  3544. }
  3545. get itemColors() {
  3546. return String(this._itemColors);
  3547. }
  3548. set itemColors(value) {
  3549. this._itemColors = UIUtils.fillArray(this._itemColors, value, String);
  3550. this._listChanged = true;
  3551. }
  3552. get itemSize() {
  3553. return this._itemSize;
  3554. }
  3555. set itemSize(value) {
  3556. this._itemSize = value;
  3557. this._listChanged = true;
  3558. }
  3559. get isOpen() {
  3560. return this._isOpen;
  3561. }
  3562. set isOpen(value) {
  3563. if (this._isOpen != value) {
  3564. this._isOpen = value;
  3565. this._button.selected = this._isOpen;
  3566. if (this._isOpen) {
  3567. this._list || this._createList();
  3568. this._listChanged && !this._isCustomList && this.changeList();
  3569. this._itemChanged && this.changeItem();
  3570. var p = this.localToGlobal(Laya.Point.TEMP.setTo(0, 0));
  3571. var py = p.y + this._button.height;
  3572. py = py + this._listHeight <= Laya.ILaya.stage.height ? py : p.y - this._listHeight;
  3573. this._list.pos(p.x, py);
  3574. this._list.zOrder = 1001;
  3575. Laya.ILaya.stage.addChild(this._list);
  3576. Laya.ILaya.stage.once(Laya.Event.MOUSE_DOWN, this, this.removeList);
  3577. Laya.ILaya.stage.on(Laya.Event.MOUSE_WHEEL, this, this._onStageMouseWheel);
  3578. this._list.selectedIndex = this._selectedIndex;
  3579. }
  3580. else {
  3581. this._list && this._list.removeSelf();
  3582. }
  3583. }
  3584. }
  3585. _onStageMouseWheel(e) {
  3586. if (!this._list || this._list.contains(e.target))
  3587. return;
  3588. this.removeList(null);
  3589. }
  3590. removeList(e) {
  3591. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this.removeList);
  3592. Laya.ILaya.stage.off(Laya.Event.MOUSE_WHEEL, this, this._onStageMouseWheel);
  3593. this.isOpen = false;
  3594. }
  3595. get scrollBarSkin() {
  3596. return this._scrollBarSkin;
  3597. }
  3598. set scrollBarSkin(value) {
  3599. this._scrollBarSkin = value;
  3600. }
  3601. get sizeGrid() {
  3602. return this._button.sizeGrid;
  3603. }
  3604. set sizeGrid(value) {
  3605. this._button.sizeGrid = value;
  3606. }
  3607. get scrollBar() {
  3608. return this.list.scrollBar;
  3609. }
  3610. get button() {
  3611. return this._button;
  3612. }
  3613. get list() {
  3614. this._list || this._createList();
  3615. return this._list;
  3616. }
  3617. set list(value) {
  3618. if (value) {
  3619. value.removeSelf();
  3620. this._isCustomList = true;
  3621. this._list = value;
  3622. this._setListEvent(value);
  3623. this._itemHeight = value.getCell(0).height + value.spaceY;
  3624. }
  3625. }
  3626. set dataSource(value) {
  3627. this._dataSource = value;
  3628. if (typeof (value) == 'number' || typeof (value) == 'string')
  3629. this.selectedIndex = parseInt(value);
  3630. else if (value instanceof Array)
  3631. this.labels = value.join(",");
  3632. else
  3633. super.dataSource = value;
  3634. }
  3635. get dataSource() {
  3636. return super.dataSource;
  3637. }
  3638. get labelColors() {
  3639. return this._button.labelColors;
  3640. }
  3641. set labelColors(value) {
  3642. if (this._button.labelColors != value) {
  3643. this._button.labelColors = value;
  3644. }
  3645. }
  3646. get labelPadding() {
  3647. return this._button.text.padding.join(",");
  3648. }
  3649. set labelPadding(value) {
  3650. this._button.text.padding = UIUtils.fillArray(Styles.labelPadding, value, Number);
  3651. }
  3652. get labelSize() {
  3653. return this._button.text.fontSize;
  3654. }
  3655. set labelSize(value) {
  3656. this._button.text.fontSize = value;
  3657. }
  3658. get labelBold() {
  3659. return this._button.text.bold;
  3660. }
  3661. set labelBold(value) {
  3662. this._button.text.bold = value;
  3663. }
  3664. get labelFont() {
  3665. return this._button.text.font;
  3666. }
  3667. set labelFont(value) {
  3668. this._button.text.font = value;
  3669. }
  3670. get stateNum() {
  3671. return this._button.stateNum;
  3672. }
  3673. set stateNum(value) {
  3674. this._button.stateNum = value;
  3675. }
  3676. }
  3677. Laya.ILaya.regClass(ComboBox);
  3678. Laya.ClassUtils.regClass("laya.ui.ComboBox", ComboBox);
  3679. Laya.ClassUtils.regClass("Laya.ComboBox", ComboBox);
  3680. class ProgressBar extends UIComponent {
  3681. constructor(skin = null) {
  3682. super();
  3683. this._value = 0.5;
  3684. this.skin = skin;
  3685. }
  3686. destroy(destroyChild = true) {
  3687. super.destroy(destroyChild);
  3688. this._bg && this._bg.destroy(destroyChild);
  3689. this._bar && this._bar.destroy(destroyChild);
  3690. this._bg = this._bar = null;
  3691. this.changeHandler = null;
  3692. }
  3693. createChildren() {
  3694. this.addChild(this._bg = new Image());
  3695. this.addChild(this._bar = new Image());
  3696. this._bar._bitmap.autoCacheCmd = false;
  3697. }
  3698. get skin() {
  3699. return this._skin;
  3700. }
  3701. set skin(value) {
  3702. if (this._skin != value) {
  3703. this._skin = value;
  3704. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  3705. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  3706. }
  3707. else {
  3708. this._skinLoaded();
  3709. }
  3710. }
  3711. }
  3712. _skinLoaded() {
  3713. this._bg.skin = this._skin;
  3714. this._bar.skin = this._skin.replace(".png", "$bar.png");
  3715. this.callLater(this.changeValue);
  3716. this._sizeChanged();
  3717. this.event(Laya.Event.LOADED);
  3718. }
  3719. measureWidth() {
  3720. return this._bg.width;
  3721. }
  3722. measureHeight() {
  3723. return this._bg.height;
  3724. }
  3725. get value() {
  3726. return this._value;
  3727. }
  3728. set value(num) {
  3729. if (this._value != num) {
  3730. num = num > 1 ? 1 : num < 0 ? 0 : num;
  3731. this._value = num;
  3732. this.callLater(this.changeValue);
  3733. this.event(Laya.Event.CHANGE);
  3734. this.changeHandler && this.changeHandler.runWith(num);
  3735. }
  3736. }
  3737. changeValue() {
  3738. if (this.sizeGrid) {
  3739. var grid = this.sizeGrid.split(",");
  3740. var left = Number(grid[3]);
  3741. var right = Number(grid[1]);
  3742. var max = this.width - left - right;
  3743. var sw = max * this._value;
  3744. this._bar.width = left + right + sw;
  3745. this._bar.visible = this._bar.width > left + right;
  3746. }
  3747. else {
  3748. this._bar.width = this.width * this._value;
  3749. }
  3750. }
  3751. get bar() {
  3752. return this._bar;
  3753. }
  3754. get bg() {
  3755. return this._bg;
  3756. }
  3757. get sizeGrid() {
  3758. return this._bg.sizeGrid;
  3759. }
  3760. set sizeGrid(value) {
  3761. this._bg.sizeGrid = this._bar.sizeGrid = value;
  3762. }
  3763. set width(value) {
  3764. super.width = value;
  3765. this._bg.width = this._width;
  3766. this.callLater(this.changeValue);
  3767. }
  3768. get width() {
  3769. return super.width;
  3770. }
  3771. set height(value) {
  3772. super.height = value;
  3773. this._bg.height = this._height;
  3774. this._bar.height = this._height;
  3775. }
  3776. get height() {
  3777. return super.height;
  3778. }
  3779. set dataSource(value) {
  3780. this._dataSource = value;
  3781. if (typeof (value) == 'number' || typeof (value) == 'string')
  3782. this.value = Number(value);
  3783. else
  3784. super.dataSource = value;
  3785. }
  3786. get dataSource() {
  3787. return super.dataSource;
  3788. }
  3789. }
  3790. Laya.ILaya.regClass(ProgressBar);
  3791. Laya.ClassUtils.regClass("laya.ui.ProgressBar", ProgressBar);
  3792. Laya.ClassUtils.regClass("Laya.ProgressBar", ProgressBar);
  3793. class Radio extends Button {
  3794. constructor(skin = null, label = "") {
  3795. super(skin, label);
  3796. this.toggle = false;
  3797. this._autoSize = false;
  3798. }
  3799. destroy(destroyChild = true) {
  3800. super.destroy(destroyChild);
  3801. this._value = null;
  3802. }
  3803. preinitialize() {
  3804. super.preinitialize();
  3805. this.toggle = false;
  3806. this._autoSize = false;
  3807. }
  3808. initialize() {
  3809. super.initialize();
  3810. this.createText();
  3811. this._text.align = "left";
  3812. this._text.valign = "top";
  3813. this._text.width = 0;
  3814. this.on(Laya.Event.CLICK, this, this.onClick);
  3815. }
  3816. onClick(e) {
  3817. this.selected = true;
  3818. }
  3819. get value() {
  3820. return this._value != null ? this._value : this.label;
  3821. }
  3822. set value(obj) {
  3823. this._value = obj;
  3824. }
  3825. }
  3826. Laya.ILaya.regClass(Radio);
  3827. Laya.ClassUtils.regClass("laya.ui.Radio", Radio);
  3828. Laya.ClassUtils.regClass("Laya.Radio", Radio);
  3829. class UIGroup extends Box {
  3830. constructor(labels = null, skin = null) {
  3831. super();
  3832. this._selectedIndex = -1;
  3833. this._direction = "horizontal";
  3834. this._space = 0;
  3835. this.skin = skin;
  3836. this.labels = labels;
  3837. }
  3838. preinitialize() {
  3839. this.mouseEnabled = true;
  3840. }
  3841. destroy(destroyChild = true) {
  3842. super.destroy(destroyChild);
  3843. this._items && (this._items.length = 0);
  3844. this._items = null;
  3845. this.selectHandler = null;
  3846. }
  3847. addItem(item, autoLayOut = true) {
  3848. var display = item;
  3849. var index = this._items.length;
  3850. display.name = "item" + index;
  3851. this.addChild(display);
  3852. this.initItems();
  3853. if (autoLayOut && index > 0) {
  3854. var preItem = this._items[index - 1];
  3855. if (this._direction == "horizontal") {
  3856. display.x = preItem._x + preItem.width + this._space;
  3857. }
  3858. else {
  3859. display.y = preItem._y + preItem.height + this._space;
  3860. }
  3861. }
  3862. else {
  3863. if (autoLayOut) {
  3864. display.x = 0;
  3865. display.y = 0;
  3866. }
  3867. }
  3868. return index;
  3869. }
  3870. delItem(item, autoLayOut = true) {
  3871. var index = this._items.indexOf(item);
  3872. if (index != -1) {
  3873. var display = item;
  3874. this.removeChild(display);
  3875. for (var i = index + 1, n = this._items.length; i < n; i++) {
  3876. var child = this._items[i];
  3877. child.name = "item" + (i - 1);
  3878. if (autoLayOut) {
  3879. if (this._direction == "horizontal") {
  3880. child.x -= display.width + this._space;
  3881. }
  3882. else {
  3883. child.y -= display.height + this._space;
  3884. }
  3885. }
  3886. }
  3887. this.initItems();
  3888. if (this._selectedIndex > -1) {
  3889. var newIndex;
  3890. newIndex = this._selectedIndex < this._items.length ? this._selectedIndex : (this._selectedIndex - 1);
  3891. this._selectedIndex = -1;
  3892. this.selectedIndex = newIndex;
  3893. }
  3894. }
  3895. }
  3896. _afterInited() {
  3897. this.initItems();
  3898. }
  3899. initItems() {
  3900. this._items || (this._items = []);
  3901. this._items.length = 0;
  3902. for (var i = 0; i < 10000; i++) {
  3903. var item = this.getChildByName("item" + i);
  3904. if (item == null)
  3905. break;
  3906. this._items.push(item);
  3907. item.selected = (i === this._selectedIndex);
  3908. item.clickHandler = Laya.Handler.create(this, this.itemClick, [i], false);
  3909. }
  3910. }
  3911. itemClick(index) {
  3912. this.selectedIndex = index;
  3913. }
  3914. get selectedIndex() {
  3915. return this._selectedIndex;
  3916. }
  3917. set selectedIndex(value) {
  3918. if (this._selectedIndex != value) {
  3919. this.setSelect(this._selectedIndex, false);
  3920. this._selectedIndex = value;
  3921. this.setSelect(value, true);
  3922. this.event(Laya.Event.CHANGE);
  3923. this.selectHandler && this.selectHandler.runWith(this._selectedIndex);
  3924. }
  3925. }
  3926. setSelect(index, selected) {
  3927. if (this._items && index > -1 && index < this._items.length)
  3928. this._items[index].selected = selected;
  3929. }
  3930. get skin() {
  3931. return this._skin;
  3932. }
  3933. set skin(value) {
  3934. if (this._skin != value) {
  3935. this._skin = value;
  3936. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  3937. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  3938. }
  3939. else {
  3940. this._skinLoaded();
  3941. }
  3942. }
  3943. }
  3944. _skinLoaded() {
  3945. this._setLabelChanged();
  3946. this.event(Laya.Event.LOADED);
  3947. }
  3948. get labels() {
  3949. return this._labels;
  3950. }
  3951. set labels(value) {
  3952. if (this._labels != value) {
  3953. this._labels = value;
  3954. this.removeChildren();
  3955. this._setLabelChanged();
  3956. if (this._labels) {
  3957. var a = this._labels.split(",");
  3958. for (var i = 0, n = a.length; i < n; i++) {
  3959. var item = this.createItem(this._skin, a[i]);
  3960. item.name = "item" + i;
  3961. this.addChild(item);
  3962. }
  3963. }
  3964. this.initItems();
  3965. }
  3966. }
  3967. createItem(skin, label) {
  3968. return null;
  3969. }
  3970. get labelColors() {
  3971. return this._labelColors;
  3972. }
  3973. set labelColors(value) {
  3974. if (this._labelColors != value) {
  3975. this._labelColors = value;
  3976. this._setLabelChanged();
  3977. }
  3978. }
  3979. get labelStroke() {
  3980. return this._labelStroke;
  3981. }
  3982. set labelStroke(value) {
  3983. if (this._labelStroke != value) {
  3984. this._labelStroke = value;
  3985. this._setLabelChanged();
  3986. }
  3987. }
  3988. get labelStrokeColor() {
  3989. return this._labelStrokeColor;
  3990. }
  3991. set labelStrokeColor(value) {
  3992. if (this._labelStrokeColor != value) {
  3993. this._labelStrokeColor = value;
  3994. this._setLabelChanged();
  3995. }
  3996. }
  3997. get strokeColors() {
  3998. return this._strokeColors;
  3999. }
  4000. set strokeColors(value) {
  4001. if (this._strokeColors != value) {
  4002. this._strokeColors = value;
  4003. this._setLabelChanged();
  4004. }
  4005. }
  4006. get labelSize() {
  4007. return this._labelSize;
  4008. }
  4009. set labelSize(value) {
  4010. if (this._labelSize != value) {
  4011. this._labelSize = value;
  4012. this._setLabelChanged();
  4013. }
  4014. }
  4015. get stateNum() {
  4016. return this._stateNum;
  4017. }
  4018. set stateNum(value) {
  4019. if (this._stateNum != value) {
  4020. this._stateNum = value;
  4021. this._setLabelChanged();
  4022. }
  4023. }
  4024. get labelBold() {
  4025. return this._labelBold;
  4026. }
  4027. set labelBold(value) {
  4028. if (this._labelBold != value) {
  4029. this._labelBold = value;
  4030. this._setLabelChanged();
  4031. }
  4032. }
  4033. get labelFont() {
  4034. return this._labelFont;
  4035. }
  4036. set labelFont(value) {
  4037. if (this._labelFont != value) {
  4038. this._labelFont = value;
  4039. this._setLabelChanged();
  4040. }
  4041. }
  4042. get labelPadding() {
  4043. return this._labelPadding;
  4044. }
  4045. set labelPadding(value) {
  4046. if (this._labelPadding != value) {
  4047. this._labelPadding = value;
  4048. this._setLabelChanged();
  4049. }
  4050. }
  4051. get direction() {
  4052. return this._direction;
  4053. }
  4054. set direction(value) {
  4055. this._direction = value;
  4056. this._setLabelChanged();
  4057. }
  4058. get space() {
  4059. return this._space;
  4060. }
  4061. set space(value) {
  4062. this._space = value;
  4063. this._setLabelChanged();
  4064. }
  4065. changeLabels() {
  4066. this._labelChanged = false;
  4067. if (this._items) {
  4068. var left = 0;
  4069. for (var i = 0, n = this._items.length; i < n; i++) {
  4070. var btn = this._items[i];
  4071. this._skin && (btn.skin = this._skin);
  4072. this._labelColors && (btn.labelColors = this._labelColors);
  4073. this._labelSize && (btn.labelSize = this._labelSize);
  4074. this._labelStroke && (btn.labelStroke = this._labelStroke);
  4075. this._labelStrokeColor && (btn.labelStrokeColor = this._labelStrokeColor);
  4076. this._strokeColors && (btn.strokeColors = this._strokeColors);
  4077. this._labelBold && (btn.labelBold = this._labelBold);
  4078. this._labelPadding && (btn.labelPadding = this._labelPadding);
  4079. this._labelAlign && (btn.labelAlign = this._labelAlign);
  4080. this._stateNum && (btn.stateNum = this._stateNum);
  4081. this._labelFont && (btn.labelFont = this._labelFont);
  4082. if (this._direction === "horizontal") {
  4083. btn.y = 0;
  4084. btn.x = left;
  4085. left += btn.width + this._space;
  4086. }
  4087. else {
  4088. btn.x = 0;
  4089. btn.y = left;
  4090. left += btn.height + this._space;
  4091. }
  4092. }
  4093. }
  4094. this._sizeChanged();
  4095. }
  4096. commitMeasure() {
  4097. this.runCallLater(this.changeLabels);
  4098. }
  4099. get items() {
  4100. return this._items;
  4101. }
  4102. get selection() {
  4103. return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? this._items[this._selectedIndex] : null;
  4104. }
  4105. set selection(value) {
  4106. this.selectedIndex = this._items.indexOf(value);
  4107. }
  4108. set dataSource(value) {
  4109. this._dataSource = value;
  4110. if (typeof (value) == 'number' || typeof (value) == 'string')
  4111. this.selectedIndex = parseInt(value);
  4112. else if (value instanceof Array)
  4113. this.labels = value.join(",");
  4114. else
  4115. super.dataSource = value;
  4116. }
  4117. get dataSource() {
  4118. return super.dataSource;
  4119. }
  4120. _setLabelChanged() {
  4121. if (!this._labelChanged) {
  4122. this._labelChanged = true;
  4123. this.callLater(this.changeLabels);
  4124. }
  4125. }
  4126. }
  4127. Laya.ILaya.regClass(UIGroup);
  4128. Laya.ClassUtils.regClass("laya.ui.UIGroup", UIGroup);
  4129. Laya.ClassUtils.regClass("Laya.UIGroup", UIGroup);
  4130. class RadioGroup extends UIGroup {
  4131. createItem(skin, label) {
  4132. return new Radio(skin, label);
  4133. }
  4134. }
  4135. Laya.ILaya.regClass(RadioGroup);
  4136. Laya.ClassUtils.regClass("laya.ui.RadioGroup", RadioGroup);
  4137. Laya.ClassUtils.regClass("Laya.RadioGroup", RadioGroup);
  4138. class Tab extends UIGroup {
  4139. createItem(skin, label) {
  4140. return new Button(skin, label);
  4141. }
  4142. }
  4143. Laya.ILaya.regClass(Tab);
  4144. Laya.ClassUtils.regClass("laya.ui.Tab", Tab);
  4145. Laya.ClassUtils.regClass("Laya.Tab", Tab);
  4146. class ViewStack extends Box {
  4147. constructor() {
  4148. super(...arguments);
  4149. this._setIndexHandler = Laya.Handler.create(this, this.setIndex, null, false);
  4150. }
  4151. setItems(views) {
  4152. this.removeChildren();
  4153. var index = 0;
  4154. for (var i = 0, n = views.length; i < n; i++) {
  4155. var item = views[i];
  4156. if (item) {
  4157. item.name = "item" + index;
  4158. this.addChild(item);
  4159. index++;
  4160. }
  4161. }
  4162. this.initItems();
  4163. }
  4164. addItem(view) {
  4165. view.name = "item" + this._items.length;
  4166. this.addChild(view);
  4167. this.initItems();
  4168. }
  4169. _afterInited() {
  4170. this.initItems();
  4171. }
  4172. initItems() {
  4173. this._items = [];
  4174. for (var i = 0; i < 10000; i++) {
  4175. var item = this.getChildByName("item" + i);
  4176. if (item == null) {
  4177. break;
  4178. }
  4179. this._items.push(item);
  4180. item.visible = (i == this._selectedIndex);
  4181. }
  4182. }
  4183. get selectedIndex() {
  4184. return this._selectedIndex;
  4185. }
  4186. set selectedIndex(value) {
  4187. if (this._selectedIndex != value) {
  4188. this.setSelect(this._selectedIndex, false);
  4189. this._selectedIndex = value;
  4190. this.setSelect(this._selectedIndex, true);
  4191. }
  4192. }
  4193. setSelect(index, selected) {
  4194. if (this._items && index > -1 && index < this._items.length) {
  4195. this._items[index].visible = selected;
  4196. }
  4197. }
  4198. get selection() {
  4199. return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? this._items[this._selectedIndex] : null;
  4200. }
  4201. set selection(value) {
  4202. this.selectedIndex = this._items.indexOf(value);
  4203. }
  4204. get setIndexHandler() {
  4205. return this._setIndexHandler;
  4206. }
  4207. set setIndexHandler(value) {
  4208. this._setIndexHandler = value;
  4209. }
  4210. setIndex(index) {
  4211. this.selectedIndex = index;
  4212. }
  4213. get items() {
  4214. return this._items;
  4215. }
  4216. set dataSource(value) {
  4217. this._dataSource = value;
  4218. if (typeof (value) == 'number' || typeof (value) == 'string') {
  4219. this.selectedIndex = parseInt(value);
  4220. }
  4221. else {
  4222. for (var prop in this._dataSource) {
  4223. if (prop in this) {
  4224. this[prop] = this._dataSource[prop];
  4225. }
  4226. }
  4227. }
  4228. }
  4229. get dataSource() {
  4230. return super.dataSource;
  4231. }
  4232. }
  4233. Laya.ILaya.regClass(ViewStack);
  4234. Laya.ClassUtils.regClass("laya.ui.ViewStack", ViewStack);
  4235. Laya.ClassUtils.regClass("Laya.ViewStack", ViewStack);
  4236. class TextInput extends Label {
  4237. constructor(text = "") {
  4238. super();
  4239. this.text = text;
  4240. this.skin = this.skin;
  4241. }
  4242. preinitialize() {
  4243. this.mouseEnabled = true;
  4244. }
  4245. destroy(destroyChild = true) {
  4246. super.destroy(destroyChild);
  4247. this._bg && this._bg.destroy();
  4248. this._bg = null;
  4249. }
  4250. createChildren() {
  4251. this.addChild(this._tf = new Laya.Input());
  4252. this._tf.padding = Styles.inputLabelPadding;
  4253. this._tf.on(Laya.Event.INPUT, this, this._onInput);
  4254. this._tf.on(Laya.Event.ENTER, this, this._onEnter);
  4255. this._tf.on(Laya.Event.BLUR, this, this._onBlur);
  4256. this._tf.on(Laya.Event.FOCUS, this, this._onFocus);
  4257. }
  4258. _onFocus() {
  4259. this.event(Laya.Event.FOCUS, this);
  4260. }
  4261. _onBlur() {
  4262. this.event(Laya.Event.BLUR, this);
  4263. }
  4264. _onInput() {
  4265. this.event(Laya.Event.INPUT, this);
  4266. }
  4267. _onEnter() {
  4268. this.event(Laya.Event.ENTER, this);
  4269. }
  4270. initialize() {
  4271. this.width = 128;
  4272. this.height = 22;
  4273. }
  4274. get bg() {
  4275. return this._bg;
  4276. }
  4277. set bg(value) {
  4278. this.graphics = this._bg = value;
  4279. }
  4280. get skin() {
  4281. return this._skin;
  4282. }
  4283. set skin(value) {
  4284. if (this._skin != value) {
  4285. this._skin = value;
  4286. if (this._skin && !Laya.Loader.getRes(this._skin)) {
  4287. Laya.ILaya.loader.load(this._skin, Laya.Handler.create(this, this._skinLoaded), null, Laya.Loader.IMAGE, 1);
  4288. }
  4289. else {
  4290. this._skinLoaded();
  4291. }
  4292. }
  4293. }
  4294. _skinLoaded() {
  4295. this._bg || (this.graphics = this._bg = new AutoBitmap());
  4296. this._bg.source = Laya.Loader.getRes(this._skin);
  4297. this._width && (this._bg.width = this._width);
  4298. this._height && (this._bg.height = this._height);
  4299. this._sizeChanged();
  4300. this.event(Laya.Event.LOADED);
  4301. }
  4302. get sizeGrid() {
  4303. return this._bg && this._bg.sizeGrid ? this._bg.sizeGrid.join(",") : null;
  4304. }
  4305. set sizeGrid(value) {
  4306. this._bg || (this.graphics = this._bg = new AutoBitmap());
  4307. this._bg.sizeGrid = UIUtils.fillArray(Styles.defaultSizeGrid, value, Number);
  4308. }
  4309. set text(value) {
  4310. if (this._tf.text != value) {
  4311. value = value + "";
  4312. this._tf.text = value;
  4313. this.event(Laya.Event.CHANGE);
  4314. }
  4315. }
  4316. get text() {
  4317. return super.text;
  4318. }
  4319. set width(value) {
  4320. super.width = value;
  4321. this._bg && (this._bg.width = value);
  4322. }
  4323. get width() {
  4324. return super.width;
  4325. }
  4326. set height(value) {
  4327. super.height = value;
  4328. this._bg && (this._bg.height = value);
  4329. }
  4330. get height() {
  4331. return super.height;
  4332. }
  4333. get multiline() {
  4334. return this._tf.multiline;
  4335. }
  4336. set multiline(value) {
  4337. this._tf.multiline = value;
  4338. }
  4339. set editable(value) {
  4340. this._tf.editable = value;
  4341. }
  4342. get editable() {
  4343. return this._tf.editable;
  4344. }
  4345. select() {
  4346. this._tf.select();
  4347. }
  4348. get restrict() {
  4349. return this._tf.restrict;
  4350. }
  4351. set restrict(pattern) {
  4352. this._tf.restrict = pattern;
  4353. }
  4354. get prompt() {
  4355. return this._tf.prompt;
  4356. }
  4357. set prompt(value) {
  4358. this._tf.prompt = value;
  4359. }
  4360. get promptColor() {
  4361. return this._tf.promptColor;
  4362. }
  4363. set promptColor(value) {
  4364. this._tf.promptColor = value;
  4365. }
  4366. get maxChars() {
  4367. return this._tf.maxChars;
  4368. }
  4369. set maxChars(value) {
  4370. this._tf.maxChars = value;
  4371. }
  4372. get focus() {
  4373. return this._tf.focus;
  4374. }
  4375. set focus(value) {
  4376. this._tf.focus = value;
  4377. }
  4378. get type() {
  4379. return this._tf.type;
  4380. }
  4381. set type(value) {
  4382. this._tf.type = value;
  4383. }
  4384. setSelection(startIndex, endIndex) {
  4385. this._tf.setSelection(startIndex, endIndex);
  4386. }
  4387. }
  4388. Laya.ILaya.regClass(TextInput);
  4389. Laya.ClassUtils.regClass("laya.ui.TextInput", TextInput);
  4390. Laya.ClassUtils.regClass("Laya.TextInput", TextInput);
  4391. class TextArea extends TextInput {
  4392. constructor(text = "") {
  4393. super(text);
  4394. this.on(Laya.Event.CHANGE, this, this._onTextChange);
  4395. }
  4396. _onTextChange() {
  4397. this.callLater(this.changeScroll);
  4398. }
  4399. destroy(destroyChild = true) {
  4400. this._vScrollBar && this._vScrollBar.destroy();
  4401. this._hScrollBar && this._hScrollBar.destroy();
  4402. this._vScrollBar = null;
  4403. this._hScrollBar = null;
  4404. super.destroy(destroyChild);
  4405. }
  4406. initialize() {
  4407. this.width = 180;
  4408. this.height = 150;
  4409. this._tf.wordWrap = true;
  4410. this.multiline = true;
  4411. }
  4412. set width(value) {
  4413. super.width = value;
  4414. this.callLater(this.changeScroll);
  4415. }
  4416. get width() {
  4417. return super.width;
  4418. }
  4419. set height(value) {
  4420. super.height = value;
  4421. this.callLater(this.changeScroll);
  4422. }
  4423. get height() {
  4424. return super.height;
  4425. }
  4426. get vScrollBarSkin() {
  4427. return this._vScrollBar ? this._vScrollBar.skin : null;
  4428. }
  4429. set vScrollBarSkin(value) {
  4430. if (this._vScrollBar == null) {
  4431. this.addChild(this._vScrollBar = new VScrollBar());
  4432. this._vScrollBar.on(Laya.Event.CHANGE, this, this.onVBarChanged);
  4433. this._vScrollBar.target = this._tf;
  4434. this.callLater(this.changeScroll);
  4435. }
  4436. this._vScrollBar.skin = value;
  4437. }
  4438. get hScrollBarSkin() {
  4439. return this._hScrollBar ? this._hScrollBar.skin : null;
  4440. }
  4441. set hScrollBarSkin(value) {
  4442. if (this._hScrollBar == null) {
  4443. this.addChild(this._hScrollBar = new HScrollBar());
  4444. this._hScrollBar.on(Laya.Event.CHANGE, this, this.onHBarChanged);
  4445. this._hScrollBar.mouseWheelEnable = false;
  4446. this._hScrollBar.target = this._tf;
  4447. this.callLater(this.changeScroll);
  4448. }
  4449. this._hScrollBar.skin = value;
  4450. }
  4451. onVBarChanged(e) {
  4452. if (this._tf.scrollY != this._vScrollBar.value) {
  4453. this._tf.scrollY = this._vScrollBar.value;
  4454. }
  4455. }
  4456. onHBarChanged(e) {
  4457. if (this._tf.scrollX != this._hScrollBar.value) {
  4458. this._tf.scrollX = this._hScrollBar.value;
  4459. }
  4460. }
  4461. get vScrollBar() {
  4462. return this._vScrollBar;
  4463. }
  4464. get hScrollBar() {
  4465. return this._hScrollBar;
  4466. }
  4467. get maxScrollY() {
  4468. return this._tf.maxScrollY;
  4469. }
  4470. get scrollY() {
  4471. return this._tf.scrollY;
  4472. }
  4473. get maxScrollX() {
  4474. return this._tf.maxScrollX;
  4475. }
  4476. get scrollX() {
  4477. return this._tf.scrollX;
  4478. }
  4479. changeScroll() {
  4480. var vShow = this._vScrollBar && this._tf.maxScrollY > 0;
  4481. var hShow = this._hScrollBar && this._tf.maxScrollX > 0;
  4482. var showWidth = vShow ? this._width - this._vScrollBar.width : this._width;
  4483. var showHeight = hShow ? this._height - this._hScrollBar.height : this._height;
  4484. var padding = this._tf.padding || Styles.labelPadding;
  4485. this._tf.width = showWidth;
  4486. this._tf.height = showHeight;
  4487. if (this._vScrollBar) {
  4488. this._vScrollBar.x = this._width - this._vScrollBar.width - padding[2];
  4489. this._vScrollBar.y = padding[1];
  4490. this._vScrollBar.height = this._height - (hShow ? this._hScrollBar.height : 0) - padding[1] - padding[3];
  4491. this._vScrollBar.scrollSize = 1;
  4492. this._vScrollBar.thumbPercent = showHeight / Math.max(this._tf.textHeight, showHeight);
  4493. this._vScrollBar.setScroll(1, this._tf.maxScrollY, this._tf.scrollY);
  4494. this._vScrollBar.visible = vShow;
  4495. }
  4496. if (this._hScrollBar) {
  4497. this._hScrollBar.x = padding[0];
  4498. this._hScrollBar.y = this._height - this._hScrollBar.height - padding[3];
  4499. this._hScrollBar.width = this._width - (vShow ? this._vScrollBar.width : 0) - padding[0] - padding[2];
  4500. this._hScrollBar.scrollSize = Math.max(showWidth * 0.033, 1);
  4501. this._hScrollBar.thumbPercent = showWidth / Math.max(this._tf.textWidth, showWidth);
  4502. this._hScrollBar.setScroll(0, this.maxScrollX, this.scrollX);
  4503. this._hScrollBar.visible = hShow;
  4504. }
  4505. }
  4506. scrollTo(y) {
  4507. this.commitMeasure();
  4508. this._tf.scrollY = y;
  4509. }
  4510. }
  4511. Laya.ILaya.regClass(TextArea);
  4512. Laya.ClassUtils.regClass("laya.ui.TextArea", TextArea);
  4513. Laya.ClassUtils.regClass("Laya.TextArea", TextArea);
  4514. class ScaleBox extends Box {
  4515. constructor() {
  4516. super(...arguments);
  4517. this._oldW = 0;
  4518. this._oldH = 0;
  4519. }
  4520. onEnable() {
  4521. Laya.ILaya.stage.on("resize", this, this.onResize);
  4522. this.onResize();
  4523. }
  4524. onDisable() {
  4525. Laya.ILaya.stage.off("resize", this, this.onResize);
  4526. }
  4527. onResize() {
  4528. let stage = Laya.ILaya.stage;
  4529. if (this.width > 0 && this.height > 0) {
  4530. var scale = Math.min(stage.width / this._oldW, stage.height / this._oldH);
  4531. super.width = stage.width;
  4532. super.height = stage.height;
  4533. this.scale(scale, scale);
  4534. }
  4535. }
  4536. set width(value) {
  4537. super.width = value;
  4538. this._oldW = value;
  4539. }
  4540. get width() {
  4541. return super.width;
  4542. }
  4543. set height(value) {
  4544. super.height = value;
  4545. this._oldH = value;
  4546. }
  4547. get height() {
  4548. return super.height;
  4549. }
  4550. }
  4551. Laya.ILaya.regClass(ScaleBox);
  4552. Laya.ClassUtils.regClass("laya.ui.ScaleBox", ScaleBox);
  4553. Laya.ClassUtils.regClass("Laya.ScaleBox", ScaleBox);
  4554. class HSlider extends Slider {
  4555. constructor(skin = null) {
  4556. super(skin);
  4557. this.isVertical = false;
  4558. }
  4559. }
  4560. Laya.ILaya.regClass(HSlider);
  4561. Laya.ClassUtils.regClass("laya.ui.HSlider", HSlider);
  4562. Laya.ClassUtils.regClass("Laya.HSlider", HSlider);
  4563. class Panel extends Box {
  4564. constructor() {
  4565. super();
  4566. this._usedCache = null;
  4567. this._elasticEnabled = false;
  4568. this.width = this.height = 100;
  4569. }
  4570. destroy(destroyChild = true) {
  4571. super.destroy(destroyChild);
  4572. this._content && this._content.destroy(destroyChild);
  4573. this._vScrollBar && this._vScrollBar.destroy(destroyChild);
  4574. this._hScrollBar && this._hScrollBar.destroy(destroyChild);
  4575. this._vScrollBar = null;
  4576. this._hScrollBar = null;
  4577. this._content = null;
  4578. }
  4579. destroyChildren() {
  4580. this._content.destroyChildren();
  4581. }
  4582. createChildren() {
  4583. super.addChild(this._content = new Box());
  4584. }
  4585. addChild(child) {
  4586. child.on(Laya.Event.RESIZE, this, this.onResize);
  4587. this._setScrollChanged();
  4588. return this._content.addChild(child);
  4589. }
  4590. onResize() {
  4591. this._setScrollChanged();
  4592. }
  4593. addChildAt(child, index) {
  4594. child.on(Laya.Event.RESIZE, this, this.onResize);
  4595. this._setScrollChanged();
  4596. return this._content.addChildAt(child, index);
  4597. }
  4598. removeChild(child) {
  4599. child.off(Laya.Event.RESIZE, this, this.onResize);
  4600. this._setScrollChanged();
  4601. return this._content.removeChild(child);
  4602. }
  4603. removeChildAt(index) {
  4604. this.getChildAt(index).off(Laya.Event.RESIZE, this, this.onResize);
  4605. this._setScrollChanged();
  4606. return this._content.removeChildAt(index);
  4607. }
  4608. removeChildren(beginIndex = 0, endIndex = 0x7fffffff) {
  4609. this._content.removeChildren(beginIndex, endIndex);
  4610. this._setScrollChanged();
  4611. return this;
  4612. }
  4613. getChildAt(index) {
  4614. return this._content.getChildAt(index);
  4615. }
  4616. getChildByName(name) {
  4617. return this._content.getChildByName(name);
  4618. }
  4619. getChildIndex(child) {
  4620. return this._content.getChildIndex(child);
  4621. }
  4622. get numChildren() {
  4623. return this._content.numChildren;
  4624. }
  4625. changeScroll() {
  4626. this._scrollChanged = false;
  4627. var contentW = this.contentWidth || 1;
  4628. var contentH = this.contentHeight || 1;
  4629. var vscroll = this._vScrollBar;
  4630. var hscroll = this._hScrollBar;
  4631. var vShow = vscroll && contentH > this._height;
  4632. var hShow = hscroll && contentW > this._width;
  4633. var showWidth = vShow ? this._width - vscroll.width : this._width;
  4634. var showHeight = hShow ? this._height - hscroll.height : this._height;
  4635. if (vscroll) {
  4636. vscroll.x = this._width - vscroll.width;
  4637. vscroll.y = 0;
  4638. vscroll.height = this._height - (hShow ? hscroll.height : 0);
  4639. vscroll.scrollSize = Math.max(this._height * 0.033, 1);
  4640. vscroll.thumbPercent = showHeight / contentH;
  4641. vscroll.setScroll(0, contentH - showHeight, vscroll.value);
  4642. }
  4643. if (hscroll) {
  4644. hscroll.x = 0;
  4645. hscroll.y = this._height - hscroll.height;
  4646. hscroll.width = this._width - (vShow ? vscroll.width : 0);
  4647. hscroll.scrollSize = Math.max(this._width * 0.033, 1);
  4648. hscroll.thumbPercent = showWidth / contentW;
  4649. hscroll.setScroll(0, contentW - showWidth, hscroll.value);
  4650. }
  4651. }
  4652. _sizeChanged() {
  4653. super._sizeChanged();
  4654. this.setContentSize(this._width, this._height);
  4655. }
  4656. get contentWidth() {
  4657. var max = 0;
  4658. for (var i = this._content.numChildren - 1; i > -1; i--) {
  4659. var comp = this._content.getChildAt(i);
  4660. max = Math.max(comp._x + comp.width * comp.scaleX - comp.pivotX, max);
  4661. }
  4662. return max;
  4663. }
  4664. get contentHeight() {
  4665. var max = 0;
  4666. for (var i = this._content.numChildren - 1; i > -1; i--) {
  4667. var comp = this._content.getChildAt(i);
  4668. max = Math.max(comp._y + comp.height * comp.scaleY - comp.pivotY, max);
  4669. }
  4670. return max;
  4671. }
  4672. setContentSize(width, height) {
  4673. var content = this._content;
  4674. content.width = width;
  4675. content.height = height;
  4676. content._style.scrollRect || (content.scrollRect = Laya.Rectangle.create());
  4677. content._style.scrollRect.setTo(0, 0, width, height);
  4678. content.scrollRect = content.scrollRect;
  4679. }
  4680. set width(value) {
  4681. super.width = value;
  4682. this._setScrollChanged();
  4683. }
  4684. get width() {
  4685. return super.width;
  4686. }
  4687. set height(value) {
  4688. super.height = value;
  4689. this._setScrollChanged();
  4690. }
  4691. get height() {
  4692. return super.height;
  4693. }
  4694. get vScrollBarSkin() {
  4695. return this._vScrollBar ? this._vScrollBar.skin : null;
  4696. }
  4697. set vScrollBarSkin(value) {
  4698. if (this._vScrollBar == null) {
  4699. super.addChild(this._vScrollBar = new VScrollBar());
  4700. this._vScrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange, [this._vScrollBar]);
  4701. this._vScrollBar.target = this._content;
  4702. this._vScrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  4703. this._setScrollChanged();
  4704. }
  4705. this._vScrollBar.skin = value;
  4706. }
  4707. get hScrollBarSkin() {
  4708. return this._hScrollBar ? this._hScrollBar.skin : null;
  4709. }
  4710. set hScrollBarSkin(value) {
  4711. if (this._hScrollBar == null) {
  4712. super.addChild(this._hScrollBar = new HScrollBar());
  4713. this._hScrollBar.on(Laya.Event.CHANGE, this, this.onScrollBarChange, [this._hScrollBar]);
  4714. this._hScrollBar.target = this._content;
  4715. this._hScrollBar.elasticDistance = this._elasticEnabled ? 200 : 0;
  4716. this._setScrollChanged();
  4717. }
  4718. this._hScrollBar.skin = value;
  4719. }
  4720. get vScrollBar() {
  4721. return this._vScrollBar;
  4722. }
  4723. get hScrollBar() {
  4724. return this._hScrollBar;
  4725. }
  4726. get content() {
  4727. return this._content;
  4728. }
  4729. onScrollBarChange(scrollBar) {
  4730. var rect = this._content._style.scrollRect;
  4731. if (rect) {
  4732. var start = Math.round(scrollBar.value);
  4733. scrollBar.isVertical ? rect.y = start : rect.x = start;
  4734. this._content.scrollRect = rect;
  4735. }
  4736. }
  4737. scrollTo(x = 0, y = 0) {
  4738. if (this.vScrollBar)
  4739. this.vScrollBar.value = y;
  4740. if (this.hScrollBar)
  4741. this.hScrollBar.value = x;
  4742. }
  4743. refresh() {
  4744. this.changeScroll();
  4745. }
  4746. set cacheAs(value) {
  4747. super.cacheAs = value;
  4748. this._usedCache = null;
  4749. if (value !== "none") {
  4750. this._hScrollBar && this._hScrollBar.on(Laya.Event.START, this, this.onScrollStart);
  4751. this._vScrollBar && this._vScrollBar.on(Laya.Event.START, this, this.onScrollStart);
  4752. }
  4753. else {
  4754. this._hScrollBar && this._hScrollBar.off(Laya.Event.START, this, this.onScrollStart);
  4755. this._vScrollBar && this._vScrollBar.off(Laya.Event.START, this, this.onScrollStart);
  4756. }
  4757. }
  4758. get cacheAs() {
  4759. return super.cacheAs;
  4760. }
  4761. get elasticEnabled() {
  4762. return this._elasticEnabled;
  4763. }
  4764. set elasticEnabled(value) {
  4765. this._elasticEnabled = value;
  4766. if (this._vScrollBar) {
  4767. this._vScrollBar.elasticDistance = value ? 200 : 0;
  4768. }
  4769. if (this._hScrollBar) {
  4770. this._hScrollBar.elasticDistance = value ? 200 : 0;
  4771. }
  4772. }
  4773. onScrollStart() {
  4774. this._usedCache || (this._usedCache = super.cacheAs);
  4775. super.cacheAs = "none";
  4776. this._hScrollBar && this._hScrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  4777. this._vScrollBar && this._vScrollBar.once(Laya.Event.END, this, this.onScrollEnd);
  4778. }
  4779. onScrollEnd() {
  4780. super.cacheAs = this._usedCache;
  4781. }
  4782. _setScrollChanged() {
  4783. if (!this._scrollChanged) {
  4784. this._scrollChanged = true;
  4785. this.callLater(this.changeScroll);
  4786. }
  4787. }
  4788. }
  4789. Laya.ILaya.regClass(Panel);
  4790. Laya.ClassUtils.regClass("laya.ui.Panel", Panel);
  4791. Laya.ClassUtils.regClass("Laya.Panel", Panel);
  4792. class VSlider extends Slider {
  4793. }
  4794. Laya.ILaya.regClass(VSlider);
  4795. Laya.ClassUtils.regClass("laya.ui.VSlider", VSlider);
  4796. Laya.ClassUtils.regClass("Laya.VSlider", VSlider);
  4797. class Tree extends Box {
  4798. constructor() {
  4799. super();
  4800. this._spaceLeft = 10;
  4801. this._spaceBottom = 0;
  4802. this._keepStatus = true;
  4803. this.width = this.height = 200;
  4804. }
  4805. destroy(destroyChild = true) {
  4806. super.destroy(destroyChild);
  4807. this._list && this._list.destroy(destroyChild);
  4808. this._list = null;
  4809. this._source = null;
  4810. this._renderHandler = null;
  4811. }
  4812. createChildren() {
  4813. this.addChild(this._list = new List());
  4814. this._list.renderHandler = Laya.Handler.create(this, this.renderItem, null, false);
  4815. this._list.repeatX = 1;
  4816. this._list.on(Laya.Event.CHANGE, this, this.onListChange);
  4817. }
  4818. onListChange(e = null) {
  4819. this.event(Laya.Event.CHANGE);
  4820. }
  4821. get keepStatus() {
  4822. return this._keepStatus;
  4823. }
  4824. set keepStatus(value) {
  4825. this._keepStatus = value;
  4826. }
  4827. get array() {
  4828. return this._list.array;
  4829. }
  4830. set array(value) {
  4831. if (this._keepStatus && this._list.array && value) {
  4832. this.parseOpenStatus(this._list.array, value);
  4833. }
  4834. this._source = value;
  4835. this._list.array = this.getArray();
  4836. }
  4837. get source() {
  4838. return this._source;
  4839. }
  4840. get list() {
  4841. return this._list;
  4842. }
  4843. get itemRender() {
  4844. return this._list.itemRender;
  4845. }
  4846. set itemRender(value) {
  4847. this._list.itemRender = value;
  4848. }
  4849. get scrollBarSkin() {
  4850. return this._list.vScrollBarSkin;
  4851. }
  4852. set scrollBarSkin(value) {
  4853. this._list.vScrollBarSkin = value;
  4854. }
  4855. get scrollBar() {
  4856. return this._list.scrollBar;
  4857. }
  4858. get mouseHandler() {
  4859. return this._list.mouseHandler;
  4860. }
  4861. set mouseHandler(value) {
  4862. this._list.mouseHandler = value;
  4863. }
  4864. get renderHandler() {
  4865. return this._renderHandler;
  4866. }
  4867. set renderHandler(value) {
  4868. this._renderHandler = value;
  4869. }
  4870. get spaceLeft() {
  4871. return this._spaceLeft;
  4872. }
  4873. set spaceLeft(value) {
  4874. this._spaceLeft = value;
  4875. }
  4876. get spaceBottom() {
  4877. return this._list.spaceY;
  4878. }
  4879. set spaceBottom(value) {
  4880. this._list.spaceY = value;
  4881. }
  4882. get selectedIndex() {
  4883. return this._list.selectedIndex;
  4884. }
  4885. set selectedIndex(value) {
  4886. this._list.selectedIndex = value;
  4887. }
  4888. get selectedItem() {
  4889. return this._list.selectedItem;
  4890. }
  4891. set selectedItem(value) {
  4892. this._list.selectedItem = value;
  4893. }
  4894. set width(value) {
  4895. super.width = value;
  4896. this._list.width = value;
  4897. }
  4898. get width() {
  4899. return super.width;
  4900. }
  4901. set height(value) {
  4902. super.height = value;
  4903. this._list.height = value;
  4904. }
  4905. get height() {
  4906. return super.height;
  4907. }
  4908. getArray() {
  4909. var arr = [];
  4910. for (let key in this._source) {
  4911. let item = this._source[key];
  4912. if (this.getParentOpenStatus(item)) {
  4913. item.x = this._spaceLeft * this.getDepth(item);
  4914. arr.push(item);
  4915. }
  4916. }
  4917. return arr;
  4918. }
  4919. getDepth(item, num = 0) {
  4920. if (item.nodeParent == null)
  4921. return num;
  4922. else
  4923. return this.getDepth(item.nodeParent, num + 1);
  4924. }
  4925. getParentOpenStatus(item) {
  4926. var parent = item.nodeParent;
  4927. if (parent == null) {
  4928. return true;
  4929. }
  4930. else {
  4931. if (parent.isOpen) {
  4932. if (parent.nodeParent != null)
  4933. return this.getParentOpenStatus(parent);
  4934. else
  4935. return true;
  4936. }
  4937. else {
  4938. return false;
  4939. }
  4940. }
  4941. }
  4942. renderItem(cell, index) {
  4943. var item = cell.dataSource;
  4944. if (item) {
  4945. cell.left = item.x;
  4946. var arrow = cell.getChildByName("arrow");
  4947. if (arrow) {
  4948. if (item.hasChild) {
  4949. arrow.visible = true;
  4950. arrow.index = item.isOpen ? 1 : 0;
  4951. arrow.tag = index;
  4952. arrow.off(Laya.Event.CLICK, this, this.onArrowClick);
  4953. arrow.on(Laya.Event.CLICK, this, this.onArrowClick);
  4954. }
  4955. else {
  4956. arrow.visible = false;
  4957. }
  4958. }
  4959. var folder = cell.getChildByName("folder");
  4960. if (folder) {
  4961. if (folder.clipY == 2) {
  4962. folder.index = item.isDirectory ? 0 : 1;
  4963. }
  4964. else {
  4965. folder.index = item.isDirectory ? item.isOpen ? 1 : 0 : 2;
  4966. }
  4967. }
  4968. this._renderHandler && this._renderHandler.runWith([cell, index]);
  4969. }
  4970. }
  4971. onArrowClick(e) {
  4972. var arrow = e.currentTarget;
  4973. var index = arrow.tag;
  4974. this._list.array[index].isOpen = !this._list.array[index].isOpen;
  4975. this.event(Laya.Event.OPEN);
  4976. this._list.array = this.getArray();
  4977. }
  4978. setItemState(index, isOpen) {
  4979. if (!this._list.array[index])
  4980. return;
  4981. this._list.array[index].isOpen = isOpen;
  4982. this._list.array = this.getArray();
  4983. }
  4984. fresh() {
  4985. this._list.array = this.getArray();
  4986. this.repaint();
  4987. }
  4988. set dataSource(value) {
  4989. this._dataSource = value;
  4990. super.dataSource = value;
  4991. }
  4992. get dataSource() {
  4993. return super.dataSource;
  4994. }
  4995. set xml(value) {
  4996. var arr = [];
  4997. this.parseXml(value.childNodes[0], arr, null, true);
  4998. this.array = arr;
  4999. }
  5000. parseXml(xml, source, nodeParent, isRoot) {
  5001. var obj;
  5002. var list = xml.childNodes;
  5003. var childCount = list.length;
  5004. if (!isRoot) {
  5005. obj = {};
  5006. var list2 = xml.attributes;
  5007. for (let key in list2) {
  5008. var attrs = list2[key];
  5009. var prop = attrs.nodeName;
  5010. var value = attrs.nodeValue;
  5011. obj[prop] = value == "true" ? true : value == "false" ? false : value;
  5012. }
  5013. obj.nodeParent = nodeParent;
  5014. if (childCount > 0)
  5015. obj.isDirectory = true;
  5016. obj.hasChild = childCount > 0;
  5017. source.push(obj);
  5018. }
  5019. for (var i = 0; i < childCount; i++) {
  5020. var node = list[i];
  5021. this.parseXml(node, source, obj, false);
  5022. }
  5023. }
  5024. parseOpenStatus(oldSource, newSource) {
  5025. for (var i = 0, n = newSource.length; i < n; i++) {
  5026. var newItem = newSource[i];
  5027. if (newItem.isDirectory) {
  5028. for (var j = 0, m = oldSource.length; j < m; j++) {
  5029. var oldItem = oldSource[j];
  5030. if (oldItem.isDirectory && this.isSameParent(oldItem, newItem) && newItem.label == oldItem.label) {
  5031. newItem.isOpen = oldItem.isOpen;
  5032. break;
  5033. }
  5034. }
  5035. }
  5036. }
  5037. }
  5038. isSameParent(item1, item2) {
  5039. if (item1.nodeParent == null && item2.nodeParent == null)
  5040. return true;
  5041. else if (item1.nodeParent == null || item2.nodeParent == null)
  5042. return false;
  5043. else {
  5044. if (item1.nodeParent.label == item2.nodeParent.label)
  5045. return this.isSameParent(item1.nodeParent, item2.nodeParent);
  5046. else
  5047. return false;
  5048. }
  5049. }
  5050. get selectedPath() {
  5051. if (this._list.selectedItem) {
  5052. return this._list.selectedItem.path;
  5053. }
  5054. return null;
  5055. }
  5056. filter(key) {
  5057. if (Boolean(key)) {
  5058. var result = [];
  5059. this.getFilterSource(this._source, result, key);
  5060. this._list.array = result;
  5061. }
  5062. else {
  5063. this._list.array = this.getArray();
  5064. }
  5065. }
  5066. getFilterSource(array, result, key) {
  5067. key = key.toLocaleLowerCase();
  5068. for (let item of array) {
  5069. if (!item.isDirectory && String(item.label).toLowerCase().indexOf(key) > -1) {
  5070. item.x = 0;
  5071. result.push(item);
  5072. }
  5073. if (item.child && item.child.length > 0) {
  5074. this.getFilterSource(item.child, result, key);
  5075. }
  5076. }
  5077. }
  5078. }
  5079. Laya.ILaya.regClass(Tree);
  5080. Laya.ClassUtils.regClass("laya.ui.Tree", Tree);
  5081. Laya.ClassUtils.regClass("Laya.Tree", Tree);
  5082. class LayoutBox extends Box {
  5083. constructor() {
  5084. super(...arguments);
  5085. this._space = 0;
  5086. this._align = "none";
  5087. this._itemChanged = false;
  5088. }
  5089. addChild(child) {
  5090. child.on(Laya.Event.RESIZE, this, this.onResize);
  5091. this._setItemChanged();
  5092. return super.addChild(child);
  5093. }
  5094. onResize(e) {
  5095. this._setItemChanged();
  5096. }
  5097. addChildAt(child, index) {
  5098. child.on(Laya.Event.RESIZE, this, this.onResize);
  5099. this._setItemChanged();
  5100. return super.addChildAt(child, index);
  5101. }
  5102. removeChildAt(index) {
  5103. this.getChildAt(index).off(Laya.Event.RESIZE, this, this.onResize);
  5104. this._setItemChanged();
  5105. return super.removeChildAt(index);
  5106. }
  5107. refresh() {
  5108. this._setItemChanged();
  5109. }
  5110. changeItems() {
  5111. this._itemChanged = false;
  5112. }
  5113. get space() {
  5114. return this._space;
  5115. }
  5116. set space(value) {
  5117. this._space = value;
  5118. this._setItemChanged();
  5119. }
  5120. get align() {
  5121. return this._align;
  5122. }
  5123. set align(value) {
  5124. this._align = value;
  5125. this._setItemChanged();
  5126. }
  5127. sortItem(items) {
  5128. if (items)
  5129. items.sort(function (a, b) { return a.y - b.y; });
  5130. }
  5131. _setItemChanged() {
  5132. if (!this._itemChanged) {
  5133. this._itemChanged = true;
  5134. this.callLater(this.changeItems);
  5135. }
  5136. }
  5137. }
  5138. Laya.ILaya.regClass(LayoutBox);
  5139. Laya.ClassUtils.regClass("laya.ui.LayoutBox", LayoutBox);
  5140. Laya.ClassUtils.regClass("Laya.LayoutBox", LayoutBox);
  5141. class HBox extends LayoutBox {
  5142. sortItem(items) {
  5143. if (items)
  5144. items.sort(function (a, b) { return a.x - b.x; });
  5145. }
  5146. set height(value) {
  5147. if (this._height != value) {
  5148. super.height = value;
  5149. this.callLater(this.changeItems);
  5150. }
  5151. }
  5152. get height() {
  5153. return super.height;
  5154. }
  5155. changeItems() {
  5156. this._itemChanged = false;
  5157. var items = [];
  5158. var maxHeight = 0;
  5159. for (var i = 0, n = this.numChildren; i < n; i++) {
  5160. var item = this.getChildAt(i);
  5161. if (item) {
  5162. items.push(item);
  5163. maxHeight = this._height ? this._height : Math.max(maxHeight, item.height * item.scaleY);
  5164. }
  5165. }
  5166. this.sortItem(items);
  5167. var left = 0;
  5168. for (i = 0, n = items.length; i < n; i++) {
  5169. item = items[i];
  5170. item.x = left;
  5171. left += item.width * item.scaleX + this._space;
  5172. if (this._align == HBox.TOP) {
  5173. item.y = 0;
  5174. }
  5175. else if (this._align == HBox.MIDDLE) {
  5176. item.y = (maxHeight - item.height * item.scaleY) * 0.5;
  5177. }
  5178. else if (this._align == HBox.BOTTOM) {
  5179. item.y = maxHeight - item.height * item.scaleY;
  5180. }
  5181. }
  5182. this._sizeChanged();
  5183. }
  5184. }
  5185. HBox.NONE = "none";
  5186. HBox.TOP = "top";
  5187. HBox.MIDDLE = "middle";
  5188. HBox.BOTTOM = "bottom";
  5189. Laya.ILaya.regClass(HBox);
  5190. Laya.ClassUtils.regClass("laya.ui.HBox", HBox);
  5191. Laya.ClassUtils.regClass("Laya.HBox", HBox);
  5192. class VBox extends LayoutBox {
  5193. set width(value) {
  5194. if (this._width != value) {
  5195. super.width = value;
  5196. this.callLater(this.changeItems);
  5197. }
  5198. }
  5199. get width() {
  5200. return super.width;
  5201. }
  5202. changeItems() {
  5203. this._itemChanged = false;
  5204. var items = [];
  5205. var maxWidth = 0;
  5206. for (var i = 0, n = this.numChildren; i < n; i++) {
  5207. var item = this.getChildAt(i);
  5208. if (item) {
  5209. items.push(item);
  5210. maxWidth = this._width ? this._width : Math.max(maxWidth, item.width * item.scaleX);
  5211. }
  5212. }
  5213. this.sortItem(items);
  5214. var top = 0;
  5215. for (i = 0, n = items.length; i < n; i++) {
  5216. item = items[i];
  5217. item.y = top;
  5218. top += item.height * item.scaleY + this._space;
  5219. if (this._align == VBox.LEFT) {
  5220. item.x = 0;
  5221. }
  5222. else if (this._align == VBox.CENTER) {
  5223. item.x = (maxWidth - item.width * item.scaleX) * 0.5;
  5224. }
  5225. else if (this._align == VBox.RIGHT) {
  5226. item.x = maxWidth - item.width * item.scaleX;
  5227. }
  5228. }
  5229. this._sizeChanged();
  5230. }
  5231. }
  5232. VBox.NONE = "none";
  5233. VBox.LEFT = "left";
  5234. VBox.CENTER = "center";
  5235. VBox.RIGHT = "right";
  5236. Laya.ILaya.regClass(VBox);
  5237. Laya.ClassUtils.regClass("laya.ui.VBox", VBox);
  5238. Laya.ClassUtils.regClass("Laya.VBox", VBox);
  5239. class FontClip extends Clip {
  5240. constructor(skin = null, sheet = null) {
  5241. super();
  5242. this._valueArr = '';
  5243. this._indexMap = null;
  5244. this._sheet = null;
  5245. this._direction = "horizontal";
  5246. this._spaceX = 0;
  5247. this._spaceY = 0;
  5248. this._align = "left";
  5249. this._wordsW = 0;
  5250. this._wordsH = 0;
  5251. if (skin)
  5252. this.skin = skin;
  5253. if (sheet)
  5254. this.sheet = sheet;
  5255. }
  5256. createChildren() {
  5257. this._bitmap = new AutoBitmap();
  5258. this.on(Laya.Event.LOADED, this, this._onClipLoaded);
  5259. }
  5260. _onClipLoaded() {
  5261. this.callLater(this.changeValue);
  5262. }
  5263. get sheet() {
  5264. return this._sheet;
  5265. }
  5266. set sheet(value) {
  5267. value += "";
  5268. this._sheet = value;
  5269. var arr = value.split(" ");
  5270. this._clipX = String(arr[0]).length;
  5271. this.clipY = arr.length;
  5272. this._indexMap = {};
  5273. for (var i = 0; i < this._clipY; i++) {
  5274. var line = arr[i].split("");
  5275. for (var j = 0, n = line.length; j < n; j++) {
  5276. this._indexMap[line[j]] = i * this._clipX + j;
  5277. }
  5278. }
  5279. }
  5280. get value() {
  5281. if (!this._valueArr)
  5282. return "";
  5283. return this._valueArr;
  5284. }
  5285. set value(value) {
  5286. value += "";
  5287. this._valueArr = value;
  5288. this.callLater(this.changeValue);
  5289. }
  5290. get direction() {
  5291. return this._direction;
  5292. }
  5293. set direction(value) {
  5294. this._direction = value;
  5295. this.callLater(this.changeValue);
  5296. }
  5297. get spaceX() {
  5298. return this._spaceX;
  5299. }
  5300. set spaceX(value) {
  5301. this._spaceX = value;
  5302. if (this._direction === "horizontal")
  5303. this.callLater(this.changeValue);
  5304. }
  5305. get spaceY() {
  5306. return this._spaceY;
  5307. }
  5308. set spaceY(value) {
  5309. this._spaceY = value;
  5310. if (!(this._direction === "horizontal"))
  5311. this.callLater(this.changeValue);
  5312. }
  5313. set align(v) {
  5314. this._align = v;
  5315. this.callLater(this.changeValue);
  5316. }
  5317. get align() {
  5318. return this._align;
  5319. }
  5320. changeValue() {
  5321. if (!this._sources)
  5322. return;
  5323. if (!this._valueArr)
  5324. return;
  5325. this.graphics.clear(true);
  5326. var texture;
  5327. texture = this._sources[0];
  5328. if (!texture)
  5329. return;
  5330. var isHorizontal = (this._direction === "horizontal");
  5331. if (isHorizontal) {
  5332. this._wordsW = this._valueArr.length * (texture.sourceWidth + this.spaceX);
  5333. this._wordsH = texture.sourceHeight;
  5334. }
  5335. else {
  5336. this._wordsW = texture.sourceWidth;
  5337. this._wordsH = (texture.sourceHeight + this.spaceY) * this._valueArr.length;
  5338. }
  5339. var dX = 0;
  5340. if (this._width) {
  5341. switch (this._align) {
  5342. case "center":
  5343. dX = 0.5 * (this._width - this._wordsW);
  5344. break;
  5345. case "right":
  5346. dX = this._width - this._wordsW;
  5347. break;
  5348. default:
  5349. dX = 0;
  5350. }
  5351. }
  5352. for (var i = 0, sz = this._valueArr.length; i < sz; i++) {
  5353. var index = this._indexMap[this._valueArr.charAt(i)];
  5354. if (!this.sources[index])
  5355. continue;
  5356. texture = this.sources[index];
  5357. if (isHorizontal)
  5358. this.graphics.drawImage(texture, dX + i * (texture.sourceWidth + this.spaceX), 0, texture.sourceWidth, texture.sourceHeight);
  5359. else
  5360. this.graphics.drawImage(texture, 0 + dX, i * (texture.sourceHeight + this.spaceY), texture.sourceWidth, texture.sourceHeight);
  5361. }
  5362. if (!this._width) {
  5363. this._widget.resetLayoutX();
  5364. this.callLater(this._sizeChanged);
  5365. }
  5366. if (!this._height) {
  5367. this._widget.resetLayoutY();
  5368. this.callLater(this._sizeChanged);
  5369. }
  5370. }
  5371. set width(value) {
  5372. super.width = value;
  5373. this.callLater(this.changeValue);
  5374. }
  5375. get width() {
  5376. return super.width;
  5377. }
  5378. set height(value) {
  5379. super.height = value;
  5380. this.callLater(this.changeValue);
  5381. }
  5382. get height() {
  5383. return super.height;
  5384. }
  5385. measureWidth() {
  5386. return this._wordsW;
  5387. }
  5388. measureHeight() {
  5389. return this._wordsH;
  5390. }
  5391. destroy(destroyChild = true) {
  5392. this._valueArr = null;
  5393. this._indexMap = null;
  5394. this.graphics.clear(true);
  5395. this.removeSelf();
  5396. this.off(Laya.Event.LOADED, this, this._onClipLoaded);
  5397. super.destroy(destroyChild);
  5398. }
  5399. }
  5400. Laya.ILaya.regClass(FontClip);
  5401. Laya.ClassUtils.regClass("laya.ui.FontClip", FontClip);
  5402. Laya.ClassUtils.regClass("Laya.FontClip", FontClip);
  5403. class View extends Laya.Scene {
  5404. constructor() {
  5405. super(false);
  5406. this._watchMap = {};
  5407. this._anchorX = NaN;
  5408. this._anchorY = NaN;
  5409. this._widget = Widget.EMPTY;
  5410. this.createChildren();
  5411. }
  5412. static __init__() {
  5413. Laya.ILaya.ClassUtils.regShortClassName([ViewStack, Button, TextArea, ColorPicker, Box, ScaleBox, CheckBox, Clip, ComboBox, UIComponent,
  5414. HScrollBar, HSlider, Image, Label, List, Panel, ProgressBar, Radio, RadioGroup, ScrollBar, Slider, Tab, TextInput, View,
  5415. VScrollBar, VSlider, Tree, HBox, VBox, Laya.Animation, Laya.Text, FontClip]);
  5416. }
  5417. static regComponent(key, compClass) {
  5418. Laya.ILaya.ClassUtils.regClass(key, compClass);
  5419. }
  5420. static regViewRuntime(key, compClass) {
  5421. Laya.ILaya.ClassUtils.regClass(key, compClass);
  5422. }
  5423. static regUI(url, json) {
  5424. Laya.ILaya.loader.cacheRes(url, json);
  5425. }
  5426. destroy(destroyChild = true) {
  5427. this._watchMap = null;
  5428. super.destroy(destroyChild);
  5429. }
  5430. changeData(key) {
  5431. var arr = this._watchMap[key];
  5432. if (!arr)
  5433. return;
  5434. for (var i = 0, n = arr.length; i < n; i++) {
  5435. var watcher = arr[i];
  5436. watcher.exe(this);
  5437. }
  5438. }
  5439. get top() {
  5440. return this._widget.top;
  5441. }
  5442. set top(value) {
  5443. if (value != this._widget.top) {
  5444. this._getWidget().top = value;
  5445. }
  5446. }
  5447. get bottom() {
  5448. return this._widget.bottom;
  5449. }
  5450. set bottom(value) {
  5451. if (value != this._widget.bottom) {
  5452. this._getWidget().bottom = value;
  5453. }
  5454. }
  5455. get left() {
  5456. return this._widget.left;
  5457. }
  5458. set left(value) {
  5459. if (value != this._widget.left) {
  5460. this._getWidget().left = value;
  5461. }
  5462. }
  5463. get right() {
  5464. return this._widget.right;
  5465. }
  5466. set right(value) {
  5467. if (value != this._widget.right) {
  5468. this._getWidget().right = value;
  5469. }
  5470. }
  5471. get centerX() {
  5472. return this._widget.centerX;
  5473. }
  5474. set centerX(value) {
  5475. if (value != this._widget.centerX) {
  5476. this._getWidget().centerX = value;
  5477. }
  5478. }
  5479. get centerY() {
  5480. return this._widget.centerY;
  5481. }
  5482. set centerY(value) {
  5483. if (value != this._widget.centerY) {
  5484. this._getWidget().centerY = value;
  5485. }
  5486. }
  5487. get anchorX() {
  5488. return this._anchorX;
  5489. }
  5490. set anchorX(value) {
  5491. if (this._anchorX != value) {
  5492. this._anchorX = value;
  5493. this.callLater(this._sizeChanged);
  5494. }
  5495. }
  5496. get anchorY() {
  5497. return this._anchorY;
  5498. }
  5499. set anchorY(value) {
  5500. if (this._anchorY != value) {
  5501. this._anchorY = value;
  5502. this.callLater(this._sizeChanged);
  5503. }
  5504. }
  5505. _sizeChanged() {
  5506. if (!isNaN(this._anchorX))
  5507. this.pivotX = this.anchorX * this.width;
  5508. if (!isNaN(this._anchorY))
  5509. this.pivotY = this.anchorY * this.height;
  5510. this.event(Laya.Event.RESIZE);
  5511. }
  5512. _getWidget() {
  5513. this._widget === Widget.EMPTY && (this._widget = this.addComponent(Widget));
  5514. return this._widget;
  5515. }
  5516. loadUI(path) {
  5517. var uiView = View.uiMap[path];
  5518. View.uiMap && this.createView(uiView);
  5519. }
  5520. get dataSource() {
  5521. return this._dataSource;
  5522. }
  5523. set dataSource(value) {
  5524. this._dataSource = value;
  5525. for (var name in value) {
  5526. var comp = this.getChildByName(name);
  5527. if (comp instanceof UIComponent)
  5528. comp.dataSource = value[name];
  5529. else if (name in this && !(this[name] instanceof Function))
  5530. this[name] = value[name];
  5531. }
  5532. }
  5533. }
  5534. View.uiMap = {};
  5535. Laya.ILaya.regClass(View);
  5536. Laya.ClassUtils.regClass("laya.ui.View", View);
  5537. Laya.ClassUtils.regClass("Laya.View", View);
  5538. class IUI {
  5539. }
  5540. IUI.Dialog = null;
  5541. class DialogManager extends Laya.Sprite {
  5542. constructor() {
  5543. super();
  5544. this.maskLayer = new Laya.Sprite();
  5545. this.popupEffect = (dialog) => {
  5546. dialog.scale(1, 1);
  5547. 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);
  5548. };
  5549. this.closeEffect = (dialog) => {
  5550. 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);
  5551. };
  5552. this.popupEffectHandler = new Laya.Handler(this, this.popupEffect);
  5553. this.closeEffectHandler = new Laya.Handler(this, this.closeEffect);
  5554. this.mouseEnabled = this.maskLayer.mouseEnabled = true;
  5555. this.zOrder = 1000;
  5556. Laya.ILaya.stage.addChild(this);
  5557. Laya.ILaya.stage.on(Laya.Event.RESIZE, this, this._onResize);
  5558. if (UIConfig.closeDialogOnSide)
  5559. this.maskLayer.on("click", this, this._closeOnSide);
  5560. this._onResize(null);
  5561. }
  5562. _closeOnSide() {
  5563. var dialog = this.getChildAt(this.numChildren - 1);
  5564. if (dialog instanceof IUI.Dialog)
  5565. dialog.close();
  5566. }
  5567. setLockView(value) {
  5568. if (!this.lockLayer) {
  5569. this.lockLayer = new Box();
  5570. this.lockLayer.mouseEnabled = true;
  5571. this.lockLayer.size(Laya.ILaya.stage.width, Laya.ILaya.stage.height);
  5572. }
  5573. this.lockLayer.removeChildren();
  5574. if (value) {
  5575. value.centerX = value.centerY = 0;
  5576. this.lockLayer.addChild(value);
  5577. }
  5578. }
  5579. _onResize(e = null) {
  5580. var width = this.maskLayer.width = Laya.ILaya.stage.width;
  5581. var height = this.maskLayer.height = Laya.ILaya.stage.height;
  5582. if (this.lockLayer)
  5583. this.lockLayer.size(width, height);
  5584. this.maskLayer.graphics.clear(true);
  5585. this.maskLayer.graphics.drawRect(0, 0, width, height, UIConfig.popupBgColor);
  5586. this.maskLayer.alpha = UIConfig.popupBgAlpha;
  5587. for (var i = this.numChildren - 1; i > -1; i--) {
  5588. var item = this.getChildAt(i);
  5589. if (item.isPopupCenter)
  5590. this._centerDialog(item);
  5591. }
  5592. }
  5593. _centerDialog(dialog) {
  5594. dialog.x = Math.round(((Laya.ILaya.stage.width - dialog.width) >> 1) + dialog.pivotX);
  5595. dialog.y = Math.round(((Laya.ILaya.stage.height - dialog.height) >> 1) + dialog.pivotY);
  5596. }
  5597. open(dialog, closeOther = false, showEffect = false) {
  5598. if (closeOther)
  5599. this._closeAll();
  5600. this._clearDialogEffect(dialog);
  5601. if (dialog.isPopupCenter)
  5602. this._centerDialog(dialog);
  5603. this.addChild(dialog);
  5604. if (dialog.isModal || this._getBit(Laya.Const.HAS_ZORDER))
  5605. Laya.ILaya.timer.callLater(this, this._checkMask);
  5606. if (showEffect && dialog.popupEffect != null)
  5607. dialog.popupEffect.runWith(dialog);
  5608. else
  5609. this.doOpen(dialog);
  5610. this.event(Laya.Event.OPEN);
  5611. }
  5612. _clearDialogEffect(dialog) {
  5613. if (dialog._effectTween) {
  5614. Laya.Tween.clear(dialog._effectTween);
  5615. dialog._effectTween = null;
  5616. }
  5617. }
  5618. doOpen(dialog) {
  5619. dialog.onOpened(dialog._param);
  5620. }
  5621. lock(value) {
  5622. if (this.lockLayer) {
  5623. if (value)
  5624. this.addChild(this.lockLayer);
  5625. else
  5626. this.lockLayer.removeSelf();
  5627. }
  5628. }
  5629. close(dialog) {
  5630. this._clearDialogEffect(dialog);
  5631. if (dialog.isShowEffect && dialog.closeEffect != null)
  5632. dialog.closeEffect.runWith([dialog]);
  5633. else
  5634. this.doClose(dialog);
  5635. this.event(Laya.Event.CLOSE);
  5636. }
  5637. doClose(dialog) {
  5638. dialog.removeSelf();
  5639. dialog.isModal && this._checkMask();
  5640. dialog.closeHandler && dialog.closeHandler.runWith(dialog.closeType);
  5641. dialog.onClosed(dialog.closeType);
  5642. if (dialog.autoDestroyAtClosed)
  5643. dialog.destroy();
  5644. }
  5645. closeAll() {
  5646. this._closeAll();
  5647. this.event(Laya.Event.CLOSE);
  5648. }
  5649. _closeAll() {
  5650. for (var i = this.numChildren - 1; i > -1; i--) {
  5651. var item = this.getChildAt(i);
  5652. if (item && item.close != null) {
  5653. this.doClose(item);
  5654. }
  5655. }
  5656. }
  5657. getDialogsByGroup(group) {
  5658. var arr = [];
  5659. for (var i = this.numChildren - 1; i > -1; i--) {
  5660. var item = this.getChildAt(i);
  5661. if (item && item.group === group) {
  5662. arr.push(item);
  5663. }
  5664. }
  5665. return arr;
  5666. }
  5667. closeByGroup(group) {
  5668. var arr = [];
  5669. for (var i = this.numChildren - 1; i > -1; i--) {
  5670. var item = this.getChildAt(i);
  5671. if (item && item.group === group) {
  5672. item.close();
  5673. arr.push(item);
  5674. }
  5675. }
  5676. return arr;
  5677. }
  5678. _checkMask() {
  5679. this.maskLayer.removeSelf();
  5680. for (var i = this.numChildren - 1; i > -1; i--) {
  5681. var dialog = this.getChildAt(i);
  5682. if (dialog && dialog.isModal) {
  5683. this.addChildAt(this.maskLayer, i);
  5684. return;
  5685. }
  5686. }
  5687. }
  5688. }
  5689. Laya.ClassUtils.regClass("laya.ui.DialogManager", DialogManager);
  5690. Laya.ClassUtils.regClass("Laya.DialogManager", DialogManager);
  5691. class Dialog extends View {
  5692. constructor() {
  5693. super();
  5694. this.isShowEffect = true;
  5695. this.isPopupCenter = true;
  5696. this.popupEffect = Dialog.manager.popupEffectHandler;
  5697. this.closeEffect = Dialog.manager.closeEffectHandler;
  5698. this._dealDragArea();
  5699. this.on(Laya.Event.CLICK, this, this._onClick);
  5700. }
  5701. static get manager() {
  5702. return Dialog._manager = Dialog._manager || new DialogManager();
  5703. }
  5704. static set manager(value) {
  5705. Dialog._manager = value;
  5706. }
  5707. _dealDragArea() {
  5708. var dragTarget = this.getChildByName("drag");
  5709. if (dragTarget) {
  5710. this.dragArea = dragTarget._x + "," + dragTarget._y + "," + dragTarget.width + "," + dragTarget.height;
  5711. dragTarget.removeSelf();
  5712. }
  5713. }
  5714. get dragArea() {
  5715. if (this._dragArea)
  5716. return this._dragArea.toString();
  5717. return null;
  5718. }
  5719. set dragArea(value) {
  5720. if (value) {
  5721. var a = UIUtils.fillArray([0, 0, 0, 0], value, Number);
  5722. this._dragArea = new Laya.Rectangle(a[0], a[1], a[2], a[3]);
  5723. this.on(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);
  5724. }
  5725. else {
  5726. this._dragArea = null;
  5727. this.off(Laya.Event.MOUSE_DOWN, this, this._onMouseDown);
  5728. }
  5729. }
  5730. _onMouseDown(e) {
  5731. var point = this.getMousePoint();
  5732. if (this._dragArea.contains(point.x, point.y))
  5733. this.startDrag();
  5734. else
  5735. this.stopDrag();
  5736. }
  5737. _onClick(e) {
  5738. var btn = e.target;
  5739. if (btn) {
  5740. switch (btn.name) {
  5741. case Dialog.CLOSE:
  5742. case Dialog.CANCEL:
  5743. case Dialog.SURE:
  5744. case Dialog.NO:
  5745. case Dialog.OK:
  5746. case Dialog.YES:
  5747. this.close(btn.name);
  5748. return;
  5749. }
  5750. }
  5751. }
  5752. open(closeOther = true, param = null) {
  5753. this._dealDragArea();
  5754. this._param = param;
  5755. Dialog.manager.open(this, closeOther, this.isShowEffect);
  5756. Dialog.manager.lock(false);
  5757. }
  5758. close(type = null) {
  5759. this.closeType = type;
  5760. Dialog.manager.close(this);
  5761. }
  5762. destroy(destroyChild = true) {
  5763. this.closeHandler = null;
  5764. this.popupEffect = null;
  5765. this.closeEffect = null;
  5766. this._dragArea = null;
  5767. super.destroy(destroyChild);
  5768. }
  5769. show(closeOther = false, showEffect = true) {
  5770. this._open(false, closeOther, showEffect);
  5771. }
  5772. popup(closeOther = false, showEffect = true) {
  5773. this._open(true, closeOther, showEffect);
  5774. }
  5775. _open(modal, closeOther, showEffect) {
  5776. this.isModal = modal;
  5777. this.isShowEffect = showEffect;
  5778. Dialog.manager.lock(true);
  5779. this.open(closeOther);
  5780. }
  5781. get isPopup() {
  5782. return this.parent != null;
  5783. }
  5784. set zOrder(value) {
  5785. super.zOrder = value;
  5786. Dialog.manager._checkMask();
  5787. }
  5788. get zOrder() {
  5789. return super.zOrder;
  5790. }
  5791. static setLockView(view) {
  5792. Dialog.manager.setLockView(view);
  5793. }
  5794. static lock(value) {
  5795. Dialog.manager.lock(value);
  5796. }
  5797. static closeAll() {
  5798. Dialog.manager.closeAll();
  5799. }
  5800. static getDialogsByGroup(group) {
  5801. return Dialog.manager.getDialogsByGroup(group);
  5802. }
  5803. static closeByGroup(group) {
  5804. return Dialog.manager.closeByGroup(group);
  5805. }
  5806. }
  5807. Dialog.CLOSE = "close";
  5808. Dialog.CANCEL = "cancel";
  5809. Dialog.SURE = "sure";
  5810. Dialog.NO = "no";
  5811. Dialog.YES = "yes";
  5812. Dialog.OK = "ok";
  5813. IUI.Dialog = Dialog;
  5814. Laya.ILaya.regClass(Dialog);
  5815. Laya.ClassUtils.regClass("laya.ui.Dialog", Dialog);
  5816. Laya.ClassUtils.regClass("Laya.Dialog", Dialog);
  5817. class TipManager extends UIComponent {
  5818. constructor() {
  5819. super();
  5820. this._tipBox = new UIComponent();
  5821. this._tipBox.addChild(this._tipText = new Laya.Text());
  5822. this._tipText.x = this._tipText.y = 5;
  5823. this._tipText.color = TipManager.tipTextColor;
  5824. this._defaultTipHandler = this._showDefaultTip;
  5825. Laya.ILaya.stage.on(UIEvent.SHOW_TIP, this, this._onStageShowTip);
  5826. Laya.ILaya.stage.on(UIEvent.HIDE_TIP, this, this._onStageHideTip);
  5827. this.zOrder = 1100;
  5828. }
  5829. _onStageHideTip(e) {
  5830. Laya.ILaya.timer.clear(this, this._showTip);
  5831. this.closeAll();
  5832. this.removeSelf();
  5833. }
  5834. _onStageShowTip(data) {
  5835. Laya.ILaya.timer.once(TipManager.tipDelay, this, this._showTip, [data], true);
  5836. }
  5837. _showTip(tip) {
  5838. if (typeof (tip) == 'string') {
  5839. var text = String(tip);
  5840. if (Boolean(text)) {
  5841. this._defaultTipHandler(text);
  5842. }
  5843. }
  5844. else if (tip instanceof Laya.Handler) {
  5845. tip.run();
  5846. }
  5847. else if (tip instanceof Function) {
  5848. tip.apply();
  5849. }
  5850. {
  5851. Laya.ILaya.stage.on(Laya.Event.MOUSE_MOVE, this, this._onStageMouseMove);
  5852. Laya.ILaya.stage.on(Laya.Event.MOUSE_DOWN, this, this._onStageMouseDown);
  5853. }
  5854. this._onStageMouseMove(null);
  5855. }
  5856. _onStageMouseDown(e) {
  5857. this.closeAll();
  5858. }
  5859. _onStageMouseMove(e) {
  5860. this._showToStage(this, TipManager.offsetX, TipManager.offsetY);
  5861. }
  5862. _showToStage(dis, offX = 0, offY = 0) {
  5863. var rec = dis.getBounds();
  5864. dis.x = Laya.ILaya.stage.mouseX + offX;
  5865. dis.y = Laya.ILaya.stage.mouseY + offY;
  5866. if (dis._x + rec.width > Laya.ILaya.stage.width) {
  5867. dis.x -= rec.width + offX;
  5868. }
  5869. if (dis._y + rec.height > Laya.ILaya.stage.height) {
  5870. dis.y -= rec.height + offY;
  5871. }
  5872. }
  5873. closeAll() {
  5874. Laya.ILaya.timer.clear(this, this._showTip);
  5875. Laya.ILaya.stage.off(Laya.Event.MOUSE_MOVE, this, this._onStageMouseMove);
  5876. Laya.ILaya.stage.off(Laya.Event.MOUSE_DOWN, this, this._onStageMouseDown);
  5877. this.removeChildren();
  5878. }
  5879. showDislayTip(tip) {
  5880. this.addChild(tip);
  5881. this._showToStage(this);
  5882. Laya.ILaya.stage.addChild(this);
  5883. }
  5884. _showDefaultTip(text) {
  5885. this._tipText.text = text;
  5886. var g = this._tipBox.graphics;
  5887. g.clear(true);
  5888. g.drawRect(0, 0, this._tipText.width + 10, this._tipText.height + 10, TipManager.tipBackColor);
  5889. this.addChild(this._tipBox);
  5890. this._showToStage(this);
  5891. Laya.ILaya.stage.addChild(this);
  5892. }
  5893. get defaultTipHandler() {
  5894. return this._defaultTipHandler;
  5895. }
  5896. set defaultTipHandler(value) {
  5897. this._defaultTipHandler = value;
  5898. }
  5899. }
  5900. TipManager.offsetX = 10;
  5901. TipManager.offsetY = 15;
  5902. TipManager.tipTextColor = "#ffffff";
  5903. TipManager.tipBackColor = "#111111";
  5904. TipManager.tipDelay = 200;
  5905. Laya.ILaya.regClass(TipManager);
  5906. Laya.ClassUtils.regClass("laya.ui.TipManager", TipManager);
  5907. Laya.ClassUtils.regClass("Laya.TipManager", TipManager);
  5908. class UILib {
  5909. static __init__() {
  5910. }
  5911. }
  5912. class WXOpenDataViewer extends UIComponent {
  5913. constructor() {
  5914. super();
  5915. this._width = this._height = 200;
  5916. var tex = new Laya.Texture();
  5917. tex.bitmap = new Laya.Texture2D();
  5918. this.texture = tex;
  5919. }
  5920. onEnable() {
  5921. this.postMsg({ type: "display", rate: Laya.Laya.stage.frameRate });
  5922. if (window.wx && window.sharedCanvas)
  5923. Laya.Laya.timer.frameLoop(1, this, this._onLoop);
  5924. }
  5925. onDisable() {
  5926. this.postMsg({ type: "undisplay" });
  5927. Laya.Laya.timer.clear(this, this._onLoop);
  5928. }
  5929. _onLoop() {
  5930. this.texture.bitmap.loadImageSource(window.sharedCanvas);
  5931. }
  5932. set width(value) {
  5933. super.width = value;
  5934. if (window.sharedCanvas)
  5935. window.sharedCanvas.width = value;
  5936. this.callLater(this._postMsg);
  5937. }
  5938. get width() {
  5939. return super.width;
  5940. }
  5941. set height(value) {
  5942. super.height = value;
  5943. if (window.sharedCanvas)
  5944. window.sharedCanvas.height = value;
  5945. this.callLater(this._postMsg);
  5946. }
  5947. get height() {
  5948. return super.height;
  5949. }
  5950. set x(value) {
  5951. super.x = value;
  5952. this.callLater(this._postMsg);
  5953. }
  5954. get x() {
  5955. return super.x;
  5956. }
  5957. set y(value) {
  5958. super.y = value;
  5959. this.callLater(this._postMsg);
  5960. }
  5961. get y() {
  5962. return super.y;
  5963. }
  5964. _postMsg() {
  5965. var mat = new Laya.Matrix();
  5966. mat.translate(this.x, this.y);
  5967. var stage = Laya.Laya.stage;
  5968. mat.scale(stage._canvasTransform.getScaleX() * this.globalScaleX * stage.transform.getScaleX(), stage._canvasTransform.getScaleY() * this.globalScaleY * stage.transform.getScaleY());
  5969. 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 });
  5970. }
  5971. postMsg(msg) {
  5972. if (window.wx && window.wx.getOpenDataContext) {
  5973. var openDataContext = window.wx.getOpenDataContext();
  5974. openDataContext.postMessage(msg);
  5975. }
  5976. }
  5977. }
  5978. Laya.ILaya.regClass(WXOpenDataViewer);
  5979. Laya.ClassUtils.regClass("laya.ui.WXOpenDataViewer", WXOpenDataViewer);
  5980. Laya.ClassUtils.regClass("Laya.WXOpenDataViewer", WXOpenDataViewer);
  5981. exports.AdvImage = AdvImage;
  5982. exports.AutoBitmap = AutoBitmap;
  5983. exports.Box = Box;
  5984. exports.Button = Button;
  5985. exports.CheckBox = CheckBox;
  5986. exports.Clip = Clip;
  5987. exports.ColorPicker = ColorPicker;
  5988. exports.ComboBox = ComboBox;
  5989. exports.Dialog = Dialog;
  5990. exports.DialogManager = DialogManager;
  5991. exports.FontClip = FontClip;
  5992. exports.HBox = HBox;
  5993. exports.HScrollBar = HScrollBar;
  5994. exports.HSlider = HSlider;
  5995. exports.IUI = IUI;
  5996. exports.Image = Image;
  5997. exports.Label = Label;
  5998. exports.LayoutBox = LayoutBox;
  5999. exports.List = List;
  6000. exports.Panel = Panel;
  6001. exports.ProgressBar = ProgressBar;
  6002. exports.Radio = Radio;
  6003. exports.RadioGroup = RadioGroup;
  6004. exports.ScaleBox = ScaleBox;
  6005. exports.ScrollBar = ScrollBar;
  6006. exports.Slider = Slider;
  6007. exports.Styles = Styles;
  6008. exports.Tab = Tab;
  6009. exports.TextArea = TextArea;
  6010. exports.TextInput = TextInput;
  6011. exports.TipManager = TipManager;
  6012. exports.Tree = Tree;
  6013. exports.UIComponent = UIComponent;
  6014. exports.UIConfig = UIConfig;
  6015. exports.UIEvent = UIEvent;
  6016. exports.UIGroup = UIGroup;
  6017. exports.UILib = UILib;
  6018. exports.UIUtils = UIUtils;
  6019. exports.VBox = VBox;
  6020. exports.VScrollBar = VScrollBar;
  6021. exports.VSlider = VSlider;
  6022. exports.View = View;
  6023. exports.ViewStack = ViewStack;
  6024. exports.WXOpenDataViewer = WXOpenDataViewer;
  6025. exports.Widget = Widget;
  6026. }(window.Laya = window.Laya || {}, Laya));