GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/textpopup.cpp Lines: 19 53 35.8 %
Date: 2021-03-17 Branches: 7 22 31.8 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  The Legend of Mazzeroth Development Team
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 "gui/popups/textpopup.h"
26
27
#include "gui/widgets/label.h"
28
29
#include "gui/fonts/font.h"
30
31
#include "debug.h"
32
33
TextPopup *textPopup = nullptr;
34
35
2
TextPopup::TextPopup() :
36
    Popup("TextPopup", "textpopup.xml"),
37

14
    mText()
38
{
39
2
}
40
41
2
void TextPopup::postInit()
42
{
43
4
    Popup::postInit();
44
2
    const int fontHeight = getFont()->getHeight();
45
2
    int y = 0;
46
8
    for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
47
    {
48
6
        Label *const label = new Label(this);
49
6
        mText[f] = label;
50
6
        label->setPosition(0, y);
51
18
        label->setForegroundColorAll(
52
        getThemeColor(ThemeColorId::POPUP, 255U),
53
6
        getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
54
6
        add(label);
55
6
        y += fontHeight;
56
    }
57
2
    addMouseListener(this);
58
2
}
59
60
2
TextPopup::~TextPopup()
61
{
62
2
}
63
64
void TextPopup::show(const int x, const int y, const std::string &str1,
65
                     const std::string &str2, const std::string &str3)
66
{
67
    mText[0]->setCaption(str1);
68
    mText[1]->setCaption(str2);
69
    mText[2]->setCaption(str3);
70
71
    int minWidth = 0;
72
    for (int f = 0; f < TEXTPOPUPCOUNT; f ++)
73
    {
74
        Label *const label = mText[f];
75
        label->adjustSize();
76
        const int width = label->getWidth();
77
        if (width > minWidth)
78
            minWidth = width;
79
    }
80
81
    const int pad2 = 2 * mPadding;
82
    minWidth += pad2;
83
    setWidth(minWidth);
84
85
    int cnt = 1;
86
    if (!str2.empty())
87
        cnt ++;
88
    if (!str3.empty())
89
        cnt ++;
90
91
    setHeight(pad2 + mText[0]->getFont()->getHeight() * cnt);
92
    const int distance = 20;
93
94
    const Rect &rect = mDimension;
95
    int posX = std::max(0, x - rect.width / 2);
96
    int posY = y + distance;
97
98
    if (posX + rect.width > mainGraphics->mWidth)
99
        posX = mainGraphics->mWidth - rect.width;
100
    if (posY + rect.height > mainGraphics->mHeight)
101
        posY = y - rect.height - distance;
102
103
    setPosition(posX, posY);
104
    setVisible(Visible_true);
105
    requestMoveToTop();
106
}
107
108
void TextPopup::mouseMoved(MouseEvent &event)
109
{
110
    Popup::mouseMoved(event);
111
112
    // When the mouse moved on top of the popup, hide it
113
    setVisible(Visible_false);
114
}