This commit is contained in:
Sam Rang
2016-04-17 21:01:56 -05:00
parent 3c18e7cf86
commit d2e3bfdb36
5 changed files with 118 additions and 4 deletions

View File

@@ -0,0 +1,60 @@
class KeyMap extends Serializable<KeyMap> {
static defaultFlag = 0x80;
// @assertUInt8
id: number;
name: string;
abbreviation: string;
isDefault: boolean;
layers: Serializable<Layers>;
_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 `<KeyMap id="${this.id}" name="${this.name}">`;
}
private getDefaultFlag() {
return this.isDefault ? KeyMap.defaultFlag : 0;
}
}

View File

@@ -0,0 +1,11 @@
class KeyMaps extends ClassArray<KeyMap> {
jsObjectToClass(jsObject: any): Serializable<KeyMap> {
return new KeyMap().fromJsObject(jsObject);
}
binaryToClass(buffer: UhkBuffer): Serializable<KeyMap> {
return new KeyMap().fromBinary(buffer);
}
}

View File

@@ -13,3 +13,5 @@
/// <reference path="Modules.ts" />
/// <reference path="Layer.ts" />
/// <reference path="Layers.ts" />
/// <reference path="KeyMap.ts" />
/// <reference path="KeyMaps.ts" />

View File

@@ -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<Layers> = new Layers().fromJsObject(config1Js);
let config1Js = uhkConfig.keymaps;
let config1Ts: Serializable<KeyMaps> = 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();

View File

@@ -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": [