1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00

Army property delegate is implemented

This commit is contained in:
nordsoft 2022-09-09 22:05:28 +04:00
parent d7de9710cc
commit 7acaa1daa9
6 changed files with 487 additions and 6 deletions

View File

@ -23,6 +23,7 @@ set(editor_SRCS
mapcontroller.cpp mapcontroller.cpp
validator.cpp validator.cpp
townbulidingswidget.cpp townbulidingswidget.cpp
armywidget.cpp
) )
set(editor_HEADERS set(editor_HEADERS
@ -49,6 +50,7 @@ set(editor_HEADERS
mapcontroller.h mapcontroller.h
validator.h validator.h
townbulidingswidget.h townbulidingswidget.h
armywidget.h
) )
set(editor_FORMS set(editor_FORMS
@ -60,6 +62,7 @@ set(editor_FORMS
playerparams.ui playerparams.ui
validator.ui validator.ui
townbulidingswidget.ui townbulidingswidget.ui
armywidget.ui
) )
assign_source_group(${editor_SRCS} ${editor_HEADERS} VCMI_launcher.rc) assign_source_group(${editor_SRCS} ${editor_HEADERS} VCMI_launcher.rc)

141
mapeditor/armywidget.cpp Normal file
View File

@ -0,0 +1,141 @@
#include "armywidget.h"
#include "ui_armywidget.h"
#include "CCreatureHandler.h"
ArmyWidget::ArmyWidget(CArmedInstance & a, QWidget *parent) :
QDialog(parent),
army(a),
ui(new Ui::ArmyWidget)
{
ui->setupUi(this);
uiCounts[0] = ui->count0; uiSlots[0] = ui->slot0;
uiCounts[1] = ui->count1; uiSlots[1] = ui->slot1;
uiCounts[2] = ui->count2; uiSlots[2] = ui->slot2;
uiCounts[3] = ui->count3; uiSlots[3] = ui->slot3;
uiCounts[4] = ui->count4; uiSlots[4] = ui->slot4;
uiCounts[5] = ui->count5; uiSlots[5] = ui->slot5;
uiCounts[6] = ui->count6; uiSlots[6] = ui->slot6;
for(int i = 0; i < TOTAL_SLOTS; ++i)
{
uiCounts[i]->setInputMask("d0000");
uiCounts[i]->setText("1");
uiSlots[i]->addItem("");
uiSlots[i]->setItemData(0, -1);
for(int c = 0; c < VLC->creh->objects.size(); ++c)
{
auto creature = VLC->creh->objects[c];
uiSlots[i]->insertItem(c + 1, tr(creature->getPluralName().c_str()));
uiSlots[i]->setItemData(c + 1, creature->getId().getNum());
}
}
ui->formationTight->setChecked(true);
}
int ArmyWidget::searchItemIndex(int slotId, CreatureID creId) const
{
for(int i = 0; i < uiSlots[slotId]->count(); ++i)
{
if(creId.getNum() == uiSlots[slotId]->itemData(i).toInt())
return i;
}
return 0;
}
void ArmyWidget::obtainData()
{
for(int i = 0; i < TOTAL_SLOTS; ++i)
{
if(army.hasStackAtSlot(SlotID(i)))
{
auto * creature = army.getCreature(SlotID(i));
uiSlots[i]->setCurrentIndex(searchItemIndex(i, creature->getId()));
uiCounts[i]->setText(QString::number(army.getStackCount(SlotID(i))));
}
}
if(army.formation)
ui->formationTight->setChecked(true);
else
ui->formationWide->setChecked(true);
}
bool ArmyWidget::commitChanges()
{
bool isArmed = false;
for(int i = 0; i < TOTAL_SLOTS; ++i)
{
CreatureID creId(uiSlots[i]->itemData(uiSlots[i]->currentIndex()).toInt());
if(creId == -1)
{
if(army.hasStackAtSlot(SlotID(i)))
army.eraseStack(SlotID(i));
}
else
{
isArmed = true;
int amount = uiCounts[i]->text().toInt();
if(amount)
{
army.setCreature(SlotID(i), creId, amount);
}
else
{
if(army.hasStackAtSlot(SlotID(i)))
army.eraseStack(SlotID(i));
army.putStack(SlotID(i), new CStackInstance(creId, amount, false));
}
}
}
army.setFormation(ui->formationTight->isChecked());
return isArmed;
}
ArmyWidget::~ArmyWidget()
{
delete ui;
}
ArmyDelegate::ArmyDelegate(CArmedInstance & t): army(t), QStyledItemDelegate()
{
}
QWidget * ArmyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return new ArmyWidget(army, parent);
}
void ArmyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(auto * ed = qobject_cast<ArmyWidget *>(editor))
{
ed->obtainData();
}
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
void ArmyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(auto * ed = qobject_cast<ArmyWidget *>(editor))
{
auto isArmed = ed->commitChanges();
if(isArmed)
model->setData(index, "HAS ARMY");
else
model->setData(index, "");
}
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}

