PipeLine.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Windows.Media.Animation;
  16. using MECF.Framework.UI.Client.IndustrialControl.Converters;
  17. namespace MECF.Framework.UI.Client.IndustrialControl.ELK
  18. {
  19. /// <summary>
  20. /// UserControl1.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class PipeLine : UserControl
  23. {
  24. #region Contructor
  25. /// <summary>
  26. /// 实例化一个管道对象
  27. /// </summary>
  28. public PipeLine()
  29. {
  30. InitializeComponent();
  31. //Binding binding = new Binding( );
  32. //binding.Source = grid1;
  33. //binding.Path = new PropertyPath( "ActualHeight" );
  34. //binding.Converter = new MultiplesValueConverter( );
  35. //binding.ConverterParameter = -1;
  36. //ellipe1.SetBinding( Canvas.TopProperty, binding );
  37. }
  38. #endregion
  39. #region Property Dependency
  40. #region LeftDirection Property
  41. /// <summary>
  42. /// 设置左边的方向
  43. /// </summary>
  44. public HslPipeTurnDirection LeftDirection
  45. {
  46. get { return (HslPipeTurnDirection)GetValue(LeftDirectionProperty); }
  47. set { SetValue(LeftDirectionProperty, value); }
  48. }
  49. // Using a DependencyProperty as the backing store for LeftDirection. This enables animation, styling, binding, etc...
  50. public static readonly DependencyProperty LeftDirectionProperty =
  51. DependencyProperty.Register("LeftDirection", typeof(HslPipeTurnDirection), typeof(PipeLine),
  52. new PropertyMetadata(HslPipeTurnDirection.Left, new PropertyChangedCallback(LeftDirectionPropertyChangedCallback)));
  53. public static void LeftDirectionPropertyChangedCallback(System.Windows.DependencyObject dependency, System.Windows.DependencyPropertyChangedEventArgs e)
  54. {
  55. PipeLine pipeLine = (PipeLine)dependency;
  56. pipeLine.UpdateLeftDirectionBinding();
  57. }
  58. public void UpdateLeftDirectionBinding()
  59. {
  60. BindingOperations.ClearBinding(ellipe1, Canvas.TopProperty);
  61. if (LeftDirection == HslPipeTurnDirection.Left)
  62. {
  63. canvas1.Visibility = Visibility.Visible;
  64. Binding binding = new Binding();
  65. binding.Source = grid1;
  66. binding.Path = new PropertyPath("ActualHeight");
  67. binding.Converter = new MultiplesValueConverter();
  68. binding.ConverterParameter = 0;
  69. ellipe1.SetBinding(Canvas.TopProperty, binding);
  70. }
  71. else if (LeftDirection == HslPipeTurnDirection.Right)
  72. {
  73. canvas1.Visibility = Visibility.Visible;
  74. Binding binding = new Binding();
  75. binding.Source = grid1;
  76. binding.Path = new PropertyPath("ActualHeight");
  77. binding.Converter = new MultiplesValueConverter();
  78. binding.ConverterParameter = -1;
  79. ellipe1.SetBinding(Canvas.TopProperty, binding);
  80. }
  81. else
  82. {
  83. canvas1.Visibility = Visibility.Collapsed;
  84. }
  85. // UpdatePathData();
  86. }
  87. #endregion
  88. #region RightDirection Property
  89. /// <summary>
  90. /// 设置右边的方向
  91. /// </summary>
  92. public HslPipeTurnDirection RightDirection
  93. {
  94. get { return (HslPipeTurnDirection)GetValue(RightDirectionProperty); }
  95. set { SetValue(RightDirectionProperty, value); }
  96. }
  97. // Using a DependencyProperty as the backing store for LeftDirection. This enables animation, styling, binding, etc...
  98. public static readonly DependencyProperty RightDirectionProperty =
  99. DependencyProperty.Register("RightDirection", typeof(HslPipeTurnDirection), typeof(PipeLine),
  100. new PropertyMetadata(HslPipeTurnDirection.Right, new PropertyChangedCallback(RightDirectionPropertyChangedCallback)));
  101. public static void RightDirectionPropertyChangedCallback(System.Windows.DependencyObject dependency, System.Windows.DependencyPropertyChangedEventArgs e)
  102. {
  103. PipeLine pipeLine = (PipeLine)dependency;
  104. pipeLine.UpdateRightDirectionBinding();
  105. }
  106. public void UpdateRightDirectionBinding()
  107. {
  108. BindingOperations.ClearBinding(ellipe2, Canvas.TopProperty);
  109. if (RightDirection == HslPipeTurnDirection.Left)
  110. {
  111. canvas2.Visibility = Visibility.Visible;
  112. Binding binding = new Binding();
  113. binding.Source = grid1;
  114. binding.Path = new PropertyPath("ActualHeight");
  115. binding.Converter = new MultiplesValueConverter();
  116. binding.ConverterParameter = 0;
  117. ellipe2.SetBinding(Canvas.TopProperty, binding);
  118. }
  119. else if (RightDirection == HslPipeTurnDirection.Right)
  120. {
  121. canvas2.Visibility = Visibility.Visible;
  122. Binding binding = new Binding();
  123. binding.Source = grid1;
  124. binding.Path = new PropertyPath("ActualHeight");
  125. binding.Converter = new MultiplesValueConverter();
  126. binding.ConverterParameter = -1;
  127. ellipe2.SetBinding(Canvas.TopProperty, binding);
  128. }
  129. else
  130. {
  131. canvas2.Visibility = Visibility.Collapsed;
  132. }
  133. // UpdatePathData();
  134. }
  135. #endregion
  136. #region PipeLineActive Property
  137. public bool PipeLineActive
  138. {
  139. get { return (bool)GetValue(PipeLineActiveProperty); }
  140. set { SetValue(PipeLineActiveProperty, value); }
  141. }
  142. // Using a DependencyProperty as the backing store for PipeLineActive. This enables animation, styling, binding, etc...
  143. public static readonly DependencyProperty PipeLineActiveProperty =
  144. DependencyProperty.Register("PipeLineActive", typeof(bool), typeof(PipeLine), new PropertyMetadata(false));
  145. #endregion
  146. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  147. {
  148. // UpdatePathData();
  149. base.OnRenderSizeChanged(sizeInfo);
  150. }
  151. #region LineOffect Property
  152. public double LineOffect
  153. {
  154. get { return (double)GetValue(LineOffectProperty); }
  155. set { SetValue(LineOffectProperty, value); }
  156. }
  157. // Using a DependencyProperty as the backing store for LineOffect. This enables animation, styling, binding, etc...
  158. public static readonly DependencyProperty LineOffectProperty =
  159. DependencyProperty.Register("LineOffect", typeof(double), typeof(PipeLine), new PropertyMetadata(0d));
  160. StreamGeometry sg = new StreamGeometry();
  161. public void UpdatePathData()
  162. {
  163. //Console.WriteLine( "Size Changed" );
  164. double height = Height;
  165. double width = Width;
  166. using (StreamGeometryContext context = sg.Open())
  167. {
  168. if (LeftDirection == HslPipeTurnDirection.Left)
  169. {
  170. context.BeginFigure(new Point(height * 0.5, height), false, false);
  171. context.ArcTo(new Point(height, height * 0.5), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Clockwise, true, false);
  172. }
  173. else if (LeftDirection == HslPipeTurnDirection.Right)
  174. {
  175. context.BeginFigure(new Point(height * 0.5, 0), false, false);
  176. context.ArcTo(new Point(height, height * 0.5), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Counterclockwise, true, false);
  177. }
  178. else
  179. {
  180. context.BeginFigure(new Point(0, height * 0.5), false, false);
  181. context.LineTo(new Point(height, height * 0.5), true, false);
  182. }
  183. context.LineTo(new Point(width - height, height * 0.5), true, false);
  184. if (RightDirection == HslPipeTurnDirection.Left)
  185. {
  186. context.ArcTo(new Point(width - height * 0.5, height), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Clockwise, true, false);
  187. }
  188. else if (RightDirection == HslPipeTurnDirection.Right)
  189. {
  190. context.ArcTo(new Point(width - height * 0.5, 0), new Size(height * 0.5, height * 0.5), 0, false, SweepDirection.Counterclockwise, true, false);
  191. }
  192. else
  193. {
  194. context.LineTo(new Point(width, height * 0.5), true, false);
  195. }
  196. }
  197. //sg.Freeze();
  198. path1.Data = sg;
  199. }
  200. #endregion
  201. #region MoveSpeed Property
  202. /// <summary>
  203. /// 获取或设置流动的速度
  204. /// </summary>
  205. public double MoveSpeed
  206. {
  207. get { return (double)GetValue(MoveSpeedProperty); }
  208. set { SetValue(MoveSpeedProperty, value); }
  209. }
  210. // Using a DependencyProperty as the backing store for MoveSpeed. This enables animation, styling, binding, etc...
  211. public static readonly DependencyProperty MoveSpeedProperty =
  212. DependencyProperty.Register("MoveSpeed", typeof(double), typeof(PipeLine), new PropertyMetadata(0.0d, new PropertyChangedCallback(MoveSpeedPropertyChangedCallback)));
  213. public static void MoveSpeedPropertyChangedCallback(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
  214. {
  215. PipeLine pipeLine = (PipeLine)dependency;
  216. pipeLine.UpdateMoveSpeed();
  217. }
  218. private Storyboard storyboard = new Storyboard();
  219. public void UpdateMoveSpeed()
  220. {
  221. //jet炉管删除流向动画
  222. //if (path1.Data != null)
  223. //{
  224. // path1.Data = null;
  225. // //offectDoubleAnimation.FillBehavior = FillBehavior.Stop;
  226. // //offectDoubleAnimation = null;
  227. // path1.Visibility = Visibility.Hidden;
  228. //}
  229. //return;
  230. if (MoveSpeed > 0)
  231. {
  232. EdgeColor = Colors.DimGray;
  233. CenterColor = Colors.LightGray;
  234. ActiveLineCenterColor = Color.FromRgb(91, 160, 91);
  235. UpdatePathData();
  236. path1.Visibility = Visibility.Visible;
  237. //VisualStateManager.GoToState(this, "WEFlowState" , false);
  238. //offectDoubleAnimation = new DoubleAnimation(0d, 10d, TimeSpan.FromMilliseconds(300 / MoveSpeed));
  239. //offectDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
  240. //BeginAnimation(LineOffectProperty, offectDoubleAnimation);
  241. //Storyboard.SetTarget(offectDoubleAnimation, path1);
  242. //Storyboard.SetTargetProperty(offectDoubleAnimation, "X");
  243. //storyboard.Children.Add(offectDoubleAnimation);
  244. //storyboard.Begin();
  245. }
  246. else if (MoveSpeed < 0)
  247. {
  248. EdgeColor = Colors.DimGray;
  249. CenterColor = Colors.LightGray;
  250. UpdatePathData();
  251. path1.Visibility = Visibility.Visible;
  252. //offectDoubleAnimation = new DoubleAnimation(0d, -10d, TimeSpan.FromMilliseconds(300 / Math.Abs(MoveSpeed)));
  253. //offectDoubleAnimation.FillBehavior = FillBehavior.HoldEnd;
  254. //offectDoubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
  255. //BeginAnimation(LineOffectProperty, offectDoubleAnimation);
  256. //Storyboard.SetTarget(offectDoubleAnimation, path1);
  257. ////Storyboard.SetTargetProperty(offectDoubleAnimation, "(UIElement.RenderTransform).(CompositeTransform.ScaleY)");
  258. //storyboard.Children.Add(offectDoubleAnimation);
  259. //storyboard.Begin();
  260. }
  261. else
  262. {
  263. if (path1.Data != null)
  264. {
  265. path1.Data = null;
  266. //offectDoubleAnimation.FillBehavior = FillBehavior.Stop;
  267. //offectDoubleAnimation = null;
  268. path1.Visibility = Visibility.Hidden;
  269. }
  270. }
  271. }
  272. #endregion
  273. #region CenterColor Property
  274. /// <summary>
  275. /// 管道的中心颜色
  276. /// </summary>
  277. public Color CenterColor
  278. {
  279. get { return (Color)GetValue(CenterColorProperty); }
  280. set { SetValue(CenterColorProperty, value); }
  281. }
  282. // Using a DependencyProperty as the backing store for CenterColor. This enables animation, styling, binding, etc...
  283. public static readonly DependencyProperty CenterColorProperty =
  284. DependencyProperty.Register("CenterColor", typeof(Color), typeof(PipeLine), new PropertyMetadata(Colors.LightGray));
  285. #endregion
  286. #region PipeLineWidth Property
  287. /// <summary>
  288. /// 管道活动状态时的中心线的线条宽度
  289. /// </summary>
  290. public int PipeLineWidth
  291. {
  292. get { return (int)GetValue(PipeLineWidthProperty); }
  293. set { SetValue(PipeLineWidthProperty, value); }
  294. }
  295. // Using a DependencyProperty as the backing store for PipeLineWidth. This enables animation, styling, binding, etc...
  296. public static readonly DependencyProperty PipeLineWidthProperty =
  297. DependencyProperty.Register("PipeLineWidth", typeof(int), typeof(PipeLine), new PropertyMetadata(2));
  298. #endregion
  299. #region ActiveLineCenterColor Property
  300. /// <summary>
  301. /// 管道活动状态时的中心线的颜色信息
  302. /// </summary>
  303. public Color ActiveLineCenterColor
  304. {
  305. get { return (Color)GetValue(ActiveLineCenterColorProperty); }
  306. set { SetValue(ActiveLineCenterColorProperty, value); }
  307. }
  308. // Using a DependencyProperty as the backing store for ActiveLineCenterColor. This enables animation, styling, binding, etc...
  309. public static readonly DependencyProperty ActiveLineCenterColorProperty =
  310. DependencyProperty.Register("ActiveLineCenterColor", typeof(Color), typeof(PipeLine), new PropertyMetadata(Colors.DodgerBlue));
  311. #endregion
  312. #region MyRegion
  313. /// <summary>
  314. /// 管道控件的边缘颜色
  315. /// </summary>
  316. public Color EdgeColor
  317. {
  318. get { return (Color)GetValue(EdgeColorProperty); }
  319. set { SetValue(EdgeColorProperty, value); }
  320. }
  321. // Using a DependencyProperty as the backing store for EdgeColor. This enables animation, styling, binding, etc...
  322. public static readonly DependencyProperty EdgeColorProperty =
  323. DependencyProperty.Register("EdgeColor", typeof(Color), typeof(PipeLine), new PropertyMetadata(Colors.DimGray));
  324. #endregion
  325. #endregion
  326. }
  327. public enum HslPipeTurnDirection
  328. {
  329. /// <summary>
  330. /// 向上
  331. /// </summary>
  332. Up = 1,
  333. /// <summary>
  334. /// 向下
  335. /// </summary>
  336. Down = 2,
  337. /// <summary>
  338. /// 向左
  339. /// </summary>
  340. Left = 3,
  341. /// <summary>
  342. /// 向右
  343. /// </summary>
  344. Right = 4,
  345. /// <summary>
  346. /// 无效果
  347. /// </summary>
  348. None = 5,
  349. }
  350. }