From a9ab5870c8a1a5b991a2520fd07c61c4a328536d Mon Sep 17 00:00:00 2001 From: hneemann Date: Sun, 29 Jul 2018 20:11:22 +0200 Subject: [PATCH] improved the LayoutShape --- .../de/neemann/digital/core/element/Keys.java | 21 ++++++++---- .../digital/draw/shapes/LayoutShape.java | 34 ++++++++++--------- .../gui/components/CircuitComponent.java | 1 + src/main/resources/lang/lang_de.xml | 2 ++ src/main/resources/lang/lang_en.xml | 2 ++ 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/neemann/digital/core/element/Keys.java b/src/main/java/de/neemann/digital/core/element/Keys.java index 7781ed078..6cf24d666 100644 --- a/src/main/java/de/neemann/digital/core/element/Keys.java +++ b/src/main/java/de/neemann/digital/core/element/Keys.java @@ -289,6 +289,21 @@ public final class Keys { = new Key.KeyInteger("Width", 3) .setMin(2); + /** + * defines the shape type of the custom circuit + */ + public static final Key SHAPE_TYPE + = new Key.KeyEnum<>("shapeType", CustomCircuitShapeType.DEFAULT, CustomCircuitShapeType.values()).setSecondary(); + + /** + * the width of an element if it is included as nested element + */ + public static final Key HEIGHT + = new Key.KeyInteger("Height", 3) + .setMin(2) + .setSecondary() + .setDependsOn(SHAPE_TYPE, cst -> cst.equals(CustomCircuitShapeType.LAYOUT)); + /** * width of the terminal */ @@ -528,12 +543,6 @@ public final class Keys { public static final Key PINNUMBER = new Key<>("pinNumber", "").setSecondary(); - /** - * defines the shape type of the custom circuit - */ - public static final Key SHAPE_TYPE - = new Key.KeyEnum<>("shapeType", CustomCircuitShapeType.DEFAULT, CustomCircuitShapeType.values()).setSecondary(); - /** * the pin count */ diff --git a/src/main/java/de/neemann/digital/draw/shapes/LayoutShape.java b/src/main/java/de/neemann/digital/draw/shapes/LayoutShape.java index f226ab9ab..21e43c263 100644 --- a/src/main/java/de/neemann/digital/draw/shapes/LayoutShape.java +++ b/src/main/java/de/neemann/digital/draw/shapes/LayoutShape.java @@ -21,7 +21,6 @@ import de.neemann.digital.lang.Lang; import java.awt.*; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; @@ -34,12 +33,11 @@ import static de.neemann.digital.draw.shapes.GenericShape.SIZE; */ public class LayoutShape implements Shape { - private final int width; - private final int height; + private int width; + private int height; private final Pins pins; private final Color color; private final String name; - private final String label; private final PinList left; private final PinList right; private final PinList top; @@ -94,16 +92,14 @@ public class LayoutShape implements Shape { } } - height = Math.max(right.size(), left.size()) + 1; - final int w = Math.max(top.size(), bottom.size()) + 1; - width = Math.max(w, custom.getAttributes().get(Keys.WIDTH)); - + height = Math.max(Math.max(right.size(), left.size()) + 1, custom.getAttributes().get(Keys.HEIGHT)); + width = Math.max(Math.max(top.size(), bottom.size()) + 1, custom.getAttributes().get(Keys.WIDTH)); HashMap map = new HashMap<>(); top.createPosition(map, new Vector(((width - top.size()) / 2 + 1) * SIZE, 0)); bottom.createPosition(map, new Vector(((width - bottom.size()) / 2 + 1) * SIZE, SIZE * height)); - left.createPosition(map, new Vector(0, SIZE)); - right.createPosition(map, new Vector(SIZE * width, SIZE)); + left.createPosition(map, new Vector(0, ((height - left.size()) / 2 + 1) * SIZE)); + right.createPosition(map, new Vector(SIZE * width, ((height - right.size()) / 2 + 1) * SIZE)); pins = new Pins(); for (PinDescription p : custom.getInputDescription(elementAttributes)) @@ -112,8 +108,12 @@ public class LayoutShape implements Shape { pins.add(createPin(map, p)); color = custom.getCircuit().getAttributes().get(Keys.BACKGROUND_COLOR); - label = elementAttributes.getCleanLabel(); - name = custom.getShortName(); + + String l = elementAttributes.getCleanLabel(); + if (l != null && l.length() > 0) + name = l; + else + name = custom.getShortName(); } private Pin createPin(HashMap map, PinDescription p) throws PinException { @@ -144,9 +144,12 @@ public class LayoutShape implements Shape { graphic.drawPolygon(poly, Style.NORMAL.deriveFillStyle(color)); graphic.drawPolygon(poly, Style.NORMAL); - Vector center = new Vector(width * SIZE / 2, height * SIZE / 2); - Graphic.drawText(graphic, center.add(0, -2), name, Orientation.CENTERBOTTOM, Style.SHAPE_PIN); - Graphic.drawText(graphic, center.add(0, 2), label, Orientation.CENTERTOP, Style.SHAPE_PIN); + if (top.size() == 0) + Graphic.drawText(graphic, new Vector(width * SIZE / 2, -4), name, Orientation.CENTERBOTTOM, Style.SHAPE_PIN); + else if (bottom.size() == 0) + Graphic.drawText(graphic, new Vector(width * SIZE / 2, height * SIZE + 4), name, Orientation.CENTERTOP, Style.SHAPE_PIN); + else + Graphic.drawText(graphic, new Vector(width * SIZE / 2, height * SIZE /2), name, Orientation.CENTERCENTER, Style.SHAPE_PIN); for (PinPos p : left) Graphic.drawText(graphic, p.pos.add(4, 0), p.label, Orientation.LEFTCENTER, Style.SHAPE_PIN); @@ -196,7 +199,6 @@ public class LayoutShape implements Shape { } private void createPosition(HashMap map, Vector pos) { - Collections.sort(pins); for (PinPos pp : pins) { map.put(pp.label, pp); pp.pos = pos; diff --git a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java index bba503414..3608d7a58 100644 --- a/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java +++ b/src/main/java/de/neemann/digital/gui/components/CircuitComponent.java @@ -67,6 +67,7 @@ public class CircuitComponent extends JComponent implements Circuit.ChangedListe ATTR_LIST.add(Keys.SHAPE_TYPE); if (Main.isExperimentalMode()) ATTR_LIST.add(Keys.CUSTOM_SHAPE); + ATTR_LIST.add(Keys.HEIGHT); ATTR_LIST.add(Keys.PINCOUNT); ATTR_LIST.add(Keys.BACKGROUND_COLOR); ATTR_LIST.add(Keys.DESCRIPTION); diff --git a/src/main/resources/lang/lang_de.xml b/src/main/resources/lang/lang_de.xml index fbdb2f831..9b6369b53 100644 --- a/src/main/resources/lang/lang_de.xml +++ b/src/main/resources/lang/lang_de.xml @@ -1000,6 +1000,8 @@ Sind evtl. die Namen der Variablen nicht eindeutig? Der Wert der Konstanten. Breite Breite des Symbols, wenn diese Schaltung in eine andere eingefügt wird. + Höhe + Höhe des Symbols, wenn diese Schaltung in eine andere eingefügt wird. Bei jedem Start automatisch neu laden. Lädt das HEX-File bei jedem Modelstart neu. Tausche Selektorposition diff --git a/src/main/resources/lang/lang_en.xml b/src/main/resources/lang/lang_en.xml index 1995568e7..cfb4d5a51 100644 --- a/src/main/resources/lang/lang_en.xml +++ b/src/main/resources/lang/lang_en.xml @@ -992,6 +992,8 @@ The value of the constant. Width With of symbol if this circuit is used as an component in an other circuit. + Height + Height of symbol if this circuit is used as an component in an other circuit. Reload at model start Reloads the hex file every time the model is started. Flip selector position