widget_drag.ts 7.0 KB

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