Changed SubmodelComponent.addSubmodelInterface() interface
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sat, 22 Jun 2019 08:56:46 +0000 (10:56 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sat, 22 Jun 2019 08:56:46 +0000 (10:56 +0200)
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUICustomComponentCreator.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SimpleRectangularSubmodelComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/SubmodelComponent.java

index 6055f1a..0e23feb 100644 (file)
@@ -6,11 +6,12 @@ import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 
 import net.mograsim.logic.ui.model.ViewModelModifiable;
-import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams;
+import net.mograsim.logic.ui.model.components.SubmodelComponentParams.ComponentCompositionParams.InnerComponentParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InnerWireParams;
 import net.mograsim.logic.ui.model.components.SubmodelComponentParams.InterfacePinParams;
 import net.mograsim.logic.ui.model.wires.GUIWire;
+import net.mograsim.logic.ui.model.wires.MovablePin;
 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
 
 /**
@@ -112,7 +113,8 @@ public final class GUICustomComponentCreator
                comp.setSize(params.width, params.height);
                for (InterfacePinParams iPinParams : params.interfacePins)
                {
-                       comp.addSubmodelInterface(iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y);
+                       comp.addSubmodelInterface(
+                                       new MovablePin(comp, iPinParams.name, iPinParams.logicWidth, iPinParams.location.x, iPinParams.location.y));
                }
                return comp;
        }
index 7a7bb9d..17fa8ab 100644 (file)
@@ -13,6 +13,7 @@ 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.wires.MovablePin;
 import net.mograsim.logic.ui.model.wires.Pin;
 
 public class SimpleRectangularSubmodelComponent extends SubmodelComponent
@@ -69,7 +70,7 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
                        String pinName = newPinNames[i];
                        int oldPinIndex = pinNamesListThisSide.indexOf(pinName);
                        if (oldPinIndex == -1)
-                               super.addSubmodelInterface(pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance);
+                               super.addSubmodelInterface(new MovablePin(this, pinName, logicWidth, relX, pinDistance / 2 + i * pinDistance));
                        else
                                getSupermodelMovablePin(pinName).setRelPos(relX, pinDistance / 2 + i * pinDistance);
                }
@@ -108,8 +109,8 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
                {
                        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.drawText(pinName, getPosX() + width - textExtent.x - pinNameMargin,
+                                       getPosY() + i * pinDistance + (pinDistance - textExtent.y) / 2, true);
                }
                gc.setFont(oldFont);
        }
@@ -135,7 +136,7 @@ public class SimpleRectangularSubmodelComponent extends SubmodelComponent
        }
 
        @Override
-       protected Pin addSubmodelInterface(String name, int logicWidth, double relX, double relY)
+       protected Pin addSubmodelInterface(MovablePin supermodelPin)
        {
                throw new UnsupportedOperationException(
                                "Can't add submodel interfaces to a SimpleRectangularSubmodelComponent directly, call setInputPins / setOutputPins instead");
index 6aad07f..5756057 100644 (file)
@@ -80,10 +80,13 @@ public abstract class SubmodelComponent extends GUIComponent
        /**
         * Returns the submodel pin.
         */
-       protected Pin addSubmodelInterface(String name, int logicWidth, double relX, double relY)
+       protected Pin addSubmodelInterface(MovablePin supermodelPin)
        {
-               MovablePin submodelPin = new MovablePin(submodelInterface, name, logicWidth, relX / submodelScale, relY / submodelScale);
-               MovablePin supermodelPin = new MovablePin(this, name, logicWidth, relX, relY);
+               super.addPin(supermodelPin);// do this first to be fail-fast if the supermodel does not belong to this component
+
+               String name = supermodelPin.name;
+               MovablePin submodelPin = new MovablePin(submodelInterface, name, supermodelPin.logicWidth, supermodelPin.getRelX() / submodelScale,
+                               supermodelPin.getRelY() / submodelScale);
 
                submodelPin.addPinMovedListener(p ->
                {
@@ -101,7 +104,6 @@ public abstract class SubmodelComponent extends GUIComponent
                });
 
                submodelInterface.addPin(submodelPin);
-               super.addPin(supermodelPin);
 
                submodelPins.put(name, submodelPin);
                supermodelPins.put(name, supermodelPin);