1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/mapeditor/mapsettings/eventsettings.cpp

121 lines
3.6 KiB
C++
Raw Normal View History

2023-09-07 23:57:56 +02:00
/*
* eventsettings.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "eventsettings.h"
2023-09-08 01:43:01 +02:00
#include "timedevent.h"
2023-09-07 23:57:56 +02:00
#include "ui_eventsettings.h"
#include "../mapcontroller.h"
2023-09-08 01:43:01 +02:00
#include "../../lib/mapping/CMapDefines.h"
#include "../../lib/constants/NumericConstants.h"
#include "../../lib/constants/StringConstants.h"
QVariant toVariant(const TResources & resources)
{
QVariantMap result;
for(int i = 0; i < GameConstants::RESOURCE_QUANTITY; ++i)
result[QString::fromStdString(GameConstants::RESOURCE_NAMES[i])] = QVariant::fromValue(resources[i]);
return result;
}
TResources resourcesFromVariant(const QVariant & v)
{
JsonNode vJson;
for(auto r : v.toMap().keys())
vJson[r.toStdString()].Integer() = v.toMap().value(r).toInt();
return TResources(vJson);
}
QVariant toVariant(const CMapEvent & event)
{
QVariantMap result;
result["name"] = QString::fromStdString(event.name);
2023-09-28 13:28:37 +02:00
result["message"] = QString::fromStdString(event.message.toString());
2023-09-08 01:43:01 +02:00
result["players"] = QVariant::fromValue(event.players);
result["humanAffected"] = QVariant::fromValue(event.humanAffected);
result["computerAffected"] = QVariant::fromValue(event.computerAffected);
result["firstOccurence"] = QVariant::fromValue(event.firstOccurence);
result["nextOccurence"] = QVariant::fromValue(event.nextOccurence);
result["resources"] = toVariant(event.resources);
return QVariant(result);
}
2023-09-28 14:49:47 +02:00
CMapEvent eventFromVariant(CMapHeader & mapHeader, const QVariant & variant)
2023-09-08 01:43:01 +02:00
{
CMapEvent result;
auto v = variant.toMap();
result.name = v.value("name").toString().toStdString();
2023-09-29 00:24:45 +02:00
result.message.appendTextID(mapRegisterLocalizedString("map", mapHeader, TextIdentifier("header", "event", result.name, "message"), v.value("message").toString().toStdString()));
2023-09-08 01:43:01 +02:00
result.players = v.value("players").toInt();
result.humanAffected = v.value("humanAffected").toInt();
result.computerAffected = v.value("computerAffected").toInt();
result.firstOccurence = v.value("firstOccurence").toInt();
result.nextOccurence = v.value("nextOccurence").toInt();
result.resources = resourcesFromVariant(v.value("resources"));
return result;
}
2023-09-07 23:57:56 +02:00
EventSettings::EventSettings(QWidget *parent) :
2023-09-08 01:43:01 +02:00
AbstractSettings(parent),
2023-09-07 23:57:56 +02:00
ui(new Ui::EventSettings)
{
ui->setupUi(this);
}
EventSettings::~EventSettings()
{
delete ui;
}
void EventSettings::initialize(MapController & c)
2023-09-07 23:57:56 +02:00
{
AbstractSettings::initialize(c);
for(const auto & event : controller->map()->events)
2023-09-08 01:43:01 +02:00
{
auto * item = new QListWidgetItem(QString::fromStdString(event.name));
item->setData(Qt::UserRole, toVariant(event));
ui->eventsList->addItem(item);
}
}
2023-09-07 23:57:56 +02:00
void EventSettings::update()
2023-09-08 01:43:01 +02:00
{
controller->map()->events.clear();
2023-09-08 01:43:01 +02:00
for(int i = 0; i < ui->eventsList->count(); ++i)
{
const auto * item = ui->eventsList->item(i);
2023-09-28 14:49:47 +02:00
controller->map()->events.push_back(eventFromVariant(*controller->map(), item->data(Qt::UserRole)));
2023-09-08 01:43:01 +02:00
}
}
void EventSettings::on_timedEventAdd_clicked()
{
CMapEvent event;
event.name = tr("New event").toStdString();
auto * item = new QListWidgetItem(QString::fromStdString(event.name));
item->setData(Qt::UserRole, toVariant(event));
ui->eventsList->addItem(item);
2023-09-08 03:21:31 +02:00
on_eventsList_itemActivated(item);
2023-09-07 23:57:56 +02:00
}
void EventSettings::on_timedEventRemove_clicked()
{
2023-09-08 01:43:01 +02:00
if(auto * item = ui->eventsList->currentItem())
2023-09-08 17:54:34 +02:00
ui->eventsList->takeItem(ui->eventsList->row(item));
2023-09-07 23:57:56 +02:00
}
void EventSettings::on_eventsList_itemActivated(QListWidgetItem *item)
{
2023-09-08 01:43:01 +02:00
new TimedEvent(item, parentWidget());
2023-09-07 23:57:56 +02:00
}