Restructured serializing/deserializing
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / symbolrenderers / CenteredTextSymbolRenderer.java
1 package net.mograsim.logic.model.snippets.symbolrenderers;
2
3 import net.haspamelodica.swt.helper.gcs.GeneralGC;
4 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
5 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
7 import net.mograsim.logic.model.model.components.GUIComponent;
8 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.model.snippets.Renderer;
10 import net.mograsim.logic.model.snippets.SnippetDefinintion;
11 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
12 import net.mograsim.preferences.ColorDefinition;
13 import net.mograsim.preferences.ColorManager;
14 import net.mograsim.preferences.Preferences;
15
16 /**
17  * Renders a text (<code>"text"</code>) with a given font height (<code>"height"</code>) in the center of the component.
18  * 
19  * @author Daniel Kirschten
20  */
21 public class CenteredTextSymbolRenderer implements Renderer
22 {
23         private final GUIComponent component;
24         private final CenteredTextParams params;
25
26         public CenteredTextSymbolRenderer(SubmodelComponent component, CenteredTextParams params)
27         {
28                 this.component = component;
29                 this.params = params;
30
31         }
32
33         @Override
34         public void render(GeneralGC gc, Rectangle visibleRegion)
35         {
36                 Font oldFont = gc.getFont();
37                 gc.setFont(new Font(oldFont.getName(), params.fontHeight, oldFont.getStyle()));
38                 ColorDefinition fg = Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.text");
39                 if (fg != null)
40                         gc.setForeground(ColorManager.current().toColor(fg));
41                 Point idSize = gc.textExtent(params.text);
42                 Rectangle bounds = component.getBounds();
43                 gc.drawText(params.text, bounds.x + (bounds.width - idSize.x) / 2, bounds.y + (bounds.height - idSize.y) / 2, true);
44                 gc.setFont(oldFont);
45         }
46
47         public static class CenteredTextParams
48         {
49                 public String text;
50                 public double fontHeight;
51         }
52
53         static
54         {
55                 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(CenteredTextSymbolRenderer.class.getCanonicalName(),
56                                 SnippetDefinintion.create(CenteredTextParams.class, CenteredTextSymbolRenderer::new));
57         }
58 }