CQuestLog.cpp 8.4 KB

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