1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/adventureMap/CMinimap.cpp

250 lines
6.1 KiB
C++
Raw Normal View History

/*
2023-02-01 16:42:03 +02:00
* CMinimap.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CMinimap.h"
#include "AdventureMapInterface.h"
#include "../widgets/Images.h"
#include "../CGameInfo.h"
#include "../CPlayerInterface.h"
#include "../gui/CGuiHandler.h"
#include "../gui/MouseButton.h"
#include "../gui/WindowHandler.h"
#include "../render/Colors.h"
#include "../render/Canvas.h"
#include "../render/Graphics.h"
#include "../renderSDL/SDL_Extensions.h"
2023-02-10 15:29:30 +02:00
#include "../windows/InfoWindows.h"
#include "../../CCallback.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/TerrainHandler.h"
#include "../../lib/mapObjects/CGHeroInstance.h"
#include "../../lib/mapping/CMapDefines.h"
2023-02-10 15:29:30 +02:00
ColorRGBA CMinimapInstance::getTileColor(const int3 & pos) const
{
const TerrainTile * tile = LOCPLINT->cb->getTile(pos, false);
// if tile is not visible it will be black on minimap
if(!tile)
return Colors::BLACK;
// if object at tile is owned - it will be colored as its owner
2014-10-01 15:19:03 +03:00
for (const CGObjectInstance *obj : tile->blockingObjects)
{
2013-03-03 20:06:03 +03:00
PlayerColor player = obj->getOwner();
if(player == PlayerColor::NEUTRAL)
return graphics->neutralColor;
2023-02-10 15:29:30 +02:00
if (player.isValidPlayer())
return graphics->playerColors[player.getNum()];
}
if (tile->blocked && (!tile->visitable))
2023-02-10 15:29:30 +02:00
return tile->terType->minimapBlocked;
else
2023-02-10 15:29:30 +02:00
return tile->terType->minimapUnblocked;
}
void CMinimapInstance::refreshTile(const int3 &tile)
{
2023-02-10 15:29:30 +02:00
if (level == tile.z)
minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
}
2023-02-10 15:29:30 +02:00
void CMinimapInstance::redrawMinimap()
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
2023-02-10 15:29:30 +02:00
for (int y = 0; y < mapSizes.y; ++y)
for (int x = 0; x < mapSizes.x; ++x)
minimap->drawPoint(Point(x, y), getTileColor(int3(x, y, level)));
}
CMinimapInstance::CMinimapInstance(CMinimap *Parent, int Level):
2015-01-13 21:57:41 +02:00
parent(Parent),
minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y), CanvasScalingPolicy::IGNORE)),
2015-01-13 21:57:41 +02:00
level(Level)
{
pos.w = parent->pos.w;
pos.h = parent->pos.h;
2023-02-10 15:29:30 +02:00
redrawMinimap();
}
void CMinimapInstance::showAll(Canvas & to)
{
to.drawScaled(*minimap, pos.topLeft(), pos.dimensions());
}
CMinimap::CMinimap(const Rect & position)
: CIntObject(LCLICK | SHOW_POPUP | DRAG | MOVE | GESTURE, position.topLeft()),
2023-02-10 15:29:30 +02:00
level(0)
{
OBJECT_CONSTRUCTION;
double maxSideLengthSrc = std::max(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y);
double maxSideLengthDst = std::max(position.w, position.h);
double resize = maxSideLengthSrc / maxSideLengthDst;
2023-10-21 16:49:50 +02:00
Point newMinimapSize = Point(LOCPLINT->cb->getMapSize().x/ resize, LOCPLINT->cb->getMapSize().y / resize);
Point offset = Point((std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.x) / 2, (std::max(newMinimapSize.x, newMinimapSize.y) - newMinimapSize.y) / 2);
pos.x += offset.x;
pos.y += offset.y;
pos.w = newMinimapSize.x;
pos.h = newMinimapSize.y;
aiShield = std::make_shared<CPicture>(ImagePath::builtin("AIShield"), -offset);
aiShield->disable();
}
2023-02-10 15:29:30 +02:00
int3 CMinimap::pixelToTile(const Point & cursorPos) const
{
// 0 = top-left corner, 1 = bottom-right corner
2023-02-10 15:29:30 +02:00
double dx = static_cast<double>(cursorPos.x) / pos.w;
double dy = static_cast<double>(cursorPos.y) / pos.h;
int3 mapSizes = LOCPLINT->cb->getMapSize();
int tileX(std::round(mapSizes.x * dx));
int tileY(std::round(mapSizes.y * dy));
return int3(tileX, tileY, level);
}
2023-02-10 15:29:30 +02:00
Point CMinimap::tileToPixels(const int3 &tile) const
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
2023-02-10 15:29:30 +02:00
double stepX = static_cast<double>(pos.w) / mapSizes.x;
double stepY = static_cast<double>(pos.h) / mapSizes.y;
int x = static_cast<int>(stepX * tile.x);
int y = static_cast<int>(stepY * tile.y);
return Point(x,y);
}
2023-05-31 13:30:24 +02:00
void CMinimap::moveAdvMapSelection(const Point & positionGlobal)
{
2023-05-31 13:30:24 +02:00
int3 newLocation = pixelToTile(positionGlobal - pos.topLeft());
adventureInt->centerOnTile(newLocation);
2023-05-19 19:03:04 +02:00
if (!(adventureInt->isActive()))
GH.windows().totalRedraw(); //redraw this as well as inactive adventure map
else
redraw();//redraw only this
}
2023-05-31 13:30:24 +02:00
void CMinimap::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
{
2023-06-01 11:10:51 +02:00
if (pos.isInside(currentPosition))
moveAdvMapSelection(currentPosition);
2023-05-31 13:30:24 +02:00
}
void CMinimap::clickPressed(const Point & cursorPosition)
{
moveAdvMapSelection(cursorPosition);
}
void CMinimap::showPopupWindow(const Point & cursorPosition)
{
CRClickPopup::createAndPush(CGI->generaltexth->zelp[291].second);
}
void CMinimap::hover(bool on)
{
if(on)
2023-05-16 17:34:23 +02:00
GH.statusbar()->write(CGI->generaltexth->zelp[291].first);
else
2023-05-16 17:34:23 +02:00
GH.statusbar()->clear();
}
void CMinimap::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
{
moveAdvMapSelection(cursorPosition);
}
void CMinimap::showAll(Canvas & to)
{
2023-10-21 17:28:23 +02:00
CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), aiShield->pos);
CIntObject::showAll(to);
if(minimap)
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
//draw radar
Rect radar =
{
2023-02-10 23:29:13 +02:00
screenArea.x * pos.w / mapSizes.x,
screenArea.y * pos.h / mapSizes.y,
screenArea.w * pos.w / mapSizes.x - 1,
screenArea.h * pos.h / mapSizes.y - 1
};
Canvas clippedTarget(to, pos);
clippedTarget.drawBorderDashed(radar, Colors::PURPLE);
}
}
void CMinimap::update()
{
if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
return;
OBJECT_CONSTRUCTION;
minimap = std::make_shared<CMinimapInstance>(this, level);
redraw();
}
void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
{
if (screenArea == visibleArea && level == mapLevel)
2023-02-10 15:29:30 +02:00
return;
screenArea = visibleArea;
if(level != mapLevel)
{
level = mapLevel;
update();
}
else
redraw();
}
void CMinimap::setAIRadar(bool on)
{
if(on)
{
aiShield->enable();
minimap.reset();
}
else
{
aiShield->disable();
update();
}
2023-03-22 00:57:08 +02:00
redraw();
}
void CMinimap::updateTiles(const std::unordered_set<int3> & positions)
{
if(minimap)
2023-04-16 00:48:49 +02:00
{
for (auto const & tile : positions)
minimap->refreshTile(tile);
}
2023-03-16 18:36:31 +02:00
redraw();
}