Added DeserializedSubmodelComponent Editor project
[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 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.ui.model.ViewModelModifiable;
12 import net.mograsim.logic.ui.model.components.GUIComponent;
13 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
14 import net.mograsim.preferences.ColorDefinition;
15 import net.mograsim.preferences.ColorManager;
16
17 /**
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.
23  * 
24  * @author Daniel Kirschten
25  */
26 public class WireCrossPoint extends GUIComponent implements ConnectionPoint
27 {
28         private static final int CIRCLE_RADIUS = 1;
29         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
30
31         /**
32          * The (single) pin of this cross point.
33          */
34         private final Pin pin;
35
36         /**
37          * A {@link LogicObserver} calling {@link #requestRedraw()}.
38          */
39         private final LogicObserver logicObs;
40         /**
41          * The {@link ReadEnd} currently bound to this cross point.
42          */
43         private ReadEnd end;
44
45         // creation and destruction
46
47         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
48         {
49                 super(model);
50                 logicObs = (i) -> requestRedraw();
51
52                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
53                 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
54         }
55
56         // pins
57
58         @Override
59         public Pin getPin()
60         {
61                 return pin;
62         }
63
64         // "graphical" operations
65
66         /**
67          * Moves the center (and therefore the pin) of this {@link WireCrossPoint} to the given location.
68          * 
69          * @author Daniel Kirschten
70          */
71         public void moveCenterTo(double x, double y)
72         {
73                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
74         }
75
76         @Override
77         public void render(GeneralGC gc, Rectangle visibleRegion)
78         {
79                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
80                 if (wireColor != null)
81                         gc.setBackground(ColorManager.current().toColor(wireColor));
82                 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
83         }
84
85         // logic model binding
86
87         /**
88          * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
89          * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
90          * The argument can be null, in which case the old binding is stopped.
91          * 
92          * @author Daniel Kirschten
93          */
94         public void setLogicModelBinding(ReadEnd end)
95         {
96                 if (this.end != null)
97                         this.end.deregisterObserver(logicObs);
98                 this.end = end;
99                 if (end != null)
100                         end.registerObserver(logicObs);
101         }
102
103         /**
104          * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
105          */
106         public boolean hasLogicModelBinding()
107         {
108                 return end != null;
109         }
110
111         // serializing
112
113         @Override
114         public JsonElement getParams()
115         {
116                 return new JsonPrimitive(pin.logicWidth);
117         }
118
119         static
120         {
121                 IndirectGUIComponentCreator.setComponentProvider(WireCrossPoint.class.getCanonicalName(),
122                                 (m, p) -> new WireCrossPoint(m, p.getAsInt()));
123         }
124 }