Browse Source

code review

Laserlicht 5 months ago
parent
commit
5aa5eff285

+ 3 - 0
lib/rmg/CRmgTemplate.cpp

@@ -522,6 +522,9 @@ void ZoneOptions::serializeJson(JsonSerializeFormat & handler)
 	handler.serializeInt("visiblePositionX", visiblePosition.x);
 	handler.serializeInt("visiblePositionY", visiblePosition.y);
 	handler.serializeFloat("visibleSize", visibleSize);
+
+	if(!handler.saving && visibleSize < 0.01)
+		visibleSize = 1.0;
 }
 
 ZoneConnection::ZoneConnection():

+ 13 - 29
mapeditor/helper.cpp

@@ -27,13 +27,13 @@
 #include "../lib/serializer/JsonDeserializer.h"
 #include "../lib/serializer/CSaveFile.h"
 
-std::unique_ptr<CMap> Helper::openMapInternal(const QString & filenameSelect, IGameInfoCallback * cb)
+ResourcePath addFilesystemAndGetResource(const QString & filenameSelect, EResType type, const std::string & typeName)
 {
 	QFileInfo fi(filenameSelect);
 	std::string fname = fi.fileName().toStdString();
 	std::string fdir = fi.dir().path().toStdString();
 	
-	ResourcePath resId("MAPEDITOR/" + fname, EResType::MAP);
+	ResourcePath resId("MAPEDITOR/" + fname, type);
 	
 	//addFilesystem takes care about memory deallocation if case of failure, no memory leak here
 	auto mapEditorFilesystem = std::make_unique<CFilesystemLoader>("MAPEDITOR/", fdir, 0);
@@ -41,7 +41,14 @@ std::unique_ptr<CMap> Helper::openMapInternal(const QString & filenameSelect, IG
 	CResourceHandler::addFilesystem("local", "mapEditor", std::move(mapEditorFilesystem));
 	
 	if(!CResourceHandler::get("mapEditor")->existsResource(resId))
-		throw std::runtime_error("Cannot open map from this folder");
+		throw std::runtime_error("Cannot open " + typeName + " from this folder");
+
+	return resId;
+}
+
+std::unique_ptr<CMap> Helper::openMapInternal(const QString & filenameSelect, IGameInfoCallback * cb)
+{
+	auto resId = addFilesystemAndGetResource(filenameSelect, EResType::MAP, "map");
 	
 	CMapService mapService;
 	if(auto header = mapService.loadMapHeader(resId))
@@ -62,19 +69,8 @@ std::unique_ptr<CMap> Helper::openMapInternal(const QString & filenameSelect, IG
 
 std::shared_ptr<CampaignState> Helper::openCampaignInternal(const QString & filenameSelect)
 {
-	QFileInfo fi(filenameSelect);
-	std::string fname = fi.fileName().toStdString();
-	std::string fdir = fi.dir().path().toStdString();
-	
-	ResourcePath resId("MAPEDITOR/" + fname, EResType::CAMPAIGN);
-	
-	//addFilesystem takes care about memory deallocation if case of failure, no memory leak here
-	auto mapEditorFilesystem = std::make_unique<CFilesystemLoader>("MAPEDITOR/", fdir, 0);
-	CResourceHandler::removeFilesystem("local", "mapEditor");
-	CResourceHandler::addFilesystem("local", "mapEditor", std::move(mapEditorFilesystem));
-	
-	if(!CResourceHandler::get("mapEditor")->existsResource(resId))
-		throw std::runtime_error("Cannot open campaign from this folder");
+	auto resId = addFilesystemAndGetResource(filenameSelect, EResType::CAMPAIGN, "campaign");
+
 	if(auto campaign = CampaignHandler::getCampaign(resId.getName()))
 		return campaign;
 	else
@@ -83,19 +79,7 @@ std::shared_ptr<CampaignState> Helper::openCampaignInternal(const QString & file
 
 std::map<std::string, std::shared_ptr<CRmgTemplate>> Helper::openTemplateInternal(const QString & filenameSelect)
 {
-	QFileInfo fi(filenameSelect);
-	std::string fname = fi.fileName().toStdString();
-	std::string fdir = fi.dir().path().toStdString();
-	
-	ResourcePath resId("MAPEDITOR/" + fname, EResType::JSON);
-	
-	//addFilesystem takes care about memory deallocation if case of failure, no memory leak here
-	auto mapEditorFilesystem = std::make_unique<CFilesystemLoader>("MAPEDITOR/", fdir, 0);
-	CResourceHandler::removeFilesystem("local", "mapEditor");
-	CResourceHandler::addFilesystem("local", "mapEditor", std::move(mapEditorFilesystem));
-	
-	if(!CResourceHandler::get("mapEditor")->existsResource(resId))
-		throw std::runtime_error("Cannot open template from this folder");
+	auto resId = addFilesystemAndGetResource(filenameSelect, EResType::JSON, "template");
 
 	auto data = CResourceHandler::get()->load(resId)->readAll();
 	JsonNode nodes(reinterpret_cast<std::byte *>(data.first.get()), data.second, resId.getName());

+ 2 - 0
mapeditor/helper.h

@@ -10,6 +10,8 @@
 
 #pragma once
 
+#include "../lib/filesystem/ResourcePath.h"
+
 class CMap;
 class CampaignState;
 class CRmgTemplate;

+ 14 - 14
mapeditor/icons/templateSquare.svg

@@ -154,46 +154,46 @@
    style="font-size:3.07692px;fill:#ffffff;"
    x="4.0261083"
    y="21.015579"
-   id="textWood"
+   id="textwood"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="8.2191515"
    y="21.010046"
-   id="textOre"
+   id="textore"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="12.484079"
    y="21.049248"
-   id="textMercury"
+   id="textmercury"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="16.661751"
    y="21.020365"
-   id="textSulfur"
+   id="textsulfur"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="20.896933"
    y="21.078743"
-   id="textCrystal"
+   id="textcrystal"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="25.133026"
    y="21.049547"
-   id="textGems"
+   id="textgems"
    text-anchor="middle">99</text>
 	<text
    style="font-size:3.07692px;fill:#ffffff;"
    x="4.0413303"
    y="27.417381"
-   id="textGold"
+   id="textgold"
    text-anchor="middle">99</text>
 	<g
-   id="iconSulfur"
+   id="iconsulfur"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-76.735738,-145.67812)">
 		<path
@@ -218,7 +218,7 @@
    id="path3" />
 	</g>
 	<g
-   id="iconGold"
+   id="icongold"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-90.060712,-144.27231)">
 		<path
@@ -329,7 +329,7 @@
    transform="translate(-84.553401,34.893164)" />
 	</g>
 	<g
-   id="iconOre"
+   id="iconore"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-97.429655,-129.12754)">
 		<path
@@ -406,7 +406,7 @@
    id="path47" />
 	</g>
 	<g
-   id="iconWood"
+   id="iconwood"
    opacity="1.0"
    transform="matrix(0.95215139,5.818528e-4,-0.16788999,0.88218099,-70.889511,-135.64427)"
    style="stroke-width:0.951609">
@@ -682,7 +682,7 @@
 		</g>
 	</g>
 	<g
-   id="iconGems"
+   id="icongems"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-74.159393,-117.99296)">
 		<path
@@ -771,7 +771,7 @@
    id="path73" />
 	</g>
 	<g
-   id="iconMercury"
+   id="iconmercury"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-64.001803,-150.87368)">
 		<path
@@ -876,7 +876,7 @@
    id="path91" />
 	</g>
 	<g
-   id="iconCrystal"
+   id="iconcrystal"
    opacity="1.0"
    transform="matrix(0.87219868,0,0,0.87219868,-78.871113,-144.49622)">
 		<path

+ 3 - 12
mapeditor/templateeditor/graphicelements/CardItem.cpp

@@ -14,6 +14,7 @@
 #include <QObject>
 
 #include "../../../lib/constants/EntityIdentifiers.h"
+#include "../../../lib/constants/StringConstants.h"
 #include "../../../lib/rmg/CRmgTemplate.h"
 
 QDomElement CardItem::getElementById(const QDomDocument& doc, const QString& id)
@@ -148,21 +149,11 @@ int CardItem::getId()
 
 void CardItem::setResAmount(GameResID res, int val)
 {
-	std::map<GameResID, QString> names =
-	{
-		{ GameResID::WOOD,    "Wood"    },
-		{ GameResID::ORE,     "Ore"     },
-		{ GameResID::MERCURY, "Mercury" },
-		{ GameResID::SULFUR,  "Sulfur"  },
-		{ GameResID::CRYSTAL, "Crystal" },
-		{ GameResID::GEMS,    "Gems"    },
-		{ GameResID::GOLD,    "Gold"    },
-	};
-	auto textElem = getElementById(doc, "text" + names[res]);
+	auto textElem = getElementById(doc, "text" + QString::fromStdString(GameConstants::RESOURCE_NAMES[res]));
 	textElem.setAttribute("style", textElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:" + QColor(useBlackText ? Qt::black : Qt::white).name() + ";"));
 	textElem.firstChild().setNodeValue(val ? QString::number(val) : "");
 
-	auto iconElem = getElementById(doc, "icon" + names[res]);
+	auto iconElem = getElementById(doc, "icon" + QString::fromStdString(GameConstants::RESOURCE_NAMES[res]));
 	iconElem.setAttribute("opacity", val ? "1.0" : "0.1");
 }
 

+ 0 - 4
mapeditor/templateeditor/templateeditor.cpp

@@ -129,10 +129,6 @@ void TemplateEditor::loadContent(bool autoPosition)
 	auto & zones = templates[selectedTemplate]->getZones();
 	if(autoPosition || std::all_of(zones.begin(), zones.end(), [](auto & item){ return item.second->getVisiblePosition().x == 0 && item.second->getVisiblePosition().y == 0; }))
 		autoPositionZones();
-	
-	for(auto & zone : zones)
-		if(zone.second->getVisibleSize() < 0.01)
-			zone.second->setVisibleSize(1.0);
 
 	for(auto & zone : zones)
 	{