Fixed GUIMerger / GUISplitter bit order
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / GUISplitter.java
index 1dc11d1..6ddc276 100644 (file)
@@ -1,5 +1,10 @@
 package net.mograsim.logic.model.model.components.atomic;
 
+import org.eclipse.swt.SWT;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.core.types.BitVectorFormatter;
@@ -9,29 +14,32 @@ import net.mograsim.logic.model.model.components.GUIComponent;
 import net.mograsim.logic.model.model.wires.Pin;
 import net.mograsim.logic.model.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.model.modeladapter.componentadapters.SplitterAdapter;
+import net.mograsim.logic.model.serializing.IdentifierGetter;
+import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
 import net.mograsim.preferences.ColorDefinition;
 import net.mograsim.preferences.ColorManager;
 import net.mograsim.preferences.Preferences;
 
 public class GUISplitter extends GUIComponent
 {
-       private static final double width = 20;
+       private static final double width = 10;
        private static final double heightPerPin = 10;
 
        public final int logicWidth;
 
        private ReadEnd inputEnd;
-       private ReadEnd[] outputEnds;
+       private final ReadEnd[] outputEnds;
 
        public GUISplitter(ViewModelModifiable model, int logicWidth, String name)
        {
                super(model, name);
                this.logicWidth = logicWidth;
                setSize(width, logicWidth * heightPerPin);
-               addPin(new Pin(this, "I", logicWidth, 0, logicWidth * heightPerPin / 2));
+               addPin(new Pin(this, "I", logicWidth, 0, (logicWidth - 1) * heightPerPin / 2));
                double outputHeight = 0;
                for (int i = 0; i < logicWidth; i++, outputHeight += 10)
                        addPin(new Pin(this, "O" + i, 1, width, outputHeight));
+               outputEnds = new ReadEnd[logicWidth];
        }
 
        @Override
@@ -43,9 +51,8 @@ public class GUISplitter extends GUIComponent
                ColorDefinition c = BitVectorFormatter.formatAsColor(inputEnd);
                if (c != null)
                        gc.setForeground(ColorManager.current().toColor(c));
-               gc.drawLine(posX, posY + heightPerPin * logicWidth / 2, posX + width / 2, posY + heightPerPin * logicWidth / 2);
-               gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
-               gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
+               double inLineY = posY + (logicWidth - 1) * heightPerPin / 2;
+               gc.drawLine(posX, inLineY, posX + width / 2, inLineY);
                double outputHeight = posY;
                for (int i = 0; i < logicWidth; i++, outputHeight += 10)
                {
@@ -54,16 +61,31 @@ public class GUISplitter extends GUIComponent
                                gc.setForeground(ColorManager.current().toColor(c));
                        gc.drawLine(posX + width / 2, outputHeight, posX + width, outputHeight);
                }
+               gc.setForeground(Preferences.current().getColor("net.mograsim.logic.model.color.foreground"));
+               int oldLineCap = gc.getLineCap();
+               int lineJoin = gc.getLineJoin();
+               // TODO find better "replacement" for JOIN_BEVEL
+               gc.setLineCap(lineJoin == SWT.JOIN_MITER ? SWT.CAP_SQUARE : lineJoin == SWT.JOIN_ROUND ? SWT.CAP_ROUND : SWT.CAP_SQUARE);
+               gc.drawLine(posX + width / 2, posY, posX + width / 2, posY + heightPerPin * (logicWidth - 1));
+               gc.setLineCap(oldLineCap);
+       }
+
+       @Override
+       public JsonElement getParamsForSerializing(IdentifierGetter idGetter)
+       {
+               return new JsonPrimitive(logicWidth);
        }
 
        public void setLogicModelBinding(ReadEnd inputEnd, ReadEnd[] outputEnds)
        {
                this.inputEnd = inputEnd;
-               this.outputEnds = outputEnds;
+               System.arraycopy(outputEnds, 0, this.outputEnds, 0, logicWidth);
        }
 
        static
        {
                ViewLogicModelAdapter.addComponentAdapter(new SplitterAdapter());
+               IndirectGUIComponentCreator.setComponentSupplier(GUISplitter.class.getCanonicalName(),
+                               (m, p, n) -> new GUISplitter(m, p.getAsInt(), n));
        }
 }
\ No newline at end of file