Added handle priorities
authorFabian Stemmler <stemmler@in.tum.de>
Mon, 19 Aug 2019 20:13:57 +0000 (22:13 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Mon, 19 Aug 2019 20:13:57 +0000 (22:13 +0200)
Handle click events are now processed in order of handle priority

net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java
net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java [new file with mode: 0644]

index c3afb70..b44f1cf 100644 (file)
@@ -22,6 +22,7 @@ public class ComponentHandle extends Handle
 
        public ComponentHandle(GUIComponent parent)
        {
+               super(4);
                this.parent = parent;
                Rectangle bounds = parent.getBounds();
                setSize(bounds.width, bounds.height);
index 74eb080..959fb89 100644 (file)
@@ -15,7 +15,7 @@ public class CornerHandle extends Handle
 
        public CornerHandle(DeserializedSubmodelComponent toBeEdited)
        {
-               super();
+               super(0);
                this.toBeEdited = toBeEdited;
                setSize(LENGTH, LENGTH);
                initPos();
index c8f9392..cbfe2a2 100644 (file)
@@ -14,9 +14,11 @@ public abstract class Handle
 {
        private final Rectangle bounds;
        private final Collection<Runnable> redrawListeners, destroyListeners;
+       private final int priority;
 
-       public Handle()
+       public Handle(int priority)
        {
+               this.priority = priority;
                redrawListeners = new ArrayList<>();
                destroyListeners = new ArrayList<>();
                bounds = new Rectangle(0, 0, 0, 0);
@@ -122,6 +124,11 @@ public abstract class Handle
     public void onDeselect() {}
     //@formatter:on
 
+       public final int getPriority()
+       {
+               return priority;
+       }
+
        public abstract HandleType getType();
 
        public static enum HandleType
index f3ef9a8..24eb36d 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));
                }
        }
 
@@ -345,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);
        }
 
index 1b963a1..f585224 100644 (file)
@@ -19,7 +19,7 @@ public class InterfacePinHandle extends PinHandle
 
        public InterfacePinHandle(MovablePin parent, DeserializedSubmodelComponent pinOwner)
        {
-               super();
+               super(2);
                this.parent = parent;
                this.owner = pinOwner;
                setSize(CIRCLE_DIAM, CIRCLE_DIAM);
index 66c18ac..2e4a9dc 100644 (file)
@@ -4,9 +4,9 @@ import net.mograsim.logic.model.model.wires.Pin;
 
 public abstract class PinHandle extends Handle
 {
-       public PinHandle()
+       public PinHandle(int priority)
        {
-               super();
+               super(priority);
        }
 
        public abstract Pin getPin();
index 5170ba2..0f4a152 100644 (file)
@@ -15,7 +15,7 @@ public class StaticPinHandle extends PinHandle
 
        public StaticPinHandle(Pin parent)
        {
-               super();
+               super(1);
                this.parent = parent;
                setSize(CIRCLE_DIAM, CIRCLE_DIAM);
                parent.addPinMovedListener((p) -> updatePos());
index 199df99..7629dcb 100644 (file)
@@ -20,6 +20,7 @@ public class WireHandle extends Handle
 
        public WireHandle(GUIWire parent)
        {
+               super(5);
                this.parent = parent;
                parent.addPathChangedListener(c -> updateBounds());
                updateBounds();
index 48c7c22..4c0b9a3 100644 (file)
@@ -19,7 +19,7 @@ public class WirePointHandle extends Handle
 
        public WirePointHandle(HandleManager manager, GUIWire parent, int pointIndex)
        {
-               super();
+               super(3);
                this.manager = manager;
                this.parent = parent;
                this.pointIndex = pointIndex;
diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java
new file mode 100644 (file)
index 0000000..253dfdd
--- /dev/null
@@ -0,0 +1,98 @@
+package net.mograsim.logic.model.editor.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * Similar to a SortedSet, except it is allowed for multiple elements to have the same priority (<code>c.compare(e1, e2) == 0</code> is
+ * allowed to be true for two different elements e1 and e2). However, to elements are not allowed to be equal according to
+ * <code>Object.equals(Object o)</code>.
+ * 
+ * @author Fabian Stemmler
+ *
+ * @param <T> the type of elements in this list
+ */
+public class PrioritySet<T> implements Set<T>
+{
+       private ArrayList<T> list;
+       private Comparator<T> c;
+
+       public PrioritySet(Comparator<T> c)
+       {
+               setComparator(c);
+               list = new ArrayList<>();
+       }
+
+       @SuppressWarnings("unchecked")
+       public PrioritySet()
+       {
+               this((e1, e2) -> ((Comparable<T>) e1).compareTo(e2));
+       }
+
+       public void setComparator(Comparator<T> c)
+       {
+               this.c = c;
+       }
+
+       //@formatter:off
+       @Override
+       public int size() { return list.size(); }
+       @Override
+       public boolean isEmpty() { return list.isEmpty(); }
+       @Override
+       public boolean contains(Object o) { return list.isEmpty(); }
+       @Override
+       public Iterator<T> iterator() { return list.iterator(); }
+       @Override
+       public Object[] toArray() { return list.toArray(); }
+       @Override
+       public <E> E[] toArray(E[] a) { return list.toArray(a); }
+       @Override
+       public boolean remove(Object o) { return list.remove(o); }
+       @Override
+       public boolean containsAll(Collection<?> c) { return list.containsAll(c); }
+       @Override
+       public boolean removeAll(Collection<?> c) { return list.removeAll(c); }
+       @Override
+       public boolean retainAll(Collection<?> c) { return list.retainAll(c); }
+       @Override
+       public void clear() { list.clear(); }
+       //@formatter:on
+
+       @Override
+       public boolean add(T e)
+       {
+               if (isEmpty())
+               {
+                       list.add(e);
+                       return true;
+               }
+               int index = Collections.binarySearch(list, e, c);
+               if (index < 0)
+                       index = -index - 1;
+               if (index < size())
+               {
+                       if (list.get(index).equals(e))
+                               return false;
+                       list.add(index, e);
+               } else
+                       list.add(e);
+               return true;
+       }
+
+       @Override
+       public boolean addAll(Collection<? extends T> c)
+       {
+               return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b);
+       }
+
+       @Override
+       public String toString()
+       {
+               return list.toString();
+       }
+}