FifeGUI 0.2.0
A C++ GUI library designed for games.
fcn::Text Class Reference

#include <text.hpp>

Public Member Functions

virtual void addRow (std::string const &row)
virtual void eraseRow (unsigned int row)
virtual int getCaretColumn () const
virtual Rectangle getCaretDimension (Font *font) const
virtual int getCaretPosition () const
virtual int getCaretRow () const
virtual int getCaretX (Font *font) const
virtual int getCaretY (Font *font) const
virtual std::string getContent () const
virtual Rectangle getDimension (Font *font) const
virtual unsigned int getMaximumCaretRow () const
virtual unsigned int getMaximumCaretRow (unsigned int row) const
virtual unsigned int getNumberOfCharacters () const
virtual unsigned int getNumberOfCharacters (unsigned int row) const
virtual unsigned int getNumberOfRows () const
virtual std::string & getRow (unsigned int row)
virtual int getWidth (int row, Font *font) const
virtual void insert (int character)
virtual void insertRow (std::string const &row, unsigned int position)
Text & operator= (Text &&)=default
Text & operator= (Text const &)=default
virtual void remove (int numberOfCharacters)
virtual void setCaretColumn (int column)
virtual void setCaretPosition (int position)
virtual void setCaretPosition (int x, int y, Font *font)
virtual void setCaretRow (int row)
virtual void setContent (std::string const &content)
virtual void setRow (unsigned int row, std::string const &content)
 Text (std::string const &content)
 Text (Text &&)=default
 Text (Text const &)=default

Protected Member Functions

void calculateCaretPositionFromRowAndColumn ()

Protected Attributes

unsigned int mCaretColumn {0}
unsigned int mCaretPosition {0}
unsigned int mCaretRow {0}
std::vector< std::string > mRows

Detailed Description

Helper class for text manipulation within widgets.

A utility class to ease working with text in widgets such as TextBox and TextField. The class wraps common text operations such as inserting and deleting text.

Definition at line 27 of file text.hpp.

Constructor & Destructor Documentation

◆ Text()

fcn::Text::Text ( std::string const & content)
explicit

Constructor.

Parameters
contentThe content of the text.

Definition at line 20 of file text.cpp.

References mRows.

Member Function Documentation

◆ addRow()

void fcn::Text::addRow ( std::string const & row)
virtual

Adds a row to the content.

Calling this method will not change the current caret position.

Parameters
rowThe row to add.

Definition at line 90 of file text.cpp.

References mRows.

Referenced by insertRow().

◆ calculateCaretPositionFromRowAndColumn()

void fcn::Text::calculateCaretPositionFromRowAndColumn ( )
protected

Calculates the caret position from the caret row and caret column.

Definition at line 384 of file text.cpp.

References mCaretColumn, mCaretPosition, mCaretRow, and mRows.

Referenced by setCaretColumn().

◆ eraseRow()

void fcn::Text::eraseRow ( unsigned int row)
virtual

Erases the given row.

Calling this method will not change the current caret position.

Parameters
rowRow to be erased.

Definition at line 124 of file text.cpp.

References mRows.

◆ getCaretColumn()

int fcn::Text::getCaretColumn ( ) const
virtual

Gets the column the caret is currently in.

Returns
The column the caret is currently in.

Definition at line 268 of file text.cpp.

References mCaretColumn.

◆ getCaretDimension()

Rectangle fcn::Text::getCaretDimension ( Font * font) const
virtual

Gets the caret dimension relative to this text.

The returned dimension is perfect for use with Widget::showPart so the caret is always shown.

Parameters
fontThe font to use when calculating the dimension.
Returns
The dimension of the caret.

Definition at line 337 of file text.cpp.

References fcn::Font::getHeight(), fcn::Font::getWidth(), fcn::Rectangle::height, mCaretColumn, mCaretRow, mRows, fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

◆ getCaretPosition()

int fcn::Text::getCaretPosition ( ) const
virtual

Gets the caret position.

Returns
The caret position.

Definition at line 220 of file text.cpp.

References mCaretPosition.

Referenced by insert().

◆ getCaretRow()

int fcn::Text::getCaretRow ( ) const
virtual

Gets the row the caret is currently in.

Returns
The row the caret is currently in.

