inspector.cpp 30 KB

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