attributes_anchor.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Component, EditBox, Vec2, Node, Size } from 'cc';
  2. import { tools } from '../../tools';
  3. import { attributes_data } from '../../../data/data';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('attributes_anchor')
  6. export class attributes_anchor extends Component {
  7. @property(EditBox) editBox_x:EditBox = null;
  8. @property(EditBox) editBox_y:EditBox = null;
  9. @property(Node) btn_set:Node = null;
  10. private m_att_data: attributes_data = null;
  11. private call_back:Function = null;
  12. start() {
  13. this.editBox_x.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
  14. this.change()
  15. })
  16. this.editBox_y.node.on(EditBox.EventType.EDITING_DID_ENDED, ()=>{
  17. this.change()
  18. })
  19. this.btn_set.on(Node.EventType.TOUCH_END, ()=>{
  20. if(this.m_att_data.src.length==0) {
  21. return tools.showToast('还没有图片资源')
  22. }
  23. tools.loadUrl(this.m_att_data.src,null,(sf)=>{
  24. if(sf==null) {
  25. return tools.showToast('图片资源加载失败')
  26. }
  27. let res_size = new Size(this.m_att_data.width, this.m_att_data.height)
  28. let res_anchor = new Vec2(this.m_att_data.anchor_x, this.m_att_data.anchor_y)
  29. tools.show_setup_res_anchor(sf,res_size,res_anchor,(v:Vec2)=>{
  30. this.update_editStatus(v.x, v.y)
  31. if(this.call_back!=null){
  32. this.call_back(v)
  33. }
  34. })
  35. })
  36. })
  37. }
  38. initView(call:Function){
  39. this.call_back = call
  40. }
  41. change(){
  42. if(this.call_back!=null){
  43. this.call_back(new Vec2(parseFloat(this.editBox_x.string),parseFloat(this.editBox_y.string)))
  44. }
  45. }
  46. update_att(att_data:attributes_data, x:number, y:number) {
  47. this.m_att_data = att_data
  48. this.update_editStatus(x,y)
  49. }
  50. update_editStatus(x:number, y:number) {
  51. if(x==undefined||x==null) {
  52. x = 0.5
  53. }
  54. if(y==undefined||y==null) {
  55. y = 0.5
  56. }
  57. this.editBox_x.string = x.toString()
  58. this.editBox_y.string = y.toString()
  59. }
  60. }