GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textpreview.cpp Lines: 28 62 45.2 %
Date: 2018-07-14 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-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus 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 "gui/widgets/textpreview.h"
24
25
#include "settings.h"
26
27
#include "gui/gui.h"
28
#include "gui/skin.h"
29
30
#include "gui/fonts/font.h"
31
32
#include "render/graphics.h"
33
34
#include "debug.h"
35
36
int TextPreview::instances = 0;
37
float TextPreview::mAlpha = 1.0;
38
Skin *TextPreview::mSkin = nullptr;
39
40
2
TextPreview::TextPreview(const Widget2 *const widget,
41
2
                         const std::string &text) :
42
    Widget(widget),
43
4
    mFont(gui->getFont()),
44
    mText(text),
45
4
    mTextColor(&getThemeColor(ThemeColorId::TEXT, 255U)),
46
4
    mTextColor2(&getThemeColor(ThemeColorId::TEXT_OUTLINE, 255U)),
47
4
    mBGColor(&getThemeColor(ThemeColorId::BACKGROUND, 255U)),
48
    mTextBGColor(nullptr),
49
    mPadding(0),
50
    mTextAlpha(false),
51
    mOpaque(Opaque_false),
52
    mShadow(false),
53
12
    mOutline(false)
54
{
55
2
    mAllowLogic = false;
56
2
    if (instances == 0)
57
    {
58
2
        if (theme != nullptr)
59
        {
60

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

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