1 package net.mograsim.logic.model.editor;
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.HashSet;
7 import java.util.Optional;
10 import com.google.gson.JsonElement;
11 import com.google.gson.JsonNull;
12 import com.google.gson.JsonParser;
13 import com.google.gson.JsonSyntaxException;
15 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
16 import net.mograsim.logic.model.editor.handles.ComponentHandle;
17 import net.mograsim.logic.model.editor.handles.Handle;
18 import net.mograsim.logic.model.editor.handles.HandleManager;
19 import net.mograsim.logic.model.editor.handles.PinHandle;
20 import net.mograsim.logic.model.editor.states.StateManager;
21 import net.mograsim.logic.model.editor.ui.DialogManager;
22 import net.mograsim.logic.model.editor.ui.EditorGUI;
23 import net.mograsim.logic.model.model.ViewModelModifiable;
24 import net.mograsim.logic.model.model.components.GUIComponent;
25 import net.mograsim.logic.model.model.wires.GUIWire;
26 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
27 import net.mograsim.logic.model.serializing.IdentifierGetter;
28 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
30 public final class Editor
32 final Selection selection = new Selection();
33 final Set<ComponentInfo> copyBuffer = new HashSet<>();
34 public final DeserializedSubmodelComponent toBeEdited;
35 public final HandleManager handleManager;
36 final static Map<GUIComponent, String> identifierPerComponent = new HashMap<>();
37 public final EditorGUI gui;
38 public final StateManager stateManager;
39 private final SaveLoadManager saveManager;
40 private Snapping snapping = Snapping.ABSOLUTE;
41 private double snapX = 5, snapY = 5;
42 public final DialogManager dialogManager;
43 public final EditorUserInput userInput;
45 public Editor(DeserializedSubmodelComponent toBeEdited)
47 this.toBeEdited = toBeEdited;
48 handleManager = new HandleManager(this);
49 gui = new EditorGUI(this);
50 userInput = new EditorUserInput(this);
51 stateManager = new StateManager(this);
53 saveManager = new SaveLoadManager(this);
54 dialogManager = new DialogManager(gui.shell);
56 toBeEdited.submodel.addComponentRemovedListener(c -> identifierPerComponent.remove(c));
61 public ViewModelModifiable getSubmodel()
63 return toBeEdited.getSubmodelModifiable();
66 public Selection getSelection()
71 // TODO: Remove this error prone method: Relative offset may change between multiple moves,
72 // because Handles have different ways of responding to reqMove(...), causing strange behaviour
74 public void moveSelection(double x, double y)
76 Point ref = selection.getTopLeft();
77 Point snapped = new Point(x, y);
78 applySnapping(snapped);
80 for (Handle c : selection)
83 newX = snapped.x + c.getPosX() - ref.x;
84 newY = snapped.y + c.getPosY() - ref.y;
85 c.reqMove(newX, newY);
89 public void moveHandles(double x, double y, Map<Handle, Point> handleOffsetMap)
91 Point snapped = new Point(x, y);
92 applySnapping(snapped);
94 for (Handle c : handleOffsetMap.keySet())
96 Point offset = handleOffsetMap.get(c);
98 newX = snapped.x + offset.x;
99 newY = snapped.y + offset.y;
100 c.reqMove(newX, newY);
104 public void deleteSelection()
106 selection.forEach(h -> h.reqDelete());
113 Point refPoint = selection.getTopLeft();
114 for (Handle h : selection)
116 Optional<ComponentInfo> cInfo = h.reqCopy(refPoint);
117 if (cInfo.isPresent())
118 copyBuffer.add(cInfo.get());
122 public void paste(double x, double y)
125 for (ComponentInfo info : copyBuffer)
127 GUIComponent comp = addComponent(info.identifier, info.params);
128 ComponentHandle h = handleManager.getHandle(comp);
129 h.reqMove(info.relX, info.relY);
142 saveManager.openSaveAsDialog();
145 public void addComponent(double x, double y)
147 boolean successful = false;
148 JsonElement params = JsonNull.INSTANCE;
149 outer: while (!successful)
151 String selected = gui.getAddListSelected();
154 GUIComponent c = addComponent(selected, params);
156 selection.add(handleManager.getHandle(c));
160 catch (@SuppressWarnings("unused") UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
162 String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:");
165 params = new JsonParser().parse(result);
170 private GUIComponent addComponent(String identifier, JsonElement params)
172 GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier, params);
173 identifierPerComponent.put(comp, identifier);
177 public static String getIdentifier(GUIComponent c)
179 if (identifierPerComponent.containsKey(c))
180 return identifierPerComponent.get(c);
181 return new IdentifierGetter().componentIDs.apply(c);
184 public void duplicate()
187 Point origin = selection.getTopLeft();
188 paste(origin.x + 20, origin.y + 20);
191 private void applySnapping(Point newP)
198 newP.x = (int) (newP.x / snapX + .5) * snapX;
199 newP.y = (int) (newP.y / snapY + .5) * snapY;
206 public static class ComponentInfo
208 public final double relX, relY;
209 public final String identifier;
210 public final JsonElement params;
212 public ComponentInfo(double relX, double relY, String identifier, JsonElement params)
216 this.identifier = identifier;
217 this.params = params;
221 @SuppressWarnings("unused")
222 public void addWire(PinHandle a, PinHandle b)
224 new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]);
227 public static enum Snapping
232 public String toString()
234 return super.toString().toLowerCase();
238 public static void main(String[] args) throws IOException
240 SaveLoadManager.openLoadDialog();
243 public Snapping getSnapping()
248 public void setSnapping(Snapping snapping)
250 this.snapping = snapping;