Merge remote-tracking branch 'origin/development' into development
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / handles / HandleManager.java
index e4fb314..f3ef9a8 100644 (file)
@@ -8,6 +8,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
 import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
@@ -16,6 +17,7 @@ import net.mograsim.logic.model.editor.Editor;
 import net.mograsim.logic.model.editor.states.EditorState;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
 import net.mograsim.logic.model.editor.states.EditorState;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
+import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
 import net.mograsim.logic.model.model.wires.GUIWire;
 import net.mograsim.logic.model.model.wires.MovablePin;
 import net.mograsim.logic.model.model.wires.Pin;
 import net.mograsim.logic.model.model.wires.GUIWire;
 import net.mograsim.logic.model.model.wires.MovablePin;
 import net.mograsim.logic.model.model.wires.Pin;
@@ -85,12 +87,12 @@ public class HandleManager
                        ViewModelModifiable model = editor.getSubmodel();
                        Map<String, GUIComponent> compsByName = model.getComponentsByName();
                        Set<GUIComponent> comps = new HashSet<>(compsByName.values());
                        ViewModelModifiable model = editor.getSubmodel();
                        Map<String, GUIComponent> compsByName = model.getComponentsByName();
                        Set<GUIComponent> comps = new HashSet<>(compsByName.values());
-                       GUIComponent interfaceComp = compsByName.get("_submodelinterface");
+                       GUIComponent interfaceComp = compsByName.get(SubmodelComponent.SUBMODEL_INTERFACE_NAME);
                        comps.remove(interfaceComp);
                        registerInterfaceComponent(interfaceComp);
                        comps.forEach(c -> registerComponent(c));
 
                        comps.remove(interfaceComp);
                        registerInterfaceComponent(interfaceComp);
                        comps.forEach(c -> registerComponent(c));
 
-                       model.getWires().forEach(w -> registerWire(w));
+                       model.getWiresByName().values().forEach(w -> registerWire(w));
                        addHandle(cornerHandle = new CornerHandle(editor.toBeEdited));
                }
        }
                        addHandle(cornerHandle = new CornerHandle(editor.toBeEdited));
                }
        }
@@ -114,8 +116,13 @@ public class HandleManager
 
        private void registerWire(GUIWire wire)
        {
 
        private void registerWire(GUIWire wire)
        {
-               wire.addPathChangedListener((w, diff) ->
+               Point[] path = wire.getPath();
+               AtomicInteger oldLength = new AtomicInteger(path == null ? 0 : path.length);
+               wire.addPathChangedListener(w ->
                {
                {
+                       Point[] newPath = w.getPath();
+                       int newLength = newPath == null ? 0 : newPath.length;
+                       int diff = newLength - oldLength.getAndSet(newLength);
                        if (diff != 0)
                        {
                                if (diff > 0)
                        if (diff != 0)
                        {
                                if (diff > 0)
@@ -134,9 +141,9 @@ public class HandleManager
                        pointHandlesPerWire.get(w).forEach(h -> h.updatePos());
                });
                addWireHandle(wire);
                        pointHandlesPerWire.get(w).forEach(h -> h.updatePos());
                });
                addWireHandle(wire);
-               if (wire.getPath() == null)
+               if (path == null)
                        return;
                        return;
-               for (int i = 0; i < wire.getPath().length; i++)
+               for (int i = 0; i < path.length; i++)
                {
                        addWirePointHandle(wire);
                }
                {
                        addWirePointHandle(wire);
                }
@@ -210,11 +217,14 @@ public class HandleManager
 
        void destroyWirePointHandle(GUIWire owner, WirePointHandle h)
        {
 
        void destroyWirePointHandle(GUIWire owner, WirePointHandle h)
        {
-               List<WirePointHandle> handles = pointHandlesPerWire.get(owner);
-               int pointIndex = handles.indexOf(h);
-               handles.remove(pointIndex);
-               removeHandle(h);
-               owner.removePathPoint(pointIndex);
+               if (pointHandlesPerWire.containsKey(owner))
+               {
+                       List<WirePointHandle> handles = pointHandlesPerWire.get(owner);
+                       int pointIndex = handles.indexOf(h);
+                       handles.remove(pointIndex);
+                       removeHandle(h);
+                       owner.removePathPoint(pointIndex);
+               }
        }
 
        private void removeWirePointHandles(GUIWire owner)
        }
 
        private void removeWirePointHandles(GUIWire owner)
@@ -336,6 +346,7 @@ public class HandleManager
        {
                EditorState entryState = editor.stateManager.getState();
 
        {
                EditorState entryState = editor.stateManager.getState();
 
+               // TODO: As soon as wires connected to a component being removed also are removed, change priority
                if (!cornerHandle.click(clicked.x, clicked.y, stateMask, entryState))
                        if (!click(handlePerPin.values(), clicked, entryState, stateMask))
                                if (!click(handlePerInterfacePin.values(), clicked, entryState, stateMask))
                if (!cornerHandle.click(clicked.x, clicked.y, stateMask, entryState))
                        if (!click(handlePerPin.values(), clicked, entryState, stateMask))
                                if (!click(handlePerInterfacePin.values(), clicked, entryState, stateMask))
@@ -346,7 +357,7 @@ public class HandleManager
                entryState.clicked(clicked, stateMask);
        }
 
                entryState.clicked(clicked, stateMask);
        }
 
-       private boolean click(Collection<? extends Handle> handles, Point clicked, EditorState state, int stateMask)
+       private static boolean click(Collection<? extends Handle> handles, Point clicked, EditorState state, int stateMask)
        {
                for (Handle h : handles)
                        if (h.click(clicked.x, clicked.y, stateMask, state))
        {
                for (Handle h : handles)
                        if (h.click(clicked.x, clicked.y, stateMask, state))