/// <summary>
        /// Create a path object based on a list of vertices - used when creating geometry
        /// </summary>
        public static UIShapes.Shape CreatePathFromVertices(Body body, PhysicsSprite element, WinUI.Color colorFill)
        {
            List<Fixture> vertices = body.FixtureList;

            // is this an ellipse?
            if (vertices[0].Shape is CircleShape)
            {
                CircleShape cs = vertices[0].Shape as CircleShape;
                UIShapes.Ellipse el = new UIShapes.Ellipse();
                Vector2 screenSize = PhysicsCanvas.BoundaryHelper.WorldSizeToScreenSize(new Vector2(cs.Radius * 2, cs.Radius * 2));

                el.Width = screenSize.X;
                el.Height = screenSize.Y;

                el.Stroke = new SysWinMedia.SolidColorBrush(new WinUI.Color() { A = 255, R = 76, G = 108, B = 76 });
                el.StrokeThickness = 4;
                el.StrokeStartLineCap = SysWinMedia.PenLineCap.Round;
                el.Fill = new SysWinMedia.SolidColorBrush(colorFill);
                return el;
            }
            else
            {
                UIShapes.Path path = new UIShapes.Path();
                path.Stroke = new SysWinMedia.SolidColorBrush(new WinUI.Color() { A = 255, R = 76, G = 108, B = 76 });
                path.StrokeThickness = 4;
                path.StrokeStartLineCap = SysWinMedia.PenLineCap.Round;
                path.Fill = new SysWinMedia.SolidColorBrush(colorFill);


                SysWinMedia.PathGeometry pathGeom = new SysWinMedia.PathGeometry();
                SysWinMedia.PathFigureCollection figures = new SysWinMedia.PathFigureCollection();
                pathGeom.Figures = figures;

                double halfWidth = element.Width / 2d;
                double halfHeight = element.Height / 2d;
                if (halfWidth == 0) halfWidth = element.ActualWidth / 2d;
                if (halfHeight == 0) halfHeight = element.ActualHeight / 2d;

                foreach (Fixture f in vertices)
                {

                    if (f.Shape is PolygonShape)
                    {
                        SysWinMedia.PathFigure figure = new SysWinMedia.PathFigure();
                        PolygonShape p = f.Shape as PolygonShape;

                        // first vertex, create the startpoint
                        Vector2 ptStart = new Vector2((float)p.Vertices[0].X, (float)p.Vertices[0].Y);
                        ptStart = PhysicsCanvas.BoundaryHelper.WorldSizeToScreenSize(ptStart);
                        ptStart.X += (float)(halfWidth);
                        ptStart.Y += (float)(halfHeight);

                        figure.StartPoint = new SysWin.Point(ptStart.X, ptStart.Y);
                        figure.Segments = new SysWinMedia.PathSegmentCollection();
                        pathGeom.Figures.Add(figure);

                        for (int i = 1; i < p.Vertices.Count; i++)
                        {
                            SysWinMedia.LineSegment line = new SysWinMedia.LineSegment();
                            Vector2 pt = new Vector2((float)p.Vertices[i].X, (float)p.Vertices[i].Y);
                            pt = PhysicsCanvas.BoundaryHelper.WorldSizeToScreenSize(pt);
                            pt.X += (float)(halfWidth);
                            pt.Y += (float)(halfHeight);
                            line.Point = new SysWin.Point(pt.X, pt.Y); ;

                            figure.Segments.Add(line);
                        }

                    }

                }

                path.Data = pathGeom;
                return path;


            }
        }
Exemplo n.º 2
0
 public PathGeometry()
 {
     // This is done here to ensure that the Parent is set properly on the new PathFigureCollection.
     Figures = new PathFigureCollection();
 }