using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using Tango.Core.EventArguments;
using Tango.SharedUI.Helpers;
namespace Tango.Touch.Components
{
public static class TransformationHelper
{
#region TransformWhenPressed
///
/// Determines whether an element is TransformWhenPressed by the drag and drop service.
///
public static readonly DependencyProperty TransformWhenPressedProperty =
DependencyProperty.RegisterAttached("TransformWhenPressed",
typeof(bool), typeof(TransformationHelper),
new FrameworkPropertyMetadata(false, TransformWhenPressedChanged));
///
/// TransformWhenPressed changed.
///
/// The d.
/// The instance containing the event data.
private static void TransformWhenPressedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
RegisterTransformWhenPressed(d as FrameworkElement);
}
else
{
UnRegisterTransformWhenPressed(d as FrameworkElement);
}
}
private static void UnRegisterTransformWhenPressed(FrameworkElement element)
{
}
private static void RegisterTransformWhenPressed(FrameworkElement element)
{
element.RenderTransformOrigin = new Point(0.5, 0.5);
element.RenderTransform = new ScaleTransform(1, 1);
element.RegisterForPreviewMouseOrTouchDown(Element_PreviewMouseDown);
element.PreviewMouseUp += Element_PreviewMouseUp;
var scrollViewer = element.FindAncestor();
if (scrollViewer != null)
{
scrollViewer.ScrollChanged += (x, y) =>
{
ScaleTransform scale = element.RenderTransform as ScaleTransform;
scale.BeginAnimation(ScaleTransform.ScaleXProperty, null);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, null);
};
}
}
private static void Element_PreviewMouseDown(object sender, MouseOrTouchEventArgs e)
{
if (!GetPreventTransform(e.OriginalSource as FrameworkElement))
{
DoubleAnimation ani = new DoubleAnimation();
ani.To = 1.09;
ani.Duration = TimeSpan.FromSeconds(0.2);
ani.AutoReverse = true;
ScaleTransform scale = (sender as FrameworkElement).RenderTransform as ScaleTransform;
scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani);
}
}
private static void Element_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DoubleAnimation ani = new DoubleAnimation();
ani.To = 1;
ani.Duration = TimeSpan.FromSeconds(0.1);
ScaleTransform scale = (sender as FrameworkElement).RenderTransform as ScaleTransform;
scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani);
scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani);
}
///
/// Sets the TransformWhenPressed attached property.
///
/// The element.
/// if set to true [value].
public static void SetTransformWhenPressed(FrameworkElement element, bool value)
{
element.SetValue(TransformWhenPressedProperty, value);
}
///
/// Gets the TransformWhenPressed attached property.
///
/// The element.
///
public static bool GetTransformWhenPressed(FrameworkElement element)
{
return (bool)element.GetValue(TransformWhenPressedProperty);
}
#endregion
#region PreventTransform
///
/// Determines whether an element is PreventTransform by the drag and drop service.
///
public static readonly DependencyProperty PreventTransformProperty =
DependencyProperty.RegisterAttached("PreventTransform",
typeof(bool), typeof(TransformationHelper),
new FrameworkPropertyMetadata(false));
///
/// Sets the PreventTransform attached property.
///
/// The element.
/// if set to true [value].
public static void SetPreventTransform(FrameworkElement element, bool value)
{
element.SetValue(PreventTransformProperty, value);
}
///
/// Gets the PreventTransform attached property.
///
/// The element.
///
public static bool GetPreventTransform(FrameworkElement element)
{
if (element != null)
{
return (bool)element.GetValue(PreventTransformProperty);
}
else
{
return false;
}
}
#endregion
}
}