Use references in key actions (#204)

This commit is contained in:
József Farkas
2016-12-13 23:09:39 +01:00
committed by GitHub
parent 3380a9d514
commit e48fdea89d
33 changed files with 266 additions and 255 deletions

View File

@@ -39,7 +39,7 @@ export class MacroActionEditorComponent implements OnInit {
ngOnInit() { ngOnInit() {
let macroAction: MacroAction = this.macroAction ? this.macroAction : new TextMacroAction(); 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); let tab: TabName = this.getTabName(this.editableMacroAction);
this.activeTab = tab; this.activeTab = tab;
} }

View File

@@ -58,9 +58,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab {
return false; return false;
} }
let switchKeymapAction: SwitchKeymapAction = <SwitchKeymapAction>keyAction; const switchKeymapAction: SwitchKeymapAction = <SwitchKeymapAction>keyAction;
this.selectedKeymap = this.keymaps this.selectedKeymap = switchKeymapAction.keymap;
.find((keymap: Keymap) => keymap.abbreviation === switchKeymapAction.keymapAbbreviation);
} }
toKeyAction(): SwitchKeymapAction { toKeyAction(): SwitchKeymapAction {
@@ -68,8 +67,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab {
throw new Error('KeyAction is not valid. No selected keymap!'); throw new Error('KeyAction is not valid. No selected keymap!');
} }
let keymapAction = new SwitchKeymapAction(); const keymapAction = new SwitchKeymapAction();
keymapAction.keymapAbbreviation = this.selectedKeymap.abbreviation; keymapAction.keymap = this.selectedKeymap;
return keymapAction; return keymapAction;
} }
} }

View File

@@ -60,8 +60,8 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab {
if (!(keyAction instanceof PlayMacroAction)) { if (!(keyAction instanceof PlayMacroAction)) {
return false; return false;
} }
let playMacroAction: PlayMacroAction = <PlayMacroAction>keyAction; const playMacroAction: PlayMacroAction = <PlayMacroAction>keyAction;
this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macroId === macro.id); this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macro === macro);
return true; return true;
} }
@@ -70,8 +70,8 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab {
throw new Error('KeyAction is not valid. No selected macro!'); throw new Error('KeyAction is not valid. No selected macro!');
} }
let keymapAction = new PlayMacroAction(); const keymapAction = new PlayMacroAction();
keymapAction.macroId = this.macros[this.selectedMacroIndex].id; keymapAction.macro = this.macros[this.selectedMacroIndex];
return keymapAction; return keymapAction;
} }

View File

