widget_drag.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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, other_widget_finish_listen_item, 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.off(Node.EventType.TOUCH_START)
  43. this.node.on(Node.EventType.TOUCH_START,(et:EventTouch)=>{
  44. if(!this.mIsActive) return
  45. this.mIsStartMove = true;
  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. this.setScenePageScroll(false)
  50. })
  51. this.node.off(Node.EventType.TOUCH_MOVE)
  52. this.node.on(Node.EventType.TOUCH_MOVE,(et:EventTouch)=>{
  53. if(!this.mIsActive) return
  54. let p = new Vec3(et.getUILocation().x,et.getUILocation().y)
  55. let n_p = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(p)
  56. this.node.position = new Vec3(n_p.x,n_p.y);
  57. })
  58. this.node.off(Node.EventType.TOUCH_END)
  59. this.node.on(Node.EventType.TOUCH_END,()=>{
  60. this.onEndEvent()
  61. this.setScenePageScroll(true)
  62. })
  63. }
  64. private setScenePageScroll(is_scroll:boolean) {
  65. let scene_page = this.getScenePage()
  66. if(scene_page==null) return
  67. if(is_scroll) {
  68. scene_page.startScrollTouch()
  69. } else {
  70. scene_page.stopScrollTouch()
  71. }
  72. }
  73. private onEndEvent(){
  74. if(!this.mIsActive) return
  75. this.mIsStartMove = false;
  76. this.checkOnecFinish()
  77. }
  78. private checkOther(){
  79. if(this.mDragData.other_drag_list==undefined){
  80. return false;
  81. }
  82. let isCheck = false;
  83. if(this.other_drag_list.length>0){
  84. for (let index = 0; index < this.other_drag_list.length; index++) {
  85. const item_data = this.other_drag_list[index];
  86. let w = item_data.drag_size_width;
  87. let h = item_data.drag_size_height;
  88. let pos = new Vec3(this.mData.att.x+item_data.drag_pos_x,this.mData.att.y+item_data.drag_pos_y)
  89. let rect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  90. let p = this.node.position;
  91. let is = rect.contains(new Vec2(p.x,p.y))
  92. if(is){
  93. if(item_data.other_event_id!=-1){
  94. this.offEvent()
  95. console.log("执行其他拖拽的完成事件id ",item_data.other_event_id,`,${index}`,rect,p)
  96. ClientEvent.dispatchEvent(config.EventRun.WIDGET_DRAG_OTHER_FINISH,this.mData.att.id,item_data.score)
  97. gameManager.Singleton.exeEvent(item_data.other_event_id)
  98. isCheck = true;
  99. break;
  100. }
  101. }
  102. }
  103. }
  104. return isCheck;
  105. }
  106. public deleteOtherDrag(index:number){
  107. // this.other_drag_list.splice(index,1)
  108. for (let i = 0; i < this.other_drag_list.length; i++) {
  109. const element = this.other_drag_list[i];
  110. if(element.index==index) {
  111. this.other_drag_list.splice(i,1)
  112. break
  113. }
  114. }
  115. }
  116. private checkOnecFinish(){
  117. if(this.checkMoveToDragRect()){
  118. this.mIsStartMove = false;
  119. this.checkAddOtherWidgetFinishListenOperation()
  120. } else {
  121. if(this.checkOther()){
  122. this.mIsStartMove = false;
  123. }else{
  124. if(this.mDragData.is_err_drag_back){
  125. this.resetOriginPostion()
  126. }
  127. }
  128. }
  129. }
  130. public getDragRect():Rect{
  131. if(this.mDragData==null){
  132. this.mDragData = this.mData.att.drag_data
  133. }
  134. let w = this.mDragData.drag_size_width;
  135. let h = this.mDragData.drag_size_height;
  136. let pos = new Vec3(this.mData.att.x+this.mDragData.drag_pos_x,this.mData.att.y+this.mDragData.drag_pos_y)
  137. this.mDragRect = new Rect(pos.x-w*0.5,pos.y-h*0.5,w,h)
  138. return this.mDragRect;
  139. }
  140. private checkMoveToDragRect():boolean{
  141. let p = this.node.position;
  142. let is = this.getDragRect().contains(new Vec2(p.x,p.y))
  143. return is
  144. }
  145. protected onFinishAnimation(): void {
  146. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  147. // node.setSiblingIndex(node.parent.childrenCount - 1);
  148. // this.icon.setSiblingIndex(999)
  149. }
  150. public showZhaoButongFinishStatus(): void {
  151. gameManager.Singleton.sys_click_correct_detail_music()
  152. this.img_zhaobutong.active = true;
  153. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  154. }
  155. public hideZhaoButongFinishStatus(): void {
  156. if(this.img_zhaobutong != null) {
  157. this.img_zhaobutong.active = false;
  158. }
  159. }
  160. public toFinishEvent(){
  161. this.onFinishEvent()
  162. }
  163. // 复位
  164. private resetOriginPostion() {
  165. this.node.position = new Vec3(this.mData.att.x,this.mData.att.y)
  166. }
  167. // 检查添加其他控件
  168. private checkAddOtherWidgetFinishListenOperation() {
  169. let onFinishEvent_cb = (()=>{
  170. this.checkOtherListenWidgetFinish(false)
  171. this.onFinishEvent()
  172. })
  173. if(this.checkOtherWidgetFinishListenListData()==false) {
  174. onFinishEvent_cb()
  175. return
  176. }
  177. // 是-直接执行自身
  178. if(this.mDragData.is_direct_execute_myself) {
  179. // console.log('直接-执行自身')
  180. onFinishEvent_cb()
  181. }
  182. // 否-直接执行自身
  183. else {
  184. // 是-选择其一执行自身
  185. let finish_list = this.checkOtherListenWidgetFinish(true)
  186. // console.log('完成的控件列表=',finish_list)
  187. if(this.mDragData.is_select_anyone_execute_myself) {
  188. let is_execute_myself = false
  189. for (let index = 0; index < finish_list.length; index++) {
  190. const element = finish_list[index];
  191. if(element.is_execute_myself) {
  192. is_execute_myself = true
  193. break
  194. }
  195. }
  196. if(is_execute_myself) {
  197. // console.log('满足其中一个条件-执行自身')
  198. onFinishEvent_cb()
  199. } else {
  200. // 归位
  201. setTimeout(()=>{
  202. this.resetOriginPostion()
  203. },50)
  204. }
  205. }
  206. // 否-选择其一执行自身
  207. else {
  208. // 控件全部完成
  209. if(finish_list.length==this.mDragData.other_widget_finish_listen_list.length) {
  210. // console.log('控件全部完成-执行自身')
  211. onFinishEvent_cb()
  212. return
  213. }
  214. // 同时满足
  215. let execute_count = 0
  216. let have_count = 0
  217. for (let i = 0; i < this.mDragData.other_widget_finish_listen_list.length; i++) {
  218. const i_element = this.mDragData.other_widget_finish_listen_list[i]
  219. if(i_element.is_execute_myself) {
  220. execute_count+=1
  221. for (let j = 0; j < finish_list.length; j++) {
  222. const j_element = finish_list[j];
  223. if(i_element.widget_id==j_element.widget_id) {
  224. have_count+=1
  225. }
  226. }
  227. }
  228. }
  229. if(execute_count>0) {
  230. if(execute_count==have_count) {
  231. // console.log('同时满足条件-执行自身')
  232. onFinishEvent_cb()
  233. return
  234. }
  235. }
  236. // 归位
  237. setTimeout(()=>{
  238. this.resetOriginPostion()
  239. },50)
  240. }
  241. }
  242. // 未完成控件,执行监控事件
  243. this.checkOtherListenWidgetFinish(false)
  244. }
  245. private checkOtherWidgetFinishListenListData():boolean {
  246. if(this.mDragData.other_widget_finish_listen_list==undefined){
  247. return false
  248. }
  249. if(this.mDragData.other_widget_finish_listen_list.length<=0){
  250. return false
  251. }
  252. return true
  253. }
  254. private checkOtherListenWidgetFinish(is_finish_status:boolean):other_widget_finish_listen_item[]{
  255. if(this.checkOtherWidgetFinishListenListData()==false) {
  256. return []
  257. }
  258. let list = gameManager.Singleton.checkWidgetList(this.mDragData.other_widget_finish_listen_list,is_finish_status)
  259. if(is_finish_status==false&&list.length>0) {
  260. // for (let index = 0; index < list.length; index++) {
  261. // const event = list[index];
  262. // gameManager.Singleton.exeEvent(event.event_id)
  263. // }
  264. // console.log('list=',list)
  265. // 未完成:其中有2,只执行2; 没有2,所有1执行; 没有2&&没有1,所有0执行;
  266. let hight_list:other_widget_finish_listen_item[] = []
  267. let middle_list:other_widget_finish_listen_item[] = []
  268. let normal_list:other_widget_finish_listen_item[] = []
  269. for (let index = 0; index < list.length; index++) {
  270. const event = list[index];
  271. if(event.grade>=2) {
  272. hight_list.push(event)
  273. break
  274. } else if(event.grade==1) {
  275. middle_list.push(event)
  276. } else {
  277. normal_list.push(event)
  278. }
  279. }
  280. let exeEvent_cb = ((item_list:other_widget_finish_listen_item[])=>{
  281. for (let index = 0; index < item_list.length; index++) {
  282. const event = item_list[index];
  283. gameManager.Singleton.exeEvent(event.event_id)
  284. }
  285. })
  286. if(hight_list.length>0) {
  287. middle_list = []
  288. normal_list = []
  289. exeEvent_cb(hight_list)
  290. } else {
  291. if(middle_list.length>0) {
  292. normal_list = []
  293. exeEvent_cb(middle_list)
  294. } else {
  295. exeEvent_cb(normal_list)
  296. }
  297. }
  298. }
  299. return list;
  300. }
  301. }