CStatisticScreen.cpp 19 KB

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