CommonItem.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }else if (this.data.type == ItemEnum.user) {
  65. this.setUser();
  66. }
  67. this.numTx.node.active = this.isNumShow;
  68. }
  69. setMaterial() {
  70. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  71. this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  72. this.icoImg.spriteFrame = res;
  73. })
  74. this.numTx.string = this.data.count;
  75. }
  76. setEquip() {
  77. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  78. this.load('common', `texture/icon/equips/equip1_${this.data.conf.Slot}1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  79. this.icoImg.spriteFrame = res;
  80. })
  81. this.numTx.string = this.data.count;
  82. }
  83. setRole() {
  84. this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  85. this.load('common', `texture/icon/hero/head_${this.data.conf.Id}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  86. this.icoImg.spriteFrame = res;
  87. })
  88. this.numTx.string = this.data.count;
  89. this.raceIco.active = true;
  90. this.load('common', `texture/image/race_ball_${this.data.conf.Race}/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  91. this.raceIco.getComponent(Sprite).spriteFrame = res;
  92. })
  93. }
  94. setUser() {
  95. // this.farmeBg.spriteFrame = this.bgFrame[this.data.conf.Quality];
  96. // this.load('common', `texture/icon/item/item_1/spriteFrame`, SpriteFrame, (res: SpriteFrame) => {
  97. // this.icoImg.spriteFrame = res;
  98. // })
  99. // this.numTx.string = this.data.count;
  100. }
  101. setSelectEnable(state: boolean) {
  102. this.selectBg.active = state;
  103. }
  104. setClickEnable(state: boolean) {
  105. this.isClickEnable = state;
  106. }
  107. setNumShow(state: boolean) {
  108. this.isNumShow = state;
  109. }
  110. setGray(state: boolean) {
  111. this.farmeBg.grayscale = state;
  112. this.icoImg.grayscale = state;
  113. this.raceIco.getComponent(Sprite).grayscale = state;
  114. }
  115. setClick(callFunc) {
  116. this.clickFunc = callFunc;
  117. }
  118. onClick() {
  119. this.isSelect = !this.isSelect;
  120. if (this.clickFunc) {
  121. this.clickFunc(this.data, this.isSelect);
  122. } else {
  123. if (this.isClickEnable) {
  124. Framework.layer.open(ViewID.ItemMsg, null, this.data);
  125. }
  126. }
  127. }
  128. }