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

Author Topic: Button class help  (Read 1230 times)

0 Members and 1 Guest are viewing this topic.

vams

  • Newbie
  • *
  • Posts: 8
    • View Profile
Button class help
« on: May 14, 2013, 05:27:47 pm »
Hi,
I want to create a button that "activates" has he get clicked on.
Here's the algorirm-implementing function of the class :

void reset(){activated = false;}

bool Button::isMouseHere(Window& window){
        double mouse_pos_x = Mouse::getPosition(window).x;
        double mouse_pos_y = Mouse::getPosition(window).y;

        return getGlobalBounds().contains(Vector2f(mouse_pos_x,mouse_pos_y));
}
bool Button::isClicked(Window& window){
        return (isMouseHere(window) && Mouse::isButtonPressed(Mouse::Left));
}

void Button::processEvent(const Event& event,Window& window) {
        if (isClicked(window)){
                if (!old_state) {
                        old_state = true;
                        changed++;
                }
        }
        else if (old_state) {
                changed++;
                old_state = false;
        }

        if (changed > 1) {
                activated = true;
                changed = 0;
        }
}
 

And here's the code in which it's used

while(window.isOpen()){
                while(window.pollEvent(event)){
                        if (event.type == Event::Closed) window.close();
                        button.processEvent(window,event);
                        if (button.active) {
                          cout << "button active " << endl; //some sort of output
                          button.reset() //optional, get the button possible to be re-activated
                }
        window.clear(Color::Black);
       
        window.display();
        }
}
 

Button reacts this way:
1) as you click in is area (and keep the button pressed) he doesn't anything (right)
2) as you release the button it starts yelling "button activated" (wrong, should just once)
3) as you leave his area with mouse the button stop yelling.
4) you can repeat steps 1,2,3 as many time u want.

Any ideas?
« Last Edit: May 14, 2013, 05:30:44 pm by vams »

vams

  • Newbie
  • *
  • Posts: 8
    • View Profile
[SOLVED] Re: Button class help
« Reply #1 on: May 14, 2013, 05:39:55 pm »
Ok i got the problem by copying here the code.
As it is here works but i had
while(window.isOpen()){
        while(window.pollEvent(event)){
            if (event.type == Event::Closed) window.close();
            button.processEvent(window,event);
                        if (button.active) {
                         
                         
        }
    window.clear(Color::Black);
    cout << "button active " << endl; //some sort of output
    button.reset() //optional, get the button possible to be re-activated
    window.display();
    }
}

with the "cout " outside the pollEvent loop