Bläddra i källkod

Merge pull request #2605 from Laserlicht/map_preview

Map preview
Nordsoft91 2 år sedan
förälder
incheckning
4edee00a97
3 ändrade filer med 158 tillägg och 3 borttagningar
  1. 122 1
      client/lobby/SelectionTab.cpp
  2. 23 1
      client/lobby/SelectionTab.h
  3. 13 1
      config/schemas/settings.json

+ 122 - 1
client/lobby/SelectionTab.cpp

@@ -18,6 +18,7 @@
 #include "../CServerHandler.h"
 #include "../gui/CGuiHandler.h"
 #include "../gui/Shortcut.h"
+#include "../gui/WindowHandler.h"
 #include "../widgets/CComponent.h"
 #include "../widgets/Buttons.h"
 #include "../widgets/MiscWidgets.h"
@@ -27,7 +28,9 @@
 #include "../windows/GUIClasses.h"
 #include "../windows/InfoWindows.h"
 #include "../render/CAnimation.h"
+#include "../render/Canvas.h"
 #include "../render/IImage.h"
+#include "../render/Graphics.h"
 
 #include "../../CCallback.h"
 
@@ -37,9 +40,12 @@
 #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"
+#include "../../lib/TerrainHandler.h"
 #include "../../lib/serializer/Connection.h"
 
 bool mapSorter::operator()(const std::shared_ptr<ElementInfo> aaa, const std::shared_ptr<ElementInfo> bbb)
@@ -357,7 +363,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);
 
-		CRClickPopup::createAndPush(text);
+		GH.windows().createAndPushWindow<CMapInfoTooltipBox>(text, ResourceID(curItems[py]->fileURI), tabType);
 	}
 }
 
@@ -806,6 +812,121 @@ std::unordered_set<ResourceID> SelectionTab::getFiles(std::string dirURI, int re
 	return ret;
 }
 
+SelectionTab::CMapInfoTooltipBox::CMapInfoTooltipBox(std::string text, ResourceID 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(ResourceID(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>("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 < PlayerColor::PLAYER_LIMIT)
+					{
+						color = graphics->playerColors[player.getNum()];
+						break;
+					}
+				}
+
+			canvas.drawPoint(Point(x, y), color);
+		}
+	
+	return canvas;
+}
+
+std::vector<std::shared_ptr<IImage>> SelectionTab::CMapInfoTooltipBox::createMinimaps(ResourceID 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 = IImage::createFromSurface(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)
 {

+ 23 - 1
client/lobby/SelectionTab.h

@@ -10,10 +10,15 @@
 #pragma once
 
 #include "CSelectionBase.h"
+VCMI_LIB_NAMESPACE_BEGIN
+class CMap;
+VCMI_LIB_NAMESPACE_END
 #include "../../lib/mapping/CMapInfo.h"
 
 class CSlider;
 class CLabel;
+class CPicture;
+class IImage;
 
 enum ESortBy
 {
@@ -62,6 +67,24 @@ class SelectionTab : public CIntObject
 	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(ResourceID resource, int size);
+	public:
+		CMapInfoTooltipBox(std::string text, ResourceID resource, ESelectionScreen tabType);
+	};
 public:
 	std::vector<std::shared_ptr<ElementInfo>> allItems;
 	std::vector<std::shared_ptr<ElementInfo>> curItems;
@@ -100,7 +123,6 @@ public:
 	void restoreLastSelection();
 
 private:
-
 	std::shared_ptr<CPicture> background;
 	std::shared_ptr<CSlider> slider;
 	std::vector<std::shared_ptr<CButton>> buttonsSortBy;

+ 13 - 1
config/schemas/settings.json

@@ -3,7 +3,7 @@
 {
 	"type" : "object",
 	"$schema" : "http://json-schema.org/draft-04/schema",
-	"required" : [ "general", "video", "adventure", "battle", "input", "server", "logging", "launcher", "gameTweaks" ],
+	"required" : [ "general", "video", "adventure", "battle", "input", "server", "logging", "launcher", "lobby", "gameTweaks" ],
 	"definitions" : {
 		"logLevelEnum" : {
 			"type" : "string",
@@ -526,6 +526,18 @@
 				}
 			}
 		},
+		"lobby" : {
+			"type" : "object",
+			"additionalProperties" : false,
+			"default" : {},
+			"required" : [ "mapPreview" ],
+			"properties" : {
+				"mapPreview" : {
+					"type" : "boolean",
+					"default" : true
+				}
+			}
+		},
 		"gameTweaks" : {
 			"type" : "object",
 			"default" : {},