Ejemplo n.º 1
0
        /// <summary>
        /// Scene redrawing
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void screen_Paint(object sender, PaintEventArgs e)
        {
            // Buffer creation, initialization
            Ruler     sizes             = new Ruler(screen);
            Rectangle board_coordinates = sizes.GetBoardCoordinates();
            Image     gfx = new Bitmap(board_coordinates.Width, board_coordinates.Height);
            Graphics  g   = Graphics.FromImage(gfx);

            // Draw all the checkers
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (_state.checkers[j, i] != null)
                    {
                        // Drawing static checkers
                        if (_state.checkers[j, i] != _mouseHandler._drag)
                        {
                            g.DrawImage(_state.checkers[j, i].img, _state.checkers[j, i].rect);
                        }
                    }
                }
            }

            // Drawing moving checker
            if (_mouseHandler._drag != null)
            {
                g.DrawImage(_mouseHandler._drag.img, _mouseHandler._drag.rect);
            }

            // Quickly draw a scene
            g.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);
            e.Graphics.DrawImage(gfx, board_coordinates);
            e.Graphics.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);
        }
        /// <summary>
        /// Handler event for moving the mouse
        /// </summary>
        /// <param name="e">Mouse options</param>
        public void MouseMove(MouseEventArgs e)
        {
            // Checker is not selected
            if (_drag == null)
            {
                return;
            }

            // Determining the visual characteristics of a dragging checker
            Ruler     ruler       = new Ruler(_screen);
            Rectangle board_coord = ruler.GetBoardCoordinates();
            Point     click_coord = new Point(e.X - board_coord.X, e.Y - board_coord.Y);

            _drag.rect.X = click_coord.X - _drag.rect.Width / 2;
            _drag.rect.Y = click_coord.Y - _drag.rect.Height / 2;
            return;
        }
        /// <summary>
        /// User-click event handler
        /// </summary>
        /// <param name="e">Mouse options</param>
        public void MouseDown(MouseEventArgs e)
        {
            // Definition of checkers under the cursor at the moment of pressing
            Ruler     ruler                = new Ruler(_screen);
            Rectangle board_coord          = ruler.GetBoardCoordinates();
            Point     click_coord          = new Point(e.X - board_coord.X, e.Y - board_coord.Y);
            Point?    checker_intersection = ruler.GetCellIntersection(e.Location);

            // Determining the selected checker and its visual characteristics
            if (!checker_intersection.HasValue)
            {
                return;
            }
            _clicked = checker_intersection;
            Rectangle checker_rect = ruler.GetCellCoordinates(checker_intersection.Value);

            // There are no checkers under the cursor
            if (_clicked == null)
            {
                return;
            }

            // Enemy checker is selected - the move is impossible
            if (_state.checkers[_clicked.Value.X, _clicked.Value.Y] != null && _state.checkers[_clicked.Value.X, _clicked.Value.Y].belong_to != _state.turn)
            {
                ShowIncorrectTurnMessage(Reason.EnemyChecker);
                return;
            }

            // Expected move continuation, another checker move is impossible.
            if (_continious != null && _clicked != _continious)
            {
                ShowIncorrectTurnMessage(Reason.TurnContinuationExpected);
                return;
            }

            // Checker selection
            _drag = _state.checkers[_clicked.Value.X, _clicked.Value.Y];
        }