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;
///
/// 相对屏幕的缩放比
///
public float CurrentScreenScale { get { return current_scale_from_root; } }
///
/// 相对屏幕的坐标
///
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;
}
}
}