1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00
vcmi/lib/logging/VisualLogger.h

121 lines
2.9 KiB
C++
Raw Normal View History

2024-02-03 12:19:48 +02:00
/*
* VisualLogger.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include "../int3.h"
#include "../constants/EntityIdentifiers.h"
2024-08-06 17:32:02 +02:00
#include "../battle/BattleHex.h"
2024-08-10 18:13:09 +02:00
#include "../Color.h"
2024-02-03 12:19:48 +02:00
VCMI_LIB_NAMESPACE_BEGIN
2024-08-06 17:32:02 +02:00
class IMapOverlayLogVisualizer
2024-02-03 12:19:48 +02:00
{
public:
virtual void drawLine(int3 start, int3 end) = 0;
virtual void drawText(int3 tile, int lineNumber, const std::string & text, const std::optional<ColorRGBA> & color) = 0;
2024-02-03 12:19:48 +02:00
};
2024-08-06 17:32:02 +02:00
class IBattleOverlayLogVisualizer
{
public:
virtual void drawText(BattleHex tile, int lineNumber, const std::string & text) = 0;
2024-08-06 17:32:02 +02:00
};
2024-08-10 18:13:09 +02:00
class DLL_LINKAGE IVisualLogBuilder
2024-02-03 12:19:48 +02:00
{
public:
virtual void addLine(int3 start, int3 end) = 0;
virtual void addText(int3 tile, const std::string & text, const std::optional<ColorRGBA> & color = {}) = 0;
virtual void addText(BattleHex tile, const std::string & text) = 0;
2024-08-10 18:13:09 +02:00
void addText(int3 tile, const std::string & text, PlayerColor background);
2024-02-03 12:19:48 +02:00
};
/// The logger is used to show screen overlay
class DLL_LINKAGE VisualLogger
{
private:
2024-08-06 17:32:02 +02:00
template<typename T>
struct Line
2024-02-03 12:19:48 +02:00
{
2024-08-06 17:32:02 +02:00
T start;
T end;
2024-02-03 12:19:48 +02:00
2024-08-06 17:32:02 +02:00
Line(T start, T end)
2024-02-03 12:19:48 +02:00
:start(start), end(end)
{
}
};
2024-08-06 17:32:02 +02:00
template<typename T>
struct Text
{
T tile;
std::string text;
2024-08-10 18:13:09 +02:00
std::optional<ColorRGBA> background;
2024-08-06 17:32:02 +02:00
2024-08-10 18:13:09 +02:00
Text(T tile, std::string text, std::optional<ColorRGBA> background)
:tile(tile), text(text), background(background)
2024-08-06 17:32:02 +02:00
{
}
};
2024-02-03 12:19:48 +02:00
class VisualLogBuilder : public IVisualLogBuilder
{
private:
2024-08-06 17:32:02 +02:00
std::vector<Line<int3>> & mapLines;
std::vector<Text<BattleHex>> & battleTexts;
2024-08-06 21:29:04 +02:00
std::vector<Text<int3>> & mapTexts;
2024-02-03 12:19:48 +02:00
public:
2024-08-06 17:32:02 +02:00
VisualLogBuilder(
std::vector<Line<int3>> & mapLines,
2024-08-06 21:29:04 +02:00
std::vector<Text<int3>> & mapTexts,
2024-08-06 17:32:02 +02:00
std::vector<Text<BattleHex>> & battleTexts)
2024-08-06 21:29:04 +02:00
:mapLines(mapLines), mapTexts(mapTexts), battleTexts(battleTexts)
2024-08-06 17:32:02 +02:00
{
}
void addLine(int3 start, int3 end) override
2024-02-03 12:19:48 +02:00
{
2024-08-06 17:32:02 +02:00
mapLines.emplace_back(start, end);
2024-02-03 12:19:48 +02:00
}
void addText(BattleHex tile, const std::string & text) override
2024-02-03 12:19:48 +02:00
{
2024-08-10 18:13:09 +02:00
battleTexts.emplace_back(tile, text, std::optional<ColorRGBA>());
2024-02-03 12:19:48 +02:00
}
2024-08-06 21:29:04 +02:00
void addText(int3 tile, const std::string & text, const std::optional<ColorRGBA> & background) override
2024-08-06 21:29:04 +02:00
{
2024-08-10 18:13:09 +02:00
mapTexts.emplace_back(tile, text, background);
2024-08-06 21:29:04 +02:00
}
2024-02-03 12:19:48 +02:00
};
private:
2024-08-06 17:32:02 +02:00
std::map<std::string, std::vector<Line<int3>>> mapLines;
2024-08-06 21:29:04 +02:00
std::map<std::string, std::vector<Text<int3>>> mapTexts;
2024-08-06 17:32:02 +02:00
std::map<std::string, std::vector<Text<BattleHex>>> battleTexts;
2024-02-03 12:19:48 +02:00
std::mutex mutex;
std::string keyToShow;
public:
void updateWithLock(const std::string & channel, const std::function<void(IVisualLogBuilder & logBuilder)> & func);
2024-08-06 17:32:02 +02:00
void visualize(IMapOverlayLogVisualizer & visulizer);
void visualize(IBattleOverlayLogVisualizer & visulizer);
void setKey(const std::string & key);
2024-02-03 12:19:48 +02:00
};
extern DLL_LINKAGE VisualLogger * logVisual;
VCMI_LIB_NAMESPACE_END