From 8c0f202b6080bade388ef4b252c6da099859db5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Sun, 25 Sep 2016 10:09:11 +0200 Subject: [PATCH] Add KeyAction Helper --- .../config-items/key-action/helper.ts | 86 +++++++++++++++++++ .../config-items/key-action/index.ts | 1 + 2 files changed, 87 insertions(+) create mode 100644 src/config-serializer/config-items/key-action/helper.ts diff --git a/src/config-serializer/config-items/key-action/helper.ts b/src/config-serializer/config-items/key-action/helper.ts new file mode 100644 index 00000000..ce5e9125 --- /dev/null +++ b/src/config-serializer/config-items/key-action/helper.ts @@ -0,0 +1,86 @@ +import { UhkBuffer } from '../../UhkBuffer'; +import { + KeyAction, + KeyActionId, + KeystrokeAction, + MouseAction, + NoneAction, + PlayMacroAction, + SwitchKeymapAction, + SwitchLayerAction, + keyActionType +} from './index'; + +export class Helper { + + static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction { + if (source instanceof KeyAction) { + return Helper.fromKeyAction(source); + } else if (source instanceof UhkBuffer) { + return Helper.fromUhkBuffer(source); + } else { + return Helper.fromJSONObject(source); + } + } + + private static fromUhkBuffer(buffer: UhkBuffer): KeyAction { + let keyActionFirstByte = buffer.readUInt8(); + buffer.backtrack(); + + if (keyActionFirstByte >= KeyActionId.KeystrokeAction && keyActionFirstByte < KeyActionId.LastKeystrokeAction) { + return new KeystrokeAction().fromBinary(buffer); + } + + switch (keyActionFirstByte) { + case KeyActionId.NoneAction: + return new NoneAction().fromBinary(buffer); + case KeyActionId.SwitchLayerAction: + return new SwitchLayerAction().fromBinary(buffer); + case KeyActionId.SwitchKeymapAction: + 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}`; + } + } + + private static fromKeyAction(keyAction: KeyAction): 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(); + } + return newKeyAction; + } + + private static fromJSONObject(keyAction: any): KeyAction { + switch (keyAction.keyActionType) { + case keyActionType.NoneAction: + return new NoneAction().fromJsObject(keyAction); + case keyActionType.KeystrokeAction: + return new KeystrokeAction().fromJsObject(keyAction); + case keyActionType.SwitchLayerAction: + return new SwitchLayerAction().fromJsObject(keyAction); + case keyActionType.SwitchKeymapAction: + return new SwitchKeymapAction().fromJsObject(keyAction); + case keyActionType.MouseAction: + return new MouseAction().fromJsObject(keyAction); + case keyActionType.PlayMacroAction: + return new PlayMacroAction().fromJsObject(keyAction); + default: + throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`; + } + } +} diff --git a/src/config-serializer/config-items/key-action/index.ts b/src/config-serializer/config-items/key-action/index.ts index 8157481c..c79a8104 100644 --- a/src/config-serializer/config-items/key-action/index.ts +++ b/src/config-serializer/config-items/key-action/index.ts @@ -6,3 +6,4 @@ export * from './NoneAction'; export * from './PlayMacroAction'; export * from './SwitchKeymapAction'; export * from './SwitchLayerAction'; +export * from './helper';