receive_sound_item.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { _decorator, Component, Label, Node, Rect, UITransform, Vec2 } from 'cc';
  2. import { bag_item_data } from '../../../data/data';
  3. import { ClientEvent } from '../../clientEvent';
  4. import { config } from '../../config';
  5. import { base_res } from '../base_res';
  6. import { sound_item } from '../sound_item';
  7. import { Attributes } from '../Attributes';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('receive_sound_item')
  10. export class receive_sound_item extends Component {
  11. private m_data:bag_item_data = null;
  12. public updateView(data:bag_item_data){
  13. this.m_data = data;
  14. let lab = this.getLabel()
  15. if(lab!=null){
  16. lab.getComponent(Label).string = this.m_data.name;
  17. }
  18. }
  19. start() {
  20. ClientEvent.on(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
  21. }
  22. protected onDestroy(): void {
  23. ClientEvent.off(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
  24. }
  25. public getRect(){
  26. let size = this.node.getComponent(UITransform).contentSize;
  27. let pos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position);
  28. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  29. return rect;
  30. }
  31. DragResEndOnCheck(v2:Vec2,node:Node){
  32. if(this.getRect().contains(v2)){
  33. let type = node.getComponent(base_res).getType()
  34. if(type===config.select_res_btn_type.SOUND_LIST){
  35. let temp_data = node.getComponent(sound_item).getData()
  36. this.m_data.name = temp_data.name;
  37. this.m_data.url = temp_data.url;
  38. let lab = this.getLabel()
  39. if(lab!=null){
  40. lab.getComponent(Label).string = this.m_data.name;
  41. }
  42. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),config.attributes_list_type.text_sound)
  43. }
  44. }
  45. }
  46. getLabel(){
  47. return this.node.getChildByPath("Sprite/Label")
  48. }
  49. }