ExchangeMediator.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { GUIMediator } from "../../../engines/gui/GUIMediator";
  2. import { _decorator, Component, Node, ProgressBarComponent, LabelComponent, Prefab, ScrollViewComponent, LayoutComponent, JsonAsset, instantiate } from 'cc';
  3. import GameConfigManager from "../../models/GameConfigManager";
  4. import { GUIManager } from "../../../engines/gui/GUIManager";
  5. import { UIConst } from "../UIConst";
  6. import { GameModel } from "../../models/GameModel";
  7. import { DataModelEventType } from "../../../engines/models/DataModelEventType";
  8. import { GamePropertys } from "../../models/GamePropertys";
  9. import StringUtils from "../../../engines/utils/StringUtils";
  10. import { ExchangeItemRenderScript } from "./ExchangeItemRenderScript";
  11. const { ccclass, property } = _decorator;
  12. @ccclass('ExchangeMediator')
  13. export default class ExchangeMediator extends GUIMediator
  14. {
  15. @property({
  16. type: LabelComponent
  17. })
  18. glodLabel: LabelComponent = null;
  19. @property({
  20. type: LabelComponent
  21. })
  22. glodLabel1: LabelComponent = null;
  23. @property({
  24. type: LabelComponent
  25. })
  26. diamondLabel: LabelComponent = null;
  27. @property({
  28. type:Prefab
  29. })
  30. itemRenderPrefab:Prefab=null;
  31. @property({
  32. type:ScrollViewComponent
  33. })
  34. list:ScrollViewComponent=null;
  35. @property({
  36. type:LayoutComponent
  37. })
  38. listContent:LayoutComponent=null;
  39. constructor(uiName:string){
  40. super(uiName);
  41. }
  42. OnShow(data?:any):void{
  43. super.OnShow(data);
  44. this.RefreshList();
  45. this.RefreshGlod();
  46. this.RefreshDiamond();
  47. this.AddEvent();
  48. }
  49. OnHide():void{
  50. this.RemoveEvent();
  51. }
  52. private AddEvent():void{
  53. GameModel.single.AddEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged, 0);
  54. }
  55. private RemoveEvent():void{
  56. GameModel.single.RemoveEvent(DataModelEventType.PROPERTY_CHANGED, this, this.GameModelPropertyChanged);
  57. }
  58. private GameModelPropertyChanged(key: string): void {
  59. switch (key) {
  60. case GamePropertys.gold:
  61. this.CallNextFrame(this.RefreshGlod.bind(this));
  62. break;
  63. case GamePropertys.diamond:
  64. this.CallNextFrame(this.RefreshDiamond.bind(this));
  65. break;
  66. }
  67. }
  68. private RefreshGlod(): void {
  69. if (this.glodLabel != null) {
  70. this.glodLabel.string = StringUtils.numberUtilsEn(GameModel.single.gold);
  71. }
  72. this.glodLabel1.string = StringUtils.numberUtilsEn(GameModel.single.fullEarnings) + "/秒";
  73. }
  74. private RefreshDiamond(): void {
  75. if (this.diamondLabel != null) {
  76. this.diamondLabel.string = GameModel.single.diamond.toString();
  77. }
  78. }
  79. /**
  80. * 关闭界面
  81. */
  82. CloseButtonClickHandler():void{
  83. GUIManager.single.Show(UIConst.PREPARE_UI);
  84. this.HideSelf();
  85. }
  86. private RefreshList():void{
  87. let jsonAsset:JsonAsset=GameConfigManager.GetConfig("ExchangeGold");
  88. let config:any=jsonAsset.json;
  89. let itemView:Node;
  90. let dataList:any[]=config;
  91. if(dataList.length!=this.listContent.node.children.length){
  92. if(dataList.length<this.listContent.node.children.length){
  93. //删除多余的
  94. while(dataList.length<this.listContent.node.children.length){
  95. this.listContent.node.removeChild(this.listContent.node.children[0]);
  96. }
  97. }else{
  98. //添加缺少的
  99. while(dataList.length>this.listContent.node.children.length){
  100. itemView=instantiate(this.itemRenderPrefab);
  101. this.listContent.node.addChild(itemView);
  102. }
  103. }
  104. }
  105. let itemScript:ExchangeItemRenderScript;
  106. let count:number=dataList.length;
  107. for (let index = 0; index < count; index++) {
  108. itemView=this.listContent.node.children[index];
  109. itemScript=itemView.getComponent(ExchangeItemRenderScript);
  110. if(itemScript==null){
  111. throw new Error("商城列表项未挂载ExchangeItemRenderScript脚本!");
  112. }
  113. itemScript.UpdateItemRender(dataList[index]);
  114. }
  115. this.list.scrollToTop(0.1);
  116. }
  117. }