Blame view

sources/RoboforkApp/Controls/ScheduleCanvas.cs 4.7 KB
d55d921a3   toan   2045: Draw Schedule
1
2
3
4
5
6
7
8
  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;
ec7e1c699   toan   2063 : Commit source
9
  using System.Windows.Media.Animation;
d55d921a3   toan   2045: Draw Schedule
10
11
12
13
14
15
  using System.Windows.Shapes;
  
  namespace RoboforkApp
  {
      public class ScheduleCanvas : Canvas
      {
4bed9f746   toan   2063 : Update source
16
17
          const double COOR_Y = 65;
          const double RADIUS_NODE = 25;
6312cbd86   doan   Task 2062
18
          private simulationRobo simulation;
4bed9f746   toan   2063 : Update source
19
20
21
22
23
          private Point startPoint;
          private Point endPoint;
          private bool _isGoal = false;
          private double _speed = 0.2; //Now _speed is fixed, but when update node
          private int index;
d55d921a3   toan   2045: Draw Schedule
24

4bed9f746   toan   2063 : Update source
25
26
27
28
29
30
31
          private List<ucNode> _lstNode;
  
          /// <summary>
          /// Create simulation
          /// </summary>
          /// <param name="lstNode"></param>
          public void CreateSimulation(List<ucNode> lstNode)
d55d921a3   toan   2045: Draw Schedule
32
          {
4bed9f746   toan   2063 : Update source
33
34
35
36
37
              //If node is less than 2 so return
              if (this.Children.Count < 2)
              {
                  return;
              }
6312cbd86   doan   Task 2062
38
              this.Children.Remove(simulation);
d55d921a3   toan   2045: Draw Schedule
39

4bed9f746   toan   2063 : Update source
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
              //Init data
              this._lstNode = lstNode;
              this.startPoint = new Point(Canvas.GetLeft(_lstNode[index]) + RADIUS_NODE, COOR_Y);
              this.endPoint = new Point(Canvas.GetLeft(_lstNode[index + 1]) + RADIUS_NODE, COOR_Y);
              this.index += 1;
              if (index == _lstNode.Count - 1)
              {
                  _isGoal = true;
              }
  
              // Start simulation
              RoboSimulation();
          }
  
          /// <summary>
          /// Create robo simulation on line
          /// </summary>
          private void RoboSimulation()
          {
6312cbd86   doan   Task 2062
59
60
61
              simulation = new simulationRobo();
              simulation.storyBoard = CreatPathAnimation(startPoint, endPoint, _speed); //pathAnimationStoryboard;
              this.Children.Add(simulation);
4bed9f746   toan   2063 : Update source
62
63
64
65
66
67
68
69
70
71
72
          }
  
          /// <summary>
          /// Get storyboard
          /// </summary>
          /// <param name="startPoint">Point start line</param>
          /// <param name="endPoit">Point end line</param>
          /// <param name="speed">speed on line</param>
          /// <returns>Storyboard</returns>
          private Storyboard CreatPathAnimation(Point startPoint, Point endPoit, double speed)
          {
ec7e1c699   toan   2063 : Commit source
73
74
              PathGeometry animationPath = new PathGeometry();
              PathFigure pFigure = new PathFigure();
4bed9f746   toan   2063 : Update source
75
              pFigure.StartPoint = startPoint; //new Point(50, 65);
ec7e1c699   toan   2063 : Commit source
76
              LineSegment lineSegment = new LineSegment();
4bed9f746   toan   2063 : Update source
77
              lineSegment.Point = endPoit; // new Point(800, 65);
ec7e1c699   toan   2063 : Commit source
78
79
80
81
82
83
84
              pFigure.Segments.Add(lineSegment);
              animationPath.Figures.Add(pFigure);
  
              // Freeze the PathGeometry for performance benefits.
              animationPath.Freeze();
  
              // Create a MatrixAnimationUsingPath to move the
4bed9f746   toan   2063 : Update source
85
              // simulation along the path by animating
ec7e1c699   toan   2063 : Commit source
86
87
88
              // its MatrixTransform.
              MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath();
              matrixAnimation.PathGeometry = animationPath;
4bed9f746   toan   2063 : Update source
89
              matrixAnimation.SpeedRatio = speed;
ec7e1c699   toan   2063 : Commit source
90
91
              matrixAnimation.AutoReverse = false;
              matrixAnimation.DoesRotateWithTangent = true;
4bed9f746   toan   2063 : Update source
92
              matrixAnimation.Completed += delegate { AnimationCompleted(this._isGoal); };
ec7e1c699   toan   2063 : Commit source
93
94
95
96
97
98
99
100
101
  
              // Set the animation to target the Matrix property
              // of the MatrixTransform named "ButtonMatrixTransform".
              Storyboard.SetTargetName(matrixAnimation, "fl");
              Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty));
  
              // Create a Storyboard to contain and apply the animation.
              Storyboard pathAnimationStoryboard = new Storyboard();
              pathAnimationStoryboard.Children.Add(matrixAnimation);
4bed9f746   toan   2063 : Update source
102
103
104
105
106
107
108
109
110
111
112
113
114
              return pathAnimationStoryboard;
          }
  
          /// <summary>
          /// Process when simulation is end line
          /// </summary>
          /// <param name="isGoal">check is node end</param>
          private void AnimationCompleted(bool isGoal)
          {
              // If not end node
              if (!isGoal)
              {
                  this.index += 1;
6312cbd86   doan   Task 2062
115
                  this.Children.Remove(simulation);
4bed9f746   toan   2063 : Update source
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                  this.startPoint = endPoint;
                  this.endPoint = new Point(Canvas.GetLeft(_lstNode[index]) + RADIUS_NODE, COOR_Y);
                  
                  if (this.index == this._lstNode.Count - 1)
                  {
                      this._isGoal = true;
                  }
                  this._speed += 0.2;
                  RoboSimulation();
                  return;
              }
  
              // Reset data when finish
              this.index = 0;
              this._speed = 0.2;
              this._isGoal = false;
d55d921a3   toan   2045: Draw Schedule
132
133
134
          }
      }
  }