new_frame.ts 15 KB

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