Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Highlight text while cursor over it  (Read 548 times)

0 Members and 1 Guest are viewing this topic.

Bill Rogers

  • Newbie
  • *
  • Posts: 1
    • View Profile
Highlight text while cursor over it
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Re: Highlight text while cursor over it
« Reply #1 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything