X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model.editor%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Feditor%2Fhandles%2FHandleManager.java;h=518900381d9cda8c401b87c4bcbda2a6f7701f10;hb=148a58630b38b30d4d24a21e3f55c357f5b4d0bc;hp=c5e06a54cc7bb2d634903a9a647fafcb02b140dc;hpb=657f9c901e20c7b2efe53e0daa4b56c8082854b9;p=Mograsim.git 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 c5e06a54..51890038 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,8 +15,10 @@ 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; import net.mograsim.logic.model.model.wires.GUIWire; import net.mograsim.logic.model.model.wires.MovablePin; import net.mograsim.logic.model.model.wires.Pin; @@ -36,8 +38,6 @@ public class HandleManager private final Editor editor; private boolean initialized = false; - private CornerHandle cornerHandle; - public HandleManager(Editor editor) { this.editor = editor; @@ -45,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<>(); @@ -86,13 +86,13 @@ public class HandleManager ViewModelModifiable model = editor.getSubmodel(); Map compsByName = model.getComponentsByName(); Set 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)); - - model.getWires().forEach(w -> registerWire(w)); - addHandle(cornerHandle = new CornerHandle(editor.toBeEdited)); + + model.getWiresByName().values().forEach(w -> registerWire(w)); + addHandle(new CornerHandle(editor.toBeEdited)); } } @@ -121,18 +121,18 @@ public class HandleManager { Point[] newPath = w.getPath(); int newLength = newPath == null ? 0 : newPath.length; - int diff = oldLength.getAndSet(newLength) - newLength; - if(diff != 0) + int diff = newLength - oldLength.getAndSet(newLength); + if (diff != 0) { - if(diff > 0) + if (diff > 0) { - for(int i = 0; i < diff; i++) + for (int i = 0; i < diff; i++) addWirePointHandle(w); } - + List wpHandles = pointHandlesPerWire.get(w); int size = wpHandles.size(); - for(int i = 0; i < size; i++) + for (int i = 0; i < size; i++) { wpHandles.get(i).setIndex(i); } @@ -154,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); } @@ -182,8 +182,8 @@ public class HandleManager private void addInterfacePinHandle(Pin p) { - //The following is not an alternative to the cast, because the new pin is not yet in the map, when the listener is called - //editor.toBeEdited.getSubmodelMovablePins().get(p.name); + // The following is not an alternative to the cast, because the new pin is not yet in the map, when the listener is called + // editor.toBeEdited.getSubmodelMovablePins().get(p.name); MovablePin pM = (MovablePin) p; InterfacePinHandle h = new InterfacePinHandle(pM, editor.toBeEdited); handlePerInterfacePin.put(pM, h); @@ -213,19 +213,22 @@ public class HandleManager this.wirePointHandles.add(h); addHandle(h); } - + void destroyWirePointHandle(GUIWire owner, WirePointHandle h) { - List handles = pointHandlesPerWire.get(owner); - int pointIndex = handles.indexOf(h); - handles.remove(pointIndex); - removeHandle(h); - owner.removePathPoint(pointIndex); + if (pointHandlesPerWire.containsKey(owner)) + { + List handles = pointHandlesPerWire.get(owner); + int pointIndex = handles.indexOf(h); + handles.remove(pointIndex); + removeHandle(h); + owner.removePathPoint(pointIndex); + } } private void removeWirePointHandles(GUIWire owner) { - if(!pointHandlesPerWire.containsKey(owner)) + if (!pointHandlesPerWire.containsKey(owner)) return; pointHandlesPerWire.get(owner).forEach(h -> { @@ -237,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); } @@ -276,7 +279,7 @@ public class HandleManager { return handlePerWire.get(parent); } - + public Handle getInterfacePinHandle(Pin p) { return handlePerInterfacePin.get(p); @@ -305,7 +308,7 @@ public class HandleManager { return Collections.unmodifiableCollection(handlePerPin.values()); } - + /** * @return An unmodifiable view of all registered {@link InterfacePinHandle}s */ @@ -341,18 +344,13 @@ public class HandleManager public void click(Point clicked, int stateMask) { EditorState entryState = editor.stateManager.getState(); - - 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); } - private boolean click(Collection handles, Point clicked, EditorState state, int stateMask) + private static boolean click(Collection handles, Point clicked, EditorState state, int stateMask) { for (Handle h : handles) if (h.click(clicked.x, clicked.y, stateMask, state))