Definition at line 273 of file text.cpp.

References mCaretRow.

◆ getCaretX()

int fcn::Text::getCaretX ( Font * font) const
virtual

Gets the x coordinate of the caret in pixels given a font.

Parameters
fontThe font to use when calculating the x coordinate.
Returns
The x coordinate of the caret in pixels.

Definition at line 304 of file text.cpp.

References fcn::Font::getWidth(), mCaretColumn, mCaretRow, and mRows.

◆ getCaretY()

int fcn::Text::getCaretY ( Font * font) const
virtual

Gets the y coordinate of the caret in pixels given a font.

Parameters
fontThe font to use when calculating the y coordinate.
Returns
The y coordinate of the caret in pixels.

Definition at line 313 of file text.cpp.

References fcn::Font::getHeight(), and mCaretRow.

◆ getContent()

std::string fcn::Text::getContent ( ) const
virtual

Gets the content of the text.

Returns
The content of the text.

Definition at line 65 of file text.cpp.

References mRows.

◆ getDimension()

Rectangle fcn::Text::getDimension ( Font * font) const
virtual

Gets the dimension in pixels of the text given a font.

If there is no text present a dimension of a white space will be returned.

Parameters
fontThe font to use when calculating the dimension.
Returns
The dimension in pixels of the text given a font.

Definition at line 319 of file text.cpp.

References fcn::Font::getHeight(), fcn::Font::getWidth(), and mRows.

◆ getMaximumCaretRow() [1/2]

unsigned int fcn::Text::getMaximumCaretRow ( ) const
virtual

Gets the maximum row the caret can be in.

Returns
The maximum row the caret can be in.

Definition at line 353 of file text.cpp.

◆ getMaximumCaretRow() [2/2]

unsigned int fcn::Text::getMaximumCaretRow ( unsigned int row) const
virtual

Gets the maximum column of a row the caret can be in.

Parameters
rowThe row of the caret.
Returns
The maximum column of a row the caret can be in.

Definition at line 358 of file text.cpp.

◆ getNumberOfCharacters() [1/2]

unsigned int fcn::Text::getNumberOfCharacters ( ) const
virtual

Gets the number of characters in the text.

Returns
The number of characters in the text.

Definition at line 363 of file text.cpp.

References mRows.

Referenced by remove().

◆ getNumberOfCharacters() [2/2]

unsigned int fcn::Text::getNumberOfCharacters ( unsigned int row) const
virtual

Gets the number of characters in a certain row in the text.

If the row does not exist, zero will be returned.

Parameters
rowThe row to get the number of characters in.
Returns
The number of characters in a certain row, or zero if the row does not exist.

Definition at line 375 of file text.cpp.

References mRows.

◆ getNumberOfRows()

unsigned int fcn::Text::getNumberOfRows ( ) const
virtual

Gets the number of rows in the text.

Returns
The number of rows in the text.

Definition at line 370 of file text.cpp.

References mRows.

◆ getRow()

std::string & fcn::Text::getRow ( unsigned int row)
virtual

Gets a reference to a row.

Parameters
rowThe row to get the content of.
Returns
The reference to a row.
Exceptions
Exceptionwhen no such row exists.

Definition at line 133 of file text.cpp.

References mRows.

◆ getWidth()

int fcn::Text::getWidth ( int row,
Font * font ) const
virtual

Gets the width in pixels of a row.

If the row is not present in the text zero will be returned.

Parameters
rowThe row to get the width of.
fontThe font to use when calculating the width.
Returns
The width in pixels of a row.

Definition at line 348 of file text.cpp.

◆ insert()

void fcn::Text::insert ( int character)
virtual

Inserts a character at the current caret position.

Parameters
characterThe character to insert.

Definition at line 142 of file text.cpp.

References getCaretPosition(), mCaretColumn, mCaretRow, mRows, and setCaretPosition().

◆ insertRow()

void fcn::Text::insertRow ( std::string const & row,
unsigned int position )
virtual

Inserts a row before the specified row position.

Calling this method will not change the current caret position.

Parameters
rowThe row to add.
positionInserts new row before this row.

Definition at line 102 of file text.cpp.

References addRow(), and mRows.

◆ remove()

void fcn::Text::remove ( int numberOfCharacters)
virtual

