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-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 
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 
60  const std::string &skin,
61  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  mSelected(false)
74 {
75  init(skin, selectedSkin);
76 }
77 
79 {
80  if (gui != nullptr)
81  gui->removeDragged(this);
82 
85  mBeing = nullptr;
86 }
87 
88 void PlayerBox::init(std::string name, std::string selectedName)
89 {
90  mAllowLogic = false;
91  setFrameSize(2);
92  addMouseListener(this);
93 
94  if (theme != nullptr)
95  {
96  if (name.empty())
97  name = "playerbox.xml";
99  name,
100  "playerbox_background.xml",
101  0,
102  8);
103  if (mSkin != nullptr)
104  {
105  mDrawBackground = (mSkin->getOption("drawbackground") != 0);
106  mOffsetX = mSkin->getOption("offsetX", -mapTileSize / 2);
107  mOffsetY = mSkin->getOption("offsetY", -mapTileSize);
108  mFrameSize = mSkin->getOption("frameSize", 2);
109  }
110  if (selectedName.empty())
111  selectedName = "playerboxselected.xml";
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 }
126 
127 void PlayerBox::draw(Graphics *const graphics)
128 {
129  BLOCK_START("PlayerBox::draw")
130  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  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 }
149 
150 void PlayerBox::safeDraw(Graphics *const graphics)
151 {
152  PlayerBox::draw(graphics);
153 }
154 
155 void PlayerBox::drawFrame(Graphics *const graphics)
156 {
157  BLOCK_START("PlayerBox::drawFrame")
158  if (mDrawBackground)
159  {
160  const int bs = mFrameSize * 2;
161  const int w = mDimension.width + bs;
162  const int h = mDimension.height + bs;
163 
164  if (!mSelected)
165  graphics->drawImageRect(0, 0, w, h, mBackground);
166  else
167  graphics->drawImageRect(0, 0, w, h, mSelectedBackground);
168  }
169  BLOCK_END("PlayerBox::drawFrame")
170 }
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 
190 {
191  if (event.getButton() == MouseButton::LEFT)
192  {
193  if (!mActionEventId.empty())
195  event.consume();
196  }
197 }
Definition: being.h:96
void drawBasic(Graphics *const graphics, const int x, const int y) const
Definition: being.cpp:3930
virtual void drawImageRect(const int x, const int y, const int w, const int h, const ImageRect &imgRect)=0
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1162
Image * grid[9]
Definition: imagerect.h:42
MouseButtonT getButton() const
Definition: mouseevent.h:116
Being * mBeing
Definition: playerbox.h:101
void draw(Graphics *const graphics)
Definition: playerbox.cpp:127
ImageRect mSelectedBackground
Definition: playerbox.h:104
ImageRect mBackground
Definition: playerbox.h:103
void init(std::string name, std::string selectedName)
Definition: playerbox.cpp:88
int mOffsetX
Definition: playerbox.h:107
void mouseReleased(MouseEvent &event)
Definition: playerbox.cpp:189
void safeDrawFrame(Graphics *const graphics)
Definition: playerbox.cpp:172
bool mSelected
Definition: playerbox.h:110
void drawFrame(Graphics *const graphics)
Definition: playerbox.cpp:155
Skin * mSelectedSkin
Definition: playerbox.h:106
Skin * mSkin
Definition: playerbox.h:105
bool mDrawBackground
Definition: playerbox.h:109
float mAlpha
Definition: playerbox.h:102
PlayerBox(Widget2 *const widget, Being *const being, const std::string &skin, const std::string &selectedSkin)
Definition: playerbox.cpp:39
int mOffsetY
Definition: playerbox.h:108
void safeDraw(Graphics *const graphics)
Definition: playerbox.cpp:150
int width
Definition: rect.h:219
int height
Definition: rect.h:224
float guiAlpha
Definition: settings.h:131
int getOption(const std::string &name) const
Definition: skin.h:106
static void unloadRect(const ImageRect &rect, const int start, const int end)
Definition: theme.cpp:915
Skin * loadSkinRect(ImageRect &image, const std::string &name, const std::string &name2, const int start, const int end)
Definition: theme.cpp:900
Definition: widget.h:99
void setFrameSize(const unsigned int frameSize)
Definition: widget.h:168
void distributeActionEvent()
Definition: widget.cpp:493
Rect mDimension
Definition: widget.h:1101
unsigned int mFrameSize
Definition: widget.h:1138
bool mAllowLogic
Definition: widget.h:1160
void addMouseListener(MouseListener *const mouseListener)
Definition: widget.cpp:292
std::string mActionEventId
Definition: widget.h:1106
static const int mapTileSize
Definition: map.h:27
Gui * gui
Definition: gui.cpp:111
#define nullptr
Definition: localconsts.h:45
#define BLOCK_END(name)
Definition: perfomance.h:80
#define BLOCK_START(name)
Definition: perfomance.h:79
Settings settings
Definition: settings.cpp:32
Theme * theme
Definition: theme.cpp:62