GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/playerbox.cpp Lines: 41 71 57.7 %
Date: 2018-06-18 21:15:20 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-2018  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, 0, 8);
83
22
    Theme::unloadRect(mSelectedBackground, 0, 8);
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,
99
            "playerbox_background.xml",
100
            0,
101
            8);
102
22
        if (mSkin != nullptr)
103
        {
104

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

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

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

88
            mFrameSize = mSkin->getOption("frameSize", 2);
108
        }
109
22
        if (selectedName.empty())
110
            selectedName = "playerboxselected.xml";
111

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