mirror of
https://github.com/luanti-org/luanti.git
synced 2025-10-24 21:35:21 +02:00
Add colored text (not only colored chat).
Add documentation, move files to a proper place and avoid memory leaks. Make it work with most kind of texts, and allow backgrounds too.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
CGUITTFont FreeType class for Irrlicht
|
||||
Copyright (c) 2009-2010 John Norman
|
||||
Copyright (c) 2016 Nathanaël Courant
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any
|
||||
@@ -545,6 +546,13 @@ void CGUITTFont::setFontHinting(const bool enable, const bool enable_auto_hintin
|
||||
|
||||
void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
|
||||
{
|
||||
draw(EnrichedString(std::wstring(text.c_str()), color), position, color, hcenter, vcenter, clip);
|
||||
}
|
||||
|
||||
void CGUITTFont::draw(const EnrichedString &text, const core::rect<s32>& position, video::SColor color, bool hcenter, bool vcenter, const core::rect<s32>* clip)
|
||||
{
|
||||
std::vector<video::SColor> colors = text.getColors();
|
||||
|
||||
if (!Driver)
|
||||
return;
|
||||
|
||||
@@ -572,7 +580,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
}
|
||||
|
||||
// Convert to a unicode string.
|
||||
core::ustring utext(text);
|
||||
core::ustring utext = text.getString();
|
||||
|
||||
// Set up our render map.
|
||||
core::map<u32, CGUITTGlyphPage*> Render_Map;
|
||||
@@ -581,6 +589,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
u32 n;
|
||||
uchar32_t previousChar = 0;
|
||||
core::ustring::const_iterator iter(utext);
|
||||
std::vector<video::SColor> applied_colors;
|
||||
while (!iter.atEnd())
|
||||
{
|
||||
uchar32_t currentChar = *iter;
|
||||
@@ -590,7 +599,7 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
if (currentChar == L'\r') // Mac or Windows breaks
|
||||
{
|
||||
lineBreak = true;
|
||||
if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks.
|
||||
if (*(iter + 1) == (uchar32_t)'\n') // Windows line breaks.
|
||||
currentChar = *(++iter);
|
||||
}
|
||||
else if (currentChar == (uchar32_t)'\n') // Unix breaks
|
||||
@@ -627,6 +636,9 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
page->render_positions.push_back(core::position2di(offset.X + offx, offset.Y + offy));
|
||||
page->render_source_rects.push_back(glyph.source_rect);
|
||||
Render_Map.set(glyph.glyph_page, page);
|
||||
u32 current_color = iter.getPos();
|
||||
if (current_color < colors.size())
|
||||
applied_colors.push_back(colors[current_color]);
|
||||
}
|
||||
offset.X += getWidthFromCharacter(currentChar);
|
||||
|
||||
@@ -645,8 +657,6 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
|
||||
CGUITTGlyphPage* page = n->getValue();
|
||||
|
||||
if (!use_transparency) color.color |= 0xff000000;
|
||||
|
||||
if (shadow_offset) {
|
||||
for (size_t i = 0; i < page->render_positions.size(); ++i)
|
||||
page->render_positions[i] += core::vector2di(shadow_offset, shadow_offset);
|
||||
@@ -654,7 +664,17 @@ void CGUITTFont::draw(const core::stringw& text, const core::rect<s32>& position
|
||||
for (size_t i = 0; i < page->render_positions.size(); ++i)
|
||||
page->render_positions[i] -= core::vector2di(shadow_offset, shadow_offset);
|
||||
}
|
||||
Driver->draw2DImageBatch(page->texture, page->render_positions, page->render_source_rects, clip, color, true);
|
||||
for (size_t i = 0; i < page->render_positions.size(); ++i) {
|
||||
irr::video::SColor col;
|
||||
if (!applied_colors.empty()) {
|
||||
col = applied_colors[i < applied_colors.size() ? i : 0];
|
||||
} else {
|
||||
col = irr::video::SColor(255, 255, 255, 255);
|
||||
}
|
||||
if (!use_transparency)
|
||||
col.color |= 0xff000000;
|
||||
Driver->draw2DImage(page->texture, page->render_positions[i], page->render_source_rects[i], clip, col, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
CGUITTFont FreeType class for Irrlicht
|
||||
Copyright (c) 2009-2010 John Norman
|
||||
Copyright (c) 2016 Nathanaël Courant
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any
|
||||
@@ -33,6 +34,8 @@
|
||||
|
||||
#include <irrlicht.h>
|
||||
#include <ft2build.h>
|
||||
#include <vector>
|
||||
#include "util/enriched_string.h"
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
namespace irr
|
||||
@@ -258,6 +261,10 @@ namespace gui
|
||||
virtual void draw(const core::stringw& text, const core::rect<s32>& position,
|
||||
video::SColor color, bool hcenter=false, bool vcenter=false,
|
||||
const core::rect<s32>* clip=0);
|
||||
|
||||
virtual void draw(const EnrichedString& text, const core::rect<s32>& position,
|
||||
video::SColor color, bool hcenter=false, bool vcenter=false,
|
||||
const core::rect<s32>* clip=0);
|
||||
|
||||
//! Returns the dimension of a character produced by this font.
|
||||
virtual core::dimension2d<u32> getCharDimension(const wchar_t ch) const;
|
||||
|
||||
Reference in New Issue
Block a user