コード例 #1
0
        protected override void Render()
        {
            var root              = Container.GetVisual();
            var compositor        = Window.Current.Compositor;
            var canvasPathBuilder = new CanvasPathBuilder(new CanvasDevice());

            if (IsStrokeRounded)
            {
                canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
            }
            else
            {
                canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.None);
            }

            // Figure
            if (IsPie)
            {
                StartPointX = CenterPointX;
                StartPointY = CenterPointY;
            }
            else
            {
                StartPointX = Radius * Math.Cos(StartAngle * Degrees2Radians) + CenterPointX;
                StartPointY = Radius * Math.Sin(StartAngle * Degrees2Radians) + CenterPointY;
            }

            canvasPathBuilder.BeginFigure(new Vector2((float)StartPointX, (float)StartPointY));

            canvasPathBuilder.AddArc(
                new Vector2((float)CenterPointX, (float)CenterPointY),
                (float)Radius,
                (float)Radius,
                (float)(StartAngle * Degrees2Radians),
                (float)(SweepAngle * Degrees2Radians));

            canvasPathBuilder.EndFigure(IsClosed || IsPie ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open);

            // Path
            var canvasGeometry  = CanvasGeometry.CreatePath(canvasPathBuilder);
            var compositionPath = new CompositionPath(canvasGeometry);
            var pathGeometry    = compositor.CreatePathGeometry();

            pathGeometry.Path = compositionPath;
            var spriteShape = compositor.CreateSpriteShape(pathGeometry);

            spriteShape.FillBrush       = compositor.CreateColorBrush(Fill);
            spriteShape.StrokeThickness = (float)StrokeThickness;
            spriteShape.StrokeBrush     = compositor.CreateColorBrush(Stroke);
            spriteShape.StrokeStartCap  = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat;
            spriteShape.StrokeEndCap    = IsStrokeRounded ? CompositionStrokeCap.Round : CompositionStrokeCap.Flat;

            // Visual
            var shapeVisual = compositor.CreateShapeVisual();

            shapeVisual.Size = new Vector2((float)Container.ActualWidth, (float)Container.ActualHeight);
            shapeVisual.Shapes.Add(spriteShape);
            root.Children.RemoveAll();
            root.Children.InsertAtTop(shapeVisual);
        }
コード例 #2
0
        protected CanvasPathBuilder GetCanvasPathBuilder()
        {
            var canvasPathBuilder = new CanvasPathBuilder(CanvasDevice);

            if (IsStrokeRounded)
            {
                canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
            }
            else
            {
                canvasPathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.None);
            }

            return(canvasPathBuilder);
        }
コード例 #3
0
ファイル: PathReceiver.cs プロジェクト: markmnl/Win2D
        public void VerifySendPathTo()
        {
            //
            // This calls each of the path functions, once, and verfies that it works.
            // Sink behavior is verified in more detail in the geometry unit tests.
            //
            CanvasDevice device = new CanvasDevice();

            CanvasPathBuilder pathBuilder = new CanvasPathBuilder(device);

            pathBuilder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Alternate);
            pathBuilder.BeginFigure(0, 0);
            pathBuilder.AddLine(0, 0);
            pathBuilder.AddQuadraticBezier(new Vector2(), new Vector2());
            pathBuilder.AddCubicBezier(new Vector2(), new Vector2(), new Vector2());

            // D2D tries to be smart about degenerate arcs and redundant set segment options, and may sometimes actually
            // surpress them. Therefore, these calls use non-defaults.
            pathBuilder.AddArc(new Vector2 {
                X = 100, Y = 100
            }, 10, 10, 90, CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceUnstroked);

            pathBuilder.EndFigure(CanvasFigureLoop.Closed);

            CanvasGeometry pathGeometry = CanvasGeometry.CreatePath(pathBuilder);

            MyGeometryStreamReader myStreamReader = new MyGeometryStreamReader();

            pathGeometry.SendPathTo(myStreamReader);

            myStreamReader.Verify();
        }