Removes a given number of characters at starting at the current caret position.

If the number of characters to remove is negative characters will be removed left of the caret position. If the number is positive characters will be removed right of the caret position. If a line feed is removed the row with the line feed will be merged with the row above the line feed.

Parameters
numberOfCharactersThe number of characters to remove.

Definition at line 166 of file text.cpp.

References getNumberOfCharacters(), mCaretColumn, mCaretPosition, mCaretRow, mRows, setCaretColumn(), setCaretPosition(), and setCaretRow().

◆ setCaretColumn()

void fcn::Text::setCaretColumn ( int column)
virtual

Sets the column the caret should be in.

The column will be clamp to the current row.

Parameters
columnThe column the caret should be in.

Definition at line 278 of file text.cpp.

References calculateCaretPositionFromRowAndColumn(), mCaretColumn, mCaretRow, and mRows.

Referenced by remove(), setCaretPosition(), and setCaretRow().

◆ setCaretPosition() [1/2]

void fcn::Text::setCaretPosition ( int position)
virtual

Sets the caret position.

The position will be clamp to the dimension of the content.

Parameters
positionThe position of the caret.

Definition at line 225 of file text.cpp.

References mCaretColumn, mCaretPosition, mCaretRow, and mRows.

Referenced by insert(), and remove().

◆ setCaretPosition() [2/2]

void fcn::Text::setCaretPosition ( int x,
int y,
Font * font )
virtual

Sets the caret position given an x and y coordinate in pixels relative to the text.

The coordinates will be clamp to the content.

Parameters
xThe x coordinate of the caret.
yThe y coordinate of the caret.
fontThe font to use when calculating the position.

Definition at line 258 of file text.cpp.

References fcn::Font::getHeight(), fcn::Font::getStringIndexAt(), mCaretRow, mRows, setCaretColumn(), and setCaretRow().

◆ setCaretRow()

void fcn::Text::setCaretRow ( int row)
virtual

Sets the row the caret should be in.

If the row lies o outside of the text, the row will be set to zero or the maximum row depending on where the row lies outside of the text.

Calling this function trigger a recalculation of the caret column.

Parameters
rowThe row the caret should be in.

Definition at line 291 of file text.cpp.

References mCaretColumn, mCaretRow, mRows, and setCaretColumn().

Referenced by remove(), and setCaretPosition().

◆ setContent()

void fcn::Text::setContent ( std::string const & content)
virtual

Sets the content of the text.

Will completely remove any previous text and reset the caret position.

Parameters
contentThe content of the text.

Definition at line 41 of file text.cpp.

References mCaretColumn, mCaretPosition, mCaretRow, and mRows.

◆ setRow()

void fcn::Text::setRow ( unsigned int row,
std::string const & content )
virtual

Sets the content of a row.

Parameters
rowThe row to set the text of.
contentThe content of the row.
Exceptions
Exceptionwhen the row does not exist.

Definition at line 81 of file text.cpp.

References mRows.

Member Data Documentation

◆ mCaretColumn

unsigned int fcn::Text::mCaretColumn {0}
protected

Holds the column the caret is in.

This variable should always be valid.

Definition at line 297 of file text.hpp.

Referenced by calculateCaretPositionFromRowAndColumn(), getCaretColumn(), getCaretDimension(), getCaretX(), insert(), remove(), setCaretColumn(), setCaretPosition(), setCaretRow(), and setContent().

◆ mCaretPosition

unsigned int fcn::Text::mCaretPosition {0}
protected

Holds the position of the caret.

This variable should always be valid.

Definition at line 285 of file text.hpp.

Referenced by calculateCaretPositionFromRowAndColumn(), getCaretPosition(), remove(), setCaretPosition(), and setContent().

◆ mCaretRow

unsigned int fcn::Text::mCaretRow {0}
protected

Holds the row the caret is in.

This variable should always be valid.

Definition at line 291 of file text.hpp.

Referenced by calculateCaretPositionFromRowAndColumn(), getCaretDimension(), getCaretRow(), getCaretX(), getCaretY(), insert(), remove(), setCaretColumn(), setCaretPosition(), setCaretPosition(), setCaretRow(), and setContent().

◆ mRows


The documentation for this class was generated from the following files: