new_frame.ts 16 KB

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