widget_drag.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. private other_drag_list:att_drag_data[] = [];
  14. protected init(){
  15. if(this.mData.att.drag_data!=null){
  16. this.mDragData = this.mData.att.drag_data
  17. }
  18. if(this.mData.att.drag_data.other_drag_list!=undefined){
  19. for (let index = 0; index < this.mData.att.drag_data.other_drag_list.length; index++) {
  20. const element = this.mData.att.drag_data.other_drag_list[index];
  21. let item = new att_drag_data;
  22. item.drag_pos_x = element.drag_pos_x;
  23. item.drag_pos_y = element.drag_pos_y;
  24. item.other_event_id = element.other_event_id;
  25. item.drag_size_width = element.drag_size_width;
  26. item.drag_size_height = element.drag_size_height;
  27. this.other_drag_list.push(item)
  28. }
  29. }
  30. }
  31. protected start(): void {
  32. if(this.mIsActive){
  33. this.registeredEvent()
  34. }
  35. }
  36. protected registeredEvent(): void {
  37. this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  38. if(!this.mIsActive) return
  39. this.mIsStartMove = true;
  40. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  41. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  42. this.node.position = new Vec3(n_p.x,n_p.y);
  43. })
  44. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  45. if(!this.mIsActive) return
  46. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  47. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  48. this.node.position = new Vec3(n_p.x,n_p.y);
  49. })
  50. this.node.on(Node.EventType.TOUCH_END,()=>{
  51. this.onEndEvent()
  52. })
  53. }
  54. private onEndEvent(){
  55. if(!this.mIsActive) return
  56. this.mIsStartMove = false;
  57. this.checkOnecFinish()
  58. }
  59. private checkOther(){
  60. if(this.mDragData.other_drag_list==undefined){
  61. return false;
  62. }
  63. let isCheck = false;
  64. if(this.other_drag_list.length>0){
  65. for (let index = 0; index < this.other_drag_list.length; index++) {
  66. const item_data = this.other_drag_list[index];
  67. let w = item_data.drag_size_width;
  68. let h = item_data.drag_size_height;
  69. let pos = new Vec3(this.mData.att.x+item_data.drag_pos_x,this.mData.att.y+item_data.drag_pos_y)
  70. let rect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  71. let p = this.node.position;
  72. let is = rect.contains(new Vec2(p.x,p.y))
  73. if(is){
  74. if(item_data.other_event_id!=-1){
  75. this.offEvent()
  76. console.log("item_data.other_event_id",item_data.other_event_id)
  77. gameManager.Singleton.exeEvent(item_data.other_event_id)
  78. isCheck = true;
  79. break;
  80. }
  81. }
  82. }
  83. }
  84. return isCheck;
  85. }
  86. checkOtherListenWidgetFinish(){
  87. let is_finish = true;
  88. if(this.mDragData.other_widget_finish_listen_list==undefined){
  89. return true
  90. }
  91. if(this.mDragData.other_widget_finish_listen_list.length<=0){
  92. return true;
  93. }
  94. let event_id = gameManager.Singleton.checkWidgetList(this.mDragData.other_widget_finish_listen_list)
  95. if(event_id==-1){
  96. is_finish = true;
  97. }else{
  98. is_finish = false;
  99. gameManager.Singleton.exeEvent(event_id)
  100. }
  101. return is_finish;
  102. }
  103. public deleteOtherDrag(index:number){
  104. this.other_drag_list.splice(index,1)
  105. }
  106. private checkOnecFinish(){
  107. if(this.checkMoveToDragRect()){
  108. this.mIsStartMove = false;
  109. if(this.checkOtherListenWidgetFinish()){
  110. this.onFinishEvent()
  111. }else{
  112. }
  113. }else{
  114. if(this.checkOther()){
  115. this.mIsStartMove = false;
  116. }else{
  117. if(this.mDragData.is_err_drag_back){
  118. this.node.position = new Vec3(this.mData.att.x,this.mData.att.y)
  119. }
  120. }
  121. }
  122. }
  123. private getDragRect():Rect{
  124. let w = this.mDragData.drag_size_width;
  125. let h = this.mDragData.drag_size_height;
  126. let pos = new Vec3(this.mData.att.x+this.mDragData.drag_pos_x,this.mData.att.y+this.mDragData.drag_pos_y)
  127. this.mDragRect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  128. return this.mDragRect;
  129. }
  130. private checkMoveToDragRect():boolean{
  131. let p = this.node.position;
  132. let is = this.getDragRect().contains(new Vec2(p.x,p.y))
  133. return is
  134. }
  135. }