From 9153fe31a450db9935a24a8c0174cc346c9ba7b1 Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Sat, 12 Oct 2019 16:01:10 +0200 Subject: [PATCH] Some components drawing Bitvectors now draw them fitting to their bounds --- .../components/atomic/ModelBitDisplay.java | 6 ++-- .../components/atomic/ModelFixedOutput.java | 6 ++-- .../components/atomic/ModelManualSwitch.java | 6 ++-- .../logic/model/util/TextRenderingHelper.java | 30 +++++++++++++++++++ 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/util/TextRenderingHelper.java diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelBitDisplay.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelBitDisplay.java index 8bff082f..68e8d456 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelBitDisplay.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelBitDisplay.java @@ -4,7 +4,6 @@ import org.eclipse.swt.graphics.Color; 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; import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.components.CoreBitDisplay; @@ -17,6 +16,7 @@ import net.mograsim.logic.model.modeladapter.LogicCoreAdapter; import net.mograsim.logic.model.modeladapter.componentadapters.BitDisplayAdapter; import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; +import net.mograsim.logic.model.util.TextRenderingHelper; import net.mograsim.preferences.Preferences; public class ModelBitDisplay extends ModelComponent @@ -24,6 +24,7 @@ public class ModelBitDisplay extends ModelComponent private static final double width = 20; private static final double height = 10; private static final double fontHeight = 5; + private static final double textMargin = 0.5; public final int logicWidth; private final Pin inputPin; @@ -59,11 +60,10 @@ public class ModelBitDisplay extends ModelComponent Font oldFont = gc.getFont(); Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); if (textColor != null) gc.setForeground(textColor); - gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true); gc.setFont(oldFont); } diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelFixedOutput.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelFixedOutput.java index bdf6546f..9a62cadb 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelFixedOutput.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelFixedOutput.java @@ -6,7 +6,6 @@ import org.eclipse.swt.graphics.Color; 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; import net.mograsim.logic.core.types.BitVector; import net.mograsim.logic.core.types.BitVectorFormatter; @@ -19,6 +18,7 @@ import net.mograsim.logic.model.modeladapter.componentadapters.FixedOutputAdapte import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.logic.model.util.JsonHandler; +import net.mograsim.logic.model.util.TextRenderingHelper; import net.mograsim.preferences.Preferences; public class ModelFixedOutput extends ModelComponent @@ -26,6 +26,7 @@ public class ModelFixedOutput extends ModelComponent private static final double width = 20; private static final double height = 20; private static final double fontHeight = 5; + private static final double textMargin = 0.5; public final BitVector bits; @@ -62,11 +63,10 @@ public class ModelFixedOutput extends ModelComponent Font oldFont = gc.getFont(); Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); if (textColor != null) gc.setForeground(textColor); - gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true); gc.setFont(oldFont); } diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java index f495b4e9..78979825 100644 --- a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/model/components/atomic/ModelManualSwitch.java @@ -8,7 +8,6 @@ import org.eclipse.swt.graphics.Color; 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; import net.mograsim.logic.core.LogicObserver; import net.mograsim.logic.core.components.CoreManualSwitch; @@ -24,6 +23,7 @@ import net.mograsim.logic.model.modeladapter.componentadapters.ManualSwitchAdapt import net.mograsim.logic.model.serializing.IdentifyParams; import net.mograsim.logic.model.serializing.IndirectModelComponentCreator; import net.mograsim.logic.model.snippets.HighLevelStateHandler; +import net.mograsim.logic.model.util.TextRenderingHelper; import net.mograsim.preferences.Preferences; public class ModelManualSwitch extends ModelComponent @@ -32,6 +32,7 @@ public class ModelManualSwitch extends ModelComponent private static final double height = 10; private static final double fontHeight = 5; private static final double heightMiniButtons = 4; // 0 is disabled + private static final double textMargin = 0.5; public final int logicWidth; private final Pin outputPin; @@ -146,11 +147,10 @@ public class ModelManualSwitch extends ModelComponent Font oldFont = gc.getFont(); Font labelFont = new Font(oldFont.getName(), fontHeight, oldFont.getStyle()); gc.setFont(labelFont); - Point textExtent = gc.textExtent(label); Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text"); if (textColor != null) gc.setForeground(textColor); - gc.drawText(label, getPosX() + (width - textExtent.x) / 2, getPosY() + (height - textExtent.y) / 2, true); + TextRenderingHelper.drawTextFitting(gc, label, getBounds(), textMargin, true); gc.setFont(oldFont); if (manualSwitch != null && logicWidth > 1 && heightMiniButtons > 0 && visibleRegion.y < getPosY() + heightMiniButtons) diff --git a/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/util/TextRenderingHelper.java b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/util/TextRenderingHelper.java new file mode 100644 index 00000000..5ac0fb71 --- /dev/null +++ b/plugins/net.mograsim.logic.model/src/net/mograsim/logic/model/util/TextRenderingHelper.java @@ -0,0 +1,30 @@ +package net.mograsim.logic.model.util; + +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 TextRenderingHelper +{ + public static void drawTextFitting(GeneralGC gc, String string, Rectangle box, double margin, boolean isTransparent) + { + drawTextFitting(gc, string, box.x, box.y, box.width, box.height, margin, isTransparent); + } + + public static void drawTextFitting(GeneralGC gc, String string, double x, double y, double width, double height, double margin, + boolean isTransparent) + { + Point textExtentOldFont = gc.textExtent(string); + Font oldFont = gc.getFont(); + double factorX = (width - margin * 2) / textExtentOldFont.x; + double factorY = (height - margin * 2) / textExtentOldFont.y; + double factor = Math.min(factorX, factorY); + gc.setFont(oldFont.scale(factor)); + Point textExtentCurrentFont = gc.textExtent(string); + double xOff = (width - textExtentCurrentFont.x) / 2; + double yOff = (height - textExtentCurrentFont.y) / 2; + gc.drawText(string, x + xOff, y + yOff, isTransparent); + gc.setFont(oldFont); + } +} \ No newline at end of file -- 2.17.1