Mana
Loading...
Searching...
No Matches
text.cpp
Go to the documentation of this file.
1/*
2 * Support for non-overlapping floating text
3 * Copyright (C) 2008 Douglas Boffey <DougABoffey@netscape.net>
4 * Copyright (C) 2008-2009 The Mana World Development Team
5 * Copyright (C) 2009-2012 The Mana Developers
6 *
7 * This file is part of The Mana Client.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "text.h"
24
25#include "textmanager.h"
26
27#include "gui/gui.h"
28
29#include "resources/theme.h"
30
31#include <guichan/font.hpp>
32
33int Text::mInstances = 0;
34
35Text::Text(const std::string &text,
36 int x,
37 int y,
38 gcn::Graphics::Alignment alignment,
39 const gcn::Color *color,
40 bool isSpeech,
41 gcn::Font *font)
42 : mText(text)
43 , mColor(color)
44 , mFont(font ? font : gui->getFont())
45 , mIsSpeech(isSpeech)
46{
47 if (textManager == nullptr)
49
50 ++mInstances;
51
52 mHeight = mFont->getHeight();
53 mWidth = mFont->getWidth(text);
54
55 switch (alignment)
56 {
57 case gcn::Graphics::LEFT:
58 mXOffset = 0;
59 break;
60 case gcn::Graphics::CENTER:
61 mXOffset = mWidth / 2;
62 break;
63 case gcn::Graphics::RIGHT:
65 break;
66 }
67
68 mX = x - mXOffset;
69 mY = y;
70
71 textManager->addText(this);
72}
73
75{
77 if (--mInstances == 0)
78 {
79 delete textManager;
80 textManager = nullptr;
81 }
82}
83
84void Text::setColor(const gcn::Color *color)
85{
86 mColor = color;
87}
88
89void Text::adviseXY(int x, int y)
90{
91 textManager->moveText(this, x - mXOffset, y);
92}
93
94void Text::draw(gcn::Graphics *graphics, int xOff, int yOff)
95{
96 auto g = static_cast<Graphics *>(graphics);
97 if (mIsSpeech)
98 {
99 WidgetState state;
100 state.x = mX - xOff - 5;
101 state.y = mY - yOff - 5;
102 state.width = mWidth + 10;
103 state.height = mHeight + 10;
104
105 auto theme = gui->getTheme();
106 theme->drawSkin(g, SkinType::SpeechBubble, state);
107
108 /*
109 if (mWidth >= 15)
110 {
111 static_cast<Graphics*>(graphics)->drawImage(
112 mBubbleArrow, mX - xOff - 7 + mWidth / 2,
113 mY - yOff + mHeight + 4);
114 }
115 */
116 }
117
118 g->drawText(mText,
119 mX - xOff, mY - yOff, gcn::Graphics::LEFT,
120 *mColor, mFont, !mIsSpeech, true);
121}
122
123
124void FlashText::draw(gcn::Graphics *graphics, int xOff, int yOff)
125{
126 if (mTimer.isSet())
127 {
128 if (!mTimer.passed())
129 {
130 if ((mTimer.elapsed() & 64) == 0)
131 return;
132 }
133 else
134 {
135 mTimer.reset();
136 }
137 }
138
139 Text::draw(graphics, xOff, yOff);
140}
Timer mTimer
Time left for flashing.
Definition text.h:95
void draw(gcn::Graphics *graphics, int xOff, int yOff) override
Draws the text.
Definition text.cpp:124
A central point of control for graphics.
Definition graphics.h:78
Theme * getTheme() const
The global GUI theme.
Definition gui.h:138
void addText(Text *text)
Add text to the manager.
void moveText(Text *text, int x, int y)
Move the text around the screen.
void removeText(const Text *text)
Remove the text from the manager.
Text(const std::string &text, int x, int y, gcn::Graphics::Alignment alignment, const gcn::Color *color, bool isSpeech=false, gcn::Font *font=nullptr)
Constructor creates a text object to display on the screen.
Definition text.cpp:35
int mXOffset
The offset of mX from the desired x.
Definition text.h:71
std::string mText
The text to display.
Definition text.h:73
const gcn::Color * mColor
The color of the text.
Definition text.h:74
virtual ~Text()
Destructor.
Definition text.cpp:74
static int mInstances
Instances of text.
Definition text.h:72
bool mIsSpeech
Is this text a speech bubble?
Definition text.h:76
gcn::Font * mFont
The font of the text.
Definition text.h:75
int mX
Actual x-value of left of text written.
Definition text.h:67
friend class TextManager
Definition text.h:35
void setColor(const gcn::Color *color)
Definition text.cpp:84
int mWidth
The width of the text.
Definition text.h:69
int mHeight
The height of the text.
Definition text.h:70
void adviseXY(int x, int y)
Allows the originator of the text to specify the ideal coordinates.
Definition text.cpp:89
int mY
Actual y-value of top of text written.
Definition text.h:68
virtual void draw(gcn::Graphics *graphics, int xOff, int yOff)
Draws the text.
Definition text.cpp:94
void drawSkin(Graphics *graphics, SkinType type, const WidgetState &state) const
Definition theme.cpp:373
bool passed() const
Returns whether the timer has passed.
Definition time.h:88
int32_t elapsed() const
Returns the number of milliseconds elapsed since the set time, or a negative value if the timer hasn'...
Definition time.cpp:65
void reset()
Reset the timer.
Definition time.h:75
bool isSet() const
Returns whether the timer has been set.
Definition time.h:81
Graphics * graphics
Definition client.cpp:104
Gui * gui
The GUI system.
Definition gui.cpp:50
int width
Definition theme.h:148
int height
Definition theme.h:149
TextManager * textManager