GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/text.cpp Lines: 44 81 54.3 %
Date: 2018-07-14 Branches: 34 78 43.6 %

Line Branch Exec Source
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;
43
ImageRect Text::mBubble;
44
45
9
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
9
           Font *const font) :
51
9
    mFont(font != nullptr ?
52
16
        font : (gui != nullptr ? gui->getFont() : nullptr)),
53
    mTextChunk(),
54
    mX(x),
55
    mY(y),
56

9
    mWidth(mFont != nullptr ? mFont->getWidth(text) : 1),
57

9
    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
54
    mTextChanged(true)
65
{
66
9
    if (textManager == nullptr)
67
    {
68

6
        textManager = new TextManager;
69
6
        if (theme != nullptr)
70
        {
71

42
            theme->loadRect(mBubble,
72
                "bubble.xml",
73
                "",
74
                0,
75
6
                8);
76
        }
77
        else
78
        {
79
            for (int f = 0; f < 9; f ++)
80
                mBubble.grid[f] = nullptr;
81
        }
82
83

24
        const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
84
60
        for (int i = 0; i < 9; i++)
85
        {
86
54
            if (mBubble.grid[i] != nullptr)
87
54
                mBubble.grid[i]->setAlpha(bubbleAlpha);
88
        }
89
    }
90
9
    ++mInstances;
91
92

9
    switch (alignment)
93
    {
94
        case Graphics::LEFT:
95
            mXOffset = 0;
96
            break;
97
        case Graphics::CENTER:
98
9
            mXOffset = mWidth / 2;
99
9
            break;
100
        case Graphics::RIGHT:
101
            mXOffset = mWidth;
102
            break;
103
        default:
104
            break;
105
    }
106
9
    mX = x - mXOffset;
107
9
    if (textManager != nullptr)
108
9
        textManager->addText(this);
109
9
}
110
111
27
Text::~Text()
112
{
113
9
    if (textManager != nullptr)
114
9
        textManager->removeText(this);
115
9
    if (--mInstances == 0)
116
    {
117
6
        delete2(textManager);
118
60
        for (int f = 0; f < 9; f ++)
119
        {
120
54
            if (mBubble.grid[f] != nullptr)
121
            {
122
54
                mBubble.grid[f]->decRef();
123
54
                mBubble.grid[f] = nullptr;
124
            }
125
        }
126
    }
127
9
}
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
9
void Text::adviseXY(const int x, const int y,
144
                    const Move move)
145
{
146

9
    if ((textManager != nullptr) && move == Move_true)
147
    {
148
        textManager->moveText(this, x - mXOffset, y);
149
    }
150
    else
151
    {
152
9
        mX = x - mXOffset;
153
9
        mY = y;
154
    }
155
9
}
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
    {
171
        mTextChunk.textFont = mFont;
172
        mTextChunk.deleteImage();
173
        mTextChunk.text = mText;
174
        mTextChunk.color = *mColor;
175
        mTextChunk.color2 = mOutlineColor;
176
        mFont->generate(mTextChunk);
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
9
FlashText::FlashText(const std::string &text,
188
                     const int x, const int y,
189
                     const Graphics::Alignment alignment,
190
                     const Color *const color,
191
9
                     Font *const font) :
192
    Text(text, x, y, alignment, color, Speech_false, font),
193
9
    mTime(0)
194
{
195
9
}
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
2
}