Skip to content

Commit

Permalink
v4 swipe bind
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jun 21, 2024
1 parent e386615 commit 539eac2
Showing 1 changed file with 122 additions and 16 deletions.
138 changes: 122 additions & 16 deletions src/plugins/KeyboardMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const NEW_SOUL_KNIGHT = {
d: {
initialX: 20,
initialY: 80,
distanceX: +10,
distanceX: 10,
distanceY: 0,
description: 'move right',
},
Expand Down Expand Up @@ -76,6 +76,102 @@ const NEW_SOUL_KNIGHT = {
],
};

const MINECRAFT = {
dPad: [
{
keys: {
z: {
initialX: 15,
initialY: 64,
distanceX: 0,
distanceY: -15,
description: 'move up',
},
s: {
initialX: 15,
initialY: 64,
distanceX: 0,
distanceY: 15,
description: 'move down',
},
q: {
initialX: 15,
initialY: 64,
distanceX: -15,
distanceY: 0,
description: 'move left',
},
d: {
initialX: 15,
initialY: 64,
distanceX: 15,
distanceY: 0,
description: 'move right',
},
},
name: 'character movement',
description: 'dpad used for move the character',
},
],
swipe: [
{
keys: {
a: {
x: 50,
y: 50,
distanceX: -10,
distanceY: 0,
description: 'swipe left',
},
e: {
x: 50,
y: 50,
distanceX: 10,
distanceY: 0,
description: 'swipe right',
},
},
},
],
};

const SUBWAY_SURFERS = {
swipe: [
{
keys: {
q: {
x: 50,
y: 50,
distanceX: -10,
distanceY: 0,
description: 'swipe left',
},
d: {
x: 50,
y: 50,
distanceX: 10,
distanceY: 0,
description: 'swipe right',
},
z: {
x: 50,
y: 50,
distanceX: 0,
distanceY: -10,
description: 'swipe up',
},
s: {
x: 50,
y: 50,
distanceX: 0,
distanceY: 10,
description: 'swipe down',
},
},
},
],
};

/**
* Instance keyboard plugin.KeyboardMapping
* Translate and forward keyboard events to instance.
Expand Down Expand Up @@ -212,7 +308,7 @@ module.exports = class KeyboardMapping {
);

// load default config to test purpose TODO delete for production
this.state.mappedKeysConfig = NEW_SOUL_KNIGHT;
this.state.mappedKeysConfig = MINECRAFT;
}
renderToolbarButton() {
const toolbars = this.instance.getChildByClass(this.instance.root, 'gm-toolbar');
Expand Down Expand Up @@ -324,13 +420,21 @@ module.exports = class KeyboardMapping {
dPadAlreadyTriggered.push(...dPadGroupKeys);

// eslint-disable-next-line no-case-declarations
const [tbm, m] = this.getDPADTouchEvent(keyConfig);
const [dPadT, dPadM] = this.getDPADTouchEvent(keyConfig);

touchPointsBeforeMove.push(tbm);
movePoints.push(m);
touchPointsBeforeMove.push(dPadT);
movePoints.push(dPadM);
break;
case 'tap':
touchPoints.push(this.getTapTouchEvent(keyConfig));
break;
case 'swipe':
// eslint-disable-next-line no-case-declarations
const [swipeT, swipem] = this.getSwipeTouchEvent(keyConfig);

touchPointsBeforeMove.push(swipeT);
movePoints.push(swipem);

break;
default:
break;
Expand Down Expand Up @@ -376,6 +480,19 @@ module.exports = class KeyboardMapping {
return this.calculateCoorFromPercent(keyConfig.x, keyConfig.y);
}

getSwipeTouchEvent(keyConfig) {
const newPosition = {x: keyConfig.x, y: keyConfig.y};

const touchPointsBeforeMove = this.calculateCoorFromPercent(keyConfig.x, keyConfig.y);

newPosition.x += keyConfig.distanceX;
newPosition.y += keyConfig.distanceY;

const movePoints = this.calculateCoorFromPercent(newPosition.x, newPosition.y);

return [touchPointsBeforeMove, movePoints];
}

/**
* Bind all event listener callback.
*/
Expand Down Expand Up @@ -410,17 +527,6 @@ module.exports = class KeyboardMapping {
});
}

slide(startX, startY, stopX, stopY) {
this.instance.sendEvent({type: 'MOUSE_PRESS', x: startX, y: startY});
this.instance.sendEvent({type: 'MOUSE_MOVE', x: stopX, y: stopY});
this.instance.sendEvent({type: 'MOUSE_RELEASE', x: stopX, y: stopY});
}

doubleTap(x, y) {
this.tap(x, y);
this.tap(x, y);
}

tilt() {
const json = {
channel: 'sensors',
Expand Down

0 comments on commit 539eac2

Please sign in to comment.