new_frame.ts 14 KB

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