CStatisticScreen.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/ComboBox.h"
  18. #include "../widgets/Images.h"
  19. #include "../widgets/GraphicalPrimitiveCanvas.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../widgets/Buttons.h"
  22. #include "../windows/InfoWindows.h"
  23. #include "../widgets/Slider.h"
  24. #include "../../lib/gameState/GameStatistics.h"
  25. #include "../../lib/texts/CGeneralTextHandler.h"
  26. #include <vstd/DateUtils.h>
  27. CStatisticScreen::CStatisticScreen(StatisticDataSet stat)
  28. : CWindowObject(BORDERED), statistic(stat)
  29. {
  30. OBJECT_CONSTRUCTION;
  31. pos = center(Rect(0, 0, 800, 600));
  32. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  33. filledBackground->setPlayerColor(PlayerColor(1));
  34. contentArea = Rect(10, 40, 780, 510);
  35. layout.push_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistic")));
  36. layout.push_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 64), ColorRGBA(64, 80, 128, 255), 1));
  37. layout.push_back(std::make_shared<CButton>(Point(725, 564), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
  38. buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
  39. std::vector<std::string> texts;
  40. for(auto & val : contentInfo)
  41. texts.push_back(CGI->generaltexth->translate(std::get<0>(val.second)));
  42. GH.windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
  43. {
  44. OBJECT_CONSTRUCTION;
  45. if(!std::get<1>(contentInfo[(Content)selectedIndex]))
  46. mainContent = getContent((Content)selectedIndex, EGameResID::NONE);
  47. else
  48. {
  49. auto content = (Content)selectedIndex;
  50. auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
  51. std::vector<std::string> resourceText;
  52. for(auto & res : possibleRes)
  53. resourceText.push_back(CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()));
  54. GH.windows().createAndPushWindow<StatisticSelector>(resourceText, [this, content, possibleRes](int selectedIndex)
  55. {
  56. OBJECT_CONSTRUCTION;
  57. mainContent = getContent(content, possibleRes[selectedIndex]);
  58. });
  59. }
  60. });
  61. });
  62. buttonSelect->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
  63. buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
  64. std::string path = statistic.writeCsv();
  65. CInfoWindow::showInfoDialog(CGI->generaltexth->translate("vcmi.statisticWindow.csvSaved") + "\n\n" + path, {});
  66. });
  67. buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.csvSave"), EFonts::FONT_SMALL, Colors::YELLOW);
  68. mainContent = getContent(OVERVIEW, EGameResID::NONE);
  69. }
  70. std::map<ColorRGBA, std::vector<float>> CStatisticScreen::extractData(StatisticDataSet stat, std::function<float(StatisticDataSetEntry val)> selector)
  71. {
  72. auto tmpData = stat.data;
  73. std::sort(tmpData.begin(), tmpData.end(), [](StatisticDataSetEntry v1, StatisticDataSetEntry v2){ return v1.player == v2.player ? v1.day < v2.day : v1.player < v2.player; });
  74. PlayerColor tmpColor = PlayerColor::NEUTRAL;
  75. std::vector<float> tmpColorSet;
  76. std::map<ColorRGBA, std::vector<float>> plotData;
  77. for(auto & val : tmpData)
  78. {
  79. if(tmpColor != val.player)
  80. {
  81. if(tmpColorSet.size())
  82. {
  83. plotData.emplace(graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet));
  84. tmpColorSet.clear();
  85. }
  86. tmpColor = val.player;
  87. }
  88. if(val.status == EPlayerStatus::INGAME)
  89. tmpColorSet.push_back(selector(val));
  90. }
  91. if(tmpColorSet.size())
  92. plotData.emplace(graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet));
  93. return plotData;
  94. }
  95. std::shared_ptr<CIntObject> CStatisticScreen::getContent(Content c, EGameResID res)
  96. {
  97. std::map<ColorRGBA, std::vector<float>> plotData;
  98. switch (c)
  99. {
  100. case OVERVIEW:
  101. return std::make_shared<OverviewPanel>(contentArea.resize(-15), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
  102. case CHART_RESOURCES:
  103. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.resources[res]; });
  104. 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, 0);
  105. case CHART_INCOME:
  106. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.income; });
  107. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  108. case CHART_NUMBER_OF_HEROES:
  109. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberHeroes; });
  110. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  111. case CHART_NUMBER_OF_TOWNS:
  112. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberTowns; });
  113. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  114. case CHART_NUMBER_OF_ARTIFACTS:
  115. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberArtifacts; });
  116. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  117. case CHART_NUMBER_OF_DWELLINGS:
  118. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberDwellings; });
  119. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  120. case CHART_NUMBER_OF_MINES:
  121. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.numMines[res]; });
  122. 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, 0);
  123. case CHART_ARMY_STRENGTH:
  124. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.armyStrength; });
  125. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  126. case CHART_EXPERIENCE:
  127. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.totalExperience; });
  128. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 0);
  129. case CHART_RESOURCES_SPENT_ARMY:
  130. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForArmy[res]; });
  131. 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, 0);
  132. case CHART_RESOURCES_SPENT_BUILDINGS:
  133. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForBuildings[res]; });
  134. 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, 0);
  135. case CHART_MAP_EXPLORED:
  136. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.mapExploredRatio; });
  137. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, 1);
  138. }
  139. return nullptr;
  140. }
  141. StatisticSelector::StatisticSelector(std::vector<std::string> texts, std::function<void(int selectedIndex)> cb)
  142. : CWindowObject(BORDERED), texts(texts), cb(cb)
  143. {
  144. OBJECT_CONSTRUCTION;
  145. pos = center(Rect(0, 0, 128 + 16, std::min((int)texts.size(), LINES) * 40));
  146. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  147. filledBackground->setPlayerColor(PlayerColor(1));
  148. 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);
  149. slider->setPanningStep(40);
  150. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  151. update(0);
  152. }
  153. void StatisticSelector::update(int to)
  154. {
  155. OBJECT_CONSTRUCTION;
  156. buttons.clear();
  157. for(int i = to; i < LINES + to; i++)
  158. {
  159. if(i>=texts.size())
  160. continue;
  161. auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
  162. button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
  163. buttons.push_back(button);
  164. }
  165. }
  166. OverviewPanel::OverviewPanel(Rect position, std::string title, StatisticDataSet data)
  167. : CIntObject()
  168. {
  169. OBJECT_CONSTRUCTION;
  170. pos = position + pos.topLeft();
  171. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  172. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, Y_OFFS, pos.w - 16, pos.h - Y_OFFS));
  173. auto playerDataFilter = [data](PlayerColor color){
  174. std::vector<StatisticDataSetEntry> tmpData;
  175. std::copy_if(data.data.begin(), data.data.end(), std::back_inserter(tmpData), [color](StatisticDataSetEntry e){return e.player == color;} );
  176. return tmpData;
  177. };
  178. dataExtract = {
  179. {
  180. CGI->generaltexth->translate("vcmi.statisticWindow.param.daysSurvived1"), [playerDataFilter](PlayerColor color){
  181. return playerDataFilter(color).size() ? std::to_string(playerDataFilter(color).size()) : "";
  182. }
  183. },
  184. //kill dead ratio (numBattles / numWinBattles)
  185. };
  186. int usedLines = dataExtract.size();
  187. 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);
  188. slider->setPanningStep(canvas->pos.h / LINES);
  189. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
  190. fieldSize = Point(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
  191. for(int x = 0; x < graphics->playerColors.size() + 1; x++)
  192. for(int y = 0; y < LINES; y++)
  193. {
  194. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
  195. int yStart = y * fieldSize.y;
  196. if(x == 0 || y == 0)
  197. canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
  198. canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
  199. }
  200. update(0);
  201. }
  202. void OverviewPanel::update(int to)
  203. {
  204. OBJECT_CONSTRUCTION;
  205. content.clear();
  206. for(int y = to; y < LINES - 1 + to; y++)
  207. {
  208. if(y >= dataExtract.size())
  209. continue;
  210. for(int x = 0; x < PlayerColor::PLAYER_LIMIT_I + 1; x++)
  211. {
  212. if(y == to)
  213. content.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), x, 0, 180 + x * fieldSize.x, 35));
  214. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x + (x == 0 ? fieldSize.x : (fieldSize.x / 2));
  215. int yStart = Y_OFFS + (y + 1 - to) * fieldSize.y + (fieldSize.y / 2);
  216. content.push_back(std::make_shared<CLabel>(xStart, yStart, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, (x == 0 ? dataExtract[y].first : dataExtract[y].second(PlayerColor(x - 1))), x == 0 ? (fieldSize.x * 2) : fieldSize.x));
  217. }
  218. }
  219. }
  220. LineChart::LineChart(Rect position, std::string title, std::map<ColorRGBA, std::vector<float>> data, float maxY)
  221. : CIntObject(), maxVal(0), maxDay(0)
  222. {
  223. OBJECT_CONSTRUCTION;
  224. addUsedEvents(LCLICK | MOVE);
  225. pos = position + pos.topLeft();
  226. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  227. chartArea = pos.resize(-50);
  228. chartArea.moveTo(Point(50, 50));
  229. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  230. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  231. ((std::shared_ptr<CIntObject>)statusBar)->setEnabled(false);
  232. // additional calculations
  233. bool skipMaxValCalc = maxY > 0;
  234. maxVal = maxY;
  235. for(auto & line : data)
  236. {
  237. for(auto & val : line.second)
  238. if(maxVal < val && !skipMaxValCalc)
  239. maxVal = val;
  240. if(maxDay < line.second.size())
  241. maxDay = line.second.size();
  242. }
  243. // draw
  244. for(auto & line : data)
  245. {
  246. Point lastPoint = Point(-1, -1);
  247. for(int i = 0; i < line.second.size(); i++)
  248. {
  249. float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
  250. float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
  251. Point p = Point(x, y) + chartArea.topLeft();
  252. if(lastPoint.x != -1)
  253. canvas->addLine(lastPoint, p, line.first);
  254. lastPoint = p;
  255. }
  256. }
  257. // Axis
  258. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  259. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  260. Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
  261. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
  262. p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  263. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(maxDay)));
  264. p = chartArea.topLeft() + Point(-5, -10);
  265. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
  266. }
  267. void LineChart::updateStatusBar(const Point & cursorPosition)
  268. {
  269. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  270. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  271. statusBar->setEnabled(r.isInside(cursorPosition));
  272. if(r.isInside(cursorPosition))
  273. {
  274. float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
  275. float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
  276. statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + std::to_string((int)x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + ((int)y > 0 ? std::to_string((int)y) : std::to_string(y)));
  277. }
  278. GH.windows().totalRedraw();
  279. }
  280. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  281. {
  282. updateStatusBar(cursorPosition);
  283. }
  284. void LineChart::clickPressed(const Point & cursorPosition)
  285. {
  286. updateStatusBar(cursorPosition);
  287. }