File: MapOverlayLogVisualizer.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (95 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * MapOverlayLogVisualizer.cpp, 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
 *
 */

#include "StdInc.h"
#include "MapOverlayLogVisualizer.h"
#include "MapViewModel.h"

#include "../../lib/logging/VisualLogger.h"
#include "../render/Canvas.h"
#include "../render/Colors.h"
#include "../render/EFont.h"
#include "../render/IFont.h"
#include "../render/IScreenHandler.h"
#include "../render/IRenderHandler.h"
#include "../render/Graphics.h"
#include "../gui/TextAlignment.h"
#include "../gui/CGuiHandler.h"


MapOverlayLogVisualizer::MapOverlayLogVisualizer(Canvas & target, std::shared_ptr<MapViewModel> model)
	: target(target), model(model)
{
}

void MapOverlayLogVisualizer::drawLine(int3 start, int3 end)
{
	auto level = model->getLevel();

	if(start.z != level || end.z != level)
		return;

	int scaling = GH.screenHandler().getScalingFactor();
	auto pStart = model->getTargetTileArea(start).center();
	auto pEnd = model->getTargetTileArea(end).center();
	Rect viewPortRaw = target.getRenderArea();
	Rect viewPort(viewPortRaw.topLeft() / scaling, viewPortRaw.dimensions() / scaling );

	Point workaroundOffset(8,8); // not sure why it is needed. Removing leads to incorrect clipping near view edges

	if(viewPort.isInside(pStart + workaroundOffset) && viewPort.isInside(pEnd + workaroundOffset))
	{
		target.drawLine(pStart, pEnd, ColorRGBA(255, 255, 0), ColorRGBA(255, 0, 0));
	}
}

void MapOverlayLogVisualizer::drawText(
	int3 tile,
	int lineNumber,
	const std::string & text,
	const std::optional<ColorRGBA> & background)
{
	const Point offset = Point(6, 6);

	auto level = model->getLevel();

	if(tile.z != level)
		return;

	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))
	{
		const auto & font = GH.renderHandler().loadFont(FONT_TINY);

		int w = font->getStringWidth(text);
		int h = font->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);
	}
}