pxt-ev3/sim/visuals/controls/remoteBeaconButtons.ts
2018-02-07 09:53:58 -08:00

57 lines
2.0 KiB
TypeScript

namespace pxsim.visuals {
enum InfraredRemoteButton {
CenterBeacon = 0x01,
TopLeft = 0x02,
BottomLeft = 0x04,
TopRight = 0x08,
BottomRight = 0x10,
}
export class RemoteBeaconButtonsControl extends ControlView<InfraredSensorNode> {
private group: SVGGElement;
private id = Math.random().toString();
getInnerView() {
this.group = svg.elt("g") as SVGGElement;
this.group.setAttribute("transform", `scale(0.9, 0.9)`)
const xml = pxsim.visuals.normalizeXml(this.id, pxsim.visuals.REMOVE_SVG);
const content = svg.parseString(xml);
this.group.appendChild(content);
const btns: Map<InfraredRemoteButton> = {
"centerbeacon": InfraredRemoteButton.CenterBeacon,
"topleft": InfraredRemoteButton.TopLeft,
"topright": InfraredRemoteButton.TopRight,
"bottomleft": InfraredRemoteButton.BottomLeft,
"bottomright": InfraredRemoteButton.BottomRight
}
Object.keys(btns).forEach(bid => {
const cid = btns[bid];
const bel = content.getElementById(pxsim.visuals.normalizeId(this.id, bid));
bel.setAttribute("class", "sim-button");
pointerEvents.down.forEach(evid => bel.addEventListener(evid, ev => {
ev3board().remoteState.setPressed(cid, true);
}));
bel.addEventListener(pointerEvents.leave, ev => {
ev3board().remoteState.setPressed(cid, false);
});
bel.addEventListener(pointerEvents.up, ev => {
ev3board().remoteState.setPressed(cid, false);
});
});
return this.group;
}
getInnerWidth() {
return 80;
}
getInnerHeight() {
return 123.773;
}
}
}