Added file menu to Editor
[Mograsim.git] / net.mograsim.logic.model.editor / src / net / mograsim / logic / model / editor / Editor.java
1 package net.mograsim.logic.model.editor;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Optional;
7 import java.util.Set;
8
9 import com.google.gson.JsonElement;
10 import com.google.gson.JsonNull;
11 import com.google.gson.JsonParser;
12 import com.google.gson.JsonSyntaxException;
13
14 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
15 import net.mograsim.logic.model.editor.handles.ComponentHandle;
16 import net.mograsim.logic.model.editor.handles.Handle;
17 import net.mograsim.logic.model.editor.handles.HandleManager;
18 import net.mograsim.logic.model.editor.handles.PinHandle;
19 import net.mograsim.logic.model.editor.states.StateManager;
20 import net.mograsim.logic.model.editor.ui.DialogManager;
21 import net.mograsim.logic.model.editor.ui.EditorGUI;
22 import net.mograsim.logic.model.model.ViewModelModifiable;
23 import net.mograsim.logic.model.model.components.GUIComponent;
24 import net.mograsim.logic.model.model.wires.GUIWire;
25 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
26 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
27
28 public final class Editor
29 {
30         final Selection selection = new Selection();
31         final Set<ComponentInfo> copyBuffer = new HashSet<>();
32         public final DeserializedSubmodelComponent toBeEdited;
33         public final HandleManager handleManager;
34         final static Map<GUIComponent, String> identifierPerComponent = new HashMap<>();
35         public final EditorGUI gui;
36         public final StateManager stateManager;
37         private final SaveLoadManager saveManager;
38         private Snapping snapping = Snapping.ABSOLUTE;
39         private double snapX = 5, snapY = 5;
40         public final DialogManager dialogManager;
41         public final EditorUserInput userInput;
42
43         public Editor(DeserializedSubmodelComponent toBeEdited)
44         {
45                 this.toBeEdited = toBeEdited;
46                 handleManager = new HandleManager(this);
47                 gui = new EditorGUI(this);
48                 userInput = new EditorUserInput(this);
49                 stateManager = new StateManager(this);
50                 handleManager.init();
51                 saveManager = new SaveLoadManager(this);
52                 dialogManager = new DialogManager(gui.shell);
53
54                 toBeEdited.submodel.addComponentRemovedListener(c -> identifierPerComponent.remove(c));
55                 
56                 gui.open();
57         }
58         
59         public ViewModelModifiable getSubmodel()
60         {
61                 return toBeEdited.getSubmodelModifiable();
62         }
63
64         public Selection getSelection()
65         {
66                 return selection;
67         }
68
69         //TODO: Remove this error prone method: Relative offset may change between multiple moves,
70         //because Handles have different ways of responding to reqMove(...), causing strange behaviour
71         @Deprecated
72         public void moveSelection(double x, double y)
73         {
74                 Point ref = selection.getTopLeft();
75                 Point snapped = new Point(x, y);
76                 applySnapping(snapped);
77
78                 for (Handle c : selection)
79                 {
80                         double newX, newY;
81                         newX = snapped.x + c.getPosX() - ref.x;
82                         newY = snapped.y + c.getPosY() - ref.y;
83                         c.reqMove(newX, newY);
84                 }
85         }
86         
87         public void moveHandles(double x, double y, Map<Handle, Point> handleOffsetMap)
88         {
89                 Point snapped = new Point(x, y);
90                 applySnapping(snapped);
91
92                 for (Handle c : handleOffsetMap.keySet())
93                 {
94                         Point offset = handleOffsetMap.get(c);
95                         double newX, newY;
96                         newX = snapped.x + offset.x;
97                         newY = snapped.y + offset.y;
98                         c.reqMove(newX, newY);
99                 }
100         }
101
102         public void deleteSelection()
103         {
104                 selection.forEach(h -> h.reqDelete());
105                 selection.clear();
106         }
107
108         public void copy()
109         {
110                 copyBuffer.clear();
111                 Point refPoint = selection.getTopLeft();
112                 for (Handle h : selection)
113                 {
114                         Optional<ComponentInfo> cInfo = h.reqCopy(refPoint);
115                         if(cInfo.isPresent())
116                                 copyBuffer.add(cInfo.get());
117                 }
118         }
119
120         public void paste(double x, double y)
121         {
122                 selection.clear();
123                 for (ComponentInfo info : copyBuffer)
124                 {
125                         GUIComponent comp = addComponent(info.identifier, info.params);
126                         ComponentHandle h = handleManager.getHandle(comp);
127                         h.reqMove(info.relX, info.relY);
128                         selection.add(h);
129                 }
130                 moveSelection(x, y);
131         }
132         
133         public void save()
134         {
135                 saveManager.save();
136         }
137         
138         public void saveAs()
139         {
140                 saveManager.openSaveAsDialog();
141         }
142
143         public void addComponent(double x, double y)
144         {
145                 boolean successful = false;
146                 JsonElement params = JsonNull.INSTANCE;
147                 outer:
148                 while(!successful)
149                 {
150                         String selected = gui.getAddListSelected();
151                         try
152                         {
153                                 GUIComponent c = addComponent(selected, params);
154                                 selection.clear();
155                                 selection.add(handleManager.getHandle(c));
156                                 moveSelection(x, y);
157                                 successful = true;
158                         }
159                         catch(UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
160                         {
161                                 String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:");
162                                 if(result == null)
163                                         break outer;
164                                 params = new JsonParser().parse(result);
165                         }
166                 }
167         }
168         
169         private GUIComponent addComponent(String identifier, JsonElement params)
170         {
171                 GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier,
172                                 params);
173                 identifierPerComponent.put(comp, identifier);
174                 return comp;
175         }
176         
177         public static String getIdentifier(GUIComponent c)
178         {
179                 return identifierPerComponent.get(c);
180         }
181
182         public void duplicate()
183         {
184                 copy();
185                 Point origin = selection.getTopLeft();
186                 paste(origin.x + 20, origin.y + 20);
187         }
188
189         private void applySnapping(Point newP)
190         {
191                 switch (snapping)
192                 {
193                 case OFF:
194                         break;
195                 case ABSOLUTE:
196                         newP.x -= newP.x % snapX;
197                         newP.y -= newP.y % snapY;
198                         break;
199                 }
200         }
201
202         public static class ComponentInfo
203         {
204                 public final double relX, relY;
205                 public final String identifier;
206                 public final JsonElement params;
207                 
208                 public ComponentInfo(double relX, double relY, String identifier, JsonElement params)
209                 {
210                         this.relX = relX;
211                         this.relY = relY;
212                         this.identifier = identifier;
213                         this.params = params;
214                 }
215         }
216
217         public void addWire(PinHandle a, PinHandle b)
218         {
219                 new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]);
220         }
221
222         public static enum Snapping
223         {
224                 OFF, ABSOLUTE;
225                 
226                 @Override
227                 public String toString()
228                 {
229                         return super.toString().toLowerCase();
230                 }
231         }
232         
233         public static void main(String[] args)
234         {
235                 SaveLoadManager.openLoadDialog();
236         }
237
238         public Snapping getSnapping()
239         {
240                 return snapping;
241         }
242
243         public void setSnapping(Snapping snapping)
244         {
245                 this.snapping = snapping;
246         }
247 }