Implement GlStack, GlFlex is now child of GlStack
This commit is contained in:
parent
e7753f244c
commit
896f98476e
12 changed files with 179 additions and 55 deletions
|
|
@ -1,5 +1,3 @@
|
|||
* stack component
|
||||
|
||||
* button
|
||||
|
||||
* layout (constraint)
|
||||
|
|
|
|||
|
|
@ -39,12 +39,14 @@ private:
|
|||
class GlComponent;
|
||||
class GlFlex;
|
||||
class GlLabel;
|
||||
class GlStack;
|
||||
class GlWindow: public GlContainer, public std::enable_shared_from_this<GlWindow>
|
||||
{
|
||||
friend class GlApplication;
|
||||
friend class GlComponent;
|
||||
friend class GlFlex;
|
||||
friend class GlLabel;
|
||||
friend class GlStack;
|
||||
|
||||
public:
|
||||
std::string title;
|
||||
|
|
@ -57,6 +59,7 @@ public:
|
|||
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);
|
||||
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:
|
||||
GlApplication* app;
|
||||
|
|
|
|||
|
|
@ -11,10 +11,12 @@
|
|||
namespace uui
|
||||
{
|
||||
class GlFlex;
|
||||
class GlStack;
|
||||
class GlComponent: public std::enable_shared_from_this<GlComponent>
|
||||
{
|
||||
friend class GlWindow;
|
||||
friend class GlFlex;
|
||||
friend class GlStack;
|
||||
|
||||
public:
|
||||
~GlComponent();
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@ public:
|
|||
enum Type
|
||||
{
|
||||
WINDOW,
|
||||
FLEX
|
||||
FLEX,
|
||||
STACK
|
||||
};
|
||||
std::vector<std::shared_ptr<GlComponent>> components;
|
||||
Type type;
|
||||
|
|
|
|||
|
|
@ -7,22 +7,15 @@
|
|||
#include "uui/opengl/component.hpp"
|
||||
#include "uui/opengl/container.hpp"
|
||||
#include "uui/opengl/label.hpp"
|
||||
#include "uui/opengl/stack.hpp"
|
||||
|
||||
namespace uui
|
||||
{
|
||||
class GlFlex: public GlComponent, public GlContainer
|
||||
class GlFlex: public GlStack
|
||||
{
|
||||
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;
|
||||
|
||||
GlFlex(
|
||||
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);
|
||||
|
|
@ -30,9 +23,6 @@ protected:
|
|||
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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -9,10 +9,12 @@
|
|||
namespace uui
|
||||
{
|
||||
class GlFlex;
|
||||
class GlStack;
|
||||
class GlLabel: public GlComponent
|
||||
{
|
||||
friend class GlWindow;
|
||||
friend class GlFlex;
|
||||
friend class GlStack;
|
||||
|
||||
size_t font_index;
|
||||
|
||||
|
|
|
|||
39
include/uui/opengl/stack.hpp
Normal file
39
include/uui/opengl/stack.hpp
Normal 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
|
||||
|
|
@ -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});
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
96
src/uui/opengl/stack.cpp
Normal 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};
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue