CQuestLog.cpp 4.8 KB

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