1 package net.mograsim.logic.ui.model.wires;
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.core.LogicObserver;
8 import net.mograsim.logic.core.types.BitVectorFormatter;
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;
10 import net.mograsim.logic.ui.ColorHelper;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.GUIComponent;
13 import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate;
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 (single) pin of this cross point.
32 private final Pin pin;
35 * A {@link LogicObserver} calling {@link #requestRedraw()}.
37 private final LogicObserver logicObs;
39 * The {@link ReadEnd} currently bound to this cross point.
43 // creation and destruction
45 public WireCrossPoint(ViewModelModifiable model, int logicWidth)
48 logicObs = (i) -> requestRedraw();
50 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
51 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
61 // "graphical" operations
64 * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
66 * @author Daniel Kirschten
68 public void moveCenterTo(double x, double y)
70 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
74 public void render(GeneralGC gc, Rectangle visibleRegion)
76 ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end),
77 () -> gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM));
80 // logic model binding
83 * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
84 * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
85 * The argument can be null, in which case the old binding is stopped.
87 * @author Daniel Kirschten
89 public void setLogicModelBinding(ReadEnd end)
92 this.end.deregisterObserver(logicObs);
95 end.registerObserver(logicObs);
99 * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
101 public boolean hasLogicModelBinding()
109 public Map<String, Object> getInstantiationParameters()
111 Map<String, Object> m = super.getInstantiationParameters();
112 m.put(SimpleRectangularGUIGate.kLogicWidth, pin.logicWidth);