2016-08-30 23:13:44 +02:00
|
|
|
namespace pxsim.visuals {
|
2016-08-31 20:14:16 +02:00
|
|
|
export interface BoardHostOpts {
|
|
|
|
state: DalBoard,
|
|
|
|
boardDef: BoardDefinition,
|
|
|
|
cmpsList?: string[],
|
|
|
|
cmpDefs: Map<PartDefinition>,
|
|
|
|
fnArgs: any,
|
|
|
|
forceBreadboard?: boolean,
|
|
|
|
maxWidth?: string,
|
|
|
|
maxHeight?: string
|
|
|
|
wireframe?: boolean
|
|
|
|
}
|
2016-08-30 23:13:44 +02:00
|
|
|
export class BoardHost {
|
|
|
|
private components: IBoardComponent<any>[] = [];
|
|
|
|
private wireFactory: WireFactory;
|
|
|
|
private breadboard: Breadboard;
|
|
|
|
private fromBBCoord: (xy: Coord) => Coord;
|
|
|
|
private fromMBCoord: (xy: Coord) => Coord;
|
|
|
|
private boardView: BoardView;
|
|
|
|
private view: SVGSVGElement;
|
|
|
|
private style: SVGStyleElement;
|
|
|
|
private defs: SVGDefsElement;
|
|
|
|
private state: DalBoard;
|
2016-09-01 03:03:34 +02:00
|
|
|
private useCrocClips: boolean;
|
2016-08-30 23:13:44 +02:00
|
|
|
|
2016-08-31 20:14:16 +02:00
|
|
|
constructor(opts: BoardHostOpts) {
|
|
|
|
this.state = opts.state;
|
|
|
|
let onboardCmps = opts.boardDef.onboardComponents || [];
|
|
|
|
let activeComponents = (opts.cmpsList || []).filter(c => onboardCmps.indexOf(c) < 0);
|
2016-08-30 23:13:44 +02:00
|
|
|
activeComponents.sort();
|
2016-09-01 03:03:34 +02:00
|
|
|
this.useCrocClips = opts.boardDef.useCrocClips;
|
2016-08-30 23:13:44 +02:00
|
|
|
|
2016-09-01 03:03:34 +02:00
|
|
|
if (opts.boardDef.visual === "microbit") {
|
|
|
|
this.boardView = new visuals.MicrobitBoardSvg({
|
|
|
|
runtime: runtime,
|
|
|
|
theme: visuals.randomTheme(),
|
|
|
|
disableTilt: false,
|
|
|
|
wireframe: opts.wireframe,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let boardVis = opts.boardDef.visual as BoardImageDefinition;
|
|
|
|
this.boardView = new visuals.GenericBoardSvg({
|
|
|
|
visualDef: boardVis,
|
|
|
|
wireframe: opts.wireframe,
|
|
|
|
});
|
|
|
|
}
|
2016-08-31 20:14:16 +02:00
|
|
|
|
|
|
|
let useBreadboard = 0 < activeComponents.length || opts.forceBreadboard;
|
|
|
|
if (useBreadboard) {
|
|
|
|
this.breadboard = new Breadboard({
|
|
|
|
wireframe: opts.wireframe,
|
|
|
|
});
|
2016-09-01 03:03:34 +02:00
|
|
|
let bMarg = opts.boardDef.marginWhenBreadboarding || [0, 0, 40, 0];
|
2016-08-30 23:13:44 +02:00
|
|
|
let composition = composeSVG({
|
|
|
|
el1: this.boardView.getView(),
|
|
|
|
scaleUnit1: this.boardView.getPinDist(),
|
|
|
|
el2: this.breadboard.getSVGAndSize(),
|
|
|
|
scaleUnit2: this.breadboard.getPinDist(),
|
2016-09-01 03:03:34 +02:00
|
|
|
margin: [bMarg[0], bMarg[1], 20, bMarg[3]],
|
|
|
|
middleMargin: bMarg[2],
|
2016-08-31 20:34:49 +02:00
|
|
|
maxWidth: opts.maxWidth,
|
|
|
|
maxHeight: opts.maxHeight,
|
2016-08-30 23:13:44 +02:00
|
|
|
});
|
|
|
|
let under = composition.under;
|
|
|
|
let over = composition.over;
|
|
|
|
this.view = composition.host;
|
|
|
|
let edges = composition.edges;
|
|
|
|
this.fromMBCoord = composition.toHostCoord1;
|
|
|
|
this.fromBBCoord = composition.toHostCoord2;
|
|
|
|
let pinDist = composition.scaleUnit;
|
|
|
|
|
|
|
|
this.style = <SVGStyleElement>svg.child(this.view, "style", {});
|
|
|
|
this.defs = <SVGDefsElement>svg.child(this.view, "defs", {});
|
|
|
|
|
|
|
|
this.wireFactory = new WireFactory(under, over, edges, this.style, this.getLocCoord.bind(this));
|
|
|
|
|
|
|
|
let allocRes = allocateDefinitions({
|
2016-08-31 20:14:16 +02:00
|
|
|
boardDef: opts.boardDef,
|
|
|
|
cmpDefs: opts.cmpDefs,
|
|
|
|
fnArgs: opts.fnArgs,
|
2016-08-30 23:13:44 +02:00
|
|
|
getBBCoord: this.breadboard.getCoord.bind(this.breadboard),
|
|
|
|
cmpList: activeComponents,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.addAll(allocRes);
|
|
|
|
} else {
|
|
|
|
let el = this.boardView.getView().el;
|
|
|
|
this.view = el;
|
2016-08-31 20:34:49 +02:00
|
|
|
if (opts.maxWidth)
|
|
|
|
svg.hydrate(this.view, { width: opts.maxWidth });
|
|
|
|
if (opts.maxHeight)
|
|
|
|
svg.hydrate(this.view, { height: opts.maxHeight });
|
2016-08-30 23:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.state.updateSubscribers.push(() => this.updateState());
|
|
|
|
}
|
|
|
|
|
2016-08-31 20:14:16 +02:00
|
|
|
public highlightBoardPin(pinNm: string) {
|
|
|
|
this.boardView.highlightPin(pinNm);
|
|
|
|
}
|
|
|
|
|
|
|
|
public highlightBreadboardPin(rowCol: BBRowCol) {
|
|
|
|
this.breadboard.highlightLoc(rowCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
public highlightWire(wire: Wire) {
|
2016-08-31 22:59:20 +02:00
|
|
|
//TODO: move to wiring.ts
|
2016-08-31 20:14:16 +02:00
|
|
|
//underboard wires
|
|
|
|
wire.wires.forEach(e => {
|
2016-08-31 22:59:20 +02:00
|
|
|
svg.addClass(e, "highlight");
|
2016-08-31 20:14:16 +02:00
|
|
|
(<any>e).style["visibility"] = "visible";
|
|
|
|
});
|
|
|
|
|
|
|
|
//un greyed out
|
2016-08-31 22:59:20 +02:00
|
|
|
svg.addClass(wire.endG, "highlight");
|
2016-08-31 20:14:16 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:13:44 +02:00
|
|
|
public getView(): SVGElement {
|
|
|
|
return this.view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateState() {
|
|
|
|
this.components.forEach(c => c.updateState());
|
|
|
|
}
|
|
|
|
|
|
|
|
private getBBCoord(rowCol: BBRowCol) {
|
|
|
|
let bbCoord = this.breadboard.getCoord(rowCol);
|
|
|
|
return this.fromBBCoord(bbCoord);
|
|
|
|
}
|
|
|
|
private getPinCoord(pin: string) {
|
|
|
|
let boardCoord = this.boardView.getCoord(pin);
|
|
|
|
return this.fromMBCoord(boardCoord);
|
|
|
|
}
|
|
|
|
public getLocCoord(loc: Loc): Coord {
|
|
|
|
let coord: Coord;
|
|
|
|
if (loc.type === "breadboard") {
|
|
|
|
let rowCol = (<BBLoc>loc).rowCol;
|
|
|
|
coord = this.getBBCoord(rowCol);
|
|
|
|
} else {
|
|
|
|
let pinNm = (<BoardLoc>loc).pin;
|
|
|
|
coord = this.getPinCoord(pinNm);
|
|
|
|
}
|
|
|
|
if (!coord) {
|
|
|
|
console.error("Unknown location: " + name)
|
|
|
|
return [0, 0];
|
|
|
|
}
|
|
|
|
return coord;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addComponent(cmpDesc: CmpInst): IBoardComponent<any> {
|
|
|
|
let cmp: IBoardComponent<any> = null;
|
2016-09-01 04:28:28 +02:00
|
|
|
let colOffset = 0;
|
2016-08-30 23:13:44 +02:00
|
|
|
if (typeof cmpDesc.visual === "string") {
|
|
|
|
let builtinVisual = cmpDesc.visual as string;
|
|
|
|
let cnstr = builtinComponentSimVisual[builtinVisual];
|
|
|
|
let stateFn = builtinComponentSimState[builtinVisual];
|
2016-08-31 20:14:16 +02:00
|
|
|
cmp = cnstr();
|
2016-08-30 23:13:44 +02:00
|
|
|
cmp.init(this.state.bus, stateFn(this.state), this.view, cmpDesc.microbitPins, cmpDesc.otherArgs);
|
|
|
|
} else {
|
2016-08-31 23:48:44 +02:00
|
|
|
let vis = cmpDesc.visual as PartVisualDefinition;
|
2016-09-01 04:01:59 +02:00
|
|
|
cmp = new GenericPart(vis);
|
2016-09-01 04:28:28 +02:00
|
|
|
colOffset = vis.extraColumnOffset || 0;
|
2016-08-30 23:13:44 +02:00
|
|
|
}
|
2016-09-01 04:01:59 +02:00
|
|
|
this.components.push(cmp);
|
|
|
|
this.view.appendChild(cmp.element);
|
|
|
|
if (cmp.defs)
|
|
|
|
cmp.defs.forEach(d => this.defs.appendChild(d));
|
|
|
|
this.style.textContent += cmp.style || "";
|
2016-09-01 04:28:28 +02:00
|
|
|
let rowCol = <BBRowCol>[`${cmpDesc.breadboardStartRow}`, `${colOffset + cmpDesc.breadboardStartColumn}`];
|
2016-09-01 04:01:59 +02:00
|
|
|
let coord = this.getBBCoord(rowCol);
|
|
|
|
cmp.moveToCoord(coord);
|
|
|
|
let getCmpClass = (type: string) => `sim-${type}-cmp`;
|
|
|
|
let cls = getCmpClass(name);
|
|
|
|
svg.addClass(cmp.element, cls);
|
|
|
|
svg.addClass(cmp.element, "sim-cmp");
|
|
|
|
cmp.updateTheme();
|
|
|
|
cmp.updateState();
|
2016-08-30 23:13:44 +02:00
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
public addWire(inst: WireInst): Wire {
|
2016-09-01 03:03:34 +02:00
|
|
|
return this.wireFactory.addWire(inst.start, inst.end, inst.color, this.useCrocClips);
|
2016-08-30 23:13:44 +02:00
|
|
|
}
|
|
|
|
public addAll(basicWiresAndCmpsAndWires: AllocatorResult) {
|
|
|
|
let {powerWires, components} = basicWiresAndCmpsAndWires;
|
|
|
|
powerWires.forEach(w => this.addWire(w));
|
|
|
|
components.forEach((cAndWs, idx) => {
|
|
|
|
let {component, wires} = cAndWs;
|
|
|
|
wires.forEach(w => this.addWire(w));
|
|
|
|
this.addComponent(component);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|