CHighScoreScreen.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "../widgets/TextControls.h"
  16. #include "../widgets/Buttons.h"
  17. #include "../widgets/Images.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../render/Canvas.h"
  20. #include "../CGameInfo.h"
  21. #include "../CVideoHandler.h"
  22. #include "../CMusicHandler.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/CConfigHandler.h"
  25. #include "../../lib/CCreatureHandler.h"
  26. #include "../../lib/constants/EntityIdentifiers.h"
  27. CHighScoreScreen::CHighScoreScreen()
  28. : CWindowObject(BORDERED)
  29. {
  30. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  31. pos = center(Rect(0, 0, 800, 600));
  32. updateShadow();
  33. addHighScores();
  34. addButtons();
  35. // TODO: remove; only for testing
  36. for (int i = 0; i < 11; i++)
  37. {
  38. Settings entry = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["player"];
  39. entry->String() = "test";
  40. Settings entry1 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["campaign"];
  41. entry1->String() = "test";
  42. Settings entry2 = persistentStorage.write["highscore"]["campaign"][std::to_string(i)]["points"];
  43. entry2->Integer() = std::rand() % 400 * 5;
  44. Settings entry3 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["player"];
  45. entry3->String() = "test";
  46. Settings entry4 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["land"];
  47. entry4->String() = "test";
  48. Settings entry5 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["days"];
  49. entry5->Integer() = 123;
  50. Settings entry6 = persistentStorage.write["highscore"]["scenario"][std::to_string(i)]["points"];
  51. entry6->Integer() = std::rand() % 400;
  52. }
  53. }
  54. void CHighScoreScreen::addButtons()
  55. {
  56. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  57. buttons.clear();
  58. buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [&](){ buttonCampaginClick(); }));
  59. buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [&](){ buttonStandardClick(); }));
  60. buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [&](){ buttonResetClick(); }));
  61. buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [&](){ buttonExitClick(); }));
  62. }
  63. void CHighScoreScreen::addHighScores()
  64. {
  65. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  66. background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
  67. texts.clear();
  68. images.clear();
  69. static const JsonNode configCreatures(JsonPath::builtin("CONFIG/highscoreCreatures.json"));
  70. auto creatures = configCreatures["creatures"].Vector();
  71. // Header
  72. texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433")));
  73. texts.push_back(std::make_shared<CLabel>(220, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434")));
  74. if(highscorepage == HighScorePage::SCENARIO)
  75. {
  76. texts.push_back(std::make_shared<CLabel>(400, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435")));
  77. texts.push_back(std::make_shared<CLabel>(555, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436")));
  78. texts.push_back(std::make_shared<CLabel>(625, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  79. }
  80. else
  81. {
  82. texts.push_back(std::make_shared<CLabel>(410, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672")));
  83. texts.push_back(std::make_shared<CLabel>(590, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75")));
  84. }
  85. // Content
  86. int y = 65;
  87. auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
  88. for (int i = 0; i < 11; i++)
  89. {
  90. auto & curData = data[std::to_string(i)];
  91. texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(i+1)));
  92. texts.push_back(std::make_shared<CLabel>(220, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["player"].String()));
  93. if(highscorepage == HighScorePage::SCENARIO)
  94. {
  95. texts.push_back(std::make_shared<CLabel>(400, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["land"].String()));
  96. texts.push_back(std::make_shared<CLabel>(555, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["days"].Integer())));
  97. texts.push_back(std::make_shared<CLabel>(625, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  98. }
  99. else
  100. {
  101. texts.push_back(std::make_shared<CLabel>(410, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, curData["campaign"].String()));
  102. texts.push_back(std::make_shared<CLabel>(590, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, std::to_string(curData["points"].Integer())));
  103. }
  104. int divide = (highscorepage == HighScorePage::SCENARIO) ? 1 : 5;
  105. for(auto & creature : creatures) {
  106. if(curData["points"].Integer() / divide <= creature["max"].Integer() && curData["points"].Integer() / divide >= creature["min"].Integer())
  107. images.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), (*CGI->creh)[CreatureID::decode(creature["creature"].String())]->getIconIndex(), 0, 670, y - 15 + i * 50));
  108. }
  109. }
  110. }
  111. void CHighScoreScreen::buttonCampaginClick()
  112. {
  113. highscorepage = HighScorePage::CAMPAIGN;
  114. addHighScores();
  115. addButtons();
  116. redraw();
  117. }
  118. void CHighScoreScreen::buttonStandardClick()
  119. {
  120. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  121. highscorepage = HighScorePage::SCENARIO;
  122. addHighScores();
  123. addButtons();
  124. redraw();
  125. }
  126. void CHighScoreScreen::buttonResetClick()
  127. {
  128. CInfoWindow::showYesNoDialog(
  129. CGI->generaltexth->allTexts[666],
  130. {},
  131. [this]()
  132. {
  133. Settings entry = persistentStorage.write["highscore"];
  134. entry->clear();
  135. addHighScores();
  136. addButtons();
  137. redraw();
  138. },
  139. 0
  140. );
  141. }
  142. void CHighScoreScreen::buttonExitClick()
  143. {
  144. close();
  145. }
  146. CHighScoreInputScreen::CHighScoreInputScreen(bool won)
  147. : CWindowObject(BORDERED), won(won)
  148. {
  149. addUsedEvents(LCLICK);
  150. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  151. pos = center(Rect(0, 0, 800, 600));
  152. updateShadow();
  153. if(won)
  154. {
  155. int border = 100;
  156. int textareaW = ((pos.w - 2 * border) / 4);
  157. std::vector<std::string> t = { "438", "439", "440", "441", "676" };
  158. for (int i = 0; i < 5; i++)
  159. 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])));
  160. CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
  161. }
  162. else
  163. CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
  164. video = won ? "HSANIM.SMK" : "LOSEGAME.SMK";
  165. }
  166. void CHighScoreInputScreen::addEntry(std::string text) {
  167. }
  168. void CHighScoreInputScreen::show(Canvas & to)
  169. {
  170. CCS->videoh->update(pos.x, pos.y, to.getInternalSurface(), true, false,
  171. [&]()
  172. {
  173. if(won)
  174. {
  175. CCS->videoh->close();
  176. video = "HSLOOP.SMK";
  177. CCS->videoh->open(VideoPath::builtin(video));
  178. }
  179. else
  180. close();
  181. });
  182. redraw();
  183. CIntObject::show(to);
  184. }
  185. void CHighScoreInputScreen::activate()
  186. {
  187. CCS->videoh->open(VideoPath::builtin(video));
  188. CIntObject::activate();
  189. }
  190. void CHighScoreInputScreen::deactivate()
  191. {
  192. CCS->videoh->close();
  193. CIntObject::deactivate();
  194. }
  195. void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
  196. {
  197. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  198. if(!won)
  199. {
  200. close();
  201. return;
  202. }
  203. if(!input)
  204. {
  205. input = std::make_shared<CHighScoreInput>(
  206. [&] (std::string text)
  207. {
  208. if(!text.empty())
  209. {
  210. addEntry(text);
  211. close();
  212. GH.windows().createAndPushWindow<CHighScoreScreen>();
  213. }
  214. else
  215. close();
  216. });
  217. }
  218. }
  219. CHighScoreInput::CHighScoreInput(std::function<void(std::string text)> readyCB)
  220. : CWindowObject(0), ready(readyCB)
  221. {
  222. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  223. pos = center(Rect(0, 0, 232, 212));
  224. updateShadow();
  225. background = std::make_shared<CPicture>(ImagePath::builtin("HIGHNAME"));
  226. text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
  227. buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
  228. buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
  229. statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
  230. textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, 0);
  231. textInput->setText(settings["general"]["playerName"].String());
  232. }
  233. void CHighScoreInput::okay()
  234. {
  235. ready(textInput->getText());
  236. }
  237. void CHighScoreInput::abort()
  238. {
  239. ready("");
  240. }