Beispiel #1
0
        private void Add_Station_Ellipse_On_Field(object sender, RoutedEventArgs e)
        {
            if (on)
            {
                if (fieldCanvas.Children.Count > 0 && fieldCanvas.Children[fieldCanvas.Children.Count - 1] is Menu)
                {
                    //clear right click menu in field
                    fieldCanvas.Children.RemoveAt(fieldCanvas.Children.Count - 1);
                }

                if (SubwayLinesLBox.SelectedIndex != -1)
                {
                    double x = Mouse.GetPosition(fieldCanvas).X;
                    double y = Mouse.GetPosition(fieldCanvas).Y;

                    //if too near for window borders
                    if (x < 20 || y < 20 ||
                        x > (fieldCanvas.Width - 20) || y > (fieldCanvas.Height - 20))
                    {
                        return;
                    }

                    Point point = new Point(x, y);
                    foreach (Railway rl in subway.GetRailways)
                    {
                        foreach (Station st in rl.GetStations)
                        {
                            //if too near for 2 any station
                            if (Math.Sqrt((st.Coordinate.X - point.X) * (st.Coordinate.X - point.X) + (st.Coordinate.Y - point.Y) * (st.Coordinate.Y - point.Y)) < 40)
                            {
                                TooNearStationPP.IsOpen = true;
                                return;
                            }
                        }
                    }

                    foreach (Railway rl in subway.GetRailways)
                    {
                        //if too near for station and railway
                        if (rl.GetStations.Count > 1)
                        {
                            for (int i = 0; i < rl.GetStations.Count - 1; i++)
                            {
                                if (IntersectionCircleSegment
                                        (rl.GetStations.ElementAt(i).Coordinate.X, rl.GetStations.ElementAt(i).Coordinate.Y,
                                        rl.GetStations.ElementAt(i + 1).Coordinate.X, rl.GetStations.ElementAt(i + 1).Coordinate.Y,
                                        x, y, 30))
                                {
                                    TooNearPathPP.IsOpen = true;
                                    return;
                                }
                            }
                        }
                    }
                    foreach (Station st in subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations)
                    {
                        //if too near for 2 station at 1 railway
                        if (Math.Sqrt((st.Coordinate.X - point.X) * (st.Coordinate.X - point.X) +
                                      (st.Coordinate.Y - point.Y) * (st.Coordinate.Y - point.Y)) < 120)
                        {
                            TooNearStationPP.IsOpen = true;
                            return;
                        }
                    }

                    foreach (Railway rl in subway.GetRailways)
                    {
                        int count = (subway.GetRailways.IndexOf(rl) == SubwayLinesLBox.SelectedIndex) ? rl.GetStations.Count - 1 : rl.GetStations.Count;
                        for (int i = 0; i < count; i++)
                        {
                            //if the railway crosses stations
                            if (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count > 0)
                            {
                                if (IntersectionCircleSegment
                                        (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                        ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).Coordinate.X,
                                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                        ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).Coordinate.Y,
                                        point.X, point.Y, rl.GetStations.ElementAt(i).Coordinate.X, rl.GetStations.ElementAt(i).Coordinate.Y, 30))
                                {
                                    CrossesStationPP.IsOpen = true;
                                    return;
                                }
                            }
                        }
                    }
                    AddStationWindow win = new AddStationWindow(this);
                    win.Owner = this;
                    win.ShowDialog();

                    if (DialogResultOk)
                    {
                        DialogResultOk = false;
                        //Paint railway and station
                        if (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count > 1)
                        {
                            System.Windows.Shapes.Path linePath = new System.Windows.Shapes.Path();
                            linePath.Stroke              = SwitchFillBrushes(SubwayLinesLBox.SelectedIndex);
                            linePath.StrokeThickness     = 4;
                            linePath.HorizontalAlignment = HorizontalAlignment.Center;
                            linePath.VerticalAlignment   = VerticalAlignment.Center;
                            List <Point> pointsList = new List <Point>();
                            pointsList.Add(point);
                            pointsList.Add(point);
                            pointsList.Add(point);
                            PathFigure lineFigure = new PathFigure();
                            Point      ps         = subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                                    ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 2).Coordinate;
                            lineFigure.StartPoint = ps;
                            lineFigure.Segments.Add(new PolyBezierSegment(pointsList, true));
                            PathGeometry lineGeometry = new PathGeometry();
                            lineGeometry.Figures.Add(lineFigure);
                            linePath.Data = lineGeometry;
                            linePath.MouseLeftButtonDown += Line_Mouse_Left_Down;
                            linePath.MouseLeftButtonUp   += Line_Mouse_Left_Up;
                            Canvas.SetZIndex(linePath, -1);
                            subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 2).startLinePath = linePath;
                            fieldCanvas.Children.Add(linePath);

                            linePath   = new System.Windows.Shapes.Path();
                            lineFigure = new PathFigure();
                            pointsList = new List <Point>();
                            pointsList.Add(ps);
                            pointsList.Add(ps);
                            pointsList.Add(ps);
                            lineFigure.StartPoint = point;
                            lineFigure.Segments.Add(new PolyBezierSegment(pointsList, true));
                            lineGeometry = new PathGeometry();
                            lineGeometry.Figures.Add(lineFigure);
                            linePath.Data = lineGeometry;
                            subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).endLinePath = linePath;
                        }

                        System.Windows.Shapes.Path ellipsePath = new System.Windows.Shapes.Path();
                        ellipsePath.Stroke              = Brushes.LightSlateGray;
                        ellipsePath.Fill                = SwitchFillBrushes(SubwayLinesLBox.SelectedIndex);
                        ellipsePath.StrokeThickness     = 2;
                        ellipsePath.HorizontalAlignment = HorizontalAlignment.Center;
                        ellipsePath.VerticalAlignment   = VerticalAlignment.Center;
                        EllipseGeometry myEllipseGeometry = new EllipseGeometry();
                        myEllipseGeometry.Center          = new Point(x, y);
                        myEllipseGeometry.RadiusX         = 20;
                        myEllipseGeometry.RadiusY         = 20;
                        ellipsePath.Data                  = myEllipseGeometry;
                        ellipsePath.MouseLeftButtonDown  += Ellipse_Mouse_Left_Down;
                        ellipsePath.MouseLeftButtonUp    += Ellipse_Mouse_Left_Up;
                        ellipsePath.MouseRightButtonDown += Ellipse_Mouse_Right_Down;
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                        ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).ellipsePath = ellipsePath;

                        ContextMenu menu   = new ContextMenu();
                        MenuItem    delete = new MenuItem();
                        delete.Header = "Delete station";
                        menu.Items.Add(delete);
                        delete.Click += Delete_Station_Click;
                        MenuItem board = new MenuItem();
                        board.Header = "Open board";
                        menu.Items.Add(board);
                        board.Click            += Open_Board_Click;
                        ellipsePath.ContextMenu = menu;

                        fieldCanvas.Children.Add(ellipsePath);
                        TextBlock stationName = new TextBlock();
                        stationName.FontFamily = new FontFamily("Impact");
                        stationName.FontWeight = FontWeights.Light;
                        stationName.Text       = subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                                 ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).stationName.Text;
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                        ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).stationName = stationName;
                        stationName.Margin = new Thickness(x - 35, y - 35, x, y);
                        fieldCanvas.Children.Add(stationName);
                        Canvas.SetZIndex(stationName, 100);
                        //Added  station coordinates
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                        ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).
                        Coordinate = new Point(x, y);
                    }
                }
            }
        }
