The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / wires / ModelWireCrossPoint.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.CoreWire.ReadEnd;
8 import net.mograsim.logic.model.model.LogicModelModifiable;
9 import net.mograsim.logic.model.model.components.ModelComponent;
10 import net.mograsim.logic.model.serializing.IdentifyParams;
11 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
14
15 /**
16  * A {@link ModelComponent} 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  * ModelWireCrossPoint (<code>WCP</code>) and create the ModelWires <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 ModelWireCrossPoint extends ModelComponent
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 ModelWireCrossPoint(LogicModelModifiable model, int logicWidth)
50         {
51                 this(model, logicWidth, null);
52         }
53
54         public ModelWireCrossPoint(LogicModelModifiable model, int logicWidth, String name)
55         {
56                 super(model, name, false);
57                 this.logicWidth = logicWidth;
58                 logicObs = (i) -> model.requestRedraw();
59
60                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
61                 addPin(this.pin = new Pin(model, this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
62
63                 init();
64         }
65
66         // pins
67
68         public Pin getPin()
69         {
70                 return pin;
71         }
72
73         // "graphical" operations
74
75         /**
76          * Moves the center (and therefore the pin) of this {@link ModelWireCrossPoint} to the given location.
77          * 
78          * @author Daniel Kirschten
79          */
80         public void moveCenterTo(double x, double y)
81         {
82                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
83         }
84
85         @Override
86         public void render(GeneralGC gc, Rectangle visibleRegion)
87         {
88                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(end);
89                 if (wireColor != null)
90                         gc.setBackground(ColorManager.current().toColor(wireColor));
91                 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
92         }
93
94         // core model binding
95
96         /**
97          * Binds this {@link ModelWireCrossPoint} to the given {@link ReadEnd}: The color of this {@link ModelWireCrossPoint} will now depend on
98          * the state of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being
99          * called.<br>
100          * The argument can be null, in which case the old binding is stopped.
101          * 
102          * @author Daniel Kirschten
103          */
104         public void setCoreModelBinding(ReadEnd end)
105         {
106                 if (this.end != null)
107                         this.end.deregisterObserver(logicObs);
108                 this.end = end;
109                 if (end != null)
110                         end.registerObserver(logicObs);
111         }
112
113         /**
114          * Returns whether this {@link ModelWireCrossPoint} has a core model binding or not.
115          */
116         public boolean hasCoreModelBinding()
117         {
118                 return end != null;
119         }
120
121         // serializing
122
123         @Override
124         public String getIDForSerializing(IdentifyParams idParams)
125         {
126                 return "WireCrossPoint";
127         }
128
129         @Override
130         public Integer getParamsForSerializing(IdentifyParams idParams)
131         {
132                 return logicWidth;
133         }
134
135         static
136         {
137                 IndirectModelComponentCreator.setComponentSupplier(ModelWireCrossPoint.class.getCanonicalName(),
138                                 (m, p, n) -> new ModelWireCrossPoint(m, p.getAsInt(), n));
139         }
140 }