objectselector.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. fillRequiredObjects();
  33. show();
  34. }
  35. QMainWindow* getMainWindow()
  36. {
  37. foreach (QWidget *w, qApp->topLevelWidgets())
  38. if (QMainWindow* mainWin = qobject_cast<QMainWindow*>(w))
  39. return mainWin;
  40. return nullptr;
  41. }
  42. std::map<CompoundMapObjectID, QString> ObjectSelector::getAdventureMapItems()
  43. {
  44. std::map<CompoundMapObjectID, QString> objects;
  45. auto& controller = qobject_cast<MainWindow *>(getMainWindow())->controller;
  46. auto knownObjects = LIBRARY->objtypeh->knownObjects();
  47. for(auto & id : knownObjects)
  48. {
  49. auto knownSubObjects = LIBRARY->objtypeh->knownSubObjects(id);
  50. for(auto & subId : knownSubObjects)
  51. {
  52. auto objId = CompoundMapObjectID(id, subId);
  53. auto factory = LIBRARY->objtypeh->getHandlerFor(id, subId);
  54. auto templates = factory->getTemplates();
  55. QString name{};
  56. try
  57. {
  58. auto templ = templates.at(0);
  59. auto temporaryObj(factory->create(controller.getCallback(), templ));
  60. QString translated = QString::fromStdString(temporaryObj->getObjectName().c_str());
  61. name = translated;
  62. }
  63. catch(...) {}
  64. if(name.isEmpty())
  65. {
  66. auto subGroupName = QString::fromStdString(LIBRARY->objtypeh->getObjectName(id, subId));
  67. name = subGroupName;
  68. }
  69. if(!name.isEmpty())
  70. objects[objId] = name;
  71. }
  72. }
  73. return objects;
  74. }
  75. void ObjectSelector::fillBannedObjectCategories()
  76. {
  77. ui->tableWidgetBannedObjectCategories->setColumnCount(2);
  78. ui->tableWidgetBannedObjectCategories->setRowCount(obj.bannedObjectCategories.size() + 1);
  79. ui->tableWidgetBannedObjectCategories->setHorizontalHeaderLabels({tr("Category"), tr("Action")});
  80. auto addRow = [this](ObjectConfig::EObjectCategory category, int row){
  81. std::map<ObjectConfig::EObjectCategory, QString> values = {
  82. { ObjectConfig::EObjectCategory::OTHER, tr("Other") },
  83. { ObjectConfig::EObjectCategory::ALL, tr("All") },
  84. { ObjectConfig::EObjectCategory::NONE, tr("None") },
  85. { ObjectConfig::EObjectCategory::CREATURE_BANK, tr("Creature bank") },
  86. { ObjectConfig::EObjectCategory::BONUS, tr("Bonus") },
  87. { ObjectConfig::EObjectCategory::DWELLING, tr("Dwelling") },
  88. { ObjectConfig::EObjectCategory::RESOURCE, tr("Resource") },
  89. { ObjectConfig::EObjectCategory::RESOURCE_GENERATOR, tr("Resource generator") },
  90. { ObjectConfig::EObjectCategory::SPELL_SCROLL, tr("Spell scroll") },
  91. { ObjectConfig::EObjectCategory::RANDOM_ARTIFACT, tr("Random artifact") },
  92. { ObjectConfig::EObjectCategory::PANDORAS_BOX, tr("Pandoras box") },
  93. { ObjectConfig::EObjectCategory::QUEST_ARTIFACT, tr("Quest artifact") },
  94. { ObjectConfig::EObjectCategory::SEER_HUT, tr("Seer hut") }
  95. };
  96. QComboBox *combo = new QComboBox();
  97. for(auto & item : values)
  98. combo->addItem(item.second, QVariant(static_cast<int>(item.first)));
  99. int index = combo->findData(static_cast<int>(category));
  100. if (index != -1)
  101. combo->setCurrentIndex(index);
  102. ui->tableWidgetBannedObjectCategories->setCellWidget(row, 0, combo);
  103. auto deleteButton = new QPushButton(tr("Delete"));
  104. ui->tableWidgetBannedObjectCategories->setCellWidget(row, 1, deleteButton);
  105. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  106. for (int r = 0; r < ui->tableWidgetBannedObjectCategories->rowCount(); ++r) {
  107. if (ui->tableWidgetBannedObjectCategories->cellWidget(r, 1) == deleteButton) {
  108. ui->tableWidgetBannedObjectCategories->removeRow(r);
  109. break;
  110. }
  111. }
  112. });
  113. };
  114. for (int row = 0; row < obj.bannedObjectCategories.size(); ++row)
  115. addRow(obj.bannedObjectCategories[row], row);
  116. auto addButton = new QPushButton(tr("Add"));
  117. ui->tableWidgetBannedObjectCategories->setCellWidget(ui->tableWidgetBannedObjectCategories->rowCount() - 1, 1, addButton);
  118. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  119. ui->tableWidgetBannedObjectCategories->insertRow(ui->tableWidgetBannedObjectCategories->rowCount() - 1);
  120. addRow(ObjectConfig::EObjectCategory::ALL, ui->tableWidgetBannedObjectCategories->rowCount() - 2);
  121. });
  122. ui->tableWidgetBannedObjectCategories->resizeColumnsToContents();
  123. ui->tableWidgetBannedObjectCategories->setColumnWidth(0, 300);
  124. }
  125. void ObjectSelector::getBannedObjectCategories()
  126. {
  127. obj.bannedObjectCategories.clear();
  128. for (int row = 0; row < ui->tableWidgetBannedObjectCategories->rowCount() - 1; ++row)
  129. {
  130. auto val = static_cast<ObjectConfig::EObjectCategory>(static_cast<QComboBox *>(ui->tableWidgetBannedObjectCategories->cellWidget(row, 0))->currentData().toInt());
  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(tr("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(tr("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::fillRequiredObjects()
  185. {
  186. auto reqObjs = obj.getRequiredObjects();
  187. ui->tableWidgetRequiredObjects->setColumnCount(4);
  188. ui->tableWidgetRequiredObjects->setRowCount(reqObjs.size() + 1);
  189. ui->tableWidgetRequiredObjects->setHorizontalHeaderLabels({tr("Object"), tr("Count"), tr("Guard"), tr("Action")});
  190. auto addRow = [this](CompoundMapObjectID objId, ui16 count, std::optional<ui32> guard, int row){
  191. // Column 0: Object selector (QComboBox with searchable dropdown)
  192. QComboBox *combo = new QComboBox();
  193. for(auto & item : advObjects)
  194. combo->addItem(item.second, QVariant::fromValue(item.first));
  195. int index = combo->findData(QVariant::fromValue(objId));
  196. if (index != -1)
  197. combo->setCurrentIndex(index);
  198. combo->setEditable(true);
  199. QCompleter* completer = new QCompleter(combo);
  200. completer->setModel(combo->model());
  201. completer->setCompletionMode(QCompleter::PopupCompletion);
  202. completer->setFilterMode(Qt::MatchContains);
  203. combo->setCompleter(completer);
  204. ui->tableWidgetRequiredObjects->setCellWidget(row, 0, combo);
  205. // Column 1: Count (QSpinBox, range 1-100)
  206. QSpinBox *spinCount = new QSpinBox();
  207. spinCount->setRange(1, 100);
  208. spinCount->setValue(count);
  209. ui->tableWidgetRequiredObjects->setCellWidget(row, 1, spinCount);
  210. // Column 2: Guard level (QSpinBox, range -1-999999, -1 = no guard)
  211. QSpinBox *spinGuard = new QSpinBox();
  212. spinGuard->setRange(-1, 999999);
  213. spinGuard->setValue(guard.value_or(-1));
  214. spinGuard->setSpecialValueText(tr("None")); // Shows "None" when value is -1
  215. ui->tableWidgetRequiredObjects->setCellWidget(row, 2, spinGuard);
  216. // Column 3: Delete button
  217. auto deleteButton = new QPushButton(tr("Delete"));
  218. ui->tableWidgetRequiredObjects->setCellWidget(row, 3, deleteButton);
  219. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  220. for (int r = 0; r < ui->tableWidgetRequiredObjects->rowCount(); ++r) {
  221. if (ui->tableWidgetRequiredObjects->cellWidget(r, 3) == deleteButton) {
  222. ui->tableWidgetRequiredObjects->removeRow(r);
  223. break;
  224. }
  225. }
  226. });
  227. };
  228. int row = 0;
  229. for (const auto & [objId, info] : reqObjs)
  230. addRow(objId, info.first, info.second, row++);
  231. // Add "Add" button in last row
  232. auto addButton = new QPushButton(tr("Add"));
  233. ui->tableWidgetRequiredObjects->setCellWidget(ui->tableWidgetRequiredObjects->rowCount() - 1, 3, addButton);
  234. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  235. ui->tableWidgetRequiredObjects->insertRow(ui->tableWidgetRequiredObjects->rowCount() - 1);
  236. addRow((*advObjects.begin()).first, 1, std::nullopt, ui->tableWidgetRequiredObjects->rowCount() - 2);
  237. });
  238. ui->tableWidgetRequiredObjects->resizeColumnsToContents();
  239. ui->tableWidgetRequiredObjects->setColumnWidth(0, 400);
  240. }
  241. void ObjectSelector::getRequiredObjects()
  242. {
  243. obj.clearRequiredObjects();
  244. for (int row = 0; row < ui->tableWidgetRequiredObjects->rowCount() - 1; ++row)
  245. {
  246. auto id = static_cast<QComboBox *>(ui->tableWidgetRequiredObjects->cellWidget(row, 0))->currentData().value<CompoundMapObjectID>();
  247. auto count = static_cast<QSpinBox *>(ui->tableWidgetRequiredObjects->cellWidget(row, 1))->value();
  248. auto guardValue = static_cast<QSpinBox *>(ui->tableWidgetRequiredObjects->cellWidget(row, 2))->value();
  249. std::optional<ui32> guard = std::nullopt;
  250. if (guardValue >= 0)
  251. guard = guardValue;
  252. obj.addRequiredObject(id, count, guard);
  253. }
  254. }
  255. void ObjectSelector::fillCustomObjects()
  256. {
  257. ui->tableWidgetObjects->setColumnCount(5);
  258. ui->tableWidgetObjects->setRowCount(obj.customObjects.size() + 1);
  259. ui->tableWidgetObjects->setHorizontalHeaderLabels({tr("Object"), tr("Value"), tr("Probability"), tr("Max per zone"), tr("Action")});
  260. auto addRow = [this](CompoundMapObjectID obj, ui32 value, ui16 probability, ui32 maxPerZone, int row){
  261. QComboBox *combo = new QComboBox();
  262. for(auto & item : advObjects)
  263. combo->addItem(item.second, QVariant::fromValue(item.first));
  264. int index = combo->findData(QVariant::fromValue(obj));
  265. if (index != -1)
  266. combo->setCurrentIndex(index);
  267. combo->setEditable(true);
  268. QCompleter* completer = new QCompleter(combo);
  269. completer->setModel(combo->model());
  270. completer->setCompletionMode(QCompleter::PopupCompletion);
  271. completer->setFilterMode(Qt::MatchContains);
  272. combo->setCompleter(completer);
  273. ui->tableWidgetObjects->setCellWidget(row, 0, combo);
  274. QSpinBox *spinValue = new QSpinBox();
  275. spinValue->setRange(0, 10000);
  276. spinValue->setValue(value);
  277. ui->tableWidgetObjects->setCellWidget(row, 1, spinValue);
  278. QSpinBox *spinProbability = new QSpinBox();
  279. spinProbability->setRange(0, 1000);
  280. spinProbability->setValue(probability);
  281. ui->tableWidgetObjects->setCellWidget(row, 2, spinProbability);
  282. QSpinBox *spinMaxPerZone = new QSpinBox();
  283. spinMaxPerZone->setRange(0, 100);
  284. spinMaxPerZone->setValue(maxPerZone);
  285. ui->tableWidgetObjects->setCellWidget(row, 3, spinMaxPerZone);
  286. auto deleteButton = new QPushButton(tr("Delete"));
  287. ui->tableWidgetObjects->setCellWidget(row, 4, deleteButton);
  288. connect(deleteButton, &QPushButton::clicked, this, [this, deleteButton]() {
  289. for (int r = 0; r < ui->tableWidgetObjects->rowCount(); ++r) {
  290. if (ui->tableWidgetObjects->cellWidget(r, 4) == deleteButton) {
  291. ui->tableWidgetObjects->removeRow(r);
  292. break;
  293. }
  294. }
  295. });
  296. };
  297. for (int row = 0; row < obj.customObjects.size(); ++row)
  298. addRow(obj.customObjects[row].getCompoundID(), obj.customObjects[row].value, obj.customObjects[row].probability, obj.customObjects[row].maxPerZone, row);
  299. auto addButton = new QPushButton(tr("Add"));
  300. ui->tableWidgetObjects->setCellWidget(ui->tableWidgetObjects->rowCount() - 1, 4, addButton);
  301. connect(addButton, &QPushButton::clicked, this, [this, addRow]() {
  302. ui->tableWidgetObjects->insertRow(ui->tableWidgetObjects->rowCount() - 1);
  303. addRow((*advObjects.begin()).first, 0, 0, 1, ui->tableWidgetObjects->rowCount() - 2);
  304. });
  305. ui->tableWidgetObjects->resizeColumnsToContents();
  306. ui->tableWidgetObjects->setColumnWidth(0, 300);
  307. }
  308. void ObjectSelector::getCustomObjects()
  309. {
  310. obj.customObjects.clear();
  311. for (int row = 0; row < ui->tableWidgetObjects->rowCount() - 1; ++row)
  312. {
  313. auto id = static_cast<QComboBox *>(ui->tableWidgetObjects->cellWidget(row, 0))->currentData().value<CompoundMapObjectID>();
  314. auto value = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 1))->value();
  315. auto probability = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 2))->value();
  316. auto maxPerZone = static_cast<QSpinBox *>(ui->tableWidgetObjects->cellWidget(row, 3))->value();
  317. auto info = ObjectInfo(id);
  318. info.value = value;
  319. info.probability = probability;
  320. info.maxPerZone = maxPerZone;
  321. obj.customObjects.push_back(info);
  322. }
  323. }
  324. void ObjectSelector::showObjectSelector(ObjectConfig & obj)
  325. {
  326. auto * dialog = new ObjectSelector(obj);
  327. dialog->setAttribute(Qt::WA_DeleteOnClose);
  328. dialog->exec();
  329. }
  330. void ObjectSelector::on_buttonBoxResult_accepted()
  331. {
  332. getBannedObjectCategories();
  333. getBannedObjects();
  334. getCustomObjects();
  335. getRequiredObjects();
  336. close();
  337. }
  338. void ObjectSelector::on_buttonBoxResult_rejected()
  339. {
  340. close();
  341. }