widget_drag.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { _decorator, Component, EventTouch, Node, Rect, UITransform, v2, Vec2, Vec3 } from 'cc';
  2. import { widget_base } from './widget_base';
  3. import { att_drag_data, widget_item_data } from '../../../data/data';
  4. import { gameManager } from '../gameManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('widget_drag')
  7. export class widget_drag extends widget_base {
  8. private mDragData:att_drag_data = null;
  9. private mIsStartMove:boolean = false;
  10. private offset_x:number = 0;
  11. private offset_y:number = 0;
  12. private mDragRect:Rect = null;
  13. protected init(){
  14. if(this.mData.att.drag_data!=null){
  15. this.mDragData = this.mData.att.drag_data
  16. }
  17. }
  18. protected start(): void {
  19. if(this.mIsActive){
  20. this.registeredEvent()
  21. }
  22. }
  23. protected registeredEvent(): void {
  24. this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  25. if(!this.mIsActive) return
  26. console.log("target",et.target)
  27. this.mIsStartMove = true;
  28. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  29. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  30. this.node.position = new Vec3(n_p.x,n_p.y);
  31. })
  32. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  33. if(!this.mIsActive) return
  34. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  35. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  36. this.node.position = new Vec3(n_p.x,n_p.y);
  37. })
  38. this.node.on(Node.EventType.TOUCH_END,()=>{
  39. this.onEndEvent()
  40. })
  41. }
  42. private onEndEvent(){
  43. if(!this.mIsActive) return
  44. this.mIsStartMove = false;
  45. this.checkOnecFinish()
  46. }
  47. private checkOther(){
  48. if(this.mDragData.other_drag_list==undefined){
  49. return false;
  50. }
  51. let isCheck = false;
  52. if(this.mDragData.other_drag_list.length>0){
  53. for (let index = 0; index < this.mDragData.other_drag_list.length; index++) {
  54. const item_data = this.mDragData.other_drag_list[index];
  55. let w = item_data.drag_size_width;
  56. let h = item_data.drag_size_height;
  57. let pos = new Vec3(this.mData.att.x+item_data.drag_pos_x,this.mData.att.y+item_data.drag_pos_y)
  58. let rect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  59. let p = this.node.position;
  60. let is = rect.contains(new Vec2(p.x,p.y))
  61. if(is){
  62. if(item_data.other_event_id!=-1){
  63. this.offEvent()
  64. console.log("item_data.other_event_id",item_data.other_event_id)
  65. gameManager.Singleton.exeEvent(item_data.other_event_id)
  66. isCheck = true;
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. return isCheck;
  73. }
  74. private checkOnecFinish(){
  75. if(this.checkMoveToDragRect()){
  76. this.mIsStartMove = false;
  77. this.onFinishEvent()
  78. }else{
  79. if(this.checkOther()){
  80. this.mIsStartMove = false;
  81. }else{
  82. if(this.mDragData.is_err_drag_back){
  83. this.node.position = new Vec3(this.mData.att.x,this.mData.att.y)
  84. }
  85. }
  86. }
  87. }
  88. private getDragRect():Rect{
  89. let w = this.mDragData.drag_size_width;
  90. let h = this.mDragData.drag_size_height;
  91. let pos = new Vec3(this.mData.att.x+this.mDragData.drag_pos_x,this.mData.att.y+this.mDragData.drag_pos_y)
  92. this.mDragRect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  93. return this.mDragRect;
  94. }
  95. private checkMoveToDragRect():boolean{
  96. let p = this.node.position;
  97. let is = this.getDragRect().contains(new Vec2(p.x,p.y))
  98. return is
  99. }
  100. }