SFML community forums

Help => Window => Topic started by: Daepilin on June 01, 2013, 01:46:55 pm

Title: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Daepilin on June 01, 2013, 01:46:55 pm
Hi :)

I'm quite new to SFML and atm trying to write myself a GUI for a game in want to make.

For that i need a checkbox, but that doesn't work the way i think it should.

my code:

Code: [Select]
if(mInFocus == true){
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
if(ev->MouseButtonReleased){
mState = !mState;
}
}
}

it keeps changing the mState Value when the button is pressed down and the mouse is moved in the checkbox area(mInFocus == true).

the button release seems to be ignored as long as there are other mouse events like the movement.

Any idea what i could do to prevent that from happening?
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Laurent on June 01, 2013, 01:48:36 pm
I'm sorry to say that, but your code is a total non-sense. You should really read the corresponding tutorials.
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Daepilin on June 01, 2013, 01:51:11 pm
actually i got the code from a tutorial( for a button):

void Button::HandleEvents(sf::Event *ev){
        if(GetActive() == true){
        //Is the Mouse above the button? if yes check if left button is down and release (also set the current animation id to the rigth value)
        if(mInFocus == true){
                if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){
                        SetNextAnimationID("Anim3");
                        if(ev->MouseButtonReleased){
                                SetClicked(true);
                                }
                }
                else{
                        SetClicked(false);
                        SetNextAnimationID("Anim2");
                }
        }
        else{
                SetClicked(false);
                SetNextAnimationID("Anim1");
        }
        }
}

that works, but when i try it like that and change mState outside of the event loop depending on the state of IsClicked() it won't work as well...

e: got it to work :) still wondering why it works for the button OO
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Laurent on June 01, 2013, 02:41:31 pm
Quote
actually i got the code from a tutorial
Which one? :o

This code is bad for at least two reasons:
- you mix events and real-time input states
- ev->MouseButtonReleased is a constant, your test always succeeds

If you had read the official tutorials you would know that, because it is clearly explained ;)
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Daepilin on June 01, 2013, 03:33:02 pm
well, thanks a lot for the explanation. It was some german youtube tutorial.

and btw: i don't think this( http://www.sfml-dev.org/tutorials/2.0/window-inputs.php) gives too much insight :P
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Laurent on June 01, 2013, 03:35:35 pm
Combined with this one (http://www.sfml-dev.org/tutorials/2.0/window-events.php), everything that you have to know is explained.
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Nexus on June 01, 2013, 03:39:25 pm
well, thanks a lot for the explanation. It was some german youtube tutorial.
Don't learn from YouTube tutorials. Almost all of them concerning C++ and SFML are inherently bad. You may achieve some results using them, but they teach bad practice and questionable code style. In the end, you had better learn directly from the official tutorials and come up with the functionality yourself.
Title: Re: Mouseclick event bugs out, when mouse is moved with pressed button
Post by: Daepilin on June 01, 2013, 03:43:06 pm
well thanks :)