CQuestLog.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "StdInc.h"
  2. #include "CQuestLog.h"
  3. #include "CGameInfo.h"
  4. #include "../lib/CGeneralTextHandler.h"
  5. #include "../CCallback.h"
  6. #include <SDL.h>
  7. #include "gui/SDL_Extensions.h"
  8. #include "CBitmapHandler.h"
  9. #include "CDefHandler.h"
  10. #include "Graphics.h"
  11. #include "CPlayerInterface.h"
  12. #include "../lib/CConfigHandler.h"
  13. #include "../lib/CGameState.h"
  14. #include "../lib/CArtHandler.h"
  15. #include "../lib/NetPacksBase.h"
  16. #include "gui/CGuiHandler.h"
  17. #include "gui/CIntObjectClasses.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. {
  66. tile = q->obj->pos;
  67. }
  68. else
  69. {
  70. tile = q->tile;
  71. }
  72. int x,y;
  73. minimap->tileToPixels (tile, x, y);
  74. CQuestIcon * pic = new CQuestIcon ("VwSymbol.def", 3, x, y);
  75. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  76. pic->callback = boost::bind (&CQuestMinimap::iconClicked, this);
  77. icons.push_back(pic);
  78. }
  79. void CQuestMinimap::update()
  80. {
  81. CMinimap::update();
  82. if (currentQuest)
  83. addQuestMarks (currentQuest);
  84. }
  85. void CQuestMinimap::iconClicked()
  86. {
  87. if (currentQuest->obj)
  88. adventureInt->centerOn (currentQuest->obj->pos);
  89. moveAdvMapSelection();
  90. }
  91. void CQuestMinimap::showAll(SDL_Surface * to)
  92. {
  93. CIntObject::showAll(to); // blitting IntObject directly to hide radar
  94. for (auto pic : icons)
  95. pic->showAll(to);
  96. }
  97. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests) :
  98. CWindowObject(PLAYER_COLORED, "QuestLog.pcx"),
  99. questIndex(0),
  100. currentQuest(nullptr),
  101. quests (Quests),
  102. slider(nullptr)
  103. {
  104. OBJ_CONSTRUCTION_CAPTURING_ALL;
  105. init();
  106. }
  107. void CQuestLog::init()
  108. {
  109. minimap = new CQuestMinimap (Rect (47, 33, 144, 144));
  110. description = new CTextBox ("", Rect(245, 33, 350, 355), 1, FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  111. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CQuestLog::close,this), 547, 401, "IOKAY.DEF", SDLK_RETURN);
  112. if (quests.size() > QUEST_COUNT)
  113. slider = new CSlider(203, 199, 230, boost::bind (&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, quests.size(), false, 0);
  114. for (int i = 0; i < quests.size(); ++i)
  115. {
  116. MetaString text;
  117. quests[i].quest->getRolloverText (text, false);
  118. if (quests[i].obj)
  119. text.addReplacement (quests[i].obj->getObjectName()); //get name of the object
  120. CQuestLabel * label = new CQuestLabel (Rect(28, 199 + i * 24, 172,30), FONT_SMALL, TOPLEFT, Colors::WHITE, text.toString());
  121. label->callback = boost::bind(&CQuestLog::selectQuest, this, i);
  122. labels.push_back(label);
  123. }
  124. recreateQuestList (0);
  125. }
  126. void CQuestLog::showAll(SDL_Surface * to)
  127. {
  128. CIntObject::showAll (to);
  129. for (auto label : labels)
  130. {
  131. label->show(to); //shows only if active
  132. }
  133. if (labels.size() && labels[questIndex]->active)
  134. {
  135. CSDL_Ext::drawBorder(to, Rect::around(labels[questIndex]->pos), int3(Colors::METALLIC_GOLD.r, Colors::METALLIC_GOLD.g, Colors::METALLIC_GOLD.b));
  136. }
  137. description->show(to);
  138. minimap->update();
  139. minimap->show(to);
  140. }
  141. void CQuestLog::recreateQuestList (int newpos)
  142. {
  143. for (int i = 0; i < labels.size(); ++i)
  144. {
  145. labels[i]->pos = Rect (pos.x + 28, pos.y + 207 + (i-newpos) * 25, 173, 23);
  146. if (i >= newpos && i < newpos + QUEST_COUNT)
  147. {
  148. labels[i]->activate();
  149. }
  150. else
  151. {
  152. labels[i]->deactivate();
  153. }
  154. }
  155. }
  156. void CQuestLog::selectQuest (int which)
  157. {
  158. questIndex = which;
  159. currentQuest = &quests[which];
  160. minimap->currentQuest = currentQuest;
  161. MetaString text;
  162. std::vector<Component> components; //TODO: display them
  163. currentQuest->quest->getVisitText (text, components , currentQuest->quest->isCustomFirst, true);
  164. description->setText (text.toString()); //TODO: use special log entry text
  165. redraw();
  166. }
  167. void CQuestLog::sliderMoved (int newpos)
  168. {
  169. recreateQuestList (newpos); //move components
  170. redraw();
  171. }