using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WPFRawMultitouch
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private Dictionary<int, Ellipse> _pointMap = new Dictionary<int,Ellipse>();

		public MainWindow()
		{
			InitializeComponent();
		}

		private void TouchSurface_TouchDown(object sender, TouchEventArgs e)
		{
			touchStatus.Text = "Touch Down";

			Ellipse ellipse;
			if(!_pointMap.TryGetValue(e.TouchDevice.Id, out ellipse))
			{
				ellipse = new Ellipse();
				ellipse.Fill = new SolidColorBrush(Colors.Red);
				ellipse.Stroke = new SolidColorBrush(Colors.Black);
				ellipse.Width = 80;
				ellipse.Height= 80;
				_pointMap.Add(e.TouchDevice.Id, ellipse);
				TouchSurface.Children.Add(ellipse);
			}
			Canvas.SetLeft(ellipse, e.GetTouchPoint(TouchSurface).Position.X - ellipse.Width/2);
			Canvas.SetTop(ellipse, e.GetTouchPoint(TouchSurface).Position.Y - ellipse.Height/2);
		}

		private void TouchSurface_TouchMove(object sender, TouchEventArgs e)
		{
			touchStatus.Text = "Touch Move";
			Ellipse ellipse;
			if(!_pointMap.TryGetValue(e.TouchDevice.Id, out ellipse))
				return;
			Canvas.SetLeft(ellipse, e.GetTouchPoint(TouchSurface).Position.X - ellipse.Width/2);
			Canvas.SetTop(ellipse, e.GetTouchPoint(TouchSurface).Position.Y - ellipse.Height/2);
		}

		private void TouchSurface_TouchUp(object sender, TouchEventArgs e)
		{
			touchStatus.Text = "Touch Up";
			Ellipse ellipse;
			if(!_pointMap.TryGetValue(e.TouchDevice.Id, out ellipse))
				return;
			TouchSurface.Children.Remove(ellipse);
			_pointMap.Remove(e.TouchDevice.Id);
		}
	}
}
