Win32SceneGraph.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CommonLang.Geometry.SceneGraph2D;
  6. using System.Drawing.Drawing2D;
  7. using CommonLang.Geometry;
  8. using System.Drawing;
  9. namespace CommonFroms.SceneGraph2D
  10. {
  11. public class Win32SceneGraphFactory : Factory
  12. {
  13. public static void Init()
  14. {
  15. if (Instance == null) { new Win32SceneGraphFactory(); }
  16. }
  17. private Win32SceneGraphFactory() { }
  18. public override ITransform CreateTransform()
  19. {
  20. return new Win32Transform();
  21. }
  22. }
  23. public class Win32Graphics : IGraphics
  24. {
  25. public readonly Graphics gfx;
  26. private Stack<GraphicsState> state_stack = new Stack<GraphicsState>();
  27. public Win32Graphics(Graphics g)
  28. {
  29. this.gfx = g;
  30. }
  31. public void PopTransform()
  32. {
  33. gfx.Restore(state_stack.Pop());
  34. }
  35. public void PushTransform()
  36. {
  37. state_stack.Push(gfx.Save());
  38. }
  39. public void Transform(ITransform trans)
  40. {
  41. (trans as Win32Transform).Apply(gfx);
  42. }
  43. }
  44. public class Win32Transform : ITransform
  45. {
  46. private Vector2 translation = new Vector2();
  47. private float rotation = 0f;
  48. private Vector2 scale = new Vector2(1f, 1f);
  49. public Vector2 Translation
  50. {
  51. get { return translation; }
  52. set { translation = value; }
  53. }
  54. public float Rotation
  55. {
  56. get { return rotation; }
  57. set { rotation = value; }
  58. }
  59. public Vector2 Scale
  60. {
  61. get { return scale; }
  62. set { scale = value; }
  63. }
  64. internal void Apply(System.Drawing.Drawing2D.Matrix trans)
  65. {
  66. trans.Translate(translation.X, translation.Y);
  67. trans.Rotate(rotation);
  68. trans.Scale(scale.X, scale.Y);
  69. }
  70. internal void Invert(System.Drawing.Drawing2D.Matrix trans)
  71. {
  72. trans.Scale(1f / scale.X, 1f / scale.Y);
  73. trans.Rotate(-rotation);
  74. trans.Translate(-translation.X, -translation.Y);
  75. }
  76. internal void Apply(Graphics gfx)
  77. {
  78. gfx.TranslateTransform(translation.X, translation.Y);
  79. gfx.RotateTransform(rotation);
  80. gfx.ScaleTransform(scale.X, scale.Y);
  81. }
  82. public void LocalToParent(Vector2[] local)
  83. {
  84. var pts = new PointF[local.Length];
  85. for (int i = 0; i < local.Length; i++) { pts[i] = new PointF(local[i].X, local[i].Y); }
  86. var mtx = new System.Drawing.Drawing2D.Matrix();
  87. this.Apply(mtx);
  88. mtx.TransformPoints(pts);
  89. for (int i = 0; i < local.Length; i++) { local[i] = new Vector2(pts[i].X, pts[i].Y); }
  90. }
  91. public void ParentToLocal(Vector2[] parent)
  92. {
  93. var pts = new PointF[parent.Length];
  94. for (int i = 0; i < parent.Length; i++) { pts[i] = new PointF(parent[i].X, parent[i].Y); }
  95. var mtx = new System.Drawing.Drawing2D.Matrix();
  96. this.Invert(mtx);
  97. mtx.TransformPoints(pts);
  98. for (int i = 0; i < parent.Length; i++) { parent[i] = new Vector2(pts[i].X, pts[i].Y); }
  99. }
  100. }
  101. }