2
0
Эх сурвалжийг харах

Add hero placeholder properties

nordsoft 2 жил өмнө
parent
commit
f6d7755c6a

+ 19 - 0
lib/mapObjects/CGHeroInstance.cpp

@@ -40,6 +40,25 @@
 
 VCMI_LIB_NAMESPACE_BEGIN
 
+void CGHeroPlaceholder::serializeJsonOptions(JsonSerializeFormat & handler)
+{
+	bool isHeroType = heroType.has_value();
+	handler.serializeBool("placeholderType", isHeroType, false);
+	
+	if(!handler.saving)
+	{
+		if(isHeroType)
+			heroType = HeroTypeID::NONE;
+		else
+			powerRank = 0;
+	}
+	
+	if(isHeroType)
+		handler.serializeId("heroType", heroType.value(), HeroTypeID::NONE);
+	else
+		handler.serializeInt("powerRank", powerRank.value());
+}
+
 static int lowestSpeed(const CGHeroInstance * chi)
 {
 	static const CSelector selectorSTACKS_SPEED = Selector::type()(BonusType::STACKS_SPEED);

+ 4 - 1
lib/mapObjects/CGHeroInstance.h

@@ -25,7 +25,7 @@ struct TerrainTile;
 struct TurnInfo;
 enum class EHeroGender : uint8_t;
 
-class CGHeroPlaceholder : public CGObjectInstance
+class DLL_LINKAGE CGHeroPlaceholder : public CGObjectInstance
 {
 public:
 	/// if this is placeholder by power, then power rank of desired hero
@@ -40,6 +40,9 @@ public:
 		h & powerRank;
 		h & heroType;
 	}
+	
+protected:
+	void serializeJsonOptions(JsonSerializeFormat & handler) override;
 };
 
 

+ 77 - 1
mapeditor/inspector/inspector.cpp

@@ -53,6 +53,7 @@ Initializer::Initializer(CGObjectInstance * o, const PlayerColor & pl) : default
 	INIT_OBJ_TYPE(CGDwelling);
 	INIT_OBJ_TYPE(CGTownInstance);
 	INIT_OBJ_TYPE(CGCreature);
+	INIT_OBJ_TYPE(CGHeroPlaceholder);
 	INIT_OBJ_TYPE(CGHeroInstance);
 	INIT_OBJ_TYPE(CGSignBottle);
 	INIT_OBJ_TYPE(CGLighthouse);
@@ -118,6 +119,17 @@ void Initializer::initialize(CGLighthouse * o)
 	o->tempOwner = defaultPlayer;
 }
 
+void Initializer::initialize(CGHeroPlaceholder * o)
+{
+	if(!o) return;
+	
+	if(!o->powerRank.has_value() && !o->heroType.has_value())
+		o->powerRank = 0;
+	
+	if(o->powerRank.has_value() && o->heroType.has_value())
+		o->powerRank.reset();
+}
+
 void Initializer::initialize(CGHeroInstance * o)
 {
 	if(!o)
@@ -258,6 +270,34 @@ void Inspector::updateProperties(CGShipyard * o)
 	addProperty("Owner", o->tempOwner, false);
 }
 
