BaseUI.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import {
  2. isValid,
  3. Node,
  4. Component,
  5. find,
  6. Button,
  7. Color,
  8. Label,
  9. Event,
  10. SpriteFrame,
  11. Animation,
  12. Sprite,
  13. Vec3,
  14. } from "cc";
  15. import { UIController } from "../../core_tgx/easy_ui_framework/UIController";
  16. export default class BaseUI extends Component {
  17. public onHide() {}
  18. public onShow() {}
  19. private m_objects: Map<string, Node> = new Map<string, Node>();
  20. protected setButtonEnable(name: string, enable: boolean) {
  21. if (!isValid(this.node)) {
  22. return;
  23. }
  24. this.FindAs(name, Sprite).grayscale = !enable;
  25. this.FindButton(name).interactable = enable;
  26. }
  27. public FindAs<T extends Component>(
  28. name: string,
  29. type: new (...args: any[]) => T
  30. ): T {
  31. const node = this.FindNode(name);
  32. if (!node) return null;
  33. return node.getComponent(type);
  34. }
  35. public FindLabel(name: string): Label {
  36. return this.FindAs(name, Label);
  37. }
  38. public FindButton(name: string): Button {
  39. return this.FindAs(name, Button);
  40. }
  41. public async setText(nodeName: string, text: string): Promise<void> {
  42. const node = this.FindAs<Label>(nodeName, Label);
  43. if (!node) {
  44. console.warn("setText", nodeName, "not found");
  45. return;
  46. }
  47. node.string = text;
  48. await Promise.resolve();
  49. }
  50. public async SetSprite(nodeName: string, spritePath: string): Promise<void> {
  51. // const sp: SpriteFrame = await AB.ins.loadSpriteFrame(spritePath);
  52. // const node = this.FindAs<Sprite>(nodeName, Sprite);
  53. // node.spriteFrame = sp;
  54. }
  55. public FindNode(name: string, node: Node = null): Node {
  56. if (!isValid(this.node)) {
  57. return;
  58. }
  59. if (this.m_objects == null) {
  60. return null;
  61. }
  62. // 检查缓存
  63. // 检查缓存
  64. if (this.m_objects.has(name)) {
  65. const cachedNode = this.m_objects.get(name);
  66. if (isValid(cachedNode)) return cachedNode;
  67. this.m_objects.delete(name);
  68. }
  69. // 设置起始节点
  70. node = isValid(node) ? node : this.node;
  71. if (!isValid(node)) return null;
  72. // 检查当前节点
  73. if (node.name == name) {
  74. this.m_objects.set(name, node);
  75. return node;
  76. }
  77. // 递归查找子节点
  78. for (const child of node.children) {
  79. const result = this.FindNode(name, child);
  80. if (result) {
  81. this.m_objects.set(name, result);
  82. return result;
  83. }
  84. }
  85. return null;
  86. }
  87. protected onLoad(): void {
  88. let t = new Date().getTime();
  89. this.deepScanChildAndFindButtons(this.node);
  90. console.log(
  91. "BaseUI onLoad:",
  92. this.node.name,
  93. new Date().getTime() - t,
  94. "ms"
  95. );
  96. }
  97. private _onBtnClick(event: Event, customEventData: string): void {
  98. const target = event.target as unknown as Node;
  99. const name = target.name;
  100. console.log("按钮点击", name);
  101. this.onBtnClick(name, event, customEventData);
  102. if (name != "btn_close" && name != "btn_tap_close") {
  103. this.simpleOnBtnClick(name);
  104. console.log("simpleOnBtnClick", name);
  105. }
  106. }
  107. protected onBtnClick(name: string, event: Event, customEventData: any): void {
  108. if (name == "btn_close" || name == "btn_tap_close") {
  109. this.closePage();
  110. }
  111. }
  112. protected simpleOnBtnClick(name: string) {}
  113. protected canClose: boolean = true;
  114. protected closePage() {
  115. if (!this.canClose) {
  116. return;
  117. }
  118. let isAnim = false;
  119. // let bg = this.FindNode("bg");
  120. // if (bg != null) {
  121. // // scale 0.5
  122. // tween(bg)
  123. // .to(0.1, { scale: new Vec3(1.2, 1.2, 1) })
  124. // .to(0.3, { scale: new Vec3(0, 0, 1) })
  125. // .start();
  126. // isAnim = true;
  127. // }
  128. if (this.m_objects != null) {
  129. this.m_objects.clear();
  130. }
  131. this.m_objects = null;
  132. let self = this;
  133. if (!isAnim) {
  134. self.node.destroy();
  135. } else {
  136. setTimeout(() => {
  137. if (isValid(self.node)) {
  138. self.node.destroy();
  139. }
  140. }, 1000);
  141. }
  142. }
  143. protected deepScanChildAndFindButtons(node: Node): void {
  144. // console.log("扫描节点:", node.name);
  145. // 如果节点是BaseUI,并且不是当前节点,则不扫描
  146. if (
  147. node.getComponent(BaseUI) &&
  148. node.getComponent(BaseUI).node != this.node
  149. ) {
  150. return;
  151. }
  152. if (node.name.startsWith("btn_")) {
  153. const button = node.getComponent(Button);
  154. if (!button) {
  155. let btn = node.addComponent(Button);
  156. btn.transition = Button.Transition.COLOR;
  157. btn.normalColor = new Color("#ffffff");
  158. btn.hoverColor = new Color("#D3D3D3");
  159. btn.pressedColor = new Color("#D3D3D3");
  160. btn.disabledColor = new Color("#ffffff");
  161. }
  162. node.on(Button.EventType.CLICK, this._onBtnClick, this);
  163. }
  164. node.children.forEach((child) => {
  165. this.deepScanChildAndFindButtons(child);
  166. });
  167. }
  168. protected setActive(name: string, a: boolean) {
  169. this.FindNode(name).active = a;
  170. }
  171. }
  172. // type Constructor<T> = new (...args: any[]) => T;