using System;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
using MouseGestures;
using MouseGestures.Utilities;
namespace GestureRecognizer
{
public class GesturePanel : Panel
{
private PointF[] m_Points = new PointF[0];
private Gesture m_Gesture;
private PointF m_MinPoint;
private PointF m_MaxPoint;
private Color m_BaseColor;
private int m_Transparency;
private Pen m_Pen;
private Pen m_BasePen;
private SolidBrush m_StartBrush;
private SolidBrush m_EndBrush;
public Color BaseColor
{
get { return m_BaseColor; }
set
{
m_BaseColor = value;
BackColor = Color.FromArgb(m_Transparency, value);
Invalidate();
//Refresh();
}
}
public int Transparency
{
get { return m_Transparency; }
set
{
m_Transparency = value;
BackColor = Color.FromArgb(value, m_BaseColor);
Invalidate();
//Refresh();
}
}
public Color StartColor
{
get { return m_StartBrush.Color; }
set
{
m_StartBrush.Color = value;
Invalidate();
//Refresh();
}
}
public Color StandardColor
{
get { return m_Pen.Color; }
set
{
//m_StandardBrush.Color = value;
m_Pen = new Pen(value);
Invalidate();
//Refresh();
}
}
public Color EndColor
{
get { return m_EndBrush.Color; }
set
{
m_EndBrush.Color = value;
Invalidate();
//Refresh();
}
}
public Gesture Gesture
{
get { return m_Gesture; }
set
{
if (value == null)
{
m_Gesture = null;
m_Points = new PointF[0];
}
else
{
m_Gesture = value;
m_Points = value.ToArray();
#region Performing geometric operations to fit gesture in the panel
Mathematics.GetBoundingBox(m_Points, out m_MinPoint, out m_MaxPoint);
Mathematics.TranslateToOrigin(m_Points, ref m_MinPoint, ref m_MaxPoint);
Mathematics.ScaleToFit(m_Points, Math.Min(ClientSize.Width, ClientSize.Height) - 10, ref m_MinPoint, ref m_MaxPoint);
Mathematics.TranslateToCenter(m_Points, ClientSize.Width, ClientSize.Height, ref m_MinPoint, ref m_MaxPoint);
#endregion
}
//Refresh();
Invalidate();
}
}
public PointF[] Points
{
get { return m_Points; }
set
{
m_Gesture = null;
if (value == null)
m_Points = new PointF[0];
else
{
m_Points = value;
if (m_Points.Length > 0)
{
#region Performing geometric operations to fit gesture in the panel
Mathematics.GetBoundingBox(m_Points, out m_MinPoint, out m_MaxPoint);
Mathematics.TranslateToOrigin(m_Points, ref m_MinPoint, ref m_MaxPoint);
Mathematics.ScaleToFit(m_Points, Math.Min(ClientSize.Width, ClientSize.Height) - 10, ref m_MinPoint, ref m_MaxPoint);
Mathematics.TranslateToCenter(m_Points, ClientSize.Width, ClientSize.Height, ref m_MinPoint, ref m_MaxPoint);
#endregion
}
}
//Refresh();
Invalidate();
}
}
public GesturePanel()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
DoubleBuffered = true;
m_BaseColor = Color.Gray;
m_Transparency = 100;
m_Pen = new Pen(Color.Black, 1);
m_BasePen = new Pen(Color.Gray, 1);
m_StartBrush = new SolidBrush(Color.Green);
m_EndBrush = new SolidBrush(Color.Red);
BackColor = Color.FromArgb(m_Transparency, m_BaseColor);
}
protected override void OnPaint(PaintEventArgs e)
{
if (m_Points.Length > 1)
{
e.Graphics.DrawLines(m_Pen, m_Points);
e.Graphics.FillRectangle(m_StartBrush, m_Points[0].X - 1.5f, m_Points[0].Y - 1.5f, 3, 3);
e.Graphics.FillRectangle(m_EndBrush, m_Points[m_Points.Length - 1].X - 1.5f, m_Points[m_Points.Length - 1].Y - 1.5f, 3, 3);
}
e.Graphics.DrawRectangle(new Pen(m_BaseColor), 0, 0, Width - 1, Height - 1);
}
public void Clear()
{
m_Gesture = null;
m_Points = new PointF[0];
//Refresh();
Invalidate();
}
}
}