16
SFML projects / [Resource] Little RichText
« on: November 29, 2013, 03:15:17 am »
HI today i did a class to handle sf::text.
I wait tips to optimize
RichText.hpp
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#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
/* 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();
}
#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();
}