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-2019 The ManaPlus Developers
7  * Copyright (C) 2019-2021 Andrei Karas
8  *
9  * This file is part of The ManaPlus Client.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "text.h"
26 
27 #include "configuration.h"
28 #include "textmanager.h"
29 
30 #include "gui/gui.h"
31 #include "gui/theme.h"
32 
33 #include "gui/fonts/font.h"
34 
35 #include "resources/imagerect.h"
36 
37 #include "resources/image/image.h"
38 
39 #include "utils/delete2.h"
40 
41 #include "debug.h"
42 
43 int Text::mInstances = 0;
45 
46 Text::Text(const std::string &text,
47  const int x, const int y,
48  const Graphics::Alignment alignment,
49  const Color *const color,
50  const Speech isSpeech,
51  Font *const font) :
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 }
111 
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 }
129 
130 void Text::setColor(const Color *const color)
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 }
143 
144 void Text::adviseXY(const int x, const int y,
145  const Move move)
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 }
157 
158 void Text::draw(Graphics *const graphics, const int xOff, const int yOff)
159 {
160  BLOCK_START("Text::draw")
161  if (mIsSpeech == Speech_true)
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 }
187 
188 FlashText::FlashText(const std::string &text,
189  const int x, const int y,
190  const Graphics::Alignment alignment,
191  const Color *const color,
192  Font *const font) :
193  Text(text, x, y, alignment, color, Speech_false, font),
194  mTime(0)
195 {
196 }
197 
198 void FlashText::draw(Graphics *const graphics, const int xOff, const int yOff)
199 {
200  BLOCK_START("FlashText::draw")
201  if (mTime != 0)
202  {
203  if ((--mTime & 4) == 0)
204  {
205  BLOCK_END("FlashText::draw")
206  return;
207  }
208  }
209  Text::draw(graphics, xOff, yOff);
210  BLOCK_END("FlashText::draw")
211 }
Definition: color.h:76
float getFloatValue(const std::string &key) const
FlashText(const std::string &text, const int x, const int y, const Graphics::Alignment alignment, const Color *const color, Font *const font)
Definition: text.cpp:188
int mTime
Definition: text.h:131
void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: text.cpp:198
Definition: font.h:90
void generate(TextChunk &chunk)
Definition: font.cpp:430
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
@ CENTER
Definition: graphics.h:132
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
Image * grid[9]
Definition: imagerect.h:42
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
void removeText(const Text *const text)
Definition: textmanager.cpp:58
void moveText(Text *const text, const int x, const int y) const
Definition: textmanager.cpp:48
void addText(Text *const text)
Definition: textmanager.cpp:40
Definition: text.h:40
const Color * mColor
Definition: text.h:98
int mXOffset
Definition: text.h:95
std::string mText
Definition: text.h:97
virtual ~Text()
Definition: text.cpp:112
static int mInstances
Definition: text.h:96
TextChunk mTextChunk
Definition: text.h:90
void setColor(const Color *const color)
Definition: text.cpp:130
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
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:46
Color mOutlineColor
Definition: text.h:99
virtual void draw(Graphics *const graphics, const int xOff, const int yOff)
Definition: text.cpp:158
static ImageRect mBubble
Definition: text.h:104
int mY
Definition: text.h:92
void adviseXY(const int x, const int y, const Move move)
Definition: text.cpp:144
void loadRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:883
Configuration config
#define delete2(var)
Definition: delete2.h:25
bool Move
Definition: move.h:30
const bool Move_true
Definition: move.h:30
Gui * gui
Definition: gui.cpp:111
#define nullptr
Definition: localconsts.h:45
bool move(InputEvent &event)
Definition: commands.cpp:44
const DyeColor * getColor(const std::string &name)
Definition: palettedb.cpp:109
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
bool Speech
Definition: speech.h:30
const bool Speech_false
Definition: speech.h:30
const bool Speech_true
Definition: speech.h:30
TextManager * textManager
Definition: textmanager.cpp:33
Theme * theme
Definition: theme.cpp:62