1 package net.mograsim.logic.model.snippets.symbolrenderers;
3 import java.util.Map.Entry;
5 import org.eclipse.swt.graphics.Color;
7 import net.haspamelodica.swt.helper.gcs.GeneralGC;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
11 import net.mograsim.logic.model.model.components.GUIComponent;
12 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
13 import net.mograsim.logic.model.model.wires.Pin;
14 import net.mograsim.logic.model.snippets.Renderer;
15 import net.mograsim.logic.model.snippets.SnippetDefinintion;
16 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
17 import net.mograsim.preferences.Preferences;
20 * Renders a text (<code>"centerText"</code>) with a given font height (<code>"centerTextHeight"</code>) in the center of the component and
21 * draws a label for each pin with a given font height (<code>"pinLabelHeight"</code>). The labels of pins to the left of a given x
22 * coordinate (<code>"horizontalComponentCenter"</code>) are drawn to the right of the respective pin; labels of pins to the right are drawn
23 * left. A margin (<code>"pinLabelMargin"</code>) is applied for pin label drawing.
25 * @author Daniel Kirschten
27 public class SimpleRectangularLikeSymbolRenderer implements Renderer
29 private final GUIComponent component;
30 private final SimpleRectangularLikeParams params;
32 public SimpleRectangularLikeSymbolRenderer(SubmodelComponent component, SimpleRectangularLikeParams params)
34 this.component = component;
39 public void render(GeneralGC gc, Rectangle visibleRegion)
41 double posX = component.getPosX();
42 double posY = component.getPosY();
43 double width = component.getWidth();
44 double height = component.getHeight();
46 Font oldFont = gc.getFont();
47 gc.setFont(new Font(oldFont.getName(), params.centerTextHeight, oldFont.getStyle()));
48 Point textExtent = gc.textExtent(params.centerText);
49 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
50 if (textColor != null)
51 gc.setForeground(textColor);
52 gc.drawText(params.centerText, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
53 gc.setFont(new Font(oldFont.getName(), params.pinLabelHeight, oldFont.getStyle()));
54 for (Entry<String, Pin> pinEntry : component.getPins().entrySet())
56 String pinName = pinEntry.getKey();
57 Pin pin = pinEntry.getValue();
58 double pinX = pin.getRelX();
59 double pinY = posY + pin.getRelY();
60 textExtent = gc.textExtent(pinName);
62 posX + pinX + (pinX > params.horizontalComponentCenter ? -textExtent.x - params.pinLabelMargin : params.pinLabelMargin),
63 pinY - textExtent.y / 2, true);
68 public static class SimpleRectangularLikeParams
70 public String centerText;
71 public double centerTextHeight;
72 public double horizontalComponentCenter;
73 public double pinLabelHeight;
74 public double pinLabelMargin;
79 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
80 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
81 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));