I just successfully implemented some simple mouse movement detection thanks to an old post I found here
http://www.gamedev.net/topic/619329-help-with-making-sfml-buttons/ I have some questions about the code provided though
The text changes to cyan when you put your mouse over it, the next step for me will be to make something happen when I click on it, like switching over to a different line of text or putting up a picture but that's for later.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "");
sf::Font arial;
if ( !arial.loadFromFile ( "arial.ttf" ) )
{ }
sf::Text FontOne;
FontOne.setFont ( arial );
FontOne.setCharacterSize ( 12 );
FontOne.setColor ( sf::Color::Blue );
FontOne.setPosition ( 350, 120 );
FontOne.setString ( "This is line one!" );
sf::Text FontTwo;
FontTwo.setFont ( arial );
FontTwo.setCharacterSize ( 12 );
FontTwo.setColor ( sf::Color::Green );
FontTwo.setPosition ( 350, 240 );
FontTwo.setString ( "This is line two!" );
sf::Text FontThree;
FontThree.setFont ( arial );
FontThree.setCharacterSize ( 12 );
FontThree.setColor ( sf::Color::Red );
FontThree.setPosition ( 350, 360 );
FontThree.setString ( "This is line three!" );
sf::FloatRect FirstButton ( 350, 120, 1, 1 );
sf::FloatRect SecondButton ( 350, 240, 1, 1 );
sf::FloatRect ThirdButton ( 350, 360, 1, 1 );
FirstButton = FontOne.getGlobalBounds();
SecondButton = FontTwo.getGlobalBounds();
ThirdButton = FontThree.getGlobalBounds();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if ( FirstButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )
{
FontOne.setColor ( sf::Color::Cyan );
}
else
{
FontOne.setColor ( sf::Color::Blue );
}
if ( SecondButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )
{
FontTwo.setColor ( sf::Color::Cyan );
}
else
{
FontTwo.setColor ( sf::Color::Green );
}
if ( ThirdButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )
{
FontThree.setColor ( sf::Color::Cyan );
}
else
{
FontThree.setColor ( sf::Color::Red );
}
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw ( FontOne );
window.draw ( FontTwo );
window.draw ( FontThree );
window.display();
}
return 0;
}
What I wanted to know is that I found my code worked fine after I deleted:
else if(Event.Type == Event.MouseMoveEvent)
Is this because the code is old and it's something to do with an earlier version of SFML? I don't see any reason for it being there, I'd appreciate an explanation on that one because while I did find the problem I'm still a bit baffled as to what that line is supposed to do compared to the other stuff even though I understood the individual bits. Is it actually necessary to be there for a particular reason? Have I just made it more difficult for me to do something else later on if I delete it?
Secondly I wanted to know if it was possible to make the FloatRect visible so having some kind of block representing it so you knew that it went in correctly because at the moment when it comes to the way I'm doing it now it feels like I'm just guessing where a specific FloatRect is rather than actually knowing how wide it is etc. which will become a problem if I'm making a more complicated UI which I almost certainly will be.