コード例 #4
0
        private CanvasPathBuilder CreateBorderGauge(ICanvasResourceCreator sender)
        {
            var center = new Vector2(0, 0);

            float outerRadius = 49.0f;
            float innerRadius = outerRadius * 0.7f;

            var pathBuilder = new CanvasPathBuilder(sender);

            float startAngle = (float)StartAngle;
            float endAngle   = (float)EndAngle;

            float sweepAngle = (float)(StartAngle - EndAngle);

            pathBuilder.BeginFigure(GetVector(outerRadius, startAngle));

            pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);

            pathBuilder.AddArc(center, outerRadius, outerRadius, startAngle, -sweepAngle);

            pathBuilder.AddLine(GetVector(innerRadius, endAngle));

            pathBuilder.AddArc(center, innerRadius, innerRadius, endAngle, sweepAngle);

            pathBuilder.EndFigure(CanvasFigureLoop.Closed);

            return(pathBuilder);
        }
コード例 #5
0
        private static CanvasGeometry CreateHeart(ICanvasResourceCreator resourceCreator, float scale, Vector2 center)
        {
            center = center - new Vector2(center.X / 2, center.Y / 2);
            CanvasPathBuilder pathBuilder = new CanvasPathBuilder(resourceCreator);
            var begin = new Vector2(0.47f, 0.34f) * scale + center;

            pathBuilder.BeginFigure(begin);
            pathBuilder.AddCubicBezier(new Vector2(0.66f, 0.19f) * scale + center, new Vector2(0.88f, 0.30f) * scale + center, new Vector2(0.88f, 0.48f) * scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0.88f, 0.66f) * scale + center, new Vector2(0.49f, 1) * scale + center, new Vector2(0.49f, 1) * scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0.49f, 1) * scale + center, new Vector2(0, 0.66f) * scale + center, new Vector2(0, 0.48f) * scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0, 0.30f) * scale + center, new Vector2(0.33f, 0.19f) * scale + center, begin);

            pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
            pathBuilder.EndFigure(CanvasFigureLoop.Closed);
            var geometry = CanvasGeometry.CreatePath(pathBuilder);

            return(geometry);
        }
コード例 #6
0
ファイル: Path.cs プロジェクト: SiggiRull/uwpMyScript
        public void MoveTo(float x, float y)
        {
            if (!_isConfigured)
            {
                // Settings must be set from Canvas properties and before BeginFigure
                _pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.None);
                _pathBuilder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Winding);
                _isConfigured = true;
            }

            if (_isInFigure)
            {
                _pathBuilder.EndFigure(CanvasFigureLoop.Open);
            }

            _pathBuilder.BeginFigure(x, y);
            _isInFigure = true;
        }
コード例 #7
0
        public void Draw(ICanvasAnimatedControl sender, CanvasDrawingSession drawingSession)
        {
            if (currentThunder == null)
            {
                return;
            }
            // 保护原先画布的混合模式
            var previousBlend = drawingSession.Blend;

            drawingSession.Blend = blendState;
            var builder = new CanvasPathBuilder(sender);

            builder.BeginFigure(0, 0);
            for (int i = 0; i < currentThunder.LifeLong; i++)
            {
                builder.AddLine(currentThunder.Path[i].X, currentThunder.Path[i].Y);
            }
            builder.EndFigure(CanvasFigureLoop.Open);
            builder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);

            // Draw the particle.
            var  path = CanvasGeometry.CreatePath(builder);
            var  NormalizeLifeTime = currentThunder.TimeSinceStart / currentThunder.Duration;
            byte opacity           = (byte)((NormalizeLifeTime - 1) * (NormalizeLifeTime - 1) * 255);
            CanvasCommandList cl   = new CanvasCommandList(sender);

            using (CanvasDrawingSession clds = cl.CreateDrawingSession())
            {
                clds.DrawGeometry(path, currentThunder.Position, Color.FromArgb((byte)(0.75f * opacity), 255, 255, 255), 6 * currentThunder.Luminace);
            }
            var lightAmount = 20.6f * currentThunder.Luminace * (NormalizeLifeTime - 1) * (NormalizeLifeTime - 1);

            blur.Source     = cl;
            blur.BlurAmount = lightAmount;
            drawingSession.DrawImage(blur);
            drawingSession.DrawGeometry(path, currentThunder.Position, Color.FromArgb(opacity, 255, 240, 180), 2 * currentThunder.Luminace);
            drawingSession.Blend = previousBlend;
            if (NormalizeLifeTime > 1)
            {
                currentThunder = null;
            }
        }
