Browse Source

layout doesn't reset position (unless parent)

pull/2/head
Odilitime 5 years ago
parent
commit
cd3a3f578c
  1. 88
      src/graphics/components/Component.cpp

88
src/graphics/components/Component.cpp

@ -53,6 +53,7 @@ void Component::setParent(std::shared_ptr<Component> passedParent) { @@ -53,6 +53,7 @@ void Component::setParent(std::shared_ptr<Component> passedParent) {
// update our position
x = passedParent->x;
y = passedParent->y - passedParent->height;
//std::cout << "Component::setParent - placing at " << static_cast<int>(x) << "," << static_cast<int>(y) << std::endl;
// add new component as child to parent
//std::shared_ptr<Component> child=*new std::shared_ptr<Component>(this);
@ -67,9 +68,11 @@ void Component::layout() { @@ -67,9 +68,11 @@ void Component::layout() {
//int lx = x;
//int ly = y;
// if we want to position the root component and layout (because to move things we need to trigger a resize)
// then we need this to not reset because it will affect the position we're trying to relayout them out at
// reset position
x = 0;
y = 0;
//x = 0;
//y = 0;
/*
TextComponent *textComponent = dynamic_cast<TextComponent*>(this);
@ -132,7 +135,7 @@ void Component::layout() { @@ -132,7 +135,7 @@ void Component::layout() {
}
}
//std::cout << "Component::layout - adjusted by prev: " << (int)x << "x" << (int)y << std::endl;
//std::cout << "moving component to " << (int)x << "x" << (int)y << std::endl;
//std::cout << "Component::layout - moving component to " << (int)x << "x" << (int)y << std::endl;
// change in X position
//std::cout << "Component::layout - component was at " << lx << " moved(?) to " << (int)x << std::endl;
@ -242,15 +245,93 @@ void Component::updateParentSize() { @@ -242,15 +245,93 @@ void Component::updateParentSize() {
}
}
GLuint elementBufferObject = 0;
void Component::initTheElementBufferObject() const {
glGenBuffers(1, &elementBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
GLuint Component::CreateVertexArrayObject() const {
GLuint vertexArrayObject = 0;
glGenVertexArrays(1, &vertexArrayObject);
glBindVertexArray(vertexArrayObject);
// we may need the VBO here...
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferObject);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), nullptr);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), reinterpret_cast<void*>(3 * sizeof(float)));
glEnableVertexAttribArray(1);
//glBindVertexArray(0);
return vertexArrayObject;
}
GLuint Component::CreateVertexBufferObject() const {
GLuint vertexBufferObject = 0;
glGenBuffers(1, &vertexBufferObject);
return vertexBufferObject;
}
// this always creates, we probably should just respond to existing state
bool Component::setPosition4(GLuint vertexBufferObject, int x0, int y0, int x1, int y1) const {
float vertices[20] = {
0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
vertices[(0 * 5) + 0] = x0;
vertices[(0 * 5) + 1] = y1;
vertices[(1 * 5) + 0] = x1;
vertices[(1 * 5) + 1] = y1;
vertices[(2 * 5) + 0] = x1;
vertices[(2 * 5) + 1] = y0;
vertices[(3 * 5) + 0] = x0;
vertices[(3 * 5) + 1] = y0;
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); // selects buffer
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // writes buffer
return true;
}
GLuint Component::CreateTexture() const {
GLuint textureNum = 0;
glGenTextures(1, &textureNum);
return textureNum;
}
bool Component::setTexture(GLuint textureNum, unsigned int w, unsigned int h, const unsigned char *texture) const {
glBindTexture(GL_TEXTURE_2D, textureNum);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
glGenerateMipmap(GL_TEXTURE_2D);
return true;
}
// converts 0-1 to screen
// but centered
void Component::pointToViewport(float &rawX, float &rawY) const {
if (boundToPage) {
//std::cout << "Component::pointToViewport - notBoundToPage converting from " << static_cast<int>(rawX) << "," << static_cast<int>(rawY) << std::endl;
//std::cout << "BoundToPage using " << screenWidth << "x" << screenHeight << std::endl;
rawX = ((rawX / windowWidth) * 2) - 1;
rawY = ((rawY / windowHeight) * 2) - 1;
//std::cout << "Component::pointToViewport - BoundToPage using " << static_cast<int>(rawX) << "x" << static_cast<int>(rawY) << std::endl;
//std::cout << "Component::pointToViewport - BoundToPage converted to " << rawX << "," << rawY << std::endl;
} else {
//std::cout << "notBoundToPage using " << screenWidth << "x" << screenHeight << std::endl;
//std::cout << "Component::pointToViewport - notBoundToPage converting from " << static_cast<int>(rawX) << "," << static_cast<int>(rawY) << std::endl;
if (rawX < 0) {
rawX += windowWidth;
}
@ -265,6 +346,7 @@ void Component::pointToViewport(float &rawX, float &rawY) const { @@ -265,6 +346,7 @@ void Component::pointToViewport(float &rawX, float &rawY) const {
}
rawX = (rawX * 2) - 1;
rawY = (rawY * 2) - 1;
//std::cout << "Component::pointToViewport - notBoundToPage converted to " << rawX << "," << rawY << std::endl;
}
}

Loading…
Cancel
Save