BaseView.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * 视图基类
  3. */
  4. import ViewEvent from "./ViewEvent";
  5. import { ViewManager } from "../manager/ViewManager";
  6. import UIUtils, { UIContainer } from "../../util/UIUtils";
  7. import NotificationManager from "../manager/NotificationManager";
  8. import BaseMediator from "./BaseMediator";
  9. import SdkManager from "../../../resources/sdk/script/SdkManager";
  10. const { ccclass, property } = cc._decorator;
  11. @ccclass
  12. export class BaseView extends cc.Component {
  13. /** 当前视图的事件对象 */
  14. private __event__: ViewEvent;
  15. /** UI节点引用 */
  16. public ui: UIContainer;
  17. public __init__(): void {
  18. this.__event__ = new ViewEvent();
  19. this.ui = UIUtils.seekAllSubView(this.node);
  20. this.init();
  21. }
  22. /**
  23. * view 创建时会被调用,子类可以重写.
  24. */
  25. public init(): void {
  26. }
  27. /**
  28. * 发送UI事件
  29. * @param {string} event 事件名称
  30. * @param {Object} body 事件参数
  31. */
  32. public sendEvent(event: string, body?: any): void {
  33. this.__event__.emit(event, body)
  34. }
  35. /**
  36. * 绑定UI事件
  37. * @param {string} name 事件名称
  38. * @param {(body: any)=>void} cb 事件回调
  39. * @param {BaseMediator} target 事件回调绑定对象
  40. * @private 私有函数,不得调用。
  41. */
  42. public __bindEvent__(name: string, cb: (body: any) => void, target): void {
  43. this.__event__.on(name, cb, target);
  44. }
  45. /**
  46. * 关闭当前的界面
  47. */
  48. public closeView(): void {
  49. let bgname = "blackBg" + this.node.name;
  50. if (this.node.parent && this.node.parent.getChildByName(bgname))
  51. this.node.parent.getChildByName(bgname).destroy();
  52. ViewManager.getInstance().__closeView__(this);
  53. this.__onClose__();
  54. }
  55. /**
  56. * 关闭所有弹出的界面
  57. */
  58. public closeAllPopView(): void {
  59. ViewManager.getInstance().__closeAllPopView__();
  60. }
  61. private __onClose__(): void {
  62. this.__event__.destroy();
  63. this.onClose();
  64. this.node.destroy();
  65. }
  66. /**
  67. * 当界面被关闭时会被调用,子类可以重写该方法。
  68. * @override
  69. */
  70. public onClose(): void {
  71. }
  72. /**
  73. * 子类覆盖,返回UI的prefab路径
  74. * 路径第一个目录为子包名称如:aa/bb/ccc;aa为子包名 bb/ccc为子包内的prefab路径;
  75. * 如果放在resources的prefab;则路径去掉子包名/bb/ccc
  76. * @return {string}
  77. */
  78. public static path(): string {
  79. return "";
  80. }
  81. /**
  82. * 注册mediator
  83. */
  84. public registerMediator(mediator: { new(): BaseMediator }, view: BaseView,data?:any) {
  85. let viewMediator: BaseMediator = new mediator();
  86. viewMediator["__init__"]();
  87. viewMediator.view = view;
  88. viewMediator.view.__init__();
  89. viewMediator.init(data);
  90. }
  91. }