ManaPlus
mapitem.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2017 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "resources/map/mapitem.h"
22 
24 
25 #include "gui/gui.h"
26 #include "gui/userpalette.h"
27 
28 #include "gui/fonts/font.h"
29 
30 #include "resources/image/image.h"
31 
33 
34 #include "render/graphics.h"
35 
36 #include "debug.h"
37 
39  mImage(nullptr),
40  mComment(),
41  mName(),
42  mType(MapItemType::EMPTY),
43  mX(-1),
44  mY(-1)
45 {
47 }
48 
49 MapItem::MapItem(const int type) :
50  mImage(nullptr),
51  mComment(),
52  mName(),
53  mType(type),
54  mX(-1),
55  mY(-1)
56 {
57  setType(type);
58 }
59 
60 MapItem::MapItem(const int type,
61  const std::string &comment) :
62  mImage(nullptr),
63  mComment(comment),
64  mName(),
65  mType(type),
66  mX(-1),
67  mY(-1)
68 {
69  setType(type);
70 }
71 
72 MapItem::MapItem(const int type,
73  const std::string &comment,
74  const int x,
75  const int y) :
76  mImage(nullptr),
77  mComment(comment),
78  mName(),
79  mType(type),
80  mX(x),
81  mY(y)
82 {
83  setType(type);
84 }
85 
87 {
88  if (mImage != nullptr)
89  {
90  mImage->decRef();
91  mImage = nullptr;
92  }
93 }
94 
95 void MapItem::setType(const int type)
96 {
97  std::string name;
98  mType = type;
99  if (mImage != nullptr)
100  mImage->decRef();
101 
102  switch (type)
103  {
105  name = "graphics/sprites/arrow_up.png";
106  break;
108  name = "graphics/sprites/arrow_down.png";
109  break;
111  name = "graphics/sprites/arrow_left.png";
112  break;
114  name = "graphics/sprites/arrow_right.png";
115  break;
116  default:
117  break;
118  }
119 
120  if (!name.empty())
121  mImage = Loader::getImage(name);
122  else
123  mImage = nullptr;
124 }
125 
126 void MapItem::setPos(const int x, const int y)
127 {
128  mX = x;
129  mY = y;
130 }
131 
132 void MapItem::draw(Graphics *const graphics, const int x, const int y,
133  const int dx, const int dy) const
134 {
135  BLOCK_START("MapItem::draw")
136  if (mImage != nullptr)
137  graphics->drawImage(mImage, x, y);
138 
139  switch (mType)
140  {
141  case MapItemType::ROAD:
142  case MapItemType::CROSS:
145  graphics->fillRectangle(Rect(x + dx / 3, y + dy / 3,
146  dx / 3, dy / 3));
147  break;
148  case MapItemType::HOME:
149  {
152  graphics->fillRectangle(Rect(x, y, dx, dy));
155  graphics->drawRectangle(Rect(x, y, dx, dy));
156  break;
157  }
158  default:
159  break;
160  }
161  if (!mName.empty()
164  {
165  Font *const font = gui->getFont();
166  const Color &color = userPalette->getColor(UserColorId::BEING);
167  font->drawString(graphics,
168  color,
169  color,
170  mName,
171  x, y);
172  }
173  BLOCK_END("MapItem::draw")
174 }
const Color & getColor(UserColorIdT type, const unsigned int alpha=255U)
Definition: userpalette.h:159
Gui * gui
Definition: gui.cpp:110
Definition: font.h:88
const Color & getColorWithAlpha(const UserColorIdT type)
Definition: userpalette.h:199
int mType
Definition: mapitem.h:87
MapItem()
Definition: mapitem.cpp:38
int mY
Definition: mapitem.h:89
Definition: rect.h:72
#define BLOCK_START(name)
Definition: perfomance.h:78
#define BLOCK_END(name)
Definition: perfomance.h:79
Font * getFont() const
Definition: gui.h:159
virtual void drawRectangle(const Rect &rectangle)=0
std::string mComment
Definition: mapitem.h:85
UserPalette * userPalette
Definition: userpalette.cpp:33
virtual void setColor(const Color &color)
Definition: graphics.h:319
std::string mName
Definition: mapitem.h:86
Image * getImage(const std::string &idPath)
Definition: imageloader.cpp:85
void drawString(Graphics *const graphics, Color col, const Color &col2, const std::string &text, const int x, const int y)
Definition: font.cpp:253
std::string mName
Definition: map.h:459
#define nullptr
Definition: localconsts.h:44
void setType(const int type)
Definition: mapitem.cpp:95
int mX
Definition: mapitem.h:88
virtual void fillRectangle(const Rect &rectangle)=0
virtual void drawImage(const Image *const image, int dstX, int dstY)=0
Image * mImage
Definition: mapitem.h:84
~MapItem()
Definition: mapitem.cpp:86
Definition: color.h:74
void draw(Graphics *const graphics, const int x, const int y, const int dx, const int dy) const
Definition: mapitem.cpp:132
void decRef()
Definition: image.cpp:521
void setPos(const int x, const int y)
Definition: mapitem.cpp:126