CStatisticScreen.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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), "Total Gold", 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. {
  57. plotData.emplace(graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet));
  58. tmpColorSet.clear();
  59. }
  60. tmpColor = val.player;
  61. }
  62. if(val.status == EPlayerStatus::INGAME)
  63. tmpColorSet.push_back(selector(val));
  64. }
  65. if(tmpColorSet.size())
  66. plotData.emplace(graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet));
  67. return plotData;
  68. }
  69. LineChart::LineChart(Rect position, std::string title, std::map<ColorRGBA, std::vector<float>> data)
  70. : CIntObject(), maxVal(0), maxDay(0)
  71. {
  72. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  73. addUsedEvents(LCLICK | MOVE);
  74. pos = position + pos.topLeft();
  75. chartArea = pos.resize(-50);
  76. chartArea.moveTo(Point(50, 50));
  77. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  78. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  79. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  80. statusBar->setEnabled(false);
  81. // additional calculations
  82. for(auto & line : data)
  83. {
  84. for(auto & val : line.second)
  85. if(maxVal < val)
  86. maxVal = val;
  87. if(maxDay < line.second.size())
  88. maxDay = line.second.size();
  89. }
  90. // draw
  91. for(auto & line : data)
  92. {
  93. Point lastPoint = Point(-1, -1);
  94. for(int i = 0; i < line.second.size(); i++)
  95. {
  96. float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
  97. float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
  98. Point p = Point(x, y) + chartArea.topLeft();
  99. if(lastPoint.x != -1)
  100. canvas->addLine(lastPoint, p, line.first);
  101. lastPoint = p;
  102. }
  103. }
  104. // Axis
  105. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  106. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  107. Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
  108. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
  109. p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  110. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(maxDay)));
  111. p = chartArea.topLeft() + Point(-5, -10);
  112. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
  113. }
  114. void LineChart::updateStatusBar(const Point & cursorPosition)
  115. {
  116. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  117. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  118. statusBar->setEnabled(r.isInside(cursorPosition));
  119. if(r.isInside(cursorPosition))
  120. {
  121. float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
  122. float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
  123. statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + std::to_string((int)x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + std::to_string((int)y));
  124. }
  125. GH.windows().totalRedraw();
  126. }
  127. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  128. {
  129. updateStatusBar(cursorPosition);
  130. }
  131. void LineChart::clickPressed(const Point & cursorPosition)
  132. {
  133. updateStatusBar(cursorPosition);
  134. }