Added menu for adding components with parameters
[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 addComponent(double x, double y)
139         {
140                 boolean successful = false;
141                 JsonElement params = JsonNull.INSTANCE;
142                 outer:
143                 while(!successful)
144                 {
145                         String selected = gui.getAddListSelected();
146                         try
147                         {
148                                 GUIComponent c = addComponent(selected, params);
149                                 selection.clear();
150                                 selection.add(handleManager.getHandle(c));
151                                 moveSelection(x, y);
152                                 successful = true;
153                         }
154                         catch(UnsupportedOperationException | JsonSyntaxException | NumberFormatException e)
155                         {
156                                 String result = DialogManager.openMultiLineTextDialog("Add component", "Create", "Cancel", "Parameters:");
157                                 if(result == null)
158                                         break outer;
159                                 params = new JsonParser().parse(result);
160                         }
161                 }
162         }
163         
164         private GUIComponent addComponent(String identifier, JsonElement params)
165         {
166                 GUIComponent comp = IndirectGUIComponentCreator.createComponent(toBeEdited.getSubmodelModifiable(), identifier,
167                                 params);
168                 identifierPerComponent.put(comp, identifier);
169                 return comp;
170         }
171         
172         public static String getIdentifier(GUIComponent c)
173         {
174                 return identifierPerComponent.get(c);
175         }
176
177         public void duplicate()
178         {
179                 copy();
180                 Point origin = selection.getTopLeft();
181                 paste(origin.x + 20, origin.y + 20);
182         }
183
184         private void applySnapping(Point newP)
185         {
186                 switch (snapping)
187                 {
188                 case OFF:
189                         break;
190                 case ABSOLUTE:
191                         newP.x -= newP.x % snapX;
192                         newP.y -= newP.y % snapY;
193                         break;
194                 }
195         }
196
197         public static class ComponentInfo
198         {
199                 public final double relX, relY;
200                 public final String identifier;
201                 public final JsonElement params;
202                 
203                 public ComponentInfo(double relX, double relY, String identifier, JsonElement params)
204                 {
205                         this.relX = relX;
206                         this.relY = relY;
207                         this.identifier = identifier;
208                         this.params = params;
209                 }
210         }
211
212         public void addWire(PinHandle a, PinHandle b)
213         {
214                 new GUIWire(toBeEdited.getSubmodelModifiable(), a.getPin(), b.getPin(), new Point[0]);
215         }
216
217         public static enum Snapping
218         {
219                 OFF, ABSOLUTE;
220                 
221                 @Override
222                 public String toString()
223                 {
224                         return super.toString().toLowerCase();
225                 }
226         }
227         
228         public static void main(String[] args)
229         {
230                 SaveLoadManager.openLoadDialog();
231         }
232
233         public Snapping getSnapping()
234         {
235                 return snapping;
236         }
237
238         public void setSnapping(Snapping snapping)
239         {
240                 this.snapping = snapping;
241         }
242 }