VisualLogger.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * CLogger.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 "VisualLogger.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. DLL_LINKAGE VisualLogger * logVisual = new VisualLogger();
  14. void VisualLogger::updateWithLock(const std::string & channel, const std::function<void(IVisualLogBuilder & logBuilder)> & func)
  15. {
  16. std::lock_guard lock(mutex);
  17. mapLines[channel].clear();
  18. mapTexts[channel].clear();
  19. battleTexts[channel].clear();
  20. VisualLogBuilder builder(mapLines[channel], mapTexts[channel], battleTexts[channel]);
  21. func(builder);
  22. }
  23. void VisualLogger::visualize(IMapOverlayLogVisualizer & visulizer)
  24. {
  25. std::lock_guard lock(mutex);
  26. for(const auto & line : mapLines[keyToShow])
  27. {
  28. visulizer.drawLine(line.start, line.end);
  29. }
  30. std::map<int3, std::vector<Text<int3>>> textMap;
  31. for(const auto & line : mapTexts[keyToShow])
  32. {
  33. textMap[line.tile].push_back(line);
  34. }
  35. for(const auto & pair : textMap)
  36. {
  37. for(int i = 0; i < pair.second.size(); i++)
  38. {
  39. visulizer.drawText(pair.first, i, pair.second[i].text, pair.second[i].background);
  40. }
  41. }
  42. }
  43. void VisualLogger::visualize(IBattleOverlayLogVisualizer & visulizer)
  44. {
  45. std::lock_guard lock(mutex);
  46. std::map<BattleHex, std::vector<std::string>> textMap;
  47. for(auto line : battleTexts[keyToShow])
  48. {
  49. textMap[line.tile].push_back(line.text);
  50. }
  51. for(auto & pair : textMap)
  52. {
  53. for(int i = 0; i < pair.second.size(); i++)
  54. {
  55. visulizer.drawText(pair.first, i, pair.second[i]);
  56. }
  57. }
  58. }
  59. void VisualLogger::setKey(const std::string & key)
  60. {
  61. keyToShow = key;
  62. }
  63. void IVisualLogBuilder::addText(int3 tile, const std::string & text, PlayerColor background)
  64. {
  65. std::optional<ColorRGBA> rgbColor;
  66. switch(background)
  67. {
  68. case 0:
  69. rgbColor = ColorRGBA(255, 0, 0);
  70. break;
  71. case 1:
  72. rgbColor = ColorRGBA(0, 0, 255);
  73. break;
  74. case 2:
  75. rgbColor = ColorRGBA(128, 128, 128);
  76. break;
  77. case 3:
  78. rgbColor = ColorRGBA(0, 255, 0);
  79. break;
  80. case 4:
  81. rgbColor = ColorRGBA(255, 128, 0);
  82. break;
  83. case 5:
  84. rgbColor = ColorRGBA(128, 0, 128);
  85. break;
  86. case 6:
  87. rgbColor = ColorRGBA(0, 255, 255);
  88. break;
  89. case 7:
  90. rgbColor = ColorRGBA(255, 128, 255);
  91. break;
  92. }
  93. addText(tile, text, rgbColor);
  94. }
  95. VCMI_LIB_NAMESPACE_END