GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/textpreview.cpp Lines: 27 61 44.3 %
Date: 2017-11-29 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-2017  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
4
TextPreview::TextPreview(const Widget2 *const widget,
41
4
                         const std::string &text) :
42
    Widget(widget),
43
8
    mFont(gui->getFont()),
44
    mText(text),
45
8
    mTextColor(&getThemeColor(ThemeColorId::TEXT)),
46
8
    mTextColor2(&getThemeColor(ThemeColorId::TEXT_OUTLINE)),
47
8
    mBGColor(&getThemeColor(ThemeColorId::BACKGROUND)),
48
    mTextBGColor(nullptr),
49
    mPadding(0),
50
    mTextAlpha(false),
51
    mOpaque(Opaque_false),
52
    mShadow(false),
53
24
    mOutline(false)
54
{
55
4
    mAllowLogic = false;
56
4
    if (instances == 0)
57
    {
58
4
        if (theme != nullptr)
59

32
            mSkin = theme->load("textpreview.xml", "");
60
    }
61
62
4
    instances++;
63
64
4
    if (mSkin != nullptr)
65

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