Ejemplo n.º 1
0
        public void Should_Measure_Expander_Triangle_With_Stroke_Correctly()
        {
            using (AvaloniaLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = new Path
                {
                    Data = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"),
                    Stroke = Brushes.Black,
                    StrokeThickness = 2,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    UseLayoutRounding = false,
                };

                target.Measure(new Size(100, 100));
                target.Arrange(new Rect(0, 0, 100, 100));

                // Measured geometry with stroke of 2px is:
                //
                //     {-1, -0.414, 6.414, 12.828} (see GeometryTests)
                //
                // With origin at 0,0 the bounds should equal:
                //
                //     Assert.Equal(new Rect(0, 0, 5.414, 12.414), target.Bounds, compare);
                //
                // However Path.Measure doesn't correctly handle strokes currently, so testing for
                // the (incorrect) current output for now...
                Assert.Equal(new Rect(0, 0, 4, 10), target.Bounds, Compare);
            }
        }
Ejemplo n.º 2
0
        public async Task PreciseEllipticArc_Produces_Valid_Arcs_In_All_Directions()
        {
            var grid = new Avalonia.Controls.Primitives.UniformGrid()
            {
                Columns = 2, Rows = 4, Width = 320, Height = 400
            };

            foreach (var sweepDirection in new[] { SweepDirection.Clockwise, SweepDirection.CounterClockwise })
            {
                foreach (var isLargeArc in new[] { false, true })
                {
                    foreach (var isPrecise in new[] { false, true })
                    {
                        Point Pt(double x, double y) => new Point(x, y);
                        Size Sz(double w, double h) => new Size(w, h);

                        var streamGeometry = new StreamGeometry();
                        using (var context = streamGeometry.Open())
                        {
                            context.BeginFigure(Pt(20, 20), true);

                            if (isPrecise)
                            {
                                context.PreciseArcTo(Pt(40, 40), Sz(20, 20), 0, isLargeArc, sweepDirection);
                            }
                            else
                            {
                                context.ArcTo(Pt(40, 40), Sz(20, 20), 0, isLargeArc, sweepDirection);
                            }
                            context.LineTo(Pt(40, 20));
                            context.LineTo(Pt(20, 20));
                            context.EndFigure(true);
                        }
                        var pathShape = new Avalonia.Controls.Shapes.Path();
                        pathShape.Data            = streamGeometry;
                        pathShape.Stroke          = new SolidColorBrush(Colors.CornflowerBlue);
                        pathShape.Fill            = new SolidColorBrush(Colors.Gold);
                        pathShape.StrokeThickness = 2;
                        pathShape.Margin          = new Thickness(20);
                        grid.Children.Add(pathShape);
                    }
                }
            }
            await RenderToFile(grid);
        }
Ejemplo n.º 3
0
        public void Should_Measure_Expander_Triangle_Correctly()
        {
            using (AvaloniaLocator.EnterScope())
            {
                Direct2D1Platform.Initialize();

                var target = new Path
                {
                    Data = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"),
                    StrokeThickness = 0,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    VerticalAlignment = VerticalAlignment.Top,
                    UseLayoutRounding = false,
                };

                target.Measure(new Size(100, 100));
                target.Arrange(new Rect(0, 0, 100, 100));

                Assert.Equal(new Rect(0, 0, 4, 10), target.Bounds, Compare);
            }
        }
