summaryrefslogtreecommitdiff
path: root/Runtime/GUI/TextMesh.cpp
blob: 6f3b3d6e1765413983f59ef13a48f69685c93095 (plain)
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
#include "../Graphics/CustomVertexLayout.h"
#include "Runtime/Utilities/StaticInitiator.h"
#include "../Math/Math.h"
#include "../Graphics/Color.h"
#include "TextMesh.h"
#include <vector>
#include <unordered_map>

using namespace std;

struct TextMeshVBOLayout
{
	Vector2 position;
	Vector2 uv;
	Color32 color;
};

static CustomVertexLayout s_TextMeshVBOLayout;

static unsigned int s_VertexPerText;
static unsigned int s_SizePerVertex;
static unsigned int s_SizePerText;
static unsigned int s_IndicesPerText;

struct TextInfo {
	const Character* ch;
	float offset;
};

static unordered_map<unsigned int, vector<TextInfo>> s_TextInfos;

InitializeStaticVariables([]() {
	VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout));
	VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout));
	VertexAttributeDescriptor COLOR = VertexAttributeDescriptor(sizeof(Vector2)*2, 4, VertexAttrFormat_Unsigned_Byte, sizeof(TextMeshVBOLayout), true);
	s_TextMeshVBOLayout.attributes.push_back(POSITION);
	s_TextMeshVBOLayout.attributes.push_back(UV);
	s_TextMeshVBOLayout.attributes.push_back(COLOR);

	s_VertexPerText = 4;
	s_SizePerVertex = sizeof(TextMeshVBOLayout);
	s_SizePerText = sizeof(TextMeshVBOLayout) * 4;
	s_IndicesPerText = 6;
});

TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
{
	m_Font = font;
	s_TextInfos.clear();

	float offset = 0;
	for (int i = 0; i < str.length; ++i)
	{
		character::Codepoint c = str.str[i];
		const Character* ch = font->GetCharacter(c, pixelSize);
		unsigned int index = ch->atlas;
		
		TextInfo info;
		info.ch = ch;
		info.offset = offset;

		auto list = s_TextInfos.find(index);
		if (list == s_TextInfos.end())
		{
			s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(index, vector<TextInfo>()));
			list = s_TextInfos.find(index);
		}
		vector<TextInfo>& v = list->second;
		v.push_back(info);

		offset += ch->advance;
	}

	if (s_TextInfos.size() == 0)
		return;

	for (auto iter : s_TextInfos) {
		unsigned int index = iter.first;
		vector<TextInfo>& texts = iter.second;
		int count = texts.size();

		VertexBuffer* vb = new VertexBuffer(count * s_SizePerText, count * s_IndicesPerText, VertexBuffer::VertexBufferType_Static);

	}
}

void TextMesh::Draw()
{

}