GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/popups/statuspopup.cpp Lines: 33 95 34.7 %
Date: 2021-03-17 Branches: 8 56 14.3 %

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) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2009-2021  Andrei Karas
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/statuspopup.h"
25
26
#include "gamemodifiers.h"
27
28
#include "gui/widgets/label.h"
29
30
#include "input/inputmanager.h"
31
32
#include "utils/stringutils.h"
33
34
#include "gui/fonts/font.h"
35
36
#include "debug.h"
37
38
#define addLabel(num) \
39
    { \
40
        Label *const label = mLabels[num]; \
41
        label->setPosition(0, y); \
42
        label->setForegroundColorAll( \
43
            getThemeColor(ThemeColorId::POPUP, 255U), \
44
            getThemeColor(ThemeColorId::POPUP_OUTLINE, 255U)); \
45
        add(label); \
46
        y += fontHeight; \
47
    }
48
49
2
StatusPopup::StatusPopup() :
50
    Popup("StatusPopup", "statuspopup.xml"),
51

14
    mLabels()
52
{
53
66
    for (int f = 0; f < STATUSPOPUP_NUM_LABELS; f ++)
54

32
        mLabels[f] = new Label(this);
55
2
}
56
57
2
void StatusPopup::postInit()
58
{
59
4
    Popup::postInit();
60
2
    const int fontHeight = getFont()->getHeight();
61
2
    int y = 0;
62
6
    addLabel(0)
63
6
    addLabel(1)
64
6
    addLabel(2)
65
6
    addLabel(3)
66
2
    y += 4;
67
6
    addLabel(4)
68
6
    addLabel(5)
69
6
    addLabel(9)
70
6
    addLabel(10)
71
2
    y += 4;
72
6
    addLabel(6)
73
6
    addLabel(7)
74
2
    y += 4;
75
6
    addLabel(8)
76
2
    y += 4;
77
6
    addLabel(12)
78
6
    addLabel(13)
79
6
    addLabel(14)
80
6
    addLabel(15)
81
2
    y += 4;
82
6
    addLabel(11)
83
2
}
84
85
2
StatusPopup::~StatusPopup()
86
{
87
2
}
88
89
void StatusPopup::update()
90
{
91
    updateLabels();
92
93
    int maxWidth = mLabels[0]->getWidth();
94
95
    for (int f = 0; f < STATUSPOPUP_NUM_LABELS; f ++)
96
    {
97
        const int width = mLabels[f]->getWidth();
98
        if (width > maxWidth)
99
            maxWidth = width;
100
    }
101
102
    const int pad2 = 2 * mPadding;
103
    maxWidth += pad2;
104
    setWidth(maxWidth);
105
    setHeight(mLabels[11]->getY()
106
        + mLabels[11]->getHeight() + pad2);
107
}
108
109
void StatusPopup::view(const int x, const int y)
110
{
111
    const int distance = 20;
112
113
    int posX = std::max(0, x - getWidth() / 2);
114
    int posY = y + distance;
115
116
    if (posX + getWidth() > mainGraphics->mWidth)
117
        posX = mainGraphics->mWidth - getWidth();
118
    if (posY + getHeight() > mainGraphics->mHeight)
119
        posY = y - getHeight() - distance;
120
121
    update();
122
123
    setPosition(posX, posY);
124
    setVisible(Visible_true);
125
    requestMoveToTop();
126
}
127
128
void StatusPopup::setLabelText(const int num,
129
                               const std::string &text,
130
                               const InputActionT key) const
131
{
132
    Label *const label = mLabels[num];
133
    label->setCaption(strprintf("%s  %s", text.c_str(),
134
        inputManager.getKeyValueString(key).c_str()));
135
    label->adjustSize();
136
}
137
138
void StatusPopup::updateLabels() const
139
{
140
    setLabelText(0, GameModifiers::getMoveTypeString(),
141
        InputAction::INVERT_DIRECTION);
142
    setLabelText(1, GameModifiers::getCrazyMoveTypeString(),
143
        InputAction::CHANGE_CRAZY_MOVES_TYPE);
144
    setLabelText(2, GameModifiers::getMoveToTargetTypeString(),
145
        InputAction::CHANGE_MOVE_TO_TARGET);
146
    setLabelText(3, GameModifiers::getFollowModeString(),
147
        InputAction::CHANGE_FOLLOW_MODE);
148
    setLabelText(4, GameModifiers::getAttackWeaponTypeString(),
149
        InputAction::CHANGE_ATTACK_WEAPON_TYPE);
150
    setLabelText(5, GameModifiers::getAttackTypeString(),
151
        InputAction::CHANGE_ATTACK_TYPE);
152
    setLabelText(6, GameModifiers::getQuickDropCounterString(),
153
        InputAction::SWITCH_QUICK_DROP);
154
    setLabelText(7, GameModifiers::getPickUpTypeString(),
155
        InputAction::CHANGE_PICKUP_TYPE);
156
    setLabelText(8, GameModifiers::getMapDrawTypeString(),
157
        InputAction::PATHFIND);
158
    setLabelText(9, GameModifiers::getMagicAttackTypeString(),
159
        InputAction::SWITCH_MAGIC_ATTACK);
160
    setLabelText(10, GameModifiers::getPvpAttackTypeString(),
161
        InputAction::SWITCH_PVP_ATTACK);
162
    setLabelText(11, GameModifiers::getGameModifiersString(),
163
        InputAction::DISABLE_GAME_MODIFIERS);
164
    setLabelText(12, GameModifiers::getImitationModeString(),
165
        InputAction::CHANGE_IMITATION_MODE);
166
    setLabelText(13, GameModifiers::getAwayModeString(),
167
        InputAction::AWAY);
168
    setLabelText(14, GameModifiers::getCameraModeString(),
169
        InputAction::CAMERA);
170
    setLabelText(15, GameModifiers::getTargetingTypeString(),
171
        InputAction::CHANGE_TARGETING_TYPE);
172
}