CHighScoreScreen.cpp 13 KB

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