2023-09-30 15:05:12 +02:00
|
|
|
/*
|
|
|
|
* CMapOverview.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 "CMapOverview.h"
|
|
|
|
|
|
|
|
#include "../lobby/SelectionTab.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/WindowHandler.h"
|
|
|
|
#include "../widgets/CComponent.h"
|
|
|
|
#include "../widgets/MiscWidgets.h"
|
|
|
|
#include "../widgets/TextControls.h"
|
|
|
|
#include "../windows/GUIClasses.h"
|
|
|
|
#include "../windows/InfoWindows.h"
|
|
|
|
#include "../render/Canvas.h"
|
|
|
|
#include "../render/IImage.h"
|
|
|
|
#include "../render/IRenderHandler.h"
|
|
|
|
#include "../render/Graphics.h"
|
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/campaign/CampaignState.h"
|
|
|
|
#include "../../lib/mapping/CMap.h"
|
|
|
|
#include "../../lib/mapping/CMapService.h"
|
|
|
|
#include "../../lib/mapping/CMapInfo.h"
|
|
|
|
#include "../../lib/mapping/CMapHeader.h"
|
|
|
|
#include "../../lib/mapping/MapFormat.h"
|
|
|
|
#include "../../lib/TerrainHandler.h"
|
2023-09-30 17:56:51 +02:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2023-09-30 15:05:12 +02:00
|
|
|
|
2023-09-30 20:03:33 +02:00
|
|
|
#include "../../lib/StartInfo.h"
|
|
|
|
#include "../../lib/rmg/CMapGenOptions.h"
|
2024-07-20 14:55:17 +02:00
|
|
|
#include "../../lib/serializer/CLoadFile.h"
|
|
|
|
#include "../../lib/texts/CGeneralTextHandler.h"
|
|
|
|
#include "../../lib/texts/TextOperations.h"
|
2023-09-30 20:03:33 +02:00
|
|
|
|
2024-08-12 20:26:30 +02:00
|
|
|
CMapOverview::CMapOverview(const std::string & mapName, const std::string & fileName, const std::string & date, const std::string & author, const std::string & version, const ResourcePath & resource, ESelectionScreen tabType)
|
2024-06-29 13:13:59 +02:00
|
|
|
: CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), author(author), version(version), tabType(tabType)
|
2023-09-30 15:05:12 +02:00
|
|
|
{
|
|
|
|
|
2024-08-09 17:30:04 +02:00
|
|
|
OBJECT_CONSTRUCTION;
|
2023-09-30 15:05:12 +02:00
|
|
|
|
2023-09-30 17:14:22 +02:00
|
|
|
widget = std::make_shared<CMapOverviewWidget>(*this);
|
2023-09-30 15:05:12 +02:00
|
|
|
|
|
|
|
updateShadow();
|
|
|
|
|
|
|
|
center(GH.getCursorPosition()); //center on mouse
|
|
|
|
#ifdef VCMI_MOBILE
|
|
|
|
moveBy({0, -pos.h / 2});
|
|
|
|
#endif
|
|
|
|
fitToScreen(10);
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
Canvas CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
|
2023-09-30 15:05:12 +02:00
|
|
|
{
|
2024-07-22 13:00:45 +02:00
|
|
|
Canvas canvas = Canvas(Point(map->width, map->height), CanvasScalingPolicy::IGNORE);
|
2023-09-30 15:05:12 +02:00
|
|
|
|
|
|
|
for (int y = 0; y < map->height; ++y)
|
|
|
|
for (int x = 0; x < map->width; ++x)
|
|
|
|
{
|
|
|
|
TerrainTile & tile = map->getTile(int3(x, y, layer));
|
|
|
|
|
2024-07-13 20:37:13 +02:00
|
|
|
ColorRGBA color = tile.getTerrain()->minimapUnblocked;
|
|
|
|
if (tile.blocked() && !tile.visitable())
|
|
|
|
color = tile.getTerrain()->minimapBlocked;
|
2023-09-30 15:05:12 +02:00
|
|
|
|
|
|
|
if(drawPlayerElements)
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
color = graphics->neutralColor;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (player.isValidPlayer())
|
|
|
|
{
|
|
|
|
color = graphics->playerColors[player.getNum()];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas.drawPoint(Point(x, y), color);
|
|
|
|
}
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
std::vector<Canvas> CMapOverviewWidget::createMinimaps(ResourcePath resource) const
|
2023-09-30 15:05:12 +02:00
|
|
|
{
|
2024-01-17 14:50:00 +02:00
|
|
|
auto ret = std::vector<Canvas>();
|
2023-09-30 15:05:12 +02:00
|
|
|
|
|
|
|
CMapService mapService;
|
|
|
|
std::unique_ptr<CMap> map;
|
|
|
|
try
|
|
|
|
{
|
2024-01-01 16:37:48 +02:00
|
|
|
map = mapService.loadMap(resource, nullptr);
|
2023-09-30 15:05:12 +02:00
|
|
|
}
|
2023-10-01 17:35:56 +02:00
|
|
|
catch (const std::exception & e)
|
2023-09-30 15:05:12 +02:00
|
|
|
{
|
2023-10-01 17:35:56 +02:00
|
|
|
logGlobal->warn("Failed to generate map preview! %s", e.what());
|
2023-09-30 15:05:12 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-30 23:50:27 +02:00
|
|
|
return createMinimaps(map);
|
2023-09-30 20:03:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
std::vector<Canvas> CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map) const
|
2023-09-30 20:03:33 +02:00
|
|
|
{
|
2024-01-17 14:50:00 +02:00
|
|
|
auto ret = std::vector<Canvas>();
|
2023-09-30 20:03:33 +02:00
|
|
|
|
2023-09-30 15:05:12 +02:00
|
|
|
for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
|
2023-09-30 23:50:27 +02:00
|
|
|
ret.push_back(createMinimapForLayer(map, i));
|
2023-09-30 15:05:12 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
std::shared_ptr<CPicture> CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
|
2023-09-30 15:05:12 +02:00
|
|
|
{
|
|
|
|
logGlobal->debug("Building widget drawMinimap");
|
|
|
|
|
|
|
|
auto rect = readRect(config["rect"]);
|
|
|
|
auto id = config["id"].Integer();
|
2023-09-30 17:14:22 +02:00
|
|
|
|
2023-09-30 23:50:27 +02:00
|
|
|
if(id >= minimaps.size())
|
2023-09-30 17:14:22 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2023-10-21 16:49:50 +02:00
|
|
|
Rect minimapRect = minimaps[id].getRenderArea();
|
2024-06-24 03:23:26 +02:00
|
|
|
double maxSideLengthSrc = std::max(minimapRect.w, minimapRect.h);
|
|
|
|
double maxSideLengthDst = std::max(rect.w, rect.h);
|
|
|
|
double resize = maxSideLengthSrc / maxSideLengthDst;
|
2023-10-21 16:49:50 +02:00
|
|
|
Point newMinimapSize = Point(minimapRect.w / resize, minimapRect.h / resize);
|
|
|
|
|
2024-07-22 13:00:45 +02:00
|
|
|
Canvas canvasScaled = Canvas(Point(rect.w, rect.h), CanvasScalingPolicy::AUTO);
|
2023-10-21 16:49:50 +02:00
|
|
|
canvasScaled.drawScaled(minimaps[id], Point((rect.w - newMinimapSize.x) / 2, (rect.h - newMinimapSize.y) / 2), newMinimapSize);
|
2023-09-30 23:50:27 +02:00
|
|
|
std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
|
|
|
|
|
|
|
|
return std::make_shared<CPicture>(img, Point(rect.x, rect.y));
|
|
|
|
}
|
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
|
2023-09-30 23:50:27 +02:00
|
|
|
InterfaceObjectConfigurable(), p(parent)
|
|
|
|
{
|
|
|
|
drawPlayerElements = p.tabType == ESelectionScreen::newGame;
|
|
|
|
|
2023-10-01 00:18:25 +02:00
|
|
|
const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
|
|
|
|
|
2023-10-11 11:23:38 +02:00
|
|
|
if(settings["lobby"]["mapPreview"].Bool() && p.tabType != ESelectionScreen::campaignList)
|
2023-09-30 20:03:33 +02:00
|
|
|
{
|
2023-10-01 00:18:25 +02:00
|
|
|
ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
|
|
|
|
std::unique_ptr<CMap> campaignMap = nullptr;
|
|
|
|
if(p.tabType != ESelectionScreen::newGame && config["variables"]["mapPreviewForSaves"].Bool())
|
|
|
|
{
|
2024-01-20 20:34:51 +02:00
|
|
|
CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), ESerializationVersion::MINIMAL);
|
2023-10-01 00:18:25 +02:00
|
|
|
lf.checkMagicBytes(SAVEGAME_MAGIC);
|
2023-09-30 20:03:33 +02:00
|
|
|
|
2024-01-17 14:50:00 +02:00
|
|
|
auto mapHeader = std::make_unique<CMapHeader>();
|
2023-10-01 00:18:25 +02:00
|
|
|
StartInfo * startInfo;
|
|
|
|
lf >> *(mapHeader) >> startInfo;
|
2023-09-30 20:03:33 +02:00
|
|
|
|
2023-10-01 00:18:25 +02:00
|
|
|
if(startInfo->campState)
|
2024-01-01 16:37:48 +02:00
|
|
|
campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario(), nullptr);
|
2023-10-01 00:18:25 +02:00
|
|
|
res = ResourcePath(startInfo->fileURI, EResType::MAP);
|
|
|
|
}
|
|
|
|
if(!campaignMap)
|
|
|
|
minimaps = createMinimaps(res);
|
|
|
|
else
|
|
|
|
minimaps = createMinimaps(campaignMap);
|
2023-09-30 20:03:33 +02:00
|
|
|
}
|
2023-09-30 15:05:12 +02:00
|
|
|
|
2023-10-01 17:35:56 +02:00
|
|
|
REGISTER_BUILDER("drawMinimap", &CMapOverviewWidget::buildDrawMinimap);
|
2023-09-30 17:14:22 +02:00
|
|
|
|
2023-09-30 15:05:12 +02:00
|
|
|
build(config);
|
2023-09-30 22:49:22 +02:00
|
|
|
|
2023-10-01 19:44:04 +02:00
|
|
|
if(auto w = widget<CFilledTexture>("background"))
|
|
|
|
{
|
|
|
|
p.pos = w->pos;
|
|
|
|
}
|
2023-09-30 22:49:22 +02:00
|
|
|
if(auto w = widget<CTextBox>("fileName"))
|
|
|
|
{
|
|
|
|
w->setText(p.fileName);
|
|
|
|
}
|
|
|
|
if(auto w = widget<CLabel>("mapName"))
|
|
|
|
{
|
|
|
|
w->setText(p.mapName);
|
|
|
|
}
|
|
|
|
if(auto w = widget<CLabel>("date"))
|
|
|
|
{
|
|
|
|
if(p.date.empty())
|
|
|
|
{
|
2023-10-11 11:23:38 +02:00
|
|
|
std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), p.tabType == ESelectionScreen::campaignList ? EResType::CAMPAIGN : EResType::MAP)));
|
2023-12-16 22:10:27 +02:00
|
|
|
w->setText(TextOperations::getFormattedDateTimeLocal(time));
|
2023-09-30 22:49:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
w->setText(p.date);
|
|
|
|
}
|
2024-06-29 13:13:59 +02:00
|
|
|
if(auto w = widget<CLabel>("author"))
|
|
|
|
{
|
|
|
|
w->setText(p.author.empty() ? "-" : p.author);
|
|
|
|
}
|
|
|
|
if(auto w = widget<CLabel>("version"))
|
|
|
|
{
|
|
|
|
w->setText(p.version);
|
|
|
|
}
|
2023-10-01 00:35:11 +02:00
|
|
|
if(auto w = widget<CLabel>("noUnderground"))
|
|
|
|
{
|
|
|
|
if(minimaps.size() == 0)
|
|
|
|
w->setText("");
|
|
|
|
}
|
2023-10-01 00:46:29 +02:00
|
|
|
}
|