View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic 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
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.client.rep.command;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.vectomatic.client.rep.RepApplication;
24  import org.vectomatic.client.rep.controller.MouseControllerSelector;
25  import org.vectomatic.client.rep.controller.SelectShapeController;
26  import org.vectomatic.common.model.Shape;
27  import org.vectomatic.common.model.geometry.TransformMatrix;
28  
29  /**
30   * Command to alter the 2D transform of a shape
31   */
32  public class TransformShapeCommand extends CommandBase  {
33  	public static final int SCALE = 1;
34  	public static final int ROTATE = 2;
35  	public static final int TRANSLATE = 3;
36  	private TransformMatrix _t, _mSelection;
37  	private List<Shape> _shapes;
38  	private int _kind;
39  	private MouseControllerSelector _selector;
40  	private SelectShapeController _selectShapeController;
41  	
42  	/**
43  	 * Constructor
44  	 * @param selection
45  	 * The selection object
46  	 * @param t
47  	 * The transform to apply to all shapes positional matrix
48  	 * @param mSelection
49  	 * The positional matrix of the selection before the transform was applied
50  	 * @param kind
51  	 * The kind of transformation (SCALING, ROTATION, TRANSLATION)
52  	 * @param selector
53  	 * The controller selector
54  	 * @param selectShapeController 
55  	 */
56  	public TransformShapeCommand(RepApplication app, TransformMatrix t, TransformMatrix mSelection, int kind, MouseControllerSelector selector, SelectShapeController selectShapeController) {
57  		super(app);
58  		_shapes = new ArrayList<Shape>(_app.getSelection().getSelectedShapes());
59  		_t = new TransformMatrix(t);
60  		_mSelection = new TransformMatrix(mSelection);
61  		_kind = kind;
62  		_selector = selector;
63  		_selectShapeController = selectShapeController;
64  	}
65  	
66  	public String getDescription() {
67  		switch(_kind) {
68  			case SCALE:
69  				return _app.getConstants().scaleShapeCommand();
70  			case ROTATE:
71  				return _app.getConstants().rotateShapeCommand();
72  			default:
73  				return _app.getConstants().translateShapeCommand();
74  		}
75  	}
76  	
77  	public void unexecute() {
78  		// Activate the transform controller
79  		if (_selector != null) {
80  			_selector.selectController(_selectShapeController.getButton());
81  		}
82  		
83  		// Cancel all previous transforms and reselect the shapes
84  		TransformMatrix mTmp = new TransformMatrix();
85  		TransformMatrix tInv = new TransformMatrix();
86  		TransformMatrix mInv = new TransformMatrix();
87  		_t.invert(tInv);
88  		_mSelection.invert(mInv);
89  		tInv.preMultiply(mInv);
90  		for (int i = 0, size = _shapes.size(); i < size; i++) {
91  			Shape shape = _shapes.get(i);
92  			shape.setTransform(shape.getTransform().preMultiply(tInv, mTmp));
93  		}
94  		
95  		// Select the shapes
96  		_app.getSelection().select(_shapes);			
97  		
98  		for (int i = 0, size = _shapes.size(); i < size; i++) {
99  			Shape shape = _shapes.get(i);
100 			shape.setTransform(shape.getTransform().preMultiply(_mSelection));
101 		}
102 
103 		_app.getSelection().getRootShape().setTransform(_mSelection);
104 	}
105 	
106 	public void execute() {
107 		// Activate the transform controller
108 		if (_selector != null) {
109 			_selector.selectController(_selectShapeController.getButton());
110 		}
111 	
112 		// Cancel all previous transforms and reselect the shapes
113 		TransformMatrix mTmp = new TransformMatrix();
114 		TransformMatrix mInv = new TransformMatrix();
115 		TransformMatrix t = new TransformMatrix();
116 		_mSelection.invert(mInv);
117 		for (int i = 0, size = _shapes.size(); i < size; i++) {
118 			Shape shape = _shapes.get(i);
119 			shape.setTransform(shape.getTransform().preMultiply(mInv, mTmp));
120 		}
121 
122 		// Select the shapes
123 		_app.getSelection().select(_shapes);			
124 		
125 		_mSelection.preMultiply(_t, t);
126 		for (int i = 0, size = _shapes.size(); i < size; i++) {
127 			Shape shape = _shapes.get(i);
128 			shape.setTransform(shape.getTransform().preMultiply(t, mTmp));
129 		}
130 		_app.getSelection().getRootShape().setTransform(t);
131 	}
132 }