ui.pop-up.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { _decorator, Node, tween, Tween, v3 } from "cc";
  2. import { BaseUI } from "./ui.base";
  3. const {ccclass, property} = _decorator;
  4. @ccclass
  5. export abstract class BasePopUp<POPUP, RETURN> extends BaseUI {
  6. /** 弹窗是否被关闭 */
  7. public get is_close(){
  8. return this.npromise==undefined;
  9. }
  10. private npromise: mtec.NudityPromise<RETURN>;
  11. private closed: boolean;
  12. /** 生成一个promise实例 */
  13. protected promise(){
  14. this.closed = false;
  15. this.npromise = new mtec.NudityPromise();
  16. return this.npromise.promise;
  17. }
  18. /**
  19. * 关闭弹窗
  20. * @param param 回复数据,如果不传,则会被当作弹窗操作失败传入默认参数
  21. */
  22. protected close(param?: RETURN){
  23. if(this.closed) return undefined;
  24. else this.closed = true;
  25. // 调用回调函数
  26. this.onClose(param);
  27. // 回复promise
  28. if(param!==undefined) this.npromise.resolve(param);
  29. else this.npromise.reject(this.default_return);
  30. // 清空promise相关的响应
  31. this.npromise = undefined;
  32. }
  33. /** 从外部关闭弹窗 */
  34. public closeFromOutside(){
  35. this.close(this.default_return);
  36. }
  37. /** 默认返回值 */
  38. protected abstract default_return: RETURN;
  39. /** 管理节点 */
  40. protected pop: POPUP;
  41. /**
  42. * 显示弹窗并返回promise
  43. * @param args
  44. */
  45. public show(pop: POPUP, ...args: Parameters<typeof this.onShow>){
  46. this.pop = pop;
  47. this.node.active = true;
  48. this.call_func(this.onShow, ...args);
  49. return this.promise();
  50. }
  51. /**
  52. * 显示时调用
  53. * @param param 可选参数
  54. */
  55. public abstract onShow(...param: any[]): void;
  56. /** 关闭弹窗时的回调 */
  57. protected onClose(param: RETURN): void{};
  58. /**
  59. * 果冻效果(入场)
  60. * @param node
  61. */
  62. protected static jelly_enter(node: Node, duration?: number){
  63. let np = new mtec.NudityPromise<Node>();
  64. Tween.stopAllByTarget(node);
  65. duration = duration ?? 0.5;
  66. tween(node)
  67. .set({scale: v3(0.8, 0.8, 1)})
  68. .to(duration, {scale: v3(1, 1, 1)}, {easing: 'elasticOut'})
  69. .set({scale: v3(1, 1, 1)})
  70. .call(()=>np.resolve(node))
  71. .start();
  72. return np.promise;
  73. }
  74. /**
  75. * 果冻效果(离场)
  76. * @param node
  77. * @param duration
  78. */
  79. protected static jelly_leave(node: Node, duration?: number){
  80. let np = new mtec.NudityPromise<Node>();
  81. Tween.stopAllByTarget(node);
  82. duration = duration ?? 0.25;
  83. tween(node)
  84. .to(duration, {scale: v3(0.8, 0.8, 1)}, {easing: "backIn"})
  85. .set({scale: v3(0.8, 0.8, 1)})
  86. .call(()=>np.resolve(node))
  87. .start();
  88. return np.promise;
  89. }
  90. }