CQuestLog.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. for (auto icon : icons)
  63. delete icon;
  64. icons.clear();
  65. int3 tile;
  66. if (q->obj)
  67. {
  68. tile = q->obj->pos;
  69. }
  70. else
  71. {
  72. tile = q->tile;
  73. }
  74. int x,y;
  75. minimap->tileToPixels (tile, x, y);
  76. if (level != tile.z)
  77. setLevel(tile.z);
  78. CQuestIcon * pic = new CQuestIcon ("VwSymbol.def", 3, x, y);
  79. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  80. pic->callback = std::bind (&CQuestMinimap::iconClicked, this);
  81. icons.push_back(pic);
  82. }
  83. void CQuestMinimap::update()
  84. {
  85. CMinimap::update();
  86. if (currentQuest)
  87. addQuestMarks (currentQuest);
  88. }
  89. void CQuestMinimap::iconClicked()
  90. {
  91. if (currentQuest->obj)
  92. adventureInt->centerOn (currentQuest->obj->pos);
  93. //moveAdvMapSelection();
  94. }
  95. void CQuestMinimap::showAll(SDL_Surface * to)
  96. {
  97. CIntObject::showAll(to); // blitting IntObject directly to hide radar
  98. for (auto pic : icons)
  99. pic->showAll(to);
  100. }
  101. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests) :
  102. CWindowObject(PLAYER_COLORED | BORDERED, "questDialog.pcx"),
  103. questIndex(0),
  104. currentQuest(nullptr),
  105. componentsBox(nullptr),
  106. quests (Quests),
  107. slider(nullptr)
  108. {
  109. OBJ_CONSTRUCTION_CAPTURING_ALL;
  110. init();
  111. }
  112. void CQuestLog::init()
  113. {
  114. minimap = new CQuestMinimap (Rect (12, 12, 169, 169));
  115. // 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
  116. description = new CTextBox ("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  117. ok = new CButton(Point(539, 398), "IOKAY.DEF", CGI->generaltexth->zelp[445], boost::bind(&CQuestLog::close,this), SDLK_RETURN);
  118. int currentLabel = 0;
  119. for (int i = 0; i < quests.size(); ++i)
  120. {
  121. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  122. if (quests[i].quest->missionType == CQuest::MISSION_NONE)
  123. continue;
  124. MetaString text;
  125. quests[i].quest->getRolloverText (text, false);
  126. if (quests[i].obj)
  127. text.addReplacement (quests[i].obj->getObjectName()); //get name of the object
  128. CQuestLabel * label = new CQuestLabel (Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  129. label->disable();
  130. label->callback = boost::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  131. labels.push_back(label);
  132. // Select latest active quest
  133. if (quests[i].quest->progress != CQuest::COMPLETE) // TODO: there need to be difference between active and completed quests
  134. selectQuest(i, currentLabel);
  135. currentLabel = labels.size();
  136. }
  137. recreateQuestList (0);
  138. slider = new CSlider(Point(166, 195), 191, std::bind (&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, currentLabel, false, CSlider::BROWN);
  139. if (currentLabel > QUEST_COUNT)
  140. slider->moveToMax();
  141. else
  142. slider->block(true);
  143. }
  144. void CQuestLog::showAll(SDL_Surface * to)
  145. {
  146. CWindowObject::showAll (to);
  147. for (auto label : labels)
  148. {
  149. label->show(to); //shows only if active
  150. }
  151. if (labels.size() && labels[questIndex]->active)
  152. {
  153. Rect rect = Rect::around(labels[questIndex]->pos);
  154. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  155. rect.w += 2;
  156. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  157. }
  158. description->show(to);
  159. minimap->show(to);
  160. }
  161. void CQuestLog::recreateQuestList (int newpos)
  162. {
  163. for (int i = 0; i < labels.size(); ++i)
  164. {
  165. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  166. if (i >= newpos && i < newpos + QUEST_COUNT)
  167. labels[i]->enable();
  168. else
  169. labels[i]->disable();
  170. }
  171. minimap->update();
  172. }
  173. void CQuestLog::selectQuest (int which, int labelId)
  174. {
  175. questIndex = labelId;
  176. currentQuest = &quests[which];
  177. minimap->currentQuest = currentQuest;
  178. MetaString text;
  179. std::vector<Component> components;
  180. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  181. if (description->slider)
  182. description->slider->moveToMin(); // scroll text to start position
  183. description->setText (text.toString()); //TODO: use special log entry text
  184. vstd::clear_pointer(componentsBox);
  185. int componentsSize = components.size();
  186. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  187. if (componentsSize)
  188. {
  189. descriptionHeight -= 15;
  190. CComponent::ESize imageSize = CComponent::large;
  191. switch (currentQuest->quest->missionType)
  192. {
  193. case CQuest::MISSION_ARMY:
  194. {
  195. if (componentsSize > 4)
  196. descriptionHeight -= 195;
  197. else
  198. descriptionHeight -= 100;
  199. break;
  200. }
  201. case CQuest::MISSION_ART:
  202. {
  203. if (componentsSize > 4)
  204. descriptionHeight -= 190;
  205. else
  206. descriptionHeight -= 90;
  207. break;
  208. }
  209. case CQuest::MISSION_PRIMARY_STAT:
  210. case CQuest::MISSION_RESOURCES:
  211. {
  212. if (componentsSize > 4)
  213. {
  214. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  215. descriptionHeight -= 140;
  216. }
  217. else
  218. descriptionHeight -= 125;
  219. break;
  220. }
  221. default:
  222. descriptionHeight -= 115;
  223. break;
  224. }
  225. OBJ_CONSTRUCTION_CAPTURING_ALL;
  226. std::vector<CComponent *> comps;
  227. for (auto & component : components)
  228. comps.push_back(new CComponent(component, imageSize));
  229. componentsBox = new CComponentBox(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  230. }
  231. description->resize(Point(385, descriptionHeight));
  232. minimap->update();
  233. redraw();
  234. }
  235. void CQuestLog::sliderMoved (int newpos)
  236. {
  237. recreateQuestList (newpos); //move components
  238. redraw();
  239. }