Merge remote-tracking branch 'origin/development' into development
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / symbolrenderers / SimpleRectangularLikeSymbolRenderer.java
1 package net.mograsim.logic.model.snippets.symbolrenderers;
2
3 import java.util.Map.Entry;
4
5 import org.eclipse.swt.graphics.Color;
6
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;
18
19 /**
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.
24  * 
25  * @author Daniel Kirschten
26  */
27 public class SimpleRectangularLikeSymbolRenderer implements Renderer
28 {
29         private final GUIComponent component;
30         private final SimpleRectangularLikeParams params;
31
32         public SimpleRectangularLikeSymbolRenderer(SubmodelComponent component, SimpleRectangularLikeParams params)
33         {
34                 this.component = component;
35                 this.params = params;
36         }
37
38         @Override
39         public void render(GeneralGC gc, Rectangle visibleRegion)
40         {
41                 double posX = component.getPosX();
42                 double posY = component.getPosY();
43                 double width = component.getWidth();
44                 double height = component.getHeight();
45
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())
55                 {
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);
61                         gc.drawText(pinName,
62                                         posX + pinX + (pinX > params.horizontalComponentCenter ? -textExtent.x - params.pinLabelMargin : params.pinLabelMargin),
63                                         pinY - textExtent.y / 2, true);
64                 }
65                 gc.setFont(oldFont);
66         }
67
68         public static class SimpleRectangularLikeParams
69         {
70                 public String centerText;
71                 public double centerTextHeight;
72                 public double horizontalComponentCenter;
73                 public double pinLabelHeight;
74                 public double pinLabelMargin;
75         }
76
77         static
78         {
79                 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
80                                 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
81                                 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));
82         }
83 }