CQuestLog.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include "../../lib/mapObjects/CQuest.h"
  19. /*
  20. * CQuestLog.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. struct QuestInfo;
  29. class CAdvmapInterface;
  30. void CQuestLabel::clickLeft(tribool down, bool previousState)
  31. {
  32. if (down)
  33. callback();
  34. }
  35. void CQuestLabel::showAll(SDL_Surface * to)
  36. {
  37. if (active)
  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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  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. componentsBox(nullptr),
  101. hideComplete(false),
  102. quests(Quests),
  103. slider(nullptr)
  104. {
  105. OBJ_CONSTRUCTION_CAPTURING_ALL;
  106. init();
  107. }
  108. void CQuestLog::init()
  109. {
  110. const JsonNode & texts = CGI->generaltexth->localizedTexts["questLog"];
  111. minimap = new CQuestMinimap (Rect (12, 12, 169, 169));
  112. // 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
  113. description = new CTextBox ("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  114. ok = new CButton(Point(539, 398), "IOKAY.DEF", CGI->generaltexth->zelp[445], std::bind(&CQuestLog::close,this), SDLK_RETURN);
  115. // 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
  116. hideCompleteButton = new CToggleButton(Point(10, 396), "sysopchk.def", CButton::tooltip(texts["hideComplete"]), std::bind(&CQuestLog::toggleComplete, this, _1));
  117. hideCompleteLabel = new CLabel(46, 398, FONT_MEDIUM, TOPLEFT, Colors::WHITE, texts["hideComplete"]["label"].String());
  118. slider = new CSlider(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, false, CSlider::BROWN);
  119. recreateLabelList();
  120. recreateQuestList (0);
  121. }
  122. void CQuestLog::recreateLabelList()
  123. {
  124. if (labels.size())
  125. labels.clear();
  126. bool completeMissing = true;
  127. int currentLabel = 0;
  128. for (int i = 0; i < quests.size(); ++i)
  129. {
  130. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  131. if (quests[i].quest->missionType == CQuest::MISSION_NONE)
  132. continue;
  133. if (quests[i].quest->progress == CQuest::COMPLETE)
  134. {
  135. completeMissing = false;
  136. if (hideComplete)
  137. continue;
  138. }
  139. MetaString text;
  140. quests[i].quest->getRolloverText (text, false);
  141. if (quests[i].obj)
  142. {
  143. if (auto seersHut = dynamic_cast<const CGSeerHut *>(quests[i].obj))
  144. {
  145. MetaString toSeer;
  146. toSeer << VLC->generaltexth->allTexts[347];
  147. toSeer.addReplacement(seersHut->seerName);
  148. text.addReplacement(toSeer.toString());
  149. }
  150. else
  151. text.addReplacement(quests[i].obj->getObjectName()); //get name of the object
  152. }
  153. auto label = std::make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  154. label->disable();
  155. label->callback = std::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  156. labels.push_back(label);
  157. // Select latest active quest
  158. if (quests[i].quest->progress != CQuest::COMPLETE)
  159. selectQuest(i, currentLabel);
  160. currentLabel = labels.size();
  161. }
  162. if (completeMissing) // We can't use block(completeMissing) because if false button state reset to NORMAL
  163. hideCompleteButton->block(true);
  164. slider->setAmount(currentLabel);
  165. if (currentLabel > QUEST_COUNT)
  166. {
  167. slider->block(false);
  168. slider->moveToMax();
  169. }
  170. else
  171. {
  172. slider->block(true);
  173. slider->moveToMin();
  174. }
  175. }
  176. void CQuestLog::showAll(SDL_Surface * to)
  177. {
  178. CWindowObject::showAll (to);
  179. if (labels.size() && labels[questIndex]->active)
  180. {
  181. Rect rect = Rect::around(labels[questIndex]->pos);
  182. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  183. rect.w += 2;
  184. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  185. }
  186. }
  187. void CQuestLog::recreateQuestList (int newpos)
  188. {
  189. for (int i = 0; i < labels.size(); ++i)
  190. {
  191. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  192. if (i >= newpos && i < newpos + QUEST_COUNT)
  193. labels[i]->enable();
  194. else
  195. labels[i]->disable();
  196. }
  197. minimap->update();
  198. }
  199. void CQuestLog::selectQuest (int which, int labelId)
  200. {
  201. questIndex = labelId;
  202. currentQuest = &quests[which];
  203. minimap->currentQuest = currentQuest;
  204. MetaString text;
  205. std::vector<Component> components;
  206. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  207. if (description->slider)
  208. description->slider->moveToMin(); // scroll text to start position
  209. description->setText (text.toString()); //TODO: use special log entry text
  210. vstd::clear_pointer(componentsBox);
  211. int componentsSize = components.size();
  212. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  213. if (componentsSize)
  214. {
  215. descriptionHeight -= 15;
  216. CComponent::ESize imageSize = CComponent::large;
  217. switch (currentQuest->quest->missionType)
  218. {
  219. case CQuest::MISSION_ARMY:
  220. {
  221. if (componentsSize > 4)
  222. descriptionHeight -= 195;
  223. else
  224. descriptionHeight -= 100;
  225. break;
  226. }
  227. case CQuest::MISSION_ART:
  228. {
  229. if (componentsSize > 4)
  230. descriptionHeight -= 190;
  231. else
  232. descriptionHeight -= 90;
  233. break;
  234. }
  235. case CQuest::MISSION_PRIMARY_STAT:
  236. case CQuest::MISSION_RESOURCES:
  237. {
  238. if (componentsSize > 4)
  239. {
  240. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  241. descriptionHeight -= 140;
  242. }
  243. else
  244. descriptionHeight -= 125;
  245. break;
  246. }
  247. default:
  248. descriptionHeight -= 115;
  249. break;
  250. }
  251. OBJ_CONSTRUCTION_CAPTURING_ALL;
  252. std::vector<CComponent *> comps;
  253. for (auto & component : components)
  254. comps.push_back(new CComponent(component, imageSize));
  255. componentsBox = new 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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  269. hideComplete = on;
  270. recreateLabelList();
  271. recreateQuestList(0);
  272. redraw();
  273. }