Skip to content

Commit

Permalink
V4 adding dynamic configFile + big opti on number of dPad event
Browse files Browse the repository at this point in the history
  • Loading branch information
jparez committed Jun 21, 2024
1 parent 539eac2 commit f4cdb19
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions src/plugins/KeyboardMapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ const MINECRAFT = {
},
},
],
tap: [
{
keys: {
r: {
x: 93,
y: 30,
description: 'jump',
},
},
name: 'jump',
description: 'jump',
},
],
};

const SUBWAY_SURFERS = {
Expand Down Expand Up @@ -193,6 +206,8 @@ module.exports = class KeyboardMapping {

this.keyboardCallbacks = [];

this.dPadPushed = [];

this.state = new Proxy(
{
isActive: false,
Expand Down Expand Up @@ -228,22 +243,7 @@ module.exports = class KeyboardMapping {
);
this.instance.sendEvent(jsonForReleaseTouch);
} else {
const reversedCurrentlyPressedKeys = value.reverse();
const {touchPoints, movePoints} =
this.generateTouchEventForPush(reversedCurrentlyPressedKeys);
const json = {type: 'MULTI_TOUCH', nb: 0, mode: 0, points: []};
if (touchPoints.length) {
json.nb = touchPoints.length;
json.points = touchPoints;
this.instance.sendEvent(json);
}

if (movePoints.length) {
json.nb = movePoints.length;
json.points = movePoints;
json.mode = 2;
this.instance.sendEvent(json);
}
this.sendMultiTouch();
}
break;
default:
Expand Down Expand Up @@ -277,7 +277,7 @@ module.exports = class KeyboardMapping {
(config) => {
// check it's a valid JSON
try {
JSON.parse(JSON.stringify(config));
this.state.mappedKeysConfig = JSON.parse(JSON.stringify(config));
} catch (err) {
throw new Error('Invalid JSON');
}
Expand Down Expand Up @@ -308,8 +308,29 @@ module.exports = class KeyboardMapping {
);

// load default config to test purpose TODO delete for production
this.state.mappedKeysConfig = MINECRAFT;
this.state.mappedKeysConfig = NEW_SOUL_KNIGHT;

}

sendMultiTouch() {
const reversedCurrentlyPressedKeys = this.state.currentlyPressedKeys.reverse();
const {touchPoints, movePoints} = this.generateTouchEventForPush(reversedCurrentlyPressedKeys);

const json = {type: 'MULTI_TOUCH', nb: 0, mode: 0, points: []};
if (touchPoints.length) {
json.nb = touchPoints.length;
json.points = touchPoints;
this.instance.sendEvent(json);
}

if (movePoints.length) {
json.nb = movePoints.length;
json.points = movePoints;
json.mode = 2;
this.instance.sendEvent(json);
}
}

renderToolbarButton() {
const toolbars = this.instance.getChildByClass(this.instance.root, 'gm-toolbar');
if (!toolbars) {
Expand Down Expand Up @@ -339,6 +360,9 @@ module.exports = class KeyboardMapping {
}

setupMappedKeysConfig() {
// reset
this.state.workingMappedKeysConfig = {};

// Create a working version of the mappedKeysConfig, this will chane the structure to be more performant and avoid incepth loop
Object.entries(this.state.mappedKeysConfig).forEach(([gestureType, gestureConfig]) => {
gestureConfig.forEach((gesture) => {
Expand Down Expand Up @@ -392,6 +416,12 @@ module.exports = class KeyboardMapping {
(pressedKey) => pressedKey !== key,
);
}
// Remove the dPadPushed key if in dPadPushed

const keyConfig = this.state.workingMappedKeysConfig[key];
if (keyConfig && keyConfig.type === 'dPad') {
this.dPadPushed = this.dPadPushed.filter((groupId) => groupId !== keyConfig.groupId);
}
}

/**
Expand All @@ -407,6 +437,7 @@ module.exports = class KeyboardMapping {
const dPadAlreadyTriggered = [];

this.state.currentlyPressedKeys.forEach((key) => {
// All keys in currentPressedKeys for a given dPad are calculate once, as a single touch event
if (dPadAlreadyTriggered.includes(key)) {
return;
}
Expand All @@ -422,18 +453,24 @@ module.exports = class KeyboardMapping {
// eslint-disable-next-line no-case-declarations
const [dPadT, dPadM] = this.getDPADTouchEvent(keyConfig);

if (this.dPadPushed.includes(keyConfig.groupId)) {
touchPoints.push(dPadT);
} else {
touchPointsBeforeMove.push(dPadT);
movePoints.push(dPadM);
// also add the groupId to the dPadPushed array to keep track of the dPad pressed
this.dPadPushed.push(keyConfig.groupId);
}
break;
case 'tap':
touchPoints.push(this.getTapTouchEvent(keyConfig));
break;
case 'swipe':
// eslint-disable-next-line no-case-declarations
const [swipeT, swipem] = this.getSwipeTouchEvent(keyConfig);
const [swipeT, swipeM] = this.getSwipeTouchEvent(keyConfig);

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

break;
default:
Expand All @@ -444,7 +481,7 @@ module.exports = class KeyboardMapping {
// adding touchpoins to movePoints to keep static touch when moving
return {
touchPoints: [...touchPointsBeforeMove, ...touchPoints],
movePoints: [...touchPoints, ...movePoints],
movePoints: movePoints.length ? [...touchPoints, ...movePoints] : [],
};
}

Expand Down Expand Up @@ -474,6 +511,9 @@ module.exports = class KeyboardMapping {
const touchPointsBeforeMove = this.calculateCoorFromPercent(keyConfig.initialX, keyConfig.initialY);
const movePoints = this.calculateCoorFromPercent(newPosition.x, newPosition.y);

if (this.dPadPushed.includes(keyConfig.groupId)) {
return [movePoints];
}
return [touchPointsBeforeMove, movePoints];
}
getTapTouchEvent(keyConfig) {
Expand Down

0 comments on commit f4cdb19

Please sign in to comment.