Added GUI component for TriStateBuffer
[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.JsonElement;
6 import com.google.gson.JsonObject;
7 import com.google.gson.JsonParseException;
8 import com.google.gson.JsonPrimitive;
9 import com.google.gson.JsonSyntaxException;
10
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.model.model.ViewModelModifiable;
14 import net.mograsim.logic.model.model.components.GUIComponent;
15 import net.mograsim.logic.model.model.wires.Pin;
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
17 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
18 import net.mograsim.logic.model.serializing.IdentifierGetter;
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
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 Orientation orientation;
31         private double[] path;
32
33         public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation)
34         {
35                 this(model, logicWidth, orientation, null);
36         }
37
38         public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation, String name)
39         {
40                 super(model, name);
41                 this.orientation = orientation;
42
43                 double wHalf = width / 2;
44                 double hHalf = height / 2;
45                 double wQuar = width / 4;
46                 double hQuar = height / 4;
47                 int ordi = orientation.ordinal();
48                 int isVerti = (ordi % 4) / 2;
49                 int isHori = 1 ^ isVerti;
50                 int isAlt = ordi / 4;
51                 int isInv = ordi % 2;
52                 int isStd = 1 ^ isInv;
53
54                 this.input = new Pin(this, "IN", logicWidth, width * isInv * isHori + wHalf * isVerti, height * isVerti * isStd + hHalf * isHori);
55                 this.output = new Pin(this, "OUT", logicWidth, width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori);
56                 this.enable = new Pin(this, "EN", 1, wQuar * isVerti + wHalf * (isAlt | isHori), hQuar * isHori + hHalf * (isAlt | isVerti));
57                 this.path = new double[] { width * (isStd ^ isHori), height * (isStd ^ isHori), width * isInv, height * isStd,
58                                 width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori };
59
60                 setSize(width, height);
61                 addPin(input);
62                 addPin(output);
63                 addPin(enable);
64         }
65
66         @Override
67         public void render(GeneralGC gc, Rectangle visibleRegion)
68         {
69                 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
70                 if (foreground != null)
71                         gc.setForeground(foreground);
72                 double x = getPosX();
73                 double y = getPosY();
74                 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
75 //              Font oldFont = gc.getFont();
76 //              Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
77 //              gc.setFont(labelFont);
78 //              Point textExtent = gc.textExtent(label);
79 //              Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
80 //              if (textColor != null)
81 //                      gc.setForeground(textColor);
82 //              gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
83 //              gc.setFont(oldFont);
84         }
85
86         @Override
87         public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
88         {
89                 JsonObject jo = new JsonObject();
90                 jo.addProperty("logicWidth", input.logicWidth);
91                 jo.addProperty("orientation", orientation.name());
92                 return jo;
93         }
94
95         static
96         {
97                 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
98                 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
99                 {
100                         if (!p.isJsonObject())
101                                 throw new JsonSyntaxException("TriStateBuffer Params are not a JsonObject");
102                         JsonObject jo = p.getAsJsonObject();
103                         return new GUITriStateBuffer(m, jo.getAsJsonPrimitive("logicWidth").getAsInt(),
104                                         Orientation.valueOf(jo.getAsJsonPrimitive("orientation").getAsString()), n);
105                 });
106         }
107
108         public enum Orientation
109         {
110                 RIGHT, LEFT, UP, DOWN, RIGHT_ALT, LEFT_ALT, UP_ALT, DOWN_ALT;
111         }
112 }