comparison dwt/graphics/Transform.d @ 23:3c2d10c00f61

Transform
author Frank Benoit <benoit@tionex.de>
date Mon, 07 Jan 2008 09:39:59 +0100
parents
children 27324bbbac70
comparison
equal deleted inserted replaced
22:b5521a2c27c2 23:3c2d10c00f61
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.SWT;
14 import dwt.internal.Compatibility;
15 import dwt.internal.cairo.Cairo;
16 import dwt.internal.gtk.c.cairotypes;
17 import dwt.graphics.Resource;
18 import dwt.graphics.Device;
19
20 import tango.text.convert.Format;
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 : 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 SWT
43 * public API. It is marked public only so that it can be shared
44 * within the packages provided by SWT. It is not available on all
45 * platforms and should never be accessed from application code.
46 * </p>
47 */
48 public double[] 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 SWTException <ul>
64 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
65 * </ul>
66 * @exception SWTError <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 this (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 SWTException <ul>
93 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
94 * </ul>
95 * @exception SWTError <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 this(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 SWTException <ul>
126 * <li>ERROR_NO_GRAPHICS_LIBRARY - if advanced graphics are not available</li>
127 * </ul>
128 * @exception SWTError <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 this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) {
135 if (device is null) device = Device.getDevice();
136 if (device is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
137 this.device = device;
138 device.checkCairo();
139 handle = new double[6];
140 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
141 Cairo.cairo_matrix_init( cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy);
142 if (device.tracking) device.new_Object(this);
143 }
144
145 static float[] checkTransform(float[] elements) {
146 if (elements == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
147 if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
148 return elements;
149 }
150
151 /**
152 * Disposes of the operating system resources associated with
153 * the Transform. Applications must dispose of all Transforms that
154 * they allocate.
155 */
156 public void dispose() {
157 if (handle is null) return;
158 if (device.isDisposed()) return;
159 handle = null;
160 if (device.tracking) device.dispose_Object(this);
161 device = null;
162 }
163
164 /**
165 * Fills the parameter with the values of the transformation matrix
166 * that the receiver represents, in the order {m11, m12, m21, m22, dx, dy}.
167 *
168 * @param elements array to hold the matrix values
169 *
170 * @exception SWTException <ul>
171 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
172 * </ul>
173 * @exception IllegalArgumentException <ul>
174 * <li>ERROR_NULL_ARGUMENT - if the parameter is null</li>
175 * <li>ERROR_INVALID_ARGUMENT - if the parameter is too small to hold the matrix values</li>
176 * </ul>
177 */
178 public void getElements(float[] elements) {
179 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
180 if (elements is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
181 if (elements.length < 6) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
182 elements[0] = cast(float)handle[0];
183 elements[1] = cast(float)handle[1];
184 elements[2] = cast(float)handle[2];
185 elements[3] = cast(float)handle[3];
186 elements[4] = cast(float)handle[4];
187 elements[5] = cast(float)handle[5];
188 }
189
190 /**
191 * Modifies the receiver such that the matrix it represents becomes the
192 * the mathematical inverse of the matrix it previously represented.
193 *
194 * @exception SWTException <ul>
195 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
196 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li>
197 * </ul>
198 */
199 public void invert() {
200 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
201 if (Cairo.cairo_matrix_invert(cast(cairo_matrix_t*)handle.ptr) != 0) {
202 SWT.error(SWT.ERROR_CANNOT_INVERT_MATRIX);
203 }
204 }
205
206 /**
207 * Returns <code>true</code> if the Transform has been disposed,
208 * and <code>false</code> otherwise.
209 * <p>
210 * This method gets the dispose state for the Transform.
211 * When a Transform has been disposed, it is an error to
212 * invoke any other method using the Transform.
213 *
214 * @return <code>true</code> when the Transform is disposed, and <code>false</code> otherwise
215 */
216 public bool isDisposed() {
217 return handle is null;
218 }
219
220 /**
221 * Returns <code>true</code> if the Transform represents the identity matrix
222 * and false otherwise.
223 *
224 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise
225 */
226 public bool isIdentity() {
227 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
228 float[] m = new float[6];
229 getElements(m);
230 return m[0] == 1 && m[1] == 0 && m[2] == 0 && m[3] == 1 && m[4] == 0 && m[5] == 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 SWTException <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()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
250 if (matrix == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
251 if (matrix.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
252 Cairo.cairo_matrix_multiply(cast(cairo_matrix_t*)handle.ptr,cast(cairo_matrix_t*)matrix.handle.ptr, cast(cairo_matrix_t*)handle.ptr);
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 SWTException <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()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
270 Cairo.cairo_matrix_rotate(cast(cairo_matrix_t*)handle.ptr, angle * cast(float)Compatibility.PI / 180);
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 SWTException <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()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
286 Cairo.cairo_matrix_scale(cast(cairo_matrix_t*)handle.ptr, 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 SWTException <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()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
306 Cairo.cairo_matrix_init(cast(cairo_matrix_t*)handle.ptr, m11, m12, m21, m22, dx, dy);
307 }
308
309 /**
310 * Given an array containing points described by alternating x and y values,
311 * modify that array such that each point has been replaced with the result of
312 * applying the transformation represented by the receiver to that point.
313 *
314 * @param pointArray an array of alternating x and y values to be transformed
315 *
316 * @exception IllegalArgumentException <ul>
317 * <li>ERROR_NULL_ARGUMENT - if the point array is null</li>
318 * </ul>
319 * @exception SWTException <ul>
320 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
321 * </ul>
322 */
323 public void transform(float[] pointArray) {
324 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
325 if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
326 double dx, dy;
327 int length = pointArray.length / 2;
328 for (int i = 0, j = 0; i < length; i++, j += 2) {
329 dx = pointArray[j];
330 dy = pointArray[j + 1];
331 Cairo.cairo_matrix_transform_point(cast(cairo_matrix_t*)handle.ptr, &dx, &dy);
332 pointArray[j] = cast(float)dx;
333 pointArray[j + 1] = cast(float)dy;
334 }
335 }
336
337 /**
338 * Modifies the receiver so that it represents a transformation that is
339 * equivalent to its previous transformation translated by (offsetX, offsetY).
340 *
341 * @param offsetX the distance to translate in the X direction
342 * @param offsetY the distance to translate in the Y direction
343 *
344 * @exception SWTException <ul>
345 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
346 * </ul>
347 */
348 public void translate(float offsetX, float offsetY) {
349 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
350 Cairo.cairo_matrix_translate(cast(cairo_matrix_t*)handle.ptr, offsetX, offsetY);
351 }
352
353 /**
354 * Returns a string containing a concise, human-readable
355 * description of the receiver.
356 *
357 * @return a string representation of the receiver
358 */
359 public char[] toString() {
360 if (isDisposed()) return "Transform {*DISPOSED*}";
361 float[] elements = new float[6];
362 getElements(elements);
363 return Format( "Transform {{{}}", elements );
364 }
365
366 }