CHighScoreScreen.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "../widgets/TextControls.h"
  14. #include "../widgets/Buttons.h"
  15. #include "../widgets/Images.h"
  16. #include "../windows/InfoWindows.h"
  17. #include "../render/Canvas.h"
  18. #include "../CGameInfo.h"
  19. #include "../../lib/CGeneralTextHandler.h"
  20. #include "../../lib/CConfigHandler.h"
  21. #include "../../lib/CCreatureHandler.h"
  22. #include "../../lib/constants/EntityIdentifiers.h"
  23. CHighScoreScreen::CHighScoreScreen()
  24. : CWindowObject(BORDERED)
  25. {
  26. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  27. pos = center(Rect(0, 0, 800, 600));
  28. updateShadow();
  29. addHighScores();
  30. addButtons();
  31. // TODO: remove; only for testing
  32. for (int i = 0; i < 11; i++)
  33. {
  34. Settings entry = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["player"];
  35. entry->String() = "test";
  36. Settings entry1 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["campaign"];
  37. entry1->String() = "test";
  38. Settings entry2 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["points"];
  39. entry2->Integer() = std::rand() % 400 * 5;
  40. Settings entry3 = persistentStorage.write["highscore"]["standard"][std::to_string(i)]["player"];
  41. entry3->String() = "test";
  42. Settings entry4 = persistentStorage.write["highscore"]["standard"][std::to_string(i)]["land"];
  43. entry4->String() = "test";
  44. Settings entry5 = persistentStorage.write["highscore"]["standard"][std::to_string(i)]["days"];
  45. entry5->Integer() = 123;
  46. Settings entry6 = persistentStorage.write["highscore"]["standard"][std::to_string(i)]["points"];
  47. entry6->Integer() = std::rand() % 400;
  48. }
  49. }
  50. void CHighScoreScreen::addButtons()
  51. {
  52. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  53. buttons.clear();
  54. buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaginClick(); }));
  55. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonStandardClick(); }));
  56. buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }));
  57. buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }));
  58. }
  59. void CHighScoreScreen::addHighScores()
  60. {
  61. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  62. background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::STANDARD ? "HISCORE" : "HISCORE2"));
  63. texts.clear();
  64. images.clear();
  65. static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
  66. auto creatures = configCreatures["creatures"].Vector();
  67. // Header
  68. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433")));
  69. texts.push_back(std::make_shared<CLabel>(220, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
  70. if(highscorepage == HighScorePage::STANDARD)
  71. {
  72. texts.push_back(std::make_shared<CLabel>(400, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435")));
  73. texts.push_back(std::make_shared<CLabel>(555, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
  74. texts.push_back(std::make_shared<CLabel>(625, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  75. }
  76. else
  77. {
  78. texts.push_back(std::make_shared<CLabel>(410, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672")));
  79. texts.push_back(std::make_shared<CLabel>(590, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  80. }
  81. // Content
  82. int y = 65;
  83. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::STANDARD ? "standard" : "campaign"];
  84. for (int i = 0; i < 11; i++)
  85. {
  86. auto & curData = data[std::to_string(i)];
  87. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(i)));
  88. texts.push_back(std::make_shared<CLabel>(220, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["player"].String()));
  89. if(highscorepage == HighScorePage::STANDARD)
  90. {
  91. texts.push_back(std::make_shared<CLabel>(400, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["land"].String()));
  92. texts.push_back(std::make_shared<CLabel>(555, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["days"].Integer())));
  93. texts.push_back(std::make_shared<CLabel>(625, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  94. }
  95. else
  96. {
  97. texts.push_back(std::make_shared<CLabel>(410, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["campaign"].String()));
  98. texts.push_back(std::make_shared<CLabel>(590, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  99. }
  100. int divide = (highscorepage == HighScorePage::STANDARD) ? 1 : 5;
  101. for(auto & creature : creatures) {
  102. if(curData["points"].Integer() / divide <= creature["max"].Integer() && curData["points"].Integer() / divide >= creature["min"].Integer())
  103. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[CreatureID::decode(creature["creature"].String())]->getIconIndex(), 0, 670, y - 15 + i * 50));
  104. }
  105. }
  106. }
  107. void CHighScoreScreen::buttonCampaginClick()
  108. {
  109. highscorepage = HighScorePage::CAMPAIGN;
  110. addHighScores();
  111. addButtons();
  112. redraw();
  113. }
  114. void CHighScoreScreen::buttonStandardClick()
  115. {
  116. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  117. highscorepage = HighScorePage::STANDARD;
  118. addHighScores();
  119. addButtons();
  120. redraw();
  121. }
  122. void CHighScoreScreen::buttonResetClick()
  123. {
  124. CInfoWindow::showYesNoDialog(
  125. CGI->generaltexth->allTexts[666],
  126. {},
  127. [this]()
  128. {
  129. Settings entry = persistentStorage.write["highscore"];
  130. entry->clear();
  131. addHighScores();
  132. addButtons();
  133. redraw();
  134. },
  135. 0
  136. );
  137. }
  138. void CHighScoreScreen::buttonExitClick()
  139. {
  140. close();
  141. }