X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model.editor%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Feditor%2FEditor.java;h=e3279150e4710f780cebd3302fa066f1698c07f3;hb=ec0a13e824a9058dc41ba0952fdf8ad99fb19772;hp=36dcbc7f1aa1ad6698826544746131e66c371f85;hpb=47ea68ed5c444dd14864412639f6a6fd60ab8a0f;p=Mograsim.git diff --git a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java index 36dcbc7f..e3279150 100644 --- a/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java +++ b/net.mograsim.logic.model.editor/src/net/mograsim/logic/model/editor/Editor.java @@ -1,5 +1,6 @@ package net.mograsim.logic.model.editor; +import java.io.IOException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -7,7 +8,9 @@ import java.util.Optional; import java.util.Set; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.google.gson.JsonNull; +import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import net.haspamelodica.swt.helper.swtobjectwrappers.Point; import net.mograsim.logic.model.editor.handles.ComponentHandle; @@ -15,6 +18,8 @@ import net.mograsim.logic.model.editor.handles.Handle; import net.mograsim.logic.model.editor.handles.HandleManager; import net.mograsim.logic.model.editor.handles.PinHandle; import net.mograsim.logic.model.editor.states.StateManager; +import net.mograsim.logic.model.editor.ui.DialogManager; +import net.mograsim.logic.model.editor.ui.EditorGUI; import net.mograsim.logic.model.model.ViewModelModifiable; import net.mograsim.logic.model.model.components.GUIComponent; import net.mograsim.logic.model.model.wires.GUIWire; @@ -31,25 +36,27 @@ public final class Editor public final EditorGUI gui; public final StateManager stateManager; private final SaveLoadManager saveManager; - Snapping snapping = Snapping.ABSOLUTE; + private Snapping snapping = Snapping.ABSOLUTE; private double snapX = 5, snapY = 5; public final DialogManager dialogManager; + public final EditorUserInput userInput; public Editor(DeserializedSubmodelComponent toBeEdited) { this.toBeEdited = toBeEdited; handleManager = new HandleManager(this); gui = new EditorGUI(this); + userInput = new EditorUserInput(this); stateManager = new StateManager(this); handleManager.init(); saveManager = new SaveLoadManager(this); dialogManager = new DialogManager(gui.shell); toBeEdited.submodel.addComponentRemovedListener(c -> identifierPerComponent.remove(c)); - + gui.open(); } - + public ViewModelModifiable getSubmodel() { return toBeEdited.getSubmodelModifiable(); @@ -60,8 +67,8 @@ public final class Editor return selection; } - //TODO: Remove this error prone method: Relative offset may change between multiple moves, - //because Handles have different ways of responding to reqMove(...), causing strange behaviour + // TODO: Remove this error prone method: Relative offset may change between multiple moves, + // because Handles have different ways of responding to reqMove(...), causing strange behaviour @Deprecated public void moveSelection(double x, double y) { @@ -77,7 +84,7 @@ public final class Editor c.reqMove(newX, newY); } } - + public void moveHandles(double x, double y, Map handleOffsetMap) { Point snapped = new Point(x, y); @@ -106,7 +113,7 @@ public final class Editor for (Handle h : selection) { Optional cInfo = h.reqCopy(refPoint); - if(cInfo.isPresent()) + if (cInfo.isPresent()) copyBuffer.add(cInfo.get()); } } @@ -123,28 +130,49 @@ public final class Editor } moveSelection(x, y); } - + public void save() { saveManager.save(); } + public void saveAs() + { + saveManager.openSaveAsDialog(); + } + public void addComponent(double x, double y) { - GUIComponent c = addComponent(gui.getAddListSelected(), new JsonObject()); - selection.clear(); - selection.add(handleManager.getHandle(c)); - moveSelection(x, y); + boolean successful = false; + JsonElement params = JsonNull.INSTANCE; + outer: while (!successful) + { + String selected = gui.getAddListSelected(); + try + { + GUIComponent c = addComponent(selected, params); + selection.clear(); + selection.add(handleManager.getHandle(c)); + moveSelection(x, y); + successful = true; + } + catch (@SuppressWarnings("unused") UnsupportedOperationException | JsonSyntaxException | NumberFormatException e) + { + String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:"); + if (result == null) + break outer; + params = new JsonParser().parse(result); + } + } } - + private GUIComponent addComponent(String identifier, JsonElement params) { - GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier, - params); + GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier, params); identifierPerComponent.put(comp, identifier); return comp; } - + public static String getIdentifier(GUIComponent c) { return identifierPerComponent.get(c); @@ -164,8 +192,10 @@ public final class Editor case OFF: break; case ABSOLUTE: - newP.x -= newP.x % snapX; - newP.y -= newP.y % snapY; + newP.x = (int) (newP.x / snapX + .5) * snapX; + newP.y = (int) (newP.y / snapY + .5) * snapY; + break; + default: break; } } @@ -175,7 +205,7 @@ public final class Editor public final double relX, relY; public final String identifier; public final JsonElement params; - + public ComponentInfo(double relX, double relY, String identifier, JsonElement params) { this.relX = relX; @@ -185,31 +215,35 @@ public final class Editor } } - public Point getCanvasMousePosition() + @SuppressWarnings("unused") + public void addWire(PinHandle a, PinHandle b) { - //TODO - org.eclipse.swt.graphics.Point canvasLoc = gui.logicCanvas.getLocation(), - mouseLoc = gui.display.getCursorLocation(), shellLoc = gui.shell.getLocation(); - return new Point(mouseLoc.x - shellLoc.x - canvasLoc.x, mouseLoc.y - shellLoc.y - canvasLoc.y); + new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]); } - public Point getWorldMousePosition() + public static enum Snapping { - return gui.logicCanvas.canvasToWorldCoords(getCanvasMousePosition()); + OFF, ABSOLUTE; + + @Override + public String toString() + { + return super.toString().toLowerCase(); + } } - public void addWire(PinHandle a, PinHandle b) + public static void main(String[] args) throws IOException { - new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]); + SaveLoadManager.openLoadDialog(); } - public static enum Snapping + public Snapping getSnapping() { - OFF, ABSOLUTE; + return snapping; } - - public static void main(String[] args) + + public void setSnapping(Snapping snapping) { - SaveLoadManager.openLoadDialog(); + this.snapping = snapping; } }