edit_animation.ts 11 KB

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