8900b6e00c2797b54e003859c240c95bb6b0a0d8
[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.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.ViewModelModifiable;
10 import net.mograsim.logic.model.model.components.GUIComponent;
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.ViewLogicModelAdapter;
16 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
17 import net.mograsim.logic.model.serializing.IdentifierGetter;
18 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
19 import net.mograsim.logic.model.util.JsonHandler;
20 import net.mograsim.preferences.Preferences;
21
22 public class GUITriStateBuffer extends GUIComponent
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 GUITriStateBufferParams params;
33         private OrientationCalculator oc;
34
35         public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params)
36         {
37                 this(model, params, null);
38         }
39
40         public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params, String name)
41         {
42                 super(model, name);
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(this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
52                 this.output = new Pin(this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
53                 this.enable = new Pin(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
63         public final Pin getInputPin()
64         {
65                 return input;
66         }
67
68         public final Pin getOutputPin()
69         {
70                 return output;
71         }
72
73         public final Pin getEnablePin()
74         {
75                 return enable;
76         }
77
78         @Override
79         public void render(GeneralGC gc, Rectangle visibleRegion)
80         {
81                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
82                 if (foreground != null)
83                         gc.setForeground(foreground);
84                 double x = getPosX();
85                 double y = getPosY();
86                 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
87         }
88
89         @Override
90         public GUITriStateBufferParams getParamsForSerializing(IdentifierGetter idGetter)
91         {
92                 return params;
93         }
94
95         static
96         {
97                 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
98                 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
99                 {
100                         GUITriStateBufferParams params = JsonHandler.fromJsonTree(p, GUITriStateBufferParams.class);
101                         if (params == null)
102                                 throw new JsonSyntaxException("Invalid!!!");
103                         return new GUITriStateBuffer(m, params, n);
104                 });
105         }
106
107         public static class GUITriStateBufferParams
108         {
109                 int logicWidth;
110                 Orientation orientation;
111
112                 public GUITriStateBufferParams(int logicWidth, Orientation orientation)
113                 {
114                         this.logicWidth = logicWidth;
115                         this.orientation = orientation;
116                 }
117         }
118 }