Add missing keyboard-key labels

This commit is contained in:
József Farkas
2016-04-29 21:19:50 +02:00
parent a03588cc02
commit e2eb46b0de
5 changed files with 102 additions and 23 deletions

View File

@@ -11,15 +11,16 @@ import { Component, OnInit, Input } from 'angular2/core';
<svg:text
[attr.x]="0"
[attr.y]="height/2"
[attr.text-anchor]="start"
[attr.text-anchor]="'middle'"
[attr.font-size]="19"
[attr.font-family]="Helvetica"
fill="#ffffff"
*ngIf="asciiCode && asciiCode.length > 0">
[attr.font-family]="'Helvetica'"
[attr.fill]="'#ffffff'"
[attr.style]="'dominant-baseline: central'"
*ngIf="labels && labels.length > 0">
<tspan
x="0"
[attr.x]="width / 2"
dy="0"
id="SvgjsTspan1180">{{asciiCode[0]}}</tspan>
id="SvgjsTspan1180">{{ labels[0] }}</tspan>
</svg:text>
`
})
@@ -29,10 +30,10 @@ export class SvgKeyboardKeyComponent implements OnInit {
@Input() ry: string;
@Input() height: string;
@Input() width: string;
@Input() asciiCode: string[];
@Input() labels: string[];
constructor() {
this.asciiCode = [];
this.labels = [];
}
ngOnInit() { }

View File

@@ -4,6 +4,8 @@ import {SvgKeyboardKey} from './svg-keyboard-key.model';
import {SvgKeyboardKeyComponent} from './svg-keyboard-key.component';
import {KeyAction} from '../../config-serializer/config-items/KeyAction';
import {KeystrokeAction} from '../../config-serializer/config-items/KeystrokeAction';
import {KeystrokeModifiersAction, KeyModifiers} from '../../config-serializer/config-items/KeystrokeModifiersAction';
import {SwitchLayerAction, LayerName} from '../../config-serializer/config-items/SwitchLayerAction';
import {Mapper} from '../utils/mapper';
@Component({
@@ -16,7 +18,7 @@ import {Mapper} from '../utils/mapper';
[rx]="key.rx" [ry]="key.ry"
[width]="key.width" [height]="key.height"
[attr.transform]="'translate(' + key.x + ' ' + key.y + ')'"
[asciiCode]="asciiCodes[i]"
[labels]="labels[i]"
/>
`,
directives: [SvgKeyboardKeyComponent]
@@ -25,40 +27,78 @@ export class SvgModuleComponent implements OnInit, OnChanges {
@Input() coverages: any[];
@Input() keyboardKeys: SvgKeyboardKey[];
@Input() keyActions: KeyAction[];
private asciiCodes: string[][];
private labels: string[][];
constructor() {
this.keyboardKeys = [];
this.asciiCodes = [];
this.labels = [];
}
ngOnInit() {
this.setAsciiCodes();
this.setLabels();
console.log(this);
}
ngOnChanges(changes: { [propertyName: string]: SimpleChange }) {
/* tslint:disable:no-string-literal */
if (changes['keyActions']) {
this.setAsciiCodes();
this.setLabels();
}
/* tslint:enable:no-string-literal */
}
private setAsciiCodes(): void {
private setLabels(): void {
if (!this.keyActions) {
return;
}
let newAsciiCodes: string[][] = [];
let newLabels: string[][] = [];
this.keyActions.forEach((keyAction: KeyAction) => {
if (keyAction instanceof KeystrokeAction) {
newAsciiCodes.push(Mapper.scanCodeToText((keyAction as KeystrokeAction).scancode));
if (keyAction instanceof KeystrokeModifiersAction) {
if (keyAction.isOnlyOneModifierActive()) {
switch (keyAction.modifierMask) {
case KeyModifiers.leftCtrl:
case KeyModifiers.rightCtrl:
newLabels.push(['Ctrl']);
break;
case KeyModifiers.leftShift:
case KeyModifiers.rightShift:
newLabels.push(['Shift']);
break;
case KeyModifiers.leftAlt:
case KeyModifiers.rightAlt:
newLabels.push(['Alt']);
break;
case KeyModifiers.leftGui:
case KeyModifiers.rightGui:
newLabels.push(['Super']);
break;
default:
newLabels.push(['Undefined']);
break;
}
}
} else if (keyAction instanceof KeystrokeAction) {
newLabels.push(Mapper.scanCodeToText((keyAction as KeystrokeAction).scancode));
} else if (keyAction instanceof SwitchLayerAction) {
switch (keyAction.layer) {
case LayerName.mod:
newLabels.push(['Mod']);
break;
case LayerName.fn:
newLabels.push(['Fn']);
break;
case LayerName.mouse:
newLabels.push(['Mouse']);
break;
default:
break;
}
} else {
newAsciiCodes.push([]);
newLabels.push([]);
}
});
this.asciiCodes = newAsciiCodes;
this.labels = newLabels;
}
}