The basic idea is to take a picture, and if the cursor is over the picture, allow it to be dragged, but have it drag in relation to where the mouse is.
For instance, if I dragged it from the bottom right corner, it won't snap to the top left corner.
I've gotten it down, and it works, but the code is horribly messy, and not just because of my general "I'm doing this to try it out, who cares what it looks like", and I was wondering if there was a better way of doing it.
This is the area where the dragging portion of the code is. If you're wondering, it's C# and XNA 3.1
Code:
//dummy function protected void UpdateMouse() { MouseState mouse = Mouse.GetState();//Mouse functions/vars if (!isDown && mouse.RightButton == ButtonState.Pressed)//If the right mouse button was previously not pressed and now is... { this.IsMouseVisible = false;//Make it invisible, so we know if ((mouse.X > spritevect.X && mouse.X < spritevect.X + spritetext.Width) && (mouse.Y > spritevect.Y && mouse.Y < spritevect.Y + spritetext.Height))//<-- is what I'm curious about. {//The if statement just checks to make sure that the mouse is within the box xoffset = mouse.X - spritevect.X;//Find the offset yoffset = mouse.Y - spritevect.Y; isDragging = true;//If you move the mouse too fast, it will sometimes move it out of the box before the box can move. This is my "Fix". } isDown = true;//The left mouse is now down, for later uses } else if (isDown && mouse.RightButton == ButtonState.Released)//If the right mouse button was previously pressed and now isn't { this.IsMouseVisible = true;//Make it visible isDown = false;//No longer pressed isDragging = false;//We're no longer dragging. } else if (isDown) { if (((mouse.X > spritevect.X && mouse.X < spritevect.X + spritetext.Width) && (mouse.Y > spritevect.Y && mouse.Y < spritevect.Y + spritetext.Height)) || isDragging) {//Same concept as above, except now if we're dragging it'll still come in here spritevect.X = mouse.X - xoffset;//Move it spritevect.Y = mouse.Y - yoffset; } }