ArtifactsUIController.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ArtifactsUIController.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 "ArtifactsUIController.h"
  12. #include "CPlayerInterface.h"
  13. #include "../CCallback.h"
  14. #include "../lib/ArtifactUtils.h"
  15. #include "../lib/texts/CGeneralTextHandler.h"
  16. #include "../lib/mapObjects/CGHeroInstance.h"
  17. #include "GameEngine.h"
  18. #include "GameInstance.h"
  19. #include "gui/WindowHandler.h"
  20. #include "widgets/CComponent.h"
  21. #include "windows/CWindowWithArtifacts.h"
  22. ArtifactsUIController::ArtifactsUIController()
  23. {
  24. numOfMovedArts = 0;
  25. numOfArtsAskAssembleSession = 0;
  26. }
  27. bool ArtifactsUIController::askToAssemble(const ArtifactLocation & al, const bool onlyEquipped, const bool checkIgnored)
  28. {
  29. if(auto hero = GAME->interface()->cb->getHero(al.artHolder))
  30. {
  31. if(hero->getArt(al.slot) == nullptr)
  32. {
  33. logGlobal->error("artifact location %d points to nothing", al.slot.num);
  34. return false;
  35. }
  36. return askToAssemble(hero, al.slot, onlyEquipped, checkIgnored);
  37. }
  38. return false;
  39. }
  40. bool ArtifactsUIController::askToAssemble(const CGHeroInstance * hero, const ArtifactPosition & slot,
  41. const bool onlyEquipped, const bool checkIgnored)
  42. {
  43. assert(hero);
  44. const auto art = hero->getArt(slot);
  45. assert(art);
  46. if(hero->tempOwner != GAME->interface()->playerID)
  47. return false;
  48. if(numOfArtsAskAssembleSession != 0)
  49. numOfArtsAskAssembleSession--;
  50. auto assemblyPossibilities = ArtifactUtils::assemblyPossibilities(hero, art->getTypeId(), onlyEquipped);
  51. if(!assemblyPossibilities.empty())
  52. {
  53. auto askThread = new boost::thread([this, hero, art, slot, assemblyPossibilities, checkIgnored]() -> void
  54. {
  55. boost::mutex::scoped_lock askLock(askAssembleArtifactMutex);
  56. for(const auto combinedArt : assemblyPossibilities)
  57. {
  58. boost::mutex::scoped_lock interfaceLock(ENGINE->interfaceMutex);
  59. if(checkIgnored)
  60. {
  61. if(vstd::contains(ignoredArtifacts, combinedArt->getId()))
  62. continue;
  63. ignoredArtifacts.emplace(combinedArt->getId());
  64. }
  65. bool assembleConfirmed = false;
  66. MetaString message = MetaString::createFromTextID(art->getType()->getDescriptionTextID());
  67. message.appendEOL();
  68. message.appendEOL();
  69. if(combinedArt->isFused())
  70. message.appendRawString(LIBRARY->generaltexth->translate("vcmi.heroWindow.fusingArtifact.fusing"));
  71. else
  72. message.appendRawString(LIBRARY->generaltexth->allTexts[732]); // You possess all of the components needed to assemble the
  73. message.replaceName(ArtifactID(combinedArt->getId()));
  74. GAME->interface()->showYesNoDialog(message.toString(), [&assembleConfirmed, hero, slot, combinedArt]()
  75. {
  76. assembleConfirmed = true;
  77. GAME->interface()->cb->assembleArtifacts(hero->id, slot, true, combinedArt->getId());
  78. }, nullptr, {std::make_shared<CComponent>(ComponentType::ARTIFACT, combinedArt->getId())});
  79. GAME->interface()->waitWhileDialog();
  80. if(assembleConfirmed)
  81. break;
  82. }
  83. });
  84. askThread->detach();
  85. return true;
  86. }
  87. return false;
  88. }
  89. bool ArtifactsUIController::askToDisassemble(const CGHeroInstance * hero, const ArtifactPosition & slot)
  90. {
  91. assert(hero);
  92. const auto art = hero->getArt(slot);
  93. assert(art);
  94. if(hero->tempOwner != GAME->interface()->playerID)
  95. return false;
  96. if(art->hasParts())
  97. {
  98. if(ArtifactUtils::isSlotBackpack(slot) && !ArtifactUtils::isBackpackFreeSlots(hero, art->getType()->getConstituents().size() - 1))
  99. return false;
  100. MetaString message = MetaString::createFromTextID(art->getType()->getDescriptionTextID());
  101. message.appendEOL();
  102. message.appendEOL();
  103. message.appendRawString(LIBRARY->generaltexth->allTexts[733]); // Do you wish to disassemble this artifact?
  104. GAME->interface()->showYesNoDialog(message.toString(), [hero, slot]()
  105. {
  106. GAME->interface()->cb->assembleArtifacts(hero->id, slot, false, ArtifactID());
  107. }, nullptr);
  108. return true;
  109. }
  110. return false;
  111. }
  112. void ArtifactsUIController::artifactRemoved()
  113. {
  114. for(const auto & artWin : ENGINE->windows().findWindows<CWindowWithArtifacts>())
  115. artWin->update();
  116. GAME->interface()->waitWhileDialog();
  117. }
  118. void ArtifactsUIController::artifactMoved()
  119. {
  120. // If a bulk transfer has arrived, then redrawing only the last art movement.
  121. if(numOfMovedArts != 0)
  122. numOfMovedArts--;
  123. if(numOfMovedArts == 0)
  124. for(const auto & artWin : ENGINE->windows().findWindows<CWindowWithArtifacts>())
  125. {
  126. artWin->update();
  127. }
  128. GAME->interface()->waitWhileDialog();
  129. }
  130. void ArtifactsUIController::bulkArtMovementStart(size_t totalNumOfArts, size_t possibleAssemblyNumOfArts)
  131. {
  132. assert(totalNumOfArts >= possibleAssemblyNumOfArts);
  133. numOfMovedArts = totalNumOfArts;
  134. if(numOfArtsAskAssembleSession == 0)
  135. {
  136. // Do not start the next session until the previous one is finished
  137. numOfArtsAskAssembleSession = possibleAssemblyNumOfArts;
  138. ignoredArtifacts.clear();
  139. }
  140. }
  141. void ArtifactsUIController::artifactAssembled()
  142. {
  143. for(const auto & artWin : ENGINE->windows().findWindows<CWindowWithArtifacts>())
  144. artWin->update();
  145. }
  146. void ArtifactsUIController::artifactDisassembled()
  147. {
  148. for(const auto & artWin : ENGINE->windows().findWindows<CWindowWithArtifacts>())
  149. artWin->update();
  150. }