show_setup_res_anchor.ts 2.9 KB

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