Use ids in key actions instead of references (#239)

Partially reverts e48fdea
This commit is contained in:
József Farkas
2016-12-30 11:51:46 +01:00
committed by GitHub
parent 6a7efcc2b1
commit 9b2aa188e6
13 changed files with 165 additions and 164 deletions

View File

@@ -59,7 +59,8 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab {
}
const switchKeymapAction: SwitchKeymapAction = <SwitchKeymapAction>keyAction;
this.selectedKeymap = switchKeymapAction.keymap;
this.selectedKeymap = this.keymaps
.find((keymap: Keymap) => keymap.abbreviation === switchKeymapAction.keymapAbbreviation);
}
toKeyAction(): SwitchKeymapAction {
@@ -68,7 +69,7 @@ export class KeymapTabComponent implements OnInit, OnChanges, Tab {
}
const keymapAction = new SwitchKeymapAction();
keymapAction.keymap = this.selectedKeymap;
keymapAction.keymapAbbreviation = this.selectedKeymap.abbreviation;
return keymapAction;
}
}

View File

@@ -61,7 +61,7 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab {
return false;
}
const playMacroAction: PlayMacroAction = <PlayMacroAction>keyAction;
this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macro === macro);
this.selectedMacroIndex = this.macros.findIndex(macro => playMacroAction.macroId === macro.id);
return true;
}
@@ -71,7 +71,7 @@ export class MacroTabComponent implements OnInit, OnChanges, OnDestroy, Tab {
}
const keymapAction = new PlayMacroAction();
keymapAction.macro = this.macros[this.selectedMacroIndex];
keymapAction.macroId = this.macros[this.selectedMacroIndex].id;
return keymapAction;
}

View File

