objectselector.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * objectselector.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "objectselector.h"
  12. #include "ui_objectselector.h"
  13. #include "../mainwindow.h"
  14. #include "../mapcontroller.h"
  15. #include "../../lib/mapping/CMap.h"
  16. #include "../../lib/rmg/ObjectConfig.h"
  17. #include "../../lib/GameLibrary.h"
  18. #include "../../lib/mapObjects/CGObjectInstance.h"
  19. #include "../../lib/mapObjectConstructors/AObjectTypeHandler.h"
  20. #include "../../lib/mapObjectConstructors/CObjectClassesHandler.h"
  21. ObjectSelector::ObjectSelector(ObjectConfig & obj) :
  22. ui(new Ui::ObjectSelector),
  23. obj(obj),
  24. advObjects(getAdventureMapItems())
  25. {
  26. ui->setupUi(this);
  27. setWindowTitle(tr("Object Selector"));
  28. setWindowModality(Qt::ApplicationModal);
  29. fillBannedObjectCategories();
  30. fillBannedObjects();
  31. fillCustomObjects();
  32. show();
  33. }
  34. QMainWindow* getMainWindow()
  35. {
  36. foreach (QWidget *w, qApp->topLevelWidgets())
  37. if (QMainWindow* mainWin = qobject_cast<QMainWindow*>(w))
  38. return mainWin;
  39. return nullptr;
  40. }
  41. std::map<CompoundMapObjectID, QString> ObjectSelector::getAdventureMapItems()
  42. {
  43. std::map<CompoundMapObjectID, QString> objects;
  44. auto& controller = qobject_cast<MainWindow *>(getMainWindow())->controller;
  45. auto knownObjects = LIBRARY->objtypeh->knownObjects();
  46. for(auto & id : knownObjects)
  47. {
  48. auto knownSubObjects = LIBRARY->objtypeh->knownSubObjects(id);
  49. for(auto & subId : knownSubObjects)
  50. {
  51. auto objId = CompoundMapObjectID(id, subId);
  52. auto factory = LIBRARY->objtypeh->getHandlerFor(id, subId);
  53. auto templates = factory->getTemplates();
  54. QString name{};
  55. try
  56. {
  57. auto templ = templates.at(0);
  58. auto temporaryObj(factory->create(controller.getCallback(), templ));
  59. QString translated = QString::fromStdString(temporaryObj->getObjectName().c_str());
  60. name = translated;
  61. }
  62. catch(...) {}
  63. if(name.isEmpty())
  64. {
  65. auto subGroupName = QString::fromStdString(LIBRARY->objtypeh->getObjectName(id, subId));
  66. name = subGroupName;
  67. }
  68. if(!name.isEmpty())
  69. objects[objId] = name;
  70. }
  71. }
  72. return objects;
  73. }
  74. void ObjectSelector::fillBannedObjectCategories()
  75. {
  76. ui->tableWidgetBannedObjectCategories->setColumnCount(2);
  77. ui->tableWidgetBannedObjectCategories->setRowCount(obj.bannedObjectCategories.size() + 1);
  78. ui->tableWidgetBannedObjectCategories->setHorizontalHeaderLabels({tr("Category"), tr("Action")});
  79. auto addRow = [this](ObjectConfig::EObjectCategory category, int row){
  80. std::map<ObjectConfig::EObjectCategory, QString> values = {
  81. { ObjectConfig::EObjectCategory::OTHER, tr("Other") },
  82. { ObjectConfig::EObjectCategory::ALL, tr("All") },
  83. { ObjectConfig::EObjectCategory::NONE, tr("None") },
  84. { ObjectConfig::EObjectCategory::CREATURE_BANK, tr("Creature bank") },
  85. { ObjectConfig::EObjectCategory::BONUS, tr("Bonus") },
  86. { ObjectConfig::EObjectCategory::DWELLING, tr("Dwelling") },
  87. { ObjectConfig::EObjectCategory::RESOURCE, tr("Resource") },
  88. { ObjectConfig::EObjectCategory::RESOURCE_GENERATOR, tr("Resource generator") },
  89. { ObjectConfig::EObjectCategory::SPELL_SCROLL, tr("Spell scroll") },
  90. { ObjectConfig::EObjectCategory::RANDOM_ARTIFACT, tr("Random artifact") },
  91. { ObjectConfig::EObjectCategory::PANDORAS_BOX, tr("Pandoras box") },
  92. { ObjectConfig::EObjectCategory::QUEST_ARTIFACT, tr("Quest artifact") },
  93. { ObjectConfig::EObjectCategory::SEER_HUT, tr("Seer hut") }
  94. };
  95. QComboBox *combo = new QComboBox();
  96. for(auto & item : values)
  97. combo->addItem(item.second, QVariant(static_cast<int>(item.first)));
  98. int index = combo->findData(static_cast<int>(category));
  99. if (index != -1)
  100. combo->setCurrentIndex(index);
  101. ui->tableWidgetBannedObjectCategories->setCellWidget(row, 0, combo);
  102. auto deleteButton = new QPushButton("Delete");
  103. ui->tableWidgetBannedObjectCategories->setCellWidget(row, 1, deleteButton);
  104. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  105. for (int r = 0; r < ui->tableWidgetBannedObjectCategories->rowCount(); ++r) {
  106. if (ui->tableWidgetBannedObjectCategories->cellWidget(r, 1) == deleteButton) {
  107. ui->tableWidgetBannedObjectCategories->removeRow(r);
  108. break;
  109. }
  110. }
  111. });
  112. };
  113. for (int row = 0; row < obj.bannedObjectCategories.size(); ++row)
  114. addRow(obj.bannedObjectCategories[row], row);
  115. auto addButton = new QPushButton("Add");
  116. ui->tableWidgetBannedObjectCategories->setCellWidget(ui->tableWidgetBannedObjectCategories->rowCount() - 1, 1, addButton);
  117. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  118. ui->tableWidgetBannedObjectCategories->insertRow(ui->tableWidgetBannedObjectCategories->rowCount() - 1);
  119. addRow(ObjectConfig::EObjectCategory::ALL, ui->tableWidgetBannedObjectCategories->rowCount() - 2);
  120. });
  121. ui->tableWidgetBannedObjectCategories->resizeColumnsToContents();
  122. ui->tableWidgetBannedObjectCategories->setColumnWidth(0, 300);
  123. }
  124. void ObjectSelector::getBannedObjectCategories()
  125. {
  126. obj.bannedObjectCategories.clear();
  127. for (int row = 0; row < ui->tableWidgetBannedObjectCategories->rowCount() - 1; ++row)
  128. {
  129. auto val = static_cast<ObjectConfig::EObjectCategory>(static_cast<QComboBox *>(ui->tableWidgetBannedObjectCategories->cellWidget(row, 0))->currentData().toInt());
  130. if(std::find(obj.bannedObjectCategories.begin(), obj.bannedObjectCategories.end(), val) == obj.bannedObjectCategories.end())
  131. obj.bannedObjectCategories.push_back(val);
  132. }
  133. }
  134. void ObjectSelector::fillBannedObjects()
  135. {
  136. ui->tableWidgetBannedObjects->setColumnCount(2);
  137. ui->tableWidgetBannedObjects->setRowCount(obj.bannedObjects.size() + 1);
  138. ui->tableWidgetBannedObjects->setHorizontalHeaderLabels({tr("Object"), tr("Action")});
  139. auto addRow = [this](CompoundMapObjectID obj, int row){
  140. QComboBox *combo = new QComboBox();
  141. for(auto & item : advObjects)
  142. combo->addItem(item.second, QVariant::fromValue(item.first));
  143. int index = combo->findData(QVariant::fromValue(obj));
  144. if (index != -1)
  145. combo->setCurrentIndex(index);
  146. combo->setEditable(true);
  147. QCompleter* completer = new QCompleter(combo);
  148. completer->setModel(combo->model());
  149. completer->setCompletionMode(QCompleter::PopupCompletion);
  150. completer->setFilterMode(Qt::MatchContains);
  151. combo->setCompleter(completer);
  152. ui->tableWidgetBannedObjects->setCellWidget(row, 0, combo);
  153. auto deleteButton = new QPushButton("Delete");
  154. ui->tableWidgetBannedObjects->setCellWidget(row, 1, deleteButton);
  155. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  156. for (int r = 0; r < ui->tableWidgetBannedObjects->rowCount(); ++r) {
  157. if (ui->tableWidgetBannedObjects->cellWidget(r, 1) == deleteButton) {
  158. ui->tableWidgetBannedObjects->removeRow(r);
  159. break;
  160. }
  161. }
  162. });
  163. };
  164. for (int row = 0; row < obj.bannedObjects.size(); ++row)
  165. addRow(obj.bannedObjects[row], row);
  166. auto addButton = new QPushButton("Add");
  167. ui->tableWidgetBannedObjects->setCellWidget(ui->tableWidgetBannedObjects->rowCount() - 1, 1, addButton);
  168. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  169. ui->tableWidgetBannedObjects->insertRow(ui->tableWidgetBannedObjects->rowCount() - 1);
  170. addRow((*advObjects.begin()).first, ui->tableWidgetBannedObjects->rowCount() - 2);
  171. });
  172. ui->tableWidgetBannedObjects->resizeColumnsToContents();
  173. ui->tableWidgetBannedObjects->setColumnWidth(0, 300);
  174. }
  175. void ObjectSelector::getBannedObjects()
  176. {
  177. obj.bannedObjects.clear();
  178. for (int row = 0; row < ui->tableWidgetBannedObjects->rowCount() - 1; ++row)
  179. {
  180. auto val = static_cast<QComboBox *>(ui->tableWidgetBannedObjects->cellWidget(row, 0))->currentData().value<CompoundMapObjectID>();
  181. obj.bannedObjects.push_back(val);
  182. }
  183. }
  184. void ObjectSelector::fillCustomObjects()
  185. {
  186. }
  187. void ObjectSelector::getCustomObjects()
  188. {
  189. }
  190. void ObjectSelector::showObjectSelector(ObjectConfig & obj)
  191. {
  192. auto * dialog = new ObjectSelector(obj);
  193. dialog->setAttribute(Qt::WA_DeleteOnClose);
  194. dialog->exec();
  195. }
  196. void ObjectSelector::on_buttonBoxResult_accepted()
  197. {
  198. getBannedObjectCategories();
  199. getBannedObjects();
  200. getCustomObjects();
  201. close();
  202. }
  203. void ObjectSelector::on_buttonBoxResult_rejected()
  204. {
  205. close();
  206. }