FifeGUI 0.3.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
virtual ~Text ()

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 31 of file text.hpp.

Constructor & Destructor Documentation

◆ Text() [1/2]

fcn::Text::Text ( )

Definition at line 25 of file text.cpp.

◆ Text() [2/2]

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

Constructor.

Parameters
contentThe content of the text.

Definition at line 31 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 112 of file text.cpp.

References mRows, and fcn::throwException().

Referenced by insertRow(), and operator=().

◆ calculateCaretPositionFromRowAndColumn()

void fcn::Text::calculateCaretPositionFromRowAndColumn ( )
protected

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

Definition at line 449 of file text.cpp.

References mCaretColumn, mCaretPosition, mCaretRow, and mRows.

Referenced by operator=(), and 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 146 of file text.cpp.

References mRows, and fcn::throwException().

Referenced by operator=().

◆ 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 324 of file text.cpp.

References mCaretColumn.

Referenced by operator=().

◆ 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 400 of file text.cpp.

References fcn::Rectangle::height, mCaretColumn, mCaretRow, mRows, fcn::Rectangle::width, fcn::Rectangle::x, and fcn::Rectangle::y.

Referenced by operator=().

◆ getCaretPosition()

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

Gets the caret position.

Returns
The caret position.

Definition at line 250 of file text.cpp.

References mCaretPosition.

Referenced by insert(), and operator=().

◆ 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 329 of file text.cpp.

References mCaretRow.

Referenced by operator=().

◆ 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 360 of file text.cpp.

References mCaretColumn, mCaretRow, and mRows.

Referenced by operator=().

◆ 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 371 of file text.cpp.

References mCaretRow.

Referenced by operator=().

◆ getContent()

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

Gets the content of the text.

Returns
The content of the text.

Definition at line 86 of file text.cpp.

References mRows.

Referenced by operator=().

◆ 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 379 of file text.cpp.

References mRows.

Referenced by operator=().

◆ 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 418 of file text.cpp.

Referenced by operator=().

◆ 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 423 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 428 of file text.cpp.

References mRows.

Referenced by operator=(), remove(), and setCaretPosition().

◆ 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 440 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 435 of file text.cpp.

References mRows.

Referenced by operator=().

◆ 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 155 of file text.cpp.

References mRows, and fcn::throwException().

Referenced by operator=().

◆ 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 413 of file text.cpp.

Referenced by operator=().

◆ insert()

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

Inserts a character at the current caret position.

Parameters
characterThe character to insert.

Definition at line 164 of file text.cpp.

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

Referenced by operator=().

◆ 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 124 of file text.cpp.

References addRow(), mRows, and fcn::throwException().

Referenced by operator=().

◆ 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 191 of file text.cpp.

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

Referenced by operator=().

◆ 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 334 of file text.cpp.

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

Referenced by operator=(), 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 255 of file text.cpp.

References getNumberOfCharacters(), mCaretColumn, mCaretPosition, mCaretRow, and mRows.

Referenced by insert(), operator=(), 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 311 of file text.cpp.

References 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 347 of file text.cpp.

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

Referenced by operator=(), 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 57 of file text.cpp.

References mCaretColumn, mCaretPosition, mCaretRow, and mRows.

Referenced by operator=().

◆ 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 102 of file text.cpp.

References mRows, and fcn::throwException().

Referenced by operator=().

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 319 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 307 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 313 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: