123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using CommonLang;
- using CommonLang.Geometry;
- using CommonLang.Geometry.SceneGraph2D;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- namespace CommonFroms.SceneGraph2D.Edit
- {
- //----------------------------------------------------------------------------------------------------------------------------------
- public abstract class EditorNode : DisplayNode
- {
- public bool IsPickable { get; set; }
- public bool IsSelectable { get; set; }
- public bool IsDragMoveable { get; set; }
- public bool IsSelected { get; internal set; }
- public bool IsPicked { get; internal set; }
- public EditorNode()
- {
- this.IsDragMoveable = true;
- this.IsPickable = true;
- this.IsSelectable = true;
- }
- //----------------------------------------------------------------------
- private float current_scale_from_root;
- private Vector2 current_root_point;
- /// <summary>
- /// 相对屏幕的缩放比
- /// </summary>
- public float CurrentScreenScale { get { return current_scale_from_root; } }
- /// <summary>
- /// 相对屏幕的坐标
- /// </summary>
- public Vector2 CurrentScreenPoint { get { return current_root_point; } }
- internal void VisitHUD(Graphics g)
- {
- this.current_root_point = LocalToRoot(Vector2.Zero);
- this.current_scale_from_root = LocalToRootLength(1);
- g.TranslateTransform(current_root_point.X, current_root_point.Y);
- try
- {
- OnDrawHUD(g);
- }
- finally
- {
- g.TranslateTransform(-current_root_point.X, -current_root_point.Y);
- }
- }
- protected virtual void OnDrawHUD(Graphics g) { }
- //----------------------------------------------------------------------
- protected override void OnDrawAfter(IGraphics g)
- {
- base.OnDrawAfter(g);
- var gfx = (g as Win32Graphics).gfx;
- if (IsPicked)
- {
- DrawPicked(gfx);
- }
- if (IsSelected)
- {
- DrawSelected(gfx);
- }
- }
- public Pen pen_select = new Pen(System.Drawing.Color.LightSkyBlue);
- public Pen pen_pick = new Pen(System.Drawing.Color.White);
- protected virtual void DrawPicked(Graphics g)
- {
- g.DrawEllipse(pen_pick, -4, -4, 8, 8);
- }
- protected virtual void DrawSelected(Graphics g)
- {
- pen_select.Width = 2;
- //pen_select.StartCap = LineCap.Round;
- //pen_select.DashStyle = System.Drawing.Drawing2D.DashStyle.
- g.DrawEllipse(pen_select, -4, -4, 8, 8);
- }
- //----------------------------------------------------------------------
- #region Utils
- private Vector2[] temp_pts = new Vector2[2];
- public float LocalToRootLength(float len)
- {
- temp_pts[0] = Vector2.Zero;
- temp_pts[1] = new Vector2(0, len);
- base.TransformLocalToRoot(temp_pts);
- return Vector2.Distance(temp_pts[0], temp_pts[1]);
- }
- public float RootToLocalLength(float len)
- {
- temp_pts[0] = Vector2.Zero;
- temp_pts[1] = new Vector2(0, len);
- base.TransformRootToLocal(temp_pts);
- return Vector2.Distance(temp_pts[0], temp_pts[1]);
- }
- #endregion
- //----------------------------------------------------------------------
- }
- //----------------------------------------------------------------------------------------------------------------------------------
- public abstract class EditorRectNode : EditorNode
- {
- public abstract RectangleF Bounds { get; }
- protected override void DrawSelected(Graphics g)
- {
- base.DrawSelected(g);
- var bounds = this.Bounds;
- g.DrawRectangle(pen_select, bounds.X, bounds.Y, bounds.Width, bounds.Height);
- }
- protected override void DrawPicked(Graphics g)
- {
- var bounds = this.Bounds;
- g.DrawRectangle(pen_pick, bounds.X, bounds.Y, bounds.Width, bounds.Height);
- }
- protected override bool OnHitTest(ref Vector2 localPoint)
- {
- var bounds = this.Bounds;
- return CMath.includeRectPoint2(bounds.X, bounds.Y, bounds.Width, bounds.Height, localPoint.X, localPoint.Y);
- }
- }
- //----------------------------------------------------------------------------------------------------------------------------------
- public class EditorImageNode : EditorRectNode
- {
- private Bitmap _dimg;
- private RectangleF _bounds;
- public override RectangleF Bounds
- {
- get { return _bounds; }
- }
- public Bitmap Image
- {
- get { return _dimg; }
- }
- public EditorImageNode(System.Drawing.Image src)
- {
- _dimg = ImageUtils.ImageToBitmap(src);
- _bounds = new RectangleF(-_dimg.Width / 2f, -_dimg.Height / 2f, _dimg.Width, _dimg.Height);
- }
- public bool GetPixel(int x, int y, out Color ret)
- {
- if (x >= 0 && x < _dimg.Width && y >= 0 && y < _dimg.Height)
- {
- ret = _dimg.GetPixel(x, y);
- return true;
- }
- ret = System.Drawing.Color.FromArgb(0, 0, 0, 0);
- return false;
- }
- protected override void OnDrawBegin(IGraphics g)
- {
- base.OnDrawBegin(g);
- var gfx = (g as Win32Graphics).gfx;
- gfx.DrawImage(_dimg, _bounds);
- }
- // protected override bool OnHitTest(ref Vector2 localPoint)
- // {
- // Color pix;
- // if (GetPixel(
- // (int)(localPoint.X - _bounds.X),
- // (int)(localPoint.Y - _bounds.Y),
- // out pix))
- // {
- // return pix.A != 0;
- // }
- // return false;
- // }
- public static EditorImageNode CreateImage(string file)
- {
- System.Drawing.Image image = System.Drawing.Image.FromFile(file);
- var ret = new EditorImageNode(image);
- return ret;
- }
- }
- }
|