public ComponentHandle(GUIComponent parent)
{
+ super(4);
this.parent = parent;
Rectangle bounds = parent.getBounds();
setSize(bounds.width, bounds.height);
public CornerHandle(DeserializedSubmodelComponent toBeEdited)
{
- super();
+ super(0);
this.toBeEdited = toBeEdited;
setSize(LENGTH, LENGTH);
initPos();
{
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);
public void onDeselect() {}
//@formatter:on
+ public final int getPriority()
+ {
+ return priority;
+ }
+
public abstract HandleType getType();
public static enum HandleType
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;
private final Editor editor;
private boolean initialized = false;
- private CornerHandle cornerHandle;
-
public HandleManager(Editor editor)
{
this.editor = editor;
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<>();
comps.forEach(c -> registerComponent(c));
model.getWiresByName().values().forEach(w -> registerWire(w));
- addHandle(cornerHandle = new CornerHandle(editor.toBeEdited));
+ addHandle(new CornerHandle(editor.toBeEdited));
}
}
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);
}
public InterfacePinHandle(MovablePin parent, DeserializedSubmodelComponent pinOwner)
{
- super();
+ super(2);
this.parent = parent;
this.owner = pinOwner;
setSize(CIRCLE_DIAM, CIRCLE_DIAM);
public abstract class PinHandle extends Handle
{
- public PinHandle()
+ public PinHandle(int priority)
{
- super();
+ super(priority);
}
public abstract Pin getPin();
public StaticPinHandle(Pin parent)
{
- super();
+ super(1);
this.parent = parent;
setSize(CIRCLE_DIAM, CIRCLE_DIAM);
parent.addPinMovedListener((p) -> updatePos());
public WireHandle(GUIWire parent)
{
+ super(5);
this.parent = parent;
parent.addPathChangedListener(c -> updateBounds());
updateBounds();
public WirePointHandle(HandleManager manager, GUIWire parent, int pointIndex)
{
- super();
+ super(3);
this.manager = manager;
this.parent = parent;
this.pointIndex = pointIndex;
--- /dev/null
+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();
+ }
+}