GameItem.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { AppKernel } from '@scripts/app/AppKernel';
  2. import { IGameInfo } from '@scripts/store/gameStore/data';
  3. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  4. import { addBtnClick, removeBtnClick, setAlpha } from '@scripts/utils/UIHelper';
  5. import { _decorator, Button, Color, Component, Label, Node, Size, Sprite } from 'cc';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('GameItem')
  8. export class GameItem extends Component {
  9. @property(Sprite)
  10. gameIcon: Sprite;
  11. @property(Node)
  12. btnFavo: Node;
  13. @property(Sprite)
  14. favoFlag: Sprite;
  15. @property(Label)
  16. lblGameName: Label;
  17. private _data: IGameInfo;
  18. private _dataIdx: number;
  19. protected onLoad(): void {
  20. addBtnClick(this.node, this.onClick, this);
  21. addBtnClick(this.btnFavo, this.onBtnFavoClick, this);
  22. }
  23. protected onDestroy(): void {
  24. removeBtnClick(this.node, this.onClick, this);
  25. removeBtnClick(this.btnFavo, this.onBtnFavoClick, this);
  26. }
  27. public updateItem(data: IGameInfo, index: number) {
  28. this._data = data;
  29. this._dataIdx = index;
  30. if (!this.node.active) this.node.active = true;
  31. if (this.lblGameName) this.lblGameName.string = data.gameName;
  32. setAlpha(this.favoFlag, data.isFavorite);
  33. setSpriteFrame(this.gameIcon, "https://cdn.usgamestatic.com/24680/game/lw/bankersbreachcollisionways@1x.webp");
  34. }
  35. private onClick() {
  36. // AppKernel.appLobby.openGame(this._gameConfig.id);
  37. }
  38. private async onBtnFavoClick() {
  39. const gameStore = AppKernel.stores.gameStore;
  40. if (gameStore.favoriting) return;
  41. const index = this._dataIdx;
  42. const data = this._data;
  43. const isFavorite = data.isFavorite;
  44. const game = isFavorite ? await gameStore.reqFavoRemove(data.gameID)
  45. : await gameStore.reqFavoAdd(data.gameID);
  46. if (!!game && this._dataIdx === index) {
  47. this.updateItem(game, index);
  48. }
  49. }
  50. }