123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import { GUIMediator } from "../../../engines/gui/GUIMediator";
- import { _decorator, Component, Node, ProgressBarComponent, LabelComponent, Prefab, ScrollViewComponent, LayoutComponent, JsonAsset, instantiate } from 'cc';
- import GameConfigManager from "../../models/GameConfigManager";
- import { GUIManager } from "../../../engines/gui/GUIManager";
- import { UIConst } from "../UIConst";
- import { GameModel } from "../../models/GameModel";
- import { DataModelEventType } from "../../../engines/models/DataModelEventType";
- import { GamePropertys } from "../../models/GamePropertys";
- import StringUtils from "../../../engines/utils/StringUtils";
- import { ExchangeItemRenderScript } from "./ExchangeItemRenderScript";
- const { ccclass, property } = _decorator;
- @ccclass('ExchangeMediator')
- export default class ExchangeMediator extends GUIMediator
- {
- @property({
- type: LabelComponent
- })
- glodLabel: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- glodLabel1: LabelComponent = null;
- @property({
- type: LabelComponent
- })
- diamondLabel: LabelComponent = null;
- @property({
- type:Prefab
- })
- itemRenderPrefab:Prefab=null;
- @property({
- type:ScrollViewComponent
- })
- list:ScrollViewComponent=null;
- @property({
- type:LayoutComponent
- })
- listContent:LayoutComponent=null;
- constructor(uiName:string){
- super(uiName);
- }
- OnShow(data?:any):void{
- super.OnShow(data);
- this.RefreshList();
- this.RefreshGlod();
- this.RefreshDiamond();
- this.AddEvent();
- }
- OnHide():void{
- this.RemoveEvent();
- }
- private AddEvent():void{
- GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
- }
- private RemoveEvent():void{
- GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
- }
- private GameModelPropertyChanged(key: string): void {
- switch (key) {
- case GamePropertys.gold:
- this.CallNextFrame(this.RefreshGlod.bind(this));
- break;
- case GamePropertys.diamond:
- this.CallNextFrame(this.RefreshDiamond.bind(this));
- break;
- }
- }
- private RefreshGlod(): void {
- if (this.glodLabel != null) {
- this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
- }
- this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
- }
- private RefreshDiamond(): void {
- if (this.diamondLabel != null) {
- this.diamondLabel.string = GameModel.single.diamond.toString();
- }
- }
- /**
- * 关闭界面
- */
- CloseButtonClickHandler():void{
- GUIManager.single.Show(UIConst.PREPARE_UI);
- this.HideSelf();
- }
- private RefreshList():void{
- let jsonAsset:JsonAsset=GameConfigManager.GetConfig("ExchangeGold");
- let config:any=jsonAsset.json;
- let itemView:Node;
- let dataList:any[]=config;
- if(dataList.length!=this.listContent.node.children.length){
- if(dataList.length<this.listContent.node.children.length){
- //删除多余的
- while(dataList.length<this.listContent.node.children.length){
- this.listContent.node.removeChild(this.listContent.node.children[0]);
- }
- }else{
- //添加缺少的
- while(dataList.length>this.listContent.node.children.length){
- itemView=instantiate(this.itemRenderPrefab);
- this.listContent.node.addChild(itemView);
- }
- }
- }
- let itemScript:ExchangeItemRenderScript;
- let count:number=dataList.length;
- for (let index = 0; index < count; index++) {
- itemView=this.listContent.node.children[index];
- itemScript=itemView.getComponent(ExchangeItemRenderScript);
- if(itemScript==null){
- throw new Error("商城列表项未挂载ExchangeItemRenderScript脚本!");
- }
- itemScript.UpdateItemRender(dataList[index]);
- }
- this.list.scrollToTop(0.1);
- }
- }
|