1 package net.mograsim.logic.model.snippets.symbolrenderers;
3 import java.util.HashMap;
6 import net.haspamelodica.swt.helper.gcs.GeneralGC;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
8 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
9 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
10 import net.mograsim.logic.model.model.components.GUIComponent;
11 import net.mograsim.logic.model.model.wires.Pin;
12 import net.mograsim.logic.model.serializing.IdentifierGetter;
13 import net.mograsim.logic.model.snippets.Renderer;
14 import net.mograsim.logic.model.snippets.SnippetDefinintion;
15 import net.mograsim.logic.model.snippets.SubmodelComponentSnippetSuppliers;
16 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
18 public class PinNamesSymbolRenderer implements Renderer
20 private final GUIComponent component;
21 private final Map<Pin, Position> pinPositions;
22 private final double pinLabelHeight;
23 private final double pinLabelMargin;
25 public PinNamesSymbolRenderer(GUIComponent component, PinNamesParams params)
27 this.component = component;
28 this.pinPositions = new HashMap<>();
29 this.pinLabelHeight = params.pinLabelHeight;
30 this.pinLabelMargin = params.pinLabelMargin;
31 if (params.pinNamePositions != null)
32 params.pinNamePositions.forEach(this::setPinPosition);
35 public void setPinPosition(String pinName, Position position)
37 setPinPosition(component.getPin(pinName), position);
40 public void setPinPosition(Pin pin, Position position)
43 pinPositions.remove(pin);
45 pinPositions.put(pin, position);
49 public void render(GeneralGC gc, Rectangle visibleRegion)
51 Font oldFont = gc.getFont();
52 gc.setFont(new Font(oldFont.getName(), pinLabelHeight, oldFont.getStyle()));
53 for (Pin pin : component.getPins().values())
55 Position pos = pinPositions.get(pin);
59 Point topleft = pin.getPos();
60 Point textExtent = gc.textExtent(pin.name);
62 double x2 = topleft.x - textExtent.x - pinLabelMargin;
63 double y2 = topleft.y - textExtent.y - pinLabelMargin;
64 double x1 = topleft.x + pinLabelMargin;
65 double y1 = topleft.y + pinLabelMargin;
67 double x = pos.posX * x1 + (1 - pos.posX) * x2;
68 double y = pos.posY * y1 + (1 - pos.posY) * y2;
70 gc.drawText(pin.name, x, y, true);
76 public PinNamesParams getParamsForSerializing(IdentifierGetter idGetter)
78 PinNamesParams params = new PinNamesParams();
79 params.pinNamePositions = new HashMap<>();
80 pinPositions.forEach((pin, pos) -> params.pinNamePositions.put(pin.name, pos));
84 public static class PinNamesParams
86 public Map<String, Position> pinNamePositions;
87 public double pinLabelHeight;
88 public double pinLabelMargin;
90 public static enum Position
92 TOP(.5, 0), TOP_LEFT(0, 0), LEFT(0, .5), BOTTOM_LEFT(0, 1), BOTTOM(.5, 1), BOTTOM_RIGHT(1, 1), RIGHT(1, .5), TOP_RIGHT(1, 0);
94 private final double posX, posY;
96 private Position(double posX, double posY)
106 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(PinNamesSymbolRenderer.class.getCanonicalName(),
107 SnippetDefinintion.create(PinNamesParams.class, PinNamesSymbolRenderer::new));