| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 | 
							- /*
 
-  * 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 "CAdvMapInt.h"
 
- #include "CTerrainRect.h"
 
- #include "../widgets/Images.h"
 
- #include "../CGameInfo.h"
 
- #include "../CPlayerInterface.h"
 
- #include "../gui/CGuiHandler.h"
 
- #include "../render/Colors.h"
 
- #include "../renderSDL/SDL_PixelAccess.h"
 
- #include "../windows/InfoWindows.h"
 
- #include "../../CCallback.h"
 
- #include "../../lib/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 CSDL_Ext::fromSDL(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 CSDL_Ext::fromSDL(*graphics->neutralColor);
 
- 		if (player < PlayerColor::PLAYER_LIMIT)
 
- 			return CSDL_Ext::fromSDL(graphics->playerColors[player.getNum()]);
 
- 	}
 
- 	if (tile->blocked && (!tile->visitable))
 
- 		return tile->terType->minimapBlocked;
 
- 	else
 
- 		return tile->terType->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(CMinimap *Parent, int Level):
 
- 	parent(Parent),
 
- 	minimap(new Canvas(Point(LOCPLINT->cb->getMapSize().x, LOCPLINT->cb->getMapSize().y))),
 
- 	level(Level)
 
- {
 
- 	pos.w = parent->pos.w;
 
- 	pos.h = parent->pos.h;
 
- 	redrawMinimap();
 
- }
 
- void CMinimapInstance::showAll(SDL_Surface * to)
 
- {
 
- 	Canvas target(to);
 
- 	target.draw(*minimap, pos.topLeft(), pos.dimensions());
 
- }
 
- CMinimap::CMinimap(const Rect & position)
 
- 	: CIntObject(LCLICK | RCLICK | HOVER | MOVE, position.topLeft()),
 
- 	level(0)
 
- {
 
- 	OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
 
- 	pos.w = position.w;
 
- 	pos.h = position.h;
 
- 	aiShield = std::make_shared<CPicture>("AIShield");
 
- 	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()
 
- {
 
- 	int3 newLocation = pixelToTile(GH.getCursorPosition() - pos.topLeft());
 
- 	adventureInt->centerOn(newLocation);
 
- 	if (!(adventureInt->active & GENERAL))
 
- 		GH.totalRedraw(); //redraw this as well as inactive adventure map
 
- 	else
 
- 		redraw();//redraw only this
 
- }
 
- void CMinimap::clickLeft(tribool down, bool previousState)
 
- {
 
- 	if(down)
 
- 		moveAdvMapSelection();
 
- }
 
- void CMinimap::clickRight(tribool down, bool previousState)
 
- {
 
- 	if (down)
 
- 		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::mouseMoved(const Point & cursorPosition)
 
- {
 
- 	if(mouseState(MouseButton::LEFT))
 
- 		moveAdvMapSelection();
 
- }
 
- void CMinimap::showAll(SDL_Surface * to)
 
- {
 
- 	CIntObject::showAll(to);
 
- 	if(minimap)
 
- 	{
 
- 		Canvas target(to);
 
- 		int3 mapSizes = LOCPLINT->cb->getMapSize();
 
- 		Rect screenArea = adventureInt->terrainAreaTiles();
 
- 		//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(target, pos);
 
- 		clippedTarget.drawBorderDashed(radar, CSDL_Ext::fromSDL(Colors::PURPLE));
 
- 	}
 
- }
 
- void CMinimap::update()
 
- {
 
- 	if(aiShield->recActions & UPDATE) //AI turn is going on. There is no need to update minimap
 
- 		return;
 
- 	OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
 
- 	minimap = std::make_shared<CMinimapInstance>(this, level);
 
- 	redraw();
 
- }
 
- void CMinimap::setLevel(int newLevel)
 
- {
 
- 	if (level == newLevel)
 
- 		return;
 
- 	level = newLevel;
 
- 	update();
 
- }
 
- void CMinimap::setAIRadar(bool on)
 
- {
 
- 	if(on)
 
- 	{
 
- 		aiShield->enable();
 
- 		minimap.reset();
 
- 	}
 
- 	else
 
- 	{
 
- 		aiShield->disable();
 
- 		update();
 
- 	}
 
- 	// this may happen during AI turn when this interface is inactive
 
- 	// force redraw in order to properly update interface
 
- 	GH.totalRedraw();
 
- }
 
- void CMinimap::updateTile(const int3 &pos)
 
- {
 
- 	if(minimap)
 
- 		minimap->refreshTile(pos);
 
- }
 
 
  |