From bffda9003a621f34f431230c265b76a79597a1ae Mon Sep 17 00:00:00 2001 From: Fabian Stemmler Date: Mon, 19 Aug 2019 22:13:57 +0200 Subject: [PATCH] Added handle priorities Handle click events are now processed in order of handle priority --- .../model/editor/handles/ComponentHandle.java | 1 + .../model/editor/handles/CornerHandle.java | 2 +- .../logic/model/editor/handles/Handle.java | 9 +- .../model/editor/handles/HandleManager.java | 19 ++-- .../editor/handles/InterfacePinHandle.java | 2 +- .../logic/model/editor/handles/PinHandle.java | 4 +- .../model/editor/handles/StaticPinHandle.java | 2 +- .../model/editor/handles/WireHandle.java | 1 + .../model/editor/handles/WirePointHandle.java | 2 +- .../logic/model/editor/util/PrioritySet.java | 98 +++++++++++++++++++ 10 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java index c3afb701..b44f1cf0 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/ComponentHandle.java @@ -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); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java index 74eb080e..959fb896 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/CornerHandle.java @@ -15,7 +15,7 @@ public class CornerHandle extends Handle public CornerHandle(DeserializedSubmodelComponent toBeEdited) { - super(); + super(0); this.toBeEdited = toBeEdited; setSize(LENGTH, LENGTH); initPos(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java index c8f93922..cbfe2a2a 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/Handle.java @@ -14,9 +14,11 @@ public abstract class Handle { private final Rectangle bounds; private final Collection 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 diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java index f3ef9a88..24eb36d7 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/HandleManager.java @@ -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); } diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java index 1b963a13..f585224a 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/InterfacePinHandle.java @@ -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); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java index 66c18acb..2e4a9dc6 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/PinHandle.java @@ -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(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java index 5170ba25..0f4a1524 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/StaticPinHandle.java @@ -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()); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java index 199df99c..7629dcb1 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WireHandle.java @@ -20,6 +20,7 @@ public class WireHandle extends Handle public WireHandle(GUIWire parent) { + super(5); this.parent = parent; parent.addPathChangedListener(c -> updateBounds()); updateBounds(); diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java index 48c7c227..4c0b9a31 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/handles/WirePointHandle.java @@ -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 index 00000000..253dfdd3 --- /dev/null +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/util/PrioritySet.java @@ -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 (c.compare(e1, e2) == 0 is + * allowed to be true for two different elements e1 and e2). However, to elements are not allowed to be equal according to + * Object.equals(Object o). + * + * @author Fabian Stemmler + * + * @param the type of elements in this list + */ +public class PrioritySet implements Set +{ + private ArrayList list; + private Comparator c; + + public PrioritySet(Comparator c) + { + setComparator(c); + list = new ArrayList<>(); + } + + @SuppressWarnings("unchecked") + public PrioritySet() + { + this((e1, e2) -> ((Comparable) e1).compareTo(e2)); + } + + public void setComparator(Comparator 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 iterator() { return list.iterator(); } + @Override + public Object[] toArray() { return list.toArray(); } + @Override + public 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 c) + { + return c.stream().map(e -> add(e)).reduce(false, (a, b) -> a || b); + } + + @Override + public String toString() + { + return list.toString(); + } +} -- 2.17.1