GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/playerbox.cpp Lines: 41 71 57.7 %
Date: 2021-03-17 Branches: 19 50 38.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-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/widgets/playerbox.h"
25
26
#include "settings.h"
27
28
#include "being/being.h"
29
30
#include "gui/gui.h"
31
#include "gui/skin.h"
32
33
#include "render/graphics.h"
34
35
#include "resources/image/image.h"
36
37
#include "debug.h"
38
39
PlayerBox::PlayerBox(Widget2 *const widget,
40
                     Being *const being,
41
                     const std::string &skin,
42
                     const std::string &selectedSkin) :
43
    Widget(widget),
44
    MouseListener(),
45
    mBeing(being),
46
    mAlpha(1.0),
47
    mBackground(),
48
    mSelectedBackground(),
49
    mSkin(nullptr),
50
    mSelectedSkin(nullptr),
51
    mOffsetX(-mapTileSize / 2),
52
    mOffsetY(-mapTileSize),
53
    mDrawBackground(false),
54
    mSelected(false)
55
{
56
    init(skin, selectedSkin);
57
}
58
59
11
PlayerBox::PlayerBox(Widget2 *const widget,
60
                     const std::string &skin,
61
11
                     const std::string &selectedSkin) :
62
    Widget(widget),
63
    MouseListener(),
64
    mBeing(nullptr),
65
    mAlpha(1.0),
66
    mBackground(),
67
    mSelectedBackground(),
68
    mSkin(nullptr),
69
    mSelectedSkin(nullptr),
70
    mOffsetX(-mapTileSize / 2),
71
    mOffsetY(-mapTileSize),
72
    mDrawBackground(false),
73
44
    mSelected(false)
74
{
75
44
    init(skin, selectedSkin);
76
11
}
77
78
44
PlayerBox::~PlayerBox()
79
{
80
11
    if (gui != nullptr)
81
11
        gui->removeDragged(this);
82
83
11
    Theme::unloadRect(mBackground, 0, 8);
84
11
    Theme::unloadRect(mSelectedBackground, 0, 8);
85
11
    mBeing = nullptr;
86
22
}
87
88
11
void PlayerBox::init(std::string name, std::string selectedName)
89
{
90
11
    mAllowLogic = false;
91
22
    setFrameSize(2);
92
11
    addMouseListener(this);
93
94
11
    if (theme != nullptr)
95
    {
96
11
        if (name.empty())
97
            name = "playerbox.xml";
98
44
        mSkin = theme->loadSkinRect(mBackground,
99
            name,
100
            "playerbox_background.xml",
101
            0,
102
            8);
103
11
        if (mSkin != nullptr)
104
        {
105
44
            mDrawBackground = (mSkin->getOption("drawbackground") != 0);
106
44
            mOffsetX = mSkin->getOption("offsetX", -mapTileSize / 2);
107
44
            mOffsetY = mSkin->getOption("offsetY", -mapTileSize);
108
44
            mFrameSize = mSkin->getOption("frameSize", 2);
109
        }
110
11
        if (selectedName.empty())
111
            selectedName = "playerboxselected.xml";
112
44
        mSelectedSkin = theme->loadSkinRect(mSelectedBackground,
113
            selectedName,
114
            "playerbox_background.xml",
115
            0,
116
            8);
117
    }
118
    else
119
    {
120
        for (int f = 0; f < 9; f ++)
121
            mBackground.grid[f] = nullptr;
122
        for (int f = 0; f < 9; f ++)
123
            mSelectedBackground.grid[f] = nullptr;
124
    }
125
11
}
126
127
9
void PlayerBox::draw(Graphics *const graphics)
128
{
129
    BLOCK_START("PlayerBox::draw")
130
9
    if (mBeing != nullptr)
131
    {
132
        const int bs = mFrameSize;
133
        const int x = mDimension.width / 2 + bs + mOffsetX;
134
        const int y = mDimension.height - bs + mOffsetY;
135
        mBeing->drawBasic(graphics, x, y);
136
    }
137
138
9
    if (settings.guiAlpha != mAlpha)
139
    {
140
        const float alpha = settings.guiAlpha;
141
        for (int a = 0; a < 9; a++)
142
        {
143
            if (mBackground.grid[a] != nullptr)
144
                mBackground.grid[a]->setAlpha(alpha);
145
        }
146
    }
147
    BLOCK_END("PlayerBox::draw")
148
9
}
149
150
void PlayerBox::safeDraw(Graphics *const graphics)
151
{
152
    PlayerBox::draw(graphics);
153
}
154
155
9
void PlayerBox::drawFrame(Graphics *const graphics)
156
{
157
    BLOCK_START("PlayerBox::drawFrame")
158
9
    if (mDrawBackground)
159
    {
160
9
        const int bs = mFrameSize * 2;
161
9
        const int w = mDimension.width + bs;
162
9
        const int h = mDimension.height + bs;
163
164
9
        if (!mSelected)
165
8
            graphics->drawImageRect(0, 0, w, h, mBackground);
166
        else
167
1
            graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
168
    }
169
    BLOCK_END("PlayerBox::drawFrame")
170
9
}
171
172
void PlayerBox::safeDrawFrame(Graphics *const graphics)
173
{
174
    BLOCK_START("PlayerBox::drawFrame")
175
    if (mDrawBackground)
176
    {
177
        const int bs = mFrameSize * 2;
178
        const int w = mDimension.width + bs;
179
        const int h = mDimension.height + bs;
180
181
        if (!mSelected)
182
            graphics->drawImageRect(0, 0, w, h, mBackground);
183
        else
184
            graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
185
    }
186
    BLOCK_END("PlayerBox::drawFrame")
187
}
188
189
void PlayerBox::mouseReleased(MouseEvent& event)
190
{
191
    if (event.getButton() == MouseButton::LEFT)
192
    {
193
        if (!mActionEventId.empty())
194
            distributeActionEvent();
195
        event.consume();
196
    }
197
2
}