2
0

CStatisticScreen.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, 64), ColorRGBA(64, 80, 128, 255), 1));
  37. layout.push_back(std::make_shared<CButton>(Point(725, 564), 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. switch (c)
  98. {
  99. case OVERVIEW:
  100. return std::make_shared<OverviewPanel>(contentArea.resize(-15), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), statistic);
  101. case CHART_RESOURCES:
  102. auto plotData = extractData(statistic, [res](StatisticDataSetEntry val) -> float { return val.resources[res]; });
  103. return std::make_shared<LineChart>(contentArea.resize(-5), CGI->generaltexth->translate(std::get<0>(contentInfo[c])), plotData);
  104. }
  105. return nullptr;
  106. }
  107. StatisticSelector::StatisticSelector(std::vector<std::string> texts, std::function<void(int selectedIndex)> cb)
  108. : CWindowObject(BORDERED), texts(texts), cb(cb)
  109. {
  110. OBJECT_CONSTRUCTION;
  111. pos = center(Rect(0, 0, 128 + 16, std::min((int)texts.size(), LINES) * 40));
  112. filledBackground = std::make_shared<FilledTexturePlayerColored>(ImagePath::builtin("DiBoxBck"), Rect(0, 0, pos.w, pos.h));
  113. filledBackground->setPlayerColor(PlayerColor(1));
  114. 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);
  115. slider->setPanningStep(40);
  116. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  117. update(0);
  118. }
  119. void StatisticSelector::update(int to)
  120. {
  121. OBJECT_CONSTRUCTION;
  122. buttons.clear();
  123. for(int i = to; i < LINES + to; i++)
  124. {
  125. if(i>=texts.size())
  126. continue;
  127. auto button = std::make_shared<CToggleButton>(Point(0, 10 + (i - to) * 40), AnimationPath::builtin("GSPBUT2"), CButton::tooltip(), [this, i](bool on){ close(); cb(i); });
  128. button->setTextOverlay(texts[i], EFonts::FONT_SMALL, Colors::WHITE);
  129. buttons.push_back(button);
  130. }
  131. }
  132. OverviewPanel::OverviewPanel(Rect position, std::string title, StatisticDataSet data)
  133. : CIntObject()
  134. {
  135. OBJECT_CONSTRUCTION;
  136. pos = position + pos.topLeft();
  137. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 10, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  138. int yOffs = 30;
  139. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, yOffs, pos.w - 16, pos.h - yOffs));
  140. int usedLines = 100;
  141. slider = std::make_shared<CSlider>(Point(pos.w - 16, yOffs), pos.h - yOffs, [this](int to){ update(); setRedrawParent(true); redraw(); }, LINES, usedLines, 0, Orientation::VERTICAL, CSlider::BLUE);
  142. slider->setPanningStep(canvas->pos.h / LINES);
  143. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, canvas->pos.h));
  144. Point fieldSize(canvas->pos.w / (graphics->playerColors.size() + 2), canvas->pos.h / LINES);
  145. for(int x = 0; x < graphics->playerColors.size() + 1; x++)
  146. for(int y = 0; y < LINES; y++)
  147. {
  148. int xStart = (x + (x == 0 ? 0 : 1)) * fieldSize.x;
  149. int yStart = y * fieldSize.y;
  150. if(x == 0 || y == 0)
  151. canvas->addBox(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(0, 0, 0, 100));
  152. canvas->addRectangle(Point(xStart, yStart), Point(x == 0 ? 2 * fieldSize.x : fieldSize.x, fieldSize.y), ColorRGBA(127, 127, 127, 255));
  153. }
  154. update();
  155. }
  156. void OverviewPanel::update()
  157. {
  158. OBJECT_CONSTRUCTION;
  159. content.clear();
  160. content.push_back(std::make_shared<CLabel>(pos.w / 2, 100, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, "blah" + vstd::getDateTimeISO8601Basic(std::time(0))));
  161. }
  162. LineChart::LineChart(Rect position, std::string title, std::map<ColorRGBA, std::vector<float>> data)
  163. : CIntObject(), maxVal(0), maxDay(0)
  164. {
  165. OBJECT_CONSTRUCTION;
  166. addUsedEvents(LCLICK | MOVE);
  167. pos = position + pos.topLeft();
  168. layout.push_back(std::make_shared<CLabel>(pos.w / 2, 20, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, title));
  169. chartArea = pos.resize(-50);
  170. chartArea.moveTo(Point(50, 50));
  171. canvas = std::make_shared<GraphicalPrimitiveCanvas>(Rect(0, 0, pos.w, pos.h));
  172. statusBar = CGStatusBar::create(0, 0, ImagePath::builtin("radialMenu/statusBar"));
  173. ((std::shared_ptr<CIntObject>)statusBar)->setEnabled(false);
  174. // additional calculations
  175. for(auto & line : data)
  176. {
  177. for(auto & val : line.second)
  178. if(maxVal < val)
  179. maxVal = val;
  180. if(maxDay < line.second.size())
  181. maxDay = line.second.size();
  182. }
  183. // draw
  184. for(auto & line : data)
  185. {
  186. Point lastPoint = Point(-1, -1);
  187. for(int i = 0; i < line.second.size(); i++)
  188. {
  189. float x = ((float)chartArea.w / (float)(maxDay-1)) * (float)i;
  190. float y = (float)chartArea.h - ((float)chartArea.h / maxVal) * line.second[i];
  191. Point p = Point(x, y) + chartArea.topLeft();
  192. if(lastPoint.x != -1)
  193. canvas->addLine(lastPoint, p, line.first);
  194. lastPoint = p;
  195. }
  196. }
  197. // Axis
  198. canvas->addLine(chartArea.topLeft() + Point(0, -10), chartArea.topLeft() + Point(0, chartArea.h + 10), Colors::WHITE);
  199. canvas->addLine(chartArea.topLeft() + Point(-10, chartArea.h), chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h), Colors::WHITE);
  200. Point p = chartArea.topLeft() + Point(-5, chartArea.h + 10);
  201. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, "0"));
  202. p = chartArea.topLeft() + Point(chartArea.w + 10, chartArea.h + 10);
  203. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(maxDay)));
  204. p = chartArea.topLeft() + Point(-5, -10);
  205. layout.push_back(std::make_shared<CLabel>(p.x, p.y, FONT_SMALL, ETextAlignment::CENTERRIGHT, Colors::WHITE, std::to_string((int)maxVal)));
  206. }
  207. void LineChart::updateStatusBar(const Point & cursorPosition)
  208. {
  209. statusBar->moveTo(cursorPosition + Point(-statusBar->pos.w / 2, 20));
  210. Rect r(pos.x + chartArea.x, pos.y + chartArea.y, chartArea.w, chartArea.h);
  211. statusBar->setEnabled(r.isInside(cursorPosition));
  212. if(r.isInside(cursorPosition))
  213. {
  214. float x = ((float)maxDay / (float)chartArea.w) * ((float)cursorPosition.x - (float)r.x) + 1.0f;
  215. float y = maxVal - ((float)maxVal / (float)chartArea.h) * ((float)cursorPosition.y - (float)r.y);
  216. statusBar->write(CGI->generaltexth->translate("core.genrltxt.64") + ": " + std::to_string((int)x) + " " + CGI->generaltexth->translate("vcmi.statisticWindow.value") + ": " + std::to_string((int)y));
  217. }
  218. GH.windows().totalRedraw();
  219. }
  220. void LineChart::mouseMoved(const Point & cursorPosition, const Point & lastUpdateDistance)
  221. {
  222. updateStatusBar(cursorPosition);
  223. }
  224. void LineChart::clickPressed(const Point & cursorPosition)
  225. {
  226. updateStatusBar(cursorPosition);
  227. }