using System; using System.Collections.Generic; using System.Linq; using System.Text; using CommonLang.Geometry.SceneGraph2D; using System.Drawing.Drawing2D; using CommonLang.Geometry; using System.Drawing; namespace CommonFroms.SceneGraph2D { public class Win32SceneGraphFactory : Factory { public static void Init() { if (Instance == null) { new Win32SceneGraphFactory(); } } private Win32SceneGraphFactory() { } public override ITransform CreateTransform() { return new Win32Transform(); } } public class Win32Graphics : IGraphics { public readonly Graphics gfx; private Stack state_stack = new Stack(); public Win32Graphics(Graphics g) { this.gfx = g; } public void PopTransform() { gfx.Restore(state_stack.Pop()); } public void PushTransform() { state_stack.Push(gfx.Save()); } public void Transform(ITransform trans) { (trans as Win32Transform).Apply(gfx); } } public class Win32Transform : ITransform { private Vector2 translation = new Vector2(); private float rotation = 0f; private Vector2 scale = new Vector2(1f, 1f); public Vector2 Translation { get { return translation; } set { translation = value; } } public float Rotation { get { return rotation; } set { rotation = value; } } public Vector2 Scale { get { return scale; } set { scale = value; } } internal void Apply(System.Drawing.Drawing2D.Matrix trans) { trans.Translate(translation.X, translation.Y); trans.Rotate(rotation); trans.Scale(scale.X, scale.Y); } internal void Invert(System.Drawing.Drawing2D.Matrix trans) { trans.Scale(1f / scale.X, 1f / scale.Y); trans.Rotate(-rotation); trans.Translate(-translation.X, -translation.Y); } internal void Apply(Graphics gfx) { gfx.TranslateTransform(translation.X, translation.Y); gfx.RotateTransform(rotation); gfx.ScaleTransform(scale.X, scale.Y); } public void LocalToParent(Vector2[] local) { var pts = new PointF[local.Length]; for (int i = 0; i < local.Length; i++) { pts[i] = new PointF(local[i].X, local[i].Y); } var mtx = new System.Drawing.Drawing2D.Matrix(); this.Apply(mtx); mtx.TransformPoints(pts); for (int i = 0; i < local.Length; i++) { local[i] = new Vector2(pts[i].X, pts[i].Y); } } public void ParentToLocal(Vector2[] parent) { var pts = new PointF[parent.Length]; for (int i = 0; i < parent.Length; i++) { pts[i] = new PointF(parent[i].X, parent[i].Y); } var mtx = new System.Drawing.Drawing2D.Matrix(); this.Invert(mtx); mtx.TransformPoints(pts); for (int i = 0; i < parent.Length; i++) { parent[i] = new Vector2(pts[i].X, pts[i].Y); } } } }