GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textpreview.cpp Lines: 28 62 45.2 %
Date: 2021-03-17 Branches: 12 46 26.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2006-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
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 "gui/widgets/textpreview.h"
25
26
#include "settings.h"
27
28
#include "gui/gui.h"
29
#include "gui/skin.h"
30
31
#include "gui/fonts/font.h"
32
33
#include "render/graphics.h"
34
35
#include "debug.h"
36
37
int TextPreview::instances = 0;
38
float TextPreview::mAlpha = 1.0;
39
Skin *TextPreview::mSkin = nullptr;
40
41
2
TextPreview::TextPreview(const Widget2 *const widget,
42
2
                         const std::string &text) :
43
    Widget(widget),
44
4
    mFont(gui->getFont()),
45
    mText(text),
46
4
    mTextColor(&getThemeColor(ThemeColorId::TEXT, 255U)),
47
4
    mTextColor2(&getThemeColor(ThemeColorId::TEXT_OUTLINE, 255U)),
48
4
    mBGColor(&getThemeColor(ThemeColorId::BACKGROUND, 255U)),
49
    mTextBGColor(nullptr),
50
    mPadding(0),
51
    mTextAlpha(false),
52
    mOpaque(Opaque_false),
53
    mShadow(false),
54
12
    mOutline(false)
55
{
56
2
    mAllowLogic = false;
57
2
    if (instances == 0)
58
    {
59
2
        if (theme != nullptr)
60
        {
61

14
            mSkin = theme->load("textpreview.xml",
62
                "",
63
                true,
64
2
                Theme::getThemePath());
65
        }
66
    }
67
68
2
    instances++;
69
70
2
    if (mSkin != nullptr)
71

8
        mPadding = mSkin->getOption("padding", 0);
72
73
2
    adjustSize();
74
2
}
75
76
8
TextPreview::~TextPreview()
77
{
78
2
    if (gui != nullptr)
79
2
        gui->removeDragged(this);
80
81
2
    instances--;
82
83
2
    if (instances == 0)
84
    {
85
2
        if (theme != nullptr)
86
2
            theme->unload(mSkin);
87
    }
88
4
}
89
90
void TextPreview::draw(Graphics *const graphics)
91
{
92
    if (mFont == nullptr)
93
        return;
94
95
    BLOCK_START("TextPreview::draw")
96
    if (settings.guiAlpha != mAlpha)
97
        mAlpha = settings.guiAlpha;
98
99
    const int intAlpha = CAST_S32(mAlpha * 255.0F);
100
    const int alpha = mTextAlpha ? intAlpha : 255;
101
102
    if (mOpaque == Opaque_true)
103
    {
104
        graphics->setColor(Color(CAST_S32(mBGColor->r),
105
                    CAST_S32(mBGColor->g),
106
                    CAST_S32(mBGColor->b),
107
                    CAST_S32(mAlpha * 255.0F)));
108
        graphics->fillRectangle(Rect(0, 0,
109
            mDimension.width, mDimension.height));
110
    }
111
112
    if (mTextBGColor != nullptr)
113
    {
114
        const int x = mFont->getWidth(mText) + 1
115
            + 2 * ((mOutline || mShadow) ? 1 :0);
116
        const int y = mFont->getHeight() + 1
117
            + 2 * ((mOutline || mShadow) ? 1 : 0);
118
        graphics->setColor(Color(CAST_S32(mTextBGColor->r),
119
            CAST_S32(mTextBGColor->g),
120
            CAST_S32(mTextBGColor->b),
121
            intAlpha));
122
        graphics->fillRectangle(Rect(mPadding, mPadding, x, y));
123
    }
124
125
    Color color1(mTextColor->r, mTextColor->g, mTextColor->b, alpha);
126
127
    if (mOutline && mTextColor != mTextColor2)
128
    {
129
        const Color &color2 = getThemeColor(ThemeColorId::OUTLINE, 255U);
130
        mFont->drawString(graphics,
131
            color1,
132
            color2,
133
            mText,
134
            mPadding + 1, mPadding + 1);
135
    }
136
    else
137
    {
138
        Color color2(mTextColor2->r, mTextColor2->g, mTextColor2->b, alpha);
139
        mFont->drawString(graphics,
140
            color1,
141
            color2,
142
            mText,
143
            mPadding + 1, mPadding + 1);
144
    }
145
146
    BLOCK_END("TextPreview::draw")
147
}
148
149
void TextPreview::safeDraw(Graphics *const graphics)
150
{
151
    TextPreview::draw(graphics);
152
}
153
154
2
void TextPreview::adjustSize()
155
{
156
2
    setHeight(getFont()->getHeight() + 2 * mPadding);
157
2
}