X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fserializing%2Fsnippets%2Fsymbolrenderers%2FSimpleRectangularLikeSymbolRenderer.java;fp=net.mograsim.logic.ui%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fui%2Fserializing%2Fsnippets%2Fsymbolrenderers%2FSimpleRectangularLikeSymbolRenderer.java;h=515c9c4414cdf3fad8710416754616f504cddd4a;hb=f594aef8abc8f444911333f6c32ef0fca18e18ba;hp=0000000000000000000000000000000000000000;hpb=dbda073d92ae9dd8e701d904c4c71dd0edd1fce7;p=Mograsim.git
diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/snippets/symbolrenderers/SimpleRectangularLikeSymbolRenderer.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/snippets/symbolrenderers/SimpleRectangularLikeSymbolRenderer.java
new file mode 100644
index 00000000..515c9c44
--- /dev/null
+++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/serializing/snippets/symbolrenderers/SimpleRectangularLikeSymbolRenderer.java
@@ -0,0 +1,82 @@
+package net.mograsim.logic.ui.serializing.snippets.symbolrenderers;
+
+import java.util.Map.Entry;
+
+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.ui.model.components.GUIComponent;
+import net.mograsim.logic.ui.model.components.submodels.SubmodelComponent;
+import net.mograsim.logic.ui.model.wires.Pin;
+import net.mograsim.logic.ui.serializing.CodeSnippetSupplier;
+import net.mograsim.logic.ui.serializing.snippets.Renderer;
+import net.mograsim.logic.ui.serializing.snippets.SnippetSupplier;
+import net.mograsim.preferences.Preferences;
+
+/**
+ * Renders a text ("centerText"
) with a given font height ("centerTextHeight"
) in the center of the component and
+ * draws a label for each pin with a given font height ("pinLabelHeight"
). The labels of pins to the left of a given x
+ * coordinate ("horizontalComponentCenter"
) are drawn to the right of the respective pin; labels of pins to the right are drawn
+ * left. A margin ("pinLabelMargin"
) is applied for pin label drawing.
+ *
+ * @author Daniel Kirschten
+ */
+public class SimpleRectangularLikeSymbolRenderer implements Renderer
+{
+ private final GUIComponent component;
+ private final SimpleRectangularLikeParams params;
+
+ public SimpleRectangularLikeSymbolRenderer(SubmodelComponent component, SimpleRectangularLikeParams params)
+ {
+ this.component = component;
+ this.params = params;
+ }
+
+ @Override
+ public void render(GeneralGC gc, Rectangle visibleRegion)
+ {
+ double posX = component.getPosX();
+ double posY = component.getPosY();
+ double width = component.getWidth();
+ double height = component.getHeight();
+
+ Font oldFont = gc.getFont();
+ gc.setFont(new Font(oldFont.getName(), params.centerTextHeight, oldFont.getStyle()));
+ Point textExtent = gc.textExtent(params.centerText);
+ Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
+ if (textColor != null)
+ gc.setForeground(textColor);
+ gc.drawText(params.centerText, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
+ gc.setFont(new Font(oldFont.getName(), params.pinLabelHeight, oldFont.getStyle()));
+ for (Entry pinEntry : component.getPins().entrySet())
+ {
+ String pinName = pinEntry.getKey();
+ Pin pin = pinEntry.getValue();
+ double pinX = pin.getRelX();
+ double pinY = posY + pin.getRelY();
+ textExtent = gc.textExtent(pinName);
+ gc.drawText(pinName,
+ posX + pinX + (pinX > params.horizontalComponentCenter ? -textExtent.x - params.pinLabelMargin : params.pinLabelMargin),
+ pinY - textExtent.y / 2, true);
+ }
+ gc.setFont(oldFont);
+ }
+
+ public static class SimpleRectangularLikeParams
+ {
+ public String centerText;
+ public double centerTextHeight;
+ public double horizontalComponentCenter;
+ public double pinLabelHeight;
+ public double pinLabelMargin;
+ }
+
+ static
+ {
+ CodeSnippetSupplier.symbolRendererProviderSupplier.setSnippetProvider(SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
+ SnippetSupplier.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));
+ }
+}
\ No newline at end of file