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