de5ba4ac5026bb1eeedfec536434acbd5ddf64da
[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.ColorHelper;
11 import net.mograsim.logic.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.GUIComponent;
13 import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate;
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 (single) pin of this cross point.
31          */
32         private final Pin pin;
33
34         /**
35          * A {@link LogicObserver} calling {@link #requestRedraw()}.
36          */
37         private final LogicObserver logicObs;
38         /**
39          * The {@link ReadEnd} currently bound to this cross point.
40          */
41         private ReadEnd end;
42
43         // creation and destruction
44
45         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
46         {
47                 super(model);
48                 logicObs = (i) -> requestRedraw();
49
50                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
51                 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
52         }
53
54         // pins
55
56         public Pin getPin()
57         {
58                 return pin;
59         }
60
61         // "graphical" operations
62
63         /**
64          * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
65          * 
66          * @author Daniel Kirschten
67          */
68         public void moveCenterTo(double x, double y)
69         {
70                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
71         }
72
73         @Override
74         public void render(GeneralGC gc, Rectangle visibleRegion)
75         {
76                 ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end),
77                                 () -> gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM));
78         }
79
80         // logic model binding
81
82         /**
83          * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
84          * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
85          * The argument can be null, in which case the old binding is stopped.
86          * 
87          * @author Daniel Kirschten
88          */
89         public void setLogicModelBinding(ReadEnd end)
90         {
91                 if (this.end != null)
92                         this.end.deregisterObserver(logicObs);
93                 this.end = end;
94                 if (end != null)
95                         end.registerObserver(logicObs);
96         }
97
98         /**
99          * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
100          */
101         public boolean hasLogicModelBinding()
102         {
103                 return end != null;
104         }
105
106         // serializing
107
108         @Override
109         public Map<String, Object> getInstantiationParameters()
110         {
111                 Map<String, Object> m = super.getInstantiationParameters();
112                 m.put(SimpleRectangularGUIGate.kLogicWidth, pin.logicWidth);
113                 return m;
114         }
115 }