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() {
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;
}

View File

@@ -58,9 +58,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab {
return false;
}
let switchKeymapAction: SwitchKeymapAction = <SwitchKeymapAction>keyAction;
this.selectedKeymap = this.keymaps
.find((keymap: Keymap) => keymap.abbreviation === switchKeymapAction.keymapAbbreviation);
const switchKeymapAction: 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;
}
}

View File

@@ -60,8 +60,8 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab {
if (!(keyAction instanceof PlayMacroAction)) {
return false;
}
let playMacroAction: PlayMacroAction = <PlayMacroAction>keyAction;
this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macroId === macro.id);
const playMacroAction: 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;
}

View File

@@ -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;

View File

@@ -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<Keymap>(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({

View File

@@ -8,33 +8,12 @@ export abstract class Serializable<T> {
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<T> {
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<T> {
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;

View File

@@ -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<T extends Serializable<T>>(type: { new (): T }): T[] {
readArray<T extends Serializable<T>>(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;
}

View File

@@ -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<Keymap> {
@@ -14,7 +15,7 @@ export class Keymap extends Serializable<Keymap> {
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<Keymap> {
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>(Layer);
this.layers = buffer.readArray<Layer>(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);

View File

@@ -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<Layer> {
@@ -8,28 +10,30 @@ export class Layer extends Serializable<Layer> {
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>(Module);
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
this.modules = buffer.readArray<Module>(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())
};
}

View File

@@ -28,16 +28,16 @@ export class Macro extends Serializable<Macro> {
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<Macro> {
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())
};
}

View File

@@ -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<Module> {
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[<string>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[<string>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();
}
})
};

View File

@@ -20,15 +20,15 @@ export class ModuleConfiguration extends Serializable<ModuleConfiguration> {
@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<ModuleConfiguration> {
return this;
}
_toJsObject(): any {
_toJsonObject(): any {
return {
id: this.id,
initialPointerSpeed: this.initialPointerSpeed,

View File

@@ -30,44 +30,60 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
@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>(ModuleConfiguration);
this.keymaps = buffer.readArray<Keymap>(Keymap);
this.macros = buffer.readArray<Macro>(Macro);
this.moduleConfigurations = buffer.readArray<ModuleConfiguration>(uhkBuffer => {
return new ModuleConfiguration().fromBinary(uhkBuffer);
});
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();
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<UhkConfiguration> {
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<UhkConfiguration> {
return `<UhkConfiguration signature="${this.signature}">`;
}
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);
}
}

View File

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

View File

@@ -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) {

View File

@@ -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[<string>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]

View File

@@ -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
};

View File

@@ -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 `<PlayMacroAction macroId="${this.macroId}">`;
return `<PlayMacroAction macroId="${this.macro.id}">`;
}
}

View File

@@ -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 `<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;
}
_fromJsObject(jsObject: any): SwitchLayerAction {
this.assertKeyActionType(jsObject);
this.layer = LayerName[<string>jsObject.layer];
this.isLayerToggleable = jsObject.toggle;
fromJsonObject(jsonObject: any): SwitchLayerAction {
this.assertKeyActionType(jsonObject);
this.layer = LayerName[<string>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],

View File

@@ -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}"`;
}

View File

@@ -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

View File

@@ -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 <KeystrokeAction>(new KeystrokeAction().fromJsObject(data));
return <KeystrokeAction>(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

View File

@@ -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]

View File

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

View File

@@ -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],

View File

@@ -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,

View File

@@ -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,

View File

@@ -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

View File

@@ -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}"`;
}

View File

@@ -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 = (<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 {

View File

@@ -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()));
}
}

View File

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