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