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