Restructured the Preferences system
[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.wires.CoreWire.ReadEnd;
7 import net.mograsim.logic.model.BitVectorFormatter;
8 import net.mograsim.logic.model.model.LogicModelModifiable;
9 import net.mograsim.logic.model.model.components.ModelComponent;
10 import net.mograsim.logic.model.preferences.RenderPreferences;
11 import net.mograsim.logic.model.serializing.IdentifyParams;
12 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
13 import net.mograsim.preferences.ColorDefinition;
14 import net.mograsim.preferences.ColorManager;
15
16 /**
17  * A {@link ModelComponent} with only one pin. Is used to create wires connecting more than two pins. <br>
18  * Example: There are three pins <code>P1</code>, <code>P2</code>, <code>P3</code> that need to be connected. Solution: Create a
19  * ModelWireCrossPoint (<code>WCP</code>) and create the ModelWires <code>P1</code>-<code>WCP</code>, <code>P2</code>-<code>WCP</code>,
20  * <code>P3</code>-<code>WCP</code>.<br>
21  * Cross points are drawn as circles. The pin of cross points is in the center of this circle.
22  * 
23  * @author Daniel Kirschten
24  */
25 public class ModelWireCrossPoint extends ModelComponent
26 {
27         private static final int CIRCLE_RADIUS = 1;
28         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
29
30         /**
31          * The logical width of this cross point.
32          */
33         public final int logicWidth;
34         /**
35          * The (single) pin of this cross point.
36          */
37         private final Pin pin;
38
39         /**
40          * A {@link LogicObserver} calling {@link #requestRedraw()}.
41          */
42         private final LogicObserver logicObs;
43         /**
44          * The {@link ReadEnd} currently bound to this cross point.
45          */
46         private ReadEnd end;
47
48         // creation and destruction
49
50         public ModelWireCrossPoint(LogicModelModifiable model, int logicWidth)
51         {
52                 this(model, logicWidth, null);
53         }
54
55         public ModelWireCrossPoint(LogicModelModifiable model, int logicWidth, String name)
56         {
57                 super(model, name, false);
58                 this.logicWidth = logicWidth;
59                 logicObs = (i) -> model.requestRedraw();
60
61                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
62                 addPin(this.pin = new Pin(model, this, "", logicWidth, PinUsage.TRISTATE, CIRCLE_RADIUS, CIRCLE_RADIUS));
63
64                 init();
65         }
66
67         // pins
68
69         public Pin getPin()
70         {
71                 return pin;
72         }
73
74         // "graphical" operations
75
76         /**
77          * Moves the center (and therefore the pin) of this {@link ModelWireCrossPoint} to the given location.
78          * 
79          * @author Daniel Kirschten
80          */
81         public void moveCenterTo(double x, double y)
82         {
83                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
84         }
85
86         @Override
87         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
88         {
89                 ColorDefinition wireColor = BitVectorFormatter.formatAsColor(renderPrefs, end);
90                 if (wireColor != null)
91                         gc.setBackground(ColorManager.current().toColor(wireColor));
92                 gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM);
93         }
94
95         // core model binding
96
97         /**
98          * Binds this {@link ModelWireCrossPoint} to the given {@link ReadEnd}: The color of this {@link ModelWireCrossPoint} will now depend on
99          * the state of the given {@link ReadEnd}, and further changes of the given {@link ReadEnd} will result in readrawListeners being
100          * called.<br>
101          * The argument can be null, in which case the old binding is stopped.
102          * 
103          * @author Daniel Kirschten
104          */
105         public void setCoreModelBinding(ReadEnd end)
106         {
107                 if (this.end != null)
108                         this.end.deregisterObserver(logicObs);
109                 this.end = end;
110                 if (end != null)
111                         end.registerObserver(logicObs);
112         }
113
114         /**
115          * Returns whether this {@link ModelWireCrossPoint} has a core model binding or not.
116          */
117         public boolean hasCoreModelBinding()
118         {
119                 return end != null;
120         }
121
122         // serializing
123
124         @Override
125         public String getIDForSerializing(IdentifyParams idParams)
126         {
127                 return "WireCrossPoint";
128         }
129
130         @Override
131         public Integer getParamsForSerializing(IdentifyParams idParams)
132         {
133                 return logicWidth;
134         }
135
136         static
137         {
138                 IndirectModelComponentCreator.setComponentSupplier(ModelWireCrossPoint.class.getCanonicalName(),
139                                 (m, p, n) -> new ModelWireCrossPoint(m, p.getAsInt(), n));
140         }
141 }