GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/text.cpp Lines: 44 83 53.0 %
Date: 2017-11-29 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-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;
43
ImageRect Text::mBubble;
44
45
18
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
18
           Font *const font) :
51
18
    mFont(font != nullptr ?
52
32
        font : (gui != nullptr ? gui->getFont() : nullptr)),
53
    mTextChunk(),
54
    mX(x),
55
    mY(y),
56

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

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

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

84
            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

48
        const float bubbleAlpha = config.getFloatValue("speechBubbleAlpha");
80
120
        for (int i = 0; i < 9; i++)
81
        {
82
108
            if (mBubble.grid[i] != nullptr)
83
108
                mBubble.grid[i]->setAlpha(bubbleAlpha);
84
        }
85
    }
86
18
    ++mInstances;
87
88

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

18
    if ((textManager != nullptr) && move == Move_true)
143
    {
144
        textManager->moveText(this, x - mXOffset, y);
145
    }
146
    else
147
    {
148
18
        mX = x - mXOffset;
149
18
        mY = y;
150
    }
151
18
}
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
    {
167
        mTextChunk.textFont = mFont;
168
        mTextChunk.deleteImage();
169
        mTextChunk.text = mText;
170
        mTextChunk.color = *mColor;
171
        mTextChunk.color2 = mOutlineColor;
172
        mFont->generate(mTextChunk);
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
18
FlashText::FlashText(const std::string &text,
184
                     const int x, const int y,
185
                     const Graphics::Alignment alignment,
186
                     const Color *const color,
187
18
                     Font *const font) :
188
    Text(text, x, y, alignment, color, Speech_false, font),
189
18
    mTime(0)
190
{
191
18
}
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
4
}