コード例 #8
0
        private CanvasGeometry CreateCanvasGeometry(CanvasVirtualControl device)
        {
            switch (GeometryType)
            {
            case GeometryType.Rectangle:
                return(CanvasGeometry.CreateRectangle(device, (float)Points[0].X, (float)Points[0].Y,
                                                      (float)MapController.Instance.MousePosition.X - (float)Points[0].X, (float)MapController.Instance.MousePosition.Y - (float)Points[0].Y));

            case GeometryType.Line:
                CanvasPathBuilder pathBuilder = new CanvasPathBuilder(device);
                pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
                pathBuilder.BeginFigure((float)Points[0].X, (float)Points[0].Y);
                pathBuilder.AddLine(new Vector2((float)MapController.Instance.MousePosition.X, (float)MapController.Instance.MousePosition.Y));
                pathBuilder.EndFigure(CanvasFigureLoop.Open);
                return(CanvasGeometry.CreatePath(pathBuilder));

            case GeometryType.Circle:
                return(CanvasGeometry.CreateCircle(device, new Vector2((float)Points[0].X, (float)Points[0].Y), GetSegmentLength.CalculateSegmentLength(Points[0], MapController.Instance.MousePosition)));
            }
            return(null);
        }
コード例 #9
0
        public void Draw(ICanvasAnimatedControl sender, CanvasDrawingSession drawingSession)
        {
            if (currentThunder == null)
            {
                return;
            }
            // 保护原先画布的混合模式
            var previousBlend = drawingSession.Blend;
            drawingSession.Blend = blendState;
            var builder = new CanvasPathBuilder(sender);
            builder.BeginFigure(0, 0);
            for (int i = 0; i < currentThunder.LifeLong; i++)
            {
                builder.AddLine(currentThunder.Path[i].X, currentThunder.Path[i].Y);
            }
            builder.EndFigure(CanvasFigureLoop.Open);
            builder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);

            // Draw the particle.
            var path = CanvasGeometry.CreatePath(builder);
            var NormalizeLifeTime = currentThunder.TimeSinceStart / currentThunder.Duration;
            byte opacity = (byte)((NormalizeLifeTime - 1) * (NormalizeLifeTime - 1) * 255);
            CanvasCommandList cl = new CanvasCommandList(sender);
            using (CanvasDrawingSession clds = cl.CreateDrawingSession())
            {
                clds.DrawGeometry(path, currentThunder.Position, Color.FromArgb((byte)(0.75f * opacity), 255, 255, 255), 6 * currentThunder.Luminace);
            }
            var lightAmount = 20.6f * currentThunder.Luminace * (NormalizeLifeTime - 1) * (NormalizeLifeTime - 1);
            blur.Source = cl;
            blur.BlurAmount = lightAmount;
            drawingSession.DrawImage(blur);
            drawingSession.DrawGeometry(path, currentThunder.Position, Color.FromArgb(opacity, 255, 240, 180), 2 * currentThunder.Luminace);
            drawingSession.Blend = previousBlend;
            if (NormalizeLifeTime > 1)
            {
                currentThunder = null;
            }
        }
