diff --git a/include/uui/component.hpp b/include/uui/component.hpp index a9ad0bb..ef91c34 100644 --- a/include/uui/component.hpp +++ b/include/uui/component.hpp @@ -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; - std::size_t window_index; - std::shared_ptr 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; + std::size_t _window_index; + std::shared_ptr _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 background_color; + bool _coord_all_relative; - bool initialized; + std::array _background_color; + + bool _initialized = false; virtual void render() = 0; }; diff --git a/include/uui/container.hpp b/include/uui/container.hpp index 43d1b24..782ff97 100644 --- a/include/uui/container.hpp +++ b/include/uui/container.hpp @@ -1,36 +1,12 @@ #pragma once -#include - -#include -#include -#include - +#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 child) = 0; - virtual std::shared_ptr get_child(std::size_t index) const = 0; - virtual void remove_child(std::size_t index) = 0; - virtual std::tuple get_child_position(const std::size_t child_index [[maybe_unused]]) const = 0; -}; +class Container: virtual public Component, virtual public ContainerInterface +{}; } // namespace uui diff --git a/include/uui/container_interface.hpp b/include/uui/container_interface.hpp new file mode 100644 index 0000000..287e918 --- /dev/null +++ b/include/uui/container_interface.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +#include "uui/types.hpp" + +namespace uui +{ +class Component; +class ContainerInterface +{ +public: + virtual std::tuple get_child_position(const std::size_t child_index [[maybe_unused]]) const = 0; +protected: + virtual std::size_t push_child(std::shared_ptr child) = 0; + virtual std::shared_ptr get_child(std::size_t index) const = 0; + virtual void remove_child(std::size_t index) = 0; +}; + +} // namespace uui diff --git a/include/uui/font_manager.hpp b/include/uui/font_manager.hpp index e7126d6..52da172 100644 --- a/include/uui/font_manager.hpp +++ b/include/uui/font_manager.hpp @@ -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 diff --git a/include/uui/label.hpp b/include/uui/label.hpp index 2de7c22..6b4f42a 100644 --- a/include/uui/label.hpp +++ b/include/uui/label.hpp @@ -20,3 +20,4 @@ protected: }; } // namespace uui + diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index 441e322..6ecaa99 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -3,19 +3,18 @@ #include #include -#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, virtual public Component { - friend class Container; friend class GlContainer; friend class GlWindow; friend class GlFlex; @@ -36,7 +35,7 @@ protected: std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, const ComponentParams& params); GlComponent(std::shared_ptr window, std::size_t window_index, const ComponentParams& params): - GlComponent(window, window_index, std::static_pointer_cast(window), window_index, params) + GlComponent(window, window_index, nullptr, 0, params) {} GlComponent(const GlComponent&) = delete; GlComponent(GlComponent&&) = default; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index ace8ae1..8deaf6d 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -6,28 +6,13 @@ #include #include -#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 child [[maybe_unused]]) final; - std::size_t push_child(std::shared_ptr child); - std::shared_ptr get_child(std::size_t index) const final; - virtual void remove_child(std::size_t index) final; - virtual std::tuple get_child_position(const std::size_t child_index [[maybe_unused]]) const override; - - std::vector> components; - -public: - ~GlContainer(); -}; +class GlContainer: virtual public Container, virtual public GlContainerInterface +{}; } // namespace uui diff --git a/include/uui/opengl/container_interface.hpp b/include/uui/opengl/container_interface.hpp new file mode 100644 index 0000000..e068ffa --- /dev/null +++ b/include/uui/opengl/container_interface.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include + +#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 get_child_position(const std::size_t child_index [[maybe_unused]]) const override; + +protected: + virtual std::size_t push_child(std::shared_ptr child) final; + std::size_t push_child(std::shared_ptr child); + virtual std::shared_ptr get_child(std::size_t index) const final; + virtual void remove_child(std::size_t index) final; + + virtual ~GlContainerInterface(); + + std::vector> components; +}; +} // namespace uui diff --git a/include/uui/opengl/flex.hpp b/include/uui/opengl/flex.hpp index e0bcdc5..975d30c 100644 --- a/include/uui/opengl/flex.hpp +++ b/include/uui/opengl/flex.hpp @@ -20,7 +20,7 @@ protected: std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, const StackParams& params); GlFlex(std::shared_ptr 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 get_child_position(const std::size_t child_index) const override; diff --git a/include/uui/opengl/font_manager.hpp b/include/uui/opengl/font_manager.hpp index 19d1ab7..e797f1b 100644 --- a/include/uui/opengl/font_manager.hpp +++ b/include/uui/opengl/font_manager.hpp @@ -5,9 +5,6 @@ #include #include -#include -#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 diff --git a/include/uui/opengl/graphic_context.hpp b/include/uui/opengl/graphic_context.hpp index 5837abc..62635a7 100644 --- a/include/uui/opengl/graphic_context.hpp +++ b/include/uui/opengl/graphic_context.hpp @@ -12,7 +12,7 @@ #include // clang-format off -namespace Config +namespace GlConfig { constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; } diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index 4570717..266b615 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -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 window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, const LabelParams& params); GlLabel(std::shared_ptr 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; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index b625314..52310c4 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -23,7 +23,7 @@ protected: std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, const StackParams& params); GlStack(std::shared_ptr 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 get_child_position(const std::size_t child_index) const override; diff --git a/include/uui/opengl/window.hpp b/include/uui/opengl/window.hpp index 67543da..6a5ab32 100644 --- a/include/uui/opengl/window.hpp +++ b/include/uui/opengl/window.hpp @@ -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, virtual public Window, virtual public GlContainer +class GlWindow: + public std::enable_shared_from_this, + 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 diff --git a/include/uui/types.hpp b/include/uui/types.hpp index fae7b2f..d329768 100644 --- a/include/uui/types.hpp +++ b/include/uui/types.hpp @@ -6,12 +6,28 @@ namespace uui { +namespace Config +{ +constexpr int MOUSE_GRID_WIDTH = 20; +constexpr int MOUSE_GRID_HEIGHT = 20; +}; // namespace Config + template using InitFunction = const std::function&; typedef std::variant position_t; typedef std::variant size_t; typedef std::tuple color_t; -enum Direction +enum class ComponentType +{ + UNASSIGNED, + COMPONENT, + LABEL, + FLEX, + STACK, + WINDOW +}; + +enum class Direction { HORIZONTAL, VERTICAL diff --git a/include/uui/window.hpp b/include/uui/window.hpp index 936b273..16abe65 100644 --- a/include/uui/window.hpp +++ b/include/uui/window.hpp @@ -1,10 +1,11 @@ #pragma once +#include #include #include #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 font_manager; virtual void create_component(const ComponentParams& params, InitFunction init) = 0; @@ -28,19 +27,26 @@ public: virtual void create_flex(const StackParams& params, InitFunction init) = 0; virtual void create_stack(const StackParams& params, InitFunction init) = 0; + const std::string& title() { return _title; } int width() { return _width; }; int height() { return _height; }; protected: + ComponentType _type = ComponentType::UNASSIGNED; + std::shared_ptr _app; bool _initialized; + std::string _title; int _width; int _height; bool _render_needed; bool _resize_needed; bool _iconified; + std::array>, Config::MOUSE_GRID_HEIGHT>, Config::MOUSE_GRID_WIDTH> + _grid_click; + virtual void render() = 0; }; } // namespace uui diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index 1fc5879..ab964f2 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -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; diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index 3857260..35c1e10 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -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(window); + this->_window = std::static_pointer_cast(window); + this->_window_index = window_index; + this->_parent = std::static_pointer_cast(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(std::get<0>(position_x)) / static_cast(window->width()); - float vert_y = position_y.index() == 1 - ? std::get<1>(position_y) - : static_cast(std::get<0>(position_y)) / static_cast(window->height()); - float vert_w = size_width.index() == 1 - ? std::get<1>(size_width) - : static_cast(std::get<0>(size_width)) / static_cast(window->width()); - float vert_h = size_height.index() == 1 - ? std::get<1>(size_height) - : static_cast(std::get<0>(size_height)) / static_cast(window->height()); + float vert_x = _position_x.index() == 1 + ? std::get<1>(_position_x) + : static_cast(std::get<0>(_position_x)) / static_cast(_window->width()); + float vert_y = _position_y.index() == 1 + ? std::get<1>(_position_y) + : static_cast(std::get<0>(_position_y)) / static_cast(_window->height()); + float vert_w = _width.index() == 1 ? std::get<1>(_width) + : static_cast(std::get<0>(_width)) / static_cast(_window->width()); + float vert_h = _height.index() == 1 + ? std::get<1>(_height) + : static_cast(std::get<0>(_height)) / static_cast(_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 diff --git a/src/uui/opengl/container.cpp b/src/uui/opengl/container.cpp deleted file mode 100644 index 4d45bf0..0000000 --- a/src/uui/opengl/container.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "uui/opengl/container.hpp" - -#include "uui/opengl/component.hpp" - -std::size_t uui::GlContainer::push_child(std::shared_ptr 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 child) -{ - components.push_back(child); - // components.push_back(std::static_pointer_cast(child)); - return components.size() - 1; -} - -std::shared_ptr 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 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(); -} diff --git a/src/uui/opengl/container_interface.cpp b/src/uui/opengl/container_interface.cpp new file mode 100644 index 0000000..2abe608 --- /dev/null +++ b/src/uui/opengl/container_interface.cpp @@ -0,0 +1,38 @@ +#include "uui/opengl/container_interface.hpp" + +#include + +#include "uui/opengl/component.hpp" + +std::size_t uui::GlContainerInterface::push_child(std::shared_ptr 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 child) +{ + components.push_back(child); + return components.size() - 1; +} + +std::shared_ptr 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 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(); +} diff --git a/src/uui/opengl/flex.cpp b/src/uui/opengl/flex.cpp index bbfd4f8..e38b6e0 100644 --- a/src/uui/opengl/flex.cpp +++ b/src/uui/opengl/flex.cpp @@ -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 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(std::get<1>(position_x) * static_cast(window->width())); - const int initial_y = position_y.index() == 0 - ? std::get<0>(position_y) - : static_cast(std::get<1>(position_y) * static_cast(window->height())); + const int initial_x = _position_x.index() == 0 + ? std::get<0>(_position_x) + : static_cast(std::get<1>(_position_x) * static_cast(_window->width())); + const int initial_y = _position_y.index() == 0 + ? std::get<0>(_position_y) + : static_cast(std::get<1>(_position_y) * static_cast(_window->height())); if(child_index == 0) return {initial_x, initial_y}; @@ -37,14 +38,14 @@ std::tuple 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(std::get<1>(component->size_width) * static_cast(window->width())); + if(component->width().index() == 1) + width = static_cast(std::get<1>(component->width()) * static_cast(_window->width())); else - width = std::get<0>(component->size_width); - if(component->size_height.index() == 1) - height = static_cast(std::get<1>(component->size_height) * static_cast(window->height())); + width = std::get<0>(component->width()); + if(component->height().index() == 1) + height = static_cast(std::get<1>(component->height()) * static_cast(_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 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 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 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 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; diff --git a/src/uui/opengl/font_manager.cpp b/src/uui/opengl/font_manager.cpp index 974ff23..18e67f8 100644 --- a/src/uui/opengl/font_manager.cpp +++ b/src/uui/opengl/font_manager.cpp @@ -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); diff --git a/src/uui/opengl/label.cpp b/src/uui/opengl/label.cpp index 7003b7d..46aee62 100644 --- a/src/uui/opengl/label.cpp +++ b/src/uui/opengl/label.cpp @@ -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(window->width()) - : static_cast(std::get<0>(position_x)); - float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast(window->height()) - : static_cast(std::get<0>(position_y)); + float text_x = _position_x.index() == 1 ? std::get<1>(_position_x) * static_cast(_window->width()) + : static_cast(std::get<0>(_position_x)); + float text_y = _position_y.index() == 1 ? std::get<1>(_position_y) * static_cast(_window->height()) + : static_cast(std::get<0>(_position_y)); text_y += static_cast(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); } diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp index f736185..a6e1eed 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/stack.cpp @@ -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 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 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(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 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(std::get<1>(position_x) * static_cast(window->width())); - int y = position_y.index() == 0 ? std::get<0>(position_y) - : static_cast(std::get<1>(position_y) * static_cast(window->height())); + int x = _position_x.index() == 0 ? std::get<0>(_position_x) + : static_cast(std::get<1>(_position_x) * static_cast(_window->width())); + int y = _position_y.index() == 0 ? std::get<0>(_position_y) + : static_cast(std::get<1>(_position_y) * static_cast(_window->height())); if(child_index == 0) return {x, y}; @@ -73,10 +74,10 @@ std::tuple 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(std::get<1>(component->size_width) * static_cast(window->width())); + if(component->width().index() == 1) + x += static_cast(std::get<1>(component->width()) * static_cast(_window->width())); else - x += std::get<0>(component->size_width); + x += std::get<0>(component->width()); } } else @@ -86,10 +87,10 @@ std::tuple 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(std::get<1>(component->size_height) * static_cast(window->height())); + if(component->height().index() == 1) + y += static_cast(std::get<1>(component->height()) * static_cast(_window->height())); else - y += std::get<0>(component->size_height); + y += std::get<0>(component->height()); } } } diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index 6fd60d0..a139ba7 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -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 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 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(); font_manager = std::static_pointer_cast(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);