using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Serialization;
namespace DrawTools
{
///
/// Base class for all draw objects
///
abstract class DrawObject
{
#region Members
// Object properties
private bool selected;
private Color color;
private int penWidth;
// Allows to write Undo - Redo functions and don't care about
// objects order in the list.
int id;
// Last used property values (may be kept in the Registry)
private static Color lastUsedColor = Color.Black;
private static int lastUsedPenWidth = 1;
// Entry names for serialization
private const string entryColor = "Color";
private const string entryPenWidth = "PenWidth";
#endregion
public DrawObject()
{
id = this.GetHashCode();
}
#region Properties
///
/// Selection flag
///
public bool Selected
{
get
{
return selected;
}
set
{
selected = value;
}
}
///
/// Color
///
public Color Color
{
get
{
return color;
}
set
{
color = value;
}
}
///
/// Pen width
///
public int PenWidth
{
get
{
return penWidth;
}
set
{
penWidth = value;
}
}
///
/// Number of handles
///
public virtual int HandleCount
{
get
{
return 0;
}
}
///
/// Object ID
///
public int ID
{
get { return id; }
set { id = value; }
}
///
/// Last used color
///
public static Color LastUsedColor
{
get
{
return lastUsedColor;
}
set
{
lastUsedColor = value;
}
}
///
/// Last used pen width
///
public static int LastUsedPenWidth
{
get
{
return lastUsedPenWidth;
}
set
{
lastUsedPenWidth = value;
}
}
#endregion
#region Virtual Functions
///
/// Clone this instance.
///
public abstract DrawObject Clone();
///
/// Draw object
///
///
public virtual void Draw(Graphics g)
{
}
///
/// Get handle point by 1-based number
///
///
///
public virtual Point GetHandle(int handleNumber)
{
return new Point(0, 0);
}
///
/// Get handle rectangle by 1-based number
///
///
///
public virtual Rectangle GetHandleRectangle(int handleNumber)
{
Point point = GetHandle(handleNumber);
return new Rectangle(point.X - 3, point.Y - 3, 7, 7);
}
///
/// Draw tracker for selected object
///
///
public virtual void DrawTracker(Graphics g)
{
if ( ! Selected )
return;
SolidBrush brush = new SolidBrush(Color.Black);
for ( int i = 1; i <= HandleCount; i++ )
{
g.FillRectangle(brush, GetHandleRectangle(i));
}
brush.Dispose();
}
///
/// Hit test.
/// Return value: -1 - no hit
/// 0 - hit anywhere
/// > 1 - handle number
///
///
///
public virtual int HitTest(Point point)
{
return -1;
}
///
/// Test whether point is inside of the object
///
///
///
protected virtual bool PointInObject(Point point)
{
return false;
}
///
/// Get cursor for the handle
///
///
///
public virtual Cursor GetHandleCursor(int handleNumber)
{
return Cursors.Default;
}
///
/// Test whether object intersects with rectangle
///
///
///
public virtual bool IntersectsWith(Rectangle rectangle)
{
return false;
}
///
/// Move object
///
///
///
public virtual void Move(int deltaX, int deltaY)
{
}
///
/// Move handle to the point
///
///
///
public virtual void MoveHandleTo(Point point, int handleNumber)
{
}
///
/// Dump (for debugging)
///
public virtual void Dump()
{
Trace.WriteLine(this.GetType().Name);
Trace.WriteLine("Selected = " +
selected.ToString(CultureInfo.InvariantCulture)
+ " ID = " + id.ToString(CultureInfo.InvariantCulture));
}
///
/// Normalize object.
/// Call this function in the end of object resizing.
///
public virtual void Normalize()
{
}
///
/// Save object to serialization stream
///
///
///
public virtual void SaveToStream(SerializationInfo info, int orderNumber)
{
info.AddValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryColor, orderNumber),
Color.ToArgb());
info.AddValue(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryPenWidth, orderNumber),
PenWidth);
}
///
/// Load object from serialization stream
///
///
///
public virtual void LoadFromStream(SerializationInfo info, int orderNumber)
{
int n = info.GetInt32(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryColor, orderNumber));
Color = Color.FromArgb(n);
PenWidth = info.GetInt32(
String.Format(CultureInfo.InvariantCulture,
"{0}{1}",
entryPenWidth, orderNumber));
id = this.GetHashCode();
}
#endregion
#region Other functions
///
/// Initialization
///
protected void Initialize()
{
color = lastUsedColor;
penWidth = LastUsedPenWidth;
}
///
/// Copy fields from this instance to cloned instance drawObject.
/// Called from Clone functions of derived classes.
///
protected void FillDrawObjectFields(DrawObject drawObject)
{
drawObject.selected = this.selected;
drawObject.color = this.color;
drawObject.penWidth = this.penWidth;
drawObject.ID = this.ID;
}
#endregion
}
}