2
0

CQuestLog.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * CQuestLog.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CQuestLog.h"
  12. #include "../CGameInfo.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/CComponent.h"
  18. #include "../widgets/Slider.h"
  19. #include "../adventureMap/AdventureMapInterface.h"
  20. #include "../adventureMap/CMinimap.h"
  21. #include "../render/Canvas.h"
  22. #include "../renderSDL/SDL_Extensions.h"
  23. #include "../../CCallback.h"
  24. #include "../../lib/CArtHandler.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/gameState/QuestInfo.h"
  27. #include "../../lib/CGeneralTextHandler.h"
  28. #include "../../lib/MetaString.h"
  29. #include "../../lib/mapObjects/CQuest.h"
  30. VCMI_LIB_NAMESPACE_BEGIN
  31. struct QuestInfo;
  32. VCMI_LIB_NAMESPACE_END
  33. class CAdvmapInterface;
  34. void CQuestLabel::clickLeft(tribool down, bool previousState)
  35. {
  36. if (down)
  37. callback();
  38. }
  39. void CQuestLabel::showAll(Canvas & to)
  40. {
  41. CMultiLineLabel::showAll (to);
  42. }
  43. CQuestIcon::CQuestIcon (const std::string &defname, int index, int x, int y) :
  44. CAnimImage(defname, index, 0, x, y)
  45. {
  46. addUsedEvents(LCLICK);
  47. }
  48. void CQuestIcon::clickLeft(tribool down, bool previousState)
  49. {
  50. if (down)
  51. callback();
  52. }
  53. void CQuestIcon::showAll(Canvas & to)
  54. {
  55. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), parent->pos);
  56. CAnimImage::showAll(to);
  57. }
  58. CQuestMinimap::CQuestMinimap(const Rect & position)
  59. : CMinimap(position),
  60. currentQuest(nullptr)
  61. {
  62. }
  63. void CQuestMinimap::addQuestMarks (const QuestInfo * q)
  64. {
  65. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  66. icons.clear();
  67. int3 tile;
  68. if (q->obj)
  69. tile = q->obj->pos;
  70. else
  71. tile = q->tile;
  72. Point offset = tileToPixels(tile);
  73. onMapViewMoved(Rect(), tile.z);
  74. auto pic = std::make_shared<CQuestIcon>("VwSymbol.def", 3, offset.x, offset.y);
  75. pic->moveBy (Point ( -pic->pos.w/2, -pic->pos.h/2));
  76. pic->callback = std::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->centerOnTile(currentQuest->obj->pos);
  89. //moveAdvMapSelection();
  90. }
  91. void CQuestMinimap::showAll(Canvas & 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 | BORDERED, "questDialog"),
  99. questIndex(0),
  100. currentQuest(nullptr),
  101. hideComplete(false),
  102. quests(Quests)
  103. {
  104. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  105. minimap = std::make_shared<CQuestMinimap>(Rect(12, 12, 169, 169));
  106. // 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
  107. description = std::make_shared<CTextBox>("", Rect(205, 18, 385, DESCRIPTION_HEIGHT_MAX), CSlider::BROWN, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE);
  108. ok = std::make_shared<CButton>(Point(539, 398), "IOKAY.DEF", CGI->generaltexth->zelp[445], std::bind(&CQuestLog::close, this), EShortcut::GLOBAL_ACCEPT);
  109. // Both button and lable are shifted to -2px by x and y to not make them actually look like they're on same line with quests list and ok button
  110. hideCompleteButton = std::make_shared<CToggleButton>(Point(10, 396), "sysopchk.def", CButton::tooltipLocalized("vcmi.questLog.hideComplete"), std::bind(&CQuestLog::toggleComplete, this, _1));
  111. hideCompleteLabel = std::make_shared<CLabel>(46, 398, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->translate("vcmi.questLog.hideComplete.hover"));
  112. slider = std::make_shared<CSlider>(Point(166, 195), 191, std::bind(&CQuestLog::sliderMoved, this, _1), QUEST_COUNT, 0, 0, Orientation::VERTICAL, CSlider::BROWN);
  113. slider->setPanningStep(32);
  114. recreateLabelList();
  115. recreateQuestList(0);
  116. }
  117. void CQuestLog::recreateLabelList()
  118. {
  119. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  120. labels.clear();
  121. bool completeMissing = true;
  122. int currentLabel = 0;
  123. for (int i = 0; i < quests.size(); ++i)
  124. {
  125. // Quests with MISSION_NONE type don't have text for them and can't be displayed
  126. if (quests[i].quest->missionType == CQuest::MISSION_NONE)
  127. continue;
  128. if (quests[i].quest->progress == CQuest::COMPLETE)
  129. {
  130. completeMissing = false;
  131. if (hideComplete)
  132. continue;
  133. }
  134. MetaString text;
  135. quests[i].quest->getRolloverText (text, false);
  136. if (quests[i].obj)
  137. {
  138. if (auto seersHut = dynamic_cast<const CGSeerHut *>(quests[i].obj))
  139. {
  140. MetaString toSeer;
  141. toSeer.appendRawString(VLC->generaltexth->allTexts[347]);
  142. toSeer.replaceRawString(seersHut->seerName);
  143. text.replaceRawString(toSeer.toString());
  144. }
  145. else
  146. text.replaceRawString(quests[i].obj->getObjectName()); //get name of the object
  147. }
  148. auto label = std::make_shared<CQuestLabel>(Rect(13, 195, 149,31), FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, text.toString());
  149. label->disable();
  150. label->callback = std::bind(&CQuestLog::selectQuest, this, i, currentLabel);
  151. labels.push_back(label);
  152. // Select latest active quest
  153. if (quests[i].quest->progress != CQuest::COMPLETE)
  154. selectQuest(i, currentLabel);
  155. currentLabel = static_cast<int>(labels.size());
  156. }
  157. if (completeMissing) // We can't use block(completeMissing) because if false button state reset to NORMAL
  158. hideCompleteButton->block(true);
  159. slider->setAmount(currentLabel);
  160. if (currentLabel > QUEST_COUNT)
  161. {
  162. slider->block(false);
  163. slider->scrollToMax();
  164. }
  165. else
  166. {
  167. slider->block(true);
  168. slider->scrollToMin();
  169. }
  170. }
  171. void CQuestLog::showAll(Canvas & to)
  172. {
  173. CWindowObject::showAll(to);
  174. if(questIndex >= 0 && questIndex < labels.size())
  175. {
  176. //TODO: use child object to selection rect
  177. Rect rect = Rect::createAround(labels[questIndex]->pos, 1);
  178. rect.x -= 2; // Adjustment needed as we want selection box on top of border in graphics
  179. rect.w += 2;
  180. to.drawBorder(rect, Colors::METALLIC_GOLD);
  181. }
  182. }
  183. void CQuestLog::recreateQuestList (int newpos)
  184. {
  185. for (int i = 0; i < labels.size(); ++i)
  186. {
  187. labels[i]->pos = Rect (pos.x + 14, pos.y + 195 + (i-newpos) * 32, 151, 31);
  188. if (i >= newpos && i < newpos + QUEST_COUNT)
  189. labels[i]->enable();
  190. else
  191. labels[i]->disable();
  192. }
  193. minimap->update();
  194. }
  195. void CQuestLog::selectQuest(int which, int labelId)
  196. {
  197. questIndex = labelId;
  198. currentQuest = &quests[which];
  199. minimap->currentQuest = currentQuest;
  200. MetaString text;
  201. std::vector<Component> components;
  202. currentQuest->quest->getVisitText (text, components, currentQuest->quest->isCustomFirst, true);
  203. if(description->slider)
  204. description->slider->scrollToMin(); // scroll text to start position
  205. description->setText(text.toString()); //TODO: use special log entry text
  206. componentsBox.reset();
  207. int componentsSize = static_cast<int>(components.size());
  208. int descriptionHeight = DESCRIPTION_HEIGHT_MAX;
  209. if(componentsSize)
  210. {
  211. descriptionHeight -= 15;
  212. CComponent::ESize imageSize = CComponent::large;
  213. switch (currentQuest->quest->missionType)
  214. {
  215. case CQuest::MISSION_ARMY:
  216. {
  217. if (componentsSize > 4)
  218. descriptionHeight -= 195;
  219. else
  220. descriptionHeight -= 100;
  221. break;
  222. }
  223. case CQuest::MISSION_ART:
  224. {
  225. if (componentsSize > 4)
  226. descriptionHeight -= 190;
  227. else
  228. descriptionHeight -= 90;
  229. break;
  230. }
  231. case CQuest::MISSION_PRIMARY_STAT:
  232. case CQuest::MISSION_RESOURCES:
  233. {
  234. if (componentsSize > 4)
  235. {
  236. imageSize = CComponent::small; // Only small icons can be used for resources as 4+ icons take too much space
  237. descriptionHeight -= 140;
  238. }
  239. else
  240. descriptionHeight -= 125;
  241. break;
  242. }
  243. default:
  244. descriptionHeight -= 115;
  245. break;
  246. }
  247. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  248. std::vector<std::shared_ptr<CComponent>> comps;
  249. for(auto & component : components)
  250. {
  251. auto c = std::make_shared<CComponent>(component, imageSize);
  252. comps.push_back(c);
  253. }
  254. componentsBox = std::make_shared<CComponentBox>(comps, Rect(202, 20+descriptionHeight+15, 391, DESCRIPTION_HEIGHT_MAX-(20+descriptionHeight)));
  255. }
  256. description->resize(Point(385, descriptionHeight));
  257. minimap->update();
  258. redraw();
  259. }
  260. void CQuestLog::sliderMoved(int newpos)
  261. {
  262. recreateQuestList(newpos); //move components
  263. redraw();
  264. }
  265. void CQuestLog::toggleComplete(bool on)
  266. {
  267. hideComplete = on;
  268. recreateLabelList();
  269. recreateQuestList(0);
  270. redraw();
  271. }