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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BPASmartClient.CustomResource.UserControls
{
///
/// PipeLineBR.xaml 的交互逻辑
///
public partial class PipeLineBR : UserControl
{
public PipeLineBR()
{
InitializeComponent();
this.Loaded += (s, e) => { Play(); Refresh(); };
}
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as PipeLineBR)?.Refresh();
}
private void Refresh()
{
this.mp.StrokeThickness = LineWidth;
Canvas.SetTop(this.mp, 25 - LineWidth / 2);
Canvas.SetLeft(this.mp, 25 - LineWidth / 2);
this.mp.Stroke = LineBrush;
//this.Width = PipeWidth * 2;
//this.Height = PipeWidth * 2;
}
#region 动画相关
Storyboard sb = new Storyboard();
private void Play()
{
sb.Children.Clear();
DoubleAnimation da = new DoubleAnimation();
da.From = 0;
if (Direction == 2) da.To = this.ActualWidth * 3.14 / 4;
else if (Direction == 1) da.To = this.ActualWidth * 3.14 / 4 * -1;
da.RepeatBehavior = RepeatBehavior.Forever;//设置无限循环播放
da.Duration = TimeSpan.FromMilliseconds(Speed * 1000);//设置动画时间
//da.AutoReverse = true;//设置可以反转播放动画
Storyboard.SetTarget(da, mp);//绑定动画
Storyboard.SetTargetProperty(da, new PropertyPath("StrokeDashOffset"));
sb.Children.Add(da);//添加动画
if (Direction != 0) this.mp.Visibility = Visibility.Visible;
sb.Begin();//播放动画
}
private static void OnAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as PipeLineBR)?.Animation();
}
private void Animation()
{
if (Direction == 0)
{
sb.Stop();
this.mp.Visibility = Visibility.Collapsed;
}
else Play();
}
///
/// 液体流向控制
/// 0:停止流动,1正向流动,2反向流动
///
public int Direction
{
get { return (int)GetValue(DirectionProperty); }
set { SetValue(DirectionProperty, value); }
}
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register("Direction", typeof(int), typeof(PipeLineBR), new(0, new(OnAnimationChanged)));
///
/// 液体流动速度
/// 液体流动一次的时间,单位秒
///
public float Speed
{
get { return (float)GetValue(SpeedProperty); }
set { SetValue(SpeedProperty, value); }
}
public static readonly DependencyProperty SpeedProperty =
DependencyProperty.Register("Speed", typeof(float), typeof(PipeLineBR), new(8.0f, new(OnAnimationChanged)));
#endregion
///
/// 流动宽度
///
public int LineWidth
{
get { return (int)GetValue(LineWidthProperty); }
set { SetValue(LineWidthProperty, value); }
}
public static readonly DependencyProperty LineWidthProperty =
DependencyProperty.Register("LineWidth", typeof(int), typeof(PipeLineBR), new(4, new(OnPropertyChanged)));
///
/// 流动线条颜色
///
public Brush LineBrush
{
get { return (Brush)GetValue(LineBrushProperty); }
set { SetValue(LineBrushProperty, value); }
}
public static readonly DependencyProperty LineBrushProperty =
DependencyProperty.Register("LineBrush", typeof(Brush), typeof(PipeLineBR), new(Brushes.DeepSkyBlue));
public int PipeWidth
{
get { return (int)GetValue(PipeWidthProperty); }
set { SetValue(PipeWidthProperty, value); }
}
public static readonly DependencyProperty PipeWidthProperty =
DependencyProperty.Register("PipeWidth", typeof(int), typeof(PipeLineBR), new(30, new(OnPropertyChanged)));
///
/// 管道内壁颜色
///
public Color Fill
{
get { return (Color)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Color), typeof(PipeLineBR), new(Brushes.DimGray.Color));
}
}