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

Revision 1218, 5.9 kB (checked in by gregor, 8 months ago)
  • added workaround to hide issue with undo methods leaving references to deleted nodes in the selections instead of replacing them - this makes the transform and extrude modes loose the set of active/selected elements, though
  • fixed toolchest group of extrude tool
  • fixed removal of operator graph node in create tools of mesh plugin
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
38 public class ScaleObjectCommand implements Command {
39         private String selectionName;
40         private Vector3D oldPivot;
41         private Vector3D oldScale;
42         private Vector3D newPivot;
43         private Vector3D newScale;
44         private boolean createNode;
45         private String scaleObjectNodeName;
46        
47         public ScaleObjectCommand(String selectionName, Vector3D newPivot, Vector3D newScale) {
48                 this.createNode=true;
49                 this.selectionName=selectionName;
50                 this.newPivot=newPivot;
51                 this.newScale=newScale;
52         }
53        
54         public ScaleObjectCommand(String selectionName, String scaleObjectNodeName, Vector3D newPivot, Vector3D newScale) {
55                 this.createNode=false;
56                 this.selectionName=selectionName;
57                 this.scaleObjectNodeName=scaleObjectNodeName;
58                 this.newPivot=newPivot;
59                 this.newScale=newScale;
60                
61                 // get old pivot and scale from node
62                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
63                 Node currentOperation=ogManager.getNodeByName(scaleObjectNodeName);
64                 try {
65                         oldPivot=(Vector3D) currentOperation.getProperty("pivot").getValue().getData();
66                         oldScale=(Vector3D) currentOperation.getProperty("scale").getValue().getData();
67                 } catch (Exception e) {
68                         ExceptionDialog.run(e);
69                 }                               
70         }
71
72         public boolean execute() {
73                 // no checks required - they have been made in the mode and gizmo listener already
74                
75                 redo();
76                
77                 return true;
78         }
79
80         public String getBriefDescription() {
81                 return L10N.translate("Scale Object");
82         }
83
84         public boolean isUndoable() {
85                 return true;
86         }
87
88         public void redo() {
89                 try {
90                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
91                 Selection selection = ((Document) State.getInstance().getDocumentManager().getMainDocument()).getSelectionManager().getSelection("viewSelection");
92
93                 // get name of currently active object, og node and slot
94                 String ownerOGNodeName=SelectionHelper.getOGNodeName(selectionName);
95                 String ownerSGNodeName=SelectionHelper.getSGNodeName(selectionName);
96                 Node currentOperation;
97                         if(createNode) {
98                         currentOperation=OperatorGraphHelper.insert(selectionName,"ScaleObject","input","output");
99                         scaleObjectNodeName=currentOperation.getName();
100        
101                         // pass name of SG node to MoveObject OG node
102                         currentOperation.getProperty("name").setValue(new StringValue(ownerSGNodeName));
103                         } else {
104                                 currentOperation=ogManager.getNodeByName(scaleObjectNodeName);
105                         }
106                        
107                         currentOperation.getProperty("pivot").setValue(new Vector3DValue(newPivot));
108                         currentOperation.getProperty("scale").setValue(new Vector3DValue(newScale));
109                        
110                 // update selection to point to output of new node
111                 ogManager.update();
112                 selection.updateBase("OperatorGraph/" + ownerOGNodeName,"OperatorGraph/" + currentOperation.getName());                 
113                 } catch(Exception e) {
114                         ExceptionDialog.run(e);
115                 }
116
117         }
118
119         public void undo() {
120                 try {
121                 Selection selection = ((Document) State.getInstance().getDocumentManager().getMainDocument()).getSelectionManager().getSelection("viewSelection");
122                 selection.clearSelection(selection.getType());
123
124                         Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
125                         Node currentOperation=ogManager.getNodeByName(scaleObjectNodeName);
126                         if(createNode) {
127                                 if(currentOperation.isDrain()) {
128                                         OperatorGraphHelper.remove(scaleObjectNodeName);
129                                 } else {
130                                         OperatorGraphHelper.removeAndBridge(scaleObjectNodeName);
131                                 }
132                                 ogManager.removeNode(currentOperation);
133                         } else {
134                                 currentOperation.getProperty("pivot").setValue(new Vector3DValue(oldPivot));
135                                 currentOperation.getProperty("scale").setValue(new Vector3DValue(oldScale));
136                         }
137                         ogManager.update();
138                 } catch(Exception e) {
139                         ExceptionDialog.run(e);
140                 }
141         }
142        
143         public void update(Vector3D newPivot, Vector3D newScale) {
144                 this.newPivot=newPivot;
145                 this.newScale=newScale;
146                 Manager ogManager=((Document) State.getInstance().getDocumentManager().getMainDocument()).getOGManager();
147                 Node currentOperation=ogManager.getNodeByName(scaleObjectNodeName);
148                 try {
149                         currentOperation.getProperty("pivot").setValue(new Vector3DValue(newPivot));
150                         currentOperation.getProperty("scale").setValue(new Vector3DValue(newScale));
151                         ogManager.update();
152                 } catch(Exception e) {
153                         ExceptionDialog.run(e);
154                 }
155         }
156
157         public String getScaleObjectNodeName() {
158                 return scaleObjectNodeName;
159         }
160 }
Note: See TracBrowser for help on using the browser.