Beispiel #2
0
        private void Add_Station_Ellipse_On_Field(object sender, RoutedEventArgs e)
        {
            if (on)
            {
                if (fieldCanvas.Children.Count > 0 && fieldCanvas.Children[fieldCanvas.Children.Count - 1] is Menu)
                {
                    //clear right click menu in field
                    fieldCanvas.Children.RemoveAt(fieldCanvas.Children.Count - 1);
                }

                if (SubwayLinesLBox.SelectedIndex != -1)
                {
                    double x = Mouse.GetPosition(fieldCanvas).X;
                    double y = Mouse.GetPosition(fieldCanvas).Y;

                    //if too near for window borders
                    if (x < 20 || y < 20 ||
                        x > (fieldCanvas.Width - 20) || y > (fieldCanvas.Height - 20))
                    { return; }

                    Point point = new Point(x, y);
                    foreach (Railway rl in subway.GetRailways)
                    {
                        foreach (Station st in rl.GetStations)
                        {
                            //if too near for 2 any station
                            if (Math.Sqrt((st.Coordinate.X - point.X) * (st.Coordinate.X - point.X) + (st.Coordinate.Y - point.Y) * (st.Coordinate.Y - point.Y)) < 40)
                            {
                                TooNearStationPP.IsOpen = true;
                                return;
                            }
                        }
                    }

                    foreach (Railway rl in subway.GetRailways)
                    {
                        //if too near for station and railway
                        if (rl.GetStations.Count > 1)
                        {
                            for (int i = 0; i < rl.GetStations.Count - 1; i++)
                            {
                                if (IntersectionCircleSegment
                                    (rl.GetStations.ElementAt(i).Coordinate.X, rl.GetStations.ElementAt(i).Coordinate.Y,
                                     rl.GetStations.ElementAt(i + 1).Coordinate.X, rl.GetStations.ElementAt(i + 1).Coordinate.Y,
                                     x, y, 30))
                                {
                                    TooNearPathPP.IsOpen = true;
                                    return;
                                }
                            }
                        }
                    }
                    foreach (Station st in subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations)
                    {
                        //if too near for 2 station at 1 railway
                        if (Math.Sqrt((st.Coordinate.X - point.X) * (st.Coordinate.X - point.X) +
                            (st.Coordinate.Y - point.Y) * (st.Coordinate.Y - point.Y)) < 120)
                        {
                            TooNearStationPP.IsOpen = true;
                            return;
                        }
                    }

                    foreach (Railway rl in subway.GetRailways)
                    {
                        int count = (subway.GetRailways.IndexOf(rl) == SubwayLinesLBox.SelectedIndex) ? rl.GetStations.Count - 1 : rl.GetStations.Count;
                        for (int i = 0; i < count; i++)
                        {
                            //if the railway crosses stations
                            if (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count > 0)
                            {
                                if (IntersectionCircleSegment
                                           (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                     ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).Coordinate.X,
                                     subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                                     ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).Coordinate.Y,
                                           point.X, point.Y, rl.GetStations.ElementAt(i).Coordinate.X, rl.GetStations.ElementAt(i).Coordinate.Y, 30))
                                {
                                    CrossesStationPP.IsOpen = true;
                                    return;
                                }
                            }
                        }
                    }
                    AddStationWindow win = new AddStationWindow(this);
                    win.Owner = this;
                    win.ShowDialog();

                    if (DialogResultOk)
                    {
                        DialogResultOk = false;
                        //Paint railway and station
                        if (subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count > 1)
                        {
                            System.Windows.Shapes.Path linePath = new System.Windows.Shapes.Path();
                            linePath.Stroke = SwitchFillBrushes(SubwayLinesLBox.SelectedIndex);
                            linePath.StrokeThickness = 4;
                            linePath.HorizontalAlignment = HorizontalAlignment.Center;
                            linePath.VerticalAlignment = VerticalAlignment.Center;
                            List<Point> pointsList = new List<Point>();
                            pointsList.Add(point);
                            pointsList.Add(point);
                            pointsList.Add(point);
                            PathFigure lineFigure = new PathFigure();
                            Point ps = subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                              ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 2).Coordinate;
                            lineFigure.StartPoint = ps;
                            lineFigure.Segments.Add(new PolyBezierSegment(pointsList, true));
                            PathGeometry lineGeometry = new PathGeometry();
                            lineGeometry.Figures.Add(lineFigure);
                            linePath.Data = lineGeometry;
                            linePath.MouseLeftButtonDown += Line_Mouse_Left_Down;
                            linePath.MouseLeftButtonUp += Line_Mouse_Left_Up;
                            Canvas.SetZIndex(linePath, -1);
                            subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                           ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 2).startLinePath = linePath;
                            fieldCanvas.Children.Add(linePath);

                            linePath = new System.Windows.Shapes.Path();
                            lineFigure = new PathFigure();
                            pointsList = new List<Point>();
                            pointsList.Add(ps);
                            pointsList.Add(ps);
                            pointsList.Add(ps);
                            lineFigure.StartPoint = point;
                            lineFigure.Segments.Add(new PolyBezierSegment(pointsList, true));
                            lineGeometry = new PathGeometry();
                            lineGeometry.Figures.Add(lineFigure);
                            linePath.Data = lineGeometry;
                            subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                           ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).endLinePath = linePath;
                        }

                        System.Windows.Shapes.Path ellipsePath = new System.Windows.Shapes.Path();
                        ellipsePath.Stroke = Brushes.LightSlateGray;
                        ellipsePath.Fill = SwitchFillBrushes(SubwayLinesLBox.SelectedIndex);
                        ellipsePath.StrokeThickness = 2;
                        ellipsePath.HorizontalAlignment = HorizontalAlignment.Center;
                        ellipsePath.VerticalAlignment = VerticalAlignment.Center;
                        EllipseGeometry myEllipseGeometry = new EllipseGeometry();
                        myEllipseGeometry.Center = new Point(x, y);
                        myEllipseGeometry.RadiusX = 20;
                        myEllipseGeometry.RadiusY = 20;
                        ellipsePath.Data = myEllipseGeometry;
                        ellipsePath.MouseLeftButtonDown += Ellipse_Mouse_Left_Down;
                        ellipsePath.MouseLeftButtonUp += Ellipse_Mouse_Left_Up;
                        ellipsePath.MouseRightButtonDown += Ellipse_Mouse_Right_Down;
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).ellipsePath = ellipsePath;

                        ContextMenu menu = new ContextMenu();
                        MenuItem delete = new MenuItem();
                        delete.Header = "Delete station";
                        menu.Items.Add(delete);
                        delete.Click += Delete_Station_Click;
                        MenuItem board = new MenuItem();
                        board.Header = "Open board";
                        menu.Items.Add(board);
                        board.Click += Open_Board_Click;
                        ellipsePath.ContextMenu = menu;

                        fieldCanvas.Children.Add(ellipsePath);
                        TextBlock stationName = new TextBlock();
                        stationName.FontFamily = new FontFamily("Impact");
                        stationName.FontWeight = FontWeights.Light;
                        stationName.Text = subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).stationName.Text;
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).stationName = stationName;
                        stationName.Margin = new Thickness(x - 35, y - 35, x, y);
                        fieldCanvas.Children.Add(stationName);
                        Canvas.SetZIndex(stationName, 100);
                        //Added  station coordinates
                        subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.
                            ElementAt(subway.GetRailways.ElementAt(SubwayLinesLBox.SelectedIndex).GetStations.Count - 1).
                            Coordinate = new Point(x, y);
                    }
                }
            }
        }