ShopMediator.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { _decorator, Component, Node, Prefab, LayoutComponent, instantiate, ScrollViewComponent } from 'cc';
  2. import { GUIManager } from '../../../engines/gui/GUIManager';
  3. import { GUIMediator } from '../../../engines/gui/GUIMediator';
  4. import GameConfigManager from '../../models/GameConfigManager';
  5. import { UIConst } from '../UIConst';
  6. import { ShopItemRenderScript } from './ShopItemRenderScript';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('ShopMediator')
  9. export class ShopMediator extends GUIMediator {
  10. @property({
  11. type:Prefab
  12. })
  13. ListItemRenderPrefab:Prefab=null;
  14. @property({
  15. type:LayoutComponent
  16. })
  17. ListContext:LayoutComponent=null;
  18. @property({
  19. type:ScrollViewComponent
  20. })
  21. List:ScrollViewComponent=null;
  22. OnShow(data?:any):void{
  23. super.OnShow(data);
  24. this.RefreshList(true);
  25. this.AddEvent();
  26. }
  27. OnHide():void{
  28. this.RemoveEvent();
  29. }
  30. /**
  31. * 返回按钮点击
  32. */
  33. public BackButtonClick():void{
  34. GUIManager.single.Show(UIConst.PREPARE_UI);
  35. this.HideSelf();
  36. }
  37. private AddEvent():void{
  38. }
  39. private RemoveEvent():void{
  40. }
  41. private RefreshList(srollToZ:boolean):void{
  42. let itemView:Node;
  43. if(GameConfigManager.ShopList.length!=this.ListContext.node.children.length){
  44. if(GameConfigManager.ShopList.length<this.ListContext.node.children.length){
  45. //删除多余的
  46. while(GameConfigManager.ShopList.length<this.ListContext.node.children.length){
  47. this.ListContext.node.removeChild(this.ListContext.node.children[0]);
  48. }
  49. }else{
  50. //添加缺少的
  51. while(GameConfigManager.ShopList.length>this.ListContext.node.children.length){
  52. itemView=instantiate(this.ListItemRenderPrefab);
  53. this.ListContext.node.addChild(itemView);
  54. }
  55. }
  56. }
  57. let itemScript:ShopItemRenderScript;
  58. let count:number=GameConfigManager.ShopList.length;
  59. for (let index = 0; index < count; index++) {
  60. itemView=this.ListContext.node.children[index];
  61. itemScript=itemView.getComponent(ShopItemRenderScript);
  62. if(itemScript==null){
  63. throw new Error("商城列表项未挂载ShopItemRenderScript脚本!");
  64. }
  65. itemScript.UpdateItemRender(GameConfigManager.ShopList[index]);
  66. }
  67. if(srollToZ){
  68. this.List.scrollToTop(0.1);
  69. }
  70. }
  71. start () {
  72. // Your initialization goes here.
  73. }
  74. // update (deltaTime: number) {
  75. // // Your update function goes here.
  76. // }
  77. }