VisualLogger.h 2.4 KB

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