Restructured the Preferences system
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / symbolrenderers / SimpleRectangularLikeSymbolRenderer.java
1 package net.mograsim.logic.model.snippets.symbolrenderers;
2
3 import static net.mograsim.logic.model.preferences.RenderPreferences.TEXT_COLOR;
4
5 import java.util.Map.Entry;
6
7 import org.eclipse.swt.graphics.Color;
8
9 import net.haspamelodica.swt.helper.gcs.GeneralGC;
10 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
11 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
12 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
13 import net.mograsim.logic.model.model.components.ModelComponent;
14 import net.mograsim.logic.model.model.wires.Pin;
15 import net.mograsim.logic.model.preferences.RenderPreferences;
16 import net.mograsim.logic.model.serializing.IdentifyParams;
17 import net.mograsim.logic.model.snippets.Renderer;
18 import net.mograsim.logic.model.snippets.SnippetDefinintion;
19 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
20
21 /**
22  * Renders a text (<code>"centerText"</code>) with a given font height (<code>"centerTextHeight"</code>) in the center of the component and
23  * 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
24  * coordinate (<code>"horizontalComponentCenter"</code>) are drawn to the right of the respective pin; labels of pins to the right are drawn
25  * left. A margin (<code>"pinLabelMargin"</code>) is applied for pin label drawing.
26  * 
27  * @author Daniel Kirschten
28  */
29 public class SimpleRectangularLikeSymbolRenderer implements Renderer
30 {
31         private final ModelComponent component;
32         private final String centerText;
33         private final double centerTextHeight;
34         private final double horizontalComponentCenter;
35         private final double pinLabelHeight;
36         private final double pinLabelMargin;
37
38         public SimpleRectangularLikeSymbolRenderer(ModelComponent component, SimpleRectangularLikeParams params)
39         {
40                 this.component = component;
41                 this.centerText = params.centerText;
42                 this.centerTextHeight = params.centerTextHeight;
43                 this.horizontalComponentCenter = params.horizontalComponentCenter;
44                 this.pinLabelHeight = params.pinLabelHeight;
45                 this.pinLabelMargin = params.pinLabelMargin;
46         }
47
48         @Override
49         public void render(GeneralGC gc, RenderPreferences renderPrefs, Rectangle visibleRegion)
50         {
51                 double posX = component.getPosX();
52                 double posY = component.getPosY();
53                 double width = component.getWidth();
54                 double height = component.getHeight();
55
56                 Font oldFont = gc.getFont();
57                 gc.setFont(new Font(oldFont.getName(), centerTextHeight, oldFont.getStyle()));
58                 Point textExtent = gc.textExtent(centerText);
59                 Color textColor = renderPrefs.getColor(TEXT_COLOR);
60                 if (textColor != null)
61                         gc.setForeground(textColor);
62                 gc.drawText(centerText, posX + (width - textExtent.x) / 2, posY + (height - textExtent.y) / 2, true);
63                 gc.setFont(new Font(oldFont.getName(), pinLabelHeight, oldFont.getStyle()));
64                 for (Entry<String, Pin> pinEntry : component.getPins().entrySet())
65                 {
66                         String pinName = pinEntry.getKey();
67                         Pin pin = pinEntry.getValue();
68                         double pinX = pin.getRelX();
69                         double pinY = posY + pin.getRelY();
70                         textExtent = gc.textExtent(pinName);
71                         gc.drawText(pinName, posX + pinX + (pinX > horizontalComponentCenter ? -textExtent.x - pinLabelMargin : pinLabelMargin),
72                                         pinY - textExtent.y / 2, true);
73                 }
74                 gc.setFont(oldFont);
75         }
76
77         @Override
78         public String getIDForSerializing(IdentifyParams idParams)
79         {
80                 return "simpleRectangularLike";
81         }
82
83         @Override
84         public SimpleRectangularLikeParams getParamsForSerializing(IdentifyParams idParams)
85         {
86                 SimpleRectangularLikeParams params = new SimpleRectangularLikeParams();
87                 params.centerText = centerText;
88                 params.centerTextHeight = centerTextHeight;
89                 params.horizontalComponentCenter = horizontalComponentCenter;
90                 params.pinLabelHeight = pinLabelHeight;
91                 params.pinLabelMargin = pinLabelMargin;
92                 return params;
93         }
94
95         public static class SimpleRectangularLikeParams
96         {
97                 public String centerText;
98                 public double centerTextHeight;
99                 public double horizontalComponentCenter;
100                 public double pinLabelHeight;
101                 public double pinLabelMargin;
102         }
103
104         static
105         {
106                 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
107                                 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
108                                 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));
109         }
110 }