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;
8 import net.haspamelodica.swt.helper.gcs.GCConfig;
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.gcs.TranslatedGC;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
12 import net.mograsim.logic.model.LogicUIRenderer;
13 import net.mograsim.logic.model.model.ViewModel;
14 import net.mograsim.logic.model.model.ViewModelModifiable;
15 import net.mograsim.logic.model.model.components.GUIComponent;
16 import net.mograsim.logic.model.model.wires.MovablePin;
17 import net.mograsim.logic.model.model.wires.Pin;
18 import net.mograsim.logic.model.snippets.Renderer;
21 * A {@link GUIComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
22 * inner and outer models.
24 //TODO override getParams
25 public abstract class SubmodelComponent extends GUIComponent
27 public static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
29 * A modifiable view of {@link #submodel}.
31 protected final ViewModelModifiable submodelModifiable;
33 * The model this {@link SubmodelComponent} consists of.
35 public final ViewModel submodel;
37 * The list of all submodel interface pins of this {@link SubmodelComponent} on the submodel side.
39 private final Map<String, MovablePin> submodelPins;
41 * An unmodifiable view of {@link #submodelPins}.
43 private final Map<String, MovablePin> submodelMovablePinsUnmodifiable;
45 * An unmodifiable view of {@link #submodelPins} where pins are not movable.
47 private final Map<String, Pin> submodelUnmovablePinsUnmodifiable;
49 * The list of all submodel interface pins of this {@link SubmodelComponent} on the supermodel side.
51 private final Map<String, MovablePin> supermodelPins;
53 * An unmodifiable view of {@link #supermodelPins}.
55 private final Map<String, MovablePin> supermodelMovablePinsUnmodifiable;
57 * An unmodifiable view of {@link #supermodelPins} where pins are not movable.
59 private final Map<String, Pin> supermodelUnmovablePinsUnmodifiable;
61 * A pseudo-component containing all submodel interface pins on the submodel side.
63 private final SubmodelInterface submodelInterface;
66 * The factor by which the submodel is scaled when rendering.
68 private double submodelScale;
70 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel starts to
73 private double maxVisibleRegionFillRatioForAlpha0;
75 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel is fully
78 private double minVisibleRegionFillRatioForAlpha1;
80 * The renderer used for rendering the submodel.
82 private final LogicUIRenderer renderer;
85 * The {@link Renderer} used to render the symbol of this SubmodelCoponent.
87 private Renderer symbolRenderer;
89 * The {@link Renderer} used to render the outline of this SubmodelCoponent.
91 private Renderer outlineRenderer;
93 // creation and destruction
95 public SubmodelComponent(ViewModelModifiable model, String name)
98 this.submodelModifiable = new ViewModelModifiable();
99 this.submodel = submodelModifiable;
100 this.submodelPins = new HashMap<>();
101 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
102 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
103 this.supermodelPins = new HashMap<>();
104 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
105 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
106 this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
108 this.submodelScale = 1;
109 this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
110 this.minVisibleRegionFillRatioForAlpha1 = 0.8;
111 this.renderer = new LogicUIRenderer(submodelModifiable);
113 submodelModifiable.setRedrawHandler(model.getRedrawHandler());
119 * Adds a new submodel interface pin.
121 * @param supermodelPin the submodel interface pin on the supermodel side
123 * @return the submodel interface pin on the submodel side
125 * @author Daniel Kirschten
127 protected Pin addSubmodelInterface(MovablePin supermodelPin)
129 super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
131 String name = supermodelPin.name;
132 MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, supermodelPin.getRelX() / submodelScale,
133 supermodelPin.getRelY() / submodelScale);
135 submodelPin.addPinMovedListener(p ->
137 double newRelX = p.getRelX() * submodelScale;
138 double newRelY = p.getRelY() * submodelScale;
139 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
140 supermodelPin.setRelPos(newRelX, newRelY);
142 supermodelPin.addPinMovedListener(p ->
144 double newRelX = p.getRelX() / submodelScale;
145 double newRelY = p.getRelY() / submodelScale;
146 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
147 submodelPin.setRelPos(newRelX, newRelY);
150 submodelInterface.addPin(submodelPin);
152 submodelPins.put(name, submodelPin);
153 supermodelPins.put(name, supermodelPin);
155 // no need to call requestRedraw() because addPin() will request a redraw
160 * Removes a submodel interface pin.
162 * @author Daniel Kirschten
164 protected void removeSubmodelInterface(String name)
166 super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name
167 Pin submodelPin = submodelPins.remove(name);
168 submodelInterface.removePin(submodelPin.name);
169 supermodelPins.remove(name);
171 // no need to call requestRedraw() because removePin() will request a redraw
175 * Returns a collection of submodel interface pins on the submodel side of this component.
177 * @author Daniel Kirschten
179 public Map<String, Pin> getSubmodelPins()
181 return submodelUnmovablePinsUnmodifiable;
185 * Returns the submodel interface pin with the given name on the submodel side of this component.
187 * @author Daniel Kirschten
189 public Pin getSubmodelPin(String name)
191 return getSubmodelMovablePin(name);
195 * Returns a collection of movable submodel interface pins on the submodel side of this component.
197 * @author Daniel Kirschten
199 protected Map<String, MovablePin> getSubmodelMovablePins()
201 return submodelMovablePinsUnmodifiable;
205 * Returns the movable submodel interface pin with the given name on the submodel side of this component.
207 * @author Daniel Kirschten
209 protected MovablePin getSubmodelMovablePin(String name)
211 return submodelPins.get(name);
215 * Returns a collection of submodel interface pins on the supermodel side of this component.
217 * @author Daniel Kirschten
219 public Map<String, Pin> getSupermodelPins()
221 return supermodelUnmovablePinsUnmodifiable;
225 * Returns the submodel interface pin with the given name on the supermodel side of this component.
227 * @author Daniel Kirschten
229 public Pin getSupermodelPin(String name)
231 return getSupermodelMovablePin(name);
235 * Returns a collection of movable submodel interface pins on the supermodel side of this component.
237 * @author Daniel Kirschten
239 protected Map<String, MovablePin> getSupermodelMovablePins()
241 return supermodelMovablePinsUnmodifiable;
245 * Returns the movable submodel interface pin with the given name on the supermodel side of this component.
247 * @author Daniel Kirschten
249 protected MovablePin getSupermodelMovablePin(String name)
251 return supermodelPins.get(name);
254 // "graphical" operations
257 * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
258 * stay at their position relative to the supermodel, which means they will move relative to the submodel.
260 * @author Daniel Kirschten
262 protected void setSubmodelScale(double submodelScale)
264 this.submodelScale = submodelScale;
266 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
267 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
269 model.requestRedraw();// needed if there is no submodel interface pin
273 * Returns the current factor by which the submodel is scaled when rendering.
275 * @author Daniel Kirschten
277 public double getSubmodelScale()
279 return submodelScale;
283 * @see #renderSymbol(GeneralGC, Rectangle)
285 * @author Daniel Kirschten
287 protected void setSymbolRenderer(Renderer symbolRenderer)
289 this.symbolRenderer = symbolRenderer;
290 model.requestRedraw();
294 * @see #renderSymbol(GeneralGC, Rectangle)
296 * @author Daniel Kirschten
298 public Renderer getSymbolRenderer()
300 return symbolRenderer;
304 * @see #renderOutline(GeneralGC, Rectangle)
306 * @author Daniel Kirschten
308 protected void setOutlineRenderer(Renderer outlineRenderer)
310 this.outlineRenderer = outlineRenderer;
311 model.requestRedraw();
315 * @see #renderOutline(GeneralGC, Rectangle)
317 * @author Daniel Kirschten
319 public Renderer getOutlineRenderer()
321 return outlineRenderer;
325 public void render(GeneralGC gc, Rectangle visibleRegion)
327 GCConfig conf = new GCConfig(gc);
328 TranslatedGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true);
330 double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height);
331 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
332 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
333 // we need to take the old alpha into account to support nested submodules better.
334 int oldAlpha = gc.getAlpha();
335 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
336 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
337 if (submodelAlpha != 0)
339 gc.setAlpha(submodelAlpha);
340 renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale));
344 gc.setAlpha(labelAlpha);
345 renderSymbol(gc, visibleRegion);
348 // draw the outline after all other operations to make interface pins look better
349 renderOutline(gc, visibleRegion);
353 * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
355 * @author Daniel Kirschten
357 protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
359 if (symbolRenderer != null)
360 symbolRenderer.render(gc, visibleRegion);
364 * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
366 * @author Daniel Kirschten
368 protected void renderOutline(GeneralGC gc, Rectangle visibleRegion)
370 if (outlineRenderer != null)
371 outlineRenderer.render(gc, visibleRegion);
374 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
376 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
380 public boolean clicked(double x, double y)
382 double scaledX = (x - getPosX()) / submodelScale;
383 double scaledY = (y - getPosY()) / submodelScale;
384 for (GUIComponent component : submodel.getComponentsByName().values())
385 if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
390 // operations no longer supported
393 protected void addPin(Pin pin)
395 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
399 protected void removePin(String name)
401 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");