ANSI and ISO layout handling (#309)

* Read left and right modules

* Add missing styles

* Calculate indices from ids

* Skip missing keys

* Align key actions to match the order specified by the svg

* Fix svg key hover

* Fix key hover

Without using css important
This commit is contained in:
József Farkas
2017-06-13 23:04:57 +02:00
committed by László Monda
parent f246900cd5
commit aa38762c42
11 changed files with 675 additions and 559 deletions

View File

@@ -1,15 +1,13 @@
<svg xmlns="http://www.w3.org/2000/svg" [attr.viewBox]="svgAttributes.viewBox" height="100%" width="100%">
<svg:g [attr.transform]="svgAttributes.transform" [attr.fill]="svgAttributes.fill">
<svg:g svg-module *ngFor="let module of modules; let i = index"
[coverages]="module.coverages"
[keyboardKeys]="module.keyboardKeys"
[keybindAnimationEnabled]="keybindAnimationEnabled"
[capturingEnabled]="capturingEnabled"
[attr.transform]="module.attributes.transform"
[keyActions]="moduleConfig[i].keyActions"
(keyClick)="onKeyClick(i, $event.index, $event.keyTarget)"
(keyHover)="onKeyHover($event.index, $event.event, $event.over, i)"
(capture)="onCapture(i, $event.index, $event.captured)"
/>
</svg:g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" [attr.viewBox]="viewBox" height="100%" width="100%">
<svg:g svg-module *ngFor="let module of modules; let i = index"
[coverages]="module.coverages"
[keyboardKeys]="module.keyboardKeys"
[keybindAnimationEnabled]="keybindAnimationEnabled"
[capturingEnabled]="capturingEnabled"
[attr.transform]="module.attributes.transform"
[keyActions]="moduleConfig[i].keyActions"
(keyClick)="onKeyClick(i, $event.index, $event.keyTarget)"
(keyHover)="onKeyHover($event.index, $event.event, $event.over, i)"
(capture)="onCapture(i, $event.index, $event.captured)"
/>
</svg>

Before

Width:  |  Height:  |  Size: 871 B

After

Width:  |  Height:  |  Size: 713 B

View File

@@ -3,6 +3,11 @@ import { Component, EventEmitter, Input, OnInit, Output, ChangeDetectionStrategy
import { Module } from '../../../config-serializer/config-items/Module';
import { SvgModule } from '../module';
enum KeyboardLayout {
ANSI,
ISO
}
@Component({
selector: 'svg-keyboard',
templateUrl: './svg-keyboard.component.html',
@@ -17,12 +22,12 @@ export class SvgKeyboardComponent implements OnInit {
@Output() keyHover = new EventEmitter();
@Output() capture = new EventEmitter();
private modules: SvgModule[];
private svgAttributes: { viewBox: string, transform: string, fill: string };
modules: SvgModule[];
viewBox: string;
constructor() {
this.modules = [];
this.svgAttributes = this.getKeyboardSvgAttributes();
this.viewBox = '-520 582 1100 470';
}
ngOnInit() {
@@ -37,7 +42,7 @@ export class SvgKeyboardComponent implements OnInit {
});
}
onCapture(moduleId: number, keyId: number, captured: {code: number, left: boolean[], right: boolean[]}): void {
onCapture(moduleId: number, keyId: number, captured: { code: number, left: boolean[], right: boolean[] }): void {
this.capture.emit({
moduleId,
keyId,
@@ -54,22 +59,21 @@ export class SvgKeyboardComponent implements OnInit {
});
}
private getKeyboardSvgAttributes(): { viewBox: string, transform: string, fill: string } {
const svg: any = this.getBaseLayer();
return {
viewBox: svg.$.viewBox,
transform: svg.g[0].$.transform,
fill: svg.g[0].$.fill
};
}
private getSvgModules(): SvgModule[] {
const modules = this.getBaseLayer().g[0].g.map((obj: any) => new SvgModule(obj));
return [modules[1], modules[0]]; // TODO: remove if the svg will be correct
const leftModule = new SvgModule(this.getLeftModule());
const rightModule = new SvgModule(this.getRightModule());
return [rightModule, leftModule];
}
private getBaseLayer(): any {
return require('xml-loader!../../../../../images/base-layer.svg').svg;
private getLeftModule(layout = KeyboardLayout.ANSI): any {
if (layout === KeyboardLayout.ISO) {
return require('xml-loader!../../../../../modules/uhk60-left-half/layout-iso.svg').svg;
}
return require('xml-loader!../../../../../modules/uhk60-left-half/layout-ansi.svg').svg;
}
private getRightModule(): any {
return require('xml-loader!../../../../../modules/uhk60-right-half/layout.svg').svg;
}
}

View File

@@ -6,4 +6,5 @@ export interface SvgKeyboardKey {
ry: string;
height: number;
width: number;
fill: string;
}

View File

@@ -1,14 +1,18 @@
<svg:path *ngFor="let path of coverages" [attr.d]="path.$.d" />
<svg:g svg-keyboard-key *ngFor="let key of keyboardKeys; let i = index"
[id]="key.id"
[rx]="key.rx" [ry]="key.ry"
[width]="key.width" [height]="key.height"
[attr.transform]="'translate(' + key.x + ' ' + key.y + ')'"
[keyAction]="keyActions[i]"
[keybindAnimationEnabled]="keybindAnimationEnabled"
[capturingEnabled]="capturingEnabled"
(keyClick)="onKeyClick(i, $event)"
(capture)="onCapture(i, $event)"
(mouseenter)="onKeyHover(i, $event, true)"
(mouseleave)="onKeyHover(i, $event, false)"
/>
<svg:path *ngFor="let path of coverages" [attr.d]="path.$.d" [attr.style]="path.$.style | safeStyle" />
<ng-container *ngFor="let key of keyboardKeys; let i = index">
<svg:g svg-keyboard-key
*ngIf="key"
[id]="key.id"
[rx]="key.rx" [ry]="key.ry"
[width]="key.width" [height]="key.height"
[attr.transform]="'translate(' + key.x + ' ' + key.y + ')'"
[attr.fill]="key.fill"
[keyAction]="keyActions[i]"
[keybindAnimationEnabled]="keybindAnimationEnabled"
[capturingEnabled]="capturingEnabled"
(keyClick)="onKeyClick(i, $event)"
(capture)="onCapture(i, $event)"
(mouseenter)="onKeyHover(i, $event, true)"
(mouseleave)="onKeyHover(i, $event, false)"
/>
</ng-container>

Before

Width:  |  Height:  |  Size: 643 B

After

Width:  |  Height:  |  Size: 826 B

View File

@@ -1,16 +1,21 @@
import { SvgKeyboardKey } from '../keys';
export class SvgModule {
private coverages: any[];
private keyboardKeys: SvgKeyboardKey[];
private attributes: any;
coverages: any[];
keyboardKeys: SvgKeyboardKey[];
attributes: any;
constructor(obj: { rect: any[], path: any[], $: Object }) {
this.keyboardKeys = obj.rect.map(rect => rect.$).map(rect => {
rect.height = +rect.height;
rect.width = +rect.width;
return rect;
});
let index: number;
const keys = obj.rect.map(rect => rect.$);
this.keyboardKeys = [];
for (let i = 0; i < keys.length; ++i) {
index = keys[i].id.slice(4) - 1; // remove 'key-' then switch to index from 0
keys[i].height = +keys[i].height;
keys[i].width = +keys[i].width;
keys[i].fill = keys[i].style.slice(5); // remove 'fill:'
this.keyboardKeys[index] = keys[i];
}
this.coverages = obj.path;
this.attributes = obj.$;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
export { SafeStylePipe } from './safe-style.pipe';

View File

@@ -0,0 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';
import { SafeStyle, DomSanitizer} from '@angular/platform-browser';
@Pipe({
name: 'safeStyle'
})
export class SafeStylePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {}
transform(style: string): SafeStyle {
return this.sanitizer.bypassSecurityTrustStyle(style);
}
}