objectselector.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. ui->tableWidgetObjects->setColumnCount(5);
  187. ui->tableWidgetObjects->setRowCount(obj.customObjects.size() + 1);
  188. ui->tableWidgetObjects->setHorizontalHeaderLabels({tr("Object"), tr("Value"), tr("Probability"), tr("Max per zone"), tr("Action")});
  189. auto addRow = [this](CompoundMapObjectID obj, ui32 value, ui16 probability, ui32 maxPerZone, int row){
  190. QComboBox *combo = new QComboBox();
  191. for(auto & item : advObjects)
  192. combo->addItem(item.second, QVariant::fromValue(item.first));
  193. int index = combo->findData(QVariant::fromValue(obj));
  194. if (index != -1)
  195. combo->setCurrentIndex(index);
  196. combo->setEditable(true);
  197. QCompleter* completer = new QCompleter(combo);
  198. completer->setModel(combo->model());
  199. completer->setCompletionMode(QCompleter::PopupCompletion);
  200. completer->setFilterMode(Qt::MatchContains);
  201. combo->setCompleter(completer);
  202. ui->tableWidgetObjects->setCellWidget(row, 0, combo);
  203. QSpinBox *spinValue = new QSpinBox();
  204. spinValue->setRange(0, 10000);
  205. spinValue->setValue(value);
  206. ui->tableWidgetObjects->setCellWidget(row, 1, spinValue);
  207. QSpinBox *spinProbability = new QSpinBox();
  208. spinProbability->setRange(0, 1000);
  209. spinProbability->setValue(probability);
  210. ui->tableWidgetObjects->setCellWidget(row, 2, spinProbability);
  211. QSpinBox *spinMaxPerZone = new QSpinBox();
  212. spinMaxPerZone->setRange(0, 100);
  213. spinMaxPerZone->setValue(maxPerZone);
  214. ui->tableWidgetObjects->setCellWidget(row, 3, spinMaxPerZone);
  215. auto deleteButton = new QPushButton("Delete");
  216. ui->tableWidgetObjects->setCellWidget(row, 4, deleteButton);
  217. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  218. for (int r = 0; r < ui->tableWidgetObjects->rowCount(); ++r) {
  219. if (ui->tableWidgetObjects->cellWidget(r, 4) == deleteButton) {
  220. ui->tableWidgetObjects->removeRow(r);
  221. break;
  222. }
  223. }
  224. });
  225. };
  226. for (int row = 0; row < obj.customObjects.size(); ++row)
  227. addRow(obj.customObjects[row].getCompoundID(), obj.customObjects[row].value, obj.customObjects[row].probability, obj.customObjects[row].maxPerZone, row);
  228. auto addButton = new QPushButton("Add");
  229. ui->tableWidgetObjects->setCellWidget(ui->tableWidgetObjects->rowCount() - 1, 4, addButton);
  230. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  231. ui->tableWidgetObjects->insertRow(ui->tableWidgetObjects->rowCount() - 1);
  232. addRow((*advObjects.begin()).first, 0, 0, 1, ui->tableWidgetObjects->rowCount() - 2);
  233. });
  234. ui->tableWidgetObjects->resizeColumnsToContents();
  235. ui->tableWidgetObjects->setColumnWidth(0, 300);
  236. }
  237. void ObjectSelector::getCustomObjects()
  238. {
  239. obj.customObjects.clear();
  240. for (int row = 0; row < ui->tableWidgetObjects->rowCount() - 1; ++row)
  241. {
  242. auto id = static_cast<QComboBox *>(ui->tableWidgetObjects->cellWidget(row, 0))->currentData().value<CompoundMapObjectID>();
  243. auto value = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 1))->value();
  244. auto probability = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 2))->value();
  245. auto maxPerZone = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 3))->value();
  246. auto info = ObjectInfo(id);
  247. info.value = value;
  248. info.probability = probability;
  249. info.maxPerZone = maxPerZone;
  250. obj.customObjects.push_back(info);
  251. }
  252. }
  253. void ObjectSelector::showObjectSelector(ObjectConfig & obj)
  254. {
  255. auto * dialog = new ObjectSelector(obj);
  256. dialog->setAttribute(Qt::WA_DeleteOnClose);
  257. dialog->exec();
  258. }
  259. void ObjectSelector::on_buttonBoxResult_accepted()
  260. {
  261. getBannedObjectCategories();
  262. getBannedObjects();
  263. getCustomObjects();
  264. close();
  265. }
  266. void ObjectSelector::on_buttonBoxResult_rejected()
  267. {
  268. close();
  269. }