126 lines
4.6 KiB
C++
126 lines
4.6 KiB
C++
#include <optional>
|
|
#include <stdexcept>
|
|
|
|
#include "SDL.h"
|
|
#include "SDL_ttf.h"
|
|
#include "sdl2-gfx/SDL2_rotozoom.h"
|
|
|
|
#include "Model.hpp"
|
|
#include "Color.hpp"
|
|
#include "Log.hpp"
|
|
|
|
namespace sb
|
|
{
|
|
class Text : public sb::Plane
|
|
{
|
|
|
|
private:
|
|
|
|
inline static const sb::Color DEFAULT_FG {0.0f, 0.0f, 0.0f, 255.0f};
|
|
inline static const sb::Color DEFAULT_BG {0.0f, 0.0f, 0.0f, 0.0f};
|
|
|
|
std::string _content;
|
|
sb::Color _foreground, _background;
|
|
std::shared_ptr<TTF_Font> _font;
|
|
std::optional<glm::vec2> _dimensions;
|
|
GLint _scaling_quality = GL_LINEAR;
|
|
|
|
public:
|
|
|
|
/*!
|
|
* Construct a sb::Text object from a font, string, and background and foreground colors. A texture will be created and attached. The
|
|
* texture is not generated or rendered. Text::refresh() must be called afterward with the GL context loaded. The content, dimensions,
|
|
* foreground, background, font, and scaling quality functions call Text::refresh() automatically.
|
|
*
|
|
* The font must be wrapped in a shared pointer. A default loaded font is available from Game::font().
|
|
*
|
|
* Dimensions can be given in pixels, in which case the text will be rendered at the center of a texture in exactly the given dimensions,
|
|
* regardless of the length of the text. If the given dimensions are larger than the text, the resulting texture will have padding around
|
|
* the text. This can be useful, for example, for creating identically sized text buttons.
|
|
*
|
|
* @param font a TTF_Font object wrapped in a shared pointer
|
|
* @param content text content
|
|
* @param foreground text color
|
|
* @param background background color
|
|
* @param dimensions force the texture to be a certain size in pixels, extra area is filled with the background color
|
|
*/
|
|
Text(std::shared_ptr<TTF_Font> font, const std::string& content = "", const sb::Color& foreground = DEFAULT_FG,
|
|
const sb::Color& background = DEFAULT_BG, std::optional<glm::vec2> dimensions = std::nullopt) :
|
|
_content(content), _foreground(foreground), _background(background), _font(font), _dimensions(dimensions)
|
|
{
|
|
/* Add an empty Texture object */
|
|
texture(sb::Texture());
|
|
}
|
|
|
|
/*!
|
|
* Bind the texture rendered from the text object's text content and any other textures attached.
|
|
*/
|
|
void bind_textures() const;
|
|
|
|
/*!
|
|
* @param content text to be displayed
|
|
*/
|
|
void content(const std::string& content);
|
|
|
|
/*!
|
|
* @param content Single character to be used for text content
|
|
*/
|
|
void content(char content);
|
|
|
|
/*!
|
|
* @return Text content to be displayed
|
|
*/
|
|
const std::string& content() const;
|
|
|
|
/*!
|
|
* @param foreground text color
|
|
*/
|
|
void foreground(const sb::Color& foreground);
|
|
|
|
/*!
|
|
* @param background text background color which fills the Plane
|
|
*/
|
|
void background(const sb::Color& background);
|
|
|
|
/*!
|
|
* @param font shared pointer to a TTF_Font
|
|
*/
|
|
void font(std::shared_ptr<TTF_Font> font);
|
|
|
|
/*!
|
|
* @param dimensions force the texture to be a certain size in pixels, extra area is filled with the background color
|
|
*/
|
|
void dimensions(const glm::vec2& dimensions);
|
|
|
|
/*!
|
|
* This GL parameter will be passed to `glTexParameter` for `GL_TEXTURE_MIN_FILTER` and `GL_TEXTURE_MAG_FILTER`.
|
|
*
|
|
* @param quality quality of texture scaling passed to `glTexParameter`
|
|
*/
|
|
void scaling_quality(GLint quality);
|
|
|
|
/*!
|
|
* Load the texture with the appropriate SDL_Surface pixels created by the SDL TTF library.
|
|
*/
|
|
void refresh();
|
|
|
|
/*!
|
|
* Convert the text object to a string with some debugging information.
|
|
*/
|
|
operator std::string() const;
|
|
|
|
/*!
|
|
* Overload the stream operator to support text objects. Add a string representation of the text object to the output stream. Since
|
|
* this is defined as a friend function and isn't in the global scope, it should prevent it being looked up with arguments other
|
|
* than text objects.
|
|
*
|
|
* @param out output stream
|
|
* @param text text object to print
|
|
* @return edited output stream
|
|
*/
|
|
friend std::ostream& operator<<(std::ostream& out, const Text& text);
|
|
};
|
|
}
|
|
|
|
#include "Game.hpp"
|