Moved code serializing a symbol renderer to where it belongs
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / submodels / SimpleRectangularSubmodelComponent.java
index 605e020..529767e 100644 (file)
@@ -5,16 +5,18 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
+import java.util.function.Function;
 
 import org.eclipse.swt.graphics.Color;
 
+import com.google.gson.JsonObject;
+
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Font;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
 import net.mograsim.logic.ui.model.ViewModelModifiable;
+import net.mograsim.logic.ui.model.components.GUIComponent;
 import net.mograsim.logic.ui.model.wires.MovablePin;
 import net.mograsim.logic.ui.model.wires.Pin;
 import net.mograsim.logic.ui.serializing.SubmodelComponentParams;
@@ -22,8 +24,6 @@ import net.mograsim.preferences.Preferences;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
 {
-       public static String kLabel = "label", kInCount = "input_count", kOutCount = "output_count", kLogicWidth = "logic_width";
-
        private static final double width = 35;
        private static final double pinDistance = 10;
        private static final double pinNameMargin = .5;
@@ -98,6 +98,7 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
        @Override
        protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
        {
+               // TODO code duplication: see SimpleRectagularLikeSymbolRendererProvider
                Font oldFont = gc.getFont();
                gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
                Point textExtent = gc.textExtent(label);
@@ -131,18 +132,21 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
                gc.drawRectangle(getBounds());
        }
 
+       // serializing
+
        @Override
-       public SubmodelComponentParams calculateParams()
+       public SubmodelComponentParams calculateParams(Function<GUIComponent, String> getIdentifier)
        {
-               SubmodelComponentParams ret = super.calculateParams();
-               ret.type = SimpleRectangularSubmodelComponent.class.getSimpleName();
-               Map<String, Object> m = new TreeMap<>();
-               m.put(kLabel, label);
-               m.put(kInCount, inputPinNames.toArray());
-               m.put(kOutCount, outputPinNames.toArray());
-               m.put(kLogicWidth, logicWidth);
-               ret.specialized = m;
-               return ret;
+               SubmodelComponentParams params = super.calculateParams(getIdentifier);
+               JsonObject symbolRendererParams = new JsonObject();
+               symbolRendererParams.addProperty("centerText", label);
+               symbolRendererParams.addProperty("horizontalComponentCenter", getWidth() / 2);
+               symbolRendererParams.addProperty("centerTextHeight", labelFontHeight);
+               symbolRendererParams.addProperty("pinLabelHeight", pinNameFontHeight);
+               symbolRendererParams.addProperty("pinLabelMargin", pinNameMargin);
+               params.symbolRendererSnippetID = "SimpleRectangularLikeSymbolRenderer";
+               params.symbolRendererParams = symbolRendererParams;
+               return params;
        }
 
        @Override