GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/playerbox.cpp Lines: 41 71 57.7 %
Date: 2017-11-29 Branches: 25 62 40.3 %

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-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/widgets/playerbox.h"
24
25
#include "settings.h"
26
27
#include "being/being.h"
28
29
#include "gui/gui.h"
30
#include "gui/skin.h"
31
32
#include "render/graphics.h"
33
34
#include "resources/image/image.h"
35
36
#include "debug.h"
37
38
PlayerBox::PlayerBox(Widget2 *const widget,
39
                     Being *const being,
40
                     const std::string &skin,
41
                     const std::string &selectedSkin) :
42
    Widget(widget),
43
    MouseListener(),
44
    mBeing(being),
45
    mAlpha(1.0),
46
    mBackground(),
47
    mSelectedBackground(),
48
    mSkin(nullptr),
49
    mSelectedSkin(nullptr),
50
    mOffsetX(-mapTileSize / 2),
51
    mOffsetY(-mapTileSize),
52
    mDrawBackground(false),
53
    mSelected(false)
54
{
55
    init(skin, selectedSkin);
56
}
57
58
22
PlayerBox::PlayerBox(Widget2 *const widget,
59
                     const std::string &skin,
60
22
                     const std::string &selectedSkin) :
61
    Widget(widget),
62
    MouseListener(),
63
    mBeing(nullptr),
64
    mAlpha(1.0),
65
    mBackground(),
66
    mSelectedBackground(),
67
    mSkin(nullptr),
68
    mSelectedSkin(nullptr),
69
    mOffsetX(-mapTileSize / 2),
70
    mOffsetY(-mapTileSize),
71
    mDrawBackground(false),
72
88
    mSelected(false)
73
{
74
88
    init(skin, selectedSkin);
75
22
}
76
77
88
PlayerBox::~PlayerBox()
78
{
79
22
    if (gui != nullptr)
80
22
        gui->removeDragged(this);
81
82
22
    Theme::unloadRect(mBackground);
83
22
    Theme::unloadRect(mSelectedBackground);
84
22
    mBeing = nullptr;
85
44
}
86
87
22
void PlayerBox::init(std::string name, std::string selectedName)
88
{
89
22
    mAllowLogic = false;
90
44
    setFrameSize(2);
91
22
    addMouseListener(this);
92
93
22
    if (theme != nullptr)
94
    {
95
22
        if (name.empty())
96
            name = "playerbox.xml";
97

88
        mSkin = theme->loadSkinRect(mBackground,
98
            name, "playerbox_background.xml");
99
22
        if (mSkin != nullptr)
100
        {
101

88
            mDrawBackground = (mSkin->getOption("drawbackground") != 0);
102

88
            mOffsetX = mSkin->getOption("offsetX", -mapTileSize / 2);
103

88
            mOffsetY = mSkin->getOption("offsetY", -mapTileSize);
104

88
            mFrameSize = mSkin->getOption("frameSize", 2);
105
        }
106
22
        if (selectedName.empty())
107
            selectedName = "playerboxselected.xml";
108

88
        mSelectedSkin = theme->loadSkinRect(mSelectedBackground,
109
            selectedName, "playerbox_background.xml");
110
    }
111
    else
112
    {
113
        for (int f = 0; f < 9; f ++)
114
            mBackground.grid[f] = nullptr;
115
        for (int f = 0; f < 9; f ++)
116
            mSelectedBackground.grid[f] = nullptr;
117
    }
118
22
}
119
120
18
void PlayerBox::draw(Graphics *const graphics)
121
{
122
    BLOCK_START("PlayerBox::draw")
123
18
    if (mBeing != nullptr)
124
    {
125
        const int bs = mFrameSize;
126
        const int x = mDimension.width / 2 + bs + mOffsetX;
127
        const int y = mDimension.height - bs + mOffsetY;
128
        mBeing->drawBasic(graphics, x, y);
129
    }
130
131
18
    if (settings.guiAlpha != mAlpha)
132
    {
133
        const float alpha = settings.guiAlpha;
134
        for (int a = 0; a < 9; a++)
135
        {
136
            if (mBackground.grid[a] != nullptr)
137
                mBackground.grid[a]->setAlpha(alpha);
138
        }
139
    }
140
    BLOCK_END("PlayerBox::draw")
141
18
}
142
143
void PlayerBox::safeDraw(Graphics *const graphics)
144
{
145
    PlayerBox::draw(graphics);
146
}
147
148
18
void PlayerBox::drawFrame(Graphics *const graphics)
149
{
150
    BLOCK_START("PlayerBox::drawFrame")
151
18
    if (mDrawBackground)
152
    {
153
18
        const int bs = mFrameSize * 2;
154
18
        const int w = mDimension.width + bs;
155
18
        const int h = mDimension.height + bs;
156
157
18
        if (!mSelected)
158
16
            graphics->drawImageRect(0, 0, w, h, mBackground);
159
        else
160
2
            graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
161
    }
162
    BLOCK_END("PlayerBox::drawFrame")
163
18
}
164
165
void PlayerBox::safeDrawFrame(Graphics *const graphics)
166
{
167
    BLOCK_START("PlayerBox::drawFrame")
168
    if (mDrawBackground)
169
    {
170
        const int bs = mFrameSize * 2;
171
        const int w = mDimension.width + bs;
172
        const int h = mDimension.height + bs;
173
174
        if (!mSelected)
175
            graphics->drawImageRect(0, 0, w, h, mBackground);
176
        else
177
            graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
178
    }
179
    BLOCK_END("PlayerBox::drawFrame")
180
}
181
182
void PlayerBox::mouseReleased(MouseEvent& event)
183
{
184
    if (event.getButton() == MouseButton::LEFT)
185
    {
186
        if (!mActionEventId.empty())
187
            distributeActionEvent();
188
        event.consume();
189
    }
190
4
}