Blame view
sources/RoboforkApp/Controls/ScheduleCanvas.cs
5.13 KB
|
1fe3e8a87
|
1 2 |
using RoboforkApp.DataModel; using System; |
|
d55d921a3
|
3 4 5 6 7 8 9 |
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
|
10 |
using System.Windows.Media.Animation; |
|
d55d921a3
|
11 12 13 14 15 16 |
using System.Windows.Shapes;
namespace RoboforkApp
{
public class ScheduleCanvas : Canvas
{
|
|
4bed9f746
|
17 18 |
const double COOR_Y = 65;
const double RADIUS_NODE = 25;
|
|
1fe3e8a87
|
19 20 |
public simulationRobo simulation;
private VehicleModelList vehicleModelList;
|
|
4bed9f746
|
21 22 23 |
private Point startPoint;
private Point endPoint;
private bool _isGoal = false;
|
|
1fe3e8a87
|
24 |
private double _speed; |
|
4bed9f746
|
25 |
private int index; |
|
d55d921a3
|
26 |
|
|
4bed9f746
|
27 28 29 30 31 |
private List<ucNode> _lstNode;
/// <summary>
/// Create simulation
/// </summary>
|
|
1fe3e8a87
|
32 33 34 35 |
/// <param name="lstNode">List node on schedule</param>
/// <param name="vehicleModel">Vehicle model on map</param>
/// <param name="vehicleIndex">Current index of vehicle</param>
public void CreateSimulation(List<ucNode> lstNode, VehicleModel vehicleModel, int vehicleIndex)
|
|
d55d921a3
|
36 |
{
|
|
4bed9f746
|
37 38 39 40 41 |
//If node is less than 2 so return
if (this.Children.Count < 2)
{
return;
}
|
|
6312cbd86
|
42 |
this.Children.Remove(simulation); |
|
d55d921a3
|
43 |
|
|
4bed9f746
|
44 45 |
//Init data
this._lstNode = lstNode;
|
|
1fe3e8a87
|
46 |
this.vehicleModelList = vehicleModel.VehicleModelList[vehicleIndex]; |
|
4bed9f746
|
47 48 |
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);
|
|
1fe3e8a87
|
49 |
this._speed = vehicleModelList.pointMapList[index].speed_Schedule; |
|
4bed9f746
|
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
this.index += 1;
if (index == _lstNode.Count - 1)
{
_isGoal = true;
}
// Start simulation
RoboSimulation();
}
/// <summary>
/// Create robo simulation on line
/// </summary>
private void RoboSimulation()
{
|
|
6312cbd86
|
65 66 67 |
simulation = new simulationRobo();
simulation.storyBoard = CreatPathAnimation(startPoint, endPoint, _speed); //pathAnimationStoryboard;
this.Children.Add(simulation);
|
|
4bed9f746
|
68 69 70 71 72 73 74 75 76 77 78 |
}
/// <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
|
79 80 |
PathGeometry animationPath = new PathGeometry();
PathFigure pFigure = new PathFigure();
|
|
4bed9f746
|
81 |
pFigure.StartPoint = startPoint; //new Point(50, 65); |
|
ec7e1c699
|
82 |
LineSegment lineSegment = new LineSegment(); |
|
4bed9f746
|
83 |
lineSegment.Point = endPoit; // new Point(800, 65); |
|
ec7e1c699
|
84 85 86 87 88 89 90 |
pFigure.Segments.Add(lineSegment);
animationPath.Figures.Add(pFigure);
// Freeze the PathGeometry for performance benefits.
animationPath.Freeze();
// Create a MatrixAnimationUsingPath to move the
|
|
4bed9f746
|
91 |
// simulation along the path by animating |
|
ec7e1c699
|
92 93 94 |
// its MatrixTransform.
MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath();
matrixAnimation.PathGeometry = animationPath;
|
|
4bed9f746
|
95 |
matrixAnimation.SpeedRatio = speed; |
|
ec7e1c699
|
96 97 |
matrixAnimation.AutoReverse = false;
matrixAnimation.DoesRotateWithTangent = true;
|
|
4bed9f746
|
98 |
matrixAnimation.Completed += delegate { AnimationCompleted(this._isGoal); };
|
|
ec7e1c699
|
99 100 101 102 103 104 105 106 107 |
// 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
|
108 109 110 111 112 113 114 115 116 117 118 119 |
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)
{
|
|
1fe3e8a87
|
120 |
this._speed = vehicleModelList.pointMapList[index].speed_Schedule; |
|
4bed9f746
|
121 |
this.index += 1; |
|
6312cbd86
|
122 |
this.Children.Remove(simulation); |
|
4bed9f746
|
123 124 125 126 127 128 129 |
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;
}
|
|
4bed9f746
|
130 131 132 133 134 135 |
RoboSimulation();
return;
}
// Reset data when finish
this.index = 0;
|
|
1fe3e8a87
|
136 |
this._speed = 0; |
|
4bed9f746
|
137 |
this._isGoal = false; |
|
d55d921a3
|
138 139 140 |
}
}
}
|