res_preview.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { _decorator, Component, EventTouch, Label, Node, Size, Sprite, SpriteFrame, UITransform, Vec2, Vec3 } from 'cc';
  2. import { ani_frame } from '../../../data/data';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('res_preview')
  5. export class res_preview extends Component {
  6. @property(Node) img_res:Node = null;
  7. @property(Node) img_point:Node = null;
  8. @property(Node) btn_cancel:Node = null;
  9. @property(Node) btn_sure:Node = null;
  10. @property(Node) lab_show:Node = null;
  11. private m_data:ani_frame = null;
  12. private m_anchor_point:Vec2 = new Vec2(0.5,0.5)
  13. private m_callback:Function = null;
  14. start() {
  15. this.btn_cancel.on(Node.EventType.TOUCH_END, ()=> {
  16. this.close()
  17. },this)
  18. this.btn_sure.on(Node.EventType.TOUCH_END, ()=> {
  19. this.close(true)
  20. },this)
  21. this.img_res.on(Node.EventType.TOUCH_END, (et:EventTouch)=> {
  22. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  23. let n_p = this.img_res.getComponent(UITransform).convertToNodeSpaceAR(p)
  24. this.img_point.position = new Vec3(n_p.x, n_p.y)
  25. let img_contentSize = this.img_res.getComponent(UITransform).contentSize
  26. let anchor_point_x = (img_contentSize.width / 2 + n_p.x) / img_contentSize.width
  27. let anchor_point_y = (img_contentSize.height / 2 + n_p.y) / img_contentSize.height
  28. this.m_anchor_point = new Vec2(anchor_point_x, anchor_point_y)
  29. // console.log('当前锚点=',this.m_anchor_point)
  30. this.setupLabShow()
  31. },this)
  32. }
  33. initView(imgSf:SpriteFrame, data:ani_frame, callback:Function) {
  34. this.m_data = data
  35. this.m_callback = callback
  36. this.m_anchor_point = new Vec2(data.anchor_point_x, data.anchor_point_y)
  37. this.img_res.getComponent(Sprite).spriteFrame = imgSf
  38. this.img_res.getComponent(UITransform).contentSize = new Size(data.size_width, data.size_height)
  39. this.img_res.getComponent(UITransform).setAnchorPoint(0.5, 0.5) //进入就原始值
  40. // 计算小圆点坐标
  41. let img_contentSize = this.img_res.getComponent(UITransform).contentSize
  42. let n_p_x = (img_contentSize.width * this.m_anchor_point.x) - (img_contentSize.width / 2)
  43. let n_p_y = (img_contentSize.height * this.m_anchor_point.y) - (img_contentSize.height / 2)
  44. this.img_point.position = new Vec3(n_p_x, n_p_y)
  45. this.setupLabShow()
  46. }
  47. private setupLabShow() {
  48. let x = parseFloat(this.m_anchor_point.x.toFixed(2))
  49. let y = parseFloat(this.m_anchor_point.y.toFixed(2))
  50. this.lab_show.getComponent(Label).string = '锚点x: ' + x + ' ' + '锚点y: ' + y
  51. }
  52. close(isSure:boolean = false) {
  53. if(isSure) {
  54. this.m_data.anchor_point_x = parseFloat(this.m_anchor_point.x.toFixed(2))
  55. this.m_data.anchor_point_y = parseFloat(this.m_anchor_point.y.toFixed(2))
  56. this.m_callback(this.m_data)
  57. } else {
  58. this.m_callback(null)
  59. }
  60. this.node.active = false
  61. }
  62. }