diff --git a/Samples/Samples/DrawingTransforms.cs b/Samples/Samples/DrawingTransforms.cs index 2f7ed538..cf2b4db0 100644 --- a/Samples/Samples/DrawingTransforms.cs +++ b/Samples/Samples/DrawingTransforms.cs @@ -4,6 +4,7 @@ // Authors: // Lluis Sanchez // Lytico (http://limada.sourceforge.net) +// Hywel Thomas // // 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) diff --git a/Samples/Samples/ScrollWindowSample.cs b/Samples/Samples/ScrollWindowSample.cs index 149099ec..0d82af4a 100644 --- a/Samples/Samples/ScrollWindowSample.cs +++ b/Samples/Samples/ScrollWindowSample.cs @@ -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 (); diff --git a/Xwt.Gtk/Xwt.CairoBackend/CairoContextBackendHandler.cs b/Xwt.Gtk/Xwt.CairoBackend/CairoContextBackendHandler.cs index b4955723..80e1085d 100644 --- a/Xwt.Gtk/Xwt.CairoBackend/CairoContextBackendHandler.cs +++ b/Xwt.Gtk/Xwt.CairoBackend/CairoContextBackendHandler.cs @@ -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; diff --git a/Xwt.Mac/Xwt.Mac/ContextBackendHandler.cs b/Xwt.Mac/Xwt.Mac/ContextBackendHandler.cs index 742f8ee7..b88891bc 100644 --- a/Xwt.Mac/Xwt.Mac/ContextBackendHandler.cs +++ b/Xwt.Mac/Xwt.Mac/ContextBackendHandler.cs @@ -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)); } diff --git a/Xwt.WPF/Xwt.WPFBackend/ContextBackendHandler.cs b/Xwt.WPF/Xwt.WPFBackend/ContextBackendHandler.cs index c741da3a..9006924d 100644 --- a/Xwt.WPF/Xwt.WPFBackend/ContextBackendHandler.cs +++ b/Xwt.WPF/Xwt.WPFBackend/ContextBackendHandler.cs @@ -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); diff --git a/Xwt/Xwt.Drawing/Context.cs b/Xwt/Xwt.Drawing/Context.cs index 5ba54a30..ec0255f6 100644 --- a/Xwt/Xwt.Drawing/Context.cs +++ b/Xwt/Xwt.Drawing/Context.cs @@ -181,14 +181,6 @@ namespace Xwt.Drawing handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha); } - /// - /// Resets the current trasnformation matrix (CTM) to the Identity Matrix - /// - public void ResetTransform () - { - handler.ResetTransform (Backend); - } - /// /// Applies a rotation transformation ///