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-2017 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  {
71  theme->loadRect(mBubble, "bubble.xml", "");
72  }
73  else
74  {
75  for (int f = 0; f < 9; f ++)
76  mBubble.grid[f] = nullptr;
77  }
78 
79  const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
80  for (int i = 0; i < 9; i++)
81  {
82  if (mBubble.grid[i] != nullptr)
83  mBubble.grid[i]->setAlpha(bubbleAlpha);
84  }
85  }
86  ++mInstances;
87 
88  switch (alignment)
89  {
90  case Graphics::LEFT:
91  mXOffset = 0;
92  break;
93  case Graphics::CENTER:
94  mXOffset = mWidth / 2;
95  break;
96  case Graphics::RIGHT:
97  mXOffset = mWidth;
98  break;
99  default:
100  break;
101  }
102  mX = x - mXOffset;
103  if (textManager != nullptr)
104  textManager->addText(this);
105 }
106 
108 {
109  if (textManager != nullptr)
110  textManager->removeText(this);
111  if (--mInstances == 0)
112  {
114  for (int f = 0; f < 9; f ++)
115  {
116  if (mBubble.grid[f] != nullptr)
117  {
118  mBubble.grid[f]->decRef();
119  mBubble.grid[f] = nullptr;
120  }
121  }
122  }
123 }
124 
125 void Text::setColor(const Color *const color)
126 {
127  mTextChanged = true;
128  if (mIsSpeech == Speech_true)
129  {
130  mColor = color;
131  mOutlineColor = *color;
132  }
133  else
134  {
135  mColor = color;
136  }
137 }
138 
139 void Text::adviseXY(const int x, const int y,
140  const Move move)
141 {
142  if ((textManager != nullptr) && move == Move_true)
143  {
144  textManager->moveText(this, x - mXOffset, y);
145  }
146  else
147  {
148  mX = x - mXOffset;
149  mY = y;
150  }
151 }
152 
153 void Text::draw(Graphics *const graphics, const int xOff, const int yOff)
154 {
155  BLOCK_START("Text::draw")
156  if (mIsSpeech == Speech_true)
157  {
158  graphics->drawImageRect(mX - xOff - 5,
159  mY - yOff - 5,
160  mWidth + 10,
161  mHeight + 10,
162  mBubble);
163  }
164 
165  if (mTextChanged)
166  {
173  mTextChanged = false;
174  }
175 
176  const Image *const image = mTextChunk.img;
177  if (image != nullptr)
178  graphics->drawImage(image, mX - xOff, mY - yOff);
179 
180  BLOCK_END("Text::draw")
181 }
182 
183 FlashText::FlashText(const std::string &text,
184  const int x, const int y,
185  const Graphics::Alignment alignment,
186  const Color *const color,
187  Font *const font) :
188  Text(text, x, y, alignment, color, Speech_false, font),
189  mTime(0)
190 {
191 }
192 
193 void FlashText::draw(Graphics *const graphics, const int xOff, const int yOff)
194 {
195  BLOCK_START("FlashText::draw")
196  if (mTime != 0)
197  {
198  if ((--mTime & 4) == 0)
199  {
200  BLOCK_END("FlashText::draw")
201  return;
202  }
203  }
204  Text::draw(graphics, xOff, yOff);
205  BLOCK_END("FlashText::draw")
206 }
void moveText(Text *const text, const int x, const int y) const
Definition: textmanager.cpp:47
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:125
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:285
const Color * mColor
Definition: text.h:97
Image * grid[9]
Definition: imagerect.h:41
Text(const std::string &text, const int x, const int y, const Graphics::Alignment alignment, const Color *const color, const Speech isSpeech=Speech_false, Font *const font=0)
Definition: text.cpp:45
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
void loadRect(ImageRect &image, const std::string &name, const std::string &name2, const int start=0, const int end=8)
Definition: theme.cpp:1092
int mTime
Definition: text.h:135
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:193
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:107
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:139
void decRef()
Definition: image.cpp:521
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:183
virtual void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: text.cpp:153
Definition: text.h:38
void generate(TextChunk &chunk)
Definition: font.cpp:428
int mWidth
Definition: text.h:92