CStatisticScreen.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "../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/texts/CGeneralTextHandler.h"
  28. #include "../../lib/texts/TextOperations.h"
  29. #include <vstd/DateUtils.h>
  30. CStatisticScreen::CStatisticScreen(StatisticDataSet stat)
  31. : CWindowObject(BORDERED), statistic(stat)
  32. {
  33. OBJECT_CONSTRUCTION;
  34. pos = center(Rect(0, 0, 800, 600));
  35. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  36. filledBackground->setPlayerColor(PlayerColor(1));
  37. contentArea = Rect(10, 40, 780, 510);
  38. layout.push_back(std::make_shared<CLabel>(400, 20, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->translate("vcmi.statisticWindow.statistic")));
  39. layout.push_back(std::make_shared<TransparentFilledRectangle>(contentArea, ColorRGBA(0, 0, 0, 128), ColorRGBA(64, 80, 128, 255), 1));
  40. layout.push_back(std::make_shared<CButton>(Point(725, 558), AnimationPath::builtin("MUBCHCK"), CButton::tooltip(), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT));
  41. buttonSelect = std::make_shared<CToggleButton>(Point(10, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){
  42. std::vector<std::string> texts;
  43. for(auto & val : contentInfo)
  44. texts.push_back(CGI->generaltexth->translate(std::get<0>(val.second)));
  45. GH.windows().createAndPushWindow<StatisticSelector>(texts, [this](int selectedIndex)
  46. {
  47. OBJECT_CONSTRUCTION;
  48. if(!std::get<1>(contentInfo[(Content)selectedIndex]))
  49. mainContent = getContent((Content)selectedIndex, EGameResID::NONE);
  50. else
  51. {
  52. auto content = (Content)selectedIndex;
  53. auto possibleRes = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};
  54. std::vector<std::string> resourceText;
  55. for(auto & res : possibleRes)
  56. resourceText.push_back(CGI->generaltexth->translate(TextIdentifier("core.restypes", res.getNum()).get()));
  57. GH.windows().createAndPushWindow<StatisticSelector>(resourceText, [this, content, possibleRes](int selectedIndex)
  58. {
  59. OBJECT_CONSTRUCTION;
  60. mainContent = getContent(content, possibleRes[selectedIndex]);
  61. });
  62. }
  63. });
  64. });
  65. buttonSelect->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.selectView"), EFonts::FONT_SMALL, Colors::YELLOW);
  66. buttonCsvSave = std::make_shared<CToggleButton>(Point(150, 564), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this](bool on){ GH.input().copyToClipBoard(statistic.toCsv("\t")); });
  67. buttonCsvSave->setTextOverlay(CGI->generaltexth->translate("vcmi.statisticWindow.tsvCopy"), EFonts::FONT_SMALL, Colors::YELLOW);
  68. mainContent = getContent(OVERVIEW, EGameResID::NONE);
  69. }
  70. TData 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. TData 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. TData plotData;
  98. TIcons icons;
  99. switch (c)
  100. {
  101. case OVERVIEW:
  102. return std::make_shared<OverviewPanel>(contentArea.resize(-15), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
  103. case CHART_RESOURCES:
  104. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.resources[res]; });
  105. 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);
  106. case CHART_INCOME:
  107. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.income; });
  108. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  109. case CHART_NUMBER_OF_HEROES:
  110. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberHeroes; });
  111. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  112. case CHART_NUMBER_OF_TOWNS:
  113. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberTowns; });
  114. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  115. case CHART_NUMBER_OF_ARTIFACTS:
  116. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberArtifacts; });
  117. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  118. case CHART_NUMBER_OF_DWELLINGS:
  119. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.numberDwellings; });
  120. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  121. case CHART_NUMBER_OF_MINES:
  122. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.numMines[res]; });
  123. 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);
  124. case CHART_ARMY_STRENGTH:
  125. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.armyStrength; });
  126. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  127. case CHART_EXPERIENCE:
  128. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.totalExperience; });
  129. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 0);
  130. case CHART_RESOURCES_SPENT_ARMY:
  131. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForArmy[res]; });
  132. 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);
  133. case CHART_RESOURCES_SPENT_BUILDINGS:
  134. plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.spentResourcesForBuildings[res]; });
  135. 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);
  136. case CHART_MAP_EXPLORED:
  137. plotData = extractData(statistic, [](StatisticDataSetEntry val) -> float { return val.mapExploredRatio; });
  138. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData, icons, 1);
  139. }
  140. return nullptr;
  141. }
  142. StatisticSelector::StatisticSelector(std::vector<std::string> texts, std::function<void(int selectedIndex)> cb)
  143. : CWindowObject(BORDERED | NEEDS_ANIMATED_BACKGROUND), texts(texts), cb(cb)
  144. {
  145. OBJECT_CONSTRUCTION;
  146. pos = center(Rect(0, 0, 128 + 16, std::min((int)texts.size(), LINES) * 40));
  147. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  148. filledBackground->setPlayerColor(PlayerColor(1));
  149. 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);
  150. slider->setPanningStep(40);
  151. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  152. update(0);
  153. }
  154. void StatisticSelector::update(int to)
  155. {
  156. OBJECT_CONSTRUCTION;
  157. buttons.clear();
  158. for(int i = to; i < LINES + to; i++)
  159. {
  160. if(i>=texts.size())
  161. continue;
  162. auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
  163. button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
  164. buttons.push_back(button);
  165. }
  166. }
  167. OverviewPanel::OverviewPanel(Rect position, std::string title, StatisticDataSet stat)
  168. : CIntObject(), data(stat)
  169. {
  170. OBJECT_CONSTRUCTION;
  171. pos = position + pos.topLeft();
  172. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  173. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, Y_OFFS, pos.w - 16, pos.h - Y_OFFS));
  174. dataExtract = {
  175. {
  176. CGI->generaltexth->translate("vcmi.statisticWindow.param.playerName"), [this](PlayerColor color){
  177. return playerDataFilter(color).front().playerName;
  178. }
  179. },
  180. {
  181. CGI->generaltexth->translate("vcmi.statisticWindow.param.daysSurvived"), [this](PlayerColor color){
  182. return std::to_string(playerDataFilter(color).size());
  183. }
  184. },
  185. {
  186. CGI->generaltexth->translate("vcmi.statisticWindow.param.maxHeroLevel"), [this](PlayerColor color){
  187. int maxLevel = 0;
  188. for(auto val : playerDataFilter(color))
  189. if(maxLevel < val.maxHeroLevel)
  190. maxLevel = val.maxHeroLevel;
  191. return std::to_string(maxLevel);
  192. }
  193. },
  194. {
  195. CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioHero"), [this](PlayerColor color){
  196. auto val = playerDataFilter(color).back();
  197. if(!val.numBattlesPlayer)
  198. return std::string("");
  199. float tmp = ((float)val.numWinBattlesPlayer / (float)val.numBattlesPlayer) * 100;
  200. return std::to_string((int)tmp) + " %";
  201. }
  202. },
  203. {
  204. CGI->generaltexth->translate("vcmi.statisticWindow.param.battleWinRatioNeutral"), [this](PlayerColor color){
  205. auto val = playerDataFilter(color).back();
  206. if(!val.numWinBattlesNeutral)
  207. return std::string("");
  208. float tmp = ((float)val.numWinBattlesNeutral / (float)val.numBattlesNeutral) * 100;
  209. return std::to_string((int)tmp) + " %";
  210. }
  211. },
  212. {
  213. CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesHero"), [this](PlayerColor color){
  214. auto val = playerDataFilter(color).back();
  215. return std::to_string(val.numBattlesPlayer);
  216. }
  217. },
  218. {
  219. CGI->generaltexth->translate("vcmi.statisticWindow.param.battlesNeutral"), [this](PlayerColor color){
  220. auto val = playerDataFilter(color).back();
  221. return std::to_string(val.numBattlesNeutral);
  222. }
  223. },
  224. {
  225. CGI->generaltexth->translate("vcmi.statisticWindow.param.obeliskVisited"), [this](PlayerColor color){
  226. auto val = playerDataFilter(color).back();
  227. return std::to_string((int)(val.obeliskVisitedRatio * 100)) + " %";
  228. }
  229. },
  230. {
  231. CGI->generaltexth->translate("vcmi.statisticWindow.param.maxArmyStrength"), [this](PlayerColor color){
  232. int maxArmyStrength = 0;
  233. for(auto val : playerDataFilter(color))
  234. if(maxArmyStrength < val.armyStrength)
  235. maxArmyStrength = val.armyStrength;
  236. return TextOperations::formatMetric(maxArmyStrength, 6);
  237. }
  238. },
  239. {
  240. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GOLD).get()), [this](PlayerColor color){
  241. auto val = playerDataFilter(color).back();
  242. return std::to_string(val.tradeVolume[EGameResID::GOLD]);
  243. }
  244. },
  245. {
  246. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::WOOD).get()), [this](PlayerColor color){
  247. auto val = playerDataFilter(color).back();
  248. return std::to_string(val.tradeVolume[EGameResID::WOOD]);
  249. }
  250. },
  251. {
  252. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::MERCURY).get()), [this](PlayerColor color){
  253. auto val = playerDataFilter(color).back();
  254. return std::to_string(val.tradeVolume[EGameResID::MERCURY]);
  255. }
  256. },
  257. {
  258. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::ORE).get()), [this](PlayerColor color){
  259. auto val = playerDataFilter(color).back();
  260. return std::to_string(val.tradeVolume[EGameResID::ORE]);
  261. }
  262. },
  263. {
  264. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::SULFUR).get()), [this](PlayerColor color){
  265. auto val = playerDataFilter(color).back();
  266. return std::to_string(val.tradeVolume[EGameResID::SULFUR]);
  267. }
  268. },
  269. {
  270. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::CRYSTAL).get()), [this](PlayerColor color){
  271. auto val = playerDataFilter(color).back();
  272. return std::to_string(val.tradeVolume[EGameResID::CRYSTAL]);
  273. }
  274. },
  275. {
  276. CGI->generaltexth->translate("vcmi.statisticWindow.param.tradeVolume") + " - " + CGI->generaltexth->translate(TextIdentifier("core.restypes", EGameResID::GEMS).get()), [this](PlayerColor color){
  277. auto val = playerDataFilter(color).back();
  278. return std::to_string(val.tradeVolume[EGameResID::GEMS]);
  279. }
  280. },
  281. };
  282. int usedLines = dataExtract.size();
  283. 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);
  284. slider->setPanningStep(canvas->pos.h / LINES);
  285. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
  286. fieldSize = Point(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
  287. for(int x = 0; x < graphics->playerColors.size() + 1; x++)
  288. for(int y = 0; y < LINES; y++)
  289. {
  290. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
  291. int yStart = y * fieldSize.y;
  292. if(x == 0 || y == 0)
  293. canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
  294. canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
  295. }
  296. update(0);
  297. }
  298. std::vector<StatisticDataSetEntry> OverviewPanel::playerDataFilter(PlayerColor color)
  299. {
  300. std::vector<StatisticDataSetEntry> tmpData;
  301. std::copy_if(data.data.begin(), data.data.end(), std::back_inserter(tmpData), [color](StatisticDataSetEntry e){ return e.player == color; });
  302. return tmpData;
  303. }
  304. void OverviewPanel::update(int to)
  305. {
  306. OBJECT_CONSTRUCTION;
  307. content.clear();
  308. for(int y = to; y < LINES - 1 + to; y++)
  309. {
  310. if(y >= dataExtract.size())
  311. continue;
  312. for(int x = 0; x < PlayerColor::PLAYER_LIMIT_I + 1; x++)
  313. {
  314. if(y == to && x < PlayerColor::PLAYER_LIMIT_I)
  315. content.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ITGFLAGS"), x, 0, 180 + x * fieldSize.x, 35));
  316. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x + (x == 0 ? fieldSize.x : (fieldSize.x / 2));
  317. int yStart = Y_OFFS + (y + 1 - to) * fieldSize.y + (fieldSize.y / 2);
  318. PlayerColor tmpColor(x - 1);
  319. if(playerDataFilter(tmpColor).size() || x == 0)
  320. 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));
  321. }
  322. }
  323. }
  324. LineChart::LineChart(Rect position, std::string title, TData data, TIcons icons, float maxY)
  325. : CIntObject(), maxVal(0), maxDay(0)
  326. {
  327. OBJECT_CONSTRUCTION;
  328. addUsedEvents(LCLICK | MOVE);
  329. pos = position + pos.topLeft();
  330. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  331. chartArea = pos.resize(-50);
  332. chartArea.moveTo(Point(50, 50));
  333. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  334. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  335. ((std::shared_ptr<CIntObject>)statusBar)->setEnabled(false);
  336. // additional calculations
  337. bool skipMaxValCalc = maxY > 0;
  338. maxVal = maxY;
  339. for(auto & line : data)
  340. {
  341. for(auto & val : line.second)
  342. if(maxVal < val && !skipMaxValCalc)
  343. maxVal = val;
  344. if(maxDay < line.second.size())
  345. maxDay = line.second.size();
  346. }
  347. // draw
  348. for(auto & line : data)
  349. {
  350. Point lastPoint = Point(-1, -1);
  351. for(int i = 0; i < line.second.size(); i++)
  352. {
  353. float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
  354. float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
  355. Point p = Point(x, y) + chartArea.topLeft();
  356. if(lastPoint.x != -1)
  357. canvas->addLine(lastPoint, p, line.first);
  358. lastPoint = p;
  359. }
  360. }
  361. // Axis
  362. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  363. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  364. Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
  365. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
  366. p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  367. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(maxDay)));
  368. p = chartArea.topLeft() + Point(-5, -10);
  369. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
  370. }
  371. void LineChart::updateStatusBar(const Point & cursorPosition)
  372. {
  373. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  374. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  375. statusBar->setEnabled(r.isInside(cursorPosition));
  376. if(r.isInside(cursorPosition))
  377. {
  378. float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
  379. float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
  380. 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)));
  381. }
  382. GH.windows().totalRedraw();
  383. }
  384. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  385. {
  386. updateStatusBar(cursorPosition);
  387. }
  388. void LineChart::clickPressed(const Point & cursorPosition)
  389. {
  390. updateStatusBar(cursorPosition);
  391. }