receive_res_item.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. if(this.btn_clear_res!=null) {
  26. this.btn_clear_res.on(Node.EventType.TOUCH_END, ()=>{
  27. this.m_data.res = '';
  28. this.m_data.res_name = '';
  29. let lab = this.getLabel()
  30. if(lab!=null){
  31. lab.getComponent(Label).string = "无"
  32. }
  33. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
  34. })
  35. }
  36. }
  37. protected onDestroy(): void {
  38. ClientEvent.off(config.Event.DragResEndOnCheck,this.DragResEndOnCheck,this)
  39. this.unscheduleAllCallbacks()
  40. }
  41. public getRect(){
  42. let size = this.node.getComponent(UITransform).contentSize;
  43. let pos = this.node.parent.getComponent(UITransform).convertToWorldSpaceAR(this.node.position);
  44. let rect = new Rect(pos.x-size.width/2,pos.y-size.height/2,size.width,size.height)
  45. return rect;
  46. }
  47. getParentIsShow(){
  48. let parent = this.node.parent;
  49. let count = 0;
  50. while(parent!=null){
  51. if(!parent.active){
  52. return false;
  53. }
  54. parent = parent.parent;
  55. count ++;
  56. if(count>=10){
  57. return true;
  58. }
  59. }
  60. return true;
  61. }
  62. DragResEndOnCheck(v2:Vec2,node:Node){
  63. let is_show = this.getParentIsShow();
  64. console.log("DragResEndOnCheck",is_show)
  65. if(this.getRect().contains(v2)&&is_show){
  66. let type = node.getComponent(base_res).getType()
  67. if(type===config.select_res_btn_type.SOUND_LIST){
  68. }else{
  69. let temp_data = node.getComponent(img_item).getData()
  70. this.m_data.res = temp_data.url;
  71. this.m_data.res_name = temp_data.name;
  72. let lab = this.getLabel()
  73. if(lab!=null){
  74. lab.getComponent(Label).string = this.m_data.res_name;
  75. }
  76. ClientEvent.dispatchEvent(config.Event.UpdateAttributesToView,Attributes.Singleton.get_cur_att_data(),this.widget_type)
  77. }
  78. }
  79. }
  80. getLabel(){
  81. return this.node.getChildByPath("Sprite/Label")
  82. }
  83. }