GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/text.cpp Lines: 44 81 54.3 %
Date: 2021-03-17 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-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;
44
ImageRect Text::mBubble;
45
46
9
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
9
           Font *const font) :
52
9
    mFont(font != nullptr ?
53
16
        font : (gui != nullptr ? gui->getFont() : nullptr)),
54
    mTextChunk(),
55
    mX(x),
56
    mY(y),
57

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

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

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

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

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

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

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