InputHandler.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. var t = require;
  2. var e = module;
  3. var a = exports;
  4. e =
  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(a, "__esModule", {value: !0}), (a.InputType = void 0);
  17. const i = t("Toucher"),
  18. s = t("TouchPinch"),
  19. {ccclass: o, property: n} = cc._decorator;
  20. let r = class extends cc.Component {
  21. constructor() {
  22. super(...arguments),
  23. (this.inputNode = null),
  24. (this.stickBaseNode = null),
  25. (this.stickNode = null),
  26. (this.screenCoord = !0),
  27. (this.ignoreInput = !1);
  28. }
  29. onLoad() {
  30. (this.inputNode = this.inputNode || this.node),
  31. (this._eventTarget = new cc.EventTarget()),
  32. this.initToucher(),
  33. this.initKeyboard(),
  34. this.initMouse();
  35. }
  36. initToucher() {
  37. (cc.macro.TOUCH_TIMEOUT = 120),
  38. (this.touchFingers = 0),
  39. (this.toucher = this.inputNode.getComponent(i.default));
  40. const t = this.toucher;
  41. t.init(), t.registerEvents(this.inputNode, !1), t.registerEventsOnChildren(this.inputNode, !1);
  42. const o = new s.default();
  43. (t.onStart = (t, e, i) => {
  44. var s = i.target;
  45. (this.touchFingers = this.getFingerCount("onStart")),
  46. 1 === this.touchFingers
  47. ? (o.reset(), o.onTouchStart(i))
  48. : 2 === this.touchFingers
  49. ? o.onTouchStart(i)
  50. : o.reset(),
  51. this._eventTarget.emit(a.InputType.TOUCH_START, t, e, s);
  52. }),
  53. (t.onMove = (t, e, i) => {
  54. i = i.target;
  55. (this.touchFingers = this.getFingerCount("onMove")),
  56. 2 === this.touchFingers &&
  57. o.onPinch() &&
  58. (this.screenCoord && (o.pinchCenter.y = cc.visibleRect.height - o.pinchCenter.y),
  59. this._eventTarget.emit(a.InputType.PINCH, o.pinchCenter, o.lastPinchDist, o.pinchDist, i));
  60. }),
  61. (t.onEnd = (t, e, i) => {
  62. var s = i.target;
  63. (this.touchFingers = this.getFingerCount("onEnd")),
  64. 0 === this.touchFingers ? o.reset() : o.onTouchEnd(i),
  65. this.screenCoord && (e = cc.visibleRect.height - e),
  66. this._eventTarget.emit(a.InputType.TOUCH_END, t, e, s);
  67. }),
  68. (t.onPan = (t, e, i, s, o) => {
  69. o = o.target;
  70. 1 === this.touchFingers &&
  71. (this.screenCoord && ((e = cc.visibleRect.height - e), (s = -s)),
  72. this._eventTarget.emit(a.InputType.PAN, t, e, i, s, o));
  73. }),
  74. (t.onTap = (t, e, i) => {
  75. this.touchFingers = this.getFingerCount("onTap");
  76. i = i.target;
  77. this._eventTarget.emit(a.InputType.TAP, t, e, i);
  78. }),
  79. (t.holdStart = (t, e, i) => {
  80. i = i.target;
  81. (this.touchFingers = this.getFingerCount("holdStart")),
  82. 1 === this.touchFingers &&
  83. (this.screenCoord && (e = cc.visibleRect.height - e),
  84. this._eventTarget.emit(a.InputType.HOLD_START, t, e, i));
  85. }),
  86. (t.holdEnd = (t, e, i) => {
  87. this.touchFingers = this.getFingerCount("holdEnd");
  88. i = i.target;
  89. this.screenCoord && (e = cc.visibleRect.height - e),
  90. this._eventTarget.emit(a.InputType.HOLD_END, t, e, i);
  91. }),
  92. (t.onJoystickChanged = (t, e) => {
  93. const i = t.pressed,
  94. s = this.stickBaseNode,
  95. o = this.stickNode;
  96. i &&
  97. ((s.width = 2 * t.radius),
  98. (s.height = 2 * t.radius),
  99. s.setPosition(t.centerX - this.inputNode.width / 2, t.centerY - this.inputNode.height / 2),
  100. o.setPosition(t.x - this.inputNode.width / 2, t.y - this.inputNode.height / 2)),
  101. (o.active = i),
  102. (s.active = i),
  103. this.screenCoord && (t.rotation = -t.rotation);
  104. e = e.target;
  105. t.pressed && t.distance, this._eventTarget.emit(a.InputType.JOYSTICK, t, e);
  106. });
  107. }
  108. addEventListener(t, e, i, s) {
  109. this._eventTarget.on(t, e, i, s);
  110. }
  111. addEventListenerOnce(t, e, i, s) {
  112. this._eventTarget.once(t, e, i, s);
  113. }
  114. removeEventListener(t, e, i) {
  115. this._eventTarget.off(t, e, i);
  116. }
  117. onTouchStart(t, e, i) {
  118. this._eventTarget.on(a.InputType.TOUCH_START, t, e, i);
  119. }
  120. onTouchEnd(t, e, i) {
  121. this._eventTarget.on(a.InputType.TOUCH_END, t, e, i);
  122. }
  123. onPan(t, e, i) {
  124. this._eventTarget.on(a.InputType.PAN, t, e, i);
  125. }
  126. onTap(t, e, i) {
  127. this._eventTarget.on(a.InputType.TAP, t, e, i);
  128. }
  129. onHoldStart(t, e, i) {
  130. this._eventTarget.on(a.InputType.HOLD_START, t, e, i);
  131. }
  132. onHoldEnd(t, e, i) {
  133. this._eventTarget.on(a.InputType.HOLD_END, t, e, i);
  134. }
  135. onPinch(t, e, i) {
  136. this._eventTarget.on(a.InputType.PINCH, t, e, i);
  137. }
  138. onJoystickChanged(t, e, i) {
  139. this._eventTarget.on(a.InputType.JOYSTICK, t, e, i);
  140. }
  141. onKeyDown(t) {
  142. this._eventTarget.on(a.InputType.KEY_DOWN, t);
  143. }
  144. onKeyUp(t) {
  145. this._eventTarget.on(a.InputType.KEY_UP, t);
  146. }
  147. onMouseWheel(t) {
  148. this._eventTarget.on(a.InputType.MOUSE_WHEEL, t);
  149. }
  150. offTouchStart(t, e) {
  151. this._eventTarget.off(a.InputType.TOUCH_START, t, e);
  152. }
  153. offTouchEnd(t, e) {
  154. this._eventTarget.off(a.InputType.TOUCH_END, t, e);
  155. }
  156. offPan(t, e) {
  157. this._eventTarget.off(a.InputType.PAN, t, e);
  158. }
  159. offTap(t, e) {
  160. this._eventTarget.off(a.InputType.TAP, t, e);
  161. }
  162. offHoldStart(t, e) {
  163. this._eventTarget.off(a.InputType.HOLD_START, t, e);
  164. }
  165. offHoldEnd(t, e) {
  166. this._eventTarget.off(a.InputType.HOLD_END, t, e);
  167. }
  168. offPinch(t, e) {
  169. this._eventTarget.off(a.InputType.PINCH, t, e);
  170. }
  171. offJoystickChanged(t, e) {
  172. this._eventTarget.off(a.InputType.JOYSTICK, t, e);
  173. }
  174. offKeyDown(t) {
  175. this._eventTarget.off(a.InputType.KEY_DOWN, t);
  176. }
  177. offKeyUp(t) {
  178. this._eventTarget.off(a.InputType.KEY_UP, t);
  179. }
  180. offMouseWheel(t) {
  181. this._eventTarget.off(a.InputType.MOUSE_WHEEL, t);
  182. }
  183. initKeyboard() {
  184. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.keyDownListener, this),
  185. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.keyUpListener, this);
  186. }
  187. initMouse() {
  188. cc.systemEvent.on(cc.Node.EventType.MOUSE_WHEEL, this.mouseWheelListener, this);
  189. }
  190. getFingerCount(t) {
  191. return cc.internal.inputManager.getGlobalTouchCount();
  192. }
  193. keyDownListener(t) {
  194. this._eventTarget.emit(a.InputType.KEY_DOWN, t);
  195. }
  196. keyUpListener(t) {
  197. this._eventTarget.emit(a.InputType.KEY_UP, t);
  198. }
  199. mouseWheelListener(t) {
  200. this._eventTarget.emit(a.InputType.MOUSE_WHEEL, t);
  201. }
  202. lateUpdate(t) {}
  203. onDestroy() {
  204. this._eventTarget.clear(),
  205. (this._eventTarget = null),
  206. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.keyDownListener, this),
  207. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.keyUpListener, this),
  208. cc.systemEvent.off(cc.Node.EventType.MOUSE_WHEEL, this.mouseWheelListener, this);
  209. }
  210. };
  211. e([n(cc.Node)], r.prototype, "inputNode", void 0),
  212. e([n(cc.Node)], r.prototype, "stickBaseNode", void 0),
  213. e([n(cc.Node)], r.prototype, "stickNode", void 0),
  214. e([n()], r.prototype, "screenCoord", void 0),
  215. (r = e([o], r)),
  216. (a.default = r),
  217. (r.prototype.on = r.prototype.addEventListener),
  218. (r.prototype.once = r.prototype.addEventListenerOnce),
  219. (r.prototype.off = r.prototype.removeEventListener),
  220. (a.InputType = {
  221. TOUCH_START: "touchStart",
  222. TOUCH_END: "touchEnd",
  223. PAN: "pan",
  224. TAP: "tap",
  225. HOLD_START: "holdStart",
  226. HOLD_END: "holdEnd",
  227. PINCH: "pinch",
  228. JOYSTICK: "joystick",
  229. KEY_DOWN: "keyDown",
  230. KEY_UP: "keyUP",
  231. MOUSE_WHEEL: "mouseWheel"
  232. });