[API] Remove ResetTransform from Drawing API and update Backends, Samples, etc

ResetTransform cannot be safely implemented in Mac CoreGraphics.
The same functionality is available using a Save/Restore pair, so it can be removed.
An unfortunate case of the Backend wagging the API!
This commit is contained in:
Hywel Thomas 2012-12-11 10:54:03 +00:00 коммит произвёл Lluis Sanchez
Родитель 402a56a834
Коммит a9779043cf
6 изменённых файлов: 31 добавлений и 49 удалений

Просмотреть файл

@ -4,6 +4,7 @@
// Authors:
// Lluis Sanchez <lluis@xamarin.com>
// Lytico (http://limada.sourceforge.net)
// Hywel Thomas <hywel.w.thomas@gmail.com>
//
// Copyright (c) 2011 Xamarin Inc
//
@ -54,39 +55,49 @@ namespace Samples
public virtual void Rotate (Xwt.Drawing.Context ctx, double x, double y)
{
ctx.Save ();
ctx.Translate (x + 30, y + 30);
ctx.SetLineWidth (3);
// draws a line along the x-axis from (0,0) to (r,0) with a constant translation and an increasing
// rotational component. This composite transform is then applied to a vertical line, with inverse
// color, and an additional x-offset, to form a mirror image figure for easy visual comparison.
// These transformed points must be drawn with the identity CTM, hence the Restore() each time.
// Rotation
double end = 270;
ctx.Save (); // save caller's context (assumed to be the Identity CTM)
ctx.SetLineWidth (3); // should align exactly if drawn with half-pixel coordinates
// Vector length (pixels) and rotation limit (degrees)
double r = 30;
double end = 270;
for (double n = 0; n<=end; n += 5) {
ctx.Save ();
ctx.Save (); // save context and identity CTM for each line
// Set up translation to centre point of first figure, ensuring pixel alignment
ctx.Translate (x + 30.5, y + 30.5);
ctx.Rotate (n);
ctx.MoveTo (0, 0);
ctx.RelLineTo (r, 0);
double c = n / end;
ctx.SetColor (new Color (c, c, c));
ctx.Stroke ();
ctx.Stroke (); // stroke first figure with composite Translation and Rotation CTM
// Visual test for TransformPoints
// Generate mirror image figure as a visual test of TransformPoints
Point p0 = new Point (0,0);
Point p1 = new Point (0, -r);
Point[] p = new Point[] {p0, p1};
ctx.TransformPoints (p);
ctx.ResetTransform ();
ctx.Translate (2 * r + 1, 0);
ctx.TransformPoints (p); // using composite transformation
ctx.Restore (); // restore identity CTM
ctx.Save (); // save again (to restore after additional Translation)
ctx.Translate (2 * r + 1, 0); // extra x-offset to clear first figure
ctx.MoveTo (p[0]);
ctx.LineTo (p[1]);
c = 1-c;
ctx.SetColor (new Color (c, c, c));
ctx.Stroke();
ctx.Restore ();
ctx.Stroke(); // stroke transformed points with offset in CTM
ctx.Restore (); // restore identity CTM for next line
}
ctx.Restore ();
ctx.Restore (); // restore caller's context
}
public virtual void Scale (Context ctx, double ax, double ay)

Просмотреть файл

@ -90,6 +90,7 @@ namespace Samples
protected override void OnDraw (Context ctx, Rectangle dirtyRect)
{
ctx.Save ();
ctx.Translate (-hscroll.Value, -vscroll.Value);
ctx.Rectangle (new Rectangle (0, 0, imageSize, imageSize));
ctx.SetColor (Xwt.Drawing.Colors.White);
@ -97,8 +98,8 @@ namespace Samples
ctx.Arc (imageSize / 2, imageSize / 2, imageSize / 2 - 20, 0, 360);
ctx.SetColor (new Color (0,0,1));
ctx.Fill ();
ctx.ResetTransform ();
ctx.Restore ();
ctx.Rectangle (0, 0, Bounds.Width, 30);
ctx.SetColor (new Color (1, 0, 0, 0.5));
ctx.Fill ();

Просмотреть файл

@ -279,12 +279,6 @@ namespace Xwt.CairoBackend
return new Size (0,0);
}
public override void ResetTransform (object backend)
{
CairoContextBackend gc = (CairoContextBackend)backend;
gc.Context.IdentityMatrix();
}
public override void Rotate (object backend, double angle)
{
CairoContextBackend gc = (CairoContextBackend)backend;

Просмотреть файл

@ -271,17 +271,7 @@ namespace Xwt.Mac
ctx.RestoreState ();
}
public override void ResetTransform (object backend)
{
// http://stackoverflow.com/questions/469505/how-to-reset-to-identity-the-current-transformation-matrix-with-some-cgcontext
// "Note that inverting the current CTM with CGAffineTransformInvert does not work if your current CTM is singular."
CGContext ctx = ((CGContextBackend)backend).Context;
CGAffineTransform matrix = ctx.GetCTM ();
ctx.ConcatCTM (matrix.Invert ());
}
public override void Rotate (object backend, double angle)
public void Rotate (object backend, double angle)
{
((CGContextBackend)backend).Context.RotateCTM ((float)(angle * degrees));
}

Просмотреть файл

@ -315,13 +315,7 @@ namespace Xwt.WPFBackend
DrawImageCore (c.Graphics, bmp, srcRect, destRect, (float) alpha);
}
public override void ResetTransform (object backend)
{
var c = (DrawingContext)backend;
c.Graphics.ResetTransform ();
}
public override void Rotate (object backend, double angle)
public void Rotate (object backend, double angle)
{
var c = (DrawingContext)backend;
c.Graphics.RotateTransform ((float)angle);

Просмотреть файл

@ -181,14 +181,6 @@ namespace Xwt.Drawing
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha);
}
/// <summary>
/// Resets the current trasnformation matrix (CTM) to the Identity Matrix
/// </summary>
public void ResetTransform ()
{
handler.ResetTransform (Backend);
}
/// <summary>
/// Applies a rotation transformation
/// </summary>