PipeLineKnot.xaml.cs 16 KB

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