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