root/trunk/Moonlight/src/eu/moonlight3d/ml3d/modelling/objects/MoveObjectCommand.java

Revision 1225, 5.6 kB (checked in by gregor, 7 months ago)
Line 
1 /*
2  * Moonlight|3D
3  * Copyright (C) 2009 The Moonlight|3D team
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Created on Nov 29, 2009
20  */
21
22 package eu.moonlight3d.ml3d.modelling.objects;
23
24 import eu.moonlight3d.common.property.math.Vector3DValue;
25 import eu.moonlight3d.common.ui.command.Command;
26 import eu.moonlight3d.framework.State;
27 import eu.moonlight3d.framework.localization.L10N;
28 import eu.moonlight3d.framework.properties.StringValue;
29 import eu.moonlight3d.framework.ui.ExceptionDialog;
30 import eu.moonlight3d.math.Vector3D;
31 import eu.moonlight3d.ml3d.backend.selection.Selection;
32 import eu.moonlight3d.ml3d.backend.threedim.document.Document;
33 import eu.moonlight3d.ml3d.backend.threedim.helper.OperatorGraphHelper;
34 import eu.moonlight3d.ml3d.backend.threedim.helper.SelectionHelper;
35 import eu.moonlight3d.ml3d.backend.threedim.og.Manager;
36 import eu.moonlight3d.ml3d.backend.threedim.og.Node;
37 import eu.moonlight3d.ml3d.ui.command.CommandBase;
38
39 public class MoveObjectCommand extends CommandBase implements Command {
40         private String selectionName;
41         private Vector3D oldDisplacement;
42         private Vector3D newDisplacement;
43         private boolean createNode;
44         private String moveObjectNodeName;
45        
46         public MoveObjectCommand(String selectionName, Vector3D newDisplacement) {
47                 createNode=true;
48                 this.selectionName=selectionName;
49                 this.newDisplacement=newDisplacement;
50         }
51
52         public MoveObjectCommand(String selectionName, String moveObjectNodeName, Vector3D newDisplacement) {
53                 createNode=false;
54                 this.moveObjectNodeName=moveObjectNodeName;
55                 this.newDisplacement=newDisplacement;
56                
57                 // get old displacement from node
58                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
59                 Node currentOperation=ogManager.getNodeByName(moveObjectNodeName);
60                 try {
61                         oldDisplacement=(Vector3D) currentOperation.getProperty("displacement").getValue().getData();
62                 } catch (Exception e) {
63                         ExceptionDialog.run(e);
64                 }                               
65         }
66
67         public boolean execute() {
68                 // no checks required here - they have been made in the mode and gizmo listener already
69                
70                 redo();
71                
72                 return true;
73         }
74
75         public String getBriefDescription() {
76                 return L10N.translate("Move Object");
77         }
78
79         public boolean isUndoable() {
80                 return true;
81         }
82
83         public void redo() {
84                 try {
85                         saveOldSelection();
86                        
87                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
88                 Selection selection = ((Document) State.getInstance().getDocumentManager().getMainDocument()).getSelectionManager().getSelection("viewSelection");
89
90                 // get name of currently active object, og node and slot
91                 String ownerOGNodeName=SelectionHelper.getOGNodeName(selectionName);
92                 String ownerSGNodeName=SelectionHelper.getSGNodeName(selectionName);
93                 Node currentOperation;
94                         if(createNode) {
95                         currentOperation=OperatorGraphHelper.insert(selectionName,"MoveObject","input","output");
96                         moveObjectNodeName=currentOperation.getName();
97        
98                         // pass name of SG node to MoveObject OG node
99                         currentOperation.getProperty("name").setValue(new StringValue(ownerSGNodeName));
100                         } else {
101                                 currentOperation=ogManager.getNodeByName(moveObjectNodeName);
102                         }
103                        
104                         currentOperation.getProperty("displacement").setValue(new Vector3DValue(newDisplacement));
105                        
106                 // update selection to point to output of new node
107                 ogManager.update();
108                 selection.updateBase("OperatorGraph/" + ownerOGNodeName,"OperatorGraph/" + currentOperation.getName());                 
109                 } catch(Exception e) {
110                         ExceptionDialog.run(e);
111                 }
112         }
113
114         public void undo() {
115                 try {
116                 Selection selection = ((Document) State.getInstance().getDocumentManager().getMainDocument()).getSelectionManager().getSelection("viewSelection");
117                 selection.clearSelection(selection.getType());
118
119                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
120                         Node currentOperation=ogManager.getNodeByName(moveObjectNodeName);
121                         if(createNode) {
122                                 if(currentOperation.isDrain()) {
123                                         OperatorGraphHelper.remove(moveObjectNodeName);
124                                 } else {
125                                         OperatorGraphHelper.removeAndBridge(moveObjectNodeName);
126                                 }
127                         } else {
128                                 currentOperation.getProperty("displacement").setValue(new Vector3DValue(oldDisplacement));                             
129                         }
130                         restoreOldSelection();
131                         ogManager.update();
132                 } catch(Exception e) {
133                         ExceptionDialog.run(e);
134                 }
135         }
136        
137         public void update(Vector3D newDisplacement) {
138                 this.newDisplacement=newDisplacement;
139                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
140                 Node currentOperation=ogManager.getNodeByName(moveObjectNodeName);
141                 try {
142                         currentOperation.getProperty("displacement").setValue(new Vector3DValue(newDisplacement));                             
143                         ogManager.update();
144                 } catch(Exception e) {
145                         ExceptionDialog.run(e);
146                 }
147         }
148
149         public String getMoveObjectNodeName() {
150                 return moveObjectNodeName;
151         }
152 }
Note: See TracBrowser for help on using the browser.