mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-15 20:03:15 +02:00
Timed events implemented
This commit is contained in:
@@ -9,10 +9,60 @@
|
||||
*/
|
||||
#include "StdInc.h"
|
||||
#include "eventsettings.h"
|
||||
#include "timedevent.h"
|
||||
#include "ui_eventsettings.h"
|
||||
#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);
|
||||
result["message"] = QString::fromStdString(event.message);
|
||||
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);
|
||||
}
|
||||
|
||||
CMapEvent eventFromVariant(const QVariant & variant)
|
||||
{
|
||||
CMapEvent result;
|
||||
auto v = variant.toMap();
|
||||
result.name = v.value("name").toString().toStdString();
|
||||
result.message = v.value("message").toString().toStdString();
|
||||
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;
|
||||
}
|
||||
|
||||
EventSettings::EventSettings(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
AbstractSettings(parent),
|
||||
ui(new Ui::EventSettings)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
@@ -23,20 +73,45 @@ EventSettings::~EventSettings()
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void EventSettings::initialize(const CMap & map)
|
||||
{
|
||||
for(const auto & event : map.events)
|
||||
{
|
||||
auto * item = new QListWidgetItem(QString::fromStdString(event.name));
|
||||
item->setData(Qt::UserRole, toVariant(event));
|
||||
ui->eventsList->addItem(item);
|
||||
}
|
||||
}
|
||||
|
||||
void EventSettings::update(CMap & map)
|
||||
{
|
||||
map.events.clear();
|
||||
for(int i = 0; i < ui->eventsList->count(); ++i)
|
||||
{
|
||||
const auto * item = ui->eventsList->item(i);
|
||||
map.events.push_back(eventFromVariant(item->data(Qt::UserRole)));
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void EventSettings::on_timedEventRemove_clicked()
|
||||
{
|
||||
|
||||
if(auto * item = ui->eventsList->currentItem())
|
||||
ui->eventsList->removeItemWidget(item);
|
||||
}
|
||||
|
||||
|
||||
void EventSettings::on_eventsList_itemActivated(QListWidgetItem *item)
|
||||
{
|
||||
|
||||
new TimedEvent(item, parentWidget());
|
||||
}
|
||||
|
||||
|
@@ -1,3 +1,12 @@
|
||||
/*
|
||||
* loseconditions.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 "loseconditions.h"
|
||||
#include "ui_loseconditions.h"
|
||||
|
@@ -1,14 +1,3 @@
|
||||
/*
|
||||
* loseconditions.h, 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
|
||||
*
|
||||
*/
|
||||
#ifndef LOSECONDITIONS_H
|
||||
#define LOSECONDITIONS_H
|
||||
/*
|
||||
* loseconditions.h, part of VCMI engine
|
||||
*
|
||||
@@ -19,6 +8,8 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LOSECONDITIONS_H
|
||||
#define LOSECONDITIONS_H
|
||||
#include "abstractsettings.h"
|
||||
|
||||
namespace Ui {
|
||||
|
@@ -67,22 +67,7 @@ MapSettings::MapSettings(MapController & ctrl, QWidget *parent) :
|
||||
ui->mods->initialize(*controller.map());
|
||||
ui->victory->initialize(*controller.map());
|
||||
ui->lose->initialize(*controller.map());
|
||||
|
||||
//timed events
|
||||
for(auto & ev : controller.map()->events)
|
||||
{
|
||||
QVariantMap descriptor;
|
||||
descriptor["message"] = QVariant::fromValue(QString::fromStdString(ev.message));
|
||||
descriptor["players"] = QVariant::fromValue(ev.players);
|
||||
descriptor["humanAffected"] = QVariant::fromValue(ev.humanAffected);
|
||||
descriptor["computerAffected"] = QVariant::fromValue(ev.computerAffected);
|
||||
descriptor["firstOccurence"] = QVariant::fromValue(ev.firstOccurence);
|
||||
descriptor["nextOccurence"] = QVariant::fromValue(ev.nextOccurence);
|
||||
|
||||
auto * item = new QListWidgetItem(QString::fromStdString(ev.name));
|
||||
item->setData(Qt::UserRole, descriptor);
|
||||
ui->eventsList->addItem(item);
|
||||
}
|
||||
ui->events->initialize(*controller.map());
|
||||
}
|
||||
|
||||
MapSettings::~MapSettings()
|
||||
@@ -112,6 +97,7 @@ void MapSettings::on_pushButton_clicked()
|
||||
ui->mods->update(*controller.map());
|
||||
ui->victory->update(*controller.map());
|
||||
ui->lose->update(*controller.map());
|
||||
ui->events->update(*controller.map());
|
||||
|
||||
controller.commitChangeWithoutRedraw();
|
||||
|
||||
|
@@ -147,32 +147,7 @@
|
||||
<number>12</number>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_7">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_7">
|
||||
<property name="text">
|
||||
<string>Timed events</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="timedEventAdd">
|
||||
<property name="text">
|
||||
<string>Add</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="timedEventRemove">
|
||||
<property name="text">
|
||||
<string>Remove</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QListWidget" name="eventsList"/>
|
||||
<widget class="EventSettings" name="events" native="true"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
@@ -356,6 +331,12 @@
|
||||
<header>mapsettings/loseconditions.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
<customwidget>
|
||||
<class>EventSettings</class>
|
||||
<extends>QWidget</extends>
|
||||
<header>mapsettings/eventsettings.h</header>
|
||||
<container>1</container>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
@@ -1,3 +1,12 @@
|
||||
/*
|
||||
* modsettings.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 "modsettings.h"
|
||||
#include "ui_modsettings.h"
|
||||
|
@@ -1,3 +1,12 @@
|
||||
/*
|
||||
* modsettings.h, 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
|
||||
*
|
||||
*/
|
||||
#ifndef MODSETTINGS_H
|
||||
#define MODSETTINGS_H
|
||||
|
||||
|
@@ -10,12 +10,35 @@
|
||||
#include "StdInc.h"
|
||||
#include "timedevent.h"
|
||||
#include "ui_timedevent.h"
|
||||
#include "../../lib/constants/EntityIdentifiers.h"
|
||||
#include "../../lib/constants/StringConstants.h"
|
||||
|
||||
TimedEvent::TimedEvent(QWidget *parent) :
|
||||
TimedEvent::TimedEvent(QListWidgetItem * t, QWidget *parent) :
|
||||
QDialog(parent),
|
||||
target(t),
|
||||
ui(new Ui::TimedEvent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
const auto params = t->data(Qt::UserRole).toMap();
|
||||
ui->eventNameText->setText(params.value("name").toString());
|
||||
ui->eventMessageText->setPlainText(params.value("message").toString());
|
||||
ui->eventAffectsCpu->setChecked(params.value("computerAffected").toBool());
|
||||
ui->eventAffectsHuman->setChecked(params.value("humanAffected").toBool());
|
||||
ui->eventFirstOccurance->setValue(params.value("firstOccurence").toInt());
|
||||
ui->eventRepeatAfter->setValue(params.value("nextOccurence").toInt());
|
||||
|
||||
for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
|
||||
{
|
||||
bool isAffected = (1 << i) & params.value("players").toInt();
|
||||
auto * item = new QListWidgetItem(QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[i]));
|
||||
item->setData(Qt::UserRole, QVariant::fromValue(i));
|
||||
item->setCheckState(isAffected ? Qt::Checked : Qt::Unchecked);
|
||||
ui->playersAffected->addItem(item);
|
||||
}
|
||||
//result.resources = resourcesFromVariant(v.value("resources"));
|
||||
|
||||
show();
|
||||
}
|
||||
|
||||
TimedEvent::~TimedEvent()
|
||||
@@ -29,8 +52,26 @@ void TimedEvent::on_eventResources_clicked()
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TimedEvent::on_TimedEvent_finished(int result)
|
||||
{
|
||||
QVariantMap descriptor;
|
||||
descriptor["name"] = ui->eventNameText->text();
|
||||
descriptor["message"] = ui->eventMessageText->toPlainText();
|
||||
descriptor["humanAffected"] = QVariant::fromValue(ui->eventAffectsHuman->isChecked());
|
||||
descriptor["computerAffected"] = QVariant::fromValue(ui->eventAffectsCpu->isChecked());
|
||||
descriptor["firstOccurence"] = QVariant::fromValue(ui->eventFirstOccurance->value());
|
||||
descriptor["nextOccurence"] = QVariant::fromValue(ui->eventRepeatAfter->value());
|
||||
|
||||
int players = 0;
|
||||
for(int i = 0; i < ui->playersAffected->count(); ++i)
|
||||
{
|
||||
auto * item = ui->playersAffected->item(i);
|
||||
if(item->checkState() == Qt::Checked)
|
||||
players |= 1 << i;
|
||||
}
|
||||
descriptor["players"] = QVariant::fromValue(players);
|
||||
target->setData(Qt::UserRole, descriptor);
|
||||
target->setText(ui->eventNameText->text());
|
||||
}
|
||||
|
||||
|
@@ -21,7 +21,7 @@ class TimedEvent : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit TimedEvent(QWidget *parent = nullptr);
|
||||
explicit TimedEvent(QListWidgetItem *, QWidget *parent = nullptr);
|
||||
~TimedEvent();
|
||||
|
||||
private slots:
|
||||
@@ -31,6 +31,7 @@ private slots:
|
||||
|
||||
private:
|
||||
Ui::TimedEvent *ui;
|
||||
QListWidgetItem * target;
|
||||
};
|
||||
|
||||
#endif // TIMEDEVENT_H
|
||||
|
@@ -23,7 +23,7 @@
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_12">
|
||||
<item>
|
||||
<widget class="QLineEdit" name="lineEdit">
|
||||
<widget class="QLineEdit" name="eventNameText">
|
||||
<property name="placeholderText">
|
||||
<string>Event name</string>
|
||||
</property>
|
||||
@@ -103,7 +103,7 @@
|
||||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QListWidget" name="listWidget">
|
||||
<widget class="QListWidget" name="playersAffected">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
|
@@ -1,3 +1,12 @@
|
||||
/*
|
||||
* victoryconditions.h, 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 "victoryconditions.h"
|
||||
#include "ui_victoryconditions.h"
|
||||
|
Reference in New Issue
Block a user