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.IndirectGUIComponentCreator;
29 public final class Editor
31 final Selection selection = new Selection();
32 final Set<ComponentInfo> copyBuffer = new HashSet<>();
33 public final DeserializedSubmodelComponent toBeEdited;
34 public final HandleManager handleManager;
35 final static Map<GUIComponent, String> identifierPerComponent = new HashMap<>();
36 public final EditorGUI gui;
37 public final StateManager stateManager;
38 private final SaveLoadManager saveManager;
39 private Snapping snapping = Snapping.ABSOLUTE;
40 private double snapX = 5, snapY = 5;
41 public final DialogManager dialogManager;
42 public final EditorUserInput userInput;
44 public Editor(DeserializedSubmodelComponent toBeEdited)
46 this.toBeEdited = toBeEdited;
47 handleManager = new HandleManager(this);
48 gui = new EditorGUI(this);
49 userInput = new EditorUserInput(this);
50 stateManager = new StateManager(this);
52 saveManager = new SaveLoadManager(this);
53 dialogManager = new DialogManager(gui.shell);
55 toBeEdited.submodel.addComponentRemovedListener(c -> identifierPerComponent.remove(c));
60 public ViewModelModifiable getSubmodel()
62 return toBeEdited.getSubmodelModifiable();
65 public Selection getSelection()
70 // TODO: Remove this error prone method: Relative offset may change between multiple moves,
71 // because Handles have different ways of responding to reqMove(...), causing strange behaviour
73 public void moveSelection(double x, double y)
75 Point ref = selection.getTopLeft();
76 Point snapped = new Point(x, y);
77 applySnapping(snapped);
79 for (Handle c : selection)
82 newX = snapped.x + c.getPosX() - ref.x;
83 newY = snapped.y + c.getPosY() - ref.y;
84 c.reqMove(newX, newY);
88 public void moveHandles(double x, double y, Map<Handle, Point> handleOffsetMap)
90 Point snapped = new Point(x, y);
91 applySnapping(snapped);
93 for (Handle c : handleOffsetMap.keySet())
95 Point offset = handleOffsetMap.get(c);
97 newX = snapped.x + offset.x;
98 newY = snapped.y + offset.y;
99 c.reqMove(newX, newY);
103 public void deleteSelection()
105 selection.forEach(h -> h.reqDelete());
112 Point refPoint = selection.getTopLeft();
113 for (Handle h : selection)
115 Optional<ComponentInfo> cInfo = h.reqCopy(refPoint);
116 if (cInfo.isPresent())
117 copyBuffer.add(cInfo.get());
121 public void paste(double x, double y)
124 for (ComponentInfo info : copyBuffer)
126 GUIComponent comp = addComponent(info.identifier, info.params);
127 ComponentHandle h = handleManager.getHandle(comp);
128 h.reqMove(info.relX, info.relY);
141 saveManager.openSaveAsDialog();
144 public void addComponent(double x, double y)
146 boolean successful = false;
147 JsonElement params = JsonNull.INSTANCE;
148 outer: while (!successful)
150 String selected = gui.getAddListSelected();
153 GUIComponent c = addComponent(selected, params);
155 selection.add(handleManager.getHandle(c));
159 catch (@SuppressWarnings("unused") UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
161 String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:");
164 params = new JsonParser().parse(result);
169 private GUIComponent addComponent(String identifier, JsonElement params)
171 GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier, params);
172 identifierPerComponent.put(comp, identifier);
176 public static String getIdentifier(GUIComponent c)
178 return identifierPerComponent.get(c);
181 public void duplicate()
184 Point origin = selection.getTopLeft();
185 paste(origin.x + 20, origin.y + 20);
188 private void applySnapping(Point newP)
195 newP.x = (int) (newP.x / snapX + .5) * snapX;
196 newP.y = (int) (newP.y / snapY + .5) * snapY;
203 public static class ComponentInfo
205 public final double relX, relY;
206 public final String identifier;
207 public final JsonElement params;
209 public ComponentInfo(double relX, double relY, String identifier, JsonElement params)
213 this.identifier = identifier;
214 this.params = params;
218 @SuppressWarnings("unused")
219 public void addWire(PinHandle a, PinHandle b)
221 new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]);
224 public static enum Snapping
229 public String toString()
231 return super.toString().toLowerCase();
235 public static void main(String[] args) throws IOException
237 SaveLoadManager.openLoadDialog();
240 public Snapping getSnapping()
245 public void setSnapping(Snapping snapping)
247 this.snapping = snapping;