widget_drag.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. @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){
  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. this.setScenePageScroll(false)
  45. })
  46. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  47. if(!this.mIsActive) return
  48. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  49. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  50. this.node.position = new Vec3(n_p.x,n_p.y);
  51. })
  52. this.node.on(Node.EventType.TOUCH_END,()=>{
  53. this.onEndEvent()
  54. this.setScenePageScroll(true)
  55. })
  56. }
  57. private setScenePageScroll(is_scroll:boolean) {
  58. let scene_page = this.getScenePage()
  59. if(scene_page==null) return
  60. if(is_scroll) {
  61. scene_page.startScrollTouch()
  62. } else {
  63. scene_page.stopScrollTouch()
  64. }
  65. }
  66. private onEndEvent(){
  67. if(!this.mIsActive) return
  68. this.mIsStartMove = false;
  69. this.checkOnecFinish()
  70. }
  71. private checkOther(){
  72. if(this.mDragData.other_drag_list==undefined){
  73. return false;
  74. }
  75. let isCheck = false;
  76. if(this.other_drag_list.length>0){
  77. for (let index = 0; index < this.other_drag_list.length; index++) {
  78. const item_data = this.other_drag_list[index];
  79. let w = item_data.drag_size_width;
  80. let h = item_data.drag_size_height;
  81. let pos = new Vec3(this.mData.att.x+item_data.drag_pos_x,this.mData.att.y+item_data.drag_pos_y)
  82. let rect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  83. let p = this.node.position;
  84. let is = rect.contains(new Vec2(p.x,p.y))
  85. if(is){
  86. if(item_data.other_event_id!=-1){
  87. this.offEvent()
  88. console.log("item_data.other_event_id",item_data.other_event_id)
  89. gameManager.Singleton.exeEvent(item_data.other_event_id)
  90. isCheck = true;
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. return isCheck;
  97. }
  98. checkOtherListenWidgetFinish(){
  99. let is_finish = true;
  100. if(this.mDragData.other_widget_finish_listen_list==undefined){
  101. return true
  102. }
  103. if(this.mDragData.other_widget_finish_listen_list.length<=0){
  104. return true;
  105. }
  106. let event_id = gameManager.Singleton.checkWidgetList(this.mDragData.other_widget_finish_listen_list)
  107. if(event_id==-1){
  108. is_finish = true;
  109. }else{
  110. is_finish = false;
  111. gameManager.Singleton.exeEvent(event_id)
  112. }
  113. return is_finish;
  114. }
  115. public deleteOtherDrag(index:number){
  116. this.other_drag_list.splice(index,1)
  117. }
  118. private checkOnecFinish(){
  119. if(this.checkMoveToDragRect()){
  120. this.mIsStartMove = false;
  121. if(this.checkOtherListenWidgetFinish()){
  122. this.onFinishEvent()
  123. }else{
  124. }
  125. }else{
  126. if(this.checkOther()){
  127. this.mIsStartMove = false;
  128. }else{
  129. if(this.mDragData.is_err_drag_back){
  130. this.node.position = new Vec3(this.mData.att.x,this.mData.att.y)
  131. }
  132. }
  133. }
  134. }
  135. public getDragRect():Rect{
  136. if(this.mDragData==null){
  137. this.mDragData = this.mData.att.drag_data
  138. }
  139. let w = this.mDragData.drag_size_width;
  140. let h = this.mDragData.drag_size_height;
  141. let pos = new Vec3(this.mData.att.x+this.mDragData.drag_pos_x,this.mData.att.y+this.mDragData.drag_pos_y)
  142. this.mDragRect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  143. return this.mDragRect;
  144. }
  145. private checkMoveToDragRect():boolean{
  146. let p = this.node.position;
  147. let is = this.getDragRect().contains(new Vec2(p.x,p.y))
  148. return is
  149. }
  150. protected onFinishAnimation(): void {
  151. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  152. // node.setSiblingIndex(node.parent.childrenCount - 1);
  153. // this.icon.setSiblingIndex(999)
  154. }
  155. public showZhaoButongFinishStatus(): void {
  156. gameManager.Singleton.sys_click_correct_detail_music()
  157. this.img_zhaobutong.active = true;
  158. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  159. }
  160. public hideZhaoButongFinishStatus(): void {
  161. if(this.img_zhaobutong != null) {
  162. this.img_zhaobutong.active = false;
  163. }
  164. }
  165. public toFinishEvent(){
  166. this.onFinishEvent()
  167. }
  168. }