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