1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-28 08:48:48 +02:00
vcmi/client/windows/CMapOverview.cpp

206 lines
5.9 KiB
C++
Raw Normal View History

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"
2023-09-30 17:56:51 +02:00
#include <vstd/DateUtils.h>
2023-09-30 15:05:12 +02:00
#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/CAnimation.h"
#include "../render/Canvas.h"
#include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h"
#include "../../lib/CGeneralTextHandler.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/serializer/BinaryDeserializer.h"
#include "../../lib/StartInfo.h"
#include "../../lib/rmg/CMapGenOptions.h"
2023-09-30 17:14:22 +02:00
CMapOverview::CMapOverview(std::string mapName, std::string fileName, std::string date, ResourcePath resource, ESelectionScreen tabType)
: CWindowObject(BORDERED | RCLICK_POPUP), resource(resource), mapName(mapName), fileName(fileName), date(date), tabType(tabType)
2023-09-30 15:05:12 +02:00
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
2023-09-30 17:14:22 +02:00
const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
pos = Rect(0, 0, config["items"][0]["rect"]["w"].Integer(), config["items"][0]["rect"]["h"].Integer());
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);
}
Canvas CMapOverview::CMapOverviewWidget::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const
{
Canvas canvas = Canvas(Point(map->width, map->height));
for (int y = 0; y < map->height; ++y)
for (int x = 0; x < map->width; ++x)
{
TerrainTile & tile = map->getTile(int3(x, y, layer));
ColorRGBA color = tile.terType->minimapUnblocked;
if (tile.blocked && (!tile.visitable))
color = tile.terType->minimapBlocked;
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;
}
std::vector<std::shared_ptr<IImage>> CMapOverview::CMapOverviewWidget::createMinimaps(ResourcePath resource, Point size) const
{
std::vector<std::shared_ptr<IImage>> ret = std::vector<std::shared_ptr<IImage>>();
CMapService mapService;
std::unique_ptr<CMap> map;
try
{
map = mapService.loadMap(resource);
}
catch (...)
{
return ret;
}
2023-09-30 20:03:33 +02:00
return createMinimaps(map, size);
}
std::vector<std::shared_ptr<IImage>> CMapOverview::CMapOverviewWidget::createMinimaps(std::unique_ptr<CMap> & map, Point size) const
{
std::vector<std::shared_ptr<IImage>> ret = std::vector<std::shared_ptr<IImage>>();
2023-09-30 15:05:12 +02:00
for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
{
Canvas canvas = createMinimapForLayer(map, i);
Canvas canvasScaled = Canvas(size);
canvasScaled.drawScaled(canvas, Point(0, 0), size);
std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
ret.push_back(img);
}
return ret;
}
std::shared_ptr<CPicture> CMapOverview::CMapOverviewWidget::buildDrawMinimap(const JsonNode & config) const
{
logGlobal->debug("Building widget drawMinimap");
auto rect = readRect(config["rect"]);
auto id = config["id"].Integer();
2023-09-30 17:14:22 +02:00
if(!renderImage)
return nullptr;
2023-09-30 22:49:22 +02:00
ResourcePath res = ResourcePath(p.resource.getName(), EResType::MAP);
2023-09-30 20:03:33 +02:00
std::unique_ptr<CMap> campaignMap = nullptr;
2023-09-30 22:49:22 +02:00
if(p.tabType != ESelectionScreen::newGame)
2023-09-30 20:03:33 +02:00
{
2023-09-30 22:49:22 +02:00
CLoadFile lf(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::SAVEGAME)), MINIMAL_SERIALIZATION_VERSION);
2023-09-30 20:03:33 +02:00
lf.checkMagicBytes(SAVEGAME_MAGIC);
std::unique_ptr<CMapHeader> mapHeader = std::make_unique<CMapHeader>();
StartInfo * startInfo;
lf >> *(mapHeader) >> startInfo;
if(startInfo->campState)
campaignMap = startInfo->campState->getMap(*startInfo->campState->currentScenario());
res = ResourcePath(startInfo->fileURI, EResType::MAP);
}
std::vector<std::shared_ptr<IImage>> images;
if(!campaignMap)
images = createMinimaps(res, Point(rect.w, rect.h));
else
images = createMinimaps(campaignMap, Point(rect.w, rect.h));
2023-09-30 17:14:22 +02:00
if(id >= images.size())
return nullptr;
2023-09-30 15:05:12 +02:00
return std::make_shared<CPicture>(images[id], Point(rect.x, rect.y));
}
2023-09-30 17:14:22 +02:00
CMapOverview::CMapOverviewWidget::CMapOverviewWidget(CMapOverview& parent):
2023-09-30 22:49:22 +02:00
InterfaceObjectConfigurable(), p(parent)
2023-09-30 17:14:22 +02:00
{
2023-09-30 22:49:22 +02:00
drawPlayerElements = p.tabType == ESelectionScreen::newGame;
2023-09-30 20:03:33 +02:00
renderImage = settings["lobby"]["mapPreview"].Bool();
2023-09-30 15:05:12 +02:00
const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
REGISTER_BUILDER("drawMinimap", &CMapOverview::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
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())
{
std::time_t time = boost::filesystem::last_write_time(*CResourceHandler::get()->getResourceName(ResourcePath(p.resource.getName(), EResType::MAP)));
w->setText(vstd::getFormattedDateTime(time));
}
else
w->setText(p.date);
}
2023-09-30 15:05:12 +02:00
}