CStatisticScreen.cpp 23 KB

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