layer and layers
This commit is contained in:
25
config-serializer/config-items/Layer.ts
Normal file
25
config-serializer/config-items/Layer.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
class Layer extends Serializable<Layer> {
|
||||||
|
|
||||||
|
modules: Serializable<Modules>;
|
||||||
|
|
||||||
|
_fromJsObject(jsObject: any): Layer {
|
||||||
|
this.modules = new Modules().fromJsObject(jsObject.modules);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_fromBinary(buffer: UhkBuffer): Layer {
|
||||||
|
this.modules = new Modules().fromBinary(buffer);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_toJsObject(): any {
|
||||||
|
return {
|
||||||
|
modules: this.modules.toJsObject()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
_toBinary(buffer: UhkBuffer): void {
|
||||||
|
this.modules.toBinary(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
config-serializer/config-items/Layers.ts
Normal file
11
config-serializer/config-items/Layers.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
class Layers extends ClassArray<Layer> {
|
||||||
|
|
||||||
|
jsObjectToClass(jsObject: any): Serializable<Layer> {
|
||||||
|
return new Layer().fromJsObject(jsObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
binaryToClass(buffer: UhkBuffer): Serializable<Layer> {
|
||||||
|
return new Layer().fromBinary(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
enum Layer {
|
enum KeyLayer {
|
||||||
mod,
|
mod,
|
||||||
fn,
|
fn,
|
||||||
mouse
|
mouse
|
||||||
@@ -10,12 +10,12 @@ class SwitchLayerAction extends KeyAction {
|
|||||||
|
|
||||||
isLayerToggleable: boolean;
|
isLayerToggleable: boolean;
|
||||||
|
|
||||||
// @assertEnum(Layer)
|
// @assertEnum(KeyLayer)
|
||||||
private layer: Layer;
|
private layer: KeyLayer;
|
||||||
|
|
||||||
_fromJsObject(jsObject: any): SwitchLayerAction {
|
_fromJsObject(jsObject: any): SwitchLayerAction {
|
||||||
this.assertKeyActionType(jsObject);
|
this.assertKeyActionType(jsObject);
|
||||||
this.layer = Layer[<string> jsObject.layer];
|
this.layer = KeyLayer[<string> jsObject.layer];
|
||||||
this.isLayerToggleable = jsObject.toggle;
|
this.isLayerToggleable = jsObject.toggle;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ class SwitchLayerAction extends KeyAction {
|
|||||||
_toJsObject(): any {
|
_toJsObject(): any {
|
||||||
return {
|
return {
|
||||||
keyActionType: keyActionType.SwitchLayerAction,
|
keyActionType: keyActionType.SwitchLayerAction,
|
||||||
layer: Layer[this.layer],
|
layer: KeyLayer[this.layer],
|
||||||
toggle: this.isLayerToggleable
|
toggle: this.isLayerToggleable
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,3 +11,5 @@
|
|||||||
/// <reference path="NoneAction.ts" />
|
/// <reference path="NoneAction.ts" />
|
||||||
/// <reference path="Module.ts" />
|
/// <reference path="Module.ts" />
|
||||||
/// <reference path="Modules.ts" />
|
/// <reference path="Modules.ts" />
|
||||||
|
/// <reference path="Layer.ts" />
|
||||||
|
/// <reference path="Layers.ts" />
|
||||||
|
|||||||
@@ -10,31 +10,31 @@ let fs = require('fs');
|
|||||||
|
|
||||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||||
|
|
||||||
let modules1Js = uhkConfig.keymaps[0].layers[0].modules;
|
let config1Js = uhkConfig.keymaps[0].layers;
|
||||||
let modules1Ts: Serializable<Modules> = new Modules().fromJsObject(modules1Js);
|
let config1Ts: Serializable<Layers> = new Layers().fromJsObject(config1Js);
|
||||||
let modules1Buffer = new UhkBuffer();
|
let config1Buffer = new UhkBuffer();
|
||||||
modules1Ts.toBinary(modules1Buffer);
|
config1Ts.toBinary(config1Buffer);
|
||||||
let modules1BufferContent = modules1Buffer.getBufferContent();
|
let config1BufferContent = config1Buffer.getBufferContent();
|
||||||
fs.writeFileSync('uhk-config.bin', modules1BufferContent);
|
fs.writeFileSync('uhk-config.bin', config1BufferContent);
|
||||||
|
|
||||||
modules1Buffer.offset = 0;
|
config1Buffer.offset = 0;
|
||||||
console.log();
|
console.log();
|
||||||
let modules2Ts = new Modules().fromBinary(modules1Buffer);
|
let config2Ts = new Layers().fromBinary(config1Buffer);
|
||||||
console.log('\n');
|
console.log('\n');
|
||||||
let modules2Js = modules2Ts.toJsObject();
|
let config2Js = config2Ts.toJsObject();
|
||||||
let modules2Buffer = new UhkBuffer();
|
let config2Buffer = new UhkBuffer();
|
||||||
modules2Ts.toBinary(modules2Buffer);
|
config2Ts.toBinary(config2Buffer);
|
||||||
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(modules2Js, undefined, 4));
|
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4));
|
||||||
let modules2BufferContent = modules1Buffer.getBufferContent();
|
let config2BufferContent = config1Buffer.getBufferContent();
|
||||||
fs.writeFileSync('uhk-config-serialized.bin', modules2BufferContent);
|
fs.writeFileSync('uhk-config-serialized.bin', config2BufferContent);
|
||||||
|
|
||||||
console.log('\n');
|
console.log('\n');
|
||||||
try {
|
try {
|
||||||
assert.deepEqual(modules1Js, modules2Js);
|
assert.deepEqual(config1Js, config2Js);
|
||||||
console.log('JSON configurations are identical.');
|
console.log('JSON configurations are identical.');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('JSON configurations differ.');
|
console.log('JSON configurations differ.');
|
||||||
}
|
}
|
||||||
|
|
||||||
let buffersContentsAreEqual = Buffer.compare(modules1BufferContent, modules2BufferContent) === 0;
|
let buffersContentsAreEqual = Buffer.compare(config1BufferContent, config2BufferContent) === 0;
|
||||||
console.log('Binary configurations ' + (buffersContentsAreEqual ? 'are identical' : 'differ') + '.');
|
console.log('Binary configurations ' + (buffersContentsAreEqual ? 'are identical' : 'differ') + '.');
|
||||||
|
|||||||
Reference in New Issue
Block a user