Pins now have a PinUsage
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / wires / WireCrossPoint.java
1 package net.mograsim.logic.model.model.wires;
2
3 import com.google.gson.JsonElement;
4 import com.google.gson.JsonPrimitive;
5
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.IdentifierGetter;
14 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
15 import net.mograsim.preferences.ColorDefinition;
16 import net.mograsim.preferences.ColorManager;
17
18 /**
19  * A {@link GUIComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
20  * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
21  * WireCrossPoint (<code>WCP</code>) and create the GUIWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
22  * <code>P3</code>-<code>WCP</code>.<br>
23  * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
24  * 
25  * @author Daniel Kirschten
26  */
27 public class WireCrossPoint extends GUIComponent
28 {
29         private static final int CIRCLE_RADIUS = 1;
30         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
31
32         /**
33          * The logical width of this cross point.
34          */
35         public final int logicWidth;
36         /**
37          * The (single) pin of this cross point.
38          */
39         private final Pin pin;
40
41         /**
42          * A {@link LogicObserver} calling {@link #requestRedraw()}.
43          */
44         private final LogicObserver logicObs;
45         /**
46          * The {@link ReadEnd} currently bound to this cross point.
47          */
48         private ReadEnd end;
49
50         // creation and destruction
51
52         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
53         {
54                 this(model, logicWidth, null);
55         }
56
57         public WireCrossPoint(ViewModelModifiable model, int logicWidth, String name)
58         {
59                 super(model, name);
60                 this.logicWidth = logicWidth;
61                 logicObs = (i) -> model.requestRedraw();
62
63                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
64                 addPin(this.pin = new Pin(this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
65         }
66
67         // pins
68
69         public Pin getPin()
70         {
71                 return pin;
72         }
73
74         // "graphical" operations
75
76         /**
77          * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
78          * 
79          * @author Daniel Kirschten
80          */
81         public void moveCenterTo(double x, double y)
82         {
83                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
84         }
85
86         @Override
87         public void render(GeneralGC gc, Rectangle visibleRegion)
88         {
89                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
90                 if (wireColor != null)
91                         gc.setBackground(ColorManager.current().toColor(wireColor));
92                 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
93         }
94
95         // logic model binding
96
97         /**
98          * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
99          * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
100          * The argument can be null, in which case the old binding is stopped.
101          * 
102          * @author Daniel Kirschten
103          */
104         public void setLogicModelBinding(ReadEnd end)
105         {
106                 if (this.end != null)
107                         this.end.deregisterObserver(logicObs);
108                 this.end = end;
109                 if (end != null)
110                         end.registerObserver(logicObs);
111         }
112
113         /**
114          * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
115          */
116         public boolean hasLogicModelBinding()
117         {
118                 return end != null;
119         }
120
121         // serializing
122
123         @Override
124         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
125         {
126                 return new JsonPrimitive(logicWidth);
127         }
128
129         static
130         {
131                 IndirectGUIComponentCreator.setComponentSupplier(WireCrossPoint.class.getCanonicalName(),
132                                 (m, p, n) -> new WireCrossPoint(m, p.getAsInt(), n));
133         }
134 }