Mana
Loading...
Searching...
No Matches
minimap.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana 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 "gui/minimap.h"
23
24#include "actorspritemanager.h"
25#include "being.h"
26#include "configuration.h"
27#include "graphics.h"
28#include "localplayer.h"
29#include "map.h"
30
31#include "gui/setup.h"
32
33#include "resources/image.h"
36
37#include "utils/filesystem.h"
38#include "utils/gettext.h"
39
40#include <algorithm>
41#include <guichan/font.hpp>
42
44 Window(SkinType::Popup, _("Map"))
45{
46 setWindowName("Minimap");
47 setDefaultSize(5, 25, 100, 100);
48 // set this to false as the minimap window size is changed
49 //depending on the map size
50 setResizable(false);
52
54 setSaveVisible(true);
55
56 setStickyButton(true);
57 setSticky(false);
58
61}
62
63Minimap::~Minimap() = default;
64
66{
67 // Set the title for the Minimap
68 std::string caption;
69
70 if (map)
71 caption = map->getName();
72
73 if (caption.empty())
74 caption = _("Map");
75
76 minimap->setCaption(caption);
77
78 // Adapt the image
79 mMapImage = nullptr;
80
81 if (map)
82 {
83 mMap = map;
84 std::string tempname =
85 "graphics/minimaps/" + map->getFilename() + ".png";
87
88 std::string minimapName = map->getProperty("minimap");
89
90 if (minimapName.empty() && FS::exists(tempname))
91 minimapName = tempname;
92
93 if (!minimapName.empty())
94 mMapImage = resman->getImage(minimapName);
95 }
96
97 if (mMapImage)
98 {
99 const int offsetX = 2 * getPadding();
100 const int offsetY = getTitleBarHeight() + getPadding();
101 const int titleWidth = getFont()->getWidth(getCaption()) + 15;
102 const int mapWidth = mMapImage->getWidth() < 100 ?
103 mMapImage->getWidth() + offsetX : 100;
104 const int mapHeight = mMapImage->getHeight() < 100 ?
105 mMapImage->getHeight() + offsetY : 100;
106
107 setMinWidth(mapWidth > titleWidth ? mapWidth : titleWidth);
108 setMinHeight(mapHeight);
109
110 mWidthProportion = (float) mMapImage->getWidth() / map->getWidth();
111 mHeightProportion = (float) mMapImage->getHeight() / map->getHeight();
112
113 setMaxWidth(mMapImage->getWidth() > titleWidth ?
114 mMapImage->getWidth() + offsetX : titleWidth);
115 setMaxHeight(mMapImage->getHeight() + offsetY);
116
117 setDefaultSize(getX(), getY(), getWidth(), getHeight());
119
121 setVisible(true);
122 }
123 else
124 {
125 if (!isSticky())
126 setVisible(false);
127 }
128}
129
131{
132 setVisible(!isVisible(), isSticky());
133 config.showMinimap = isVisible();
134}
135
136void Minimap::draw(gcn::Graphics *graphics)
137{
139
140 if (!mMap)
141 return;
142
143 auto g = static_cast<Graphics*>(graphics);
144 const gcn::Rectangle a = getChildrenArea();
145
146 g->pushClipRect(a); // does actual clipping
147 g->pushClipArea(a); // only applies an offset
148
149 const int tileWidth = mMap->getTileWidth();
150 const int tileHeight = mMap->getTileHeight();
151
152 int mapOriginX = 0;
153 int mapOriginY = 0;
154
155 if (mMapImage)
156 {
157 const Vector &p = local_player->getPosition();
158 const int minOriginX = a.width - mMapImage->getWidth();
159 const int minOriginY = a.height - mMapImage->getHeight();
160
161 if (minOriginX < 0)
162 {
163 mapOriginX = (a.width / 2) - (int) (p.x * mWidthProportion) / tileWidth;
164 mapOriginX = std::clamp(mapOriginX, minOriginX, 0);
165 }
166 if (minOriginY < 0)
167 {
168 mapOriginY = (a.height / 2) - (int) (p.y * mHeightProportion) / tileHeight;
169 mapOriginY = std::clamp(mapOriginY, minOriginY, 0);
170 }
171
172 g->drawImage(mMapImage, mapOriginX, mapOriginY);
173 }
174
175 for (auto actor : actorSpriteManager->getAll())
176 {
177 if (actor->getType() == ActorSprite::FLOOR_ITEM)
178 continue;
179
180 const Being *being = static_cast<Being*>(actor);
181
182 if (!being->isAlive())
183 continue;
184
185 int dotSize = 2;
186
187 int type = UserPalette::PC;
188
189 if (being == local_player)
190 {
191 type = UserPalette::SELF;
192 dotSize = 3;
193 }
194 else if (being->isGM())
195 type = UserPalette::GM;
196 else if (being->isInParty())
197 type = UserPalette::PARTY;
198 else
199 {
200 switch (being->getType())
201 {
204 break;
205
206 case ActorSprite::NPC:
207 type = UserPalette::NPC;
208 break;
209
210 default:
211 continue;
212 }
213 }
214
216
217 const int offsetHeight = (int) ((dotSize - 1) * mHeightProportion);
218 const int offsetWidth = (int) ((dotSize - 1) * mWidthProportion);
219 const Vector &pos = being->getPosition();
220
221 g->fillRectangle(
222 gcn::Rectangle((int) (pos.x * mWidthProportion) / tileWidth + mapOriginX - offsetWidth,
223 (int) (pos.y * mHeightProportion) / tileHeight + mapOriginY
224 - offsetHeight,
225 dotSize,
226 dotSize));
227 }
228
229 g->popClipArea();
230 g->popClipRect();
231}
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
const ActorSprites & getAll() const
Returns the whole list of beings.
const Vector & getPosition() const
Returns the pixel position of this actor.
Definition actor.h:53
Definition being.h:65
Type getType() const final
Returns the type of the ActorSprite.
Definition being.h:122
bool isGM() const
Whether or not this player is a GM.
Definition being.h:417
bool isAlive() const
Returns whether this being is still alive.
Definition being.h:351
bool isInParty() const
Definition being.h:257
A central point of control for graphics.
Definition graphics.h:78
void setColor(const gcn::Color &color) override
Definition graphics.h:255
int getHeight() const
Returns the height of the image.
Definition image.h:89
int getWidth() const
Returns the width of the image.
Definition image.h:83
A tile map.
Definition map.h:147
int getHeight() const
Returns the height of this map in tiles.
Definition map.h:260
int getTileHeight() const
Returns the tile height used by this map.
Definition map.h:271
std::string getName() const
Definition map.cpp:598
std::string getFilename() const
Gives the map id based on filepath (ex: 009-1)
Definition map.cpp:606
int getWidth() const
Returns the width of this map in tiles.
Definition map.h:255
int getTileWidth() const
Returns the tile width of this map.
Definition map.h:265
void draw(gcn::Graphics *graphics) override
Draws the minimap.
Definition minimap.cpp:136
Map * mMap
Definition minimap.h:62
void toggle()
Toggles the displaying of the minimap.
Definition minimap.cpp:130
Minimap()
Definition minimap.cpp:43
~Minimap() override
ResourceRef< Image > mMapImage
Definition minimap.h:63
void setMap(Map *map)
Sets the map image that should be displayed.
Definition minimap.cpp:65
float mHeightProportion
Definition minimap.h:65
float mWidthProportion
Definition minimap.h:64
const gcn::Color & getColor(int type) const
Gets the color associated with the type.
Definition palette.h:72
A light version of the Window class.
Definition popup.h:48
std::string getProperty(const std::string &name, const std::string &def=std::string()) const
Get a map property.
Definition properties.h:44
A class for loading and managing resources.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< Image > getImage(const std::string &idPath)
Loads the Image resource found at the given identifier path.
void registerWindowForReset(Window *window)
Enables the Reset Windows button.
Definition setup.cpp:108
Vector class.
Definition vector.h:33
float y
Definition vector.h:172
float x
Definition vector.h:171
A window.
Definition window.h:59
void draw(gcn::Graphics *graphics) override
Draws the window contents.
Definition window.cpp:103
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
void setDefaultVisible(bool save)
Sets whether the window is visible by default.
Definition window.h:215
void setMinHeight(int height)
Sets the minimum height of the window.
Definition window.cpp:197
void setStickyButton(bool flag)
Sets whether or not the window has a sticky button.
Definition window.cpp:272
void setWindowName(const std::string &name)
Sets the name of the window.
Definition window.h:272
bool isSticky() const
Returns whether the window is sticky.
Definition window.h:193
virtual void resetToDefaultSize()
Reset the win pos and size to default.
Definition window.cpp:600
void setSticky(bool sticky)
Sets whether the window is sticky.
Definition window.cpp:277
void setResizable(bool resize)
Sets whether or not the window can be resized.
Definition window.cpp:212
void setSaveVisible(bool save)
Sets whether the window will save it's visibility.
Definition window.h:225
void setDefaultSize()
Set the default win pos and size to the current ones.
Definition window.cpp:545
void loadWindowState()
Reads the position (and the size for resizable windows) in the configuration based on the given strin...
Definition window.cpp:467
void setMaxHeight(int height)
Sets the minimum height of the window.
Definition window.cpp:207
void setMinWidth(int width)
Sets the minimum width of the window.
Definition window.cpp:192
void setMaxWidth(int width)
Sets the maximum width of the window.
Definition window.cpp:202
Config config
Global settings (config.xml)
Definition client.cpp:97
Graphics * graphics
Definition client.cpp:104
UserPalette * userPalette
Definition client.cpp:103
#define _(s)
Definition gettext.h:38
LocalPlayer * local_player
Minimap * minimap
Definition game.cpp:99
bool exists(const std::string &path)
Checks whether the given file or directory exists in the search path.
Definition filesystem.h:93
Setup * setupWindow
Definition setup.cpp:120
bool showMinimap
SkinType
Definition theme.h:69