laya.ui.js 192 KB

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