ManaPlus
playerbox.cpp
Go to the documentation of this file.
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 
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 
59  const std::string &skin,
60  const std::string &selectedSkin) :
61  Widget(widget),
62  MouseListener(),
63  mBeing(nullptr),
64  mAlpha(1.0),
65  mBackground(),
67  mSkin(nullptr),
69  mOffsetX(-mapTileSize / 2),
71  mDrawBackground(false),
72  mSelected(false)
73 {
74  init(skin, selectedSkin);
75 }
76 
78 {
79  if (gui != nullptr)
80  gui->removeDragged(this);
81 
84  mBeing = nullptr;
85 }
86 
87 void PlayerBox::init(std::string name, std::string selectedName)
88 {
89  mAllowLogic = false;
90  setFrameSize(2);
91  addMouseListener(this);
92 
93  if (theme != nullptr)
94  {
95  if (name.empty())
96  name = "playerbox.xml";
98  name,
99  "playerbox_background.xml",
100  0,
101  8);
102  if (mSkin != nullptr)
103  {
104  mDrawBackground = (mSkin->getOption("drawbackground") != 0);
105  mOffsetX = mSkin->getOption("offsetX", -mapTileSize / 2);
106  mOffsetY = mSkin->getOption("offsetY", -mapTileSize);
107  mFrameSize = mSkin->getOption("frameSize", 2);
108  }
109  if (selectedName.empty())
110  selectedName = "playerboxselected.xml";
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 }
125 
126 void PlayerBox::draw(Graphics *const graphics)
127 {
128  BLOCK_START("PlayerBox::draw")
129  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  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 }
148 
149 void PlayerBox::safeDraw(Graphics *const graphics)
150 {
151  PlayerBox::draw(graphics);
152 }
153 
154 void PlayerBox::drawFrame(Graphics *const graphics)
155 {
156  BLOCK_START("PlayerBox::drawFrame")
157  if (mDrawBackground)
158  {
159  const int bs = mFrameSize * 2;
160  const int w = mDimension.width + bs;
161  const int h = mDimension.height + bs;
162 
163  if (!mSelected)
164  graphics->drawImageRect(0, 0, w, h, mBackground);
165  else
166  graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
167  }
168  BLOCK_END("PlayerBox::drawFrame")
169 }
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 
189 {
190  if (event.getButton() == MouseButton::LEFT)
191  {
192  if (!mActionEventId.empty())
194  event.consume();
195  }
196 }
int width
Definition: rect.h:218
int mOffsetY
Definition: playerbox.h:107
Gui * gui
Definition: gui.cpp:110
void safeDrawFrame(Graphics *const graphics)
Definition: playerbox.cpp:171
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
MouseButtonT getButton() const
Definition: mouseevent.h:115
bool mDrawBackground
Definition: playerbox.h:108
#define BLOCK_START(name)
Definition: perfomance.h:78
bool mSelected
Definition: playerbox.h:109
void drawBasic(Graphics *const graphics, const int x, const int y) const
Definition: being.cpp:3930
void draw(Graphics *const graphics)
Definition: playerbox.cpp:126
ImageRect mBackground
Definition: playerbox.h:102
float mAlpha
Definition: playerbox.h:101
#define BLOCK_END(name)
Definition: perfomance.h:79
int mOffsetX
Definition: playerbox.h:106
int getOption(const std::string &name) const
Definition: skin.h:105
void distributeActionEvent()
Definition: widget.cpp:492
Settings settings
Definition: settings.cpp:31
virtual void setAlpha(const float alpha)
Definition: image.cpp:286
Image * grid[9]
Definition: imagerect.h:41
void drawFrame(Graphics *const graphics)
Definition: playerbox.cpp:154
Skin * loadSkinRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:899
unsigned int mFrameSize
Definition: widget.h:1137
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:167
void mouseReleased(MouseEvent &event)
Definition: playerbox.cpp:188
#define nullptr
Definition: localconsts.h:44
bool mAllowLogic
Definition: widget.h:1159
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:291
int height
Definition: rect.h:223
Being * mBeing
Definition: playerbox.h:100
Skin * mSkin
Definition: playerbox.h:104
Theme * theme
Definition: theme.cpp:61
Skin * mSelectedSkin
Definition: playerbox.h:105
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1161
Definition: widget.h:97
PlayerBox(Widget2 *const widget, Being *const being, const std::string &skin, const std::string &selectedSkin)
Definition: playerbox.cpp:38
static const int mapTileSize
Definition: map.h:26
std::string mActionEventId
Definition: widget.h:1105
void init(std::string name, std::string selectedName)
Definition: playerbox.cpp:87
ImageRect mSelectedBackground
Definition: playerbox.h:103
static void unloadRect(const ImageRect &rect, const int start, const int end)
Definition: theme.cpp:914
float guiAlpha
Definition: settings.h:129
void safeDraw(Graphics *const graphics)
Definition: playerbox.cpp:149
Rect mDimension
Definition: widget.h:1100
Definition: being.h:93