TipsLayer.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { _decorator, Component, Node, Event } from "cc";
  2. import BaseUI from "../../scripts/base/BaseUI";
  3. import { Hall } from "../hall/Hall";
  4. import { RichText } from "cc";
  5. import { Label } from "cc";
  6. const { ccclass, property } = _decorator;
  7. export class TipsData {
  8. forceConfirm: boolean = false;
  9. title: string;
  10. content: string;
  11. onConfirm: () => void = null;
  12. singleName: string = "";
  13. link: string = null;
  14. }
  15. export interface OnConfirm {
  16. (): void;
  17. }
  18. @ccclass("TipsLayer")
  19. export class TipsLayer extends BaseUI {
  20. static confirm(title: string, content: string) {
  21. return new Promise((resolve, reject) => {
  22. let tipsData: TipsData = {
  23. title: title,
  24. link: null,
  25. content: content,
  26. onConfirm: () => {
  27. resolve(true);
  28. },
  29. forceConfirm: true,
  30. singleName: null,
  31. };
  32. TipsLayer.show(tipsData);
  33. });
  34. }
  35. private onConfirm: OnConfirm = null;
  36. private static single: Map<string, Node> = new Map();
  37. static async show(data: TipsData): Promise<TipsLayer> {
  38. if (data.singleName) {
  39. if (TipsLayer.single.has(data.singleName)) {
  40. return TipsLayer.single.get(data.singleName).getComponent(TipsLayer);
  41. }
  42. }
  43. let layer = await Hall.ins.showLayer("prefab/layer/TipsLayer");
  44. let comp = layer.getComponent(TipsLayer);
  45. comp.init(data);
  46. if (data.singleName) {
  47. TipsLayer.single.set(data.singleName, layer);
  48. }
  49. return comp;
  50. }
  51. private data: TipsData;
  52. init(data: TipsData) {
  53. this.data = data;
  54. if (data.title == null || data.title == "") {
  55. this.FindNode("lbl_title").active = false;
  56. } else {
  57. this.setText("lbl_title", data.title);
  58. }
  59. // this.setText("lbl_content", data.content);
  60. this.FindAs("lbl_content", RichText).string = data.content;
  61. if (data.forceConfirm) {
  62. this.FindNode("tap_close").active = false;
  63. this.FindNode("btn_close").active = false;
  64. this.FindNode("close_obj").active = false;
  65. }
  66. this.FindNode("btn_link").active = data.link != null;
  67. this.FindNode("btn_link").getComponent(Label).string = data.link;
  68. // this.setText("btn_link", data.link);
  69. }
  70. protected onBtnClick(name: string, event: Event, customEventData: any): void {
  71. if (name == "btn_confirm") {
  72. this.data?.onConfirm?.();
  73. this.closePage();
  74. }
  75. }
  76. protected onDestroy(): void {
  77. if (this.data?.singleName) {
  78. TipsLayer.single.delete(this.data.singleName);
  79. }
  80. }
  81. protected simpleOnBtnClick(name: string): void {
  82. if (name == "btn_link") {
  83. let url = this.FindNode("btn_link").getComponent(Label).string;
  84. window.open(url, "_blank");
  85. }
  86. }
  87. }