Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 129 additions & 125 deletions engine/src/components/textrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ TString TextRender::text() const {
*/
void TextRender::setText(const TString text) {
m_text = text;
composeMesh(m_font, m_mesh, m_size, m_text, m_alignment, m_kerning, m_wrap, m_boundaries);
if(m_font) {
composeMesh(m_font, m_mesh, m_size, m_text, m_alignment, m_kerning, m_wrap, m_boundaries);

for(auto it : m_materials) {
it->setTexture(gTexture, m_font->page());
}
}
}
/*!
Returns the font which will be used to draw a text.
Expand Down Expand Up @@ -233,136 +239,134 @@ AABBox TextRender::localBound() const {
\internal
*/
void TextRender::composeMesh(Font *font, Mesh *mesh, int size, const TString &text, int alignment, bool kerning, bool wrap, const Vector2 &boundaries) {
if(font) {
float spaceWidth = font->spaceWidth() * size;
float spaceLine = font->lineHeight() * size;

TString data = Engine::translate(text);
font->requestCharacters(data);

uint32_t length = font->length(data);
if(length) {
std::u32string u32 = data.toUtf32();

IndexVector &indices = mesh->indices();
Vector3Vector &vertices = mesh->vertices();
Vector2Vector &uv0 = mesh->uv0();
Vector4Vector &colors = mesh->colors();

vertices.resize(length * 4);
indices.resize(length * 6);
uv0.resize(length * 4);
colors.resize(length * 4);

std::list<float> width;
std::list<uint32_t> position;

Vector3 pos(0.0, boundaries.y - size, 0.0f);
uint32_t previous = 0;
uint32_t it = 0;
uint32_t space = 0;

for(uint32_t i = 0; i < length; i++) {
uint32_t ch = u32[i];
switch(ch) {
case ' ': {
pos += Vector3(spaceWidth, 0.0f, 0.0f);
space = it;
} break;
case '\t': {
pos += Vector3(spaceWidth * 4, 0.0f, 0.0f);
space = it;
} break;
case '\r': break;
case '\n': {
width.push_back(pos.x);
position.push_back(it);
pos = Vector3(0.0f, pos.y - spaceLine, 0.0f);
space = 0;
} break;
default: {
if(kerning) {
pos.x += font->requestKerning(ch, previous);
}
uint32_t index = font->atlasIndex(ch);

Mesh *glyph = font->shape(index);
if(glyph == nullptr) {
continue;
}

Vector3Vector &shape = glyph->vertices();
Vector2Vector &uv = glyph->uv0();

float x = pos.x + shape[2].x * size;
if(wrap && boundaries.x > 0.0f && boundaries.x < x && space > 0 && space < it) {
float shift = vertices[space * 4].x;
if((shift - spaceWidth) > 0.0f) {
for(uint32_t s = space; s < it; s++) {
vertices[s * 4 + 0] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 1] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 2] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 3] -= Vector3(shift, spaceLine, 0.0f);
}
width.push_back(shift - spaceWidth);
position.push_back(space);
pos = Vector3(pos.x - shift, pos.y - spaceLine, 0.0f);
float spaceWidth = font->spaceWidth() * size;
float spaceLine = font->lineHeight() * size;

TString data = Engine::translate(text);
font->requestCharacters(data);

uint32_t length = font->length(data);
if(length) {
std::u32string u32 = data.toUtf32();

IndexVector &indices = mesh->indices();
Vector3Vector &vertices = mesh->vertices();
Vector2Vector &uv0 = mesh->uv0();
Vector4Vector &colors = mesh->colors();

vertices.resize(length * 4);
indices.resize(length * 6);
uv0.resize(length * 4);
colors.resize(length * 4);

std::list<float> width;
std::list<uint32_t> position;

Vector3 pos(0.0, boundaries.y - size, 0.0f);
uint32_t previous = 0;
uint32_t it = 0;
uint32_t space = 0;

for(uint32_t i = 0; i < length; i++) {
uint32_t ch = u32[i];
switch(ch) {
case ' ': {
pos += Vector3(spaceWidth, 0.0f, 0.0f);
space = it;
} break;
case '\t': {
pos += Vector3(spaceWidth * 4, 0.0f, 0.0f);
space = it;
} break;
case '\r': break;
case '\n': {
width.push_back(pos.x);
position.push_back(it);
pos = Vector3(0.0f, pos.y - spaceLine, 0.0f);
space = 0;
} break;
default: {
if(kerning) {
pos.x += font->requestKerning(ch, previous);
}
uint32_t index = font->atlasIndex(ch);

Mesh *glyph = font->shape(index);
if(glyph == nullptr) {
continue;
}

Vector3Vector &shape = glyph->vertices();
Vector2Vector &uv = glyph->uv0();

float x = pos.x + shape[2].x * size;
if(wrap && boundaries.x > 0.0f && boundaries.x < x && space > 0 && space < it) {
float shift = vertices[space * 4].x;
if((shift - spaceWidth) > 0.0f) {
for(uint32_t s = space; s < it; s++) {
vertices[s * 4 + 0] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 1] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 2] -= Vector3(shift, spaceLine, 0.0f);
vertices[s * 4 + 3] -= Vector3(shift, spaceLine, 0.0f);
}
width.push_back(shift - spaceWidth);
position.push_back(space);
pos = Vector3(pos.x - shift, pos.y - spaceLine, 0.0f);
}

vertices[it * 4 + 0] = pos + shape[0] * size;
vertices[it * 4 + 1] = pos + shape[1] * size;
vertices[it * 4 + 2] = pos + shape[2] * size;
vertices[it * 4 + 3] = pos + shape[3] * size;

uv0[it * 4 + 0] = uv[0];
uv0[it * 4 + 1] = uv[1];
uv0[it * 4 + 2] = uv[2];
uv0[it * 4 + 3] = uv[3];

colors[it * 4 + 0] = Vector4(1.0f);
colors[it * 4 + 1] = Vector4(1.0f);
colors[it * 4 + 2] = Vector4(1.0f);
colors[it * 4 + 3] = Vector4(1.0f);

indices[it * 6 + 0] = it * 4 + 0;
indices[it * 6 + 1] = it * 4 + 1;
indices[it * 6 + 2] = it * 4 + 2;

indices[it * 6 + 3] = it * 4 + 0;
indices[it * 6 + 4] = it * 4 + 2;
indices[it * 6 + 5] = it * 4 + 3;

pos += Vector3(shape[2].x * size, 0.0f, 0.0f);
it++;
} break;
}
previous = ch;
}

vertices[it * 4 + 0] = pos + shape[0] * size;
vertices[it * 4 + 1] = pos + shape[1] * size;
vertices[it * 4 + 2] = pos + shape[2] * size;
vertices[it * 4 + 3] = pos + shape[3] * size;

uv0[it * 4 + 0] = uv[0];
uv0[it * 4 + 1] = uv[1];
uv0[it * 4 + 2] = uv[2];
uv0[it * 4 + 3] = uv[3];

colors[it * 4 + 0] = Vector4(1.0f);
colors[it * 4 + 1] = Vector4(1.0f);
colors[it * 4 + 2] = Vector4(1.0f);
colors[it * 4 + 3] = Vector4(1.0f);

indices[it * 6 + 0] = it * 4 + 0;
indices[it * 6 + 1] = it * 4 + 1;
indices[it * 6 + 2] = it * 4 + 2;

indices[it * 6 + 3] = it * 4 + 0;
indices[it * 6 + 4] = it * 4 + 2;
indices[it * 6 + 5] = it * 4 + 3;

pos += Vector3(shape[2].x * size, 0.0f, 0.0f);
it++;
} break;
}
previous = ch;
}

width.push_back(pos.x);
position.push_back(it);

vertices.resize(it * 4);
indices.resize(it * 6);
uv0.resize(it * 4);

auto w = width.begin();
auto p = position.begin();
float shiftX = (!(alignment & Left)) ? (boundaries.x - (*w)) / ((alignment & Center) ? 2 : 1) : 0.0f;
float shiftY = (!(alignment & Top)) ? (boundaries.y - position.size() * spaceLine) / ((alignment & Middle) ? 2 : 1) : 0.0f;
for(uint32_t i = 0; i < vertices.size(); i++) {
if(uint32_t(i / 4) >= *p) {
w++;
p++;
shiftX = (!(alignment & Left)) ? (boundaries.x - (*w)) / ((alignment & Center) ? 2 : 1) : 0.0f;
}
vertices[i].x += shiftX;
vertices[i].y -= shiftY;
width.push_back(pos.x);
position.push_back(it);

vertices.resize(it * 4);
indices.resize(it * 6);
uv0.resize(it * 4);

auto w = width.begin();
auto p = position.begin();
float shiftX = (!(alignment & Left)) ? (boundaries.x - (*w)) / ((alignment & Center) ? 2 : 1) : 0.0f;
float shiftY = (!(alignment & Top)) ? (boundaries.y - position.size() * spaceLine) / ((alignment & Middle) ? 2 : 1) : 0.0f;
for(uint32_t i = 0; i < vertices.size(); i++) {
if(uint32_t(i / 4) >= *p) {
w++;
p++;
shiftX = (!(alignment & Left)) ? (boundaries.x - (*w)) / ((alignment & Center) ? 2 : 1) : 0.0f;
}

mesh->recalcBounds();
vertices[i].x += shiftX;
vertices[i].y -= shiftY;
}

mesh->recalcBounds();
}
}
/*!
Expand Down
2 changes: 1 addition & 1 deletion engine/src/editor/converters/animconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AssetConverter::ReturnCode AnimConverter::convertFile(AssetConverterSettings *se
clip->loadUserData(map);
src.close();

return settings->saveBinary(clip);
return settings->saveBinary(Engine::toVariant(clip));
}

return InternalError;
Expand Down
2 changes: 1 addition & 1 deletion engine/src/editor/converters/assimpconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ AssetConverter::ReturnCode AssimpConverter::convertFile(AssetConverterSettings *
Prefab *prefab = Engine::objectCreate<Prefab>("");
prefab->setActor(root);

return settings->saveBinary(prefab);
return settings->saveBinary(Engine::toVariant(prefab));
}
return InternalError;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/editor/converters/controlschemeconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ AssetConverter::ReturnCode ControlSchemeConverter::convertFile(AssetConverterSet

src.close();

return settings->saveBinary(scheme);
return settings->saveBinary(Engine::toVariant(scheme));
}
return InternalError;
}
Expand Down
2 changes: 1 addition & 1 deletion engine/src/editor/converters/fontconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ AssetConverter::ReturnCode FontConverter::convertFile(AssetConverterSettings *se

font->loadUserData(map);

return settings->saveBinary(font);
return settings->saveBinary(Engine::toVariant(font));
}
return InternalError;
}
Expand Down
10 changes: 2 additions & 8 deletions engine/src/resources/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <cstring>

namespace {
const char *gPages = "Pages";
const char *gShapes = "Shapes";
const char *gPages("Pages");
const char *gShapes("Shapes");
}

/*!
Expand Down Expand Up @@ -258,12 +258,6 @@ Texture *Sprite::page(int key) {
index = it->second.second;
}

if(m_pages.empty()) {
Texture *texture = Engine::objectCreate<Texture>();
texture->setFiltering(Texture::Bilinear);
addPage(texture);
}

return (index < m_pages.size()) ? m_pages[index] : nullptr;
}
/*!
Expand Down
12 changes: 6 additions & 6 deletions modules/editor/shadertools/converter/functions/materialparam.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ class ParamVector : public ShaderNode {
A_OBJECT(ParamVector, ShaderNode, Shader/Parameters)

A_PROPERTIES(
A_PROPERTY(TString, Parameter_Name, ParamFloat::name, ParamFloat::setName),
A_PROPERTY(Vector4, Default_Value, ParamFloat::defaultValue, ParamFloat::setDefaultValue)
A_PROPERTY(TString, Parameter_Name, ParamVector::name, ParamVector::setName),
A_PROPERTY(Vector4, Default_Value, ParamVector::defaultValue, ParamVector::setDefaultValue)
)

public:
ParamVector() :
m_defaultValue(Vector4(0, 0, 0, 0)) {
m_defaultValue(Vector4(0.0f, 0.0f, 0.0f, 0.0f)) {

m_outputs.push_back(std::make_pair("Output", MetaType::VECTOR4));

Expand All @@ -68,9 +68,9 @@ class ParamVector : public ShaderNode {
type = link.oport->m_type;
}
static_cast<ShaderGraph *>(m_graph)->addUniform(name().data(), type, Vector4(m_defaultValue.x,
m_defaultValue.y,
m_defaultValue.z,
m_defaultValue.z));
m_defaultValue.y,
m_defaultValue.z,
m_defaultValue.z));
stack.push(QString("uni.%1").arg(name().data()));

return ShaderNode::build(code, stack, link, depth, type);
Expand Down
4 changes: 2 additions & 2 deletions modules/uikit/src/components/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ void Label::draw(CommandBuffer &buffer) {
if(m_material && !m_text.isEmpty()) {
m_material->setTransform(transform());

if(m_dirty) {
if(m_dirty && m_font) {
m_mesh->setName(actor()->name());
TextRender::composeMesh(m_font, m_mesh, m_size, m_text, m_alignment, m_kerning, m_wrap, m_meshSize);

if(m_material && m_font) {
if(m_material) {
m_material->setTexture(gTexture, m_font->page());
}

Expand Down
Loading