Finish using-c++11
commit
9843fe78d1
390
RichText.cpp
390
RichText.cpp
|
@ -2,242 +2,330 @@
|
||||||
// Headers
|
// Headers
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
#include "RichText.hpp"
|
#include "RichText.hpp"
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/Rect.hpp>
|
||||||
#include <SFML/Graphics/RenderTarget.hpp>
|
#include <SFML/Graphics/RenderTarget.hpp>
|
||||||
#include <iostream>
|
|
||||||
|
#include <SFML/System/String.hpp>
|
||||||
|
|
||||||
namespace sfe
|
namespace sfe
|
||||||
{
|
{
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void RichText::Line::setCharacterSize(unsigned int size)
|
||||||
|
{
|
||||||
|
for (sf::Text &text : m_texts)
|
||||||
|
text.setCharacterSize(size);
|
||||||
|
|
||||||
|
updateGeometry();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void RichText::Line::setFont(const sf::Font &font)
|
||||||
|
{
|
||||||
|
for (sf::Text &text : m_texts)
|
||||||
|
text.setFont(font);
|
||||||
|
|
||||||
|
updateGeometry();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
const std::vector<sf::Text> &RichText::Line::getTexts() const
|
||||||
|
{
|
||||||
|
return m_texts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void RichText::Line::appendText(sf::Text text)
|
||||||
|
{
|
||||||
|
// Set text offset
|
||||||
|
text.setPosition(m_bounds.width, 0.f);
|
||||||
|
|
||||||
|
// Push back
|
||||||
|
m_texts.push_back(std::move(text));
|
||||||
|
|
||||||
|
// Update bounds
|
||||||
|
m_bounds.height = std::max(m_bounds.height, text.getGlobalBounds().height);
|
||||||
|
m_bounds.width += text.getGlobalBounds().width;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
sf::FloatRect RichText::Line::getLocalBounds() const
|
||||||
|
{
|
||||||
|
return m_bounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
sf::FloatRect RichText::Line::getGlobalBounds() const
|
||||||
|
{
|
||||||
|
return getTransform().transformRect(getLocalBounds());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void RichText::Line::draw(sf::RenderTarget &target, sf::RenderStates states) const
|
||||||
|
{
|
||||||
|
states.transform *= getTransform();
|
||||||
|
|
||||||
|
for (const sf::Text &text : m_texts)
|
||||||
|
target.draw(text, states);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
void RichText::Line::updateGeometry() const
|
||||||
|
{
|
||||||
|
m_bounds = sf::FloatRect();
|
||||||
|
|
||||||
|
for (sf::Text &text : m_texts) {
|
||||||
|
text.setPosition(m_bounds.width, 0.f);
|
||||||
|
|
||||||
|
m_bounds.height = std::max(m_bounds.height, text.getGlobalBounds().height);
|
||||||
|
m_bounds.width += text.getGlobalBounds().height;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
RichText::RichText()
|
RichText::RichText()
|
||||||
: myCurrentColor(sf::Color::White),
|
: RichText(nullptr)
|
||||||
myCurrentStyle(sf::Text::Regular),
|
|
||||||
mySizeUpdated(false),
|
|
||||||
myPositionUpdated(false)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Operator << sf::Color
|
RichText::RichText(const sf::Font &font)
|
||||||
|
: RichText(&font)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
RichText & RichText::operator << (const sf::Color &color)
|
RichText & RichText::operator << (const sf::Color &color)
|
||||||
{
|
{
|
||||||
myCurrentColor = color;
|
m_currentColor = color;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Operator << sf::Text::Style
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
RichText & RichText::operator << (sf::Text::Style style)
|
RichText & RichText::operator << (sf::Text::Style style)
|
||||||
{
|
{
|
||||||
myCurrentStyle = style;
|
m_currentStyle = style;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Operator << sf::String
|
std::vector<sf::String> explode(const sf::String &string, sf::Uint32 delimiter) {
|
||||||
|
if (string.isEmpty())
|
||||||
|
return std::vector<sf::String>();
|
||||||
|
|
||||||
|
// For each character in the string
|
||||||
|
std::vector<sf::String> result;
|
||||||
|
sf::String buffer;
|
||||||
|
for (sf::Uint32 character : string) {
|
||||||
|
// If we've hit the delimiter character
|
||||||
|
if (character == delimiter) {
|
||||||
|
// Add them to the result vector
|
||||||
|
result.push_back(buffer);
|
||||||
|
buffer.clear();
|
||||||
|
} else {
|
||||||
|
// Accumulate the next character into the sequence
|
||||||
|
buffer += character;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add to the result if buffer still has contents or if the last character is a delimiter
|
||||||
|
if (!buffer.isEmpty() || string[string.getSize() - 1] == delimiter)
|
||||||
|
result.push_back(buffer);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
/*
|
|
||||||
** Must parse the strings to look for '\n' characters. If found, we break
|
|
||||||
** the string into two pieces.
|
|
||||||
*/
|
|
||||||
RichText & RichText::operator << (const sf::String &string)
|
RichText & RichText::operator << (const sf::String &string)
|
||||||
{
|
{
|
||||||
// It is not updated
|
// Maybe skip
|
||||||
mySizeUpdated = false;
|
if (string.isEmpty())
|
||||||
myPositionUpdated = false;
|
return *this;
|
||||||
|
|
||||||
// Find \n characters (assert)
|
// Explode into substrings
|
||||||
//assert(string.Find('\n') == std::string::npos);
|
std::vector<sf::String> subStrings = explode(string, '\n');
|
||||||
if(string.find('\n') != std::string::npos)
|
|
||||||
std::cerr << "sfe::RichtText: Oops, character \n found."
|
|
||||||
"You will get visual errors." << std::endl;
|
|
||||||
|
|
||||||
//String cannot be void
|
|
||||||
//assert(string != "");
|
|
||||||
|
|
||||||
// Add string
|
|
||||||
myTexts.push_back(sf::Text(string));
|
|
||||||
|
|
||||||
// Setup string
|
// Append first substring using the last line
|
||||||
sf::Text &text = myTexts.back();
|
auto it = subStrings.begin();
|
||||||
text.setColor(myCurrentColor);
|
if (it != subStrings.end()) {
|
||||||
text.setStyle(myCurrentStyle);
|
// If there isn't any line, just create it
|
||||||
|
if (m_lines.empty())
|
||||||
|
m_lines.resize(1);
|
||||||
|
|
||||||
// Return
|
// Remove last line's height
|
||||||
return *this;
|
Line &line = m_lines.back();
|
||||||
|
m_bounds.height -= line.getGlobalBounds().height;
|
||||||
|
|
||||||
|
// Append text
|
||||||
|
line.appendText(createText(*it));
|
||||||
|
|
||||||
|
// Update bounds
|
||||||
|
m_bounds.height += line.getGlobalBounds().height;
|
||||||
|
m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append the rest of substrings as new lines
|
||||||
|
while (++it != subStrings.end()) {
|
||||||
|
Line line;
|
||||||
|
line.setPosition(0.f, m_bounds.height);
|
||||||
|
line.appendText(createText(*it));
|
||||||
|
m_lines.push_back(std::move(line));
|
||||||
|
|
||||||
|
// Update bounds
|
||||||
|
m_bounds.height += line.getGlobalBounds().height;
|
||||||
|
m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Set size
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void RichText::setCharacterSize(unsigned int size)
|
void RichText::setCharacterSize(unsigned int size)
|
||||||
{
|
{
|
||||||
// Set character size
|
// Maybe skip
|
||||||
for(collection_type::iterator it = myTexts.begin();
|
if (m_characterSize == size)
|
||||||
it != myTexts.end(); ++it)
|
return;
|
||||||
{
|
|
||||||
it->setCharacterSize(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// It is not updated
|
// Update character size
|
||||||
mySizeUpdated = false;
|
m_characterSize = size;
|
||||||
myPositionUpdated = false;
|
|
||||||
|
// Set texts character size
|
||||||
|
for (Line &line : m_lines)
|
||||||
|
line.setCharacterSize(size);
|
||||||
|
|
||||||
|
updateGeometry();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Set font
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void RichText::setFont(const sf::Font &font)
|
void RichText::setFont(const sf::Font &font)
|
||||||
{
|
{
|
||||||
// Set character size
|
// Maybe skip
|
||||||
for(collection_type::iterator it = myTexts.begin();
|
if (m_font == &font)
|
||||||
it != myTexts.end(); ++it)
|
return;
|
||||||
{
|
|
||||||
it->setFont(font);
|
|
||||||
}
|
|
||||||
|
|
||||||
// It is not updated
|
// Update font
|
||||||
mySizeUpdated = false;
|
m_font = &font;
|
||||||
myPositionUpdated = false;
|
|
||||||
|
// Set texts font
|
||||||
|
for (Line &line : m_lines)
|
||||||
|
line.setFont(font);
|
||||||
|
|
||||||
|
updateGeometry();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Clear
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void RichText::clear()
|
void RichText::clear()
|
||||||
{
|
{
|
||||||
// Clear text list
|
// Clear texts
|
||||||
myTexts.clear();
|
m_lines.clear();
|
||||||
|
|
||||||
// Reset size
|
// Reset bounds
|
||||||
mySize = sf::Vector2f(0.f, 0.f);
|
m_bounds = sf::FloatRect();
|
||||||
|
|
||||||
// It is updated
|
|
||||||
mySizeUpdated = true;
|
|
||||||
myPositionUpdated = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Get text list
|
const std::vector<RichText::Line> &RichText::getLines() const
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
const RichText::collection_type & RichText::getTextList() const
|
|
||||||
{
|
{
|
||||||
return myTexts;
|
return m_lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Get character size
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
unsigned int RichText::getCharacterSize() const
|
unsigned int RichText::getCharacterSize() const
|
||||||
{
|
{
|
||||||
if(myTexts.size()) return myTexts.begin()->getCharacterSize();
|
return m_characterSize;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Get font
|
const sf::Font *RichText::getFont() const
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
const sf::Font & RichText::getFont() const
|
|
||||||
{
|
{
|
||||||
if(myTexts.size()) return myTexts.begin()->getFont();
|
return m_font;
|
||||||
return sf::Font::getDefaultFont();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Get width
|
////////////////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
sf::FloatRect RichText::getLocalBounds() const
|
||||||
float RichText::getWidth() const
|
|
||||||
{
|
{
|
||||||
updateSize();
|
return m_bounds;
|
||||||
return mySize.x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Get height
|
////////////////////////////////////////////////////////////
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
sf::FloatRect RichText::getGlobalBounds() const
|
||||||
float RichText::getHeight() const
|
|
||||||
{
|
{
|
||||||
updateSize();
|
return getTransform().transformRect(getLocalBounds());
|
||||||
return mySize.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Render
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void RichText::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
void RichText::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||||
{
|
{
|
||||||
// Update position
|
states.transform *= getTransform();
|
||||||
updatePosition();
|
|
||||||
|
|
||||||
states.transform *= getTransform();
|
for (const Line &line : m_lines)
|
||||||
|
target.draw(line, states);
|
||||||
// Draw
|
|
||||||
for(collection_type::const_iterator it = myTexts.begin();
|
|
||||||
it != myTexts.end(); ++it)
|
|
||||||
{
|
|
||||||
// Add transformation
|
|
||||||
//it->setT
|
|
||||||
|
|
||||||
// Draw text
|
|
||||||
target.draw(*it, states);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Update size
|
RichText::RichText(const sf::Font *font)
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
: m_font(font),
|
||||||
void RichText::updateSize() const
|
m_characterSize(30),
|
||||||
|
m_currentColor(sf::Color::White),
|
||||||
|
m_currentStyle(sf::Text::Regular)
|
||||||
{
|
{
|
||||||
// Return if updated
|
|
||||||
if(mySizeUpdated) return;
|
|
||||||
|
|
||||||
// Return if empty
|
|
||||||
if(myTexts.begin() == myTexts.end()) return;
|
|
||||||
|
|
||||||
// It is updated
|
|
||||||
mySizeUpdated = true;
|
|
||||||
|
|
||||||
// Sum all sizes (height not implemented)
|
|
||||||
mySize.x = 0.f;
|
|
||||||
mySize.y = myTexts.begin()->getGlobalBounds().height;
|
|
||||||
for(collection_type::const_iterator it = myTexts.begin();
|
|
||||||
it != myTexts.end(); ++it)
|
|
||||||
{
|
|
||||||
// Update width
|
|
||||||
mySize.x += it->getGlobalBounds().width;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Update position
|
sf::Text RichText::createText(const sf::String &string) const
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void RichText::updatePosition() const
|
|
||||||
{
|
{
|
||||||
// Return if updated
|
sf::Text text;
|
||||||
if(myPositionUpdated) return;
|
text.setString(string);
|
||||||
|
text.setColor(m_currentColor);
|
||||||
|
text.setStyle(m_currentStyle);
|
||||||
|
if (m_font)
|
||||||
|
text.setFont(*m_font);
|
||||||
|
|
||||||
// Return if empty
|
return text;
|
||||||
if(myTexts.begin() == myTexts.end()) return;
|
}
|
||||||
|
|
||||||
// It is updated
|
|
||||||
myPositionUpdated = true;
|
|
||||||
|
|
||||||
// Get starting position
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
sf::Vector2f offset;
|
void RichText::updateGeometry() const
|
||||||
|
{
|
||||||
|
m_bounds = sf::FloatRect();
|
||||||
|
|
||||||
// Draw
|
for (Line &line : m_lines) {
|
||||||
for(collection_type::iterator it = myTexts.begin();
|
line.setPosition(0.f, m_bounds.height);
|
||||||
it != myTexts.end(); ++it)
|
|
||||||
{
|
|
||||||
// Set all the origins to the first one
|
|
||||||
it->setOrigin(it->getPosition() - myTexts.begin()->getPosition() - offset);
|
|
||||||
|
|
||||||
// Set offset
|
m_bounds.height += line.getGlobalBounds().height;
|
||||||
const sf::FloatRect rect = it->getGlobalBounds();
|
m_bounds.width = std::max(m_bounds.width, line.getGlobalBounds().width);
|
||||||
offset.x += rect.width;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
216
RichText.hpp
216
RichText.hpp
|
@ -1,14 +1,24 @@
|
||||||
#ifndef RICHTEXT_HPP
|
#pragma once
|
||||||
#define RICHTEXT_HPP
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Headers
|
// Headers
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <SFML/Graphics/Transformable.hpp>
|
||||||
#include <SFML/Graphics/Drawable.hpp>
|
#include <SFML/Graphics/Drawable.hpp>
|
||||||
#include <SFML/Graphics/Color.hpp>
|
#include <SFML/Graphics/Color.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include <SFML/System/String.hpp>
|
|
||||||
#include <vector>
|
#include <SFML/System/Vector2.hpp>
|
||||||
|
|
||||||
|
namespace sf
|
||||||
|
{
|
||||||
|
class Font;
|
||||||
|
class String;
|
||||||
|
template <class T> class Rect;
|
||||||
|
typedef Rect<float> FloatRect;
|
||||||
|
}
|
||||||
|
|
||||||
namespace sfe
|
namespace sfe
|
||||||
{
|
{
|
||||||
|
@ -16,91 +26,149 @@ namespace sfe
|
||||||
class RichText : public sf::Drawable, public sf::Transformable
|
class RichText : public sf::Drawable, public sf::Transformable
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Typedef for collection type
|
// Nested class that represents a single line
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
typedef std::vector<sf::Text> collection_type;
|
class Line : public sf::Transformable, public sf::Drawable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
// Set character size
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
void setCharacterSize(unsigned int size);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Constructor
|
// Set font
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
RichText();
|
void setFont(const sf::Font &font);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Operators
|
// Get texts
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
RichText & operator << (const sf::Color &color);
|
const std::vector<sf::Text> &getTexts() const;
|
||||||
RichText & operator << (sf::Text::Style style);
|
|
||||||
RichText & operator << (const sf::String &string);
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Set character size
|
// Append text
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
void setCharacterSize(unsigned int size);
|
void appendText(sf::Text text);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Set font
|
// Get local bounds
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
void setFont(const sf::Font &font);
|
sf::FloatRect getLocalBounds() const;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Clear
|
// Get global bounds
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
void clear();
|
sf::FloatRect getGlobalBounds() const;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
protected:
|
||||||
// Get text list
|
//////////////////////////////////////////////////////////////////////
|
||||||
//////////////////////////////////////////////////////////////////////////
|
// Draw
|
||||||
const collection_type &getTextList() const;
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
private:
|
||||||
// Get character size
|
//////////////////////////////////////////////////////////////////////
|
||||||
//////////////////////////////////////////////////////////////////////////
|
// Update geometry
|
||||||
unsigned int getCharacterSize() const;
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
void updateGeometry() const;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
// Get font
|
// Member data
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
const sf::Font & getFont() const;
|
mutable std::vector<sf::Text> m_texts; ///< List of texts
|
||||||
|
mutable sf::FloatRect m_bounds; ///< Local bounds
|
||||||
|
};
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Get width
|
// Constructor
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
float getWidth() const;
|
RichText();
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Get height
|
// Constructor
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
float getHeight() const;
|
RichText(const sf::Font &font);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Operators
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
RichText & operator << (const sf::Color &color);
|
||||||
|
RichText & operator << (sf::Text::Style style);
|
||||||
|
RichText & operator << (const sf::String &string);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Set character size
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
void setCharacterSize(unsigned int size);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Set font
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
void setFont(const sf::Font &font);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Clear
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
void clear();
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Get text list
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
const std::vector<Line> &getLines() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Get character size
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
unsigned int getCharacterSize() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Get font
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
const sf::Font *getFont() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Get local bounds
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
sf::FloatRect getLocalBounds() const;
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Get global bounds
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
sf::FloatRect getGlobalBounds() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
// Render
|
||||||
|
//////////////////////////////////////////////////////////////////////////
|
||||||
|
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Update size
|
// Delegate constructor
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
void updateSize() const;
|
RichText(const sf::Font *font);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Update position
|
// Creates a sf::Text instance using the current styles
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
void updatePosition() const;
|
sf::Text createText(const sf::String &string) const;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Render
|
// Update geometry
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
void updateGeometry() const;
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Member data
|
// Member data
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
mutable collection_type myTexts; ///< List of texts
|
mutable std::vector<Line> m_lines; ///< List of lines
|
||||||
sf::Color myCurrentColor; ///< Last used color
|
const sf::Font *m_font; ///< Font
|
||||||
sf::Text::Style myCurrentStyle; ///< Last style used
|
unsigned int m_characterSize; ///< Character size
|
||||||
mutable sf::Vector2f mySize; ///< Size of the text
|
mutable sf::FloatRect m_bounds; ///< Local bounds
|
||||||
mutable bool mySizeUpdated; ///< Do we need to recompute the size?
|
sf::Color m_currentColor; ///< Last used color
|
||||||
mutable bool myPositionUpdated; ///< Do we need to recompute the
|
sf::Text::Style m_currentStyle; ///< Last style used
|
||||||
///< position?
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // RICHTEXT_HPP
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
TEMPLATE = app
|
||||||
|
CONFIG -= qt
|
||||||
|
CONFIG -= app_bundle
|
||||||
|
CONFIG -= console
|
||||||
|
CONFIG += C++11
|
||||||
|
|
||||||
|
INCLUDEPATH += SFML
|
||||||
|
|
||||||
|
LIBS += -lsfml-system-d -lsfml-window-d -lsfml-graphics-d -ljpeg -lGLEW
|
||||||
|
|
||||||
|
SOURCES += main.cpp \
|
||||||
|
RichText.cpp
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
RichText.hpp
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#include "RichText.hpp"
|
||||||
|
|
||||||
|
int main(int, char *[])
|
||||||
|
{
|
||||||
|
sf::RenderWindow window;
|
||||||
|
window.create(sf::VideoMode(800, 600), "Test");
|
||||||
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
||||||
|
sf::Font font;
|
||||||
|
font.loadFromFile("/usr/share/fonts/truetype/freefont/FreeMono.ttf");
|
||||||
|
sfe::RichText text(font);
|
||||||
|
text << sf::Color::Red << "Test\nbla" << sf::Color::Blue << "Lol";
|
||||||
|
|
||||||
|
while (window.isOpen()) {
|
||||||
|
sf::Event event;
|
||||||
|
while (window.pollEvent(event)) {
|
||||||
|
switch (event.type) {
|
||||||
|
case sf::Event::Closed:
|
||||||
|
window.close();
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.clear();
|
||||||
|
window.draw(text);
|
||||||
|
window.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue