CQuestLog.cpp 8.3 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 "CAdvmapInterface.h"
  13. #include "../CBitmapHandler.h"
  14. #include "../CGameInfo.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../Graphics.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/SDL_Extensions.h"
  19. #include "../widgets/CComponent.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. 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 = std::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. hideComplete(false),
  101. quests(Quests),
  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], std::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 = std::make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  153. label->disable();
  154. label->callback = std::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. {
  171. slider->block(true);
  172. slider->moveToMin();
  173. }
  174. }
  175. void CQuestLog::showAll(SDL_Surface * to)
  176. {
  177. CWindowObject::showAll (to);
  178. if (labels.size() && labels[questIndex]->active)
  179. {
  180. Rect rect = Rect::around(labels[questIndex]->pos);
  181. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  182. rect.w += 2;
  183. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  184. }
  185. }
  186. void CQuestLog::recreateQuestList (int newpos)
  187. {
  188. for (int i = 0; i < labels.size(); ++i)
  189. {
  190. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  191. if (i >= newpos && i < newpos + QUEST_COUNT)
  192. labels[i]->enable();
  193. else
  194. labels[i]->disable();
  195. }
  196. minimap->update();
  197. }
  198. void CQuestLog::selectQuest (int which, int labelId)
  199. {
  200. questIndex = labelId;
  201. currentQuest = &quests[which];
  202. minimap->currentQuest = currentQuest;
  203. MetaString text;
  204. std::vector<Component> components;
  205. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  206. if (description->slider)
  207. description->slider->moveToMin(); // scroll text to start position
  208. description->setText (text.toString()); //TODO: use special log entry text
  209. vstd::clear_pointer(componentsBox);
  210. int componentsSize = components.size();
  211. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  212. if (componentsSize)
  213. {
  214. descriptionHeight -= 15;
  215. CComponent::ESize imageSize = CComponent::large;
  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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  251. std::vector<CComponent *> comps;
  252. for (auto & component : components)
  253. comps.push_back(new CComponent(component, imageSize));
  254. componentsBox = new CComponentBox(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  255. }
  256. description->resize(Point(385, descriptionHeight));
  257. minimap->update();
  258. redraw();
  259. }
  260. void CQuestLog::sliderMoved (int newpos)
  261. {
  262. recreateQuestList (newpos); //move components
  263. redraw();
  264. }
  265. void CQuestLog::toggleComplete(bool on)
  266. {
  267. OBJ_CONSTRUCTION_CAPTURING_ALL;
  268. hideComplete = on;
  269. recreateLabelList();
  270. recreateQuestList(0);
  271. redraw();
  272. }