1 package net.mograsim.logic.model.model.wires;
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
5 import net.mograsim.logic.core.LogicObserver;
6 import net.mograsim.logic.core.types.BitVectorFormatter;
7 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
8 import net.mograsim.logic.model.model.LogicModelModifiable;
9 import net.mograsim.logic.model.model.components.ModelComponent;
10 import net.mograsim.logic.model.serializing.IdentifyParams;
11 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
16 * A {@link ModelComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
17 * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
18 * ModelWireCrossPoint (<code>WCP</code>) and create the ModelWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
19 * <code>P3</code>-<code>WCP</code>.<br>
20 * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
22 * @author Daniel Kirschten
24 public class ModelWireCrossPoint extends ModelComponent
26 private static final int CIRCLE_RADIUS = 1;
27 private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
30 * The logical width of this cross point.
32 public final int logicWidth;
34 * The (single) pin of this cross point.
36 private final Pin pin;
39 * A {@link LogicObserver} calling {@link #requestRedraw()}.
41 private final LogicObserver logicObs;
43 * The {@link ReadEnd} currently bound to this cross point.
47 // creation and destruction
49 public ModelWireCrossPoint(LogicModelModifiable model, int logicWidth)
51 this(model, logicWidth, null);
54 public ModelWireCrossPoint(LogicModelModifiable model, int logicWidth, String name)
56 super(model, name, false);
57 this.logicWidth = logicWidth;
58 logicObs = (i) -> model.requestRedraw();
60 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
61 addPin(this.pin = new Pin(model, this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
73 // "graphical" operations
76 * Moves the center (and therefore the pin) of this {@link ModelWireCrossPoint} to the given location.
78 * @author Daniel Kirschten
80 public void moveCenterTo(double x, double y)
82 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
86 public void render(GeneralGC gc, Rectangle visibleRegion)
88 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
89 if (wireColor != null)
90 gc.setBackground(ColorManager.current().toColor(wireColor));
91 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
97 * Binds this {@link ModelWireCrossPoint} to the given {@link ReadEnd}: The color of this {@link ModelWireCrossPoint} will now depend on
98 * the state of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being
100 * The argument can be null, in which case the old binding is stopped.
102 * @author Daniel Kirschten
104 public void setCoreModelBinding(ReadEnd end)
106 if (this.end != null)
107 this.end.deregisterObserver(logicObs);
110 end.registerObserver(logicObs);
114 * Returns whether this {@link ModelWireCrossPoint} has a core model binding or not.
116 public boolean hasCoreModelBinding()
124 public String getIDForSerializing(IdentifyParams idParams)
126 return "WireCrossPoint";
130 public Integer getParamsForSerializing(IdentifyParams idParams)
137 IndirectModelComponentCreator.setComponentSupplier(ModelWireCrossPoint.class.getCanonicalName(),
138 (m, p, n) -> new ModelWireCrossPoint(m, p.getAsInt(), n));