-
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
window.clear(Color(210, 180, 140));
window.draw(titleText);
window.draw(equationsButtonText);
window.draw(variablesButtonText);
window.draw(onlineResourcesButtonText);
window.draw(unitsButtonText);
window.display();
switch(event.type)
{
case Event::Closed:
window.close();
break;
case Event::KeyPressed:
if(event.type == Event::MouseButtonPressed){
if(event.mouseButton.button == Mouse::Left){
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
cout << "This works!" << endl;
}
}
}
default:
break;
}
}
}
Not sure what part of this is wrong but clicking on the text doesn't work and im not entirely sure why. I'm thinking getLocalBounds doesn't work for text objects but really don't want to draw invisible sprites over each button unless theres a faster way to do that.
-
MouseButtonPressed is the event type you want, KeyPressed is for keyboard keys. Also, you should not have clearing, drawing and window display inside the event loop.
-
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
switch(event.type)
{
case Event::Closed:
window.close();
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;
}
}
}
Okay yeah I just fixed the KeyPressed before I checked the post, it gets to here:
> cout << " if(event.mouseButton.button == Mouse::Left){" << endl; (Testing to see how far it goes)
and im assuming the if statement just isnt valid at that point
-
Are you using a view? If so, you should probably convert the co-ordinates. Can be best to do so anyway, just to be safe:
https://www.sfml-dev.org/tutorials/2.5/graphics-view.php#coordinates-conversions
The local bounding rectangle will be an sf::FloatRect and the co-ordinates you are providing to "contains" are ints. I'm not 100% sure but I don't think these can be implicit. Try converting to floats or passing them to an sf::Vector2f.
-
Forgot to send myself the .cpp file from school but no I'm not using a view. So ill check that tomorrow
And when I used getGlobalBounds I believe it gave me a FloatRect error and when i tried
cout << equationsButtonText.getLocalBounds(); to see what it was puting in for the coords it gave me a FloatRect error.
-
You can't output a float rect to a steam. You can output their components individually, however e.g. top, left, width, height.
However, since the code you provided isn't working (does it fail to compile?), instead of:
if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y))
try
if(equationsButtonText.getLocalBounds().contains(sf::Vector2f(event.mouseButton.x, event.mouseButton.y)))
It would be helpful if you provided the errors that you are getting.
-
mapPixelToCoords worked even though im not using a view. I don't know why it works but it does. Thanks!
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y})
-
The window comes with a default view.
-
Just to clarify slightly, the window's default view matches the dimensions of the window in pixels. The view no longer aligns with the pixels if the window is resized, which I presume you must have done.
If you want your view to continue matching the window's size, you should use the part above the co-ordinates conversion (https://www.sfml-dev.org/tutorials/2.5/graphics-view.php#showing-more-when-the-window-is-resized).