bag_drag_item.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { _decorator, Component, EventTouch, instantiate, Node, Prefab, Rect, Sprite, UITransform, Vec2, Vec3 } from 'cc';
  2. import { dai_dao_ju_item, widget_item_data } from '../../../data/data';
  3. import { widget_base } from '../widget/widget_base';
  4. import { widget_drag } from '../widget/widget_drag';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('bag_drag_item')
  7. export class bag_drag_item extends Component {
  8. @property(Node) spr_select_status:Node = null;
  9. @property(Node) drag_item_parent:Node = null;
  10. @property(Prefab) drag_item_prefab:Prefab = null;
  11. private mData:dai_dao_ju_item = null;
  12. private mCallBack = null;
  13. private drag_item:Node = null;
  14. private action_node:Node = null;
  15. private mFinishCall = null;
  16. public initView(data:dai_dao_ju_item,widget_data:widget_item_data,call,finish_call){
  17. this.drag_item= instantiate(this.drag_item_prefab)
  18. this.drag_item_parent.removeAllChildren()
  19. this.drag_item.parent = this.drag_item_parent;
  20. let com = this.drag_item.getComponent(widget_base);
  21. com.initView(widget_data,false);
  22. this.mData = data;
  23. this.mCallBack = call;
  24. this.mFinishCall = finish_call;
  25. this.node.off(Node.EventType.TOUCH_END)
  26. this.node.on(Node.EventType.TOUCH_END,()=>{
  27. if(this.mCallBack!=null){
  28. this.mCallBack(this)
  29. }
  30. })
  31. }
  32. public DeleteSelf(){
  33. this.unSelect()
  34. this.node.off(Node.EventType.TOUCH_END)
  35. this.node.removeFromParent()
  36. }
  37. public isZero():boolean{
  38. return this.drag_item.position.x==Vec3.ZERO.x&&this.drag_item.position.y==Vec3.ZERO.y
  39. }
  40. public getData(){
  41. return this.mData;
  42. }
  43. public onSelect(){
  44. this.spr_select_status.active = true;
  45. }
  46. public unSelect(){
  47. this.spr_select_status.active = false;
  48. this.drag_item.off(Node.EventType.TOUCH_END)
  49. this.drag_item.off(Node.EventType.TOUCH_MOVE)
  50. }
  51. public getRect():Rect{
  52. if(this.drag_item!=null){
  53. return this.drag_item.getComponent(widget_drag).getDragRect();
  54. }
  55. }
  56. public registeredEvent(action_node:Node){
  57. this.action_node = action_node;
  58. this.drag_item.off(Node.EventType.TOUCH_MOVE)
  59. this.drag_item.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  60. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  61. let n_p = this.drag_item_parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  62. this.drag_item.position = new Vec3(n_p.x,n_p.y);
  63. })
  64. this.drag_item.off(Node.EventType.TOUCH_END)
  65. this.drag_item.on(Node.EventType.TOUCH_END,()=>{
  66. this.onEndEvent()
  67. })
  68. }
  69. private onEndEvent(){
  70. this.checkOnecFinish()
  71. }
  72. private checkOnecFinish(){
  73. if(this.checkMoveToDragRect()){
  74. this.drag_item.getComponent(widget_drag).toFinishEvent()
  75. // console.log("完成了自己的任务")
  76. if(this.mFinishCall!=null){
  77. this.mFinishCall(this)
  78. }
  79. }else{
  80. this.drag_item.position = Vec3.ZERO;
  81. }
  82. }
  83. private checkMoveToDragRect():boolean{
  84. let p = this.drag_item.position;
  85. let n_p = this.drag_item_parent.getComponent(UITransform).convertToWorldSpaceAR(p)
  86. let nn_p = this.action_node.getComponent(UITransform).convertToNodeSpaceAR(n_p)
  87. let is = this.getRect().contains(new Vec2(nn_p.x,nn_p.y))
  88. return is
  89. }
  90. }