ItemIcon.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { gameMethod } from "../../../common/gameMethod";
  2. import { KindItem } from "../../../common/Xys";
  3. import { BaseItemType } from "../../../data/const/TypeConst";
  4. import GameDataCenter from "../../../data/GameDataCenter";
  5. import ResSprite from "../../../frameWork/compment/ResSprite";
  6. import FguiLoadMgr from "../../../frameWork/fgui/FguiLoadMgr";
  7. import ClickAuEffect from "../../../utils/ClickAuEffect";
  8. export interface ItemIconParam {
  9. showNum?: boolean;
  10. type?: BaseItemType;
  11. }
  12. export default class ItemIcon extends fgui.GLabel {
  13. item: KindItem;
  14. itemIcon: fgui.GLoader;
  15. itemNum: fgui.GTextField;
  16. isextra: fgui.Controller;
  17. extra: fgui.GComponent;
  18. itemType: string; // 道具类型
  19. type: BaseItemType = BaseItemType.empty;
  20. itemIconResSprite: ResSprite;
  21. protected onConstruct(): void {
  22. this.itemIcon = this.getChild("icon").asLoader;
  23. this.itemNum = this.getChild("title").asTextField;
  24. this.extra = this.getChild("Extra").asCom;
  25. this.isextra = this.getController("isExtra");
  26. this.itemIconResSprite = this.itemIcon.node.addComponent(ResSprite);
  27. }
  28. protected onEnable(): void {
  29. this.onClick(this.onClickItem, this);
  30. }
  31. protected onDisable(): void {
  32. this.offClick(this.onClickItem, this);
  33. }
  34. setData(item: KindItem, param?: ItemIconParam) {
  35. if (gameMethod.isEmpty(item)) {
  36. this.itemIcon.url = '';
  37. this.itemIconResSprite.unsetSpriteFrame();
  38. this.itemNum.text = '';
  39. return;
  40. }
  41. this.item = item;
  42. this.type = param?.type ?? BaseItemType.empty;
  43. let confBase = GameDataCenter.item.getItemCfgBase(item);
  44. this.itemType = confBase.type;
  45. if (confBase == null) {
  46. console.error("没有找到配置信息:", confBase);
  47. this.itemIcon.url = '';
  48. return;
  49. }
  50. this.itemIconResSprite.unsetSpriteFrame();
  51. FguiLoadMgr.loadItemIcon(this.itemIcon, item, () => { }, 1);
  52. if (param?.showNum) {
  53. this.itemNum.text = "x" + item?.[2];
  54. }
  55. else {
  56. this.itemNum.text = "";
  57. }
  58. }
  59. @ClickAuEffect()
  60. onClickItem(evt: Event) {
  61. // 阻止事件继续传播
  62. evt.stopPropagation();
  63. if (this.type == BaseItemType.detail) {
  64. this.showItemDetail();
  65. }
  66. }
  67. showItemDetail() {
  68. // 显示道具详情
  69. }
  70. }