Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[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.ModelComponent;
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;
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 ModelComponent 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;
35
36         public SimpleRectangularLikeSymbolRenderer(ModelComponent component, SimpleRectangularLikeParams params)
37         {
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;
44         }
45
46         @Override
47         public void render(GeneralGC gc, Rectangle visibleRegion)
48         {
49                 double posX = component.getPosX();
50                 double posY = component.getPosY();
51                 double width = component.getWidth();
52                 double height = component.getHeight();
53
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())
63                 {
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);
71                 }
72                 gc.setFont(oldFont);
73         }
74
75         @Override
76         public String getIDForSerializing(IdentifyParams idParams)
77         {
78                 return "simpleRectangularLike";
79         }
80
81         @Override
82         public SimpleRectangularLikeParams getParamsForSerializing(IdentifyParams idParams)
83         {
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;
90                 return params;
91         }
92
93         public static class SimpleRectangularLikeParams
94         {
95                 public String centerText;
96                 public double centerTextHeight;
97                 public double horizontalComponentCenter;
98                 public double pinLabelHeight;
99                 public double pinLabelMargin;
100         }
101
102         static
103         {
104                 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(
105                                 SimpleRectangularLikeSymbolRenderer.class.getCanonicalName(),
106                                 SnippetDefinintion.create(SimpleRectangularLikeParams.class, SimpleRectangularLikeSymbolRenderer::new));
107         }
108 }