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=0a33509c3a72613b509016589ec4213289e76abb;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=91b0e3b07630d2ce32903a04edcab2b835a5ff3a;hpb=a00663c79d0e26b494ff79eee4b6c049f086c7e0;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 91b0e3b0..0a33509c 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,15 +8,18 @@ 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; 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.model.ViewModelModifiable; -import net.mograsim.logic.model.model.components.GUIComponent; -import net.mograsim.logic.model.model.wires.GUIWire; +import net.mograsim.logic.model.editor.util.PrioritySet; +import net.mograsim.logic.model.model.LogicModelModifiable; +import net.mograsim.logic.model.model.components.ModelComponent; +import net.mograsim.logic.model.model.components.submodels.SubmodelComponent; +import net.mograsim.logic.model.model.wires.ModelWire; import net.mograsim.logic.model.model.wires.MovablePin; import net.mograsim.logic.model.model.wires.Pin; @@ -24,19 +27,17 @@ public class HandleManager { private final Map handlePerPin; private final Map handlePerInterfacePin; - private final Map> pointHandlesPerWire; - private final Map handlePerWire; + private final Map> pointHandlesPerWire; + private final Map handlePerWire; private final Set handles; private final Set wirePointHandles; - private final Map handlePerComp; + private final Map handlePerComp; private final Collection> handleAddedListeners; private final Collection> handleRemovedListeners; private final Editor editor; private boolean initialized = false; - private CornerHandle cornerHandle; - public HandleManager(Editor editor) { this.editor = editor; @@ -44,14 +45,14 @@ 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<>(); handleAddedListeners = new ArrayList<>(); handleRemovedListeners = new ArrayList<>(); - ViewModelModifiable model = editor.getSubmodel(); + LogicModelModifiable model = editor.getSubmodel(); model.addComponentAddedListener(c -> registerComponent(c)); @@ -82,27 +83,27 @@ public class HandleManager System.err.println("Warning! HandleManager was already initialized."); else { - ViewModelModifiable model = editor.getSubmodel(); - Map compsByName = model.getComponentsByName(); - Set comps = new HashSet<>(compsByName.values()); - GUIComponent interfaceComp = compsByName.get("_submodelinterface"); + LogicModelModifiable model = editor.getSubmodel(); + Map compsByName = model.getComponentsByName(); + Set comps = new HashSet<>(compsByName.values()); + ModelComponent 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)); } } - private void registerInterfaceComponent(GUIComponent c) + private void registerInterfaceComponent(ModelComponent c) { c.getPins().values().forEach(p -> addInterfacePinHandle(p)); c.addPinAddedListener(p -> addInterfacePinHandle(p)); c.addPinRemovedListener(p -> removeInterfacePinHandle(p)); } - private void registerComponent(GUIComponent c) + private void registerComponent(ModelComponent c) { addComponentHandle(c); @@ -112,21 +113,26 @@ public class HandleManager c.addPinRemovedListener(p -> removePinHandle(p)); } - private void registerWire(GUIWire wire) + private void registerWire(ModelWire 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); } - + 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); } @@ -134,9 +140,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); } @@ -146,14 +152,14 @@ public class HandleManager // -- Adding/Removing handles -- /// /////////////////////////////////// - private void addComponentHandle(GUIComponent c) + private void addComponentHandle(ModelComponent c) { - ComponentHandle h = new ComponentHandle(c); + ComponentHandle h = new ComponentHandle(editor.getSubmodel(), c); handlePerComp.put(c, h); addHandle(h); } - private void removeComponentHandle(GUIComponent c) + private void removeComponentHandle(ModelComponent c) { ComponentHandle h = handlePerComp.get(c); handlePerComp.remove(c); @@ -176,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); @@ -191,7 +197,7 @@ public class HandleManager removeHandle(h); } - private void addWirePointHandle(GUIWire w) + private void addWirePointHandle(ModelWire w) { List wireHandles = pointHandlesPerWire.get(w); WirePointHandle h; @@ -207,19 +213,22 @@ public class HandleManager this.wirePointHandles.add(h); addHandle(h); } - - void destroyWirePointHandle(GUIWire owner, WirePointHandle h) + + void destroyWirePointHandle(ModelWire 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) + private void removeWirePointHandles(ModelWire owner) { - if(!pointHandlesPerWire.containsKey(owner)) + if (!pointHandlesPerWire.containsKey(owner)) return; pointHandlesPerWire.get(owner).forEach(h -> { @@ -229,14 +238,14 @@ public class HandleManager pointHandlesPerWire.remove(owner); } - private void addWireHandle(GUIWire w) + private void addWireHandle(ModelWire w) { - WireHandle h = new WireHandle(w); + WireHandle h = new WireHandle(editor.getSubmodel(), w); handlePerWire.put(w, h); addHandle(h); } - private void removeWireHandle(GUIWire w) + private void removeWireHandle(ModelWire w) { WireHandle h = handlePerWire.get(w); handlePerWire.remove(w); @@ -261,16 +270,16 @@ public class HandleManager return handlePerPin.get(parent); } - public ComponentHandle getHandle(GUIComponent parent) + public ComponentHandle getHandle(ModelComponent parent) { return handlePerComp.get(parent); } - public WireHandle getHandle(GUIWire parent) + public WireHandle getHandle(ModelWire parent) { return handlePerWire.get(parent); } - + public Handle getInterfacePinHandle(Pin p) { return handlePerInterfacePin.get(p); @@ -279,7 +288,7 @@ public class HandleManager /** * @return A Collection of the registered {@link WirePointHandle}s of the specified wire */ - public Collection getWirePointHandles(GUIWire parent) + public Collection getWirePointHandles(ModelWire parent) { return pointHandlesPerWire.get(parent).stream().collect(Collectors.toSet()); } @@ -299,7 +308,7 @@ public class HandleManager { return Collections.unmodifiableCollection(handlePerPin.values()); } - + /** * @return An unmodifiable view of all registered {@link InterfacePinHandle}s */ @@ -335,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))