@@ -283,13 +283,14 @@ 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.keymap.abbreviation;
this.labelSource = keyAction.keymapAbbreviation;
} else if (this.keyAction instanceof PlayMacroAction) {
let keyAction: PlayMacroAction = this.keyAction as PlayMacroAction;
const macro: Macro = this.macros.find((macro: Macro) => macro.id === keyAction.macroId);
this.labelType = LabelTypes.IconText;
this.labelSource = {
icon: this.mapper.getIcon('macro'),
text: keyAction.macro.name
text: macro.name
};
} else if (this.keyAction instanceof MouseAction) {
this.labelType = LabelTypes.MouseKey;

View File

@@ -16,7 +16,7 @@
>
<div class="tooltip-arrow"></div>
<div class="tooltip-inner">
<p *ngFor="let item of tooltipData.content">
<p *ngFor="let item of tooltipData.content | async">
{{ item.name }}: {{ item.value }}
</p>
</div>

View File

@@ -12,6 +12,9 @@ import {
SimpleChanges
} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import { Store } from '@ngrx/store';
import { MapperService } from '../../../services/mapper.service';
@@ -35,6 +38,11 @@ import { AppState } from '../../../store';
import { KeymapActions } from '../../../store/actions';
import { PopoverComponent } from '../../popover';
interface NameValuePair {
name: string;
value: string;
}
@Component({
selector: 'svg-keyboard-wrap',
template: require('./svg-keyboard-wrap.component.html'),
@@ -46,14 +54,19 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
@Input() popoverEnabled: boolean = true;
@Input() tooltipEnabled: boolean = false;
@ViewChild(PopoverComponent, { read: ElementRef}) popover: ElementRef;
@ViewChild(PopoverComponent, { read: ElementRef }) popover: ElementRef;
private popoverShown: boolean;
private keyEditConfig: { moduleId: number, keyId: number };
private popoverInitKeyAction: KeyAction;
private keybindAnimationEnabled: boolean;
private currentLayer: number = 0;
private tooltipData: { posTop: number, posLeft: number, content: { name: string, value: string }[], show: boolean };
private tooltipData: {
posTop: number,
posLeft: number,
content: Observable<NameValuePair[]>,
show: boolean
};
private layers: Layer[];
private keyPosition: ClientRect;
private wrapPosition: ClientRect;
@@ -89,7 +102,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
this.tooltipData = {
posTop: 0,
posLeft: 0,
content: [],
content: Observable.of([]),
show: false
};
}
@@ -139,7 +152,7 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
}
}
onCapture(moduleId: number, keyId: number, captured: {code: number, left: boolean[], right: boolean[]}): void {
onCapture(moduleId: number, keyId: number, captured: { code: number, left: boolean[], right: boolean[] }): void {
let keystrokeAction: KeystrokeAction = new KeystrokeAction();
const modifiers = captured.left.concat(captured.right).map(x => x ? 1 : 0);
@@ -194,13 +207,30 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
posTop = position.top + position.height;
}
let content: {
name: string,
value: string
}[] = [];
this.tooltipData = {
posLeft: posLeft,
posTop: posTop,
content: this.getKeyActionContent(keyAction),
show: true
};
}
hideTooltip() {
this.tooltipData.show = false;
}
hidePopover(): void {
this.popoverShown = false;
}
selectLayer(index: number): void {
this.currentLayer = index;
}
private getKeyActionContent(keyAction: KeyAction): Observable<NameValuePair[]> {
if (keyAction instanceof KeystrokeAction) {
const keystrokeAction: KeystrokeAction = keyAction;
const content: NameValuePair[] = [];
content.push({
name: 'Action type',
value: 'Keystroke'
@@ -231,71 +261,79 @@ export class SvgKeyboardWrapComponent implements OnInit, OnChanges {
value: LongPressAction[keystrokeAction.longPressAction]
});
}
return Observable.of(content);
} else if (keyAction instanceof MouseAction) {
const mouseAction: MouseAction = keyAction;
content.push({
name: 'Action type',
value: 'Mouse'
});
content.push({
name: 'Action',
value: camelCaseToSentence(MouseActionParam[mouseAction.mouseAction])
});
const content: NameValuePair[] =
[
{
name: 'Action type',
value: 'Mouse'
},
{
name: 'Action',
value: camelCaseToSentence(MouseActionParam[mouseAction.mouseAction])
}
];
return Observable.of(content);
} else if (keyAction instanceof PlayMacroAction) {
const playMacroAction: PlayMacroAction = keyAction;
content.push({
name: 'Action type',
value: 'Play macro'
});
content.push({
name: 'Macro name',
value: playMacroAction.macro.name.toString()
});
return this.store
.select(appState => appState.macros)
.map(macroState => macroState.entities.find(macro => {
return macro.id === playMacroAction.macroId;
}).name)
.map(macroName => {
const content: NameValuePair[] = [
{
name: 'Action type',
value: 'Play macro'
},
{
name: 'Macro name',
value: macroName
}
];
return content;
});
} else if (keyAction instanceof SwitchKeymapAction) {
const switchKeymapAction: SwitchKeymapAction = keyAction;
content.push({
name: 'Action type',
value: 'Switch keymap'
});
content.push({
name: 'Keymap',
value: switchKeymapAction.keymap.name
});
return this.store
.select(appState => appState.keymaps.entities)
.map(keymaps => keymaps.find(keymap => keymap.abbreviation === switchKeymapAction.keymapAbbreviation).name)
.map(keymapName => {
const content: NameValuePair[] = [
{
name: 'Action type',
value: 'Switch keymap'
},
{
name: 'Keymap',
value: keymapName
}
];
return content;
});
} else if (keyAction instanceof SwitchLayerAction) {
const switchLayerAction: SwitchLayerAction = keyAction;
content.push({
name: 'Action type',
value: 'Switch layer'
});
content.push({
name: 'Layer',
value: capitalizeFirstLetter(LayerName[switchLayerAction.layer])
});
content.push({
name: 'Toogle',
value: switchLayerAction.isLayerToggleable ? 'On' : 'Off'
});
const content: NameValuePair[] =
[
{
name: 'Action type',
value: 'Switch layer'
},
{
name: 'Layer',
value: capitalizeFirstLetter(LayerName[switchLayerAction.layer])
},
{
name: 'Toogle',
value: switchLayerAction.isLayerToggleable ? 'On' : 'Off'
}
];
return Observable.of(content);
}
this.tooltipData = {
posLeft: posLeft,
posTop: posTop,
content,
show: true
};
}
hideTooltip() {
this.tooltipData.show = false;
}
hidePopover(): void {
this.popoverShown = false;
}
selectLayer(index: number): void {
this.currentLayer = index;
return Observable.of([]);
}
}

View File

@@ -15,7 +15,7 @@ export class Keymap extends Serializable<Keymap> {
layers: Layer[];
constructor(keymap?: Keymap, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
constructor(keymap?: Keymap) {
super();
if (!keymap) {
return;
@@ -25,25 +25,25 @@ 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, getKeymap, getMacro));
this.layers = keymap.layers.map(layer => new Layer(layer));
}
fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
fromJsonObject(jsonObject: any): 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));
this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer));
return this;
}
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
fromBinary(buffer: UhkBuffer): Keymap {
this.abbreviation = buffer.readString();
this.isDefault = buffer.readBoolean();
this.name = buffer.readString();
this.description = buffer.readString();
this.layers = buffer.readArray<Layer>(uhkBuffer => {
return new Layer().fromBinary(uhkBuffer, getKeymap, getMacro);
return new Layer().fromBinary(uhkBuffer);
});
return this;
}

View File

