Completely changed the structure and switched to Eclipse Plugin.
[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 java.util.Map;
4
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.core.LogicObservable;
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.ColorHelper;
12 import net.mograsim.logic.ui.model.ModelVisitor;
13 import net.mograsim.logic.ui.model.ViewModelModifiable;
14 import net.mograsim.logic.ui.model.components.GUIComponent;
15 import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate;
16
17 public class WireCrossPoint extends GUIComponent
18 {
19         private static final int CIRCLE_RADIUS = 1;
20         private static final int CIRCLE_DIAM = CIRCLE_RADIUS * 2;
21
22         private final Pin pin;
23         private final int logicWidth;
24
25         private final LogicObserver logicObs;
26         private ReadEnd end;
27
28         public WireCrossPoint(ViewModelModifiable model, int logicWidth)
29         {
30                 super(model);
31                 logicObs = (i) -> requestRedraw();
32
33                 this.logicWidth = logicWidth;
34                 setSize(CIRCLE_DIAM, CIRCLE_DIAM);
35                 addPin(this.pin = new Pin(this, "", logicWidth, CIRCLE_RADIUS, CIRCLE_RADIUS));
36         }
37
38         public void moveCenterTo(double x, double y)
39         {
40                 moveTo(x - CIRCLE_RADIUS, y - CIRCLE_RADIUS);
41         }
42
43         @Override
44         public void render(GeneralGC gc, Rectangle visibleRegion)
45         {
46                 ColorHelper.executeWithDifferentBackground(gc, BitVectorFormatter.formatAsColor(end),
47                                 () -> gc.fillOval(getPosX(), getPosY(), CIRCLE_DIAM, CIRCLE_DIAM));
48         }
49
50         public void setLogicModelBinding(ReadEnd end)
51         {
52                 deregisterLogicObs(this.end);
53                 this.end = end;
54                 registerLogicObs(end);
55         }
56
57         private void registerLogicObs(LogicObservable observable)
58         {
59                 if (observable != null)
60                         observable.registerObserver(logicObs);
61         }
62
63         private void deregisterLogicObs(LogicObservable observable)
64         {
65                 if (observable != null)
66                         observable.deregisterObserver(logicObs);
67         }
68
69         public int getLogicWidth()
70         {
71                 return logicWidth;
72         }
73
74         public Pin getPin()
75         {
76                 return pin;
77         }
78
79         @Override
80         public Map<String, Object> getInstantiationParameters()
81         {
82                 Map<String, Object> m = super.getInstantiationParameters();
83                 m.put(SimpleRectangularGUIGate.kLogicWidth, logicWidth);
84                 return m;
85         }
86
87         @Override
88         public void accept(ModelVisitor mv)
89         {
90                 mv.visit(this);
91         }
92 }