diff --git a/src/components/macro/action-editor/macro-action-editor.component.ts b/src/components/macro/action-editor/macro-action-editor.component.ts index 480fe6f6..7d131af1 100644 --- a/src/components/macro/action-editor/macro-action-editor.component.ts +++ b/src/components/macro/action-editor/macro-action-editor.component.ts @@ -39,7 +39,7 @@ export class MacroActionEditorComponent implements OnInit { ngOnInit() { let macroAction: MacroAction = this.macroAction ? this.macroAction : new TextMacroAction(); - this.editableMacroAction = new EditableMacroAction(macroAction.toJsObject()); + this.editableMacroAction = new EditableMacroAction(macroAction.toJsonObject()); let tab: TabName = this.getTabName(this.editableMacroAction); this.activeTab = tab; } diff --git a/src/components/popover/tab/keymap/keymap-tab.component.ts b/src/components/popover/tab/keymap/keymap-tab.component.ts index 19415463..554d6e42 100644 --- a/src/components/popover/tab/keymap/keymap-tab.component.ts +++ b/src/components/popover/tab/keymap/keymap-tab.component.ts @@ -58,9 +58,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab { return false; } - let switchKeymapAction: SwitchKeymapAction = keyAction; - this.selectedKeymap = this.keymaps - .find((keymap: Keymap) => keymap.abbreviation === switchKeymapAction.keymapAbbreviation); + const switchKeymapAction: SwitchKeymapAction = keyAction; + this.selectedKeymap = switchKeymapAction.keymap; } toKeyAction(): SwitchKeymapAction { @@ -68,8 +67,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab { throw new Error('KeyAction is not valid. No selected keymap!'); } - let keymapAction = new SwitchKeymapAction(); - keymapAction.keymapAbbreviation = this.selectedKeymap.abbreviation; + const keymapAction = new SwitchKeymapAction(); + keymapAction.keymap = this.selectedKeymap; return keymapAction; } } diff --git a/src/components/popover/tab/macro/macro-tab.component.ts b/src/components/popover/tab/macro/macro-tab.component.ts index 5ac47ff2..9bd5574f 100644 --- a/src/components/popover/tab/macro/macro-tab.component.ts +++ b/src/components/popover/tab/macro/macro-tab.component.ts @@ -60,8 +60,8 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab { if (!(keyAction instanceof PlayMacroAction)) { return false; } - let playMacroAction: PlayMacroAction = keyAction; - this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macroId === macro.id); + const playMacroAction: PlayMacroAction = keyAction; + this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macro === macro); return true; } @@ -70,8 +70,8 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab { throw new Error('KeyAction is not valid. No selected macro!'); } - let keymapAction = new PlayMacroAction(); - keymapAction.macroId = this.macros[this.selectedMacroIndex].id; + const keymapAction = new PlayMacroAction(); + keymapAction.macro = this.macros[this.selectedMacroIndex]; return keymapAction; } diff --git a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts index b6917597..2f1b652a 100644 --- a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts +++ b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts @@ -185,14 +185,13 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges, OnDestroy { } else if (this.keyAction instanceof SwitchKeymapAction) { let keyAction: SwitchKeymapAction = this.keyAction as SwitchKeymapAction; this.labelType = LabelTypes.SwitchKeymap; - this.labelSource = keyAction.keymapAbbreviation; + this.labelSource = keyAction.keymap.abbreviation; } else if (this.keyAction instanceof PlayMacroAction) { let keyAction: PlayMacroAction = this.keyAction as PlayMacroAction; - let macro: Macro = this.macros.find((macro: Macro) => macro.id === keyAction.macroId); this.labelType = LabelTypes.IconText; this.labelSource = { icon: this.mapper.getIcon('macro'), - text: macro.name + text: keyAction.macro.name }; } else if (this.keyAction instanceof MouseAction) { this.labelType = LabelTypes.MouseKey; diff --git a/src/components/svg/wrap/svg-keyboard-wrap.component.ts b/src/components/svg/wrap/svg-keyboard-wrap.component.ts index 32c6b182..5048c2d7 100644 --- a/src/components/svg/wrap/svg-keyboard-wrap.component.ts +++ b/src/components/svg/wrap/svg-keyboard-wrap.component.ts @@ -17,13 +17,6 @@ import { } from '@angular/core'; import { Store } from '@ngrx/store'; -import { Observable } from 'rxjs/Observable'; - -import 'rxjs/add/observable/from'; -import 'rxjs/add/operator/filter'; -import 'rxjs/add/operator/first'; -import 'rxjs/add/operator/map'; -import 'rxjs/add/operator/switchMap'; import { MapperService } from '../../../services/mapper.service'; @@ -270,23 +263,10 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { }); content.push({ - name: 'Macro id', - value: playMacroAction.macroId.toString() + name: 'Macro name', + value: playMacroAction.macro.name.toString() }); - // Replace the macro id with the name - this.store - .select(appState => appState.macros) - .first() - .map(macroState => macroState.entities.filter(macro => { - return macro.id === playMacroAction.macroId; - })[0].name) - .subscribe(name => { - content[1] = { - name: 'Macro name', - value: name - }; - }); } else if (keyAction instanceof SwitchKeymapAction) { const switchKeymapAction: SwitchKeymapAction = keyAction; content.push({ @@ -295,14 +275,8 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges { }); content.push({ name: 'Keymap', - value: '...' + value: switchKeymapAction.keymap.name }); - this.store - .select(appState => appState.keymaps) - .first() - .switchMap(keymaps => Observable.from(keymaps.entities)) - .filter(keymap => keymap.abbreviation === switchKeymapAction.keymapAbbreviation) - .subscribe(keymap => content[1].value = keymap.name); } else if (keyAction instanceof SwitchLayerAction) { const switchLayerAction: SwitchLayerAction = keyAction; content.push({ diff --git a/src/config-serializer/Serializable.ts b/src/config-serializer/Serializable.ts index 5fba0b03..1ed1fd32 100644 --- a/src/config-serializer/Serializable.ts +++ b/src/config-serializer/Serializable.ts @@ -8,33 +8,12 @@ export abstract class Serializable { private static maxDisplayedJsonLength = 160; private static enableDump = false; - fromJsObject(jsObject: any): T { - this.dump(`${this.getIndentation()}${this.constructor.name}.fromJsObject: ` + - `${this.strintifyJsObject(jsObject)}\n`); - Serializable.depth++; - let value = this._fromJsObject(jsObject); - Serializable.depth--; - this.dump(`${this.getIndentation()}=> ${value}\n`); - return value; - } - - fromBinary(buffer: UhkBuffer): T { - this.dump(`\n${this.getIndentation()}${this.constructor.name}.fromBinary: [`); - Serializable.depth++; - buffer.enableDump = Serializable.enableDump; - let value = this._fromBinary(buffer); - buffer.enableDump = false; - Serializable.depth--; - this.dump(`]\n${this.getIndentation()}=> ${value}`); - return value; - } - - toJsObject(): any { + toJsonObject(): any { this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`); Serializable.depth++; - let value = this._toJsObject(); + let value = this._toJsonObject(); Serializable.depth--; - this.dump(`${this.getIndentation()}=> ${this.strintifyJsObject(value)}\n`); + this.dump(`${this.getIndentation()}=> ${this.stringifyJsonObject(value)}\n`); return value; } @@ -50,9 +29,7 @@ export abstract class Serializable { return value; } - abstract _fromJsObject(jsObject: any): T; - abstract _fromBinary(buffer: UhkBuffer): T; - abstract _toJsObject(): any; + abstract _toJsonObject(): any; abstract _toBinary(buffer: UhkBuffer): void; private dump(value: any) { @@ -65,8 +42,8 @@ export abstract class Serializable { return new Array(Serializable.depth + 1).join(' '); } - private strintifyJsObject(jsObject: any): string { - let json = JSON.stringify(jsObject); + private stringifyJsonObject(jsonObject: any): string { + let json = JSON.stringify(jsonObject); return json.length > Serializable.maxDisplayedJsonLength ? json.substr(0, Serializable.maxDisplayedJsonLength) + '...' : json; diff --git a/src/config-serializer/UhkBuffer.ts b/src/config-serializer/UhkBuffer.ts index d3ca33ba..aa9866bf 100644 --- a/src/config-serializer/UhkBuffer.ts +++ b/src/config-serializer/UhkBuffer.ts @@ -153,12 +153,11 @@ export class UhkBuffer { this.writeUInt8(bool ? 1 : 0); } - // See: How to create a new object from type parameter in generic class in typescript? http://stackoverflow.com/q/17382143 - readArray>(type: { new (): T }): T[] { + readArray>(elementReader: (buffer: UhkBuffer) => T): T[] { let array: T[] = []; let length = this.readCompactLength(); for (let i = 0; i < length; ++i) { - array.push(new type().fromBinary(this)); + array.push(elementReader(this)); } return array; } diff --git a/src/config-serializer/config-items/Keymap.ts b/src/config-serializer/config-items/Keymap.ts index 88a35fab..fc417a7f 100644 --- a/src/config-serializer/config-items/Keymap.ts +++ b/src/config-serializer/config-items/Keymap.ts @@ -1,6 +1,7 @@ import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Layer } from './Layer'; +import { Macro } from './Macro'; export class Keymap extends Serializable { @@ -14,7 +15,7 @@ export class Keymap extends Serializable { layers: Layer[]; - constructor(keymap?: Keymap) { + constructor(keymap?: Keymap, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { super(); if (!keymap) { return; @@ -24,40 +25,42 @@ export class Keymap extends Serializable { this.description = keymap.description; this.abbreviation = keymap.abbreviation; this.isDefault = keymap.isDefault; - this.layers = keymap.layers.map(layer => new Layer(layer)); + this.layers = keymap.layers.map(layer => new Layer(layer, getKeymap, getMacro)); } - _fromJsObject(jsObject: any): Keymap { - this.isDefault = jsObject.isDefault; - this.abbreviation = jsObject.abbreviation; - this.name = jsObject.name; - this.description = jsObject.description; - this.layers = jsObject.layers.map((layer: any) => new Layer().fromJsObject(layer)); + fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap { + this.isDefault = jsonObject.isDefault; + this.abbreviation = jsonObject.abbreviation; + this.name = jsonObject.name; + this.description = jsonObject.description; + this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer, getKeymap, getMacro)); return this; } - _fromBinary(buffer: UhkBuffer): Keymap { - this.isDefault = buffer.readBoolean(); + fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap { this.abbreviation = buffer.readString(); + this.isDefault = buffer.readBoolean(); this.name = buffer.readString(); this.description = buffer.readString(); - this.layers = buffer.readArray(Layer); + this.layers = buffer.readArray(uhkBuffer => { + return new Layer().fromBinary(uhkBuffer, getKeymap, getMacro); + }); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { isDefault: this.isDefault, abbreviation: this.abbreviation, name: this.name, description: this.description, - layers: this.layers.map(layer => layer.toJsObject()) + layers: this.layers.map(layer => layer.toJsonObject()) }; } _toBinary(buffer: UhkBuffer): void { - buffer.writeBoolean(this.isDefault); buffer.writeString(this.abbreviation); + buffer.writeBoolean(this.isDefault); buffer.writeString(this.name); buffer.writeString(this.description); buffer.writeArray(this.layers); diff --git a/src/config-serializer/config-items/Layer.ts b/src/config-serializer/config-items/Layer.ts index 81a3ad32..2c269fa7 100644 --- a/src/config-serializer/config-items/Layer.ts +++ b/src/config-serializer/config-items/Layer.ts @@ -1,6 +1,8 @@ import { AnimationKeyboard } from '../../components/svg/wrap'; import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; +import { Keymap } from './Keymap'; +import { Macro } from './Macro'; import { Module } from './Module'; export class Layer extends Serializable { @@ -8,28 +10,30 @@ export class Layer extends Serializable { modules: Module[]; animation: AnimationKeyboard; - constructor(layers?: Layer) { + constructor(layers?: Layer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { super(); if (!layers) { return; } - this.modules = layers.modules.map(module => new Module(module)); + this.modules = layers.modules.map(module => new Module(module, getKeymap, getMacro)); this.animation = layers.animation; } - _fromJsObject(jsObject: any): Layer { - this.modules = jsObject.modules.map((module: any) => new Module().fromJsObject(module)); + fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer { + this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module, getKeymap, getMacro)); return this; } - _fromBinary(buffer: UhkBuffer): Layer { - this.modules = buffer.readArray(Module); + fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer { + this.modules = buffer.readArray(uhkBuffer => { + return new Module().fromBinary(uhkBuffer, getKeymap, getMacro); + }); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { - modules: this.modules.map(module => module.toJsObject()) + modules: this.modules.map(module => module.toJsonObject()) }; } diff --git a/src/config-serializer/config-items/Macro.ts b/src/config-serializer/config-items/Macro.ts index ad723c21..a6abc24a 100644 --- a/src/config-serializer/config-items/Macro.ts +++ b/src/config-serializer/config-items/Macro.ts @@ -28,16 +28,16 @@ export class Macro extends Serializable { this.macroActions = other.macroActions.map(macroAction => MacroActionHelper.createMacroAction(macroAction)); } - _fromJsObject(jsObject: any): Macro { - this.id = jsObject.id; - this.isLooped = jsObject.isLooped; - this.isPrivate = jsObject.isPrivate; - this.name = jsObject.name; - this.macroActions = jsObject.macroActions.map((macroAction: any) => MacroActionHelper.createMacroAction(macroAction)); + fromJsonObject(jsonObject: any): Macro { + this.id = jsonObject.id; + this.isLooped = jsonObject.isLooped; + this.isPrivate = jsonObject.isPrivate; + this.name = jsonObject.name; + this.macroActions = jsonObject.macroActions.map((macroAction: any) => MacroActionHelper.createMacroAction(macroAction)); return this; } - _fromBinary(buffer: UhkBuffer): Macro { + fromBinary(buffer: UhkBuffer): Macro { this.id = buffer.readUInt8(); this.isLooped = buffer.readBoolean(); this.isPrivate = buffer.readBoolean(); @@ -50,13 +50,13 @@ export class Macro extends Serializable { return this; } - _toJsObject(): any { + _toJsonObject(): any { return { id: this.id, isLooped: this.isLooped, isPrivate: this.isPrivate, name: this.name, - macroActions: this.macroActions.map(macroAction => macroAction.toJsObject()) + macroActions: this.macroActions.map(macroAction => macroAction.toJsonObject()) }; } diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index 4a4aa838..623d9933 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -2,6 +2,8 @@ import { assertEnum, assertUInt8 } from '../assert'; import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action'; +import { Keymap } from './Keymap'; +import { Macro } from './Macro'; enum PointerRole { none, @@ -17,42 +19,45 @@ export class Module extends Serializable { keyActions: KeyAction[]; @assertEnum(PointerRole) - private pointerRole: PointerRole; + pointerRole: PointerRole; - constructor(other?: Module) { + constructor(other?: Module, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) { super(); if (!other) { return; } this.id = other.id; - this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction)); + this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro)); this.pointerRole = other.pointerRole; } - _fromJsObject(jsObject: any): Module { - this.id = jsObject.id; - this.pointerRole = PointerRole[jsObject.pointerRole]; - this.keyActions = jsObject.keyActions.map((keyAction: any) => KeyActionHelper.createKeyAction(keyAction)); + + fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module { + this.id = jsonObject.id; + this.pointerRole = PointerRole[jsonObject.pointerRole]; + this.keyActions = jsonObject.keyActions.map((keyAction: any) => { + return KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro); + }); return this; } - _fromBinary(buffer: UhkBuffer): Module { + fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module { this.id = buffer.readUInt8(); this.pointerRole = buffer.readUInt8(); let keyActionsLength: number = buffer.readCompactLength(); this.keyActions = []; for (let i = 0; i < keyActionsLength; ++i) { - this.keyActions.push(KeyActionHelper.createKeyAction(buffer)); + this.keyActions.push(KeyActionHelper.createKeyAction(buffer, getKeymap, getMacro)); } return this; } - _toJsObject(): any { + _toJsonObject(): any { return { id: this.id, pointerRole: PointerRole[this.pointerRole], keyActions: this.keyActions.map(keyAction => { if (keyAction) { - return keyAction.toJsObject(); + return keyAction.toJsonObject(); } }) }; diff --git a/src/config-serializer/config-items/ModuleConfiguration.ts b/src/config-serializer/config-items/ModuleConfiguration.ts index 5b8a033b..257afecb 100644 --- a/src/config-serializer/config-items/ModuleConfiguration.ts +++ b/src/config-serializer/config-items/ModuleConfiguration.ts @@ -20,15 +20,15 @@ export class ModuleConfiguration extends Serializable { @assertUInt8 maxPointerSpeed: number; - _fromJsObject(jsObject: any): ModuleConfiguration { - this.id = jsObject.id; - this.initialPointerSpeed = jsObject.initialPointerSpeed; - this.pointerAcceleration = jsObject.pointerAcceleration; - this.maxPointerSpeed = jsObject.maxPointerSpeed; + fromJsonObject(jsonObject: any): ModuleConfiguration { + this.id = jsonObject.id; + this.initialPointerSpeed = jsonObject.initialPointerSpeed; + this.pointerAcceleration = jsonObject.pointerAcceleration; + this.maxPointerSpeed = jsonObject.maxPointerSpeed; return this; } - _fromBinary(buffer: UhkBuffer): ModuleConfiguration { + fromBinary(buffer: UhkBuffer): ModuleConfiguration { this.id = buffer.readUInt8(); this.initialPointerSpeed = buffer.readUInt8(); this.pointerAcceleration = buffer.readUInt8(); @@ -36,7 +36,7 @@ export class ModuleConfiguration extends Serializable { return this; } - _toJsObject(): any { + _toJsonObject(): any { return { id: this.id, initialPointerSpeed: this.initialPointerSpeed, diff --git a/src/config-serializer/config-items/UhkConfiguration.ts b/src/config-serializer/config-items/UhkConfiguration.ts index 2a6c25d1..d6712cfa 100644 --- a/src/config-serializer/config-items/UhkConfiguration.ts +++ b/src/config-serializer/config-items/UhkConfiguration.ts @@ -30,44 +30,60 @@ export class UhkConfiguration extends Serializable { @assertUInt32 epilogue: number; - _fromJsObject(jsObject: any): UhkConfiguration { - this.signature = jsObject.signature; - this.dataModelVersion = jsObject.dataModelVersion; - this.prologue = jsObject.prologue; - this.hardwareId = jsObject.hardwareId; - this.brandId = jsObject.brandId; - this.moduleConfigurations = jsObject.moduleConfigurations.map((moduleConfiguration: any) => { - return new ModuleConfiguration().fromJsObject(moduleConfiguration); + fromJsonObject(jsonObject: any): UhkConfiguration { + this.signature = jsonObject.signature; + this.dataModelVersion = jsonObject.dataModelVersion; + this.prologue = jsonObject.prologue; + this.hardwareId = jsonObject.hardwareId; + this.brandId = jsonObject.brandId; + this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => { + return new ModuleConfiguration().fromJsonObject(moduleConfiguration); }); - this.keymaps = jsObject.keymaps.map((keymap: any) => new Keymap().fromJsObject(keymap)); - this.macros = jsObject.macros.map((macro: any) => new Macro().fromJsObject(macro)); - this.epilogue = jsObject.epilogue; + this.macros = jsonObject.macros.map((macro: any) => new Macro().fromJsonObject(macro)); + this.keymaps = jsonObject.keymaps.map((keymap: any) => { + const newKeymap = new Keymap(); + newKeymap.abbreviation = keymap.abbreviation; + return newKeymap; + }); + for (let i = 0; i < this.keymaps.length; ++i) { + this.keymaps[i].fromJsonObject( + jsonObject.keymaps[i], + abbrevation => this.getKeymap(abbrevation, true), + this.getMacro.bind(this) + ); + } + this.epilogue = jsonObject.epilogue; return this; } - _fromBinary(buffer: UhkBuffer): UhkConfiguration { + fromBinary(buffer: UhkBuffer): UhkConfiguration { this.signature = buffer.readString(); this.dataModelVersion = buffer.readUInt8(); this.prologue = buffer.readUInt32(); this.hardwareId = buffer.readUInt8(); this.brandId = buffer.readUInt8(); - this.moduleConfigurations = buffer.readArray(ModuleConfiguration); - this.keymaps = buffer.readArray(Keymap); - this.macros = buffer.readArray(Macro); + this.moduleConfigurations = buffer.readArray(uhkBuffer => { + return new ModuleConfiguration().fromBinary(uhkBuffer); + }); + this.macros = buffer.readArray(uhkBuffer => new Macro().fromBinary(uhkBuffer)); + this.keymaps = []; + this.keymaps = buffer.readArray(uhkBuffer => { + return new Keymap().fromBinary(uhkBuffer, abbrevation => this.getKeymap(abbrevation, true), this.getMacro.bind(this)); + }); this.epilogue = buffer.readUInt32(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { signature: this.signature, dataModelVersion: this.dataModelVersion, prologue: this.prologue, hardwareId: this.hardwareId, brandId: this.brandId, - moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsObject()), - keymaps: this.keymaps.map(keymap => keymap.toJsObject()), - macros: this.macros.map(macro => macro.toJsObject()), + moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()), + keymaps: this.keymaps.map(keymap => keymap.toJsonObject()), + macros: this.macros.map(macro => macro.toJsonObject()), epilogue: this.epilogue }; } @@ -79,8 +95,8 @@ export class UhkConfiguration extends Serializable { buffer.writeUInt8(this.hardwareId); buffer.writeUInt8(this.brandId); buffer.writeArray(this.moduleConfigurations); - buffer.writeArray(this.keymaps); buffer.writeArray(this.macros); + buffer.writeArray(this.keymaps); buffer.writeUInt32(this.epilogue); } @@ -88,11 +104,18 @@ export class UhkConfiguration extends Serializable { return ``; } - getKeymap(keymapAbbreviation: string): Keymap { - return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); + getKeymap(keymapAbbreviation: string, createIfNotExist = false): Keymap { + let resultKeymap = this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); + if (createIfNotExist && !resultKeymap) { + resultKeymap = new Keymap(); + resultKeymap.abbreviation = keymapAbbreviation; + this.keymaps.push(resultKeymap); + } + return resultKeymap; } getMacro(macroId: number): Macro { return this.macros.find(macro => macroId === macro.id); } + } diff --git a/src/config-serializer/config-items/key-action/KeyAction.ts b/src/config-serializer/config-items/key-action/KeyAction.ts index 3e15505f..e4b09b93 100644 --- a/src/config-serializer/config-items/key-action/KeyAction.ts +++ b/src/config-serializer/config-items/key-action/KeyAction.ts @@ -53,8 +53,6 @@ export abstract class KeyAction extends Serializable { return readKeyActionId; } - abstract _fromJsObject(jsObject: any): KeyAction; - abstract _fromBinary(buffer: UhkBuffer): KeyAction; - abstract _toJsObject(): any; + abstract _toJsonObject(): any; abstract _toBinary(buffer: UhkBuffer): void; } diff --git a/src/config-serializer/config-items/key-action/KeystrokeAction.ts b/src/config-serializer/config-items/key-action/KeystrokeAction.ts index 751f4228..9cd9d402 100644 --- a/src/config-serializer/config-items/key-action/KeystrokeAction.ts +++ b/src/config-serializer/config-items/key-action/KeystrokeAction.ts @@ -10,7 +10,7 @@ export enum KeystrokeActionFlag { longPressAction = 1 << 2, } -interface JsObjectKeystrokeAction { +interface JsonObjectKeystrokeAction { keyActionType: string; scancode?: number; modifierMask?: number; @@ -40,15 +40,15 @@ export class KeystrokeAction extends KeyAction { this.longPressAction = other.longPressAction; } - _fromJsObject(jsObject: JsObjectKeystrokeAction): KeystrokeAction { - this.assertKeyActionType(jsObject); - this.scancode = jsObject.scancode; - this.modifierMask = jsObject.modifierMask; - this.longPressAction = LongPressAction[jsObject.longPressAction]; + fromJsonObject(jsonObject: JsonObjectKeystrokeAction): KeystrokeAction { + this.assertKeyActionType(jsonObject); + this.scancode = jsonObject.scancode; + this.modifierMask = jsonObject.modifierMask; + this.longPressAction = LongPressAction[jsonObject.longPressAction]; return this; } - _fromBinary(buffer: UhkBuffer): KeystrokeAction { + fromBinary(buffer: UhkBuffer): KeystrokeAction { let keyActionId: KeyActionId = this.readAndAssertKeyActionId(buffer); let flags: number = keyActionId - KeyActionId.KeystrokeAction; if (flags & KeystrokeActionFlag.scancode) { @@ -63,24 +63,24 @@ export class KeystrokeAction extends KeyAction { return this; } - _toJsObject(): JsObjectKeystrokeAction { - let jsObject: JsObjectKeystrokeAction = { + _toJsonObject(): JsonObjectKeystrokeAction { + let jsonObject: JsonObjectKeystrokeAction = { keyActionType: keyActionType.KeystrokeAction }; if (this.hasScancode()) { - jsObject.scancode = this.scancode; + jsonObject.scancode = this.scancode; } if (this.hasActiveModifier()) { - jsObject.modifierMask = this.modifierMask; + jsonObject.modifierMask = this.modifierMask; } if (this.hasLongPressAction()) { - jsObject.longPressAction = LongPressAction[this.longPressAction]; + jsonObject.longPressAction = LongPressAction[this.longPressAction]; } - return jsObject; + return jsonObject; } _toBinary(buffer: UhkBuffer) { diff --git a/src/config-serializer/config-items/key-action/MouseAction.ts b/src/config-serializer/config-items/key-action/MouseAction.ts index 3e9e76dd..dea12860 100644 --- a/src/config-serializer/config-items/key-action/MouseAction.ts +++ b/src/config-serializer/config-items/key-action/MouseAction.ts @@ -31,19 +31,19 @@ export class MouseAction extends KeyAction { this.mouseAction = other.mouseAction; } - _fromJsObject(jsObject: any): MouseAction { + fromJsonObject(jsObject: any): MouseAction { this.assertKeyActionType(jsObject); this.mouseAction = MouseActionParam[jsObject.mouseAction]; return this; } - _fromBinary(buffer: UhkBuffer): MouseAction { + fromBinary(buffer: UhkBuffer): MouseAction { this.readAndAssertKeyActionId(buffer); this.mouseAction = buffer.readUInt8(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { keyActionType: keyActionType.MouseAction, mouseAction: MouseActionParam[this.mouseAction] diff --git a/src/config-serializer/config-items/key-action/NoneAction.ts b/src/config-serializer/config-items/key-action/NoneAction.ts index 12cb182f..908b3c08 100644 --- a/src/config-serializer/config-items/key-action/NoneAction.ts +++ b/src/config-serializer/config-items/key-action/NoneAction.ts @@ -9,17 +9,17 @@ import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; export class NoneAction extends KeyAction { - _fromJsObject(jsObject: any): NoneAction { - this.assertKeyActionType(jsObject); + fromJsonObject(jsonObject: any): NoneAction { + this.assertKeyActionType(jsonObject); return this; } - _fromBinary(buffer: UhkBuffer): NoneAction { + fromBinary(buffer: UhkBuffer): NoneAction { this.readAndAssertKeyActionId(buffer); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { keyActionType: keyActionType.NoneAction }; diff --git a/src/config-serializer/config-items/key-action/PlayMacroAction.ts b/src/config-serializer/config-items/key-action/PlayMacroAction.ts index 72411078..1dc756d8 100644 --- a/src/config-serializer/config-items/key-action/PlayMacroAction.ts +++ b/src/config-serializer/config-items/key-action/PlayMacroAction.ts @@ -1,45 +1,49 @@ -import { assertUInt8 } from '../../assert'; import { UhkBuffer } from '../../UhkBuffer'; +import { Macro } from '../Macro'; import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; export class PlayMacroAction extends KeyAction { - @assertUInt8 - macroId: number; + macro: Macro; - constructor(other?: PlayMacroAction) { + constructor(parameter?: PlayMacroAction | Macro) { super(); - if (!other) { + if (!parameter) { return; } - this.macroId = other.macroId; + if (parameter instanceof PlayMacroAction) { + this.macro = parameter.macro; + } else { + this.macro = parameter; + } } - _fromJsObject(jsObject: any): PlayMacroAction { - this.assertKeyActionType(jsObject); - this.macroId = jsObject.macroId; + fromJsonObject(jsonObject: any, getMacro: (macroId: number) => Macro): PlayMacroAction { + this.assertKeyActionType(jsonObject); + this.macro = getMacro(jsonObject.macroId); return this; } - _fromBinary(buffer: UhkBuffer): PlayMacroAction { + fromBinary(buffer: UhkBuffer, getMacro: (macroId: number) => Macro): PlayMacroAction { this.readAndAssertKeyActionId(buffer); - this.macroId = buffer.readUInt8(); + const macroId = buffer.readUInt8(); + this.macro = getMacro(macroId); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { keyActionType: keyActionType.PlayMacroAction, - macroId: this.macroId + macroId: this.macro.id }; } _toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.PlayMacroAction); - buffer.writeUInt8(this.macroId); + buffer.writeUInt8(this.macro.id); } toString(): string { - return ``; + return ``; } } diff --git a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts index 0eed276d..f0c6c3a8 100644 --- a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts +++ b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts @@ -1,43 +1,49 @@ import { UhkBuffer } from '../../UhkBuffer'; +import { Keymap } from '../Keymap'; import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; export class SwitchKeymapAction extends KeyAction { - keymapAbbreviation: string; + keymap: Keymap; - constructor(other?: SwitchKeymapAction) { + constructor(parameter?: SwitchKeymapAction | Keymap) { super(); - if (!other) { + if (!parameter) { return; } - this.keymapAbbreviation = other.keymapAbbreviation; + if (parameter instanceof SwitchKeymapAction) { + this.keymap = parameter.keymap; + } else { + this.keymap = parameter; + } } - _fromJsObject(jsObject: any): SwitchKeymapAction { - this.assertKeyActionType(jsObject); - this.keymapAbbreviation = jsObject.keymapAbbreviation; + fromJsonObject(jsonObject: any, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction { + this.assertKeyActionType(jsonObject); + this.keymap = getKeymap(jsonObject.keymapAbbreviation); return this; } - _fromBinary(buffer: UhkBuffer): SwitchKeymapAction { + fromBinary(buffer: UhkBuffer, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction { this.readAndAssertKeyActionId(buffer); - this.keymapAbbreviation = buffer.readString(); + const keymapAbbreviation = buffer.readString(); + this.keymap = getKeymap(keymapAbbreviation); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { keyActionType: keyActionType.SwitchKeymapAction, - keymapAbbreviation: this.keymapAbbreviation + keymapAbbreviation: this.keymap.abbreviation }; } _toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.SwitchKeymapAction); - buffer.writeString(this.keymapAbbreviation); + buffer.writeString(this.keymap.abbreviation); } toString(): string { - return ``; + return ``; } } diff --git a/src/config-serializer/config-items/key-action/SwitchLayerAction.ts b/src/config-serializer/config-items/key-action/SwitchLayerAction.ts index 832abad8..d750a4fc 100644 --- a/src/config-serializer/config-items/key-action/SwitchLayerAction.ts +++ b/src/config-serializer/config-items/key-action/SwitchLayerAction.ts @@ -24,21 +24,21 @@ export class SwitchLayerAction extends KeyAction { this.layer = other.layer; } - _fromJsObject(jsObject: any): SwitchLayerAction { - this.assertKeyActionType(jsObject); - this.layer = LayerName[jsObject.layer]; - this.isLayerToggleable = jsObject.toggle; + fromJsonObject(jsonObject: any): SwitchLayerAction { + this.assertKeyActionType(jsonObject); + this.layer = LayerName[jsonObject.layer]; + this.isLayerToggleable = jsonObject.toggle; return this; } - _fromBinary(buffer: UhkBuffer): SwitchLayerAction { + fromBinary(buffer: UhkBuffer): SwitchLayerAction { this.readAndAssertKeyActionId(buffer); this.layer = buffer.readUInt8(); this.isLayerToggleable = buffer.readBoolean(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { keyActionType: keyActionType.SwitchLayerAction, layer: LayerName[this.layer], diff --git a/src/config-serializer/config-items/key-action/helper.ts b/src/config-serializer/config-items/key-action/helper.ts index 4ff386d0..1463b373 100644 --- a/src/config-serializer/config-items/key-action/helper.ts +++ b/src/config-serializer/config-items/key-action/helper.ts @@ -10,19 +10,30 @@ import { keyActionType } from './index'; +import { Keymap } from '../Keymap'; +import { Macro } from '../Macro'; + export class Helper { - static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction { + static createKeyAction( + source: KeyAction | UhkBuffer | any, + getKeymap?: (abbrevation: string) => Keymap, + getMacro?: (macroId: number) => Macro + ): KeyAction { if (source instanceof KeyAction) { return Helper.fromKeyAction(source); } else if (source instanceof UhkBuffer) { - return Helper.fromUhkBuffer(source); + return Helper.fromUhkBuffer(source, getKeymap, getMacro); } else { - return Helper.fromJSONObject(source); + return Helper.fromJSONObject(source, getKeymap, getMacro); } } - private static fromUhkBuffer(buffer: UhkBuffer): KeyAction { + private static fromUhkBuffer( + buffer: UhkBuffer, + getKeymap?: (abbrevation: string) => Keymap, + getMacro?: (macroId: number) => Macro + ): KeyAction { let keyActionFirstByte = buffer.readUInt8(); buffer.backtrack(); @@ -37,11 +48,11 @@ export class Helper { case KeyActionId.SwitchLayerAction: return new SwitchLayerAction().fromBinary(buffer); case KeyActionId.SwitchKeymapAction: - return new SwitchKeymapAction().fromBinary(buffer); + return new SwitchKeymapAction().fromBinary(buffer, getKeymap); case KeyActionId.MouseAction: return new MouseAction().fromBinary(buffer); case KeyActionId.PlayMacroAction: - return new PlayMacroAction().fromBinary(buffer); + return new PlayMacroAction().fromBinary(buffer, getMacro); default: throw `Invalid KeyAction first byte: ${keyActionFirstByte}`; } @@ -63,22 +74,26 @@ export class Helper { return newKeyAction; } - private static fromJSONObject(keyAction: any): KeyAction { + private static fromJSONObject( + keyAction: any, + getKeymap?: (abbrevation: string) => Keymap, + getMacro?: (macroId: number) => Macro + ): KeyAction { if (!keyAction) { return; } switch (keyAction.keyActionType) { case keyActionType.KeystrokeAction: - return new KeystrokeAction().fromJsObject(keyAction); + return new KeystrokeAction().fromJsonObject(keyAction); case keyActionType.SwitchLayerAction: - return new SwitchLayerAction().fromJsObject(keyAction); + return new SwitchLayerAction().fromJsonObject(keyAction); case keyActionType.SwitchKeymapAction: - return new SwitchKeymapAction().fromJsObject(keyAction); + return new SwitchKeymapAction().fromJsonObject(keyAction, getKeymap); case keyActionType.MouseAction: - return new MouseAction().fromJsObject(keyAction); + return new MouseAction().fromJsonObject(keyAction); case keyActionType.PlayMacroAction: - return new PlayMacroAction().fromJsObject(keyAction); + return new PlayMacroAction().fromJsonObject(keyAction, getMacro); default: throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`; } diff --git a/src/config-serializer/config-items/macro-action/DelayMacroAction.ts b/src/config-serializer/config-items/macro-action/DelayMacroAction.ts index e0ca5e5c..d942399c 100644 --- a/src/config-serializer/config-items/macro-action/DelayMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/DelayMacroAction.ts @@ -15,19 +15,19 @@ export class DelayMacroAction extends MacroAction { this.delay = other.delay; } - _fromJsObject(jsObject: any): DelayMacroAction { + fromJsonObject(jsObject: any): DelayMacroAction { this.assertMacroActionType(jsObject); this.delay = jsObject.delay; return this; } - _fromBinary(buffer: UhkBuffer): DelayMacroAction { + fromBinary(buffer: UhkBuffer): DelayMacroAction { this.readAndAssertMacroActionId(buffer); this.delay = buffer.readUInt16(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { macroActionType: macroActionType.DelayMacroAction, delay: this.delay diff --git a/src/config-serializer/config-items/macro-action/EditableMacroAction.ts b/src/config-serializer/config-items/macro-action/EditableMacroAction.ts index b2a165c8..58f6c9d6 100644 --- a/src/config-serializer/config-items/macro-action/EditableMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/EditableMacroAction.ts @@ -93,7 +93,7 @@ export class EditableMacroAction { } fromKeyAction(keyAction: KeyAction): void { - let data = keyAction.toJsObject(); + let data = keyAction.toJsonObject(); this.scancode = data.scancode; this.modifierMask = data.modifierMask; } @@ -101,7 +101,7 @@ export class EditableMacroAction { toKeystrokeAction(): KeystrokeAction { let data = this.toJsObject(); data.keyActionType = keyActionType.KeystrokeAction; - return (new KeystrokeAction().fromJsObject(data)); + return (new KeystrokeAction().fromJsonObject(data)); } setMouseButtons(buttonStates: boolean[]): void { @@ -124,19 +124,19 @@ export class EditableMacroAction { switch (this.macroActionType) { // Delay action case macroActionType.DelayMacroAction: - return new DelayMacroAction().fromJsObject({ + return new DelayMacroAction().fromJsonObject({ macroActionType: this.macroActionType, delay: this.delay }); // Text action case macroActionType.TextMacroAction: - return new TextMacroAction().fromJsObject({ + return new TextMacroAction().fromJsonObject({ macroActionType: this.macroActionType, text: this.text }); // Keypress action case macroActionType.KeyMacroAction: - return new KeyMacroAction().fromJsObject({ + return new KeyMacroAction().fromJsonObject({ macroActionType: this.macroActionType, action: MacroSubAction[this.action], scancode: this.scancode, @@ -144,19 +144,19 @@ export class EditableMacroAction { }); // Mouse actions case macroActionType.MouseButtonMacroAction: - return new MouseButtonMacroAction().fromJsObject({ + return new MouseButtonMacroAction().fromJsonObject({ macroActionType: this.macroActionType, action: MacroSubAction[this.action], mouseButtonsMask: this.mouseButtonsMask }); case macroActionType.MoveMouseMacroAction: - return new MoveMouseMacroAction().fromJsObject({ + return new MoveMouseMacroAction().fromJsonObject({ macroActionType: this.macroActionType, x: this.moveX, y: this.moveY }); case macroActionType.ScrollMouseMacroAction: - return new ScrollMouseMacroAction().fromJsObject({ + return new ScrollMouseMacroAction().fromJsonObject({ macroActionType: this.macroActionType, x: this.scrollX, y: this.scrollY diff --git a/src/config-serializer/config-items/macro-action/KeyMacroAction.ts b/src/config-serializer/config-items/macro-action/KeyMacroAction.ts index c8b999d7..4029002e 100644 --- a/src/config-serializer/config-items/macro-action/KeyMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/KeyMacroAction.ts @@ -33,7 +33,7 @@ export class KeyMacroAction extends MacroAction { this.modifierMask = other.modifierMask; } - _fromJsObject(jsObject: JsObjectKeyMacroAction): KeyMacroAction { + fromJsonObject(jsObject: JsObjectKeyMacroAction): KeyMacroAction { this.assertMacroActionType(jsObject); this.action = MacroSubAction[jsObject.action]; this.scancode = jsObject.scancode; @@ -41,7 +41,7 @@ export class KeyMacroAction extends MacroAction { return this; } - _fromBinary(buffer: UhkBuffer): KeyMacroAction { + fromBinary(buffer: UhkBuffer): KeyMacroAction { let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer); let keyMacroType: number = macroActionId - MacroActionId.KeyMacroAction; this.action = Math.floor(keyMacroType / NUM_OF_COMBINATIONS); @@ -55,7 +55,7 @@ export class KeyMacroAction extends MacroAction { return this; } - _toJsObject(): any { + _toJsonObject(): any { let jsObject: JsObjectKeyMacroAction = { macroActionType: macroActionType.KeyMacroAction, action: MacroSubAction[this.action] diff --git a/src/config-serializer/config-items/macro-action/MacroAction.ts b/src/config-serializer/config-items/macro-action/MacroAction.ts index f6c7e4b7..62b7a2ad 100644 --- a/src/config-serializer/config-items/macro-action/MacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MacroAction.ts @@ -73,8 +73,6 @@ export abstract class MacroAction extends Serializable { return readMacroActionId; } - abstract _fromJsObject(jsObject: any): MacroAction; - abstract _fromBinary(buffer: UhkBuffer): MacroAction; - abstract _toJsObject(): any; + abstract _toJsonObject(): any; abstract _toBinary(buffer: UhkBuffer): void; } diff --git a/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts b/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts index e087eb92..562598da 100644 --- a/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts @@ -30,21 +30,21 @@ export class MouseButtonMacroAction extends MacroAction { this.mouseButtonsMask = other.mouseButtonsMask; } - _fromJsObject(jsObject: JsObjectMouseButtonMacroAction): MouseButtonMacroAction { + fromJsonObject(jsObject: JsObjectMouseButtonMacroAction): MouseButtonMacroAction { this.assertMacroActionType(jsObject); this.action = MacroSubAction[jsObject.action]; this.mouseButtonsMask = jsObject.mouseButtonsMask; return this; } - _fromBinary(buffer: UhkBuffer): MouseButtonMacroAction { + fromBinary(buffer: UhkBuffer): MouseButtonMacroAction { let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer); this.action = macroActionId - MacroActionId.MouseButtonMacroAction; this.mouseButtonsMask = buffer.readUInt8(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { macroActionType: macroActionType.MouseButtonMacroAction, action: MacroSubAction[this.action], diff --git a/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts b/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts index c42bbb22..3dd74fd7 100644 --- a/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts @@ -19,21 +19,21 @@ export class MoveMouseMacroAction extends MacroAction { this.y = other.y; } - _fromJsObject(jsObject: any): MoveMouseMacroAction { + fromJsonObject(jsObject: any): MoveMouseMacroAction { this.assertMacroActionType(jsObject); this.x = jsObject.x; this.y = jsObject.y; return this; } - _fromBinary(buffer: UhkBuffer): MoveMouseMacroAction { + fromBinary(buffer: UhkBuffer): MoveMouseMacroAction { this.readAndAssertMacroActionId(buffer); this.x = buffer.readInt16(); this.y = buffer.readInt16(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { macroActionType: macroActionType.MoveMouseMacroAction, x: this.x, diff --git a/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts b/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts index 8780f701..c7734841 100644 --- a/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts @@ -19,21 +19,21 @@ export class ScrollMouseMacroAction extends MacroAction { this.y = other.y; } - _fromJsObject(jsObject: any): ScrollMouseMacroAction { + fromJsonObject(jsObject: any): ScrollMouseMacroAction { this.assertMacroActionType(jsObject); this.x = jsObject.x; this.y = jsObject.y; return this; } - _fromBinary(buffer: UhkBuffer): ScrollMouseMacroAction { + fromBinary(buffer: UhkBuffer): ScrollMouseMacroAction { this.readAndAssertMacroActionId(buffer); this.x = buffer.readInt16(); this.y = buffer.readInt16(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { macroActionType: macroActionType.ScrollMouseMacroAction, x: this.x, diff --git a/src/config-serializer/config-items/macro-action/TextMacroAction.ts b/src/config-serializer/config-items/macro-action/TextMacroAction.ts index c6d25cef..7c98268f 100644 --- a/src/config-serializer/config-items/macro-action/TextMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/TextMacroAction.ts @@ -13,19 +13,19 @@ export class TextMacroAction extends MacroAction { this.text = other.text; } - _fromJsObject(jsObject: any): TextMacroAction { + fromJsonObject(jsObject: any): TextMacroAction { this.assertMacroActionType(jsObject); this.text = jsObject.text; return this; } - _fromBinary(buffer: UhkBuffer): TextMacroAction { + fromBinary(buffer: UhkBuffer): TextMacroAction { this.readAndAssertMacroActionId(buffer); this.text = buffer.readString(); return this; } - _toJsObject(): any { + _toJsonObject(): any { return { macroActionType: macroActionType.TextMacroAction, text: this.text diff --git a/src/config-serializer/config-items/macro-action/helper.ts b/src/config-serializer/config-items/macro-action/helper.ts index f7913b3b..c5b7d429 100644 --- a/src/config-serializer/config-items/macro-action/helper.ts +++ b/src/config-serializer/config-items/macro-action/helper.ts @@ -70,17 +70,17 @@ export class Helper { private static fromJSONObject(macroAction: any): MacroAction { switch (macroAction.macroActionType) { case macroActionType.KeyMacroAction: - return new KeyMacroAction().fromJsObject(macroAction); + return new KeyMacroAction().fromJsonObject(macroAction); case macroActionType.MouseButtonMacroAction: - return new MouseButtonMacroAction().fromJsObject(macroAction); + return new MouseButtonMacroAction().fromJsonObject(macroAction); case macroActionType.MoveMouseMacroAction: - return new MoveMouseMacroAction().fromJsObject(macroAction); + return new MoveMouseMacroAction().fromJsonObject(macroAction); case macroActionType.ScrollMouseMacroAction: - return new ScrollMouseMacroAction().fromJsObject(macroAction); + return new ScrollMouseMacroAction().fromJsonObject(macroAction); case macroActionType.DelayMacroAction: - return new DelayMacroAction().fromJsObject(macroAction); + return new DelayMacroAction().fromJsonObject(macroAction); case macroActionType.TextMacroAction: - return new TextMacroAction().fromJsObject(macroAction); + return new TextMacroAction().fromJsonObject(macroAction); default: throw `Invalid MacroAction.macroActionType: "${macroAction.macroActionType}"`; } diff --git a/src/store/storage/index.ts b/src/store/storage/index.ts index da4b555d..62513593 100644 --- a/src/store/storage/index.ts +++ b/src/store/storage/index.ts @@ -81,9 +81,16 @@ export class DataStorage { } initUHKJson() { - this.uhkConfiguration = new UhkConfiguration().fromJsObject(require('json!../../config-serializer/uhk-config.json')); + this.uhkConfiguration = new UhkConfiguration().fromJsonObject(require('json!../../config-serializer/uhk-config.json')); this.uhkPresets = (require('json!../../config-serializer/preset-keymaps.json')) - .map(keymap => new Keymap().fromJsObject(keymap)); + /* TODO: Remove passing getters, because there shouldn't be any SwitchKeymapAction or PlayMacroAction in presets, + * so they shouldn't be needed. + */ + .map(keymap => new Keymap().fromJsonObject( + keymap, + this.uhkConfiguration.getKeymap.bind(this.uhkConfiguration), + this.uhkConfiguration.getMacro.bind(this.uhkConfiguration) + )); } getConfiguration(): UhkConfiguration { diff --git a/src/store/storage/local.ts b/src/store/storage/local.ts index 3fe2c64a..45cd21b6 100644 --- a/src/store/storage/local.ts +++ b/src/store/storage/local.ts @@ -11,7 +11,7 @@ export class Local { if (configJsonString) { const configJsonObject = JSON.parse(configJsonString); if (configJsonObject.dataModelVersion === this.dataModelVersion) { - config = new UhkConfiguration().fromJsObject(configJsonObject); + config = new UhkConfiguration().fromJsonObject(configJsonObject); } } @@ -19,6 +19,6 @@ export class Local { } saveConfig(config: UhkConfiguration): void { - localStorage.setItem('config', JSON.stringify(config.toJsObject())); + localStorage.setItem('config', JSON.stringify(config.toJsonObject())); } } diff --git a/test-serializer/test-serializer.ts b/test-serializer/test-serializer.ts index 31886c44..d361b048 100644 --- a/test-serializer/test-serializer.ts +++ b/test-serializer/test-serializer.ts @@ -8,7 +8,7 @@ let fs = require('fs'); let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json')); let config1Js = uhkConfig; -let config1Ts: Serializable = new UhkConfiguration().fromJsObject(config1Js); +let config1Ts: Serializable = new UhkConfiguration().fromJsonObject(config1Js); let config1Buffer = new UhkBuffer(); config1Ts.toBinary(config1Buffer); let config1BufferContent = config1Buffer.getBufferContent(); @@ -18,7 +18,7 @@ config1Buffer.offset = 0; console.log(); let config2Ts = new UhkConfiguration().fromBinary(config1Buffer); console.log('\n'); -let config2Js = config2Ts.toJsObject(); +let config2Js = config2Ts.toJsonObject(); let config2Buffer = new UhkBuffer(); config2Ts.toBinary(config2Buffer); fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4));