new_frame.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import { _decorator, Component, Director, director, EditBox, Label, Node, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { tools } from '../../tools';
  3. import { ani_frame, bag_item_data } from '../../../data/data';
  4. import { Attributes } from '../Attributes';
  5. import { frame_item } from './frame_item';
  6. import { control } from '../control';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('new_frame')
  9. export class new_frame extends Component {
  10. @property(Node) btn_sure:Node = null;
  11. @property(Node) btn_cancle:Node = null;
  12. @property(Node) next_time:Node = null;
  13. @property(Node) pos_x:Node = null;
  14. @property(Node) pos_y:Node = null;
  15. @property(Node) size_width:Node = null;
  16. @property(Node) size_height:Node = null;
  17. @property(Node) color:Node = null;
  18. @property(Node) transparent:Node = null;
  19. @property(Node) btn_select_res:Node = null;
  20. @property(Node) lab_origin_pos:Node = null;
  21. @property(Node) lab_origin_size:Node = null;
  22. @property(Node) btn_origin_res:Node = null;
  23. @property(Node) rotation:Node = null;
  24. @property(Node) anchor_point_x:Node = null;
  25. @property(Node) anchor_point_y:Node = null;
  26. @property(Node) btn_rotation_center:Node = null;
  27. @property(Node) hide_spr:Node = null;
  28. private m_up_frame:ani_frame = null;
  29. private m_create_call = null;
  30. private m_edit_call = null;
  31. private m_data:ani_frame = new ani_frame();
  32. private edit_status:boolean = false;
  33. private m_item:frame_item = null;
  34. public initView(create_call,edit_call){
  35. this.m_edit_call = edit_call;
  36. this.m_create_call = create_call;
  37. this.btn_cancle.on(Node.EventType.TOUCH_END,()=>{
  38. this.close()
  39. })
  40. this.btn_sure.on(Node.EventType.TOUCH_END,()=>{
  41. if(this.edit_status){
  42. this.edit_frame()
  43. }else{
  44. this.create_frame()
  45. }
  46. this.close()
  47. })
  48. this.btn_select_res.on(Node.EventType.TOUCH_END,()=>{
  49. tools.select_res_list((data:bag_item_data)=>{
  50. this.m_data.url = data.url;
  51. this.m_data.url_name = data.name;
  52. // this.btn_select_res.getComponent(Sprite).spriteFrame = control.res_map.get(this.m_data.url_name)
  53. // this.hide_spr.getComponent(Sprite).spriteFrame = this.btn_select_res.getComponent(Sprite).spriteFrame
  54. this.hide_spr.getComponent(Sprite).spriteFrame = control.res_map.get(this.m_data.url_name)
  55. this.setupInitAnchorPointXY()
  56. this.setupSprRatationAction()
  57. })
  58. })
  59. this.btn_origin_res.on(Node.EventType.TOUCH_END,()=>{
  60. let size = this.hide_spr.getComponent(UITransform).contentSize;
  61. this.m_data.size_width = size.width;
  62. this.m_data.size_height = size.height;
  63. this.size_width.getComponent(EditBox).string = this.m_data.size_width.toString()
  64. this.size_height.getComponent(EditBox).string = this.m_data.size_height.toString()
  65. })
  66. this.btn_rotation_center.on(Node.EventType.TOUCH_END, ()=> {
  67. this.setupInitAnchorPointXY()
  68. },this)
  69. this.next_time.on('editing-did-ended', this.change, this);
  70. this.pos_x.on('editing-did-ended', this.change, this);
  71. this.pos_y.on('editing-did-ended', this.change, this);
  72. this.size_height.on('editing-did-ended', this.change, this);
  73. this.size_width.on('editing-did-ended', this.change, this);
  74. this.color.on('editing-did-ended', this.change, this);
  75. this.transparent.on('editing-did-ended', this.change, this);
  76. this.rotation.on('editing-did-ended', this.change, this);
  77. this.anchor_point_x.on('editing-did-ended', this.change, this);
  78. this.anchor_point_y.on('editing-did-ended', this.change, this);
  79. }
  80. close(){
  81. this.node.active = false;
  82. }
  83. edit_frame(){
  84. if(this.m_item!=null){
  85. this.m_item.setData(this.m_data)
  86. }
  87. if(this.m_edit_call!=null){
  88. if(this.checkAnchorXY()) {
  89. this.m_edit_call()
  90. }
  91. }
  92. }
  93. create_frame(){
  94. if(this.m_create_call!=null){
  95. this.updateData()
  96. let ani_frame_data = new ani_frame;
  97. ani_frame_data.next_time = this.m_data.next_time;
  98. ani_frame_data.pos_x = this.m_data.pos_x;
  99. ani_frame_data.pos_y = this.m_data.pos_y;
  100. ani_frame_data.size_width = this.m_data.size_width;
  101. ani_frame_data.size_height = this.m_data.size_height;
  102. ani_frame_data.color = this.m_data.color;
  103. ani_frame_data.transparent = this.m_data.transparent;
  104. ani_frame_data.url = this.m_data.url;
  105. ani_frame_data.url_name = this.m_data.url_name;
  106. ani_frame_data.rotation = this.m_data.rotation;
  107. // console.log('x=',this.m_data.rotation_x, 'y=',this.m_data.rotation_y)
  108. ani_frame_data.anchor_point_x = this.m_data.anchor_point_x;
  109. ani_frame_data.anchor_point_y = this.m_data.anchor_point_y;
  110. console.log("ani_frame_data//",ani_frame_data)
  111. if(this.checkAnchorXY()) {
  112. this.m_create_call(ani_frame_data)
  113. }
  114. }
  115. }
  116. public show(up_frame:ani_frame=null){
  117. console.log("show show show show show")
  118. this.edit_status = false;
  119. this.m_up_frame = up_frame;
  120. this.node.active = true;
  121. let att = Attributes.Singleton.get_cur_att_data();
  122. let url_name = att.src_name
  123. let url = att.src;
  124. if(this.m_up_frame!=null){
  125. if(this.m_up_frame.url.length>0){
  126. url_name = this.m_up_frame.url_name
  127. url = this.m_up_frame.url;
  128. }
  129. }
  130. // if(url_name.length>0){
  131. // this.btn_select_res.getComponent(Sprite).spriteFrame = control.res_map.get(url_name)
  132. // }
  133. // this.hide_spr.getComponent(Sprite).spriteFrame = this.btn_select_res.getComponent(Sprite).spriteFrame
  134. if(url_name.length>0){
  135. this.hide_spr.getComponent(Sprite).spriteFrame = control.res_map.get(url_name)
  136. }
  137. let x = up_frame?up_frame.pos_x: Math.floor(att.x);
  138. let y = up_frame?up_frame.pos_y: Math.floor(att.y);
  139. this.m_data.url = url;
  140. this.m_data.pos_x =x;
  141. this.m_data.pos_y = y;
  142. this.m_data.size_width = up_frame?up_frame.size_width:att.width;
  143. this.m_data.size_height = up_frame?up_frame.size_height:att.height;
  144. this.m_data.url_name = up_frame?up_frame.url_name:att.src_name;
  145. if(this.m_data.rotation==undefined||this.m_data.rotation==null){
  146. this.m_data.rotation=0;
  147. }
  148. if(up_frame){
  149. if(up_frame.rotation==undefined||up_frame==null){
  150. up_frame.rotation = 0
  151. }
  152. }
  153. this.m_data.rotation = up_frame?up_frame.rotation:0;
  154. this.lab_origin_pos.getComponent(Label).string = `(x:${Math.floor(att.x)},y:${Math.floor(att.y)})`
  155. this.lab_origin_size.getComponent(Label).string = `(w:${att.width},h:${att.height})`
  156. this.initAtt()
  157. }
  158. public show_edit(item:frame_item,up_frame:ani_frame=null){
  159. console.log("show_edit show_edit show_edit show_edit show_edit")
  160. this.m_up_frame = up_frame;
  161. this.node.active = true;
  162. this.m_data = item.getData();
  163. this.m_item = item;
  164. let url = this.m_data.url
  165. if(this.m_data.url.length>0){
  166. url = this.m_data.url;
  167. }else{
  168. if(this.m_up_frame!=null){
  169. if(this.m_up_frame.url.length>0){
  170. url = this.m_up_frame.url
  171. }
  172. }
  173. }
  174. this.edit_status = true;
  175. // this.btn_select_res.getComponent(Sprite).spriteFrame = control.res_map.get(this.m_data.url_name)
  176. // this.hide_spr.getComponent(Sprite).spriteFrame = this.btn_select_res.getComponent(Sprite).spriteFrame
  177. this.hide_spr.getComponent(Sprite).spriteFrame = control.res_map.get(this.m_data.url_name)
  178. this.initAtt()
  179. this.setupSprRatationAction()
  180. }
  181. initAtt(){
  182. this.next_time.getComponent(EditBox).string = this.m_data.next_time.toString()
  183. this.pos_x.getComponent(EditBox).string = this.m_data.pos_x.toString()
  184. this.pos_y.getComponent(EditBox).string = this.m_data.pos_y.toString()
  185. this.size_width.getComponent(EditBox).string = this.m_data.size_width.toString()
  186. this.size_height.getComponent(EditBox).string = this.m_data.size_height.toString()
  187. this.color.getComponent(EditBox).string = this.m_data.color.toString()
  188. this.transparent.getComponent(EditBox).string = this.m_data.transparent.toString()
  189. if(this.m_data.rotation==undefined){
  190. this.m_data.rotation = 0;
  191. }
  192. if(this.m_data.anchor_point_x==undefined) {
  193. this.m_data.anchor_point_x = 0.5;
  194. }
  195. if(this.m_data.anchor_point_y==undefined) {
  196. this.m_data.anchor_point_y = 0.5;
  197. }
  198. this.rotation.getComponent(EditBox).string = this.m_data.rotation.toString()
  199. if(this.edit_status) {
  200. this.anchor_point_x.getComponent(EditBox).string = this.m_data.anchor_point_x.toString()
  201. this.anchor_point_y.getComponent(EditBox).string = this.m_data.anchor_point_y.toString()
  202. } else {
  203. this.setupInitAnchorPointXY()
  204. }
  205. }
  206. change(){
  207. this.updateData()
  208. this.setupSprRatationAction()
  209. }
  210. updateData(){
  211. this.m_data.rotation = parseFloat(this.rotation.getComponent(EditBox).string)
  212. this.m_data.anchor_point_x = parseFloat(this.anchor_point_x.getComponent(EditBox).string)
  213. this.m_data.anchor_point_y = parseFloat(this.anchor_point_y.getComponent(EditBox).string)
  214. this.m_data.next_time = parseFloat(this.next_time.getComponent(EditBox).string)
  215. this.m_data.pos_x = parseInt(this.pos_x.getComponent(EditBox).string)
  216. this.m_data.pos_y = parseInt(this.pos_y.getComponent(EditBox).string)
  217. this.m_data.size_width = parseInt(this.size_width.getComponent(EditBox).string)
  218. this.m_data.size_height = parseInt(this.size_height.getComponent(EditBox).string)
  219. this.m_data.color = this.color.getComponent(EditBox).string
  220. this.m_data.transparent = parseInt(this.transparent.getComponent(EditBox).string)
  221. }
  222. checkAnchorXY():boolean {
  223. if(this.m_data.anchor_point_x<0||this.m_data.anchor_point_x>1) {
  224. tools.showToast('锚点X应在0-1之间')
  225. return false
  226. }
  227. if(this.m_data.anchor_point_y<0||this.m_data.anchor_point_y>1) {
  228. tools.showToast('锚点Y应在0-1之间')
  229. return false
  230. }
  231. return true
  232. }
  233. setupInitAnchorPointXY() {
  234. this.m_data.anchor_point_x = 0.5
  235. this.m_data.anchor_point_y = 0.5
  236. this.anchor_point_x.getComponent(EditBox).string = this.m_data.anchor_point_x.toString()
  237. this.anchor_point_y.getComponent(EditBox).string = this.m_data.anchor_point_y.toString()
  238. this.hide_spr.getComponent(UITransform).setAnchorPoint(this.m_data.anchor_point_x, this.m_data.anchor_point_y)
  239. }
  240. setupSprRatationAction() {
  241. this.hide_spr.angle = this.m_data.rotation
  242. this.hide_spr.getComponent(UITransform).setAnchorPoint(this.m_data.anchor_point_x, this.m_data.anchor_point_y)
  243. }
  244. }