UtilsPanel.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { _decorator, SpriteFrame, Node, Sprite, Button, Label, Toggle, EditBox, RichText, sp, isValid, Prefab, instantiate, assetManager, UITransform, UIOpacity, ImageAsset, Component, js, builtinResMgr, UIRenderer, Material, ProgressBar, Layers, view, NodeEventType, Color, tween, Tween } from 'cc';
  2. import { Generate } from './components/Generate';
  3. export class UtilsPanel {
  4. public static getAllNeedCom(sc: any, node: Node, recursive: boolean = false) {
  5. SC = sc;
  6. let children = node.children;
  7. if (recursive) {
  8. for (let i = 0, len = children.length; i < len; i++) {
  9. children[i].walk(getComByName);
  10. }
  11. }
  12. else {
  13. for (let i = 0, len = children.length; i < len; i++) {
  14. getComByName(children[i]);
  15. }
  16. }
  17. }
  18. public static getArrCom(node: Node, name: string, count: number, type = null) {
  19. let arr = new Array(count);
  20. let children = node.children;
  21. let cur_connt = 0;
  22. for (let i = 0; i < children.length; i++) {
  23. if (children[i].name.startsWith(name)) {
  24. cur_connt++;
  25. let index = parseInt(children[i].name.slice(name.length));
  26. arr[index] = type ? children[i].getComponent(type) : children[i];
  27. if (cur_connt >= count) break;
  28. }
  29. }
  30. return arr;
  31. }
  32. public static addBtnEvent(btn_node: Node, func: Function, target?: any) {
  33. let btn = btn_node.getComponent(Button);
  34. if (!btn) {
  35. btn = btn_node.addComponent(Button);
  36. btn.transition = Button.Transition.SCALE;
  37. btn.target = btn_node;
  38. btn.zoomScale = 0.9;
  39. btn.duration = 0.1;
  40. }
  41. btn_node.on("click", func, target);
  42. return btn;
  43. }
  44. public static addBtnEvent2(btn_node: Node, func: string, target: any, param: string | number) {
  45. let btn = btn_node.getComponent(Button);
  46. if (!btn) {
  47. btn = btn_node.addComponent(Button);
  48. btn.transition = Button.Transition.SCALE;
  49. btn.target = btn_node;
  50. btn.zoomScale = 0.9;
  51. btn.duration = 0.1;
  52. }
  53. let handler = new Component.EventHandler();
  54. handler.target = target.node;
  55. handler.component = js.getClassName(target);
  56. handler.handler = func;
  57. //@ts-ignore
  58. handler.customEventData = param;
  59. btn.clickEvents.push(handler);
  60. return btn;
  61. }
  62. public static clearBtnEvent(node: Node, remove_btn: boolean) {
  63. node.off("click");
  64. let btn = node.getComponent(Button);
  65. if (btn) {
  66. btn.clickEvents.length = 0;
  67. if (remove_btn) btn.destroy();
  68. }
  69. }
  70. public static removeBtns(node: Node) {
  71. node.walk(function (node1: Node) {
  72. let btn = node1.getComponent(Button);
  73. if (btn) btn.destroy();
  74. node1.off("click");
  75. })
  76. }
  77. public static setNodeGray(node_or_btn: Node | Button, state: boolean, recursive = false) {
  78. let node = node_or_btn;
  79. let btn = null;
  80. if (node_or_btn instanceof Button) {
  81. btn = node_or_btn;
  82. node = node_or_btn.node;
  83. }
  84. else {
  85. btn = (node as Node).getComponent(Button);
  86. }
  87. if (btn) btn.interactable = state;
  88. // let mat_name = state ? "ui-sprite-material" : "";
  89. let mat = state ? null : builtinResMgr.get("ui-sprite-gray-material");
  90. let func = function (node0: any) {
  91. let btn = node0.getComponent(Button)
  92. btn && (btn.interactable = state);
  93. let com_rendener = node0.getComponent(UIRenderer);
  94. if (com_rendener && !com_rendener.setAnimation) com_rendener.customMaterial = mat;
  95. }
  96. if (recursive) {
  97. (node as Node).walk(func);
  98. }
  99. else {
  100. func(node);
  101. }
  102. }
  103. public static setItemIcon(bundle_name: string, url: string, sp_icon: Sprite) {
  104. assetManager.getBundle(bundle_name)?.load(url, SpriteFrame, (err: any, sp: SpriteFrame) => {
  105. if (err) return;
  106. if (isValid(sp_icon)) {
  107. sp_icon.spriteFrame = sp;
  108. }
  109. })
  110. }
  111. }
  112. var SC = null;
  113. function getComByName(node: Node) {
  114. let name = node.name;
  115. let com = null;
  116. switch (name[0]) {
  117. case "a":
  118. if (name.startsWith("auto_btn_")) {
  119. let click_name = name.slice(10);
  120. let func_name = "on" + name[9].toUpperCase() + click_name + "Click";
  121. if (SC[func_name]) UtilsPanel.addBtnEvent(node, SC[func_name], SC);
  122. }
  123. break;
  124. case "b":
  125. if (name.startsWith("btn_")) com = node.getComponent(Button);
  126. break;
  127. case "e":
  128. if (name.startsWith("edit_")) com = node.getComponent(EditBox);
  129. break;
  130. case "g":
  131. if (name.startsWith("gene_")) com = node.getComponent(Generate);
  132. break;
  133. case "l":
  134. if (name.startsWith("lb_")) com = node.getComponent(Label);
  135. break;
  136. case "n":
  137. if (name.startsWith("node_")) com = node;
  138. break;
  139. case "r":
  140. if (name.startsWith("rich_")) com = node.getComponent(RichText);
  141. break;
  142. case "s":
  143. if (name.startsWith("spine_")) com = node.getComponent(sp.Skeleton);
  144. else if (name.startsWith("sp_")) com = node.getComponent(Sprite);
  145. else if (name.startsWith("sc_")) {
  146. name = name.slice(3);
  147. //@ts-ignore
  148. com = node._components[0];//cocos creator 中组件排序 如果取不到留意顺序是否出错
  149. }
  150. break;
  151. case "t":
  152. if (name.startsWith("toggle_")) com = node.getComponent(Toggle);
  153. break;
  154. case "u":
  155. if (name.startsWith("uitrans_")) com = node.getComponent(UITransform);
  156. if (name.startsWith("uiopca_")) com = node.getComponent(UIOpacity);
  157. break;
  158. }
  159. if (com && SC.hasOwnProperty(name)) {
  160. if (SC[name]) console.warn("重复调用?", name);
  161. SC[name] = com;
  162. }
  163. }