Made finding the ID of a JsonSerializable less ugly
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / symbolrenderers / PinNamesSymbolRenderer.java
1 package net.mograsim.logic.model.snippets.symbolrenderers;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
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.IdentifyParams;
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;
17
18 public class PinNamesSymbolRenderer implements Renderer
19 {
20         private final GUIComponent component;
21         private final Map<Pin, Position> pinPositions;
22         private final double pinLabelHeight;
23         private final double pinLabelMargin;
24
25         public PinNamesSymbolRenderer(GUIComponent component, PinNamesParams params)
26         {
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);
33         }
34
35         public void setPinPosition(String pinName, Position position)
36         {
37                 setPinPosition(component.getPin(pinName), position);
38         }
39
40         public void setPinPosition(Pin pin, Position position)
41         {
42                 if (position == null)
43                         pinPositions.remove(pin);
44                 else
45                         pinPositions.put(pin, position);
46         }
47
48         @Override
49         public void render(GeneralGC gc, Rectangle visibleRegion)
50         {
51                 Font oldFont = gc.getFont();
52                 gc.setFont(new Font(oldFont.getName(), pinLabelHeight, oldFont.getStyle()));
53                 for (Pin pin : component.getPins().values())
54                 {
55                         Position pos = pinPositions.get(pin);
56                         if (pos == null)
57                                 pos = Position.RIGHT;
58
59                         Point topleft = pin.getPos();
60                         Point textExtent = gc.textExtent(pin.name);
61
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;
66
67                         double x = pos.posX * x1 + (1 - pos.posX) * x2;
68                         double y = pos.posY * y1 + (1 - pos.posY) * y2;
69
70                         gc.drawText(pin.name, x, y, true);
71                 }
72                 gc.setFont(oldFont);
73         }
74
75         @Override
76         public String getIDForSerializing(IdentifyParams idParams)
77         {
78                 return "pinNames";
79         }
80
81         @Override
82         public PinNamesParams getParamsForSerializing(IdentifyParams idParams)
83         {
84                 PinNamesParams params = new PinNamesParams();
85                 params.pinNamePositions = new HashMap<>();
86                 pinPositions.forEach((pin, pos) -> params.pinNamePositions.put(pin.name, pos));
87                 return params;
88         }
89
90         public static class PinNamesParams
91         {
92                 public Map<String, Position> pinNamePositions;
93                 public double pinLabelHeight;
94                 public double pinLabelMargin;
95
96                 public static enum Position
97                 {
98                         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);
99
100                         private final double posX, posY;
101
102                         private Position(double posX, double posY)
103                         {
104                                 this.posX = posX;
105                                 this.posY = posY;
106                         }
107                 }
108         }
109
110         static
111         {
112                 SubmodelComponentSnippetSuppliers.symbolRendererSupplier.setSnippetSupplier(PinNamesSymbolRenderer.class.getCanonicalName(),
113                                 SnippetDefinintion.create(PinNamesParams.class, PinNamesSymbolRenderer::new));
114         }
115 }