diff --git a/README.md b/README.md index 2f5ac5a3..6406ed5c 100644 --- a/README.md +++ b/README.md @@ -169,67 +169,85 @@ sendData({ ### `keymapping` - #### `setConfig` + supply a config for keymapping + ```js { - dpad:[{ - keys: { - z: { - initialX: 20, - initialY: 80, - distanceX: 0, - distanceY: -10, + dPad:[{ + keys:[ + { + key: 'w', + effect: { + initialX: 20, + initialY: 80, + distanceX: 0, + distanceY: -10, + }, + name: 'up', description: 'move up', }, - s: { - initialX: 20, - initialY: 80, - distanceX: 0, - distanceY: 10, + { + key: 's', + effect: { + initialX: 20, + initialY: 80, + distanceX: 0, + distanceY: 10, + }, + name: 'down', description: 'move down', }, - q: { - initialX: 20, - initialY: 80, - distanceX: -10, - distanceY: 0, + { + key: 'a', + effect: { + initialX: 20, + initialY: 80, + distanceX: -10, + distanceY: 0, + }, + name: 'left', description: 'move left', }, - d: { - initialX: 20, - initialY: 80, - distanceX: 10, - distanceY: 0, + { + key: 'd', + effect: { + initialX: 20, + initialY: 80, + distanceX: 10, + distanceY: 0, + }, + name: 'up', description: 'move right', }, - }, + + ], name: 'character movement', description: 'left joystick used to move the character', }], tap:[{ - key: { - p: { - initialX: 50, - initialY: 50, - }, - } + key: 'p', + effect: { + initialX: 50, + initialY: 50, + }, name:'Fire' }], swipe: [{ - key: { - u: { - initialX: 50, - initialY: 50, - distanceX: -10, - distanceY: 0, - description: 'swipe left', - }, - } - name:'Left dodge' + key: 'u', + effect: { + initialX: 50, + initialY: 50, + distanceX: -10, + distanceY: 0, + description: 'swipe left', + }, + name:'Left dodge', description: 'Dodge on the left' }] } ``` + - #### `activeKeyMappingDebug` helper to create the config mapping diff --git a/index.d.ts b/index.d.ts index cca5fc87..dace9f96 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,8 +10,8 @@ interface KeyEffect { description?: string; } -interface KeysMap { - keys: Record; +interface KeyList { + keys: Key[]; name?: string; description?: string; } @@ -24,7 +24,7 @@ interface Key { } interface KeyMappingConfig { - dpad?: KeysMap[]; + dpad?: KeyList[]; tap?: Key[]; swipe?: Key[]; } diff --git a/src/plugins/KeyboardMapping.js b/src/plugins/KeyboardMapping.js index 0d057bed..bc36eef8 100644 --- a/src/plugins/KeyboardMapping.js +++ b/src/plugins/KeyboardMapping.js @@ -225,11 +225,11 @@ module.exports = class KeyboardMapping { gestureConfig.forEach((gesture) => { const groupId = generateUID(); this.sequences[groupId] = []; - const keyName = gestureType === 'dPad' ? 'keys' : 'key'; - Object.entries(gesture[keyName]).forEach(([key, value]) => { - this.state.workingMappedKeysConfig[key] = { - ...value, - key, + const gestureObject = gestureType === 'dPad' ? gesture.keys : gestureConfig; + Object.values(gestureObject).forEach((value) => { + this.state.workingMappedKeysConfig[value.key] = { + ...value.effect, + key: value.key, type: gestureType, name: gesture.name, groupId, @@ -342,8 +342,8 @@ module.exports = class KeyboardMapping { generateTouchSequence(key) { const groupId = this.state.workingMappedKeysConfig[key].groupId; - const x = this.state.workingMappedKeysConfig[key].x; - const y = this.state.workingMappedKeysConfig[key].y; + const x = this.state.workingMappedKeysConfig[key].initialX; + const y = this.state.workingMappedKeysConfig[key].initialY; this.sequences[groupId].push({ type: 'MULTI_TOUCH',