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.wires.Pin;
13 import net.mograsim.logic.model.serializing.IdentifyParams;
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 String centerText;
31 private final double centerTextHeight;
32 private final double horizontalComponentCenter;
33 private final double pinLabelHeight;
34 private final double pinLabelMargin;
36 public SimpleRectangularLikeSymbolRenderer(GUIComponent component, SimpleRectangularLikeParams params)
38 this.component = component;
39 this.centerText = params.centerText;
40 this.centerTextHeight = params.centerTextHeight;
41 this.horizontalComponentCenter = params.horizontalComponentCenter;
42 this.pinLabelHeight = params.pinLabelHeight;
43 this.pinLabelMargin = params.pinLabelMargin;
47 public void render(GeneralGC gc, Rectangle visibleRegion)
49 double posX = component.getPosX();
50 double posY = component.getPosY();
51 double width = component.getWidth();
52 double height = component.getHeight();
54 Font oldFont = gc.getFont();
55 gc.setFont(new Font(oldFont.getName(), centerTextHeight, oldFont.getStyle()));
56 Point textExtent = gc.textExtent(centerText);
57 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
58 if (textColor != null)
59 gc.setForeground(textColor);
60 gc.drawText(centerText, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
61 gc.setFont(new Font(oldFont.getName(), pinLabelHeight, oldFont.getStyle()));
62 for (Entry<String, Pin> pinEntry : component.getPins().entrySet())
64 String pinName = pinEntry.getKey();
65 Pin pin = pinEntry.getValue();
66 double pinX = pin.getRelX();
67 double pinY = posY + pin.getRelY();
68 textExtent = gc.textExtent(pinName);
69 gc.drawText(pinName, posX + pinX + (pinX > horizontalComponentCenter ? -textExtent.x - pinLabelMargin : pinLabelMargin),
70 pinY - textExtent.y / 2, true);
76 public String getIDForSerializing(IdentifyParams idParams)
78 return "simpleRectangularLike";
82 public SimpleRectangularLikeParams getParamsForSerializing(IdentifyParams idParams)
84 SimpleRectangularLikeParams params = new SimpleRectangularLikeParams();
85 params.centerText = centerText;
86 params.centerTextHeight = centerTextHeight;
87 params.horizontalComponentCenter = horizontalComponentCenter;
88 params.pinLabelHeight = pinLabelHeight;
89 params.pinLabelMargin = pinLabelMargin;
93 public static class SimpleRectangularLikeParams
95 public String centerText;
96 public double centerTextHeight;
97 public double horizontalComponentCenter;
98 public double pinLabelHeight;
99 public double pinLabelMargin;
104 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
105 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
106 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));