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