Added methods to get a TriStateBuffer's pins comfortably
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUITriStateBuffer.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import com.google.gson.Gson;
6 import com.google.gson.JsonElement;
7 import com.google.gson.JsonSyntaxException;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.model.model.ViewModelModifiable;
12 import net.mograsim.logic.model.model.components.GUIComponent;
13 import net.mograsim.logic.model.model.components.Orientation;
14 import net.mograsim.logic.model.model.components.OrientationCalculator;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.model.wires.PinUsage;
17 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
18 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
19 import net.mograsim.logic.model.serializing.IdentifierGetter;
20 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
21 import net.mograsim.preferences.Preferences;
22
23 public class GUITriStateBuffer extends GUIComponent
24 {
25
26         private static final double width = 20;
27         private static final double height = 20;
28         private Pin input;
29         private Pin output;
30         private Pin enable;
31         private double[] path;
32
33         private GUITriStateBufferParams params;
34         private OrientationCalculator oc;
35
36         public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params)
37         {
38                 this(model, params, null);
39         }
40
41         public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params, String name)
42         {
43                 super(model, name);
44                 this.params = params;
45
46                 oc = new OrientationCalculator(params.orientation, width, height);
47
48                 double wHalf = width / 2;
49                 double hHalf = height / 2;
50                 double hQuar = height / 4;
51
52                 this.input = new Pin(this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
53                 this.output = new Pin(this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
54                 this.enable = new Pin(this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
55                 this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
56                                 oc.newY(0, height) };
57
58                 setSize(oc.width(), oc.height());
59                 addPin(input);
60                 addPin(output);
61                 addPin(enable);
62         }
63
64         public final Pin getInputPin()
65         {
66                 return input;
67         }
68
69         public final Pin getOutputPin()
70         {
71                 return output;
72         }
73
74         public final Pin getEnablePin()
75         {
76                 return enable;
77         }
78
79         @Override
80         public void render(GeneralGC gc, Rectangle visibleRegion)
81         {
82                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
83                 if (foreground != null)
84                         gc.setForeground(foreground);
85                 double x = getPosX();
86                 double y = getPosY();
87                 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
88         }
89
90         @Override
91         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
92         {
93                 return new Gson().toJsonTree(params);
94         }
95
96         static
97         {
98                 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
99                 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
100                 {
101                         GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
102                         if (params == null)
103                                 throw new JsonSyntaxException("Invalid!!!");
104                         return new GUITriStateBuffer(m, params, n);
105                 });
106         }
107
108         public static class GUITriStateBufferParams
109         {
110                 int logicWidth;
111                 Orientation orientation;
112
113                 public GUITriStateBufferParams(int logicWidth, Orientation orientation)
114                 {
115                         this.logicWidth = logicWidth;
116                         this.orientation = orientation;
117                 }
118         }
119 }