GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/skillpopup.cpp Lines: 35 118 29.7 %
Date: 2017-11-29 Branches: 26 130 20.0 %

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-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/skillpopup.h"
25
26
#include "gui/gui.h"
27
28
#include "gui/fonts/font.h"
29
30
#include "gui/widgets/label.h"
31
#include "gui/widgets/textbox.h"
32
33
#include "utils/gettext.h"
34
#include "utils/stringutils.h"
35
36
#include "resources/skill/skilldata.h"
37
#include "resources/skill/skillinfo.h"
38
39
#include "debug.h"
40
41
SkillPopup *skillPopup = nullptr;
42
43
4
SkillPopup::SkillPopup() :
44
    Popup("SkillPopup", "skillpopup.xml"),
45

4
    mSkillName(new Label(this)),
46

4
    mSkillDesc(new TextBox(this)),
47

4
    mSkillEffect(new TextBox(this)),
48

4
    mSkillLevel(new TextBox(this)),
49

4
    mSkillCastType(new TextBox(this)),
50
    mCastType(CastType::Default),
51
    mLastId(0U),
52
    mLastLevel(-1),
53
    mOffsetX(0),
54

48
    mOffsetY(0)
55
{
56
4
    mSkillName->setFont(boldFont);
57
4
    mSkillName->setPosition(0, 0);
58
12
    mSkillName->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
59
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
60
61

4
    const int fontHeight = getFont()->getHeight();
62
63
8
    mSkillDesc->setEditable(false);
64
4
    mSkillDesc->setPosition(0, fontHeight);
65
12
    mSkillDesc->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
66
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
67
68
8
    mSkillEffect->setEditable(false);
69
4
    mSkillEffect->setPosition(0, 2 * fontHeight);
70
12
    mSkillEffect->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
71
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
72
73
8
    mSkillLevel->setEditable(false);
74
4
    mSkillLevel->setPosition(0, 3 * fontHeight);
75
12
    mSkillLevel->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
76
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
77
78
8
    mSkillCastType->setEditable(false);
79
4
    mSkillCastType->setPosition(0, 4 * fontHeight);
80
12
    mSkillCastType->setForegroundColorAll(getThemeColor(ThemeColorId::POPUP),
81
        getThemeColor(ThemeColorId::POPUP_OUTLINE));
82
4
}
83
84
4
void SkillPopup::postInit()
85
{
86
8
    Popup::postInit();
87
4
    add(mSkillName);
88
4
    add(mSkillDesc);
89
4
    add(mSkillEffect);
90
4
    add(mSkillLevel);
91
4
    add(mSkillCastType);
92
93
4
    addMouseListener(this);
94
4
}
95
96
4
SkillPopup::~SkillPopup()
97
{
98
4
}
99
100
void SkillPopup::show(const SkillInfo *const skill,
101
                      const int level,
102
                      const CastTypeT castType,
103
                      const int offsetX,
104
                      const int offsetY)
