1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Visual logger colored text

This commit is contained in:
Andrii Danylchenko 2024-08-10 19:13:09 +03:00
parent 47c77826c3
commit 78dea24017
8 changed files with 105 additions and 22 deletions

View File

@ -36,7 +36,10 @@ void logHitmap(PlayerColor playerID, DangerHitMapAnalyzer & data)
b.addText(pos, std::to_string(treat.danger));
if(treat.hero.validAndSet())
{
b.addText(pos, std::to_string(treat.turn));
b.addText(pos, treat.hero->getNameTranslated());
}
});
});
@ -48,7 +51,10 @@ void logHitmap(PlayerColor playerID, DangerHitMapAnalyzer & data)
b.addText(pos, std::to_string(treat.danger));
if(treat.hero.validAndSet())
{
b.addText(pos, std::to_string(treat.turn));
b.addText(pos, treat.hero->getNameTranslated());
}
});
});
#endif

View File

@ -16,7 +16,9 @@
#include "../render/Canvas.h"
#include "../render/Colors.h"
#include "../render/EFont.h"
#include "../render/IFont.h"
#include "../gui/TextAlignment.h"
#include "../render/Graphics.h"
BattleOverlayLogVisualizer::BattleOverlayLogVisualizer(
BattleRenderer::RendererRef & target,
@ -25,9 +27,12 @@ BattleOverlayLogVisualizer::BattleOverlayLogVisualizer(
{
}
void BattleOverlayLogVisualizer::drawText(BattleHex hex, std::vector<std::string> texts)
void BattleOverlayLogVisualizer::drawText(BattleHex hex, int lineNumber, std::string text)
{
const Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
Point offset = owner.fieldController->hexPositionLocal(hex).topLeft() + Point(20, 20);
int h = graphics->fonts[EFonts::FONT_TINY]->getLineHeight();
target.drawText(offset, EFonts::FONT_TINY, Colors::RED, ETextAlignment::TOPCENTER, texts);
offset.y += h * lineNumber;
target.drawText(offset, EFonts::FONT_TINY, Colors::YELLOW, ETextAlignment::TOPCENTER, text);
}

View File

@ -24,5 +24,5 @@ private:
public:
BattleOverlayLogVisualizer(BattleRenderer::RendererRef & target, BattleInterface & owner);
void drawText(BattleHex hex, std::vector<std::string> texts) override;
void drawText(BattleHex hex, int lineNumber, std::string text) override;
};

View File

@ -75,5 +75,6 @@ void BattleRenderer::execute(BattleRenderer::RendererRef targetCanvas)
sortObjects();
renderObjects(targetCanvas);
logVisual->visualize(BattleOverlayLogVisualizer(targetCanvas, owner));
BattleOverlayLogVisualizer r(targetCanvas, owner);
logVisual->visualize(r);
}

View File

@ -16,6 +16,8 @@
#include "../render/Canvas.h"
#include "../render/Colors.h"
#include "../render/EFont.h"
#include "../render/IFont.h"
#include "../render/Graphics.h"
#include "../gui/TextAlignment.h"
@ -49,9 +51,13 @@ void MapOverlayLogVisualizer::drawLine(int3 start, int3 end)
}
}
void MapOverlayLogVisualizer::drawText(int3 tile, std::vector<std::string> texts)
void MapOverlayLogVisualizer::drawText(
int3 tile,
int lineNumber,
std::string text,
std::optional<ColorRGBA> background)
{
const Point offset = Point(5, 5);
const Point offset = Point(6, 6);
auto level = model->getLevel();
@ -61,8 +67,28 @@ void MapOverlayLogVisualizer::drawText(int3 tile, std::vector<std::string> texts
auto pStart = offset + model->getTargetTileArea(tile).topLeft();
auto viewPort = target.getRenderArea();
ColorRGBA color = Colors::YELLOW;
if(background)
{
color = ((background->b + background->r + background->g) < 300)
? Colors::WHITE
: Colors::BLACK;
}
if(viewPort.isInside(pStart))
{
target.drawText(pStart, EFonts::FONT_TINY, Colors::YELLOW, ETextAlignment::TOPCENTER, texts);
int w = graphics->fonts[EFonts::FONT_TINY]->getStringWidth(text);
int h = graphics->fonts[EFonts::FONT_TINY]->getLineHeight();
pStart.y += h * lineNumber;
if(background)
{
target.drawColor(Rect(pStart, Point(w + 4, h)), *background);
pStart.x += 2;
}
target.drawText(pStart, EFonts::FONT_TINY, color, ETextAlignment::TOPLEFT, text);
}
}

View File

@ -29,5 +29,5 @@ private:
public:
MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model);
void drawLine(int3 start, int3 end) override;
void drawText(int3 tile, std::vector<std::string> texts) override;
void drawText(int3 tile, int lineNumber, std::string text, std::optional<ColorRGBA> color) override;
};

