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

Author Topic: Mouseclick event bugs out, when mouse is moved with pressed button  (Read 2647 times)

0 Members and 1 Guest are viewing this topic.

Daepilin

  • Newbie
  • *
  • Posts: 10
    • View Profile
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?
« Last Edit: June 01, 2013, 01:49:49 pm by Daepilin »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #1 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.
Laurent Gomila - SFML developer

Daepilin

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #2 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
« Last Edit: June 01, 2013, 02:40:04 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #3 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 ;)
Laurent Gomila - SFML developer

Daepilin

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #4 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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #5 on: June 01, 2013, 03:35:35 pm »
Combined with this one, everything that you have to know is explained.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #6 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Daepilin

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mouseclick event bugs out, when mouse is moved with pressed button
« Reply #7 on: June 01, 2013, 03:43:06 pm »
well thanks :)

 

anything