From e2b8c2e9f8ded3f8369407edb9c134accf9ec219 Mon Sep 17 00:00:00 2001 From: nordsoft Date: Mon, 9 Oct 2023 01:24:00 +0200 Subject: [PATCH] PickObject delegate for random dwellings --- mapeditor/CMakeLists.txt | 2 + mapeditor/inspector/PickObjectDelegate.cpp | 59 ++++++++++++++++++++++ mapeditor/inspector/PickObjectDelegate.h | 45 +++++++++++++++++ mapeditor/inspector/inspector.cpp | 53 ++++++++++++------- mapeditor/inspector/inspector.h | 8 ++- mapeditor/mainwindow.cpp | 4 +- 6 files changed, 146 insertions(+), 25 deletions(-) create mode 100644 mapeditor/inspector/PickObjectDelegate.cpp create mode 100644 mapeditor/inspector/PickObjectDelegate.h diff --git a/mapeditor/CMakeLists.txt b/mapeditor/CMakeLists.txt index 3a2c6191b..8cdf87e09 100644 --- a/mapeditor/CMakeLists.txt +++ b/mapeditor/CMakeLists.txt @@ -34,6 +34,7 @@ set(editor_SRCS inspector/rewardswidget.cpp inspector/questwidget.cpp inspector/heroskillswidget.cpp + inspector/PickObjectDelegate.cpp resourceExtractor/ResourceConverter.cpp ) @@ -72,6 +73,7 @@ set(editor_HEADERS inspector/rewardswidget.h inspector/questwidget.h inspector/heroskillswidget.h + inspector/PickObjectDelegate.h resourceExtractor/ResourceConverter.h ) diff --git a/mapeditor/inspector/PickObjectDelegate.cpp b/mapeditor/inspector/PickObjectDelegate.cpp new file mode 100644 index 000000000..369b5ee90 --- /dev/null +++ b/mapeditor/inspector/PickObjectDelegate.cpp @@ -0,0 +1,59 @@ +/* + * PickObjectDelegate.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 "PickObjectDelegate.h" + +#include "../mapcontroller.h" +#include "../../lib/mapObjects/CGObjectInstance.h" + +PickObjectDelegate::PickObjectDelegate(MapController & c): controller(c) +{ + filter = [](const CGObjectInstance *) + { + return true; + }; +} + +PickObjectDelegate::PickObjectDelegate(MapController & c, std::function f): controller(c), filter(f) +{ + +} + +void PickObjectDelegate::onObjectPicked(const CGObjectInstance * o) +{ + for(int lvl : {0, 1}) + { + auto & l = controller.scene(lvl)->objectPickerView; + l.clear(); + l.update(); + QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &PickObjectDelegate::onObjectPicked); + } + + QMap data; + data[Qt::DisplayRole] = QVariant("None"); + data[Qt::UserRole] = QVariant::fromValue(data_cast(o)); + if(o) + data[Qt::DisplayRole] = QVariant(QString::fromStdString(o->instanceName)); + const_cast(modelIndex.model())->setItemData(modelIndex, data); +} + +QWidget * PickObjectDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + for(int lvl : {0, 1}) + { + auto & l = controller.scene(lvl)->objectPickerView; + l.highlight(filter); + l.update(); + QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &PickObjectDelegate::onObjectPicked); + } + + modelIndex = index; + return nullptr; +} diff --git a/mapeditor/inspector/PickObjectDelegate.h b/mapeditor/inspector/PickObjectDelegate.h new file mode 100644 index 000000000..f1a0089ac --- /dev/null +++ b/mapeditor/inspector/PickObjectDelegate.h @@ -0,0 +1,45 @@ +/* + * PickObjectDelegate.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 + * + */ + +#pragma once + +#include + +class MapController; + +VCMI_LIB_NAMESPACE_BEGIN + +class CGObjectInstance; + +VCMI_LIB_NAMESPACE_END + +class PickObjectDelegate : public QItemDelegate +{ + Q_OBJECT +public: + PickObjectDelegate(MapController &); + PickObjectDelegate(MapController &, std::function); + + template + static bool typedFilter(const CGObjectInstance * o) + { + return dynamic_cast(o) != nullptr; + } + + QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const override; + +public slots: + void onObjectPicked(const CGObjectInstance *); + +private: + MapController & controller; + std::function filter; + mutable QModelIndex modelIndex; +}; diff --git a/mapeditor/inspector/inspector.cpp b/mapeditor/inspector/inspector.cpp index 386d6e05e..b77f78079 100644 --- a/mapeditor/inspector/inspector.cpp +++ b/mapeditor/inspector/inspector.cpp @@ -26,6 +26,8 @@ #include "rewardswidget.h" #include "questwidget.h" #include "heroskillswidget.h" +#include "PickObjectDelegate.h" +#include "../mapcontroller.h" static QList> MissionIdentifiers { @@ -239,6 +241,12 @@ void Inspector::updateProperties(CGDwelling * o) if(!o) return; addProperty("Owner", o->tempOwner, false); + + if(auto * info = dynamic_cast(o->info)) + { + auto * delegate = new PickObjectDelegate(controller, PickObjectDelegate::typedFilter); + addProperty("Same as town", PropertyEditorPlaceholder(), delegate, false); + } } void Inspector::updateProperties(CGLighthouse * o) @@ -288,7 +296,7 @@ void Inspector::updateProperties(CGHeroInstance * o) auto * delegate = new InspectorDelegate; for(int i = 0; i < VLC->heroh->objects.size(); ++i) { - if(map->allowedHeroes.at(i)) + if(controller.map()->allowedHeroes.at(i)) { if(o->ID == Obj::PRISON || (o->type && VLC->heroh->objects[i]->heroClass->getIndex() == o->type->heroClass->getIndex())) { @@ -325,7 +333,7 @@ void Inspector::updateProperties(CGArtifact * o) auto * delegate = new InspectorDelegate; for(auto spell : VLC->spellh->objects) { - if(map->allowedSpells.at(spell->id)) + if(controller.map()->allowedSpells.at(spell->id)) delegate->options.push_back({QObject::tr(spell->getNameTranslated().c_str()), QVariant::fromValue(int(spell->getId()))}); } addProperty("Spell", VLC->spellh->getById(spellId)->getNameTranslated(), delegate, false); @@ -379,7 +387,7 @@ void Inspector::updateProperties(CRewardableObject * o) { if(!o) return; - auto * delegate = new RewardsDelegate(*map, *o); + auto * delegate = new RewardsDelegate(*controller.map(), *o); addProperty("Reward", PropertyEditorPlaceholder(), delegate, false); } @@ -415,7 +423,7 @@ void Inspector::updateProperties(CGSeerHut * o) addProperty("Completed text", o->quest->completedText, new MessageDelegate, false); { //Quest - auto * delegate = new QuestDelegate(*map, *o); + auto * delegate = new QuestDelegate(*controller.map(), *o); addProperty("Quest", PropertyEditorPlaceholder(), delegate, false); } } @@ -441,8 +449,8 @@ void Inspector::updateProperties() auto * delegate = new InspectorDelegate(); delegate->options.push_back({QObject::tr("neutral"), QVariant::fromValue(PlayerColor::NEUTRAL.getNum())}); - for(int p = 0; p < map->players.size(); ++p) - if(map->players[p].canAnyonePlay()) + for(int p = 0; p < controller.map()->players.size(); ++p) + if(controller.map()->players[p].canAnyonePlay()) delegate->options.push_back({QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[p]), QVariant::fromValue(PlayerColor(p).getNum())}); addProperty("Owner", obj->tempOwner, delegate, true); @@ -530,7 +538,7 @@ void Inspector::setProperty(CGPandoraBox * o, const QString & key, const QVarian if(!o) return; if(key == "Message") - o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString())); + o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString())); } void Inspector::setProperty(CGEvent * o, const QString & key, const QVariant & value) @@ -552,7 +560,7 @@ void Inspector::setProperty(CGTownInstance * o, const QString & key, const QVari if(!o) return; if(key == "Town name") - o->setNameTextId(mapRegisterLocalizedString("map", *map, TextIdentifier("town", o->instanceName, "name"), value.toString().toStdString())); + o->setNameTextId(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("town", o->instanceName, "name"), value.toString().toStdString())); } void Inspector::setProperty(CGSignBottle * o, const QString & key, const QVariant & value) @@ -560,7 +568,7 @@ void Inspector::setProperty(CGSignBottle * o, const QString & key, const QVarian if(!o) return; if(key == "Message") - o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("sign", o->instanceName, "message"), value.toString().toStdString())); + o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("sign", o->instanceName, "message"), value.toString().toStdString())); } void Inspector::setProperty(CGMine * o, const QString & key, const QVariant & value) @@ -576,7 +584,7 @@ void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant if(!o) return; if(key == "Message") - o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString())); + o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString())); if(o->storedArtifact && key == "Spell") { @@ -587,6 +595,16 @@ void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant void Inspector::setProperty(CGDwelling * o, const QString & key, const QVariant & value) { if(!o) return; + + if(key == "Same as town") + { + if(auto * info = dynamic_cast(o->info)) + { + info->instanceId = ""; + if(CGTownInstance * town = data_cast(value.toLongLong())) + info->instanceId = town->instanceName; + } + } } void Inspector::setProperty(CGGarrison * o, const QString & key, const QVariant & value) @@ -605,10 +623,10 @@ void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVari o->gender = EHeroGender(value.toInt()); if(key == "Name") - o->nameCustomTextId = mapRegisterLocalizedString("map", *map, TextIdentifier("hero", o->instanceName, "name"), value.toString().toStdString()); + o->nameCustomTextId = mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("hero", o->instanceName, "name"), value.toString().toStdString()); if(key == "Biography") - o->biographyCustomTextId = mapRegisterLocalizedString("map", *map, TextIdentifier("hero", o->instanceName, "biography"), value.toString().toStdString()); + o->biographyCustomTextId = mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("hero", o->instanceName, "biography"), value.toString().toStdString()); if(key == "Experience") o->exp = value.toString().toInt(); @@ -644,7 +662,7 @@ void Inspector::setProperty(CGCreature * o, const QString & key, const QVariant if(!o) return; if(key == "Message") - o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("monster", o->instanceName, "message"), value.toString().toStdString())); + o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("monster", o->instanceName, "message"), value.toString().toStdString())); if(key == "Character") o->character = CGCreature::Character(value.toInt()); if(key == "Never flees") @@ -662,11 +680,11 @@ void Inspector::setProperty(CGSeerHut * o, const QString & key, const QVariant & if(key == "Mission type") o->quest->missionType = CQuest::Emission(value.toInt()); if(key == "First visit text") - o->quest->firstVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "firstVisit"), value.toString().toStdString())); + o->quest->firstVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("quest", o->instanceName, "firstVisit"), value.toString().toStdString())); if(key == "Next visit text") - o->quest->nextVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "nextVisit"), value.toString().toStdString())); + o->quest->nextVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("quest", o->instanceName, "nextVisit"), value.toString().toStdString())); if(key == "Completed text") - o->quest->completedText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *map, TextIdentifier("quest", o->instanceName, "completed"), value.toString().toStdString())); + o->quest->completedText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(), TextIdentifier("quest", o->instanceName, "completed"), value.toString().toStdString())); } @@ -799,7 +817,7 @@ QTableWidgetItem * Inspector::addProperty(CQuest::Emission value) //======================================================================== -Inspector::Inspector(CMap * m, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), map(m) +Inspector::Inspector(MapController & c, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), controller(c) { } @@ -845,4 +863,3 @@ void InspectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, QStyledItemDelegate::setModelData(editor, model, index); } } - diff --git a/mapeditor/inspector/inspector.h b/mapeditor/inspector/inspector.h index c31f41818..69633a3db 100644 --- a/mapeditor/inspector/inspector.h +++ b/mapeditor/inspector/inspector.h @@ -32,6 +32,7 @@ void setProperty(x*, const QString &, const QVariant &); #define SET_PROPERTIES(x) setProperty(dynamic_cast(obj), key, value) +class MapController; class Initializer { public: @@ -101,7 +102,7 @@ protected: //===============END OF DECLARATION======================================= public: - Inspector(CMap *, CGObjectInstance *, QTableWidget *); + Inspector(MapController &, CGObjectInstance *, QTableWidget *); void setProperty(const QString & key, const QTableWidgetItem * item); @@ -154,12 +155,10 @@ protected: QTableWidget * table; CGObjectInstance * obj; QMap keyItems; - CMap * map; + MapController & controller; }; - - class InspectorDelegate : public QStyledItemDelegate { Q_OBJECT @@ -172,4 +171,3 @@ public: QList> options; }; - diff --git a/mapeditor/mainwindow.cpp b/mapeditor/mainwindow.cpp index fac99c05b..1dcb6b544 100644 --- a/mapeditor/mainwindow.cpp +++ b/mapeditor/mainwindow.cpp @@ -998,7 +998,7 @@ void MainWindow::loadInspector(CGObjectInstance * obj, bool switchTab) { if(switchTab) ui->tabWidget->setCurrentIndex(1); - Inspector inspector(controller.map(), obj, ui->inspectorWidget); + Inspector inspector(controller, obj, ui->inspectorWidget); inspector.updateProperties(); } @@ -1022,7 +1022,7 @@ void MainWindow::on_inspectorWidget_itemChanged(QTableWidgetItem *item) auto param = tableWidget->item(r, c - 1)->text(); //set parameter - Inspector inspector(controller.map(), obj, tableWidget); + Inspector inspector(controller, obj, tableWidget); inspector.setProperty(param, item); controller.commitObjectChange(mapLevel); }