From 927cc80815ef97f4551c00b40fa4f593fe12e920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Sun, 25 Sep 2016 10:09:30 +0200 Subject: [PATCH] refactor: Module --- .../svg/keyboard/svg-keyboard.component.html | 2 +- .../svg/wrap/svg-keyboard-wrap.component.ts | 4 +-- src/config-serializer/config-items/Module.ts | 27 ++++++++++--------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/components/svg/keyboard/svg-keyboard.component.html b/src/components/svg/keyboard/svg-keyboard.component.html index 1c187522..7d7d3841 100644 --- a/src/components/svg/keyboard/svg-keyboard.component.html +++ b/src/components/svg/keyboard/svg-keyboard.component.html @@ -4,7 +4,7 @@ [coverages]="module.coverages" [keyboardKeys]="module.keyboardKeys" [attr.transform]="module.attributes.transform" - [keyActions]="moduleConfig[i].keyActions.elements" + [keyActions]="moduleConfig[i].keyActions" (keyClick)="onKeyClick(i, $event)" (keyHover)="onKeyHover($event.index, $event.event, $event.over, i)" /> diff --git a/src/components/svg/wrap/svg-keyboard-wrap.component.ts b/src/components/svg/wrap/svg-keyboard-wrap.component.ts index dd69cc26..cc3d6080 100644 --- a/src/components/svg/wrap/svg-keyboard-wrap.component.ts +++ b/src/components/svg/wrap/svg-keyboard-wrap.component.ts @@ -130,7 +130,7 @@ export class SvgKeyboardWrapComponent implements OnChanges { onKeyClick(moduleId: number, keyId: number): void { if (!this.popoverShown && this.popoverEnabled) { this.keyEditConfig = { - keyActions: this.layers[this.currentLayer].modules[moduleId].keyActions.elements, + keyActions: this.layers[this.currentLayer].modules[moduleId].keyActions, keyId }; @@ -140,7 +140,7 @@ export class SvgKeyboardWrapComponent implements OnChanges { } onKeyHover(moduleId: number, event: MouseEvent, over: boolean, keyId: number): void { - let keyActionToEdit: KeyAction = this.layers[this.currentLayer].modules[moduleId].keyActions.elements[keyId]; + let keyActionToEdit: KeyAction = this.layers[this.currentLayer].modules[moduleId].keyActions[keyId]; if (this.tooltipEnabled) { if (over) { diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index f76d9f64..1065a92d 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -1,7 +1,7 @@ import { assertEnum, assertUInt8 } from '../assert'; import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; -import { KeyActions } from './key-action'; +import { Helper as KeyActionHelper, KeyAction } from './key-action'; enum PointerRole { none, @@ -14,32 +14,35 @@ export class Module extends Serializable { @assertUInt8 id: number; - keyActions: KeyActions; + keyActions: KeyAction[]; @assertEnum(PointerRole) private pointerRole: PointerRole; - constructor(moduleI?: Module) { + constructor(other?: Module) { super(); - if (!moduleI) { + if (!other) { return; } - this.id = moduleI.id; - this.keyActions = new KeyActions(moduleI.keyActions); - this.pointerRole = moduleI.pointerRole; + this.id = other.id; + this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction)); + this.pointerRole = other.pointerRole; } - _fromJsObject(jsObject: any): Module { this.id = jsObject.id; this.pointerRole = PointerRole[jsObject.pointerRole]; - this.keyActions = new KeyActions().fromJsObject(jsObject.keyActions); + this.keyActions = jsObject.keyActions.map((keyAction: any) => KeyActionHelper.createKeyAction(keyAction)); return this; } _fromBinary(buffer: UhkBuffer): Module { this.id = buffer.readUInt8(); this.pointerRole = buffer.readUInt8(); - this.keyActions = new KeyActions().fromBinary(buffer); + let keyActionsLength: number = buffer.readCompactLength(); + this.keyActions = []; + for (let i = 0; i < keyActionsLength; ++i) { + this.keyActions.push(KeyActionHelper.createKeyAction(buffer)); + } return this; } @@ -47,14 +50,14 @@ export class Module extends Serializable { return { id: this.id, pointerRole: PointerRole[this.pointerRole], - keyActions: this.keyActions.toJsObject() + keyActions: this.keyActions.map(keyAction => keyAction.toJsObject()) }; } _toBinary(buffer: UhkBuffer): void { buffer.writeUInt8(this.id); buffer.writeUInt8(this.pointerRole); - this.keyActions.toBinary(buffer); + buffer.writeArray(this.keyActions); } toString(): string {