From 1de2f8df3331a825de8563575620fe606bb347c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3zsef=20Farkas?= Date: Sun, 17 Jul 2016 17:01:00 +0200 Subject: [PATCH] Keypress-tab KeystrokeAction remapping. --- .../tab/keypress/keypress-tab.component.html | 14 +++- .../tab/keypress/keypress-tab.component.ts | 76 ++++++++++++++++--- 2 files changed, 77 insertions(+), 13 deletions(-) diff --git a/src/components/popover/tab/keypress/keypress-tab.component.html b/src/components/popover/tab/keypress/keypress-tab.component.html index 94b67e41..3af8531a 100644 --- a/src/components/popover/tab/keypress/keypress-tab.component.html +++ b/src/components/popover/tab/keypress/keypress-tab.component.html @@ -1,18 +1,24 @@
Scancode: - +
Modifiers:
-
-
@@ -20,6 +26,6 @@
Long press action: - +
\ No newline at end of file diff --git a/src/components/popover/tab/keypress/keypress-tab.component.ts b/src/components/popover/tab/keypress/keypress-tab.component.ts index c5486862..2fd6144b 100644 --- a/src/components/popover/tab/keypress/keypress-tab.component.ts +++ b/src/components/popover/tab/keypress/keypress-tab.component.ts @@ -1,9 +1,11 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, ViewChild } from '@angular/core'; import { CaptureKeystrokeButtonComponent } from '../../widgets/capture-keystroke/capture-keystroke-button.component'; import { KeyAction } from '../../../../../config-serializer/config-items/KeyAction'; +import { KeyModifiers } from '../../../../../config-serializer/config-items/KeyModifiers'; import { KeystrokeAction } from '../../../../../config-serializer/config-items/KeystrokeAction'; +import { LongPressAction } from '../../../../../config-serializer/config-items/LongPressAction'; import { KeyActionSaver } from '../../key-action-saver'; import {IconComponent} from '../../widgets/icon/icon.component'; @@ -19,6 +21,11 @@ import {OptionData} from 'ng2-select2/dist/select2'; directives: [CaptureKeystrokeButtonComponent, IconComponent, SELECT2_DIRECTIVES] }) export class KeypressTabComponent implements OnInit, KeyActionSaver { + + // TODO(@Nejc): We need a type for select2 component instead of any + @ViewChild('scanCodeSelect') scanCodeSelect: any; + @ViewChild('longPressSelect') longPressSelect: any; + private leftModifiers: string[]; private rightModifiers: string[]; @@ -31,22 +38,68 @@ export class KeypressTabComponent implements OnInit, KeyActionSaver { constructor() { this.leftModifiers = ['LShift', 'LCtrl', 'LSuper', 'LAlt']; this.rightModifiers = ['RShift', 'RCtrl', 'RSuper', 'RAlt']; - this.scanCodeGroups = require('json!./scancodes.json'); + this.scanCodeGroups = [{ + id: '0', + text: 'None' + }]; + this.scanCodeGroups = this.scanCodeGroups.concat(require('json!./scancodes.json')); this.longPressGroups = require('json!./longPress.json'); + this.leftModifierSelects = Array(4).fill(false); + this.rightModifierSelects = Array(4).fill(false); } ngOnInit() { } - getKeyAction(): KeystrokeAction { - return undefined; + keyActionValid(keystrokeAction?: KeystrokeAction): boolean { + if (!keystrokeAction) { + keystrokeAction = this.toKeyAction(); + } + return keystrokeAction.scancode > 0 || keystrokeAction.modifierMask > 0; } - keyActionValid(): boolean { - return false; - } + toKeyAction(): KeystrokeAction { + let keystrokeAction: KeystrokeAction = new KeystrokeAction(); + keystrokeAction.scancode = +this.scanCodeSelect.selector.nativeElement.value; - toKeyAction(): KeyAction { - return undefined; + let mapper = (x: number) => { + if (x < 8) { + return Math.floor(x / 2) * 4 + 1 - x; // 1, 0, 3, 2, 5, 4, 7, 6 + } else { + return x; + } + }; + keystrokeAction.modifierMask = 0; + let modifiers = this.leftModifierSelects.concat(this.rightModifierSelects).map(x => x ? 1 : 0); + for (let i = 0; i < modifiers.length; ++i) { + keystrokeAction.modifierMask |= modifiers[i] << mapper(i); + } + let selectedLongPressIndex = 0; + let tempIndex: number; + const selectedValue: string = this.longPressSelect.selector.nativeElement.value; + for (let i = 0; i < this.longPressGroups.length; ++i) { + if (this.longPressGroups[i].children) { + tempIndex = this.longPressGroups[i].children.findIndex(x => x.id === selectedValue); + if (tempIndex >= 0) { + selectedLongPressIndex += tempIndex; + break; + } else { + selectedLongPressIndex += this.longPressGroups[i].children.length; + } + } else { + if (this.longPressGroups[i].id === selectedValue) { + break; + } else { + ++selectedLongPressIndex; + } + } + } + + keystrokeAction.longPressAction = selectedLongPressIndex === 0 ? undefined : mapper(selectedLongPressIndex - 1); + if (!this.keyActionValid(keystrokeAction)) { + throw new Error('KeyAction is invalid!'); + } + + return keystrokeAction; } scanCodeTemplateResult: Function = (state: any) => { @@ -66,4 +119,9 @@ export class KeypressTabComponent implements OnInit, KeyActionSaver { return jQuery('' + state.text + ''); } } + + toggleModifier(right: boolean, index: number) { + let modifierSelects: boolean[] = right ? this.rightModifierSelects : this.leftModifierSelects; + modifierSelects[index] = !modifierSelects[index]; + } }