Vastly improved orientation calculation and implementation
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUITriStateBuffer.java
index 7517808..c9036d2 100644 (file)
@@ -2,17 +2,18 @@ package net.mograsim.logic.model.model.components.atomic;
 
 import org.eclipse.swt.graphics.Color;
 
+import com.google.gson.Gson;
 import com.google.gson.JsonElement;
-import com.google.gson.JsonObject;
-import com.google.gson.JsonParseException;
-import com.google.gson.JsonPrimitive;
 import com.google.gson.JsonSyntaxException;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.model.components.Orientation;
+import net.mograsim.logic.model.model.components.OrientationCalculator;
 import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.modeladapter.componentadapters.TriStateBufferAdapter;
 import net.mograsim.logic.model.serializing.IdentifierGetter;
@@ -27,37 +28,34 @@ public class GUITriStateBuffer extends GUIComponent
        private Pin input;
        private Pin output;
        private Pin enable;
-       private Orientation orientation;
        private double[] path;
 
-       public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation)
+       private GUITriStateBufferParams params;
+       private OrientationCalculator oc;
+
+       public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params)
        {
-               this(model, logicWidth, orientation, null);
+               this(model, params, null);
        }
 
-       public GUITriStateBuffer(ViewModelModifiable model, int logicWidth, Orientation orientation, String name)
+       public GUITriStateBuffer(ViewModelModifiable model, GUITriStateBufferParams params, String name)
        {
                super(model, name);
-               this.orientation = orientation;
+               this.params = params;
+
+               oc = new OrientationCalculator(params.orientation, width, height);
 
                double wHalf = width / 2;
                double hHalf = height / 2;
-               double wQuar = width / 4;
                double hQuar = height / 4;
-               int ordi = orientation.ordinal();
-               int isVerti = (ordi % 4) / 2;
-               int isHori = 1 ^ isVerti;
-               int isAlt = ordi / 4;
-               int isInv = ordi % 2;
-               int isStd = 1 ^ isInv;
-
-               this.input = new Pin(this, "IN", logicWidth, width * isInv * isHori + wHalf * isVerti, height * isVerti * isStd + hHalf * isHori);
-               this.output = new Pin(this, "OUT", logicWidth, width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori);
-               this.enable = new Pin(this, "EN", 1, wQuar * isVerti + wHalf * (isAlt | isHori), hQuar * isHori + hHalf * (isAlt | isVerti));
-               this.path = new double[] { width * (isStd ^ isHori), height * (isStd ^ isHori), width * isInv, height * isStd,
-                               width * isStd * isHori + wHalf * isVerti, height * isVerti * isInv + hHalf * isHori };
-
-               setSize(width, height);
+
+               this.input = new Pin(this, "IN", params.logicWidth, PinUsage.INPUT, oc.newX(0, hHalf), oc.newY(0, hHalf));
+               this.output = new Pin(this, "OUT", params.logicWidth, PinUsage.OUTPUT, oc.newX(width, hHalf), oc.newY(width, hHalf));
+               this.enable = new Pin(this, "EN", 1, PinUsage.INPUT, oc.newX(wHalf, hQuar), oc.newY(wHalf, hQuar));
+               this.path = new double[] { oc.newX(0, 0), oc.newY(0, 0), oc.newX(width, hHalf), oc.newY(width, hHalf), oc.newX(0, height),
+                               oc.newY(0, height) };
+
+               setSize(oc.width(), oc.height());
                addPin(input);
                addPin(output);
                addPin(enable);
@@ -72,24 +70,12 @@ public class GUITriStateBuffer extends GUIComponent
                double x = getPosX();
                double y = getPosY();
                gc.drawPolygon(new double[] { x + path[0], y + path[1], x + path[2], y + path[3], x + path[4], y + path[5] });
-//             Font oldFont = gc.getFont();
-//             Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
-//             gc.setFont(labelFont);
-//             Point textExtent = gc.textExtent(label);
-//             Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
-//             if (textColor != null)
-//                     gc.setForeground(textColor);
-//             gc.drawText(label, getPosX() + (rectWidth - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true);
-//             gc.setFont(oldFont);
        }
 
        @Override
        public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
        {
-               JsonObject jo = new JsonObject();
-               jo.addProperty("logicWidth", input.logicWidth);
-               jo.addProperty("orientation", orientation.name());
-               return jo;
+               return new Gson().toJsonTree(params);
        }
 
        static
@@ -97,16 +83,22 @@ public class GUITriStateBuffer extends GUIComponent
                ViewLogicModelAdapter.addComponentAdapter(new TriStateBufferAdapter());
                IndirectGUIComponentCreator.setComponentSupplier(GUITriStateBuffer.class.getName(), (m, p, n) ->
                {
-                       if (!p.isJsonObject())
-                               throw new JsonSyntaxException("TriStateBuffer Params are not a JsonObject");
-                       JsonObject jo = p.getAsJsonObject();
-                       return new GUITriStateBuffer(m, jo.getAsJsonPrimitive("logicWidth").getAsInt(),
-                                       Orientation.valueOf(jo.getAsJsonPrimitive("orientation").getAsString()), n);
+                       GUITriStateBufferParams params = new Gson().fromJson(p, GUITriStateBufferParams.class);
+                       if (params == null)
+                               throw new JsonSyntaxException("Invalid!!!");
+                       return new GUITriStateBuffer(m, params, n);
                });
        }
 
-       public enum Orientation
+       public static class GUITriStateBufferParams
        {
-               RIGHT, LEFT, UP, DOWN, RIGHT_ALT, LEFT_ALT, UP_ALT, DOWN_ALT;
+               int logicWidth;
+               Orientation orientation;
+
+               public GUITriStateBufferParams(int logicWidth, Orientation orientation)
+               {
+                       this.logicWidth = logicWidth;
+                       this.orientation = orientation;
+               }
        }
 }