示例#1
0
    private Grid Piece()
    {
        Grid grid = new Grid()
        {
            HeightRequest = 60,
            WidthRequest  = 60,
        };

        if (_piece == cross)
        {
            BoxView line1 = new BoxView()
            {
                Color             = Color.Red,
                HeightRequest     = 60,
                WidthRequest      = 5,
                VerticalOptions   = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center,
                Rotation          = 45,
            };
            BoxView line2 = new BoxView()
            {
                Color             = Color.Red,
                HeightRequest     = 60,
                WidthRequest      = 5,
                VerticalOptions   = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center,
                Rotation          = 135,
            };
            grid.Children.Add(line1);
            grid.Children.Add(line2);
        }
        else if (_piece == nought)
        {
            EllipseView circle = new EllipseView()
            {
                Color             = Color.Blue,
                HeightRequest     = 60,
                WidthRequest      = 60,
                StrokeWidth       = 5,
                VerticalOptions   = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center
            };
            grid.Children.Add(circle);
        }
        return(grid);
    }
示例#2
0
        void NewGame(float width, float height)
        {
            // The constructor creates the random maze layout
            mazeGrid = new MazeGrid(MAZE_HORZ_CHAMBERS, MAZE_VERT_CHAMBERS);

            // Initialize borders collection
            borders.Clear();

            // Create Line2D objects for the border lines of the maze
            float cellWidth     = width / mazeGrid.Width;
            float cellHeight    = height / mazeGrid.Height;
            int   halfWallWidth = WALL_WIDTH / 2;

            for (int x = 0; x < mazeGrid.Width; x++)
            {
                for (int y = 0; y < mazeGrid.Height; y++)
                {
                    MazeCell mazeCell = mazeGrid.Cells[x, y];
                    Vector2  ll       = new Vector2(x * cellWidth, (y + 1) * cellHeight);
                    Vector2  ul       = new Vector2(x * cellWidth, y * cellHeight);
                    Vector2  ur       = new Vector2((x + 1) * cellWidth, y * cellHeight);
                    Vector2  lr       = new Vector2((x + 1) * cellWidth, (y + 1) * cellHeight);
                    Vector2  right    = halfWallWidth * Vector2.UnitX;
                    Vector2  left     = -right;
                    Vector2  down     = halfWallWidth * Vector2.UnitY;
                    Vector2  up       = -down;

                    if (mazeCell.HasLeft)
                    {
                        borders.Add(new Line2D(ll + down, ll + down + right));
                        borders.Add(new Line2D(ll + down + right, ul + up + right));
                        borders.Add(new Line2D(ul + up + right, ul + up));
                    }
                    if (mazeCell.HasTop)
                    {
                        borders.Add(new Line2D(ul + left, ul + left + down));
                        borders.Add(new Line2D(ul + left + down, ur + right + down));
                        borders.Add(new Line2D(ur + right + down, ur + right));
                    }
                    if (mazeCell.HasRight)
                    {
                        borders.Add(new Line2D(ur + up, ur + up + left));
                        borders.Add(new Line2D(ur + up + left, lr + down + left));
                        borders.Add(new Line2D(lr + down + left, lr + down));
                    }
                    if (mazeCell.HasBottom)
                    {
                        borders.Add(new Line2D(lr + right, lr + right + up));
                        borders.Add(new Line2D(lr + right + up, ll + left + up));
                        borders.Add(new Line2D(ll + left + up, ll + left));
                    }
                }
            }

            // Prepare AbsoluteLayout for new children
            absoluteLayout.Children.Clear();

            // "Draw" the walls of the maze using BoxView
            BoxView createBoxView() => new BoxView
            {
                Color = Color.Green
            };

            for (int x = 0; x < mazeGrid.Width; x++)
            {
                for (int y = 0; y < mazeGrid.Height; y++)
                {
                    MazeCell mazeCell = mazeGrid.Cells[x, y];

                    if (mazeCell.HasLeft)
                    {
                        Rectangle rect = new Rectangle(x * cellWidth,
                                                       y * cellHeight - halfWallWidth,
                                                       halfWallWidth, cellHeight + WALL_WIDTH);

                        absoluteLayout.Children.Add(createBoxView(), rect);
                    }

                    if (mazeCell.HasRight)
                    {
                        Rectangle rect = new Rectangle((x + 1) * cellWidth - halfWallWidth,
                                                       y * cellHeight - halfWallWidth,
                                                       halfWallWidth, cellHeight + WALL_WIDTH);

                        absoluteLayout.Children.Add(createBoxView(), rect);
                    }

                    if (mazeCell.HasTop)
                    {
                        Rectangle rect = new Rectangle(x * cellWidth - halfWallWidth,
                                                       y * cellHeight,
                                                       cellWidth + WALL_WIDTH, halfWallWidth);

                        absoluteLayout.Children.Add(createBoxView(), rect);
                    }

                    if (mazeCell.HasBottom)
                    {
                        Rectangle rect = new Rectangle(x * cellWidth - halfWallWidth,
                                                       (y + 1) * cellHeight - halfWallWidth,
                                                       cellWidth + WALL_WIDTH, halfWallWidth);

                        absoluteLayout.Children.Add(createBoxView(), rect);
                    }
                }
            }

            // Randomly position ball in one of the corners
            bool isBallLeftCorner = random.Next(2) == 0;
            bool isBallTopCorner  = random.Next(2) == 0;

            // Create the hole first (so Z order is under the ball)
            //      and position it in the opposite corner from the ball
            hole = new EllipseView
            {
                Color         = Color.Black,
                WidthRequest  = 2 * HOLE_RADIUS,
                HeightRequest = 2 * HOLE_RADIUS
            };

            holePosition = new Vector2((isBallLeftCorner ? 2 * mazeGrid.Width - 1 : 1) * (width / mazeGrid.Width / 2),
                                       (isBallTopCorner ? 2 * mazeGrid.Height - 1 : 1) * (height / mazeGrid.Height / 2));

            absoluteLayout.Children.Add(hole, new Point(holePosition.X - HOLE_RADIUS,
                                                        holePosition.Y - HOLE_RADIUS));

            // Create the ball and set initial position
            ball = new EllipseView
            {
                Color         = Color.Red,
                WidthRequest  = 2 * BALL_RADIUS,
                HeightRequest = 2 * BALL_RADIUS
            };

            ballPosition = new Vector2((isBallLeftCorner ? 1 : 2 * mazeGrid.Width - 1) * (width / mazeGrid.Width / 2),
                                       (isBallTopCorner ? 1 : 2 * mazeGrid.Height - 1) * (height / mazeGrid.Height / 2));

            absoluteLayout.Children.Add(ball, new Point(ballPosition.X - BALL_RADIUS,
                                                        ballPosition.Y - BALL_RADIUS));
        }
