ManaPlus
Public Member Functions | Static Protected Attributes | Private Attributes | Static Private Attributes | Friends
Text Class Reference

#include <text.h>

Inheritance diagram for Text:
FlashText

Public Member Functions

 Text (const std::string &text, const int x, const int y, const Graphics::Alignment alignment, const Color *const color, const Speech isSpeech, Font *const font)
 
virtual ~Text ()
 
void setColor (const Color *const color)
 
int getWidth () const
 
int getHeight () const
 
void adviseXY (const int x, const int y, const Move move)
 
virtual void draw (Graphics *const graphics, const int xOff, const int yOff)
 
int getX () const
 
int getY () const
 

Static Protected Attributes

static ImageRect mBubble
 

Private Attributes

FontmFont
 
TextChunk mTextChunk
 
int mX
 
int mY
 
int mWidth
 
int mHeight
 
int mXOffset
 
std::string mText
 
const ColormColor
 
Color mOutlineColor
 
Speech mIsSpeech
 
bool mTextChanged
 

Static Private Attributes

static int mInstances = 0
 

Friends

class TextManager
 

Detailed Description

Definition at line 39 of file text.h.

Constructor & Destructor Documentation

◆ Text()

Text::Text ( const std::string &  text,
const int  x,
const int  y,
const Graphics::Alignment  alignment,
const Color *const  color,
const Speech  isSpeech,
Font *const  font 
)

Constructor creates a text object to display on the screen.

Definition at line 46 of file text.cpp.

51  :
52  mFont(font != nullptr ?
53  font : (gui != nullptr ? gui->getFont() : nullptr)),
54  mTextChunk(),
55  mX(x),
56  mY(y),
57  mWidth(mFont != nullptr ? mFont->getWidth(text) : 1),
58  mHeight(mFont != nullptr ? mFont->getHeight() : 1),
59  mXOffset(0),
60  mText(text),
61  mColor(color),
62  mOutlineColor(color != nullptr ? (isSpeech == Speech_true ?
63  *color : theme->getColor(ThemeColorId::OUTLINE, 255)) : Color()),
64  mIsSpeech(isSpeech),
65  mTextChanged(true)
66 {
67  if (textManager == nullptr)
68  {
70  if (theme != nullptr)
71  {
73  "bubble.xml",
74  "",
75  0,
76  8);
77  }
78  else
79  {
80  for (int f = 0; f < 9; f ++)
81  mBubble.grid[f] = nullptr;
82  }
83 
84  const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
85  for (int i = 0; i < 9; i++)
86  {
87  if (mBubble.grid[i] != nullptr)
88  mBubble.grid[i]->setAlpha(bubbleAlpha);
89  }
90  }
91  ++mInstances;
92 
93  switch (alignment)
94  {
95  case Graphics::LEFT:
96  mXOffset = 0;
97  break;
98  case Graphics::CENTER:
99  mXOffset = mWidth / 2;
100  break;
101  case Graphics::RIGHT:
102  mXOffset = mWidth;
103  break;
104  default:
105  break;
106  }
107  mX = x - mXOffset;
108  if (textManager != nullptr)
109  textManager->addText(this);
110 }
Definition: color.h:76
float getFloatValue(const std::string &key) const
int getHeight() const
Definition: font.cpp:362
int getWidth(const std::string &text) const
Definition: font.cpp:334
@ CENTER
Definition: graphics.h:132
Font * getFont() const
Definition: gui.h:160
Image * grid[9]
Definition: imagerect.h:42
void addText(Text *const text)
Definition: textmanager.cpp:40
const Color * mColor
Definition: text.h:98
int mXOffset
Definition: text.h:95
std::string mText
Definition: text.h:97
static int mInstances
Definition: text.h:96
TextChunk mTextChunk
Definition: text.h:90
Font * mFont
Definition: text.h:89
int mX
Definition: text.h:91
Speech mIsSpeech
Definition: text.h:100
friend class TextManager
Definition: text.h:41
bool mTextChanged
Definition: text.h:101
int mWidth
Definition: text.h:93
int mHeight
Definition: text.h:94
Color mOutlineColor
Definition: text.h:99
static ImageRect mBubble
Definition: text.h:104
int mY
Definition: text.h:92
const Color & getColor(const ThemeColorIdT type, const unsigned int alpha)
Definition: theme.h:136
void loadRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:883
Configuration config
Gui * gui
Definition: gui.cpp:111
const bool Speech_true
Definition: speech.h:30
TextManager * textManager
Definition: textmanager.cpp:33
Theme * theme
Definition: theme.cpp:62