コード例 #10
0
        public static CanvasGeometry ToCanvasGeometry(this XPathGeometry pg, CanvasDrawingSession ds)
        {
            CanvasGeometry g;

            using (var builder = new CanvasPathBuilder(ds))
            {
                if (pg.FillRule == XFillRule.EvenOdd)
                {
                    builder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Alternate);
                }
                else
                {
                    builder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Winding);
                }

                foreach (var pf in pg.Figures)
                {
                    builder.BeginFigure(
                        (float)pf.StartPoint.X,
                        (float)pf.StartPoint.Y,
                        pf.IsFilled ? CanvasFigureFill.Default : CanvasFigureFill.DoesNotAffectFills);

                    foreach (var segment in pf.Segments)
                    {
                        var options = CanvasFigureSegmentOptions.None;
                        if (!segment.IsStroked)
                        {
                            options |= CanvasFigureSegmentOptions.ForceUnstroked;
                        }

                        if (segment.IsSmoothJoin)
                        {
                            options |= CanvasFigureSegmentOptions.ForceRoundLineJoin;
                        }

                        builder.SetSegmentOptions(options);

                        if (segment is XArcSegment)
                        {
                            var arcSegment = segment as XArcSegment;
                            builder.AddArc(
                                new N.Vector2(
                                    (float)arcSegment.Point.X,
                                    (float)arcSegment.Point.Y),
                                (float)(arcSegment.Size.Width / 2.0),
                                (float)(arcSegment.Size.Height / 2.0),
                                (float)arcSegment.RotationAngle,
                                arcSegment.SweepDirection == XSweepDirection.Clockwise ? CanvasSweepDirection.Clockwise : CanvasSweepDirection.CounterClockwise,
                                arcSegment.IsLargeArc ? CanvasArcSize.Large : CanvasArcSize.Small);
                        }
                        else if (segment is XBezierSegment)
                        {
                            var bezierSegment = segment as XBezierSegment;
                            builder.AddCubicBezier(
                                new N.Vector2(
                                    (float)bezierSegment.Point1.X,
                                    (float)bezierSegment.Point1.Y),
                                new N.Vector2(
                                    (float)bezierSegment.Point2.X,
                                    (float)bezierSegment.Point2.Y),
                                new N.Vector2(
                                    (float)bezierSegment.Point3.X,
                                    (float)bezierSegment.Point3.Y));
                        }
                        else if (segment is XLineSegment)
                        {
                            var lineSegment = segment as XLineSegment;
                            builder.AddLine(
                                new N.Vector2(
                                    (float)lineSegment.Point.X,
                                    (float)lineSegment.Point.Y));
                        }
                        else if (segment is XPolyBezierSegment)
                        {
                            var polyBezierSegment = segment as XPolyBezierSegment;
                            if (polyBezierSegment.Points.Count >= 3)
                            {
                                builder.AddCubicBezier(
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[0].X,
                                        (float)polyBezierSegment.Points[0].Y),
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[1].X,
                                        (float)polyBezierSegment.Points[1].Y),
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[2].X,
                                        (float)polyBezierSegment.Points[2].Y));
                            }

                            if (polyBezierSegment.Points.Count > 3 &&
                                polyBezierSegment.Points.Count % 3 == 0)
                            {
                                for (int i = 3; i < polyBezierSegment.Points.Count; i += 3)
                                {
                                    builder.AddCubicBezier(
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i].X,
                                            (float)polyBezierSegment.Points[i].Y),
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i + 1].X,
                                            (float)polyBezierSegment.Points[i + 1].Y),
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i + 2].X,
                                            (float)polyBezierSegment.Points[i + 2].Y));
                                }
                            }
                        }
                        else if (segment is XPolyLineSegment)
                        {
                            var polyLineSegment = segment as XPolyLineSegment;

                            if (polyLineSegment.Points.Count >= 1)
                            {
                                builder.AddLine(
                                    new N.Vector2(
                                        (float)polyLineSegment.Points[0].X,
                                        (float)polyLineSegment.Points[0].Y));
                            }

                            if (polyLineSegment.Points.Count > 1)
                            {
                                for (int i = 1; i < polyLineSegment.Points.Count; i++)
                                {
                                    builder.AddLine(
                                        new N.Vector2(
                                            (float)polyLineSegment.Points[i].X,
                                            (float)polyLineSegment.Points[i].Y));
                                }
                            }
                        }
                        else if (segment is XPolyQuadraticBezierSegment)
                        {
                            var polyQuadraticSegment = segment as XPolyQuadraticBezierSegment;
                            if (polyQuadraticSegment.Points.Count >= 2)
                            {
                                builder.AddQuadraticBezier(
                                    new N.Vector2(
                                        (float)polyQuadraticSegment.Points[0].X,
                                        (float)polyQuadraticSegment.Points[0].Y),
                                    new N.Vector2(
                                        (float)polyQuadraticSegment.Points[1].X,
                                        (float)polyQuadraticSegment.Points[1].Y));
                            }

                            if (polyQuadraticSegment.Points.Count > 2 &&
                                polyQuadraticSegment.Points.Count % 2 == 0)
                            {
                                for (int i = 3; i < polyQuadraticSegment.Points.Count; i += 3)
                                {
                                    builder.AddQuadraticBezier(
                                        new N.Vector2(
                                            (float)polyQuadraticSegment.Points[i].X,
                                            (float)polyQuadraticSegment.Points[i].Y),
                                        new N.Vector2(
                                            (float)polyQuadraticSegment.Points[i + 1].X,
                                            (float)polyQuadraticSegment.Points[i + 1].Y));
                                }
                            }
                        }
                        else if (segment is XQuadraticBezierSegment)
                        {
                            var qbezierSegment = segment as XQuadraticBezierSegment;
                            builder.AddQuadraticBezier(
                                new N.Vector2(
                                    (float)qbezierSegment.Point1.X,
                                    (float)qbezierSegment.Point1.Y),
                                new N.Vector2(
                                    (float)qbezierSegment.Point2.X,
                                    (float)qbezierSegment.Point2.Y));
                        }
                        else
                        {
                            throw new NotSupportedException("Not supported segment type: " + segment.GetType());
                        }
                    }

                    builder.EndFigure(pf.IsClosed ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open);
                }

                g = CanvasGeometry.CreatePath(builder);
            }

            return(g);
        }