+void Inspector::updateProperties(CGHeroPlaceholder * o)
+{
+	if(!o) return;
+	
+	bool type = false;
+	if(o->heroType.has_value())
+		type = true;
+	else if(!o->powerRank.has_value())
+		assert(0); //one of values must be initialized
+	
+	{
+		auto * delegate = new InspectorDelegate;
+		delegate->options = {{"POWER RANK", QVariant::fromValue(false)}, {"HERO TYPE", QVariant::fromValue(true)}};
+		addProperty("Placeholder type", delegate->options[type].first, delegate, false);
+	}
+	
+	addProperty("Power rank", o->powerRank.has_value() ? o->powerRank.value() : 0, type);
+	
+	{
+		auto * delegate = new InspectorDelegate;
+		for(int i = 0; i < VLC->heroh->objects.size(); ++i)
+		{
+			delegate->options.push_back({QObject::tr(VLC->heroh->objects[i]->getNameTranslated().c_str()), QVariant::fromValue(VLC->heroh->objects[i]->getId().getNum())});
+		}
+		addProperty("Hero type", o->heroType.has_value() ? VLC->heroh->getById(o->heroType.value())->getNameTranslated() : "", delegate, !type);
+	}
+}
+
 void Inspector::updateProperties(CGHeroInstance * o)
 {
 	if(!o) return;
@@ -454,6 +494,7 @@ void Inspector::updateProperties()
 	UPDATE_OBJ_PROPERTIES(CGDwelling);
 	UPDATE_OBJ_PROPERTIES(CGTownInstance);
 	UPDATE_OBJ_PROPERTIES(CGCreature);
+	UPDATE_OBJ_PROPERTIES(CGHeroPlaceholder);
 	UPDATE_OBJ_PROPERTIES(CGHeroInstance);
 	UPDATE_OBJ_PROPERTIES(CGSignBottle);
 	UPDATE_OBJ_PROPERTIES(CGLighthouse);
@@ -500,6 +541,7 @@ void Inspector::setProperty(const QString & key, const QVariant & value)
 	SET_PROPERTIES(CGDwelling);
 	SET_PROPERTIES(CGGarrison);
 	SET_PROPERTIES(CGCreature);
+	SET_PROPERTIES(CGHeroPlaceholder);
 	SET_PROPERTIES(CGHeroInstance);
 	SET_PROPERTIES(CGShipyard);
 	SET_PROPERTIES(CGSignBottle);
@@ -612,6 +654,37 @@ void Inspector::setProperty(CGGarrison * o, const QString & key, const QVariant
 		o->removableUnits = value.toBool();
 }
 
+void Inspector::setProperty(CGHeroPlaceholder * o, const QString & key, const QVariant & value)
+{
+	if(!o) return;
+
+	if(key == "Placeholder type")
+	{
+		if(value.toBool())
+		{
+			if(!o->heroType.has_value())
+				o->heroType = HeroTypeID(0);
+			o->powerRank.reset();
+		}
+		else
+		{
+			if(!o->powerRank.has_value())
+				o->powerRank = 0;
+			o->heroType.reset();
+		}
+		
+		updateProperties();
+	}
+	
+	if(key == "Power rank")
+		o->powerRank = value.toInt();
+	
+	if(key == "Hero type")
+	{
+		o->heroType.value() = HeroTypeID(value.toInt());
+	}
+}
+
 void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVariant & value)
 {
 	if(!o) return;
@@ -634,8 +707,11 @@ void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVari
 	{
 		for(auto t : VLC->heroh->objects)
 		{
-			if(t->getNameTranslated() == value.toString().toStdString())
+			if(t->getId() == value.toInt())
+			{
+				o->subID = value.toInt();
 				o->type = t.get();
+			}
 		}
 		o->gender = o->type->gender;
 		o->randomizeArmy(o->type->heroClass->faction);

+ 2 - 0
mapeditor/inspector/inspector.h

@@ -45,6 +45,7 @@ public:
 	DECLARE_OBJ_TYPE(CGResource);
 	DECLARE_OBJ_TYPE(CGDwelling);
 	DECLARE_OBJ_TYPE(CGGarrison);
+	DECLARE_OBJ_TYPE(CGHeroPlaceholder);
 	DECLARE_OBJ_TYPE(CGHeroInstance);
 	DECLARE_OBJ_TYPE(CGCreature);
 	DECLARE_OBJ_TYPE(CGSignBottle);
@@ -74,6 +75,7 @@ protected:
 	DECLARE_OBJ_PROPERTY_METHODS(CGResource);
 	DECLARE_OBJ_PROPERTY_METHODS(CGDwelling);
 	DECLARE_OBJ_PROPERTY_METHODS(CGGarrison);
+	DECLARE_OBJ_PROPERTY_METHODS(CGHeroPlaceholder);
 	DECLARE_OBJ_PROPERTY_METHODS(CGHeroInstance);
 	DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
 	DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);

+ 1 - 0
mapeditor/inspector/portraitwidget.cpp

@@ -22,6 +22,7 @@ PortraitWidget::PortraitWidget(CGHeroInstance & h, QWidget *parent):
 	ui->setupUi(this);
 	ui->portraitView->setScene(&scene);
 	ui->portraitView->fitInView(scene.itemsBoundingRect(), Qt::KeepAspectRatio);
+	show();
 }
 
 PortraitWidget::~PortraitWidget()

+ 2 - 2
mapeditor/mainwindow.cpp

@@ -222,8 +222,6 @@ MainWindow::MainWindow(QWidget* parent) :
 	scenePreview = new QGraphicsScene(this);
 	ui->objectPreview->setScene(scenePreview);
 
-	initialScale = ui->mapView->viewport()->geometry();
-
 	//loading objects
 	loadObjectsTree();
 	
@@ -298,6 +296,8 @@ void MainWindow::initializeMap(bool isNew)
 	ui->mapView->setScene(controller.scene(mapLevel));
 	ui->minimapView->setScene(controller.miniScene(mapLevel));
 	ui->minimapView->dimensions();
+	if(initialScale.isValid())
+		on_actionZoom_reset_triggered();
 	initialScale = ui->mapView->mapToScene(ui->mapView->viewport()->geometry()).boundingRect();
 	
 	//enable settings