edit_animation.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. @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. let self = this;
  122. let tweenDuration: number = frame.next_time;
  123. let origin_x = self.m_data.ani_frame_list[0].pos_x;
  124. let origin_y = self.m_data.ani_frame_list[0].pos_y
  125. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(frame.url_name)
  126. let n_pos:Vec3 = new Vec3(frame.pos_x-origin_x,frame.pos_y-origin_y)
  127. let _color = new Color()
  128. _color = _color.fromHEX(frame.color)
  129. class BindTarget{
  130. color : Color;
  131. opacity:number;
  132. size:Size;
  133. pos:Vec3;
  134. rotation:number;
  135. anchorPointX:number;
  136. anchorPointY:number;
  137. }
  138. let bindTarget : BindTarget = new BindTarget();
  139. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  140. bindTarget.rotation = rotation
  141. bindTarget.color = new Color().fromHEX(up_frame.color)
  142. bindTarget.opacity = up_frame.transparent;
  143. bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  144. bindTarget.pos = new Vec3(up_frame.pos_x-origin_x,up_frame.pos_y-origin_y)
  145. bindTarget.anchorPointX = up_frame.anchor_point_x==undefined?0.5:up_frame.anchor_point_x
  146. bindTarget.anchorPointY = up_frame.anchor_point_y==undefined?0.5:up_frame.anchor_point_y
  147. let color_opactiy_tw_size = tween(bindTarget)
  148. .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 }, {
  149. onUpdate(tar:BindTarget){
  150. self.ani_sf.getComponent(Sprite).color = tar.color;
  151. self.ani_sf.getComponent(UIOpacity).opacity = tar.opacity;
  152. self.ani_sf.getComponent(UITransform).setContentSize(tar.size)
  153. self.ani_sf.position = tar.pos
  154. self.ani_sf.angle = tar.rotation;
  155. self.ani_sf.getComponent(UITransform).setAnchorPoint(tar.anchorPointX, tar.anchorPointY)
  156. }
  157. }).call(()=>{
  158. call()
  159. });
  160. color_opactiy_tw_size.start();
  161. }
  162. onSlider(){
  163. this.ani_sf.scale = new Vec3( this.slider_scale.progress, this.slider_scale.progress)
  164. this.lab_slider.getComponent(Label).string = `显示缩放:${ this.slider_scale.progress.toFixed(3)}`
  165. }
  166. public show(data:att_ani_data){
  167. this.node.active = true;
  168. this.m_data = data;
  169. this.updateFrameList()
  170. }
  171. updateFrameList(){
  172. this.frame_content.removeAllChildren()
  173. for (let index = 0; index < this.m_data.ani_frame_list.length; index++) {
  174. const element = this.m_data.ani_frame_list[index];
  175. this.addFrameOnList(element,index)
  176. }
  177. this.updateFirstFrameStatus()
  178. }
  179. updateFirstFrameStatus(){
  180. if(this.m_data.ani_frame_list.length>0){
  181. let frame = this.m_data.ani_frame_list[0];
  182. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(frame.url_name)
  183. this.ani_sf.getComponent(UITransform).contentSize = new Size(frame.size_width,frame.size_height);
  184. }else{
  185. let att = Attributes.Singleton.get_cur_att_data()
  186. this.ani_sf.getComponent(Sprite).spriteFrame = control.res_map.get(att.src_name)
  187. this.ani_sf.getComponent(UITransform).contentSize = new Size(att.width,att.height);
  188. }
  189. }
  190. onClickDelete(item:frame_item){
  191. tools.show_dialog(`是否删除第${item.getIndex()}帧`,()=>{
  192. this.m_data.ani_frame_list.splice(item.getIndex(),1)
  193. item.node.removeFromParent()
  194. this.updateFrameList()
  195. })
  196. }
  197. onUpdateFrame(){
  198. this.updateFrameList()
  199. }
  200. onClickEdit(item:frame_item){
  201. // let up_frame = item.getIndex()>0?this.m_data.ani_frame_list[item.getIndex()-1]:null
  202. // this.new_frame.getComponent(new_frame).show_edit(item,up_frame);
  203. console.log('item.getIndex()=',item.getIndex())
  204. let up_frame = null;
  205. if(item.getIndex()>=0) {
  206. up_frame = this.m_data.ani_frame_list[item.getIndex()]
  207. }
  208. this.new_frame.getComponent(new_frame).show_edit(item,up_frame);
  209. }
  210. unSelectAllItem(){
  211. for (let index = 0; index < this.frame_content.children.length; index++) {
  212. const element = this.frame_content.children[index];
  213. element.getComponent(frame_item).unSelectStatus()
  214. }
  215. }
  216. updateFrameItemSelectStatus(i:number){
  217. for (let index = 0; index < this.frame_content.children.length; index++) {
  218. const element = this.frame_content.children[index];
  219. if(i===index){
  220. element.getComponent(frame_item).selectStatus()
  221. }else{
  222. element.getComponent(frame_item).unSelectStatus()
  223. }
  224. }
  225. }
  226. onClickFrame(item:frame_item){
  227. this.opt_frame.getComponent(opt_frame).show(item)
  228. }
  229. addFrameOnList(frame:ani_frame,index:number){
  230. let item = instantiate(this.item_prefab)
  231. item.parent = this.frame_content;
  232. item.getComponent(frame_item).initView(frame,this.onClickFrame.bind(this),index)
  233. }
  234. onCreateFrame(frame:ani_frame){
  235. console.log('this.m_data.ani_frame_list=',this.m_data.ani_frame_list)
  236. this.addFrameOnList(frame,this.m_data.ani_frame_list.length)
  237. this.m_data.ani_frame_list.push(frame)
  238. this.updateFirstFrameStatus()
  239. }
  240. }