Restructured serializing / deserializing
[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 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.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.GUIComponent;
10 import net.mograsim.logic.ui.serializing.IndirectGUIComponentCreator;
11 import net.mograsim.preferences.ColorDefinition;
12 import net.mograsim.preferences.ColorManager;
13
14 /**
15  * A {@link GUIComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
16  * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
17  * WireCrossPoint (<code>WCP</code>) and create the GUIWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
18  * <code>P3</code>-<code>WCP</code>.<br>
19  * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
20  * 
21  * @author Daniel Kirschten
22  */
23 public class WireCrossPoint extends GUIComponent implements ConnectionPoint
24 {
25         private static final int CIRCLE_RADIUS = 1;
26         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
27
28         /**
29          * The (single) pin of this cross point.
30          */
31         private final Pin pin;
32
33         /**
34          * A {@link LogicObserver} calling {@link #requestRedraw()}.
35          */
36         private final LogicObserver logicObs;
37         /**
38          * The {@link ReadEnd} currently bound to this cross point.
39          */
40         private ReadEnd end;
41
42         // creation and destruction
43
44         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
45         {
46                 super(model);
47                 logicObs = (i) -> requestRedraw();
48
49                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
50                 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
51         }
52
53         // pins
54
55         @Override
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                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
77                 if (wireColor != null)
78                         gc.setBackground(ColorManager.current().toColor(wireColor));
79                 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
80         }
81
82         // logic model binding
83
84         /**
85          * Binds this {@link WireCrossPoint} to the given {@link ReadEnd}: The color of this {@link WireCrossPoint} will now depend on the state
86          * of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being called.<br>
87          * The argument can be null, in which case the old binding is stopped.
88          * 
89          * @author Daniel Kirschten
90          */
91         public void setLogicModelBinding(ReadEnd end)
92         {
93                 if (this.end != null)
94                         this.end.deregisterObserver(logicObs);
95                 this.end = end;
96                 if (end != null)
97                         end.registerObserver(logicObs);
98         }
99
100         /**
101          * Returns whether this {@link WireCrossPoint} has a logic model binding or not.
102          */
103         public boolean hasLogicModelBinding()
104         {
105                 return end != null;
106         }
107
108         static
109         {
110                 // TODO read params
111                 IndirectGUIComponentCreator.setComponentProvider(WireCrossPoint.class.getCanonicalName(), (m, p) -> new WireCrossPoint(m, 1));
112         }
113 }