-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.cpp
More file actions
119 lines (99 loc) · 2.77 KB
/
GUI.cpp
File metadata and controls
119 lines (99 loc) · 2.77 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
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
#include "GUI.h"
#include "Loader.h"
GUI::GUI(GUI* parent, float x, float y, float sx, float sy, std::string& textureFile, bool relativePos /* true */)
{
printf("IN GUI Constructorn 1\n");
_vaoID = 0;
_vboID = 0;
if(relativePos) {
_position = parent->getPosition() + glm::vec2(x,y);
_origin = parent->getOrigin() + (glm::vec2(x,y));
}
else {
_position = glm::vec2(x,y);
_origin = _position + glm::vec2(_scale.x/2.0f, 0.0f);
}
_scale = glm::vec2(sx, sy);
_texture = Loader::loadPNG(textureFile);
_uv = glm::vec4(0,0,1,1);
_visible = true;
_parent = parent;
_parent->addChild(this);
createIDs();
}
GUI::GUI(float x, float y, float sx, float sy, std::string& textureFile)
{
printf("IN GUI Constructorn 2\n");
_vaoID = 0;
_vboID = 0;
_position = glm::vec2(x,y);
_scale = glm::vec2(sx, sy);
_origin = _position + glm::vec2(_scale.x/2.0f, 0.0f);
_texture = Loader::loadPNG(textureFile);
_uv = glm::vec4(0,0,1,1);
_visible = true;
printf("About to assign parent nullptr\n");
_parent = nullptr;
createIDs();
}
GUI::GUI()
{
printf("IN GUI Constructorn 3\n");
_vaoID = 0;
_vboID = 0;
_position = glm::vec2(0.0f);
_scale = glm::vec2(1.0f);
_origin = _position + glm::vec2(_scale.x/2.0f, 0.0f);
_uv = glm::vec4(0,0,1,1);
_visible = true;
_parent = nullptr;
createIDs();
}
GUI::~GUI()
{
}
void GUI::createIDs()
{
glGenVertexArrays(1, &_vaoID);
glGenBuffers(1, &_vboID);
glBindVertexArray(_vaoID);
glBindBuffer(GL_ARRAY_BUFFER, _vboID);
glBufferData(GL_ARRAY_BUFFER, 10 * sizeof(float), &_vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
bool GUI::inBounds(glm::vec2 coords)
{
glm::vec4 bounds = getBounds();
return coords.x > bounds.x && coords.x < bounds.y && coords.y > bounds.z && coords.y < bounds.w;
}
void GUI::render()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _texture.id);
glBindVertexArray(_vaoID);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 10);
glBindVertexArray(0);
}
glm::vec4 GUI::getBounds()
{
glm::vec4 bounds;
bounds.x = _position.x - _scale.x;
bounds.y = _position.x + _scale.x;
bounds.z = _position.y - _scale.y;
bounds.w = _position.y + _scale.y;
return bounds;
}
void GUI::addChild(GUI* child)
{
bool exists = false;
for(unsigned int i = 0; i < _children.size(); i++)
if(_children[i]->getID() == child->getID())
exists = true;
if(!exists)
_children.push_back(child);
else
printf("Child [%d] already exists\n", child->getID());
}