BaseUI.ts 4.5 KB

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