root/dwt/graphics/Transform.d

Revision 246:fd9c62a2998e, 13.3 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

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