From d2e3bfdb368d2e0eb56c954b0cf2b846eabaead6 Mon Sep 17 00:00:00 2001 From: Sam Rang Date: Sun, 17 Apr 2016 21:01:56 -0500 Subject: [PATCH] keymaps --- config-serializer/config-items/KeyMap.ts | 60 +++++++++++++++++++ config-serializer/config-items/KeyMaps.ts | 11 ++++ .../config-items/config-items.ts | 2 + config-serializer/test-serializer.ts | 6 +- config-serializer/uhk-config.json | 43 ++++++++++++- 5 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 config-serializer/config-items/KeyMap.ts create mode 100644 config-serializer/config-items/KeyMaps.ts diff --git a/config-serializer/config-items/KeyMap.ts b/config-serializer/config-items/KeyMap.ts new file mode 100644 index 00000000..d1ed5d53 --- /dev/null +++ b/config-serializer/config-items/KeyMap.ts @@ -0,0 +1,60 @@ +class KeyMap extends Serializable { + + static defaultFlag = 0x80; + + // @assertUInt8 + id: number; + + name: string; + + abbreviation: string; + + isDefault: boolean; + + layers: Serializable; + + _fromJsObject(jsObject: any): KeyMap { + this.id = jsObject.id; + this.isDefault = jsObject.isDefault; + this.abbreviation = jsObject.abbreviation; + this.name = jsObject.name; + this.layers = new Layers().fromJsObject(jsObject.layers); + return this; + } + + _fromBinary(buffer: UhkBuffer): KeyMap { + let map = buffer.readUInt8(); + /* saves almost a byte but limits number of keymaps... */ + this.isDefault = (map & KeyMap.defaultFlag) !== 0; + this.id = map & ~KeyMap.defaultFlag; // Clear isDefault bit.; + this.abbreviation = buffer.readString(); + this.name = buffer.readString(); + this.layers = new Layers().fromBinary(buffer); + return this; + } + + _toJsObject(): any { + return { + id: this.id, + isDefault: this.isDefault, + abbreviation: this.abbreviation, + name: this.name, + layers: this.layers.toJsObject() + }; + } + + _toBinary(buffer: UhkBuffer): void { + buffer.writeUInt8(this.id | this.getDefaultFlag()); + buffer.writeString(this.abbreviation); + buffer.writeString(this.name); + this.layers.toBinary(buffer); + } + + toString(): string { + return ``; + } + + private getDefaultFlag() { + return this.isDefault ? KeyMap.defaultFlag : 0; + } +} diff --git a/config-serializer/config-items/KeyMaps.ts b/config-serializer/config-items/KeyMaps.ts new file mode 100644 index 00000000..354ce359 --- /dev/null +++ b/config-serializer/config-items/KeyMaps.ts @@ -0,0 +1,11 @@ +class KeyMaps extends ClassArray { + + jsObjectToClass(jsObject: any): Serializable { + return new KeyMap().fromJsObject(jsObject); + } + + binaryToClass(buffer: UhkBuffer): Serializable { + return new KeyMap().fromBinary(buffer); + } + +} diff --git a/config-serializer/config-items/config-items.ts b/config-serializer/config-items/config-items.ts index b4f4cece..62fa52fd 100644 --- a/config-serializer/config-items/config-items.ts +++ b/config-serializer/config-items/config-items.ts @@ -13,3 +13,5 @@ /// /// /// +/// +/// diff --git a/config-serializer/test-serializer.ts b/config-serializer/test-serializer.ts index 78f2f61d..50f0791e 100644 --- a/config-serializer/test-serializer.ts +++ b/config-serializer/test-serializer.ts @@ -10,8 +10,8 @@ let fs = require('fs'); let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json')); -let config1Js = uhkConfig.keymaps[0].layers; -let config1Ts: Serializable = new Layers().fromJsObject(config1Js); +let config1Js = uhkConfig.keymaps; +let config1Ts: Serializable = new KeyMaps().fromJsObject(config1Js); let config1Buffer = new UhkBuffer(); config1Ts.toBinary(config1Buffer); let config1BufferContent = config1Buffer.getBufferContent(); @@ -19,7 +19,7 @@ fs.writeFileSync('uhk-config.bin', config1BufferContent); config1Buffer.offset = 0; console.log(); -let config2Ts = new Layers().fromBinary(config1Buffer); +let config2Ts = new KeyMaps().fromBinary(config1Buffer); console.log('\n'); let config2Js = config2Ts.toJsObject(); let config2Buffer = new UhkBuffer(); diff --git a/config-serializer/uhk-config.json b/config-serializer/uhk-config.json index c562c2e1..d00e7d1a 100644 --- a/config-serializer/uhk-config.json +++ b/config-serializer/uhk-config.json @@ -148,7 +148,48 @@ }, { "id": 1, - "name": "Dvorak" + "isDefault": false, + "abbreviation": "VIM", + "name": "VIM", + "layers": [ + { + "modules": [ + { + "id": 0, + "pointerRole": "move", + "keyActions": [ + { + "keyActionType": "mouse", + "mouseAction": "scrollDown" + }, + { + "keyActionType": "playMacro", + "macroId": 0 + }, + { + "keyActionType": "switchKeymap", + "keymapId": 1 + } + ] + }, + { + "id": 1, + "pointerRole": "scroll", + "keyActions": [] + }, + { + "id": 2, + "pointerRole": "move", + "keyActions": [ + { + "keyActionType": "keystroke", + "scancode": 111 + } + ] + } + ] + } + ] } ], "macros": [