Okay. So we're ready to start module eight, where we finally start getting the user input in our XNA games. Here are the learning objectives for the module. So as you can see, we're going to learn how to use both the mouse and the controller as input devices in our XNA games. In this lecture, were going to focus on using mouse input. And in particular, the mouse location input in our games. And over the course of the next few lectures in the module, we'll learn how to use mouse buttons and controller thumb sticks, and buttons, and so on. So you should start with an in-lecture quiz, telling me how you feel about using mouse input in games. I've already created a Windows game project that we'll use to start doing the mouse input, so I'm going to open that up now. So I've done some preliminary stuff. Here's the idea behind this game we're building today. We're going to generate a character, a texture 2D, and we're going to move that character around the screen by dragging the mouse around the screen. And we'll use this as the basis for later projects in later lectures as well. So, as you can see, oops. As you can see if I zoom in, I have the window within window height. And down here in the constructor, I have set the preferred back buffer with that height to those, and I have also said that I want the mouse to be visible. If you are using the mouse for input, you really want the mouse to be visible, otherwise the player has no idea where it is in the game window. You can certainly build a customized mouse cursor if you want, but I'm just going to use the standard arrow cursor here. We have our standard, we've got a texture 2D we're going to draw and a rectangle in which we're going to draw it. I've called the texture 2D current character, because in fact we're going to let the user click between four different characters as they play. Not today. Today they'll just have the character follow the mouse around, but next time, they'll be able to click and change it. Planning ahead, I've included a random number generation object that we'll use to randomly pick the next character when they click. And I have four different character sprites. So, when we go down to load content, you can see that I have loaded all four different characters, because I have them over here. So I've loaded those just like we typically load texture 2Ds. And I've said we're going to start at character0. And we'll set that character into the center of the window. So, this part says the current character will be character0. And this next part says, the draw rectangle for that character is located at the horizontal center of the window minus half the character width. At the vertical center of the window, minus half the character height. And we're drawing actual size. So we're using the width and height properties of the Texture2D object, the current character variable, to set the width and height of the drawRectangle. So when I run this right now, I get a character drawn in the center of the screen. And by the way, this is one of the characters from the Colorado History Arcade game that [INAUDIBLE] Game Studios built. So, we should go back to the slides so that we can talk before we start doing our update stuff. I'll move to the update method. We're going to make the character follow the mouse. And the way that we're going to make the character follow the mouse, is we're going to grab the current mouse state and use information contained in that mouse state to make the character follow it. So go do the lecture quiz about a particular method we're going to use and then come on that. Okay, so we are going to now use the mouse state, the current mouse state to move around. So I want to make the character follow the mouse. Now what I need to do is I need to extract the current mouse state. And hopefully you realize from the in-lecture quiz that the Mouse.GetState method returns a mouse state to us. So I'll call this mouse. And I'm calling the Mouse.GetState method. So the Mouse.GetState method returned the current state of the mouse. It returns a MouseState object. So we should go take a look at the help to see what that Mouse.State object really has in it. And as we can see, there are a number of interesting properties that we have available to us. So we have Left button, Middle button, Right button, we can get the status or the state of those buttons. We have the scroll wheel value, if you happen to have a mouse with a scroll wheel. We can get an X and Y. The horizontal and vertical position of the mouse. And x button 1 and x button 2, in case you have one of those fancy mice that actually has a couple of buttons on the left hand side. So that you can go back in your browser. Which by the way, if you use that twice, you feel like you can never live without it again. So, let's actually take a look at, at least at first. I guess we'll come back to the buttons later on. So for now, let's look at the XNY because those are going to be very useful to us. So I'll click on this property, and it says okay, it's an int. So it's going to tell me basically which pixel location in my game window the mouse is located at. And that's going to be useful to us here because what we want to do is we want to adjust the drawRectangle so that the drawRectangle is aligned. Let's center it on the mouse location. So what that means is we need to take the drawRectangle and modify its X. And we modify its X by saying, well, we'll make it mouse.X, which would anchor the drawRectangle, the upper left hand corner of the drawRectangle, on the mouse.X position. And then we'll subtract half of the width of the currentCharacter. And that will make the characters stay centered horizontally on the mouse location. And we'll do something very similar for height. So now, when we run our program, we see that the character is following the mouse around. Now it turns out that this is probably bad, letting the player drag the character outside the screen. We probably should not let them do that. So let's do what's called clamping the character inside the window. So the term clamping means, if in fact your particular value is outside its valid range, we clamp it. We set it to either the minimum or the maximum, whichever side it's outside. So, we're actually going to clamp the character inside the window. So to do so, we have to check four different things. And we've actually seen this with the teddy bear bouncing left and right. We're going to do it again here, but we'll do it from scratch. Just as a reminder. So, let's say, we want to check if the drawRectangle.Left. So we'll check if the left part of the drawRectangle is less than zero. Which means that at least part of the drawRectangle is outside the window. If that's the case, we'll set drawRectangle.X equal to zero. We will clamp. We will make the drawRectangle stay at the left side of the window. We'll check the right-hand side of the drawRectangle as well. So, if that's greater than WINDOW_WIDTH. So, here's another great benefit of using WINDOW_WIDTH as a constant, rather than hard coating it in multiple places. Because this code will continue to work even if we change the resolution. So, now it's outside the right-hand side. We still can only change X. We, it might be kind of interesting actually to change the width of the drawRectangle instead to sort of squish the character against the right-hand side. And you can experiment with that if you'd like to. But I'm going to shift the drawRectangle left again so that the entire drawRectangle still stays in the window. So, to do that, I have to start at window width, which would put the drawRectangle totally outside on the right, and then back it up to the left the width of the drawRectangle. And that will clamp. Let's go ahead and check clamping left and right. I'm trying to go past on the right and I can't. I'm trying to go past on the left and I can't. Moving up and down I'm still not clamped. So we'll do that now. But it's reasonable to write a few lines of code and test before moving on. So, now I need to clamp top and bottom. So if the top is less than zero, I'll just set Y to be zero not 90. That would look kind of weird. And if the bottom is greater than the height, then I will do a similar thing to what I did on the right. I'll start at the height, which puts it all the way outside the window. And then I'll subtract the height of the current character. So now, the character stays clamped inside the window. And I can move around in the window, it follows the mouse. But it stays clamped in the window. So, you should go do an in-lecture quiz. And by the way, that's all the coding we'll be doing for today. But go do the in-lecture quiz about clamping, and now we can recap. And this is our first step in using user input inside our XNA games and we started with the mouse, obviously. We learned how we can use the mouse location to move something around on the screen. And we also learned that clamping is a valuable technique that we can use to, to keep objects or game objects, game entities, inside particular boundaries. Next time we'll continue working with the mouse, although now we'll start working with the mouse button.