CStatisticScreen.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 "../GameEngine.h"
  13. #include "../gui/WindowHandler.h"
  14. #include "../eventsSDL/InputHandler.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../render/Graphics.h"
  17. #include "../render/IImage.h"
  18. #include "../render/IRenderHandler.h"
  19. #include "../widgets/ComboBox.h"
  20. #include "../widgets/Images.h"
  21. #include "../widgets/GraphicalPrimitiveCanvas.h"
  22. #include "../widgets/TextControls.h"
  23. #include "../widgets/Buttons.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../widgets/Slider.h"
  26. #include "../../lib/gameState/GameStatistics.h"
  27. #include "../../lib/gameState/CGameState.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/texts/TextOperations.h"
  30. #include "../../lib/VCMI_Lib.h"
  31. #include <vstd/DateUtils.h>
  32. std::string CStatisticScreen::getDay(int d)
  33. {
  34. 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));
  35. }
  36. CStatisticScreen::CStatisticScreen(const StatisticDataSet & stat)
  37. : CWindowObject(BORDERED), statistic(stat)
  38. {
  39. OBJECT_CONSTRUCTION;
  40. pos = center(Rect(0, 0, 800, 600));
  41. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  42. filledBackground->setPlayerColor(PlayerColor(1));
  43. contentArea = Rect(10, 40, 780, 510);
  44. layout.emplace_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, VLC->generaltexth->translate("vcmi.statisticWindow.statistics")));
  45. layout.emplace_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 80, 128, 255), 1));
  46. layout.emplace_back(std::make_shared<CButton>(Point(725, 558), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
  47. buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ onSelectButton(); });
  48. buttonSelect->setTextOverlay(VLC->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
  49. buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ ENGINE->input().copyToClipBoard(statistic.toCsv("\t")); });
  50. buttonCsvSave->setTextOverlay(VLC->generaltexth->translate("vcmi.statisticWindow.tsvCopy"), EFonts::FONT_SMALL, Colors::YELLOW);
  51. mainContent = getContent(OVERVIEW, EGameResID::NONE);
  52. }
  53. void CStatisticScreen::onSelectButton()
  54. {
  55. std::vector<std::string> texts;
  56. for(auto & val : contentInfo)
  57. texts.emplace_back(VLC->generaltexth->translate(std::get<0>(val.second)));
  58. ENGINE->windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
  59. {
  60. OBJECT_CONSTRUCTION;
  61. if(!std::get<1>(contentInfo[static_cast<Content>(selectedIndex)]))
  62. mainContent = getContent(static_cast<Content>(selectedIndex), EGameResID::NONE);
  63. else
  64. {
  65. auto content = static_cast<Content>(selectedIndex);
  66. auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
  67. std::vector<std::string> resourceText;
  68. for(const auto & res : possibleRes)
  69. resourceText.emplace_back(VLC->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()));
  70. ENGINE->windows().createAndPushWindow<StatisticSelector>(resourceText, [this, content, possibleRes](int index)
  71. {
  72. OBJECT_CONSTRUCTION;
  73. mainContent = getContent(content, possibleRes[index]);
  74. });
  75. }
  76. });
  77. }
  78. TData CStatisticScreen::extractData(const StatisticDataSet & stat, const ExtractFunctor & selector) const
  79. {
  80. auto tmpData = stat.data;
  81. 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; });
  82. PlayerColor tmpColor = PlayerColor::NEUTRAL;
  83. std::vector<float> tmpColorSet;
  84. TData plotData;
  85. EPlayerStatus statusLastRound = EPlayerStatus::INGAME;
  86. for(const auto & val : tmpData)
  87. {
  88. if(tmpColor != val.player)
  89. {
  90. if(tmpColorSet.size())
  91. {
  92. plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
  93. tmpColorSet.clear();
  94. }
  95. tmpColor = val.player;
  96. }
  97. if(val.status == EPlayerStatus::INGAME || (statusLastRound == EPlayerStatus::INGAME && val.status == EPlayerStatus::LOSER))
  98. tmpColorSet.emplace_back(selector(val));
  99. statusLastRound = val.status; //to keep at least one dataset after loose
  100. }
  101. if(tmpColorSet.size())
  102. plotData.push_back({graphics->playerColors[tmpColor.getNum()], std::vector<float>(tmpColorSet)});
  103. return plotData;
  104. }
  105. TIcons CStatisticScreen::extractIcons() const
  106. {
  107. TIcons icons;
  108. auto tmpData = statistic.data;
  109. 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; });
  110. auto imageTown = ENGINE->renderHandler().loadImage(AnimationPath::builtin("cradvntr"), 3, 0, EImageBlitMode::COLORKEY);
  111. auto imageBattle = ENGINE->renderHandler().loadImage(AnimationPath::builtin("cradvntr"), 5, 0, EImageBlitMode::COLORKEY);
  112. auto imageDefeated = ENGINE->renderHandler().loadImage(AnimationPath::builtin("crcombat"), 0, 0, EImageBlitMode::COLORKEY);
  113. auto imageGrail = ENGINE->renderHandler().loadImage(AnimationPath::builtin("vwsymbol"), 2, 0, EImageBlitMode::COLORKEY);
  114. std::map<PlayerColor, bool> foundDefeated;
  115. std::map<PlayerColor, bool> foundGrail;
  116. for(const auto & val : tmpData)
  117. {
  118. if(val.eventCapturedTown)
  119. icons.push_back({ graphics->playerColors[val.player], val.day, imageTown, VLC->generaltexth->translate("vcmi.statisticWindow.icon.townCaptured") });
  120. if(val.eventDefeatedStrongestHero)
  121. icons.push_back({ graphics->playerColors[val.player], val.day, imageBattle, VLC->generaltexth->translate("vcmi.statisticWindow.icon.strongestHeroDefeated") });
  122. if(val.status == EPlayerStatus::LOSER && !foundDefeated[val.player])
  123. {
  124. foundDefeated[val.player] = true;
  125. icons.push_back({ graphics->playerColors[val.player], val.day, imageDefeated, VLC->generaltexth->translate("vcmi.statisticWindow.icon.defeated") });
  126. }
  127. if(val.hasGrail && !foundGrail[val.player])
  128. {
  129. foundGrail[val.player] = true;
  130. icons.push_back({ graphics->playerColors[val.player], val.day, imageGrail, VLC->generaltexth->translate("vcmi.statisticWindow.icon.grailFound") });
  131. }
  132. }
  133. return icons;
  134. }
  135. std::shared_ptr<CIntObject> CStatisticScreen::getContent(Content c, EGameResID res)
  136. {
  137. TData plotData;
  138. TIcons icons = extractIcons();
  139. switch (c)
  140. {
  141. case OVERVIEW:
  142. return std::make_shared<OverviewPanel>(contentArea.resize(-15), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
  143. case CHART_RESOURCES:
  144. plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.resources[res]; });
  145. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  146. case CHART_INCOME:
  147. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.income; });
  148. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  149. case CHART_NUMBER_OF_HEROES:
  150. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberHeroes; });
  151. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  152. case CHART_NUMBER_OF_TOWNS:
  153. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberTowns; });
  154. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  155. case CHART_NUMBER_OF_ARTIFACTS:
  156. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberArtifacts; });
  157. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  158. case CHART_NUMBER_OF_DWELLINGS:
  159. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.numberDwellings; });
  160. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  161. case CHART_NUMBER_OF_MINES:
  162. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.numMines[res]; });
  163. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  164. case CHART_ARMY_STRENGTH:
  165. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.armyStrength; });
  166. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  167. case CHART_EXPERIENCE:
  168. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.totalExperience; });
  169. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  170. case CHART_RESOURCES_SPENT_ARMY:
  171. plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.spentResourcesForArmy[res]; });
  172. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  173. case CHART_RESOURCES_SPENT_BUILDINGS:
  174. plotData = extractData(statistic, [res](const StatisticDataSetEntry & val) -> float { return val.spentResourcesForBuildings[res]; });
  175. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])) + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()), plotData, icons, 0);
  176. case CHART_MAP_EXPLORED:
  177. plotData = extractData(statistic, [](const StatisticDataSetEntry & val) -> float { return val.mapExploredRatio; });
  178. return std::make_shared<LineChart>(contentArea.resize(-5), VLC->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 1);
  179. }
  180. return nullptr;
  181. }
  182. StatisticSelector::StatisticSelector(const std::vector<std::string> & texts, const std::function<void(int selectedIndex)> & cb)
  183. : CWindowObject(BORDERED | NEEDS_ANIMATED_BACKGROUND), texts(texts), cb(cb)
  184. {
  185. OBJECT_CONSTRUCTION;
  186. pos = center(Rect(0, 0, 128 + 16, std::min(static_cast<int>(texts.size()), LINES) * 40));
  187. filledBackground = std::make_shared<FilledTexturePlayerColored>(Rect(0, 0, pos.w, pos.h));
  188. filledBackground->setPlayerColor(PlayerColor(1));
  189. 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);
  190. slider->setPanningStep(40);
  191. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  192. update(0);
  193. }
  194. void StatisticSelector::update(int to)
  195. {
  196. OBJECT_CONSTRUCTION;
  197. buttons.clear();
  198. for(int i = to; i < LINES + to; i++)
  199. {
  200. if(i>=texts.size())
  201. continue;
  202. auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
  203. button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
  204. buttons.emplace_back(button);
  205. }
  206. }
  207. OverviewPanel::OverviewPanel(Rect position, std::string title, const StatisticDataSet & stat)
  208. : CIntObject(), data(stat)
  209. {
  210. OBJECT_CONSTRUCTION;
  211. pos = position + pos.topLeft();
  212. layout.emplace_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  213. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, Y_OFFS, pos.w - 16, pos.h - Y_OFFS));
  214. dataExtract = {
  215. {
  216. VLC->generaltexth->translate("vcmi.statisticWindow.param.playerName"), [this](PlayerColor color){
  217. return playerDataFilter(color).front().playerName;
  218. }
  219. },
  220. {
  221. VLC->generaltexth->translate("vcmi.statisticWindow.param.daysSurvived"), [this](PlayerColor color){
  222. auto playerData = playerDataFilter(color);
  223. for(int i = 0; i < playerData.size(); i++)
  224. if(playerData[i].status == EPlayerStatus::LOSER)
  225. return CStatisticScreen::getDay(i + 1);
  226. return CStatisticScreen::getDay(playerData.size());
  227. }
  228. },
  229. {
  230. VLC->generaltexth->translate("vcmi.statisticWindow.param.maxHeroLevel"), [this](PlayerColor color){
  231. int maxLevel = 0;
  232. for(const auto & val : playerDataFilter(color))
  233. if(maxLevel < val.maxHeroLevel)
  234. maxLevel = val.maxHeroLevel;
  235. return std::to_string(maxLevel);
  236. }
  237. },
  238. {
  239. VLC->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioHero"), [this](PlayerColor color){
  240. auto val = playerDataFilter(color).back();
  241. if(!val.numBattlesPlayer)
  242. return std::string("");
  243. float tmp = (static_cast<float>(val.numWinBattlesPlayer) / static_cast<float>(val.numBattlesPlayer)) * 100;
  244. return std::to_string(static_cast<int>(tmp)) + " %";
  245. }
  246. },
  247. {
  248. VLC->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioNeutral"), [this](PlayerColor color){
  249. auto val = playerDataFilter(color).back();
  250. if(!val.numWinBattlesNeutral)
  251. return std::string("");
  252. float tmp = (static_cast<float>(val.numWinBattlesNeutral) / static_cast<float>(val.numBattlesNeutral)) * 100;
  253. return std::to_string(static_cast<int>(tmp)) + " %";
  254. }
  255. },
  256. {
  257. VLC->generaltexth->translate("vcmi.statisticWindow.param.battlesHero"), [this](PlayerColor color){
  258. auto val = playerDataFilter(color).back();
  259. return std::to_string(val.numBattlesPlayer);
  260. }
  261. },
  262. {
  263. VLC->generaltexth->translate("vcmi.statisticWindow.param.battlesNeutral"), [this](PlayerColor color){
  264. auto val = playerDataFilter(color).back();
  265. return std::to_string(val.numBattlesNeutral);
  266. }
  267. },
  268. {
  269. VLC->generaltexth->translate("vcmi.statisticWindow.param.obeliskVisited"), [this](PlayerColor color){
  270. auto val = playerDataFilter(color).back();
  271. return std::to_string(static_cast<int>(val.obeliskVisitedRatio * 100)) + " %";
  272. }
  273. },
  274. {
  275. VLC->generaltexth->translate("vcmi.statisticWindow.param.maxArmyStrength"), [this](PlayerColor color){
  276. int maxArmyStrength = 0;
  277. for(const auto & val : playerDataFilter(color))
  278. if(maxArmyStrength < val.armyStrength)
  279. maxArmyStrength = val.armyStrength;
  280. return TextOperations::formatMetric(maxArmyStrength, 6);
  281. }
  282. },
  283. {
  284. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GOLD).get()), [this](PlayerColor color){
  285. auto val = playerDataFilter(color).back();
  286. return std::to_string(val.tradeVolume[EGameResID::GOLD]);
  287. }
  288. },
  289. {
  290. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::WOOD).get()), [this](PlayerColor color){
  291. auto val = playerDataFilter(color).back();
  292. return std::to_string(val.tradeVolume[EGameResID::WOOD]);
  293. }
  294. },
  295. {
  296. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::MERCURY).get()), [this](PlayerColor color){
  297. auto val = playerDataFilter(color).back();
  298. return std::to_string(val.tradeVolume[EGameResID::MERCURY]);
  299. }
  300. },
  301. {
  302. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::ORE).get()), [this](PlayerColor color){
  303. auto val = playerDataFilter(color).back();
  304. return std::to_string(val.tradeVolume[EGameResID::ORE]);
  305. }
  306. },
  307. {
  308. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::SULFUR).get()), [this](PlayerColor color){
  309. auto val = playerDataFilter(color).back();
  310. return std::to_string(val.tradeVolume[EGameResID::SULFUR]);
  311. }
  312. },
  313. {
  314. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::CRYSTAL).get()), [this](PlayerColor color){
  315. auto val = playerDataFilter(color).back();
  316. return std::to_string(val.tradeVolume[EGameResID::CRYSTAL]);
  317. }
  318. },
  319. {
  320. VLC->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + VLC->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GEMS).get()), [this](PlayerColor color){
  321. auto val = playerDataFilter(color).back();
  322. return std::to_string(val.tradeVolume[EGameResID::GEMS]);
  323. }
  324. },
  325. };
  326. int usedLines = dataExtract.size();
  327. 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);
  328. slider->setPanningStep(canvas->pos.h / LINES);
  329. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
  330. fieldSize = Point(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
  331. for(int x = 0; x < graphics->playerColors.size() + 1; x++)
  332. for(int y = 0; y < LINES; y++)
  333. {
  334. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
  335. int yStart = y * fieldSize.y;
  336. if(x == 0 || y == 0)
  337. canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
  338. canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
  339. }
  340. update(0);
  341. }
  342. std::vector<StatisticDataSetEntry> OverviewPanel::playerDataFilter(PlayerColor color)
  343. {
  344. std::vector<StatisticDataSetEntry> tmpData;
  345. std::copy_if(data.data.begin(), data.data.end(), std::back_inserter(tmpData), [color](const StatisticDataSetEntry & e){ return e.player == color; });
  346. return tmpData;
  347. }
  348. void OverviewPanel::update(int to)
  349. {
  350. OBJECT_CONSTRUCTION;
  351. content.clear();
  352. for(int y = to; y < LINES - 1 + to; y++)
  353. {
  354. if(y >= dataExtract.size())
  355. continue;
  356. for(int x = 0; x < PlayerColor::PLAYER_LIMIT_I + 1; x++)
  357. {
  358. if(y == to && x < PlayerColor::PLAYER_LIMIT_I)
  359. content.emplace_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), x, 0, 180 + x * fieldSize.x, 35));
  360. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x + (x == 0 ? fieldSize.x : (fieldSize.x / 2));
  361. int yStart = Y_OFFS + (y + 1 - to) * fieldSize.y + (fieldSize.y / 2);
  362. PlayerColor tmpColor(x - 1);
  363. if(playerDataFilter(tmpColor).size() || x == 0)
  364. 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));
  365. }
  366. }
  367. }
  368. int computeGridStep(int maxAmount, int linesLimit)
  369. {
  370. for (int lineInterval = 1;;lineInterval *= 10)
  371. {
  372. for (int factor : { 1, 2, 5 } )
  373. {
  374. int lineIntervalToTest = lineInterval * factor;
  375. if (maxAmount / lineIntervalToTest <= linesLimit)
  376. return lineIntervalToTest;
  377. }
  378. }
  379. }
  380. LineChart::LineChart(Rect position, std::string title, TData data, TIcons icons, float maxY)
  381. : CIntObject(), maxVal(0), maxDay(0)
  382. {
  383. OBJECT_CONSTRUCTION;
  384. addUsedEvents(LCLICK | MOVE | GESTURE);
  385. pos = position + pos.topLeft();
  386. layout.emplace_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  387. chartArea = pos.resize(-50);
  388. chartArea.moveTo(Point(50, 50));
  389. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  390. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  391. static_cast<std::shared_ptr<CIntObject>>(statusBar)->setEnabled(false);
  392. // additional calculations
  393. bool skipMaxValCalc = maxY > 0;
  394. maxVal = maxY;
  395. for(const auto & line : data)
  396. {
  397. for(auto & val : line.second)
  398. if(maxVal < val && !skipMaxValCalc)
  399. maxVal = val;
  400. if(maxDay < line.second.size())
  401. maxDay = line.second.size();
  402. }
  403. //calculate nice maxVal
  404. int gridLineCount = 10;
  405. int gridStep = computeGridStep(maxVal, gridLineCount);
  406. niceMaxVal = gridStep * std::ceil(maxVal / gridStep);
  407. niceMaxVal = std::max(1, niceMaxVal); // avoid zero size Y axis (if all values are 0)
  408. // calculate points in chart
  409. auto getPoint = [this](int i, std::vector<float> data){
  410. float x = (static_cast<float>(chartArea.w) / static_cast<float>(maxDay - 1)) * static_cast<float>(i);
  411. float y = static_cast<float>(chartArea.h) - (static_cast<float>(chartArea.h) / niceMaxVal) * data[i];
  412. return Point(x, y);
  413. };
  414. // draw grid (vertical lines)
  415. int dayGridInterval = maxDay < 700 ? 7 : 28;
  416. for(const auto & line : data)
  417. {
  418. for(int i = 0; i < line.second.size(); i += dayGridInterval)
  419. {
  420. Point p = getPoint(i, line.second) + chartArea.topLeft();
  421. canvas->addLine(Point(p.x, chartArea.topLeft().y), Point(p.x, chartArea.topLeft().y + chartArea.h), ColorRGBA(70, 70, 70));
  422. }
  423. }
  424. // draw grid (horizontal lines)
  425. if(maxVal > 0)
  426. {
  427. int gridStepPx = int((static_cast<float>(chartArea.h) / niceMaxVal) * gridStep);
  428. for(int i = 0; i < std::ceil(maxVal / gridStep) + 1; i++)
  429. {
  430. canvas->addLine(chartArea.topLeft() + Point(0, chartArea.h - gridStepPx * i), chartArea.topLeft() + Point(chartArea.w, chartArea.h - gridStepPx * i), ColorRGBA(70, 70, 70));
  431. 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)));
  432. }
  433. }
  434. // draw
  435. for(const auto & line : data)
  436. {
  437. Point lastPoint(-1, -1);
  438. for(int i = 0; i < line.second.size(); i++)
  439. {
  440. Point p = getPoint(i, line.second) + chartArea.topLeft();
  441. if(lastPoint.x != -1)
  442. canvas->addLine(lastPoint, p, line.first);
  443. // icons
  444. for(auto & icon : icons)
  445. if(std::get<0>(icon) == line.first && std::get<1>(icon) == i + 1) // color && day
  446. {
  447. 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))));
  448. pictures.back()->addRClickCallback([icon](){ CRClickPopup::createAndPush(std::get<3>(icon)); });
  449. }
  450. lastPoint = p;
  451. }
  452. }
  453. // Axis
  454. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  455. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  456. Point p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  457. layout.emplace_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CStatisticScreen::getDay(maxDay)));
  458. p = chartArea.bottomLeft() + Point(chartArea.w / 2, + 20);
  459. layout.emplace_back(std::make_shared<CLabel>(p.x, p.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, VLC->generaltexth->translate("core.genrltxt.64")));
  460. }
  461. void LineChart::updateStatusBar(const Point & cursorPosition)
  462. {
  463. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  464. statusBar->fitToRect(pos, 10);
  465. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  466. statusBar->setEnabled(r.isInside(cursorPosition));
  467. if(r.isInside(cursorPosition))
  468. {
  469. float x = (static_cast<float>(maxDay - 1) / static_cast<float>(chartArea.w)) * (static_cast<float>(cursorPosition.x) - static_cast<float>(r.x)) + 1.0f;
  470. float y = niceMaxVal - (niceMaxVal / static_cast<float>(chartArea.h)) * (static_cast<float>(cursorPosition.y) - static_cast<float>(r.y));
  471. statusBar->write(VLC->generaltexth->translate("core.genrltxt.64") + ": " + CStatisticScreen::getDay(x) + " " + VLC->generaltexth->translate("vcmi.statisticWindow.value") + ": " + (static_cast<int>(y) > 0 ? std::to_string(static_cast<int>(y)) : std::to_string(y)));
  472. }
  473. setRedrawParent(true);
  474. redraw();
  475. }
  476. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  477. {
  478. updateStatusBar(cursorPosition);
  479. }
  480. void LineChart::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  481. {
  482. updateStatusBar(currentPosition);
  483. }