|
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Bornander.UI.DropHandlers
{
public class TextDropHandler
{
private static readonly Random random = new Random();
private SurfacePanel panel;
private double maxVelocity;
private double maxAngularVelocity;
private bool originalAllowDrop;
public TextDropHandler(SurfacePanel panel, double maxVelocity, double maxAngularVelocity)
{
this.panel = panel;
this.maxVelocity = maxVelocity;
this.maxAngularVelocity = maxAngularVelocity;
originalAllowDrop = panel.AllowDrop;
}
public void Attach()
{
panel.AllowDrop = true;
panel.Drop += HandleDrop;
}
public void Detatch()
{
panel.AllowDrop = originalAllowDrop;
panel.Drop -= HandleDrop;
}
private void HandleDrop(object sender, DragEventArgs e)
{
if (e.Data.GetFormats().Contains(DataFormats.Text) && e.Data.GetDataPresent(DataFormats.Text))
{
foreach (string text in ((string)e.Data.GetData(DataFormats.Text)).Split(' '))
{
Label label = new Label { Content = text, Background = Brushes.White };
label.Measure(new Size(panel.ActualWidth, panel.ActualHeight));
SurfacePanel.SetPosition(label, (Point)panel.MousePosition);
SurfacePanel.SetSize(label, label.DesiredSize);
SurfacePanel.SetAngularVelocity(label, random.NextDouble() * maxAngularVelocity - maxAngularVelocity / 2.0);
SurfacePanel.SetVelocity(label, new Vector(random.NextDouble() * maxVelocity - maxVelocity / 2.0, random.NextDouble() * maxVelocity - maxVelocity / 2.0));
panel.Children.Add(label);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Article videos
Oakmead Apps Android Games
21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010