Improve class structure

This commit is contained in:
Corentin 2022-10-11 17:22:20 +09:00
commit 3ad40381e5
25 changed files with 271 additions and 216 deletions

View file

@ -14,22 +14,30 @@ class Component
public:
virtual void set_background_color(float r, float g, float b, float a = 1.0f) = 0;
ComponentType type() { return _type; }
uui::position_t x() { return _position_x; }
uui::position_t y() { return _position_y; }
uui::size_t width() { return _width; }
uui::size_t height() { return _height; }
protected:
std::shared_ptr<Window> window;
std::size_t window_index;
std::shared_ptr<Container> parent;
std::size_t parent_index;
ComponentType _type = ComponentType::UNASSIGNED;
uui::position_t position_x;
uui::position_t position_y;
uui::size_t size_width;
uui::size_t size_height;
std::shared_ptr<Window> _window;
std::size_t _window_index;
std::shared_ptr<Container> _parent;
std::size_t _parent_index;
bool coord_all_relative;
uui::position_t _position_x;
uui::position_t _position_y;
uui::size_t _width;
uui::size_t _height;
std::array<float, 4> background_color;
bool _coord_all_relative;
bool initialized;
std::array<float, 4> _background_color;
bool _initialized = false;
virtual void render() = 0;
};

View file

@ -1,36 +1,12 @@
#pragma once
#include <cstdlib>
#include <iostream>
#include <memory>
#include <vector>
#include "uui/component.hpp"
#include "uui/container_interface.hpp"
#include "uui/types.hpp"
namespace uui
{
class Component;
class GlComponent;
class Container
{
friend class GlComponent;
public:
enum Type
{
WINDOW,
FLEX,
STACK
};
Type type;
protected:
virtual std::size_t push_child(std::shared_ptr<Component> child) = 0;
virtual std::shared_ptr<Component> get_child(std::size_t index) const = 0;
virtual void remove_child(std::size_t index) = 0;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const = 0;
};
class Container: virtual public Component, virtual public ContainerInterface
{};
} // namespace uui

View file

@ -0,0 +1,21 @@
#pragma once
#include <memory>
#include <tuple>
#include "uui/types.hpp"
namespace uui
{
class Component;
class ContainerInterface
{
public:
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const = 0;
protected:
virtual std::size_t push_child(std::shared_ptr<Component> child) = 0;
virtual std::shared_ptr<Component> get_child(std::size_t index) const = 0;
virtual void remove_child(std::size_t index) = 0;
};
} // namespace uui

View file

@ -51,6 +51,6 @@ protected:
bool initialized;
FT_Library ft;
virtual const Font& init_font(const std::string& font_path, std::size_t font_size) = 0;
virtual const Font& init_font(const std::string& font_path, unsigned int font_size) = 0;
};
} // namespace uui

View file

@ -20,3 +20,4 @@ protected:
};
} // namespace uui

View file

