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

Author Topic: [Resource] Little RichText  (Read 3880 times)

0 Members and 1 Guest are viewing this topic.

Xafi

  • Newbie
  • *
  • Posts: 18
  • My project, http://1ndieidea.hol.es/
    • View Profile
[Resource] Little RichText
« on: November 29, 2013, 03:15:17 am »
HI today i did a class to handle sf::text.

https://www.youtube.com/watch?v=BSytgKJ3DMY

I wait tips to optimize

RichText.hpp
/* AUTHOR: XAFI */
#ifndef RICHTEXT_H
#define RICHTEXT_H
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>
class RichText : public sf::Drawable,public sf::Transformable
{
public:
        RichText();
        RichText & operator << (const std::string&);
        RichText & operator << (const sf::Color&);
        RichText & operator << (const sf::Uint32);

        void setFont(const sf::Font &);
        void setCharacterSize(int);
        void setColor(int, sf::Color);
        void setString(int, std::string);
        void Clear();
       
        sf::Vector2f getSize() const;
        const std::string getString();
        int getCharacterSize();

private:
        void draw(sf::RenderTarget& target, sf::RenderStates states) const;
        void UpdatePos() const;
        mutable std::vector<sf::Text> Text;
        sf::Color LastColor;
        sf::Uint32 LastStyle;
        mutable sf::Vector2f Size;



};

#endif //RICHTECT_H
 
RichText.cpp
/* AUTHOR: XAFI */

#include "RichText.h"

RichText::RichText() :
        LastColor(sf::Color::White),
        LastStyle(sf::Text::Style::Regular)


{
}

RichText & RichText::operator << (const std::string& string)
{
        sf::Text t;
        t.setString(string);
        t.setColor(LastColor);
        t.setStyle(LastStyle);
        Text.push_back( t );
        return *this;
}
RichText & RichText::operator << (const sf::Color &color)
{
        LastColor = color;
        return *this;
}

RichText & RichText::operator << (const sf::Uint32 style)
{
        LastStyle = style;
        return *this;
}

void RichText::setFont(const sf::Font& font)
{
        for (std::vector<sf::Text>::iterator it = Text.begin(); it != Text.end(); ++it)
        {
                it->setFont(font);
        }
}

void RichText::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
        UpdatePos();
        states.transform *= getTransform();
        for (std::vector<sf::Text>::const_iterator it = Text.begin(); it != Text.end(); ++it)
        {
                target.draw(*it, states);
        }
}
void RichText::setCharacterSize(int size)
{
        for (std::vector<sf::Text>::iterator it = Text.begin(); it != Text.end(); ++it)
        {
                it->setCharacterSize(size);
        }
}
const std::string RichText::getString()
{
        std::string tmp;
        for (std::vector<sf::Text>::iterator it = Text.begin(); it != Text.end(); ++it)
        {
                tmp += it->getString().toAnsiString();
        }
        return tmp;
}
void RichText::UpdatePos() const
{
        int x = 0;
        for (std::vector<sf::Text>::iterator it = Text.begin(); it != Text.end(); ++it)
        {
                x += it->getLocalBounds().width;
                it->setPosition(x - it->getLocalBounds().width, 0);
        }
        Size.x = x;
}
void RichText::setColor(int index, sf::Color color)
{
        Text[index].setColor(color);
}
void RichText::setString(int index, std::string string)
{
Text[index].setString(string);
}
void RichText::Clear()
{
        LastColor = sf::Color::White;
        LastStyle = sf::Text::Style::Regular;
        Text.erase(Text.begin(), Text.end());
}
sf::Vector2f RichText::getSize() const
{
        return Size;
}

int RichText::getCharacterSize()
{
        return Text[0].getCharacterSize();
}
 

« Last Edit: November 29, 2013, 05:03:09 pm by Xafi »

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: [Resource] Little RichText
« Reply #1 on: November 29, 2013, 08:14:23 am »
"BIG BOOBIES" and "big tits"?

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: [Resource] Little RichText
« Reply #2 on: November 29, 2013, 09:37:45 am »
First, I'm not a c++ expert  ;) . For a high quality answer you have to wait for some other guys, e.g. Nexus.

        const std::string getString();
        int getCharacterSize();

        void UpdatePos() const;
 

Here you could inprove the const correctness. I would write:

        std::string getString() const;
        //Or maybe const sf::String &getString() const;
        int getCharacterSize() const;

        void UpdatePos(); //Why const here? It changes something inside the class
 

And you could improve the performance a tiny bit:
    //your version:
     void setString(int, std::string);
   // new:
     void setString(int, const std::string &);

//Also here
    //your version
    sf::Vector2f getSize() const;
    //new:
    const sf::Vector2f &getSize() const;
 


And you should use ONE style for the names of functions. e.g. you use getString() but UpdatePosition()...


But the result in your video looks very nice!


AlexAUT

Xafi

  • Newbie
  • *
  • Posts: 18
  • My project, http://1ndieidea.hol.es/
    • View Profile
Re: [Resource] Little RichText
« Reply #3 on: November 29, 2013, 12:47:54 pm »
OK right now i revise the code
Thank you!.
"BIG BOOBIES" and "big tits"?
I could not think otherwise  ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: [Resource] Little RichText
« Reply #4 on: November 29, 2013, 01:44:34 pm »
void RichText::setCharacterSize(int size)
should probably be
void RichText::setCharacterSize(unsigned int size)
to match the SFML signature and since negative font sizes don't make sense. ;)

void RichText::setString(int index, std::string string)
should use std::size_t instead of int. Negative indices don't make sense and can cause crashes, plus the index type is std::size_t.
The question here is though, whether it makes sense to let the user access the text with a random index. If you do allow it, you'll need to make sure that the index exists, otherwise your application will crash.

Does it support multiple lines?
Btw have you ever seen this Richt Text class?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xafi

  • Newbie
  • *
  • Posts: 18
  • My project, http://1ndieidea.hol.es/
    • View Profile
Re: [Resource] Little RichText
« Reply #5 on: November 29, 2013, 02:11:49 pm »
Quote from: eXpl0it3r
Does it support multiple lines?
still not, i am thinking the best way implement it.
Thank you!

---------------------------------------------------

Quote
should use std::size_t instead of int. Negative indices don't make sense and can cause crashes, plus the index type is std::size_t.
The question here is though, whether it makes sense to let the user access the text with a random index. If you do allow it, you'll need to make sure that the index exists, otherwise your application will crash.

Something like that?
 
void RichText::setString(std::size_t index, std::string string)
{
        if (index < Text.size())
        {
                Text[index].setString(string);
        }
}

« Last Edit: November 29, 2013, 02:51:08 pm by Xafi »

 

anything