1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Merge pull request #4686 from godric3/map_editor_timed_events_objects_removal

Add map editor ui to set objects to remove on timed event
This commit is contained in:
Dydzio 2024-11-25 12:51:25 +01:00 committed by GitHub
commit f64c6df05c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 126 additions and 59 deletions

View File

@ -210,7 +210,6 @@ void CGameState::init(const IMapService * mapService, StartInfo * si, Load::Prog
buildBonusSystemTree();
initVisitingAndGarrisonedHeroes();
initFogOfWar();
initTimedEventsRemovableObjects();
for(auto & elem : teams)
{
@ -952,23 +951,6 @@ void CGameState::initMapObjects()
map->calculateGuardingGreaturePositions(); //calculate once again when all the guards are placed and initialized
}
void CGameState::initTimedEventsRemovableObjects()
{
for(auto & timedEvent : map->events)
{
for(int3 coordinate : timedEvent.deletedObjectsCoordinates)
{
if(isInTheMap(coordinate))
{
for(const CGObjectInstance * object : getBlockingObjs(coordinate))
{
timedEvent.deletedObjectsInstances.push_back(object->id);
}
}
}
}
}
void CGameState::placeHeroesInTowns()
{
for(auto & player : players)

View File

@ -196,7 +196,6 @@ private:
void initTowns();
void initTownNames();
void initMapObjects();
void initTimedEventsRemovableObjects();
void initVisitingAndGarrisonedHeroes();
void initCampaign();

View File

@ -376,7 +376,7 @@ void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
std::string CGTownInstance::getObjectName() const
{
return getNameTranslated() + ", " + town->faction->getNameTranslated();
return getNameTranslated() + ", " + (ID == Obj::RANDOM_TOWN ? "Random town" : getFaction().toEntity(VLC)->getNameTranslated());
}
bool CGTownInstance::townEnvisagesBuilding(BuildingSubID::EBuildingSubID subId) const

View File

@ -104,24 +104,8 @@ void CMapEvent::serializeJson(JsonSerializeFormat & handler)
handler.serializeInt("nextOccurrence", nextOccurrence);
resources.serializeJson(handler, "resources");
JsonNode deletedObjectsJson;
for (const auto & entry : deletedObjectsCoordinates)
{
JsonNode values;
JsonNode valueX;
JsonNode valueY;
JsonNode valueZ;
valueX.Float() = static_cast<int>(entry.x);
valueY.Float() = static_cast<int>(entry.y);
valueZ.Float() = static_cast<int>(entry.z);
values.Vector().push_back(valueX);
values.Vector().push_back(valueY);
values.Vector().push_back(valueZ);
deletedObjectsJson.Vector().push_back(values);
}
handler.serializeRaw("deletedObjectsCoordinates", deletedObjectsJson, std::nullopt);
auto deletedObjects = handler.enterArray("deletedObjectsInstances");
deletedObjects.serializeArray(deletedObjectsInstances);
}
void CCastleEvent::serializeJson(JsonSerializeFormat & handler)

View File

@ -16,5 +16,6 @@ enum MapEditorRoles
TownEventRole = Qt::UserRole + 1,
PlayerIDRole,
BuildingIDRole,
SpellIDRole
SpellIDRole,
ObjectInstanceIDRole
};

View File

@ -55,23 +55,23 @@ TResources resourcesFromVariant(const QVariant & v)
return TResources(vJson);
}
QVariant toVariant(std::vector<int3> positions)
QVariant toVariant(std::vector<ObjectInstanceID> objects)
{
QVariantList result;
for(int3 position : positions)
for(auto obj : objects)
{
result.push_back(QVariant::fromValue<int3>(position));
result.push_back(QVariant::fromValue(obj.num));
}
return result;
}
std::vector<int3> deletedObjectsPositionsFromVariant(const QVariant & v)
std::vector<ObjectInstanceID> deletedObjectsIdsFromVariant(const QVariant & v)
{
std::vector<int3> result;
for (auto positionAsVariant : v.toList())
std::vector<ObjectInstanceID> result;
for(auto idAsVariant : v.toList())
{
int3 position = positionAsVariant.value<int3>();
result.push_back(position);
auto id = idAsVariant.value<int>();
result.push_back(ObjectInstanceID(id));
}
return result;
@ -88,7 +88,7 @@ QVariant toVariant(const CMapEvent & event)
result["firstOccurrence"] = QVariant::fromValue(event.firstOccurrence);
result["nextOccurrence"] = QVariant::fromValue(event.nextOccurrence);
result["resources"] = toVariant(event.resources);
result["deletedObjectsPositions"] = toVariant(event.deletedObjectsCoordinates);
result["deletedObjectsInstances"] = toVariant(event.deletedObjectsInstances);
return QVariant(result);
}
@ -104,7 +104,7 @@ CMapEvent eventFromVariant(CMapHeader & mapHeader, const QVariant & variant)
result.firstOccurrence = v.value("firstOccurrence").toInt();
result.nextOccurrence = v.value("nextOccurrence").toInt();
result.resources = resourcesFromVariant(v.value("resources"));
result.deletedObjectsCoordinates = deletedObjectsPositionsFromVariant(v.value("deletedObjectsPositions"));
result.deletedObjectsInstances = deletedObjectsIdsFromVariant(v.value("deletedObjectsInstances"));
return result;
}
@ -161,6 +161,6 @@ void EventSettings::on_timedEventRemove_clicked()
void EventSettings::on_eventsList_itemActivated(QListWidgetItem *item)
{
new TimedEvent(item, parentWidget());
new TimedEvent(*controller, item, parentWidget());
}

