ManaPlus
icon.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-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/icon.h"
24 
25 #include "gui/gui.h"
26 
27 #include "render/graphics.h"
28 
29 #include "resources/image/image.h"
30 
32 
33 #include "debug.h"
34 
35 Icon::Icon(const Widget2 *const widget,
36  const std::string &file,
37  const AutoRelease autoRelease) :
38  Widget(widget),
39  mImage(Loader::getImage(file)),
40  mAutoRelease(autoRelease)
41 {
42  if (mImage != nullptr)
43  {
44  const SDL_Rect &bounds = mImage->mBounds;
45  setSize(bounds.w, bounds.h);
46  }
47  mAllowLogic = false;
48 }
49 
50 Icon::Icon(const Widget2 *const widget,
51  Image *const image,
52  const AutoRelease autoRelease) :
53  Widget(widget),
54  mImage(image),
55  mAutoRelease(autoRelease)
56 {
57  if (mImage != nullptr)
58  {
59  const SDL_Rect &bounds = mImage->mBounds;
60  setSize(bounds.w, bounds.h);
61  }
62  mAllowLogic = false;
63 }
64 
66 {
67  if (gui != nullptr)
68  gui->removeDragged(this);
69  if ((mImage != nullptr) && mAutoRelease == AutoRelease_true)
70  mImage->decRef();
71 }
72 
73 void Icon::setImage(Image *const image)
74 {
75  mImage = image;
76  if (mImage != nullptr)
77  {
78  const SDL_Rect &bounds = mImage->mBounds;
79  setSize(bounds.w, bounds.h);
80  }
81 }
82 
83 void Icon::draw(Graphics *const graphics)
84 {
85  BLOCK_START("Icon::draw")
86  if (mImage != nullptr)
87  {
88  graphics->drawImage(mImage,
89  (mDimension.width - mImage->mBounds.w) / 2,
90  (mDimension.height - mImage->mBounds.h) / 2);
91  }
92  BLOCK_END("Icon::draw")
93 }
94 
95 void Icon::safeDraw(Graphics *const graphics)
96 {
97  BLOCK_START("Icon::draw")
98  if (mImage != nullptr)
99  {
100  graphics->drawImage(mImage,
101  (mDimension.width - mImage->mBounds.w) / 2,
102  (mDimension.height - mImage->mBounds.h) / 2);
103  }
104  BLOCK_END("Icon::draw")
105 }
void setSize(const int width, const int height)
Definition: widget.cpp:366
void safeDraw(Graphics *const g)
Definition: icon.cpp:95
int width
Definition: rect.h:218
Gui * gui
Definition: gui.cpp:110
Image * mImage
Definition: icon.h:79
#define BLOCK_START(name)
Definition: perfomance.h:78
#define BLOCK_END(name)
Definition: perfomance.h:79
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
bool mAllowLogic
Definition: widget.h:1159
int height
Definition: rect.h:223
Icon(const Widget2 *const widget, const std::string &filename, const AutoRelease autoRelease=AutoRelease_false)
Definition: icon.cpp:35
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
void setImage(Image *const image)
Definition: icon.cpp:73
void removeDragged(const Widget *const widget)
Definition: gui.cpp:1144
Definition: widget.h:97
Definition: image.h:61
const bool AutoRelease_true
Definition: autorelease.h:29
AutoRelease mAutoRelease
Definition: icon.h:80
SDL_Rect mBounds
Definition: image.h:210
~Icon()
Definition: icon.cpp:65
void draw(Graphics *const g)
Definition: icon.cpp:83
Rect mDimension
Definition: widget.h:1100
void decRef()
Definition: image.cpp:521