References TextManager::addText(), Graphics::CENTER, config, Configuration::getFloatValue(), ImageRect::grid, Graphics::LEFT, Theme::loadRect(), mBubble, mInstances, mWidth, mX, mXOffset, Graphics::RIGHT, TextManager, textManager, theme, and x.

◆ ~Text()

Text::~Text ( )
virtual

Destructor. The text is removed from the screen.

Definition at line 112 of file text.cpp.

113 {
114  if (textManager != nullptr)
115  textManager->removeText(this);
116  if (--mInstances == 0)
117  {
119  for (int f = 0; f < 9; f ++)
120  {
121  if (mBubble.grid[f] != nullptr)
122  {
123  mBubble.grid[f]->decRef();
124  mBubble.grid[f] = nullptr;
125  }
126  }
127  }
128 }
void removeText(const Text *const text)
Definition: textmanager.cpp:58
#define delete2(var)
Definition: delete2.h:25

References delete2, ImageRect::grid, mBubble, mInstances, TextManager::removeText(), and textManager.

Member Function Documentation

◆ adviseXY()

void Text::adviseXY ( const int  x,
const int  y,
const Move  move 
)

Allows the originator of the text to specify the ideal coordinates.

Definition at line 144 of file text.cpp.

146 {
147  if ((textManager != nullptr) && move == Move_true)
148  {
149  textManager->moveText(this, x - mXOffset, y);
150  }
151  else
152  {
153  mX = x - mXOffset;
154  mY = y;
155  }
156 }
void moveText(Text *const text, const int x, const int y) const
Definition: textmanager.cpp:48
const bool Move_true
Definition: move.h:30
bool move(InputEvent &event)
Definition: commands.cpp:44

References Actions::move(), Move_true, TextManager::moveText(), mX, mXOffset, mY, textManager, x, and y.

Referenced by Being::updateCoords().

◆ draw()

void Text::draw ( Graphics *const  graphics,
const int  xOff,
const int  yOff 
)
virtual

Draws the text.

Reimplemented in FlashText.

Definition at line 158 of file text.cpp.

159 {
160  BLOCK_START("Text::draw")
162  {
163  graphics->drawImageRect(mX - xOff - 5,
164  mY - yOff - 5,
165  mWidth + 10,
166  mHeight + 10,
167  mBubble);
168  }
169 
170  if (mTextChanged)
171  {
178  mTextChanged = false;
179  }
180 
181  const Image *const image = mTextChunk.img;
182  if (image != nullptr)
183  graphics->drawImage(image, mX - xOff, mY - yOff);
184 
185  BLOCK_END("Text::draw")
186 }
void generate(TextChunk &chunk)
Definition: font.cpp:430
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
void deleteImage()
Definition: textchunk.cpp:193
Font * textFont
Definition: textchunk.h:64
Image * img
Definition: textchunk.h:63
Color color
Definition: textchunk.h:66
Color color2
Definition: textchunk.h:67
std::string text
Definition: textchunk.h:65
if(!vert) return
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79

References BLOCK_END, BLOCK_START, TextChunk::color, TextChunk::color2, TextChunk::deleteImage(), Graphics::drawImage(), Graphics::drawImageRect(), Font::generate(), TextChunk::img, mBubble, mColor, mFont, mHeight, mIsSpeech, mOutlineColor, mText, mTextChanged, mTextChunk, mWidth, mX, mY, Speech_true, TextChunk::text, and TextChunk::textFont.

