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