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

Author Topic: New RichText class  (Read 31492 times)

0 Members and 2 Guests are viewing this topic.

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
New RichText class
« on: February 28, 2013, 07:45:39 pm »
I've been looking around for a way to combine multiple colors and styles in a single sf::Text.  This class came pretty close, but it doesn't have newline support, which is kind of a deal breaker, so I wrote my own. You can find it here.

The interface is a direct copy of sf::Text (except for findCharacterPos; I'm not sure how to do this at the moment). It's a drawable and transformable, so you can use it just like any other class.

Currently it has a minimal markup parser built in with support for colors and bold, italic, and underlined text. You can use the built-in color names (includes all the ones that are predefined in sf::Color) or HTML-style hex color codes, and define your own color names as well. The parser could probably use some memory optimization, but it only runs when you set the text, and runs quickly at that.

And here's a screenshot, because everyone loves them.  ;D


« Last Edit: February 28, 2013, 09:42:16 pm by TechRogue »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: New RichText class
« Reply #1 on: February 28, 2013, 07:53:20 pm »
Sounds and looks great, thanks for the effort! :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Re: New RichText class
« Reply #2 on: February 28, 2013, 09:54:52 pm »
That's perfect!

I'm sorry I didn't work to fix the newline bug... I have other projects, and uni (and a job ~~).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: New RichText class
« Reply #3 on: March 01, 2013, 05:43:43 am »
I just looked quickly through code but I think you made it impossible to have \ in your text(no escaping \ by writing it as \\). And it's c++11 enough to not work with vs 10.
« Last Edit: March 01, 2013, 05:52:55 am by FRex »
Back to C++ gamedev with SFML in May 2023

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: New RichText class
« Reply #4 on: March 01, 2013, 02:58:45 pm »
Quote
I'm sorry I didn't work to fix the newline bug... I have other projects, and uni (and a job ~~).

Hey, no worries! Your class was a good inspiration for me. I doubt I would have thought of storing separate text instances on my own.

Quote
I just looked quickly through code but I think you made it impossible to have \ in your text(no escaping \ by writing it as \\). And it's c++11 enough to not work with vs 10.

Yes, it does require C++11. I forgot to mention that. The changes to make it C++98 compatible should be pretty minimal though.
The most recent revision doesn't require a C++11 compatible compiler any more (as painful as it was to give up nullptr).

Nice catch on the backslash thing! I'll fix that right away.
« Last Edit: March 01, 2013, 03:51:01 pm by TechRogue »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: New RichText class
« Reply #5 on: March 01, 2013, 04:39:32 pm »
Quote
And it's c++11 enough to not work with vs 10.
(as painful as it was to give up nullptr).
VS10 supports nullptr, so no need to remove it. ;)
Not sure what FRex noticed that doesn't work with VS10.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: New RichText class
« Reply #6 on: March 01, 2013, 04:43:23 pm »
I used to have auto declarations, initialization lists and a lambda function. I imagine most of those wouldn't be supported.

Quote
VS10 supports nullptr, so no need to remove it. ;)

Oh, I didn't know that. I just ended up doing this:

#ifndef nullptr
#define nullptr NULL
#endif

It's probably a horrible hack.  ;D Anyway, it compiles with MinGW without -std=c++0x, so it should run mostly anywhere now.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: New RichText class
« Reply #7 on: March 01, 2013, 04:55:07 pm »
nullptr is not a macro (it's a keyword), so with your code it will always be replaced with NULL, even with a C++11 compiler. Just replace every occurence of nullptr with NULL if you really want to write C++-98 code, don't write such ugly hacks ;)
Laurent Gomila - SFML developer

TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: New RichText class
« Reply #8 on: March 01, 2013, 04:57:18 pm »
Oh :X. I saw that code on StackOverflow (I think), thought "genius!" and stuck it in. That'll teach me. :P

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: New RichText class
« Reply #9 on: March 01, 2013, 06:08:08 pm »
Quote
Not sure what FRex noticed that doesn't work with VS10.
braced init lists.. ::)
std::vector<int> a={1,2,3,4,5};


And I didn't notice anything, visual did when I wanted to check if \ can be escaped, but it didn't compile and I had little time so I just eyeballed it.
« Last Edit: March 01, 2013, 06:11:14 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: New RichText class
« Reply #10 on: March 01, 2013, 07:03:32 pm »
Why don't people upgrade to the latest compilers? Come on, they are free, and you don't want to miss the benefits of C++11! Upgrade today! *Microsoft ad plays*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Re: New RichText class
« Reply #11 on: March 01, 2013, 07:28:30 pm »
Why don't people upgrade to the latest compilers? Come on, they are free, and you don't want to miss the benefits of C++11! Upgrade today! *Microsoft ad plays*
Hahaha, I'd never imagined the day when Tank would make Microsoft ads - but he's right. ;)

(Students even get the Ultimate version for free ;D )
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: New RichText class
« Reply #12 on: March 01, 2013, 09:04:43 pm »
I love it!  :) This is free to use, right?

Something though, why not add the default font to this constructor like sf::Text does?

explicit RichText(const String& source, const Font& font, unsigned int characterSize = 30);

This way the following is possible.

sf::RichText text("_nice and clean_");

Also, not sure how practical this is but, how about something like:

#rgb(255,0,0) red_text

This is my take on it.

Color
RichText::getColorByRGBA(String source) const
{

            //replace all non numeric characters with white-spaces
            for (size_t i = 0; i < source.getSize(); ++i)
            {
                if ( !isdigit(source[i]) )
                    source[i] = ' ';
            }

            //split string
            std::string part;
            std::vector<std::string> rgbValues;
            std::stringstream ss(source);

            while (ss >> part)
            {
                rgbValues.push_back(part);
            }

            unsigned char r = atoi( rgbValues[0].c_str() );
            unsigned char g = atoi( rgbValues[1].c_str() );
            unsigned char b = atoi( rgbValues[2].c_str() );
            unsigned char a = rgbValues.size() < 4 ? 255 : atoi( rgbValues[3].c_str() );

            return sf::Color(r, g, b, a);

}

Called here:
Color
        RichText::getColor(const String& source) const
        {
                std::map<String, Color>::const_iterator result = colors.find(source);
                if (result == colors.end())
                {
                        //HERE
                        if (source.find("rgb") != std::string::npos)
                        {
                            return getColorByRGBA(source);
                        }

                        unsigned hex = 0x0;
                        if (!(std::stringstream(source) >> std::hex >> hex))
                        {
                                //      Error parsing; return default
                                return Color::White;
                        };

                        return getColor(hex);
                }

                return result->second;
        }
« Last Edit: March 01, 2013, 09:06:19 pm by kaB00M »



TechRogue

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: New RichText class
« Reply #13 on: March 01, 2013, 09:20:38 pm »
Quote
This is free to use, right?
Yep, the license in the readme is the same as SFML.

I'm using the very latest SFML 2.x release and the default font is no longer included. That's the only reason it isn't supported with my class. If you're using a version of SFML that still has it, you can get it like so:

sf::RichText text("_nice and clean_", sf::Font::getDefaultFont());

You could also modify the header to have a default parameter for the font.

Quote
#rgb(255,0,0) red text
(snip)

I like that! If you have a Bitbucket account I'd accept a pull request, or I can just add it in myself if not.

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: New RichText class
« Reply #14 on: March 03, 2013, 05:15:10 am »
Quote
I like that! If you have a Bitbucket account I'd accept a pull request, or I can just add it in myself if not.

Glad you like it.  :) This is my first contribution to a project ever!



 

anything