X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fmodel%2Fcomponents%2FSubmodelComponent.java;h=9fa933bdb00630a3aa147678b109c0940985d908;hb=7aa6eab72deba89dbc22ae9c90ffb5df53f9d496;hp=5e5d18a6e17daaad27c100185d3ecc0510d34b57;hpb=c1a8e2103a1a79039163ad2e414c0650f8cee3ea;p=Mograsim.git diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java index 5e5d18a6..9fa933bd 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java @@ -2,25 +2,35 @@ package net.mograsim.logic.ui.model.components; import java.util.Collections; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import net.haspamelodica.swt.helper.gcs.GCConfig; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.gcs.TranslatedGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.ui.LogicUIRenderer; import net.mograsim.logic.ui.model.ViewModel; import net.mograsim.logic.ui.model.ViewModelModifiable; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerPinParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams; +import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams; +import net.mograsim.logic.ui.model.wires.GUIWire; +import net.mograsim.logic.ui.model.wires.MovablePin; import net.mograsim.logic.ui.model.wires.Pin; public abstract class SubmodelComponent extends GUIComponent { protected final ViewModelModifiable submodelModifiable; public final ViewModel submodel; - private final Map submodelPinsPerSupermodelPin; + private final Map submodelPinsPerSupermodelPin; private final Map submodelPinsPerSupermodelPinUnmodifiable; - private final Map supermodelPinsPerSubmodelPin; + private final Map supermodelPinsPerSubmodelPin; private final Map supermodelPinsPerSubmodelPinUnmodifiable; private final SubmodelInterface submodelInterface; @@ -52,21 +62,26 @@ public abstract class SubmodelComponent extends GUIComponent { this.submodelScale = submodelScale; - for (Entry e : supermodelPinsPerSubmodelPin.entrySet()) + for (Entry e : supermodelPinsPerSubmodelPin.entrySet()) e.getKey().setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale); requestRedraw();// needed if there is no submodel interface pin } + protected double getSubmodelScale() + { + return submodelScale; + } + /** * Returns the submodel pin. */ - protected Pin addSubmodelInterface(int logicWidth, double relX, double relY) + protected Pin addSubmodelInterface(String name, int logicWidth, double relX, double relY) { - PinMovable submodelPin = new PinMovable(submodelInterface, logicWidth, relX / submodelScale, relY / submodelScale); + MovablePin submodelPin = new MovablePin(submodelInterface, name, logicWidth, relX / submodelScale, relY / submodelScale); submodelInterface.addPin(submodelPin); - PinMovable supermodelPin = new PinMovable(this, logicWidth, relX, relY); + MovablePin supermodelPin = new MovablePin(this, name, logicWidth, relX, relY); addPin(supermodelPin); submodelPinsPerSupermodelPin.put(supermodelPin, submodelPin); @@ -78,8 +93,8 @@ public abstract class SubmodelComponent extends GUIComponent protected void moveSubmodelInterface(Pin supermodelPin, double relX, double relY) { - PinMovable submodelPin = getSubmodelMovablePin(supermodelPin); - PinMovable supermodelPinMovable = getSupermodelMovablePin(submodelPin); + MovablePin submodelPin = getSubmodelMovablePin(supermodelPin); + MovablePin supermodelPinMovable = getSupermodelMovablePin(submodelPin); submodelPin.setRelPos(relX / submodelScale, relY / submodelScale); supermodelPinMovable.setRelPos(relX, relY); @@ -109,7 +124,7 @@ public abstract class SubmodelComponent extends GUIComponent return getSupermodelMovablePin(submodelPin); } - protected PinMovable getSupermodelMovablePin(Pin submodelPin) + protected MovablePin getSupermodelMovablePin(Pin submodelPin) { return supermodelPinsPerSubmodelPin.get(submodelPin); } @@ -124,7 +139,7 @@ public abstract class SubmodelComponent extends GUIComponent return getSubmodelMovablePin(supermodelPin); } - protected PinMovable getSubmodelMovablePin(Pin supermodelPin) + protected MovablePin getSubmodelMovablePin(Pin supermodelPin) { return submodelPinsPerSupermodelPin.get(supermodelPin); } @@ -148,21 +163,21 @@ public abstract class SubmodelComponent extends GUIComponent if (submodelAlpha != 0) { gc.setAlpha(submodelAlpha); - renderer.render(tgc, visibleRegion.translate(-posX, -posY, 1 / submodelScale)); + renderer.render(tgc, visibleRegion.translate(posX / submodelScale, posY / submodelScale, 1 / submodelScale)); } if (labelAlpha != 0) { gc.setAlpha(labelAlpha); - renderSymbol(gc); + renderSymbol(gc, visibleRegion); } conf.reset(gc); // draw the outline after all other operations to make interface pins look better - renderOutline(gc); + renderOutline(gc, visibleRegion); } - protected abstract void renderOutline(GeneralGC gc); + protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion); - protected abstract void renderSymbol(GeneralGC gc); + protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion); private static double map(double val, double valMin, double valMax, double mapMin, double mapMax) { @@ -181,17 +196,77 @@ public abstract class SubmodelComponent extends GUIComponent return true; } - private static class PinMovable extends Pin + /** + * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}. + */ + public SubmodelComponentParams calculateParams() + { + SubmodelComponentParams params = new SubmodelComponentParams(); + params.type = SubmodelComponent.class.getSimpleName(); + params.composition = calculateCompositionParams(); + + Rectangle bounds = getBounds(); + params.width = bounds.width; + params.height = bounds.height; + + List pinList = pinsUnmodifiable; + InterfacePinParams[] iPins = new InterfacePinParams[pinList.size()]; + int i = 0; + for (Pin p : pinList) + { + InterfacePinParams iPinParams = new InterfacePinParams(); + iPins[i] = iPinParams; + iPinParams.location = p.getRelPos(); + iPinParams.name = p.name; + iPinParams.logicWidth = p.logicWidth; + i++; + } + params.interfacePins = iPins; + return params; + } + + protected ComponentCompositionParams calculateCompositionParams() { - public PinMovable(GUIComponent component, int logicWidth, double relX, double relY) + ComponentCompositionParams params = new ComponentCompositionParams(); + params.innerScale = getSubmodelScale(); + + List compList = submodelModifiable.getComponents(); + Iterator componentIt = compList.iterator(); + componentIt.next(); // Skip inner SubmodelInterface + InnerComponentParams[] comps = new InnerComponentParams[compList.size() - 1]; + int i = 0; + while (componentIt.hasNext()) { - super(component, logicWidth, relX, relY); + GUIComponent component = componentIt.next(); + InnerComponentParams inner = new InnerComponentParams(); + comps[i] = inner; + inner.logicWidth = component.getPins().get(0).logicWidth; // This could be done a little more elegantly + Rectangle bounds = component.getBounds(); + inner.pos = new Point(bounds.x, bounds.y); + inner.type = component.getIdentifier(); + i++; } + params.subComps = comps; - @Override - protected void setRelPos(double relX, double relY) + List wireList = submodelModifiable.getWires(); + InnerWireParams wires[] = new InnerWireParams[wireList.size()]; + i = 0; + for (GUIWire wire : wireList) { - super.setRelPos(relX, relY); + InnerWireParams inner = new InnerWireParams(); + wires[i] = inner; + InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams(); + + pin1Params.pinIndex = wire.getPin1().component.getPins().indexOf(wire.getPin1()); + pin1Params.compId = compList.indexOf(wire.getPin1().component); + pin2Params.pinIndex = wire.getPin2().component.getPins().indexOf(wire.getPin2()); + pin2Params.compId = compList.indexOf(wire.getPin2().component); + inner.pin1 = pin1Params; + inner.pin2 = pin2Params; + inner.path = wire.getPath(); + i++; } + params.innerWires = wires; + return params; } } \ No newline at end of file