UIPopup.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { _decorator, Tween, tween, UIOpacity, Node, Vec3, easing } from 'cc';
  2. import { UIView } from './UIView';
  3. import { UICloseReason } from './UILayer';
  4. import { waitForSeconds } from '@scripts/utils/Helper';
  5. const { ccclass, property } = _decorator;
  6. const TWEEM_TIMES = 0.3;
  7. @ccclass('UIPopup')
  8. export class UIPopup extends UIView {
  9. @property(UIOpacity)
  10. popupMask: UIOpacity;
  11. @property(Node)
  12. popupRoot: Node;
  13. private maskTween: Tween;
  14. private rootTween: Tween;
  15. internalCreate(): Promise<void> {
  16. if (this.popupMask) this.popupMask.opacity = 0;
  17. if (this.popupRoot) this.popupRoot.scale = Vec3.ZERO;
  18. return super.internalCreate();
  19. }
  20. async internalOpen(params: unknown): Promise<void> {
  21. this.openAnimation();
  22. await super.internalOpen(params);
  23. }
  24. protected openAnimation(): void | Promise<void> {
  25. if (this.popupMask) {
  26. this.stopTween(this.maskTween);
  27. this.popupMask.opacity = 0;
  28. this.maskTween = tween(this.popupMask)
  29. .to(TWEEM_TIMES * 0.6, { opacity: 255 })
  30. .start();
  31. }
  32. if (this.popupRoot) {
  33. this.stopTween(this.rootTween);
  34. this.popupRoot.scale = Vec3.ZERO;
  35. this.rootTween = tween(this.popupRoot)
  36. .to(TWEEM_TIMES, { scale: Vec3.ONE }, { easing: 'backOut' })
  37. .start();
  38. }
  39. }
  40. protected async onClose(_reason: UICloseReason) {
  41. if (!this.popupMask && !this.popupRoot) return;
  42. this.stopTween(this.maskTween);
  43. this.stopTween(this.rootTween);
  44. if (this.popupMask) {
  45. this.maskTween = tween(this.popupMask)
  46. .to(TWEEM_TIMES, { opacity: 0 })
  47. .start();
  48. }
  49. if (this.popupRoot) {
  50. this.rootTween = tween(this.popupRoot)
  51. .to(TWEEM_TIMES, { scale: Vec3.ZERO })
  52. .start();
  53. }
  54. await waitForSeconds(TWEEM_TIMES);
  55. }
  56. private stopTween(tweener: Tween) {
  57. if (tweener) tweener.stop();
  58. }
  59. }