d558daa5082a16bb053cebb05116df724929f157
[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 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.model.model.ViewModelModifiable;
9 import net.mograsim.logic.model.model.components.GUIComponent;
10 import net.mograsim.logic.model.serializing.IdentifierGetter;
11 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
14
15 /**
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.
21  * 
22  * @author Daniel Kirschten
23  */
24 public class WireCrossPoint extends GUIComponent
25 {
26         private static final int CIRCLE_RADIUS = 1;
27         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
28
29         /**
30          * The logical width of this cross point.
31          */
32         public final int logicWidth;
33         /**
34          * The (single) pin of this cross point.
35          */
36         private final Pin pin;
37
38         /**
39          * A {@link LogicObserver} calling {@link #requestRedraw()}.
40          */
41         private final LogicObserver logicObs;
42         /**
43          * The {@link ReadEnd} currently bound to this cross point.
44          */
45         private ReadEnd end;
46
47         // creation and destruction
48
49         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
50         {
51                 this(model, logicWidth, null);
52         }
53
54         public WireCrossPoint(ViewModelModifiable model, int logicWidth, String name)
55         {
56                 super(model, name);
57                 this.logicWidth = logicWidth;
58                 logicObs = (i) -> model.requestRedraw();
59
60                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
61                 addPin(this.pin = new Pin(this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
62         }
63
64         // pins
65
66         public Pin getPin()
67         {
68                 return pin;
69         }
70
71         // "graphical" operations
72
73         /**
74          * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
75          * 
76          * @author Daniel Kirschten
77          */
78         public void moveCenterTo(double x, double y)
79         {
80                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
81         }
82
83         @Override
84         public void render(GeneralGC gc, Rectangle visibleRegion)
85         {
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);
90         }
91
92         // logic model binding
93
94         /**
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.
98          * 
99          * @author Daniel Kirschten
100          */
101         public void setLogicModelBinding(ReadEnd end)
102         {
103                 if (this.end != null)
104                         this.end.deregisterObserver(logicObs);
105                 this.end = end;
106                 if (end != null)
107                         end.registerObserver(logicObs);
108         }
109
110         /**
111          * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
112          */
113         public boolean hasLogicModelBinding()
114         {
115                 return end != null;
116         }
117
118         // serializing
119
120         @Override
121         public Integer getParamsForSerializing(IdentifierGetter idGetter)
122         {
123                 return logicWidth;
124         }
125
126         static
127         {
128                 IndirectGUIComponentCreator.setComponentSupplier(WireCrossPoint.class.getCanonicalName(),
129                                 (m, p, n) -> new WireCrossPoint(m, p.getAsInt(), n));
130         }
131 }