View File

@ -37,16 +37,19 @@ void VisualLogger::visualize(IMapOverlayLogVisualizer & visulizer)
visulizer.drawLine(line.start, line.end);
}
std::map<int3, std::vector<std::string>> textMap;
std::map<int3, std::vector<Text<int3>>> textMap;
for(auto line : mapTexts[keyToShow])
{
textMap[line.tile].push_back(line.text);
textMap[line.tile].push_back(line);
}
for(auto & pair : textMap)
{
visulizer.drawText(pair.first, pair.second);
for(int i = 0; i < pair.second.size(); i++)
{
visulizer.drawText(pair.first, i, pair.second[i].text, pair.second[i].background);
}
}
}
@ -62,7 +65,10 @@ void VisualLogger::visualize(IBattleOverlayLogVisualizer & visulizer)
for(auto & pair : textMap)
{
visulizer.drawText(pair.first, pair.second);
for(int i = 0; i < pair.second.size(); i++)
{
visulizer.drawText(pair.first, i, pair.second[i]);
}
}
}
@ -71,4 +77,39 @@ void VisualLogger::setKey(std::string key)
keyToShow = key;
}
void IVisualLogBuilder::addText(int3 tile, std::string text, PlayerColor background)
{
std::optional<ColorRGBA> rgbColor;
switch(background)
{
case 0:
rgbColor = ColorRGBA(255, 0, 0);
break;
case 1:
rgbColor = ColorRGBA(0, 0, 255);
break;
case 2:
rgbColor = ColorRGBA(128, 128, 128);
break;
case 3:
rgbColor = ColorRGBA(0, 255, 0);
break;
case 4:
rgbColor = ColorRGBA(255, 128, 0);
break;
case 5:
rgbColor = ColorRGBA(128, 0, 128);
break;
case 6:
rgbColor = ColorRGBA(0, 255, 255);
break;
case 7:
rgbColor = ColorRGBA(255, 128, 255);
break;
}
addText(tile, text, rgbColor);
}
VCMI_LIB_NAMESPACE_END

View File

@ -12,6 +12,7 @@
#include "../int3.h"
#include "../constants/EntityIdentifiers.h"
#include "../battle/BattleHex.h"
#include "../Color.h"
VCMI_LIB_NAMESPACE_BEGIN
@ -19,21 +20,23 @@ class IMapOverlayLogVisualizer
{
public:
virtual void drawLine(int3 start, int3 end) = 0;
virtual void drawText(int3 tile, std::vector<std::string> texts) = 0;
virtual void drawText(int3 tile, int lineNumber, std::string text, std::optional<ColorRGBA> background) = 0;
};
class IBattleOverlayLogVisualizer
{
public:
virtual void drawText(BattleHex tile, std::vector<std::string> texts) = 0;
virtual void drawText(BattleHex tile, int lineNumber, std::string text) = 0;
};
class IVisualLogBuilder
class DLL_LINKAGE IVisualLogBuilder
{
public:
virtual void addLine(int3 start, int3 end) = 0;
virtual void addText(int3 tile, std::string text) = 0;
virtual void addText(int3 tile, std::string text, std::optional<ColorRGBA> color = {}) = 0;
virtual void addText(BattleHex tile, std::string text) = 0;
void addText(int3 tile, std::string text, PlayerColor background);
};
/// The logger is used to show screen overlay
@ -57,9 +60,10 @@ private:
{
T tile;
std::string text;
std::optional<ColorRGBA> background;
Text(T tile, std::string text)
:tile(tile), text(text)
Text(T tile, std::string text, std::optional<ColorRGBA> background)
:tile(tile), text(text), background(background)
{
}
};
@ -87,12 +91,12 @@ private:
void addText(BattleHex tile, std::string text) override
{
battleTexts.emplace_back(tile, text);
battleTexts.emplace_back(tile, text, std::optional<ColorRGBA>());
}
void addText(int3 tile, std::string text) override
void addText(int3 tile, std::string text, std::optional<ColorRGBA> background) override
{
mapTexts.emplace_back(tile, text);
mapTexts.emplace_back(tile, text, background);
}
};