UIPopup.ts 2.3 KB

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