refactor part global lists

This commit is contained in:
Peli de Halleux 2016-09-13 09:44:58 -07:00
parent 1fa9bf12d5
commit 9db91d89d6
4 changed files with 16 additions and 13 deletions

View File

@ -10,8 +10,10 @@ namespace pxsim {
// updates // updates
updateSubscribers: (() => void)[]; updateSubscribers: (() => void)[];
// builtin state // builtin
builtinParts: Map<any>; builtinParts: Map<any>;
builtinVisuals: Map<() => visuals.IBoardPart<any>>;
builtinPartVisuals: Map<(xy: visuals.Coord) => visuals.SVGElAndSize>;
constructor() { constructor() {
super() super()
@ -25,6 +27,8 @@ namespace pxsim {
} }
this.builtinParts = {}; this.builtinParts = {};
this.builtinVisuals = {};
this.builtinPartVisuals = {};
} }
kill() { kill() {
@ -67,6 +71,14 @@ namespace pxsim {
this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState(); this.builtinParts["lightsensor"] = this.lightSensorState = new LightSensorState();
this.builtinParts["compass"] = this.compassState = new CompassState(); this.builtinParts["compass"] = this.compassState = new CompassState();
this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState(); this.builtinParts["neopixel"] = this.neopixelState = new NeoPixelState();
this.builtinVisuals["buttonpair"] = () => new visuals.ButtonPairView();
this.builtinVisuals["ledmatrix"] = () => new visuals.LedMatrixView();
this.builtinVisuals["neopixel"] = () => new visuals.NeoPixelView();
this.builtinPartVisuals["buttonpair"] = (xy: visuals.Coord) => visuals.mkBtnSvg(xy);
this.builtinPartVisuals["ledmatrix"] = (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8);
this.builtinPartVisuals["neopixel"] = (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy);
} }
receiveMessage(msg: SimulatorMessage) { receiveMessage(msg: SimulatorMessage) {

View File

@ -53,16 +53,6 @@ namespace pxsim {
marginWhenBreadboarding: [0, 0, 80, 0], marginWhenBreadboarding: [0, 0, 80, 0],
} }
export const builtinComponentSimVisual: Map<() => visuals.IBoardPart<any>> = {
"buttonpair": () => new visuals.ButtonPairView(),
"ledmatrix": () => new visuals.LedMatrixView(),
"neopixel": () => new visuals.NeoPixelView(),
};
export const builtinComponentPartVisual: Map<(xy: visuals.Coord) => visuals.SVGElAndSize> = {
"buttonpair": (xy: visuals.Coord) => visuals.mkBtnSvg(xy),
"ledmatrix": (xy: visuals.Coord) => visuals.mkLedMatrixSvg(xy, 8, 8),
"neopixel": (xy: visuals.Coord) => visuals.mkNeoPixelPart(xy),
};
//TODO: add multiple board support //TODO: add multiple board support
export const CURRENT_BOARD = MICROBIT_DEF; export const CURRENT_BOARD = MICROBIT_DEF;

View File

@ -282,13 +282,14 @@ namespace pxsim.instructions {
return div; return div;
} }
function mkCmpDiv(cmp: "wire" | PartVisualDefinition, opts: mkCmpDivOpts): HTMLElement { function mkCmpDiv(cmp: "wire" | PartVisualDefinition, opts: mkCmpDivOpts): HTMLElement {
let state = runtime.board as pxsim.CoreBoard;
let el: visuals.SVGElAndSize; let el: visuals.SVGElAndSize;
if (cmp == "wire") { if (cmp == "wire") {
el = visuals.mkWirePart([0, 0], opts.wireClr || "red", opts.crocClips); el = visuals.mkWirePart([0, 0], opts.wireClr || "red", opts.crocClips);
} else { } else {
let partVis = <PartVisualDefinition>cmp; let partVis = <PartVisualDefinition>cmp;
if (typeof partVis.builtIn == "string") { if (typeof partVis.builtIn == "string") {
let cnstr = builtinComponentPartVisual[partVis.builtIn]; let cnstr = state.builtinPartVisuals[partVis.builtIn];
el = cnstr([0, 0]); el = cnstr([0, 0]);
} else { } else {
el = visuals.mkGenericPartSVG(partVis); el = visuals.mkGenericPartSVG(partVis);

View File

@ -159,7 +159,7 @@ namespace pxsim.visuals {
if (partInst.simulationBehavior) { if (partInst.simulationBehavior) {
//TODO: seperate simulation behavior from builtin visual //TODO: seperate simulation behavior from builtin visual
let builtinBehavior = partInst.simulationBehavior; let builtinBehavior = partInst.simulationBehavior;
let cnstr = builtinComponentSimVisual[builtinBehavior]; let cnstr = this.state.builtinVisuals[builtinBehavior];
let stateFn = this.state.builtinParts[builtinBehavior]; let stateFn = this.state.builtinParts[builtinBehavior];
part = cnstr(); part = cnstr();
part.init(this.state.bus, stateFn, this.view, partInst.params); part.init(this.state.bus, stateFn, this.view, partInst.params);