1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/client/mainmenu/CHighScoreScreen.cpp

333 lines
13 KiB
C++
Raw Normal View History

2023-09-22 01:39:35 +02:00
/*
* CHighScoreScreen.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "CHighScoreScreen.h"
2024-08-11 22:44:16 +02:00
#include "CStatisticScreen.h"
2024-09-18 22:10:25 +02:00
#include "CMainMenu.h"
2023-09-22 01:39:35 +02:00
#include "../gui/CGuiHandler.h"
2023-09-22 20:39:20 +02:00
#include "../gui/WindowHandler.h"
#include "../gui/Shortcut.h"
#include "../media/IMusicPlayer.h"
#include "../media/ISoundPlayer.h"
2023-09-22 01:39:35 +02:00
#include "../widgets/Buttons.h"
#include "../widgets/CTextInput.h"
2023-09-22 01:39:35 +02:00
#include "../widgets/Images.h"
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/VideoWidget.h"
2023-09-22 01:39:35 +02:00
#include "../windows/InfoWindows.h"
#include "../widgets/TextControls.h"
2023-09-22 01:39:35 +02:00
#include "../render/Canvas.h"
2024-05-18 19:58:51 +02:00
#include "../render/IRenderHandler.h"
2023-09-22 01:39:35 +02:00
#include "../CGameInfo.h"
#include "../../lib/texts/CGeneralTextHandler.h"
#include "../../lib/texts/TextOperations.h"
2023-09-22 01:39:35 +02:00
#include "../../lib/CConfigHandler.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/constants/EntityIdentifiers.h"
2024-07-26 03:33:44 +02:00
#include "../../lib/gameState/HighScore.h"
#include "../../lib/gameState/GameStatistics.h"
2023-09-23 13:14:45 +02:00
CHighScoreScreen::CHighScoreScreen(HighScorePage highscorepage, int highlighted)
: CWindowObject(BORDERED), highscorepage(highscorepage), highlighted(highlighted)
2023-09-22 01:39:35 +02:00
{
2023-09-23 14:51:39 +02:00
addUsedEvents(SHOW_POPUP);
OBJECT_CONSTRUCTION;
2023-09-22 01:39:35 +02:00
pos = center(Rect(0, 0, 800, 600));
2024-05-18 19:58:51 +02:00
backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
2023-09-22 01:39:35 +02:00
2023-09-23 14:51:39 +02:00
addHighScores();
addButtons();
}
void CHighScoreScreen::showPopupWindow(const Point & cursorPosition)
{
2023-09-24 02:00:42 +02:00
for (int i = 0; i < screenRows; i++)
2023-09-23 14:51:39 +02:00
{
2023-09-24 02:00:42 +02:00
bool currentGameNotInListEntry = i == (screenRows - 1) && highlighted > (screenRows - 1);
2023-09-23 23:11:17 +02:00
2023-09-23 14:51:39 +02:00
Rect r = Rect(80, 40 + i * 50, 635, 50);
if(r.isInside(cursorPosition - pos))
{
2023-09-23 23:11:17 +02:00
std::string tmp = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"][currentGameNotInListEntry ? highlighted : i]["datetime"].String();
2023-09-23 14:51:39 +02:00
if(!tmp.empty())
CRClickPopup::createAndPush(tmp);
}
}
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::addButtons()
{
OBJECT_CONSTRUCTION;
2023-09-23 14:51:39 +02:00
buttons.clear();
2023-09-22 01:39:35 +02:00
buttons.push_back(std::make_shared<CButton>(Point(31, 113), AnimationPath::builtin("HISCCAM.DEF"), CButton::tooltip(), [this](){ buttonCampaignClick(); }, EShortcut::HIGH_SCORES_CAMPAIGNS));
buttons.push_back(std::make_shared<CButton>(Point(31, 345), AnimationPath::builtin("HISCSTA.DEF"), CButton::tooltip(), [this](){ buttonScenarioClick(); }, EShortcut::HIGH_SCORES_SCENARIOS));
buttons.push_back(std::make_shared<CButton>(Point(726, 113), AnimationPath::builtin("HISCRES.DEF"), CButton::tooltip(), [this](){ buttonResetClick(); }, EShortcut::HIGH_SCORES_RESET));
buttons.push_back(std::make_shared<CButton>(Point(726, 345), AnimationPath::builtin("HISCEXT.DEF"), CButton::tooltip(), [this](){ buttonExitClick(); }, EShortcut::GLOBAL_RETURN));
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::addHighScores()
{
OBJECT_CONSTRUCTION;
2023-09-22 01:39:35 +02:00
2023-09-23 14:51:39 +02:00
background = std::make_shared<CPicture>(ImagePath::builtin(highscorepage == HighScorePage::SCENARIO ? "HISCORE" : "HISCORE2"));
texts.clear();
images.clear();
// Header
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(115, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.433"))); // rank
texts.push_back(std::make_shared<CLabel>(225, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.434"))); // player
2023-09-23 14:51:39 +02:00
if(highscorepage == HighScorePage::SCENARIO)
{
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.435"))); // land
texts.push_back(std::make_shared<CLabel>(557, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.436"))); // days
texts.push_back(std::make_shared<CLabel>(627, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
2023-09-23 14:51:39 +02:00
}
else
{
2023-09-23 20:41:30 +02:00
texts.push_back(std::make_shared<CLabel>(405, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.672"))); // campaign
texts.push_back(std::make_shared<CLabel>(592, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.75"))); // score
2023-09-23 14:51:39 +02:00
}
// Content
2023-09-23 15:11:48 +02:00
int y = 66;
2023-09-23 14:51:39 +02:00
auto & data = persistentStorage["highscore"][highscorepage == HighScorePage::SCENARIO ? "scenario" : "campaign"];
2023-09-24 02:00:42 +02:00
for (int i = 0; i < screenRows; i++)
2023-09-23 14:51:39 +02:00
{
2023-09-24 02:00:42 +02:00
bool currentGameNotInListEntry = (i == (screenRows - 1) && highlighted > (screenRows - 1));
2023-09-23 22:45:38 +02:00
auto & curData = data[currentGameNotInListEntry ? highlighted : i];
2023-09-23 14:51:39 +02:00
2023-09-23 22:45:38 +02:00
ColorRGBA color = (i == highlighted || currentGameNotInListEntry) ? Colors::YELLOW : Colors::WHITE;
texts.push_back(std::make_shared<CLabel>(115, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string((currentGameNotInListEntry ? highlighted : i) + 1)));
2024-06-29 14:44:29 +02:00
texts.push_back(std::make_shared<CLabel>(225, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["player"].String(), 120));
2023-09-23 14:51:39 +02:00
if(highscorepage == HighScorePage::SCENARIO)
{
2024-06-29 14:44:29 +02:00
texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["scenarioName"].String(), 200));
2023-09-23 15:11:48 +02:00
texts.push_back(std::make_shared<CLabel>(557, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["days"].Integer())));
texts.push_back(std::make_shared<CLabel>(627, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
2023-09-23 14:51:39 +02:00
}
else
{
2024-06-29 14:44:29 +02:00
texts.push_back(std::make_shared<CLabel>(405, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, curData["campaignName"].String(), 200));
2023-09-23 15:11:48 +02:00
texts.push_back(std::make_shared<CLabel>(592, y + i * 50, FONT_MEDIUM, ETextAlignment::CENTER, color, std::to_string(curData["points"].Integer())));
2023-09-23 14:51:39 +02:00
}
2023-09-23 23:11:17 +02:00
if(curData["points"].Integer() > 0 && curData["points"].Integer() <= ((highscorepage == HighScorePage::CAMPAIGN) ? 2500 : 500))
2023-09-23 14:51:39 +02:00
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));
}
2023-09-22 01:39:35 +02:00
}
2023-09-23 20:41:30 +02:00
void CHighScoreScreen::buttonCampaignClick()
2023-09-22 01:39:35 +02:00
{
2023-09-23 14:51:39 +02:00
highscorepage = HighScorePage::CAMPAIGN;
addHighScores();
addButtons();
redraw();
2023-09-22 01:39:35 +02:00
}
2023-09-23 20:41:30 +02:00
void CHighScoreScreen::buttonScenarioClick()
2023-09-22 01:39:35 +02:00
{
OBJECT_CONSTRUCTION;
2023-09-23 14:51:39 +02:00
highscorepage = HighScorePage::SCENARIO;
addHighScores();
addButtons();
redraw();
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::buttonResetClick()
{
2023-09-23 14:51:39 +02:00
CInfoWindow::showYesNoDialog(
CGI->generaltexth->allTexts[666],
{},
[this]()
{
Settings entry = persistentStorage.write["highscore"];
entry->clear();
addHighScores();
addButtons();
redraw();
},
0
);
2023-09-22 01:39:35 +02:00
}
void CHighScoreScreen::buttonExitClick()
{
2023-09-23 14:51:39 +02:00
close();
2024-09-18 22:10:25 +02:00
CMM->playMusic();
2023-09-22 01:39:35 +02:00
}
2023-09-22 20:39:20 +02:00
2024-08-15 23:09:04 +02:00
CHighScoreInputScreen::CHighScoreInputScreen(bool won, HighScoreCalculation calc, const StatisticDataSet & statistic)
2024-08-11 22:44:16 +02:00
: CWindowObject(BORDERED), won(won), calc(calc), stat(statistic)
2023-09-22 20:39:20 +02:00
{
2023-09-23 15:11:48 +02:00
addUsedEvents(LCLICK | KEYBOARD);
2023-09-22 20:39:20 +02:00
OBJECT_CONSTRUCTION;
2023-09-22 20:39:20 +02:00
pos = center(Rect(0, 0, 800, 600));
2024-05-18 19:58:51 +02:00
backgroundAroundMenu = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(-pos.x, -pos.y, GH.screenDimensions().x, GH.screenDimensions().y));
2023-09-23 16:28:28 +02:00
background = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, pos.w, pos.h), Colors::BLACK);
2023-09-23 14:51:39 +02:00
if(won)
{
2024-05-15 18:34:23 +02:00
videoPlayer = std::make_shared<VideoWidget>(Point(0, 0), VideoPath::builtin("HSANIM.SMK"), VideoPath::builtin("HSLOOP.SMK"), true);
2023-09-23 14:51:39 +02:00
int border = 100;
int textareaW = ((pos.w - 2 * border) / 4);
2023-09-23 20:41:30 +02:00
std::vector<std::string> t = { "438", "439", "440", "441", "676" }; // time, score, difficulty, final score, rank
2023-09-23 14:51:39 +02:00
for (int i = 0; i < 5; i++)
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])));
std::string creatureName = (calc.calculate().cheater) ? CGI->generaltexth->translate("core.genrltxt.260") : (*CGI->creh)[HighScoreCalculation::getCreatureForPoints(calc.calculate().total, calc.isCampaign)]->getNameSingularTranslated();
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 };
for (int i = 0; i < 5; i++)
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]));
2023-09-22 21:45:12 +02:00
2023-09-23 14:51:39 +02:00
CCS->musich->playMusic(AudioPath::builtin("music/Win Scenario"), true, true);
}
else
{
videoPlayer = std::make_shared<VideoWidgetOnce>(Point(0, 0), VideoPath::builtin("LOSEGAME.SMK"), true, this);
2023-09-23 14:51:39 +02:00
CCS->musich->playMusic(AudioPath::builtin("music/UltimateLose"), false, true);
}
2024-08-12 01:10:47 +02:00
if (settings["general"]["enableUiEnhancements"].Bool())
{
2024-08-14 21:50:53 +02:00
statisticButton = std::make_shared<CButton>(Point(726, 10), AnimationPath::builtin("TPTAV02.DEF"), CButton::tooltip(CGI->generaltexth->translate("vcmi.statisticWindow.statistics")), [this](){ GH.windows().createAndPushWindow<CStatisticScreen>(stat); }, EShortcut::HIGH_SCORES_STATISTICS);
texts.push_back(std::make_shared<CLabel>(716, 25, EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTERRIGHT, Colors::WHITE, CGI->generaltexth->translate("vcmi.statisticWindow.statistics") + ":"));
2024-08-12 01:10:47 +02:00
}
2023-09-22 20:39:20 +02:00
}
void CHighScoreInputScreen::onVideoPlaybackFinished()
{
close();
}
2023-09-23 14:51:39 +02:00
int CHighScoreInputScreen::addEntry(std::string text) {
2023-09-23 22:15:05 +02:00
std::vector<JsonNode> baseNode = persistentStorage["highscore"][calc.isCampaign ? "campaign" : "scenario"].Vector();
auto sortFunctor = [](const JsonNode & left, const JsonNode & right)
2023-09-23 14:51:39 +02:00
{
2023-09-23 23:11:17 +02:00
if(left["points"].Integer() == right["points"].Integer())
2023-10-27 18:52:25 +02:00
return left["posFlag"].Bool() > right["posFlag"].Bool();
2023-09-23 22:15:05 +02:00
return left["points"].Integer() > right["points"].Integer();
};
2023-09-23 14:51:39 +02:00
2023-09-23 22:15:05 +02:00
JsonNode newNode = JsonNode();
newNode["player"].String() = text;
if(calc.isCampaign)
2023-09-25 18:06:40 +02:00
newNode["campaignName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].campaignName;
2023-09-23 22:15:05 +02:00
else
2023-09-25 18:06:40 +02:00
newNode["scenarioName"].String() = calc.calculate().cheater ? CGI->generaltexth->translate("core.genrltxt.260") : calc.parameters[0].scenarioName;
2023-09-23 22:15:05 +02:00
newNode["days"].Integer() = calc.calculate().sumDays;
newNode["points"].Integer() = calc.calculate().cheater ? 0 : calc.calculate().total;
2023-12-16 22:10:27 +02:00
newNode["datetime"].String() = TextOperations::getFormattedDateTimeLocal(std::time(nullptr));
2023-09-23 22:15:05 +02:00
newNode["posFlag"].Bool() = true;
baseNode.push_back(newNode);
boost::range::sort(baseNode, sortFunctor);
int pos = -1;
for (int i = 0; i < baseNode.size(); i++)
{
if(!baseNode[i]["posFlag"].isNull())
2023-09-23 14:51:39 +02:00
{
2023-09-23 22:15:05 +02:00
baseNode[i]["posFlag"].clear();
pos = i;
2023-09-23 14:51:39 +02:00
}
}
2023-09-23 23:11:17 +02:00
Settings s = persistentStorage.write["highscore"][calc.isCampaign ? "campaign" : "scenario"];
2023-09-23 22:15:05 +02:00
s->Vector() = baseNode;
2023-09-23 14:51:39 +02:00
2023-09-23 22:15:05 +02:00
return pos;
2023-09-22 20:39:20 +02:00
}
2024-05-15 18:34:23 +02:00
void CHighScoreInputScreen::show(Canvas & to)
{
CWindowObject::showAll(to);
}
2023-09-22 20:39:20 +02:00
void CHighScoreInputScreen::clickPressed(const Point & cursorPosition)
{
if(statisticButton->pos.isInside(cursorPosition))
return;
OBJECT_CONSTRUCTION;
2023-09-23 14:51:39 +02:00
if(!won)
{
close();
return;
}
if(!input)
{
2023-09-26 22:06:04 +02:00
input = std::make_shared<CHighScoreInput>(calc.parameters[0].playerName,
2023-09-23 14:51:39 +02:00
[&] (std::string text)
{
if(!text.empty())
{
int pos = addEntry(text);
close();
GH.windows().createAndPushWindow<CHighScoreScreen>(calc.isCampaign ? CHighScoreScreen::HighScorePage::CAMPAIGN : CHighScoreScreen::HighScorePage::SCENARIO, pos);
}
else
close();
});
}
2023-09-22 20:39:20 +02:00
}
2023-09-23 15:11:48 +02:00
void CHighScoreInputScreen::keyPressed(EShortcut key)
{
2024-08-14 21:50:53 +02:00
if(key == EShortcut::HIGH_SCORES_STATISTICS) // ignore shortcut for skipping video with key
2024-08-12 01:07:58 +02:00
return;
2023-09-23 15:11:48 +02:00
clickPressed(Point());
}
2023-09-26 22:06:04 +02:00
CHighScoreInput::CHighScoreInput(std::string playerName, std::function<void(std::string text)> readyCB)
2023-10-31 23:05:22 +02:00
: CWindowObject(NEEDS_ANIMATED_BACKGROUND, ImagePath::builtin("HIGHNAME")), ready(readyCB)
2023-09-22 20:39:20 +02:00
{
OBJECT_CONSTRUCTION;
2023-09-22 20:39:20 +02:00
pos = center(Rect(0, 0, 232, 212));
updateShadow();
2023-09-23 14:51:39 +02:00
text = std::make_shared<CMultiLineLabel>(Rect(15, 15, 202, 202), FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.96"));
2023-09-22 20:39:20 +02:00
2023-09-23 14:51:39 +02:00
buttonOk = std::make_shared<CButton>(Point(26, 142), AnimationPath::builtin("MUBCHCK.DEF"), CGI->generaltexth->zelp[560], std::bind(&CHighScoreInput::okay, this), EShortcut::GLOBAL_ACCEPT);
buttonCancel = std::make_shared<CButton>(Point(142, 142), AnimationPath::builtin("MUBCANC.DEF"), CGI->generaltexth->zelp[561], std::bind(&CHighScoreInput::abort, this), EShortcut::GLOBAL_CANCEL);
// FIXME: broken. Never activates?
// statusBar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(7, 186, 218, 18), 7, 186));
2024-05-12 16:52:20 +02:00
textInput = std::make_shared<CTextInput>(Rect(18, 104, 200, 25), FONT_SMALL, ETextAlignment::CENTER, true);
2023-09-26 22:06:04 +02:00
textInput->setText(playerName);
2023-09-22 20:39:20 +02:00
}
void CHighScoreInput::okay()
{
2023-09-23 14:51:39 +02:00
ready(textInput->getText());
2023-09-22 20:39:20 +02:00
}
void CHighScoreInput::abort()
{
2023-09-23 14:51:39 +02:00
ready("");
}