widget_drag.ts 6.8 KB

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