コード例 #11
0
        private static CanvasGeometry CreateHeart(ICanvasResourceCreator resourceCreator, float scale, Vector2 center)
        {
            center = center - new Vector2(center.X / 2, center.Y / 2);
            CanvasPathBuilder pathBuilder = new CanvasPathBuilder(resourceCreator);
            var begin = new Vector2(0.47f, 0.34f) * scale + center;
            pathBuilder.BeginFigure(begin);
            pathBuilder.AddCubicBezier(new Vector2(0.66f, 0.19f) * scale + center, new Vector2(0.88f, 0.30f) * scale + center, new Vector2(0.88f, 0.48f) * scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0.88f, 0.66f) * scale + center, new Vector2(0.49f, 1) * scale + center, new Vector2(0.49f, 1)* scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0.49f, 1) * scale + center, new Vector2(0, 0.66f) * scale + center, new Vector2(0, 0.48f) * scale + center);
            pathBuilder.AddCubicBezier(new Vector2(0, 0.30f) * scale + center, new Vector2(0.33f, 0.19f) * scale + center, begin);

            pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
            pathBuilder.EndFigure(CanvasFigureLoop.Closed);
            var geometry = CanvasGeometry.CreatePath(pathBuilder);
            return geometry;
        }
コード例 #12
0
        public static CanvasGeometry ToCanvasGeometry(this XPathGeometry pg, CanvasDrawingSession ds)
        {
            CanvasGeometry g;

            using (var builder = new CanvasPathBuilder(ds))
            {
                if (pg.FillRule == XFillRule.EvenOdd)
                    builder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Alternate);
                else
                    builder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Winding);

                foreach (var pf in pg.Figures)
                {
                    builder.BeginFigure(
                        (float)pf.StartPoint.X,
                        (float)pf.StartPoint.Y,
                        pf.IsFilled ? CanvasFigureFill.Default : CanvasFigureFill.DoesNotAffectFills);

                    foreach (var segment in pf.Segments)
                    {
                        var options = CanvasFigureSegmentOptions.None;
                        if (!segment.IsStroked)
                            options |= CanvasFigureSegmentOptions.ForceUnstroked;

                        if (segment.IsSmoothJoin)
                            options |= CanvasFigureSegmentOptions.ForceRoundLineJoin;

                        builder.SetSegmentOptions(options);

                        if (segment is XArcSegment)
                        {
                            var arcSegment = segment as XArcSegment;
                            builder.AddArc(
                                new N.Vector2(
                                    (float)arcSegment.Point.X,
                                    (float)arcSegment.Point.Y),
                                (float)(arcSegment.Size.Width / 2.0),
                                (float)(arcSegment.Size.Height / 2.0),
                                (float)arcSegment.RotationAngle,
                                arcSegment.SweepDirection == XSweepDirection.Clockwise ? CanvasSweepDirection.Clockwise : CanvasSweepDirection.CounterClockwise,
                                arcSegment.IsLargeArc ? CanvasArcSize.Large : CanvasArcSize.Small);
                        }
                        else if (segment is XBezierSegment)
                        {
                            var bezierSegment = segment as XBezierSegment;
                            builder.AddCubicBezier(
                                new N.Vector2(
                                    (float)bezierSegment.Point1.X,
                                    (float)bezierSegment.Point1.Y),
                                new N.Vector2(
                                    (float)bezierSegment.Point2.X,
                                    (float)bezierSegment.Point2.Y),
                                new N.Vector2(
                                    (float)bezierSegment.Point3.X,
                                    (float)bezierSegment.Point3.Y));
                        }
                        else if (segment is XLineSegment)
                        {
                            var lineSegment = segment as XLineSegment;
                            builder.AddLine(
                                new N.Vector2(
                                    (float)lineSegment.Point.X,
                                    (float)lineSegment.Point.Y));
                        }
                        else if (segment is XPolyBezierSegment)
                        {
                            var polyBezierSegment = segment as XPolyBezierSegment;
                            if (polyBezierSegment.Points.Count >= 3)
                            {
                                builder.AddCubicBezier(
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[0].X,
                                        (float)polyBezierSegment.Points[0].Y),
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[1].X,
                                        (float)polyBezierSegment.Points[1].Y),
                                    new N.Vector2(
                                        (float)polyBezierSegment.Points[2].X,
                                        (float)polyBezierSegment.Points[2].Y));
                            }

                            if (polyBezierSegment.Points.Count > 3
                                && polyBezierSegment.Points.Count % 3 == 0)
                            {
                                for (int i = 3; i < polyBezierSegment.Points.Count; i += 3)
                                {
                                    builder.AddCubicBezier(
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i].X,
                                            (float)polyBezierSegment.Points[i].Y),
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i + 1].X,
                                            (float)polyBezierSegment.Points[i + 1].Y),
                                        new N.Vector2(
                                            (float)polyBezierSegment.Points[i + 2].X,
                                            (float)polyBezierSegment.Points[i + 2].Y));
                                }
                            }
                        }
                        else if (segment is XPolyLineSegment)
                        {
                            var polyLineSegment = segment as XPolyLineSegment;

                            if (polyLineSegment.Points.Count >= 1)
                            {
                                builder.AddLine(
                                    new N.Vector2(
                                        (float)polyLineSegment.Points[0].X,
                                        (float)polyLineSegment.Points[0].Y));
                            }

                            if (polyLineSegment.Points.Count > 1)
                            {
                                for (int i = 1; i < polyLineSegment.Points.Count; i++)
                                {
                                    builder.AddLine(
                                        new N.Vector2(
                                            (float)polyLineSegment.Points[i].X,
                                            (float)polyLineSegment.Points[i].Y));
                                }
                            }
                        }
                        else if (segment is XPolyQuadraticBezierSegment)
                        {
                            var polyQuadraticSegment = segment as XPolyQuadraticBezierSegment;
                            if (polyQuadraticSegment.Points.Count >= 2)
                            {
                                builder.AddQuadraticBezier(
                                    new N.Vector2(
                                        (float)polyQuadraticSegment.Points[0].X,
                                        (float)polyQuadraticSegment.Points[0].Y),
                                    new N.Vector2(
                                        (float)polyQuadraticSegment.Points[1].X,
                                        (float)polyQuadraticSegment.Points[1].Y));
                            }

                            if (polyQuadraticSegment.Points.Count > 2
                                && polyQuadraticSegment.Points.Count % 2 == 0)
                            {
                                for (int i = 3; i < polyQuadraticSegment.Points.Count; i += 3)
                                {
                                    builder.AddQuadraticBezier(
                                        new N.Vector2(
                                            (float)polyQuadraticSegment.Points[i].X,
                                            (float)polyQuadraticSegment.Points[i].Y),
                                        new N.Vector2(
                                            (float)polyQuadraticSegment.Points[i + 1].X,
                                            (float)polyQuadraticSegment.Points[i + 1].Y));
                                }
                            }
                        }
                        else if (segment is XQuadraticBezierSegment)
                        {
                            var qbezierSegment = segment as XQuadraticBezierSegment;
                            builder.AddQuadraticBezier(
                                new N.Vector2(
                                    (float)qbezierSegment.Point1.X,
                                    (float)qbezierSegment.Point1.Y),
                                new N.Vector2(
                                    (float)qbezierSegment.Point2.X,
                                    (float)qbezierSegment.Point2.Y));
                        }
                        else
                        {
                            throw new NotSupportedException("Not supported segment type: " + segment.GetType());
                        }
                    }

                    builder.EndFigure(pf.IsClosed ? CanvasFigureLoop.Closed : CanvasFigureLoop.Open);
                }

                g = CanvasGeometry.CreatePath(builder);
            }

            return g;
        }
