Restructured the Preferences system
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelTriStateBuffer.java
1 package net.mograsim.logic.model.model.components.atomic;
2
3 import static net.mograsim.logic.model.preferences.RenderPreferences.FOREGROUND_COLOR;
4
5 import org.eclipse.swt.graphics.Color;
6
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.LogicModelModifiable;
12 import net.mograsim.logic.model.model.components.ModelComponent;
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.LogicCoreAdapter;
18 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
19 import net.mograsim.logic.model.preferences.RenderPreferences;
20 import net.mograsim.logic.model.serializing.IdentifyParams;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.logic.model.util.JsonHandler;
23
24 public class ModelTriStateBuffer extends ModelComponent
25 {
26
27         private static final double width = 20;
28         private static final double height = 20;
29         private Pin input;
30         private Pin output;
31         private Pin enable;
32         private double[] path;
33
34         private ModelTriStateBufferParams params;
35         private OrientationCalculator oc;
36
37         public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params)
38         {
39                 this(model, params, null);
40         }
41
42         public ModelTriStateBuffer(LogicModelModifiable model, ModelTriStateBufferParams params, String name)
43         {
44                 super(model, name, false);
45                 this.params = params;
46
47                 oc = new OrientationCalculator(params.orientation, width, height);
48
49                 double wHalf = width / 2;
50                 double hHalf = height / 2;
51                 double hQuar = height / 4;
52
53                 this.input = new Pin(model, this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
54                 this.output = new Pin(model, this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
55                 this.enable = new Pin(model, this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
56                 this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
57                                 oc.newY(0, height) };
58
59                 setSize(oc.width(), oc.height());
60                 addPin(input);
61                 addPin(output);
62                 addPin(enable);
63
64                 init();
65         }
66
67         public final Pin getInputPin()
68         {
69                 return input;
70         }
71
72         public final Pin getOutputPin()
73         {
74                 return output;
75         }
76
77         public final Pin getEnablePin()
78         {
79                 return enable;
80         }
81
82         @Override
83         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
84         {
85                 Color foreground = renderPrefs.getColor(FOREGROUND_COLOR);
86                 if (foreground != null)
87                         gc.setForeground(foreground);
88                 double x = getPosX();
89                 double y = getPosY();
90                 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
91         }
92
93         @Override
94         public String getIDForSerializing(IdentifyParams idParams)
95         {
96                 return "TriStateBuffer";
97         }
98
99         @Override
100         public ModelTriStateBufferParams getParamsForSerializing(IdentifyParams idParams)
101         {
102                 return params;
103         }
104
105         static
106         {
107                 LogicCoreAdapter.addComponentAdapter(new TriStateBufferAdapter());
108                 IndirectModelComponentCreator.setComponentSupplier(ModelTriStateBuffer.class.getName(), (m, p, n) ->
109                 {
110                         ModelTriStateBufferParams params = JsonHandler.fromJsonTree(p, ModelTriStateBufferParams.class);
111                         if (params == null)
112                                 throw new JsonSyntaxException("Invalid!!!");
113                         return new ModelTriStateBuffer(m, params, n);
114                 });
115         }
116
117         public static class ModelTriStateBufferParams
118         {
119                 int logicWidth;
120                 Orientation orientation;
121
122                 public ModelTriStateBufferParams(int logicWidth, Orientation orientation)
123                 {
124                         this.logicWidth = logicWidth;
125                         this.orientation = orientation;
126                 }
127         }
128 }