Add copy constructors for key actions

This commit is contained in:
Farkas József
2016-09-17 19:42:21 +02:00
parent 813714073e
commit fb8cd163ec
6 changed files with 81 additions and 22 deletions
@@ -10,12 +10,28 @@ import { SwitchLayerAction } from './SwitchLayerAction';
export class KeyActions extends ClassArray<KeyAction> { export class KeyActions extends ClassArray<KeyAction> {
constructor(keyActions?: KeyActions) { constructor(other?: KeyActions) {
super(); super();
if (!keyActions) { if (!other) {
return; return;
} }
keyActions.elements.forEach(keyaction => this.elements.push(this.jsObjectToClass(keyaction))); other.elements.forEach(keyAction => {
let newKeyAction: KeyAction;
if (keyAction instanceof KeystrokeAction) {
newKeyAction = new KeystrokeAction(keyAction);
} else if (keyAction instanceof SwitchLayerAction) {
newKeyAction = new SwitchLayerAction(keyAction);
} else if (keyAction instanceof SwitchKeymapAction) {
newKeyAction = new SwitchKeymapAction(keyAction);
} else if (keyAction instanceof MouseAction) {
newKeyAction = new MouseAction(keyAction);
} else if (keyAction instanceof PlayMacroAction) {
newKeyAction = new PlayMacroAction(keyAction);
} else {
newKeyAction = new NoneAction();
}
this.elements.push(newKeyAction);
});
} }
jsObjectToClass(jsObject: any): KeyAction { jsObjectToClass(jsObject: any): KeyAction {
@@ -28,6 +28,16 @@ export class KeystrokeAction extends KeyAction {
@assertEnum(LongPressAction) @assertEnum(LongPressAction)
longPressAction: LongPressAction; longPressAction: LongPressAction;
constructor(other?: KeystrokeAction) {
super();
if (!other) {
return;
}
this.scancode = other.scancode;
this.modifierMask = other.modifierMask;
this.longPressAction = other.longPressAction;
}
_fromJsObject(jsObject: JsObjectKeystrokeAction): KeystrokeAction { _fromJsObject(jsObject: JsObjectKeystrokeAction): KeystrokeAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.scancode = jsObject.scancode; this.scancode = jsObject.scancode;
@@ -23,6 +23,14 @@ export class MouseAction extends KeyAction {
@assertEnum(MouseActionParam) @assertEnum(MouseActionParam)
mouseAction: MouseActionParam; mouseAction: MouseActionParam;
constructor(other?: MouseAction) {
super();
if (!other) {
return;
}
this.mouseAction = other.mouseAction;
}
_fromJsObject(jsObject: any): MouseAction { _fromJsObject(jsObject: any): MouseAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.mouseAction = MouseActionParam[<string>jsObject.mouseAction]; this.mouseAction = MouseActionParam[<string>jsObject.mouseAction];
@@ -7,6 +7,14 @@ export class PlayMacroAction extends KeyAction {
@assertUInt8 @assertUInt8
macroId: number; macroId: number;
constructor(other?: PlayMacroAction) {
super();
if (!other) {
return;
}
this.macroId = other.macroId;
}
_fromJsObject(jsObject: any): PlayMacroAction { _fromJsObject(jsObject: any): PlayMacroAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.macroId = jsObject.macroId; this.macroId = jsObject.macroId;
@@ -7,6 +7,14 @@ export class SwitchKeymapAction extends KeyAction {
@assertUInt8 @assertUInt8
keymapId: number; keymapId: number;
constructor(other?: SwitchKeymapAction) {
super();
if (!other) {
return;
}
this.keymapId = other.keymapId;
}
_fromJsObject(jsObject: any): SwitchKeymapAction { _fromJsObject(jsObject: any): SwitchKeymapAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.keymapId = jsObject.keymapId; this.keymapId = jsObject.keymapId;
@@ -15,6 +15,15 @@ export class SwitchLayerAction extends KeyAction {
@assertEnum(LayerName) @assertEnum(LayerName)
layer: LayerName; layer: LayerName;
constructor(other?: SwitchLayerAction) {
super();
if (!other) {
return;
}
this.isLayerToggleable = other.isLayerToggleable;
this.layer = other.layer;
}
_fromJsObject(jsObject: any): SwitchLayerAction { _fromJsObject(jsObject: any): SwitchLayerAction {
this.assertKeyActionType(jsObject); this.assertKeyActionType(jsObject);
this.layer = LayerName[<string>jsObject.layer]; this.layer = LayerName[<string>jsObject.layer];