CommonItem.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { _decorator, Label, Node, Sprite, SpriteFrame } from 'cc';
  2. import { ResKeeper } from '../../../framework/res/ResKeeper';
  3. import { ItemEnum } from '../../common/InterfaceAddEnum';
  4. import { Framework } from '../../../framework/Framework';
  5. import { ViewID } from '../../../framework/config/LayerConf';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('CommonItem')
  8. export class CommonItem extends ResKeeper {
  9. @property({ type: Sprite, tooltip: "背景" })
  10. farmeBg: Sprite = null;
  11. @property({ type: Label, tooltip: "数量" })
  12. numTx: Label = null;
  13. @property({ type: Sprite, tooltip: "图标" })
  14. icoImg: Sprite = null;
  15. @property({ type: Node, tooltip: "碎片标记" })
  16. pieceIco: Node = null;
  17. @property({ type: Node, tooltip: "阵营标记" })
  18. raceIco: Node = null;
  19. @property({ type: Node, tooltip: "星级节点" })
  20. starNode: Node = null;
  21. @property({ type: [Node], tooltip: "星星" })
  22. stars: Node[] = [];
  23. @property({ type: [SpriteFrame], tooltip: "星星" })
  24. gradeBgs: SpriteFrame[] = [];
  25. @property({ type: Node, tooltip: "选中效果" })
  26. selectBg: Node = null;
  27. @property({ type: [SpriteFrame], tooltip: "背景图" })
  28. bgFrame: SpriteFrame[] = [];
  29. private isSelect = false;
  30. private isNumShow = true;
  31. private isClickEnable = true;
  32. private data = null;
  33. private clickFunc = null;
  34. protected onLoad() {
  35. this.selectBg.active = false;
  36. this.pieceIco.active = false;
  37. this.raceIco.active = false;
  38. this.starNode.active = false;
  39. }
  40. protected onDestroy() {
  41. //如果该组件有事件自行取消注释
  42. //Framework.event.removeEvent(this);
  43. super.onDestroy();
  44. }
  45. //如果使用了池中的节点,在该函数内归还,该函数会在onDestroy前调用
  46. onClose() {
  47. }
  48. //UI事件处理
  49. private onTouchButton(event: Event) {
  50. //Framework.audio.playEffect(AudioID.Click);
  51. let target: any = event.target;
  52. }
  53. refreshItem(data) {
  54. this.data = data;
  55. if (data.clickFunc) {
  56. this.clickFunc = data.clickFunc;
  57. }
  58. if (this.data.type == ItemEnum.material) {
  59. this.setMaterial();
  60. } else if (this.data.type == ItemEnum.equip) {
  61. this.setEquip();
  62. } else if (this.data.type == ItemEnum.role) {
  63. this.setRole();
  64. }
  65. else if (this.data.type == ItemEnum.user) {
  66. this.setUser();
  67. }
  68. this.numTx.node.active = this.isNumShow;
  69. }
  70. setMaterial() {
  71. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  72. this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  73. this.icoImg.spriteFrame = res;
  74. })
  75. this.numTx.string = this.data.count;
  76. }
  77. setEquip() {
  78. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  79. this.load('common', `texture/icon/equips/equip1_${this.data.conf.Slot}1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  80. this.icoImg.spriteFrame = res;
  81. })
  82. this.numTx.string = this.data.count;
  83. }
  84. setRole() {
  85. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  86. this.load('common', `texture/icon/hero/head_${this.data.conf.Id}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  87. this.icoImg.spriteFrame = res;
  88. })
  89. this.numTx.string = this.data.count;
  90. }
  91. setUser() {
  92. // this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  93. // this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  94. // this.icoImg.spriteFrame = res;
  95. // })
  96. // this.numTx.string = this.data.count;
  97. }
  98. setSelectEnable(state: boolean) {
  99. this.selectBg.active = state;
  100. }
  101. setClickEnable(state: boolean) {
  102. this.isClickEnable = state;
  103. }
  104. setNumShow(state: boolean) {
  105. this.isNumShow = state;
  106. }
  107. setClick(callFunc) {
  108. this.clickFunc = callFunc;
  109. }
  110. onClick() {
  111. this.isSelect = !this.isSelect;
  112. if (this.clickFunc) {
  113. this.clickFunc(this.data, this.isSelect);
  114. } else {
  115. if (this.isClickEnable) {
  116. Framework.layer.open(ViewID.ItemMsg, null, this.data);
  117. }
  118. }
  119. }
  120. }