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.ViewModelModifiable;
9 import net.mograsim.logic.model.model.components.GUIComponent;
10 import net.mograsim.logic.model.serializing.IdentifyParams;
11 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
16 * A {@link GUIComponent} 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 * WireCrossPoint (<code>WCP</code>) and create the GUIWires <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 WireCrossPoint extends GUIComponent
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 WireCrossPoint(ViewModelModifiable model, int logicWidth)
51 this(model, logicWidth, null);
54 public WireCrossPoint(ViewModelModifiable model, int logicWidth, String name)
57 this.logicWidth = logicWidth;
58 logicObs = (i) -> model.requestRedraw();
60 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
61 addPin(this.pin = new Pin(this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
71 // "graphical" operations
74 * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
76 * @author Daniel Kirschten
78 public void moveCenterTo(double x, double y)
80 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
84 public void render(GeneralGC gc, Rectangle visibleRegion)
86 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
87 if (wireColor != null)
88 gc.setBackground(ColorManager.current().toColor(wireColor));
89 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
92 // logic model binding
95 * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
96 * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
97 * The argument can be null, in which case the old binding is stopped.
99 * @author Daniel Kirschten
101 public void setLogicModelBinding(ReadEnd end)
103 if (this.end != null)
104 this.end.deregisterObserver(logicObs);
107 end.registerObserver(logicObs);
111 * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
113 public boolean hasLogicModelBinding()
121 public String getIDForSerializing(IdentifyParams idParams)
123 return "WireCrossPoint";
127 public Integer getParamsForSerializing(IdentifyParams idParams)
134 IndirectGUIComponentCreator.setComponentSupplier(WireCrossPoint.class.getCanonicalName(),
135 (m, p, n) -> new WireCrossPoint(m, p.getAsInt(), n));