1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/mainmenu/CStatisticScreen.cpp

257 lines
9.4 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-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-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-12 01:07:58 +02:00
#include "../../lib/texts/CGeneralTextHandler.h"
2024-08-11 22:44:16 +02:00
2024-08-12 02:56:33 +02:00
#include <vstd/DateUtils.h>
2024-08-12 01:19:00 +02:00
CStatisticScreen::CStatisticScreen(StatisticDataSet stat)
: 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>(ImagePath::builtin("DiBoxBck"), 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-12 20:14:36 +02:00
layout.push_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistic")));
layout.push_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1));
2024-08-12 01:07:58 +02:00
layout.push_back(std::make_shared<CButton>(Point(725, 564), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
2024-08-12 02:56:33 +02:00
2024-08-13 20:41:55 +02:00
buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
2024-08-13 21:30:54 +02:00
std::vector<std::string> texts;
for(auto & val : contentText)
texts.push_back(CGI->generaltexth->translate(val));
GH.windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
2024-08-13 20:41:55 +02:00
{
2024-08-13 21:30:54 +02:00
mainContent = getContent((Content)selectedIndex);
2024-08-13 20:41:55 +02:00
});
});
buttonSelect->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
2024-08-12 20:14:36 +02:00
std::string path = statistic.writeCsv();
CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.statisticWindow.csvSaved") + "\n\n" + path, {});
2024-08-12 02:56:33 +02:00
});
buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.csvSave"), EFonts::FONT_SMALL, Colors::YELLOW);
2024-08-12 20:14:36 +02:00
2024-08-13 21:30:54 +02:00
mainContent = getContent(OVERVIEW);
2024-08-12 21:47:59 +02:00
}
std::map<ColorRGBA, std::vector<float>> CStatisticScreen::extractData(StatisticDataSet stat, std::function<float(StatisticDataSetEntry val)> selector)
{
auto tmpData = stat.data;
std::sort(tmpData.begin(), tmpData.end(), [](StatisticDataSetEntry v1, StatisticDataSetEntry v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
PlayerColor tmpColor = PlayerColor::NEUTRAL;
std::vector<float> tmpColorSet;
std::map<ColorRGBA, std::vector<float>> plotData;
for(auto & val : tmpData)
{
if(tmpColor != val.player)
{
if(tmpColorSet.size())
2024-08-12 23:28:08 +02:00
{
plotData.emplace(graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet));
tmpColorSet.clear();
}
2024-08-12 21:47:59 +02:00
tmpColor = val.player;
}
2024-08-12 23:28:08 +02:00
if(val.status == EPlayerStatus::INGAME)
tmpColorSet.push_back(selector(val));
2024-08-12 21:47:59 +02:00
}
2024-08-12 23:28:08 +02:00
if(tmpColorSet.size())
plotData.emplace(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-13 21:30:54 +02:00
std::shared_ptr<CIntObject> CStatisticScreen::getContent(Content c)
{
switch (c)
{
case OVERVIEW:
return std::make_shared<OverviewPanel>(contentArea.resize(-15), statistic);
case CHART_RESOURCES:
auto plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.resources[EGameResID::GOLD]; });
return std::make_shared<LineChart>(contentArea.resize(-5), contentText[c], plotData);
}
}
2024-08-13 20:41:55 +02:00
StatisticSelector::StatisticSelector(std::vector<std::string> texts, std::function<void(int selectedIndex)> cb)
: CWindowObject(BORDERED), texts(texts), cb(cb)
{
OBJECT_CONSTRUCTION;
pos = center(Rect(0, 0, 128 + 16, std::min((int)texts.size(), LINES) * 40));
filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
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;
auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ cb(i); close(); });
button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
buttons.push_back(button);
}
}
OverviewPanel::OverviewPanel(Rect position, StatisticDataSet data)
: CIntObject()
{
OBJECT_CONSTRUCTION;
pos = position + pos.topLeft();
layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("vcmi.statisticWindow.title.overview")));
int yOffs = 30;
canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, yOffs, pos.w - 16, pos.h - yOffs));
int usedLines = 100;
slider = std::make_shared<CSlider>(Point(pos.w - 16, yOffs), pos.h - yOffs, [this](int to){ update(); redraw(); }, LINES, usedLines, 0, Orientation::VERTICAL, CSlider::BLUE);
slider->setPanningStep(canvas->pos.h / LINES);
slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
update();
}
void OverviewPanel::update()
{
Point fieldSize(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
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 00:17:12 +02:00
LineChart::LineChart(Rect position, std::string title, std::map<ColorRGBA, std::vector<float>> data)
: 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-13 00:17:12 +02:00
addUsedEvents(LCLICK | MOVE);
2024-08-12 20:14:36 +02:00
pos = position + pos.topLeft();
2024-08-13 20:41:55 +02:00
layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
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"));
statusBar->setEnabled(false);
2024-08-12 23:28:08 +02:00
// additional calculations
for(auto & line : data)
{
for(auto & val : line.second)
if(maxVal < val)
maxVal = val;
if(maxDay < line.second.size())
maxDay = line.second.size();
}
// draw
for(auto & line : data)
{
Point lastPoint = Point(-1, -1);
for(int i = 0; i < line.second.size(); i++)
{
float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
Point p = Point(x, y) + chartArea.topLeft();
if(lastPoint.x != -1)
canvas->addLine(lastPoint, p, line.first);
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
Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(maxDay)));
p = chartArea.topLeft() + Point(-5, -10);
layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
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));
Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
statusBar->setEnabled(r.isInside(cursorPosition));
if(r.isInside(cursorPosition))
{
float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + std::to_string((int)x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + std::to_string((int)y));
}
GH.windows().totalRedraw();
}
void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
{
updateStatusBar(cursorPosition);
}
void LineChart::clickPressed(const Point & cursorPosition)
{
updateStatusBar(cursorPosition);
}