RedUtil.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { gameMethod } from "../../common/gameMethod";
  2. import { RedUtilType } from "../../data/const/TypeConst";
  3. import { ItemLabelRender } from "./ListUtil";
  4. export class RedUtil {
  5. private static _redMap: Map<RedUtilType, RedDotClass> = new Map();
  6. static clear() {
  7. this._redMap.clear()
  8. }
  9. static registerBaseRed(type: RedUtilType, func: () => boolean, target: any = null) {
  10. if (this.getRedDot(type)) {
  11. if (CC_PREVIEW) console.warn("红点" + type + "已经注册过了");
  12. return;
  13. }
  14. let red = new RedDotBase(type, func.bind(target));
  15. if (red == null) return;
  16. this._redMap.set(type, red);
  17. }
  18. static registerUpperRed(type: RedUtilType, childs: RedUtilType[], func: () => boolean = null) {
  19. if (this.getRedDot(type)) {
  20. if (CC_PREVIEW) console.warn("红点" + type + "已经注册过了");
  21. return;
  22. }
  23. let rednode = new RedDotNode(type);
  24. this.setChilds(rednode, childs);
  25. this._redMap.set(type, rednode);
  26. }
  27. static getRedState(type: RedUtilType): boolean {
  28. let red = this.getRedDot(type);
  29. if (red == null) {
  30. if (CC_PREVIEW) console.warn(`红点 ${type} 不存在`);
  31. return false;
  32. }
  33. return red.getRedState();
  34. }
  35. static getRedDot(type) {
  36. return this._redMap?.get(type);
  37. }
  38. static upRedNode(type: RedUtilType, node: RedDot) {
  39. let red = this.getRedDot(type);
  40. if (red == null) {
  41. if (CC_PREVIEW) console.warn(`节点:${node.name} 的红点 ${type} 不存在 `);
  42. return;
  43. }
  44. if (red.node && red.node != node) {
  45. if (CC_PREVIEW) console.warn(`节点${red.node?.name}已经挂载了${type}红点,${node?.name}重复挂载了`);
  46. return;
  47. }
  48. red.node = node;
  49. }
  50. static offRedNode(type: RedUtilType) {
  51. let red = this.getRedDot(type);
  52. if (red == null) {
  53. if (CC_PREVIEW) console.warn(`红点 ${type} 不存在 `);
  54. return;
  55. }
  56. red.node = null;
  57. }
  58. private static setChilds(red: RedDotNode, childs: RedUtilType[]) {
  59. for (let child of childs) {
  60. if (child == red.type) {
  61. if (CC_PREVIEW) console.warn(`红点 ${red.type} 不能自己依赖自己`);
  62. continue;
  63. }
  64. this.setChild(red.type, child);
  65. }
  66. red.childs = childs;
  67. }
  68. static updateRed(type: RedUtilType) {
  69. let red = this.getRedDot(type);
  70. if (red instanceof RedDotBase) {
  71. red.updateRed();
  72. } else {
  73. if (CC_PREVIEW) console.warn(`红点类型不是底部红点 ${type}`);
  74. }
  75. }
  76. private static setChild(parent: RedUtilType, child: RedUtilType) {
  77. let red = this._redMap.get(child);
  78. //排除重复挂载依赖
  79. if (red == null) {
  80. if (CC_PREVIEW) console.warn(`${child} 红点不存在`);
  81. return
  82. }
  83. if (red.parent) {
  84. if (CC_PREVIEW) console.warn(`${child} 已经有父红点了`);
  85. return
  86. }
  87. red.parent = parent;
  88. }
  89. public static initRedUtil() {
  90. this._redMap?.forEach((red: RedDotClass) => {
  91. if (red instanceof RedDotBase) red.updateRed();
  92. });
  93. }
  94. }
  95. abstract class RedDotClass {
  96. type: RedUtilType;
  97. parent: RedUtilType;
  98. node: RedDot;
  99. constructor(type: RedUtilType) {
  100. this.type = type;
  101. }
  102. abstract getRedState(): boolean;
  103. abstract updateRed();
  104. }
  105. class RedDotNode extends RedDotClass {
  106. childs: RedUtilType[] = [];
  107. getRedState(): boolean {
  108. let isred = false;
  109. for (let i of this.childs) {
  110. let red = RedUtil.getRedDot(i);
  111. isred = red.getRedState();
  112. if (isred) break;
  113. }
  114. return isred;
  115. }
  116. updateRed() {
  117. this.node?.upRed();
  118. if (this.parent) {
  119. let red = RedUtil.getRedDot(this.parent);
  120. if (red == null) {
  121. if (CC_PREVIEW) console.warn(`红点:${this.parent} 不存在`);
  122. return;
  123. }
  124. red.updateRed();
  125. }
  126. }
  127. }
  128. class RedDotBase extends RedDotClass {
  129. redCount: boolean;
  130. func: () => boolean;
  131. constructor(type: RedUtilType, func: () => boolean) {
  132. super(type);
  133. this.func = func;
  134. }
  135. reCalRed(): boolean {
  136. try {
  137. this.redCount = this.func() ?? false;
  138. } catch (err) {
  139. if (CC_PREVIEW) console.warn(err);
  140. this.redCount = false;
  141. }
  142. return this.redCount;
  143. }
  144. getRedState(): boolean {
  145. return this.redCount;
  146. }
  147. updateRed() {
  148. this.reCalRed();
  149. this.node?.upRed();
  150. if (this.parent) {
  151. let red = RedUtil.getRedDot(this.parent);
  152. if (red == null) {
  153. if (CC_PREVIEW) console.warn(`红点:${this.parent} 不存在`);
  154. return;
  155. }
  156. red.updateRed();
  157. }
  158. }
  159. }
  160. export class RedDot extends ItemLabelRender {
  161. type: RedUtilType;
  162. show: fairygui.Controller;
  163. onInit(): void {
  164. this.initControllers();
  165. }
  166. onShow(): void {
  167. this.upNode();
  168. }
  169. onHide(): void {
  170. this.offNode();
  171. }
  172. upNode() {
  173. if (this.type == null) {
  174. this.show.selectedIndex = 1;
  175. return;
  176. }
  177. RedUtil.upRedNode(this.type, this);
  178. this.upRed();
  179. }
  180. offNode() {
  181. if (this.type == null) return;
  182. RedUtil.offRedNode(this.type);
  183. }
  184. setData(data: RedUtilType, index?: number, param?: any) {
  185. this.type = data;
  186. this.upNode()
  187. }
  188. upRed() {
  189. if (gameMethod.isEmpty(this.node)) {
  190. return;
  191. }
  192. this.show.selectedIndex = RedUtil.getRedState(this.type) ? 0 : 1;
  193. }
  194. isShow() {
  195. return this.show.selectedIndex == 0;
  196. }
  197. }