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;
28 * A {@link GUIComponent} consisting of another model. A <code>SubmodelComponent</code> can have so-called "interface pins" connecting the
29 * inner and outer models.
31 public abstract class SubmodelComponent extends GUIComponent
34 * A modifiable view of {@link #submodel}.
36 protected final ViewModelModifiable submodelModifiable;
38 * The model this {@link SubmodelComponent} consists of.
40 public final ViewModel submodel;
42 * The list of all submodel interface pins of this {@link SubmodelComponent} on the submodel side.
44 private final Map<String, MovablePin> submodelPins;
46 * An unmodifiable view of {@link #submodelPins}.
48 private final Map<String, MovablePin> submodelMovablePinsUnmodifiable;
50 * An unmodifiable view of {@link #submodelPins} where pins are not movable.
52 private final Map<String, Pin> submodelUnmovablePinsUnmodifiable;
54 * The list of all submodel interface pins of this {@link SubmodelComponent} on the supermodel side.
56 private final Map<String, MovablePin> supermodelPins;
58 * An unmodifiable view of {@link #supermodelPins}.
60 private final Map<String, MovablePin> supermodelMovablePinsUnmodifiable;
62 * An unmodifiable view of {@link #supermodelPins} where pins are not movable.
64 private final Map<String, Pin> supermodelUnmovablePinsUnmodifiable;
66 * A pseudo-component containing all submodel interface pins on the submodel side.
68 private final SubmodelInterface submodelInterface;
71 * The factor by which the submodel is scaled when rendering.
73 private double submodelScale;
75 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel starts to
78 private double maxVisibleRegionFillRatioForAlpha0;
80 * If this {@link SubmodelComponent} fills at least this amount of the visible region vertically or horizontally, the submodel is fully
83 private double minVisibleRegionFillRatioForAlpha1;
85 * The renderer used for rendering the submodel.
87 private final LogicUIRenderer renderer;
89 // creation and destruction
91 public SubmodelComponent(ViewModelModifiable model)
94 this.submodelModifiable = new ViewModelModifiable();
95 this.submodel = submodelModifiable;
96 this.submodelPins = new HashMap<>();
97 this.submodelMovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
98 this.submodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(submodelPins);
99 this.supermodelPins = new HashMap<>();
100 this.supermodelMovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
101 this.supermodelUnmovablePinsUnmodifiable = Collections.unmodifiableMap(supermodelPins);
102 this.submodelInterface = new SubmodelInterface(submodelModifiable);
104 this.submodelScale = 1;
105 this.maxVisibleRegionFillRatioForAlpha0 = 0.4;
106 this.minVisibleRegionFillRatioForAlpha1 = 0.8;
107 this.renderer = new LogicUIRenderer(submodelModifiable);
109 submodelModifiable.addRedrawListener(this::requestRedraw);
115 * Adds a new submodel interface pin.
117 * @param supermodelPin the submodel interface pin on the supermodel side
119 * @return the submodel interface pin on the submodel side
121 * @author Daniel Kirschten
123 protected Pin addSubmodelInterface(MovablePin supermodelPin)
125 super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
127 String name = supermodelPin.name;
128 MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, supermodelPin.getRelX() / submodelScale,
129 supermodelPin.getRelY() / submodelScale);
131 submodelPin.addPinMovedListener(p ->
133 double newRelX = p.getRelX() * submodelScale;
134 double newRelY = p.getRelY() * submodelScale;
135 if (supermodelPin.getRelX() != newRelX || supermodelPin.getRelY() != newRelY)
136 supermodelPin.setRelPos(newRelX, newRelY);
138 supermodelPin.addPinMovedListener(p ->
140 double newRelX = p.getRelX() / submodelScale;
141 double newRelY = p.getRelY() / submodelScale;
142 if (submodelPin.getRelX() != newRelX || submodelPin.getRelY() != newRelY)
143 submodelPin.setRelPos(newRelX, newRelY);
146 submodelInterface.addPin(submodelPin);
148 submodelPins.put(name, submodelPin);
149 supermodelPins.put(name, supermodelPin);
151 // no need to call requestRedraw() because addPin() will request a redraw
156 * Removes a submodel interface pin.
158 * @author Daniel Kirschten
160 protected void removeSubmodelInterface(String name)
162 super.removePin(name);// do this first to be fail-fast if this component doesn't have a pin with the given name
163 Pin submodelPin = submodelPins.remove(name);
164 submodelInterface.removePin(submodelPin.name);
165 supermodelPins.remove(name);
167 // no need to call requestRedraw() because removePin() will request a redraw
171 * Returns a collection of submodel interface pins on the submodel side of this component.
173 * @author Daniel Kirschten
175 public Map<String, Pin> getSubmodelPins()
177 return submodelUnmovablePinsUnmodifiable;
181 * Returns the submodel interface pin with the given name on the submodel side of this component.
183 * @author Daniel Kirschten
185 public Pin getSubmodelPin(String name)
187 return getSubmodelMovablePin(name);
191 * Returns a collection of movable submodel interface pins on the submodel side of this component.
193 * @author Daniel Kirschten
195 protected Map<String, MovablePin> getSubmodelMovablePins()
197 return submodelMovablePinsUnmodifiable;
201 * Returns the movable submodel interface pin with the given name on the submodel side of this component.
203 * @author Daniel Kirschten
205 protected MovablePin getSubmodelMovablePin(String name)
207 return submodelPins.get(name);
211 * Returns a collection of submodel interface pins on the supermodel side of this component.
213 * @author Daniel Kirschten
215 public Map<String, Pin> getSupermodelPins()
217 return supermodelUnmovablePinsUnmodifiable;
221 * Returns the submodel interface pin with the given name on the supermodel side of this component.
223 * @author Daniel Kirschten
225 public Pin getSupermodelPin(String name)
227 return getSupermodelMovablePin(name);
231 * Returns a collection of movable submodel interface pins on the supermodel side of this component.
233 * @author Daniel Kirschten
235 protected Map<String, MovablePin> getSupermodelMovablePins()
237 return supermodelMovablePinsUnmodifiable;
241 * Returns the movable submodel interface pin with the given name on the supermodel side of this component.
243 * @author Daniel Kirschten
245 protected MovablePin getSupermodelMovablePin(String name)
247 return supermodelPins.get(name);
250 // "graphical" operations
253 * Sets the factor by which the submodel is scaled when rendering and calls redrawListeners. Note that the submodel interface pins will
254 * stay at their position relative to the supermodel, which means they will move relative to the submodel.
256 * @author Daniel Kirschten
258 protected void setSubmodelScale(double submodelScale)
260 this.submodelScale = submodelScale;
262 for (Entry<String, MovablePin> e : supermodelPins.entrySet())
263 getSubmodelMovablePin(e.getKey()).setRelPos(e.getValue().getRelX() * submodelScale, e.getValue().getRelY() * submodelScale);
265 requestRedraw();// needed if there is no submodel interface pin
269 * Returns the current factor by which the submodel is scaled when rendering.
271 * @author Daniel Kirschten
273 protected double getSubmodelScale()
275 return submodelScale;
279 public void render(GeneralGC gc, Rectangle visibleRegion)
281 GCConfig conf = new GCConfig(gc);
282 TranslatedGC tgc = new TranslatedGC(gc, getPosX(), getPosY(), submodelScale, true);
284 double visibleRegionFillRatio = Math.max(getWidth() / visibleRegion.width, getHeight() / visibleRegion.height);
285 double alphaFactor = map(visibleRegionFillRatio, maxVisibleRegionFillRatioForAlpha0, minVisibleRegionFillRatioForAlpha1, 0, 1);
286 alphaFactor = Math.max(0, Math.min(1, alphaFactor));
287 // we need to take the old alpha into account to support nested submodules better.
288 int oldAlpha = gc.getAlpha();
289 int submodelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * alphaFactor)));
290 int labelAlpha = Math.max(0, Math.min(255, (int) (oldAlpha * (1 - alphaFactor))));
291 if (submodelAlpha != 0)
293 gc.setAlpha(submodelAlpha);
294 renderer.render(tgc, visibleRegion.translate(getPosX() / submodelScale, getPosY() / submodelScale, 1 / submodelScale));
298 gc.setAlpha(labelAlpha);
299 renderSymbol(gc, visibleRegion);
302 // draw the outline after all other operations to make interface pins look better
303 renderOutline(gc, visibleRegion);
306 protected abstract void renderOutline(GeneralGC gc, Rectangle visibleRegion);
308 protected abstract void renderSymbol(GeneralGC gc, Rectangle visibleRegion);
310 private static double map(double val, double valMin, double valMax, double mapMin, double mapMax)
312 return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin);
316 public boolean clicked(double x, double y)
318 double scaledX = (x - getPosX()) / submodelScale;
319 double scaledY = (y - getPosY()) / submodelScale;
320 for (GUIComponent component : submodel.getComponents())
321 if (component.getBounds().contains(scaledX, scaledY) && component.clicked(scaledX, scaledY))
329 * @return {@link SubmodelComponentParams}, which describe this {@link SubmodelComponent}.
331 public SubmodelComponentParams calculateParams()
333 SubmodelComponentParams params = new SubmodelComponentParams();
334 params.name = getIdentifier();
335 params.type = SubmodelComponent.class.getSimpleName();
336 params.composition = calculateCompositionParams();
338 params.width = getWidth();
339 params.height = getHeight();
341 InterfacePinParams[] iPins = new InterfacePinParams[getPins().size()];
343 for (Pin p : getPins().values())
345 InterfacePinParams iPinParams = new InterfacePinParams();
346 iPins[i] = iPinParams;
347 iPinParams.location = p.getRelPos();
348 iPinParams.name = p.name;
349 iPinParams.logicWidth = p.logicWidth;
352 params.interfacePins = iPins;
356 protected ComponentCompositionParams calculateCompositionParams()
358 ComponentCompositionParams params = new ComponentCompositionParams();
359 params.innerScale = getSubmodelScale();
361 List<GUIComponent> compList = submodelModifiable.getComponents();
362 Iterator<GUIComponent> componentIt = compList.iterator();
363 componentIt.next(); // Skip inner SubmodelInterface
364 InnerComponentParams[] comps = new InnerComponentParams[compList.size() - 1];
366 while (componentIt.hasNext())
368 GUIComponent component = componentIt.next();
369 InnerComponentParams inner = new InnerComponentParams();
371 inner.params = component.getInstantiationParameters();
372 inner.pos = new Point(component.getPosX(), component.getPosY());
373 inner.name = component.getIdentifier();
376 params.subComps = comps;
378 List<GUIWire> wireList = submodelModifiable.getWires();
379 InnerWireParams wires[] = new InnerWireParams[wireList.size()];
381 for (GUIWire wire : wireList)
383 InnerWireParams inner = new InnerWireParams();
385 InnerPinParams pin1Params = new InnerPinParams(), pin2Params = new InnerPinParams();
387 pin1Params.pinName = wire.getPin1().name;
388 pin1Params.compId = compList.indexOf(wire.getPin1().component);
389 pin2Params.pinName = wire.getPin2().name;
390 pin2Params.compId = compList.indexOf(wire.getPin2().component);
391 inner.pin1 = pin1Params;
392 inner.pin2 = pin2Params;
393 inner.path = wire.getPath();
396 params.innerWires = wires;
400 public List<GUIComponent> getComponents()
402 return submodel.getComponents();
405 public List<GUIWire> getWires()
407 return submodel.getWires();
410 // operations no longer supported
413 protected void addPin(Pin pin)
415 throw new UnsupportedOperationException("Can't add pins to a SubmodelComponent directly, call addSubmodelInterface instead");
419 protected void removePin(String name)
421 throw new UnsupportedOperationException("Can't remove pins of a SubmodelComponent directly, call removeSubmodelInterface instead");