Referenced by FlashText::draw().

◆ getHeight()

int Text::getHeight ( ) const
inline

Definition at line 66 of file text.h.

67  { return mHeight; }

References mHeight.

Referenced by Being::updateBadgesPosition(), and Being::updateCoords().

◆ getWidth()

int Text::getWidth ( ) const
inline

Definition at line 63 of file text.h.

64  { return mWidth; }

References mWidth.

Referenced by Being::updateBadgesPosition().

◆ getX()

int Text::getX ( ) const
inline

Definition at line 82 of file text.h.

83  { return mX; }

References mX.

Referenced by Being::updateBadgesPosition().

◆ getY()

int Text::getY ( ) const
inline

Definition at line 85 of file text.h.

86  { return mY; }

References mY.

Referenced by Being::updateBadgesPosition().

◆ setColor()

void Text::setColor ( const Color *const  color)

Definition at line 130 of file text.cpp.

131 {
132  mTextChanged = true;
133  if (mIsSpeech == Speech_true)
134  {
135  mColor = color;
136  mOutlineColor = *color;
137  }
138  else
139  {
140  mColor = color;
141  }
142 }

References mColor, mIsSpeech, mOutlineColor, mTextChanged, and Speech_true.

Referenced by Being::updateColors().

Friends And Related Function Documentation

◆ TextManager

friend class TextManager
friend

Definition at line 41 of file text.h.

Referenced by Text().

Field Documentation

◆ mBubble

ImageRect Text::mBubble
staticprotected

Speech bubble graphic

Definition at line 104 of file text.h.

Referenced by draw(), Text(), and ~Text().

◆ mColor

const Color* Text::mColor
private

The color of the text.

Definition at line 98 of file text.h.

Referenced by draw(), and setColor().

◆ mFont

Font* Text::mFont
private

The font of the text

Definition at line 89 of file text.h.

Referenced by draw().

◆ mHeight

int Text::mHeight
private

The height of the text.

Definition at line 94 of file text.h.

Referenced by TextManager::addText(), draw(), getHeight(), TextManager::moveText(), and TextManager::place().

◆ mInstances

int Text::mInstances = 0
staticprivate

Instances of text.

Definition at line 96 of file text.h.

Referenced by Text(), and ~Text().

◆ mIsSpeech

Speech Text::mIsSpeech
private

Is this text a speech bubble?

Definition at line 100 of file text.h.

Referenced by draw(), and setColor().

◆ mOutlineColor

Color Text::mOutlineColor
private

Definition at line 99 of file text.h.

Referenced by draw(), and setColor().

◆ mText

std::string Text::mText
private

The text to display.

Definition at line 97 of file text.h.

Referenced by draw().

◆ mTextChanged

bool Text::mTextChanged
private

Definition at line 101 of file text.h.

Referenced by draw(), and setColor().

◆ mTextChunk

TextChunk Text::mTextChunk
private

Definition at line 90 of file text.h.

Referenced by draw().

◆ mWidth

int Text::mWidth
private

The width of the text.

Definition at line 93 of file text.h.

Referenced by draw(), getWidth(), TextManager::place(), and Text().

◆ mX

int Text::mX
private

Actual x-value of left of text written.

Definition at line 91 of file text.h.

Referenced by TextManager::addText(), adviseXY(), draw(), getX(), TextManager::moveText(), TextManager::place(), and Text().

◆ mXOffset

int Text::mXOffset
private

The offset of mX from the desired x.

Definition at line 95 of file text.h.

Referenced by adviseXY(), and Text().

◆ mY

int Text::mY
private

Actual y-value of top of text written.

Definition at line 92 of file text.h.

Referenced by TextManager::addText(), adviseXY(), draw(), getY(), TextManager::moveText(), and TextManager::place().


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