using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace lauamang { public partial class Kabelaud : UserControl { int a = 30; //Ruudu külje pikkus int ridu = 8; int veerge = 8; Kabenupp liigutatav = null; Kabenupp iseliikuv = null; Storyboard tahvel = new Storyboard(); DoubleAnimation xAnim = new DoubleAnimation(); DoubleAnimation yAnim = new DoubleAnimation(); public Kabelaud() { InitializeComponent(); looRuudud(); looNupud(); this.MouseMove += new MouseEventHandler(Kabelaud_MouseMove); tahvel.Children.Add(xAnim); tahvel.Children.Add(yAnim); Storyboard.SetTargetProperty(xAnim, new PropertyPath("(Canvas.Left)")); Storyboard.SetTargetProperty(yAnim, new PropertyPath("(Canvas.Top)")); tahvel.Completed += new EventHandler(tahvel_Completed); } void liigutaKomponent(UIElement kujund, double lopux, double lopuy) { //tahvel.Pause(); Storyboard.SetTarget(xAnim, kujund); Storyboard.SetTarget(yAnim, kujund); xAnim.To = lopux; yAnim.To = lopuy; iseliikuv = kujund as Kabenupp; tahvel.Begin(); } void tahvel_Completed(object sender, EventArgs e) { tahvel.Stop(); Canvas.SetLeft(iseliikuv, iseliikuv.Veerg * a + 5); Canvas.SetTop(iseliikuv, iseliikuv.Rida* a + 5); //MessageBox.Show("kohal"); } void Kabelaud_MouseMove(object sender, MouseEventArgs e) { if (liigutatav != null) { Canvas.SetLeft(liigutatav, e.GetPosition(null).X-liigutatav.Width/2); Canvas.SetTop(liigutatav, e.GetPosition(null).Y-liigutatav.Height/2); } } void looRuudud() { for (int rida = 0; rida < ridu; rida++) { for (int veerg = 0; veerg < veerge; veerg++) { Rectangle ruut = new Rectangle(); if ((rida + veerg) % 2 == 0) { ruut.Fill = new SolidColorBrush(Colors.Black); } else { ruut.Fill = new SolidColorBrush(Colors.White); } ruut.Width = a; ruut.Height = a; Canvas.SetLeft(ruut, veerg * a); Canvas.SetTop(ruut, rida * a); juur.Children.Add(ruut); } } juur.UpdateLayout(); } void looNupud() { for (int rida = 0; rida < ridu; rida++) { for (int veerg = 0; veerg < veerge; veerg++) { if ((rida + veerg) % 2 == 0) { Kabenupp nupp = null; if (rida < 3) { nupp = new Kabenupp(Colors.Gray); } if (rida > 4) { nupp = new Kabenupp(Colors.White); } if(nupp!=null){ Canvas.SetLeft(nupp, veerg * a+5); Canvas.SetTop(nupp, rida * a+5); nupp.Rida = rida; nupp.Veerg = veerg; juur.Children.Add(nupp); nupp.MouseLeftButtonDown += new MouseButtonEventHandler( nupp_MouseLeftButtonDown); nupp.MouseLeftButtonUp += new MouseButtonEventHandler(nupp_MouseLeftButtonUp); } } } } juur.UpdateLayout(); } void nupp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if(liigutatav!=null){ int veerg = (int)(Canvas.GetLeft(liigutatav) / a); int rida = (int)(Canvas.GetTop(liigutatav) / a); if (rida < ridu) { Canvas.SetLeft(liigutatav, a * veerg + 5); Canvas.SetTop(liigutatav, a * rida + 5); liigutatav.Rida = rida; liigutatav.Veerg = veerg; } else { liigutaKomponent(liigutatav, liigutatav.Veerg * a + 5, liigutatav.Rida*a+5); } liigutatav = null; } } void nupp_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { liigutatav = sender as Kabenupp; } } }