btn_question_item.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { _decorator, assetManager, Component, ImageAsset, Label, Node, Size, Sprite, SpriteFrame, Texture2D, UITransform, Vec3 } from 'cc';
  2. import { ui_att_item } from '../../../data/data';
  3. import { ClientEvent } from '../../clientEvent';
  4. import { config } from '../../config';
  5. import { Attributes } from '../Attributes';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('btn_question_item')
  8. export class btn_question_item extends Component {
  9. @property(Node) btn_click:Node = null;
  10. @property(Node) lab_title:Node = null;
  11. private m_data:ui_att_item = null;
  12. public initView(data:ui_att_item,index:number){
  13. if(data===null){
  14. this.m_data = new ui_att_item;
  15. this.init()
  16. }else{
  17. this.m_data = data;
  18. }
  19. this.lab_title.getComponent(Label).string = `按钮${index}`;
  20. this.updateView()
  21. this.btn_click.on(Node.EventType.TOUCH_MOVE,(et)=>{
  22. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  23. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  24. this.node.position = n_p;
  25. this.m_data.x = this.node.position.x;
  26. this.m_data.y = this.node.position.y;
  27. ClientEvent.dispatchEvent(config.Event.UpdateAttributes,Attributes.Singleton.get_cur_att_data())
  28. })
  29. }
  30. init(){
  31. this.m_data.x = this.node.position.x;
  32. this.m_data.y = this.node.position.y;
  33. this.m_data.width = this.node.getComponent(UITransform).contentSize.width;
  34. this.m_data.height = this.node.getComponent(UITransform).contentSize.height;
  35. }
  36. public getData(){
  37. return this.m_data;
  38. }
  39. public updateView(){
  40. this.node.getComponent(UITransform).contentSize = new Size(this.m_data.width,this.m_data.height)
  41. this.node.position = new Vec3(this.m_data.x,this.m_data.y)
  42. this.lab_title.position = new Vec3(0,0,0)
  43. if(this.m_data.res.length>0){
  44. assetManager.loadRemote<ImageAsset>(this.m_data.res, (err, imageAsset2)=>{
  45. if (!err && imageAsset2) {
  46. const texture = new Texture2D();
  47. texture.image = imageAsset2;
  48. let spFrame2 = new SpriteFrame();
  49. spFrame2.texture = texture;
  50. this.btn_click.getComponent(Sprite).spriteFrame = spFrame2
  51. }
  52. });
  53. }
  54. }
  55. }