CQuestLog.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 "UIFramework/SDL_Extensions.h"
  8. #include "CBitmapHandler.h"
  9. #include "CDefHandler.h"
  10. #include "Graphics.h"
  11. #include "CPlayerInterface.h"
  12. #include "CConfigHandler.h"
  13. #include "../lib/CGameState.h"
  14. #include "../lib/CArtHandler.h"
  15. #include "../lib/NetPacks.h"
  16. #include "../lib/CObjectHandler.h"
  17. #include "UIFramework/CGuiHandler.h"
  18. #include "UIFramework/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 &bmpname, int x, int y) :
  41. CPicture (bmpname, 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. if(bg)
  53. {
  54. if(srcRect)
  55. {
  56. SDL_Rect srcRectCpy = *srcRect;
  57. SDL_Rect dstRect = srcRectCpy;
  58. dstRect.x = pos.x;
  59. dstRect.y = pos.y;
  60. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  61. }
  62. else //TODO: allow blitting with offset correction (center of picture on the center of pos)
  63. {
  64. SDL_Rect dstRect = pos;
  65. dstRect.x -= pos.w + 2;
  66. dstRect.y -= pos.h + 2;
  67. blitAt(bg, dstRect, to);
  68. }
  69. }
  70. }
  71. CQuestMinimap::CQuestMinimap (const Rect & position) :
  72. CMinimap (position),
  73. currentQuest (NULL)
  74. {
  75. }
  76. void CQuestMinimap::addQuestMarks (const QuestInfo * q)
  77. {
  78. OBJ_CONSTRUCTION_CAPTURING_ALL;
  79. icons.clear();
  80. int3 tile;
  81. if (q->obj)
  82. {
  83. tile = q->obj->pos;
  84. }
  85. else
  86. {
  87. tile = q->tile;
  88. }
  89. CQuestIcon * pic = new CQuestIcon ("", 0, 0);
  90. CDefHandler * def = CDefHandler::giveDef("VwSymbol.def");
  91. CSDL_Ext::alphaTransform(def->ourImages[3].bitmap);
  92. pic->bg = def->ourImages[3].bitmap;
  93. pic->pos.w = 8;
  94. pic->pos.h = 8;
  95. int x, y;
  96. minimap->tileToPixels (tile, x, y);
  97. pic->moveTo (Point (minimap->pos.x, minimap->pos.y), true);
  98. pic->pos.x += x - pic->pos.w / 2 - 1;
  99. pic->pos.y += y - pic->pos.h / 2 - 1;
  100. pic->callback = boost::bind (&CQuestMinimap::iconClicked, this);
  101. icons.push_back(pic);
  102. }
  103. void CQuestMinimap::update()
  104. {
  105. CMinimap::update();
  106. if (currentQuest)
  107. addQuestMarks (currentQuest);
  108. }
  109. void CQuestMinimap::iconClicked()
  110. {
  111. if (currentQuest->obj)
  112. adventureInt->centerOn (currentQuest->obj->pos);
  113. moveAdvMapSelection();
  114. }
  115. void CQuestMinimap::showAll(SDL_Surface * to)
  116. {
  117. CMinimap::showAll(to);
  118. BOOST_FOREACH (auto pic, icons)
  119. pic->showAll(to);
  120. }
  121. CQuestLog::CQuestLog (const std::vector<QuestInfo> & Quests) :
  122. CWindowObject(PLAYER_COLORED, "QuestLog.pcx"),
  123. questIndex(0),
  124. currentQuest(NULL),
  125. quests (Quests),
  126. slider(NULL)
  127. {
  128. OBJ_CONSTRUCTION_CAPTURING_ALL;
  129. init();
  130. }
  131. void CQuestLog::init()
  132. {
  133. minimap = new CQuestMinimap (Rect (47, 33, 144, 144));
  134. description = new CTextBox ("", Rect(245, 33, 350, 355), 1, FONT_MEDIUM, TOPLEFT, Colors::Cornsilk);
  135. ok = new CAdventureMapButton("",CGI->generaltexth->zelp[445].second, boost::bind(&CQuestLog::close,this), 547, 401, "IOKAY.DEF", SDLK_RETURN);
  136. if (quests.size() > QUEST_COUNT)
  137. slider = new CSlider(203, 199, 230, boost::bind (&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, quests.size(), false, 0);
  138. auto map = LOCPLINT->cb->getVisibilityMap(); //TODO: another function to get all tiles?
  139. for (int g = 0; g < map.size(); ++g)
  140. for (int h = 0; h < map[g].size(); ++h)
  141. for (int y = 0; y < map[g][h].size(); ++y)
  142. minimap->showTile (int3 (g, h, y));
  143. for (int i = 0; i < quests.size(); ++i)
  144. {
  145. MetaString text;
  146. quests[i].quest->getRolloverText (text, false);
  147. if (quests[i].obj)
  148. text.addReplacement (quests[i].obj->getHoverText()); //get name of the object
  149. CQuestLabel * label = new CQuestLabel (28, 199 + i * 24, FONT_SMALL, TOPLEFT, Colors::Cornsilk, text.toString());
  150. label->callback = boost::bind(&CQuestLog::selectQuest, this, i);
  151. label->setBounds (172, 30);
  152. labels.push_back(label);
  153. }
  154. recreateQuestList (0);
  155. showAll (screen2);
  156. }
  157. void CQuestLog::showAll(SDL_Surface * to)
  158. {
  159. CIntObject::showAll (to);
  160. BOOST_FOREACH (auto label, labels)
  161. {
  162. label->show(to); //shows only if active
  163. }
  164. if (labels.size() && labels[questIndex]->active)
  165. {
  166. CSDL_Ext::drawBorder(to, Rect::around(labels[questIndex]->pos), int3(Colors::MetallicGold.r, Colors::MetallicGold.g, Colors::MetallicGold.b));
  167. }
  168. description->show(to);
  169. minimap->update();
  170. minimap->show(to);
  171. }
  172. void CQuestLog::recreateQuestList (int newpos)
  173. {
  174. for (int i = 0; i < labels.size(); ++i)
  175. {
  176. labels[i]->pos = Rect (pos.x + 28, pos.y + 207 + (i-newpos) * 25, 173, 23);
  177. if (i >= newpos && i < newpos + QUEST_COUNT)
  178. {
  179. labels[i]->activate();
  180. }
  181. else
  182. {
  183. labels[i]->deactivate();
  184. }
  185. }
  186. }
  187. void CQuestLog::selectQuest (int which)
  188. {
  189. questIndex = which;
  190. currentQuest = &quests[which];
  191. minimap->currentQuest = currentQuest;
  192. MetaString text;
  193. std::vector<Component> components; //TODO: display them
  194. currentQuest->quest->getVisitText (text, components , currentQuest->quest->isCustomFirst, true);
  195. description->setTxt (text.toString()); //TODO: use special log entry text
  196. redraw();
  197. }
  198. void CQuestLog::sliderMoved (int newpos)
  199. {
  200. recreateQuestList (newpos); //move components
  201. redraw();
  202. }