ManaPlus
text.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Douglas Boffey <[email protected]>
4  * Copyright (C) 2008-2009 The Mana World Development Team
5  * Copyright (C) 2009-2010 The Mana Developers
6  * Copyright (C) 2011-2018 The ManaPlus Developers
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "text.h"
25 
26 #include "configuration.h"
27 #include "textmanager.h"
28 
29 #include "gui/gui.h"
30 #include "gui/theme.h"
31 
32 #include "gui/fonts/font.h"
33 
34 #include "resources/imagerect.h"
35 
36 #include "resources/image/image.h"
37 
38 #include "utils/delete2.h"
39 
40 #include "debug.h"
41 
42 int Text::mInstances = 0;
44 
45 Text::Text(const std::string &text,
46  const int x, const int y,
47  const Graphics::Alignment alignment,
48  const Color *const color,
49  const Speech isSpeech,
50  Font *const font) :
51  mFont(font != nullptr ?
52  font : (gui != nullptr ? gui->getFont() : nullptr)),
53  mTextChunk(),
54  mX(x),
55  mY(y),
56  mWidth(mFont != nullptr ? mFont->getWidth(text) : 1),
57  mHeight(mFont != nullptr ? mFont->getHeight() : 1),
58  mXOffset(0),
59  mText(text),
60  mColor(color),
61  mOutlineColor(color != nullptr ? (isSpeech == Speech_true ?
62  *color : theme->getColor(ThemeColorId::OUTLINE, 255)) : Color()),
63  mIsSpeech(isSpeech),
64  mTextChanged(true)
65 {
66  if (textManager == nullptr)
67  {
69  if (theme != nullptr)
70  {
72  "bubble.xml",
73  "",
74  0,
75  8);
76  }
77  else
78  {
79  for (int f = 0; f < 9; f ++)
80  mBubble.grid[f] = nullptr;
81  }
82 
83  const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
84  for (int i = 0; i < 9; i++)
85  {
86  if (mBubble.grid[i] != nullptr)
87  mBubble.grid[i]->setAlpha(bubbleAlpha);
88  }
89  }
90  ++mInstances;
91 
92  switch (alignment)
93  {
94  case Graphics::LEFT:
95  mXOffset = 0;
96  break;
97  case Graphics::CENTER:
98  mXOffset = mWidth / 2;
99  break;
100  case Graphics::RIGHT:
101  mXOffset = mWidth;
102  break;
103  default:
104  break;
105  }
106  mX = x - mXOffset;
107  if (textManager != nullptr)
108  textManager->addText(this);
109 }
110 
112 {
113  if (textManager != nullptr)
114  textManager->removeText(this);
115  if (--mInstances == 0)
116  {
118  for (int f = 0; f < 9; f ++)
119  {
120  if (mBubble.grid[f] != nullptr)
121  {
122  mBubble.grid[f]->decRef();
123  mBubble.grid[f] = nullptr;
124  }
125  }
126  }
127 }
128 
129 void Text::setColor(const Color *const color)
130 {
131  mTextChanged = true;
132  if (mIsSpeech == Speech_true)
133  {
134  mColor = color;
135  mOutlineColor = *color;
136  }
137  else
138  {
139  mColor = color;
140  }
141 }
142 
143 void Text::adviseXY(const int x, const int y,
144  const Move move)
145 {
146  if ((textManager != nullptr) && move == Move_true)
147  {
148  textManager->moveText(this, x - mXOffset, y);
149  }
150  else
151  {
152  mX = x - mXOffset;
153  mY = y;
154  }
155 }
156 
157 void Text::draw(Graphics *const graphics, const int xOff, const int yOff)
158 {
159  BLOCK_START("Text::draw")
160  if (mIsSpeech == Speech_true)
161  {
162  graphics->drawImageRect(mX - xOff - 5,
163  mY - yOff - 5,
164  mWidth + 10,
165  mHeight + 10,
166  mBubble);
167  }
168 
169  if (mTextChanged)
170  {
177  mTextChanged = false;
178  }
179 
180  const Image *const image = mTextChunk.img;
181  if (image != nullptr)
182  graphics->drawImage(image, mX - xOff, mY - yOff);
183 
184  BLOCK_END("Text::draw")
185 }
186 
187 FlashText::FlashText(const std::string &text,
188  const int x, const int y,
189  const Graphics::Alignment alignment,
190  const Color *const color,
191  Font *const font) :
192  Text(text, x, y, alignment, color, Speech_false, font),
193  mTime(0)
194 {
195 }
196 
197 void FlashText::draw(Graphics *const graphics, const int xOff, const int yOff)
198 {
199  BLOCK_START("FlashText::draw")
200  if (mTime != 0)
201  {
202  if ((--mTime & 4) == 0)
203  {
204  BLOCK_END("FlashText::draw")
205  return;
206  }
207  }
208  Text::draw(graphics, xOff, yOff);
209  BLOCK_END("FlashText::draw")
210 }
void moveText(Text *const text, const int x, const int y) const
Definition: textmanager.cpp:47
void loadRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:882
void addText(Text *const text)
Definition: textmanager.cpp:39
std::string text
Definition: textchunk.h:64
void setColor(const Color *const color)
Definition: text.cpp:129
TextManager * textManager
Definition: textmanager.cpp:32
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
int mXOffset
Definition: text.h:94
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
#define BLOCK_START(name)
Definition: perfomance.h:78
friend class TextManager
Definition: text.h:40
Configuration config
static int mInstances
Definition: text.h:95
#define BLOCK_END(name)
Definition: perfomance.h:79
float getFloatValue(const std::string &key) const
#define delete2(var)
Definition: delete2.h:24
Font * textFont
Definition: textchunk.h:63
int mX
Definition: text.h:90
Font * mFont
Definition: text.h:88
int mY
Definition: text.h:91
virtual void setAlpha(const float alpha)
Definition: image.cpp:286
const Color * mColor
Definition: text.h:97
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)
Definition: text.cpp:45
Image * grid[9]
Definition: imagerect.h:41
static ImageRect mBubble
Definition: text.h:103
Color mOutlineColor
Definition: text.h:98
#define nullptr
Definition: localconsts.h:44
bool move(InputEvent &event)
Definition: commands.cpp:43
bool mTextChanged
Definition: text.h:100
int mTime
Definition: text.h:130
const DyeColor * getColor(const std::string &name)
Definition: palettedb.cpp:108
const bool Move_true
Definition: move.h:29
const bool Speech_true
Definition: speech.h:29
void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: text.cpp:197
void removeText(const Text *const text)
Definition: textmanager.cpp:57
TextChunk mTextChunk
Definition: text.h:89
Theme * theme
Definition: theme.cpp:61
int mHeight
Definition: text.h:93
Image * img
Definition: textchunk.h:62
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
const bool Speech_false
Definition: speech.h:29
Definition: image.h:61
Color color
Definition: textchunk.h:65
Definition: color.h:74
virtual ~Text()
Definition: text.cpp:111
void deleteImage()
Definition: textchunk.cpp:192
Color color2
Definition: textchunk.h:66
void adviseXY(const int x, const int y, const Move move)
Definition: text.cpp:143
void decRef()
Definition: image.cpp:522
std::string mText
Definition: text.h:96
Speech mIsSpeech
Definition: text.h:99
FlashText(const std::string &text, const int x, const int y, const Graphics::Alignment alignment, const Color *const color, Font *const font=0)
Definition: text.cpp:187
virtual void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: text.cpp:157
Definition: text.h:38
void generate(TextChunk &chunk)
Definition: font.cpp:429
int mWidth
Definition: text.h:92