inspector.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * inspector.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 "inspector.h"
  12. #include "../lib/ArtifactUtils.h"
  13. #include "../lib/CArtHandler.h"
  14. #include "../lib/entities/hero/CHeroClass.h"
  15. #include "../lib/entities/hero/CHeroHandler.h"
  16. #include "../lib/spells/CSpellHandler.h"
  17. #include "../lib/CRandomGenerator.h"
  18. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  19. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  20. #include "../lib/mapObjects/ObjectTemplate.h"
  21. #include "../lib/mapping/CMap.h"
  22. #include "../lib/constants/StringConstants.h"
  23. #include "../lib/CPlayerState.h"
  24. #include "../lib/texts/CGeneralTextHandler.h"
  25. #include "townbuildingswidget.h"
  26. #include "towneventswidget.h"
  27. #include "townspellswidget.h"
  28. #include "armywidget.h"
  29. #include "messagewidget.h"
  30. #include "rewardswidget.h"
  31. #include "questwidget.h"
  32. #include "heroartifactswidget.h"
  33. #include "heroskillswidget.h"
  34. #include "herospellwidget.h"
  35. #include "portraitwidget.h"
  36. #include "PickObjectDelegate.h"
  37. #include "../mapcontroller.h"
  38. //===============IMPLEMENT OBJECT INITIALIZATION FUNCTIONS================
  39. Initializer::Initializer(CGObjectInstance * o, const PlayerColor & pl) : defaultPlayer(pl)
  40. {
  41. logGlobal->info("New object instance initialized");
  42. ///IMPORTANT! initialize order should be from base objects to derived objects
  43. INIT_OBJ_TYPE(CGResource);
  44. INIT_OBJ_TYPE(CGArtifact);
  45. INIT_OBJ_TYPE(CArmedInstance);
  46. INIT_OBJ_TYPE(CGShipyard);
  47. INIT_OBJ_TYPE(CGGarrison);
  48. INIT_OBJ_TYPE(CGMine);
  49. INIT_OBJ_TYPE(CGDwelling);
  50. INIT_OBJ_TYPE(CGTownInstance);
  51. INIT_OBJ_TYPE(CGCreature);
  52. INIT_OBJ_TYPE(CGHeroPlaceholder);
  53. INIT_OBJ_TYPE(CGHeroInstance);
  54. INIT_OBJ_TYPE(CGSignBottle);
  55. INIT_OBJ_TYPE(FlaggableMapObject);
  56. //INIT_OBJ_TYPE(CRewardableObject);
  57. //INIT_OBJ_TYPE(CGPandoraBox);
  58. //INIT_OBJ_TYPE(CGEvent);
  59. //INIT_OBJ_TYPE(CGSeerHut);
  60. }
  61. void Initializer::initialize(CArmedInstance * o)
  62. {
  63. if(!o) return;
  64. }
  65. void Initializer::initialize(CGSignBottle * o)
  66. {
  67. if(!o) return;
  68. }
  69. void Initializer::initialize(CGCreature * o)
  70. {
  71. if(!o) return;
  72. o->character = CGCreature::Character::HOSTILE;
  73. if(!o->hasStackAtSlot(SlotID(0)))
  74. o->putStack(SlotID(0), new CStackInstance(CreatureID(o->subID), 0, false));
  75. }
  76. void Initializer::initialize(CGDwelling * o)
  77. {
  78. if(!o) return;
  79. o->tempOwner = defaultPlayer;
  80. if(o->ID == Obj::RANDOM_DWELLING || o->ID == Obj::RANDOM_DWELLING_LVL || o->ID == Obj::RANDOM_DWELLING_FACTION)
  81. {
  82. o->randomizationInfo = CGDwellingRandomizationInfo();
  83. if(o->ID == Obj::RANDOM_DWELLING_LVL)
  84. {
  85. o->randomizationInfo->minLevel = o->subID;
  86. o->randomizationInfo->maxLevel = o->subID;
  87. }
  88. if(o->ID == Obj::RANDOM_DWELLING_FACTION)
  89. {
  90. o->randomizationInfo->allowedFactions.insert(FactionID(o->subID));
  91. }
  92. }
  93. }
  94. void Initializer::initialize(CGGarrison * o)
  95. {
  96. if(!o) return;
  97. o->tempOwner = defaultPlayer;
  98. o->removableUnits = true;
  99. }
  100. void Initializer::initialize(CGShipyard * o)
  101. {
  102. if(!o) return;
  103. o->tempOwner = defaultPlayer;
  104. }
  105. void Initializer::initialize(FlaggableMapObject * o)
  106. {
  107. if(!o) return;
  108. o->tempOwner = defaultPlayer;
  109. }
  110. void Initializer::initialize(CGHeroPlaceholder * o)
  111. {
  112. if(!o) return;
  113. o->tempOwner = defaultPlayer;
  114. if(!o->powerRank.has_value() && !o->heroType.has_value())
  115. o->powerRank = 0;
  116. if(o->powerRank.has_value() && o->heroType.has_value())
  117. o->powerRank.reset();
  118. }
  119. void Initializer::initialize(CGHeroInstance * o)
  120. {
  121. if(!o)
  122. return;
  123. o->tempOwner = defaultPlayer;
  124. if(o->ID == Obj::PRISON)
  125. {
  126. o->subID = 0;
  127. o->tempOwner = PlayerColor::NEUTRAL;
  128. }
  129. if(o->ID == Obj::HERO)
  130. {
  131. for(auto const & t : VLC->heroh->objects)
  132. {
  133. if(t->heroClass->getId() == HeroClassID(o->subID))
  134. {
  135. o->subID = t->getId();
  136. break;
  137. }
  138. }
  139. }
  140. if(o->getHeroTypeID().hasValue())
  141. {
  142. o->gender = o->getHeroType()->gender;
  143. o->randomizeArmy(o->getFactionID());
  144. }
  145. }
  146. void Initializer::initialize(CGTownInstance * o)
  147. {
  148. if(!o) return;
  149. const std::vector<std::string> castleLevels{"village", "fort", "citadel", "castle", "capitol"};
  150. int lvl = vstd::find_pos(castleLevels, o->appearance->stringID);
  151. o->addBuilding(BuildingID::DEFAULT);
  152. if(lvl > -1) o->addBuilding(BuildingID::TAVERN);
  153. if(lvl > 0) o->addBuilding(BuildingID::FORT);
  154. if(lvl > 1) o->addBuilding(BuildingID::CITADEL);
  155. if(lvl > 2) o->addBuilding(BuildingID::CASTLE);
  156. if(lvl > 3) o->addBuilding(BuildingID::CAPITOL);
  157. if(o->possibleSpells.empty())
  158. {
  159. for(auto const & spellId : VLC->spellh->getDefaultAllowed()) //add all regular spells to town
  160. {
  161. o->possibleSpells.push_back(spellId);
  162. }
  163. }
  164. }
  165. void Initializer::initialize(CGArtifact * o)
  166. {
  167. if(!o) return;
  168. if(o->ID == Obj::SPELL_SCROLL)
  169. {
  170. std::vector<SpellID> out;
  171. for(auto const & spell : VLC->spellh->objects) //spellh size appears to be greater (?)
  172. {
  173. if(VLC->spellh->getDefaultAllowed().count(spell->id) != 0)
  174. {
  175. out.push_back(spell->id);
  176. }
  177. }
  178. auto a = ArtifactUtils::createScroll(*RandomGeneratorUtil::nextItem(out, CRandomGenerator::getDefault()));
  179. o->storedArtifact = a;
  180. }
  181. }
  182. void Initializer::initialize(CGMine * o)
  183. {
  184. if(!o) return;
  185. o->tempOwner = defaultPlayer;
  186. if(o->isAbandoned())
  187. {
  188. for(auto r = GameResID(0); r < GameResID::COUNT; ++r)
  189. o->abandonedMineResources.insert(r);
  190. }
  191. else
  192. {
  193. o->producedResource = GameResID(o->subID);
  194. o->producedQuantity = o->defaultResProduction();
  195. }
  196. }
  197. void Initializer::initialize(CGResource * o)
  198. {
  199. if(!o) return;
  200. o->amount = CGResource::RANDOM_AMOUNT;
  201. }
  202. //===============IMPLEMENT PROPERTIES SETUP===============================
  203. void Inspector::updateProperties(CArmedInstance * o)
  204. {
  205. if(!o) return;
  206. auto * delegate = new ArmyDelegate(*o);
  207. addProperty(QObject::tr("Army"), PropertyEditorPlaceholder(), delegate, false);
  208. }
  209. void Inspector::updateProperties(CGDwelling * o)
  210. {
  211. if(!o) return;
  212. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  213. if (o->ID == Obj::RANDOM_DWELLING || o->ID == Obj::RANDOM_DWELLING_LVL)
  214. {
  215. auto * delegate = new PickObjectDelegate(controller, PickObjectDelegate::typedFilter<CGTownInstance>);
  216. addProperty(QObject::tr("Same as town"), o->randomizationInfo, delegate, false);
  217. }
  218. }
  219. void Inspector::updateProperties(FlaggableMapObject * o)
  220. {
  221. if(!o) return;
  222. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  223. }
  224. void Inspector::updateProperties(CGGarrison * o)
  225. {
  226. if(!o) return;
  227. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  228. addProperty(QObject::tr("Removable units"), o->removableUnits, false);
  229. }
  230. void Inspector::updateProperties(CGShipyard * o)
  231. {
  232. if(!o) return;
  233. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  234. }
  235. void Inspector::updateProperties(CGHeroPlaceholder * o)
  236. {
  237. if(!o) return;
  238. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  239. bool type = false;
  240. if(o->heroType.has_value())
  241. type = true;
  242. else if(!o->powerRank.has_value())
  243. assert(0); //one of values must be initialized
  244. {
  245. auto * delegate = new InspectorDelegate;
  246. delegate->options = {{QObject::tr("POWER RANK"), QVariant::fromValue(false)}, {QObject::tr("HERO TYPE"), QVariant::fromValue(true)}};
  247. addProperty(QObject::tr("Placeholder type"), delegate->options[type].first, delegate, false);
  248. }
  249. addProperty(QObject::tr("Power rank"), o->powerRank.has_value() ? o->powerRank.value() : 0, type);
  250. {
  251. auto * delegate = new InspectorDelegate;
  252. for(int i = 0; i < VLC->heroh->objects.size(); ++i)
  253. {
  254. delegate->options.push_back({QObject::tr(VLC->heroh->objects[i]->getNameTranslated().c_str()), QVariant::fromValue(VLC->heroh->objects[i]->getId().getNum())});
  255. }
  256. addProperty(QObject::tr("Hero type"), o->heroType.has_value() ? VLC->heroh->getById(o->heroType.value())->getNameTranslated() : "", delegate, !type);
  257. }
  258. }
  259. void Inspector::updateProperties(CGHeroInstance * o)
  260. {
  261. if(!o) return;
  262. auto isPrison = o->ID == Obj::PRISON;
  263. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller, isPrison), isPrison); //field is not editable for prison
  264. addProperty<int>(QObject::tr("Experience"), o->exp, false);
  265. addProperty(QObject::tr("Hero class"), o->getHeroClassID().hasValue() ? o->getHeroClass()->getNameTranslated() : "", true);
  266. { //Gender
  267. auto * delegate = new InspectorDelegate;
  268. delegate->options = {{QObject::tr("MALE"), QVariant::fromValue(int(EHeroGender::MALE))}, {QObject::tr("FEMALE"), QVariant::fromValue(int(EHeroGender::FEMALE))}};
  269. addProperty<std::string>(QObject::tr("Gender"), (o->gender == EHeroGender::FEMALE ? QObject::tr("FEMALE") : QObject::tr("MALE")).toStdString(), delegate , false);
  270. }
  271. addProperty(QObject::tr("Name"), o->getNameTranslated(), false);
  272. addProperty(QObject::tr("Biography"), o->getBiographyTranslated(), new MessageDelegate, false);
  273. addProperty(QObject::tr("Portrait"), PropertyEditorPlaceholder(), new PortraitDelegate(*o), false);
  274. auto * delegate = new HeroSkillsDelegate(*o);
  275. addProperty(QObject::tr("Skills"), PropertyEditorPlaceholder(), delegate, false);
  276. addProperty(QObject::tr("Spells"), PropertyEditorPlaceholder(), new HeroSpellDelegate(*o), false);
  277. addProperty(QObject::tr("Artifacts"), PropertyEditorPlaceholder(), new HeroArtifactsDelegate(*o), false);
  278. if(o->getHeroTypeID().hasValue() || o->ID == Obj::PRISON)
  279. { //Hero type
  280. auto * delegate = new InspectorDelegate;
  281. for(const auto & heroPtr : VLC->heroh->objects)
  282. {
  283. if(controller.map()->allowedHeroes.count(heroPtr->getId()) != 0)
  284. {
  285. if(o->ID == Obj::PRISON || heroPtr->heroClass->getIndex() == o->getHeroClassID())
  286. {
  287. delegate->options.push_back({QObject::tr(heroPtr->getNameTranslated().c_str()), QVariant::fromValue(heroPtr->getIndex())});
  288. }
  289. }
  290. }
  291. addProperty(QObject::tr("Hero type"), o->getHeroTypeID().hasValue() ? o->getHeroType()->getNameTranslated() : "", delegate, false);
  292. }
  293. {
  294. const int maxRadius = 60;
  295. auto * patrolDelegate = new InspectorDelegate;
  296. patrolDelegate->options = { {QObject::tr("No patrol"), QVariant::fromValue(CGHeroInstance::NO_PATROLLING)} };
  297. for(int i = 0; i <= maxRadius; ++i)
  298. patrolDelegate->options.push_back({ QObject::tr("%n tile(s)", "", i), QVariant::fromValue(i)});
  299. auto patrolRadiusText = o->patrol.patrolling ? QObject::tr("%n tile(s)", "", o->patrol.patrolRadius) : QObject::tr("No patrol");
  300. addProperty(QObject::tr("Patrol radius"), patrolRadiusText, patrolDelegate, false);
  301. }
  302. }
  303. void Inspector::updateProperties(CGTownInstance * o)
  304. {
  305. if(!o) return;
  306. addProperty(QObject::tr("Town name"), o->getNameTranslated(), false);
  307. auto * delegate = new TownBuildingsDelegate(*o);
  308. addProperty(QObject::tr("Buildings"), PropertyEditorPlaceholder(), delegate, false);
  309. addProperty(QObject::tr("Spells"), PropertyEditorPlaceholder(), new TownSpellsDelegate(*o), false);
  310. addProperty(QObject::tr("Events"), PropertyEditorPlaceholder(), new TownEventsDelegate(*o, controller), false);
  311. if(o->ID == Obj::RANDOM_TOWN)
  312. addProperty(QObject::tr("Same as player"), o->alignmentToPlayer, new OwnerDelegate(controller), false);
  313. }
  314. void Inspector::updateProperties(CGArtifact * o)
  315. {
  316. if(!o) return;
  317. addProperty(QObject::tr("Message"), o->message, false);
  318. CArtifactInstance * instance = o->storedArtifact;
  319. if(instance)
  320. {
  321. SpellID spellId = instance->getScrollSpellID();
  322. if(spellId != SpellID::NONE)
  323. {
  324. auto * delegate = new InspectorDelegate;
  325. for(auto const & spell : VLC->spellh->objects)
  326. {
  327. if(controller.map()->allowedSpells.count(spell->id) != 0)
  328. delegate->options.push_back({QObject::tr(spell->getNameTranslated().c_str()), QVariant::fromValue(int(spell->getId()))});
  329. }
  330. addProperty(QObject::tr("Spell"), VLC->spellh->getById(spellId)->getNameTranslated(), delegate, false);
  331. }
  332. }
  333. }
  334. void Inspector::updateProperties(CGMine * o)
  335. {
  336. if(!o) return;
  337. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  338. addProperty(QObject::tr("Resource"), o->producedResource);
  339. addProperty(QObject::tr("Productivity"), o->producedQuantity);
  340. }
  341. void Inspector::updateProperties(CGResource * o)
  342. {
  343. if(!o) return;
  344. addProperty(QObject::tr("Amount"), o->amount, false);
  345. addProperty(QObject::tr("Message"), o->message, false);
  346. }
  347. void Inspector::updateProperties(CGSignBottle * o)
  348. {
  349. if(!o) return;
  350. addProperty(QObject::tr("Message"), o->message, new MessageDelegate, false);
  351. }
  352. void Inspector::updateProperties(CGCreature * o)
  353. {
  354. if(!o) return;
  355. addProperty(QObject::tr("Message"), o->message, false);
  356. { //Character
  357. auto * delegate = new InspectorDelegate;
  358. delegate->options = characterIdentifiers;
  359. addProperty<CGCreature::Character>("Character", (CGCreature::Character)o->character, delegate, false);
  360. }
  361. addProperty(QObject::tr("Never flees"), o->neverFlees, false);
  362. addProperty(QObject::tr("Not growing"), o->notGrowingTeam, false);
  363. addProperty(QObject::tr("Artifact reward"), o->gainedArtifact); //TODO: implement in setProperty
  364. addProperty(QObject::tr("Army"), PropertyEditorPlaceholder(), true);
  365. addProperty(QObject::tr("Amount"), o->stacks[SlotID(0)]->count, false);
  366. //addProperty(QObject::tr("Resources reward"), o->resources); //TODO: implement in setProperty
  367. }
  368. void Inspector::updateProperties(CRewardableObject * o)
  369. {
  370. if(!o) return;
  371. auto * delegate = new RewardsDelegate(*controller.map(), *o);
  372. addProperty(QObject::tr("Reward"), PropertyEditorPlaceholder(), delegate, false);
  373. }
  374. void Inspector::updateProperties(CGPandoraBox * o)
  375. {
  376. if(!o) return;
  377. addProperty(QObject::tr("Message"), o->message, new MessageDelegate, false);
  378. }
  379. void Inspector::updateProperties(CGEvent * o)
  380. {
  381. if(!o) return;
  382. addProperty(QObject::tr("Remove after"), o->removeAfterVisit, false);
  383. addProperty(QObject::tr("Human trigger"), o->humanActivate, false);
  384. addProperty(QObject::tr("Cpu trigger"), o->computerActivate, false);
  385. //ui8 availableFor; //players whom this event is available for
  386. }
  387. void Inspector::updateProperties(CGSeerHut * o)
  388. {
  389. if(!o || !o->quest) return;
  390. addProperty(QObject::tr("First visit text"), o->quest->firstVisitText, new MessageDelegate, false);
  391. addProperty(QObject::tr("Next visit text"), o->quest->nextVisitText, new MessageDelegate, false);
  392. addProperty(QObject::tr("Completed text"), o->quest->completedText, new MessageDelegate, false);
  393. addProperty(QObject::tr("Repeat quest"), o->quest->repeatedQuest, false);
  394. addProperty(QObject::tr("Time limit"), o->quest->lastDay, false);
  395. { //Quest
  396. auto * delegate = new QuestDelegate(controller, *o->quest);
  397. addProperty(QObject::tr("Quest"), PropertyEditorPlaceholder(), delegate, false);
  398. }
  399. }
  400. void Inspector::updateProperties(CGQuestGuard * o)
  401. {
  402. if(!o || !o->quest) return;
  403. addProperty(QObject::tr("Reward"), PropertyEditorPlaceholder(), nullptr, true);
  404. addProperty(QObject::tr("Repeat quest"), o->quest->repeatedQuest, true);
  405. }
  406. void Inspector::updateProperties()
  407. {
  408. if(!obj)
  409. return;
  410. table->setRowCount(0); //cleanup table
  411. addProperty(QObject::tr("Identifier"), obj);
  412. addProperty(QObject::tr("ID"), obj->ID.getNum());
  413. addProperty(QObject::tr("SubID"), obj->subID);
  414. addProperty(QObject::tr("InstanceName"), obj->instanceName);
  415. if(obj->ID != Obj::HERO_PLACEHOLDER && !dynamic_cast<CGHeroInstance*>(obj))
  416. {
  417. auto factory = VLC->objtypeh->getHandlerFor(obj->ID, obj->subID);
  418. addProperty(QObject::tr("IsStatic"), factory->isStaticObject());
  419. }
  420. addProperty(QObject::tr("Owner"), obj->tempOwner, new OwnerDelegate(controller), true);
  421. UPDATE_OBJ_PROPERTIES(CArmedInstance);
  422. UPDATE_OBJ_PROPERTIES(CGResource);
  423. UPDATE_OBJ_PROPERTIES(CGArtifact);
  424. UPDATE_OBJ_PROPERTIES(CGMine);
  425. UPDATE_OBJ_PROPERTIES(CGGarrison);
  426. UPDATE_OBJ_PROPERTIES(CGShipyard);
  427. UPDATE_OBJ_PROPERTIES(CGDwelling);
  428. UPDATE_OBJ_PROPERTIES(CGTownInstance);
  429. UPDATE_OBJ_PROPERTIES(CGCreature);
  430. UPDATE_OBJ_PROPERTIES(CGHeroPlaceholder);
  431. UPDATE_OBJ_PROPERTIES(CGHeroInstance);
  432. UPDATE_OBJ_PROPERTIES(CGSignBottle);
  433. UPDATE_OBJ_PROPERTIES(FlaggableMapObject);
  434. UPDATE_OBJ_PROPERTIES(CRewardableObject);
  435. UPDATE_OBJ_PROPERTIES(CGPandoraBox);
  436. UPDATE_OBJ_PROPERTIES(CGEvent);
  437. UPDATE_OBJ_PROPERTIES(CGSeerHut);
  438. UPDATE_OBJ_PROPERTIES(CGQuestGuard);
  439. table->show();
  440. }
  441. //===============IMPLEMENT PROPERTY UPDATE================================
  442. void Inspector::setProperty(const QString & key, const QTableWidgetItem * item)
  443. {
  444. if(!item->data(Qt::UserRole).isNull())
  445. {
  446. setProperty(key, item->data(Qt::UserRole));
  447. return;
  448. }
  449. if(item->flags() & Qt::ItemIsUserCheckable)
  450. {
  451. setProperty(key, QVariant::fromValue(item->checkState() == Qt::Checked));
  452. return;
  453. }
  454. setProperty(key, item->text());
  455. }
  456. void Inspector::setProperty(const QString & key, const QVariant & value)
  457. {
  458. if(!obj)
  459. return;
  460. if(key == QObject::tr("Owner"))
  461. obj->tempOwner = PlayerColor(value.toInt());
  462. SET_PROPERTIES(CArmedInstance);
  463. SET_PROPERTIES(CGTownInstance);
  464. SET_PROPERTIES(CGArtifact);
  465. SET_PROPERTIES(CGMine);
  466. SET_PROPERTIES(CGResource);
  467. SET_PROPERTIES(CGDwelling);
  468. SET_PROPERTIES(CGGarrison);
  469. SET_PROPERTIES(CGCreature);
  470. SET_PROPERTIES(CGHeroPlaceholder);
  471. SET_PROPERTIES(CGHeroInstance);
  472. SET_PROPERTIES(CGShipyard);
  473. SET_PROPERTIES(CGSignBottle);
  474. SET_PROPERTIES(FlaggableMapObject);
  475. SET_PROPERTIES(CRewardableObject);
  476. SET_PROPERTIES(CGPandoraBox);
  477. SET_PROPERTIES(CGEvent);
  478. SET_PROPERTIES(CGSeerHut);
  479. SET_PROPERTIES(CGQuestGuard);
  480. }
  481. void Inspector::setProperty(CArmedInstance * o, const QString & key, const QVariant & value)
  482. {
  483. if(!o) return;
  484. }
  485. void Inspector::setProperty(FlaggableMapObject * o, const QString & key, const QVariant & value)
  486. {
  487. if(!o) return;
  488. }
  489. void Inspector::setProperty(CRewardableObject * o, const QString & key, const QVariant & value)
  490. {
  491. if(!o) return;
  492. }
  493. void Inspector::setProperty(CGPandoraBox * o, const QString & key, const QVariant & value)
  494. {
  495. if(!o) return;
  496. if(key == QObject::tr("Message"))
  497. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  498. TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  499. }
  500. void Inspector::setProperty(CGEvent * o, const QString & key, const QVariant & value)
  501. {
  502. if(!o) return;
  503. if(key == QObject::tr("Remove after"))
  504. o->removeAfterVisit = value.toBool();
  505. if(key == QObject::tr("Human trigger"))
  506. o->humanActivate = value.toBool();
  507. if(key == QObject::tr("Cpu trigger"))
  508. o->computerActivate = value.toBool();
  509. }
  510. void Inspector::setProperty(CGTownInstance * o, const QString & key, const QVariant & value)
  511. {
  512. if(!o) return;
  513. if(key == QObject::tr("Town name"))
  514. o->setNameTextId(mapRegisterLocalizedString("map", *controller.map(),
  515. TextIdentifier("town", o->instanceName, "name"), value.toString().toStdString()));
  516. if(key == QObject::tr("Same as player"))
  517. o->alignmentToPlayer = PlayerColor(value.toInt());
  518. }
  519. void Inspector::setProperty(CGSignBottle * o, const QString & key, const QVariant & value)
  520. {
  521. if(!o) return;
  522. if(key == QObject::tr("Message"))
  523. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  524. TextIdentifier("sign", o->instanceName, "message"), value.toString().toStdString()));
  525. }
  526. void Inspector::setProperty(CGMine * o, const QString & key, const QVariant & value)
  527. {
  528. if(!o) return;
  529. if(key == QObject::tr("Productivity"))
  530. o->producedQuantity = value.toString().toInt();
  531. }
  532. void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant & value)
  533. {
  534. if(!o) return;
  535. if(key == QObject::tr("Message"))
  536. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  537. TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  538. if(o->storedArtifact && key == QObject::tr("Spell"))
  539. {
  540. o->storedArtifact = ArtifactUtils::createScroll(SpellID(value.toInt()));
  541. }
  542. }
  543. void Inspector::setProperty(CGDwelling * o, const QString & key, const QVariant & value)
  544. {
  545. if(!o) return;
  546. if(key == QObject::tr("Same as town"))
  547. {
  548. if (!o->randomizationInfo.has_value())
  549. o->randomizationInfo = CGDwellingRandomizationInfo();
  550. o->randomizationInfo->instanceId = "";
  551. if(CGTownInstance * town = data_cast<CGTownInstance>(value.toLongLong()))
  552. o->randomizationInfo->instanceId = town->instanceName;
  553. }
  554. }
  555. void Inspector::setProperty(CGGarrison * o, const QString & key, const QVariant & value)
  556. {
  557. if(!o) return;
  558. if(key == QObject::tr("Removable units"))
  559. o->removableUnits = value.toBool();
  560. }
  561. void Inspector::setProperty(CGHeroPlaceholder * o, const QString & key, const QVariant & value)
  562. {
  563. if(!o) return;
  564. if(key == QObject::tr("Placeholder type"))
  565. {
  566. if(value.toBool())
  567. {
  568. if(!o->heroType.has_value())
  569. o->heroType = HeroTypeID(0);
  570. o->powerRank.reset();
  571. }
  572. else
  573. {
  574. if(!o->powerRank.has_value())
  575. o->powerRank = 0;
  576. o->heroType.reset();
  577. }
  578. updateProperties();
  579. }
  580. if(key == QObject::tr("Power rank"))
  581. o->powerRank = value.toInt();
  582. if(key == QObject::tr("Hero type"))
  583. {
  584. o->heroType = HeroTypeID(value.toInt());
  585. }
  586. }
  587. void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVariant & value)
  588. {
  589. if(!o) return;
  590. if(key == QObject::tr("Gender"))
  591. o->gender = EHeroGender(value.toInt());
  592. if(key == QObject::tr("Name"))
  593. o->nameCustomTextId = mapRegisterLocalizedString("map", *controller.map(),
  594. TextIdentifier("hero", o->instanceName, "name"), value.toString().toStdString());
  595. if(key == QObject::tr("Biography"))
  596. o->biographyCustomTextId = mapRegisterLocalizedString("map", *controller.map(),
  597. TextIdentifier("hero", o->instanceName, "biography"), value.toString().toStdString());
  598. if(key == QObject::tr("Experience"))
  599. o->exp = value.toString().toInt();
  600. if(key == QObject::tr("Hero type"))
  601. {
  602. for(auto const & t : VLC->heroh->objects)
  603. {
  604. if(t->getId() == value.toInt())
  605. o->subID = value.toInt();
  606. }
  607. o->gender = o->getHeroType()->gender;
  608. o->randomizeArmy(o->getHeroType()->heroClass->faction);
  609. updateProperties(); //updating other properties after change
  610. }
  611. if(key == QObject::tr("Patrol radius"))
  612. {
  613. auto radius = value.toInt();
  614. o->patrol.patrolRadius = radius;
  615. o->patrol.patrolling = radius != CGHeroInstance::NO_PATROLLING;
  616. }
  617. }
  618. void Inspector::setProperty(CGShipyard * o, const QString & key, const QVariant & value)
  619. {
  620. if(!o) return;
  621. }
  622. void Inspector::setProperty(CGResource * o, const QString & key, const QVariant & value)
  623. {
  624. if(!o) return;
  625. if(key == QObject::tr("Amount"))
  626. o->amount = value.toString().toInt();
  627. }
  628. void Inspector::setProperty(CGCreature * o, const QString & key, const QVariant & value)
  629. {
  630. if(!o) return;
  631. if(key == QObject::tr("Message"))
  632. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  633. TextIdentifier("monster", o->instanceName, "message"), value.toString().toStdString()));
  634. if(key == QObject::tr("Character"))
  635. o->character = CGCreature::Character(value.toInt());
  636. if(key == QObject::tr("Never flees"))
  637. o->neverFlees = value.toBool();
  638. if(key == QObject::tr("Not growing"))
  639. o->notGrowingTeam = value.toBool();
  640. if(key == QObject::tr("Amount"))
  641. o->stacks[SlotID(0)]->count = value.toString().toInt();
  642. }
  643. void Inspector::setProperty(CGSeerHut * o, const QString & key, const QVariant & value)
  644. {
  645. if(!o) return;
  646. if(key == QObject::tr("First visit text"))
  647. o->quest->firstVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  648. TextIdentifier("quest", o->instanceName, "firstVisit"), value.toString().toStdString()));
  649. if(key == QObject::tr("Next visit text"))
  650. o->quest->nextVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  651. TextIdentifier("quest", o->instanceName, "nextVisit"), value.toString().toStdString()));
  652. if(key == QObject::tr("Completed text"))
  653. o->quest->completedText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  654. TextIdentifier("quest", o->instanceName, "completed"), value.toString().toStdString()));
  655. if(key == QObject::tr("Repeat quest"))
  656. o->quest->repeatedQuest = value.toBool();
  657. if(key == QObject::tr("Time limit"))
  658. o->quest->lastDay = value.toString().toInt();
  659. }
  660. void Inspector::setProperty(CGQuestGuard * o, const QString & key, const QVariant & value)
  661. {
  662. if(!o) return;
  663. }
  664. //===============IMPLEMENT PROPERTY VALUE TYPE============================
  665. QTableWidgetItem * Inspector::addProperty(CGObjectInstance * value)
  666. {
  667. auto * item = new QTableWidgetItem(QString::number(data_cast<CGObjectInstance>(value)));
  668. item->setFlags(Qt::NoItemFlags);
  669. return item;
  670. }
  671. QTableWidgetItem * Inspector::addProperty(Inspector::PropertyEditorPlaceholder value)
  672. {
  673. auto item = new QTableWidgetItem("...");
  674. item->setFlags(Qt::NoItemFlags);
  675. return item;
  676. }
  677. QTableWidgetItem * Inspector::addProperty(unsigned int value)
  678. {
  679. auto * item = new QTableWidgetItem(QString::number(value));
  680. item->setFlags(Qt::NoItemFlags);
  681. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  682. return item;
  683. }
  684. QTableWidgetItem * Inspector::addProperty(int value)
  685. {
  686. auto * item = new QTableWidgetItem(QString::number(value));
  687. item->setFlags(Qt::NoItemFlags);
  688. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  689. return item;
  690. }
  691. QTableWidgetItem * Inspector::addProperty(bool value)
  692. {
  693. auto item = new QTableWidgetItem;
  694. item->setFlags(Qt::ItemIsUserCheckable);
  695. item->setCheckState(value ? Qt::Checked : Qt::Unchecked);
  696. return item;
  697. }
  698. QTableWidgetItem * Inspector::addProperty(const std::string & value)
  699. {
  700. return addProperty(QString::fromStdString(value));
  701. }
  702. QTableWidgetItem * Inspector::addProperty(const TextIdentifier & value)
  703. {
  704. return addProperty(VLC->generaltexth->translate(value.get()));
  705. }
  706. QTableWidgetItem * Inspector::addProperty(const MetaString & value)
  707. {
  708. return addProperty(value.toString());
  709. }
  710. QTableWidgetItem * Inspector::addProperty(const QString & value)
  711. {
  712. auto * item = new QTableWidgetItem(value);
  713. item->setFlags(Qt::NoItemFlags);
  714. item->setToolTip(value);
  715. return item;
  716. }
  717. QTableWidgetItem * Inspector::addProperty(const int3 & value)
  718. {
  719. auto * item = new QTableWidgetItem(QString("(%1, %2, %3)").arg(value.x, value.y, value.z));
  720. item->setFlags(Qt::NoItemFlags);
  721. return item;
  722. }
  723. QTableWidgetItem * Inspector::addProperty(const PlayerColor & value)
  724. {
  725. auto str = QObject::tr("UNFLAGGABLE");
  726. if(value == PlayerColor::NEUTRAL)
  727. str = QObject::tr("neutral");
  728. MetaString playerStr;
  729. playerStr.appendName(value);
  730. if(value.isValidPlayer())
  731. str = QString::fromStdString(playerStr.toString());
  732. auto * item = new QTableWidgetItem(str);
  733. item->setFlags(Qt::NoItemFlags);
  734. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  735. return item;
  736. }
  737. QTableWidgetItem * Inspector::addProperty(const GameResID & value)
  738. {
  739. MetaString str;
  740. str.appendName(value);
  741. auto * item = new QTableWidgetItem(QString::fromStdString(str.toString()));
  742. item->setFlags(Qt::NoItemFlags);
  743. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  744. return item;
  745. }
  746. QTableWidgetItem * Inspector::addProperty(CGCreature::Character value)
  747. {
  748. auto * item = new QTableWidgetItem;
  749. item->setFlags(Qt::NoItemFlags);
  750. item->setData(Qt::UserRole, QVariant::fromValue(int(value)));
  751. for(auto & i : characterIdentifiers)
  752. {
  753. if(i.second.toInt() == value)
  754. {
  755. item->setText(i.first);
  756. break;
  757. }
  758. }
  759. return item;
  760. }
  761. QTableWidgetItem * Inspector::addProperty(const std::optional<CGDwellingRandomizationInfo> & value)
  762. {
  763. QString text = QObject::tr("Select town");
  764. if(value.has_value() && !value->instanceId.empty())
  765. text = QString::fromStdString(value->instanceId);
  766. auto * item = new QTableWidgetItem(text);
  767. item->setFlags(Qt::NoItemFlags);
  768. return item;
  769. }
  770. //========================================================================
  771. Inspector::Inspector(MapController & c, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), controller(c)
  772. {
  773. characterIdentifiers = {
  774. { QObject::tr("Compliant"), QVariant::fromValue(int(CGCreature::Character::COMPLIANT)) },
  775. { QObject::tr("Friendly"), QVariant::fromValue(int(CGCreature::Character::FRIENDLY)) },
  776. { QObject::tr("Aggressive"), QVariant::fromValue(int(CGCreature::Character::AGGRESSIVE)) },
  777. { QObject::tr("Hostile"), QVariant::fromValue(int(CGCreature::Character::HOSTILE)) },
  778. { QObject::tr("Savage"), QVariant::fromValue(int(CGCreature::Character::SAVAGE)) },
  779. };
  780. }
  781. /*
  782. * Delegates
  783. */
  784. QWidget * InspectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  785. {
  786. return new QComboBox(parent);
  787. }
  788. void InspectorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  789. {
  790. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  791. {
  792. for(auto & i : options)
  793. {
  794. ed->addItem(i.first);
  795. ed->setItemData(ed->count() - 1, i.second);
  796. }
  797. }
  798. else
  799. {
  800. QStyledItemDelegate::setEditorData(editor, index);
  801. }
  802. }
  803. void InspectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  804. {
  805. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  806. {
  807. if(!options.isEmpty())
  808. {
  809. QMap<int, QVariant> data;
  810. data[Qt::DisplayRole] = options[ed->currentIndex()].first;
  811. data[Qt::UserRole] = options[ed->currentIndex()].second;
  812. model->setItemData(index, data);
  813. }
  814. }
  815. else
  816. {
  817. QStyledItemDelegate::setModelData(editor, model, index);
  818. }
  819. }
  820. OwnerDelegate::OwnerDelegate(MapController & controller, bool addNeutral)
  821. {
  822. if(addNeutral)
  823. options.push_back({QObject::tr("neutral"), QVariant::fromValue(PlayerColor::NEUTRAL.getNum()) });
  824. for(int p = 0; p < controller.map()->players.size(); ++p)
  825. if(controller.map()->players[p].canAnyonePlay())
  826. {
  827. MetaString str;
  828. str.appendName(PlayerColor(p));
  829. options.push_back({QString::fromStdString(str.toString()), QVariant::fromValue(PlayerColor(p).getNum()) });
  830. }
  831. }