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=f3ef9a88f1df491ec37253d0539e4ad6136724c2;hb=53e2bb4363368cc84b12da42bdd228d69b2aca9b;hp=f999c96a28324d690a23127c70596d6ee08ff46a;hpb=6ff37062ffff3ee3499e15b8d8bf532b4eaa6608;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 f999c96a..f3ef9a88 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 @@ -8,6 +8,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -16,6 +17,7 @@ import net.mograsim.logic.model.editor.Editor; import net.mograsim.logic.model.editor.states.EditorState; 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; @@ -85,12 +87,12 @@ 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)); + + model.getWiresByName().values().forEach(w -> registerWire(w)); addHandle(cornerHandle = new CornerHandle(editor.toBeEdited)); } } @@ -114,26 +116,24 @@ public class HandleManager private void registerWire(GUIWire wire) { - wire.addPathChangedListener((w, diff) -> + Point[] path = wire.getPath(); + AtomicInteger oldLength = new AtomicInteger(path == null ? 0 : path.length); + wire.addPathChangedListener(w -> { - if(diff != 0) + Point[] newPath = w.getPath(); + int newLength = newPath == null ? 0 : newPath.length; + 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); } - else - { - for(int i = 0; i > diff; i--) - { - removeLastWirePointHandle(wire); - } - } - + 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); } @@ -141,9 +141,9 @@ public class HandleManager pointHandlesPerWire.get(w).forEach(h -> h.updatePos()); }); addWireHandle(wire); - if (wire.getPath() == null) + if (path == null) return; - for (int i = 0; i < wire.getPath().length; i++) + for (int i = 0; i < path.length; i++) { addWirePointHandle(wire); } @@ -183,8 +183,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); @@ -203,11 +203,11 @@ public class HandleManager List wireHandles = pointHandlesPerWire.get(w); WirePointHandle h; if (wireHandles != null) - wireHandles.add(h = new WirePointHandle(w, wireHandles.size())); + wireHandles.add(h = new WirePointHandle(this, w, wireHandles.size())); else { wireHandles = new ArrayList<>(); - h = new WirePointHandle(w, 0); + h = new WirePointHandle(this, w, 0); wireHandles.add(h); pointHandlesPerWire.put(h.parent, wireHandles); } @@ -215,16 +215,21 @@ public class HandleManager addHandle(h); } - private void removeLastWirePointHandle(GUIWire owner) + void destroyWirePointHandle(GUIWire owner, WirePointHandle h) { - Handle h = pointHandlesPerWire.get(owner).remove(0); - wirePointHandles.remove(h); - removeHandle(h); + 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 -> { @@ -275,7 +280,7 @@ public class HandleManager { return handlePerWire.get(parent); } - + public Handle getInterfacePinHandle(Pin p) { return handlePerInterfacePin.get(p); @@ -304,7 +309,7 @@ public class HandleManager { return Collections.unmodifiableCollection(handlePerPin.values()); } - + /** * @return An unmodifiable view of all registered {@link InterfacePinHandle}s */ @@ -341,17 +346,18 @@ public class HandleManager { 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(handlePerComp.values(), clicked, entryState, stateMask)) - if (!click(getWireHandles(), clicked, entryState, stateMask)) + if (!click(getWireHandles(), clicked, entryState, stateMask)) + if (!click(handlePerComp.values(), 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))