I am trying to do a similar thing with my project. I think it has something to do with the sf::Rect class and the Contains function, but the documentation I have been able to find on it isnt very helpful.
EDIT: Perhaps the below code will help people in trying to figure out what we are trying to do. There are no problems with compiling it (using SFML 1.6), but when the .exe is run, the window closes as soon as the cursor enters it.
EDIT #2: Lol, nevermind. I figured it out. Just had my code in the wrong place. Below is the code for a simple button click if anyone wants it.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(400, 300), "Button Test");
sf::Image Button1Img;
if (!Button1Img.LoadFromFile("button1.png"))
return EXIT_FAILURE;
sf::Sprite Button1Obj(Button1Img);
sf::String Text1;
Text1.SetText("FAILURE");
Text1.SetPosition(0, 100);
float XPos = Button1Obj.GetPosition().x;//the x position of the button
float YPos = Button1Obj.GetPosition().y;//the y position of the button
float XSize = Button1Obj.GetSize().x;//the width of the button
float YSize = Button1Obj.GetSize().y;//the height of the button
float MouseX = App.GetInput().GetMouseX();//the x coord of the mouse when clicked
float MouseY = App.GetInput().GetMouseY();//the y coord of the mouse when clicked
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
if (App.GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
MouseX = App.GetInput().GetMouseX();
MouseY = App.GetInput().GetMouseY();
if((MouseX >= XPos && MouseX <= XPos+XSize && MouseY >= YPos && MouseY <= YPos+YSize) == true) {
Text1.SetText("SUCCESS");
}}
}
App.Clear();
App.Draw(Button1Obj);
App.Draw(Text1);
App.Display();
}
return EXIT_SUCCESS;
}