2017-12-22 23:00:23 +01:00
|
|
|
/// <reference path="./moduleView.ts" />
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
namespace pxsim.visuals {
|
2017-12-22 23:00:23 +01:00
|
|
|
export class TouchSensorView extends ModuleView implements LayoutElement {
|
2017-12-18 22:04:17 +01:00
|
|
|
|
|
|
|
private static RECT_ID = ["touch_gradient4", "touch_gradient3", "touch_gradient2", "touch_gradient1"];
|
|
|
|
private static TOUCH_GRADIENT_UNPRESSED = ["linear-gradient-2", "linear-gradient-3", "linear-gradient-4", "linear-gradient-5"];
|
|
|
|
private static TOUCH_GRADIENT_PRESSED = ["linear-gradient-6", "linear-gradient-7", "linear-gradient-8", "linear-gradient-9"];
|
|
|
|
|
|
|
|
private unpressedGradient: string;
|
|
|
|
private pressedGradient: string;
|
|
|
|
|
|
|
|
private xLinkGradients: string[];
|
|
|
|
|
2018-03-28 22:36:52 +02:00
|
|
|
private static LIGHT_TOUCH_BLACK_COLOR = '#000';
|
|
|
|
private static LIGHT_TOUCH_RED_COLOR = '#d42715';
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
constructor(port: number) {
|
|
|
|
super(TOUCH_SENSOR_SVG, "touch", NodeType.TouchSensor, port);
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:36:52 +02:00
|
|
|
protected optimizeForLightMode() {
|
|
|
|
(this.content.getElementById(this.normalizeId('touch_box_2-2')) as SVGElement).style.fill = '#a8aaa8';
|
|
|
|
}
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
public getPaddingRatio() {
|
2017-12-27 08:19:04 +01:00
|
|
|
return 1 / 4;
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public hasClick() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private setAttribute(svgId: string, attribute: string, value: string) {
|
|
|
|
const el = this.content.getElementById(svgId);
|
|
|
|
if (el) el.setAttribute(attribute, value);
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:36:52 +02:00
|
|
|
private setStyleFill(svgId: string, fillUrl: string, lightFill: string) {
|
2017-12-18 22:04:17 +01:00
|
|
|
const el = (this.content.getElementById(svgId) as SVGRectElement);
|
2018-03-28 22:36:52 +02:00
|
|
|
if (el) el.style.fill = inLightMode() ? lightFill : `url("#${fillUrl}")`;
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public attachEvents() {
|
|
|
|
this.content.style.cursor = "pointer";
|
|
|
|
const btn = this.content;
|
|
|
|
const state = ev3board().getSensor(this.port, DAL.DEVICE_TYPE_TOUCH) as TouchSensorNode;
|
2018-01-31 23:21:17 +01:00
|
|
|
pointerEvents.down.forEach(evid => btn.addEventListener(evid, ev => {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.setPressed(true);
|
|
|
|
state.setPressed(true);
|
2018-01-31 23:21:17 +01:00
|
|
|
}))
|
2017-12-18 22:04:17 +01:00
|
|
|
btn.addEventListener(pointerEvents.leave, ev => {
|
|
|
|
this.setPressed(false);
|
|
|
|
state.setPressed(false);
|
|
|
|
})
|
|
|
|
btn.addEventListener(pointerEvents.up, ev => {
|
|
|
|
this.setPressed(false);
|
|
|
|
state.setPressed(false);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private setPressed(pressed: boolean) {
|
|
|
|
if (pressed) {
|
|
|
|
for (let i = 0; i < 4; i ++) {
|
2018-03-28 22:36:52 +02:00
|
|
|
this.setStyleFill(`${this.normalizeId(TouchSensorView.RECT_ID[i])}`, `${this.normalizeId(TouchSensorView.TOUCH_GRADIENT_PRESSED[i])}`, TouchSensorView.LIGHT_TOUCH_BLACK_COLOR);
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < 4; i ++) {
|
2018-03-28 22:36:52 +02:00
|
|
|
this.setStyleFill(`${this.normalizeId(TouchSensorView.RECT_ID[i])}`, `${this.normalizeId(TouchSensorView.TOUCH_GRADIENT_UNPRESSED[i])}`, TouchSensorView.LIGHT_TOUCH_RED_COLOR);
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|