CQuestLog.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "../../CCallback.h"
  12. #include "../../lib/CArtHandler.h"
  13. #include "../../lib/CConfigHandler.h"
  14. #include "../../lib/CGameState.h"
  15. #include "../../lib/CGeneralTextHandler.h"
  16. #include "../../lib/NetPacksBase.h"
  17. /*
  18. * CQuestLog.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. struct QuestInfo;
  27. class CAdvmapInterface;
  28. void CQuestLabel::clickLeft(tribool down, bool previousState)
  29. {
  30. if (down)
  31. callback();
  32. }
  33. void CQuestLabel::showAll(SDL_Surface * to)
  34. {
  35. if (active)
  36. CMultiLineLabel::showAll (to);
  37. }
  38. CQuestIcon::CQuestIcon (const std::string &defname, int index, int x, int y) :
  39. CAnimImage(defname, index, 0, x, y)
  40. {
  41. addUsedEvents(LCLICK);
  42. }
  43. void CQuestIcon::clickLeft(tribool down, bool previousState)
  44. {
  45. if (down)
  46. callback();
  47. }
  48. void CQuestIcon::showAll(SDL_Surface * to)
  49. {
  50. CSDL_Ext::CClipRectGuard guard(to, parent->pos);
  51. CAnimImage::showAll(to);
  52. }
  53. CQuestMinimap::CQuestMinimap (const Rect & position) :
  54. CMinimap (position),
  55. currentQuest (nullptr)
  56. {
  57. }
  58. void CQuestMinimap::addQuestMarks (const QuestInfo * q)
  59. {
  60. OBJ_CONSTRUCTION_CAPTURING_ALL;
  61. for (auto icon : icons)
  62. delete icon;
  63. icons.clear();
  64. int3 tile;
  65. if (q->obj)
  66. {
  67. tile = q->obj->pos;
  68. }
  69. else
  70. {
  71. tile = q->tile;
  72. }
  73. int x,y;
  74. minimap->tileToPixels (tile, x, y);
  75. if (level != tile.z)
  76. setLevel(tile.z);
  77. CQuestIcon * pic = new CQuestIcon ("VwSymbol.def", 3, x, y);
  78. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  79. pic->callback = std::bind (&CQuestMinimap::iconClicked, this);
  80. icons.push_back(pic);
  81. }
  82. void CQuestMinimap::update()
  83. {
  84. CMinimap::update();
  85. if (currentQuest)
  86. addQuestMarks (currentQuest);
  87. }
  88. void CQuestMinimap::iconClicked()
  89. {
  90. if (currentQuest->obj)
  91. adventureInt->centerOn (currentQuest->obj->pos);
  92. //moveAdvMapSelection();
  93. }
  94. void CQuestMinimap::showAll(SDL_Surface * to)
  95. {
  96. CIntObject::showAll(to); // blitting IntObject directly to hide radar
  97. for (auto pic : icons)
  98. pic->showAll(to);
  99. }
  100. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests) :
  101. CWindowObject(PLAYER_COLORED | BORDERED, "questDialog.pcx"),
  102. questIndex(0),
  103. currentQuest(nullptr),
  104. quests (Quests),
  105. slider(nullptr)
  106. {
  107. OBJ_CONSTRUCTION_CAPTURING_ALL;
  108. init();
  109. }
  110. void CQuestLog::init()
  111. {
  112. minimap = new CQuestMinimap (Rect (12, 12, 169, 169));
  113. // 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
  114. description = new CTextBox ("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  115. ok = new CButton(Point(539, 398), "IOKAY.DEF", CGI->generaltexth->zelp[445], boost::bind(&CQuestLog::close,this), SDLK_RETURN);
  116. int currentLabel = 0;
  117. for (int i = 0; i < quests.size(); ++i)
  118. {
  119. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  120. if (quests[i].quest->missionType == CQuest::MISSION_NONE)
  121. continue;
  122. MetaString text;
  123. quests[i].quest->getRolloverText (text, false);
  124. if (quests[i].obj)
  125. text.addReplacement (quests[i].obj->getObjectName()); //get name of the object
  126. CQuestLabel * label = new CQuestLabel (Rect(13, 195, 149,31), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  127. label->disable();
  128. label->callback = boost::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  129. labels.push_back(label);
  130. // Select latest active quest
  131. if (quests[i].quest->progress != CQuest::COMPLETE) // TODO: there need to be difference between active and completed quests
  132. selectQuest(i, currentLabel);
  133. currentLabel = labels.size();
  134. }
  135. recreateQuestList (0);
  136. slider = new CSlider(Point(166, 195), 191, std::bind (&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, currentLabel, false, CSlider::BROWN);
  137. if (currentLabel > QUEST_COUNT)
  138. slider->moveToMax();
  139. else
  140. slider->block(true);
  141. }
  142. void CQuestLog::showAll(SDL_Surface * to)
  143. {
  144. CWindowObject::showAll (to);
  145. for (auto label : labels)
  146. {
  147. label->show(to); //shows only if active
  148. }
  149. if (labels.size() && labels[questIndex]->active)
  150. {
  151. Rect rect = Rect::around(labels[questIndex]->pos);
  152. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  153. rect.w += 2;
  154. CSDL_Ext::drawBorder(to, rect, int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  155. }
  156. description->show(to);
  157. minimap->show(to);
  158. }
  159. void CQuestLog::recreateQuestList (int newpos)
  160. {
  161. for (int i = 0; i < labels.size(); ++i)
  162. {
  163. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  164. if (i >= newpos && i < newpos + QUEST_COUNT)
  165. labels[i]->enable();
  166. else
  167. labels[i]->disable();
  168. }
  169. minimap->update();
  170. }
  171. void CQuestLog::selectQuest (int which, int labelId)
  172. {
  173. questIndex = labelId;
  174. currentQuest = &quests[which];
  175. minimap->currentQuest = currentQuest;
  176. MetaString text;
  177. std::vector<Component> components; //TODO: display them
  178. currentQuest->quest->getVisitText (text, components , currentQuest->quest->isCustomFirst, true);
  179. if (description->slider)
  180. description->slider->moveToMin(); // scroll text to start position
  181. description->setText (text.toString()); //TODO: use special log entry text
  182. minimap->update();
  183. redraw();
  184. }
  185. void CQuestLog::sliderMoved (int newpos)
  186. {
  187. recreateQuestList (newpos); //move components
  188. redraw();
  189. }