1 package net.mograsim.logic.model.model.components.submodels;
3 import java.util.Collections;
4 import java.util.HashMap;
6 import java.util.Map.Entry;
7 import java.util.function.Consumer;
9 import net.haspamelodica.swt.helper.gcs.GCConfig;
10 import net.haspamelodica.swt.helper.gcs.GeneralGC;
11 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.model.LogicUIRenderer;
14 import net.mograsim.logic.model.model.LogicModel;
15 import net.mograsim.logic.model.model.LogicModelModifiable;
16 import net.mograsim.logic.model.model.components.ModelComponent;
17 import net.mograsim.logic.model.model.wires.MovablePin;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.serializing.IdentifyParams;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.logic.model.serializing.SubmodelComponentParams;
23 import net.mograsim.logic.model.serializing.SubmodelComponentSerializer;
24 import net.mograsim.logic.model.snippets.Renderer;
25 import net.mograsim.logic.model.util.JsonHandler;
26 import net.mograsim.preferences.Preferences;
29 * A {@link ModelComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
30 * inner and outer models.
32 public abstract class SubmodelComponent extends ModelComponent
34 public static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
36 * A modifiable view of {@link #submodel}.
38 protected final LogicModelModifiable submodelModifiable;
40 * The model this {@link SubmodelComponent} consists of.
42 public final LogicModel submodel;
44 * The list of all submodel interface pins of this {@link SubmodelComponent} on the submodel side.
46 private final Map<String, MovablePin> submodelPins;
48 * An unmodifiable view of {@link #submodelPins}.
50 private final Map<String, MovablePin> submodelMovablePinsUnmodifiable;
52 * An unmodifiable view of {@link #submodelPins} where pins are not movable.
54 private final Map<String, Pin> submodelUnmovablePinsUnmodifiable;
56 * The list of all submodel interface pins of this {@link SubmodelComponent} on the supermodel side.
58 private final Map<String, MovablePin> supermodelPins;
60 * An unmodifiable view of {@link #supermodelPins}.
62 private final Map<String, MovablePin> supermodelMovablePinsUnmodifiable;
64 * An unmodifiable view of {@link #supermodelPins} where pins are not movable.
66 private final Map<String, Pin> supermodelUnmovablePinsUnmodifiable;
68 * A pseudo-component containing all submodel interface pins on the submodel side.
70 private final SubmodelInterface submodelInterface;
73 * The factor by which the submodel is scaled when rendering.
75 private double submodelScale;
77 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel starts to
80 private double maxVisibleRegionFillRatioForAlpha0;
82 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel is fully
85 private double minVisibleRegionFillRatioForAlpha1;
87 * The renderer used for rendering the submodel.
89 private final LogicUIRenderer renderer;
92 * The {@link Renderer} used to render the symbol of this SubmodelCoponent.
94 private Renderer symbolRenderer;
96 * The {@link Renderer} used to render the outline of this SubmodelCoponent.
98 private Renderer outlineRenderer;
100 // creation and destruction
102 public SubmodelComponent(LogicModelModifiable model, String name)
104 this(model, name, true);
107 protected SubmodelComponent(LogicModelModifiable model, String name, boolean callInit)
109 super(model, name, false);
110 this.submodelModifiable = new LogicModelModifiable();
111 this.submodel = submodelModifiable;
112 this.submodelPins = new HashMap<>();
113 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
114 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
115 this.supermodelPins = new HashMap<>();
116 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
117 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
118 this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
120 this.submodelScale = 1;
121 this.maxVisibleRegionFillRatioForAlpha0 = 0.8;
122 this.minVisibleRegionFillRatioForAlpha1 = 0.9;
123 this.renderer = new LogicUIRenderer(submodelModifiable);
125 Consumer<Runnable> redrawHandlerChangedListener = submodelModifiable::setRedrawHandler;
126 model.addRedrawHandlerChangedListener(redrawHandlerChangedListener);
127 model.addComponentRemovedListener(c ->
130 model.removeRedrawHandlerChangedListener(redrawHandlerChangedListener);
132 submodelModifiable.setRedrawHandler(model.getRedrawHandler());
141 * Adds a new submodel interface pin.
143 * @param supermodelPin the submodel interface pin on the supermodel side
145 * @return the submodel interface pin on the submodel side
147 * @author Daniel Kirschten
149 protected Pin addSubmodelInterface(MovablePin supermodelPin)
151 super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
153 String name = supermodelPin.name;
154 // TODO if we upgrade to Java 12, replace with switch-expression
155 PinUsage submodelPinUsage;
156 switch (supermodelPin.usage)
159 submodelPinUsage = PinUsage.OUTPUT;
162 submodelPinUsage = PinUsage.INPUT;
165 submodelPinUsage = PinUsage.TRISTATE;
168 throw new IllegalArgumentException("Unknown enum constant: " + supermodelPin.usage);
170 MovablePin submodelPin = new MovablePin(model, submodelInterface, name, supermodelPin.logicWidth, submodelPinUsage,
171 supermodelPin.getRelX() / submodelScale, supermodelPin.getRelY() / submodelScale);
173 submodelPin.addPinMovedListener(p ->
175 double newRelX = p.getRelX() * submodelScale;
176 double newRelY = p.getRelY() * submodelScale;
177 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
178 supermodelPin.setRelPos(newRelX, newRelY);
180 supermodelPin.addPinMovedListener(p ->
182 double newRelX = p.getRelX() / submodelScale;
183 double newRelY = p.getRelY() / submodelScale;
184 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
185 submodelPin.setRelPos(newRelX, newRelY);
188 submodelInterface.addPin(submodelPin);
190 submodelPins.put(name, submodelPin);
191 supermodelPins.put(name, supermodelPin);
193 // no need to call requestRedraw() because addPin() will request a redraw
198 * Removes a submodel interface pin.
200 * @author Daniel Kirschten
202 protected void removeSubmodelInterface(String name)
204 super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name
205 Pin submodelPin = submodelPins.remove(name);
206 submodelInterface.removePin(submodelPin.name);
207 supermodelPins.remove(name);
209 // no need to call requestRedraw() because removePin() will request a redraw
213 * Returns a collection of submodel interface pins on the submodel side of this component.
215 * @author Daniel Kirschten
217 public Map<String, Pin> getSubmodelPins()
219 return submodelUnmovablePinsUnmodifiable;
223 * Returns the submodel interface pin with the given name on the submodel side of this component.
225 * @author Daniel Kirschten
227 public Pin getSubmodelPin(String name)
229 return getSubmodelMovablePin(name);
233 * Returns a collection of movable submodel interface pins on the submodel side of this component.
235 * @author Daniel Kirschten
237 protected Map<String, MovablePin> getSubmodelMovablePins()
239 return submodelMovablePinsUnmodifiable;
243 * Returns the movable submodel interface pin with the given name on the submodel side of this component.
245 * @author Daniel Kirschten
247 protected MovablePin getSubmodelMovablePin(String name)
249 return submodelPins.get(name);
253 * Returns a collection of submodel interface pins on the supermodel side of this component.
255 * @author Daniel Kirschten
257 public Map<String, Pin> getSupermodelPins()
259 return supermodelUnmovablePinsUnmodifiable;
263 * Returns the submodel interface pin with the given name on the supermodel side of this component.
265 * @author Daniel Kirschten
267 public Pin getSupermodelPin(String name)
269 return getSupermodelMovablePin(name);
273 * Returns a collection of movable submodel interface pins on the supermodel side of this component.
275 * @author Daniel Kirschten
277 protected Map<String, MovablePin> getSupermodelMovablePins()
279 return supermodelMovablePinsUnmodifiable;
283 * Returns the movable submodel interface pin with the given name on the supermodel side of this component.
285 * @author Daniel Kirschten
287 protected MovablePin getSupermodelMovablePin(String name)
289 return supermodelPins.get(name);
292 // "graphical" operations
295 * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
296 * stay at their position relative to the supermodel, which means they will move relative to the submodel.
298 * @author Daniel Kirschten
300 protected void setSubmodelScale(double submodelScale)
302 this.submodelScale = submodelScale;
304 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
305 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
307 model.requestRedraw();// needed if there is no submodel interface pin
311 * Returns the current factor by which the submodel is scaled when rendering.
313 * @author Daniel Kirschten
315 public double getSubmodelScale()
317 return submodelScale;
321 * @see #renderSymbol(GeneralGC, Rectangle)
323 * @author Daniel Kirschten
325 protected void setSymbolRenderer(Renderer symbolRenderer)
327 this.symbolRenderer = symbolRenderer;
328 model.requestRedraw();
332 * @see #renderSymbol(GeneralGC, Rectangle)
334 * @author Daniel Kirschten
336 public Renderer getSymbolRenderer()
338 return symbolRenderer;
342 * @see #renderOutline(GeneralGC, Rectangle)
344 * @author Daniel Kirschten
346 protected void setOutlineRenderer(Renderer outlineRenderer)
348 this.outlineRenderer = outlineRenderer;
349 model.requestRedraw();
353 * @see #renderOutline(GeneralGC, Rectangle)
355 * @author Daniel Kirschten
357 public Renderer getOutlineRenderer()
359 return outlineRenderer;
363 public boolean clicked(double x, double y)
365 double scaledX = (x - getPosX()) / submodelScale;
366 double scaledY = (y - getPosY()) / submodelScale;
367 for (ModelComponent component : submodel.getComponentsByName().values())
368 if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
374 public void render(GeneralGC gc, Rectangle visibleRegion)
376 GCConfig conf = new GCConfig(gc);
377 GeneralGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true);
379 double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height);
380 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
381 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
382 // we need to take the old alpha into account to support nested submodules better.
383 int oldAlpha = gc.getAlpha();
384 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
385 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
386 if (submodelAlpha != 0)
388 gc.setAlpha(submodelAlpha);
389 renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale));
393 gc.setAlpha(labelAlpha);
394 renderSymbol(gc, visibleRegion);
397 // reset line width explicitly to avoid rounding errors causing weird glitches
398 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
399 // draw the outline after all other operations to make interface pins look better
400 renderOutline(gc, visibleRegion);
404 * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
406 * @author Daniel Kirschten
408 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
410 if (symbolRenderer != null)
411 symbolRenderer.render(gc, visibleRegion);
415 * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
417 * @author Daniel Kirschten
419 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
421 if (outlineRenderer != null)
422 outlineRenderer.render(gc, visibleRegion);
425 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
427 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
433 public String getIDForSerializing(IdentifyParams idParams)
435 return "submodel";// TODO what ID?
439 public SubmodelComponentParams getParamsForSerializing(IdentifyParams idParams)
441 return SubmodelComponentSerializer.serialize(this, idParams);
444 // operations no longer supported
447 protected void addPin(Pin pin)
449 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
453 protected void removePin(String name)
455 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");
460 IndirectModelComponentCreator.setComponentSupplier(SubmodelComponent.class.getCanonicalName(),
461 (m, p, n) -> SubmodelComponentSerializer.deserialize(m, JsonHandler.fromJsonTree(p, SubmodelComponentParams.class), n));