1 package net.mograsim.logic.model.snippets.symbolrenderers;
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.serializing.IdentifierGetter;
10 import net.mograsim.logic.model.snippets.Renderer;
11 import net.mograsim.logic.model.snippets.SnippetDefinintion;
12 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
13 import net.mograsim.preferences.ColorDefinition;
14 import net.mograsim.preferences.ColorManager;
15 import net.mograsim.preferences.Preferences;
18 * Renders a text (<code>"text"</code>) with a given font height (<code>"height"</code>) in the center of the component.
20 * @author Daniel Kirschten
22 public class CenteredTextSymbolRenderer implements Renderer
24 private final GUIComponent component;
25 private final String text;
26 private final double fontHeight;
28 public CenteredTextSymbolRenderer(SubmodelComponent component, CenteredTextParams params)
30 this.component = component;
31 this.text = params.text;
32 this.fontHeight = params.fontHeight;
37 public void render(GeneralGC gc, Rectangle visibleRegion)
39 Font oldFont = gc.getFont();
40 gc.setFont(new Font(oldFont.getName(), fontHeight, oldFont.getStyle()));
41 ColorDefinition fg = Preferences.current().getColorDefinition("net.mograsim.logic.model.color.text");
43 gc.setForeground(ColorManager.current().toColor(fg));
44 Point idSize = gc.textExtent(text);
45 Rectangle bounds = component.getBounds();
46 gc.drawText(text, bounds.x + (bounds.width - idSize.x) / 2, bounds.y + (bounds.height - idSize.y) / 2, true);
51 public CenteredTextParams getParamsForSerializing(IdentifierGetter idGetter)
53 CenteredTextParams params = new CenteredTextParams();
55 params.fontHeight = fontHeight;
59 public static class CenteredTextParams
62 public double fontHeight;
67 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(CenteredTextSymbolRenderer.class.getCanonicalName(),
68 SnippetDefinintion.create(CenteredTextParams.class, CenteredTextSymbolRenderer::new));