Implement GlStack, GlFlex is now child of GlStack

This commit is contained in:
Corentin 2021-07-22 17:47:03 +09:00
commit 896f98476e
12 changed files with 179 additions and 55 deletions

View file

@ -31,14 +31,30 @@ int main()
label->set_background_color(0.5f, 0.5f, 0.1f);
label = window->create_label(0.75f, 0.75f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.1f});
auto stack = window->create_flex(200, 0.6f, uui::Direction::HORIZONTAL);
auto flex = window->create_flex(200, 0.6f, uui::Direction::HORIZONTAL);
comp = flex->create_component(100, 50);
comp->set_background_color(0.2f, 0.75f, 0.25f);
flex->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f});
comp = flex->create_component(100, 20);
comp->set_background_color(0.2f, 0.25f, 0.75f);
flex = window->create_flex(50, 400, uui::Direction::VERTICAL);
comp = flex->create_component(50, 100);
comp->set_background_color(0.2f, 0.75f, 0.25f);
flex->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f});
comp = flex->create_component(100, 20);
comp->set_background_color(0.2f, 0.25f, 0.75f);
window = app.create_window(800, 600, "OpenGl 2");
auto stack = window->create_stack(200, 0.6f, uui::Direction::HORIZONTAL);
comp = stack->create_component(100, 50);
comp->set_background_color(0.2f, 0.75f, 0.25f);
stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f});
comp = stack->create_component(100, 20);
comp->set_background_color(0.2f, 0.25f, 0.75f);
stack = window->create_flex(50, 400, uui::Direction::VERTICAL);
stack = window->create_stack(50, 400, uui::Direction::VERTICAL);
comp = stack->create_component(50, 100);
comp->set_background_color(0.2f, 0.75f, 0.25f);
stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f});

View file

@ -148,7 +148,7 @@ void uui::GlComponent::render()
void uui::GlComponent::on_resize()
{
bool is_in_stack = parent->type == GlContainer::Type::FLEX;
bool is_in_stack = parent->type == GlContainer::Type::FLEX || parent->type == GlContainer::Type::STACK;
if(!coord_all_relative || is_in_stack)
{
if(is_in_stack)

View file

@ -7,8 +7,7 @@
uui::GlFlex::GlFlex(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
direction(direction)
GlStack(window, window_index, parent, parent_index, x, y, direction)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
@ -17,40 +16,6 @@ uui::GlFlex::GlFlex(
coord_all_relative = false;
}
std::shared_ptr<uui::GlComponent> uui::GlFlex::create_component(const position_t width, const position_t height)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlComponent> component(new uui::GlComponent(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, width,
height));
push_child(component);
component->init();
std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1);
window->components.push_back(component);
return component;
}
std::shared_ptr<uui::GlLabel> uui::GlFlex::create_label(const std::string& text, const std::tuple<float, float, float, float>& text_color)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, text,
text_color));
push_child(label);
label->init();
std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1);
window->components.push_back(label);
return label;
}
std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_index) const
{
const int initial_x =

96
src/uui/opengl/stack.cpp Normal file
View file

@ -0,0 +1,96 @@
#include "uui/opengl/stack.hpp"
#include <iostream>
#include "uui/opengl/gl_utils.hpp"
uui::GlStack::GlStack(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
direction(direction)
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl;
std::cout << "Stack:" << direction << std::endl;
type = Type::STACK;
coord_all_relative = false;
}
std::shared_ptr<uui::GlComponent> uui::GlStack::create_component(const position_t width, const position_t height)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlComponent> component(new uui::GlComponent(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, width,
height));
push_child(component);
component->init();
std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1);
window->components.push_back(component);
return component;
}
std::shared_ptr<uui::GlLabel> uui::GlStack::create_label(const std::string& text, const std::tuple<float, float, float, float>& text_color)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, text,
text_color));
push_child(label);
label->init();
std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1);
window->components.push_back(label);
return label;
}
std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_index) const
{
int x =
position_x.index() == 0 ? std::get<0>(position_x) : static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
int y =
position_y.index() == 0 ? std::get<0>(position_y) : static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
if(child_index == 0)
return {x, y};
if(child_index >= components.size())
throw std::runtime_error("GlStack error: child_index greater than children count");
if(direction == Direction::HORIZONTAL)
{
for(std::size_t index = 0; index < child_index; index += 1)
if(components[index] != nullptr)
{
auto& component = components[index];
if(component->size_width.index() == 1)
x += static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width));
else
x += std::get<0>(component->size_width);
}
}
else
{
for(std::size_t index = 0; index < child_index; index += 1)
{
if(components[index] != nullptr)
{
auto& component = components[index];
if(component->size_height.index() == 1)
y += static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height));
else
y += std::get<0>(component->size_height);
}
}
}
return {x, y};
}

View file

@ -275,12 +275,24 @@ std::shared_ptr<uui::GlLabel> uui::GlWindow::create_label(
}
std::shared_ptr<uui::GlFlex> uui::GlWindow::create_flex(const position_t x, const position_t y, const Direction direction)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create flex while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlFlex> flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction));
push_child(flex);
flex->init();
return flex;
}
std::shared_ptr<uui::GlStack> uui::GlWindow::create_stack(const position_t x, const position_t y, const Direction direction)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create stack while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlFlex> stack(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction));
std::shared_ptr<uui::GlStack> stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction));
push_child(stack);
stack->init();
return stack;