widget_drag.ts 6.1 KB

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