105
{
106
    if ((skill == nullptr) ||
107
        (skill->data == nullptr) ||
108
        (skill->id == mLastId &&
109
        level == mLastLevel &&
110
        castType == mCastType &&
111
        offsetX == mOffsetX &&
112
        offsetY == mOffsetY))
113
    {
114
        return;
115
    }
116
117
    mLastId = skill->id;
118
    mLastLevel = level;
119
    mCastType = castType;
120
    mOffsetX = offsetX;
121
    mOffsetY = offsetY;
122
123
    mSkillName->setCaption(skill->data->dispName);
124
    mSkillName->adjustSize();
125
    mSkillName->setPosition(0, 0);
126
127
    std::string description = skill->data->description;
128
    std::string effect = skill->skillEffect;
129
    if (description.empty())
130
    {
131
        description = effect;
132
        effect.clear();
133
    }
134
    mSkillDesc->setTextWrapped(description, 196);
135
    mSkillEffect->setTextWrapped(effect, 196);
136
    if (level != 0)
137
    {
138
        mSkillLevel->setTextWrapped(strprintf(
139
            // TRANSLATORS: skill level
140
            _("Level: %d / %d"), level, skill->level),
141
            196);
142
    }
143
    else
144
    {
145
        if (skill->level != 0)
146
        {
147
            mSkillLevel->setTextWrapped(strprintf(
148
                // TRANSLATORS: skill level
149
                _("Level: %d"), skill->level),
150
                196);
151
        }
152
        else
153
        {
154
            mSkillLevel->setTextWrapped(
155
                // TRANSLATORS: skill level for tmw fake skills
156
                _("Level: Unknown"),
157
                196);
158
        }
159
    }
160
    std::string castStr;
161
    switch (castType)
162
    {
163
        case CastType::Default:
164
        default:
165
            // TRANSLATORS: skill cast type
166
            castStr = _("Default");
167
            break;
168
        case CastType::Target:
169
            // TRANSLATORS: skill cast type
170
            castStr = _("Target");
171
            break;
172
        case CastType::Position:
173
            // TRANSLATORS: skill cast type
174
            castStr = _("Mouse position");
175
            break;
176
        case CastType::Self:
177
            // TRANSLATORS: skill cast type
178
            castStr = _("Self position");
179
            break;
180
    }
181
    if (offsetX != 0 ||
182
        offsetY != 0)
183
    {
184
        castStr.append(strprintf(" (%+d,%+d)",
185
            offsetX,
186
            offsetY));
187
    }
188
    mSkillCastType->setTextWrapped(strprintf(
189
        // TRANSLATORS: skill cast type
190
        _("Cast type: %s"),
191
        castStr.c_str()), 196);
192
193
    int minWidth = mSkillName->getWidth();
194
195
    if (mSkillName->getWidth() > minWidth)
196
        minWidth = mSkillName->getWidth();
197
    if (mSkillDesc->getMinWidth() > minWidth)
198
        minWidth = mSkillDesc->getMinWidth();
199
    if (mSkillEffect->getMinWidth() > minWidth)
200
        minWidth = mSkillEffect->getMinWidth();
201
    if (mSkillLevel->getMinWidth() > minWidth)
202
        minWidth = mSkillLevel->getMinWidth();
203
    if (mSkillCastType->getMinWidth() > minWidth)
204
        minWidth = mSkillCastType->getMinWidth();
205
206
    const int numRowsDesc = mSkillDesc->getNumberOfRows();
207
    const int numRowsEffect = mSkillEffect->getNumberOfRows();
208
    const int numRowsLevel = mSkillLevel->getNumberOfRows();
209
    const int numRowsCast = mSkillCastType->getNumberOfRows();
210
    const int height = getFont()->getHeight();
211
212
    if (skill->skillEffect.empty())
213
    {
214
        setContentSize(minWidth,
215
            (numRowsDesc + numRowsLevel + numRowsCast + 1) * height);
216
        mSkillLevel->setPosition(0, (numRowsDesc + 1) * height);
217
        mSkillCastType->setPosition(0, (numRowsDesc + 2) * height);
218
    }
219
    else
220
    {
221
        setContentSize(minWidth,
222
            (numRowsDesc + numRowsLevel + numRowsEffect + numRowsCast + 1) *
223
            height);
224
        mSkillEffect->setPosition(0, (numRowsDesc + 1) * height);
225
        mSkillLevel->setPosition(0,
226
            (numRowsDesc + numRowsEffect + 1) * height);
227
        mSkillCastType->setPosition(0,
228
            (numRowsDesc + numRowsEffect + 2) * height);
229
    }
230
231
    mSkillDesc->setPosition(0, 1 * height);
232
}
233
234
void SkillPopup::mouseMoved(MouseEvent &event)
235
{
236
    Popup::mouseMoved(event);
237
238
    // When the mouse moved on top of the popup, hide it
239
    setVisible(Visible_false);
240
    mLastId = 0;
241
}
242
243
void SkillPopup::reset()
244
{
245
    mLastId = 0;
246
    mLastLevel = 0;
247
    mCastType = CastType::Default;
248
    mOffsetX = 0;
249
    mOffsetY = 0;
250
}