1 package net.mograsim.logic.ui.model.components;
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Iterator;
8 import java.util.Map.Entry;
10 import net.haspamelodica.swt.helper.gcs.GCConfig;
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
13 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
15 import net.mograsim.logic.ui.LogicUIRenderer;
16 import net.mograsim.logic.ui.model.ViewModel;
17 import net.mograsim.logic.ui.model.ViewModelModifiable;
18 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
19 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
20 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerPinParams;
21 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
22 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
23 import net.mograsim.logic.ui.model.wires.GUIWire;
24 import net.mograsim.logic.ui.model.wires.MovablePin;
25 import net.mograsim.logic.ui.model.wires.Pin;
27 public abstract class SubmodelComponent extends GUIComponent
29 protected final ViewModelModifiable submodelModifiable;
30 public final ViewModel submodel;
31 private final Map<String, MovablePin> submodelPins;
32 private final Map<String, MovablePin> submodelMovablePinsUnmodifiable;
33 private final Map<String, Pin> submodelUnmovablePinsUnmodifiable;
34 private final Map<String, MovablePin> supermodelPins;
35 private final Map<String, MovablePin> supermodelMovablePinsUnmodifiable;
36 private final Map<String, Pin> supermodelUnmovablePinsUnmodifiable;
37 private final SubmodelInterface submodelInterface;
39 private double submodelScale;
40 private double maxVisibleRegionFillRatioForAlpha0;
41 private double minVisibleRegionFillRatioForAlpha1;
42 private final LogicUIRenderer renderer;
44 public SubmodelComponent(ViewModelModifiable model)
47 this.submodelModifiable = new ViewModelModifiable();
48 this.submodel = submodelModifiable;
49 this.submodelPins = new HashMap<>();
50 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
51 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
52 this.supermodelPins = new HashMap<>();
53 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
54 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
55 this.submodelInterface = new SubmodelInterface(submodelModifiable);
57 this.submodelScale = 1;
58 this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
59 this.minVisibleRegionFillRatioForAlpha1 = 0.8;
60 this.renderer = new LogicUIRenderer(submodelModifiable);
62 submodelModifiable.addRedrawListener(this::requestRedraw);
65 protected void setSubmodelScale(double submodelScale)
67 this.submodelScale = submodelScale;
69 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
70 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
72 requestRedraw();// needed if there is no submodel interface pin
75 protected double getSubmodelScale()
81 * Returns the submodel pin.
83 protected Pin addSubmodelInterface(String name, int logicWidth, double relX, double relY)
85 MovablePin submodelPin = new MovablePin(submodelInterface, name, logicWidth, relX / submodelScale, relY / submodelScale);
86 MovablePin supermodelPin = new MovablePin(this, name, logicWidth, relX, relY);
88 submodelPin.addPinMovedListener(p ->
90 double newRelX = p.getRelX() * submodelScale;
91 double newRelY = p.getRelY() * submodelScale;
92 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
93 supermodelPin.setRelPos(newRelX, newRelY);
95 supermodelPin.addPinMovedListener(p ->
97 double newRelX = p.getRelX() / submodelScale;
98 double newRelY = p.getRelY() / submodelScale;
99 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
100 submodelPin.setRelPos(newRelX, newRelY);
103 submodelInterface.addPin(submodelPin);
104 super.addPin(supermodelPin);
106 submodelPins.put(name, submodelPin);
107 supermodelPins.put(name, supermodelPin);
109 // no need to call requestRedraw() because addPin() will request a redraw
113 protected void removeSubmodelInterface(String name)
115 super.removePin(name);
116 Pin submodelPin = getSubmodelMovablePin(name);
117 submodelInterface.removePin(submodelPin.name);
119 submodelPins.remove(name);
120 supermodelPins.remove(name);
122 // no need to call requestRedraw() because removePin() will request a redraw
125 public Map<String, Pin> getSubmodelPins()
127 return submodelUnmovablePinsUnmodifiable;
130 public Pin getSubmodelPin(String name)
132 return getSubmodelMovablePin(name);
135 protected Map<String, MovablePin> getSubmodelMovablePins()
137 return submodelMovablePinsUnmodifiable;
140 protected MovablePin getSubmodelMovablePin(String name)
142 return submodelPins.get(name);
145 public Map<String, Pin> getSupermodelPins()
147 return supermodelUnmovablePinsUnmodifiable;
150 public Pin getSupermodelPin(String name)
152 return getSupermodelMovablePin(name);
155 protected Map<String, MovablePin> getSupermodelMovablePins()
157 return supermodelMovablePinsUnmodifiable;
160 protected MovablePin getSupermodelMovablePin(String name)
162 return supermodelPins.get(name);
166 public void render(GeneralGC gc, Rectangle visibleRegion)
168 double posX = getBounds().x;
169 double posY = getBounds().y;
171 GCConfig conf = new GCConfig(gc);
172 TranslatedGC tgc = new TranslatedGC(gc, posX, posY, submodelScale, true);
174 double visibleRegionFillRatio = Math.max(getBounds().width / visibleRegion.width, getBounds().height / visibleRegion.height);
175 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
176 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
177 // we need to take the old alpha into account to support nested submodules better.
178 int oldAlpha = gc.getAlpha();
179 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
180 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
181 if (submodelAlpha != 0)
183 gc.setAlpha(submodelAlpha);
184 renderer.render(tgc, visibleRegion.translate(posX / submodelScale, posY / submodelScale, 1 / submodelScale));
188 gc.setAlpha(labelAlpha);
189 renderSymbol(gc, visibleRegion);
192 // draw the outline after all other operations to make interface pins look better
193 renderOutline(gc, visibleRegion);
196 protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion);
198 protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion);
200 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
202 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
206 public boolean clicked(double x, double y)
209 double scaledX = (x - getBounds().x) / submodelScale;
210 double scaledY = (y - getBounds().y) / submodelScale;
211 double roundedScaledX = Math.round(scaledX / 5 * 2) * 5 / 2.;
212 double roundedScaledY = Math.round(scaledY / 5 * 2) * 5 / 2.;
213 System.out.println(scaledX + "|" + scaledY + ", rounded " + roundedScaledX + "|" + roundedScaledY);
218 * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
220 public SubmodelComponentParams calculateParams()
222 SubmodelComponentParams params = new SubmodelComponentParams();
223 params.type = SubmodelComponent.class.getSimpleName();
224 params.composition = calculateCompositionParams();
226 Rectangle bounds = getBounds();
227 params.width = bounds.width;
228 params.height = bounds.height;
230 InterfacePinParams[] iPins = new InterfacePinParams[getPins().size()];
232 for (Pin p : getPins())
234 InterfacePinParams iPinParams = new InterfacePinParams();
235 iPins[i] = iPinParams;
236 iPinParams.location = p.getRelPos();
237 iPinParams.name = p.name;
238 iPinParams.logicWidth = p.logicWidth;
241 params.interfacePins = iPins;
245 protected ComponentCompositionParams calculateCompositionParams()
247 ComponentCompositionParams params = new ComponentCompositionParams();
248 params.innerScale = getSubmodelScale();
250 List<GUIComponent> compList = submodelModifiable.getComponents();
251 Iterator<GUIComponent> componentIt = compList.iterator();
252 componentIt.next(); // Skip inner SubmodelInterface
253 InnerComponentParams[] comps = new InnerComponentParams[compList.size() - 1];
255 while (componentIt.hasNext())
257 GUIComponent component = componentIt.next();
258 InnerComponentParams inner = new InnerComponentParams();
260 inner.logicWidth = component.getPins().iterator().next().logicWidth; // TODO This could be done a little more elegantly
261 Rectangle bounds = component.getBounds();
262 inner.pos = new Point(bounds.x, bounds.y);
263 inner.type = component.getIdentifier();
266 params.subComps = comps;
268 List<GUIWire> wireList = submodelModifiable.getWires();
269 InnerWireParams wires[] = new InnerWireParams[wireList.size()];
271 for (GUIWire wire : wireList)
273 InnerWireParams inner = new InnerWireParams();
275 InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams();
277 pin1Params.pinName = wire.getPin1().name;
278 pin1Params.compId = compList.indexOf(wire.getPin1().component);
279 pin2Params.pinName = wire.getPin2().name;
280 pin2Params.compId = compList.indexOf(wire.getPin2().component);
281 inner.pin1 = pin1Params;
282 inner.pin2 = pin2Params;
283 inner.path = wire.getPath();
286 params.innerWires = wires;
291 protected void addPin(Pin pin)
293 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
297 protected void removePin(String name)
299 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");