Implemented GUIManualSwitch
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 28 May 2019 21:40:31 +0000 (23:40 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 28 May 2019 21:40:31 +0000 (23:40 +0200)
LogicUI/oldsrc/GUIManualSwitch.java [deleted file]
LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java [new file with mode: 0644]

diff --git a/LogicUI/oldsrc/GUIManualSwitch.java b/LogicUI/oldsrc/GUIManualSwitch.java
deleted file mode 100644 (file)
index 318c3f0..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-package era.mi.gui.components;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import era.mi.logic.components.ManualSwitch;
-import era.mi.logic.types.Bit;
-import era.mi.logic.wires.Wire.ReadEnd;
-import era.mi.logic.wires.Wire.ReadWriteEnd;
-import net.haspamelodica.swt.helper.gcs.GeneralGC;
-import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
-import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
-import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
-
-public class GUIManualSwitch extends ManualSwitch implements GUIComponent
-{
-       private static final Map<Bit, String> bitNames;
-       static
-       {
-               Map<Bit, String> bitNamesModifiable = new HashMap<>();
-               bitNamesModifiable.put(Bit.ONE, "1");
-               bitNamesModifiable.put(Bit.ZERO, "0");
-               bitNamesModifiable.put(Bit.Z, "Z");
-               bitNamesModifiable.put(Bit.U, "U");
-               bitNamesModifiable.put(Bit.X, "X");
-               bitNames = Collections.unmodifiableMap(bitNamesModifiable);
-       }
-
-       private final ReadEnd we;
-       private final List<ReadEnd> connectedWireEnds;
-       private final List<Point> wireEndConnectionPoints;
-
-       public GUIManualSwitch(ReadWriteEnd output)
-       {
-               super(output);
-
-               this.we = output;
-
-               List<ReadEnd> connectedWireEndsModifiable = new ArrayList<>();
-               List<Point> wireEndConnectionPointsModifiable = new ArrayList<>();
-
-               connectedWireEndsModifiable.add(output);
-               wireEndConnectionPointsModifiable.add(new Point(20, 7.5));
-
-               this.connectedWireEnds = Collections.unmodifiableList(connectedWireEndsModifiable);
-               this.wireEndConnectionPoints = Collections.unmodifiableList(wireEndConnectionPointsModifiable);
-       }
-
-       @Override
-       public Rectangle getBounds()
-       {
-               return new Rectangle(0, 0, 20, 15);
-       }
-
-       @Override
-       public void render(GeneralGC gc)
-       {
-               gc.drawRectangle(0, 0, 20, 15);
-               String label = bitNames.get(we.getValue());
-               Font oldFont = gc.getFont();
-               Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle());
-               gc.setFont(labelFont);
-               Point textExtent = gc.textExtent(label);
-               gc.drawText(label, 10 - textExtent.x / 2, 7.5 - textExtent.y / 2, true);
-               gc.setFont(oldFont);
-       }
-
-       @Override
-       public boolean clicked(double x, double y)
-       {
-               toggle();
-               return true;
-       }
-
-       @Override
-       public int getConnectedWireEndsCount()
-       {
-               return connectedWireEnds.size();
-       }
-
-       @Override
-       public ReadEnd getConnectedWireEnd(int connectionIndex)
-       {
-               return connectedWireEnds.get(connectionIndex);
-       }
-
-       @Override
-       public Point getWireEndConnectionPoint(int connectionI)
-       {
-               return wireEndConnectionPoints.get(connectionI);
-       }
-}
\ No newline at end of file
diff --git a/LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java b/LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java
new file mode 100644 (file)
index 0000000..870a55a
--- /dev/null
@@ -0,0 +1,56 @@
+package era.mi.gui.model.components;
+
+import era.mi.gui.model.ViewModel;
+import era.mi.gui.model.wires.Pin;
+import era.mi.logic.components.ManualSwitch;
+import era.mi.logic.types.BitVectorFormatter;
+import era.mi.logic.wires.Wire.ReadEnd;
+import net.haspamelodica.swt.helper.gcs.GeneralGC;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
+import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
+
+public class GUIManualSwitch extends GUIComponent
+{
+       private static final double width = 20;
+       private static final double height = 15;
+       private static final double fontHeight = 5;
+
+       private ManualSwitch logicSwitch;
+       private ReadEnd end;
+
+       public GUIManualSwitch(ViewModel model)
+       {
+               super(model);
+               setSize(width, height);
+               addPin(new Pin(this, width, height / 2));
+       }
+
+       @Override
+       public void render(GeneralGC gc, Rectangle visibleRegion)
+       {
+               gc.drawRectangle(0, 0, width, height);
+               String label = BitVectorFormatter.formatValueAsString(end);
+               Font oldFont = gc.getFont();
+               Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle());
+               gc.setFont(labelFont);
+               Point textExtent = gc.textExtent(label);
+               gc.drawText(label, (width - textExtent.x) / 2, (height - textExtent.y) / 2, true);
+               gc.setFont(oldFont);
+       }
+
+       public void setLogicModelBinding(ManualSwitch logicSwitch, ReadEnd end)
+       {
+               this.logicSwitch = logicSwitch;
+               this.end = end;
+               // TODO when ManualSwitch supports it, add listeners
+               end.addObserver((i, o) -> callComponentChangedListeners());
+       }
+
+       @Override
+       public boolean clicked(double x, double y)
+       {
+               logicSwitch.toggle();
+               return true;
+       }
+}
\ No newline at end of file