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;
20 * A {@link GUIComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
21 * inner and outer models.
23 //TODO override getParams
24 public abstract class SubmodelComponent extends GUIComponent
26 public static final String SUBMODEL_INTERFACE_NAME = "_submodelinterface";
28 * A modifiable view of {@link #submodel}.
30 protected final ViewModelModifiable submodelModifiable;
32 * The model this {@link SubmodelComponent} consists of.
34 public final ViewModel submodel;
36 * The list of all submodel interface pins of this {@link SubmodelComponent} on the submodel side.
38 private final Map<String, MovablePin> submodelPins;
40 * An unmodifiable view of {@link #submodelPins}.
42 private final Map<String, MovablePin> submodelMovablePinsUnmodifiable;
44 * An unmodifiable view of {@link #submodelPins} where pins are not movable.
46 private final Map<String, Pin> submodelUnmovablePinsUnmodifiable;
48 * The list of all submodel interface pins of this {@link SubmodelComponent} on the supermodel side.
50 private final Map<String, MovablePin> supermodelPins;
52 * An unmodifiable view of {@link #supermodelPins}.
54 private final Map<String, MovablePin> supermodelMovablePinsUnmodifiable;
56 * An unmodifiable view of {@link #supermodelPins} where pins are not movable.
58 private final Map<String, Pin> supermodelUnmovablePinsUnmodifiable;
60 * A pseudo-component containing all submodel interface pins on the submodel side.
62 private final SubmodelInterface submodelInterface;
65 * The factor by which the submodel is scaled when rendering.
67 private double submodelScale;
69 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel starts to
72 private double maxVisibleRegionFillRatioForAlpha0;
74 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel is fully
77 private double minVisibleRegionFillRatioForAlpha1;
79 * The renderer used for rendering the submodel.
81 private final LogicUIRenderer renderer;
83 // creation and destruction
85 public SubmodelComponent(ViewModelModifiable model, String name)
88 this.submodelModifiable = new ViewModelModifiable();
89 this.submodel = submodelModifiable;
90 this.submodelPins = new HashMap<>();
91 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
92 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
93 this.supermodelPins = new HashMap<>();
94 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
95 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
96 this.submodelInterface = new SubmodelInterface(submodelModifiable, SUBMODEL_INTERFACE_NAME);
98 this.submodelScale = 1;
99 this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
100 this.minVisibleRegionFillRatioForAlpha1 = 0.8;
101 this.renderer = new LogicUIRenderer(submodelModifiable);
103 submodelModifiable.addRedrawListener(this::requestRedraw);
109 * Adds a new submodel interface pin.
111 * @param supermodelPin the submodel interface pin on the supermodel side
113 * @return the submodel interface pin on the submodel side
115 * @author Daniel Kirschten
117 protected Pin addSubmodelInterface(MovablePin supermodelPin)
119 super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
121 String name = supermodelPin.name;
122 MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, supermodelPin.getRelX() / submodelScale,
123 supermodelPin.getRelY() / submodelScale);
125 submodelPin.addPinMovedListener(p ->
127 double newRelX = p.getRelX() * submodelScale;
128 double newRelY = p.getRelY() * submodelScale;
129 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
130 supermodelPin.setRelPos(newRelX, newRelY);
132 supermodelPin.addPinMovedListener(p ->
134 double newRelX = p.getRelX() / submodelScale;
135 double newRelY = p.getRelY() / submodelScale;
136 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
137 submodelPin.setRelPos(newRelX, newRelY);
140 submodelInterface.addPin(submodelPin);
142 submodelPins.put(name, submodelPin);
143 supermodelPins.put(name, supermodelPin);
145 // no need to call requestRedraw() because addPin() will request a redraw
150 * Removes a submodel interface pin.
152 * @author Daniel Kirschten
154 protected void removeSubmodelInterface(String name)
156 super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name
157 Pin submodelPin = submodelPins.remove(name);
158 submodelInterface.removePin(submodelPin.name);
159 supermodelPins.remove(name);
161 // no need to call requestRedraw() because removePin() will request a redraw
165 * Returns a collection of submodel interface pins on the submodel side of this component.
167 * @author Daniel Kirschten
169 public Map<String, Pin> getSubmodelPins()
171 return submodelUnmovablePinsUnmodifiable;
175 * Returns the submodel interface pin with the given name on the submodel side of this component.
177 * @author Daniel Kirschten
179 public Pin getSubmodelPin(String name)
181 return getSubmodelMovablePin(name);
185 * Returns a collection of movable submodel interface pins on the submodel side of this component.
187 * @author Daniel Kirschten
189 protected Map<String, MovablePin> getSubmodelMovablePins()
191 return submodelMovablePinsUnmodifiable;
195 * Returns the movable submodel interface pin with the given name on the submodel side of this component.
197 * @author Daniel Kirschten
199 protected MovablePin getSubmodelMovablePin(String name)
201 return submodelPins.get(name);
205 * Returns a collection of submodel interface pins on the supermodel side of this component.
207 * @author Daniel Kirschten
209 public Map<String, Pin> getSupermodelPins()
211 return supermodelUnmovablePinsUnmodifiable;
215 * Returns the submodel interface pin with the given name on the supermodel side of this component.
217 * @author Daniel Kirschten
219 public Pin getSupermodelPin(String name)
221 return getSupermodelMovablePin(name);
225 * Returns a collection of movable submodel interface pins on the supermodel side of this component.
227 * @author Daniel Kirschten
229 protected Map<String, MovablePin> getSupermodelMovablePins()
231 return supermodelMovablePinsUnmodifiable;
235 * Returns the movable submodel interface pin with the given name on the supermodel side of this component.
237 * @author Daniel Kirschten
239 protected MovablePin getSupermodelMovablePin(String name)
241 return supermodelPins.get(name);
244 // "graphical" operations
247 * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
248 * stay at their position relative to the supermodel, which means they will move relative to the submodel.
250 * @author Daniel Kirschten
252 protected void setSubmodelScale(double submodelScale)
254 this.submodelScale = submodelScale;
256 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
257 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
259 requestRedraw();// needed if there is no submodel interface pin
263 * Returns the current factor by which the submodel is scaled when rendering.
265 * @author Daniel Kirschten
267 public double getSubmodelScale()
269 return submodelScale;
273 public void render(GeneralGC gc, Rectangle visibleRegion)
275 GCConfig conf = new GCConfig(gc);
276 TranslatedGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true);
278 double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height);
279 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
280 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
281 // we need to take the old alpha into account to support nested submodules better.
282 int oldAlpha = gc.getAlpha();
283 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
284 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
285 if (submodelAlpha != 0)
287 gc.setAlpha(submodelAlpha);
288 renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale));
292 gc.setAlpha(labelAlpha);
293 renderSymbol(gc, visibleRegion);
296 // draw the outline after all other operations to make interface pins look better
297 renderOutline(gc, visibleRegion);
300 // TODO make this a path
302 * Render the outline of this {@link SubmodelComponent}, e.g. the graphical elements that should stay visible if the submodel is drawn.
304 * @author Daniel Kirschten
306 protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion);
309 * Render the symbol of this {@link SubmodelComponent}, e.g. the things that should be hidden if the submodel is drawn.
311 * @author Daniel Kirschten
313 protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion);
315 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
317 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
321 public boolean clicked(double x, double y)
323 double scaledX = (x - getPosX()) / submodelScale;
324 double scaledY = (y - getPosY()) / submodelScale;
325 for (GUIComponent component : submodel.getComponentsByName().values())
326 if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
331 // operations no longer supported
334 protected void addPin(Pin pin)
336 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
340 protected void removePin(String name)
342 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");