Skip to content

Instantly share code, notes, and snippets.

@wieslawsoltes
Created June 3, 2015 07:01
Show Gist options
  • Save wieslawsoltes/d0e5aa8b4c38d89e3982 to your computer and use it in GitHub Desktop.
Save wieslawsoltes/d0e5aa8b4c38d89e3982 to your computer and use it in GitHub Desktop.
Eto.Forms Direct2D transform test
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eto.Forms;
using Eto.Drawing;
namespace MyApp1
{
class MyForm : Eto.Forms.Form
{
public MyForm()
{
this.ClientSize = new Eto.Drawing.Size(900, 700);
this.Title = "Hello, Eto.Forms " + Platform.ID;
var drawable = new Drawable { Size = new Size(800, 600) };
drawable.Paint += (sender, e) =>
{
var t = new
{
CenterX = 0f,
CenterY = 0f,
OffsetX = 120f,
OffsetY = 90f,
RotateAngle = 0f,
SkewAngleY = 10f,
SkewAngleX = 10f,
ScaleX = 16f,
ScaleY = 16f
};
var g = e.Graphics;
var gp = new GraphicsPath();
gp.FillMode = FillMode.Winding;
gp.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f));
gp.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f));
gp.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f));
gp.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f));
gp.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f));
gp.AddLine(7.64f, 9.64f, 10f, 12f);
gp.AddLine(10f, 12f, 7.64f, 14.36f);
gp.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f));
gp.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f));
gp.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f));
gp.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f));
gp.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f));
gp.AddLine(9.64f, 16.36f, 12f, 14f);
gp.AddLine(12f, 14f, 19f, 21f);
gp.AddLine(19f, 21f, 22f, 21f);
gp.AddLine(22f, 21f, 22f, 20f);
gp.AddLine(22f, 20f, 9.64f, 7.64f);
gp.CloseFigure();
gp.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f));
gp.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f));
gp.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f));
gp.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f));
gp.CloseFigure();
gp.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f));
gp.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f));
gp.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f));
gp.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f));
gp.CloseFigure();
gp.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f));
gp.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f));
gp.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f));
gp.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f));
gp.CloseFigure();
gp.AddLine(19f, 3f, 13f, 9f);
gp.AddLine(13f, 9f, 15f, 11f);
gp.AddLine(15f, 11f, 22f, 4f);
gp.AddLine(22f, 4f, 22f, 3f);
gp.CloseFigure();
gp.AddBezier(new PointF(9.64f, 7.64f), new PointF(9.87f, 7.14f), new PointF(10f, 6.59f), new PointF(10f, 6f));
gp.AddBezier(new PointF(10f, 6f), new PointF(10f, 3.79f), new PointF(8.21f, 2f), new PointF(6f, 2f));
gp.AddBezier(new PointF(6f, 2f), new PointF(3.79f, 2f), new PointF(2f, 3.79f), new PointF(2f, 6f));
gp.AddBezier(new PointF(2f, 6f), new PointF(2f, 8.21f), new PointF(3.79f, 10f), new PointF(6f, 10f));
gp.AddBezier(new PointF(6f, 10f), new PointF(6.59f, 10f), new PointF(7.14f, 9.87f), new PointF(7.64f, 9.64f));
gp.AddLine(7.64f, 9.64f, 10f, 12f);
gp.AddLine(10f, 12f, 7.64f, 14.36f);
gp.AddBezier(new PointF(7.64f, 14.36f), new PointF(7.14f, 14.13f), new PointF(6.59f, 14f), new PointF(6f, 14f));
gp.AddBezier(new PointF(6f, 14f), new PointF(3.79f, 14f), new PointF(2f, 15.79f), new PointF(2f, 18f));
gp.AddBezier(new PointF(2f, 18f), new PointF(2f, 20.21f), new PointF(3.79f, 22f), new PointF(6f, 22f));
gp.AddBezier(new PointF(6f, 22f), new PointF(8.21f, 22f), new PointF(10f, 20.21f), new PointF(10f, 18f));
gp.AddBezier(new PointF(10f, 18f), new PointF(10f, 17.41f), new PointF(9.87f, 16.86f), new PointF(9.64f, 16.36f));
gp.AddLine(9.64f, 16.36f, 12f, 14f);
gp.AddLine(12f, 14f, 19f, 21f);
gp.AddLine(19f, 21f, 22f, 21f);
gp.AddLine(22f, 21f, 22f, 20f);
gp.AddLine(22f, 20f, 9.64f, 7.64f);
gp.CloseFigure();
gp.AddBezier(new PointF(6f, 8f), new PointF(4.9f, 8f), new PointF(4f, 7.11f), new PointF(4f, 6f));
gp.AddBezier(new PointF(4f, 6f), new PointF(4f, 4.89f), new PointF(4.9f, 4f), new PointF(6f, 4f));
gp.AddBezier(new PointF(6f, 4f), new PointF(7.1f, 4f), new PointF(8f, 4.89f), new PointF(8f, 6f));
gp.AddBezier(new PointF(8f, 6f), new PointF(8f, 7.11f), new PointF(7.1f, 8f), new PointF(6f, 8f));
gp.CloseFigure();
gp.AddBezier(new PointF(6f, 20f), new PointF(4.9f, 20f), new PointF(4f, 19.11f), new PointF(4f, 18f));
gp.AddBezier(new PointF(4f, 18f), new PointF(4f, 16.89f), new PointF(4.9f, 16f), new PointF(6f, 16f));
gp.AddBezier(new PointF(6f, 16f), new PointF(7.1f, 16f), new PointF(8f, 16.89f), new PointF(8f, 18f));
gp.AddBezier(new PointF(8f, 18f), new PointF(8f, 19.11f), new PointF(7.1f, 20f), new PointF(6f, 20f));
gp.CloseFigure();
gp.AddBezier(new PointF(12f, 12.5f), new PointF(11.72f, 12.5f), new PointF(11.5f, 12.28f), new PointF(11.5f, 12f));
gp.AddBezier(new PointF(11.5f, 12f), new PointF(11.5f, 11.72f), new PointF(11.72f, 11.5f), new PointF(12f, 11.5f));
gp.AddBezier(new PointF(12f, 11.5f), new PointF(12.28f, 11.5f), new PointF(12.5f, 11.72f), new PointF(12.5f, 12f));
gp.AddBezier(new PointF(12.5f, 12f), new PointF(12.5f, 12.28f), new PointF(12.28f, 12.5f), new PointF(12f, 12.5f));
gp.CloseFigure();
gp.AddLine(19f, 3f, 13f, 9f);
gp.AddLine(13f, 9f, 15f, 11f);
gp.AddLine(15f, 11f, 22f, 4f);
gp.AddLine(22f, 4f, 22f, 3f);
gp.CloseFigure();
var m = Matrix.Create();
var c = new PointF((float)t.CenterX, (float)t.CenterY);
// translate
m.Translate((float)t.OffsetX, (float)t.OffsetY);
// rotate
m.RotateAt((float)t.RotateAngle, c);
// skew
m.Translate(-c.X, -c.Y);
m.Prepend(
Matrix.Create(
1,
(float)Math.Tan(Math.PI * t.SkewAngleY / 180.0),
(float)Math.Tan(Math.PI * t.SkewAngleX / 180.0),
1,
0,
0));
m.Translate(c.X, c.Y);
// scale
m.ScaleAt((float)t.ScaleX, (float)t.ScaleY, (float)t.CenterX, (float)t.CenterY);
g.SaveTransform();
g.MultiplyTransform(m);
var brush = new SolidBrush(Colors.Black);
var pen = new Pen(Colors.Red, 0.5f);
g.FillPath(
brush,
gp);
g.DrawPath(
pen,
gp);
brush.Dispose();
pen.Dispose();
g.RestoreTransform();
};
var layout = new PixelLayout();
layout.Add(drawable, 20, 20);
Content = layout;
}
}
class Program
{
static void Main(string[] args)
{
new Application(Eto.Platforms.Direct2D).Run(new MyForm());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment