Toucher.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var s =
  5. (this && this.__decorate) ||
  6. function (t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", {value: !0});
  17. const {ccclass: o, property: a} = cc._decorator;
  18. e = class extends cc.Component {
  19. constructor() {
  20. super(...arguments),
  21. (this.target = null),
  22. (this.enableTap = !0),
  23. (this.enableDoubleTap = !1),
  24. (this.tapDuration = 300),
  25. (this.tapRadius = 30),
  26. (this.enableHold = !1),
  27. (this.holdDelay = 400),
  28. (this.enablePan = !1),
  29. (this.enableSwipe = !1),
  30. (this.swipeDuration = 1e3),
  31. (this.swipeMinDistance = 90),
  32. (this.swipeMaxDistance = 1e3),
  33. (this.enableJoystick = !1),
  34. (this.joystickRadius = 100),
  35. (this.floatStick = !1),
  36. (this.disabled = !1),
  37. (this.onStart = null),
  38. (this.onMove = null),
  39. (this.onEnd = null),
  40. (this.onTap = null),
  41. (this.onDoubleTap = null),
  42. (this.holdStart = null),
  43. (this.holdEnd = null),
  44. (this.onPan = null),
  45. (this.onSwipeUp = null),
  46. (this.onSwipeDown = null),
  47. (this.onSwipeLeft = null),
  48. (this.onSwipeRight = null),
  49. (this.onJoystickChanged = null),
  50. (this.touchedCount = 0),
  51. (this.RAD_TO_DEG = 180 / Math.PI),
  52. (this.DEG_TO_RAD = Math.PI / 180);
  53. }
  54. onLoad() {
  55. this.init();
  56. }
  57. init() {
  58. (this.statusCache = {}),
  59. (this.listenedNodes = []),
  60. this.resetStatus(),
  61. this.target && this.registerEvents(this.target);
  62. }
  63. resetStatus() {
  64. (this.statusCache = {}), (this.hooks = {});
  65. }
  66. getTouchStatus(t) {
  67. return this.statusCache[t];
  68. }
  69. removeTouchStatus(t) {
  70. delete this.statusCache[t];
  71. }
  72. createTouchStatus(t) {
  73. var e = {
  74. id: t,
  75. event: null,
  76. target: null,
  77. touchedID: -1,
  78. startTime: 0,
  79. startScreenPos: null,
  80. endTime: 0,
  81. endScreenPos: null,
  82. screenPos: null,
  83. _holdTimeoutId: null,
  84. holding: !1,
  85. _prevTapPos: null,
  86. _prevTapTime: 0,
  87. _tapIndex: 0,
  88. joystick: null
  89. };
  90. return (this.statusCache[t] = e);
  91. }
  92. createJoystickStatus() {
  93. return {
  94. x: 0,
  95. y: 0,
  96. centerX: 0,
  97. centerY: 0,
  98. rotation: 0,
  99. angle: 0,
  100. distance: 0,
  101. radius: this.joystickRadius,
  102. pressed: !1
  103. };
  104. }
  105. resetEventStatus(t) {
  106. null !== t._holdTimeoutId && (clearTimeout(t._holdTimeoutId), (t._holdTimeoutId = null)),
  107. (t.holding = !1),
  108. (t.touchedID = -1),
  109. (t.event = null),
  110. (t.joystick = null),
  111. (t.startTime = 0),
  112. (t.startScreenPos = null),
  113. (t.endTime = 0),
  114. (t.endScreenPos = null),
  115. (t.screenPos = null);
  116. }
  117. registerEvents(o, t = !1, u = !0) {
  118. this.listenedNodes.push(o);
  119. const p = this.tapRadius * this.tapRadius,
  120. e = i => {
  121. if ((this.touchedCount++, !0 !== this.disabled)) {
  122. const s = i.getID(),
  123. e = this.createTouchStatus(s);
  124. (e.target = o), (e.touchedID = i.getID()), (e.event = i), (e.startTime = Date.now());
  125. var t = i.getLocation();
  126. (e.screenPos = t),
  127. (e.startScreenPos = t),
  128. this.onStart && this.onStart(t.x, t.y, e),
  129. this.enableHold &&
  130. this.holdStart &&
  131. 0 < this.holdDelay &&
  132. (e._holdTimeoutId = setTimeout(() => {
  133. if (this.isValid) {
  134. const e = this.getTouchStatus(s);
  135. var t;
  136. e &&
  137. ((t = i.getLocation()),
  138. (e.screenPos = t),
  139. (e.holding = !0),
  140. this.holdStart(t.x, t.y, e));
  141. }
  142. }, this.holdDelay)),
  143. this.enableJoystick && this.resetJoystick(t.x, t.y, !0, e),
  144. u && i.stopPropagation();
  145. }
  146. };
  147. o.on(cc.Node.EventType.TOUCH_START, e, null, t);
  148. var i = t => {
  149. if (!0 !== this.disabled) {
  150. const o = t.getID(),
  151. a = this.getTouchStatus(o);
  152. var e, i, s;
  153. a &&
  154. ((e = (a.event = t).getDeltaX()),
  155. (i = t.getDeltaY()),
  156. (0 === e && 0 === i) ||
  157. ((s = t.getLocation()),
  158. (a.screenPos = s),
  159. this.onMove && this.onMove(s.x, s.y, a),
  160. this.enableJoystick && this.moveJoystick(s.x, s.y, a),
  161. this.enablePan && this.onPan && this.onPan(s.x, s.y, e, i, a),
  162. u && t.stopPropagation()));
  163. }
  164. };
  165. o.on(cc.Node.EventType.TOUCH_MOVE, i, null, t);
  166. var s = e => {
  167. this.touchedCount--;
  168. const i = e.getID(),
  169. s = this.getTouchStatus(i);
  170. if (s)
  171. if (
  172. ((s.event = e),
  173. null !== s._holdTimeoutId && (clearTimeout(s._holdTimeoutId), (s._holdTimeoutId = null)),
  174. !0 !== this.disabled)
  175. ) {
  176. const o = Date.now();
  177. s.endTime = o;
  178. const a = e.getLocation();
  179. (s.screenPos = a),
  180. (s.endScreenPos = a),
  181. this.onEnd && this.onEnd(a.x, a.y, s),
  182. s.holding && this.holdEnd && ((s.holding = !1), this.holdEnd(a.x, a.y, s));
  183. const n = s.startTime,
  184. r = s.startScreenPos,
  185. l = o - n,
  186. h = a.x - r.x,
  187. c = a.y - r.y,
  188. d = h * h + c * c;
  189. let t = !1;
  190. if (
  191. this.enableDoubleTap &&
  192. this.onDoubleTap &&
  193. l < this.tapDuration &&
  194. d < p &&
  195. 1 === s._tapIndex
  196. ) {
  197. const e = n - s._prevTapTime,
  198. i = s._prevTapPos.x,
  199. u = s._prevTapPos.y,
  200. o = r.x - i,
  201. a = r.y - u,
  202. l = o * o + a * a;
  203. e < this.tapDuration && l < p && ((t = !0), this.onDoubleTap(i, u, s)), (s._tapIndex = 0);
  204. }
  205. if (
  206. (s._tapIndex++,
  207. this.onTap(r.x, r.y, s),
  208. (s._prevTapPos = r),
  209. this.enableTap &&
  210. !t &&
  211. this.onTap &&
  212. l < this.tapDuration &&
  213. d < this.tapRadius * this.tapRadius)
  214. )
  215. s._prevTapTime = o;
  216. else if (this.enableSwipe && l < this.swipeDuration) {
  217. const e = Math.sqrt(d);
  218. if (e > this.swipeMinDistance && e < this.swipeMaxDistance) {
  219. const i = Math.abs(c) > Math.abs(h),
  220. u = e / l;
  221. i && 0 < c
  222. ? this.onSwipeUp && this.onSwipeUp(u, s)
  223. : i
  224. ? this.onSwipeDown && this.onSwipeDown(u, s)
  225. : !i && 0 < h
  226. ? this.onSwipeRight && this.onSwipeRight(u, s)
  227. : this.onSwipeLeft && this.onSwipeLeft(u, s),
  228. (s._tapIndex = 0);
  229. }
  230. }
  231. this.enableJoystick && this.resetJoystick(null, null, !1, s),
  232. this.removeTouchStatus(i),
  233. u && e.stopPropagation();
  234. } else this.removeTouchStatus(i);
  235. };
  236. o.on(cc.Node.EventType.TOUCH_END, s, null, t),
  237. o.on(cc.Node.EventType.TOUCH_CANCEL, s, null, t),
  238. (this.hooks = {start: e, move: i, end: s});
  239. }
  240. registerEventsOnChildren(t, e = !1, i = !0) {
  241. t.children.forEach(t => {
  242. this.registerEvents(t, e, i);
  243. });
  244. }
  245. resetJoystick(t, e, i, s) {
  246. const o = (s.joystick = this.createJoystickStatus());
  247. t || 0 === t ? (o.centerX = t) : (t = o.centerX),
  248. e || 0 === e ? (o.centerY = e) : (e = o.centerY),
  249. (o.x = t),
  250. (o.y = e),
  251. (o.distance = 0),
  252. (o.angle = 0),
  253. (o.pressed = !!i),
  254. this.onJoystickChanged && this.onJoystickChanged(o, s);
  255. }
  256. moveJoystick(e = 0, i = 0, s) {
  257. const o = s.joystick;
  258. if (o) {
  259. (o.x = e), (o.y = i);
  260. (e = o.x - o.centerX), (i = o.y - o.centerY);
  261. let t = Math.sqrt(e * e + i * i) >> 0;
  262. if (((o.distance = t), t > o.radius)) {
  263. const a = o.radius / t;
  264. this.floatStick
  265. ? ((o.centerX = o.x - e * a), (o.centerY = o.y - i * a))
  266. : ((o.x = o.centerX + e * a), (o.y = o.centerY + i * a)),
  267. (t = o.radius);
  268. }
  269. o.distance = t;
  270. (i = Math.atan2(i, e)), (e = ((i * this.RAD_TO_DEG + 360) >> 0) % 360);
  271. (o.rotation = i), (o.angle = e), this.onJoystickChanged && this.onJoystickChanged(o, s);
  272. }
  273. }
  274. clear(t) {
  275. const e = this.hooks;
  276. this.listenedNodes.forEach(t => {
  277. t.off(cc.Node.EventType.TOUCH_START, e.start),
  278. t.off(cc.Node.EventType.TOUCH_MOVE, e.move),
  279. t.off(cc.Node.EventType.TOUCH_END, e.end);
  280. }),
  281. (this.listenedNodes = null),
  282. t ? ((this.statusCache = null), (this.hooks = null)) : this.init();
  283. }
  284. onDestroy() {
  285. this.clear(!0);
  286. }
  287. };
  288. s([a({type: cc.Node, displayName: "Target"})], e.prototype, "target", void 0),
  289. s([a({displayName: "Tap"})], e.prototype, "enableTap", void 0),
  290. s([a({displayName: "Double Tap"})], e.prototype, "enableDoubleTap", void 0),
  291. s([a({displayName: ". . Tap Duration"})], e.prototype, "tapDuration", void 0),
  292. s([a({displayName: ". . Tap Radius"})], e.prototype, "tapRadius", void 0),
  293. s([a({displayName: "Hold"})], e.prototype, "enableHold", void 0),
  294. s([a({displayName: ". . Hold Delay"})], e.prototype, "holdDelay", void 0),
  295. s([a({displayName: "Pan"})], e.prototype, "enablePan", void 0),
  296. s([a({displayName: "Swipe"})], e.prototype, "enableSwipe", void 0),
  297. s([a({displayName: ". . Swipe Delay"})], e.prototype, "swipeDuration", void 0),
  298. s([a({displayName: ". . Swipe min Distance"})], e.prototype, "swipeMinDistance", void 0),
  299. s([a({displayName: ". . Swipe max Distance"})], e.prototype, "swipeMaxDistance", void 0),
  300. s([a({displayName: "Joystick"})], e.prototype, "enableJoystick", void 0),
  301. s([a({displayName: ". . Joystick Radius"})], e.prototype, "joystickRadius", void 0),
  302. s([a({displayName: ". . Float Stick"})], e.prototype, "floatStick", void 0),
  303. (e = s([o], e)),
  304. (i.default = e);