1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-02 00:10:22 +02:00

Pandora rewards delegate

This commit is contained in:
nordsoft 2022-09-12 14:28:38 +04:00
parent 330b107a23
commit 38fd6ec610
6 changed files with 454 additions and 5 deletions

View File

@ -9,6 +9,7 @@
#include "townbulidingswidget.h"
#include "armywidget.h"
#include "messagewidget.h"
#include "rewardswidget.h"
//===============IMPLEMENT OBJECT INITIALIZATION FUNCTIONS================
Initializer::Initializer(CMap * m, CGObjectInstance * o, const PlayerColor & pl) : map(m), defaultPlayer(pl)
@ -26,6 +27,7 @@ Initializer::Initializer(CMap * m, CGObjectInstance * o, const PlayerColor & pl)
INIT_OBJ_TYPE(CGHeroInstance);
INIT_OBJ_TYPE(CGSignBottle);
INIT_OBJ_TYPE(CGLighthouse);
//INIT_OBJ_TYPE(CGPandoraBox);
}
bool stringToBool(const QString & s)
@ -273,6 +275,14 @@ void Inspector::updateProperties(CGCreature * o)
//addProperty("Resources reward", o->resources); //TODO: implement in setProperty
}
void Inspector::updateProperties(CGPandoraBox * o)
{
if(!o) return;
auto * delegate = new RewardsPandoraDelegate(*o);
addProperty("Reward", PropertyEditorPlaceholder(), delegate, false);
}
void Inspector::updateProperties()
{
@ -308,6 +318,7 @@ void Inspector::updateProperties()
UPDATE_OBJ_PROPERTIES(CGHeroInstance);
UPDATE_OBJ_PROPERTIES(CGSignBottle);
UPDATE_OBJ_PROPERTIES(CGLighthouse);
UPDATE_OBJ_PROPERTIES(CGPandoraBox);
table->show();
}
@ -340,6 +351,7 @@ void Inspector::setProperty(const QString & key, const QVariant & value)
SET_PROPERTIES(CGShipyard);
SET_PROPERTIES(CGSignBottle);
SET_PROPERTIES(CGLighthouse);
SET_PROPERTIES(CGPandoraBox);
}
void Inspector::setProperty(CArmedInstance * object, const QString & key, const QVariant & value)
@ -352,6 +364,11 @@ void Inspector::setProperty(CGLighthouse * object, const QString & key, const QV
if(!object) return;
}
void Inspector::setProperty(CGPandoraBox * object, const QString & key, const QVariant & value)
{
if(!object) return;
}
void Inspector::setProperty(CGTownInstance * object, const QString & key, const QVariant & value)
{
if(!object) return;

View File

@ -35,6 +35,7 @@ public:
DECLARE_OBJ_TYPE(CGCreature);
DECLARE_OBJ_TYPE(CGSignBottle);
DECLARE_OBJ_TYPE(CGLighthouse);
//DECLARE_OBJ_TYPE(CGPandoraBox);
Initializer(CMap *, CGObjectInstance *, const PlayerColor &);
@ -62,6 +63,7 @@ protected:
DECLARE_OBJ_PROPERTY_METHODS(CGCreature);
DECLARE_OBJ_PROPERTY_METHODS(CGSignBottle);
DECLARE_OBJ_PROPERTY_METHODS(CGLighthouse);
DECLARE_OBJ_PROPERTY_METHODS(CGPandoraBox);
//===============DECLARE PROPERTY VALUE TYPE==============================
QTableWidgetItem * addProperty(unsigned int value);

View File

@ -1,14 +1,325 @@
#include "rewardswidget.h"
#include "ui_rewardswidget.h"
#include "../lib/VCMI_Lib.h"
#include "../lib/CSkillHandler.h"
#include "../lib/spells/CSpellHandler.h"
#include "../lib/CArtHandler.h"
#include "../lib/CCreatureHandler.h"
#include "../lib/StringConstants.h"
RewardsWidget::RewardsWidget(QWidget *parent) :
RewardsWidget::RewardsWidget(CGPandoraBox & p, QWidget *parent) :
QDialog(parent),
pandora(&p),
ui(new Ui::RewardsWidget)
{
ui->setupUi(this);
for(auto & type : rewardTypes)
ui->rewardType->addItem(QString::fromStdString(type));
}
RewardsWidget::~RewardsWidget()
{
delete ui;
}
QList<QString> RewardsWidget::getListForType(int typeId)
{
assert(typeId < rewardTypes.size());
QList<QString> result;
switch (typeId) {
case 4: //resources
//to convert string to index WOOD = 0, MERCURY, ORE, SULFUR, CRYSTAL, GEMS, GOLD, MITHRIL,
result.append("Wood");
result.append("Mercury");
result.append("Ore");
result.append("Sulfur");
result.append("Crystals");
result.append("Gems");
result.append("Gold");
break;
case 5:
for(auto s : PrimarySkill::names)
result.append(QString::fromStdString(s));
break;
case 6:
//abilities
for(auto skill : VLC->skillh->objects)
{
result.append(QString::fromStdString(skill->getName()));
}
break;
case 7:
//arts
for(auto art : VLC->arth->objects)
{
result.append(QString::fromStdString(art->getName()));
}
break;
case 8:
//spells
for(auto spell : VLC->spellh->objects)
{
result.append(QString::fromStdString(spell->getName()));
}
break;
case 9:
//creatures
for(auto creature : VLC->creh->objects)
{
result.append(QString::fromStdString(creature->getName()));
}
break;
}
return result;
}
void RewardsWidget::on_rewardType_activated(int index)
{
ui->rewardList->clear();
ui->rewardList->setEnabled(true);
assert(index < rewardTypes.size());
auto l = getListForType(index);
if(l.empty())
ui->rewardList->setEnabled(false);
for(auto & s : l)
ui->rewardList->addItem(s);
}
void RewardsWidget::obtainData()
{
if(pandora)
{
if(pandora->gainedExp > 0)
addReward(0, 0, pandora->gainedExp);
if(pandora->manaDiff)
addReward(1, 0, pandora->manaDiff);
if(pandora->moraleDiff)
addReward(2, 0, pandora->moraleDiff);
if(pandora->luckDiff)
addReward(3, 0, pandora->luckDiff);
if(pandora->resources.nonZero())
{
for(Res::ResourceSet::nziterator resiter(pandora->resources); resiter.valid(); ++resiter)
addReward(4, resiter->resType, resiter->resVal);
}
for(int idx = 0; idx < pandora->primskills.size(); ++idx)
{
if(pandora->primskills[idx])
addReward(5, idx, pandora->primskills[idx]);
}
assert(pandora->abilities.size() == pandora->abilityLevels.size());
for(int idx = 0; idx < pandora->abilities.size(); ++idx)
{
addReward(6, pandora->abilities[idx].getNum(), pandora->abilityLevels[idx]);
}
for(auto art : pandora->artifacts)
{
addReward(7, art.getNum(), 1);
}
for(auto spell : pandora->spells)
{
addReward(8, spell.getNum(), 1);
}
for(int i = 0; i < pandora->creatures.Slots().size(); ++i)
{
if(auto c = pandora->creatures.getCreature(SlotID(i)))
addReward(9, c->getId(), pandora->creatures.getStackCount(SlotID(i)));
}
}
}
bool RewardsWidget::commitChanges()
{
bool haveRewards = false;
if(pandora)
{
pandora->abilities.clear();
pandora->abilityLevels.clear();
pandora->primskills.resize(GameConstants::PRIMARY_SKILLS, 0);
pandora->resources = Res::ResourceSet();
pandora->artifacts.clear();
pandora->spells.clear();
pandora->creatures.clear();
for(int row = 0; row < rewards; ++row)
{
haveRewards = true;
int typeId = ui->rewardsTable->item(row, 0)->data(Qt::UserRole).toInt();
int listId = ui->rewardsTable->item(row, 1) ? ui->rewardsTable->item(row, 1)->data(Qt::UserRole).toInt() : 0;
int amount = ui->rewardsTable->item(row, 2)->data(Qt::UserRole).toInt();
switch(typeId)
{
case 0:
pandora->gainedExp = amount;
break;
case 1:
pandora->manaDiff = amount;
break;
case 2:
pandora->moraleDiff = amount;
break;
case 3:
pandora->luckDiff = amount;
break;
case 4:
pandora->resources.at(listId) = amount;
break;
case 5:
pandora->primskills[listId] = amount;
break;
case 6:
pandora->abilities.push_back(SecondarySkill(listId));
pandora->abilityLevels.push_back(amount);
break;
case 7:
pandora->artifacts.push_back(ArtifactID(listId));
break;
case 8:
pandora->spells.push_back(SpellID(listId));
break;
case 9:
auto slot = pandora->creatures.getFreeSlot();
if(slot != SlotID() && amount > 0)
pandora->creatures.addToSlot(slot, CreatureID(listId), amount);
break;
}
}
}
return haveRewards;
}
void RewardsWidget::on_rewardList_activated(int index)
{
ui->rewardAmount->setText(QString::number(1));
}
void RewardsWidget::addReward(int typeId, int listId, int amount)
{
ui->rewardsTable->setRowCount(++rewards);
auto itemType = new QTableWidgetItem(QString::fromStdString(rewardTypes[typeId]));
itemType->setData(Qt::UserRole, typeId);
ui->rewardsTable->setItem(rewards - 1, 0, itemType);
auto l = getListForType(typeId);
if(!l.empty())
{
auto itemCurr = new QTableWidgetItem(getListForType(typeId)[listId]);
itemCurr->setData(Qt::UserRole, listId);
ui->rewardsTable->setItem(rewards - 1, 1, itemCurr);
}
QString am = QString::number(amount);
switch(ui->rewardType->currentIndex())
{
case 6:
if(amount <= 1)
am = "Basic";
if(amount == 2)
am = "Advanced";
if(amount >= 3)
am = "Expert";
break;
case 7:
case 8:
am = "";
amount = 1;
break;
}
auto itemCount = new QTableWidgetItem(am);
itemCount->setData(Qt::UserRole, amount);
ui->rewardsTable->setItem(rewards - 1, 2, itemCount);
}
void RewardsWidget::on_buttonAdd_clicked()
{
addReward(ui->rewardType->currentIndex(), ui->rewardList->currentIndex(), ui->rewardAmount->text().toInt());
}
void RewardsWidget::on_buttonRemove_clicked()
{
ui->rewardsTable->removeRow(ui->rewardsTable->currentRow());
--rewards;
}
void RewardsWidget::on_buttonClear_clicked()
{
ui->rewardsTable->clear();
rewards = 0;
}
void RewardsWidget::on_rewardsTable_itemSelectionChanged()
{
/*auto type = ui->rewardsTable->item(ui->rewardsTable->currentRow(), 0);
ui->rewardType->setCurrentIndex(type->data(Qt::UserRole).toInt());
ui->rewardType->activated(ui->rewardType->currentIndex());
type = ui->rewardsTable->item(ui->rewardsTable->currentRow(), 1);
ui->rewardList->setCurrentIndex(type->data(Qt::UserRole).toInt());
ui->rewardList->activated(ui->rewardList->currentIndex());
type = ui->rewardsTable->item(ui->rewardsTable->currentRow(), 2);
ui->rewardAmount->setText(QString::number(type->data(Qt::UserRole).toInt()));*/
}
RewardsPandoraDelegate::RewardsPandoraDelegate(CGPandoraBox & t): pandora(t), QStyledItemDelegate()
{
}
QWidget * RewardsPandoraDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return new RewardsWidget(pandora, parent);
}
void RewardsPandoraDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(auto * ed = qobject_cast<RewardsWidget *>(editor))
{
ed->obtainData();
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
void RewardsPandoraDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(auto * ed = qobject_cast<RewardsWidget *>(editor))
{
auto isArmed = ed->commitChanges();
model->setData(index, "dummy");
if(isArmed)
model->setData(index, "HAS REWARD");
else
model->setData(index, "");
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}

View File

@ -2,21 +2,75 @@
#define REWARDSWIDGET_H
#include <QDialog>
#include "../lib/mapObjects/CGPandoraBox.h"
namespace Ui {
class RewardsWidget;
}
/*
ui32 gainedExp;
si32 manaDiff; //amount of gained / lost mana
si32 moraleDiff; //morale modifier
si32 luckDiff; //luck modifier
TResources resources;//gained / lost resources
std::vector<si32> primskills;//gained / lost prim skills
std::vector<SecondarySkill> abilities; //gained abilities
std::vector<si32> abilityLevels; //levels of gained abilities
std::vector<ArtifactID> artifacts; //gained artifacts
std::vector<SpellID> spells; //gained spells
CCreatureSet creatures; //gained creatures
*/
const std::array<std::string, 10> rewardTypes{"Experience", "Mana", "Morale", "Luck", "Resource", "Primary skill", "Secondary skill", "Artifact", "Spell", "Creature"};
class RewardsWidget : public QDialog
{
Q_OBJECT
public:
explicit RewardsWidget(QWidget *parent = nullptr);
explicit RewardsWidget(CGPandoraBox &, QWidget *parent = nullptr);
~RewardsWidget();
void obtainData();
bool commitChanges();
private slots:
void on_rewardType_activated(int index);
void on_rewardList_activated(int index);
void on_buttonAdd_clicked();
void on_buttonRemove_clicked();
void on_buttonClear_clicked();
void on_rewardsTable_itemSelectionChanged();
private:
void addReward(int typeId, int listId, int amount);
QList<QString> getListForType(int typeId);
Ui::RewardsWidget *ui;
CGPandoraBox * pandora;
int rewards = 0;
};
class RewardsPandoraDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
RewardsPandoraDelegate(CGPandoraBox &);
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
private:
CGPandoraBox & pandora;
};
#endif // REWARDSWIDGET_H

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RewardsWidget</class>
<widget class="QDialog" name="RewardsWidget">
@ -5,13 +6,77 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>645</width>
<height>335</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QPushButton" name="buttonRemove">
<property name="text">
<string>Remove selected</string>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QLineEdit" name="rewardAmount">
<property name="maximumSize">
<size>
<width>80</width>
<height>16777215</height>
</size>
</property>
<property name="inputMethodHints">
<set>Qt::ImhDigitsOnly</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="buttonClear">
<property name="text">
<string>Delete all</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="buttonAdd">
<property name="text">
<string>Add or change</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="5">
<widget class="QTableWidget" name="rewardsTable">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="columnCount">
<number>3</number>
</property>
<attribute name="horizontalHeaderVisible">
<bool>false</bool>
</attribute>
<column/>
<column/>
<column/>
</widget>
</item>
<item row="2" column="1" colspan="3">
<widget class="QComboBox" name="rewardList"/>
</item>
<item row="2" column="0">
<widget class="QComboBox" name="rewardType"/>
</item>
</layout>
</widget>
<resources/>
<connections/>

View File

@ -561,7 +561,7 @@ void MainWindow::loadObjectsTree()
addGroupIntoCatalog("ARTIFACTS", false, false, Obj::RANDOM_MAJOR_ART);
addGroupIntoCatalog("ARTIFACTS", false, false, Obj::RANDOM_RELIC_ART);
addGroupIntoCatalog("ARTIFACTS", true, false, Obj::SPELL_SCROLL);
//addGroupIntoCatalog("RESOURCES", true, false, Obj::PANDORAS_BOX);
addGroupIntoCatalog("ARTIFACTS", true, false, Obj::PANDORAS_BOX);
addGroupIntoCatalog("RESOURCES", true, false, Obj::RANDOM_RESOURCE);
addGroupIntoCatalog("RESOURCES", false, false, Obj::RESOURCE);
addGroupIntoCatalog("RESOURCES", true, false, Obj::SEA_CHEST);