Move components to an actual resource folder (that will remain in jar)
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / handles / HandleManager.java
index 7fe6e43..5189003 100644 (file)
@@ -15,6 +15,7 @@ import java.util.stream.Collectors;
 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
 import net.mograsim.logic.model.editor.Editor;
 import net.mograsim.logic.model.editor.states.EditorState;
+import net.mograsim.logic.model.editor.util.PrioritySet;
 import net.mograsim.logic.model.model.ViewModelModifiable;
 import net.mograsim.logic.model.model.components.GUIComponent;
 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
@@ -37,8 +38,6 @@ public class HandleManager
        private final Editor editor;
        private boolean initialized = false;
 
-       private CornerHandle cornerHandle;
-
        public HandleManager(Editor editor)
        {
                this.editor = editor;
@@ -46,7 +45,7 @@ public class HandleManager
                handlePerInterfacePin = new HashMap<>();
                pointHandlesPerWire = new HashMap<>();
                handlePerComp = new HashMap<>();
-               handles = new HashSet<>();
+               handles = new PrioritySet<>((a, b) -> Integer.compare(a.getPriority(), b.getPriority()));
                wirePointHandles = new HashSet<>();
                handlePerWire = new HashMap<>();
 
@@ -93,7 +92,7 @@ public class HandleManager
                        comps.forEach(c -> registerComponent(c));
 
                        model.getWiresByName().values().forEach(w -> registerWire(w));
-                       addHandle(cornerHandle = new CornerHandle(editor.toBeEdited));
+                       addHandle(new CornerHandle(editor.toBeEdited));
                }
        }
 
@@ -122,7 +121,7 @@ public class HandleManager
                {
                        Point[] newPath = w.getPath();
                        int newLength = newPath == null ? 0 : newPath.length;
-                       int diff = oldLength.getAndSet(newLength) - newLength;
+                       int diff = newLength - oldLength.getAndSet(newLength);
                        if (diff != 0)
                        {
                                if (diff > 0)
@@ -155,7 +154,7 @@ public class HandleManager
 
        private void addComponentHandle(GUIComponent c)
        {
-               ComponentHandle h = new ComponentHandle(c);
+               ComponentHandle h = new ComponentHandle(editor.getSubmodel(), c);
                handlePerComp.put(c, h);
                addHandle(h);
        }
@@ -217,11 +216,14 @@ public class HandleManager
 
        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)
@@ -238,7 +240,7 @@ public class HandleManager
 
        private void addWireHandle(GUIWire w)
        {
-               WireHandle h = new WireHandle(w);
+               WireHandle h = new WireHandle(editor.getSubmodel(), w);
                handlePerWire.put(w, h);
                addHandle(h);
        }
@@ -342,15 +344,9 @@ public class HandleManager
        public void click(Point clicked, int stateMask)
        {
                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 (!click(getWirePointHandles(), clicked, entryState, stateMask))
-                                               if (!click(getWireHandles(), clicked, entryState, stateMask))
-                                                       if (!click(handlePerComp.values(), clicked, entryState, stateMask))
-                                                               entryState.clickedEmpty(clicked, stateMask);
+               // TODO: As soon as wires connected to a component being removed also are removed, change priority)
+               if (!click(handles, clicked, entryState, stateMask))
+                       entryState.clickedEmpty(clicked, stateMask);
                entryState.clicked(clicked, stateMask);
        }