CQuestLog.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. bool completeMissing = true;
  126. int currentLabel = 0;
  127. for (int i = 0; i < quests.size(); ++i)
  128. {
  129. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  130. if (quests[i].quest->missionType == CQuest::MISSION_NONE)
  131. continue;
  132. if (quests[i].quest->progress == CQuest::COMPLETE)
  133. {
  134. completeMissing = false;
  135. if (hideComplete)
  136. continue;
  137. }
  138. MetaString text;
  139. quests[i].quest->getRolloverText (text, false);
  140. if (quests[i].obj)
  141. {
  142. if (auto seersHut = dynamic_cast<const CGSeerHut *>(quests[i].obj))
  143. {
  144. MetaString toSeer;
  145. toSeer << VLC->generaltexth->allTexts[347];
  146. toSeer.addReplacement(seersHut->seerName);
  147. text.addReplacement(toSeer.toString());
  148. }
  149. else
  150. text.addReplacement(quests[i].obj->getObjectName()); //get name of the object
  151. }
  152. auto label = make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  153. label->disable();
  154. label->callback = boost::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  155. labels.push_back(label);
  156. // Select latest active quest
  157. if (quests[i].quest->progress != CQuest::COMPLETE)
  158. selectQuest(i, currentLabel);
  159. currentLabel = labels.size();
  160. }
  161. if (completeMissing) // We can't use block(completeMissing) because if false button state reset to NORMAL
  162. hideCompleteButton->block(true);
  163. slider->setAmount(currentLabel);
  164. if (currentLabel > QUEST_COUNT)
  165. {
  166. slider->block(false);
  167. slider->moveToMax();
  168. }
  169. else
  170. slider->block(true);
  171. }
  172. void CQuestLog::showAll(SDL_Surface * to)
  173. {
  174. CWindowObject::showAll (to);
  175. if (labels.size() && labels[questIndex]->active)
  176. {
  177. Rect rect = Rect::around(labels[questIndex]->pos);
  178. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  179. rect.w += 2;
  180. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  181. }
  182. }
  183. void CQuestLog::recreateQuestList (int newpos)
  184. {
  185. for (int i = 0; i < labels.size(); ++i)
  186. {
  187. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  188. if (i >= newpos && i < newpos + QUEST_COUNT)
  189. labels[i]->enable();
  190. else
  191. labels[i]->disable();
  192. }
  193. minimap->update();
  194. }
  195. void CQuestLog::selectQuest (int which, int labelId)
  196. {
  197. questIndex = labelId;
  198. currentQuest = &quests[which];
  199. minimap->currentQuest = currentQuest;
  200. MetaString text;
  201. std::vector<Component> components;
  202. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  203. if (description->slider)
  204. description->slider->moveToMin(); // scroll text to start position
  205. description->setText (text.toString()); //TODO: use special log entry text
  206. vstd::clear_pointer(componentsBox);
  207. int componentsSize = components.size();
  208. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  209. if (componentsSize)
  210. {
  211. descriptionHeight -= 15;
  212. CComponent::ESize imageSize = CComponent::large;
  213. switch (currentQuest->quest->missionType)
  214. {
  215. case CQuest::MISSION_ARMY:
  216. {
  217. if (componentsSize > 4)
  218. descriptionHeight -= 195;
  219. else
  220. descriptionHeight -= 100;
  221. break;
  222. }
  223. case CQuest::MISSION_ART:
  224. {
  225. if (componentsSize > 4)
  226. descriptionHeight -= 190;
  227. else
  228. descriptionHeight -= 90;
  229. break;
  230. }
  231. case CQuest::MISSION_PRIMARY_STAT:
  232. case CQuest::MISSION_RESOURCES:
  233. {
  234. if (componentsSize > 4)
  235. {
  236. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  237. descriptionHeight -= 140;
  238. }
  239. else
  240. descriptionHeight -= 125;
  241. break;
  242. }
  243. default:
  244. descriptionHeight -= 115;
  245. break;
  246. }
  247. OBJ_CONSTRUCTION_CAPTURING_ALL;
  248. std::vector<CComponent *> comps;
  249. for (auto & component : components)
  250. comps.push_back(new CComponent(component, imageSize));
  251. componentsBox = new CComponentBox(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  252. }
  253. description->resize(Point(385, descriptionHeight));
  254. minimap->update();
  255. redraw();
  256. }
  257. void CQuestLog::sliderMoved (int newpos)
  258. {
  259. recreateQuestList (newpos); //move components
  260. redraw();
  261. }
  262. void CQuestLog::toggleComplete(bool on)
  263. {
  264. OBJ_CONSTRUCTION_CAPTURING_ALL;
  265. hideComplete = on;
  266. recreateLabelList();
  267. recreateQuestList(0);
  268. redraw();
  269. }