Nodes.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using CommonLang;
  2. using CommonLang.Geometry;
  3. using CommonLang.Geometry.SceneGraph2D;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Linq;
  9. using System.Text;
  10. namespace CommonFroms.SceneGraph2D.Edit
  11. {
  12. //----------------------------------------------------------------------------------------------------------------------------------
  13. public abstract class EditorNode : DisplayNode
  14. {
  15. public bool IsPickable { get; set; }
  16. public bool IsSelectable { get; set; }
  17. public bool IsDragMoveable { get; set; }
  18. public bool IsSelected { get; internal set; }
  19. public bool IsPicked { get; internal set; }
  20. public EditorNode()
  21. {
  22. this.IsDragMoveable = true;
  23. this.IsPickable = true;
  24. this.IsSelectable = true;
  25. }
  26. //----------------------------------------------------------------------
  27. private float current_scale_from_root;
  28. private Vector2 current_root_point;
  29. /// <summary>
  30. /// 相对屏幕的缩放比
  31. /// </summary>
  32. public float CurrentScreenScale { get { return current_scale_from_root; } }
  33. /// <summary>
  34. /// 相对屏幕的坐标
  35. /// </summary>
  36. public Vector2 CurrentScreenPoint { get { return current_root_point; } }
  37. internal void VisitHUD(Graphics g)
  38. {
  39. this.current_root_point = LocalToRoot(Vector2.Zero);
  40. this.current_scale_from_root = LocalToRootLength(1);
  41. g.TranslateTransform(current_root_point.X, current_root_point.Y);
  42. try
  43. {
  44. OnDrawHUD(g);
  45. }
  46. finally
  47. {
  48. g.TranslateTransform(-current_root_point.X, -current_root_point.Y);
  49. }
  50. }
  51. protected virtual void OnDrawHUD(Graphics g) { }
  52. //----------------------------------------------------------------------
  53. protected override void OnDrawAfter(IGraphics g)
  54. {
  55. base.OnDrawAfter(g);
  56. var gfx = (g as Win32Graphics).gfx;
  57. if (IsPicked)
  58. {
  59. DrawPicked(gfx);
  60. }
  61. if (IsSelected)
  62. {
  63. DrawSelected(gfx);
  64. }
  65. }
  66. public Pen pen_select = new Pen(System.Drawing.Color.LightSkyBlue);
  67. public Pen pen_pick = new Pen(System.Drawing.Color.White);
  68. protected virtual void DrawPicked(Graphics g)
  69. {
  70. g.DrawEllipse(pen_pick, -4, -4, 8, 8);
  71. }
  72. protected virtual void DrawSelected(Graphics g)
  73. {
  74. pen_select.Width = 2;
  75. //pen_select.StartCap = LineCap.Round;
  76. //pen_select.DashStyle = System.Drawing.Drawing2D.DashStyle.
  77. g.DrawEllipse(pen_select, -4, -4, 8, 8);
  78. }
  79. //----------------------------------------------------------------------
  80. #region Utils
  81. private Vector2[] temp_pts = new Vector2[2];
  82. public float LocalToRootLength(float len)
  83. {
  84. temp_pts[0] = Vector2.Zero;
  85. temp_pts[1] = new Vector2(0, len);
  86. base.TransformLocalToRoot(temp_pts);
  87. return Vector2.Distance(temp_pts[0], temp_pts[1]);
  88. }
  89. public float RootToLocalLength(float len)
  90. {
  91. temp_pts[0] = Vector2.Zero;
  92. temp_pts[1] = new Vector2(0, len);
  93. base.TransformRootToLocal(temp_pts);
  94. return Vector2.Distance(temp_pts[0], temp_pts[1]);
  95. }
  96. #endregion
  97. //----------------------------------------------------------------------
  98. }
  99. //----------------------------------------------------------------------------------------------------------------------------------
  100. public abstract class EditorRectNode : EditorNode
  101. {
  102. public abstract RectangleF Bounds { get; }
  103. protected override void DrawSelected(Graphics g)
  104. {
  105. base.DrawSelected(g);
  106. var bounds = this.Bounds;
  107. g.DrawRectangle(pen_select, bounds.X, bounds.Y, bounds.Width, bounds.Height);
  108. }
  109. protected override void DrawPicked(Graphics g)
  110. {
  111. var bounds = this.Bounds;
  112. g.DrawRectangle(pen_pick, bounds.X, bounds.Y, bounds.Width, bounds.Height);
  113. }
  114. protected override bool OnHitTest(ref Vector2 localPoint)
  115. {
  116. var bounds = this.Bounds;
  117. return CMath.includeRectPoint2(bounds.X, bounds.Y, bounds.Width, bounds.Height, localPoint.X, localPoint.Y);
  118. }
  119. }
  120. //----------------------------------------------------------------------------------------------------------------------------------
  121. public class EditorImageNode : EditorRectNode
  122. {
  123. private Bitmap _dimg;
  124. private RectangleF _bounds;
  125. public override RectangleF Bounds
  126. {
  127. get { return _bounds; }
  128. }
  129. public Bitmap Image
  130. {
  131. get { return _dimg; }
  132. }
  133. public EditorImageNode(System.Drawing.Image src)
  134. {
  135. _dimg = ImageUtils.ImageToBitmap(src);
  136. _bounds = new RectangleF(-_dimg.Width / 2f, -_dimg.Height / 2f, _dimg.Width, _dimg.Height);
  137. }
  138. public bool GetPixel(int x, int y, out Color ret)
  139. {
  140. if (x >= 0 && x < _dimg.Width && y >= 0 && y < _dimg.Height)
  141. {
  142. ret = _dimg.GetPixel(x, y);
  143. return true;
  144. }
  145. ret = System.Drawing.Color.FromArgb(0, 0, 0, 0);
  146. return false;
  147. }
  148. protected override void OnDrawBegin(IGraphics g)
  149. {
  150. base.OnDrawBegin(g);
  151. var gfx = (g as Win32Graphics).gfx;
  152. gfx.DrawImage(_dimg, _bounds);
  153. }
  154. // protected override bool OnHitTest(ref Vector2 localPoint)
  155. // {
  156. // Color pix;
  157. // if (GetPixel(
  158. // (int)(localPoint.X - _bounds.X),
  159. // (int)(localPoint.Y - _bounds.Y),
  160. // out pix))
  161. // {
  162. // return pix.A != 0;
  163. // }
  164. // return false;
  165. // }
  166. public static EditorImageNode CreateImage(string file)
  167. {
  168. System.Drawing.Image image = System.Drawing.Image.FromFile(file);
  169. var ret = new EditorImageNode(image);
  170. return ret;
  171. }
  172. }
  173. }