1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

map overview rework

This commit is contained in:
Laserlicht 2023-09-30 15:05:12 +02:00 committed by GitHub
parent f54a3c4e6c
commit 718bafc8af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 259 additions and 139 deletions

View File

@ -119,6 +119,7 @@ set(client_SRCS
windows/CHeroOverview.cpp
windows/CHeroWindow.cpp
windows/CKingdomInterface.cpp
windows/CMapOverview.cpp
windows/CMessage.cpp
windows/CPuzzleWindow.cpp
windows/CQuestLog.cpp
@ -285,6 +286,7 @@ set(client_HEADERS
windows/CHeroWindow.h
windows/CKingdomInterface.h
windows/CMessage.h
windows/CMapOverview.h
windows/CPuzzleWindow.h
windows/CQuestLog.h
windows/CSpellWindow.h

View File

@ -27,11 +27,10 @@
#include "../widgets/TextControls.h"
#include "../windows/GUIClasses.h"
#include "../windows/InfoWindows.h"
#include "../windows/CMapOverview.h"
#include "../render/CAnimation.h"
#include "../render/Canvas.h"
#include "../render/IImage.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h"
#include "../../CCallback.h"
@ -41,8 +40,6 @@
#include "../../lib/GameSettings.h"
#include "../../lib/filesystem/Filesystem.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"
@ -371,7 +368,7 @@ void SelectionTab::showPopupWindow(const Point & cursorPosition)
if(curItems[py]->date != "")
text += boost::str(boost::format("\r\n\r\n%1%:\r\n%2%") % CGI->generaltexth->translate("vcmi.lobby.creationDate") % curItems[py]->date);
GH.windows().createAndPushWindow<CMapInfoTooltipBox>(text, ResourcePath(curItems[py]->fileURI), tabType);
GH.windows().createAndPushWindow<CMapOverview>(text, ResourcePath(curItems[py]->fileURI), tabType);
}
else
CRClickPopup::createAndPush(curItems[py]->folderName);
@ -822,121 +819,6 @@ std::unordered_set<ResourcePath> SelectionTab::getFiles(std::string dirURI, ERes
return ret;
}
SelectionTab::CMapInfoTooltipBox::CMapInfoTooltipBox(std::string text, ResourcePath resource, ESelectionScreen tabType)
: CWindowObject(BORDERED | RCLICK_POPUP)
{
drawPlayerElements = tabType == ESelectionScreen::newGame;
renderImage = tabType == ESelectionScreen::newGame && settings["lobby"]["mapPreview"].Bool();
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
std::vector<std::shared_ptr<IImage>> mapLayerImages;
if(renderImage)
mapLayerImages = createMinimaps(ResourcePath(resource.getName(), EResType::MAP), IMAGE_SIZE);
if(mapLayerImages.size() == 0)
renderImage = false;
pos = Rect(0, 0, 3 * BORDER + 2 * IMAGE_SIZE, 2000);
auto drawLabel = [&]() {
label = std::make_shared<CTextBox>(text, Rect(BORDER, BORDER, BORDER + 2 * IMAGE_SIZE, 350), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
if(!label->slider)
label->resize(Point(BORDER + 2 * IMAGE_SIZE, label->label->textSize.y));
};
drawLabel();
int textHeight = std::min(350, label->label->textSize.y);
pos.h = BORDER + textHeight + BORDER;
if(renderImage)
pos.h += IMAGE_SIZE + BORDER;
backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
updateShadow();
drawLabel();
if(renderImage)
{
if(mapLayerImages.size() == 1)
image1 = std::make_shared<CPicture>(mapLayerImages[0], Point(BORDER + (BORDER + IMAGE_SIZE) / 2, textHeight + 2 * BORDER));
else
{
image1 = std::make_shared<CPicture>(mapLayerImages[0], Point(BORDER, textHeight + 2 * BORDER));
image2 = std::make_shared<CPicture>(mapLayerImages[1], Point(BORDER + IMAGE_SIZE + BORDER, textHeight + 2 * BORDER));
}
}
center(GH.getCursorPosition()); //center on mouse
#ifdef VCMI_MOBILE
moveBy({0, -pos.h / 2});
#endif
fitToScreen(10);
}
Canvas SelectionTab::CMapInfoTooltipBox::createMinimapForLayer(std::unique_ptr<CMap> & map, int layer)
{
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>> SelectionTab::CMapInfoTooltipBox::createMinimaps(ResourcePath resource, int size)
{
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;
}
for(int i = 0; i < (map->twoLevel ? 2 : 1); i++)
{
Canvas canvas = createMinimapForLayer(map, i);
Canvas canvasScaled = Canvas(Point(size, size));
canvasScaled.drawScaled(canvas, Point(0, 0), Point(size, size));
std::shared_ptr<IImage> img = GH.renderHandler().createImage(canvasScaled.getInternalSurface());
ret.push_back(img);
}
return ret;
}
SelectionTab::ListItem::ListItem(Point position, std::shared_ptr<CAnimation> iconsFormats, std::shared_ptr<CAnimation> iconsVictory, std::shared_ptr<CAnimation> iconsLoss)
: CIntObject(LCLICK, position)
{

View File

@ -67,25 +67,6 @@ class SelectionTab : public CIntObject
// FIXME: CSelectionBase use them too!
std::shared_ptr<CAnimation> iconsVictoryCondition;
std::shared_ptr<CAnimation> iconsLossCondition;
class CMapInfoTooltipBox : public CWindowObject
{
const int IMAGE_SIZE = 169;
const int BORDER = 30;
bool drawPlayerElements;
bool renderImage;
std::shared_ptr<CFilledTexture> backgroundTexture;
std::shared_ptr<CTextBox> label;
std::shared_ptr<CPicture> image1;
std::shared_ptr<CPicture> image2;
Canvas createMinimapForLayer(std::unique_ptr<CMap> & map, int layer);
std::vector<std::shared_ptr<IImage>> createMinimaps(ResourcePath resource, int size);
public:
CMapInfoTooltipBox(std::string text, ResourcePath resource, ESelectionScreen tabType);
};
public:
std::vector<std::shared_ptr<ElementInfo>> allItems;
std::vector<std::shared_ptr<ElementInfo>> curItems;

View File

@ -0,0 +1,197 @@
/*
* 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/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"
CMapOverview::CMapOverview(std::string text, ResourcePath resource, ESelectionScreen tabType)
: CWindowObject(BORDERED | RCLICK_POPUP)
{
OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
pos = Rect(0, 0, 400, 300);
widget = std::make_shared<CMapOverviewWidget>(text, resource, tabType);
updateShadow();
/*
std::vector<std::shared_ptr<IImage>> mapLayerImages;
if(renderImage)
mapLayerImages = createMinimaps(ResourcePath(resource.getName(), EResType::MAP), IMAGE_SIZE);
if(mapLayerImages.size() == 0)
renderImage = false;
pos = Rect(0, 0, 3 * BORDER + 2 * IMAGE_SIZE, 2000);
auto drawLabel = [&]() {
label = std::make_shared<CTextBox>(text, Rect(BORDER, BORDER, BORDER + 2 * IMAGE_SIZE, 350), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
if(!label->slider)
label->resize(Point(BORDER + 2 * IMAGE_SIZE, label->label->textSize.y));
};
drawLabel();
int textHeight = std::min(350, label->label->textSize.y);
pos.h = BORDER + textHeight + BORDER;
if(renderImage)
pos.h += IMAGE_SIZE + BORDER;
backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), pos);
updateShadow();
drawLabel();
if(renderImage)
{
if(mapLayerImages.size() == 1)
image1 = std::make_shared<CPicture>(mapLayerImages[0], Point(BORDER + (BORDER + IMAGE_SIZE) / 2, textHeight + 2 * BORDER));
else
{
image1 = std::make_shared<CPicture>(mapLayerImages[0], Point(BORDER, textHeight + 2 * BORDER));
image2 = std::make_shared<CPicture>(mapLayerImages[1], Point(BORDER + IMAGE_SIZE + BORDER, textHeight + 2 * BORDER));
}
}
*/
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;
}
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<TransparentFilledRectangle> CMapOverview::CMapOverviewWidget::CMapOverviewWidget::buildDrawTransparentRect(const JsonNode & config) const
{
logGlobal->debug("Building widget drawTransparentRect");
auto rect = readRect(config["rect"]);
auto color = readColor(config["color"]);
if(!config["colorLine"].isNull())
{
auto colorLine = readColor(config["colorLine"]);
return std::make_shared<TransparentFilledRectangle>(rect, color, colorLine);
}
return std::make_shared<TransparentFilledRectangle>(rect, color);
}
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();
const std::vector<std::shared_ptr<IImage>> images = createMinimaps(ResourcePath(resource.getName(), EResType::MAP), Point(rect.w, rect.h));
return std::make_shared<CPicture>(images[id], Point(rect.x, rect.y));
}
CMapOverview::CMapOverviewWidget::CMapOverviewWidget(std::string text, ResourcePath resource, ESelectionScreen tabType):
InterfaceObjectConfigurable(), resource(resource)
{
drawPlayerElements = tabType == ESelectionScreen::newGame;
renderImage = tabType == ESelectionScreen::newGame && settings["lobby"]["mapPreview"].Bool();
const JsonNode config(JsonPath::builtin("config/widgets/mapOverview.json"));
REGISTER_BUILDER("drawTransparentRect", &CMapOverview::CMapOverviewWidget::buildDrawTransparentRect);
REGISTER_BUILDER("drawMinimap", &CMapOverview::CMapOverviewWidget::buildDrawMinimap);
build(config);
}

View File

@ -0,0 +1,58 @@
/*
* CMapOverview.h, 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
*
*/
#pragma once
VCMI_LIB_NAMESPACE_BEGIN
class CMap;
VCMI_LIB_NAMESPACE_END
#include "CWindowObject.h"
#include "../../lib/filesystem/ResourcePath.h"
#include "../gui/InterfaceObjectConfigurable.h"
class CSlider;
class CLabel;
class CPicture;
class CFilledTexture;
class CTextBox;
class IImage;
class Canvas;
class TransparentFilledRectangle;
enum ESelectionScreen : ui8;
class CMapOverview : public CWindowObject
{
//const int IMAGE_SIZE = 169;
//const int BORDER = 30;
std::shared_ptr<CFilledTexture> backgroundTexture;
std::shared_ptr<CTextBox> label;
std::shared_ptr<CPicture> image1;
std::shared_ptr<CPicture> image2;
class CMapOverviewWidget : public InterfaceObjectConfigurable
{
ResourcePath resource;
bool drawPlayerElements;
bool renderImage;
Canvas createMinimapForLayer(std::unique_ptr<CMap> & map, int layer) const;
std::vector<std::shared_ptr<IImage>> createMinimaps(ResourcePath resource, Point size) const;
std::shared_ptr<TransparentFilledRectangle> buildDrawTransparentRect(const JsonNode & config) const;
std::shared_ptr<CPicture> buildDrawMinimap(const JsonNode & config) const;
public:
CMapOverviewWidget(std::string text, ResourcePath resource, ESelectionScreen tabType);
};
std::shared_ptr<CMapOverviewWidget> widget;
public:
CMapOverview(std::string text, ResourcePath resource, ESelectionScreen tabType);
};