SFML community forums

Help => Graphics => Topic started by: Bill Rogers on November 11, 2022, 06:01:05 am

Title: Highlight text while cursor over it
Post by: Bill Rogers on November 11, 2022, 06:01:05 am
I'd like to implement a feature where text is highlighted when the mouse is positioned over it. However the only way I can think to do it is to set the color when the cursor is inside the box then reset the color when not.

Text text;
text.setString("Test");
//do text setting stuff

while (window.isOpen)
{
     if (cursor positioned in text box) text.setFillColor(Color::Red);
     else text.setFillColor(Color::White);

//draw stuff
}
 

This works but now is setting the text color every frame. Is there a more efficient way to do this or is the performance hit small enough to where it doesn't matter?
Title: Re: Highlight text while cursor over it
Post by: eXpl0it3r on November 11, 2022, 08:10:47 am
You can separately track the state of the highlight.
If the cursor is no longer in the text box and the state was previously "highlighting" then change it, otherwise don't do anything.
Similarly, if the cursor is in the text box and the state was previously "highlighting" then you don't need to set the color again.

Depending on what you do, you may be interested in Hapax's recent utility function: https://en.sfml-dev.org/forums/index.php?topic=28777.0