@@ -185,14 +185,13 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges, OnDestroy {
} else if (this.keyAction instanceof SwitchKeymapAction) { } else if (this.keyAction instanceof SwitchKeymapAction) {
let keyAction: SwitchKeymapAction = this.keyAction as SwitchKeymapAction; let keyAction: SwitchKeymapAction = this.keyAction as SwitchKeymapAction;
this.labelType = LabelTypes.SwitchKeymap; this.labelType = LabelTypes.SwitchKeymap;
this.labelSource = keyAction.keymapAbbreviation; this.labelSource = keyAction.keymap.abbreviation;
} else if (this.keyAction instanceof PlayMacroAction) { } else if (this.keyAction instanceof PlayMacroAction) {
let keyAction: PlayMacroAction = this.keyAction as 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.labelType = LabelTypes.IconText;
this.labelSource = { this.labelSource = {
icon: this.mapper.getIcon('macro'), icon: this.mapper.getIcon('macro'),
text: macro.name text: keyAction.macro.name
}; };
} else if (this.keyAction instanceof MouseAction) { } else if (this.keyAction instanceof MouseAction) {
this.labelType = LabelTypes.MouseKey; this.labelType = LabelTypes.MouseKey;

View File

@@ -17,13 +17,6 @@ import {
} from '@angular/core'; } from '@angular/core';
import { Store } from '@ngrx/store'; 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'; import { MapperService } from '../../../services/mapper.service';
@@ -270,23 +263,10 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
}); });
content.push({ content.push({
name: 'Macro id', name: 'Macro name',
value: playMacroAction.macroId.toString() 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) { } else if (keyAction instanceof SwitchKeymapAction) {
const switchKeymapAction: SwitchKeymapAction = keyAction; const switchKeymapAction: SwitchKeymapAction = keyAction;
content.push({ content.push({
@@ -295,14 +275,8 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
}); });
content.push({ content.push({
name: 'Keymap', name: 'Keymap',
value: '...' value: switchKeymapAction.keymap.name
}); });
this.store
.select(appState => appState.keymaps)
.first()
.switchMap<Keymap>(keymaps => Observable.from(keymaps.entities))
.filter(keymap => keymap.abbreviation === switchKeymapAction.keymapAbbreviation)
.subscribe(keymap => content[1].value = keymap.name);
} else if (keyAction instanceof SwitchLayerAction) { } else if (keyAction instanceof SwitchLayerAction) {
const switchLayerAction: SwitchLayerAction = keyAction; const switchLayerAction: SwitchLayerAction = keyAction;
content.push({ content.push({

View File

@@ -8,33 +8,12 @@ export abstract class Serializable<T> {
private static maxDisplayedJsonLength = 160; private static maxDisplayedJsonLength = 160;
private static enableDump = false; private static enableDump = false;
fromJsObject(jsObject: any): T { toJsonObject(): any {
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 {
this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`); this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`);
Serializable.depth++; Serializable.depth++;
let value = this._toJsObject(); let value = this._toJsonObject();
Serializable.depth--; Serializable.depth--;
this.dump(`${this.getIndentation()}=> ${this.strintifyJsObject(value)}\n`); this.dump(`${this.getIndentation()}=> ${this.stringifyJsonObject(value)}\n`);
return value; return value;
} }
@@ -50,9 +29,7 @@ export abstract class Serializable<T> {
return value; return value;
} }
abstract _fromJsObject(jsObject: any): T; abstract _toJsonObject(): any;
abstract _fromBinary(buffer: UhkBuffer): T;
abstract _toJsObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract _toBinary(buffer: UhkBuffer): void;
private dump(value: any) { private dump(value: any) {
@@ -65,8 +42,8 @@ export abstract class Serializable<T> {
return new Array(Serializable.depth + 1).join(' '); return new Array(Serializable.depth + 1).join(' ');
} }
private strintifyJsObject(jsObject: any): string { private stringifyJsonObject(jsonObject: any): string {
let json = JSON.stringify(jsObject); let json = JSON.stringify(jsonObject);
return json.length > Serializable.maxDisplayedJsonLength return json.length > Serializable.maxDisplayedJsonLength
? json.substr(0, Serializable.maxDisplayedJsonLength) + '...' ? json.substr(0, Serializable.maxDisplayedJsonLength) + '...'
: json; : json;

View File

@@ -153,12 +153,11 @@ export class UhkBuffer {
this.writeUInt8(bool ? 1 : 0); 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<T extends Serializable<T>>(elementReader: (buffer: UhkBuffer) => T): T[] {
readArray<T extends Serializable<T>>(type: { new (): T }): T[] {
let array: T[] = []; let array: T[] = [];
let length = this.readCompactLength(); let length = this.readCompactLength();
for (let i = 0; i < length; ++i) { for (let i = 0; i < length; ++i) {
array.push(new type().fromBinary(this)); array.push(elementReader(this));
} }
return array; return array;
} }

View File

@@ -1,6 +1,7 @@
import { Serializable } from '../Serializable'; import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Layer } from './Layer'; import { Layer } from './Layer';
import { Macro } from './Macro';
export class Keymap extends Serializable<Keymap> { export class Keymap extends Serializable<Keymap> {
@@ -14,7 +15,7 @@ export class Keymap extends Serializable<Keymap> {
layers: Layer[]; layers: Layer[];
constructor(keymap?: Keymap) { constructor(keymap?: Keymap, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
super(); super();
if (!keymap) { if (!keymap) {
return; return;
@@ -24,40 +25,42 @@ export class Keymap extends Serializable<Keymap> {
this.description = keymap.description; this.description = keymap.description;
this.abbreviation = keymap.abbreviation; this.abbreviation = keymap.abbreviation;
this.isDefault = keymap.isDefault; 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 { fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
this.isDefault = jsObject.isDefault; this.isDefault = jsonObject.isDefault;
this.abbreviation = jsObject.abbreviation; this.abbreviation = jsonObject.abbreviation;
this.name = jsObject.name; this.name = jsonObject.name;
this.description = jsObject.description; this.description = jsonObject.description;
this.layers = jsObject.layers.map((layer: any) => new Layer().fromJsObject(layer)); this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer, getKeymap, getMacro));
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): Keymap { fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
this.isDefault = buffer.readBoolean();
this.abbreviation = buffer.readString(); this.abbreviation = buffer.readString();
this.isDefault = buffer.readBoolean();
this.name = buffer.readString(); this.name = buffer.readString();
this.description = buffer.readString(); this.description = buffer.readString();
this.layers = buffer.readArray<Layer>(Layer); this.layers = buffer.readArray<Layer>(uhkBuffer => {
return new Layer().fromBinary(uhkBuffer, getKeymap, getMacro);
});
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
isDefault: this.isDefault, isDefault: this.isDefault,
abbreviation: this.abbreviation, abbreviation: this.abbreviation,
name: this.name, name: this.name,
description: this.description, description: this.description,
layers: this.layers.map(layer => layer.toJsObject()) layers: this.layers.map(layer => layer.toJsonObject())
}; };
} }
_toBinary(buffer: UhkBuffer): void { _toBinary(buffer: UhkBuffer): void {
buffer.writeBoolean(this.isDefault);
buffer.writeString(this.abbreviation); buffer.writeString(this.abbreviation);
buffer.writeBoolean(this.isDefault);
buffer.writeString(this.name); buffer.writeString(this.name);
buffer.writeString(this.description); buffer.writeString(this.description);
buffer.writeArray(this.layers); buffer.writeArray(this.layers);

View File

@@ -1,6 +1,8 @@
import { AnimationKeyboard } from '../../components/svg/wrap'; import { AnimationKeyboard } from '../../components/svg/wrap';
import { Serializable } from '../Serializable'; import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Keymap } from './Keymap';
import { Macro } from './Macro';
import { Module } from './Module'; import { Module } from './Module';
export class Layer extends Serializable<Layer> { export class Layer extends Serializable<Layer> {
@@ -8,28 +10,30 @@ export class Layer extends Serializable<Layer> {
modules: Module[]; modules: Module[];
animation: AnimationKeyboard; animation: AnimationKeyboard;
constructor(layers?: Layer) { constructor(layers?: Layer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
super(); super();
if (!layers) { if (!layers) {
return; 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; this.animation = layers.animation;
} }
_fromJsObject(jsObject: any): Layer { fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
this.modules = jsObject.modules.map((module: any) => new Module().fromJsObject(module)); this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module, getKeymap, getMacro));
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): Layer { fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
this.modules = buffer.readArray<Module>(Module); this.modules = buffer.readArray<Module>(uhkBuffer => {
return new Module().fromBinary(uhkBuffer, getKeymap, getMacro);
});
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
modules: this.modules.map(module => module.toJsObject()) modules: this.modules.map(module => module.toJsonObject())
}; };
} }

View File

@@ -28,16 +28,16 @@ export class Macro extends Serializable<Macro> {
this.macroActions = other.macroActions.map(macroAction => MacroActionHelper.createMacroAction(macroAction)); this.macroActions = other.macroActions.map(macroAction => MacroActionHelper.createMacroAction(macroAction));
} }
_fromJsObject(jsObject: any): Macro { fromJsonObject(jsonObject: any): Macro {
this.id = jsObject.id; this.id = jsonObject.id;
this.isLooped = jsObject.isLooped; this.isLooped = jsonObject.isLooped;
this.isPrivate = jsObject.isPrivate; this.isPrivate = jsonObject.isPrivate;
this.name = jsObject.name; this.name = jsonObject.name;
this.macroActions = jsObject.macroActions.map((macroAction: any) => MacroActionHelper.createMacroAction(macroAction)); this.macroActions = jsonObject.macroActions.map((macroAction: any) => MacroActionHelper.createMacroAction(macroAction));
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): Macro { fromBinary(buffer: UhkBuffer): Macro {
this.id = buffer.readUInt8(); this.id = buffer.readUInt8();
this.isLooped = buffer.readBoolean(); this.isLooped = buffer.readBoolean();
this.isPrivate = buffer.readBoolean(); this.isPrivate = buffer.readBoolean();
@@ -50,13 +50,13 @@ export class Macro extends Serializable<Macro> {
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
id: this.id, id: this.id,
isLooped: this.isLooped, isLooped: this.isLooped,
isPrivate: this.isPrivate, isPrivate: this.isPrivate,
name: this.name, name: this.name,
macroActions: this.macroActions.map(macroAction => macroAction.toJsObject()) macroActions: this.macroActions.map(macroAction => macroAction.toJsonObject())
}; };
} }

View File

@@ -2,6 +2,8 @@ import { assertEnum, assertUInt8 } from '../assert';
import { Serializable } from '../Serializable'; import { Serializable } from '../Serializable';
import { UhkBuffer } from '../UhkBuffer'; import { UhkBuffer } from '../UhkBuffer';
import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action'; import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action';
import { Keymap } from './Keymap';
import { Macro } from './Macro';
enum PointerRole { enum PointerRole {
none, none,
@@ -17,42 +19,45 @@ export class Module extends Serializable<Module> {
keyActions: KeyAction[]; keyActions: KeyAction[];
@assertEnum(PointerRole) @assertEnum(PointerRole)
private pointerRole: PointerRole; pointerRole: PointerRole;
constructor(other?: Module) { constructor(other?: Module, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
super(); super();
if (!other) { if (!other) {
return; return;
} }
this.id = other.id; 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; this.pointerRole = other.pointerRole;
} }
_fromJsObject(jsObject: any): Module {
this.id = jsObject.id; fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
this.pointerRole = PointerRole[<string>jsObject.pointerRole]; this.id = jsonObject.id;
this.keyActions = jsObject.keyActions.map((keyAction: any) => KeyActionHelper.createKeyAction(keyAction)); this.pointerRole = PointerRole[<string>jsonObject.pointerRole];
this.keyActions = jsonObject.keyActions.map((keyAction: any) => {
return KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro);
});
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): Module { fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
this.id = buffer.readUInt8(); this.id = buffer.readUInt8();
this.pointerRole = buffer.readUInt8(); this.pointerRole = buffer.readUInt8();
let keyActionsLength: number = buffer.readCompactLength(); let keyActionsLength: number = buffer.readCompactLength();
this.keyActions = []; this.keyActions = [];
for (let i = 0; i < keyActionsLength; ++i) { for (let i = 0; i < keyActionsLength; ++i) {
this.keyActions.push(KeyActionHelper.createKeyAction(buffer)); this.keyActions.push(KeyActionHelper.createKeyAction(buffer, getKeymap, getMacro));
} }
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
id: this.id, id: this.id,
pointerRole: PointerRole[this.pointerRole], pointerRole: PointerRole[this.pointerRole],
keyActions: this.keyActions.map(keyAction => { keyActions: this.keyActions.map(keyAction => {
if (keyAction) { if (keyAction) {
return keyAction.toJsObject(); return keyAction.toJsonObject();
} }
}) })
}; };

View File

@@ -20,15 +20,15 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
@assertUInt8 @assertUInt8
maxPointerSpeed: number; maxPointerSpeed: number;
_fromJsObject(jsObject: any): ModuleConfiguration { fromJsonObject(jsonObject: any): ModuleConfiguration {
this.id = jsObject.id; this.id = jsonObject.id;
this.initialPointerSpeed = jsObject.initialPointerSpeed; this.initialPointerSpeed = jsonObject.initialPointerSpeed;
this.pointerAcceleration = jsObject.pointerAcceleration; this.pointerAcceleration = jsonObject.pointerAcceleration;
this.maxPointerSpeed = jsObject.maxPointerSpeed; this.maxPointerSpeed = jsonObject.maxPointerSpeed;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): ModuleConfiguration { fromBinary(buffer: UhkBuffer): ModuleConfiguration {
this.id = buffer.readUInt8(); this.id = buffer.readUInt8();
this.initialPointerSpeed = buffer.readUInt8(); this.initialPointerSpeed = buffer.readUInt8();
this.pointerAcceleration = buffer.readUInt8(); this.pointerAcceleration = buffer.readUInt8();
@@ -36,7 +36,7 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
id: this.id, id: this.id,
initialPointerSpeed: this.initialPointerSpeed, initialPointerSpeed: this.initialPointerSpeed,

View File

@@ -30,44 +30,60 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
@assertUInt32 @assertUInt32
epilogue: number; epilogue: number;
_fromJsObject(jsObject: any): UhkConfiguration { fromJsonObject(jsonObject: any): UhkConfiguration {
this.signature = jsObject.signature; this.signature = jsonObject.signature;
this.dataModelVersion = jsObject.dataModelVersion; this.dataModelVersion = jsonObject.dataModelVersion;
this.prologue = jsObject.prologue; this.prologue = jsonObject.prologue;
this.hardwareId = jsObject.hardwareId; this.hardwareId = jsonObject.hardwareId;
this.brandId = jsObject.brandId; this.brandId = jsonObject.brandId;
this.moduleConfigurations = jsObject.moduleConfigurations.map((moduleConfiguration: any) => { this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => {
return new ModuleConfiguration().fromJsObject(moduleConfiguration); return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
}); });
this.keymaps = jsObject.keymaps.map((keymap: any) => new Keymap().fromJsObject(keymap)); this.macros = jsonObject.macros.map((macro: any) => new Macro().fromJsonObject(macro));
this.macros = jsObject.macros.map((macro: any) => new Macro().fromJsObject(macro)); this.keymaps = jsonObject.keymaps.map((keymap: any) => {
this.epilogue = jsObject.epilogue; 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; return this;
} }
_fromBinary(buffer: UhkBuffer): UhkConfiguration { fromBinary(buffer: UhkBuffer): UhkConfiguration {
this.signature = buffer.readString(); this.signature = buffer.readString();
this.dataModelVersion = buffer.readUInt8(); this.dataModelVersion = buffer.readUInt8();
this.prologue = buffer.readUInt32(); this.prologue = buffer.readUInt32();
this.hardwareId = buffer.readUInt8(); this.hardwareId = buffer.readUInt8();
this.brandId = buffer.readUInt8(); this.brandId = buffer.readUInt8();
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(ModuleConfiguration); this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
this.keymaps = buffer.readArray<Keymap>(Keymap); return new ModuleConfiguration().fromBinary(uhkBuffer);
this.macros = buffer.readArray<Macro>(Macro); });
this.macros = buffer.readArray<Macro>(uhkBuffer => new Macro().fromBinary(uhkBuffer));
this.keymaps = [];
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => {
return new Keymap().fromBinary(uhkBuffer, abbrevation => this.getKeymap(abbrevation, true), this.getMacro.bind(this));
});
this.epilogue = buffer.readUInt32(); this.epilogue = buffer.readUInt32();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
signature: this.signature, signature: this.signature,
dataModelVersion: this.dataModelVersion, dataModelVersion: this.dataModelVersion,
prologue: this.prologue, prologue: this.prologue,
hardwareId: this.hardwareId, hardwareId: this.hardwareId,
brandId: this.brandId, brandId: this.brandId,
moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsObject()), moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()),
keymaps: this.keymaps.map(keymap => keymap.toJsObject()), keymaps: this.keymaps.map(keymap => keymap.toJsonObject()),
macros: this.macros.map(macro => macro.toJsObject()), macros: this.macros.map(macro => macro.toJsonObject()),
epilogue: this.epilogue epilogue: this.epilogue
}; };
} }
@@ -79,8 +95,8 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
buffer.writeUInt8(this.hardwareId); buffer.writeUInt8(this.hardwareId);
buffer.writeUInt8(this.brandId); buffer.writeUInt8(this.brandId);
buffer.writeArray(this.moduleConfigurations); buffer.writeArray(this.moduleConfigurations);
buffer.writeArray(this.keymaps);
buffer.writeArray(this.macros); buffer.writeArray(this.macros);
buffer.writeArray(this.keymaps);
buffer.writeUInt32(this.epilogue); buffer.writeUInt32(this.epilogue);
} }
@@ -88,11 +104,18 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
return `<UhkConfiguration signature="${this.signature}">`; return `<UhkConfiguration signature="${this.signature}">`;
} }
getKeymap(keymapAbbreviation: string): Keymap { getKeymap(keymapAbbreviation: string, createIfNotExist = false): Keymap {
return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); 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 { getMacro(macroId: number): Macro {
return this.macros.find(macro => macroId === macro.id); return this.macros.find(macro => macroId === macro.id);
} }
} }

View File

@@ -53,8 +53,6 @@ export abstract class KeyAction extends Serializable<KeyAction> {
return readKeyActionId; return readKeyActionId;
} }
abstract _fromJsObject(jsObject: any): KeyAction; abstract _toJsonObject(): any;
abstract _fromBinary(buffer: UhkBuffer): KeyAction;
abstract _toJsObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract _toBinary(buffer: UhkBuffer): void;
} }

View File

@@ -10,7 +10,7 @@ export enum KeystrokeActionFlag {
longPressAction = 1 << 2, longPressAction = 1 << 2,
} }
interface JsObjectKeystrokeAction { interface JsonObjectKeystrokeAction {
keyActionType: string; keyActionType: string;
scancode?: number; scancode?: number;
modifierMask?: number; modifierMask?: number;
@@ -40,15 +40,15 @@ export class KeystrokeAction extends KeyAction {
this.longPressAction = other.longPressAction; this.longPressAction = other.longPressAction;
} }
_fromJsObject(jsObject: JsObjectKeystrokeAction): KeystrokeAction { fromJsonObject(jsonObject: JsonObjectKeystrokeAction): KeystrokeAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsonObject);
this.scancode = jsObject.scancode; this.scancode = jsonObject.scancode;
this.modifierMask = jsObject.modifierMask; this.modifierMask = jsonObject.modifierMask;
this.longPressAction = LongPressAction[jsObject.longPressAction]; this.longPressAction = LongPressAction[jsonObject.longPressAction];
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): KeystrokeAction { fromBinary(buffer: UhkBuffer): KeystrokeAction {
let keyActionId: KeyActionId = this.readAndAssertKeyActionId(buffer); let keyActionId: KeyActionId = this.readAndAssertKeyActionId(buffer);
let flags: number = keyActionId - KeyActionId.KeystrokeAction; let flags: number = keyActionId - KeyActionId.KeystrokeAction;
if (flags & KeystrokeActionFlag.scancode) { if (flags & KeystrokeActionFlag.scancode) {
@@ -63,24 +63,24 @@ export class KeystrokeAction extends KeyAction {
return this; return this;
} }
_toJsObject(): JsObjectKeystrokeAction { _toJsonObject(): JsonObjectKeystrokeAction {
let jsObject: JsObjectKeystrokeAction = { let jsonObject: JsonObjectKeystrokeAction = {
keyActionType: keyActionType.KeystrokeAction keyActionType: keyActionType.KeystrokeAction
}; };
if (this.hasScancode()) { if (this.hasScancode()) {
jsObject.scancode = this.scancode; jsonObject.scancode = this.scancode;
} }
if (this.hasActiveModifier()) { if (this.hasActiveModifier()) {
jsObject.modifierMask = this.modifierMask; jsonObject.modifierMask = this.modifierMask;
} }
if (this.hasLongPressAction()) { if (this.hasLongPressAction()) {
jsObject.longPressAction = LongPressAction[this.longPressAction]; jsonObject.longPressAction = LongPressAction[this.longPressAction];
} }
return jsObject; return jsonObject;
} }
_toBinary(buffer: UhkBuffer) { _toBinary(buffer: UhkBuffer) {

View File

@@ -31,19 +31,19 @@ export class MouseAction extends KeyAction {
this.mouseAction = other.mouseAction; this.mouseAction = other.mouseAction;
} }
_fromJsObject(jsObject: any): MouseAction { fromJsonObject(jsObject: any): MouseAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.mouseAction = MouseActionParam[<string>jsObject.mouseAction]; this.mouseAction = MouseActionParam[<string>jsObject.mouseAction];
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): MouseAction { fromBinary(buffer: UhkBuffer): MouseAction {
this.readAndAssertKeyActionId(buffer); this.readAndAssertKeyActionId(buffer);
this.mouseAction = buffer.readUInt8(); this.mouseAction = buffer.readUInt8();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
keyActionType: keyActionType.MouseAction, keyActionType: keyActionType.MouseAction,
mouseAction: MouseActionParam[this.mouseAction] mouseAction: MouseActionParam[this.mouseAction]

View File

@@ -9,17 +9,17 @@ import { KeyAction, KeyActionId, keyActionType } from './KeyAction';
export class NoneAction extends KeyAction { export class NoneAction extends KeyAction {
_fromJsObject(jsObject: any): NoneAction { fromJsonObject(jsonObject: any): NoneAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsonObject);
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): NoneAction { fromBinary(buffer: UhkBuffer): NoneAction {
this.readAndAssertKeyActionId(buffer); this.readAndAssertKeyActionId(buffer);
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
keyActionType: keyActionType.NoneAction keyActionType: keyActionType.NoneAction
}; };

View File

@@ -1,45 +1,49 @@
import { assertUInt8 } from '../../assert';
import { UhkBuffer } from '../../UhkBuffer'; import { UhkBuffer } from '../../UhkBuffer';
import { Macro } from '../Macro';
import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; import { KeyAction, KeyActionId, keyActionType } from './KeyAction';
export class PlayMacroAction extends KeyAction { export class PlayMacroAction extends KeyAction {
@assertUInt8 macro: Macro;
macroId: number;
constructor(other?: PlayMacroAction) { constructor(parameter?: PlayMacroAction | Macro) {
super(); super();
if (!other) { if (!parameter) {
return; return;
} }
this.macroId = other.macroId; if (parameter instanceof PlayMacroAction) {
this.macro = parameter.macro;
} else {
this.macro = parameter;
}
} }
_fromJsObject(jsObject: any): PlayMacroAction { fromJsonObject(jsonObject: any, getMacro: (macroId: number) => Macro): PlayMacroAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsonObject);
this.macroId = jsObject.macroId; this.macro = getMacro(jsonObject.macroId);
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): PlayMacroAction { fromBinary(buffer: UhkBuffer, getMacro: (macroId: number) => Macro): PlayMacroAction {
this.readAndAssertKeyActionId(buffer); this.readAndAssertKeyActionId(buffer);
this.macroId = buffer.readUInt8(); const macroId = buffer.readUInt8();
this.macro = getMacro(macroId);
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
keyActionType: keyActionType.PlayMacroAction, keyActionType: keyActionType.PlayMacroAction,
macroId: this.macroId macroId: this.macro.id
}; };
} }
_toBinary(buffer: UhkBuffer) { _toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.PlayMacroAction); buffer.writeUInt8(KeyActionId.PlayMacroAction);
buffer.writeUInt8(this.macroId); buffer.writeUInt8(this.macro.id);
} }
toString(): string { toString(): string {
return `<PlayMacroAction macroId="${this.macroId}">`; return `<PlayMacroAction macroId="${this.macro.id}">`;
} }
} }

View File

@@ -1,43 +1,49 @@
import { UhkBuffer } from '../../UhkBuffer'; import { UhkBuffer } from '../../UhkBuffer';
import { Keymap } from '../Keymap';
import { KeyAction, KeyActionId, keyActionType } from './KeyAction'; import { KeyAction, KeyActionId, keyActionType } from './KeyAction';
export class SwitchKeymapAction extends KeyAction { export class SwitchKeymapAction extends KeyAction {
keymapAbbreviation: string; keymap: Keymap;
constructor(other?: SwitchKeymapAction) { constructor(parameter?: SwitchKeymapAction | Keymap) {
super(); super();
if (!other) { if (!parameter) {
return; return;
} }
this.keymapAbbreviation = other.keymapAbbreviation; if (parameter instanceof SwitchKeymapAction) {
this.keymap = parameter.keymap;
} else {
this.keymap = parameter;
}
} }
_fromJsObject(jsObject: any): SwitchKeymapAction { fromJsonObject(jsonObject: any, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsonObject);
this.keymapAbbreviation = jsObject.keymapAbbreviation; this.keymap = getKeymap(jsonObject.keymapAbbreviation);
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): SwitchKeymapAction { fromBinary(buffer: UhkBuffer, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction {
this.readAndAssertKeyActionId(buffer); this.readAndAssertKeyActionId(buffer);
this.keymapAbbreviation = buffer.readString(); const keymapAbbreviation = buffer.readString();
this.keymap = getKeymap(keymapAbbreviation);
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
keyActionType: keyActionType.SwitchKeymapAction, keyActionType: keyActionType.SwitchKeymapAction,
keymapAbbreviation: this.keymapAbbreviation keymapAbbreviation: this.keymap.abbreviation
}; };
} }
_toBinary(buffer: UhkBuffer) { _toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.SwitchKeymapAction); buffer.writeUInt8(KeyActionId.SwitchKeymapAction);
buffer.writeString(this.keymapAbbreviation); buffer.writeString(this.keymap.abbreviation);
} }
toString(): string { toString(): string {
return `<SwitchKeymapAction keymapAbbreviation="${this.keymapAbbreviation}">`; return `<SwitchKeymapAction keymapAbbreviation="${this.keymap.abbreviation}">`;
} }
} }

View File

@@ -24,21 +24,21 @@ export class SwitchLayerAction extends KeyAction {
this.layer = other.layer; this.layer = other.layer;
} }
_fromJsObject(jsObject: any): SwitchLayerAction { fromJsonObject(jsonObject: any): SwitchLayerAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsonObject);
this.layer = LayerName[<string>jsObject.layer]; this.layer = LayerName[<string>jsonObject.layer];
this.isLayerToggleable = jsObject.toggle; this.isLayerToggleable = jsonObject.toggle;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): SwitchLayerAction { fromBinary(buffer: UhkBuffer): SwitchLayerAction {
this.readAndAssertKeyActionId(buffer); this.readAndAssertKeyActionId(buffer);
this.layer = buffer.readUInt8(); this.layer = buffer.readUInt8();
this.isLayerToggleable = buffer.readBoolean(); this.isLayerToggleable = buffer.readBoolean();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
keyActionType: keyActionType.SwitchLayerAction, keyActionType: keyActionType.SwitchLayerAction,
layer: LayerName[this.layer], layer: LayerName[this.layer],

View File

@@ -10,19 +10,30 @@ import {
keyActionType keyActionType
} from './index'; } from './index';
import { Keymap } from '../Keymap';
import { Macro } from '../Macro';
export class Helper { 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) { if (source instanceof KeyAction) {
return Helper.fromKeyAction(source); return Helper.fromKeyAction(source);
} else if (source instanceof UhkBuffer) { } else if (source instanceof UhkBuffer) {
return Helper.fromUhkBuffer(source); return Helper.fromUhkBuffer(source, getKeymap, getMacro);
} else { } 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(); let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack(); buffer.backtrack();
@@ -37,11 +48,11 @@ export class Helper {
case KeyActionId.SwitchLayerAction: case KeyActionId.SwitchLayerAction:
return new SwitchLayerAction().fromBinary(buffer); return new SwitchLayerAction().fromBinary(buffer);
case KeyActionId.SwitchKeymapAction: case KeyActionId.SwitchKeymapAction:
return new SwitchKeymapAction().fromBinary(buffer); return new SwitchKeymapAction().fromBinary(buffer, getKeymap);
case KeyActionId.MouseAction: case KeyActionId.MouseAction:
return new MouseAction().fromBinary(buffer); return new MouseAction().fromBinary(buffer);
case KeyActionId.PlayMacroAction: case KeyActionId.PlayMacroAction:
return new PlayMacroAction().fromBinary(buffer); return new PlayMacroAction().fromBinary(buffer, getMacro);
default: default:
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`; throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
} }
@@ -63,22 +74,26 @@ export class Helper {
return newKeyAction; return newKeyAction;
} }
private static fromJSONObject(keyAction: any): KeyAction { private static fromJSONObject(
keyAction: any,
getKeymap?: (abbrevation: string) => Keymap,
getMacro?: (macroId: number) => Macro
): KeyAction {
if (!keyAction) { if (!keyAction) {
return; return;
} }
switch (keyAction.keyActionType) { switch (keyAction.keyActionType) {
case keyActionType.KeystrokeAction: case keyActionType.KeystrokeAction:
return new KeystrokeAction().fromJsObject(keyAction); return new KeystrokeAction().fromJsonObject(keyAction);
case keyActionType.SwitchLayerAction: case keyActionType.SwitchLayerAction:
return new SwitchLayerAction().fromJsObject(keyAction); return new SwitchLayerAction().fromJsonObject(keyAction);
case keyActionType.SwitchKeymapAction: case keyActionType.SwitchKeymapAction:
return new SwitchKeymapAction().fromJsObject(keyAction); return new SwitchKeymapAction().fromJsonObject(keyAction, getKeymap);
case keyActionType.MouseAction: case keyActionType.MouseAction:
return new MouseAction().fromJsObject(keyAction); return new MouseAction().fromJsonObject(keyAction);
case keyActionType.PlayMacroAction: case keyActionType.PlayMacroAction:
return new PlayMacroAction().fromJsObject(keyAction); return new PlayMacroAction().fromJsonObject(keyAction, getMacro);
default: default:
throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`; throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`;
} }

View File

@@ -15,19 +15,19 @@ export class DelayMacroAction extends MacroAction {
this.delay = other.delay; this.delay = other.delay;
} }
_fromJsObject(jsObject: any): DelayMacroAction { fromJsonObject(jsObject: any): DelayMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.delay = jsObject.delay; this.delay = jsObject.delay;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): DelayMacroAction { fromBinary(buffer: UhkBuffer): DelayMacroAction {
this.readAndAssertMacroActionId(buffer); this.readAndAssertMacroActionId(buffer);
this.delay = buffer.readUInt16(); this.delay = buffer.readUInt16();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
macroActionType: macroActionType.DelayMacroAction, macroActionType: macroActionType.DelayMacroAction,
delay: this.delay delay: this.delay

View File

@@ -93,7 +93,7 @@ export class EditableMacroAction {
} }
fromKeyAction(keyAction: KeyAction): void { fromKeyAction(keyAction: KeyAction): void {
let data = keyAction.toJsObject(); let data = keyAction.toJsonObject();
this.scancode = data.scancode; this.scancode = data.scancode;
this.modifierMask = data.modifierMask; this.modifierMask = data.modifierMask;
} }
@@ -101,7 +101,7 @@ export class EditableMacroAction {
toKeystrokeAction(): KeystrokeAction { toKeystrokeAction(): KeystrokeAction {
let data = this.toJsObject(); let data = this.toJsObject();
data.keyActionType = keyActionType.KeystrokeAction; data.keyActionType = keyActionType.KeystrokeAction;
return <KeystrokeAction>(new KeystrokeAction().fromJsObject(data)); return <KeystrokeAction>(new KeystrokeAction().fromJsonObject(data));
} }
setMouseButtons(buttonStates: boolean[]): void { setMouseButtons(buttonStates: boolean[]): void {
@@ -124,19 +124,19 @@ export class EditableMacroAction {
switch (this.macroActionType) { switch (this.macroActionType) {
// Delay action // Delay action
case macroActionType.DelayMacroAction: case macroActionType.DelayMacroAction:
return new DelayMacroAction().fromJsObject({ return new DelayMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
delay: this.delay delay: this.delay
}); });
// Text action // Text action
case macroActionType.TextMacroAction: case macroActionType.TextMacroAction:
return new TextMacroAction().fromJsObject({ return new TextMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
text: this.text text: this.text
}); });
// Keypress action // Keypress action
case macroActionType.KeyMacroAction: case macroActionType.KeyMacroAction:
return new KeyMacroAction().fromJsObject({ return new KeyMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
action: MacroSubAction[this.action], action: MacroSubAction[this.action],
scancode: this.scancode, scancode: this.scancode,
@@ -144,19 +144,19 @@ export class EditableMacroAction {
}); });
// Mouse actions // Mouse actions
case macroActionType.MouseButtonMacroAction: case macroActionType.MouseButtonMacroAction:
return new MouseButtonMacroAction().fromJsObject({ return new MouseButtonMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
action: MacroSubAction[this.action], action: MacroSubAction[this.action],
mouseButtonsMask: this.mouseButtonsMask mouseButtonsMask: this.mouseButtonsMask
}); });
case macroActionType.MoveMouseMacroAction: case macroActionType.MoveMouseMacroAction:
return new MoveMouseMacroAction().fromJsObject({ return new MoveMouseMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
x: this.moveX, x: this.moveX,
y: this.moveY y: this.moveY
}); });
case macroActionType.ScrollMouseMacroAction: case macroActionType.ScrollMouseMacroAction:
return new ScrollMouseMacroAction().fromJsObject({ return new ScrollMouseMacroAction().fromJsonObject({
macroActionType: this.macroActionType, macroActionType: this.macroActionType,
x: this.scrollX, x: this.scrollX,
y: this.scrollY y: this.scrollY

View File

@@ -33,7 +33,7 @@ export class KeyMacroAction extends MacroAction {
this.modifierMask = other.modifierMask; this.modifierMask = other.modifierMask;
} }
_fromJsObject(jsObject: JsObjectKeyMacroAction): KeyMacroAction { fromJsonObject(jsObject: JsObjectKeyMacroAction): KeyMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.action = MacroSubAction[jsObject.action]; this.action = MacroSubAction[jsObject.action];
this.scancode = jsObject.scancode; this.scancode = jsObject.scancode;
@@ -41,7 +41,7 @@ export class KeyMacroAction extends MacroAction {
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): KeyMacroAction { fromBinary(buffer: UhkBuffer): KeyMacroAction {
let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer); let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer);
let keyMacroType: number = macroActionId - MacroActionId.KeyMacroAction; let keyMacroType: number = macroActionId - MacroActionId.KeyMacroAction;
this.action = Math.floor(keyMacroType / NUM_OF_COMBINATIONS); this.action = Math.floor(keyMacroType / NUM_OF_COMBINATIONS);
@@ -55,7 +55,7 @@ export class KeyMacroAction extends MacroAction {
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
let jsObject: JsObjectKeyMacroAction = { let jsObject: JsObjectKeyMacroAction = {
macroActionType: macroActionType.KeyMacroAction, macroActionType: macroActionType.KeyMacroAction,
action: MacroSubAction[this.action] action: MacroSubAction[this.action]

View File

@@ -73,8 +73,6 @@ export abstract class MacroAction extends Serializable<MacroAction> {
return readMacroActionId; return readMacroActionId;
} }
abstract _fromJsObject(jsObject: any): MacroAction; abstract _toJsonObject(): any;
abstract _fromBinary(buffer: UhkBuffer): MacroAction;
abstract _toJsObject(): any;
abstract _toBinary(buffer: UhkBuffer): void; abstract _toBinary(buffer: UhkBuffer): void;
} }

View File

@@ -30,21 +30,21 @@ export class MouseButtonMacroAction extends MacroAction {
this.mouseButtonsMask = other.mouseButtonsMask; this.mouseButtonsMask = other.mouseButtonsMask;
} }
_fromJsObject(jsObject: JsObjectMouseButtonMacroAction): MouseButtonMacroAction { fromJsonObject(jsObject: JsObjectMouseButtonMacroAction): MouseButtonMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.action = MacroSubAction[jsObject.action]; this.action = MacroSubAction[jsObject.action];
this.mouseButtonsMask = jsObject.mouseButtonsMask; this.mouseButtonsMask = jsObject.mouseButtonsMask;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): MouseButtonMacroAction { fromBinary(buffer: UhkBuffer): MouseButtonMacroAction {
let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer); let macroActionId: MacroActionId = this.readAndAssertMacroActionId(buffer);
this.action = macroActionId - MacroActionId.MouseButtonMacroAction; this.action = macroActionId - MacroActionId.MouseButtonMacroAction;
this.mouseButtonsMask = buffer.readUInt8(); this.mouseButtonsMask = buffer.readUInt8();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
macroActionType: macroActionType.MouseButtonMacroAction, macroActionType: macroActionType.MouseButtonMacroAction,
action: MacroSubAction[this.action], action: MacroSubAction[this.action],

View File

@@ -19,21 +19,21 @@ export class MoveMouseMacroAction extends MacroAction {
this.y = other.y; this.y = other.y;
} }
_fromJsObject(jsObject: any): MoveMouseMacroAction { fromJsonObject(jsObject: any): MoveMouseMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.x = jsObject.x; this.x = jsObject.x;
this.y = jsObject.y; this.y = jsObject.y;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): MoveMouseMacroAction { fromBinary(buffer: UhkBuffer): MoveMouseMacroAction {
this.readAndAssertMacroActionId(buffer); this.readAndAssertMacroActionId(buffer);
this.x = buffer.readInt16(); this.x = buffer.readInt16();
this.y = buffer.readInt16(); this.y = buffer.readInt16();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
macroActionType: macroActionType.MoveMouseMacroAction, macroActionType: macroActionType.MoveMouseMacroAction,
x: this.x, x: this.x,

View File

@@ -19,21 +19,21 @@ export class ScrollMouseMacroAction extends MacroAction {
this.y = other.y; this.y = other.y;
} }
_fromJsObject(jsObject: any): ScrollMouseMacroAction { fromJsonObject(jsObject: any): ScrollMouseMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.x = jsObject.x; this.x = jsObject.x;
this.y = jsObject.y; this.y = jsObject.y;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): ScrollMouseMacroAction { fromBinary(buffer: UhkBuffer): ScrollMouseMacroAction {
this.readAndAssertMacroActionId(buffer); this.readAndAssertMacroActionId(buffer);
this.x = buffer.readInt16(); this.x = buffer.readInt16();
this.y = buffer.readInt16(); this.y = buffer.readInt16();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
macroActionType: macroActionType.ScrollMouseMacroAction, macroActionType: macroActionType.ScrollMouseMacroAction,
x: this.x, x: this.x,

View File

@@ -13,19 +13,19 @@ export class TextMacroAction extends MacroAction {
this.text = other.text; this.text = other.text;
} }
_fromJsObject(jsObject: any): TextMacroAction { fromJsonObject(jsObject: any): TextMacroAction {
this.assertMacroActionType(jsObject); this.assertMacroActionType(jsObject);
this.text = jsObject.text; this.text = jsObject.text;
return this; return this;
} }
_fromBinary(buffer: UhkBuffer): TextMacroAction { fromBinary(buffer: UhkBuffer): TextMacroAction {
this.readAndAssertMacroActionId(buffer); this.readAndAssertMacroActionId(buffer);
this.text = buffer.readString(); this.text = buffer.readString();
return this; return this;
} }
_toJsObject(): any { _toJsonObject(): any {
return { return {
macroActionType: macroActionType.TextMacroAction, macroActionType: macroActionType.TextMacroAction,
text: this.text text: this.text

View File

@@ -70,17 +70,17 @@ export class Helper {
private static fromJSONObject(macroAction: any): MacroAction { private static fromJSONObject(macroAction: any): MacroAction {
switch (macroAction.macroActionType) { switch (macroAction.macroActionType) {
case macroActionType.KeyMacroAction: case macroActionType.KeyMacroAction:
return new KeyMacroAction().fromJsObject(macroAction); return new KeyMacroAction().fromJsonObject(macroAction);
case macroActionType.MouseButtonMacroAction: case macroActionType.MouseButtonMacroAction:
return new MouseButtonMacroAction().fromJsObject(macroAction); return new MouseButtonMacroAction().fromJsonObject(macroAction);
case macroActionType.MoveMouseMacroAction: case macroActionType.MoveMouseMacroAction:
return new MoveMouseMacroAction().fromJsObject(macroAction); return new MoveMouseMacroAction().fromJsonObject(macroAction);
case macroActionType.ScrollMouseMacroAction: case macroActionType.ScrollMouseMacroAction:
return new ScrollMouseMacroAction().fromJsObject(macroAction); return new ScrollMouseMacroAction().fromJsonObject(macroAction);
case macroActionType.DelayMacroAction: case macroActionType.DelayMacroAction:
return new DelayMacroAction().fromJsObject(macroAction); return new DelayMacroAction().fromJsonObject(macroAction);
case macroActionType.TextMacroAction: case macroActionType.TextMacroAction:
return new TextMacroAction().fromJsObject(macroAction); return new TextMacroAction().fromJsonObject(macroAction);
default: default:
throw `Invalid MacroAction.macroActionType: "${macroAction.macroActionType}"`; throw `Invalid MacroAction.macroActionType: "${macroAction.macroActionType}"`;
} }

View File

@@ -81,9 +81,16 @@ export class DataStorage {
} }
initUHKJson() { 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 = (<any[]>require('json!../../config-serializer/preset-keymaps.json')) this.uhkPresets = (<any[]>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 { getConfiguration(): UhkConfiguration {

View File

@@ -11,7 +11,7 @@ export class Local {
if (configJsonString) { if (configJsonString) {
const configJsonObject = JSON.parse(configJsonString); const configJsonObject = JSON.parse(configJsonString);
if (configJsonObject.dataModelVersion === this.dataModelVersion) { 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 { saveConfig(config: UhkConfiguration): void {
localStorage.setItem('config', JSON.stringify(config.toJsObject())); localStorage.setItem('config', JSON.stringify(config.toJsonObject()));
} }
} }

View File

@@ -8,7 +8,7 @@ let fs = require('fs');
let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json')); let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json'));
let config1Js = uhkConfig; let config1Js = uhkConfig;
let config1Ts: Serializable<UhkConfiguration> = new UhkConfiguration().fromJsObject(config1Js); let config1Ts: Serializable<UhkConfiguration> = new UhkConfiguration().fromJsonObject(config1Js);
let config1Buffer = new UhkBuffer(); let config1Buffer = new UhkBuffer();
config1Ts.toBinary(config1Buffer); config1Ts.toBinary(config1Buffer);
let config1BufferContent = config1Buffer.getBufferContent(); let config1BufferContent = config1Buffer.getBufferContent();
@@ -18,7 +18,7 @@ config1Buffer.offset = 0;
console.log(); console.log();
let config2Ts = new UhkConfiguration().fromBinary(config1Buffer); let config2Ts = new UhkConfiguration().fromBinary(config1Buffer);
console.log('\n'); console.log('\n');
let config2Js = config2Ts.toJsObject(); let config2Js = config2Ts.toJsonObject();
let config2Buffer = new UhkBuffer(); let config2Buffer = new UhkBuffer();
config2Ts.toBinary(config2Buffer); config2Ts.toBinary(config2Buffer);
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4)); fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4));