CQuestLog.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * CQuestLog.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 "CQuestLog.h"
  12. #include "../CPlayerInterface.h"
  13. #include "../GameEngine.h"
  14. #include "../GameInstance.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../widgets/Buttons.h"
  18. #include "../widgets/CComponent.h"
  19. #include "../widgets/Slider.h"
  20. #include "../adventureMap/AdventureMapInterface.h"
  21. #include "../adventureMap/CMinimap.h"
  22. #include "../render/Canvas.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CArtHandler.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/GameLibrary.h"
  27. #include "../../lib/gameState/QuestInfo.h"
  28. #include "../../lib/mapObjects/CQuest.h"
  29. #include "../../lib/texts/CGeneralTextHandler.h"
  30. VCMI_LIB_NAMESPACE_BEGIN
  31. struct QuestInfo;
  32. VCMI_LIB_NAMESPACE_END
  33. class CAdvmapInterface;
  34. void CQuestLabel::clickPressed(const Point & cursorPosition)
  35. {
  36. callback();
  37. }
  38. void CQuestLabel::showAll(Canvas & to)
  39. {
  40. CMultiLineLabel::showAll (to);
  41. }
  42. CQuestIcon::CQuestIcon (const AnimationPath &defname, int index, int x, int y) :
  43. CAnimImage(defname, index, 0, x, y)
  44. {
  45. addUsedEvents(LCLICK);
  46. }
  47. void CQuestIcon::clickPressed(const Point & cursorPosition)
  48. {
  49. callback();
  50. }
  51. void CQuestIcon::showAll(Canvas & to)
  52. {
  53. CanvasClipRectGuard guard(to, parent->pos);
  54. CAnimImage::showAll(to);
  55. }
  56. CQuestMinimap::CQuestMinimap(const Rect & position)
  57. : CMinimap(position),
  58. currentQuest(nullptr)
  59. {
  60. }
  61. void CQuestMinimap::addQuestMarks (const QuestInfo * q)
  62. {
  63. OBJECT_CONSTRUCTION;
  64. icons.clear();
  65. int3 tile = q->getPosition(GAME->interface()->cb.get());
  66. Point offset = tileToPixels(tile);
  67. onMapViewMoved(Rect(), tile.z);
  68. auto pic = std::make_shared<CQuestIcon>(AnimationPath::builtin("VwSymbol.def"), 3, offset.x, offset.y);
  69. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  70. pic->callback = std::bind (&CQuestMinimap::iconClicked, this);
  71. icons.push_back(pic);
  72. }
  73. void CQuestMinimap::update()
  74. {
  75. CMinimap::update();
  76. if(currentQuest)
  77. addQuestMarks(currentQuest);
  78. }
  79. void CQuestMinimap::iconClicked()
  80. {
  81. if(currentQuest->obj)
  82. adventureInt->centerOnTile(currentQuest->getObject(GAME->interface()->cb.get())->visitablePos());
  83. //moveAdvMapSelection();
  84. }
  85. void CQuestMinimap::showAll(Canvas & to)
  86. {
  87. CIntObject::showAll(to); // blitting IntObject directly to hide radar
  88. // for (auto pic : icons)
  89. // pic->showAll(to);
  90. }
  91. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests)
  92. : CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin("questDialog")),
  93. questIndex(0),
  94. currentQuest(nullptr),
  95. hideComplete(false),
  96. quests(Quests)
  97. {
  98. OBJECT_CONSTRUCTION;
  99. minimap = std::make_shared<CQuestMinimap>(Rect(12, 12, 169, 169));
  100. // TextBox have it's own 4 pixel padding from top at least for English. To achieve 10px from both left and top only add 6px margin
  101. description = std::make_shared<CTextBox>("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE);
  102. ok = std::make_shared<CButton>(Point(539, 398), AnimationPath::builtin("IOKAY.DEF"), LIBRARY->generaltexth->zelp[445], std::bind(&CQuestLog::close, this), EShortcut::GLOBAL_RETURN);
  103. // Both button and label are shifted to -2px by x and y to not make them actually look like they're on same line with quests list and ok button
  104. hideCompleteButton = std::make_shared<CToggleButton>(Point(10, 396), AnimationPath::builtin("sysopchk.def"), CButton::tooltipLocalized("vcmi.questLog.hideComplete"), std::bind(&CQuestLog::toggleComplete, this, _1));
  105. hideCompleteLabel = std::make_shared<CLabel>(46, 398, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, LIBRARY->generaltexth->translate("vcmi.questLog.hideComplete.hover"));
  106. slider = std::make_shared<CSlider>(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, 0, Orientation::VERTICAL, CSlider::BROWN);
  107. slider->setPanningStep(32);
  108. recreateLabelList();
  109. recreateQuestList(0);
  110. }
  111. void CQuestLog::recreateLabelList()
  112. {
  113. OBJECT_CONSTRUCTION;
  114. labels.clear();
  115. bool completeMissing = true;
  116. int currentLabel = 0;
  117. for (int i = 0; i < quests.size(); ++i)
  118. {
  119. auto questPtr = quests[i].getQuest(GAME->interface()->cb.get());
  120. auto questObject = quests[i].getObject(GAME->interface()->cb.get());
  121. // Quests without mision don't have text for them and can't be displayed
  122. if (quests[i].getQuest(GAME->interface()->cb.get())->mission == Rewardable::Limiter{})
  123. continue;
  124. if (questPtr->isCompleted)
  125. {
  126. completeMissing = false;
  127. if (hideComplete)
  128. continue;
  129. }
  130. MetaString text;
  131. questPtr->getRolloverText(GAME->interface()->cb.get(), text, false);
  132. if (quests[i].obj)
  133. {
  134. if (auto seersHut = dynamic_cast<const CGSeerHut *>(questObject))
  135. {
  136. MetaString toSeer;
  137. toSeer.appendRawString(LIBRARY->generaltexth->allTexts[347]);
  138. toSeer.replaceRawString(seersHut->seerName);
  139. text.replaceRawString(toSeer.toString());
  140. }
  141. else
  142. text.replaceRawString(questObject->getObjectName()); //get name of the object
  143. }
  144. auto label = std::make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, text.toString());
  145. label->disable();
  146. label->callback = std::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  147. labels.push_back(label);
  148. // Select latest active quest
  149. if(!questPtr->isCompleted)
  150. selectQuest(i, currentLabel);
  151. currentLabel = static_cast<int>(labels.size());
  152. }
  153. if (completeMissing) // We can't use block(completeMissing) because if false button state reset to NORMAL
  154. hideCompleteButton->block(true);
  155. slider->setAmount(currentLabel);
  156. if (currentLabel > QUEST_COUNT)
  157. {
  158. slider->block(false);
  159. slider->scrollToMax();
  160. }
  161. else
  162. {
  163. slider->block(true);
  164. slider->scrollToMin();
  165. }
  166. }
  167. void CQuestLog::showAll(Canvas & to)
  168. {
  169. CWindowObject::showAll(to);
  170. if(questIndex >= 0 && questIndex < labels.size())
  171. {
  172. //TODO: use child object to selection rect
  173. Rect rect = Rect::createAround(labels[questIndex]->pos, 1);
  174. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  175. rect.w += 2;
  176. to.drawBorder(rect, Colors::METALLIC_GOLD);
  177. }
  178. }
  179. void CQuestLog::recreateQuestList (int newpos)
  180. {
  181. for (int i = 0; i < labels.size(); ++i)
  182. {
  183. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  184. if (i >= newpos && i < newpos + QUEST_COUNT)
  185. labels[i]->enable();
  186. else
  187. labels[i]->disable();
  188. }
  189. minimap->update();
  190. }
  191. void CQuestLog::selectQuest(int which, int labelId)
  192. {
  193. questIndex = labelId;
  194. currentQuest = &quests[which];
  195. minimap->currentQuest = currentQuest;
  196. MetaString text;
  197. std::vector<Component> components;
  198. currentQuest->getQuest(GAME->interface()->cb.get())->getVisitText(GAME->interface()->cb.get(), text, components, true);
  199. if(description->slider)
  200. description->slider->scrollToMin(); // scroll text to start position
  201. description->setText(text.toString()); //TODO: use special log entry text
  202. componentsBox.reset();
  203. int componentsSize = static_cast<int>(components.size());
  204. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  205. if(componentsSize)
  206. {
  207. CComponent::ESize imageSize = CComponent::large;
  208. if (componentsSize > 4)
  209. {
  210. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  211. descriptionHeight -= 155;
  212. }
  213. else
  214. descriptionHeight -= 130;
  215. /*switch (currentQuest->quest->missionType)
  216. {
  217. case CQuest::MISSION_ARMY:
  218. {
  219. if (componentsSize > 4)
  220. descriptionHeight -= 195;
  221. else
  222. descriptionHeight -= 100;
  223. break;
  224. }
  225. case CQuest::MISSION_ART:
  226. {
  227. if (componentsSize > 4)
  228. descriptionHeight -= 190;
  229. else
  230. descriptionHeight -= 90;
  231. break;
  232. }
  233. case CQuest::MISSION_PRIMARY_STAT:
  234. case CQuest::MISSION_RESOURCES:
  235. {
  236. if (componentsSize > 4)
  237. {
  238. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  239. descriptionHeight -= 140;
  240. }
  241. else
  242. descriptionHeight -= 125;
  243. break;
  244. }
  245. default:
  246. descriptionHeight -= 115;
  247. break;
  248. }*/
  249. OBJECT_CONSTRUCTION;
  250. std::vector<std::shared_ptr<CComponent>> comps;
  251. for(auto & component : components)
  252. {
  253. auto c = std::make_shared<CComponent>(component, imageSize);
  254. comps.push_back(c);
  255. }
  256. componentsBox = std::make_shared<CComponentBox>(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  257. }
  258. description->resize(Point(385, descriptionHeight));
  259. minimap->update();
  260. redraw();
  261. }
  262. void CQuestLog::sliderMoved(int newpos)
  263. {
  264. recreateQuestList(newpos); //move components
  265. redraw();
  266. }
  267. void CQuestLog::toggleComplete(bool on)
  268. {
  269. hideComplete = on;
  270. recreateLabelList();
  271. recreateQuestList(0);
  272. redraw();
  273. }