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?