Trying to make buttons using SFML for Comp Sci final and really don't want to draw invisible sprites over every single button.
I found some solutions but they were all using older versions of sfml and those functions have since been removed or changed and not sure what they've been changed to.
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
switch(event.type)
{
case Event::Closed:
window.close();
cout << "Window Closed!" << endl;
break;
case Event::MouseButtonPressed:
if(event.mouseButton.button == Mouse::Left){
cout << " if(event.mouseButton.button == Mouse::Left){" << endl;
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
cout << "This works!" << endl;
}
}
default:
break;
}
}
}
cout << " if(event.mouseButton.button == Mouse::Left){" << endl; was just to test how far into the loop it got.
getLocalBounds
returns the bounds in the local coordinates of the text. You need to use getGlobalBounds
to get it in world coordinates.
You also need to use the mapPixelToCoords
method of your window to transform the coordinates of the mouse also to world coordinates.
It would be something like this:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}