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

564 lines
24 KiB
C++
Raw Normal View History

2024-08-11 22:44:16 +02:00
/*
* CStatisticScreen.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 "CStatisticScreen.h"
2024-08-12 01:07:58 +02:00
#include "../CGameInfo.h"
2024-08-11 22:44:16 +02:00
#include "../gui/CGuiHandler.h"
#include "../gui/WindowHandler.h"
2024-08-14 21:01:37 +02:00
#include "../eventsSDL/InputHandler.h"
2024-08-12 01:07:58 +02:00
#include "../gui/Shortcut.h"
2024-08-11 22:44:16 +02:00
2024-08-12 21:47:59 +02:00
#include "../render/Graphics.h"
2024-08-14 21:11:57 +02:00
#include "../render/IImage.h"
2024-08-15 00:21:02 +02:00
#include "../render/IRenderHandler.h"
2024-08-12 21:47:59 +02:00
2024-08-13 20:41:55 +02:00
#include "../widgets/ComboBox.h"
2024-08-11 22:44:16 +02:00
#include "../widgets/Images.h"
2024-08-12 01:07:58 +02:00
#include "../widgets/GraphicalPrimitiveCanvas.h"
#include "../widgets/TextControls.h"
#include "../widgets/Buttons.h"
2024-08-12 02:56:33 +02:00
#include "../windows/InfoWindows.h"
2024-08-13 20:41:55 +02:00
#include "../widgets/Slider.h"
2024-08-11 22:44:16 +02:00
#include "../../lib/gameState/GameStatistics.h"
2024-08-14 22:36:54 +02:00
#include "../../lib/gameState/CGameState.h"
2024-08-12 01:07:58 +02:00
#include "../../lib/texts/CGeneralTextHandler.h"
2024-08-14 19:24:40 +02:00
#include "../../lib/texts/TextOperations.h"
2024-08-11 22:44:16 +02:00
2024-08-12 02:56:33 +02:00
#include <vstd/DateUtils.h>
2024-08-14 22:36:54 +02:00
std::string CStatisticScreen::getDay(int d)
{
return std::to_string(CGameState::getDate(d, Date::MONTH)) + "/" + std::to_string(CGameState::getDate(d, Date::WEEK)) + "/" + std::to_string(CGameState::getDate(d, Date::DAY_OF_WEEK));
}
2024-08-15 23:09:04 +02:00
CStatisticScreen::CStatisticScreen(const StatisticDataSet & stat)
2024-08-12 01:19:00 +02:00
: CWindowObject(BORDERED), statistic(stat)
2024-08-11 22:44:16 +02:00
{
2024-08-13 01:07:55 +02:00
OBJECT_CONSTRUCTION;
2024-08-11 22:44:16 +02:00
pos = center(Rect(0, 0, 800, 600));
filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
2024-08-12 01:07:58 +02:00
filledBackground->setPlayerColor(PlayerColor(1));
2024-08-11 22:44:16 +02:00
2024-08-13 21:30:54 +02:00
contentArea = Rect(10, 40, 780, 510);
2024-08-15 21:48:35 +02:00
layout.emplace_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistics")));
layout.emplace_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 80, 128, 255), 1));
layout.emplace_back(std::make_shared<CButton>(Point(725, 558), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
2024-08-12 02:56:33 +02:00
2024-08-14 21:51:08 +02:00
buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ onSelectButton(); });
2024-08-13 20:41:55 +02:00
buttonSelect->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
2024-08-14 21:01:37 +02:00
buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ GH.input().copyToClipBoard(statistic.toCsv("\t")); });
buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.tsvCopy"), EFonts::FONT_SMALL, Colors::YELLOW);
2024-08-12 20:14:36 +02:00
2024-08-13 22:40:37 +02:00
mainContent = getContent(OVERVIEW, EGameResID::NONE);
2024-08-12 21:47:59 +02:00
}
2024-08-14 21:51:08 +02:00
void CStatisticScreen::onSelectButton()
{
std::vector<std::string> texts;
for(auto & val : contentInfo)
2024-08-15 21:48:35 +02:00
texts.emplace_back(CGI->generaltexth->translate(std::get<0>(val.second)));
2024-08-14 21:51:08 +02:00
GH.windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
{
OBJECT_CONSTRUCTION;
2024-08-15 21:48:35 +02:00
if(!std::get<1>(contentInfo[static_cast<Content>(selectedIndex)]))
mainContent = getContent(static_cast<Content>(selectedIndex), EGameResID::NONE);
2024-08-14 21:51:08 +02:00
else
{
2024-08-15 21:48:35 +02:00
auto content = static_cast<Content>(selectedIndex);
2024-08-14 21:51:08 +02:00
auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
std::vector<std::string> resourceText;
2024-08-15 23:09:04 +02:00
for(const auto & res : possibleRes)
2024-08-15 21:48:35 +02:00
resourceText.emplace_back(CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()));
2024-08-14 21:51:08 +02:00
2024-08-15 21:48:35 +02:00
GH.windows().createAndPushWindow<StatisticSelector>(resourceText, [this, content, possibleRes](int index)
2024-08-14 21:51:08 +02:00
{
OBJECT_CONSTRUCTION;
2024-08-15 21:48:35 +02:00
mainContent = getContent(content, possibleRes[index]);
2024-08-14 21:51:08 +02:00
});
}
});
}
2024-08-16 00:03:45 +02:00
TData CStatisticScreen::extractData(const StatisticDataSet & stat, const ExtractFunctor & selector) const
2024-08-12 21:47:59 +02:00
{
auto tmpData = stat.data;
2024-08-15 23:09:04 +02:00
std::sort(tmpData.begin(), tmpData.end(), [](const StatisticDataSetEntry & v1, const StatisticDataSetEntry & v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
2024-08-12 21:47:59 +02:00
PlayerColor tmpColor = PlayerColor::NEUTRAL;
std::vector<float> tmpColorSet;
2024-08-14 21:01:37 +02:00
TData plotData;
2024-08-15 00:53:38 +02:00
EPlayerStatus statusLastRound = EPlayerStatus::INGAME;
2024-08-15 23:09:04 +02:00
for(const auto & val : tmpData)
2024-08-12 21:47:59 +02:00
{
if(tmpColor != val.player)
{
if(tmpColorSet.size())
2024-08-12 23:28:08 +02:00
{
2024-08-14 22:36:54 +02:00
plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
2024-08-12 23:28:08 +02:00
tmpColorSet.clear();
}
2024-08-12 21:47:59 +02:00
tmpColor = val.player;
}
2024-08-15 00:53:38 +02:00
if(val.status == EPlayerStatus::INGAME || (statusLastRound == EPlayerStatus::INGAME && val.status == EPlayerStatus::LOSER))
2024-08-15 21:48:35 +02:00
tmpColorSet.emplace_back(selector(val));
2024-08-15 00:53:38 +02:00
statusLastRound = val.status; //to keep at least one dataset after loose
2024-08-12 21:47:59 +02:00
}
2024-08-12 23:28:08 +02:00
if(tmpColorSet.size())
2024-08-14 22:36:54 +02:00
plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
2024-08-12 21:47:59 +02:00
return plotData;
2024-08-12 20:14:36 +02:00
}
2024-08-15 23:09:04 +02:00
TIcons CStatisticScreen::extractIcons() const
2024-08-15 00:21:02 +02:00
{
TIcons icons;
auto tmpData = statistic.data;
2024-08-15 23:09:04 +02:00
std::sort(tmpData.begin(), tmpData.end(), [](const StatisticDataSetEntry & v1, const StatisticDataSetEntry & v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
2024-08-15 00:21:02 +02:00
auto imageTown = GH.renderHandler().loadImage(AnimationPath::builtin("cradvntr"), 3, 0, EImageBlitMode::COLORKEY);
auto imageBattle = GH.renderHandler().loadImage(AnimationPath::builtin("cradvntr"), 5, 0, EImageBlitMode::COLORKEY);
auto imageDefeated = GH.renderHandler().loadImage(AnimationPath::builtin("crcombat"), 0, 0, EImageBlitMode::COLORKEY);
2024-08-15 00:21:02 +02:00
auto imageGrail = GH.renderHandler().loadImage(AnimationPath::builtin("vwsymbol"), 2, 0, EImageBlitMode::COLORKEY);
std::map<PlayerColor, bool> foundDefeated;
std::map<PlayerColor, bool> foundGrail;
2024-08-15 23:09:04 +02:00
for(const auto & val : tmpData)
2024-08-15 00:21:02 +02:00
{
if(val.eventCapturedTown)
icons.push_back({ graphics->playerColors[val.player], val.day, imageTown, CGI->generaltexth->translate("vcmi.statisticWindow.icon.townCaptured") });
if(val.eventDefeatedStrongestHero)
icons.push_back({ graphics->playerColors[val.player], val.day, imageBattle, CGI->generaltexth->translate("vcmi.statisticWindow.icon.strongestHeroDefeated") });
if(val.status == EPlayerStatus::LOSER && !foundDefeated[val.player])
{
foundDefeated[val.player] = true;
icons.push_back({ graphics->playerColors[val.player], val.day, imageDefeated, CGI->generaltexth->translate("vcmi.statisticWindow.icon.defeated") });
}
if(val.hasGrail && !foundGrail[val.player])
{
foundGrail[val.player] = true;
icons.push_back({ graphics->playerColors[val.player], val.day, imageGrail, CGI->generaltexth->translate("vcmi.statisticWindow.icon.grailFound") });
}
}
return icons;
}
2024-08-13 22:40:37 +02:00
std::shared_ptr<CIntObject> CStatisticScreen::getContent(Content c, EGameResID res)
2024-08-13 21:30:54 +02:00
{
2024-08-14 21:01:37 +02:00
TData plotData;
2024-08-15 21:48:35 +02:00
TIcons icons = extractIcons();
2024-08-14 21:11:57 +02:00
2024-08-13 21:30:54 +02:00
switch (c)
{
case OVERVIEW:
2024-08-13 22:40:37 +02:00
return std::make_shared<OverviewPanel>(contentArea.resize(-15), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
2024-08-13 21:30:54 +02:00
case CHART_RESOURCES:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.resources[res]; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_INCOME:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.income; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_NUMBER_OF_HEROES:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberHeroes; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_NUMBER_OF_TOWNS:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberTowns; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_NUMBER_OF_ARTIFACTS:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberArtifacts; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_NUMBER_OF_DWELLINGS:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberDwellings; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_NUMBER_OF_MINES:
plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.numMines[res]; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_ARMY_STRENGTH:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.armyStrength; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_EXPERIENCE:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.totalExperience; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_RESOURCES_SPENT_ARMY:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.spentResourcesForArmy[res]; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_RESOURCES_SPENT_BUILDINGS:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.spentResourcesForBuildings[res]; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
2024-08-13 23:32:12 +02:00
case CHART_MAP_EXPLORED:
2024-08-15 21:48:35 +02:00
plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.mapExploredRatio; });
2024-08-14 21:11:57 +02:00
return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 1);
2024-08-13 21:30:54 +02:00
}
2024-08-13 22:07:09 +02:00
return nullptr;
2024-08-13 21:30:54 +02:00
}
2024-08-15 23:09:04 +02:00
StatisticSelector::StatisticSelector(const std::vector<std::string> & texts, const std::function<void(int selectedIndex)> & cb)
2024-08-14 21:11:57 +02:00
: CWindowObject(BORDERED | NEEDS_ANIMATED_BACKGROUND), texts(texts), cb(cb)
2024-08-13 20:41:55 +02:00
{
OBJECT_CONSTRUCTION;
2024-08-15 21:48:35 +02:00
pos = center(Rect(0, 0, 128 + 16, std::min(static_cast<int>(texts.size()), LINES) * 40));
filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
2024-08-13 20:41:55 +02:00
filledBackground->setPlayerColor(PlayerColor(1));
slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, [this](int to){ update(to); redraw(); }, LINES, texts.size(), 0, Orientation::VERTICAL, CSlider::BLUE);
slider->setPanningStep(40);
slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
update(0);
}
void StatisticSelector::update(int to)
{
OBJECT_CONSTRUCTION;
buttons.clear();
for(int i = to; i < LINES + to; i++)
{
if(i>=texts.size())
continue;
2024-08-13 22:07:09 +02:00
auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
2024-08-13 20:41:55 +02:00
button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
2024-08-15 21:48:35 +02:00
buttons.emplace_back(button);
2024-08-13 20:41:55 +02:00
}
}
2024-08-15 23:09:04 +02:00
OverviewPanel::OverviewPanel(Rect position, std::string title, const StatisticDataSet & stat)
2024-08-14 01:38:18 +02:00
: CIntObject(), data(stat)
2024-08-13 20:41:55 +02:00
{
OBJECT_CONSTRUCTION;
pos = position + pos.topLeft();
2024-08-15 21:48:35 +02:00
layout.emplace_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
2024-08-13 20:41:55 +02:00
2024-08-14 01:06:29 +02:00
canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, Y_OFFS, pos.w - 16, pos.h - Y_OFFS));
2024-08-13 20:41:55 +02:00
2024-08-14 01:06:29 +02:00
dataExtract = {
2024-08-14 02:49:54 +02:00
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.playerName"), [this](PlayerColor color){
return playerDataFilter(color).front().playerName;
}
},
2024-08-14 01:06:29 +02:00
{
2024-08-14 01:38:18 +02:00
CGI->generaltexth->translate("vcmi.statisticWindow.param.daysSurvived"), [this](PlayerColor color){
2024-08-24 23:36:01 +02:00
auto playerData = playerDataFilter(color);
for(int i = 0; i < playerData.size(); i++)
if(playerData[i].status == EPlayerStatus::LOSER)
return CStatisticScreen::getDay(i + 1);
return CStatisticScreen::getDay(playerData.size());
2024-08-14 01:38:18 +02:00
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.maxHeroLevel"), [this](PlayerColor color){
int maxLevel = 0;
2024-08-16 00:03:45 +02:00
for(const auto & val : playerDataFilter(color))
2024-08-14 01:38:18 +02:00
if(maxLevel < val.maxHeroLevel)
maxLevel = val.maxHeroLevel;
return std::to_string(maxLevel);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioHero"), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
if(!val.numBattlesPlayer)
return std::string("");
2024-08-15 21:48:35 +02:00
float tmp = (static_cast<float>(val.numWinBattlesPlayer) / static_cast<float>(val.numBattlesPlayer)) * 100;
2024-08-15 23:09:04 +02:00
return std::to_string(static_cast<int>(tmp)) + " %";
2024-08-14 01:38:18 +02:00
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioNeutral"), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
2024-08-14 02:49:54 +02:00
if(!val.numWinBattlesNeutral)
2024-08-14 01:38:18 +02:00
return std::string("");
2024-08-15 21:48:35 +02:00
float tmp = (static_cast<float>(val.numWinBattlesNeutral) / static_cast<float>(val.numBattlesNeutral)) * 100;
2024-08-15 23:09:04 +02:00
return std::to_string(static_cast<int>(tmp)) + " %";
2024-08-14 01:06:29 +02:00
}
},
2024-08-14 02:49:54 +02:00
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesHero"), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.numBattlesPlayer);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesNeutral"), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.numBattlesNeutral);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.obeliskVisited"), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
2024-08-15 23:09:04 +02:00
return std::to_string(static_cast<int>(val.obeliskVisitedRatio * 100)) + " %";
2024-08-14 02:49:54 +02:00
}
},
2024-08-14 19:24:40 +02:00
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.maxArmyStrength"), [this](PlayerColor color){
int maxArmyStrength = 0;
2024-08-16 00:03:45 +02:00
for(const auto & val : playerDataFilter(color))
2024-08-14 19:24:40 +02:00
if(maxArmyStrength < val.armyStrength)
maxArmyStrength = val.armyStrength;
return TextOperations::formatMetric(maxArmyStrength, 6);
}
},
2024-08-14 02:49:54 +02:00
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GOLD).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::GOLD]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::WOOD).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::WOOD]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::MERCURY).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::MERCURY]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::ORE).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::ORE]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::SULFUR).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::SULFUR]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::CRYSTAL).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::CRYSTAL]);
}
},
{
CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GEMS).get()), [this](PlayerColor color){
auto val = playerDataFilter(color).back();
return std::to_string(val.tradeVolume[EGameResID::GEMS]);
}
},
2024-08-14 01:06:29 +02:00
};
2024-08-13 20:41:55 +02:00
2024-08-14 01:06:29 +02:00
int usedLines = dataExtract.size();
slider = std::make_shared<CSlider>(Point(pos.w - 16, Y_OFFS), pos.h - Y_OFFS, [this](int to){ update(to); setRedrawParent(true); redraw(); }, LINES - 1, usedLines, 0, Orientation::VERTICAL, CSlider::BLUE);
2024-08-13 20:41:55 +02:00
slider->setPanningStep(canvas->pos.h / LINES);
slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
2024-08-14 01:06:29 +02:00
fieldSize = Point(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
2024-08-13 20:41:55 +02:00
for(int x = 0; x < graphics->playerColors.size() + 1; x++)
for(int y = 0; y < LINES; y++)
{
int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
int yStart = y * fieldSize.y;
if(x == 0 || y == 0)
canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
}
2024-08-13 22:40:37 +02:00
2024-08-14 01:06:29 +02:00
update(0);
2024-08-13 22:40:37 +02:00
}
2024-08-14 01:38:18 +02:00
std::vector<StatisticDataSetEntry> OverviewPanel::playerDataFilter(PlayerColor color)
{
std::vector<StatisticDataSetEntry> tmpData;
2024-08-15 23:09:04 +02:00
std::copy_if(data.data.begin(), data.data.end(), std::back_inserter(tmpData), [color](const StatisticDataSetEntry & e){ return e.player == color; });
2024-08-14 01:38:18 +02:00
return tmpData;
}
2024-08-14 01:06:29 +02:00
void OverviewPanel::update(int to)
2024-08-13 22:40:37 +02:00
{
OBJECT_CONSTRUCTION;
content.clear();
2024-08-14 01:06:29 +02:00
for(int y = to; y < LINES - 1 + to; y++)
{
if(y >= dataExtract.size())
continue;
for(int x = 0; x < PlayerColor::PLAYER_LIMIT_I + 1; x++)
{
2024-08-14 21:11:57 +02:00
if(y == to && x < PlayerColor::PLAYER_LIMIT_I)
2024-08-15 21:48:35 +02:00
content.emplace_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), x, 0, 180 + x * fieldSize.x, 35));
2024-08-14 01:06:29 +02:00
int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x + (x == 0 ? fieldSize.x : (fieldSize.x / 2));
int yStart = Y_OFFS + (y + 1 - to) * fieldSize.y + (fieldSize.y / 2);
2024-08-14 01:38:18 +02:00
PlayerColor tmpColor(x - 1);
if(playerDataFilter(tmpColor).size() || x == 0)
2024-08-15 21:48:35 +02:00
content.emplace_back(std::make_shared<CLabel>(xStart, yStart, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, (x == 0 ? dataExtract[y].first : dataExtract[y].second(tmpColor)), x == 0 ? (fieldSize.x * 2) : fieldSize.x));
2024-08-14 01:06:29 +02:00
}
}
2024-08-13 20:41:55 +02:00
}
2024-08-25 02:21:24 +02:00
int computeGridStep(int maxAmount, int linesLimit)
{
for (int lineInterval = 1;;lineInterval *= 10)
{
for (int factor : { 1, 2, 5 } )
{
int lineIntervalToTest = lineInterval * factor;
if (maxAmount / lineIntervalToTest <= linesLimit)
return lineIntervalToTest;
}
}
}
2024-08-14 21:11:57 +02:00
LineChart::LineChart(Rect position, std::string title, TData data, TIcons icons, float maxY)
2024-08-13 00:17:12 +02:00
: CIntObject(), maxVal(0), maxDay(0)
2024-08-12 20:14:36 +02:00
{
2024-08-13 01:07:55 +02:00
OBJECT_CONSTRUCTION;
2024-08-12 20:14:36 +02:00
2024-08-22 23:27:21 +02:00
addUsedEvents(LCLICK | MOVE | GESTURE);
2024-08-13 00:17:12 +02:00
2024-08-12 20:14:36 +02:00
pos = position + pos.topLeft();
2024-08-15 21:48:35 +02:00
layout.emplace_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
2024-08-13 20:41:55 +02:00
2024-08-13 00:17:12 +02:00
chartArea = pos.resize(-50);
2024-08-12 20:14:36 +02:00
chartArea.moveTo(Point(50, 50));
canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
2024-08-13 00:17:12 +02:00
statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
2024-08-15 23:09:04 +02:00
static_cast<std::shared_ptr<CIntObject>>(statusBar)->setEnabled(false);
2024-08-13 00:17:12 +02:00
2024-08-12 23:28:08 +02:00
// additional calculations
2024-08-13 23:32:12 +02:00
bool skipMaxValCalc = maxY > 0;
maxVal = maxY;
2024-08-16 00:03:45 +02:00
for(const auto & line : data)
2024-08-12 23:28:08 +02:00
{
for(auto & val : line.second)
2024-08-13 23:32:12 +02:00
if(maxVal < val && !skipMaxValCalc)
2024-08-12 23:28:08 +02:00
maxVal = val;
if(maxDay < line.second.size())
maxDay = line.second.size();
}
2024-08-25 02:21:24 +02:00
//calculate nice maxVal
int gridLineCount = 10;
int gridStep = computeGridStep(maxVal, gridLineCount);
niceMaxVal = gridStep * std::ceil(maxVal / gridStep);
2024-08-27 21:41:50 +02:00
niceMaxVal = std::max(1, niceMaxVal); // avoid zero size Y axis (if all values are 0)
2024-08-25 02:21:24 +02:00
2024-08-23 00:46:46 +02:00
// calculate points in chart
auto getPoint = [this](int i, std::vector<float> data){
float x = (static_cast<float>(chartArea.w) / static_cast<float>(maxDay - 1)) * static_cast<float>(i);
2024-08-25 02:21:24 +02:00
float y = static_cast<float>(chartArea.h) - (static_cast<float>(chartArea.h) / niceMaxVal) * data[i];
2024-08-23 00:46:46 +02:00
return Point(x, y);
};
2024-08-25 02:21:24 +02:00
// draw grid (vertical lines)
2024-08-23 00:46:46 +02:00
int dayGridInterval = maxDay < 700 ? 7 : 28;
for(const auto & line : data)
{
2024-08-25 00:43:55 +02:00
for(int i = 0; i < line.second.size(); i += dayGridInterval)
2024-08-23 00:46:46 +02:00
{
Point p = getPoint(i, line.second) + chartArea.topLeft();
canvas->addLine(Point(p.x, chartArea.topLeft().y), Point(p.x, chartArea.topLeft().y + chartArea.h), ColorRGBA(70, 70, 70));
}
}
2024-08-25 02:21:24 +02:00
// draw grid (horizontal lines)
if(maxVal > 0)
{
int gridStepPx = int((static_cast<float>(chartArea.h) / niceMaxVal) * gridStep);
for(int i = 0; i < std::ceil(maxVal / gridStep) + 1; i++)
{
canvas->addLine(chartArea.topLeft() + Point(0, chartArea.h - gridStepPx * i), chartArea.topLeft() + Point(chartArea.w, chartArea.h - gridStepPx * i), ColorRGBA(70, 70, 70));
layout.emplace_back(std::make_shared<CLabel>(chartArea.topLeft().x - 5, chartArea.topLeft().y + 10 + chartArea.h - gridStepPx * i, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, TextOperations::formatMetric(i * gridStep, 5)));
}
}
2024-08-12 23:28:08 +02:00
// draw
2024-08-16 00:03:45 +02:00
for(const auto & line : data)
2024-08-12 23:28:08 +02:00
{
2024-08-15 23:09:04 +02:00
Point lastPoint(-1, -1);
2024-08-12 23:28:08 +02:00
for(int i = 0; i < line.second.size(); i++)
{
2024-08-23 00:46:46 +02:00
Point p = getPoint(i, line.second) + chartArea.topLeft();
2024-08-12 23:28:08 +02:00
if(lastPoint.x != -1)
canvas->addLine(lastPoint, p, line.first);
2024-08-15 00:21:02 +02:00
// icons
for(auto & icon : icons)
if(std::get<0>(icon) == line.first && std::get<1>(icon) == i + 1) // color && day
{
pictures.emplace_back(std::make_shared<CPicture>(std::get<2>(icon), Point(p.x - (std::get<2>(icon)->width() / 2), p.y - (std::get<2>(icon)->height() / 2))));
2024-08-15 00:21:02 +02:00
pictures.back()->addRClickCallback([icon](){ CRClickPopup::createAndPush(std::get<3>(icon)); });
}
2024-08-12 23:28:08 +02:00
lastPoint = p;
}
}
2024-08-12 20:14:36 +02:00
// Axis
canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
2024-08-12 23:28:08 +02:00
2024-08-25 11:48:17 +02:00
Point p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
2024-08-15 21:48:35 +02:00
layout.emplace_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CStatisticScreen::getDay(maxDay)));
2024-08-14 22:36:54 +02:00
p = chartArea.bottomLeft() + Point(chartArea.w / 2, + 20);
2024-08-15 21:48:35 +02:00
layout.emplace_back(std::make_shared<CLabel>(p.x, p.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("core.genrltxt.64")));
2024-08-11 22:54:19 +02:00
}
2024-08-13 00:17:12 +02:00
void LineChart::updateStatusBar(const Point & cursorPosition)
{
statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
2024-08-17 12:25:16 +02:00
statusBar->fitToRect(pos, 10);
2024-08-13 00:17:12 +02:00
Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
statusBar->setEnabled(r.isInside(cursorPosition));
if(r.isInside(cursorPosition))
{
2024-08-25 02:21:24 +02:00
float x = (static_cast<float>(maxDay - 1) / static_cast<float>(chartArea.w)) * (static_cast<float>(cursorPosition.x) - static_cast<float>(r.x)) + 1.0f;
float y = niceMaxVal - (niceMaxVal / static_cast<float>(chartArea.h)) * (static_cast<float>(cursorPosition.y) - static_cast<float>(r.y));
2024-08-15 23:09:04 +02:00
statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + CStatisticScreen::getDay(x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + (static_cast<int>(y) > 0 ? std::to_string(static_cast<int>(y)) : std::to_string(y)));
2024-08-13 00:17:12 +02:00
}
2024-08-17 12:25:16 +02:00
setRedrawParent(true);
redraw();
2024-08-13 00:17:12 +02:00
}
void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
{
updateStatusBar(cursorPosition);
}
2024-08-22 23:27:21 +02:00
void LineChart::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
2024-08-13 00:17:12 +02:00
{
2024-08-22 23:27:21 +02:00
updateStatusBar(currentPosition);
2024-08-13 00:17:12 +02:00
}