示例#3
0
        public override void Run(ElmSharp.Box parent)
        {
            var scrollView = new ScrollView(parent)
            {
                WeightX    = 1,
                WeightY    = 1,
                AlignmentY = -1,
                AlignmentX = -1,
                VerticalScrollBarVisibility = ScrollBarVisibility.Always,
            };

            scrollView.Show();

            var content = new ElmSharp.Box(parent)
            {
                WeightX    = 1,
                WeightY    = 1,
                AlignmentY = -1,
                AlignmentX = -1,
            };

            scrollView.SetScrollCanvas(content);
            parent.PackEnd(scrollView);

            var ellipseText1 = new ElmSharp.Label(parent)
            {
                WeightX = 1,
                WeightY = 1,
                Text    = "EllipseView with SolidBrush"
            };

            ellipseText1.Show();
            var ellipse1 = new EllipseView(parent)
            {
                WeightX         = 1,
                WeightY         = 1,
                AlignmentY      = -1,
                AlignmentX      = -1,
                Stroke          = new SolidColorBrush(Color.Maroon),
                StrokeThickness = 5,
                Fill            = new SolidColorBrush(Color.Lavender),
                Aspect          = Stretch.Uniform,
            };

            ellipse1.Show();

            content.PackEnd(ellipseText1);
            content.PackEnd(ellipse1);

            var ellipseText2 = new ElmSharp.Label(parent)
            {
                WeightX = 1,
                WeightY = 1,
                Text    = "EllipseView with GradientBrush"
            };

            ellipseText2.Show();
            var ellipse2 = new EllipseView(parent)
            {
                WeightX    = 1,
                WeightY    = 1,
                AlignmentY = -1,
                AlignmentX = -1,
                Stroke     = new LinearGradientBrush(new System.Collections.Generic.List <GradientStop>()
                {
                    new GradientStop(Color.Lavender, 0.2f),
                    new GradientStop(Color.LightSkyBlue, 0.4f),
                    new GradientStop(Color.LightCyan, 0.6f),
                    new GradientStop(Color.LightPink, 0.8f),
                    new GradientStop(Color.YellowGreen, 1.0f),
                }),
                Fill = new RadialGradientBrush(new System.Collections.Generic.List <GradientStop>()
                {
                    new GradientStop(Color.YellowGreen, 0.2f),
                    new GradientStop(Color.LightPink, 0.4f),
                    new GradientStop(Color.LightCyan, 0.6f),
                    new GradientStop(Color.LightSkyBlue, 0.8f),
                    new GradientStop(Color.Lavender, 1.0f),
                }),
                StrokeThickness = 15,
                Aspect          = Stretch.Fill,
            };

            ellipse2.Show();
            content.PackEnd(ellipseText2);
            content.PackEnd(ellipse2);

            var ellipseText3 = new ElmSharp.Label(parent)
            {
                WeightX = 1,
                WeightY = 1,
                Text    = "EllipseView Aspect Test"
            };

            ellipseText3.Show();
            var ellipse3 = new EllipseView(parent)
            {
                WeightX         = 1,
                WeightY         = 1,
                AlignmentY      = -1,
                AlignmentX      = -1,
                Stroke          = new SolidColorBrush(Color.PaleGoldenrod),
                StrokeThickness = 5,
                Fill            = new SolidColorBrush(Color.MediumPurple),
                Aspect          = Stretch.Fill
            };

            ellipse3.Show();
            var aspectButton = new Button(parent)
            {
                Text       = "Change Aspect",
                WeightX    = 0.5,
                WeightY    = 0.5,
                AlignmentY = -1,
                AlignmentX = -1,
            };

            aspectButton.Show();
            aspectButton.Clicked += (s, e) =>
            {
                switch (ellipse3.Aspect)
                {
                case (Stretch.Fill):
                    ellipse3.Aspect = Stretch.Uniform;
                    break;

                case (Stretch.Uniform):
                    ellipse3.Aspect = Stretch.UniformToFill;
                    break;

                case (Stretch.UniformToFill):
                    ellipse3.Aspect = Stretch.Fill;
                    break;
                }
            };

            content.PackEnd(ellipseText3);
            content.PackEnd(ellipse3);
            content.PackEnd(aspectButton);
        }