Move the classes of the config items into the newly created config-items directory.
This commit is contained in:
26
config-serializer/config-items/KeyAction.ts
Normal file
26
config-serializer/config-items/KeyAction.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
class KeyAction {
|
||||
|
||||
static fromJsObject(jsObject: any): KeyAction {
|
||||
switch (jsObject.keyActionType) {
|
||||
case KeystrokeAction.actionTypeString:
|
||||
return new KeystrokeAction().fromJsObject(jsObject);
|
||||
case KeyActionNone.actionTypeString:
|
||||
return new KeyActionNone().fromJsObject(jsObject);
|
||||
default:
|
||||
throw 'Invalid KeyAction.keyActionType: "${jsObject.actionType}"';
|
||||
}
|
||||
}
|
||||
|
||||
static fromBinary(buffer: UhkBuffer): KeyAction {
|
||||
let keyActionFirstByte = buffer.readUInt8();
|
||||
buffer.backtrack();
|
||||
|
||||
if (KeystrokeAction.isScancodeValid(keyActionFirstByte)) {
|
||||
return new KeystrokeAction().fromBinary(buffer);
|
||||
} else if (keyActionFirstByte === KeyActionNone.keyActionNoneId) {
|
||||
return new KeyActionNone().fromBinary(buffer);
|
||||
} else {
|
||||
throw 'Invalid KeyAction first byte "${keyActionFirstByte}"';
|
||||
}
|
||||
}
|
||||
}
|
||||
38
config-serializer/config-items/KeyActionNone.ts
Normal file
38
config-serializer/config-items/KeyActionNone.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
class KeyActionNone extends KeyAction implements Serializable<KeyActionNone> {
|
||||
|
||||
static actionTypeString = 'none';
|
||||
static keyActionNoneId = 0;
|
||||
static keyActionNoneParam = 0;
|
||||
|
||||
fromJsObject(jsObject: any): KeyActionNone {
|
||||
if (jsObject.keyActionType !== KeyActionNone.actionTypeString) {
|
||||
throw 'Invalid KeyActionNone.keyActionType: "${jsObject.keyActionType}"';
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): KeyActionNone {
|
||||
let keyActionId = buffer.readUInt8();
|
||||
if (keyActionId !== KeyActionNone.keyActionNoneId) {
|
||||
throw 'Invalid KeyActionNone.id: ${keyActionId}';
|
||||
}
|
||||
|
||||
let keyActionParam = buffer.readUInt8();
|
||||
if (keyActionParam !== KeyActionNone.keyActionNoneParam) {
|
||||
throw 'Invalid KeyActionNone.param: ${keyActionParam}';
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionNone.actionTypeString
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionNone.keyActionNoneId);
|
||||
buffer.writeUInt8(KeyActionNone.keyActionNoneParam);
|
||||
}
|
||||
}
|
||||
50
config-serializer/config-items/KeystrokeAction.ts
Normal file
50
config-serializer/config-items/KeystrokeAction.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
|
||||
|
||||
static actionTypeString = 'keystroke';
|
||||
static firstValidScancode = 1;
|
||||
static lastValidScancode = 231;
|
||||
|
||||
_scancode: number;
|
||||
modifierMask: number;
|
||||
|
||||
get scancode() {
|
||||
return this._scancode;
|
||||
}
|
||||
|
||||
set scancode(value) {
|
||||
if (!KeystrokeAction.isScancodeValid(value)) {
|
||||
throw 'Invalid KeystrokeAction.scancode: ${scancode}';
|
||||
}
|
||||
this._scancode = value;
|
||||
}
|
||||
|
||||
static isScancodeValid(scancode) {
|
||||
return KeystrokeAction.firstValidScancode <= scancode &&
|
||||
scancode <= KeystrokeAction.lastValidScancode;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): KeystrokeAction {
|
||||
this.scancode = jsObject.scancode;
|
||||
this.modifierMask = jsObject.modifierMask;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): KeystrokeAction {
|
||||
this.scancode = buffer.readUInt8();
|
||||
this.modifierMask = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeystrokeAction.actionTypeString,
|
||||
scancode: this.scancode,
|
||||
modifierMask: this.modifierMask
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(this.scancode);
|
||||
buffer.writeUInt8(this.modifierMask);
|
||||
}
|
||||
}
|
||||
3
config-serializer/config-items/config-items.ts
Normal file
3
config-serializer/config-items/config-items.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
/// <reference path="KeyAction.ts" />
|
||||
/// <reference path="KeystrokeAction.ts" />
|
||||
/// <reference path="KeyActionNone.ts" />
|
||||
Reference in New Issue
Block a user