inspector.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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->character = 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 || (o->ID >= Obj::RANDOM_ART && o->ID <= Obj::RANDOM_RELIC_ART))
  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. const CArtifactInstance * instance = o->getArtifactInstance();
  326. if(instance && o->ID == Obj::SPELL_SCROLL)
  327. {
  328. SpellID spellId = instance->getScrollSpellID();
  329. if(spellId != SpellID::NONE)
  330. {
  331. auto * delegate = new InspectorDelegate;
  332. for(auto const & spell : LIBRARY->spellh->objects)
  333. {
  334. if(controller.map()->allowedSpells.count(spell->id) != 0)
  335. delegate->options.push_back({QObject::tr(spell->getNameTranslated().c_str()), QVariant::fromValue(int(spell->getId()))});
  336. }
  337. addProperty(QObject::tr("Spell"), LIBRARY->spellh->getById(spellId)->getNameTranslated(), delegate, false);
  338. }
  339. }
  340. }
  341. void Inspector::updateProperties(CGMine * o)
  342. {
  343. if(!o) return;
  344. addProperty(QObject::tr("Owner"), o->tempOwner, new OwnerDelegate(controller), false);
  345. if(o->producedResource != GameResID::NONE)
  346. addProperty(QObject::tr("Resource"), o->producedResource);
  347. addProperty(QObject::tr("Productivity"), o->producedQuantity);
  348. }
  349. void Inspector::updateProperties(CGResource * o)
  350. {
  351. if(!o) return;
  352. addProperty(QObject::tr("Amount"), o->amount, false);
  353. addProperty(QObject::tr("Message"), o->message, false);
  354. }
  355. void Inspector::updateProperties(CGSignBottle * o)
  356. {
  357. if(!o) return;
  358. addProperty(QObject::tr("Message"), o->message, new MessageDelegate, false);
  359. }
  360. void Inspector::updateProperties(CGCreature * o)
  361. {
  362. if(!o) return;
  363. addProperty(QObject::tr("Message"), o->message, false);
  364. { //Character
  365. auto * delegate = new InspectorDelegate;
  366. delegate->options = characterIdentifiers;
  367. addProperty<CGCreature::Character>("Character", (CGCreature::Character)o->character, delegate, false);
  368. }
  369. addProperty(QObject::tr("Never flees"), o->neverFlees, false);
  370. addProperty(QObject::tr("Not growing"), o->notGrowingTeam, false);
  371. addProperty(QObject::tr("Artifact reward"), o->gainedArtifact); //TODO: implement in setProperty
  372. addProperty(QObject::tr("Army"), PropertyEditorPlaceholder(), true);
  373. addProperty(QObject::tr("Amount"), o->stacks[SlotID(0)]->getCount(), false);
  374. //addProperty(QObject::tr("Resources reward"), o->resources); //TODO: implement in setProperty
  375. }
  376. void Inspector::updateProperties(CRewardableObject * o)
  377. {
  378. if(!o) return;
  379. auto * delegate = new RewardsDelegate(*controller.map(), *o);
  380. addProperty(QObject::tr("Reward"), PropertyEditorPlaceholder(), delegate, false);
  381. }
  382. void Inspector::updateProperties(CGPandoraBox * o)
  383. {
  384. if(!o) return;
  385. addProperty(QObject::tr("Message"), o->message, new MessageDelegate, false);
  386. }
  387. void Inspector::updateProperties(CGEvent * o)
  388. {
  389. if(!o) return;
  390. addProperty(QObject::tr("Remove after"), o->removeAfterVisit, false);
  391. addProperty(QObject::tr("Human trigger"), o->humanActivate, false);
  392. addProperty(QObject::tr("Cpu trigger"), o->computerActivate, false);
  393. //ui8 availableFor; //players whom this event is available for
  394. }
  395. void Inspector::updateProperties(CGSeerHut * o)
  396. {
  397. if(!o) return;
  398. addProperty(QObject::tr("First visit text"), o->getQuest().firstVisitText, new MessageDelegate, false);
  399. addProperty(QObject::tr("Next visit text"), o->getQuest().nextVisitText, new MessageDelegate, false);
  400. addProperty(QObject::tr("Completed text"), o->getQuest().completedText, new MessageDelegate, false);
  401. addProperty(QObject::tr("Repeat quest"), o->getQuest().repeatedQuest, false);
  402. addProperty(QObject::tr("Time limit"), o->getQuest().lastDay, false);
  403. { //Quest
  404. auto * delegate = new QuestDelegate(controller, o->getQuest());
  405. addProperty(QObject::tr("Quest"), PropertyEditorPlaceholder(), delegate, false);
  406. }
  407. }
  408. void Inspector::updateProperties(CGQuestGuard * o)
  409. {
  410. if(!o) return;
  411. addProperty(QObject::tr("Reward"), PropertyEditorPlaceholder(), nullptr, true);
  412. addProperty(QObject::tr("Repeat quest"), o->getQuest().repeatedQuest, true);
  413. }
  414. void Inspector::updateProperties()
  415. {
  416. if(!obj)
  417. return;
  418. table->setRowCount(0); //cleanup table
  419. addProperty(QObject::tr("Identifier"), obj);
  420. addProperty(QObject::tr("ID"), obj->ID.getNum());
  421. addProperty(QObject::tr("SubID"), obj->subID);
  422. addProperty(QObject::tr("InstanceName"), obj->instanceName);
  423. if(obj->ID != Obj::HERO_PLACEHOLDER && !dynamic_cast<CGHeroInstance*>(obj))
  424. {
  425. auto factory = LIBRARY->objtypeh->getHandlerFor(obj->ID, obj->subID);
  426. addProperty(QObject::tr("IsStatic"), factory->isStaticObject());
  427. }
  428. addProperty(QObject::tr("Owner"), obj->tempOwner, new OwnerDelegate(controller), true);
  429. UPDATE_OBJ_PROPERTIES(CArmedInstance);
  430. UPDATE_OBJ_PROPERTIES(CGResource);
  431. UPDATE_OBJ_PROPERTIES(CGArtifact);
  432. UPDATE_OBJ_PROPERTIES(CGMine);
  433. UPDATE_OBJ_PROPERTIES(CGGarrison);
  434. UPDATE_OBJ_PROPERTIES(CGShipyard);
  435. UPDATE_OBJ_PROPERTIES(CGDwelling);
  436. UPDATE_OBJ_PROPERTIES(CGTownInstance);
  437. UPDATE_OBJ_PROPERTIES(CGCreature);
  438. UPDATE_OBJ_PROPERTIES(CGHeroPlaceholder);
  439. UPDATE_OBJ_PROPERTIES(CGHeroInstance);
  440. UPDATE_OBJ_PROPERTIES(CGSignBottle);
  441. UPDATE_OBJ_PROPERTIES(FlaggableMapObject);
  442. UPDATE_OBJ_PROPERTIES(CRewardableObject);
  443. UPDATE_OBJ_PROPERTIES(CGPandoraBox);
  444. UPDATE_OBJ_PROPERTIES(CGEvent);
  445. UPDATE_OBJ_PROPERTIES(CGSeerHut);
  446. UPDATE_OBJ_PROPERTIES(CGQuestGuard);
  447. table->show();
  448. }
  449. //===============IMPLEMENT PROPERTY UPDATE================================
  450. void Inspector::setProperty(const QString & key, const QTableWidgetItem * item)
  451. {
  452. if(!item->data(Qt::UserRole).isNull())
  453. {
  454. setProperty(key, item->data(Qt::UserRole));
  455. return;
  456. }
  457. if(item->flags() & Qt::ItemIsUserCheckable)
  458. {
  459. setProperty(key, QVariant::fromValue(item->checkState() == Qt::Checked));
  460. return;
  461. }
  462. setProperty(key, item->text());
  463. }
  464. void Inspector::setProperty(const QString & key, const QVariant & value)
  465. {
  466. if(!obj)
  467. return;
  468. if(key == QObject::tr("Owner"))
  469. obj->tempOwner = PlayerColor(value.toInt());
  470. SET_PROPERTIES(CArmedInstance);
  471. SET_PROPERTIES(CGTownInstance);
  472. SET_PROPERTIES(CGArtifact);
  473. SET_PROPERTIES(CGMine);
  474. SET_PROPERTIES(CGResource);
  475. SET_PROPERTIES(CGDwelling);
  476. SET_PROPERTIES(CGGarrison);
  477. SET_PROPERTIES(CGCreature);
  478. SET_PROPERTIES(CGHeroPlaceholder);
  479. SET_PROPERTIES(CGHeroInstance);
  480. SET_PROPERTIES(CGShipyard);
  481. SET_PROPERTIES(CGSignBottle);
  482. SET_PROPERTIES(FlaggableMapObject);
  483. SET_PROPERTIES(CRewardableObject);
  484. SET_PROPERTIES(CGPandoraBox);
  485. SET_PROPERTIES(CGEvent);
  486. SET_PROPERTIES(CGSeerHut);
  487. SET_PROPERTIES(CGQuestGuard);
  488. }
  489. void Inspector::setProperty(CArmedInstance * o, const QString & key, const QVariant & value)
  490. {
  491. if(!o) return;
  492. }
  493. void Inspector::setProperty(FlaggableMapObject * o, const QString & key, const QVariant & value)
  494. {
  495. if(!o) return;
  496. }
  497. void Inspector::setProperty(CRewardableObject * o, const QString & key, const QVariant & value)
  498. {
  499. if(!o) return;
  500. }
  501. void Inspector::setProperty(CGPandoraBox * o, const QString & key, const QVariant & value)
  502. {
  503. if(!o) return;
  504. if(key == QObject::tr("Message"))
  505. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  506. TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  507. }
  508. void Inspector::setProperty(CGEvent * o, const QString & key, const QVariant & value)
  509. {
  510. if(!o) return;
  511. if(key == QObject::tr("Remove after"))
  512. o->removeAfterVisit = value.toBool();
  513. if(key == QObject::tr("Human trigger"))
  514. o->humanActivate = value.toBool();
  515. if(key == QObject::tr("Cpu trigger"))
  516. o->computerActivate = value.toBool();
  517. }
  518. void Inspector::setProperty(CGTownInstance * o, const QString & key, const QVariant & value)
  519. {
  520. if(!o) return;
  521. if(key == QObject::tr("Town name"))
  522. o->setNameTextId(mapRegisterLocalizedString("map", *controller.map(),
  523. TextIdentifier("town", o->instanceName, "name"), value.toString().toStdString()));
  524. if(key == QObject::tr("Same as player"))
  525. o->alignmentToPlayer = PlayerColor(value.toInt());
  526. }
  527. void Inspector::setProperty(CGSignBottle * o, const QString & key, const QVariant & value)
  528. {
  529. if(!o) return;
  530. if(key == QObject::tr("Message"))
  531. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  532. TextIdentifier("sign", o->instanceName, "message"), value.toString().toStdString()));
  533. }
  534. void Inspector::setProperty(CGMine * o, const QString & key, const QVariant & value)
  535. {
  536. if(!o) return;
  537. if(key == QObject::tr("Productivity"))
  538. o->producedQuantity = value.toString().toInt();
  539. }
  540. void Inspector::setProperty(CGArtifact * o, const QString & key, const QVariant & value)
  541. {
  542. if(!o) return;
  543. if(key == QObject::tr("Message"))
  544. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  545. TextIdentifier("guards", o->instanceName, "message"), value.toString().toStdString()));
  546. if(key == QObject::tr("Spell"))
  547. {
  548. o->setArtifactInstance(controller.map()->createScroll(SpellID(value.toInt())));
  549. }
  550. }
  551. void Inspector::setProperty(CGDwelling * o, const QString & key, const QVariant & value)
  552. {
  553. if(!o) return;
  554. if(key == QObject::tr("Same as town"))
  555. {
  556. if (!o->randomizationInfo.has_value())
  557. o->randomizationInfo = CGDwellingRandomizationInfo();
  558. o->randomizationInfo->instanceId = "";
  559. if(CGTownInstance * town = data_cast<CGTownInstance>(value.toLongLong()))
  560. o->randomizationInfo->instanceId = town->instanceName;
  561. }
  562. }
  563. void Inspector::setProperty(CGGarrison * o, const QString & key, const QVariant & value)
  564. {
  565. if(!o) return;
  566. if(key == QObject::tr("Removable units"))
  567. o->removableUnits = value.toBool();
  568. }
  569. void Inspector::setProperty(CGHeroPlaceholder * o, const QString & key, const QVariant & value)
  570. {
  571. if(!o) return;
  572. if(key == QObject::tr("Placeholder type"))
  573. {
  574. if(value.toBool())
  575. {
  576. if(!o->heroType.has_value())
  577. o->heroType = HeroTypeID(0);
  578. o->powerRank.reset();
  579. }
  580. else
  581. {
  582. if(!o->powerRank.has_value())
  583. o->powerRank = 0;
  584. o->heroType.reset();
  585. }
  586. updateProperties();
  587. }
  588. if(key == QObject::tr("Power rank"))
  589. o->powerRank = value.toInt();
  590. if(key == QObject::tr("Hero type"))
  591. {
  592. o->heroType = HeroTypeID(value.toInt());
  593. }
  594. }
  595. void Inspector::setProperty(CGHeroInstance * o, const QString & key, const QVariant & value)
  596. {
  597. if(!o) return;
  598. if(key == QObject::tr("Gender"))
  599. o->gender = EHeroGender(value.toInt());
  600. if(key == QObject::tr("Name"))
  601. o->nameCustomTextId = mapRegisterLocalizedString("map", *controller.map(),
  602. TextIdentifier("hero", o->instanceName, "name"), value.toString().toStdString());
  603. if(key == QObject::tr("Biography"))
  604. o->biographyCustomTextId = mapRegisterLocalizedString("map", *controller.map(),
  605. TextIdentifier("hero", o->instanceName, "biography"), value.toString().toStdString());
  606. if(key == QObject::tr("Experience"))
  607. o->exp = value.toString().toInt();
  608. if(key == QObject::tr("Hero type"))
  609. {
  610. for(auto const & t : LIBRARY->heroh->objects)
  611. {
  612. if(t->getId() == value.toInt())
  613. o->subID = value.toInt();
  614. }
  615. o->gender = o->getHeroType()->gender;
  616. o->randomizeArmy(o->getHeroType()->heroClass->faction);
  617. updateProperties(); //updating other properties after change
  618. }
  619. if(key == QObject::tr("Patrol radius"))
  620. {
  621. auto radius = value.toInt();
  622. o->patrol.patrolRadius = radius;
  623. o->patrol.patrolling = radius != CGHeroInstance::NO_PATROLLING;
  624. }
  625. }
  626. void Inspector::setProperty(CGShipyard * o, const QString & key, const QVariant & value)
  627. {
  628. if(!o) return;
  629. }
  630. void Inspector::setProperty(CGResource * o, const QString & key, const QVariant & value)
  631. {
  632. if(!o) return;
  633. if(key == QObject::tr("Amount"))
  634. o->amount = value.toString().toInt();
  635. }
  636. void Inspector::setProperty(CGCreature * o, const QString & key, const QVariant & value)
  637. {
  638. if(!o) return;
  639. if(key == QObject::tr("Message"))
  640. o->message = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  641. TextIdentifier("monster", o->instanceName, "message"), value.toString().toStdString()));
  642. if(key == QObject::tr("Character"))
  643. o->character = CGCreature::Character(value.toInt());
  644. if(key == QObject::tr("Never flees"))
  645. o->neverFlees = value.toBool();
  646. if(key == QObject::tr("Not growing"))
  647. o->notGrowingTeam = value.toBool();
  648. if(key == QObject::tr("Amount"))
  649. o->stacks[SlotID(0)]->setCount(value.toString().toInt());
  650. }
  651. void Inspector::setProperty(CGSeerHut * o, const QString & key, const QVariant & value)
  652. {
  653. if(!o) return;
  654. if(key == QObject::tr("First visit text"))
  655. o->getQuest().firstVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  656. TextIdentifier("quest", o->instanceName, "firstVisit"), value.toString().toStdString()));
  657. if(key == QObject::tr("Next visit text"))
  658. o->getQuest().nextVisitText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  659. TextIdentifier("quest", o->instanceName, "nextVisit"), value.toString().toStdString()));
  660. if(key == QObject::tr("Completed text"))
  661. o->getQuest().completedText = MetaString::createFromTextID(mapRegisterLocalizedString("map", *controller.map(),
  662. TextIdentifier("quest", o->instanceName, "completed"), value.toString().toStdString()));
  663. if(key == QObject::tr("Repeat quest"))
  664. o->getQuest().repeatedQuest = value.toBool();
  665. if(key == QObject::tr("Time limit"))
  666. o->getQuest().lastDay = value.toString().toInt();
  667. }
  668. void Inspector::setProperty(CGQuestGuard * o, const QString & key, const QVariant & value)
  669. {
  670. if(!o) return;
  671. }
  672. //===============IMPLEMENT PROPERTY VALUE TYPE============================
  673. QTableWidgetItem * Inspector::addProperty(CGObjectInstance * value)
  674. {
  675. auto * item = new QTableWidgetItem(QString::number(data_cast<CGObjectInstance>(value)));
  676. item->setFlags(Qt::NoItemFlags);
  677. return item;
  678. }
  679. QTableWidgetItem * Inspector::addProperty(Inspector::PropertyEditorPlaceholder value)
  680. {
  681. auto item = new QTableWidgetItem("...");
  682. item->setFlags(Qt::NoItemFlags);
  683. return item;
  684. }
  685. QTableWidgetItem * Inspector::addProperty(unsigned int value)
  686. {
  687. auto * item = new QTableWidgetItem(QString::number(value));
  688. item->setFlags(Qt::NoItemFlags);
  689. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  690. return item;
  691. }
  692. QTableWidgetItem * Inspector::addProperty(int value)
  693. {
  694. auto * item = new QTableWidgetItem(QString::number(value));
  695. item->setFlags(Qt::NoItemFlags);
  696. //item->setData(Qt::UserRole, QVariant::fromValue(value));
  697. return item;
  698. }
  699. QTableWidgetItem * Inspector::addProperty(bool value)
  700. {
  701. auto item = new QTableWidgetItem;
  702. item->setFlags(Qt::ItemIsUserCheckable);
  703. item->setCheckState(value ? Qt::Checked : Qt::Unchecked);
  704. return item;
  705. }
  706. QTableWidgetItem * Inspector::addProperty(const std::string & value)
  707. {
  708. return addProperty(QString::fromStdString(value));
  709. }
  710. QTableWidgetItem * Inspector::addProperty(const TextIdentifier & value)
  711. {
  712. return addProperty(LIBRARY->generaltexth->translate(value.get()));
  713. }
  714. QTableWidgetItem * Inspector::addProperty(const MetaString & value)
  715. {
  716. return addProperty(value.toString());
  717. }
  718. QTableWidgetItem * Inspector::addProperty(const QString & value)
  719. {
  720. auto * item = new QTableWidgetItem(value);
  721. item->setFlags(Qt::NoItemFlags);
  722. item->setToolTip(value);
  723. return item;
  724. }
  725. QTableWidgetItem * Inspector::addProperty(const int3 & value)
  726. {
  727. auto * item = new QTableWidgetItem(QString("(%1, %2, %3)").arg(value.x, value.y, value.z));
  728. item->setFlags(Qt::NoItemFlags);
  729. return item;
  730. }
  731. QTableWidgetItem * Inspector::addProperty(const PlayerColor & value)
  732. {
  733. auto str = QObject::tr("UNFLAGGABLE");
  734. if(value == PlayerColor::NEUTRAL)
  735. str = QObject::tr("neutral");
  736. MetaString playerStr;
  737. playerStr.appendName(value);
  738. if(value.isValidPlayer())
  739. str = QString::fromStdString(playerStr.toString());
  740. auto * item = new QTableWidgetItem(str);
  741. item->setFlags(Qt::NoItemFlags);
  742. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  743. return item;
  744. }
  745. QTableWidgetItem * Inspector::addProperty(const GameResID & value)
  746. {
  747. MetaString str;
  748. str.appendName(value);
  749. auto * item = new QTableWidgetItem(QString::fromStdString(str.toString()));
  750. item->setFlags(Qt::NoItemFlags);
  751. item->setData(Qt::UserRole, QVariant::fromValue(value.getNum()));
  752. return item;
  753. }
  754. QTableWidgetItem * Inspector::addProperty(CGCreature::Character value)
  755. {
  756. auto * item = new QTableWidgetItem;
  757. item->setFlags(Qt::NoItemFlags);
  758. item->setData(Qt::UserRole, QVariant::fromValue(int(value)));
  759. for(const auto & i : characterIdentifiers)
  760. {
  761. if(i.second.toInt() == value)
  762. {
  763. item->setText(i.first);
  764. break;
  765. }
  766. }
  767. return item;
  768. }
  769. QTableWidgetItem * Inspector::addProperty(const std::optional<CGDwellingRandomizationInfo> & value)
  770. {
  771. QString text = QObject::tr("Select town");
  772. if(value.has_value() && !value->instanceId.empty())
  773. text = QString::fromStdString(value->instanceId);
  774. auto * item = new QTableWidgetItem(text);
  775. item->setFlags(Qt::NoItemFlags);
  776. return item;
  777. }
  778. //========================================================================
  779. Inspector::Inspector(MapController & c, CGObjectInstance * o, QTableWidget * t): obj(o), table(t), controller(c)
  780. {
  781. characterIdentifiers = {
  782. { QObject::tr("Compliant"), QVariant::fromValue(int(CGCreature::Character::COMPLIANT)) },
  783. { QObject::tr("Friendly"), QVariant::fromValue(int(CGCreature::Character::FRIENDLY)) },
  784. { QObject::tr("Aggressive"), QVariant::fromValue(int(CGCreature::Character::AGGRESSIVE)) },
  785. { QObject::tr("Hostile"), QVariant::fromValue(int(CGCreature::Character::HOSTILE)) },
  786. { QObject::tr("Savage"), QVariant::fromValue(int(CGCreature::Character::SAVAGE)) },
  787. };
  788. }
  789. /*
  790. * Delegates
  791. */
  792. QWidget * InspectorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  793. {
  794. return new QComboBox(parent);
  795. }
  796. void InspectorDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  797. {
  798. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  799. {
  800. for(auto & i : options)
  801. {
  802. ed->addItem(i.first);
  803. ed->setItemData(ed->count() - 1, i.second);
  804. }
  805. }
  806. else
  807. {
  808. QStyledItemDelegate::setEditorData(editor, index);
  809. }
  810. }
  811. void InspectorDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  812. {
  813. if(QComboBox *ed = qobject_cast<QComboBox *>(editor))
  814. {
  815. if(!options.isEmpty())
  816. {
  817. QMap<int, QVariant> data;
  818. data[Qt::DisplayRole] = options[ed->currentIndex()].first;
  819. data[Qt::UserRole] = options[ed->currentIndex()].second;
  820. model->setItemData(index, data);
  821. }
  822. }
  823. else
  824. {
  825. QStyledItemDelegate::setModelData(editor, model, index);
  826. }
  827. }
  828. OwnerDelegate::OwnerDelegate(MapController & controller, bool addNeutral)
  829. {
  830. if(addNeutral)
  831. options.push_back({QObject::tr("neutral"), QVariant::fromValue(PlayerColor::NEUTRAL.getNum()) });
  832. for(int p = 0; p < controller.map()->players.size(); ++p)
  833. if(controller.map()->players[p].canAnyonePlay())
  834. {
  835. MetaString str;
  836. str.appendName(PlayerColor(p));
  837. options.push_back({QString::fromStdString(str.toString()), QVariant::fromValue(PlayerColor(p).getNum()) });
  838. }
  839. }