From 39819e05a6572612cb22c6373d2bc3bfa78ef33c Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Wed, 5 Jun 2019 17:24:09 +0200 Subject: [PATCH 1/1] Splitted SubmodelComponent and SimpleRectangularSubmodelComponent --- .../SimpleRectangularSubmodelComponent.java | 125 ++++++++++++++++++ .../model/components/SubmodelComponent.java | 23 ++-- 2 files changed, 134 insertions(+), 14 deletions(-) create mode 100644 net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java new file mode 100644 index 00000000..e2af6f1a --- /dev/null +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java @@ -0,0 +1,125 @@ +package net.mograsim.logic.ui.model.components; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import net.haspamelodica.swt.helper.gcs.GeneralGC; +import net.haspamelodica.swt.helper.swtobjectwrappers.Font; +import net.haspamelodica.swt.helper.swtobjectwrappers.Point; +import net.mograsim.logic.ui.model.ViewModelModifiable; +import net.mograsim.logic.ui.model.wires.Pin; + +public class SimpleRectangularSubmodelComponent extends SubmodelComponent +{ + private static final double width = 35; + private static final double pinDistance = 10; + private static final double minHeight = 25; + private static final double fontHeight = 5; + + private final String label; + protected final int logicWidth; + + private final List inputSupermodelPins; + private final List inputSupermodelPinsUnmodifiable; + private final List outputSupermodelPins; + private final List outputSupermodelPinsUnmodifiable; + private final List inputSubmodelPins; + private final List inputSubmodelPinsUnmodifiable; + private final List outputSubmodelPins; + private final List outputSubmodelPinsUnmodifiable; + + protected SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label) + { + super(model); + this.label = label; + this.logicWidth = logicWidth; + this.inputSupermodelPins = new ArrayList<>(); + this.inputSupermodelPinsUnmodifiable = Collections.unmodifiableList(inputSupermodelPins); + this.outputSupermodelPins = new ArrayList<>(); + this.outputSupermodelPinsUnmodifiable = Collections.unmodifiableList(outputSupermodelPins); + this.inputSubmodelPins = new ArrayList<>(); + this.inputSubmodelPinsUnmodifiable = Collections.unmodifiableList(inputSubmodelPins); + this.outputSubmodelPins = new ArrayList<>(); + this.outputSubmodelPinsUnmodifiable = Collections.unmodifiableList(outputSubmodelPins); + } + + protected void setInputCount(int inputCount) + { + int oldInputCount = inputSupermodelPins.size(); + double height = Math.max(inputCount, outputSupermodelPins.size()) * pinDistance; + setSize(width, height); + if (oldInputCount > inputCount) + while (inputSupermodelPins.size() > inputCount) + { + inputSubmodelPins.remove(inputCount); + removePin(inputSupermodelPins.remove(inputCount)); + } + else if (oldInputCount < inputCount) + for (int i = oldInputCount; i < inputCount; i++) + { + Pin submodelPin = addSubmodelInterface(logicWidth, 0, pinDistance / 2 + i * pinDistance); + inputSubmodelPins.add(submodelPin); + inputSupermodelPins.add(getSupermodelPin(submodelPin)); + } + } + + protected void setOutputCount(int outputCount) + { + int oldOutputCount = outputSupermodelPins.size(); + setSize(width, Math.max(inputSupermodelPins.size(), outputCount) * pinDistance); + if (oldOutputCount > outputCount) + while (outputSupermodelPins.size() > outputCount) + { + outputSubmodelPins.remove(outputCount); + removePin(outputSupermodelPins.get(outputCount)); + } + else if (oldOutputCount < outputCount) + for (int i = oldOutputCount; i < outputCount; i++) + { + Pin submodelPin = addSubmodelInterface(logicWidth, width, pinDistance / 2 + i * pinDistance); + outputSubmodelPins.add(submodelPin); + outputSupermodelPins.add(getSupermodelPin(submodelPin)); + } + } + + public List getInputPins() + { + return inputSupermodelPinsUnmodifiable; + } + + public List getOutputPins() + { + return outputSupermodelPinsUnmodifiable; + } + + protected List getInputSubmodelPins() + { + return inputSubmodelPinsUnmodifiable; + } + + protected List getOutputSubmodelPins() + { + return outputSubmodelPinsUnmodifiable; + } + + @Override + protected void renderSymbol(GeneralGC gc) + { + double posX = getBounds().x; + double posY = getBounds().y; + + 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 + (getBounds().width - textExtent.x) / 2, posY + (getBounds().height - textExtent.y) / 2, true); + gc.setFont(oldFont); + } + + @Override + protected void renderOutline(GeneralGC gc) + { + gc.drawRectangle(getBounds()); + } +} \ No newline at end of file diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java index 7dceec8c..5e5d18a6 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java @@ -8,15 +8,13 @@ import java.util.Map.Entry; import net.haspamelodica.swt.helper.gcs.GCConfig; import net.haspamelodica.swt.helper.gcs.GeneralGC; import net.haspamelodica.swt.helper.gcs.TranslatedGC; -import net.haspamelodica.swt.helper.swtobjectwrappers.Font; -import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle; import net.mograsim.logic.ui.LogicUIRenderer; import net.mograsim.logic.ui.model.ViewModel; import net.mograsim.logic.ui.model.ViewModelModifiable; import net.mograsim.logic.ui.model.wires.Pin; -public class SubmodelComponent extends GUIComponent +public abstract class SubmodelComponent extends GUIComponent { protected final ViewModelModifiable submodelModifiable; public final ViewModel submodel; @@ -26,13 +24,12 @@ public class SubmodelComponent extends GUIComponent private final Map supermodelPinsPerSubmodelPinUnmodifiable; private final SubmodelInterface submodelInterface; - private final String label; private double submodelScale; private double maxVisibleRegionFillRatioForAlpha0; private double minVisibleRegionFillRatioForAlpha1; private final LogicUIRenderer renderer; - public SubmodelComponent(ViewModelModifiable model, String label) + public SubmodelComponent(ViewModelModifiable model) { super(model); this.submodelModifiable = new ViewModelModifiable(); @@ -43,7 +40,6 @@ public class SubmodelComponent extends GUIComponent this.supermodelPinsPerSubmodelPinUnmodifiable = Collections.unmodifiableMap(supermodelPinsPerSubmodelPin); this.submodelInterface = new SubmodelInterface(submodelModifiable); - this.label = label; this.submodelScale = 1; this.maxVisibleRegionFillRatioForAlpha0 = 0.4; this.minVisibleRegionFillRatioForAlpha1 = 0.8; @@ -157,18 +153,17 @@ public class SubmodelComponent extends GUIComponent if (labelAlpha != 0) { gc.setAlpha(labelAlpha); - Font oldFont = gc.getFont(); - Font labelFont = new Font(oldFont.getName(), 6, oldFont.getStyle()); - gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); - gc.drawText(label, posX + (getBounds().width - textExtent.x) / 2, posY + (getBounds().height - textExtent.y) / 2, true); - gc.setFont(oldFont); + renderSymbol(gc); } conf.reset(gc); - // draw the "bounding box" after all other operations to make interface pins look better - gc.drawRectangle(getBounds()); + // draw the outline after all other operations to make interface pins look better + renderOutline(gc); } + protected abstract void renderOutline(GeneralGC gc); + + protected abstract void renderSymbol(GeneralGC gc); + private static double map(double val, double valMin, double valMax, double mapMin, double mapMax) { return mapMin + (val - valMin) * (mapMax - mapMin) / (valMax - valMin); -- 2.17.1