CStatisticScreen.cpp 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * CStatisticScreen.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 "CStatisticScreen.h"
  12. #include "../CGameInfo.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/WindowHandler.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../render/Graphics.h"
  17. #include "../widgets/Images.h"
  18. #include "../widgets/GraphicalPrimitiveCanvas.h"
  19. #include "../widgets/TextControls.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/gameState/GameStatistics.h"
  23. #include "../../lib/texts/CGeneralTextHandler.h"
  24. #include <vstd/DateUtils.h>
  25. CStatisticScreen::CStatisticScreen(StatisticDataSet stat)
  26. : CWindowObject(BORDERED), statistic(stat)
  27. {
  28. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  29. pos = center(Rect(0, 0, 800, 600));
  30. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  31. filledBackground->setPlayerColor(PlayerColor(1));
  32. auto contentArea = Rect(10, 40, 780, 510);
  33. layout.push_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistic")));
  34. layout.push_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1));
  35. layout.push_back(std::make_shared<CButton>(Point(725, 564), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
  36. buttonCsvSave = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
  37. std::string path = statistic.writeCsv();
  38. CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.statisticWindow.csvSaved") + "\n\n" + path, {});
  39. });
  40. buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.csvSave"), EFonts::FONT_SMALL, Colors::YELLOW);
  41. auto plotData = extractData(stat, [](StatisticDataSetEntry val){ return val.resources[EGameResID::GOLD]; });
  42. chart = std::make_shared<LineChart>(contentArea.resize(-5), "test title", plotData);
  43. }
  44. std::map<ColorRGBA, std::vector<float>> CStatisticScreen::extractData(StatisticDataSet stat, std::function<float(StatisticDataSetEntry val)> selector)
  45. {
  46. auto tmpData = stat.data;
  47. std::sort(tmpData.begin(), tmpData.end(), [](StatisticDataSetEntry v1, StatisticDataSetEntry v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
  48. PlayerColor tmpColor = PlayerColor::NEUTRAL;
  49. std::vector<float> tmpColorSet;
  50. std::map<ColorRGBA, std::vector<float>> plotData;
  51. for(auto & val : tmpData)
  52. {
  53. if(tmpColor != val.player)
  54. {
  55. if(tmpColorSet.size())
  56. plotData[graphics->playerColors[tmpColor.getNum()]] = tmpColorSet;
  57. tmpColorSet.clear();
  58. tmpColor = val.player;
  59. }
  60. tmpColorSet.push_back(selector(val));
  61. }
  62. plotData[graphics->playerColors[tmpColor.getNum()]] = tmpColorSet;
  63. return plotData;
  64. }
  65. LineChart::LineChart(Rect position, std::string title, std::map<ColorRGBA, std::vector<float>> data) : CIntObject()
  66. {
  67. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  68. pos = position + pos.topLeft();
  69. auto chartArea = pos.resize(-50);
  70. chartArea.moveTo(Point(50, 50));
  71. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  72. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  73. // Axis
  74. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  75. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  76. canvas->addLine(chartArea.topLeft(), chartArea.bottomRight(), Colors::GREEN);
  77. }