@@ -8,22 +8,22 @@ export class Layer extends Serializable<Layer> {
modules: Module[];
constructor(layers?: Layer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
constructor(layers?: Layer) {
super();
if (!layers) {
return;
}
this.modules = layers.modules.map(module => new Module(module, getKeymap, getMacro));
this.modules = layers.modules.map(module => new Module(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));
fromJsonObject(jsonObject: any): Layer {
this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module));
return this;
}
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
fromBinary(buffer: UhkBuffer): Layer {
this.modules = buffer.readArray<Module>(uhkBuffer => {
return new Module().fromBinary(uhkBuffer, getKeymap, getMacro);
return new Module().fromBinary(uhkBuffer);
});
return this;
}

View File

@@ -21,32 +21,32 @@ export class Module extends Serializable<Module> {
@assertEnum(PointerRole)
pointerRole: PointerRole;
constructor(other?: Module, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
constructor(other?: Module) {
super();
if (!other) {
return;
}
this.id = other.id;
this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro));
this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction));
this.pointerRole = other.pointerRole;
}
fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
fromJsonObject(jsonObject: any): 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 KeyActionHelper.createKeyAction(keyAction);
});
return this;
}
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
fromBinary(buffer: UhkBuffer): 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, getKeymap, getMacro));
this.keyActions.push(KeyActionHelper.createKeyAction(buffer));
}
return this;
}

View File

@@ -40,18 +40,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
});
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.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap));
this.epilogue = jsonObject.epilogue;
return this;
}
@@ -66,10 +55,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
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.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer));
this.epilogue = buffer.readUInt32();
return this;
}
@@ -104,14 +90,8 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
return `<UhkConfiguration signature="${this.signature}">`;
}
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;
getKeymap(keymapAbbreviation: string): Keymap {
return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation);
}
getMacro(macroId: number): Macro {

View File

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

View File

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

View File

@@ -15,25 +15,17 @@ import { Macro } from '../Macro';
export class Helper {
static createKeyAction(
source: KeyAction | UhkBuffer | any,
getKeymap?: (abbrevation: string) => Keymap,
getMacro?: (macroId: number) => Macro
): KeyAction {
static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction {
if (source instanceof KeyAction) {
return Helper.fromKeyAction(source);
} else if (source instanceof UhkBuffer) {
return Helper.fromUhkBuffer(source, getKeymap, getMacro);
return Helper.fromUhkBuffer(source);
} else {
return Helper.fromJSONObject(source, getKeymap, getMacro);
return Helper.fromJSONObject(source);
}
}
private static fromUhkBuffer(
buffer: UhkBuffer,
getKeymap?: (abbrevation: string) => Keymap,
getMacro?: (macroId: number) => Macro
): KeyAction {
private static fromUhkBuffer(buffer: UhkBuffer): KeyAction {
let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack();
@@ -48,11 +40,11 @@ export class Helper {
case KeyActionId.SwitchLayerAction:
return new SwitchLayerAction().fromBinary(buffer);
case KeyActionId.SwitchKeymapAction:
return new SwitchKeymapAction().fromBinary(buffer, getKeymap);
return new SwitchKeymapAction().fromBinary(buffer);
case KeyActionId.MouseAction:
return new MouseAction().fromBinary(buffer);
case KeyActionId.PlayMacroAction:
return new PlayMacroAction().fromBinary(buffer, getMacro);
return new PlayMacroAction().fromBinary(buffer);
default:
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
}
@@ -74,11 +66,7 @@ export class Helper {
return newKeyAction;
}
private static fromJSONObject(
keyAction: any,
getKeymap?: (abbrevation: string) => Keymap,
getMacro?: (macroId: number) => Macro
): KeyAction {
private static fromJSONObject(keyAction: any): KeyAction {
if (!keyAction) {
return;
}
@@ -89,11 +77,11 @@ export class Helper {
case keyActionType.SwitchLayerAction:
return new SwitchLayerAction().fromJsonObject(keyAction);
case keyActionType.SwitchKeymapAction:
return new SwitchKeymapAction().fromJsonObject(keyAction, getKeymap);
return new SwitchKeymapAction().fromJsonObject(keyAction);
case keyActionType.MouseAction:
return new MouseAction().fromJsonObject(keyAction);
case keyActionType.PlayMacroAction:
return new PlayMacroAction().fromJsonObject(keyAction, getMacro);
return new PlayMacroAction().fromJsonObject(keyAction);
default:
throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`;
}

View File

@@ -83,14 +83,7 @@ export class DataStorage {
initUHKJson() {
this.uhkConfiguration = new UhkConfiguration().fromJsonObject(require('json!../../config-serializer/uhk-config.json'));
this.uhkPresets = (<any[]>require('json!../../config-serializer/preset-keymaps.json'))
/* 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)
));
.map(keymap => new Keymap().fromJsonObject(keymap));
}
getConfiguration(): UhkConfiguration {