1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-11-24 08:32:34 +02:00

Add visual logger for battle interface

This commit is contained in:
Andrii Danylchenko 2024-08-06 18:32:02 +03:00
parent 8e79263b21
commit 9ffd6368d4
5 changed files with 103 additions and 15 deletions

View File

@ -26,6 +26,7 @@
#include "../../lib/CStack.h" // TODO: remove
// Eventually only IBattleInfoCallback and battle::Unit should be used,
// CUnitState should be private and CStack should be removed completely
#include "../../lib/logging/VisualLogger.h"
#define LOGL(text) print(text)
#define LOGFL(text, formattingEl) print(boost::str(boost::format(text) % formattingEl))
@ -47,6 +48,17 @@ CBattleAI::~CBattleAI()
}
}
void logHexNumbers()
{
#if BATTLE_TRACE_LEVEL >= 1
logVisual->updateWithLock("hexes", [](IVisualLogBuilder & b)
{
for(BattleHex hex = BattleHex(0); hex < GameConstants::BFIELD_SIZE; hex = BattleHex(hex + 1))
b.addText(hex, std::to_string(hex.hex));
});
#endif
}
void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB)
{
env = ENV;
@ -57,6 +69,8 @@ void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
CB->waitTillRealize = false;
CB->unlockGsWhenWaiting = false;
movesSkippedByDefense = 0;
logHexNumbers();
}
void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences)

View File

@ -16,7 +16,12 @@
#include "BattleWindow.h"
#include "BattleSiegeController.h"
#include "BattleStacksController.h"
#include "BattleFieldController.h"
#include "BattleObstacleController.h"
#include "logging/VisualLogger.h"
#include "../client/render/Canvas.h"
#include "../client/render/Colors.h"
#include "../client/gui/TextAlignment.h"
void BattleRenderer::collectObjects()
{
@ -68,9 +73,30 @@ void BattleRenderer::insert(EBattleFieldLayer layer, BattleHex tile, BattleRende
objects.push_back({functor, layer, tile});
}
class VisualLoggerBattleRenderer : public IBattleOverlayLogVisualizer
{
private:
Canvas & target;
BattleInterface & owner;
public:
VisualLoggerBattleRenderer(Canvas & target, BattleInterface & owner) : target(target), owner(owner)
{
}
virtual void drawText(BattleHex hex, std::vector<std::string> texts) override
{
const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts);
}
};
void BattleRenderer::execute(BattleRenderer::RendererRef targetCanvas)
{
collectObjects();
sortObjects();
renderObjects(targetCanvas);
logVisual->visualize(VisualLoggerBattleRenderer(targetCanvas, owner));
}

View File

@ -58,7 +58,7 @@ BasicMapView::BasicMapView(const Point & offset, const Point & dimensions)
pos.h = dimensions.y;
}
class VisualLoggerRenderer : public ILogVisualizer
class VisualLoggerRenderer : public IMapOverlayLogVisualizer
{
private:
Canvas & target;

View File

@ -20,13 +20,14 @@ void VisualLogger::updateWithLock(std::string channel, std::function<void(IVisua
std::lock_guard<std::mutex> lock(mutex);
mapLines[channel].clear();
battleTexts[channel].clear();
VisualLogBuilder builder(mapLines[channel]);
VisualLogBuilder builder(mapLines[channel], battleTexts[channel]);
func(builder);
}
void VisualLogger::visualize(ILogVisualizer & visulizer)
void VisualLogger::visualize(IMapOverlayLogVisualizer & visulizer)
{
std::lock_guard<std::mutex> lock(mutex);
@ -36,6 +37,22 @@ void VisualLogger::visualize(ILogVisualizer & visulizer)
}
}
void VisualLogger::visualize(IBattleOverlayLogVisualizer & visulizer)
{
std::lock_guard<std::mutex> lock(mutex);
std::map<BattleHex, std::vector<std::string>> textMap;
for(auto line : battleTexts[keyToShow])
{
textMap[line.tile].push_back(line.text);
}
for(auto & pair : textMap)
{
visulizer.drawText(pair.first, pair.second);
}
}
void VisualLogger::setKey(std::string key)
{
keyToShow = key;

View File

@ -11,62 +11,93 @@
#include "../int3.h"
#include "../constants/EntityIdentifiers.h"
#include "../battle/BattleHex.h"
VCMI_LIB_NAMESPACE_BEGIN
class ILogVisualizer
class IMapOverlayLogVisualizer
{
public:
virtual void drawLine(int3 start, int3 end) = 0;
};
class IBattleOverlayLogVisualizer
{
public:
virtual void drawText(BattleHex tile, std::vector<std::string> texts) = 0;
};
class IVisualLogBuilder
{
public:
virtual void addLine(int3 start, int3 end) = 0;
virtual void addText(BattleHex tile, std::string text) = 0;
};
/// The logger is used to show screen overlay
class DLL_LINKAGE VisualLogger
{
private:
struct MapLine
template<typename T>
struct Line
{
int3 start;
int3 end;
T start;
T end;
MapLine(int3 start, int3 end)
Line(T start, T end)
:start(start), end(end)
{
}
};
template<typename T>
struct Text
{
T tile;
std::string text;
Text(T tile, std::string text)
:tile(tile), text(text)
{
}
};
class VisualLogBuilder : public IVisualLogBuilder
{
private:
std::vector<MapLine> & mapLines;
std::vector<Line<int3>> & mapLines;
std::vector<Text<BattleHex>> & battleTexts;
public:
VisualLogBuilder(std::vector<MapLine> & mapLines)
:mapLines(mapLines)
VisualLogBuilder(
std::vector<Line<int3>> & mapLines,
std::vector<Text<BattleHex>> & battleTexts)
:mapLines(mapLines), battleTexts(battleTexts)
{
}
virtual void addLine(int3 start, int3 end) override
void addLine(int3 start, int3 end) override
{
mapLines.push_back(MapLine(start, end));
mapLines.emplace_back(start, end);
}
void addText(BattleHex tile, std::string text) override
{
battleTexts.emplace_back(tile, text);
}
};
private:
std::map<std::string, std::vector<MapLine>> mapLines;
std::map<std::string, std::vector<Line<int3>>> mapLines;
std::map<std::string, std::vector<Text<BattleHex>>> battleTexts;
std::mutex mutex;
std::string keyToShow;
public:
void updateWithLock(std::string channel, std::function<void(IVisualLogBuilder & logBuilder)> func);
void visualize(ILogVisualizer & visulizer);
void visualize(IMapOverlayLogVisualizer & visulizer);
void visualize(IBattleOverlayLogVisualizer & visulizer);
void setKey(std::string key);
};