edit_animation.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Size, Slider, Sprite, Toggle, tween, Tween, UIOpacity, UITransform, Vec3 } from 'cc';
  2. import { ani_frame, att_ani_data } from '../../../data/data';
  3. import { frame_item } from './frame_item';
  4. import { new_frame } from './new_frame';
  5. import { Attributes } from '../Attributes';
  6. import { tools } from '../../tools';
  7. import { opt_frame } from './opt_frame';
  8. import { control } from '../control';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('edit_animation')
  11. export class edit_animation extends Component {
  12. @property(Node) ani_sf:Node = null;
  13. @property(Node) btn_new_ani_frame:Node = null;
  14. @property(Node) btn_run:Node = null;
  15. @property(Node) frame_content:Node = null;
  16. @property(Prefab) item_prefab:Prefab = null;
  17. @property(Node) new_frame:Node = null;
  18. @property(Node) btn_close:Node = null;
  19. @property(Slider) slider_scale:Slider = null;
  20. @property(Node) lab_slider:Node = null;
  21. @property(Node) opt_frame:Node = null;
  22. @property(Node) lab_run_status:Node = null;
  23. @property(Node) toggle_loop:Node = null;
  24. @property(Node) toggle_move:Node = null;
  25. private m_data:att_ani_data = null;
  26. private play_status:boolean = false;
  27. public initView(){
  28. this.new_frame.getComponent(new_frame).initView(this.onCreateFrame.bind(this),this.onUpdateFrame.bind(this))
  29. this.opt_frame.getComponent(opt_frame).initView(this.onClickDelete.bind(this),this.onClickEdit.bind(this))
  30. this.btn_new_ani_frame.on(Node.EventType.TOUCH_END,()=>{
  31. if(!this.play_status){
  32. let up_frame = this.m_data.ani_frame_list.length>0?this.m_data.ani_frame_list[this.m_data.ani_frame_list.length-1]:null
  33. this.new_frame.getComponent(new_frame).show(up_frame);
  34. }
  35. })
  36. if(this.m_data.isMove==undefined||this.m_data.isMove == null){
  37. this.m_data.isMove = true;
  38. }
  39. this.toggle_move.on('toggle',()=>{
  40. this.m_data.isMove = this.toggle_move.getComponent(Toggle).isChecked;
  41. })
  42. this.toggle_loop.on('toggle',()=>{
  43. Tween.stopAll()
  44. this.lab_run_status.getComponent(Label).string = "运行"
  45. this.play_status = false;
  46. this.unSelectAllItem()
  47. this.m_data.isLoop = this.toggle_loop.getComponent(Toggle).isChecked;
  48. })
  49. this.toggle_loop.getComponent(Toggle).isChecked = this.m_data.isLoop
  50. this.toggle_move.getComponent(Toggle).isChecked = this.m_data.isMove
  51. this.btn_close.on(Node.EventType.TOUCH_END,()=>{
  52. if(!this.play_status){
  53. this.node.active = false;
  54. }
  55. })
  56. this.btn_run.on(Node.EventType.TOUCH_END,()=>{
  57. if(this.m_data.ani_frame_list.length<2){
  58. return tools.showToast("动画不能低于2帧")
  59. }
  60. if(!this.play_status){
  61. this.play_status = true;
  62. this.lab_run_status.getComponent(Label).string = "停止"
  63. this.runAnimation()
  64. }else{
  65. Tween.stopAll()
  66. this.lab_run_status.getComponent(Label).string = "运行"
  67. this.play_status = false;
  68. this.unSelectAllItem()
  69. }
  70. })
  71. this.onSlider()
  72. }
  73. runAnimation(){
  74. Tween.stopAll()
  75. let getFrameData = (index)=>{
  76. this.updateFrameItemSelectStatus(index)
  77. let cur_frame:ani_frame =this.m_data.ani_frame_list[index];
  78. let up_frame:ani_frame = index>0?this.m_data.ani_frame_list[index-1]:this.m_data.ani_frame_list[0];
  79. return {"cur_frame":cur_frame,"up_frame":up_frame}
  80. }
  81. let cur_index = 0;
  82. let data = getFrameData(cur_index);
  83. let call_back = ()=>{
  84. cur_index++;
  85. if(cur_index>=this.m_data.ani_frame_list.length){
  86. if(this.m_data.isLoop){
  87. cur_index = 0;
  88. }else{
  89. this.play_status = false;
  90. this.lab_run_status.getComponent(Label).string = "运行"
  91. this.unSelectAllItem()
  92. return;
  93. }
  94. }
  95. let data = getFrameData(cur_index);
  96. this.runFrame(data.cur_frame,data.up_frame,call_back)
  97. }
  98. this.runFrame(data.cur_frame,data.up_frame,call_back)
  99. }
  100. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  101. let self = this;
  102. let tweenDuration: number = frame.next_time;
  103. let origin_x = self.m_data.ani_frame_list[0].pos_x;
  104. let origin_y = self.m_data.ani_frame_list[0].pos_y
  105. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(frame.url_name)
  106. let n_pos:Vec3 = new Vec3(frame.pos_x-origin_x,frame.pos_y-origin_y)
  107. let _color = new Color()
  108. _color = _color.fromHEX(frame.color)
  109. class BindTarget{
  110. color : Color;
  111. opacity:number;
  112. size:Size;
  113. pos:Vec3;
  114. rotation:number;
  115. }
  116. let bindTarget : BindTarget = new BindTarget();
  117. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  118. bindTarget.rotation = rotation
  119. bindTarget.color = new Color().fromHEX(up_frame.color)
  120. bindTarget.opacity = up_frame.transparent;
  121. bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  122. bindTarget.pos = new Vec3(up_frame.pos_x-origin_x,up_frame.pos_y-origin_y)
  123. let color_opactiy_tw_size = tween(bindTarget)
  124. .to( tweenDuration, { pos:n_pos,color: _color,opacity:frame.transparent,size:new Size(frame.size_width,frame.size_height),rotation:frame.rotation }, {
  125. onUpdate(tar:BindTarget){
  126. self.ani_sf.getComponent(Sprite).color = tar.color;
  127. self.ani_sf.getComponent(UIOpacity).opacity = tar.opacity;
  128. self.ani_sf.getComponent(UITransform).setContentSize(tar.size)
  129. self.ani_sf.position = tar.pos
  130. self.ani_sf.angle = tar.rotation;
  131. }
  132. }).call(()=>{
  133. call()
  134. });
  135. color_opactiy_tw_size.start();
  136. }
  137. onSlider(){
  138. this.ani_sf.scale = new Vec3( this.slider_scale.progress, this.slider_scale.progress)
  139. this.lab_slider.getComponent(Label).string = `显示缩放:${ this.slider_scale.progress.toFixed(3)}`
  140. }
  141. protected start(): void {
  142. this.initView()
  143. }
  144. public show(data:att_ani_data){
  145. this.node.active = true;
  146. this.m_data = data;
  147. this.updateFrameList()
  148. }
  149. updateFrameList(){
  150. this.frame_content.removeAllChildren()
  151. for (let index = 0; index < this.m_data.ani_frame_list.length; index++) {
  152. const element = this.m_data.ani_frame_list[index];
  153. this.addFrameOnList(element,index)
  154. }
  155. this.updateFirstFrameStatus()
  156. }
  157. updateFirstFrameStatus(){
  158. if(this.m_data.ani_frame_list.length>0){
  159. let frame = this.m_data.ani_frame_list[0];
  160. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(frame.url_name)
  161. this.ani_sf.getComponent(UITransform).contentSize = new Size(frame.size_width,frame.size_height);
  162. }else{
  163. let att = Attributes.Singleton.get_cur_att_data()
  164. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(att.src_name)
  165. this.ani_sf.getComponent(UITransform).contentSize = new Size(att.width,att.height);
  166. }
  167. }
  168. onClickDelete(item:frame_item){
  169. tools.show_dialog(`是否删除第${item.getIndex()}帧`,()=>{
  170. this.m_data.ani_frame_list.splice(item.getIndex(),1)
  171. item.node.removeFromParent()
  172. this.updateFrameList()
  173. })
  174. }
  175. onUpdateFrame(){
  176. this.updateFrameList()
  177. }
  178. onClickEdit(item:frame_item){
  179. let up_frame = item.getIndex()>0?this.m_data.ani_frame_list[item.getIndex()-1]:null
  180. this.new_frame.getComponent(new_frame).show_edit(item,up_frame);
  181. }
  182. unSelectAllItem(){
  183. for (let index = 0; index < this.frame_content.children.length; index++) {
  184. const element = this.frame_content.children[index];
  185. element.getComponent(frame_item).unSelectStatus()
  186. }
  187. }
  188. updateFrameItemSelectStatus(i:number){
  189. for (let index = 0; index < this.frame_content.children.length; index++) {
  190. const element = this.frame_content.children[index];
  191. if(i===index){
  192. element.getComponent(frame_item).selectStatus()
  193. }else{
  194. element.getComponent(frame_item).unSelectStatus()
  195. }
  196. }
  197. }
  198. onClickFrame(item:frame_item){
  199. this.opt_frame.getComponent(opt_frame).show(item)
  200. }
  201. addFrameOnList(frame:ani_frame,index:number){
  202. let item = instantiate(this.item_prefab)
  203. item.parent = this.frame_content;
  204. item.getComponent(frame_item).initView(frame,this.onClickFrame.bind(this),index)
  205. }
  206. onCreateFrame(frame:ani_frame){
  207. this.addFrameOnList(frame,this.m_data.ani_frame_list.length)
  208. this.m_data.ani_frame_list.push(frame)
  209. this.updateFirstFrameStatus()
  210. }
  211. }