1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* 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 "../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"
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
for (const CGObjectInstance *obj : tile->blockingObjects)
{
PlayerColor player = obj->getOwner();
if(player == PlayerColor::NEUTRAL)
return graphics->neutralColor;
if (player.isValidPlayer())
return graphics->playerColors[player.getNum()];
}
if (tile->blocked() && !tile->visitable())
return tile->getTerrain()->minimapBlocked;
else
return tile->getTerrain()->minimapUnblocked;
}
void CMinimapInstance::refreshTile(const int3 &tile)
{
if (level == tile.z)
minimap->drawPoint(Point(tile.x, tile.y), getTileColor(tile));
}
void CMinimapInstance::redrawMinimap()
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
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(const Point & position, const Point & dimensions, int Level):
minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y), CanvasScalingPolicy::IGNORE)),
level(Level)
{
pos += position;
pos.w = dimensions.x;
pos.h = dimensions.y;
redrawMinimap();
}
CMinimapInstance::~CMinimapInstance() = default;
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()),
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;
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();
}
int3 CMinimap::pixelToTile(const Point & cursorPos) const
{
// 0 = top-left corner, 1 = bottom-right corner
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);
}
Point CMinimap::tileToPixels(const int3 &tile) const
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
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);
}
void CMinimap::moveAdvMapSelection(const Point & positionGlobal)
{
int3 newLocation = pixelToTile(positionGlobal - pos.topLeft());
adventureInt->centerOnTile(newLocation);
if (!(adventureInt->isActive()))
GH.windows().totalRedraw(); //redraw this as well as inactive adventure map
else
redraw();//redraw only this
}
void CMinimap::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
{
if (pos.isInside(currentPosition))
moveAdvMapSelection(currentPosition);
}
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)
GH.statusbar()->write(CGI->generaltexth->zelp[291].first);
else
GH.statusbar()->clear();
}
void CMinimap::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
{
moveAdvMapSelection(cursorPosition);
}
void CMinimap::showAll(Canvas & to)
{
CanvasClipRectGuard guard(to, aiShield->pos);
CIntObject::showAll(to);
if(minimap)
{
int3 mapSizes = LOCPLINT->cb->getMapSize();
//draw radar
Rect radar =
{
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>(Point(0,0), pos.dimensions(), level);
redraw();
}
void CMinimap::onMapViewMoved(const Rect & visibleArea, int mapLevel)
{
if (screenArea == visibleArea && level == mapLevel)
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();
}
redraw();
}
void CMinimap::updateTiles(const std::unordered_set<int3> & positions)
{
if(minimap)
{
for (auto const & tile : positions)
minimap->refreshTile(tile);
}
redraw();
}
|