コード例 #13
0
ファイル: PathReceiver.cs プロジェクト: zhangf911/Win2D
        public void VerifySendPathTo()
        {
            //
            // This calls each of the path functions, once, and verfies that it works.
            // Sink behavior is verified in more detail in the geometry unit tests.
            //
            CanvasDevice device = new CanvasDevice();

            CanvasPathBuilder pathBuilder = new CanvasPathBuilder(device);
            pathBuilder.SetFilledRegionDetermination(CanvasFilledRegionDetermination.Alternate);
            pathBuilder.BeginFigure(0, 0);
            pathBuilder.AddLine(0, 0);
            pathBuilder.AddQuadraticBezier(new Vector2(), new Vector2());
            pathBuilder.AddCubicBezier(new Vector2(), new Vector2(), new Vector2());

            // D2D tries to be smart about degenerate arcs and redundant set segment options, and may sometimes actually
            // surpress them. Therefore, these calls use non-defaults.
            pathBuilder.AddArc(new Vector2 { X = 100, Y = 100 }, 10, 10, 90, CanvasSweepDirection.Clockwise, CanvasArcSize.Small);
            pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceUnstroked);

            pathBuilder.EndFigure(CanvasFigureLoop.Closed);

            CanvasGeometry pathGeometry = CanvasGeometry.CreatePath(pathBuilder);

            MyGeometryStreamReader myStreamReader = new MyGeometryStreamReader();
            pathGeometry.SendPathTo(myStreamReader);

            myStreamReader.Verify();
        }
