2
0

CHighScoreScreen.cpp 13 KB

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