ExchangeMediator.ts 4.1 KB

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