From: Daniel Kirschten Date: Wed, 29 May 2019 13:24:49 +0000 (+0200) Subject: Added concept of wire width to GUIComponents and GUIWires X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=8e290cad73b372b954f7677f2287c6a9eb9a6313;p=Mograsim.git Added concept of wire width to GUIComponents and GUIWires --- diff --git a/LogicUI/src/era/mi/gui/examples/Playground.java b/LogicUI/src/era/mi/gui/examples/Playground.java index 78524b7d..372381a4 100644 --- a/LogicUI/src/era/mi/gui/examples/Playground.java +++ b/LogicUI/src/era/mi/gui/examples/Playground.java @@ -25,9 +25,9 @@ public class Playground public static void addComponentsAndWires(LogicUIStandalone ui, ViewModel model) { - GUIAndGate andGate = new GUIAndGate(model); + GUIAndGate andGate = new GUIAndGate(model, 1); andGate.moveTo(10, 10); - GUINotGate notGate = new GUINotGate(model); + GUINotGate notGate = new GUINotGate(model, 1); notGate.moveTo(10, 40); new GUIWire(model, andGate.getPins().get(0), notGate.getPins().get(1), new Point(20, 50)); diff --git a/LogicUI/src/era/mi/gui/model/components/GUIAndGate.java b/LogicUI/src/era/mi/gui/model/components/GUIAndGate.java index 4f69d36d..5e5c787a 100644 --- a/LogicUI/src/era/mi/gui/model/components/GUIAndGate.java +++ b/LogicUI/src/era/mi/gui/model/components/GUIAndGate.java @@ -2,11 +2,11 @@ package era.mi.gui.model.components; import era.mi.gui.model.ViewModel; -public class GUIAndGate extends RectangularShapedGUIGate +public class GUIAndGate extends SimpleRectangularGUIGate { - public GUIAndGate(ViewModel model) + public GUIAndGate(ViewModel model, int logicWidth) { - super(model, "&", false); + super(model, logicWidth, "&", false); setInputCount(2); } } \ 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 index 546b17e2..6008329c 100644 --- a/LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java +++ b/LogicUI/src/era/mi/gui/model/components/GUIManualSwitch.java @@ -23,7 +23,7 @@ public class GUIManualSwitch extends GUIComponent { super(model); setSize(width, height); - addPin(new Pin(this, width, height / 2)); + addPin(new Pin(this, 1, width, height / 2)); } @Override diff --git a/LogicUI/src/era/mi/gui/model/components/GUINotGate.java b/LogicUI/src/era/mi/gui/model/components/GUINotGate.java index 5f41592b..c22d9b28 100644 --- a/LogicUI/src/era/mi/gui/model/components/GUINotGate.java +++ b/LogicUI/src/era/mi/gui/model/components/GUINotGate.java @@ -2,11 +2,11 @@ package era.mi.gui.model.components; import era.mi.gui.model.ViewModel; -public class GUINotGate extends RectangularShapedGUIGate +public class GUINotGate extends SimpleRectangularGUIGate { - public GUINotGate(ViewModel model) + public GUINotGate(ViewModel model, int logicWidth) { - super(model, "1", true); + super(model, logicWidth, "1", true); setInputCount(1); } } \ No newline at end of file diff --git a/LogicUI/src/era/mi/gui/model/components/GUIOrGate.java b/LogicUI/src/era/mi/gui/model/components/GUIOrGate.java index df64bba1..a732509b 100644 --- a/LogicUI/src/era/mi/gui/model/components/GUIOrGate.java +++ b/LogicUI/src/era/mi/gui/model/components/GUIOrGate.java @@ -2,11 +2,11 @@ package era.mi.gui.model.components; import era.mi.gui.model.ViewModel; -public class GUIOrGate extends RectangularShapedGUIGate +public class GUIOrGate extends SimpleRectangularGUIGate { - public GUIOrGate(ViewModel model) + public GUIOrGate(ViewModel model, int logicWidth) { - super(model, "\u22651", false);// ">=1" + super(model, logicWidth, "\u22651", false);// ">=1" setInputCount(2); } } \ No newline at end of file diff --git a/LogicUI/src/era/mi/gui/model/components/RectangularShapedGUIGate.java b/LogicUI/src/era/mi/gui/model/components/RectangularShapedGUIGate.java deleted file mode 100644 index c7d0b346..00000000 --- a/LogicUI/src/era/mi/gui/model/components/RectangularShapedGUIGate.java +++ /dev/null @@ -1,74 +0,0 @@ -package era.mi.gui.model.components; - -import java.util.ArrayList; -import java.util.List; - -import era.mi.gui.model.ViewModel; -import era.mi.gui.model.wires.MovablePin; -import era.mi.gui.model.wires.Pin; -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 RectangularShapedGUIGate extends GUIComponent -{ - private static final double width = 20; - private static final double pinDistance = 10; - private static final double fontHeight = 5; - private static final double invertedCircleDiam = 3.5; - - private final String label; - private final boolean isInverted; - private final double rectWidth; - - private MovablePin outputPin; - private final List inputPins; - - protected RectangularShapedGUIGate(ViewModel model, String label, boolean isInverted) - { - super(model); - this.label = label; - this.isInverted = isInverted; - this.rectWidth = width - (isInverted ? invertedCircleDiam : 0); - this.outputPin = new MovablePin(this, width, 0); - addPin(outputPin); - this.inputPins = new ArrayList<>(); - setInputCount(1); - } - - protected void setInputCount(int inputCount) - { - int oldInputCount = inputPins.size(); - setSize(width, inputCount * pinDistance); - if (oldInputCount > inputCount) - while (inputPins.size() > inputCount) - removePin(inputPins.get(inputCount)); - else if (oldInputCount < inputCount) - for (int i = oldInputCount; i < inputCount; i++) - { - Pin pin = new Pin(this, 0, pinDistance / 2 + i * pinDistance); - inputPins.add(pin); - addPin(pin); - } - outputPin.setRelPos(width, inputCount * pinDistance / 2); - } - - @Override - public void render(GeneralGC gc, Rectangle visibleRegion) - { - double posX = getBounds().x; - double posY = getBounds().y; - - double height = inputPins.size() * pinDistance; - gc.drawRectangle(posX, posY, rectWidth, height); - Font oldFont = gc.getFont(); - Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); - gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); - gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true); - gc.setFont(oldFont); - if (isInverted) - gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam); - } -} \ No newline at end of file diff --git a/LogicUI/src/era/mi/gui/model/components/SimpleRectangularGUIGate.java b/LogicUI/src/era/mi/gui/model/components/SimpleRectangularGUIGate.java new file mode 100644 index 00000000..6edf1647 --- /dev/null +++ b/LogicUI/src/era/mi/gui/model/components/SimpleRectangularGUIGate.java @@ -0,0 +1,76 @@ +package era.mi.gui.model.components; + +import java.util.ArrayList; +import java.util.List; + +import era.mi.gui.model.ViewModel; +import era.mi.gui.model.wires.MovablePin; +import era.mi.gui.model.wires.Pin; +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 SimpleRectangularGUIGate extends GUIComponent +{ + private static final double width = 20; + private static final double pinDistance = 10; + private static final double fontHeight = 5; + private static final double invertedCircleDiam = 3.5; + + private final String label; + protected final int logicWidth; + private final boolean isInverted; + private final double rectWidth; + + private MovablePin outputPin; + private final List inputPins; + + protected SimpleRectangularGUIGate(ViewModel model, int logicWidth, String label, boolean isInverted) + { + super(model); + this.label = label; + this.logicWidth = logicWidth; + this.isInverted = isInverted; + this.rectWidth = width - (isInverted ? invertedCircleDiam : 0); + this.outputPin = new MovablePin(this, logicWidth, width, 0); + addPin(outputPin); + this.inputPins = new ArrayList<>(); + setInputCount(1); + } + + protected void setInputCount(int inputCount) + { + int oldInputCount = inputPins.size(); + setSize(width, inputCount * pinDistance); + if (oldInputCount > inputCount) + while (inputPins.size() > inputCount) + removePin(inputPins.get(inputCount)); + else if (oldInputCount < inputCount) + for (int i = oldInputCount; i < inputCount; i++) + { + Pin pin = new Pin(this, logicWidth, 0, pinDistance / 2 + i * pinDistance); + inputPins.add(pin); + addPin(pin); + } + outputPin.setRelPos(width, inputCount * pinDistance / 2); + } + + @Override + public void render(GeneralGC gc, Rectangle visibleRegion) + { + double posX = getBounds().x; + double posY = getBounds().y; + + double height = inputPins.size() * pinDistance; + gc.drawRectangle(posX, posY, rectWidth, height); + Font oldFont = gc.getFont(); + Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); + gc.setFont(labelFont); + Point textExtent = gc.textExtent(label); + gc.drawText(label, posX + (rectWidth - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true); + gc.setFont(oldFont); + if (isInverted) + gc.drawOval(posX + rectWidth, posY + (height - invertedCircleDiam) / 2, invertedCircleDiam, invertedCircleDiam); + } +} \ No newline at end of file diff --git a/LogicUI/src/era/mi/gui/model/wires/GUIWire.java b/LogicUI/src/era/mi/gui/model/wires/GUIWire.java index 0f7ff086..c8fd5740 100644 --- a/LogicUI/src/era/mi/gui/model/wires/GUIWire.java +++ b/LogicUI/src/era/mi/gui/model/wires/GUIWire.java @@ -14,6 +14,7 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Point; public class GUIWire { private final ViewModel model; + public final int logicWidth; private Pin pin1; private Pin pin2; private double[] path; @@ -25,6 +26,9 @@ public class GUIWire public GUIWire(ViewModel model, Pin pin1, Pin pin2, Point... path) { this.model = model; + this.logicWidth = pin1.logicWidth; + if (pin2.logicWidth != pin1.logicWidth) + throw new IllegalArgumentException("Can't connect pins of different logic width"); this.path = new double[path.length * 2 + 4]; for (int srcI = 0, dstI = 2; srcI < path.length; srcI++, dstI += 2) { diff --git a/LogicUI/src/era/mi/gui/model/wires/MovablePin.java b/LogicUI/src/era/mi/gui/model/wires/MovablePin.java index 673b2578..949e7d76 100644 --- a/LogicUI/src/era/mi/gui/model/wires/MovablePin.java +++ b/LogicUI/src/era/mi/gui/model/wires/MovablePin.java @@ -4,9 +4,9 @@ import era.mi.gui.model.components.GUIComponent; public class MovablePin extends Pin { - public MovablePin(GUIComponent component, double relX, double relY) + public MovablePin(GUIComponent component, int logicWidth, double relX, double relY) { - super(component, relX, relY); + super(component, logicWidth, relX, relY); } @Override diff --git a/LogicUI/src/era/mi/gui/model/wires/Pin.java b/LogicUI/src/era/mi/gui/model/wires/Pin.java index 44ea3742..abafc071 100644 --- a/LogicUI/src/era/mi/gui/model/wires/Pin.java +++ b/LogicUI/src/era/mi/gui/model/wires/Pin.java @@ -11,15 +11,17 @@ import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; public class Pin { public final GUIComponent component; + public final int logicWidth; protected double relX; protected double relY; private final List> pinMovedListeners; - public Pin(GUIComponent component, double relX, double relY) + public Pin(GUIComponent component, int logicWidth, double relX, double relY) { this.component = component; + this.logicWidth = logicWidth; this.relX = relX; this.relY = relY; diff --git a/LogicUI/src/era/mi/gui/model/wires/WireCrossPoint.java b/LogicUI/src/era/mi/gui/model/wires/WireCrossPoint.java index 7c19c13e..c54cdd9b 100644 --- a/LogicUI/src/era/mi/gui/model/wires/WireCrossPoint.java +++ b/LogicUI/src/era/mi/gui/model/wires/WireCrossPoint.java @@ -12,11 +12,11 @@ public class WireCrossPoint extends GUIComponent { private ReadEnd end; - public WireCrossPoint(ViewModel model) + public WireCrossPoint(ViewModel model, int logicWidth) { super(model); setSize(0, 0); - addPin(new Pin(this, 0, 0)); + addPin(new Pin(this, logicWidth, 0, 0)); } @Override