View File

@ -11,13 +11,15 @@
#include "timedevent.h"
#include "ui_timedevent.h"
#include "eventsettings.h"
#include "../mapeditorroles.h"
#include "../../lib/constants/EntityIdentifiers.h"
#include "../../lib/constants/StringConstants.h"
TimedEvent::TimedEvent(QListWidgetItem * t, QWidget *parent) :
TimedEvent::TimedEvent(MapController & c, QListWidgetItem * t, QWidget *parent) :
controller(c),
QDialog(parent),
target(t),
ui(new Ui::TimedEvent)
ui(new Ui::TimedEvent),
target(t)
{
ui->setupUi(this);
@ -51,7 +53,14 @@ TimedEvent::TimedEvent(QListWidgetItem * t, QWidget *parent) :
nval->setFlags(nval->flags() | Qt::ItemIsEditable);
ui->resources->setItem(i, 1, nval);
}
auto deletedObjectInstances = params.value("deletedObjectsInstances").toList();
for(auto const & idAsVariant : deletedObjectInstances)
{
auto id = ObjectInstanceID(idAsVariant.toInt());
auto obj = controller.map()->objects[id];
if(obj)
insertObjectToDelete(obj);
}
show();
}
@ -89,10 +98,63 @@ void TimedEvent::on_TimedEvent_finished(int result)
}
descriptor["resources"] = res;
QVariantList deletedObjects;
for(int i = 0; i < ui->deletedObjects->count(); ++i)
{
auto const & item = ui->deletedObjects->item(i);
auto data = item->data(MapEditorRoles::ObjectInstanceIDRole);
auto id = ObjectInstanceID(data.value<int>());
deletedObjects.push_back(QVariant::fromValue(id.num));
}
descriptor["deletedObjectsInstances"] = QVariant::fromValue(deletedObjects);
target->setData(Qt::UserRole, descriptor);
target->setText(ui->eventNameText->text());
}
void TimedEvent::on_addObjectToDelete_clicked()
{
for(int lvl : {0, 1})
{
auto & l = controller.scene(lvl)->objectPickerView;
l.highlight<const CGObjectInstance>();
l.update();
QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &TimedEvent::onObjectPicked);
}
hide();
dynamic_cast<QWidget *>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->hide();
}
void TimedEvent::on_removeObjectToDelete_clicked()
{
delete ui->deletedObjects->takeItem(ui->deletedObjects->currentRow());
}
void TimedEvent::onObjectPicked(const CGObjectInstance * obj)
{
show();
dynamic_cast<QWidget *>(parent()->parent()->parent()->parent()->parent()->parent()->parent())->show();
for(int lvl : {0, 1})
{
auto & l = controller.scene(lvl)->objectPickerView;
l.clear();
l.update();
QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &TimedEvent::onObjectPicked);
}
if(!obj)
return;
insertObjectToDelete(obj);
}
void TimedEvent::insertObjectToDelete(const CGObjectInstance * obj)
{
QString objectLabel = QString("%1, x: %2, y: %3, z: %4").arg(QString::fromStdString(obj->getObjectName())).arg(obj->pos.x).arg(obj->pos.y).arg(obj->pos.z);
auto * item = new QListWidgetItem(objectLabel);
item->setData(MapEditorRoles::ObjectInstanceIDRole, QVariant::fromValue(obj->id.num));
ui->deletedObjects->addItem(item);
}
void TimedEvent::on_pushButton_clicked()
{

View File

@ -11,6 +11,8 @@
#include <QDialog>
#include "mapcontroller.h"
namespace Ui {
class TimedEvent;
}
@ -20,18 +22,27 @@ class TimedEvent : public QDialog
Q_OBJECT
public:
explicit TimedEvent(QListWidgetItem *, QWidget *parent = nullptr);
explicit TimedEvent(MapController & map, QListWidgetItem *, QWidget * parent = nullptr);
~TimedEvent();
private slots:
void on_TimedEvent_finished(int result);
void on_addObjectToDelete_clicked();
void on_removeObjectToDelete_clicked();
void onObjectPicked(const CGObjectInstance * obj);
void insertObjectToDelete(const CGObjectInstance * obj);
void on_pushButton_clicked();
void on_resources_itemDoubleClicked(QTableWidgetItem * item);
private:
MapController & controller;
Ui::TimedEvent * ui;
QListWidgetItem * target;
};

View File

@ -9,8 +9,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>620</width>
<height>371</height>
<width>730</width>
<height>422</height>
</rect>
</property>
<property name="windowTitle">
@ -197,6 +197,34 @@
</column>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="deletedObjectsLabel">
<property name="text">
<string>Objects to delete</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addObjectToDelete">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeObjectToDelete">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="deletedObjects"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">