CQuestLog.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "StdInc.h"
  2. #include "CQuestLog.h"
  3. #include "CAdvmapInterface.h"
  4. #include "../CBitmapHandler.h"
  5. #include "../CDefHandler.h"
  6. #include "../CGameInfo.h"
  7. #include "../CPlayerInterface.h"
  8. #include "../Graphics.h"
  9. #include "../gui/CGuiHandler.h"
  10. #include "../gui/SDL_Extensions.h"
  11. #include "../widgets/CComponent.h"
  12. #include "../../CCallback.h"
  13. #include "../../lib/CArtHandler.h"
  14. #include "../../lib/CConfigHandler.h"
  15. #include "../../lib/CGameState.h"
  16. #include "../../lib/CGeneralTextHandler.h"
  17. #include "../../lib/NetPacksBase.h"
  18. /*
  19. * CQuestLog.cpp, part of VCMI engine
  20. *
  21. * Authors: listed in file AUTHORS in main folder
  22. *
  23. * License: GNU General Public License v2.0 or later
  24. * Full text of license available in license.txt file, in main folder
  25. *
  26. */
  27. struct QuestInfo;
  28. class CAdvmapInterface;
  29. void CQuestLabel::clickLeft(tribool down, bool previousState)
  30. {
  31. if (down)
  32. callback();
  33. }
  34. void CQuestLabel::showAll(SDL_Surface * to)
  35. {
  36. if (active)
  37. CMultiLineLabel::showAll (to);
  38. }
  39. CQuestIcon::CQuestIcon (const std::string &defname, int index, int x, int y) :
  40. CAnimImage(defname, index, 0, x, y)
  41. {
  42. addUsedEvents(LCLICK);
  43. }
  44. void CQuestIcon::clickLeft(tribool down, bool previousState)
  45. {
  46. if (down)
  47. callback();
  48. }
  49. void CQuestIcon::showAll(SDL_Surface * to)
  50. {
  51. CSDL_Ext::CClipRectGuard guard(to, parent->pos);
  52. CAnimImage::showAll(to);
  53. }
  54. CQuestMinimap::CQuestMinimap (const Rect & position) :
  55. CMinimap (position),
  56. currentQuest (nullptr)
  57. {
  58. }
  59. void CQuestMinimap::addQuestMarks (const QuestInfo * q)
  60. {
  61. OBJ_CONSTRUCTION_CAPTURING_ALL;
  62. icons.clear();
  63. int3 tile;
  64. if (q->obj)
  65. tile = q->obj->pos;
  66. else
  67. tile = q->tile;
  68. int x,y;
  69. minimap->tileToPixels (tile, x, y);
  70. if (level != tile.z)
  71. setLevel(tile.z);
  72. auto pic = make_shared<CQuestIcon>("VwSymbol.def", 3, x, y);
  73. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  74. pic->callback = std::bind (&CQuestMinimap::iconClicked, this);
  75. icons.push_back(pic);
  76. }
  77. void CQuestMinimap::update()
  78. {
  79. CMinimap::update();
  80. if (currentQuest)
  81. addQuestMarks (currentQuest);
  82. }
  83. void CQuestMinimap::iconClicked()
  84. {
  85. if (currentQuest->obj)
  86. adventureInt->centerOn (currentQuest->obj->pos);
  87. //moveAdvMapSelection();
  88. }
  89. void CQuestMinimap::showAll(SDL_Surface * to)
  90. {
  91. CIntObject::showAll(to); // blitting IntObject directly to hide radar
  92. for (auto pic : icons)
  93. pic->showAll(to);
  94. }
  95. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests) :
  96. CWindowObject(PLAYER_COLORED | BORDERED, "questDialog"),
  97. questIndex(0),
  98. currentQuest(nullptr),
  99. componentsBox(nullptr),
  100. quests (Quests),
  101. hideComplete(false),
  102. slider(nullptr)
  103. {
  104. OBJ_CONSTRUCTION_CAPTURING_ALL;
  105. init();
  106. }
  107. void CQuestLog::init()
  108. {
  109. const JsonNode & texts = CGI->generaltexth->localizedTexts["questLog"];
  110. minimap = new CQuestMinimap (Rect (12, 12, 169, 169));
  111. // 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
  112. description = new CTextBox ("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  113. ok = new CButton(Point(539, 398), "IOKAY.DEF", CGI->generaltexth->zelp[445], boost::bind(&CQuestLog::close,this), SDLK_RETURN);
  114. // 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
  115. hideCompleteButton = new CToggleButton(Point(10, 396), "sysopchk.def", CButton::tooltip(texts["hideComplete"]), std::bind(&CQuestLog::toggleComplete, this, _1));
  116. hideCompleteLabel = new CLabel(46, 398, FONT_MEDIUM, TOPLEFT, Colors::WHITE, texts["hideComplete"]["label"].String());
  117. slider = new CSlider(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, false, CSlider::BROWN);
  118. recreateLabelList();
  119. recreateQuestList (0);
  120. }
  121. void CQuestLog::recreateLabelList()
  122. {
  123. if (labels.size())
  124. labels.clear();
  125. int currentLabel = 0;
  126. for (int i = 0; i < quests.size(); ++i)
  127. {
  128. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  129. if (quests[i].quest->missionType == CQuest::MISSION_NONE
  130. || (hideComplete && quests[i].quest->progress == CQuest::COMPLETE))
  131. continue;
  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 = make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  147. label->disable();
  148. label->callback = boost::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 = labels.size();
  154. }
  155. slider->setAmount(currentLabel);
  156. if (currentLabel > QUEST_COUNT)
  157. {
  158. slider->block(false);
  159. slider->moveToMax();
  160. }
  161. else
  162. slider->block(true);
  163. }
  164. void CQuestLog::showAll(SDL_Surface * to)
  165. {
  166. CWindowObject::showAll (to);
  167. if (labels.size() && labels[questIndex]->active)
  168. {
  169. Rect rect = Rect::around(labels[questIndex]->pos);
  170. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  171. rect.w += 2;
  172. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  173. }
  174. }
  175. void CQuestLog::recreateQuestList (int newpos)
  176. {
  177. for (int i = 0; i < labels.size(); ++i)
  178. {
  179. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  180. if (i >= newpos && i < newpos + QUEST_COUNT)
  181. labels[i]->enable();
  182. else
  183. labels[i]->disable();
  184. }
  185. minimap->update();
  186. }
  187. void CQuestLog::selectQuest (int which, int labelId)
  188. {
  189. questIndex = labelId;
  190. currentQuest = &quests[which];
  191. minimap->currentQuest = currentQuest;
  192. MetaString text;
  193. std::vector<Component> components;
  194. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  195. if (description->slider)
  196. description->slider->moveToMin(); // scroll text to start position
  197. description->setText (text.toString()); //TODO: use special log entry text
  198. vstd::clear_pointer(componentsBox);
  199. int componentsSize = components.size();
  200. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  201. if (componentsSize)
  202. {
  203. descriptionHeight -= 15;
  204. CComponent::ESize imageSize = CComponent::large;
  205. switch (currentQuest->quest->missionType)
  206. {
  207. case CQuest::MISSION_ARMY:
  208. {
  209. if (componentsSize > 4)
  210. descriptionHeight -= 195;
  211. else
  212. descriptionHeight -= 100;
  213. break;
  214. }
  215. case CQuest::MISSION_ART:
  216. {
  217. if (componentsSize > 4)
  218. descriptionHeight -= 190;
  219. else
  220. descriptionHeight -= 90;
  221. break;
  222. }
  223. case CQuest::MISSION_PRIMARY_STAT:
  224. case CQuest::MISSION_RESOURCES:
  225. {
  226. if (componentsSize > 4)
  227. {
  228. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  229. descriptionHeight -= 140;
  230. }
  231. else
  232. descriptionHeight -= 125;
  233. break;
  234. }
  235. default:
  236. descriptionHeight -= 115;
  237. break;
  238. }
  239. OBJ_CONSTRUCTION_CAPTURING_ALL;
  240. std::vector<CComponent *> comps;
  241. for (auto & component : components)
  242. comps.push_back(new CComponent(component, imageSize));
  243. componentsBox = new CComponentBox(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  244. }
  245. description->resize(Point(385, descriptionHeight));
  246. minimap->update();
  247. redraw();
  248. }
  249. void CQuestLog::sliderMoved (int newpos)
  250. {
  251. recreateQuestList (newpos); //move components
  252. redraw();
  253. }
  254. void CQuestLog::toggleComplete(bool on)
  255. {
  256. OBJ_CONSTRUCTION_CAPTURING_ALL;
  257. hideComplete = on;
  258. recreateLabelList();
  259. recreateQuestList(0);
  260. redraw();
  261. }