receive_res_item.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { _decorator, Component, Label, Node, Rect, UITransform, Vec2 } from 'cc';
  2. import { ClientEvent } from '../../clientEvent';
  3. import { config } from '../../config';
  4. import { img_item } from '../img_item';
  5. import { base_res } from '../base_res';
  6. import { bag_item_data, ui_att_item } from '../../../data/data';
  7. import { Attributes } from '../Attributes';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('receive_res_item')
  10. export class receive_res_item extends Component {
  11. @property(Node) btn_clear_res:Node = null;
  12. private m_data:ui_att_item = null;
  13. private widget_type = config.attributes_list_type.top;
  14. public initView(data:ui_att_item,type=config.attributes_list_type.top){
  15. this.widget_type = type;
  16. this.m_data = data;
  17. if(this.m_data.res_name.length>0){
  18. this.getLabel().getComponent(Label).string = this.m_data.res_name
  19. }else{
  20. this.getLabel().getComponent(Label).string ="无"
  21. }
  22. }
  23. start() {
  24. ClientEvent.on(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
  25. this.btn_clear_res.on(Node.EventType.TOUCH_END, ()=>{
  26. this.m_data.res = '';
  27. this.m_data.res_name = '';
  28. let lab = this.getLabel()
  29. if(lab!=null){
  30. lab.getComponent(Label).string = "无"
  31. }
  32. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
  33. })
  34. }
  35. protected onDestroy(): void {
  36. ClientEvent.off(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
  37. this.unscheduleAllCallbacks()
  38. }
  39. public getRect(){
  40. let size = this.node.getComponent(UITransform).contentSize;
  41. let pos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position);
  42. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  43. return rect;
  44. }
  45. getParentIsShow(){
  46. let parent = this.node.parent;
  47. let count = 0;
  48. while(parent!=null){
  49. if(!parent.active){
  50. return false;
  51. }
  52. parent = parent.parent;
  53. count ++;
  54. if(count>=10){
  55. return true;
  56. }
  57. }
  58. return true;
  59. }
  60. DragResEndOnCheck(v2:Vec2,node:Node){
  61. let is_show = this.getParentIsShow();
  62. console.log("DragResEndOnCheck",is_show)
  63. if(this.getRect().contains(v2)&&is_show){
  64. let type = node.getComponent(base_res).getType()
  65. if(type===config.select_res_btn_type.SOUND_LIST){
  66. }else{
  67. let temp_data = node.getComponent(img_item).getData()
  68. this.m_data.res = temp_data.url;
  69. this.m_data.res_name = temp_data.name;
  70. let lab = this.getLabel()
  71. if(lab!=null){
  72. lab.getComponent(Label).string = this.m_data.res_name;
  73. }
  74. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
  75. }
  76. }
  77. }
  78. getLabel(){
  79. return this.node.getChildByPath("Sprite/Label")
  80. }
  81. }