private bool SearchRailAndCar()
        {
            Path path = new Path();

            if (myCanvas.Children.Count == 0)
            {
                MessageBox.Show("Please add Rail and Car !!!!");
                return(false);
            }
            foreach (UIElement elemnt in myCanvas.Children)
            {
                if (elemnt is Path)
                {
                    path            = elemnt as Path;
                    carPathGeometry = path.Data as PathGeometry;
                }
                if (elemnt is CommonCarControl)
                {
                    myCar = elemnt as CommonCarControl;
                }
            }
            return(true);
        }
        private void CommondTest_Click(object sender, RoutedEventArgs e)
        {
            /*
             * IList<PathSegment> segments = new List<PathSegment>();
             * PathGeometry pathGeometry = new PathGeometry();
             * LineSegment lineSegment = new LineSegment(new Point(140,100),true);
             * ArcSegment arcSegment = new ArcSegment(new Point(100,140),new Size(40,40),45,false,SweepDirection.Counterclockwise,true);
             * LineSegment lineSegment1 = new LineSegment(new Point(100,600),true);
             * segments.Add(lineSegment);
             * segments.Add(arcSegment);
             * segments.Add(lineSegment1);
             * PathFigure pathFigure = new PathFigure(new Point(800,100),segments,true);
             * pathGeometry.Figures.Add(pathFigure);
             * Path path = new Path();
             * path.Stroke = Brushes.Black;
             * path.StrokeThickness = 2;
             * path.Data = pathGeometry;
             * myCanvas.Children.Add(path);
             */
            // Button button = new Button();
            // button.Visibility = Visibility.Visible;
            // button.Height = 20;
            // button.Width = 100;
            // myCanvas.Children.Add(button);

            PathGeometry pathGeometry = new PathGeometry();
            //CommonCarControl myCar = null;
            Path path = new Path();

            if (myCanvas.Children.Count == 0)
            {
                MessageBox.Show("Canvas is not elemnt,Please add rail and car!!!!");
                return;
            }
            foreach (UIElement pg in myCanvas.Children)
            {
                if (pg is Path)
                {
                    path         = pg as Path;
                    pathGeometry = path.Data as PathGeometry;
                }
                if (pg is CommonCarControl)
                {
                    myCar = pg as CommonCarControl;
                }
            }
            //Canvas.GetLeft(myCar);
            TranslateTransform translate = new TranslateTransform();

            myCar.RenderTransform = translate;
            //this.RegisterName("XYTranslate",translate);
            DoubleAnimationUsingPath xAnimation = new DoubleAnimationUsingPath();

            xAnimation.PathGeometry = pathGeometry;
            xAnimation.Duration     = TimeSpan.FromSeconds(40);
            xAnimation.Source       = PathAnimationSource.X;
            Storyboard.SetTarget(xAnimation, myCar);
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
            DoubleAnimationUsingPath yAnimation = new DoubleAnimationUsingPath();

            yAnimation.PathGeometry = pathGeometry;
            yAnimation.Duration     = TimeSpan.FromSeconds(40);
            yAnimation.Source       = PathAnimationSource.Y;
            Storyboard.SetTarget(yAnimation, myCar);
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("RenderTransform.(TranslateTransform.Y)"));
            //Storyboard animationExecute = new Storyboard();
            animationExecute.RepeatBehavior = RepeatBehavior.Forever;
            animationExecute.Children.Add(xAnimation);
            animationExecute.Children.Add(yAnimation);
            animationExecute.Begin(myCar, true);
            // myCar.TopDistance = Canvas.GetTop(myCar);
            //myCar.LeftDistance = Canvas.GetLeft(myCar);
        }
Пример #3
0
        private void ExecuteCommand_Click(object sender, RoutedEventArgs e)
        {
            int          originStationIndex, targetStationIndex;
            double       originStationX, originStationY, targetStationX, targetStationY;
            Point        st, tg;
            PathGeometry pathGeometry = new PathGeometry();

            if (OriginStationComBox.Text == string.Empty)
            {
                return;
            }
            else
            {
                Int32.TryParse(OriginStationComBox.Text.Substring(7).Trim(), out originStationIndex);
                originStationX = temp[originStationIndex].Margin.Left + 30;
                originStationY = temp[originStationIndex].Margin.Top - 40;
            }
            if (TargetStationComBox.Text == string.Empty)
            {
                return;
            }
            else
            {
                Int32.TryParse(TargetStationComBox.Text.Substring(7), out targetStationIndex);
                targetStationX = temp[targetStationIndex].Margin.Left + 30;
                targetStationY = temp[targetStationIndex].Margin.Top - 40;
                temp[targetStationIndex].Background = Brushes.Purple;
            }
            st           = new Point(originStationX, originStationY);
            tg           = new Point(targetStationX, targetStationY);
            pathGeometry = RoadSelect(st, tg);//call RoadSelect Function.
            if (MainWindow.myCanvasStore.Children.Count == 0)
            {
                MessageBox.Show("Canvas is not elemnt");
                return;
            }
            foreach (UIElement pg in MainWindow.myCanvasStore.Children)
            {
                if (pg is CommonCarControl)
                {
                    myCar = pg as CommonCarControl;
                }
            }
            TranslateTransform translate = new TranslateTransform();

            myCar.RenderTransform = translate;
            DoubleAnimationUsingPath xAnimation = new DoubleAnimationUsingPath();

            xAnimation.PathGeometry = pathGeometry;
            xAnimation.Duration     = TimeSpan.FromSeconds(20);
            xAnimation.Source       = PathAnimationSource.X;
            Storyboard.SetTarget(xAnimation, myCar);
            Storyboard.SetTargetProperty(xAnimation, new PropertyPath("RenderTransform.(TranslateTransform.X)"));
            DoubleAnimationUsingPath yAnimation = new DoubleAnimationUsingPath();

            yAnimation.PathGeometry = pathGeometry;
            yAnimation.Duration     = TimeSpan.FromSeconds(20);
            yAnimation.Source       = PathAnimationSource.Y;
            Storyboard.SetTarget(yAnimation, myCar);
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("RenderTransform.(TranslateTransform.Y)"));
            animationExecute.Children.Add(xAnimation);
            animationExecute.Children.Add(yAnimation);
            animationExecute.Begin(myCar, true);
        }