Ver Fonte

add validation

Laserlicht há 5 dias atrás
pai
commit
206f9c432e

+ 42 - 1
mapeditor/campaigneditor/campaigneditor.cpp

@@ -142,10 +142,51 @@ void CampaignEditor::changed()
 	setTitle();
 }
 
-void CampaignEditor::saveCampaign()
+bool hasNullGap(const std::map<CampaignScenarioID, CampaignScenario>& scenarios)
+{
+	bool seenNonNull = false;
+	bool foundNullAfterNonNull = false;
+
+	for(const auto & r : scenarios)
+	{
+		if(r.second.mapName.empty())
+		{
+			if (seenNonNull)
+				foundNullAfterNonNull = true; // gap detected
+		}
+		else
+		{
+			if(foundNullAfterNonNull)
+				return true; // non-empty after a gap
+			seenNonNull = true;
+		}
+	}
+
+	// Leading empty elements
+	if (!scenarios.empty() && scenarios.begin()->second.mapName.empty())
+		return true;
+
+	return false;
+}
+
+bool CampaignEditor::validate()
 {
 	if(campaignState->mapPieces.size() != campaignState->campaignRegions.regions.size())
 		logGlobal->trace("Not all regions have a map");
+	
+	if(hasNullGap(campaignState->scenarios))
+	{
+		QMessageBox::critical(this, tr("Fewer Scenarios than regions"), tr("You have fewer scenarios than regions. This is only allowed if the missing scenarios occur in the last regions, not in the middle or beginning."), QMessageBox::Ok);
+		return false;
+	}
+
+	return true;
+}
+
+void CampaignEditor::saveCampaign()
+{
+	if(!validate())
+		return;
 
 	Helper::saveCampaign(campaignState, filename);
 	unsaved = false;

+ 1 - 0
mapeditor/campaigneditor/campaigneditor.h

@@ -45,6 +45,7 @@ private:
 	bool getAnswerAboutUnsavedChanges();
 	void setTitle();
 	void changed();
+	bool validate();
 	void saveCampaign();
 
 	void closeEvent(QCloseEvent *event) override;