CQuestLog.cpp 8.5 KB

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