pxt-ev3/sim/visuals/nodes/touchSensorView.ts

74 lines
3.0 KiB
TypeScript
Raw Normal View History

/// <reference path="./moduleView.ts" />
2017-12-18 22:04:17 +01:00
namespace pxsim.visuals {
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[];
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);
}
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);
}
private setStyleFill(svgId: string, fillUrl: string, lightFill: string) {
2017-12-18 22:04:17 +01:00
const el = (this.content.getElementById(svgId) as SVGRectElement);
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;
pointerEvents.down.forEach(evid => btn.addEventListener(evid, ev => {
2017-12-18 22:04:17 +01:00
this.setPressed(true);
state.setPressed(true);
}))
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 ++) {
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 ++) {
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
}
}
}
}
}