2017-12-18 22:04:17 +01:00
|
|
|
/// <reference path="./layoutView.ts" />
|
|
|
|
|
2017-12-19 23:20:35 +01:00
|
|
|
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);
|
|
|
|
}
|
2018-01-31 23:21:17 +01:00
|
|
|
.user-select-none, .sim-button {
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
user-select: none;
|
|
|
|
}
|
2017-07-11 10:15:17 +02:00
|
|
|
.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;
|
2017-12-28 01:30:42 +01:00
|
|
|
pointer-events: none;
|
|
|
|
user-select: none;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
.sim-text.small {
|
|
|
|
font-size:6px;
|
|
|
|
}
|
2017-12-28 01:30:42 +01:00
|
|
|
.sim-text.large {
|
|
|
|
font-size:30px;
|
|
|
|
}
|
|
|
|
.sim-text.number {
|
2017-12-28 05:43:39 +01:00
|
|
|
font-family: Courier, Lato, Work Sans, PT Serif, Source Serif Pro;
|
2017-12-28 22:23:30 +01:00
|
|
|
/*font-weight: bold;*/
|
2017-12-28 01:30:42 +01:00
|
|
|
}
|
2017-07-11 10:15:17 +02:00
|
|
|
.sim-text.inverted {
|
2017-12-28 05:43:39 +01:00
|
|
|
fill:#5A5A5A;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 21:04:37 +01:00
|
|
|
.no-drag, .sim-text, .sim-text-pin {
|
|
|
|
user-drag: none;
|
|
|
|
user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-webkit-user-drag: none;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
}
|
|
|
|
|
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-28 22:23:30 +01:00
|
|
|
|
|
|
|
/* Motor slider */
|
|
|
|
.sim-motor-btn {
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
2017-12-29 20:39:06 +01:00
|
|
|
.sim-motor-btn:hover .btn {
|
|
|
|
stroke-width: 2px;
|
|
|
|
stroke: black !important;
|
2017-12-28 22:23:30 +01:00
|
|
|
}
|
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;
|
2017-07-13 22:01:39 +02:00
|
|
|
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[] } = {};
|
|
|
|
|
2017-07-12 09:25:17 +02:00
|
|
|
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();
|
|
|
|
}
|
2017-12-19 23:55:43 +01:00
|
|
|
|
|
|
|
Runtime.messagePosted = (msg) => {
|
|
|
|
switch (msg.type || "") {
|
2017-12-22 23:00:23 +01:00
|
|
|
case "status": {
|
|
|
|
const state = (msg as pxsim.SimulatorStateMessage).state;
|
|
|
|
if (state == "killed") this.kill();
|
|
|
|
if (state == "running") this.begin();
|
|
|
|
break;
|
|
|
|
}
|
2017-12-19 23:55:43 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-01-03 07:22:14 +01:00
|
|
|
private getControlForNode(id: NodeType, port: number, useCache = true) {
|
|
|
|
if (useCache && this.cachedControlNodes[id] && this.cachedControlNodes[id][port]) {
|
2017-12-18 22:04:17 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-02-02 07:03:01 +01:00
|
|
|
case NodeType.InfraredSensor: {
|
2018-02-02 23:19:07 +01:00
|
|
|
const state = ev3board().getInputNodes()[port] as InfraredSensorNode;
|
2018-02-02 07:03:01 +01:00
|
|
|
if (state.getMode() == InfraredSensorMode.Proximity)
|
|
|
|
view = new ProximitySliderControl(this.element, this.defs, state, port);
|
2018-02-02 18:48:27 +01:00
|
|
|
else if (state.getMode() == InfraredSensorMode.RemoteControl)
|
|
|
|
view = new RemoteBeaconButtonsControl(this.element, this.defs, state, port);
|
2018-02-02 07:03:01 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-12-18 22:04:17 +01:00
|
|
|
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: {
|
2017-12-28 02:06:23 +01:00
|
|
|
const state = ev3board().getMotors()[port];
|
2018-01-10 19:00:48 +01:00
|
|
|
view = new MotorSliderControl(this.element, this.defs, state, port);
|
2017-12-28 02:06:23 +01:00
|
|
|
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;
|
2018-02-02 07:03:01 +01:00
|
|
|
case NodeType.InfraredSensor:
|
|
|
|
view = new InfraredView(port); break;
|
2017-12-18 22:04:17 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-22 23:00:23 +01: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
|
2018-01-31 07:22:21 +01:00
|
|
|
const brickCloseIcon = this.getCloseIconView();
|
2018-02-02 07:03:01 +01:00
|
|
|
brickCloseIcon.registerClick(ev => {
|
|
|
|
this.layoutView.unselectBrick();
|
2018-01-31 07:22:21 +01:00
|
|
|
this.resize();
|
|
|
|
});
|
2018-02-02 07:03:01 +01:00
|
|
|
const brick = new BrickView(-1);
|
2018-01-31 07:22:21 +01:00
|
|
|
brick.setSelected(EV3View.isPreviousBrickSelected());
|
|
|
|
this.layoutView.setBrick(brick, brickCloseIcon);
|
2017-07-12 09:25:17 +02:00
|
|
|
|
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-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
window.addEventListener("resize", e => {
|
|
|
|
this.resize();
|
2017-07-12 09:25:17 +02:00
|
|
|
});
|
2017-12-22 23:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public resize() {
|
|
|
|
if (!this.element) return;
|
2017-12-24 20:59:01 +01:00
|
|
|
this.width = document.body.offsetWidth;
|
|
|
|
this.height = document.body.offsetHeight;
|
|
|
|
this.layoutView.layout(this.width, this.height);
|
2017-12-22 23:00:23 +01:00
|
|
|
|
|
|
|
this.updateState();
|
|
|
|
let state = ev3board().screenState;
|
|
|
|
this.updateScreenStep(state);
|
2017-12-18 22:04:17 +01:00
|
|
|
}
|
2017-07-12 09:25:17 +02:00
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
private buildScreenCanvas() {
|
2017-07-12 09:25:17 +02:00
|
|
|
this.screenCanvas = document.createElement("canvas");
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvas.id = "board-screen-canvas";
|
2018-02-26 22:58:51 +01:00
|
|
|
this.screenCanvas.style.userSelect = "none";
|
|
|
|
this.screenCanvas.style.msUserSelect = "none";
|
|
|
|
this.screenCanvas.style.webkitUserSelect = "none";
|
|
|
|
(this.screenCanvas.style as any).MozUserSelect = "none";
|
2017-12-18 22:04:17 +01:00
|
|
|
this.screenCanvas.style.position = "absolute";
|
2018-01-31 07:22:21 +01:00
|
|
|
this.screenCanvas.addEventListener(pxsim.pointerEvents.up, ev => {
|
|
|
|
this.layoutView.selectBrick();
|
|
|
|
this.resize();
|
|
|
|
})
|
|
|
|
this.screenCanvas.style.cursor = "pointer";
|
|
|
|
/*
|
2017-07-12 09:25:17 +02:00
|
|
|
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));
|
2017-07-12 09:25:17 +02:00
|
|
|
}
|
|
|
|
this.screenCanvas.onmouseleave = () => {
|
2017-12-18 22:04:17 +01:00
|
|
|
this.updateXY(SCREEN_WIDTH, SCREEN_HEIGHT);
|
2017-07-12 09:25:17 +02:00
|
|
|
}
|
2018-01-31 07:22:21 +01:00
|
|
|
*/
|
2017-07-12 09:25:17 +02:00
|
|
|
|
|
|
|
this.screenCanvas.width = SCREEN_WIDTH;
|
|
|
|
this.screenCanvas.height = SCREEN_HEIGHT;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2017-07-12 09:25:17 +02: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
|
|
|
}
|
|
|
|
|
2017-12-19 23:55:43 +01:00
|
|
|
private kill() {
|
2017-12-22 23:00:23 +01:00
|
|
|
this.running = false;
|
|
|
|
if (this.lastAnimationIds.length > 0) {
|
|
|
|
this.lastAnimationIds.forEach(animationId => {
|
|
|
|
cancelAnimationFrame(animationId);
|
|
|
|
})
|
|
|
|
}
|
2017-12-28 20:17:18 +01:00
|
|
|
// Save previous inputs for the next cycle
|
|
|
|
EV3View.previousSelectedInputs = ev3board().getInputNodes().map((node, index) => (this.getDisplayViewForNode(node.id, index).getSelected()) ? node.id : -1)
|
|
|
|
EV3View.previousSeletedOutputs = ev3board().getMotors().map((node, index) => (this.getDisplayViewForNode(node.id, index).getSelected()) ? node.id : -1);
|
2018-01-31 07:22:21 +01:00
|
|
|
EV3View.previousSelectedBrick = this.layoutView.getBrick().getSelected();
|
2017-12-28 20:17:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static previousSelectedInputs: number[];
|
|
|
|
private static previousSeletedOutputs: number[];
|
2018-01-31 07:22:21 +01:00
|
|
|
private static previousSelectedBrick: boolean;
|
2017-12-28 20:17:18 +01:00
|
|
|
|
|
|
|
private static isPreviousInputSelected(index: number, id: number) {
|
|
|
|
if (EV3View.previousSelectedInputs && EV3View.previousSelectedInputs[index] == id) {
|
|
|
|
EV3View.previousSelectedInputs[index] = undefined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static isPreviousOutputSelected(index: number, id: number) {
|
|
|
|
if (EV3View.previousSeletedOutputs && EV3View.previousSeletedOutputs[index] == id) {
|
|
|
|
EV3View.previousSeletedOutputs[index] = undefined;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-19 23:55:43 +01:00
|
|
|
}
|
2018-01-31 07:22:21 +01:00
|
|
|
|
|
|
|
private static isPreviousBrickSelected() {
|
|
|
|
const b = EV3View.previousSelectedBrick;
|
|
|
|
EV3View.previousSelectedBrick = false;
|
|
|
|
return !!b;
|
|
|
|
}
|
2017-12-19 23:55:43 +01:00
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
private begin() {
|
|
|
|
this.running = true;
|
2017-12-24 20:59:01 +01:00
|
|
|
this.updateState();
|
2017-12-22 23:00:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private running: boolean = false;
|
|
|
|
private lastAnimationIds: number[] = [];
|
2017-12-19 23:20:35 +01:00
|
|
|
public updateState() {
|
2017-12-22 23:00:23 +01:00
|
|
|
if (this.lastAnimationIds.length > 0) {
|
|
|
|
this.lastAnimationIds.forEach(animationId => {
|
|
|
|
cancelAnimationFrame(animationId);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!this.running) return;
|
2017-12-19 23:20:35 +01:00
|
|
|
const fps = GAME_LOOP_FPS;
|
|
|
|
let now;
|
2017-12-28 18:07:57 +01:00
|
|
|
let then = pxsim.U.now();
|
2017-12-19 23:20:35 +01:00
|
|
|
let interval = 1000 / fps;
|
|
|
|
let delta;
|
|
|
|
let that = this;
|
|
|
|
function loop() {
|
2017-12-22 23:00:23 +01:00
|
|
|
const animationId = requestAnimationFrame(loop);
|
|
|
|
that.lastAnimationIds.push(animationId);
|
2017-12-28 18:07:57 +01:00
|
|
|
now = pxsim.U.now();
|
2017-12-19 23:20:35 +01:00
|
|
|
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);
|
2017-12-19 23:20:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:03:26 +01:00
|
|
|
private updateStateStep(elapsed: number) {
|
2017-12-19 23:20:35 +01:00
|
|
|
const inputNodes = ev3board().getInputNodes();
|
|
|
|
inputNodes.forEach((node, index) => {
|
2017-12-20 01:03:26 +01:00
|
|
|
node.updateState(elapsed);
|
2017-12-19 23:20:35 +01:00
|
|
|
const view = this.getDisplayViewForNode(node.id, index);
|
2017-12-22 23:00:23 +01:00
|
|
|
if (!node.didChange() && !view.didChange()) return;
|
2017-12-19 23:20:35 +01:00
|
|
|
if (view) {
|
2017-12-28 20:17:18 +01:00
|
|
|
const isSelected = EV3View.isPreviousInputSelected(index, node.id) || view.getSelected();
|
|
|
|
if (isSelected && !view.getSelected()) view.setSelected(true);
|
2018-01-03 07:22:14 +01:00
|
|
|
const control = isSelected ? this.getControlForNode(node.id, index, !node.modeChange()) : undefined;
|
2017-12-22 23:00:23 +01:00
|
|
|
const closeIcon = control ? this.getCloseIconView() : undefined;
|
|
|
|
this.layoutView.setInput(index, view, control, closeIcon);
|
2017-12-19 23:20:35 +01:00
|
|
|
view.updateState();
|
2017-12-22 23:00:23 +01:00
|
|
|
if (control) control.updateState();
|
2017-12-19 23:20:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const brickNode = ev3board().getBrickNode();
|
|
|
|
if (brickNode.didChange()) {
|
2017-12-22 23:00:23 +01:00
|
|
|
this.getDisplayViewForNode(brickNode.id, -1).updateState();
|
2017-12-19 23:20:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const outputNodes = ev3board().getMotors();
|
|
|
|
outputNodes.forEach((node, index) => {
|
2017-12-20 01:03:26 +01:00
|
|
|
node.updateState(elapsed);
|
2017-12-19 23:20:35 +01:00
|
|
|
const view = this.getDisplayViewForNode(node.id, index);
|
2017-12-22 23:00:23 +01:00
|
|
|
if (!node.didChange() && !view.didChange()) return;
|
2017-12-19 23:20:35 +01:00
|
|
|
if (view) {
|
2017-12-28 20:17:18 +01:00
|
|
|
const isSelected = EV3View.isPreviousOutputSelected(index, node.id) || view.getSelected();
|
|
|
|
if (isSelected && !view.getSelected()) view.setSelected(true);
|
|
|
|
const control = isSelected ? this.getControlForNode(node.id, index) : undefined;
|
2017-12-22 23:00:23 +01:00
|
|
|
const closeIcon = control ? this.getCloseIconView() : undefined;
|
|
|
|
this.layoutView.setOutput(index, view, control, closeIcon);
|
2017-12-19 23:20:35 +01:00
|
|
|
view.updateState();
|
2017-12-22 23:00:23 +01:00
|
|
|
if (control) control.updateState();
|
2017-12-19 23:20:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-12-22 23:00:23 +01:00
|
|
|
let state = ev3board().screenState;
|
2017-12-24 20:59:01 +01:00
|
|
|
if (state.didChange()) {
|
2017-12-22 23:00:23 +01:00
|
|
|
this.updateScreenStep(state);
|
2017-12-20 02:20:01 +01:00
|
|
|
}
|
2017-12-19 23:20:35 +01:00
|
|
|
}
|
|
|
|
|
2018-02-27 00:16:17 +01:00
|
|
|
private updateScreenStep(state: ScreenState) {
|
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
|
|
|
|
2018-03-08 23:34:27 +01:00
|
|
|
const scale = (bBox.height - 2) / SCREEN_HEIGHT;
|
|
|
|
this.screenScaledHeight = (bBox.height - 2);
|
|
|
|
this.screenScaledWidth = this.screenScaledHeight / SCREEN_HEIGHT * SCREEN_WIDTH;
|
2017-12-18 22:04:17 +01:00
|
|
|
|
2018-03-08 23:34:27 +01:00
|
|
|
this.screenCanvas.style.top = `${bBox.top + 1}px`;
|
|
|
|
this.screenCanvas.style.left = `${bBox.left + ((bBox.width - this.screenScaledWidth) * 0.5)}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);
|
|
|
|
|
2018-02-27 00:16:17 +01:00
|
|
|
new Uint32Array(this.screenCanvasData.data.buffer).set(state.screen)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|