123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- var t = require;
- var e = module;
- var i = exports;
- var s =
- (this && this.__decorate) ||
- function (t, e, i, s) {
- var o,
- a = arguments.length,
- n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
- if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
- else
- for (var r = t.length - 1; 0 <= r; r--)
- (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
- return 3 < a && n && Object.defineProperty(e, i, n), n;
- };
- Object.defineProperty(i, "__esModule", {value: !0});
- const {ccclass: o, property: a} = cc._decorator;
- e = class extends cc.Component {
- constructor() {
- super(...arguments),
- (this.target = null),
- (this.enableTap = !0),
- (this.enableDoubleTap = !1),
- (this.tapDuration = 300),
- (this.tapRadius = 30),
- (this.enableHold = !1),
- (this.holdDelay = 400),
- (this.enablePan = !1),
- (this.enableSwipe = !1),
- (this.swipeDuration = 1e3),
- (this.swipeMinDistance = 90),
- (this.swipeMaxDistance = 1e3),
- (this.enableJoystick = !1),
- (this.joystickRadius = 100),
- (this.floatStick = !1),
- (this.disabled = !1),
- (this.onStart = null),
- (this.onMove = null),
- (this.onEnd = null),
- (this.onTap = null),
- (this.onDoubleTap = null),
- (this.holdStart = null),
- (this.holdEnd = null),
- (this.onPan = null),
- (this.onSwipeUp = null),
- (this.onSwipeDown = null),
- (this.onSwipeLeft = null),
- (this.onSwipeRight = null),
- (this.onJoystickChanged = null),
- (this.touchedCount = 0),
- (this.RAD_TO_DEG = 180 / Math.PI),
- (this.DEG_TO_RAD = Math.PI / 180);
- }
- onLoad() {
- this.init();
- }
- init() {
- (this.statusCache = {}),
- (this.listenedNodes = []),
- this.resetStatus(),
- this.target && this.registerEvents(this.target);
- }
- resetStatus() {
- (this.statusCache = {}), (this.hooks = {});
- }
- getTouchStatus(t) {
- return this.statusCache[t];
- }
- removeTouchStatus(t) {
- delete this.statusCache[t];
- }
- createTouchStatus(t) {
- var e = {
- id: t,
- event: null,
- target: null,
- touchedID: -1,
- startTime: 0,
- startScreenPos: null,
- endTime: 0,
- endScreenPos: null,
- screenPos: null,
- _holdTimeoutId: null,
- holding: !1,
- _prevTapPos: null,
- _prevTapTime: 0,
- _tapIndex: 0,
- joystick: null
- };
- return (this.statusCache[t] = e);
- }
- createJoystickStatus() {
- return {
- x: 0,
- y: 0,
- centerX: 0,
- centerY: 0,
- rotation: 0,
- angle: 0,
- distance: 0,
- radius: this.joystickRadius,
- pressed: !1
- };
- }
- resetEventStatus(t) {
- null !== t._holdTimeoutId && (clearTimeout(t._holdTimeoutId), (t._holdTimeoutId = null)),
- (t.holding = !1),
- (t.touchedID = -1),
- (t.event = null),
- (t.joystick = null),
- (t.startTime = 0),
- (t.startScreenPos = null),
- (t.endTime = 0),
- (t.endScreenPos = null),
- (t.screenPos = null);
- }
- registerEvents(o, t = !1, u = !0) {
- this.listenedNodes.push(o);
- const p = this.tapRadius * this.tapRadius,
- e = i => {
- if ((this.touchedCount++, !0 !== this.disabled)) {
- const s = i.getID(),
- e = this.createTouchStatus(s);
- (e.target = o), (e.touchedID = i.getID()), (e.event = i), (e.startTime = Date.now());
- var t = i.getLocation();
- (e.screenPos = t),
- (e.startScreenPos = t),
- this.onStart && this.onStart(t.x, t.y, e),
- this.enableHold &&
- this.holdStart &&
- 0 < this.holdDelay &&
- (e._holdTimeoutId = setTimeout(() => {
- if (this.isValid) {
- const e = this.getTouchStatus(s);
- var t;
- e &&
- ((t = i.getLocation()),
- (e.screenPos = t),
- (e.holding = !0),
- this.holdStart(t.x, t.y, e));
- }
- }, this.holdDelay)),
- this.enableJoystick && this.resetJoystick(t.x, t.y, !0, e),
- u && i.stopPropagation();
- }
- };
- o.on(cc.Node.EventType.TOUCH_START, e, null, t);
- var i = t => {
- if (!0 !== this.disabled) {
- const o = t.getID(),
- a = this.getTouchStatus(o);
- var e, i, s;
- a &&
- ((e = (a.event = t).getDeltaX()),
- (i = t.getDeltaY()),
- (0 === e && 0 === i) ||
- ((s = t.getLocation()),
- (a.screenPos = s),
- this.onMove && this.onMove(s.x, s.y, a),
- this.enableJoystick && this.moveJoystick(s.x, s.y, a),
- this.enablePan && this.onPan && this.onPan(s.x, s.y, e, i, a),
- u && t.stopPropagation()));
- }
- };
- o.on(cc.Node.EventType.TOUCH_MOVE, i, null, t);
- var s = e => {
- this.touchedCount--;
- const i = e.getID(),
- s = this.getTouchStatus(i);
- if (s)
- if (
- ((s.event = e),
- null !== s._holdTimeoutId && (clearTimeout(s._holdTimeoutId), (s._holdTimeoutId = null)),
- !0 !== this.disabled)
- ) {
- const o = Date.now();
- s.endTime = o;
- const a = e.getLocation();
- (s.screenPos = a),
- (s.endScreenPos = a),
- this.onEnd && this.onEnd(a.x, a.y, s),
- s.holding && this.holdEnd && ((s.holding = !1), this.holdEnd(a.x, a.y, s));
- const n = s.startTime,
- r = s.startScreenPos,
- l = o - n,
- h = a.x - r.x,
- c = a.y - r.y,
- d = h * h + c * c;
- let t = !1;
- if (
- this.enableDoubleTap &&
- this.onDoubleTap &&
- l < this.tapDuration &&
- d < p &&
- 1 === s._tapIndex
- ) {
- const e = n - s._prevTapTime,
- i = s._prevTapPos.x,
- u = s._prevTapPos.y,
- o = r.x - i,
- a = r.y - u,
- l = o * o + a * a;
- e < this.tapDuration && l < p && ((t = !0), this.onDoubleTap(i, u, s)), (s._tapIndex = 0);
- }
- if (
- (s._tapIndex++,
- this.onTap(r.x, r.y, s),
- (s._prevTapPos = r),
- this.enableTap &&
- !t &&
- this.onTap &&
- l < this.tapDuration &&
- d < this.tapRadius * this.tapRadius)
- )
- s._prevTapTime = o;
- else if (this.enableSwipe && l < this.swipeDuration) {
- const e = Math.sqrt(d);
- if (e > this.swipeMinDistance && e < this.swipeMaxDistance) {
- const i = Math.abs(c) > Math.abs(h),
- u = e / l;
- i && 0 < c
- ? this.onSwipeUp && this.onSwipeUp(u, s)
- : i
- ? this.onSwipeDown && this.onSwipeDown(u, s)
- : !i && 0 < h
- ? this.onSwipeRight && this.onSwipeRight(u, s)
- : this.onSwipeLeft && this.onSwipeLeft(u, s),
- (s._tapIndex = 0);
- }
- }
- this.enableJoystick && this.resetJoystick(null, null, !1, s),
- this.removeTouchStatus(i),
- u && e.stopPropagation();
- } else this.removeTouchStatus(i);
- };
- o.on(cc.Node.EventType.TOUCH_END, s, null, t),
- o.on(cc.Node.EventType.TOUCH_CANCEL, s, null, t),
- (this.hooks = {start: e, move: i, end: s});
- }
- registerEventsOnChildren(t, e = !1, i = !0) {
- t.children.forEach(t => {
- this.registerEvents(t, e, i);
- });
- }
- resetJoystick(t, e, i, s) {
- const o = (s.joystick = this.createJoystickStatus());
- t || 0 === t ? (o.centerX = t) : (t = o.centerX),
- e || 0 === e ? (o.centerY = e) : (e = o.centerY),
- (o.x = t),
- (o.y = e),
- (o.distance = 0),
- (o.angle = 0),
- (o.pressed = !!i),
- this.onJoystickChanged && this.onJoystickChanged(o, s);
- }
- moveJoystick(e = 0, i = 0, s) {
- const o = s.joystick;
- if (o) {
- (o.x = e), (o.y = i);
- (e = o.x - o.centerX), (i = o.y - o.centerY);
- let t = Math.sqrt(e * e + i * i) >> 0;
- if (((o.distance = t), t > o.radius)) {
- const a = o.radius / t;
- this.floatStick
- ? ((o.centerX = o.x - e * a), (o.centerY = o.y - i * a))
- : ((o.x = o.centerX + e * a), (o.y = o.centerY + i * a)),
- (t = o.radius);
- }
- o.distance = t;
- (i = Math.atan2(i, e)), (e = ((i * this.RAD_TO_DEG + 360) >> 0) % 360);
- (o.rotation = i), (o.angle = e), this.onJoystickChanged && this.onJoystickChanged(o, s);
- }
- }
- clear(t) {
- const e = this.hooks;
- this.listenedNodes.forEach(t => {
- t.off(cc.Node.EventType.TOUCH_START, e.start),
- t.off(cc.Node.EventType.TOUCH_MOVE, e.move),
- t.off(cc.Node.EventType.TOUCH_END, e.end);
- }),
- (this.listenedNodes = null),
- t ? ((this.statusCache = null), (this.hooks = null)) : this.init();
- }
- onDestroy() {
- this.clear(!0);
- }
- };
- s([a({type: cc.Node, displayName: "Target"})], e.prototype, "target", void 0),
- s([a({displayName: "Tap"})], e.prototype, "enableTap", void 0),
- s([a({displayName: "Double Tap"})], e.prototype, "enableDoubleTap", void 0),
- s([a({displayName: ". . Tap Duration"})], e.prototype, "tapDuration", void 0),
- s([a({displayName: ". . Tap Radius"})], e.prototype, "tapRadius", void 0),
- s([a({displayName: "Hold"})], e.prototype, "enableHold", void 0),
- s([a({displayName: ". . Hold Delay"})], e.prototype, "holdDelay", void 0),
- s([a({displayName: "Pan"})], e.prototype, "enablePan", void 0),
- s([a({displayName: "Swipe"})], e.prototype, "enableSwipe", void 0),
- s([a({displayName: ". . Swipe Delay"})], e.prototype, "swipeDuration", void 0),
- s([a({displayName: ". . Swipe min Distance"})], e.prototype, "swipeMinDistance", void 0),
- s([a({displayName: ". . Swipe max Distance"})], e.prototype, "swipeMaxDistance", void 0),
- s([a({displayName: "Joystick"})], e.prototype, "enableJoystick", void 0),
- s([a({displayName: ". . Joystick Radius"})], e.prototype, "joystickRadius", void 0),
- s([a({displayName: ". . Float Stick"})], e.prototype, "floatStick", void 0),
- (e = s([o], e)),
- (i.default = e);
|