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