CStatisticScreen.cpp 26 KB

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