@ -3,19 +3,18 @@
#include <array>
#include <memory>
#include "graphic_context.hpp"
#include "uui/container.hpp"
#include "uui/opengl/component.hpp"
#include "uui/component.hpp"
#include "uui/opengl/graphic_context.hpp"
#include "uui/opengl/window.hpp"
#include "uui/types.hpp"
namespace uui
{
class GlContainer;
class GlFlex;
class GlStack;
class GlComponent: public std::enable_shared_from_this<GlComponent>, virtual public Component
{
friend class Container;
friend class GlContainer;
friend class GlWindow;
friend class GlFlex;
@ -36,7 +35,7 @@ protected:
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const ComponentParams& params);
GlComponent(std::shared_ptr<GlWindow> window, std::size_t window_index, const ComponentParams& params):
GlComponent(window, window_index, std::static_pointer_cast<GlContainer>(window), window_index, params)
GlComponent(window, window_index, nullptr, 0, params)
{}
GlComponent(const GlComponent&) = delete;
GlComponent(GlComponent&&) = default;

View file

@ -6,28 +6,13 @@
#include <memory>
#include <vector>
#include "uui/component.hpp"
#include "uui/container.hpp"
#include "uui/opengl/component.hpp"
#include "uui/opengl/container_interface.hpp"
namespace uui
{
class GlContainer: virtual public Container
{
protected:
GlContainer() = default;
GlContainer(const GlContainer&) = default;
GlContainer(GlContainer&&) = default;
virtual std::size_t push_child(std::shared_ptr<Component> child [[maybe_unused]]) final;
std::size_t push_child(std::shared_ptr<GlComponent> child);
std::shared_ptr<Component> get_child(std::size_t index) const final;
virtual void remove_child(std::size_t index) final;
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const override;
std::vector<std::shared_ptr<GlComponent>> components;
public:
~GlContainer();
};
class GlContainer: virtual public Container, virtual public GlContainerInterface
{};
} // namespace uui

View file

@ -0,0 +1,28 @@
#pragma once
#include <memory>
#include <tuple>
#include "uui/component.hpp"
#include "uui/container_interface.hpp"
#include "uui/types.hpp"
namespace uui
{
class GlComponent;
class GlContainerInterface: virtual public ContainerInterface
{
public:
virtual std::tuple<int, int> get_child_position(const std::size_t child_index [[maybe_unused]]) const override;
protected:
virtual std::size_t push_child(std::shared_ptr<Component> child) final;
std::size_t push_child(std::shared_ptr<GlComponent> child);
virtual std::shared_ptr<Component> get_child(std::size_t index) const final;
virtual void remove_child(std::size_t index) final;
virtual ~GlContainerInterface();
std::vector<std::shared_ptr<GlComponent>> components;
};
} // namespace uui

View file

@ -20,7 +20,7 @@ protected:
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const StackParams& params);
GlFlex(std::shared_ptr<GlWindow> window, std::size_t window_index, const StackParams& params):
GlFlex(window, window_index, window, window_index, params)
GlFlex(window, window_index, nullptr, 0, params)
{}
std::tuple<int, int> get_child_position(const std::size_t child_index) const override;

View file

@ -5,9 +5,6 @@
#include <tuple>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H
#include "uui/font_manager.hpp"
#include "uui/opengl/graphic_context.hpp"
@ -34,6 +31,6 @@ private:
void init();
void deinit();
const Font& init_font(const std::string& font_path, std::size_t font_size);
const Font& init_font(const std::string& font_path, unsigned int font_size);
};
} // namespace uui

View file

@ -12,7 +12,7 @@
#include <glm/gtc/type_ptr.hpp>
// clang-format off
namespace Config
namespace GlConfig
{
constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0;
}

View file

@ -6,6 +6,7 @@
#include "uui/label.hpp"
#include "uui/opengl/component.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/window.hpp"
namespace uui
{
@ -28,7 +29,7 @@ protected:
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const LabelParams& params);
GlLabel(std::shared_ptr<GlWindow> window, std::size_t window_index, const LabelParams& params):
GlLabel(window, window_index, window, window_index, params)
GlLabel(window, window_index, nullptr, 0, params)
{}
void render() final;

View file

@ -23,7 +23,7 @@ protected:
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const StackParams& params);
GlStack(std::shared_ptr<GlWindow> window, std::size_t window_index, const StackParams& params):
GlStack(window, window_index, window, window_index, params)
GlStack(window, window_index, nullptr, 0, params)
{}
std::tuple<int, int> get_child_position(const std::size_t child_index) const override;

View file

@ -1,7 +1,7 @@
#pragma once
#include "uui/opengl/application.hpp"
#include "uui/opengl/container.hpp"
#include "uui/opengl/container_interface.hpp"
#include "uui/opengl/font_manager.hpp"
#include "uui/types.hpp"
#include "uui/window.hpp"
@ -13,7 +13,10 @@ class GlContainer;
class GlFlex;
class GlLabel;
class GlStack;
class GlWindow final: public std::enable_shared_from_this<GlWindow>, virtual public Window, virtual public GlContainer
class GlWindow:
public std::enable_shared_from_this<GlWindow>,
virtual public Window,
virtual public GlContainerInterface
{
friend class GlApplication;
friend class GlComponent;
@ -48,5 +51,6 @@ protected:
static void glfw_resize_callback(GLFWwindow* window, int width, int height);
static void glfw_iconify_callback(GLFWwindow* window, int iconified);
static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
// static void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
};
} // namespace uui

