UIPopup.ts 2.3 KB

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