edit_animation.ts 10 KB

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