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