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

Author Topic: [Solved] Check for sf::Text::Style  (Read 2300 times)

0 Members and 1 Guest are viewing this topic.

DasOhmoff San

  • Guest
[Solved] Check for sf::Text::Style
« on: February 17, 2015, 02:55:50 pm »
Hi,

I really want to know how to check for the Style of Text's.
This is not working:

if(sf_text.getStyle() == sf::Text::Bold)

I wanted to switch between bold, italic and underlined but I think there is a better way to do that than I do.
I am doing it like this:


//****Input was b, to switch Bold on or of****//

if(/*sf_text.getStyle() == sf::Text::Bold*/) //Bold -> change to regular
{
        sf_text.setStyle(sf::Text::Regular);
}
else if(/*sf_text.getStyle() == sf::Text::Bold | sf::Text::Italic*/) //Bold & italic -> only italic
{
        sf_text.setStyle(sf::Text::Italic);
}
else if(/*sf_text.getStyle() == sf::Text::Bold | sf::Text::Underlined*/) //Bold & underlined-> only underlined
{
        sf_text.setStyle(sf::Text::Underlined);
}
//Bold, italic & underlined -> only underlined and italic
else if(/*sf_text.getStyle() == sf::Text::Bold |  sf::Text::Italic | sf::Text::Underlined*/)
{
        sf_text.setStyle(sf::Text::Italic | sf::Text::Underlined);
}
 

As you can see this can't be the best way, what is the best way?

Sry for my bad english.
« Last Edit: February 17, 2015, 06:56:16 pm by DasOhmoff San »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Check for sf::Text::Style
« Reply #1 on: February 17, 2015, 03:09:46 pm »
text.setStyle(text.getStyle() & ~sf::Text::Bold); // removes bold style
Laurent Gomila - SFML developer

DasOhmoff San

  • Guest
Re: Check for sf::Text::Style
« Reply #2 on: February 17, 2015, 03:28:31 pm »
text.setStyle(text.getStyle() & ~sf::Text::Bold); // removes bold style

THANKS A LOT!

DasOhmoff San

  • Guest
Re: Check for sf::Text::Style
« Reply #3 on: February 17, 2015, 03:39:17 pm »
text.setStyle(text.getStyle() & ~sf::Text::Bold); // removes bold style

As I said already, thanks a lot, but how do I check the Status of the Style?

(text.getStyle() == sf::Text::Bold)

And if the Style got bold, italic and underlined, how to check if one of the Style options is for example bold?

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Check for sf::Text::Style
« Reply #4 on: February 17, 2015, 05:36:35 pm »
if (text.getStyle() & sf::Text::Bold == sf::Text::Bold)
 

That will return true if the style contains bold (Even if it is mixed with other styles).
« Last Edit: February 18, 2015, 07:35:44 am by Laurent »

DasOhmoff San

  • Guest
Re: Check for sf::Text::Style
« Reply #5 on: February 17, 2015, 06:55:09 pm »
if (text.getStyle() & sf::Text::Bold == sf::Text::Bold)
 

That will return true if the style contains bold (Even if it is mixed with other styles).

Thanks!
This really works for sf::Text::Bold, but doesn't for the other Styles.
However I understood how this is working! Thanks a lot!
« Last Edit: February 18, 2015, 07:35:32 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: [Solved] Check for sf::Text::Style
« Reply #6 on: February 17, 2015, 08:17:12 pm »
It also works for the other styles, of course you have to adapt the sf::Text::Bold on both sides. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: [Solved] Check for sf::Text::Style
« Reply #7 on: February 18, 2015, 01:17:15 am »
Oops I though [cpp] was a tag :S In any case, these are bitwise operators and they are very cool for things like this.

You should probably read up a little about them before doing any serious work though, they can be a little confusing at times.