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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/*
* Text Renderer
* Copyright (C) 2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
* This file is part of The Mana Client.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "resources/theme.h"
#include <guichan/exception.hpp>
#include <guichan/font.hpp>
/**
* Class for text rendering which can apply an outline and shadow.
*/
class TextRenderer
{
public:
/**
* Renders a specified text.
*/
static void renderText(gcn::Graphics *graphics,
const std::string &text,
int x, int y,
gcn::Graphics::Alignment alignment,
const gcn::Color &color,
gcn::Font *font,
bool outline = false,
bool shadow = false,
const std::optional<gcn::Color> &outlineColor = {},
const std::optional<gcn::Color> &shadowColor = {})
{
switch (alignment)
{
case gcn::Graphics::LEFT:
break;
case gcn::Graphics::CENTER:
x -= font->getWidth(text) / 2;
break;
case gcn::Graphics::RIGHT:
x -= font->getWidth(text);
break;
default:
throw GCN_EXCEPTION("Unknown alignment.");
}
// Text shadow
if (shadow)
{
if (shadowColor)
{
graphics->setColor(*shadowColor);
}
else
{
auto sc = Theme::getThemeColor(Theme::SHADOW);
sc.a = color.a / 2;
graphics->setColor(sc);
}
if (outline)
font->drawString(graphics, text, x + 2, y + 2);
else
font->drawString(graphics, text, x + 1, y + 1);
}
if (outline)
{
/*
graphics->setColor(guiPalette->getColor(Palette::OUTLINE,
alpha/4));
// TODO: Reanable when we can draw it nicely in software mode
font->drawString(graphics, text, x + 2, y + 2);
font->drawString(graphics, text, x + 1, y + 2);
font->drawString(graphics, text, x + 2, y + 1);
*/
// Text outline
if (outlineColor)
{
graphics->setColor(*outlineColor);
}
else
{
auto oc = Theme::getThemeColor(Theme::OUTLINE);
oc.a = color.a;
graphics->setColor(oc);
}
font->drawString(graphics, text, x + 1, y);
font->drawString(graphics, text, x - 1, y);
font->drawString(graphics, text, x, y + 1);
font->drawString(graphics, text, x, y - 1);
}
graphics->setColor(color);
font->drawString(graphics, text, x, y);
}
/**
* Renders a specified text.
*/
static void renderText(gcn::Graphics *graphics,
const std::string &text,
int x,
int y,
gcn::Graphics::Alignment align,
gcn::Font *font,
const TextFormat &format)
{
renderText(graphics,
text,
x,
y,
align,
format.color,
font,
format.outlineColor.has_value(),
format.shadowColor.has_value(),
format.outlineColor,
format.shadowColor);
}
};
|