pxt-ev3/sim/visuals/board.ts

450 lines
16 KiB
TypeScript
Raw Normal View History

2017-12-18 22:04:17 +01:00
/// <reference path="./layoutView.ts" />
namespace pxsim {
export const GAME_LOOP_FPS = 32;
}
2017-07-11 10:15:17 +02:00
namespace pxsim.visuals {
2017-12-18 22:04:17 +01:00
const EV3_STYLE = `
2017-07-11 10:15:17 +02:00
svg.sim {
margin-bottom:1em;
}
svg.sim.grayscale {
-moz-filter: grayscale(1);
-webkit-filter: grayscale(1);
filter: grayscale(1);
}
.sim-button {
cursor: pointer;
}
.sim-button:hover {
stroke-width: 2px !important;
stroke: white !important;
}
.sim-systemled {
fill:#333;
stroke:#555;
stroke-width: 1px;
}
.sim-text {
font-family:"Lucida Console", Monaco, monospace;
font-size:8px;
fill:#fff;
pointer-events: none; user-select: none;
}
.sim-text.small {
font-size:6px;
}
.sim-text.inverted {
fill:#000;
}
2017-12-18 22:04:17 +01:00
/* Color Grid */
.sim-color-grid-circle:hover {
stroke-width: 0.4;
stroke: #000;
2017-07-11 10:15:17 +02:00
cursor: pointer;
}
2017-12-18 22:04:17 +01:00
.sim-color-wheel-half:hover {
stroke-width: 1;
stroke: #000;
fill: gray !important;
cursor: pointer;
2017-07-11 10:15:17 +02:00
}
`;
2017-12-18 22:04:17 +01:00
const EV3_WIDTH = 99.984346;
const EV3_HEIGHT = 151.66585;
export const SCREEN_WIDTH = 178;
export const SCREEN_HEIGHT = 128;
2017-07-11 10:15:17 +02:00
export interface IBoardTheme {
accent?: string;
display?: string;
buttonOuter?: string;
buttonUps: string[];
buttonDown?: string;
}
export var themes: IBoardTheme[] = ["#3ADCFE"].map(accent => {
return {
accent: accent,
buttonOuter: "#979797",
2017-12-18 22:04:17 +01:00
buttonUps: ["#a8aaa8", "#393939", "#a8aaa8", "#a8aaa8", "#a8aaa8", '#a8aaa8'],
buttonDown: "#000"
2017-07-11 10:15:17 +02:00
}
});
export function randomTheme(): IBoardTheme {
return themes[Math.floor(Math.random() * themes.length)];
}
export interface IBoardProps {
runtime?: pxsim.Runtime;
theme?: IBoardTheme;
disableTilt?: boolean;
wireframe?: boolean;
}
2017-12-18 22:04:17 +01:00
export class EV3View implements BoardView {
public static BOARD_WIDTH = 500;
public static BOARD_HEIGHT = 500;
public wrapper: HTMLDivElement;
2017-07-11 10:15:17 +02:00
public element: SVGSVGElement;
private style: SVGStyleElement;
private defs: SVGDefsElement;
2017-12-18 22:04:17 +01:00
private layoutView: LayoutView;
private cachedControlNodes: { [index: string]: View[] } = {};
private cachedDisplayViews: { [index: string]: LayoutElement[] } = {};
private screenCanvas: HTMLCanvasElement;
private screenCanvasCtx: CanvasRenderingContext2D;
private screenCanvasData: ImageData;
2017-12-18 22:04:17 +01:00
private screenCanvasTemp: HTMLCanvasElement;
private screenScaledWidth: number;
private screenScaledHeight: number;
private width = 0;
private height = 0;
private g: SVGGElement;
public board: pxsim.EV3Board;
2017-07-11 10:15:17 +02:00
constructor(public props: IBoardProps) {
this.buildDom();
2017-12-18 22:04:17 +01:00
const dalBoard = board();
dalBoard.updateSubscribers.push(() => this.updateState());
2017-07-11 10:15:17 +02:00
if (props && props.wireframe)
svg.addClass(this.element, "sim-wireframe");
if (props && props.theme)
this.updateTheme();
2017-12-18 22:04:17 +01:00
2017-07-11 10:15:17 +02:00
if (props && props.runtime) {
2017-12-18 22:04:17 +01:00
this.board = this.props.runtime.board as pxsim.EV3Board;
2017-07-11 10:15:17 +02:00
this.board.updateSubscribers.push(() => this.updateState());
this.updateState();
}
Runtime.messagePosted = (msg) => {
switch (msg.type || "") {
case "status": {
const state = (msg as pxsim.SimulatorStateMessage).state;
if (state == "killed") this.kill();
if (state == "running") this.begin();
break;
}
}
}
2017-07-11 10:15:17 +02:00
}
public getView(): SVGAndSize<SVGSVGElement> {
return {
2017-12-18 22:04:17 +01:00
el: this.wrapper as any,
2017-07-11 10:15:17 +02:00
y: 0,
x: 0,
2017-12-18 22:04:17 +01:00
w: EV3View.BOARD_WIDTH,
h: EV3View.BOARD_WIDTH
2017-07-11 10:15:17 +02:00
};
}
public getCoord(pinNm: string): Coord {
2017-12-18 22:04:17 +01:00
// Not needed
return undefined;
2017-07-11 10:15:17 +02:00
}
public highlightPin(pinNm: string): void {
2017-12-18 22:04:17 +01:00
// Not needed
2017-07-11 10:15:17 +02:00
}
public getPinDist(): number {
2017-12-18 22:04:17 +01:00
// Not needed
2017-07-11 10:15:17 +02:00
return 10;
}
2017-12-18 22:04:17 +01:00
public updateTheme() {
2017-07-11 10:15:17 +02:00
let theme = this.props.theme;
2017-12-18 22:04:17 +01:00
this.layoutView.updateTheme(theme);
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
private getControlForNode(id: NodeType, port: number) {
if (this.cachedControlNodes[id] && this.cachedControlNodes[id][port]) {
return this.cachedControlNodes[id][port];
}
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
let view: View;
switch (id) {
case NodeType.ColorSensor: {
const state = ev3board().getInputNodes()[port] as ColorSensorNode;
if (state.getMode() == ColorSensorMode.Colors) {
view = new ColorGridControl(this.element, this.defs, state, port);
} else if (state.getMode() == ColorSensorMode.Reflected) {
view = new ColorWheelControl(this.element, this.defs, state, port);
} else if (state.getMode() == ColorSensorMode.Ambient) {
view = new ColorWheelControl(this.element, this.defs, state, port);
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
break;
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
case NodeType.UltrasonicSensor: {
const state = ev3board().getInputNodes()[port] as UltrasonicSensorNode;
view = new DistanceSliderControl(this.element, this.defs, state, port);
break;
}
case NodeType.GyroSensor: {
const state = ev3board().getInputNodes()[port] as GyroSensorNode;
view = new RotationSliderControl(this.element, this.defs, state, port);
break;
}
case NodeType.MediumMotor:
case NodeType.LargeMotor: {
// const state = ev3board().getMotor(port)[0];
// view = new MotorInputControl(this.element, this.defs, state, port);
// break;
2017-07-11 10:15:17 +02:00
}
}
2017-12-18 22:04:17 +01:00
if (view) {
if (!this.cachedControlNodes[id]) this.cachedControlNodes[id] = [];
this.cachedControlNodes[id][port] = view;
return view;
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
return undefined;
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
private getDisplayViewForNode(id: NodeType, port: number): LayoutElement {
if (this.cachedDisplayViews[id] && this.cachedDisplayViews[id][port]) {
return this.cachedDisplayViews[id][port];
}
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
let view: LayoutElement;
switch (id) {
case NodeType.TouchSensor:
view = new TouchSensorView(port); break;
case NodeType.MediumMotor:
view = new MediumMotorView(port); break;
case NodeType.LargeMotor:
view = new LargeMotorView(port); break;
case NodeType.GyroSensor:
view = new GyroSensorView(port); break;
case NodeType.ColorSensor:
view = new ColorSensorView(port); break;
case NodeType.UltrasonicSensor:
view = new UltrasonicSensorView(port); break;
case NodeType.Brick:
//return new BrickView(0);
view = this.layoutView.getBrick(); break;
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
if (view) {
if (!this.cachedDisplayViews[id]) this.cachedDisplayViews[id] = [];
this.cachedDisplayViews[id][port] = view;
return view;
}
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
return undefined;
2017-07-11 10:15:17 +02:00
}
private getCloseIconView() {
return new CloseIconControl(this.element, this.defs, new PortNode(-1), -1);
}
2017-12-18 22:04:17 +01:00
private buildDom() {
this.wrapper = document.createElement('div');
this.wrapper.style.display = 'inline';
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.element = svg.elt("svg", { height: "100%", width: "100%" }) as SVGSVGElement;
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.defs = svg.child(this.element, "defs") as SVGDefsElement;
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.style = svg.child(this.element, "style", {}) as SVGStyleElement;
this.style.textContent = EV3_STYLE;
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.layoutView = new LayoutView();
this.layoutView.inject(this.element);
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
// Add EV3 module element
this.layoutView.setBrick(new BrickView(-1));
2017-12-18 22:04:17 +01:00
this.resize();
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
// Add Screen canvas to board
this.buildScreenCanvas();
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.wrapper.appendChild(this.element);
this.wrapper.appendChild(this.screenCanvas);
this.wrapper.appendChild(this.screenCanvasTemp);
2017-12-18 22:04:17 +01:00
window.addEventListener("resize", e => {
this.resize();
});
}
public resize() {
if (!this.element) return;
this.width = document.body.offsetWidth;
this.height = document.body.offsetHeight;
this.layoutView.layout(this.width, this.height);
this.updateState();
let state = ev3board().screenState;
this.updateScreenStep(state);
2017-12-18 22:04:17 +01:00
}
2017-12-18 22:04:17 +01:00
private buildScreenCanvas() {
this.screenCanvas = document.createElement("canvas");
2017-12-18 22:04:17 +01:00
this.screenCanvas.id = "board-screen-canvas";
this.screenCanvas.style.position = "absolute";
this.screenCanvas.style.cursor = "crosshair";
this.screenCanvas.onmousemove = (e: MouseEvent) => {
const x = e.clientX;
const y = e.clientY;
2017-12-18 22:04:17 +01:00
const bBox = this.screenCanvas.getBoundingClientRect();
this.updateXY(Math.floor((x - bBox.left) / this.screenScaledWidth * SCREEN_WIDTH),
Math.floor((y - bBox.top) / this.screenScaledHeight * SCREEN_HEIGHT));
}
this.screenCanvas.onmouseleave = () => {
2017-12-18 22:04:17 +01:00
this.updateXY(SCREEN_WIDTH, SCREEN_HEIGHT);
}
this.screenCanvas.width = SCREEN_WIDTH;
this.screenCanvas.height = SCREEN_HEIGHT;
2017-12-18 22:04:17 +01:00
this.screenCanvasCtx = this.screenCanvas.getContext("2d");
2017-12-18 22:04:17 +01:00
this.screenCanvasTemp = document.createElement("canvas");
this.screenCanvasTemp.style.display = 'none';
2017-07-11 10:15:17 +02:00
}
private kill() {
this.running = false;
if (this.lastAnimationIds.length > 0) {
this.lastAnimationIds.forEach(animationId => {
cancelAnimationFrame(animationId);
})
}
}
private begin() {
this.running = true;
this.updateState();
}
private running: boolean = false;
private lastAnimationIds: number[] = [];
public updateState() {
if (this.lastAnimationIds.length > 0) {
this.lastAnimationIds.forEach(animationId => {
cancelAnimationFrame(animationId);
})
}
if (!this.running) return;
const fps = GAME_LOOP_FPS;
let now;
let then = Date.now();
let interval = 1000 / fps;
let delta;
let that = this;
function loop() {
const animationId = requestAnimationFrame(loop);
that.lastAnimationIds.push(animationId);
now = Date.now();
delta = now - then;
if (delta > interval) {
2017-12-20 01:54:44 +01:00
then = now;
2017-12-20 01:03:26 +01:00
that.updateStateStep(delta);
}
}
loop();
}
2017-12-20 01:03:26 +01:00
private updateStateStep(elapsed: number) {
const inputNodes = ev3board().getInputNodes();
inputNodes.forEach((node, index) => {
2017-12-20 01:03:26 +01:00
node.updateState(elapsed);
const view = this.getDisplayViewForNode(node.id, index);
if (!node.didChange() && !view.didChange()) return;
if (view) {
const control = view.getSelected() ? this.getControlForNode(node.id, index) : undefined;
const closeIcon = control ? this.getCloseIconView() : undefined;
this.layoutView.setInput(index, view, control, closeIcon);
view.updateState();
if (control) control.updateState();
}
});
const brickNode = ev3board().getBrickNode();
if (brickNode.didChange()) {
this.getDisplayViewForNode(brickNode.id, -1).updateState();
}
const outputNodes = ev3board().getMotors();
outputNodes.forEach((node, index) => {
2017-12-20 01:03:26 +01:00
node.updateState(elapsed);
const view = this.getDisplayViewForNode(node.id, index);
if (!node.didChange() && !view.didChange()) return;
if (view) {
const control = view.getSelected() ? this.getControlForNode(node.id, index) : undefined;
const closeIcon = control ? this.getCloseIconView() : undefined;
this.layoutView.setOutput(index, view, control, closeIcon);
view.updateState();
if (control) control.updateState();
}
});
let state = ev3board().screenState;
if (state.didChange()) {
this.updateScreenStep(state);
2017-12-20 02:20:01 +01:00
}
}
private updateScreenStep(state: EV3ScreenState) {
2017-12-18 22:04:17 +01:00
const bBox = this.layoutView.getBrick().getScreenBBox();
2017-12-18 22:19:49 +01:00
if (!bBox || bBox.width == 0) return;
2017-12-18 22:04:17 +01:00
const scale = (bBox.width - 4) / SCREEN_WIDTH;
this.screenScaledWidth = (bBox.width - 4);
2017-12-18 22:04:17 +01:00
this.screenScaledHeight = this.screenScaledWidth / SCREEN_WIDTH * SCREEN_HEIGHT;
this.screenCanvas.style.top = `${bBox.top + 2}px`;
this.screenCanvas.style.left = `${bBox.left + 2}px`;
2017-12-18 22:04:17 +01:00
this.screenCanvas.width = this.screenScaledWidth;
this.screenCanvas.height = this.screenScaledHeight;
this.screenCanvasData = this.screenCanvasCtx.getImageData(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
let sp = 3
const points = state.points
const data = this.screenCanvasData.data
for (let i = 0; i < points.length; ++i) {
data[sp] = points[i]
sp += 4;
2017-07-11 10:15:17 +02:00
}
2017-12-18 22:04:17 +01:00
// Move the image to another canvas element in order to scale it
this.screenCanvasTemp.style.width = `${SCREEN_WIDTH}`;
this.screenCanvasTemp.style.height = `${SCREEN_HEIGHT}`;
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.screenCanvasTemp.getContext("2d").putImageData(this.screenCanvasData, 0, 0);
2017-07-11 10:15:17 +02:00
2017-12-18 22:04:17 +01:00
this.screenCanvasCtx.scale(scale, scale);
this.screenCanvasCtx.drawImage(this.screenCanvasTemp, 0, 0);
}
private updateXY(width: number, height: number) {
const screenWidth = Math.max(0, Math.min(SCREEN_WIDTH, width));
const screenHeight = Math.max(0, Math.min(SCREEN_HEIGHT, height));
console.log(`width: ${screenWidth}, height: ${screenHeight}`);
// TODO: add a reporter for the hovered XY position
2017-07-11 10:15:17 +02:00
}
}
}