Ejemplo n.º 4
0
        private void AddColorHexagon(int i, Palette palette, Canvas container)
        {
            int rowInd = i / MaxColumns;
            int colInd = i % MaxColumns;

            double centerX;
            double centerY;

            if (i == 0 && palette.Colors.Count == 1)
            {
                centerX = HexagonRadius * (1.5 + colInd * 2.5);
                centerY = HexagonRadius * Math.Sin(Math.PI / 3) * (2 + 2 * rowInd - 1);
            }
            else if (colInd == 0)
            {
                centerX = HexagonRadius * (1.5 + 2.5);
                centerY = HexagonRadius * Math.Sin(Math.PI / 3) * (2 + 2 * rowInd - 1);
            }
            else if (colInd == 1)
            {
                centerX = HexagonRadius * 1.5;
                centerY = HexagonRadius * Math.Sin(Math.PI / 3) * (2 + 2 * rowInd);
            }
            else
            {
                centerX = HexagonRadius * (1.5 + colInd * 2.5);
                centerY = HexagonRadius * Math.Sin(Math.PI / 3) * (2 + 2 * rowInd - colInd % 2);
            }

            centerX += 4.33;

            PathGeometry leftHexagon   = GetHexagonPath(new Point(centerX - 0.5 * HexagonRadius, centerY), HexagonRadius);
            PathGeometry centerHexagon = GetHexagonPath(new Point(centerX, centerY), HexagonRadius);
            PathGeometry rightHexagon  = GetHexagonPath(new Point(centerX + 0.5 * HexagonRadius, centerY), HexagonRadius);


            Color color        = palette.Colors[i % palette.Colors.Count];
            Color lighterColor = ColorPicker.GetLighterColor(color);
            Color darkerColor  = ColorPicker.GetDarkerColor(color);


            Avalonia.Controls.Shapes.Path leftPath = new Avalonia.Controls.Shapes.Path()
            {
                Data = leftHexagon, Fill = new ColorVisualBrush(lighterColor), Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand)
            };
            Avalonia.Controls.Shapes.Path rightPath = new Avalonia.Controls.Shapes.Path()
            {
                Data = rightHexagon, Fill = new ColorVisualBrush(darkerColor), Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand)
            };
            Avalonia.Controls.Shapes.Path centerPath = new Avalonia.Controls.Shapes.Path()
            {
                Data = centerHexagon, Fill = new ColorVisualBrush(color), Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand)
            };

            leftPath.Classes.Add("HexagonLeftButton");
            rightPath.Classes.Add("HexagonRightButton");
            centerPath.Classes.Add("HexagonCenterButton");

            rightPath.PointerEnter += (s, e) =>
            {
                rightPath.Classes.Add("rightOver");
                centerPath.Classes.Add("rightOver");
                leftPath.Classes.Add("rightOver");
            };

            rightPath.PointerLeave += async(s, e) =>
            {
                rightPath.Classes.Add("rightOverBlurring");
                centerPath.Classes.Add("rightOverBlurring");
                leftPath.Classes.Add("rightOverBlurring");
                rightPath.Classes.Remove("rightOver");
                centerPath.Classes.Remove("rightOver");
                leftPath.Classes.Remove("rightOver");

                await Task.Delay(100);

                rightPath.Classes.Remove("rightOverBlurring");
                centerPath.Classes.Remove("rightOverBlurring");
                leftPath.Classes.Remove("rightOverBlurring");
            };

            leftPath.PointerEnter += (s, e) =>
            {
                rightPath.Classes.Add("leftOver");
                centerPath.Classes.Add("leftOver");
                leftPath.Classes.Add("leftOver");
            };

            leftPath.PointerLeave += async(s, e) =>
            {
                rightPath.Classes.Add("leftOverBlurring");
                centerPath.Classes.Add("leftOverBlurring");
                leftPath.Classes.Add("leftOverBlurring");
                rightPath.Classes.Remove("leftOver");
                centerPath.Classes.Remove("leftOver");
                leftPath.Classes.Remove("leftOver");

                await Task.Delay(100);

                rightPath.Classes.Remove("leftOverBlurring");
                centerPath.Classes.Remove("leftOverBlurring");
                leftPath.Classes.Remove("leftOverBlurring");
            };

            centerPath.PointerEnter += (s, e) =>
            {
                rightPath.Classes.Add("centerOver");
                centerPath.Classes.Add("centerOver");
                leftPath.Classes.Add("centerOver");
            };

            centerPath.PointerLeave += async(s, e) =>
            {
                rightPath.Classes.Add("centerOverBlurring");
                centerPath.Classes.Add("centerOverBlurring");
                leftPath.Classes.Add("centerOverBlurring");
                rightPath.Classes.Remove("centerOver");
                centerPath.Classes.Remove("centerOver");
                leftPath.Classes.Remove("centerOver");

                await Task.Delay(100);

                rightPath.Classes.Remove("centerOverBlurring");
                centerPath.Classes.Remove("centerOverBlurring");
                leftPath.Classes.Remove("centerOverBlurring");
            };

            leftPath.PointerPressed += (s, e) =>
            {
                this.Color = lighterColor;
            };

            rightPath.PointerPressed += (s, e) =>
            {
                this.Color = darkerColor;
            };

            centerPath.PointerPressed += (s, e) =>
            {
                this.Color = color;
            };

            RelativePoint renderTransformOrigin = new RelativePoint(centerX, centerY, RelativeUnit.Absolute);

            leftPath.RenderTransformOrigin   = renderTransformOrigin;
            rightPath.RenderTransformOrigin  = renderTransformOrigin;
            centerPath.RenderTransformOrigin = renderTransformOrigin;

            container.Children.Add(leftPath);
            container.Children.Add(rightPath);
            container.Children.Add(centerPath);
        }