Use the members of the newly added KeyActionType object because static members were uninitialized. Handle PlayMacroAction in the KeyActionFactory.
This commit is contained in:
@@ -13,7 +13,6 @@ enum LongPressAction {
|
||||
}
|
||||
|
||||
class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRoleKeystrokeAction> {
|
||||
static keyActionTypeString = 'dualRoleKeystroke';
|
||||
|
||||
public scancode;
|
||||
|
||||
@@ -24,18 +23,18 @@ class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRole
|
||||
}
|
||||
|
||||
set longPressAction(value) {
|
||||
if (!DualRoleKeystrokeAction.isDualRoleKeystrokeActionValid(value)) {
|
||||
if (!this.isDualRoleKeystrokeActionValid(value)) {
|
||||
throw `Invalid DualRoleKeystrokeAction.longPressAction: ${value}`;
|
||||
}
|
||||
this._longPressAction = value;
|
||||
}
|
||||
|
||||
static isDualRoleKeystrokeActionValid(keyActionIdParam): boolean {
|
||||
return MouseActionParam[keyActionIdParam] !== undefined;
|
||||
isDualRoleKeystrokeActionValid(keyActionIdParam): boolean {
|
||||
return LongPressAction[keyActionIdParam] !== undefined;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): DualRoleKeystrokeAction {
|
||||
this.assertKeyActionType(jsObject, DualRoleKeystrokeAction.keyActionTypeString, 'DualRoleKeystrokeAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction');
|
||||
this.scancode = jsObject.scancode;
|
||||
this.longPressAction = jsObject.longPressAction;
|
||||
return this;
|
||||
@@ -50,7 +49,7 @@ class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRole
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: DualRoleKeystrokeAction.keyActionTypeString,
|
||||
keyActionType: KeyActionType.DualRoleKeystrokeAction,
|
||||
scancode: this.scancode,
|
||||
longPressAction: KeyActionId[this.longPressAction]
|
||||
};
|
||||
|
||||
@@ -12,6 +12,17 @@ enum KeyActionId {
|
||||
PlayMacroAction = 7
|
||||
}
|
||||
|
||||
let KeyActionType = {
|
||||
NoneAction : 'none',
|
||||
KeystrokeAction : 'keystroke',
|
||||
KeystrokeWithModifiersAction : 'keystrokeWithModifiers',
|
||||
DualRoleKeystrokeAction : 'dualRoleKeystroke',
|
||||
SwitchLayerAction : 'switchLayer',
|
||||
SwitchKeymapAction : 'switchKeymap',
|
||||
MouseAction : 'mouse',
|
||||
PlayMacroAction : 'playMacro'
|
||||
}
|
||||
|
||||
abstract class KeyAction implements Serializable<KeyAction> {
|
||||
abstract fromJsObject(jsObject: any): KeyAction;
|
||||
abstract fromBinary(buffer: UhkBuffer): KeyAction;
|
||||
|
||||
@@ -2,20 +2,22 @@ class KeyActionFactory {
|
||||
|
||||
static fromJsObject(jsObject: any): KeyAction {
|
||||
switch (jsObject.keyActionType) {
|
||||
case NoneAction.keyActionTypeString:
|
||||
case KeyActionType.NoneAction:
|
||||
return new NoneAction().fromJsObject(jsObject);
|
||||
case KeystrokeAction.keyActionTypeString:
|
||||
case KeyActionType.KeystrokeAction:
|
||||
return new KeystrokeAction().fromJsObject(jsObject);
|
||||
case KeystrokeWithModifiersAction.keyActionTypeString:
|
||||
case KeyActionType.KeystrokeWithModifiersAction:
|
||||
return new KeystrokeWithModifiersAction().fromJsObject(jsObject);
|
||||
case DualRoleKeystrokeAction.keyActionTypeString:
|
||||
case KeyActionType.DualRoleKeystrokeAction:
|
||||
return new DualRoleKeystrokeAction().fromJsObject(jsObject);
|
||||
case SwitchLayerAction.keyActionTypeString:
|
||||
case KeyActionType.SwitchLayerAction:
|
||||
return new SwitchLayerAction().fromJsObject(jsObject);
|
||||
case SwitchKeymapAction.keyActionTypeString:
|
||||
case KeyActionType.SwitchKeymapAction:
|
||||
return new SwitchKeymapAction().fromJsObject(jsObject);
|
||||
case MouseAction.keyActionTypeString:
|
||||
case KeyActionType.MouseAction:
|
||||
return new MouseAction().fromJsObject(jsObject);
|
||||
case KeyActionType.PlayMacroAction:
|
||||
return new PlayMacroAction().fromJsObject(jsObject);
|
||||
default:
|
||||
throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`;
|
||||
}
|
||||
@@ -40,6 +42,8 @@ class KeyActionFactory {
|
||||
return new SwitchKeymapAction().fromBinary(buffer);
|
||||
case KeyActionId.MouseAction:
|
||||
return new MouseAction().fromBinary(buffer);
|
||||
case KeyActionId.PlayMacroAction:
|
||||
return new PlayMacroAction().fromBinary(buffer);
|
||||
default:
|
||||
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
|
||||
|
||||
static keyActionTypeString = 'keystroke';
|
||||
|
||||
private _scancode: number;
|
||||
|
||||
get scancode(): number {
|
||||
@@ -16,7 +14,7 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): KeystrokeAction {
|
||||
this.assertKeyActionType(jsObject, KeystrokeAction.keyActionTypeString, 'KeystrokeAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.KeystrokeAction, 'KeystrokeAction');
|
||||
this.scancode = jsObject.scancode;
|
||||
return this;
|
||||
}
|
||||
@@ -29,8 +27,8 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeystrokeAction.keyActionTypeString,
|
||||
scancode: this.scancode,
|
||||
keyActionType: KeyActionType.KeystrokeAction,
|
||||
scancode: this.scancode
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,6 @@ enum MouseActionParam {
|
||||
}
|
||||
|
||||
class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
static keyActionTypeString = 'mouse';
|
||||
|
||||
private _mouseAction: MouseActionParam;
|
||||
|
||||
get mouseAction(): number {
|
||||
@@ -24,18 +22,18 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
}
|
||||
|
||||
set mouseAction(mouseAction) {
|
||||
if (!MouseAction.isMouseActionValid(mouseAction)) {
|
||||
if (!this.isMouseActionValid(mouseAction)) {
|
||||
throw `Invalid MouseAction.mouseAction: ${mouseAction}`;
|
||||
}
|
||||
this._mouseAction = mouseAction;
|
||||
}
|
||||
|
||||
static isMouseActionValid(keyActionParam): boolean {
|
||||
isMouseActionValid(keyActionParam): boolean {
|
||||
return MouseActionParam[keyActionParam] !== undefined;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): MouseAction {
|
||||
this.assertKeyActionType(jsObject, MouseAction.keyActionTypeString, 'MouseAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.MouseAction, 'MouseAction');
|
||||
this.mouseAction = jsObject.mouseAction;
|
||||
return this;
|
||||
}
|
||||
@@ -44,7 +42,7 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction');
|
||||
|
||||
this.mouseAction = buffer.readUInt8();
|
||||
if (!MouseAction.isMouseActionValid(this.mouseAction)) {
|
||||
if (!this.isMouseActionValid(this.mouseAction)) {
|
||||
throw `Invalid MouseAction.param: ${this.mouseAction}`;
|
||||
}
|
||||
|
||||
@@ -53,7 +51,7 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: MouseAction.keyActionTypeString,
|
||||
keyActionType: KeyActionType.MouseAction,
|
||||
mouseAction: MouseActionParam[this.mouseAction]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
class NoneAction extends KeyAction implements Serializable<NoneAction> {
|
||||
|
||||
static keyActionTypeString = 'none';
|
||||
static noneActionParam = 0;
|
||||
|
||||
fromJsObject(jsObject: any): NoneAction {
|
||||
this.assertKeyActionType(jsObject, NoneAction.keyActionTypeString, 'NoneAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.NoneAction, 'NoneAction');
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -21,7 +20,7 @@ class NoneAction extends KeyAction implements Serializable<NoneAction> {
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: NoneAction.keyActionTypeString
|
||||
keyActionType: KeyActionId.NoneAction
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction> {
|
||||
|
||||
static keyActionTypeString = 'playMacro';
|
||||
|
||||
private _macroId: number;
|
||||
|
||||
get macroId(): number {
|
||||
@@ -16,7 +14,7 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): PlayMacroAction {
|
||||
this.assertKeyActionType(jsObject, PlayMacroAction.keyActionTypeString, 'PlayMacroAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.PlayMacroAction, 'PlayMacroAction');
|
||||
this.macroId = jsObject.macroId;
|
||||
return this;
|
||||
}
|
||||
@@ -29,7 +27,7 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: PlayMacroAction.keyActionTypeString,
|
||||
keyActionType: KeyActionType.PlayMacroAction,
|
||||
macroId: this.macroId
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapAction> {
|
||||
|
||||
static keyActionTypeString = 'switchKeymap';
|
||||
|
||||
private _keymapId: number;
|
||||
|
||||
get keymapId(): number {
|
||||
@@ -16,7 +14,7 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): SwitchKeymapAction {
|
||||
this.assertKeyActionType(jsObject, SwitchKeymapAction.keyActionTypeString, 'SwitchKeymapAction');
|
||||
this.assertKeyActionType(jsObject, KeyActionType.SwitchKeymapAction, 'SwitchKeymapAction');
|
||||
this.keymapId = jsObject.keymapId;
|
||||
return this;
|
||||
}
|
||||
@@ -29,7 +27,7 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: SwitchKeymapAction.keyActionTypeString,
|
||||
keyActionType: KeyActionType.SwitchKeymapAction,
|
||||
keymapId: this.keymapId
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ enum Layer {
|
||||
|
||||
class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAction> {
|
||||
|
||||
static keyActionTypeString = 'switchLayer';
|
||||
static toggleFlag = 0x80;
|
||||
|
||||
isLayerToggleable: boolean;
|
||||
@@ -30,8 +29,8 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
this.assertKeyActionType(jsObject, SwitchLayerAction.keyActionTypeString, 'SwitchLayerAction');
|
||||
this.layer = jsObject.layerId;
|
||||
this.assertKeyActionType(jsObject, KeyActionType.SwitchLayerAction, 'SwitchLayerAction');
|
||||
this.layer = jsObject.layer;
|
||||
this.isLayerToggleable = jsObject.toggle;
|
||||
return this;
|
||||
}
|
||||
@@ -46,7 +45,7 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: SwitchLayerAction.keyActionTypeString,
|
||||
keyActionType: KeyActionType.SwitchLayerAction,
|
||||
layer: this.layer,
|
||||
toggle: this.isLayerToggleable
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ let writer = new UhkBuffer();
|
||||
|
||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||
let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
||||
|
||||
console.log(keyActions);
|
||||
let keyActionObjects: KeyActions = new KeyActions().fromJsObject(keyActions);
|
||||
|
||||
fs.writeFileSync('uhk-config.bin', writer.buffer);
|
||||
|
||||
Reference in New Issue
Block a user