Exemplo n.º 1
0
        protected override void OnPaint(PaintEventArgs pe)
        {
            EnsureRender();

            var g = pe.Graphics;

            g.Clear(Color.Gray);

            // draw the board in the center
            var renderPos = this.RenderPos;

            g.DrawImage(_render, renderPos.X, renderPos.Y);

            // draw carry stack
            if (_carryPos.HasValue && _carryVisible && _carryStack.Count > 0)
            {
                var stackBottom = new PointF(_carryPos.Value.X, _carryPos.Value.Y);
                foreach (var pieceID in _carryStack)
                {
                    var sprite = IsoImages.GetPieceImage(pieceID);
                    var rect   = GetBottomedRect(stackBottom, sprite.Size);
                    g.DrawImage(sprite, rect.Left, rect.Top);
                    stackBottom.Y -= IsoGeometry.FlatStone.Height;
                }
            }

            base.OnPaint(pe);
        }
Exemplo n.º 2
0
 void InitButtonImages()
 {
     foreach (var tool in _tools)
     {
         tool.Btn.Image = IsoImages.GetPieceImage(tool.PieceID);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Redraws the off-screen board render as needed
        /// </summary>
        void EnsureRender()
        {
            if (!_renderValid)
            {
                // Clear layout information every time we redraw
                _layoutItems.Clear();
                int gameSize = _game != null ? _game.Size : 5;

                // Prepare to draw game board
                var board = IsoImages.GetBoardImage(gameSize);

                if (_render != null && _render.Width != board.Width)
                {
                    _render.Dispose();
                    _render = null;
                }

                if (_render == null)
                {
                    // allocate enough extra vertical space to hold some tall stacks of stones
                    _render = new Bitmap(board.Width, board.Height + 120, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                }

                using (var g = Graphics.FromImage(_render))
                {
                    // Draw the game board on a transparent background
                    g.Clear(Color.Transparent);
                    g.DrawImage(board, 0, _render.Height - board.Height);

                    // Draw game pieces
                    if (_game != null)
                    {
                        var           boardul = new PointF(0, _render.Height - board.Height);
                        var           mask    = IsoImages.GetSquareMaskImage();
                        BoardPosition viewPos;

                        // Draw board pieces from back to front, bottom to top
                        var blSquareCenter = IsoGeometry.Board.BottomLeftSquare(gameSize);
                        for (viewPos.Y = gameSize - 1; viewPos.Y >= 0; viewPos.Y--)
                        {
                            for (viewPos.X = 0; viewPos.X < gameSize; viewPos.X++)
                            {
                                // Calculate center position for this square
                                var squareCenter = new PointF(
                                    boardul.X + blSquareCenter.X + IsoGeometry.Board.Square.XDelta.X * viewPos.X + IsoGeometry.Board.Square.YDelta.X * viewPos.Y,
                                    boardul.Y + blSquareCenter.Y + IsoGeometry.Board.Square.XDelta.Y * viewPos.X + IsoGeometry.Board.Square.YDelta.Y * viewPos.Y);

                                var pos = GetRotated(gameSize, viewPos, _rot);

                                // square mask goes at the bottom of the stack
                                var rect = GetCenteredRect(squareCenter, mask.Size);
                                _layoutItems.Add(new RenderLayoutItem(pos, null, rect, mask));

                                bool highlighting = false;
                                int? height       = null;
                                if (_highlights.TryGetValue(pos, out height))
                                {
                                    highlighting = true;
                                }

                                if (highlighting && !height.HasValue)
                                {
                                    // highlight square
                                    g.DrawImage(mask, rect.Left, rect.Top);
                                }

                                // draw pieces from bottom to top
                                var stack = _game.Board[pos.X, pos.Y];
                                for (int i = 0; i < stack.Count; i++)
                                {
                                    var piece  = stack[i];
                                    var sprite = IsoImages.GetPieceImage(piece, highlighted: highlighting && height.HasValue && i >= height.Value);
                                    rect = GetBottomedRect(squareCenter, sprite.Size);
                                    g.DrawImage(sprite, rect.Left, rect.Top);
                                    _layoutItems.Add(new RenderLayoutItem(pos, i, rect, sprite)); // remember layout information for mouse hit test
                                    squareCenter.Y -= IsoGeometry.FlatStone.Height;
                                }
                            }
                        }
                    }
                }

                // Reverse the order of sprites because the last ones drawn are actually in front, and should be tested first
                // when trying to determine which piece the user has clicked on
                _layoutItems.Reverse();
                _renderValid = true;
            }
        }