comparison dwt/graphics/Transform.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.graphics.Transform;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.DWTError;
17 import dwt.DWTException;
18 import dwt.internal.cocoa.NSAffineTransform;
19 import dwt.internal.cocoa.NSAffineTransformStruct;
20 import dwt.internal.cocoa.NSPoint;
21
22 /**
23 * Instances of this class represent transformation matrices for
24 * points expressed as (x, y) pairs of floating point numbers.
25 * <p>
26 * Application code must explicitly invoke the <code>Transform.dispose()</code>
27 * method to release the operating system resources managed by each instance
28 * when those instances are no longer required.
29 * </p>
30 * <p>
31 * This class requires the operating system's advanced graphics subsystem
32 * which may not be available on some platforms.
33 * </p>
34 *
35 * @since 3.1
36 */
37 public class Transform extends Resource {
38 /**
39 * the OS resource for the Transform
40 * (Warning: This field is platform dependent)
41 * <p>
42 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
43 * public API. It is marked public only so that it can be shared
44 * within the packages provided by DWT. It is not available on all
45 * platforms and should never be accessed from application code.
46 * </p>
47 */
48 public NSAffineTransform handle;
49
50 /**
51 * Constructs a new identity Transform.
52 * <p>
53 * This operation requires the operating system's advanced
54 * graphics subsystem which may not be available on some
55 * platforms.
56 * </p>
57 *
58 * @param device the device on which to allocate the Transform
59 *
60 * @exception IllegalArgumentException <ul>
61 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
62 * </ul>
63 * @exception DWTException <ul>
64 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
65 * </ul>
66 * @exception DWTError <ul>
67 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
68 * </ul>
69 *
70 * @see #dispose()
71 */
72 public Transform (Device device) {
73 this(device, 1, 0, 0, 1, 0, 0);
74 }
75
76 /**
77 * Constructs a new Transform given an array of elements that represent the
78 * matrix that describes the transformation.
79 * <p>
80 * This operation requires the operating system's advanced
81 * graphics subsystem which may not be available on some
82 * platforms.
83 * </p>
84 *
85 * @param device the device on which to allocate the Transform
86 * @param elements an array of floats that describe the transformation matrix
87 *
88 * @exception IllegalArgumentException <ul>
89 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device, or the elements array is null</li>
90 * <li>ERROR_INVALID_ARGUMENT - if the elements array is too small to hold the matrix values</li>
91 * </ul>
92 * @exception DWTException <ul>
93 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
94 * </ul>
95 * @exception DWTError <ul>
96 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
97 * </ul>
98 *
99 * @see #dispose()
100 */
101 public Transform(Device device, float[] elements) {
102 this (device, checkTransform(elements)[0], elements[1], elements[2], elements[3], elements[4], elements[5]);
103 }
104
105 /**
106 * Constructs a new Transform given all of the elements that represent the
107 * matrix that describes the transformation.
108 * <p>
109 * This operation requires the operating system's advanced
110 * graphics subsystem which may not be available on some
111 * platforms.
112 * </p>
113 *
114 * @param device the device on which to allocate the Transform
115 * @param m11 the first element of the first row of the matrix
116 * @param m12 the second element of the first row of the matrix
117 * @param m21 the first element of the second row of the matrix
118 * @param m22 the second element of the second row of the matrix
119 * @param dx the third element of the first row of the matrix
120 * @param dy the third element of the second row of the matrix
121 *
122 * @exception IllegalArgumentException <ul>
123 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
124 * </ul>
125 * @exception DWTException <ul>
126 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
127 * </ul>
128 * @exception DWTError <ul>
129 * <li>ERROR_NO_HANDLES if a handle for the Transform could not be obtained</li>
130 * </ul>
131 *
132 * @see #dispose()
133 */
134 public Transform (Device device, float m11, float m12, float m21, float m22, float dx, float dy) {
135 super(device);
136 handle = NSAffineTransform.transform();
137 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
138 handle.retain();
139 setElements(m11, m12, m21, m22, dx, dy);
140 init();
141 }
142
143 static float[] checkTransform(float[] elements) {
144 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
145 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
146 return elements;
147 }
148
149 void destroy() {
150 handle.release();
151 handle = null;
152 }
153
154 /**
155 * Fills the parameter with the values of the transformation matrix
156 * that the receiver represents, in the order {m11, m12, m21, m22, dx, dy}.
157 *
158 * @param elements array to hold the matrix values
159 *
160 * @exception DWTException <ul>
161 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
162 * </ul>
163 * @exception IllegalArgumentException <ul>
164 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
165 * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the matrix values</li>
166 * </ul>
167 */
168 public void getElements(float[] elements) {
169 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
170 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
171 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
172 NSAffineTransformStruct struct = handle.transformStruct();
173 elements[0] = struct.m11;
174 elements[1] = struct.m12;
175 elements[2] = struct.m21;
176 elements[3] = struct.m22;
177 elements[4] = struct.tX;
178 elements[5] = struct.tY;
179 }
180
181 public void identity() {
182 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
183 NSAffineTransformStruct struct = new NSAffineTransformStruct();
184 struct.m11 = 1;
185 struct.m22 = 1;
186 handle.setTransformStruct(struct);
187 }
188
189 /**
190 * Modifies the receiver such that the matrix it represents becomes the
191 * the mathematical inverse of the matrix it previously represented.
192 *
193 * @exception DWTException <ul>
194 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
195 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li>
196 * </ul>
197 */
198 public void invert() {
199 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
200 NSAffineTransformStruct struct = handle.transformStruct();
201 if ((struct.m11 * struct.m22 - struct.m12 * struct.m21) is 0) {
202 DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX);
203 }
204 handle.invert();
205 }
206
207 /**
208 * Returns <code>true</code> if the Transform has been disposed,
209 * and <code>false</code> otherwise.
210 * <p>
211 * This method gets the dispose state for the Transform.
212 * When a Transform has been disposed, it is an error to
213 * invoke any other method using the Transform.
214 *
215 * @return <code>true</code> when the Transform is disposed, and <code>false</code> otherwise
216 */
217 public bool isDisposed() {
218 return handle is null;
219 }
220
221 /**
222 * Returns <code>true</code> if the Transform represents the identity matrix
223 * and false otherwise.
224 *
225 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise
226 */
227 public bool isIdentity() {
228 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
229 NSAffineTransformStruct struct = handle.transformStruct();
230 return struct.m11 is 1 && struct.m12 is 0 && struct.m21 is 0 && struct.m22 is 1 && struct.tX is 0 && struct.tY is 0;
231 }
232
233 /**
234 * Modifies the receiver such that the matrix it represents becomes the
235 * the result of multiplying the matrix it previously represented by the
236 * argument.
237 *
238 * @param matrix the matrix to multiply the receiver by
239 *
240 * @exception DWTException <ul>
241 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
242 * </ul>
243 * @exception IllegalArgumentException <ul>
244 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
245 * <li>ERROR_INVALID_ARGUMENT - if the parameter has been disposed</li>
246 * </ul>
247 */
248 public void multiply(Transform matrix) {
249 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
250 if (matrix is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
251 if (matrix.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
252 handle.prependTransform(matrix.handle);
253 }
254
255 /**
256 * Modifies the receiver so that it represents a transformation that is
257 * equivalent to its previous transformation rotated by the specified angle.
258 * The angle is specified in degrees and for the identity transform 0 degrees
259 * is at the 3 o'clock position. A positive value indicates a clockwise rotation
260 * while a negative value indicates a counter-clockwise rotation.
261 *
262 * @param angle the angle to rotate the transformation by
263 *
264 * @exception DWTException <ul>
265 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
266 * </ul>
267 */
268 public void rotate(float angle) {
269 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
270 handle.rotateByDegrees(angle);
271 }
272
273 /**
274 * Modifies the receiver so that it represents a transformation that is
275 * equivalent to its previous transformation scaled by (scaleX, scaleY).
276 *
277 * @param scaleX the amount to scale in the X direction
278 * @param scaleY the amount to scale in the Y direction
279 *
280 * @exception DWTException <ul>
281 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
282 * </ul>
283 */
284 public void scale(float scaleX, float scaleY) {
285 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
286 handle.scaleXBy(scaleX, scaleY);
287 }
288
289 /**
290 * Modifies the receiver to represent a new transformation given all of
291 * the elements that represent the matrix that describes that transformation.
292 *
293 * @param m11 the first element of the first row of the matrix
294 * @param m12 the second element of the first row of the matrix
295 * @param m21 the first element of the second row of the matrix
296 * @param m22 the second element of the second row of the matrix
297 * @param dx the third element of the first row of the matrix
298 * @param dy the third element of the second row of the matrix
299 *
300 * @exception DWTException <ul>
301 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
302 * </ul>
303 */
304 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) {
305 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
306 NSAffineTransformStruct struct = new NSAffineTransformStruct();
307 struct.m11 = m11;
308 struct.m12 = m12;
309 struct.m21 = m21;
310 struct.m22 = m22;
311 struct.tX = dx;
312 struct.tY = dy;
313 handle.setTransformStruct(struct);
314 }
315
316 public void shear(float shearX, float shearY) {
317 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
318 NSAffineTransformStruct struct = new NSAffineTransformStruct();
319 struct.m11 = 1;
320 struct.m12 = shearX;
321 struct.m21 = shearY;
322 struct.m22 = 1;
323 NSAffineTransform matrix = NSAffineTransform.transform();
324 matrix.setTransformStruct(struct);
325 handle.prependTransform(matrix);
326 }
327
328 /**
329 * Given an array containing points described by alternating x and y values,
330 * modify that array such that each point has been replaced with the result of
331 * applying the transformation represented by the receiver to that point.
332 *
333 * @param pointArray an array of alternating x and y values to be transformed
334 *
335 * @exception IllegalArgumentException <ul>
336 * <li>ERROR_NULL_ARGUMENT - if the point array is null</li>
337 * </ul>
338 * @exception DWTException <ul>
339 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
340 * </ul>
341 */
342 public void transform(float[] pointArray) {
343 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
344 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
345 NSPoint point = new NSPoint();
346 int length = pointArray.length / 2;
347 for (int i = 0, j = 0; i < length; i++, j += 2) {
348 point.x = pointArray[j];
349 point.y = pointArray[j + 1];
350 point = handle.transformPoint(point);
351 pointArray[j] = point.x;
352 pointArray[j + 1] = point.y;
353 }
354 }
355
356 /**
357 * Modifies the receiver so that it represents a transformation that is
358 * equivalent to its previous transformation translated by (offsetX, offsetY).
359 *
360 * @param offsetX the distance to translate in the X direction
361 * @param offsetY the distance to translate in the Y direction
362 *
363 * @exception DWTException <ul>
364 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
365 * </ul>
366 */
367 public void translate(float offsetX, float offsetY) {
368 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
369 handle.translateXBy(offsetX, offsetY);
370 }
371
372 /**
373 * Returns a string containing a concise, human-readable
374 * description of the receiver.
375 *
376 * @return a string representation of the receiver
377 */
378 public String toString() {
379 if (isDisposed()) return "Transform {*DISPOSED*}";
380 float[] elements = new float[6];
381 getElements(elements);
382 return "Transform {" + elements [0] + ", " + elements [1] + ", " +elements [2] + ", " +elements [3] + ", " +elements [4] + ", " +elements [5] + "}";
383 }
384
385 }