CHighScoreScreen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "../gui/CGuiHandler.h"
  14. #include "../gui/WindowHandler.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../media/IMusicPlayer.h"
  17. #include "../media/ISoundPlayer.h"
  18. #include "../widgets/Buttons.h"
  19. #include "../widgets/CTextInput.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/GraphicalPrimitiveCanvas.h"
  22. #include "../widgets/VideoWidget.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../render/Canvas.h"
  26. #include "../render/IRenderHandler.h"
  27. #include "../CGameInfo.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/texts/TextOperations.h"
  30. #include "../../lib/CConfigHandler.h"
  31. #include "../../lib/CCreatureHandler.h"
  32. #include "../../lib/constants/EntityIdentifiers.h"
  33. #include "../../lib/gameState/HighScore.h"
  34. #include "../../lib/gameState/GameStatistics.h"
  35. CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
  36. : CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
  37. {
  38. addUsedEvents(SHOW_POPUP);
  39. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  40. pos = center(Rect(0, 0, 800, 600));
  41. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  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. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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. }
  148. CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc, StatisticDataSet statistic)
  149. : CWindowObject(BORDERED), won(won), calc(calc), stat(statistic)
  150. {
  151. addUsedEvents(LCLICK | KEYBOARD);
  152. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  153. pos = center(Rect(0, 0, 800, 600));
  154. backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
  155. background = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), Colors::BLACK);
  156. if(won)
  157. {
  158. videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), VideoPath::builtin("HSANIM.SMK"), VideoPath::builtin("HSLOOP.SMK"), true);
  159. int border = 100;
  160. int textareaW = ((pos.w - 2 * border) / 4);
  161. std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
  162. for (int i = 0; i < 5; i++)
  163. 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])));
  164. std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
  165. 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 };
  166. for (int i = 0; i < 5; i++)
  167. 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]));
  168. CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
  169. }
  170. else
  171. {
  172. videoPlayer = std::make_shared<VideoWidgetOnce>(Point(0, 0), VideoPath::builtin("LOSEGAME.SMK"), true, [this](){close();});
  173. CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
  174. }
  175. if (settings["general"]["enableUiEnhancements"].Bool())
  176. {
  177. statisticButton = std::make_shared<CButton>(Point(736, 0), AnimationPath::builtin("TPTAV02.DEF"), CButton::tooltip(CGI->generaltexth->translate("vcmi.statisticWindow.statistic")), [this](){ GH.windows().createAndPushWindow<CStatisticScreen>(stat); }, EShortcut::HIGH_SCORES_STATISTIC);
  178. texts.push_back(std::make_shared<CMultiLineLabel>(Rect(0, 0, 726, 32), FONT_HIGH_SCORE, ETextAlignment::CENTERRIGHT, Colors::WHITE, CGI->generaltexth->translate("vcmi.statisticWindow.statistic") + ":"));
  179. }
  180. }
  181. int CHighScoreInputScreen::addEntry(std::string text) {
  182. std::vector<JsonNode> baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"].Vector();
  183. auto sortFunctor = [](const JsonNode & left, const JsonNode & right)
  184. {
  185. if(left["points"].Integer() == right["points"].Integer())
  186. return left["posFlag"].Bool() > right["posFlag"].Bool();
  187. return left["points"].Integer() > right["points"].Integer();
  188. };
  189. JsonNode newNode = JsonNode();
  190. newNode["player"].String() = text;
  191. if(calc.isCampaign)
  192. newNode["campaignName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaignName;
  193. else
  194. newNode["scenarioName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].scenarioName;
  195. newNode["days"].Integer() = calc.calculate().sumDays;
  196. newNode["points"].Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
  197. newNode["datetime"].String() = TextOperations::getFormattedDateTimeLocal(std::time(nullptr));
  198. newNode["posFlag"].Bool() = true;
  199. baseNode.push_back(newNode);
  200. boost::range::sort(baseNode, sortFunctor);
  201. int pos = -1;
  202. for (int i = 0; i < baseNode.size(); i++)
  203. {
  204. if(!baseNode[i]["posFlag"].isNull())
  205. {
  206. baseNode[i]["posFlag"].clear();
  207. pos = i;
  208. }
  209. }
  210. Settings s = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"];
  211. s->Vector() = baseNode;
  212. return pos;
  213. }
  214. void CHighScoreInputScreen::show(Canvas & to)
  215. {
  216. CWindowObject::showAll(to);
  217. }
  218. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  219. {
  220. if(statisticButton->pos.isInside(cursorPosition))
  221. return;
  222. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  223. if(!won)
  224. {
  225. close();
  226. return;
  227. }
  228. if(!input)
  229. {
  230. input = std::make_shared<CHighScoreInput>(calc.parameters[0].playerName,
  231. [&] (std::string text)
  232. {
  233. if(!text.empty())
  234. {
  235. int pos = addEntry(text);
  236. close();
  237. GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
  238. }
  239. else
  240. close();
  241. });
  242. }
  243. }
  244. void CHighScoreInputScreen::keyPressed(EShortcut key)
  245. {
  246. if(key == EShortcut::HIGH_SCORES_STATISTIC)
  247. return;
  248. clickPressed(Point());
  249. }
  250. CHighScoreInput::CHighScoreInput(std::string playerName, std::function<void(std::string text)> readyCB)
  251. : CWindowObject(NEEDS_ANIMATED_BACKGROUND, ImagePath::builtin("HIGHNAME")), ready(readyCB)
  252. {
  253. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  254. pos = center(Rect(0, 0, 232, 212));
  255. updateShadow();
  256. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  257. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  258. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  259. // FIXME: broken. Never activates?
  260. // statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  261. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, ETextAlignment::CENTER, true);
  262. textInput->setText(playerName);
  263. }
  264. void CHighScoreInput::okay()
  265. {
  266. ready(textInput->getText());
  267. }
  268. void CHighScoreInput::abort()
  269. {
  270. ready("");
  271. }