Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving one shape over another using the mouse  (Read 2703 times)

0 Members and 1 Guest are viewing this topic.

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Moving one shape over another using the mouse
« on: March 17, 2012, 12:17:06 am »
I have two circle shapes on the screen, and when the left mouse button is held down, and the mouse cursor is over one of the circles, it should "pick it up" and the circle follows the mouse cursor until the left mouse button is released.
There is an issue when I pick up one of the circles, and move it over the other, because then this causes the second shape to be picked up also and I cannot separate them since they are on top of each other.

Here is my input code for the circles:
 
Code: [Select]
if ( (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
    && (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
 && (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}


This picks up the circle fine but causes the issue mentioned above :(

I did try to add a bool so it would only pick it up if it didn't have one picked up already, and here is the code for that:

Code: [Select]
bool nodeGrabbed = false;
if ( (nodeGrabbed == false) && (sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left)) && (sf::Mouse::GetPosition(App).x > circle.GetPosition().x - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).x < circle.GetPosition().x + circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y > circle.GetPosition().y - circle.GetRadius())
&& (sf::Mouse::GetPosition(App).y < circle.GetPosition().y + circle.GetRadius()) )
{
nodeGrabbed = true;
circle.SetPosition(sf::Mouse::GetPosition(App).x, sf::Mouse::GetPosition(App).y);
}
else
{
nodeGrabbed = false;
}


But this still did not work :(

Maybe I should mention that each shape is in a class and the two different shapes I have created are in a vector, and I go through this vector to check for user input, so maybe this has something to do with it as it just gets the input for both at the same time?

Any help would be appreciated

SoulBlade

  • Newbie
  • *
  • Posts: 26
    • View Profile
Moving one shape over another using the mouse
« Reply #1 on: March 17, 2012, 12:28:46 am »
There are a few problems I can see.

First, define your nodeGrabbed only 1 time.  Either put it in a class, then set its initial value in the constructor, or put it at the very beginning of your program before you execute the main loop.  

You also need to add more conditions to the "else."

In the else condition, just add:

if (!(sf::Mouse::IsButtonPressed(sf::Mouse::Button::Left))) {

nodeGrabbed = false;

}

Those changes should make what you're trying to do work properly.

Kiblinix

  • Newbie
  • *
  • Posts: 15
    • View Profile
Moving one shape over another using the mouse
« Reply #2 on: March 17, 2012, 12:45:12 am »
For some reason adding that second part in the else condition causes me to not be able to drag the circles (Reason which is unknown to me :P)

I can click on a part of the circle and it moves slightly, but no dragging.

SoulBlade

  • Newbie
  • *
  • Posts: 26
    • View Profile
Moving one shape over another using the mouse
« Reply #3 on: March 17, 2012, 07:25:34 am »
Quote from: "Kiblinix"
For some reason adding that second part in the else condition causes me to not be able to drag the circles (Reason which is unknown to me :P)

I can click on a part of the circle and it moves slightly, but no dragging.


Just look through the logic step by step.  The reason it disabled dragging was because it no longer updates the movement until you release the mouse key.  All you need to do is make it so it remembers which shape you're dragging (with a pointer, or integer indexing, or something) and just update the position every frame.  

If I was to do it, I would just have a pointer pointing to the object.  When you have the mouse down, if the pointer is NULL, set it to the objects address.  Every frame, if the mouse is still down and the pointer is not NULL, update that objects position.  When the mouse is release, if the pointer has an address assigned to it, set it to NULL.