Vastly improved orientation calculation and implementation
[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         @Override
65         public void render(GeneralGC gc, Rectangle visibleRegion)
66         {
67                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
68                 if (foreground != null)
69                         gc.setForeground(foreground);
70                 double x = getPosX();
71                 double y = getPosY();
72                 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
73         }
74
75         @Override
76         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
77         {
78                 return new Gson().toJsonTree(params);
79         }
80
81         static
82         {
83                 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
84                 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
85                 {
86                         GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
87                         if (params == null)
88                                 throw new JsonSyntaxException("Invalid!!!");
89                         return new GUITriStateBuffer(m, params, n);
90                 });
91         }
92
93         public static class GUITriStateBufferParams
94         {
95                 int logicWidth;
96                 Orientation orientation;
97
98                 public GUITriStateBufferParams(int logicWidth, Orientation orientation)
99                 {
100                         this.logicWidth = logicWidth;
101                         this.orientation = orientation;
102                 }
103         }
104 }