1 package net.mograsim.logic.model.model.wires;
3 import com.google.gson.JsonElement;
4 import com.google.gson.JsonPrimitive;
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
8 import net.mograsim.logic.core.LogicObserver;
9 import net.mograsim.logic.core.types.BitVectorFormatter;
10 import net.mograsim.logic.core.wires.Wire.ReadEnd;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.GUIComponent;
13 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
14 import net.mograsim.preferences.ColorDefinition;
15 import net.mograsim.preferences.ColorManager;
18 * A {@link GUIComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
19 * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
20 * WireCrossPoint (<code>WCP</code>) and create the GUIWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
21 * <code>P3</code>-<code>WCP</code>.<br>
22 * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
24 * @author Daniel Kirschten
26 public class WireCrossPoint extends GUIComponent
28 private static final int CIRCLE_RADIUS = 1;
29 private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
32 * The (single) pin of this cross point.
34 private final Pin pin;
37 * A {@link LogicObserver} calling {@link #requestRedraw()}.
39 private final LogicObserver logicObs;
41 * The {@link ReadEnd} currently bound to this cross point.
45 // creation and destruction
47 public WireCrossPoint(ViewModelModifiable model, int logicWidth)
49 this(model, logicWidth, null);
52 public WireCrossPoint(ViewModelModifiable model, int logicWidth, String name)
55 logicObs = (i) -> requestRedraw();
57 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
58 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
68 // "graphical" operations
71 * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
73 * @author Daniel Kirschten
75 public void moveCenterTo(double x, double y)
77 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
81 public void render(GeneralGC gc, Rectangle visibleRegion)
83 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
84 if (wireColor != null)
85 gc.setBackground(ColorManager.current().toColor(wireColor));
86 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
89 // logic model binding
92 * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
93 * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
94 * The argument can be null, in which case the old binding is stopped.
96 * @author Daniel Kirschten
98 public void setLogicModelBinding(ReadEnd end)
100 if (this.end != null)
101 this.end.deregisterObserver(logicObs);
104 end.registerObserver(logicObs);
108 * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
110 public boolean hasLogicModelBinding()
118 public JsonElement getParams()
120 return new JsonPrimitive(pin.logicWidth);
125 IndirectGUIComponentCreator.setComponentSupplier(WireCrossPoint.class.getCanonicalName(),
126 (m, p, n) -> new WireCrossPoint(m, p.getAsInt(), n));