CQuestLog.cpp 8.5 KB

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