comparison dwt/graphics/Transform.d @ 34:5123b17c98ef

Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sun, 14 Sep 2008 01:45:57 +0200
parents 1a8b3cb347e0
children db5a898b2119
comparison
equal deleted inserted replaced
33:965ac0a77267 34:5123b17c98ef
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 *
11 * Port to the D programming language:
12 * Jacob Carlborg <jacob.carlborg@gmail.com>
10 *******************************************************************************/ 13 *******************************************************************************/
11 module dwt.graphics.Transform; 14 module dwt.graphics.Transform;
12
13 import dwt.dwthelper.utils;
14 15
15 import dwt.DWT; 16 import dwt.DWT;
16 import dwt.DWTError; 17 import dwt.DWTError;
17 import dwt.DWTException; 18 import dwt.DWTException;
18 import dwt.internal.cocoa.NSAffineTransform; 19 import dwt.internal.cocoa.NSAffineTransform;
19 import dwt.internal.cocoa.NSAffineTransformStruct; 20 import dwt.internal.cocoa.NSAffineTransformStruct;
20 import dwt.internal.cocoa.NSPoint; 21 import dwt.internal.cocoa.NSPoint;
22
23 import tango.text.convert.Format;
24
25 import dwt.dwthelper.utils;
26 import dwt.graphics.Device;
27 import dwt.graphics.Resource;
21 28
22 /** 29 /**
23 * Instances of this class represent transformation matrices for 30 * Instances of this class represent transformation matrices for
24 * points expressed as (x, y) pairs of floating point numbers. 31 * points expressed as (x, y) pairs of floating point numbers.
25 * <p> 32 * <p>
167 */ 174 */
168 public void getElements(float[] elements) { 175 public void getElements(float[] elements) {
169 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 176 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
170 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 177 if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
171 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 178 if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
172 NSAffineTransformStruct struct = handle.transformStruct(); 179 NSAffineTransformStruct structt = handle.transformStruct();
173 elements[0] = struct.m11; 180 elements[0] = structt.m11;
174 elements[1] = struct.m12; 181 elements[1] = structt.m12;
175 elements[2] = struct.m21; 182 elements[2] = structt.m21;
176 elements[3] = struct.m22; 183 elements[3] = structt.m22;
177 elements[4] = struct.tX; 184 elements[4] = structt.tX;
178 elements[5] = struct.tY; 185 elements[5] = structt.tY;
179 } 186 }
180 187
181 public void identity() { 188 public void identity() {
182 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 189 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
183 NSAffineTransformStruct struct = new NSAffineTransformStruct(); 190 NSAffineTransformStruct structt = NSAffineTransformStruct();
184 struct.m11 = 1; 191 structt.m11 = 1;
185 struct.m22 = 1; 192 structt.m22 = 1;
186 handle.setTransformStruct(struct); 193 handle.setTransformStruct(structt);
187 } 194 }
188 195
189 /** 196 /**
190 * Modifies the receiver such that the matrix it represents becomes the 197 * Modifies the receiver such that the matrix it represents becomes the
191 * the mathematical inverse of the matrix it previously represented. 198 * the mathematical inverse of the matrix it previously represented.
195 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li> 202 * <li>ERROR_CANNOT_INVERT_MATRIX - if the matrix is not invertible</li>
196 * </ul> 203 * </ul>
197 */ 204 */
198 public void invert() { 205 public void invert() {
199 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 206 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
200 NSAffineTransformStruct struct = handle.transformStruct(); 207 NSAffineTransformStruct structt = handle.transformStruct();
201 if ((struct.m11 * struct.m22 - struct.m12 * struct.m21) is 0) { 208 if ((structt.m11 * structt.m22 - structt.m12 * structt.m21) is 0) {
202 DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX); 209 DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX);
203 } 210 }
204 handle.invert(); 211 handle.invert();
205 } 212 }
206 213
224 * 231 *
225 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise 232 * @return <code>true</code> if the receiver is an identity Transform, and <code>false</code> otherwise
226 */ 233 */
227 public bool isIdentity() { 234 public bool isIdentity() {
228 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 235 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
229 NSAffineTransformStruct struct = handle.transformStruct(); 236 NSAffineTransformStruct structt = handle.transformStruct();
230 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; 237 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;
231 } 238 }
232 239
233 /** 240 /**
234 * Modifies the receiver such that the matrix it represents becomes the 241 * Modifies the receiver such that the matrix it represents becomes the
235 * the result of multiplying the matrix it previously represented by the 242 * the result of multiplying the matrix it previously represented by the
301 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> 308 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
302 * </ul> 309 * </ul>
303 */ 310 */
304 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) { 311 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) {
305 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 312 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
306 NSAffineTransformStruct struct = new NSAffineTransformStruct(); 313 NSAffineTransformStruct structt = NSAffineTransformStruct();
307 struct.m11 = m11; 314 structt.m11 = m11;
308 struct.m12 = m12; 315 structt.m12 = m12;
309 struct.m21 = m21; 316 structt.m21 = m21;
310 struct.m22 = m22; 317 structt.m22 = m22;
311 struct.tX = dx; 318 structt.tX = dx;
312 struct.tY = dy; 319 structt.tY = dy;
313 handle.setTransformStruct(struct); 320 handle.setTransformStruct(structt);
314 } 321 }
315 322
316 public void shear(float shearX, float shearY) { 323 public void shear(float shearX, float shearY) {
317 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 324 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
318 NSAffineTransformStruct struct = new NSAffineTransformStruct(); 325 NSAffineTransformStruct structt = NSAffineTransformStruct();
319 struct.m11 = 1; 326 structt.m11 = 1;
320 struct.m12 = shearX; 327 structt.m12 = shearX;
321 struct.m21 = shearY; 328 structt.m21 = shearY;
322 struct.m22 = 1; 329 structt.m22 = 1;
323 NSAffineTransform matrix = NSAffineTransform.transform(); 330 NSAffineTransform matrix = NSAffineTransform.transform();
324 matrix.setTransformStruct(struct); 331 matrix.setTransformStruct(structt);
325 handle.prependTransform(matrix); 332 handle.prependTransform(matrix);
326 } 333 }
327 334
328 /** 335 /**
329 * Given an array containing points described by alternating x and y values, 336 * Given an array containing points described by alternating x and y values,
340 * </ul> 347 * </ul>
341 */ 348 */
342 public void transform(float[] pointArray) { 349 public void transform(float[] pointArray) {
343 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 350 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
344 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 351 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
345 NSPoint point = new NSPoint(); 352 NSPoint point = NSPoint();
346 int length = pointArray.length / 2; 353 size_t length = pointArray.length / 2;
347 for (int i = 0, j = 0; i < length; i++, j += 2) { 354 for (int i = 0, j = 0; i < length; i++, j += 2) {
348 point.x = pointArray[j]; 355 point.x = pointArray[j];
349 point.y = pointArray[j + 1]; 356 point.y = pointArray[j + 1];
350 point = handle.transformPoint(point); 357 point = handle.transformPoint(point);
351 pointArray[j] = point.x; 358 pointArray[j] = point.x;
377 */ 384 */
378 public String toString() { 385 public String toString() {
379 if (isDisposed()) return "Transform {*DISPOSED*}"; 386 if (isDisposed()) return "Transform {*DISPOSED*}";
380 float[] elements = new float[6]; 387 float[] elements = new float[6];
381 getElements(elements); 388 getElements(elements);
382 return "Transform {" + elements [0] + ", " + elements [1] + ", " +elements [2] + ", " +elements [3] + ", " +elements [4] + ", " +elements [5] + "}"; 389 return Format("Transform {{}{}{}{}{}{}{}{}{}{}{}{}" , elements [0] , ", " , elements [1] , ", " ,elements [2] , ", " ,elements [3] , ", " ,elements [4] , ", " ,elements [5] , "}");
383 } 390 }
384 391
385 } 392 }