Ejemplo n.º 1
0
        // 播放演示
        private void PlayDemo(object obj)
        {
            if (!GlobalData.IsActive)
            {
                SystemSounds.Beep.Play();
                MsgBox.Show("只有正式版软件才能使用此功能\n请前往“设置”页面激活软件");
                return;
            }

            IsPlayingDemo    = true;
            CurrentNodeIndex = -1;

            // 清空所有线条
            Record.Segments.Clear();

            // 清空车辆运行时信息
            CarRuntimeInfos.Clear();

            // 演示时长 s
            double totalDemoTime = GlobalData.DemoDuration;

            // 获取配送总时间 s
            double totalTime = Record.GetTotalTime() * 60;

            // 计算时间缩放比例
            double rate = totalDemoTime / totalTime;

            // 计算节点停留时间 s
            double nodeStayTime = Record.NodeStayTime * rate * 60;

            // 获取配送时间最长的车辆编号
            int index = Record.GetSlowestCarIndex();

            //存储动画开始的线条
            List <int> startIndex = new List <int>();

            //设置动画
            for (int iCar = 0; iCar < Record.Paths.Count; ++iCar)
            {
                int iCarTemp = iCar;

                // 跳过未参与配送车辆
                if (Record.Paths[iCar].Count == 0)
                {
                    continue;
                }

                // 保存起始线条
                int iStart = Record.Segments.Count;
                startIndex.Add(iStart);

                // 提前添加所有线条
                Brush brush = Util.RandomColorBrush();
                for (int i = 0; i < Record.Paths[iCar].Count + 1; ++i)
                {
                    Record.Segments.Add(new Segment {
                        Stroke = brush
                    });
                }

                // 初始化车辆运行时信息
                CarRuntimeInfos.Add(new CarRuntimeInfo {
                    ID = iCar, LineBrush = brush, IsFinished = false, CompletedPercent = 0
                });

                // 遍历当前车辆的所有配送点
                Node last = Record.Nodes[0];
                for (int i = 0; i < Record.Paths[iCar].Count; ++i)
                {
                    int iNode = Record.Paths[iCar][i];

                    // 设置动画
                    Record.Segments[iStart + i].XAnimationFrom = last.X;
                    Record.Segments[iStart + i].YAnimationFrom = last.Y;
                    Record.Segments[iStart + i].XAnimationTo   = Record.Nodes[iNode].X;
                    Record.Segments[iStart + i].YAnimationTo   = Record.Nodes[iNode].Y;
                    Record.Segments[iStart + i].Duration       = last.Distance(Record.Nodes[iNode]) / Record.CarSpeed * rate * 3600;
                    if (i != 0)
                    {
                        Record.Segments[iStart + i].Delay = nodeStayTime;
                    }

                    int t = i;
                    Record.Segments[iStart + i].AnimationCompleted = delegate
                    {
                        // 启动下一线条动画
                        Record.Segments[iStart + t + 1].BeginAnimation();

                        if (GlobalData.ShowCarRuntimeInfoDuringDemo)
                        {
                            // 更新车辆完成百分比
                            new Thread(delegate()
                            {
                                CarRuntimeInfos.AddCompletedPercent(iCarTemp, 1.0 / Record.Paths[iCarTemp].Count);
                            }).Start();
                        }
                    };

                    last = Record.Nodes[iNode];
                }

                // 设置最后一段线条的动画
                Record.Segments[Record.Segments.Count - 1].XAnimationFrom     = last.X;
                Record.Segments[Record.Segments.Count - 1].YAnimationFrom     = last.Y;
                Record.Segments[Record.Segments.Count - 1].XAnimationTo       = Record.Nodes[0].X;
                Record.Segments[Record.Segments.Count - 1].YAnimationTo       = Record.Nodes[0].Y;
                Record.Segments[Record.Segments.Count - 1].Duration           = last.Distance(Record.Nodes[0]) / Record.CarSpeed * rate * 3600;
                Record.Segments[Record.Segments.Count - 1].Delay              = nodeStayTime;
                Record.Segments[Record.Segments.Count - 1].AnimationCompleted = delegate
                {
                    if (GlobalData.ShowCarRuntimeInfoDuringDemo)
                    {
                        // 更新车辆运行时信息
                        new Thread(delegate()
                        {
                            CarRuntimeInfos.SetFinishedState(iCarTemp, true);
                        }).Start();
                    }

                    // 如果是最后一辆车,则演示结束
                    if (iCarTemp == index)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(delegate
                        {
                            if (GlobalData.PopupAfterDemo)
                            {
                                MsgBox.Show("演示结束!");
                            }
                            IsPlayingDemo = false;
                            CommandManager.InvalidateRequerySuggested();
                        }));
                    }
                };
            }

            if (GlobalData.ShowProgressDuringDemo)
            {
                //启动时间动画
                DoubleAnimation timeAnim = new DoubleAnimation();
                timeAnim.From     = 0;
                timeAnim.To       = totalTime / 60;
                timeAnim.Duration = new Duration(TimeSpan.FromSeconds(totalDemoTime));
                CurrentTime.BeginAnimation(AnimatableValue.ValueProperty, timeAnim);

                // 启动演示进度动画
                DoubleAnimation progressAnim = new DoubleAnimation();
                progressAnim.From     = 0;
                progressAnim.To       = 1;
                progressAnim.Duration = new Duration(TimeSpan.FromSeconds(totalDemoTime));
                CurrentDemoProgress.BeginAnimation(AnimatableValue.ValueProperty, progressAnim);
            }

            // 启动线条动画
            for (int i = 0; i < startIndex.Count; ++i)
            {
                Record.Segments[startIndex[i]].BeginAnimation();
            }
        }