コード例 #14
0
        public static void DrawGeometry(Tile.Feature feature, float scale, CanvasDrawingSession session, Windows.UI.Color fillColor, Windows.UI.Color strokeColor, float innerLineWidth, float outerLineWidth, CanvasStrokeStyle strokeStyle)
        {
            Queue <uint> q  = new Queue <uint>(feature.Geometries);
            float        cx = 0;
            float        cy = 0;

            List <System.Numerics.Vector2> poly = new List <System.Numerics.Vector2>();

            while (q.Count > 0)
            {
                var cmd = DecodeCommand(q.Dequeue());
                switch (cmd.Item1)
                {
                case GeometryCommand.MoveTo:
                    cx += DecodeParameter(q.Dequeue()) * scale;
                    cy += DecodeParameter(q.Dequeue()) * scale;
                    poly.Add(new System.Numerics.Vector2(cx, cy));
                    break;

                case GeometryCommand.LineTo:
                    for (int it = 0; it < cmd.count; it++)
                    {
                        cx += DecodeParameter(q.Dequeue()) * scale;
                        cy += DecodeParameter(q.Dequeue()) * scale;
                        poly.Add(new System.Numerics.Vector2(cx, cy));
                    }
                    break;

                case GeometryCommand.ClosePath:
                    if (feature.Type == Tile.GeomType.Polygon)
                    {
                        CanvasGeometry geom = CanvasGeometry.CreatePolygon(session.Device, poly.ToArray());
                        var            cf   = CanvasCachedGeometry.CreateFill(geom);
                        var            cd   = CanvasCachedGeometry.CreateStroke(geom, 1);
                        cache.Add(cf); cache.Add(cd);
                        session.DrawCachedGeometry(cf, fillColor);
                        session.DrawCachedGeometry(cd, strokeColor);
                        //session.FillGeometry(geom, fillColor);
                        //session.DrawGeometry(geom, strokeColor);
                    }
                    poly.Clear();
                    break;
                }
            }
            if (feature.Type == Tile.GeomType.Linestring)
            {
                CanvasPathBuilder pathBuilder = new CanvasPathBuilder(session);
                pathBuilder.SetSegmentOptions(CanvasFigureSegmentOptions.ForceRoundLineJoin);
                pathBuilder.BeginFigure(poly[0]);
                for (int it = 1; it < (poly.Count); it++)
                {
                    pathBuilder.AddLine(poly[it]);

                    //if(outerLineWidth > 0)
                    //	session.DrawLine(poly[it], poly[it + 1], strokeColor, outerLineWidth, strokeStyle);
                    //if (innerLineWidth > 0)
                    //	session.DrawLine(poly[it], poly[it + 1], fillColor, innerLineWidth, strokeStyle);
                }
                pathBuilder.EndFigure(CanvasFigureLoop.Open);
                var geometry = CanvasGeometry.CreatePath(pathBuilder);
                if (outerLineWidth > 0)
                {
                    var cg = CanvasCachedGeometry.CreateStroke(geometry, outerLineWidth, strokeStyle);
                    cache.Add(cg);
                    session.DrawCachedGeometry(cg, strokeColor);
                    //session.DrawGeometry(geometry, strokeColor, outerLineWidth, strokeStyle);
                }
                if (innerLineWidth > 0)
                {
                    var cg = CanvasCachedGeometry.CreateStroke(geometry, innerLineWidth, strokeStyle);
                    cache.Add(cg);
                    session.DrawCachedGeometry(cg, fillColor);
                    //session.DrawGeometry(geometry, fillColor, innerLineWidth, strokeStyle);
                }
                poly.Clear();
            }
        }