GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/spellpopup.cpp Lines: 15 51 29.4 %
Date: 2017-11-29 Branches: 10 46 21.7 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008  The Legend of Mazzeroth Development Team
4
 *  Copyright (C) 2009  The Mana World Development Team
5
 *  Copyright (C) 2009-2010  Andrei Karas
6
 *  Copyright (C) 2011-2017  The ManaPlus Developers
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/popups/spellpopup.h"
25
26
#include "textcommand.h"
27
28
#include "gui/gui.h"
29
30
#include "gui/widgets/label.h"
31
32
#include "debug.h"
33
34
SpellPopup *spellPopup = nullptr;
35
36
4
SpellPopup::SpellPopup() :
37
    Popup("SpellPopup", "spellpopup.xml"),
38

4
    mItemName(new Label(this)),
39


32
    mItemComment(new Label(this))
40
{
41
4
    mItemName->setFont(boldFont);
42
12
    mItemName->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
43
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
44
12
    mItemComment->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
45
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
46
4
}
47
48
4
void SpellPopup::postInit()
49
{
50
8
    Popup::postInit();
51
4
    add(mItemName);
52
4
    add(mItemComment);
53
54
4
    addMouseListener(this);
55
4
}
56
57
4
SpellPopup::~SpellPopup()
58
{
59
4
}
60
61
void SpellPopup::setItem(const TextCommand *const spell)
62
{
63
    if (spell != nullptr)
64
    {
65
        mItemName->setCaption(spell->getName());
66
        mItemComment->setCaption(spell->getComment());
67
    }
68
    else
69
    {
70
        mItemName->setCaption("?");
71
        mItemComment->setCaption("");
72
    }
73
74
    mItemName->adjustSize();
75
    mItemComment->adjustSize();
76
    int minWidth = mItemName->getWidth();
77
    if (mItemComment->getWidth() > minWidth)
78
        minWidth = mItemComment->getWidth();
79
80
    mItemName->setPosition(0, 0);
81
    const int fontHeight = mItemName->getHeight();
82
    mItemComment->setPosition(0, fontHeight);
83
    if (!mItemComment->getCaption().empty())
84
        setContentSize(minWidth, 2 * fontHeight);
85
    else
86
        setContentSize(minWidth, fontHeight);
87
}
88
89
void SpellPopup::view(const int x, const int y)
90
{
91
    const int distance = 20;
92
93
    int posX = std::max(0, x - getWidth() / 2);
94
    int posY = y + distance;
95
96
    const Rect &rect = mDimension;
97
    const int w = rect.width;
98
    const int h = rect.height;
99
    if (posX + w > mainGraphics->mWidth)
100
    {
101
        if (mainGraphics->mWidth > w)
102
            posX = mainGraphics->mWidth - w;
103
        else
104
            posX = 0;
105
    }
106
    if (posY + h > mainGraphics->mHeight)
107
    {
108
        if (y > h + distance)
109
            posY = y - h - distance;
110
    }
111
112
    setPosition(posX, posY);
113
    setVisible(Visible_true);
114
    requestMoveToTop();
115
}
116
117
void SpellPopup::mouseMoved(MouseEvent &event)
118
{
119
    Popup::mouseMoved(event);
120
121
    // When the mouse moved on top of the popup, hide it
122
    setVisible(Visible_false);
123
}