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;
|
2021-09-23 00:51:32 +02:00
|
|
|
user-select: none;
|
2018-01-31 23:21:17 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
2018-04-10 20:50:58 +02:00
|
|
|
.sim-text.medium {
|
|
|
|
font-size:16px;
|
|
|
|
}
|
2017-12-28 01:30:42 +01:00
|
|
|
.sim-text.large {
|
2018-05-02 22:58:59 +02:00
|
|
|
font-size:20px;
|
2017-12-28 01:30:42 +01:00
|
|
|
}
|
|
|
|
.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 {
|
2018-05-02 22:58:59 +02:00
|
|
|
fill: #5A5A5A; /*#F12B21;*/
|
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;
|
|
|
|
}
|
2018-05-02 22:58:59 +02:00
|
|
|
.sim-color-selected {
|
|
|
|
stroke-width: 1px !important;
|
|
|
|
stroke: #A8AAA8 !important;
|
|
|
|
}
|
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;
|
2018-05-04 19:36:48 +02:00
|
|
|
highContrast?: boolean;
|
2017-07-11 10:15:17 +02:00
|
|
|
display?: string;
|
|
|
|
buttonOuter?: string;
|
|
|
|
buttonUps: string[];
|
|
|
|
buttonDown?: string;
|
2018-05-04 19:36:48 +02:00
|
|
|
wireColor?: string;
|
|
|
|
backgroundViewColor?: string;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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'],
|
2018-05-04 19:36:48 +02:00
|
|
|
buttonDown: "#000",
|
|
|
|
wireColor: '#5A5A5A',
|
|
|
|
backgroundViewColor: '#d6edff'
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-28 22:36:52 +02:00
|
|
|
export function randomTheme(highContrast?: boolean, light?: boolean): IBoardTheme {
|
2018-05-04 19:36:48 +02:00
|
|
|
let theme = themes[Math.floor(Math.random() * themes.length)];
|
|
|
|
if (highContrast) {
|
|
|
|
theme = JSON.parse(JSON.stringify(theme)) as IBoardTheme;
|
|
|
|
theme.highContrast = true;
|
|
|
|
theme.wireColor = '#ffffff';
|
|
|
|
theme.backgroundViewColor = '#ffffff';
|
|
|
|
}
|
|
|
|
return theme;
|
2017-07-11 10:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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[] } = {};
|
2018-05-02 22:58:59 +02:00
|
|
|
private cachedCloseIcons: { [index: string]: View } = {};
|
|
|
|
private cachedBackgroundViews: { [index: string]: View } = {};
|
2017-12-18 22:04:17 +01:00
|
|
|
|
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)
|
2019-10-09 06:57:55 +02:00
|
|
|
U.addClass(this.element, "sim-wireframe");
|
2017-07-11 10:15:17 +02:00
|
|
|
|
|
|
|
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);
|
2023-05-06 00:54:56 +02:00
|
|
|
} else if (state.getMode() == ColorSensorMode.RefRaw) {
|
|
|
|
view = new ColorWheelControl(this.element, this.defs, state, port);
|
2017-12-18 22:04:17 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2018-05-02 22:58:59 +02:00
|
|
|
private getCloseIconView(port: number) {
|
|
|
|
if (this.cachedCloseIcons[port]) {
|
|
|
|
return this.cachedCloseIcons[port];
|
|
|
|
}
|
|
|
|
const closeIcon = new CloseIconControl(this.element, this.defs, new PortNode(-1), -1);
|
|
|
|
this.cachedCloseIcons[port] = closeIcon;
|
|
|
|
return closeIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBackgroundView(port: number) {
|
|
|
|
if (this.cachedBackgroundViews[port]) {
|
|
|
|
return this.cachedBackgroundViews[port];
|
|
|
|
}
|
|
|
|
const backgroundView = new BackgroundViewControl(this.element, this.defs, new PortNode(-1), -1);
|
|
|
|
this.cachedBackgroundViews[port] = backgroundView;
|
|
|
|
return backgroundView;
|
2017-12-22 23:00:23 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-03-28 22:36:52 +02:00
|
|
|
this.element = svg.elt("svg", { height: "100%", width: "100%", "class": "user-select-none" }) 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();
|
2018-05-04 19:36:48 +02:00
|
|
|
this.layoutView.inject(this.element, this.props.theme);
|
2017-07-11 10:15:17 +02:00
|
|
|
|
2018-04-10 01:21:41 +02:00
|
|
|
const brick = new BrickViewPortrait(-1);
|
|
|
|
this.layoutView.setBrick(brick);
|
|
|
|
EV3View.isPreviousBrickLandscape() ? this.layoutView.setlectBrick() : this.layoutView.unselectBrick();
|
2018-05-02 22:58:59 +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";
|
2021-09-23 00:51:32 +02:00
|
|
|
(this.screenCanvas.style as any).msUserSelect = "none";
|
2018-02-26 22:58:51 +01:00
|
|
|
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 => {
|
2018-04-10 01:21:41 +02:00
|
|
|
this.layoutView.toggleBrickSelect();
|
2018-01-31 07:22:21 +01:00
|
|
|
this.resize();
|
|
|
|
})
|
|
|
|
/*
|
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);
|
|
|
|
})
|
|
|
|
}
|
2018-03-28 22:36:52 +02:00
|
|
|
// Kill the brick
|
2018-04-13 23:01:37 +02:00
|
|
|
this.layoutView.getPortraitBrick().kill();
|
|
|
|
this.layoutView.getLandscapeBrick().kill();
|
2018-03-28 22:36:52 +02:00
|
|
|
|
2017-12-28 20:17:18 +01:00
|
|
|
// Save previous inputs for the next cycle
|
2018-05-02 22:58:59 +02:00
|
|
|
EV3View.previousSelectedInputs = {};
|
|
|
|
ev3board().getInputNodes().forEach((node, index) =>
|
|
|
|
EV3View.previousSelectedInputs[index] = (this.getDisplayViewForNode(node.id, index).getSelected()));
|
|
|
|
EV3View.previousSeletedOutputs = {};
|
|
|
|
ev3board().getMotors().forEach((node, index) =>
|
|
|
|
EV3View.previousSeletedOutputs[index] = (this.getDisplayViewForNode(node.id, index).getSelected()));
|
2018-04-10 01:21:41 +02:00
|
|
|
EV3View.previousBrickLandscape = this.layoutView.isBrickLandscape();
|
2017-12-28 20:17:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:58:59 +02:00
|
|
|
private static previousSelectedInputs: pxt.Map<boolean> = {};
|
|
|
|
private static previousSeletedOutputs: pxt.Map<boolean> = {};
|
2018-04-10 01:21:41 +02:00
|
|
|
private static previousBrickLandscape: boolean;
|
2017-12-28 20:17:18 +01:00
|
|
|
|
2018-05-02 22:58:59 +02:00
|
|
|
private static isPreviousInputSelected(index: number) {
|
|
|
|
const previousInput = EV3View.previousSelectedInputs[index];
|
|
|
|
delete EV3View.previousSelectedInputs[index];
|
|
|
|
return previousInput;
|
2017-12-28 20:17:18 +01:00
|
|
|
}
|
|
|
|
|
2018-05-02 22:58:59 +02:00
|
|
|
private static isPreviousOutputSelected(index: number) {
|
|
|
|
const previousOutput = EV3View.previousSeletedOutputs[index];
|
|
|
|
delete EV3View.previousSeletedOutputs[index];
|
|
|
|
return previousOutput;
|
2017-12-19 23:55:43 +01:00
|
|
|
}
|
2018-01-31 07:22:21 +01:00
|
|
|
|
2018-04-10 01:21:41 +02:00
|
|
|
private static isPreviousBrickLandscape() {
|
|
|
|
const b = EV3View.previousBrickLandscape;
|
|
|
|
EV3View.previousBrickLandscape = false;
|
2018-01-31 07:22:21 +01:00
|
|
|
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) {
|
2018-05-02 22:58:59 +02:00
|
|
|
const previousSelected = EV3View.isPreviousInputSelected(index);
|
|
|
|
const isSelected = previousSelected != undefined ? previousSelected : view.getSelected();
|
|
|
|
view.setSelected(isSelected);
|
2018-01-03 07:22:14 +01:00
|
|
|
const control = isSelected ? this.getControlForNode(node.id, index, !node.modeChange()) : undefined;
|
2018-05-02 22:58:59 +02:00
|
|
|
const closeIcon = control ? this.getCloseIconView(index + 10) : undefined;
|
|
|
|
const backgroundView = control && view.hasBackground() ? this.getBackgroundView(index + 10) : undefined;
|
|
|
|
this.layoutView.setInput(index, view, control, closeIcon, backgroundView);
|
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()) {
|
2018-04-13 23:01:37 +02:00
|
|
|
this.layoutView.getPortraitBrick().updateState();
|
|
|
|
this.layoutView.getLandscapeBrick().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) {
|
2018-05-02 22:58:59 +02:00
|
|
|
const previousSelected = EV3View.isPreviousOutputSelected(index);
|
|
|
|
const isSelected = previousSelected != undefined ? previousSelected : view.getSelected();
|
|
|
|
view.setSelected(isSelected);
|
2017-12-28 20:17:18 +01:00
|
|
|
const control = isSelected ? this.getControlForNode(node.id, index) : undefined;
|
2018-05-02 22:58:59 +02:00
|
|
|
const closeIcon = control ? this.getCloseIconView(index) : undefined;
|
|
|
|
const backgroundView = control && view.hasBackground() ? this.getBackgroundView(index) : undefined;
|
|
|
|
this.layoutView.setOutput(index, view, control, closeIcon, backgroundView);
|
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) {
|
2018-04-10 01:21:41 +02:00
|
|
|
const isLandscape = this.layoutView.isBrickLandscape();
|
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;
|
|
|
|
|
2018-04-10 01:21:41 +02:00
|
|
|
this.screenCanvas.style.cursor = !isLandscape ? "zoom-in" : "zoom-out";
|
|
|
|
|
2017-12-18 22:04:17 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|