View file

@ -6,12 +6,28 @@
namespace uui
{
namespace Config
{
constexpr int MOUSE_GRID_WIDTH = 20;
constexpr int MOUSE_GRID_HEIGHT = 20;
}; // namespace Config
template<typename T> using InitFunction = const std::function<void(T&)>&;
typedef std::variant<int, float> position_t;
typedef std::variant<int, float> size_t;
typedef std::tuple<float, float, float, float> color_t;
enum Direction
enum class ComponentType
{
UNASSIGNED,
COMPONENT,
LABEL,
FLEX,
STACK,
WINDOW
};
enum class Direction
{
HORIZONTAL,
VERTICAL

View file

@ -1,10 +1,11 @@
#pragma once
#include <array>
#include <memory>
#include <string>
#include "uui/application.hpp"
#include "uui/container.hpp"
#include "uui/container_interface.hpp"
#include "uui/font_manager.hpp"
#include "uui/types.hpp"
@ -14,13 +15,11 @@ class Component;
class Label;
class Flex;
class Stack;
class Window: virtual public Container
class Window: virtual public ContainerInterface
{
friend class Component;
public:
std::string title;
std::shared_ptr<FontManager> font_manager;
virtual void create_component(const ComponentParams& params, InitFunction<Component> init) = 0;
@ -28,19 +27,26 @@ public:
virtual void create_flex(const StackParams& params, InitFunction<Flex> init) = 0;
virtual void create_stack(const StackParams& params, InitFunction<Stack> init) = 0;
const std::string& title() { return _title; }
int width() { return _width; };
int height() { return _height; };
protected:
ComponentType _type = ComponentType::UNASSIGNED;
std::shared_ptr<Application> _app;
bool _initialized;
std::string _title;
int _width;
int _height;
bool _render_needed;
bool _resize_needed;
bool _iconified;
std::array<std::array<std::vector<std::shared_ptr<Component>>, Config::MOUSE_GRID_HEIGHT>, Config::MOUSE_GRID_WIDTH>
_grid_click;
virtual void render() = 0;
};
} // namespace uui

View file

@ -94,7 +94,7 @@ void uui::GlApplication::run()
glfwSwapInterval(1);
}
if constexpr(debug_mode)
std::cout << "\nGlApplication closing GlWindow : " << window->title << std::endl;
std::cout << "\nGlApplication closing GlWindow : " << window->title() << std::endl;
window->deinit();
windows.erase(it);
it -= 1;

View file

