new_frame.ts 13 KB

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