UIBase.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { _decorator, Button, Component, Enum, Node, Widget } from 'cc';
  2. import { UIType } from '../enum/UIType';
  3. import { EventMgr } from '../manager/EventMgr';
  4. import { EventType } from '../enum/EventType';
  5. import { DtoUI } from '../dto/DtoUI';
  6. const { ccclass, property, requireComponent } = _decorator;
  7. @requireComponent(Widget)
  8. export class UIBase extends Component {
  9. @property({ "type": Enum(UIType) })
  10. public uiType: UIType = UIType.Common
  11. public onOpen(data?: unknown): void { }
  12. public onClose(data?: unknown): void { }
  13. public open(uiName: string, data?: unknown, dtoUI?: DtoUI): void {
  14. EventMgr.emit(EventType.OpenUI, uiName, data, dtoUI)
  15. }
  16. public delayOpen(time: number, uiName: string, data?: unknown, dtoUI?: DtoUI): void {
  17. this.scheduleOnce(this.open.bind(this, uiName, data, dtoUI), time)
  18. }
  19. public close(uiName?: string, data?: unknown): void {
  20. if (!uiName) uiName = this.node.name
  21. EventMgr.emit(EventType.CloseUI, uiName, data)
  22. }
  23. public delayClose(time: number, uiName?: string, data?: unknown): void {
  24. this.scheduleOnce(this.close.bind(this, uiName, data), time)
  25. }
  26. public bindClick(btn: Button, node: Node, comp: string, handler: string, customData?: string): void {
  27. var clickEventHandler = new Component.EventHandler();
  28. clickEventHandler.target = node;
  29. clickEventHandler.component = comp;
  30. clickEventHandler.handler = handler;
  31. clickEventHandler.customEventData = customData;
  32. btn.clickEvents = [clickEventHandler]
  33. }
  34. public unbindClick(btn: Button): void {
  35. btn.clickEvents = []
  36. }
  37. }