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

class StylusPlugin2: IStylusSyncPlugin
{
	Color clr = Color.Black;
	Control ctrl;
	int iWidth = 100;
	int iOffset = 25;
	Pen pnShadow;
	Point ptStylus1;

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

		Graphics grfx = ctrl.CreateGraphics();
		iWidth = (int) (grfx.DpiX / 2.54f);
		iOffset = iWidth / 4;

		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));

		// Create Pen based on half-transparent bitmap
		Bitmap bm = new Bitmap(2, 2, grfx);
		bm.SetPixel(1, 0, clr);
		bm.SetPixel(0, 1, clr);
		TextureBrush tbrsh = new TextureBrush(bm);
		pnShadow = new Pen(tbrsh, iWidth);
		pnShadow.StartCap = pnShadow.EndCap = LineCap.Round;

		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 line with solid-color pen
				Pen pn = new Pen(clr, iWidth);
				pn.StartCap = pn.EndCap = LineCap.Round;
				grfx.DrawLine(pn, ptStylus1, ptStylus2); 

				// Draw line with shadow pen
				grfx.TranslateTransform(iOffset, iOffset);
				grfx.DrawLine(pnShadow, ptStylus1, ptStylus2);
				grfx.TranslateTransform(-iOffset, -iOffset);

				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) {}
}