ge211  2021.5.1
A student game engine
Text_sprite::Builder Class Reference

Detailed Description

Builder-style API for configuring and constructing Text_sprites.

The idea is that a Text_sprite::Builder allows configuring a Text_sprite in detail before actually constructing it.

Example

struct View
{
ge211::Font sans{"sans.ttf", 16},
ge211::Text_sprite percentage_sprite;
double cached_value = NAN;
void update_percentage(double unit_value);
};
void View::update_percentage(double unit_value)
{
if (unit_value == cached_value) return;
ge211::Text_sprite::Builder builder(sans);
if (unit_value < 0.5) {
builder.color(Color::medium_green());
} else {
builder.color(Color::medium_red());
}
builder << std::setprecision(1) << std::fixed
<< 100 * unit_value << '%';
percentage_sprite.reconfigure(builder);
cached_value = unit_value;
}

Definition at line 362 of file ge211_sprites.hxx.

Public Member Functions

Constructor and builder
 Builder (Font const &)
 Constructs a new Text_sprite::Builder with the given Font.
 
Text_sprite build () const
 Builds the configured Text_sprite.
 
Builder-style setters
template<typename PRINTABLE >
Builderadd_message (PRINTABLE const &value)
 Adds to the builder's message. More...
 
template<typename PRINTABLE >
Builderoperator<< (PRINTABLE const &value)
 Adds to the builder's message. More...
 
Buildermessage (std::string const &)
 Replaces the configured message with the given message. More...
 
Builderfont (Font const &)
 Sets font to use. More...
 
Buildercolor (Color)
 Sets the color to use. More...
 
Builderantialias (bool)
 Sets whether to use anti-aliasing. More...
 
Builderword_wrap (int)
 Sets the pixel width for wrapping the text. More...
 
Getters
std::string message () const
 Gets the configured message.
 
Font const & font () const
 Gets the font that will be used.
 
Color color () const
 Gets the color that will be used.
 
bool antialias () const
 Gets whether anti-aliasing will be used.
 
int word_wrap () const
 Gets the wrapping width that will be used.
 

Member Function Documentation

◆ add_message()

Builder& add_message ( PRINTABLE const &  value)
inline

Adds to the builder's message.

This takes any printable type and prints it à la operator<<. Returns the builder, for call chaining.

Definition at line 383 of file ge211_sprites.hxx.

◆ antialias()

Text_sprite::Builder & antialias ( bool  antialias)

Sets whether to use anti-aliasing.

Anti-aliasing, on by default, makes text smoother but can make it take longer to render. Returns a reference to the Builder for call chaining.

Definition at line 280 of file ge211_sprites.cxx.

◆ color()

Text_sprite::Builder & color ( Color  color)

Sets the color to use.

Returns a reference to the Builder for call chaining.

Definition at line 274 of file ge211_sprites.cxx.

◆ font()

Text_sprite::Builder & font ( Font const &  font)

Sets font to use.

Returns a reference to the Builder for call chaining.

Definition at line 268 of file ge211_sprites.cxx.

◆ message()

Text_sprite::Builder & message ( std::string const &  message)

Replaces the configured message with the given message.

Returns a reference to the Builder for call chaining.

Definition at line 262 of file ge211_sprites.cxx.

◆ operator<<()

Builder& operator<< ( PRINTABLE const &  value)
inline

Adds to the builder's message.

This is an alias for add_message(const PRINTABLE&).

For example:

Text_sprite position_sprite(Posn<int> position)
{
Builder builder(font);
builder << "(" << position.x << ", " << position.y << ")";
return builder.build();
}

Definition at line 403 of file ge211_sprites.hxx.

◆ word_wrap()

Text_sprite::Builder & word_wrap ( int  word_wrap)

Sets the pixel width for wrapping the text.

If set to 0, the text does not wrap at all. Newlines in the text will produces newlines in the output only if wrapping is on (non-zero). Returns a reference to the Builder for call chaining.

Definition at line 286 of file ge211_sprites.cxx.


The documentation for this class was generated from the following files:
std::setprecision
T setprecision(T... args)
ge211::sprites::Text_sprite::Builder::font
Font const & font() const
Gets the font that will be used.
Definition: ge211_sprites.cxx:303
ge211::Color::medium_red
static Color medium_red()
Solid red.
Definition: ge211_color.hxx:70
ge211::sprites::Text_sprite::Text_sprite
Text_sprite()
Constructs an empty text sprite.
Definition: ge211_sprites.cxx:239
ge211::sprites::Text_sprite
A Sprite that displays text.
Definition: ge211_sprites.hxx:232
ge211::sprites::Text_sprite::Builder::Builder
Builder(Font const &)
Constructs a new Text_sprite::Builder with the given Font.
Definition: ge211_sprites.cxx:258
ge211::Color::medium_green
static Color medium_green()
Solid green.
Definition: ge211_color.hxx:73
ge211::Font
Represents a font that can be used to render a sprites::Text_sprite.
Definition: ge211_resource.hxx:70
std::fixed
T fixed(T... args)