Mercurial > projects > dwt-mac
comparison dwt/graphics/Transform.d @ 45:d8635bb48c7c
Merge with SWT 3.5
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 01 Dec 2008 17:07:00 +0100 |
parents | db5a898b2119 |
children | cfa563df4fdd |
comparison
equal
deleted
inserted
replaced
44:ca5e494f2bbf | 45:d8635bb48c7c |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * | 10 * |
11 * Port to the D programming language: | 11 * Port to the D programming language: |
12 * Jacob Carlborg <jacob.carlborg@gmail.com> | 12 * Jacob Carlborg <doob@me.com> |
13 *******************************************************************************/ | 13 *******************************************************************************/ |
14 module dwt.graphics.Transform; | 14 module dwt.graphics.Transform; |
15 | 15 |
16 import dwt.DWT; | 16 import dwt.DWT; |
17 import dwt.DWTError; | 17 import dwt.DWTError; |
18 import dwt.DWTException; | 18 import dwt.DWTException; |
19 import dwt.internal.cocoa.NSAffineTransform; | 19 import dwt.internal.cocoa.NSAffineTransform; |
20 import dwt.internal.cocoa.NSAffineTransformStruct; | 20 import dwt.internal.cocoa.NSAffineTransformStruct; |
21 import dwt.internal.cocoa.NSAutoreleasePool; | |
21 import dwt.internal.cocoa.NSPoint; | 22 import dwt.internal.cocoa.NSPoint; |
23 import dwt.internal.cocoa.NSThread; | |
22 | 24 |
23 import tango.text.convert.Format; | 25 import tango.text.convert.Format; |
24 | 26 |
25 import dwt.dwthelper.utils; | 27 import dwt.dwthelper.utils; |
26 import dwt.graphics.Device; | 28 import dwt.graphics.Device; |
36 * </p> | 38 * </p> |
37 * <p> | 39 * <p> |
38 * This class requires the operating system's advanced graphics subsystem | 40 * This class requires the operating system's advanced graphics subsystem |
39 * which may not be available on some platforms. | 41 * which may not be available on some platforms. |
40 * </p> | 42 * </p> |
43 * | |
44 * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: GraphicsExample</a> | |
45 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
41 * | 46 * |
42 * @since 3.1 | 47 * @since 3.1 |
43 */ | 48 */ |
44 public class Transform : Resource { | 49 public class Transform : Resource { |
45 | 50 |
141 * | 146 * |
142 * @see #dispose() | 147 * @see #dispose() |
143 */ | 148 */ |
144 public this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) { | 149 public this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) { |
145 super(device); | 150 super(device); |
146 handle = NSAffineTransform.transform(); | 151 NSAutoreleasePool pool = null; |
147 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); | 152 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
148 handle.retain(); | 153 try { |
149 setElements(m11, m12, m21, m22, dx, dy); | 154 handle = NSAffineTransform.transform(); |
155 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); | |
156 handle.retain(); | |
157 setElements(m11, m12, m21, m22, dx, dy); | |
150 init_(); | 158 init_(); |
159 } finally { | |
160 if (pool !is null) pool.release(); | |
161 } | |
151 } | 162 } |
152 | 163 |
153 static float[] checkTransform(float[] elements) { | 164 static float[] checkTransform(float[] elements) { |
154 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 165 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
155 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 166 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
177 */ | 188 */ |
178 public void getElements(float[] elements) { | 189 public void getElements(float[] elements) { |
179 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 190 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
180 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 191 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
181 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 192 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
182 NSAffineTransformStruct structt = handle.transformStruct(); | 193 NSAutoreleasePool pool = null; |
183 elements[0] = structt.m11; | 194 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); |
184 elements[1] = structt.m12; | 195 try { |
185 elements[2] = structt.m21; | 196 NSAffineTransformStruct struct_ = handle.transformStruct(); |
186 elements[3] = structt.m22; | 197 elements[0] = (float)/*64*/struct_.m11; |
187 elements[4] = structt.tX; | 198 elements[1] = (float)/*64*/struct_.m12; |
188 elements[5] = structt.tY; | 199 elements[2] = (float)/*64*/struct_.m21; |
189 } | 200 elements[3] = (float)/*64*/struct_.m22; |
190 | 201 elements[4] = (float)/*64*/struct_.tX; |
202 elements[5] = (float)/*64*/struct_.tY; | |
203 } finally { | |
204 if (pool !is null) pool.release(); | |
205 } | |
206 } | |
207 | |
208 /** | |
209 * Modifies the receiver such that the matrix it represents becomes the | |
210 * identity matrix. | |
211 * | |
212 * @exception DWTException <ul> | |
213 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
214 * </ul> | |
215 * | |
216 * @since 3.4 | |
217 */ | |
191 public void identity() { | 218 public void identity() { |
192 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 219 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
193 NSAffineTransformStruct structt = NSAffineTransformStruct(); | 220 NSAutoreleasePool pool = null; |
194 structt.m11 = 1; | 221 try { |
195 structt.m22 = 1; | 222 NSAffineTransformStruct struct_ = NSAffineTransformStruct(); |
196 handle.setTransformStruct(structt); | 223 struct_.m11 = 1; |
197 } | 224 struct_.m22 = 1; |
198 | 225 handle.setTransformStruct(structt); |
199 /** | 226 } finally { |
200 * Modifies the receiver such that the matrix it represents becomes the | 227 if (pool !is null) pool.release(); |
228 } | |
229 } | |
230 | |
231 /** | |
232 * Modifies the receiver such that the matrix it represents becomes | |
201 * the mathematical inverse of the matrix it previously represented. | 233 * the mathematical inverse of the matrix it previously represented. |
202 * | 234 * |
203 * @exception DWTException <ul> | 235 * @exception DWTException <ul> |
204 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | 236 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
205 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li> | 237 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li> |
206 * </ul> | 238 * </ul> |
207 */ | 239 */ |
208 public void invert() { | 240 public void invert() { |
209 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 241 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
210 NSAffineTransformStruct structt = handle.transformStruct(); | 242 NSAutoreleasePool pool = null; |
211 if ((structt.m11 * structt.m22 - structt.m12 * structt.m21) is 0) { | 243 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
212 DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX); | 244 try { |
213 } | 245 NSAffineTransformStruct struct_ = handle.transformStruct(); |
214 handle.invert(); | 246 if ((struct_.m11 * struct_.m22 - struct_.m12 * struct_.m21) is 0) { |
247 DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX); | |
248 } | |
249 handle.invert(); | |
250 } finally { | |
251 if (pool !is null) pool.release(); | |
252 } | |
215 } | 253 } |
216 | 254 |
217 /** | 255 /** |
218 * Returns <code>true</code> if the Transform has been disposed, | 256 * Returns <code>true</code> if the Transform has been disposed, |
219 * and <code>false</code> otherwise. | 257 * and <code>false</code> otherwise. |
234 * | 272 * |
235 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise | 273 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise |
236 */ | 274 */ |
237 public bool isIdentity() { | 275 public bool isIdentity() { |
238 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 276 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
239 NSAffineTransformStruct structt = handle.transformStruct(); | 277 NSAutoreleasePool pool = null; |
240 return structt.m11 is 1 && structt.m12 is 0 && structt.m21 is 0 && structt.m22 is 1 && structt.tX is 0 && structt.tY is 0; | 278 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
279 try { | |
280 NSAffineTransformStruct struct = handle.transformStruct(); | |
281 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; | |
282 } finally { | |
283 if (pool !is null) pool.release(); | |
284 } | |
241 } | 285 } |
242 | 286 |
243 /** | 287 /** |
244 * Modifies the receiver such that the matrix it represents becomes the | 288 * Modifies the receiver such that the matrix it represents becomes the |
245 * the result of multiplying the matrix it previously represented by the | 289 * the result of multiplying the matrix it previously represented by the |
257 */ | 301 */ |
258 public void multiply(Transform matrix) { | 302 public void multiply(Transform matrix) { |
259 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 303 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
260 if (matrix is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 304 if (matrix is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
261 if (matrix.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); | 305 if (matrix.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT); |
262 handle.prependTransform(matrix.handle); | 306 NSAutoreleasePool pool = null; |
307 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); | |
308 try { | |
309 handle.prependTransform(matrix.handle); | |
310 } finally { | |
311 if (pool !is null) pool.release(); | |
312 } | |
263 } | 313 } |
264 | 314 |
265 /** | 315 /** |
266 * Modifies the receiver so that it represents a transformation that is | 316 * Modifies the receiver so that it represents a transformation that is |
267 * equivalent to its previous transformation rotated by the specified angle. | 317 * equivalent to its previous transformation rotated by the specified angle. |
275 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | 325 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
276 * </ul> | 326 * </ul> |
277 */ | 327 */ |
278 public void rotate(float angle) { | 328 public void rotate(float angle) { |
279 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 329 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
280 handle.rotateByDegrees(angle); | 330 NSAutoreleasePool pool = null; |
331 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); | |
332 try { | |
333 handle.rotateByDegrees(angle); | |
334 } finally { | |
335 if (pool !is null) pool.release(); | |
336 } | |
281 } | 337 } |
282 | 338 |
283 /** | 339 /** |
284 * Modifies the receiver so that it represents a transformation that is | 340 * Modifies the receiver so that it represents a transformation that is |
285 * equivalent to its previous transformation scaled by (scaleX, scaleY). | 341 * equivalent to its previous transformation scaled by (scaleX, scaleY). |
291 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | 347 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
292 * </ul> | 348 * </ul> |
293 */ | 349 */ |
294 public void scale(float scaleX, float scaleY) { | 350 public void scale(float scaleX, float scaleY) { |
295 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 351 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
296 handle.scaleXBy(scaleX, scaleY); | 352 NSAutoreleasePool pool = null; |
353 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); | |
354 try { | |
355 handle.scaleXBy(scaleX, scaleY); | |
356 } finally { | |
357 if (pool !is null) pool.release(); | |
358 } | |
297 } | 359 } |
298 | 360 |
299 /** | 361 /** |
300 * Modifies the receiver to represent a new transformation given all of | 362 * Modifies the receiver to represent a new transformation given all of |
301 * the elements that represent the matrix that describes that transformation. | 363 * the elements that represent the matrix that describes that transformation. |
311 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | 373 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
312 * </ul> | 374 * </ul> |
313 */ | 375 */ |
314 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) { | 376 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) { |
315 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 377 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
316 NSAffineTransformStruct structt = NSAffineTransformStruct(); | 378 NSAutoreleasePool pool = null; |
317 structt.m11 = m11; | 379 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
318 structt.m12 = m12; | 380 try { |
319 structt.m21 = m21; | 381 NSAffineTransformStruct struct_ = NSAffineTransformStruct(); |
320 structt.m22 = m22; | 382 struct_.m11 = m11; |
321 structt.tX = dx; | 383 struct_.m12 = m12; |
322 structt.tY = dy; | 384 struct_.m21 = m21; |
323 handle.setTransformStruct(structt); | 385 struct_.m22 = m22; |
324 } | 386 struct_.tX = dx; |
325 | 387 struct_.tY = dy; |
388 handle.setTransformStruct(struct_); | |
389 } finally { | |
390 if (pool !is null) pool.release(); | |
391 } | |
392 } | |
393 | |
394 /** | |
395 * Modifies the receiver so that it represents a transformation that is | |
396 * equivalent to its previous transformation sheared by (shearX, shearY). | |
397 * | |
398 * @param shearX the shear factor in the X direction | |
399 * @param shearY the shear factor in the Y direction | |
400 * | |
401 * @exception DWTException <ul> | |
402 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
403 * </ul> | |
404 * | |
405 * @since 3.4 | |
406 */ | |
326 public void shear(float shearX, float shearY) { | 407 public void shear(float shearX, float shearY) { |
327 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 408 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
328 NSAffineTransformStruct structt = NSAffineTransformStruct(); | 409 NSAutoreleasePool pool = null; |
329 structt.m11 = 1; | 410 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
330 structt.m12 = shearX; | 411 try { |
331 structt.m21 = shearY; | 412 NSAffineTransformStruct struct_ = NSAffineTransformStruct(); |
332 structt.m22 = 1; | 413 struct_.m11 = 1; |
333 NSAffineTransform matrix = NSAffineTransform.transform(); | 414 struct_.m12 = shearX; |
334 matrix.setTransformStruct(structt); | 415 struct_.m21 = shearY; |
335 handle.prependTransform(matrix); | 416 struct_.m22 = 1; |
417 NSAffineTransform matrix = NSAffineTransform.transform(); | |
418 matrix.setTransformStruct(struct_); | |
419 handle.prependTransform(matrix); | |
420 } finally { | |
421 if (pool !is null) pool.release(); | |
422 } | |
336 } | 423 } |
337 | 424 |
338 /** | 425 /** |
339 * Given an array containing points described by alternating x and y values, | 426 * Given an array containing points described by alternating x and y values, |
340 * modify that array such that each point has been replaced with the result of | 427 * modify that array such that each point has been replaced with the result of |
350 * </ul> | 437 * </ul> |
351 */ | 438 */ |
352 public void transform(float[] pointArray) { | 439 public void transform(float[] pointArray) { |
353 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 440 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
354 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | 441 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); |
355 NSPoint point = NSPoint(); | 442 NSAutoreleasePool pool = null; |
356 size_t length = pointArray.length / 2; | 443 if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init(); |
357 for (int i = 0, j = 0; i < length; i++, j += 2) { | 444 try { |
358 point.x = pointArray[j]; | 445 NSPoint point = NSPoint(); |
359 point.y = pointArray[j + 1]; | 446 size_t length = pointArray.length / 2; |
360 point = handle.transformPoint(point); | 447 for (size_t i = 0, j = 0; i < length; i++, j += 2) { |
361 pointArray[j] = point.x; | 448 point.x = pointArray[j]; |
362 pointArray[j + 1] = point.y; | 449 point.y = pointArray[j + 1]; |
450 point = handle.transformPoint(point); | |
451 pointArray[j] = (float)/*64*/point.x; | |
452 pointArray[j + 1] = (float)/*64*/point.y; | |
453 } | |
454 } finally { | |
455 if (pool !is null) pool.release(); | |
363 } | 456 } |
364 } | 457 } |
365 | 458 |
366 /** | 459 /** |
367 * Modifies the receiver so that it represents a transformation that is | 460 * Modifies the receiver so that it represents a transformation that is |
374 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | 467 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
375 * </ul> | 468 * </ul> |
376 */ | 469 */ |
377 public void translate(float offsetX, float offsetY) { | 470 public void translate(float offsetX, float offsetY) { |
378 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | 471 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); |
379 handle.translateXBy(offsetX, offsetY); | 472 NSAutoreleasePool pool = null; |
473 if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init(); | |
474 try { | |
475 handle.translateXBy(offsetX, offsetY); | |
476 } finally { | |
477 if (pool !is null) pool.release(); | |
478 } | |
380 } | 479 } |
381 | 480 |
382 /** | 481 /** |
383 * Returns a string containing a concise, human-readable | 482 * Returns a string containing a concise, human-readable |
384 * description of the receiver. | 483 * description of the receiver. |