Jump to content
  • Advertisement
Sign in to follow this  
SoulHeart

SFML 1.6- Help With Text Buttons

This topic is 2520 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to display text(fonts, size, position, color) but I was wondering how to make certain text a button. Also how could I make the text button light up when the cursor is over it.

Share this post


Link to post
Share on other sites
Advertisement
Well, if you can:
1) Draw text
2) Get the position of the mouse

...then you have all you need to put the two together. This is what programming is about: not the using of what is already existing, but the remixing of those separate pieces to accomplish your task. smile.png

In psuedocode:

If ( MousePosition <is within> ButtonRectangle )
{
DrawText(HighlightedColor)
}
Else
{
DrawText(NormalColor)
}


If you need more help than that, let us know.

You basically do the same thing to find out if the button was clicked or not.


OnMouseClick()
{
If ( MousePosition <is within> ButtonRectangle )
{
//The button was clicked.
}
Else
{
//The button wasn't clicked.
}
}


Finding if a point is within a rectangle is easy:

If(point.x > rectangle.x && point.x < (rectangle.x + rectangle.width))
{
//I'm horizontally within the rectangle.
If(point.y > rectangle.y && point.y < (rectangle.y + rectangle.height))
{
//I'm also vertically within the rectangle.

//If I'm both horizontally AND vertically within the rectangle, then I'm inside the rectangle fully.
}
}

Share this post


Link to post
Share on other sites
Can you explain the if statement of the mouse being within a point on the screen a little more. I Don't quite know how to make it work.

Share this post


Link to post
Share on other sites
Using SFML specifically:

You have the [color=#daa520]position of the mouse... (two floats, one for X and one for Y)
...which you got from the sf::Input device with sf::Input::GetMouseX/Y()...
...which you got from your sf::Window (or sf::RenderWindow) with sf::Window::GetInput().

You have the [color=#daa520]rectangle of your text... (an sf::FloatRect)
...which you got from your sf::String with sf::String::GetRect().

You can check if the [color=#daa520]position of the mouse is within your [color=#daa520]rectangle by using sf::FloatRect::Contains().
...and you can use it to color your text with sf::String::SetColor() before you draw it with sf::RenderWindow::Draw().

Share this post


Link to post
Share on other sites
I gave you all the lego blocks necessary for what you are building, I don't want to snap the pieces together to build your lego building for you, otherwise you're just watching me play legos. wink.png

You already know how to draw text... you said so in your first post. Show your code that draws text before we go any further.

Share this post


Link to post
Share on other sites

sf::String MenuPlay;
MenuPlay.SetText("Play");
MenuPlay.SetFont(MenuOptions);
MenuPlay.SetSize(75U);
MenuPlay.SetColor(sf::Color(150, 50, 50));
MenuPlay.SetPosition(550, 200);

Game.Draw(MenuPlay);

Share this post


Link to post
Share on other sites
Awesome. And here's code that uses sf::String::GetRect().


sf::FloatRect textRectangle = MenuPlay.GetRect();

if(textRectangle.Contains(mouseX, mouseY) == true)
{
MenuPlay.SetColor(...highlighted color...);
}
else
{
MenuPlay.SetColor(...normal color...);
}


Every time you want to draw MenuPlay, first you check whether it's within the rect, so you can set the proper color. And after that, then you draw it.

Share this post


Link to post
Share on other sites
i got it to work like this...

sf::FloatRect MenuPlayRectangle = MenuPlay.GetRect();
if(MenuPlayRectangle.Contains(Event.MouseMove.X, Event.MouseMove.Y) == true)
{
MenuPlay.SetColor(sf::Color(250, 20, 20));
Game.Draw(MenuPlay);
}
else
{
MenuPlay.SetColor(sf::Color(150, 50, 50));
Game.Draw(MenuPlay);
}


thanks for your help

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!