//----------------------------------------------------------------------
// StylusPlugin0.cs (c) 2005 by Charles Petzold, www.charlespetzold.com
//----------------------------------------------------------------------
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Microsoft.StylusInput;
using Microsoft.StylusInput.PluginData;

class StylusPlugin0: IStylusSyncPlugin
{
	Control ctrl;
	int iWidth;
	int iOffset;
	Point ptStylus1;

	public StylusPlugin0(Control ctrl)
	{
		this.ctrl = ctrl;

		Graphics grfx = ctrl.CreateGraphics();
		iWidth = (int) (grfx.DpiX / 2.54f);	// set width to 1 centimeter
		iOffset = iWidth / 4;	// set shadow offset to 1/4 centimeter

		grfx.Dispose();
	}

	public DataInterestMask DataInterest
	{ 
		get { return DataInterestMask.Packets | DataInterestMask.StylusDown; }
	}

	public void StylusDown(RealTimeStylus sender, StylusDownData data) 
	{
		Graphics grfx = ctrl.CreateGraphics();
		ptStylus1 = new Point((int) (grfx.DpiX * data[0] / 2540 + 0.5),
							  (int) (grfx.DpiY * data[1] / 2540 + 0.5));
		grfx.Dispose();
	}

	public void Packets(RealTimeStylus sender, PacketsData data) 
	{
		Graphics grfx = ctrl.CreateGraphics();
		grfx.SmoothingMode = SmoothingMode.AntiAlias;

		for (int i = 0; i < data.Count; i += data.PacketPropertyCount)
		{
			Point ptStylus2 = new Point((int) (grfx.DpiX * data[0] / 2540 + 0.5), 
										(int) (grfx.DpiY * data[1] / 2540 + 0.5));

			if (ptStylus2 != ptStylus1)
			{
				// Draw offsetted shadow in gray
				Pen pn = new Pen(Color.Gray, iWidth);
				pn.StartCap = pn.EndCap = LineCap.Round;

				grfx.TranslateTransform(iOffset, iOffset);
				grfx.DrawLine(pn, ptStylus1, ptStylus2); 
				grfx.TranslateTransform(-iOffset, -iOffset);				

				// Draw wide line in black
				pn.Color = Color.Black;
		 		grfx.DrawLine(pn, ptStylus1, ptStylus2); 
				ptStylus1 = ptStylus2;
			}
		}
		grfx.Dispose();
	}

	public void StylusUp(RealTimeStylus sender, StylusUpData data) { }
	public void CustomStylusDataAdded(RealTimeStylus sender, CustomStylusData data) {}
	public void Error(RealTimeStylus sender, ErrorData data) {}
	public void InAirPackets(RealTimeStylus sender, InAirPacketsData data) {}
	public void RealTimeStylusDisabled(RealTimeStylus sender, RealTimeStylusDisabledData data) {}
	public void RealTimeStylusEnabled(RealTimeStylus sender, RealTimeStylusEnabledData data) {}
	public void StylusButtonDown(RealTimeStylus sender, StylusButtonDownData data) {}
	public void StylusButtonUp(RealTimeStylus sender, StylusButtonUpData data) {}
	public void StylusInRange(RealTimeStylus sender, StylusInRangeData data) {}
	public void StylusOutOfRange(RealTimeStylus sender, StylusOutOfRangeData data) {}
	public void SystemGesture(RealTimeStylus sender, SystemGestureData data) {}
	public void TabletAdded(RealTimeStylus sender, TabletAddedData data) {}
	public void TabletRemoved(RealTimeStylus sender, TabletRemovedData data) {}
}