CHighScoreScreen.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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() = 100;
  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() = 100;
  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. static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
  65. auto creatures = configCreatures["creatures"].Vector();
  66. for(auto & creature : creatures) {
  67. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[CreatureID::decode(creature["creature"].String())]->getIconIndex(), 0, 10, 10));
  68. }
  69. // Header
  70. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433")));
  71. texts.push_back(std::make_shared<CLabel>(220, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
  72. if(highscorepage == HighScorePage::STANDARD)
  73. {
  74. texts.push_back(std::make_shared<CLabel>(400, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435")));
  75. texts.push_back(std::make_shared<CLabel>(555, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
  76. texts.push_back(std::make_shared<CLabel>(625, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  77. }
  78. else
  79. {
  80. texts.push_back(std::make_shared<CLabel>(410, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672")));
  81. texts.push_back(std::make_shared<CLabel>(590, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  82. }
  83. // Content
  84. int y = 65;
  85. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::STANDARD ? "standard" : "campaign"];
  86. for (int i = 0; i < 11; i++)
  87. {
  88. auto & curData = data[std::to_string(i)];
  89. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(i)));
  90. texts.push_back(std::make_shared<CLabel>(220, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["player"].String()));
  91. if(highscorepage == HighScorePage::STANDARD)
  92. {
  93. texts.push_back(std::make_shared<CLabel>(400, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["land"].String()));
  94. texts.push_back(std::make_shared<CLabel>(555, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["days"].Integer())));
  95. texts.push_back(std::make_shared<CLabel>(625, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  96. }
  97. else
  98. {
  99. texts.push_back(std::make_shared<CLabel>(410, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["campaign"].String()));
  100. texts.push_back(std::make_shared<CLabel>(590, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  101. }
  102. }
  103. }
  104. void CHighScoreScreen::buttonCampaginClick()
  105. {
  106. highscorepage = HighScorePage::CAMPAIGN;
  107. addHighScores();
  108. addButtons();
  109. redraw();
  110. }
  111. void CHighScoreScreen::buttonStandardClick()
  112. {
  113. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  114. highscorepage = HighScorePage::STANDARD;
  115. addHighScores();
  116. addButtons();
  117. redraw();
  118. }
  119. void CHighScoreScreen::buttonResetClick()
  120. {
  121. CInfoWindow::showYesNoDialog(
  122. CGI->generaltexth->allTexts[666],
  123. {},
  124. [this]()
  125. {
  126. Settings entry = persistentStorage.write["highscore"];
  127. entry->clear();
  128. addHighScores();
  129. addButtons();
  130. redraw();
  131. },
  132. 0
  133. );
  134. }
  135. void CHighScoreScreen::buttonExitClick()
  136. {
  137. close();
  138. }