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.ViewModel;
15 import net.mograsim.logic.model.model.ViewModelModifiable;
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 ViewModelModifiable submodelModifiable;
40 * The model this {@link SubmodelComponent} consists of.
42 public final ViewModel 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(ViewModelModifiable model, String name)
105 this.submodelModifiable = new ViewModelModifiable();
106 this.submodel = submodelModifiable;
107 this.submodelPins = new HashMap<>();
108 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
109 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
110 this.supermodelPins = new HashMap<>();
111 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
112 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
113 this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
115 this.submodelScale = 1;
116 this.maxVisibleRegionFillRatioForAlpha0 = 0.8;
117 this.minVisibleRegionFillRatioForAlpha1 = 0.9;
118 this.renderer = new LogicUIRenderer(submodelModifiable);
120 Consumer<Runnable> redrawHandlerChangedListener = submodelModifiable::setRedrawHandler;
121 model.addRedrawHandlerChangedListener(redrawHandlerChangedListener);
122 model.addComponentRemovedListener(c ->
125 model.removeRedrawHandlerChangedListener(redrawHandlerChangedListener);
127 submodelModifiable.setRedrawHandler(model.getRedrawHandler());
133 * Adds a new submodel interface pin.
135 * @param supermodelPin the submodel interface pin on the supermodel side
137 * @return the submodel interface pin on the submodel side
139 * @author Daniel Kirschten
141 protected Pin addSubmodelInterface(MovablePin supermodelPin)
143 super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
145 String name = supermodelPin.name;
146 // TODO if we upgrade to Java 12, replace with switch-expression
147 PinUsage submodelPinUsage;
148 switch (supermodelPin.usage)
151 submodelPinUsage = PinUsage.OUTPUT;
154 submodelPinUsage = PinUsage.INPUT;
157 submodelPinUsage = PinUsage.TRISTATE;
160 throw new IllegalArgumentException("Unknown enum constant: " + supermodelPin.usage);
162 MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, submodelPinUsage,
163 supermodelPin.getRelX() / submodelScale, supermodelPin.getRelY() / submodelScale);
165 submodelPin.addPinMovedListener(p ->
167 double newRelX = p.getRelX() * submodelScale;
168 double newRelY = p.getRelY() * submodelScale;
169 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
170 supermodelPin.setRelPos(newRelX, newRelY);
172 supermodelPin.addPinMovedListener(p ->
174 double newRelX = p.getRelX() / submodelScale;
175 double newRelY = p.getRelY() / submodelScale;
176 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
177 submodelPin.setRelPos(newRelX, newRelY);
180 submodelInterface.addPin(submodelPin);
182 submodelPins.put(name, submodelPin);
183 supermodelPins.put(name, supermodelPin);
185 // no need to call requestRedraw() because addPin() will request a redraw
190 * Removes a submodel interface pin.
192 * @author Daniel Kirschten
194 protected void removeSubmodelInterface(String name)
196 super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name
197 Pin submodelPin = submodelPins.remove(name);
198 submodelInterface.removePin(submodelPin.name);
199 supermodelPins.remove(name);
201 // no need to call requestRedraw() because removePin() will request a redraw
205 * Returns a collection of submodel interface pins on the submodel side of this component.
207 * @author Daniel Kirschten
209 public Map<String, Pin> getSubmodelPins()
211 return submodelUnmovablePinsUnmodifiable;
215 * Returns the submodel interface pin with the given name on the submodel side of this component.
217 * @author Daniel Kirschten
219 public Pin getSubmodelPin(String name)
221 return getSubmodelMovablePin(name);
225 * Returns a collection of movable submodel interface pins on the submodel side of this component.
227 * @author Daniel Kirschten
229 protected Map<String, MovablePin> getSubmodelMovablePins()
231 return submodelMovablePinsUnmodifiable;
235 * Returns the movable submodel interface pin with the given name on the submodel side of this component.
237 * @author Daniel Kirschten
239 protected MovablePin getSubmodelMovablePin(String name)
241 return submodelPins.get(name);
245 * Returns a collection of submodel interface pins on the supermodel side of this component.
247 * @author Daniel Kirschten
249 public Map<String, Pin> getSupermodelPins()
251 return supermodelUnmovablePinsUnmodifiable;
255 * Returns the submodel interface pin with the given name on the supermodel side of this component.
257 * @author Daniel Kirschten
259 public Pin getSupermodelPin(String name)
261 return getSupermodelMovablePin(name);
265 * Returns a collection of movable submodel interface pins on the supermodel side of this component.
267 * @author Daniel Kirschten
269 protected Map<String, MovablePin> getSupermodelMovablePins()
271 return supermodelMovablePinsUnmodifiable;
275 * Returns the movable submodel interface pin with the given name on the supermodel side of this component.
277 * @author Daniel Kirschten
279 protected MovablePin getSupermodelMovablePin(String name)
281 return supermodelPins.get(name);
284 // "graphical" operations
287 * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
288 * stay at their position relative to the supermodel, which means they will move relative to the submodel.
290 * @author Daniel Kirschten
292 protected void setSubmodelScale(double submodelScale)
294 this.submodelScale = submodelScale;
296 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
297 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
299 model.requestRedraw();// needed if there is no submodel interface pin
303 * Returns the current factor by which the submodel is scaled when rendering.
305 * @author Daniel Kirschten
307 public double getSubmodelScale()
309 return submodelScale;
313 * @see #renderSymbol(GeneralGC, Rectangle)
315 * @author Daniel Kirschten
317 protected void setSymbolRenderer(Renderer symbolRenderer)
319 this.symbolRenderer = symbolRenderer;
320 model.requestRedraw();
324 * @see #renderSymbol(GeneralGC, Rectangle)
326 * @author Daniel Kirschten
328 public Renderer getSymbolRenderer()
330 return symbolRenderer;
334 * @see #renderOutline(GeneralGC, Rectangle)
336 * @author Daniel Kirschten
338 protected void setOutlineRenderer(Renderer outlineRenderer)
340 this.outlineRenderer = outlineRenderer;
341 model.requestRedraw();
345 * @see #renderOutline(GeneralGC, Rectangle)
347 * @author Daniel Kirschten
349 public Renderer getOutlineRenderer()
351 return outlineRenderer;
355 public boolean clicked(double x, double y)
357 double scaledX = (x - getPosX()) / submodelScale;
358 double scaledY = (y - getPosY()) / submodelScale;
359 for (ModelComponent component : submodel.getComponentsByName().values())
360 if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
366 public void render(GeneralGC gc, Rectangle visibleRegion)
368 GCConfig conf = new GCConfig(gc);
369 GeneralGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true);
371 double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height);
372 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
373 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
374 // we need to take the old alpha into account to support nested submodules better.
375 int oldAlpha = gc.getAlpha();
376 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
377 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
378 if (submodelAlpha != 0)
380 gc.setAlpha(submodelAlpha);
381 renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale));
385 gc.setAlpha(labelAlpha);
386 renderSymbol(gc, visibleRegion);
389 // reset line width explicitly to avoid rounding errors causing weird glitches
390 gc.setLineWidth(Preferences.current().getDouble("net.mograsim.logic.model.linewidth.default"));
391 // draw the outline after all other operations to make interface pins look better
392 renderOutline(gc, visibleRegion);
396 * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
398 * @author Daniel Kirschten
400 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
402 if (symbolRenderer != null)
403 symbolRenderer.render(gc, visibleRegion);
407 * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
409 * @author Daniel Kirschten
411 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
413 if (outlineRenderer != null)
414 outlineRenderer.render(gc, visibleRegion);
417 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
419 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
425 public String getIDForSerializing(IdentifyParams idParams)
427 return "submodel";// TODO what ID?
431 public SubmodelComponentParams getParamsForSerializing(IdentifyParams idParams)
433 return SubmodelComponentSerializer.serialize(this, idParams);
436 // operations no longer supported
439 protected void addPin(Pin pin)
441 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
445 protected void removePin(String name)
447 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");
452 IndirectModelComponentCreator.setComponentSupplier(SubmodelComponent.class.getCanonicalName(),
453 (m, p, n) -> SubmodelComponentSerializer.deserialize(m, JsonHandler.fromJsonTree(p, SubmodelComponentParams.class), n));