GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/skillpopup.cpp Lines: 40 125 32.0 %
Date: 2018-07-14 Branches: 29 136 21.3 %

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-2018  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
2
SkillPopup::SkillPopup() :
44
    Popup("SkillPopup", "skillpopup.xml"),
45

2
    mSkillName(new Label(this)),
46

2
    mSkillDesc(new TextBox(this)),
47

2
    mSkillEffect(new TextBox(this)),
48

2
    mSkillLevel(new TextBox(this)),
49

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

24
    mOffsetY(0)
55
{
56
2
    mSkillName->setFont(boldFont);
57
2
    mSkillName->setPosition(0, 0);
58
6
    mSkillName->setForegroundColorAll(
59
        getThemeColor(ThemeColorId::POPUP, 255U),
60
2
        getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U));
61
62

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