CHighScoreScreen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * CHighScoreScreen.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 "CHighScoreScreen.h"
  12. #include "CStatisticScreen.h"
  13. #include "CMainMenu.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/WindowHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../media/IMusicPlayer.h"
  18. #include "../media/ISoundPlayer.h"
  19. #include "../widgets/Buttons.h"
  20. #include "../widgets/CTextInput.h"
  21. #include "../widgets/Images.h"
  22. #include "../widgets/GraphicalPrimitiveCanvas.h"
  23. #include "../widgets/VideoWidget.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../render/Canvas.h"
  27. #include "../render/IRenderHandler.h"
  28. #include "../CGameInfo.h"
  29. #include "../../lib/texts/CGeneralTextHandler.h"
  30. #include "../../lib/texts/TextOperations.h"
  31. #include "../../lib/CConfigHandler.h"
  32. #include "../../lib/CCreatureHandler.h"
  33. #include "../../lib/constants/EntityIdentifiers.h"
  34. #include "../../lib/gameState/HighScore.h"
  35. #include "../../lib/gameState/GameStatistics.h"
  36. CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
  37. : CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
  38. {
  39. addUsedEvents(SHOW_POPUP);
  40. OBJECT_CONSTRUCTION;
  41. pos = center(Rect(0, 0, 800, 600));
  42. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  43. addHighScores();
  44. addButtons();
  45. }
  46. void CHighScoreScreen::showPopupWindow(const Point & cursorPosition)
  47. {
  48. for (int i = 0; i < screenRows; i++)
  49. {
  50. bool currentGameNotInListEntry = i == (screenRows - 1) && highlighted > (screenRows - 1);
  51. Rect r = Rect(80, 40 + i * 50, 635, 50);
  52. if(r.isInside(cursorPosition - pos))
  53. {
  54. std::string tmp = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"][currentGameNotInListEntry ? highlighted : i]["datetime"].String();
  55. if(!tmp.empty())
  56. CRClickPopup::createAndPush(tmp);
  57. }
  58. }
  59. }
  60. void CHighScoreScreen::addButtons()
  61. {
  62. OBJECT_CONSTRUCTION;
  63. buttons.clear();
  64. buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [this](){ buttonCampaignClick(); }, EShortcut::HIGH_SCORES_CAMPAIGNS));
  65. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [this](){ buttonScenarioClick(); }, EShortcut::HIGH_SCORES_SCENARIOS));
  66. buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [this](){ buttonResetClick(); }, EShortcut::HIGH_SCORES_RESET));
  67. buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [this](){ buttonExitClick(); }, EShortcut::GLOBAL_RETURN));
  68. }
  69. void CHighScoreScreen::addHighScores()
  70. {
  71. OBJECT_CONSTRUCTION;
  72. background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
  73. texts.clear();
  74. images.clear();
  75. // Header
  76. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433"))); // rank
  77. texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434"))); // player
  78. if(highscorepage == HighScorePage::SCENARIO)
  79. {
  80. texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435"))); // land
  81. texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436"))); // days
  82. texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
  83. }
  84. else
  85. {
  86. texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672"))); // campaign
  87. texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
  88. }
  89. // Content
  90. int y = 66;
  91. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
  92. for (int i = 0; i < screenRows; i++)
  93. {
  94. bool currentGameNotInListEntry = (i == (screenRows - 1) && highlighted > (screenRows - 1));
  95. auto & curData = data[currentGameNotInListEntry ? highlighted : i];
  96. ColorRGBA color = (i == highlighted || currentGameNotInListEntry) ? Colors::YELLOW : Colors::WHITE;
  97. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string((currentGameNotInListEntry ? highlighted : i) + 1)));
  98. texts.push_back(std::make_shared<CLabel>(225, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["player"].String(), 120));
  99. if(highscorepage == HighScorePage::SCENARIO)
  100. {
  101. texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["scenarioName"].String(), 200));
  102. texts.push_back(std::make_shared<CLabel>(557, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["days"].Integer())));
  103. texts.push_back(std::make_shared<CLabel>(627, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  104. }
  105. else
  106. {
  107. texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["campaignName"].String(), 200));
  108. texts.push_back(std::make_shared<CLabel>(592, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
  109. }
  110. if(curData["points"].Integer() > 0 && curData["points"].Integer() <= ((highscorepage == HighScorePage::CAMPAIGN) ? 2500 : 500))
  111. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(curData["points"].Integer(), highscorepage == HighScorePage::CAMPAIGN)]->getIconIndex(), 0, 670, y - 15 + i * 50));
  112. }
  113. }
  114. void CHighScoreScreen::buttonCampaignClick()
  115. {
  116. highscorepage = HighScorePage::CAMPAIGN;
  117. addHighScores();
  118. addButtons();
  119. redraw();
  120. }
  121. void CHighScoreScreen::buttonScenarioClick()
  122. {
  123. OBJECT_CONSTRUCTION;
  124. highscorepage = HighScorePage::SCENARIO;
  125. addHighScores();
  126. addButtons();
  127. redraw();
  128. }
  129. void CHighScoreScreen::buttonResetClick()
  130. {
  131. CInfoWindow::showYesNoDialog(
  132. CGI->generaltexth->allTexts[666],
  133. {},
  134. [this]()
  135. {
  136. Settings entry = persistentStorage.write["highscore"];
  137. entry->clear();
  138. addHighScores();
  139. addButtons();
  140. redraw();
  141. },
  142. 0
  143. );
  144. }
  145. void CHighScoreScreen::buttonExitClick()
  146. {
  147. close();
  148. CMM->playMusic();
  149. }
  150. CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc, const StatisticDataSet & statistic)
  151. : CWindowObject(BORDERED), won(won), calc(calc), stat(statistic)
  152. {
  153. addUsedEvents(LCLICK | KEYBOARD);
  154. OBJECT_CONSTRUCTION;
  155. pos = center(Rect(0, 0, 800, 600));
  156. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  157. background = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), Colors::BLACK);
  158. if(won)
  159. {
  160. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), VideoPath::builtin("HSANIM.SMK"), VideoPath::builtin("HSLOOP.SMK"), true);
  161. int border = 100;
  162. int textareaW = ((pos.w - 2 * border) / 4);
  163. std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
  164. for (int i = 0; i < 5; i++)
  165. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 450, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt." + t[i])));
  166. std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
  167. t = { std::to_string(calc.calculate().sumDays), std::to_string(calc.calculate().basic), CGI->generaltexth->translate("core.arraytxt." + std::to_string((142 + calc.parameters[0].difficulty))), std::to_string(calc.calculate().total), creatureName };
  168. for (int i = 0; i < 5; i++)
  169. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(textareaW * i + border - (textareaW / 2), 530, textareaW, 100), FONT_HIGH_SCORE, ETextAlignment::TOPCENTER, Colors::WHITE, t[i]));
  170. CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
  171. }
  172. else
  173. {
  174. videoPlayer = std::make_shared<VideoWidgetOnce>(Point(0, 0), VideoPath::builtin("LOSEGAME.SMK"), true, this);
  175. CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
  176. }
  177. if (settings["general"]["enableUiEnhancements"].Bool())
  178. {
  179. statisticButton = std::make_shared<CButton>(Point(726, 10), AnimationPath::builtin("TPTAV02.DEF"), CButton::tooltip(CGI->generaltexth->translate("vcmi.statisticWindow.statistics")), [this](){ GH.windows().createAndPushWindow<CStatisticScreen>(stat); }, EShortcut::HIGH_SCORES_STATISTICS);
  180. texts.push_back(std::make_shared<CLabel>(716, 25, EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTERRIGHT, Colors::WHITE, CGI->generaltexth->translate("vcmi.statisticWindow.statistics") + ":"));
  181. }
  182. }
  183. void CHighScoreInputScreen::stopMusicAndClose()
  184. {
  185. CMM->playMusic();
  186. close();
  187. }
  188. void CHighScoreInputScreen::onVideoPlaybackFinished()
  189. {
  190. stopMusicAndClose();
  191. }
  192. int CHighScoreInputScreen::addEntry(std::string text) {
  193. std::vector<JsonNode> baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"].Vector();
  194. auto sortFunctor = [](const JsonNode & left, const JsonNode & right)
  195. {
  196. if(left["points"].Integer() == right["points"].Integer())
  197. return left["posFlag"].Bool() > right["posFlag"].Bool();
  198. return left["points"].Integer() > right["points"].Integer();
  199. };
  200. JsonNode newNode = JsonNode();
  201. newNode["player"].String() = text;
  202. if(calc.isCampaign)
  203. newNode["campaignName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaignName;
  204. else
  205. newNode["scenarioName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].scenarioName;
  206. newNode["days"].Integer() = calc.calculate().sumDays;
  207. newNode["points"].Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
  208. newNode["datetime"].String() = TextOperations::getFormattedDateTimeLocal(std::time(nullptr));
  209. newNode["posFlag"].Bool() = true;
  210. baseNode.push_back(newNode);
  211. boost::range::sort(baseNode, sortFunctor);
  212. int pos = -1;
  213. for (int i = 0; i < baseNode.size(); i++)
  214. {
  215. if(!baseNode[i]["posFlag"].isNull())
  216. {
  217. baseNode[i]["posFlag"].clear();
  218. pos = i;
  219. }
  220. }
  221. Settings s = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"];
  222. s->Vector() = baseNode;
  223. return pos;
  224. }
  225. void CHighScoreInputScreen::show(Canvas & to)
  226. {
  227. CWindowObject::showAll(to);
  228. }
  229. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  230. {
  231. if(statisticButton->pos.isInside(cursorPosition))
  232. return;
  233. OBJECT_CONSTRUCTION;
  234. if(!won)
  235. {
  236. stopMusicAndClose();
  237. return;
  238. }
  239. if(!input)
  240. {
  241. input = std::make_shared<CHighScoreInput>(calc.parameters[0].playerName,
  242. [&] (std::string text)
  243. {
  244. if(!text.empty())
  245. {
  246. int pos = addEntry(text);
  247. close();
  248. GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
  249. }
  250. else
  251. stopMusicAndClose();
  252. });
  253. }
  254. }
  255. void CHighScoreInputScreen::keyPressed(EShortcut key)
  256. {
  257. if(key == EShortcut::HIGH_SCORES_STATISTICS) // ignore shortcut for skipping video with key
  258. return;
  259. clickPressed(Point());
  260. }
  261. CHighScoreInput::CHighScoreInput(std::string playerName, std::function<void(std::string text)> readyCB)
  262. : CWindowObject(NEEDS_ANIMATED_BACKGROUND, ImagePath::builtin("HIGHNAME")), ready(readyCB)
  263. {
  264. OBJECT_CONSTRUCTION;
  265. pos = center(Rect(0, 0, 232, 212));
  266. updateShadow();
  267. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  268. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  269. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  270. // FIXME: broken. Never activates?
  271. // statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  272. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, ETextAlignment::CENTER, true);
  273. textInput->setText(playerName);
  274. }
  275. void CHighScoreInput::okay()
  276. {
  277. ready(textInput->getText());
  278. }
  279. void CHighScoreInput::abort()
  280. {
  281. ready("");
  282. }