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.serializing.IdentifierGetter;
15 import net.mograsim.logic.model.snippets.Renderer;
16 import net.mograsim.logic.model.snippets.SnippetDefinintion;
17 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
18 import net.mograsim.preferences.Preferences;
21 * Renders a text (<code>"centerText"</code>) with a given font height (<code>"centerTextHeight"</code>) in the center of the component and
22 * 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
23 * coordinate (<code>"horizontalComponentCenter"</code>) are drawn to the right of the respective pin; labels of pins to the right are drawn
24 * left. A margin (<code>"pinLabelMargin"</code>) is applied for pin label drawing.
26 * @author Daniel Kirschten
28 public class SimpleRectangularLikeSymbolRenderer implements Renderer
30 private final GUIComponent component;
31 private final String centerText;
32 private final double centerTextHeight;
33 private final double horizontalComponentCenter;
34 private final double pinLabelHeight;
35 private final double pinLabelMargin;
37 public SimpleRectangularLikeSymbolRenderer(SubmodelComponent component, SimpleRectangularLikeParams params)
39 this.component = component;
40 this.centerText = params.centerText;
41 this.centerTextHeight = params.centerTextHeight;
42 this.horizontalComponentCenter = params.horizontalComponentCenter;
43 this.pinLabelHeight = params.pinLabelHeight;
44 this.pinLabelMargin = params.pinLabelMargin;
48 public void render(GeneralGC gc, Rectangle visibleRegion)
50 double posX = component.getPosX();
51 double posY = component.getPosY();
52 double width = component.getWidth();
53 double height = component.getHeight();
55 Font oldFont = gc.getFont();
56 gc.setFont(new Font(oldFont.getName(), centerTextHeight, oldFont.getStyle()));
57 Point textExtent = gc.textExtent(centerText);
58 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
59 if (textColor != null)
60 gc.setForeground(textColor);
61 gc.drawText(centerText, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
62 gc.setFont(new Font(oldFont.getName(), pinLabelHeight, oldFont.getStyle()));
63 for (Entry<String, Pin> pinEntry : component.getPins().entrySet())
65 String pinName = pinEntry.getKey();
66 Pin pin = pinEntry.getValue();
67 double pinX = pin.getRelX();
68 double pinY = posY + pin.getRelY();
69 textExtent = gc.textExtent(pinName);
70 gc.drawText(pinName, posX + pinX + (pinX > horizontalComponentCenter ? -textExtent.x - pinLabelMargin : pinLabelMargin),
71 pinY - textExtent.y / 2, true);
77 public SimpleRectangularLikeParams getParamsForSerializing(IdentifierGetter idGetter)
79 SimpleRectangularLikeParams params = new SimpleRectangularLikeParams();
80 params.centerText = centerText;
81 params.centerTextHeight = centerTextHeight;
82 params.horizontalComponentCenter = horizontalComponentCenter;
83 params.pinLabelHeight = pinLabelHeight;
84 params.pinLabelMargin = pinLabelMargin;
88 public static class SimpleRectangularLikeParams
90 public String centerText;
91 public double centerTextHeight;
92 public double horizontalComponentCenter;
93 public double pinLabelHeight;
94 public double pinLabelMargin;
99 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
100 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
101 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));