VisualLogger.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * VisualLogger.h, 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. #pragma once
  11. #include "../int3.h"
  12. #include "../constants/EntityIdentifiers.h"
  13. #include "../battle/BattleHex.h"
  14. #include "../Color.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class IMapOverlayLogVisualizer
  17. {
  18. public:
  19. virtual void drawLine(int3 start, int3 end) = 0;
  20. virtual void drawText(int3 tile, int lineNumber, const std::string & text, const std::optional<ColorRGBA> & color) = 0;
  21. };
  22. class IBattleOverlayLogVisualizer
  23. {
  24. public:
  25. virtual void drawText(BattleHex tile, int lineNumber, const std::string & text) = 0;
  26. };
  27. class DLL_LINKAGE IVisualLogBuilder
  28. {
  29. public:
  30. virtual void addLine(int3 start, int3 end) = 0;
  31. virtual void addText(int3 tile, const std::string & text, const std::optional<ColorRGBA> & color = {}) = 0;
  32. virtual void addText(BattleHex tile, const std::string & text) = 0;
  33. void addText(int3 tile, const std::string & text, PlayerColor background);
  34. };
  35. /// The logger is used to show screen overlay
  36. class DLL_LINKAGE VisualLogger
  37. {
  38. private:
  39. template<typename T>
  40. struct Line
  41. {
  42. T start;
  43. T end;
  44. Line(T start, T end)
  45. :start(start), end(end)
  46. {
  47. }
  48. };
  49. template<typename T>
  50. struct Text
  51. {
  52. T tile;
  53. std::string text;
  54. std::optional<ColorRGBA> background;
  55. Text(T tile, std::string text, std::optional<ColorRGBA> background)
  56. :tile(tile), text(text), background(background)
  57. {
  58. }
  59. };
  60. class VisualLogBuilder : public IVisualLogBuilder
  61. {
  62. private:
  63. std::vector<Line<int3>> & mapLines;
  64. std::vector<Text<BattleHex>> & battleTexts;
  65. std::vector<Text<int3>> & mapTexts;
  66. public:
  67. VisualLogBuilder(
  68. std::vector<Line<int3>> & mapLines,
  69. std::vector<Text<int3>> & mapTexts,
  70. std::vector<Text<BattleHex>> & battleTexts)
  71. :mapLines(mapLines), mapTexts(mapTexts), battleTexts(battleTexts)
  72. {
  73. }
  74. void addLine(int3 start, int3 end) override
  75. {
  76. mapLines.emplace_back(start, end);
  77. }
  78. void addText(BattleHex tile, const std::string & text) override
  79. {
  80. battleTexts.emplace_back(tile, text, std::optional<ColorRGBA>());
  81. }
  82. void addText(int3 tile, const std::string & text, const std::optional<ColorRGBA> & background) override
  83. {
  84. mapTexts.emplace_back(tile, text, background);
  85. }
  86. };
  87. private:
  88. std::map<std::string, std::vector<Line<int3>>> mapLines;
  89. std::map<std::string, std::vector<Text<int3>>> mapTexts;
  90. std::map<std::string, std::vector<Text<BattleHex>>> battleTexts;
  91. std::mutex mutex;
  92. std::string keyToShow;
  93. public:
  94. void updateWithLock(const std::string & channel, const std::function<void(IVisualLogBuilder & logBuilder)> & func);
  95. void visualize(IMapOverlayLogVisualizer & visulizer);
  96. void visualize(IBattleOverlayLogVisualizer & visulizer);
  97. void setKey(const std::string & key);
  98. };
  99. extern DLL_LINKAGE VisualLogger * logVisual;
  100. VCMI_LIB_NAMESPACE_END