GUIComponents now have names
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / submodels / SimpleRectangularSubmodelComponent.java
index 65114b6..acf9fb2 100644 (file)
@@ -5,16 +5,22 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
+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;
+import net.mograsim.logic.ui.serializing.snippets.Renderer;
+import net.mograsim.logic.ui.serializing.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer;
+import net.mograsim.logic.ui.serializing.snippets.symbolrenderers.SimpleRectangularLikeSymbolRenderer.SimpleRectangularLikeParams;
 import net.mograsim.preferences.Preferences;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
@@ -33,15 +39,30 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
        private final List<String> outputPinNames;
        private final List<String> outputPinNamesUnmodifiable;
 
+       private Renderer symbolRenderer;
+
        public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label)
        {
-               super(model);
+               this(model, logicWidth, label, null);
+       }
+
+       public SimpleRectangularSubmodelComponent(ViewModelModifiable model, int logicWidth, String label, String name)
+       {
+               super(model, name);
                this.label = label;
                this.logicWidth = logicWidth;
                this.inputPinNames = new ArrayList<>();
                this.inputPinNamesUnmodifiable = Collections.unmodifiableList(inputPinNames);
                this.outputPinNames = new ArrayList<>();
                this.outputPinNamesUnmodifiable = Collections.unmodifiableList(outputPinNames);
+
+               SimpleRectangularLikeParams rendererParams = new SimpleRectangularLikeParams();
+               rendererParams.centerText = label;
+               rendererParams.centerTextHeight = labelFontHeight;
+               rendererParams.horizontalComponentCenter = getWidth() / 2;
+               rendererParams.pinLabelHeight = pinNameFontHeight;
+               rendererParams.pinLabelMargin = pinNameMargin;
+               symbolRenderer = new SimpleRectangularLikeSymbolRenderer(this, rendererParams);
        }
 
        protected void setInputPins(String... pinNames)
@@ -93,28 +114,7 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
        @Override
        protected void renderSymbol(GeneralGC gc, Rectangle visibleRegion)
        {
-               Font oldFont = gc.getFont();
-               gc.setFont(new Font(oldFont.getName(), labelFontHeight, oldFont.getStyle()));
-               Point textExtent = gc.textExtent(label);
-               Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
-               if (textColor != null)
-                       gc.setForeground(textColor);
-               gc.drawText(label, getPosX() + (getWidth() - textExtent.x) / 2, getPosY() + (getHeight() - textExtent.y) / 2, true);
-               gc.setFont(new Font(oldFont.getName(), pinNameFontHeight, oldFont.getStyle()));
-               for (int i = 0; i < inputPinNames.size(); i++)
-               {
-                       String pinName = inputPinNames.get(i);
-                       textExtent = gc.textExtent(pinName);
-                       gc.drawText(pinName, getPosX() + pinNameMargin, getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
-               }
-               for (int i = 0; i < outputPinNames.size(); i++)
-               {
-                       String pinName = outputPinNames.get(i);
-                       textExtent = gc.textExtent(pinName);
-                       gc.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
-                                       getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
-               }
-               gc.setFont(oldFont);
+               symbolRenderer.render(gc, visibleRegion);
        }
 
        @Override
@@ -126,6 +126,23 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
                gc.drawRectangle(getBounds());
        }
 
+       // serializing
+
+       @Override
+       public SubmodelComponentParams calculateParams(Function<GUIComponent, String> getIdentifier)
+       {
+               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
        protected Pin addSubmodelInterface(MovablePin supermodelPin)
        {