1 package net.mograsim.logic.ui.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.Wire.ReadEnd;
8 import net.mograsim.logic.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.GUIComponent;
10 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
11 import net.mograsim.preferences.ColorDefinition;
12 import net.mograsim.preferences.ColorManager;
15 * A {@link GUIComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
16 * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
17 * WireCrossPoint (<code>WCP</code>) and create the GUIWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
18 * <code>P3</code>-<code>WCP</code>.<br>
19 * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
21 * @author Daniel Kirschten
23 public class WireCrossPoint extends GUIComponent implements ConnectionPoint
25 private static final int CIRCLE_RADIUS = 1;
26 private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
29 * The (single) pin of this cross point.
31 private final Pin pin;
34 * A {@link LogicObserver} calling {@link #requestRedraw()}.
36 private final LogicObserver logicObs;
38 * The {@link ReadEnd} currently bound to this cross point.
42 // creation and destruction
44 public WireCrossPoint(ViewModelModifiable model, int logicWidth)
47 logicObs = (i) -> requestRedraw();
49 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
50 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 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
77 if (wireColor != null)
78 gc.setBackground(ColorManager.current().toColor(wireColor));
79 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
82 // logic model binding
85 * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
86 * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
87 * The argument can be null, in which case the old binding is stopped.
89 * @author Daniel Kirschten
91 public void setLogicModelBinding(ReadEnd end)
94 this.end.deregisterObserver(logicObs);
97 end.registerObserver(logicObs);
101 * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
103 public boolean hasLogicModelBinding()
111 IndirectGUIComponentCreator.setComponentProvider(WireCrossPoint.class.getCanonicalName(), (m, p) -> new WireCrossPoint(m, 1));