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

@ -1,5 +1,3 @@
* stack component
* button * button
* layout (constraint) * layout (constraint)

View file

@ -39,12 +39,14 @@ private:
class GlComponent; class GlComponent;
class GlFlex; class GlFlex;
class GlLabel; class GlLabel;
class GlStack;
class GlWindow: public GlContainer, public std::enable_shared_from_this<GlWindow> class GlWindow: public GlContainer, public std::enable_shared_from_this<GlWindow>
{ {
friend class GlApplication; friend class GlApplication;
friend class GlComponent; friend class GlComponent;
friend class GlFlex; friend class GlFlex;
friend class GlLabel; friend class GlLabel;
friend class GlStack;
public: public:
std::string title; std::string title;
@ -57,6 +59,7 @@ public:
std::shared_ptr<GlLabel> create_label( std::shared_ptr<GlLabel> create_label(
const position_t x, const position_t y, const std::string& text, const std::tuple<float, float, float, float>& text_color); const position_t x, const position_t y, const std::string& text, const std::tuple<float, float, float, float>& text_color);
std::shared_ptr<GlFlex> create_flex(const position_t x, const position_t y, const Direction direction); std::shared_ptr<GlFlex> create_flex(const position_t x, const position_t y, const Direction direction);
std::shared_ptr<GlStack> create_stack(const position_t x, const position_t y, const Direction direction);
protected: protected:
GlApplication* app; GlApplication* app;

View file

@ -11,10 +11,12 @@
namespace uui namespace uui
{ {
class GlFlex; class GlFlex;
class GlStack;
class GlComponent: public std::enable_shared_from_this<GlComponent> class GlComponent: public std::enable_shared_from_this<GlComponent>
{ {
friend class GlWindow; friend class GlWindow;
friend class GlFlex; friend class GlFlex;
friend class GlStack;
public: public:
~GlComponent(); ~GlComponent();

View file

@ -33,7 +33,8 @@ public:
enum Type enum Type
{ {
WINDOW, WINDOW,
FLEX FLEX,
STACK
}; };
std::vector<std::shared_ptr<GlComponent>> components; std::vector<std::shared_ptr<GlComponent>> components;
Type type; Type type;

View file

@ -7,22 +7,15 @@
#include "uui/opengl/component.hpp" #include "uui/opengl/component.hpp"
#include "uui/opengl/container.hpp" #include "uui/opengl/container.hpp"
#include "uui/opengl/label.hpp" #include "uui/opengl/label.hpp"
#include "uui/opengl/stack.hpp"
namespace uui namespace uui
{ {
class GlFlex: public GlComponent, public GlContainer class GlFlex: public GlStack
{ {
friend class GlWindow; friend class GlWindow;
public:
std::shared_ptr<GlComponent> create_component(const position_t width, const position_t height);
std::shared_ptr<GlLabel> create_label(const std::string& text, const std::tuple<float, float, float, float>& text_color);
protected: protected:
Direction direction;
int content_width;
int content_height;
GlFlex( GlFlex(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index, std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction); const position_t x, const position_t y, const Direction direction);
@ -30,9 +23,6 @@ protected:
GlFlex(window, window_index, window, window_index, x, y, direction) GlFlex(window, window_index, window, window_index, x, y, direction)
{} {}
float get_width() const;
float get_height() const;
std::tuple<int, int> get_child_position(const std::size_t child_index) const override; std::tuple<int, int> get_child_position(const std::size_t child_index) const override;
}; };

View file

@ -9,10 +9,12 @@
namespace uui namespace uui
{ {
class GlFlex; class GlFlex;
class GlStack;
class GlLabel: public GlComponent class GlLabel: public GlComponent
{ {
friend class GlWindow; friend class GlWindow;
friend class GlFlex; friend class GlFlex;
friend class GlStack;
size_t font_index; size_t font_index;

View file

@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <string>
#include <tuple>
#include "uui/opengl/component.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/label.hpp"
namespace uui
{
class GlStack: public GlComponent, public GlContainer
{
friend class GlWindow;
public:
std::shared_ptr<GlComponent> create_component(const position_t width, const position_t height);
std::shared_ptr<GlLabel> create_label(const std::string& text, const std::tuple<float, float, float, float>& text_color);
protected:
Direction direction;
int content_width;
int content_height;
GlStack(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction);
GlStack(std::shared_ptr<GlWindow> window, std::size_t window_index, const position_t x, const position_t y, const Direction direction):
GlStack(window, window_index, window, window_index, x, y, direction)
{}
float get_width() const;
float get_height() const;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index) const;
};
} // namespace uui

View file

@ -31,14 +31,30 @@ int main()
label->set_background_color(0.5f, 0.5f, 0.1f); 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}); 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 = stack->create_component(100, 50);
comp->set_background_color(0.2f, 0.75f, 0.25f); comp->set_background_color(0.2f, 0.75f, 0.25f);
stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f});
comp = stack->create_component(100, 20); comp = stack->create_component(100, 20);
comp->set_background_color(0.2f, 0.25f, 0.75f); 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 = stack->create_component(50, 100);
comp->set_background_color(0.2f, 0.75f, 0.25f); comp->set_background_color(0.2f, 0.75f, 0.25f);
stack->create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}); 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() 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(!coord_all_relative || is_in_stack)
{ {
if(is_in_stack) if(is_in_stack)

View file

@ -7,8 +7,7 @@
uui::GlFlex::GlFlex( 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, 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): const position_t x, const position_t y, const Direction direction):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f), GlStack(window, window_index, parent, parent_index, x, y, direction)
direction(direction)
{ {
if constexpr(uui::GlApplication::debug_mode) if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlStack" << std::endl; std::cout << "Creating a GlStack" << std::endl;
@ -17,40 +16,6 @@ uui::GlFlex::GlFlex(
coord_all_relative = false; 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 std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_index) const
{ {
const int initial_x = 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) 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) if(!initialized)
throw std::runtime_error("GlWindow error : cannot create stack while not initialized"); throw std::runtime_error("GlWindow error : cannot create stack while not initialized");
glfwMakeContextCurrent(glfw_window); 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); push_child(stack);
stack->init(); stack->init();
return stack; return stack;