@ -13,44 +13,44 @@ uui::GlComponent::GlComponent(
if constexpr(uui::Application::debug_mode)
std::cout << "Creating a GlComponent " << parent_index << std::endl;
_type = ComponentType::COMPONENT;
gl_window = window;
this->window = std::static_pointer_cast<uui::Window>(window);
this->_window = std::static_pointer_cast<uui::Window>(window);
this->_window_index = window_index;
this->_parent = std::static_pointer_cast<uui::Container>(parent);
this->_parent_index = parent_index;
this->window_index = window_index;
this->parent = parent;
this->parent_index = parent_index;
this->parent_index = parent_index;
_initialized = false;
initialized = false;
_position_x = params.x;
_position_y = params.y;
_width = params.width;
_height = params.height;
position_x = params.x;
position_y = params.y;
size_width = params.width;
size_height = params.height;
coord_all_relative = true;
_coord_all_relative = true;
auto parse_coords = [&](position_t& value) {
if(value.index() == 0)
{
if(std::get<0>(value) == 0)
value = 0.0f;
else
coord_all_relative = false;
_coord_all_relative = false;
}
};
parse_coords(position_x);
parse_coords(position_y);
parse_coords(size_width);
parse_coords(size_height);
parse_coords(_position_x);
parse_coords(_position_y);
parse_coords(_width);
parse_coords(_height);
background_color = {0.0f, 0.0f, 0.0f, 0.0f};
_background_color = {0.0f, 0.0f, 0.0f, 0.0f};
}
uui::GlComponent::~GlComponent()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Destructor GlComponent " << parent_index << std::endl;
if(initialized)
std::cout << "Destructor GlComponent " << _parent_index << std::endl;
if(_initialized)
deinit();
// if(parent != window)
// parent->remove_child(0); // TODO
@ -58,7 +58,7 @@ uui::GlComponent::~GlComponent()
void uui::GlComponent::set_background_color(float r, float g, float b, float a /*=0.0f*/)
{
background_color = {r, g, b, a};
_background_color = {r, g, b, a};
glBindVertexArray(gl_vao);
set_vbo_data();
glBindVertexArray(0); // Optional : for safety
@ -66,7 +66,7 @@ void uui::GlComponent::set_background_color(float r, float g, float b, float a /
void uui::GlComponent::init()
{
if(initialized)
if(_initialized)
throw std::runtime_error("GlComponent error : calling init while already initialized");
glGenVertexArrays(1, &gl_vao);
@ -94,23 +94,22 @@ void uui::GlComponent::init()
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
initialized = true;
_initialized = true;
}
void uui::GlComponent::set_vbo_data()
{
float vert_x = position_x.index() == 1
? std::get<1>(position_x)
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width());
float vert_y = position_y.index() == 1
? std::get<1>(position_y)
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height());
float vert_w = size_width.index() == 1
? std::get<1>(size_width)
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width());
float vert_h = size_height.index() == 1
? std::get<1>(size_height)
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height());
float vert_x = _position_x.index() == 1
? std::get<1>(_position_x)
: static_cast<float>(std::get<0>(_position_x)) / static_cast<float>(_window->width());
float vert_y = _position_y.index() == 1
? std::get<1>(_position_y)
: static_cast<float>(std::get<0>(_position_y)) / static_cast<float>(_window->height());
float vert_w = _width.index() == 1 ? std::get<1>(_width)
: static_cast<float>(std::get<0>(_width)) / static_cast<float>(_window->width());
float vert_h = _height.index() == 1
? std::get<1>(_height)
: static_cast<float>(std::get<0>(_height)) / static_cast<float>(_window->height());
float x_min = (2.0f * vert_x) - 1.0f;
float x_max = x_min + (2.0f * vert_w);
float y_min = (-2.0f * vert_y) + 1.0f;
@ -118,10 +117,10 @@ void uui::GlComponent::set_vbo_data()
// clang-format off
float vertices[] =
{
x_max, y_min, background_color[0], background_color[1], background_color[2], background_color[3], // top right
x_max, y_max, background_color[0], background_color[1], background_color[2], background_color[3], // bottom right
x_min, y_max, background_color[0], background_color[1], background_color[2], background_color[3], // bottom left
x_min, y_min, background_color[0], background_color[1], background_color[2], background_color[3] // top left
x_max, y_min, _background_color[0], _background_color[1], _background_color[2], _background_color[3], // top right
x_max, y_max, _background_color[0], _background_color[1], _background_color[2], _background_color[3], // bottom right
x_min, y_max, _background_color[0], _background_color[1], _background_color[2], _background_color[3], // bottom left
x_min, y_min, _background_color[0], _background_color[1], _background_color[2], _background_color[3] // top left
};
// clang-format on
glBindBuffer(GL_ARRAY_BUFFER, gl_vbo);
@ -150,18 +149,18 @@ void uui::GlComponent::set_vbo_data()
void uui::GlComponent::deinit()
{
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlComponent " << parent_index << std::endl;
std::cout << "Deinit GlComponent " << _parent_index << std::endl;
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlComponent error : calling deinit while not initialized");
glDeleteVertexArrays(1, &gl_vao);
glDeleteBuffers(1, &gl_vbo);
glDeleteBuffers(1, &gl_ebo);
window = nullptr;
parent = nullptr;
initialized = false;
_window = nullptr;
_parent = nullptr;
_initialized = false;
}
void uui::GlComponent::render()
@ -172,11 +171,12 @@ void uui::GlComponent::render()
void uui::GlComponent::on_resize()
{
bool is_in_stack = parent->type == GlContainer::Type::FLEX || parent->type == GlContainer::Type::STACK;
if(!coord_all_relative || is_in_stack)
bool is_in_stack =
_parent != nullptr && (_parent->type() == ComponentType::FLEX || _parent->type() == ComponentType::STACK);
if(!_coord_all_relative || is_in_stack)
{
if(is_in_stack)
std::tie(position_x, position_y) = parent->get_child_position(parent_index);
std::tie(_position_x, _position_y) = _parent->get_child_position(_parent_index);
glBindVertexArray(gl_vao);
set_vbo_data();
glBindVertexArray(0); // Optional : for safety

View file

@ -1,37 +0,0 @@
#include "uui/opengl/container.hpp"
#include "uui/opengl/component.hpp"
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::Component> child [[maybe_unused]])
{
throw std::runtime_error("GlContainer error : should only push GlComponent child, not Component");
}
std::size_t uui::GlContainer::push_child(std::shared_ptr<uui::GlComponent> child)
{
components.push_back(child);
// components.push_back(std::static_pointer_cast<uui::Component>(child));
return components.size() - 1;
}
std::shared_ptr<uui::Component> uui::GlContainer::get_child(std::size_t index) const
{
return components.at(index);
}
void uui::GlContainer::remove_child(std::size_t index)
{
components.at(index) = nullptr;
};
std::tuple<int, int> uui::GlContainer::get_child_position(const std::size_t child_index [[maybe_unused]]) const
{
return {0, 0};
}
uui::GlContainer::~GlContainer()
{
if constexpr(uui::Application::debug_mode)
std::cout << "Destroying GlContainer" << std::endl;
components.clear();
}

View file

@ -0,0 +1,38 @@
#include "uui/opengl/container_interface.hpp"
#include <iostream>
#include "uui/opengl/component.hpp"
std::size_t uui::GlContainerInterface::push_child(std::shared_ptr<uui::Component> child [[maybe_unused]])
{
throw std::runtime_error("GlContainerInterface error : should only push GlComponent child, not Component");
}
std::size_t uui::GlContainerInterface::push_child(std::shared_ptr<uui::GlComponent> child)
{
components.push_back(child);
return components.size() - 1;
}
std::shared_ptr<uui::Component> uui::GlContainerInterface::get_child(std::size_t index) const
{
return components.at(index);
}
void uui::GlContainerInterface::remove_child(std::size_t index)
{
components.at(index) = nullptr;
};
std::tuple<int, int> uui::GlContainerInterface::get_child_position(const std::size_t child_index [[maybe_unused]]) const
{
return {0, 0};
}
uui::GlContainerInterface::~GlContainerInterface()
{
if constexpr(uui::Application::debug_mode)
std::cout << "Destroying GlContainerInterface" << std::endl;
components.clear();
}

View file

@ -13,18 +13,19 @@ uui::GlFlex::GlFlex(
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating a GlFlex " << parent_index << std::endl;
type = Type::FLEX;
coord_all_relative = false;
_type = ComponentType::FLEX;
_coord_all_relative = false;
}
std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_index) const
{
const int initial_x = position_x.index() == 0
? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width()));
const int initial_y = position_y.index() == 0
? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height()));
const int initial_x = _position_x.index() == 0
? std::get<0>(_position_x)
: static_cast<int>(std::get<1>(_position_x) * static_cast<float>(_window->width()));
const int initial_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 {initial_x, initial_y};
@ -37,14 +38,14 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[index] != nullptr)
{
auto& component = components[index];
if(component->size_width.index() == 1)
width = static_cast<int>(std::get<1>(component->size_width) * static_cast<float>(window->width()));
if(component->width().index() == 1)
width = static_cast<int>(std::get<1>(component->width()) * static_cast<float>(_window->width()));
else
width = std::get<0>(component->size_width);
if(component->size_height.index() == 1)
height = static_cast<int>(std::get<1>(component->size_height) * static_cast<float>(window->height()));
width = std::get<0>(component->width());
if(component->height().index() == 1)
height = static_cast<int>(std::get<1>(component->height()) * static_cast<float>(_window->height()));
else
height = std::get<0>(component->size_height);
height = std::get<0>(component->height());
}
return {width, height};
};
@ -65,7 +66,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_height > max_height)
max_height = child_height;
if(x != initial_x && x + child_width > window->width())
if(x != initial_x && x + child_width > _window->width())
{
x = initial_x + child_width;
y += max_height;
@ -77,7 +78,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(x != initial_x && x + child_width > window->width())
if(x != initial_x && x + child_width > _window->width())
{
x = initial_x;
y += max_height;
@ -99,7 +100,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
std::tie(child_width, child_height) = get_child_size(index);
if(child_width > max_width)
max_width = child_width;
if(y != initial_y && y + child_height > window->height())
if(y != initial_y && y + child_height > _window->height())
{
y = initial_y + child_height;
x += max_width;
@ -111,7 +112,7 @@ std::tuple<int, int> uui::GlFlex::get_child_position(const std::size_t child_ind
if(components[child_index] != nullptr)
{
std::tie(child_width, child_height) = get_child_size(child_index);
if(y != initial_y && y + child_height > window->height())
if(y != initial_y && y + child_height > _window->height())
{
y = initial_y;
x += max_width;

View file

@ -131,7 +131,7 @@ void uui::GlFontManager::deinit()
initialized = false;
}
const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, std::size_t font_size)
const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, unsigned int font_size)
{
fonts.emplace_back();
auto& font = fonts.back();
@ -173,7 +173,7 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string&
glLinkProgram(gl_program);
// Create a texture that will be used to hold all ASCII glyphs
glActiveTexture(Config::FONT_TEXTURE_UNIT);
glActiveTexture(GlConfig::FONT_TEXTURE_UNIT);
glGenTextures(1, &font.atlas_texture);
glBindTexture(GL_TEXTURE_2D, font.atlas_texture);
@ -257,7 +257,7 @@ void uui::GlFontManager::render_text(
glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)(&gl_prev_program));
glUseProgram(gl_program);
glActiveTexture(Config::FONT_TEXTURE_UNIT);
glActiveTexture(GlConfig::FONT_TEXTURE_UNIT);
auto [color_r, color_g, color_b, color_a] = text_color;
glUniform4f(glGetUniformLocation(gl_program, "textColor"), color_r, color_g, color_b, color_a);
glBindVertexArray(gl_vao);

View file

@ -14,25 +14,27 @@ uui::GlLabel::GlLabel(
if constexpr(uui::Application::debug_mode)
std::cout << "Creating GlLabel " << parent_index << std::endl;
_type = ComponentType::LABEL;
font_index = 0;
std::tie(text_width, text_height) =
window->font_manager->get_text_size(text, window->font_manager->fonts[font_index], 1.0f, 1.0f);
size_width = text_width;
size_height = text_height;
coord_all_relative = false;
_width = text_width;
_height = text_height;
_coord_all_relative = false;
}
void uui::GlLabel::render()
{
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width())
: static_cast<float>(std::get<0>(position_x));
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height())
: static_cast<float>(std::get<0>(position_y));
float text_x = _position_x.index() == 1 ? std::get<1>(_position_x) * static_cast<float>(_window->width())
: static_cast<float>(std::get<0>(_position_x));
float text_y = _position_y.index() == 1 ? std::get<1>(_position_y) * static_cast<float>(_window->height())
: static_cast<float>(std::get<0>(_position_y));
text_y += static_cast<float>(text_height) - 1;
glBindVertexArray(gl_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (void*)0);
window->font_manager->render_text(
text.c_str(), window->font_manager->fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
_window->font_manager->render_text(
text.c_str(), _window->font_manager->fonts.front(), text_x, text_y, 1.0f, 1.0f, text_color);
glBindVertexArray(0);
}

View file

@ -13,15 +13,16 @@ uui::GlStack::GlStack(
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Creating GlStack " << parent_index << std::endl;
_type = ComponentType::STACK;
direction = params.direction;
type = Type::STACK;
coord_all_relative = false;
_coord_all_relative = false;
}
void uui::GlStack::create_component(const StackComponentParams& params, uui::InitFunction<uui::Component> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(gl_window->glfw_window);
@ -30,7 +31,7 @@ void uui::GlStack::create_component(const StackComponentParams& params, uui::Ini
components.size(), {0, 0, params.width, params.height}));
push_child(component);
component->init();
std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1);
std::tie(component->_position_x, component->_position_y) = get_child_position(components.size() - 1);
gl_window->components.push_back(component);
if(component != nullptr)
@ -39,7 +40,7 @@ void uui::GlStack::create_component(const StackComponentParams& params, uui::Ini
void uui::GlStack::create_label(const StackLabelParams& params, uui::InitFunction<uui::Label> init)
{
if(!initialized)
if(!_initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(gl_window->glfw_window);
@ -48,7 +49,7 @@ void uui::GlStack::create_label(const StackLabelParams& params, uui::InitFunctio
components.size(), {0, 0, params.text, params.text_color}));
push_child(std::static_pointer_cast<uui::GlComponent>(label));
label->init();
std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1);
std::tie(label->_position_x, label->_position_y) = get_child_position(components.size() - 1);
gl_window->components.push_back(label);
if(label != nullptr && init != nullptr)
@ -57,10 +58,10 @@ void uui::GlStack::create_label(const StackLabelParams& params, uui::InitFunctio
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()));
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};
@ -73,10 +74,10 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
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()));
if(component->width().index() == 1)
x += static_cast<int>(std::get<1>(component->width()) * static_cast<float>(_window->width()));
else
x += std::get<0>(component->size_width);
x += std::get<0>(component->width());
}
}
else
@ -86,10 +87,10 @@ std::tuple<int, int> uui::GlStack::get_child_position(const std::size_t child_in
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()));
if(component->height().index() == 1)
y += static_cast<int>(std::get<1>(component->height()) * static_cast<float>(_window->height()));
else
y += std::get<0>(component->size_height);
y += std::get<0>(component->height());
}
}
}

View file

@ -86,10 +86,20 @@ void uui::GlWindow::glfw_key_callback(
glfwSetWindowShouldClose(window, true);
}
// void uui::GlWindow::glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
// {
// if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
// {}
// }
uui::GlWindow::GlWindow(std::shared_ptr<uui::Application> app, int width, int height, const std::string& title)
{
_type = ComponentType::WINDOW;
_app = app;
_initialized = false;
_title = title;
_width = width;
_height = height;
_render_needed = true;
@ -98,18 +108,16 @@ uui::GlWindow::GlWindow(std::shared_ptr<uui::Application> app, int width, int he
if constexpr(uui::Application::debug_mode)
std::cout << "Creating GlWindow : " << title << std::endl;
this->title = title;
type = Type::WINDOW;
}
uui::GlWindow::~GlWindow()
{
if constexpr(uui::Application::debug_mode)
std::cout << "Destructor GlWindow : " << title << std::endl;
std::cout << "Destructor GlWindow : " << _title << std::endl;
if(_initialized)
{
if constexpr(uui::Application::debug_mode)
std::cout << "Destructor-deinit GlWindow : " << title << std::endl;
std::cout << "Destructor-deinit GlWindow : " << _title << std::endl;
deinit();
}
}
@ -122,7 +130,7 @@ void uui::GlWindow::init()
gl_font_manager = std::make_shared<uui::GlFontManager>();
font_manager = std::static_pointer_cast<uui::FontManager>(gl_font_manager);
glfw_window = glfwCreateWindow(_width, _height, title.c_str(), NULL, NULL);
glfw_window = glfwCreateWindow(_width, _height, _title.c_str(), NULL, NULL);
if(glfw_window == NULL)
throw std::runtime_error("GLWindow : failed to create GLFW window");
glfwMakeContextCurrent(glfw_window);
@ -234,7 +242,7 @@ void uui::GlWindow::deinit()
throw std::runtime_error("GlWindow error : calling deinit while not initialized");
if constexpr(uui::GlApplication::debug_mode)
std::cout << "Deinit GlWindow : " << title << std::endl;
std::cout << "Deinit GlWindow : " << _title << std::endl;
glfwMakeContextCurrent(glfw_window);
glDeleteProgram(gl_program);