49
mapeditor/armywidget.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef ARMYWIDGET_H
#define ARMYWIDGET_H
#include <QDialog>
#include "../lib/mapObjects/CArmedInstance.h"
const int TOTAL_SLOTS = 7;
namespace Ui {
class ArmyWidget;
}
class ArmyWidget : public QDialog
{
Q_OBJECT
public:
explicit ArmyWidget(CArmedInstance &, QWidget *parent = nullptr);
~ArmyWidget();
void obtainData();
bool commitChanges();
private:
int searchItemIndex(int slotId, CreatureID creId) const;
Ui::ArmyWidget *ui;
CArmedInstance & army;
std::array<QLineEdit*, TOTAL_SLOTS> uiCounts;
std::array<QComboBox*, TOTAL_SLOTS> uiSlots;
};
class ArmyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
using QStyledItemDelegate::QStyledItemDelegate;
ArmyDelegate(CArmedInstance &);
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:
CArmedInstance & army;
};
#endif // ARMYWIDGET_H

277
mapeditor/armywidget.ui Normal file
View File

@ -0,0 +1,277 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ArmyWidget</class>
<widget class="QDialog" name="ArmyWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>318</width>
<height>314</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>318</width>
<height>314</height>
</size>
</property>
<property name="windowTitle">
<string>Army settings</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="6" column="0">
<widget class="QComboBox" name="slot6">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QComboBox" name="slot3">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QComboBox" name="slot2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="count6">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="count2">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="count1">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="count5">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QRadioButton" name="formationWide">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Wide formation</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="count3">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QComboBox" name="slot1">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="slot0">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QComboBox" name="slot4">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="count4">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QComboBox" name="slot5">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="count0">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>30</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>50</width>
<height>16777215</height>
</size>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QRadioButton" name="formationTight">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Tight formation</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -7,6 +7,7 @@
#include "../lib/mapping/CMap.h" #include "../lib/mapping/CMap.h"
#include "townbulidingswidget.h" #include "townbulidingswidget.h"
#include "armywidget.h"
//===============IMPLEMENT OBJECT INITIALIZATION FUNCTIONS================ //===============IMPLEMENT OBJECT INITIALIZATION FUNCTIONS================
Initializer::Initializer(CMap * m, CGObjectInstance * o) : map(m) Initializer::Initializer(CMap * m, CGObjectInstance * o) : map(m)
@ -141,14 +142,21 @@ void Inspector::updateProperties(CArmedInstance * o)
{ {
if(!o) return; if(!o) return;
{
auto * delegate = new InspectorDelegate(); auto * delegate = new InspectorDelegate();
delegate->options << "NEUTRAL"; delegate->options << "NEUTRAL";
for(int p = 0; p < map->players.size(); ++p) for(int p = 0; p < map->players.size(); ++p)
if(map->players[p].canAnyonePlay()) if(map->players[p].canAnyonePlay())
delegate->options << QString("PLAYER %1").arg(p); delegate->options << QString("PLAYER %1").arg(p);
addProperty("Owner", o->tempOwner, delegate, true); addProperty("Owner", o->tempOwner, delegate, true);
} }
{
auto * delegate = new ArmyDelegate(*o);
addProperty("Army", PropertyEditorPlaceholder(), delegate, false);
}
}
void Inspector::updateProperties(CGDwelling * o) void Inspector::updateProperties(CGDwelling * o)
{ {
@ -241,6 +249,7 @@ void Inspector::updateProperties(CGCreature * o)
addProperty("Never flees", o->neverFlees, InspectorDelegate::boolDelegate(), false); addProperty("Never flees", o->neverFlees, InspectorDelegate::boolDelegate(), false);
addProperty("Not growing", o->notGrowingTeam, InspectorDelegate::boolDelegate(), false); addProperty("Not growing", o->notGrowingTeam, InspectorDelegate::boolDelegate(), false);
addProperty("Artifact reward", o->gainedArtifact); //TODO: implement in setProperty addProperty("Artifact reward", o->gainedArtifact); //TODO: implement in setProperty
addProperty("Army", PropertyEditorPlaceholder(), true);
addProperty("Amount", o->stacks[SlotID(0)]->count, false); addProperty("Amount", o->stacks[SlotID(0)]->count, false);
//addProperty("Resources reward", o->resources); //TODO: implement in setProperty //addProperty("Resources reward", o->resources); //TODO: implement in setProperty
} }

View File

@ -24,6 +24,8 @@ WindowNewMap::WindowNewMap(QWidget *parent) :
show(); show();
ui->widthTxt->setInputMask("d00");
ui->heightTxt->setInputMask("d00");
//setup initial parameters //setup initial parameters
mapGenOptions.setWidth(ui->widthTxt->text().toInt()); mapGenOptions.setWidth(ui->widthTxt->text().toInt());
mapGenOptions.setHeight(ui->heightTxt->text().toInt()); mapGenOptions.setHeight(ui->heightTxt->text().toInt());