EffectOpenAndClose.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import UIBG from "../UIBG";
  2. const { ccclass, property, executeInEditMode, playOnFocus } = cc._decorator;
  3. /** 效果类型 */
  4. enum EffectType {
  5. none = 0, //无
  6. scale = 1, //缩放
  7. scaleToPoint = 2, //缩放到某点
  8. centerUnfolding = 3, //从中部展开
  9. rightIn = 4, //从右侧进入
  10. bottomIn = 5, //从底部进入
  11. }
  12. /**
  13. * 界面打开 和 关闭效果
  14. * 组件请挂载到对应ui界面上
  15. * @author 薛鸿潇
  16. */
  17. @ccclass
  18. @executeInEditMode()
  19. @playOnFocus()
  20. export default class EffectOpenAndClose extends cc.Component {
  21. @property({ type: cc.Node, displayName: '动画节点' })
  22. private effect_par: cc.Node = null
  23. @property({ type: cc.Enum(EffectType), displayName: '打开动画节点效果', visible() { return this.effect_par } })
  24. private open_effect: EffectType = EffectType.scale;
  25. @property({ displayName: '是否打开关闭动画' })
  26. public isOpenCloseEffect: boolean = true;
  27. @property({ type: cc.Enum(EffectType), displayName: '关闭动画节点效果', visible() { return this.effect_par && this.isOpenCloseEffect } })
  28. private close_effect: EffectType = EffectType.scale;
  29. @property({ displayName: '关闭节点缩小到此节点坐标', visible() { return this.close_effect == EffectType.scaleToPoint } })
  30. private close_toPoint: string = "";
  31. @property({ displayName: '做关闭动画之前是否关闭黑背景' })
  32. private close_uibg: boolean = false;
  33. @property({ displayName: '是否延迟一帧做动画' })
  34. private isDelayOneFrame: boolean = false;
  35. private _play_open: boolean = false;
  36. @property({ displayName: "预览开" })
  37. get play_open(): boolean {
  38. if (this._play_open) {
  39. this.showEffect();
  40. this._play_open = false;
  41. }
  42. return this._play_open;
  43. }
  44. set play_open(v: boolean) {
  45. this._play_open = v;
  46. }
  47. private _play_close: boolean = false;
  48. @property({ displayName: "预览关" })
  49. get play_close(): boolean {
  50. if (this._play_close) {
  51. this.effect_par.scale = 1;
  52. this.hideEffect();
  53. this._play_close = false;
  54. }
  55. return this._play_close;
  56. }
  57. set play_close(v: boolean) {
  58. this._play_close = v;
  59. }
  60. onLoad() {
  61. // this.node.on('ui-close', this.hideEffect, this);// 界面关闭事件
  62. }
  63. start() {
  64. if (CC_EDITOR) return;
  65. if (this.isDelayOneFrame) {
  66. if(EffectType.scale === this.open_effect)
  67. {
  68. this.effect_par.opacity = 0;
  69. }
  70. cc.tween(this.effect_par).delay(0.16).call(() => {
  71. this.showEffect();
  72. }).start();
  73. } else {
  74. this.showEffect();
  75. }
  76. }
  77. /**
  78. * 界面打开效果
  79. */
  80. private showEffect() {
  81. if (this.effect_par) {
  82. switch (this.open_effect) {
  83. case EffectType.none:
  84. break;
  85. case EffectType.scale:
  86. let scale = this.effect_par.scale;
  87. this.effect_par.opacity = 255;
  88. mk.tween.scale(this.effect_par, 0.3, undefined, scale, undefined, 'backOut');
  89. break;
  90. case EffectType.centerUnfolding:
  91. mk.tween.scaleX(this.effect_par, 0.3, undefined, undefined, undefined, 'backOut');
  92. break;
  93. case EffectType.rightIn:
  94. this.effect_par.active = true;
  95. this.effect_par.x = 1000;
  96. cc.tween(this.effect_par)
  97. .to(0.3, { x: 0 }, cc.easeOut(3))
  98. .start()
  99. break;
  100. case EffectType.bottomIn:
  101. this.effect_par.active = true;
  102. this.effect_par.x = 0;
  103. this.effect_par.y = this.effect_par.y - cc.winSize.height;
  104. //let toY = this.effect_par.y + this.effect_par.height;
  105. let toY = 0;
  106. cc.tween(this.effect_par)
  107. .by(0.15, { y: cc.winSize.height }, cc.easeOut(3))
  108. .start()
  109. break;
  110. }
  111. }
  112. }
  113. /**
  114. * 关闭打开效果
  115. * @param cb 结束回调
  116. */
  117. public hideEffect(cb?: Function) {
  118. if (!this.isOpenCloseEffect)
  119. return;
  120. if (this.effect_par) {
  121. if (this.close_uibg) {
  122. let graph = this.node.getComponentInChildren(cc.Graphics);
  123. if (graph) {
  124. graph.enabled = false;
  125. }
  126. }
  127. switch (this.close_effect) {
  128. case EffectType.none:
  129. cb && cb();
  130. break;
  131. case EffectType.scale:
  132. mk.tween.scale(this.effect_par, 0.15, 1, 0, cb);
  133. break;
  134. case EffectType.scaleToPoint:
  135. if (this.close_toPoint == "") {
  136. console.error('需要配置缩小到终点节点路径')
  137. } else {
  138. let path = this.close_toPoint;
  139. let node = cc.find(path);
  140. if (node) {
  141. let world_pos = node.parent.convertToWorldSpaceAR(node.getPosition());
  142. let node_pos = this.effect_par.parent.convertToNodeSpaceAR(world_pos);
  143. mk.tween.scaleParallelMove(this.effect_par, 0.3, 1, 0, node_pos, cb)
  144. // cc.tween(this.effect_par)
  145. // .to(0.3, { scale: 0, position: new cc.Vec3(node_pos.x, node_pos.y, 0) })
  146. // .call(() => {
  147. // cb && cb();
  148. // })
  149. // .start()
  150. } else {
  151. cb && cb();
  152. cc.error('关闭时动画节点未找到,请检查!!!!!')
  153. }
  154. }
  155. break;
  156. case EffectType.rightIn:
  157. mk.tween.move(this.effect_par, 0.3, this.effect_par.getPosition(), new cc.Vec2(1000, this.effect_par.y), () => {
  158. this.effect_par.active = false;
  159. cb && cb();
  160. }, 'sineOut')
  161. // cc.tween(this.effect_par)
  162. // .to(0.3, { x: 1000 }, cc.easeOut(3))
  163. // .call(() => {
  164. // this.effect_par.active = false;
  165. // cb && cb();
  166. // })
  167. // .start()
  168. break;
  169. case EffectType.bottomIn:
  170. let toY = -cc.winSize.height;
  171. cc.tween(this.effect_par)
  172. .to(0.3, { y: toY }, cc.easeOut(3))
  173. .call(() => {
  174. // this.effect_par.active = false;
  175. cb && cb();
  176. })
  177. .start()
  178. break;
  179. }
  180. } else {
  181. cb && cb();
  182. }
  183. }
  184. }