ItemCount.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { KindItem } from "../../../common/Xys";
  2. import { ItemEvent } from "../../../data/const/EventConst";
  3. import GameDataCenter from "../../../data/GameDataCenter";
  4. import FguiLoadMgr from "../../../frameWork/fgui/FguiLoadMgr";
  5. import FguiMgr from "../../../frameWork/fgui/FguiMgr";
  6. import EventMng from "../../../manager/EventMng";
  7. import ClickAuEffect from "../../../utils/ClickAuEffect";
  8. import GameMath from "../../../utils/GameMath";
  9. export interface ItemCountExtraParam {
  10. showAddBtn?: boolean; //是否显示加按钮
  11. fullNumberShow?: boolean; //是否显示完整道具拥有数量
  12. isBuyItem?: boolean; //是否点击从集市购买道具
  13. notEnoughRed?: boolean; //道具不足是否显示红色字
  14. isPreCost?: boolean; //是否是预扣除类道具
  15. FixedCount?: number | string;// 显示固定数量,不用GameDataCenter.item.getItemCount
  16. }
  17. export default class ItemCount {
  18. item: KindItem;
  19. node: fairygui.GButton;
  20. icon: fairygui.GLoader;
  21. AddBtn: fairygui.GButton;
  22. CostCount: number = 0;
  23. callBack: Function;
  24. fullNumberShow: boolean;
  25. isBuyItem: boolean;
  26. redText: boolean;
  27. isPreCost: boolean;
  28. FixedCount: number | string;
  29. constructor(com: fairygui.GButton, kinditem?: KindItem, costCount: number = 0, param?: ItemCountExtraParam) {
  30. this.node = com;
  31. this.AddBtn = this.node.getChild("AddBtn") as fairygui.GButton;
  32. this.icon = this.node.getChild("icon") as fairygui.GLoader;
  33. if (kinditem) this.setData(kinditem, costCount, param);
  34. this.AddBtn.visible = param && param.showAddBtn == false ? false : true;
  35. }
  36. setData(kindItem: KindItem, costCount: number = 0, param?: ItemCountExtraParam, iconUrl?: string) {
  37. if (kindItem) {
  38. this.CostCount = costCount;
  39. this.fullNumberShow = param?.fullNumberShow ? true : false;
  40. this.isBuyItem = param?.isBuyItem ? true : false;
  41. this.redText = param?.notEnoughRed ? true : false;
  42. this.isPreCost = param?.isPreCost ?? false;
  43. this.FixedCount = param?.FixedCount;
  44. this.item = kindItem;
  45. if (!iconUrl) {
  46. FguiLoadMgr.loadItemIcon(this.icon, this.item, null, 0.4);
  47. } else {
  48. if (iconUrl.indexOf('ui://') >= 0) {
  49. this.icon.url = iconUrl;
  50. }else{
  51. //加载bundle资源
  52. let bundleName: string = iconUrl.split("|")[0];
  53. let urlStr: string = iconUrl.split("|")[1];
  54. GameDataCenter.loading.loadSpriteFormBundle(this.icon?.node, bundleName, urlStr);
  55. }
  56. }
  57. this.showItem();
  58. this.node.visible = true;
  59. } else {
  60. this.item = null;
  61. this.node.visible = false;
  62. }
  63. }
  64. showItem() {
  65. if (this.item == null) return;
  66. if (this.FixedCount) {
  67. this.node.title = this.FixedCount + "";// 显示传入的固定数量
  68. if (this.redText) {
  69. let itemCount = GameDataCenter.item.getItemCount(this.item);
  70. if (Number(this.FixedCount) > itemCount) {
  71. this.node.getChild("title").asTextField.color = cc.color().fromHEX("#E45849");
  72. } else {
  73. this.node.getChild("title").asTextField.color = cc.color().fromHEX("#FFFBDE");
  74. }
  75. }
  76. } else {
  77. let cost: string = this.CostCount ? "/" + this.CostCount : "";
  78. let itemCount = this.isPreCost ? GameDataCenter.item.getItemPreCostCount(this.item) : GameDataCenter.item.getItemCount(this.item);
  79. let num = this.fullNumberShow ? itemCount : GameMath.showNum(itemCount);
  80. this.node.title = num + cost;
  81. if (this.redText && itemCount < this.CostCount) {
  82. this.node.getChild("title").asTextField.color = cc.color().fromHEX("#E45849");
  83. } else {
  84. this.node.getChild("title").asTextField.color = cc.color().fromHEX("#FFFBDE");
  85. }
  86. }
  87. }
  88. showDetail() {
  89. // FguiMgr.Instance.openUI(ItemDetailViewView, ViewZorder.POP, null, this.item);
  90. }
  91. /**设置点击回调 如果有设置,就不会弹出详情
  92. */
  93. setCallBack(callBack: Function) {
  94. this.callBack = callBack;
  95. }
  96. onEnable() {
  97. this.node.onClick(this.onClick, this);
  98. EventMng.on(ItemEvent.UP_ITEM, this.showItem, this);
  99. this.showItem();
  100. }
  101. onDisable() {
  102. this.node.offClick(this.onClick, this);
  103. EventMng.off(ItemEvent.UP_ITEM, this.showItem, this);
  104. }
  105. @ClickAuEffect()
  106. onClick() {
  107. if (this.callBack) {
  108. this.callBack();
  109. } else {
  110. if (this.isBuyItem) {
  111. // GameDataCenter.shop.openBuyItemView(this.item);
  112. } else {
  113. this.showDetail();
  114. }
  115. }
  116. }
  117. }