-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUILabel.cpp
More file actions
74 lines (62 loc) · 1.57 KB
/
GUILabel.cpp
File metadata and controls
74 lines (62 loc) · 1.57 KB
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
#include "GUILabel.h"
GUILabel::GUILabel(GUI* parent, const char* text, float size, glm::vec2 offset, glm::vec4 color)
{
_parent = parent;
_text = text;
_size = size * SCALE_FACTOR;
_offset = offset;
_color = color;
_visible = true;
}
GUILabel::GUILabel()
{
_parent = nullptr;
_text = "";
_size = SCALE_FACTOR;
_offset = glm::vec2(0.0f);
_color = glm::vec4(1.0f);
_visible = true;
}
GUILabel::~GUILabel()
{
}
void GUILabel::render(TextRenderer& renderer, TextShader* shader, int width, int height)
{
//Don't render if not visible
if(!_visible)
return;
glm::vec2 pos;
if(_offset.x != 0.0f && _offset.y != 0.0f) {
glm::vec2 textSize = renderer.getStringSize(_text, _size);
pos = toPixelCoords(_parent->getOrigin(), width, height);
pos.x -= textSize.x/2.0f;
} else {
pos = _offset;
}
renderer.renderText(shader, _text.c_str(), pos.x, pos.y, _size, _color);
}
glm::vec2 GUILabel::toPixelCoords(glm::vec2 coords, int width, int height)
{
float fwidth = width * 1.0f;
float fheight = height * 1.0f;
glm::vec2 newCoords;
if(coords.x < 0) {
newCoords.x = (coords.x * fwidth) + fwidth;
}
else if(coords.x == 0) {
newCoords.x = fwidth/2.0f;
}
else {
newCoords.x = (coords.x * fwidth);
}
if(coords.y < 0) {
newCoords.y = (coords.y * fheight) + fheight;
}
else if(coords.y == 0) {
newCoords.y = fheight/2.0f;
}
else {
newCoords.y = (coords.y * fheight);
}
return newCoords;
}