1 package net.mograsim.logic.model.model.components.atomic;
\r
3 import org.eclipse.swt.graphics.Color;
\r
5 import com.google.gson.JsonElement;
\r
6 import com.google.gson.JsonObject;
\r
7 import com.google.gson.JsonParseException;
\r
8 import com.google.gson.JsonPrimitive;
\r
9 import com.google.gson.JsonSyntaxException;
\r
11 import net.haspamelodica.swt.helper.gcs.GeneralGC;
\r
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
\r
13 import net.mograsim.logic.model.model.ViewModelModifiable;
\r
14 import net.mograsim.logic.model.model.components.GUIComponent;
\r
15 import net.mograsim.logic.model.model.wires.Pin;
\r
16 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
\r
17 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
\r
18 import net.mograsim.logic.model.serializing.IdentifierGetter;
\r
19 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
\r
20 import net.mograsim.preferences.Preferences;
\r
22 public class GUITriStateBuffer extends GUIComponent
\r
25 private static final double width = 20;
\r
26 private static final double height = 20;
\r
30 private Orientation orientation;
\r
31 private double[] path;
\r
33 public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation)
\r
35 this(model, logicWidth, orientation, null);
\r
38 public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation, String name)
\r
41 this.orientation = orientation;
\r
43 double wHalf = width / 2;
\r
44 double hHalf = height / 2;
\r
45 double wQuar = width / 4;
\r
46 double hQuar = height / 4;
\r
47 int ordi = orientation.ordinal();
\r
48 int isVerti = (ordi % 4) / 2;
\r
49 int isHori = 1 ^ isVerti;
\r
50 int isAlt = ordi / 4;
\r
51 int isInv = ordi % 2;
\r
52 int isStd = 1 ^ isInv;
\r
54 this.input = new Pin(this, "IN", logicWidth, width * isInv * isHori + wHalf * isVerti, height * isVerti * isStd + hHalf * isHori);
\r
55 this.output = new Pin(this, "OUT", logicWidth, width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori);
\r
56 this.enable = new Pin(this, "EN", 1, wQuar * isVerti + wHalf * (isAlt | isHori), hQuar * isHori + hHalf * (isAlt | isVerti));
\r
57 this.path = new double[] { width * (isStd ^ isHori), height * (isStd ^ isHori), width * isInv, height * isStd,
\r
58 width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori };
\r
60 setSize(width, height);
\r
67 public void render(GeneralGC gc, Rectangle visibleRegion)
\r
69 Color foreground = Preferences.current().getColor("net.mograsim.logic.model.color.foreground");
\r
70 if (foreground != null)
\r
71 gc.setForeground(foreground);
\r
72 double x = getPosX();
\r
73 double y = getPosY();
\r
74 gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
\r
75 // Font oldFont = gc.getFont();
\r
76 // Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
\r
77 // gc.setFont(labelFont);
\r
78 // Point textExtent = gc.textExtent(label);
\r
79 // Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
\r
80 // if (textColor != null)
\r
81 // gc.setForeground(textColor);
\r
82 // gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
\r
83 // gc.setFont(oldFont);
\r
87 public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
\r
89 JsonObject jo = new JsonObject();
\r
90 jo.addProperty("logicWidth", input.logicWidth);
\r
91 jo.addProperty("orientation", orientation.name());
\r
97 ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
\r
98 IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
\r
100 if (!p.isJsonObject())
\r
101 throw new JsonSyntaxException("TriStateBuffer Params are not a JsonObject");
\r
102 JsonObject jo = p.getAsJsonObject();
\r
103 return new GUITriStateBuffer(m, jo.getAsJsonPrimitive("logicWidth").getAsInt(),
\r
104 Orientation.valueOf(jo.getAsJsonPrimitive("orientation").getAsString()), n);
\r
108 public enum Orientation
\r
110 RIGHT, LEFT, UP, DOWN, RIGHT_ALT, LEFT_ALT, UP_ALT, DOWN_ALT;
\r