Time for action – handling mouse input
Add the
HandleMouseInput()
helper method to theGame1
class:Private Sub HandleMouseInput(mouseInfo As MouseState) Dim x, y As Integer x = mouseInfo.X - CInt(gameBoardDisplayOrigin.X) y = mouseInfo.Y - CInt(gameBoardDisplayOrigin.Y) x = x \ GamePiece.PieceWidth y = y \ GamePiece.PieceHeight If (x >= 0) And (x <= GameBoard.GameBoardWidth) And (y >= 0) And (y <= GameBoard.GameBoardHeight) Then If mouseInfo.LeftButton = ButtonState.Pressed Then _gameBoard.RotatePiece(x, y, False) timeSinceLastInput = 0 End If If mouseInfo.RightButton = ButtonState.Pressed Then _gameBoard.RotatePiece(x, y, True) timeSinceLastInput = 0 End If End If End Sub
What just happened?
The MouseState
class reports the X
and Y
position of the mouse relative to the upper-left corner of the window. What we really need to know is what square on the game board the mouse was over.
We break the mouse position into X
and...