new_frame.ts 14 KB

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