From 2ba59d8e37f0d4e82ba8a7491a340745c3601c2f Mon Sep 17 00:00:00 2001 From: Corentin Date: Sun, 18 Jul 2021 01:19:44 +0900 Subject: [PATCH 01/10] Stack component implemented (more flex than stack) --- .ccls | 2 + ROADMAP.md | 21 ++++ include/uui/component.hpp | 17 ++-- include/uui/config.hpp | 12 +++ include/uui/opengl/application.hpp | 28 +++--- include/uui/opengl/component.hpp | 32 ++++-- include/uui/opengl/container.hpp | 44 ++++++++ include/uui/opengl/label.hpp | 12 ++- include/uui/opengl/stack.hpp | 39 ++++++++ make.py | 2 +- manifest.md | 1 - src/test/example_gl.cpp | 95 +++++++++++------- src/uui/opengl/application.cpp | 2 +- src/uui/opengl/component.cpp | 20 ++-- src/uui/opengl/label.cpp | 6 +- src/uui/opengl/stack.cpp | 155 +++++++++++++++++++++++++++++ src/uui/opengl/window.cpp | 34 +++++-- 17 files changed, 424 insertions(+), 98 deletions(-) create mode 100644 ROADMAP.md create mode 100644 include/uui/opengl/container.hpp create mode 100644 include/uui/opengl/stack.hpp create mode 100644 src/uui/opengl/stack.cpp diff --git a/.ccls b/.ccls index 68cd814..04436f7 100644 --- a/.ccls +++ b/.ccls @@ -1,6 +1,8 @@ clang++ -std=c++17 -DDEBUG +-Wall +-Wconversion -Iinclude -I/usr/include/freetype2 -I/usr/include/libpng16 diff --git a/ROADMAP.md b/ROADMAP.md new file mode 100644 index 0000000..d126f8c --- /dev/null +++ b/ROADMAP.md @@ -0,0 +1,21 @@ +* stack component + +* negative absolution position (from right/bottom) + +* flex component + +* layout (constraint) + +* scroll + +* button + +* projected vertex/raw vertex mode (projection on GPU) + +* VAO/VBO in windows (batched) + +* pool/wait mode mechanism + +* better font rendering : signed distance + +* Rich text component : parsing html? making list of struct of texts (with style information for the rendering) diff --git a/include/uui/component.hpp b/include/uui/component.hpp index 7e41fc9..1313eba 100644 --- a/include/uui/component.hpp +++ b/include/uui/component.hpp @@ -3,16 +3,15 @@ #include #include +#include "uui/config.hpp" + namespace uui { class Component { public: - Component( - const std::variant x, const std::variant y, const std::variant width, - const std::variant height): - position_x(x), - position_y(y), size_width(width), size_height(height) + Component(const position_t x, const position_t y, const position_t width, const position_t height): + position_x(x), position_y(y), size_width(width), size_height(height) { background_color = {0.0f, 0.0f, 0.0f, 0.0f}; } @@ -20,10 +19,10 @@ public: virtual void render() = 0; protected: - std::variant position_x; - std::variant position_y; - std::variant size_width; - std::variant size_height; + position_t position_x; + position_t position_y; + position_t size_width; + position_t size_height; std::array background_color; }; diff --git a/include/uui/config.hpp b/include/uui/config.hpp index 1afb3a0..03c4f94 100644 --- a/include/uui/config.hpp +++ b/include/uui/config.hpp @@ -1,8 +1,20 @@ #pragma once +#include + #include "graphic_context.hpp" namespace Config { constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; } + +namespace uui +{ +typedef std::variant position_t; +enum Direction +{ + HORIZONTAL, + VERTICAL +}; +} // namespace uui diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index df15fe4..ed7ab60 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -6,6 +6,8 @@ #include #include "graphic_context.hpp" +#include "uui/config.hpp" +#include "uui/opengl/container.hpp" #include "uui/opengl/font_manager.hpp" namespace uui @@ -34,49 +36,45 @@ private: static GlApplication* application_pointer; }; -class GlLabel; class GlComponent; -class GlWindow +class GlStack; +class GlLabel; +class GlWindow: public GlContainer, public std::enable_shared_from_this { friend class GlApplication; friend class GlComponent; + friend class GlStack; friend class GlLabel; public: std::string title; - std::vector> components; GlWindow(const GlWindow&) = delete; GlWindow(GlWindow&&) = delete; ~GlWindow(); - std::shared_ptr create_component( - const std::variant x, const std::variant y, const std::variant width, - const std::variant height); + std::shared_ptr create_component(const position_t x, const position_t y, const position_t width, const position_t height); std::shared_ptr create_label( - const std::variant x, const std::variant y, const std::string& text, - const std::tuple& text_color); + const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); protected: GlApplication* app; - - GlFontManager font_manager; + bool initialized; int width; int height; bool render_needed; bool resize_needed; - bool iconified; - size_t index; - bool initialized; + GlFontManager font_manager; GLFWwindow* glfw_window; - GLuint gl_program; - GlWindow(GlApplication* app, size_t index, int width, int height, const std::string& title); + GlWindow(GlApplication* app, int width, int height, const std::string& title); + std::shared_ptr getptr() { return shared_from_this(); } void init(); void deinit(); diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index b2c57bc..efcb31a 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -2,31 +2,35 @@ #include #include -#include #include "graphic_context.hpp" +#include "uui/config.hpp" +#include "uui/opengl/container.hpp" #include "window.hpp" namespace uui { -class GlComponent +class GlStack; +class GlComponent: public std::enable_shared_from_this { friend class GlWindow; + friend class GlStack; public: - GlComponent(const GlComponent&) = delete; - GlComponent(GlComponent&&) = delete; ~GlComponent(); void set_background_color(float r, float g, float b, float a = 1.0f); protected: std::shared_ptr window; + size_t window_index; + std::shared_ptr parent; + size_t parent_index; - std::variant position_x; - std::variant position_y; - std::variant size_width; - std::variant size_height; + position_t position_x; + position_t position_y; + position_t size_width; + position_t size_height; bool coord_all_relative; std::array background_color; @@ -38,8 +42,16 @@ protected: GLuint gl_ebo; GlComponent( - std::shared_ptr window, const std::variant x, const std::variant y, - const std::variant width, const std::variant height); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const position_t width, const position_t height); + GlComponent( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const position_t width, + const position_t height): + GlComponent(window, window_index, window, window_index, x, y, width, height) + {} + GlComponent(const GlComponent&) = delete; + GlComponent(GlComponent&&) = default; + std::shared_ptr getptr() { return shared_from_this(); } virtual void init(); virtual void set_vbo_data(); diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp new file mode 100644 index 0000000..7c10116 --- /dev/null +++ b/include/uui/opengl/container.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +#include +#include + +namespace uui +{ +class GlComponent; +class GlStack; +class GlContainer +{ + friend class GlComponent; + friend class GlStack; + +protected: + GlContainer() = default; + GlContainer(const GlContainer&) = default; + GlContainer(GlContainer&&) = default; + + virtual std::size_t push_child(std::shared_ptr child) + { + components.push_back(child); + return components.size() - 1; + } + virtual std::shared_ptr get_child(std::size_t index) const { return components.at(index); } + virtual void remove_child(std::size_t index) { components.at(index) = nullptr; }; + + virtual std::tuple get_child_position(const std::size_t child_index) const { return {0, 0}; } + +public: + enum Type + { + WINDOW, + STACK + }; + std::vector> components; + Type type; + + ~GlContainer() { components.clear(); } +}; + +} // namespace uui diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index fcb1d24..c24c5ba 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -4,12 +4,15 @@ #include #include "uui/opengl/component.hpp" +#include "uui/opengl/container.hpp" namespace uui { +class GlStack; class GlLabel: public GlComponent { friend class GlWindow; + friend class GlStack; size_t font_index; @@ -24,8 +27,13 @@ protected: int text_height; GlLabel( - std::shared_ptr window, const std::variant x, const std::variant y, const std::string& text, - const std::tuple& text_color); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + GlLabel( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color): + GlLabel(window, window_index, window, window_index, x, y, text, text_color) + {} void render(); }; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp new file mode 100644 index 0000000..e89fba1 --- /dev/null +++ b/include/uui/opengl/stack.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#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 create_component(const position_t width, const position_t height); + std::shared_ptr create_label(const std::string& text, const std::tuple& text_color); + +protected: + Direction direction; + int content_width; + int content_height; + + GlStack( + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const Direction direction); + GlStack(std::shared_ptr 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; + + std::tuple get_child_position(const std::size_t child_index) const override; +}; + +} // namespace uui diff --git a/make.py b/make.py index 7799daf..86b943e 100644 --- a/make.py +++ b/make.py @@ -21,7 +21,7 @@ class Config: COMMON_FLAGS = '-std=c++17 -fno-semantic-interposition' COMMON_DEBUG_FLAGS = '-g -DDEBUG' COMMON_RELEASE_FLAGS = '-O2 -flto' - COMPILE_FLAGS = f'-Wall -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`' + COMPILE_FLAGS = f'-Wall -Wconversion -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`' LINK_FLAGS = '-lpthread -ldl `pkg-config --libs glfw3 vulkan gl x11 freetype2`' PRE_COMPILE_FUNCTION = None diff --git a/manifest.md b/manifest.md index b7f40bd..1c88ca7 100644 --- a/manifest.md +++ b/manifest.md @@ -1,6 +1,5 @@ # UUI : micro UI Toolkit - * C++ + Vulkan/OpenGL * Python binding diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index f1eeb8b..3d3975f 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -1,53 +1,74 @@ #include +#include "uui/config.hpp" #include "uui/opengl/application.hpp" #include "uui/opengl/component.hpp" #include "uui/opengl/label.hpp" +#include "uui/opengl/stack.hpp" using namespace std; int main() { - try + // try + // { + cout << "Starting example 1" << endl; { - cout << "Starting example 1" << endl; - { - uui::GlApplication app; - // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); - auto comp = window->create_component(0, 100, 0.5f, 50); - comp->set_background_color(0.8f, 0.1f, 0.5f); - comp = window->create_component(0.25f, 50, 0.4f, 150); - comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f); - auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f); - label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); - 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}); - app.run(); - } - // cout << "\nStarting example 2" << endl; - // { - // uui::GlApplication app; - // auto window_1 = app.create_window(800, 600, "OpenGl!"); - // auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); - // comp->set_background_color(0.8f, 0.1f, 0.5f); - // comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); - // comp->set_background_color(0.2f, 0.1f, 0.5f); + uui::GlApplication app; + // uui::GlApplication app2; // exception : application already created + auto window = app.create_window(800, 600, "OpenGl!"); - // auto window_2 = app.create_window(600, 600, "OpenGL 2!"); - // comp = window_2->create_component(0, 0.5f, 100, 0.5f); - // comp->set_background_color(0.2f, 0.5f, 0.3f); - // auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - // label->set_background_color(0.1f, 0.2f, 0.5f); - // app.run(); - // } - } - catch(const std::exception& error) - { - std::cerr << error.what() << std::endl; - return EXIT_FAILURE; + auto comp = window->create_component(0, 100, 0.5f, 50); + comp->set_background_color(0.8f, 0.1f, 0.5f); + + comp = window->create_component(0.25f, 50, 0.4f, 150); + comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f); + + auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); + label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f); + + label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); + 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_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_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}); + comp = stack->create_component(100, 20); + comp->set_background_color(0.2f, 0.25f, 0.75f); + + app.run(); } + // cout << "\nStarting example 2" << endl; + // { + // uui::GlApplication app; + // auto window_1 = app.create_window(800, 600, "OpenGl!"); + // auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); + // comp->set_background_color(0.8f, 0.1f, 0.5f); + // comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); + // comp->set_background_color(0.2f, 0.1f, 0.5f); + + // auto window_2 = app.create_window(600, 600, "OpenGL 2!"); + // comp = window_2->create_component(0, 0.5f, 100, 0.5f); + // comp->set_background_color(0.2f, 0.5f, 0.3f); + // auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); + // label->set_background_color(0.1f, 0.2f, 0.5f); + // app.run(); + // } + // } + // catch(const std::exception& error) + // { + // std::cerr << error.what() << std::endl; + // return EXIT_FAILURE; + // } cout << "Example done" << endl; return 0; diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index 79ed9c6..ee36f85 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -99,7 +99,7 @@ void uui::GlApplication::run() std::shared_ptr uui::GlApplication::create_window(int width, int height, const std::string& title) { - std::shared_ptr window(new uui::GlWindow(this, windows.size(), width, height, title)); + std::shared_ptr window(new uui::GlWindow(this, width, height, title)); window->init(); windows.push_back(window); glfwSwapInterval(1); diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index 1a43e76..3429f33 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -6,16 +6,17 @@ #include "uui/opengl/gl_utils.hpp" uui::GlComponent::GlComponent( - std::shared_ptr window, const std::variant x, const std::variant y, - const std::variant width, const std::variant height): + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const position_t width, const position_t height): window(window), - position_x(x), position_y(y), size_width(width), size_height(height), initialized(false) + window_index(window_index), parent(parent), parent_index(parent_index), position_x(x), position_y(y), size_width(width), + size_height(height), initialized(false) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlBox" << std::endl; coord_all_relative = true; - auto parse_coords = [&](std::variant& value) { + auto parse_coords = [&](position_t& value) { if(value.index() == 0) { if(std::get<0>(value) == 0) @@ -35,9 +36,11 @@ uui::GlComponent::GlComponent( uui::GlComponent::~GlComponent() { if constexpr(uui::GlApplication::debug_mode) - std::cout << "Closing a GlBox" << std::endl; + std::cout << "Closing a GlComponent" << std::endl; if(initialized) deinit(); + // if(parent != window) + // parent->remove_child(0); // TODO } void uui::GlComponent::set_background_color(float r, float g, float b, float a /*=0.0f*/) @@ -95,8 +98,6 @@ void uui::GlComponent::set_vbo_data() float x_max = x_min + (2.0f * vert_w); float y_min = (-2.0f * vert_y) + 1.0f; float y_max = y_min - (2.0f * vert_h); - std::cout << "Sending vbo data with color: " << background_color[0] << ", " << background_color[1] << ", " << background_color[2] << ", " - << background_color[3] << std::endl; // clang-format off float vertices[] = { x_max, y_min, background_color[0] , background_color[1], background_color[2], background_color[3], // top right @@ -147,8 +148,11 @@ void uui::GlComponent::render() void uui::GlComponent::on_resize() { - if(!coord_all_relative) + bool is_in_stack = parent->type == GlContainer::Type::STACK; + if(!coord_all_relative || is_in_stack) { + if(is_in_stack) + 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/label.cpp b/src/uui/opengl/label.cpp index 5ba7478..7e39604 100644 --- a/src/uui/opengl/label.cpp +++ b/src/uui/opengl/label.cpp @@ -5,9 +5,9 @@ #include "uui/opengl/gl_utils.hpp" uui::GlLabel::GlLabel( - std::shared_ptr window, const std::variant x, const std::variant y, const std::string& text, - const std::tuple& text_color): - GlComponent(window, x, y, 0.0f, 0.0f), + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const std::string& text, const std::tuple& text_color): + GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f), text(text), text_color(text_color) { if constexpr(uui::GlApplication::debug_mode) diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp new file mode 100644 index 0000000..25037db --- /dev/null +++ b/src/uui/opengl/stack.cpp @@ -0,0 +1,155 @@ +#include "uui/opengl/stack.hpp" + +#include + +#include "uui/opengl/gl_utils.hpp" + +uui::GlStack::GlStack( + std::shared_ptr window, std::size_t window_index, std::shared_ptr 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; + + type = Type::STACK; + coord_all_relative = false; +} + +std::shared_ptr 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 component(new uui::GlComponent( + window, window->components.size(), std::dynamic_pointer_cast(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::GlStack::create_label(const std::string& text, const std::tuple& text_color) +{ + if(!initialized) + throw std::runtime_error("GlWindow error : cannot create component while not initialized"); + + glfwMakeContextCurrent(window->glfw_window); + std::shared_ptr label(new uui::GlLabel( + window, window->components.size(), std::dynamic_pointer_cast(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 uui::GlStack::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)); + + if(child_index == 0) + return {initial_x, initial_y}; + if(child_index >= components.size()) + throw std::runtime_error("GlStack error: child_index greater than children count"); + + auto get_child_size = [&](std::size_t index) -> std::tuple { + int width = 0; + int height = 0; + 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)); + 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)); + else + height = std::get<0>(component->size_height); + } + return {width, height}; + }; + int x = initial_x; + int y = initial_y; + if(direction == Direction::HORIZONTAL) + { + int child_width; + int child_height; + int max_height = 0; + + std::tie(child_width, child_height) = get_child_size(0); + if(child_height > max_height) + max_height = child_height; + x += child_width; + for(std::size_t index = 1; index < child_index; index += 1) + { + 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) + { + x = initial_x + child_width; + y += max_height; + max_height = child_height; + } + else + x += child_width; + } + 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) + { + x = initial_x; + y += max_height; + } + } + } + else + { + int child_width; + int child_height; + int max_width = 0; + + std::tie(child_width, child_height) = get_child_size(0); + if(child_width > max_width) + max_width = child_width; + y += child_height; + for(std::size_t index = 1; index < child_index; index += 1) + { + 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) + { + y = initial_y + child_height; + x += max_width; + max_width = child_width; + } + else + y += child_height; + } + 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) + { + y = initial_y; + x += max_width; + } + } + } + + return {x, y}; +} diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index d728d77..bb92f92 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -5,6 +5,7 @@ #include "uui/opengl/component.hpp" #include "uui/opengl/gl_utils.hpp" #include "uui/opengl/label.hpp" +#include "uui/opengl/stack.hpp" #include "uui/shader.hpp" static void APIENTRY @@ -80,12 +81,13 @@ void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode, glfwSetWindowShouldClose(window, true); } -uui::GlWindow::GlWindow(uui::GlApplication* app, size_t index, int width, int height, const std::string& title): - app(app), width(width), height(height), render_needed(true), resize_needed(false), iconified(false), index(index), initialized(false) +uui::GlWindow::GlWindow(uui::GlApplication* app, int width, int height, const std::string& title): + app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false), iconified(false) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlWindow" << std::endl; this->title = title; + type = Type::WINDOW; } uui::GlWindow::~GlWindow() @@ -247,29 +249,39 @@ void uui::GlWindow::render() } std::shared_ptr uui::GlWindow::create_component( - const std::variant x, const std::variant y, const std::variant width, - const std::variant height) + const position_t x, const position_t y, const position_t width, const position_t height) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr component(new uui::GlComponent(app->get_window(index), x, y, width, height)); + std::shared_ptr component(new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); + push_child(component); component->init(); - components.push_back(component); return component; } std::shared_ptr uui::GlWindow::create_label( - const std::variant x, const std::variant y, const std::string& text, - const std::tuple& text_color) + const position_t x, const position_t y, const std::string& text, const std::tuple& text_color) { if(!initialized) - throw std::runtime_error("GlWindow error : cannot create component while not initialized"); + throw std::runtime_error("GlWindow error : cannot create label while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr label(new uui::GlLabel(app->get_window(index), x, y, text, text_color)); + std::shared_ptr label(new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); + push_child(label); label->init(); - components.push_back(label); return label; } + +std::shared_ptr 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 stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction)); + push_child(stack); + stack->init(); + return stack; +} From e7753f244c592122578318944312402d0a50c87b Mon Sep 17 00:00:00 2001 From: Corentin Date: Thu, 22 Jul 2021 17:03:34 +0900 Subject: [PATCH 02/10] Rename stack to flex --- .gitignore | 1 + ROADMAP.md | 8 ++++---- include/uui/opengl/application.hpp | 6 +++--- include/uui/opengl/component.hpp | 4 ++-- include/uui/opengl/container.hpp | 6 +++--- include/uui/opengl/{stack.hpp => flex.hpp} | 8 ++++---- include/uui/opengl/label.hpp | 4 ++-- src/test/example_gl.cpp | 6 +++--- src/uui/opengl/component.cpp | 2 +- src/uui/opengl/{stack.cpp => flex.cpp} | 12 ++++++------ src/uui/opengl/window.cpp | 6 +++--- 11 files changed, 32 insertions(+), 31 deletions(-) rename include/uui/opengl/{stack.hpp => flex.hpp} (76%) rename src/uui/opengl/{stack.cpp => flex.cpp} (91%) diff --git a/.gitignore b/.gitignore index f6d5daf..68174dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .#.* *.kdev4 +*.ttf .ccls-cache .kdev4 diff --git a/ROADMAP.md b/ROADMAP.md index d126f8c..e4b4f52 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,17 +1,17 @@ * stack component -* negative absolution position (from right/bottom) - -* flex component +* button * layout (constraint) * scroll -* button +* Text wrap * projected vertex/raw vertex mode (projection on GPU) +* negative absolution position (from right/bottom) + * VAO/VBO in windows (batched) * pool/wait mode mechanism diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index ed7ab60..2488997 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -37,13 +37,13 @@ private: }; class GlComponent; -class GlStack; +class GlFlex; class GlLabel; class GlWindow: public GlContainer, public std::enable_shared_from_this { friend class GlApplication; friend class GlComponent; - friend class GlStack; + friend class GlFlex; friend class GlLabel; public: @@ -56,7 +56,7 @@ public: std::shared_ptr create_component(const position_t x, const position_t y, const position_t width, const position_t height); std::shared_ptr create_label( const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); - std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); + std::shared_ptr create_flex(const position_t x, const position_t y, const Direction direction); protected: GlApplication* app; diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index efcb31a..d6b3b2d 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -10,11 +10,11 @@ namespace uui { -class GlStack; +class GlFlex; class GlComponent: public std::enable_shared_from_this { friend class GlWindow; - friend class GlStack; + friend class GlFlex; public: ~GlComponent(); diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index 7c10116..0455201 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -8,11 +8,11 @@ namespace uui { class GlComponent; -class GlStack; +class GlFlex; class GlContainer { friend class GlComponent; - friend class GlStack; + friend class GlFlex; protected: GlContainer() = default; @@ -33,7 +33,7 @@ public: enum Type { WINDOW, - STACK + FLEX }; std::vector> components; Type type; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/flex.hpp similarity index 76% rename from include/uui/opengl/stack.hpp rename to include/uui/opengl/flex.hpp index e89fba1..1bd209b 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/flex.hpp @@ -10,7 +10,7 @@ namespace uui { -class GlStack: public GlComponent, public GlContainer +class GlFlex: public GlComponent, public GlContainer { friend class GlWindow; @@ -23,11 +23,11 @@ protected: int content_width; int content_height; - GlStack( + GlFlex( std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, const position_t x, const position_t y, const Direction direction); - GlStack(std::shared_ptr 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) + GlFlex(std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const Direction direction): + GlFlex(window, window_index, window, window_index, x, y, direction) {} float get_width() const; diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index c24c5ba..3257da5 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -8,11 +8,11 @@ namespace uui { -class GlStack; +class GlFlex; class GlLabel: public GlComponent { friend class GlWindow; - friend class GlStack; + friend class GlFlex; size_t font_index; diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index 3d3975f..6204a78 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -3,8 +3,8 @@ #include "uui/config.hpp" #include "uui/opengl/application.hpp" #include "uui/opengl/component.hpp" +#include "uui/opengl/flex.hpp" #include "uui/opengl/label.hpp" -#include "uui/opengl/stack.hpp" using namespace std; @@ -31,14 +31,14 @@ 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_stack(200, 0.6f, uui::Direction::HORIZONTAL); + auto stack = window->create_flex(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_stack(50, 400, uui::Direction::VERTICAL); + stack = window->create_flex(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}); diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index 3429f33..843a45c 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -148,7 +148,7 @@ void uui::GlComponent::render() void uui::GlComponent::on_resize() { - bool is_in_stack = parent->type == GlContainer::Type::STACK; + bool is_in_stack = parent->type == GlContainer::Type::FLEX; if(!coord_all_relative || is_in_stack) { if(is_in_stack) diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/flex.cpp similarity index 91% rename from src/uui/opengl/stack.cpp rename to src/uui/opengl/flex.cpp index 25037db..73f819a 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/flex.cpp @@ -1,10 +1,10 @@ -#include "uui/opengl/stack.hpp" +#include "uui/opengl/flex.hpp" #include #include "uui/opengl/gl_utils.hpp" -uui::GlStack::GlStack( +uui::GlFlex::GlFlex( std::shared_ptr window, std::size_t window_index, std::shared_ptr 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), @@ -13,11 +13,11 @@ uui::GlStack::GlStack( if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlStack" << std::endl; - type = Type::STACK; + type = Type::FLEX; coord_all_relative = false; } -std::shared_ptr uui::GlStack::create_component(const position_t width, const position_t height) +std::shared_ptr 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"); @@ -34,7 +34,7 @@ std::shared_ptr uui::GlStack::create_component(const position_ return component; } -std::shared_ptr uui::GlStack::create_label(const std::string& text, const std::tuple& text_color) +std::shared_ptr uui::GlFlex::create_label(const std::string& text, const std::tuple& text_color) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -51,7 +51,7 @@ std::shared_ptr uui::GlStack::create_label(const std::string& text return label; } -std::tuple uui::GlStack::get_child_position(const std::size_t child_index) const +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)); diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index bb92f92..3f866c6 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -3,9 +3,9 @@ #include #include "uui/opengl/component.hpp" +#include "uui/opengl/flex.hpp" #include "uui/opengl/gl_utils.hpp" #include "uui/opengl/label.hpp" -#include "uui/opengl/stack.hpp" #include "uui/shader.hpp" static void APIENTRY @@ -274,13 +274,13 @@ std::shared_ptr uui::GlWindow::create_label( return label; } -std::shared_ptr uui::GlWindow::create_stack(const position_t x, const position_t y, const Direction direction) +std::shared_ptr 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 stack while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction)); + std::shared_ptr stack(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction)); push_child(stack); stack->init(); return stack; From 896f98476e276b105a72b1c7126fc3cf2fdc67cf Mon Sep 17 00:00:00 2001 From: Corentin Date: Thu, 22 Jul 2021 17:47:03 +0900 Subject: [PATCH 03/10] Implement GlStack, GlFlex is now child of GlStack --- ROADMAP.md | 2 - include/uui/opengl/application.hpp | 3 + include/uui/opengl/component.hpp | 2 + include/uui/opengl/container.hpp | 3 +- include/uui/opengl/flex.hpp | 14 +---- include/uui/opengl/label.hpp | 2 + include/uui/opengl/stack.hpp | 39 ++++++++++++ src/test/example_gl.cpp | 20 ++++++- src/uui/opengl/component.cpp | 2 +- src/uui/opengl/flex.cpp | 37 +----------- src/uui/opengl/stack.cpp | 96 ++++++++++++++++++++++++++++++ src/uui/opengl/window.cpp | 14 ++++- 12 files changed, 179 insertions(+), 55 deletions(-) create mode 100644 include/uui/opengl/stack.hpp create mode 100644 src/uui/opengl/stack.cpp diff --git a/ROADMAP.md b/ROADMAP.md index e4b4f52..9c5580d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,5 +1,3 @@ -* stack component - * button * layout (constraint) diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index 2488997..2146237 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -39,12 +39,14 @@ private: class GlComponent; class GlFlex; class GlLabel; +class GlStack; class GlWindow: public GlContainer, public std::enable_shared_from_this { 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 create_label( const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); std::shared_ptr create_flex(const position_t x, const position_t y, const Direction direction); + std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); protected: GlApplication* app; diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index d6b3b2d..ab9a554 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -11,10 +11,12 @@ namespace uui { class GlFlex; +class GlStack; class GlComponent: public std::enable_shared_from_this { friend class GlWindow; friend class GlFlex; + friend class GlStack; public: ~GlComponent(); diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index 0455201..f690fb1 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -33,7 +33,8 @@ public: enum Type { WINDOW, - FLEX + FLEX, + STACK }; std::vector> components; Type type; diff --git a/include/uui/opengl/flex.hpp b/include/uui/opengl/flex.hpp index 1bd209b..8448c7a 100644 --- a/include/uui/opengl/flex.hpp +++ b/include/uui/opengl/flex.hpp @@ -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 create_component(const position_t width, const position_t height); - std::shared_ptr create_label(const std::string& text, const std::tuple& text_color); - protected: - Direction direction; - int content_width; - int content_height; - GlFlex( std::shared_ptr window, std::size_t window_index, std::shared_ptr 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 get_child_position(const std::size_t child_index) const override; }; diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index 3257da5..be3eee1 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -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; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp new file mode 100644 index 0000000..6a52e38 --- /dev/null +++ b/include/uui/opengl/stack.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#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 create_component(const position_t width, const position_t height); + std::shared_ptr create_label(const std::string& text, const std::tuple& text_color); + +protected: + Direction direction; + int content_width; + int content_height; + + GlStack( + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, + const position_t x, const position_t y, const Direction direction); + GlStack(std::shared_ptr 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 get_child_position(const std::size_t child_index) const; +}; + +} // namespace uui diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index 6204a78..8315822 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -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}); diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index 843a45c..f4e031e 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -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) diff --git a/src/uui/opengl/flex.cpp b/src/uui/opengl/flex.cpp index 73f819a..5cd73db 100644 --- a/src/uui/opengl/flex.cpp +++ b/src/uui/opengl/flex.cpp @@ -7,8 +7,7 @@ uui::GlFlex::GlFlex( std::shared_ptr window, std::size_t window_index, std::shared_ptr 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::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 component(new uui::GlComponent( - window, window->components.size(), std::dynamic_pointer_cast(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::GlFlex::create_label(const std::string& text, const std::tuple& text_color) -{ - if(!initialized) - throw std::runtime_error("GlWindow error : cannot create component while not initialized"); - - glfwMakeContextCurrent(window->glfw_window); - std::shared_ptr label(new uui::GlLabel( - window, window->components.size(), std::dynamic_pointer_cast(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 uui::GlFlex::get_child_position(const std::size_t child_index) const { const int initial_x = diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp new file mode 100644 index 0000000..1b2c9f1 --- /dev/null +++ b/src/uui/opengl/stack.cpp @@ -0,0 +1,96 @@ +#include "uui/opengl/stack.hpp" + +#include + +#include "uui/opengl/gl_utils.hpp" + +uui::GlStack::GlStack( + std::shared_ptr window, std::size_t window_index, std::shared_ptr 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::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 component(new uui::GlComponent( + window, window->components.size(), std::dynamic_pointer_cast(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::GlStack::create_label(const std::string& text, const std::tuple& text_color) +{ + if(!initialized) + throw std::runtime_error("GlWindow error : cannot create component while not initialized"); + + glfwMakeContextCurrent(window->glfw_window); + std::shared_ptr label(new uui::GlLabel( + window, window->components.size(), std::dynamic_pointer_cast(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 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)); + + 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(std::get<1>(component->size_width) * static_cast(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(std::get<1>(component->size_height) * static_cast(window->height)); + else + y += std::get<0>(component->size_height); + } + } + } + + return {x, y}; +} diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index 3f866c6..5063be7 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -275,12 +275,24 @@ std::shared_ptr uui::GlWindow::create_label( } std::shared_ptr 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 flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction)); + push_child(flex); + flex->init(); + return flex; +} + +std::shared_ptr 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 stack(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction)); + std::shared_ptr stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction)); push_child(stack); stack->init(); return stack; From d15b25dba60efba0cb09ab2a84557ea88116a86d Mon Sep 17 00:00:00 2001 From: Corentin Date: Sat, 1 Oct 2022 05:03:16 +0900 Subject: [PATCH 04/10] Fix code format and add Style object (unused) --- .ccls | 2 +- .clang-format | 14 ++-- ROADMAP.md | 4 +- include/uui/component.hpp | 2 +- include/uui/config.hpp | 1 + include/uui/opengl/application.hpp | 8 +- include/uui/opengl/button.hpp | 29 +++++++ include/uui/opengl/component.hpp | 23 ++--- include/uui/opengl/container.hpp | 7 +- include/uui/opengl/flex.hpp | 10 ++- include/uui/opengl/font_manager.hpp | 6 +- include/uui/opengl/label.hpp | 13 +-- include/uui/opengl/stack.hpp | 13 +-- include/uui/style.hpp | 46 ++++++++++ makefile | 3 + src/test/benchmark_text.cpp | 3 +- src/test/example_gl.cpp | 126 ++++++++++++++-------------- src/uui/opengl/application.cpp | 2 +- src/uui/opengl/component.cpp | 69 ++++++++------- src/uui/opengl/flex.cpp | 16 ++-- src/uui/opengl/font_manager.cpp | 34 ++++---- src/uui/opengl/label.cpp | 23 ++--- src/uui/opengl/stack.cpp | 29 ++++--- src/uui/opengl/window.cpp | 32 ++++--- src/uui/shader.cpp | 6 +- 25 files changed, 320 insertions(+), 201 deletions(-) create mode 100644 include/uui/opengl/button.hpp create mode 100644 include/uui/style.hpp diff --git a/.ccls b/.ccls index 04436f7..a91784d 100644 --- a/.ccls +++ b/.ccls @@ -1,4 +1,4 @@ -clang++ +clangd -std=c++17 -DDEBUG -Wall diff --git a/.clang-format b/.clang-format index f54912e..3efd2bd 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,5 @@ BasedOnStyle: Microsoft -AccessModifierOffset: -3 +AccessModifierOffset: -4 AlignAfterOpenBracket: AlwaysBreak AlignConsecutiveAssignments: None AlignConsecutiveBitFields: None @@ -21,7 +21,7 @@ AllowShortLoopsOnASingleLine: true AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: No -AttributeMacros: ['__ununsed'] +AttributeMacros: ['__unused'] BinPackArguments: true BinPackParameters: true BitFieldColonSpacing: Both @@ -50,12 +50,13 @@ BreakBeforeTernaryOperators: true BreakConstructorInitializers: AfterColon BreakInheritanceList: AfterColon BreakStringLiterals: true -ColumnLimit: 140 +ColumnLimit: 120 CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false +EmptyLineAfterAccessModifier: Never EmptyLineBeforeAccessModifier: LogicalBlock FixNamespaceComments: true IncludeBlocks: Regroup @@ -78,13 +79,14 @@ IncludeCategories: - Regex: '^"' Priority: 4 SortPriority: 4 +IndentAccessModifiers: false IndentCaseBlocks: true IndentCaseLabels: true IndentExternBlock: NoIndent IndentGotoLabels: false IndentPPDirectives: BeforeHash IndentRequires: true -IndentWidth: 3 +IndentWidth: 4 IndentWrappedFunctionNames: false InsertTrailingCommas: None KeepEmptyLinesAtTheStartOfBlocks: false @@ -116,6 +118,6 @@ SpacesInContainerLiterals: false SpacesInParentheses: false SpacesInSquareBrackets: false Standard: c++17 -TabWidth: 3 +TabWidth: 4 UseCRLF: false -UseTab: Always \ No newline at end of file +UseTab: Always diff --git a/ROADMAP.md b/ROADMAP.md index 9c5580d..6125280 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,6 +1,8 @@ +* layout + * button -* layout (constraint) +* lambda constructor * scroll diff --git a/include/uui/component.hpp b/include/uui/component.hpp index 1313eba..53a4c50 100644 --- a/include/uui/component.hpp +++ b/include/uui/component.hpp @@ -11,7 +11,7 @@ class Component { public: Component(const position_t x, const position_t y, const position_t width, const position_t height): - position_x(x), position_y(y), size_width(width), size_height(height) + position_x(x), position_y(y), size_width(width), size_height(height) { background_color = {0.0f, 0.0f, 0.0f, 0.0f}; } diff --git a/include/uui/config.hpp b/include/uui/config.hpp index 03c4f94..a45f341 100644 --- a/include/uui/config.hpp +++ b/include/uui/config.hpp @@ -12,6 +12,7 @@ constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; namespace uui { typedef std::variant position_t; +typedef std::variant size_t; enum Direction { HORIZONTAL, diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index 2146237..30c1c9f 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -30,7 +30,7 @@ public: void run(); std::shared_ptr create_window(int width, int height, const std::string& title); - std::shared_ptr get_window(size_t index) const; + std::shared_ptr get_window(std::size_t index) const; private: static GlApplication* application_pointer; @@ -55,9 +55,11 @@ public: GlWindow(GlWindow&&) = delete; ~GlWindow(); - std::shared_ptr create_component(const position_t x, const position_t y, const position_t width, const position_t height); + std::shared_ptr create_component( + const position_t x, const position_t y, const position_t width, const position_t height); std::shared_ptr create_label( - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color); std::shared_ptr create_flex(const position_t x, const position_t y, const Direction direction); std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); diff --git a/include/uui/opengl/button.hpp b/include/uui/opengl/button.hpp new file mode 100644 index 0000000..dfe5854 --- /dev/null +++ b/include/uui/opengl/button.hpp @@ -0,0 +1,29 @@ + +#pragma once + +#include +#include +#include + +#include "uui/opengl/component.hpp" + +namespace uui +{ +class GlButton: public GlComponent +{ + friend class GlWindow; + +protected: + GlButton( + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlButton( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const Direction direction): + GlButton(window, window_index, window, window_index, x, y, direction) + {} + + virtual std::tuple get_child_position(const std::size_t child_index) const; +}; + +} // namespace uui diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index ab9a554..135bbc5 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -6,6 +6,7 @@ #include "graphic_context.hpp" #include "uui/config.hpp" #include "uui/opengl/container.hpp" +#include "uui/style.hpp" #include "window.hpp" namespace uui @@ -19,21 +20,22 @@ class GlComponent: public std::enable_shared_from_this friend class GlStack; public: - ~GlComponent(); + virtual ~GlComponent(); void set_background_color(float r, float g, float b, float a = 1.0f); protected: std::shared_ptr window; - size_t window_index; + std::size_t window_index; std::shared_ptr parent; - size_t parent_index; + std::size_t parent_index; position_t position_x; position_t position_y; - position_t size_width; - position_t size_height; + size_t size_width; + size_t size_height; bool coord_all_relative; + Style style; std::array background_color; @@ -44,12 +46,13 @@ protected: GLuint gl_ebo; GlComponent( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const position_t width, const position_t height); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const position_t width, + const position_t height); GlComponent( - std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const position_t width, - const position_t height): - GlComponent(window, window_index, window, window_index, x, y, width, height) + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const position_t width, const position_t height): + GlComponent(window, window_index, window, window_index, x, y, width, height) {} GlComponent(const GlComponent&) = delete; GlComponent(GlComponent&&) = default; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index f690fb1..4673972 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -39,7 +40,11 @@ public: std::vector> components; Type type; - ~GlContainer() { components.clear(); } + virtual ~GlContainer() + { + std::cout << "Destroying Container" << std::endl; + components.clear(); + } }; } // namespace uui diff --git a/include/uui/opengl/flex.hpp b/include/uui/opengl/flex.hpp index 8448c7a..918792a 100644 --- a/include/uui/opengl/flex.hpp +++ b/include/uui/opengl/flex.hpp @@ -17,10 +17,12 @@ class GlFlex: public GlStack protected: GlFlex( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction); - GlFlex(std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const Direction direction): - GlFlex(window, window_index, window, window_index, x, y, direction) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlFlex( + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const Direction direction): + GlFlex(window, window_index, window, window_index, x, y, direction) {} 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 07184a5..926f84c 100644 --- a/include/uui/opengl/font_manager.hpp +++ b/include/uui/opengl/font_manager.hpp @@ -46,8 +46,8 @@ public: ~GlFontManager(); void render_text( - const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, - const std::tuple& text_color); + const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, + const std::tuple& text_color); std::tuple get_text_size(const std::string& text, Font& font, float scale_x, float scale_y); private: @@ -63,6 +63,6 @@ private: void init(); void deinit(); - const Font& init_font(const std::string& font_path, size_t font_size); + const Font& init_font(const std::string& font_path, std::size_t font_size); }; } // namespace uui diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index be3eee1..99f094d 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -16,7 +16,7 @@ class GlLabel: public GlComponent friend class GlFlex; friend class GlStack; - size_t font_index; + std::size_t font_index; float get_width() const; float get_height() const; @@ -29,12 +29,13 @@ protected: int text_height; GlLabel( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color); + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color); GlLabel( - std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color): - GlLabel(window, window_index, window, window_index, x, y, text, text_color) + std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, + const std::string& text, const std::tuple& text_color): + GlLabel(window, window_index, window, window_index, x, y, text, text_color) {} void render(); diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index 6a52e38..9204a64 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -16,7 +16,8 @@ class GlStack: public GlComponent, public GlContainer public: std::shared_ptr create_component(const position_t width, const position_t height); - std::shared_ptr create_label(const std::string& text, const std::tuple& text_color); + std::shared_ptr create_label( + const std::string& text, const std::tuple& text_color); protected: Direction direction; @@ -24,10 +25,12 @@ protected: int content_height; GlStack( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction); - GlStack(std::shared_ptr 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) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const Direction direction); + GlStack( + std::shared_ptr 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; diff --git a/include/uui/style.hpp b/include/uui/style.hpp new file mode 100644 index 0000000..22d7401 --- /dev/null +++ b/include/uui/style.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +#include "config.hpp" + +namespace uui +{ +struct Style +{ + enum Position + { + RELATIVE, + ABSOLUTE + }; + enum Alignment + { + START, + CENTER, + END + }; + struct AbsolutePostion + { + std::optional left; + std::optional right; + std::optional top; + std::optional bottom; + }; + + // Layout + Position position; + Alignment horizontal_alignment; + Alignment vertical_alignment; + std::optional width; + std::optional height; + std::optional margin; + std::optional padding; + // For absolute positioning + std::unique_ptr absolute_position; + + // Color + std::optional> background_color; +}; +} // namespace uui diff --git a/makefile b/makefile index db2db6a..5c9f40d 100644 --- a/makefile +++ b/makefile @@ -11,3 +11,6 @@ debug: clean: @PYTHONPATH=$(UMAKE_PATH) python $(MAKE_PATH)/make.py --clean + +lint: + @clang-format --dry-run --Werror include/uui/*.hpp include/uui/**/*.hpp src/uui/*.cpp src/uui/**/*.cpp src/test/*.cpp diff --git a/src/test/benchmark_text.cpp b/src/test/benchmark_text.cpp index d51a978..1855d8c 100644 --- a/src/test/benchmark_text.cpp +++ b/src/test/benchmark_text.cpp @@ -13,8 +13,7 @@ int main() uui::GlApplication app; // uui::GlApplication app2; // exception : application already created auto window = app.create_window(800, 600, "OpenGl!"); - for(int i = 0; i < 100; i += 1) - window->create_label(4*i, 4*i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); + for(int i = 0; i < 100; i += 1) window->create_label(4 * i, 4 * i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); app.run(); } catch(const std::exception& error) diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index 8315822..a94fe74 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -10,81 +10,81 @@ using namespace std; int main() { - // try - // { - cout << "Starting example 1" << endl; + try { - uui::GlApplication app; - // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); + cout << "Starting example 1" << endl; + { + uui::GlApplication app; + // uui::GlApplication app2; // exception : application already created + auto window = app.create_window(800, 600, "OpenGl!"); - auto comp = window->create_component(0, 100, 0.5f, 50); - comp->set_background_color(0.8f, 0.1f, 0.5f); + auto comp = window->create_component(0, 100, 0.5f, 50); + comp->set_background_color(0.8f, 0.1f, 0.5f); - comp = window->create_component(0.25f, 50, 0.4f, 150); - comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f); + comp = window->create_component(0.25f, 50, 0.4f, 150); + comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f); - auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f); + auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); + label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f); - label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); - 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.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); + 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 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); + 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); + 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"); + 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); + 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_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}); - comp = stack->create_component(100, 20); - comp->set_background_color(0.2f, 0.25f, 0.75f); + 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}); + comp = stack->create_component(100, 20); + comp->set_background_color(0.2f, 0.25f, 0.75f); - app.run(); + app.run(); + } + cout << "\nStarting example 2" << endl; + { + uui::GlApplication app; + auto window_1 = app.create_window(800, 600, "OpenGl!"); + auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); + comp->set_background_color(0.8f, 0.1f, 0.5f); + comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); + comp->set_background_color(0.2f, 0.1f, 0.5f); + + auto window_2 = app.create_window(600, 600, "OpenGL 2!"); + comp = window_2->create_component(0, 0.5f, 100, 0.5f); + comp->set_background_color(0.2f, 0.5f, 0.3f); + auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); + label->set_background_color(0.1f, 0.2f, 0.5f); + app.run(); + } + } + catch(const std::exception& error) + { + std::cerr << error.what() << std::endl; + return EXIT_FAILURE; } - // cout << "\nStarting example 2" << endl; - // { - // uui::GlApplication app; - // auto window_1 = app.create_window(800, 600, "OpenGl!"); - // auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); - // comp->set_background_color(0.8f, 0.1f, 0.5f); - // comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); - // comp->set_background_color(0.2f, 0.1f, 0.5f); - - // auto window_2 = app.create_window(600, 600, "OpenGL 2!"); - // comp = window_2->create_component(0, 0.5f, 100, 0.5f); - // comp->set_background_color(0.2f, 0.5f, 0.3f); - // auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - // label->set_background_color(0.1f, 0.2f, 0.5f); - // app.run(); - // } - // } - // catch(const std::exception& error) - // { - // std::cerr << error.what() << std::endl; - // return EXIT_FAILURE; - // } cout << "Example done" << endl; return 0; diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index ee36f85..71a747a 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -111,7 +111,7 @@ std::shared_ptr uui::GlApplication::create_window(int width, int return window; } -std::shared_ptr uui::GlApplication::get_window(size_t index) const +std::shared_ptr uui::GlApplication::get_window(std::size_t index) const { if(index > windows.size()) throw std::runtime_error("Application doesn't have window at given index"); diff --git a/src/uui/opengl/component.cpp b/src/uui/opengl/component.cpp index f4e031e..07fb49d 100644 --- a/src/uui/opengl/component.cpp +++ b/src/uui/opengl/component.cpp @@ -6,11 +6,11 @@ #include "uui/opengl/gl_utils.hpp" uui::GlComponent::GlComponent( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const position_t width, const position_t height): - window(window), - window_index(window_index), parent(parent), parent_index(parent_index), position_x(x), position_y(y), size_width(width), - size_height(height), initialized(false) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const position_t width, const position_t height): + window(window), + window_index(window_index), parent(parent), parent_index(parent_index), position_x(x), position_y(y), + size_width(width), size_height(height), initialized(false) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlBox" << std::endl; @@ -67,8 +67,8 @@ void uui::GlComponent::init() set_vbo_data(); unsigned int indices[] = { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle }; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gl_ebo); if constexpr(uui::GlApplication::debug_mode) @@ -86,45 +86,50 @@ void uui::GlComponent::init() 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 = 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 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; float y_max = y_min - (2.0f * vert_h); // 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 + 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 }; // clang-format on glBindBuffer(GL_ARRAY_BUFFER, gl_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer( - 0, // index - 2, // size - GL_FLOAT, // type - GL_FALSE, // enable normalization, - 6 * sizeof(float), // stride - (void*)0 // offset + 0, // index + 2, // size + GL_FLOAT, // type + GL_FALSE, // enable normalization, + 6 * sizeof(float), // stride + (void*)0 // offset ); glEnableVertexAttribArray(0); glVertexAttribPointer( - 1, // index - 4, // size - GL_FLOAT, // type - GL_FALSE, // enable normalization, - 6 * sizeof(float), // stride - (void*)(2 * sizeof(float)) // offset + 1, // index + 4, // size + GL_FLOAT, // type + GL_FALSE, // enable normalization, + 6 * sizeof(float), // stride + (void*)(2 * sizeof(float)) // offset ); glEnableVertexAttribArray(1); } diff --git a/src/uui/opengl/flex.cpp b/src/uui/opengl/flex.cpp index 5cd73db..bd259a2 100644 --- a/src/uui/opengl/flex.cpp +++ b/src/uui/opengl/flex.cpp @@ -5,9 +5,9 @@ #include "uui/opengl/gl_utils.hpp" uui::GlFlex::GlFlex( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const Direction direction): - GlStack(window, window_index, parent, parent_index, x, y, direction) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const 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; @@ -18,10 +18,12 @@ uui::GlFlex::GlFlex( 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}; diff --git a/src/uui/opengl/font_manager.cpp b/src/uui/opengl/font_manager.cpp index 546ae6e..554c83d 100644 --- a/src/uui/opengl/font_manager.cpp +++ b/src/uui/opengl/font_manager.cpp @@ -125,7 +125,7 @@ void uui::GlFontManager::deinit() initialized = false; } -const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, size_t font_size) +const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& font_path, std::size_t font_size) { fonts.emplace_back(); auto& font = fonts.back(); @@ -206,7 +206,8 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& } glTexSubImage2D( - GL_TEXTURE_2D, 0, offset_x, offset_y, glyph->bitmap.width, glyph->bitmap.rows, GL_RED, GL_UNSIGNED_BYTE, glyph->bitmap.buffer); + GL_TEXTURE_2D, 0, offset_x, offset_y, glyph->bitmap.width, glyph->bitmap.rows, GL_RED, GL_UNSIGNED_BYTE, + glyph->bitmap.buffer); font.char_infos[i].advance_x = glyph->advance.x >> 6; font.char_infos[i].advance_y = glyph->advance.y >> 6; @@ -228,13 +229,13 @@ const uui::GlFontManager::Font& uui::GlFontManager::init_font(const std::string& } std::tuple uui::GlFontManager::get_text_size( - const std::string& text, uui::GlFontManager::Font& font, float scale_x, float scale_y) + const std::string& text, uui::GlFontManager::Font& font, float scale_x, float scale_y) { float width = 0.0f; float height = 0.0f; for(auto current_char: text) { - const auto& char_info = font.char_infos[static_cast(current_char)]; + const auto& char_info = font.char_infos[static_cast(current_char)]; width += char_info.advance_x * scale_x; // height = std::max(height, char_info.bitmap_top + char_info.bitmap_height * scale_y); height = std::max(height, roundf(char_info.bitmap_height * scale_y)); @@ -243,8 +244,8 @@ std::tuple uui::GlFontManager::get_text_size( } void uui::GlFontManager::render_text( - const std::string& text, uui::GlFontManager::Font& font, float x, float y, float scale_x, float scale_y, - const std::tuple& text_color) + const std::string& text, uui::GlFontManager::Font& font, float x, float y, float scale_x, float scale_y, + const std::tuple& text_color) { GLuint gl_prev_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)(&gl_prev_program)); @@ -268,7 +269,7 @@ void uui::GlFontManager::render_text( float out_base_y = y; for(auto current_char: text) { - const auto& char_info = font.char_infos[static_cast(current_char)]; + const auto& char_info = font.char_infos[static_cast(current_char)]; // Calculate the vertex and texture coordinates float char_x = out_base_x + char_info.bitmap_left * scale_x; float char_y = out_base_y - char_info.bitmap_top * scale_y; @@ -284,15 +285,16 @@ void uui::GlFontManager::render_text( continue; // clang-format off - float current_vertex[24] = { - char_x, char_y, char_info.texture_x, char_info.texture_y, // top_left - char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right - char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-right - char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right - char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-left - char_x + w, char_y + h, char_info.texture_x + char_info.bitmap_width / font.atlas_width, - char_info.texture_y + char_info.bitmap_height / font.atlas_height // bottom-right - }; + float current_vertex[24] = + { + char_x, char_y, char_info.texture_x, char_info.texture_y, // top_left + char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right + char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-right + char_x + w, char_y, char_info.texture_x + char_info.bitmap_width / font.atlas_width, char_info.texture_y, // top-right + char_x, char_y + h, char_info.texture_x, char_info.texture_y + char_info.bitmap_height / font.atlas_height, // bottom-left + char_x + w, char_y + h, char_info.texture_x + char_info.bitmap_width / font.atlas_width, + char_info.texture_y + char_info.bitmap_height / font.atlas_height // bottom-right + }; std::memcpy(vertices.data() + (char_count * 24), current_vertex, 24 * sizeof(float)); // clang-format on char_count += 1; diff --git a/src/uui/opengl/label.cpp b/src/uui/opengl/label.cpp index 7e39604..c776be1 100644 --- a/src/uui/opengl/label.cpp +++ b/src/uui/opengl/label.cpp @@ -5,16 +5,18 @@ #include "uui/opengl/gl_utils.hpp" uui::GlLabel::GlLabel( - std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, std::size_t parent_index, - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color): - GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f), - text(text), text_color(text_color) + std::shared_ptr window, std::size_t window_index, std::shared_ptr parent, + std::size_t parent_index, const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color): + GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f), + text(text), text_color(text_color) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlLabel" << std::endl; 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); + 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; @@ -22,14 +24,15 @@ uui::GlLabel::GlLabel( 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 1b2c9f1..73a24ea 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/stack.cpp @@ -5,16 +5,14 @@ #include "uui/opengl/gl_utils.hpp" uui::GlStack::GlStack( - std::shared_ptr window, std::size_t window_index, std::shared_ptr 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) + std::shared_ptr window, std::size_t window_index, std::shared_ptr 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; } @@ -26,8 +24,8 @@ std::shared_ptr uui::GlStack::create_component(const position_ glfwMakeContextCurrent(window->glfw_window); std::shared_ptr component(new uui::GlComponent( - window, window->components.size(), std::dynamic_pointer_cast(shared_from_this()), components.size(), 0, 0, width, - height)); + window, window->components.size(), std::dynamic_pointer_cast(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); @@ -36,15 +34,16 @@ std::shared_ptr uui::GlStack::create_component(const position_ return component; } -std::shared_ptr uui::GlStack::create_label(const std::string& text, const std::tuple& text_color) +std::shared_ptr uui::GlStack::create_label( + const std::string& text, const std::tuple& text_color) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); glfwMakeContextCurrent(window->glfw_window); std::shared_ptr label(new uui::GlLabel( - window, window->components.size(), std::dynamic_pointer_cast(shared_from_this()), components.size(), 0, 0, text, - text_color)); + window, window->components.size(), std::dynamic_pointer_cast(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); @@ -55,10 +54,10 @@ std::shared_ptr uui::GlStack::create_label(const std::string& text 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}; diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index 5063be7..a06aa58 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -8,8 +8,9 @@ #include "uui/opengl/label.hpp" #include "uui/shader.hpp" -static void APIENTRY -glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char* message, const void* userParam) +static void APIENTRY glDebugOutput( + GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char* message, + const void* userParam) { // ignore non-significant error/warning codes if(id == 131169 || id == 131185 || id == 131218 || id == 131204) @@ -66,7 +67,9 @@ void uui::GlWindow::glfw_resize_callback(GLFWwindow* window, int width, int heig glm::mat4 projection = glm::ortho(0.0f, static_cast(width), static_cast(height), 0.0f); glfwMakeContextCurrent(window); glUseProgram(gl_window->font_manager.gl_program); - glUniformMatrix4fv(glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv( + glGetUniformLocation(gl_window->font_manager.gl_program, "projection"), 1, GL_FALSE, + glm::value_ptr(projection)); } void uui::GlWindow::glfw_iconify_callback(GLFWwindow* window, int iconified) @@ -82,7 +85,8 @@ void uui::GlWindow::glfw_key_callback(GLFWwindow* window, int key, int scancode, } uui::GlWindow::GlWindow(uui::GlApplication* app, int width, int height, const std::string& title): - app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false), iconified(false) + app(app), initialized(false), width(width), height(height), render_needed(true), resize_needed(false), + iconified(false) { if constexpr(uui::GlApplication::debug_mode) std::cout << "Creating a GlWindow" << std::endl; @@ -206,7 +210,8 @@ void uui::GlWindow::init() font_manager.init(); glUseProgram(font_manager.gl_program); glm::mat4 projection = glm::ortho(0.0f, static_cast(width), static_cast(height), 0.0f); - glUniformMatrix4fv(glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv( + glGetUniformLocation(font_manager.gl_program, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glUseProgram(gl_program); initialized = true; @@ -249,32 +254,36 @@ void uui::GlWindow::render() } std::shared_ptr uui::GlWindow::create_component( - const position_t x, const position_t y, const position_t width, const position_t height) + const position_t x, const position_t y, const position_t width, const position_t height) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr component(new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); + std::shared_ptr component( + new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); push_child(component); component->init(); return component; } std::shared_ptr uui::GlWindow::create_label( - const position_t x, const position_t y, const std::string& text, const std::tuple& text_color) + const position_t x, const position_t y, const std::string& text, + const std::tuple& text_color) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create label while not initialized"); glfwMakeContextCurrent(glfw_window); - std::shared_ptr label(new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); + std::shared_ptr label( + new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); push_child(label); label->init(); return label; } -std::shared_ptr uui::GlWindow::create_flex(const position_t x, const position_t y, const Direction direction) +std::shared_ptr 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"); @@ -286,7 +295,8 @@ std::shared_ptr uui::GlWindow::create_flex(const position_t x, cons return flex; } -std::shared_ptr uui::GlWindow::create_stack(const position_t x, const position_t y, const Direction direction) +std::shared_ptr 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"); diff --git a/src/uui/shader.cpp b/src/uui/shader.cpp index 2ce9266..ea8e207 100644 --- a/src/uui/shader.cpp +++ b/src/uui/shader.cpp @@ -1,7 +1,7 @@ #include "uui/shader.hpp" #include - +#include std::vector uui::readFile(const std::string& filename) { @@ -10,10 +10,10 @@ std::vector uui::readFile(const std::string& filename) if(!file.is_open()) throw std::runtime_error("failed to open file!"); - size_t fileSize = (size_t)file.tellg(); + std::size_t fileSize = static_cast(file.tellg()); std::vector buffer(fileSize); file.seekg(0); - file.read(buffer.data(), fileSize); + file.read(buffer.data(), static_cast(fileSize)); file.close(); return buffer; From af734cda739b19997763f26746d4ebcaadee4415 Mon Sep 17 00:00:00 2001 From: Corentin Date: Sat, 1 Oct 2022 08:06:38 +0900 Subject: [PATCH 05/10] Add init function for cleaner usage --- README.md | 3 + include/uui/opengl/application.hpp | 17 ++-- include/uui/opengl/container.hpp | 8 +- include/uui/opengl/stack.hpp | 6 +- include/uui/opengl/uui.hpp | 6 ++ make.py | 6 +- src/test/benchmark_text.cpp | 9 +-- src/test/example_gl.cpp | 122 +++++++++++++++++------------ src/test/sample.cpp | 14 ++++ src/uui/opengl/application.cpp | 7 +- src/uui/opengl/stack.cpp | 14 ++-- src/uui/opengl/window.cpp | 35 ++++++--- 12 files changed, 156 insertions(+), 91 deletions(-) create mode 100644 README.md create mode 100644 include/uui/opengl/uui.hpp create mode 100644 src/test/sample.cpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3cd877 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# UUI + +Simple and efficient C++ GUI toolkit diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index 30c1c9f..bae5211 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -12,6 +13,7 @@ namespace uui { +template using InitFunction = const std::function&; class GlWindow; class GlApplication { @@ -29,7 +31,7 @@ public: ~GlApplication(); void run(); - std::shared_ptr create_window(int width, int height, const std::string& title); + void create_window(int width, int height, const std::string& title, InitFunction init); std::shared_ptr get_window(std::size_t index) const; private: @@ -55,13 +57,14 @@ public: GlWindow(GlWindow&&) = delete; ~GlWindow(); - std::shared_ptr create_component( - const position_t x, const position_t y, const position_t width, const position_t height); - std::shared_ptr create_label( + void create_component( + const position_t x, const position_t y, const position_t width, const position_t height, + InitFunction init); + void create_label( const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color); - std::shared_ptr create_flex(const position_t x, const position_t y, const Direction direction); - std::shared_ptr create_stack(const position_t x, const position_t y, const Direction direction); + const std::tuple& text_color, InitFunction init); + void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction init); + void create_stack(const position_t x, const position_t y, const Direction direction, InitFunction init); protected: GlApplication* app; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index 4673972..0772e6d 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -31,6 +31,11 @@ protected: virtual std::tuple get_child_position(const std::size_t child_index) const { return {0, 0}; } public: + #ifdef DEBUG + constexpr static bool debug_mode = true; + #else + constexpr static bool debug_mode = false; + #endif enum Type { WINDOW, @@ -42,7 +47,8 @@ public: virtual ~GlContainer() { - std::cout << "Destroying Container" << std::endl; + if constexpr(debug_mode) + std::cout << "Destroying Container" << std::endl; components.clear(); } }; diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index 9204a64..b50dd3d 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -15,9 +15,9 @@ class GlStack: public GlComponent, public GlContainer friend class GlWindow; public: - std::shared_ptr create_component(const position_t width, const position_t height); - std::shared_ptr create_label( - const std::string& text, const std::tuple& text_color); + void create_component(const position_t width, const position_t height, InitFunction init); + void create_label( + const std::string& text, const std::tuple& text_color, InitFunction init); protected: Direction direction; diff --git a/include/uui/opengl/uui.hpp b/include/uui/opengl/uui.hpp new file mode 100644 index 0000000..f579f95 --- /dev/null +++ b/include/uui/opengl/uui.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include "uui/opengl/application.hpp" +#include "uui/opengl/component.hpp" +#include "uui/opengl/flex.hpp" +#include "uui/opengl/label.hpp" diff --git a/make.py b/make.py index 86b943e..2a627af 100644 --- a/make.py +++ b/make.py @@ -9,7 +9,7 @@ from umake import get_hash, make class Config: CC = 'g++' - APPS = ['test/example_gl', 'test/benchmark_text'] + APPS = ['test/example_gl', 'test/benchmark_text', 'test/sample'] IGNORE_APPS = [] JOB_COUNT = int(os.cpu_count() * 0.8) @@ -19,9 +19,9 @@ class Config: SOURCE_DIR = Path('src') COMMON_FLAGS = '-std=c++17 -fno-semantic-interposition' - COMMON_DEBUG_FLAGS = '-g -DDEBUG' + COMMON_DEBUG_FLAGS = '-g -Og -DDEBUG' COMMON_RELEASE_FLAGS = '-O2 -flto' - COMPILE_FLAGS = f'-Wall -Wconversion -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`' + COMPILE_FLAGS = f'-Wall -Wextra -Wpedantic -Wconversion -I{INCLUDE_DIR} `pkg-config --cflags glfw3 vulkan gl x11 freetype2`' LINK_FLAGS = '-lpthread -ldl `pkg-config --libs glfw3 vulkan gl x11 freetype2`' PRE_COMPILE_FUNCTION = None diff --git a/src/test/benchmark_text.cpp b/src/test/benchmark_text.cpp index 1855d8c..dccd7a5 100644 --- a/src/test/benchmark_text.cpp +++ b/src/test/benchmark_text.cpp @@ -1,8 +1,6 @@ #include -#include "uui/opengl/application.hpp" -#include "uui/opengl/component.hpp" -#include "uui/opengl/label.hpp" +#include "uui/opengl/uui.hpp" using namespace std; @@ -12,8 +10,9 @@ int main() { uui::GlApplication app; // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); - for(int i = 0; i < 100; i += 1) window->create_label(4 * i, 4 * i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); + app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) { + for(int i = 0; i < 100; i += 1) window.create_label(4 * i, 4 * i, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, {}); + }); app.run(); } catch(const std::exception& error) diff --git a/src/test/example_gl.cpp b/src/test/example_gl.cpp index a94fe74..169d5dd 100644 --- a/src/test/example_gl.cpp +++ b/src/test/example_gl.cpp @@ -1,10 +1,6 @@ #include -#include "uui/config.hpp" -#include "uui/opengl/application.hpp" -#include "uui/opengl/component.hpp" -#include "uui/opengl/flex.hpp" -#include "uui/opengl/label.hpp" +#include "uui/opengl/uui.hpp" using namespace std; @@ -16,67 +12,89 @@ int main() { uui::GlApplication app; // uui::GlApplication app2; // exception : application already created - auto window = app.create_window(800, 600, "OpenGl!"); + app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) { + window.create_component(0, 100, 0.5f, 50, [](uui::GlComponent& component) { + component.set_background_color(0.8f, 0.1f, 0.5f); + }); - auto comp = window->create_component(0, 100, 0.5f, 50); - comp->set_background_color(0.8f, 0.1f, 0.5f); + window.create_component(0.25f, 50, 0.4f, 150, [](uui::GlComponent& component) { + component.set_background_color(0.3f, 0.8f, 0.4f, 0.5f); + }); - comp = window->create_component(0.25f, 50, 0.4f, 150); - comp->set_background_color(0.3f, 0.8f, 0.4f, 0.5f); + window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) { + label.set_background_color(0.1f, 0.2f, 0.5f, 0.8f); + }); - auto label = window->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - label->set_background_color(0.1f, 0.2f, 0.5f, 0.8f); + window.create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}, [](uui::GlLabel& label) { + label.set_background_color(0.5f, 0.5f, 0.1f); + }); + window.create_label(0.75f, 0.75f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.1f}, {}); - label = window->create_label(0.5f, 0.5f, "Hello World!", {1.0f, 1.0f, 1.0f, 0.5f}); - 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}); + window.create_flex(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlFlex& flex) { + flex.create_component(100, 50, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.75f, 0.25f); + }); + flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {}); + flex.create_component(100, 20, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); - 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); + window.create_flex(50, 400, uui::Direction::VERTICAL, [](uui::GlFlex& flex) { + flex.create_component(50, 100, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.75f, 0.25f); + }); + flex.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {}); + flex.create_component(100, 20, [](uui::GlComponent& component) { + component.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); + app.create_window(800, 600, "OpenGl 2", [](uui::GlWindow& window) { + window.create_stack(200, 0.6f, uui::Direction::HORIZONTAL, [](uui::GlStack& stack) { + stack.create_component(100, 50, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.75f, 0.25f); + }); + stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {}); + stack.create_component(100, 20, [](uui::GlComponent& component) { + component.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_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}); - comp = stack->create_component(100, 20); - comp->set_background_color(0.2f, 0.25f, 0.75f); + window.create_stack(50, 400, uui::Direction::VERTICAL, [](uui::GlStack& stack) { + stack.create_component(100, 50, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.75f, 0.25f); + }); + stack.create_label("test", {0.8f, 0.2f, 0.2f, 1.0f}, {}); + stack.create_component(100, 20, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.25f, 0.75f); + }); + }); + }); app.run(); } cout << "\nStarting example 2" << endl; { uui::GlApplication app; - auto window_1 = app.create_window(800, 600, "OpenGl!"); - auto comp = window_1->create_component(0.25f, 0.25f, 0.5f, 0.5f); - comp->set_background_color(0.8f, 0.1f, 0.5f); - comp = window_1->create_component(0.75f, 0.75f, 0.25f, 0.25f); - comp->set_background_color(0.2f, 0.1f, 0.5f); + app.create_window(800, 600, "OpenGl!", [](uui::GlWindow& window) { + window.create_component(0.25f, 0.25f, 0.5f, 0.5f, [](uui::GlComponent& component) { + component.set_background_color(0.8f, 0.1f, 0.5f); + }); + window.create_component(0.75f, 0.75f, 0.25f, 0.25f, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.1f, 0.5f); + }); + }); - auto window_2 = app.create_window(600, 600, "OpenGL 2!"); - comp = window_2->create_component(0, 0.5f, 100, 0.5f); - comp->set_background_color(0.2f, 0.5f, 0.3f); - auto label = window_2->create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}); - label->set_background_color(0.1f, 0.2f, 0.5f); + app.create_window(600, 600, "OpenGL 2!", [](uui::GlWindow& window) { + window.create_component(0, 0.5f, 100, 0.5f, [](uui::GlComponent& component) { + component.set_background_color(0.2f, 0.5f, 0.3f); + }); + window.create_label(50, 50, "Hello World!", {0.5f, 1.0f, 0.5f, 1.0f}, [](uui::GlLabel& label) { + label.set_background_color(0.1f, 0.2f, 0.5f); + }); + }); app.run(); } } diff --git a/src/test/sample.cpp b/src/test/sample.cpp new file mode 100644 index 0000000..345a3e6 --- /dev/null +++ b/src/test/sample.cpp @@ -0,0 +1,14 @@ +#include "uui/opengl/uui.hpp" + +int main() +{ + uui::GlApplication app; + app.create_window(800, 600, "UUI sample", [](uui::GlWindow& window) { + window.create_component( + 0, 0, 200, 100, [](uui::GlComponent& component) { component.set_background_color(1.0f, 0.f, 0.f, 0.5f); }); + window.create_label(0.5f, 0.5f, "Hello world!", {0.8f, 0.8f, 0.8f, 1.0f}, nullptr); + }); + app.run(); + + return 0; +} diff --git a/src/uui/opengl/application.cpp b/src/uui/opengl/application.cpp index 71a747a..b3b3178 100644 --- a/src/uui/opengl/application.cpp +++ b/src/uui/opengl/application.cpp @@ -97,7 +97,8 @@ void uui::GlApplication::run() std::cout << "Ending the GlApplication" << std::endl; } -std::shared_ptr uui::GlApplication::create_window(int width, int height, const std::string& title) +void uui::GlApplication::create_window( + int width, int height, const std::string& title, uui::InitFunction init) { std::shared_ptr window(new uui::GlWindow(this, width, height, title)); window->init(); @@ -108,7 +109,9 @@ std::shared_ptr uui::GlApplication::create_window(int width, int glfwMakeContextCurrent((*(windows.end() - 2))->glfw_window); glfwSwapInterval(0); } - return window; + + if(window != nullptr && init != nullptr) + init(*window); } std::shared_ptr uui::GlApplication::get_window(std::size_t index) const diff --git a/src/uui/opengl/stack.cpp b/src/uui/opengl/stack.cpp index 73a24ea..8ee1a83 100644 --- a/src/uui/opengl/stack.cpp +++ b/src/uui/opengl/stack.cpp @@ -17,7 +17,8 @@ uui::GlStack::GlStack( coord_all_relative = false; } -std::shared_ptr uui::GlStack::create_component(const position_t width, const position_t height) +void uui::GlStack::create_component( + const uui::position_t width, const uui::position_t height, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -31,11 +32,13 @@ std::shared_ptr uui::GlStack::create_component(const position_ std::tie(component->position_x, component->position_y) = get_child_position(components.size() - 1); window->components.push_back(component); - return component; + if(component != nullptr) + init(*component); } -std::shared_ptr uui::GlStack::create_label( - const std::string& text, const std::tuple& text_color) +void uui::GlStack::create_label( + const std::string& text, const std::tuple& text_color, + uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -49,7 +52,8 @@ std::shared_ptr uui::GlStack::create_label( std::tie(label->position_x, label->position_y) = get_child_position(components.size() - 1); window->components.push_back(label); - return label; + if(label != nullptr && init != nullptr) + init(*label); } std::tuple uui::GlStack::get_child_position(const std::size_t child_index) const diff --git a/src/uui/opengl/window.cpp b/src/uui/opengl/window.cpp index a06aa58..89c5e31 100644 --- a/src/uui/opengl/window.cpp +++ b/src/uui/opengl/window.cpp @@ -253,8 +253,9 @@ void uui::GlWindow::render() render_needed = false; } -std::shared_ptr uui::GlWindow::create_component( - const position_t x, const position_t y, const position_t width, const position_t height) +void uui::GlWindow::create_component( + const uui::position_t x, const uui::position_t y, const uui::position_t width, const uui::position_t height, + uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create component while not initialized"); @@ -264,12 +265,14 @@ std::shared_ptr uui::GlWindow::create_component( new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height)); push_child(component); component->init(); - return component; + + if(component != nullptr && init != nullptr) + init(*component); } -std::shared_ptr uui::GlWindow::create_label( - const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color) +void uui::GlWindow::create_label( + const uui::position_t x, const uui::position_t y, const std::string& text, + const std::tuple& text_color, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create label while not initialized"); @@ -279,11 +282,13 @@ std::shared_ptr uui::GlWindow::create_label( new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color)); push_child(label); label->init(); - return label; + + if(label != nullptr && init != nullptr) + init(*label); } -std::shared_ptr uui::GlWindow::create_flex( - const position_t x, const position_t y, const Direction direction) +void uui::GlWindow::create_flex( + const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create flex while not initialized"); @@ -292,11 +297,13 @@ std::shared_ptr uui::GlWindow::create_flex( std::shared_ptr flex(new uui::GlFlex(shared_from_this(), components.size(), x, y, direction)); push_child(flex); flex->init(); - return flex; + + if(flex != nullptr && init != nullptr) + init(*flex); } -std::shared_ptr uui::GlWindow::create_stack( - const position_t x, const position_t y, const Direction direction) +void uui::GlWindow::create_stack( + const uui::position_t x, const uui::position_t y, const uui::Direction direction, uui::InitFunction init) { if(!initialized) throw std::runtime_error("GlWindow error : cannot create stack while not initialized"); @@ -305,5 +312,7 @@ std::shared_ptr uui::GlWindow::create_stack( std::shared_ptr stack(new uui::GlStack(shared_from_this(), components.size(), x, y, direction)); push_child(stack); stack->init(); - return stack; + + if(stack != nullptr && init != nullptr) + init(*stack); } From 9ec93964ba88d23ea92a9a427eabf416348b4cfc Mon Sep 17 00:00:00 2001 From: Corentin Date: Mon, 3 Oct 2022 16:06:32 +0900 Subject: [PATCH 06/10] Reformat code to have non GL interface --- include/uui/application.hpp | 23 +++- include/uui/component.hpp | 37 ++++--- include/uui/config.hpp | 9 +- include/uui/container.hpp | 40 +++++++ include/uui/flex.hpp | 16 +++ include/uui/font_manager.hpp | 56 ++++++++++ include/uui/label.hpp | 22 ++++ include/uui/opengl/application.hpp | 74 ++----------- include/uui/opengl/component.hpp | 34 ++---- include/uui/opengl/container.hpp | 44 ++------ include/uui/opengl/flex.hpp | 6 +- include/uui/opengl/font_manager.hpp | 41 ++----- include/{ => uui/opengl}/graphic_context.hpp | 5 + include/uui/opengl/label.hpp | 10 +- include/uui/opengl/stack.hpp | 18 ++-- include/uui/opengl/uui.hpp | 6 -- include/uui/opengl/window.hpp | 54 ++++++++++ include/uui/stack.hpp | 27 +++++ include/uui/uui.hpp | 9 ++ include/uui/window.hpp | 47 ++++++-- make.py | 2 +- src/test/benchmark_text.cpp | 10 +- src/test/example_gl.cpp | 60 +++++------ src/test/sample.cpp | 22 +++- src/uui/application.cpp | 19 +++- src/uui/opengl/application.cpp | 44 +++++--- src/uui/opengl/component.cpp | 47 +++++--- src/uui/opengl/container.cpp | 37 +++++++ src/uui/opengl/flex.cpp | 19 ++-- src/uui/opengl/font_manager.cpp | 10 +- src/uui/opengl/label.cpp | 15 +-- src/uui/opengl/stack.cpp | 34 +++--- src/uui/opengl/window.cpp | 107 +++++++++++-------- umake | 2 +- 34 files changed, 644 insertions(+), 362 deletions(-) create mode 100644 include/uui/container.hpp create mode 100644 include/uui/flex.hpp create mode 100644 include/uui/font_manager.hpp create mode 100644 include/uui/label.hpp rename include/{ => uui/opengl}/graphic_context.hpp (78%) delete mode 100644 include/uui/opengl/uui.hpp create mode 100644 include/uui/stack.hpp create mode 100644 include/uui/uui.hpp create mode 100644 src/uui/opengl/container.cpp diff --git a/include/uui/application.hpp b/include/uui/application.hpp index 151ca36..17c7d2f 100644 --- a/include/uui/application.hpp +++ b/include/uui/application.hpp @@ -3,17 +3,34 @@ #include #include -#include "window.hpp" +#include "uui/config.hpp" namespace uui { +class Window; class Application { public: + enum Implementaion + { + OPENGL + }; + virtual void run() = 0; - // virtual std::weak_ptr add_window(int width, int height, const std::string& title) = 0; + virtual void create_window(int width, int height, const std::string& title, InitFunction init) = 0; + virtual std::shared_ptr get_window(std::size_t index [[maybe_unused]]) const = 0; + virtual ~Application() {}; protected: - static bool application_created; + static std::shared_ptr current_application; + + bool initialized; + + virtual void init() = 0; + virtual void deinit() = 0; + + friend std::shared_ptr create_application(Application::Implementaion implementation); }; + +std::shared_ptr create_application(Application::Implementaion implementation); } // namespace uui diff --git a/include/uui/component.hpp b/include/uui/component.hpp index 53a4c50..208720a 100644 --- a/include/uui/component.hpp +++ b/include/uui/component.hpp @@ -1,30 +1,43 @@ #pragma once #include -#include +#include #include "uui/config.hpp" +#include "uui/window.hpp" namespace uui { +class Container; class Component { public: - Component(const position_t x, const position_t y, const position_t width, const position_t height): - position_x(x), position_y(y), size_width(width), size_height(height) - { - background_color = {0.0f, 0.0f, 0.0f, 0.0f}; - } - - virtual void render() = 0; + virtual void set_background_color(float r, float g, float b, float a = 1.0f) = 0; protected: - position_t position_x; - position_t position_y; - position_t size_width; - position_t size_height; + #ifdef DEBUG + constexpr static bool debug_mode = true; + #else + constexpr static bool debug_mode = false; + #endif + + std::shared_ptr window; + std::size_t window_index; + std::shared_ptr parent; + std::size_t parent_index; + + uui::position_t position_x; + uui::position_t position_y; + uui::position_t size_width; + uui::position_t size_height; + + bool coord_all_relative; std::array background_color; + + bool initialized; + + virtual void render() = 0; }; } // namespace uui diff --git a/include/uui/config.hpp b/include/uui/config.hpp index a45f341..7e8ab70 100644 --- a/include/uui/config.hpp +++ b/include/uui/config.hpp @@ -1,16 +1,11 @@ #pragma once +#include #include -#include "graphic_context.hpp" - -namespace Config -{ -constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; -} - namespace uui { +template using InitFunction = const std::function&; typedef std::variant position_t; typedef std::variant size_t; enum Direction diff --git a/include/uui/container.hpp b/include/uui/container.hpp new file mode 100644 index 0000000..f5a3d84 --- /dev/null +++ b/include/uui/container.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include + +#include +#include +#include + +#include "uui/config.hpp" + +namespace uui +{ +class Component; +class GlComponent; +class Container +{ + friend class GlComponent; +public: + enum Type + { + WINDOW, + FLEX, + STACK + }; + + Type type; + +protected: +#ifdef DEBUG + constexpr static bool debug_mode = true; +#else + constexpr static bool debug_mode = false; +#endif + 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; +}; + +} // namespace uui diff --git a/include/uui/flex.hpp b/include/uui/flex.hpp new file mode 100644 index 0000000..1adf974 --- /dev/null +++ b/include/uui/flex.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +#include "uui/stack.hpp" + +namespace uui +{ +class Flex: virtual public Stack +{ +protected: + virtual std::tuple get_child_position(const std::size_t child_index) const = 0; +}; + +} // namespace uui diff --git a/include/uui/font_manager.hpp b/include/uui/font_manager.hpp new file mode 100644 index 0000000..e7126d6 --- /dev/null +++ b/include/uui/font_manager.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include +#include +#include + +#include +#include FT_FREETYPE_H + +namespace uui +{ +class Window; +class FontManager +{ + friend class Window; + +public: + struct character_info + { + float advance_x; // advance.x + float advance_y; // advance.y + + float bitmap_width; // bitmap.width; + float bitmap_height; // bitmap.rows; + + float bitmap_left; // bitmap_left; + float bitmap_top; // bitmap_top; + + float texture_x; // x offset of glyph in texture coordinates + float texture_y; // y offset of glyph in texture coordinates + }; + + struct Font + { + character_info char_infos[256]; + unsigned int atlas_texture; + unsigned int atlas_width; + unsigned int atlas_height; + }; + + std::vector fonts; + + virtual void render_text( + const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, + const std::tuple& text_color) = 0; + virtual std::tuple get_text_size(const std::string& text, Font& font, float scale_x, float scale_y) = 0; + +protected: + static constexpr int ATLAS_MAX_WIDTH = 1024; + + bool initialized; + + FT_Library ft; + virtual const Font& init_font(const std::string& font_path, std::size_t font_size) = 0; +}; +} // namespace uui diff --git a/include/uui/label.hpp b/include/uui/label.hpp new file mode 100644 index 0000000..2de7c22 --- /dev/null +++ b/include/uui/label.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include "uui/component.hpp" + +namespace uui +{ +class Label: virtual public Component +{ +protected: + std::size_t font_index; + + std::string text; + std::tuple text_color; + + int text_width; + int text_height; +}; + +} // namespace uui diff --git a/include/uui/opengl/application.hpp b/include/uui/opengl/application.hpp index bae5211..4860c89 100644 --- a/include/uui/opengl/application.hpp +++ b/include/uui/opengl/application.hpp @@ -1,21 +1,17 @@ #pragma once -#include #include #include -#include #include #include "graphic_context.hpp" +#include "uui/application.hpp" #include "uui/config.hpp" -#include "uui/opengl/container.hpp" -#include "uui/opengl/font_manager.hpp" namespace uui { -template using InitFunction = const std::function&; class GlWindow; -class GlApplication +class GlApplication final: virtual public Application { friend class GlWindow; @@ -28,69 +24,15 @@ public: std::vector> windows; GlApplication(); - ~GlApplication(); + virtual ~GlApplication() final; - void run(); - void create_window(int width, int height, const std::string& title, InitFunction init); - std::shared_ptr get_window(std::size_t index) const; - -private: - static GlApplication* application_pointer; -}; - -class GlComponent; -class GlFlex; -class GlLabel; -class GlStack; -class GlWindow: public GlContainer, public std::enable_shared_from_this -{ - friend class GlApplication; - friend class GlComponent; - friend class GlFlex; - friend class GlLabel; - friend class GlStack; - -public: - std::string title; - - GlWindow(const GlWindow&) = delete; - GlWindow(GlWindow&&) = delete; - ~GlWindow(); - - void create_component( - const position_t x, const position_t y, const position_t width, const position_t height, - InitFunction init); - void create_label( - const position_t x, const position_t y, const std::string& text, - const std::tuple& text_color, InitFunction init); - void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction init); - void create_stack(const position_t x, const position_t y, const Direction direction, InitFunction init); + void run() final; + void create_window(int width, int height, const std::string& title, InitFunction init) final; + std::shared_ptr get_window(std::size_t index) const final; protected: - GlApplication* app; - bool initialized; - - int width; - int height; - bool render_needed; - bool resize_needed; - bool iconified; - - GlFontManager font_manager; - - GLFWwindow* glfw_window; - GLuint gl_program; - - GlWindow(GlApplication* app, int width, int height, const std::string& title); - std::shared_ptr getptr() { return shared_from_this(); } - - void init(); - void deinit(); - void render(); - - 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); + void init() final; + void deinit() final; }; } // namespace uui diff --git a/include/uui/opengl/component.hpp b/include/uui/opengl/component.hpp index 135bbc5..223d154 100644 --- a/include/uui/opengl/component.hpp +++ b/include/uui/opengl/component.hpp @@ -4,17 +4,19 @@ #include #include "graphic_context.hpp" +#include "uui/opengl/component.hpp" +#include "uui/container.hpp" #include "uui/config.hpp" -#include "uui/opengl/container.hpp" -#include "uui/style.hpp" -#include "window.hpp" +#include "uui/opengl/window.hpp" namespace uui { class GlFlex; class GlStack; -class GlComponent: public std::enable_shared_from_this +class GlComponent: public std::enable_shared_from_this, virtual public Component { + friend class Container; + friend class GlContainer; friend class GlWindow; friend class GlFlex; friend class GlStack; @@ -22,25 +24,10 @@ class GlComponent: public std::enable_shared_from_this public: virtual ~GlComponent(); - void set_background_color(float r, float g, float b, float a = 1.0f); + void set_background_color(float r, float g, float b, float a = 1.0f) final; protected: - std::shared_ptr window; - std::size_t window_index; - std::shared_ptr parent; - std::size_t parent_index; - - position_t position_x; - position_t position_y; - size_t size_width; - size_t size_height; - bool coord_all_relative; - Style style; - - std::array background_color; - - bool initialized; - + std::shared_ptr gl_window; GLuint gl_vbo; GLuint gl_vao; GLuint gl_ebo; @@ -52,16 +39,17 @@ protected: GlComponent( std::shared_ptr window, std::size_t window_index, const position_t x, const position_t y, const position_t width, const position_t height): - GlComponent(window, window_index, window, window_index, x, y, width, height) + GlComponent(window, window_index, std::static_pointer_cast(window), window_index, x, y, width, height) {} GlComponent(const GlComponent&) = delete; GlComponent(GlComponent&&) = default; std::shared_ptr getptr() { return shared_from_this(); } + virtual void render(); + virtual void init(); virtual void set_vbo_data(); virtual void deinit(); - virtual void render(); virtual void on_resize(); }; diff --git a/include/uui/opengl/container.hpp b/include/uui/opengl/container.hpp index 0772e6d..973059e 100644 --- a/include/uui/opengl/container.hpp +++ b/include/uui/opengl/container.hpp @@ -6,51 +6,29 @@ #include #include +#include "uui/container.hpp" +#include "uui/component.hpp" + namespace uui { -class GlComponent; -class GlFlex; -class GlContainer +class GlContainer: virtual public Container { - friend class GlComponent; - friend class GlFlex; protected: GlContainer() = default; GlContainer(const GlContainer&) = default; GlContainer(GlContainer&&) = default; - virtual std::size_t push_child(std::shared_ptr child) - { - components.push_back(child); - return components.size() - 1; - } - virtual std::shared_ptr get_child(std::size_t index) const { return components.at(index); } - virtual void remove_child(std::size_t index) { components.at(index) = nullptr; }; + 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; - virtual std::tuple get_child_position(const std::size_t child_index) const { return {0, 0}; } + std::vector> components; public: - #ifdef DEBUG - constexpr static bool debug_mode = true; - #else - constexpr static bool debug_mode = false; - #endif - enum Type - { - WINDOW, - FLEX, - STACK - }; - std::vector> components; - Type type; - - virtual ~GlContainer() - { - if constexpr(debug_mode) - std::cout << "Destroying Container" << std::endl; - components.clear(); - } + ~GlContainer(); }; } // namespace uui diff --git a/include/uui/opengl/flex.hpp b/include/uui/opengl/flex.hpp index 918792a..b73fac3 100644 --- a/include/uui/opengl/flex.hpp +++ b/include/uui/opengl/flex.hpp @@ -4,14 +4,14 @@ #include #include -#include "uui/opengl/component.hpp" +#include "uui/flex.hpp" #include "uui/opengl/container.hpp" -#include "uui/opengl/label.hpp" #include "uui/opengl/stack.hpp" +#include "uui/opengl/window.hpp" namespace uui { -class GlFlex: public GlStack +class GlFlex: virtual public GlStack, virtual public Flex { friend class GlWindow; diff --git a/include/uui/opengl/font_manager.hpp b/include/uui/opengl/font_manager.hpp index 926f84c..19d1ab7 100644 --- a/include/uui/opengl/font_manager.hpp +++ b/include/uui/opengl/font_manager.hpp @@ -7,56 +7,27 @@ #include #include FT_FREETYPE_H -#include "graphic_context.hpp" + +#include "uui/font_manager.hpp" +#include "uui/opengl/graphic_context.hpp" namespace uui { class GlWindow; -class GlFontManager +class GlFontManager final: public FontManager { friend class GlWindow; public: - struct character_info - { - float advance_x; // advance.x - float advance_y; // advance.y - - float bitmap_width; // bitmap.width; - float bitmap_height; // bitmap.rows; - - float bitmap_left; // bitmap_left; - float bitmap_top; // bitmap_top; - - float texture_x; // x offset of glyph in texture coordinates - float texture_y; // y offset of glyph in texture coordinates - }; - - struct Font - { - character_info char_infos[256]; - GLuint atlas_texture; - unsigned int atlas_width; - unsigned int atlas_height; - }; - - std::vector fonts; - GlFontManager(); ~GlFontManager(); void render_text( const std::string& text, Font& font, float x, float y, float scale_x, float scale_y, - const std::tuple& text_color); - std::tuple get_text_size(const std::string& text, Font& font, float scale_x, float scale_y); + const std::tuple& text_color) final; + std::tuple get_text_size(const std::string& text, Font& font, float scale_x, float scale_y) final; private: - static constexpr int ATLAS_MAX_WIDTH = 1024; - - bool initialized; - - FT_Library ft; - GLuint gl_program; GLuint gl_vao; GLuint gl_vbo; diff --git a/include/graphic_context.hpp b/include/uui/opengl/graphic_context.hpp similarity index 78% rename from include/graphic_context.hpp rename to include/uui/opengl/graphic_context.hpp index 800a70d..5837abc 100644 --- a/include/graphic_context.hpp +++ b/include/uui/opengl/graphic_context.hpp @@ -11,3 +11,8 @@ #include #include // clang-format off + +namespace Config +{ + constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0; +} diff --git a/include/uui/opengl/label.hpp b/include/uui/opengl/label.hpp index 99f094d..bbcb296 100644 --- a/include/uui/opengl/label.hpp +++ b/include/uui/opengl/label.hpp @@ -3,6 +3,7 @@ #include #include +#include "uui/label.hpp" #include "uui/opengl/component.hpp" #include "uui/opengl/container.hpp" @@ -10,17 +11,12 @@ namespace uui { class GlFlex; class GlStack; -class GlLabel: public GlComponent +class GlLabel final: virtual public GlComponent, virtual public Label { friend class GlWindow; friend class GlFlex; friend class GlStack; - std::size_t font_index; - - float get_width() const; - float get_height() const; - protected: std::string text; std::tuple text_color; @@ -38,7 +34,7 @@ protected: GlLabel(window, window_index, window, window_index, x, y, text, text_color) {} - void render(); + void render() final; }; } // namespace uui diff --git a/include/uui/opengl/stack.hpp b/include/uui/opengl/stack.hpp index b50dd3d..97d58de 100644 --- a/include/uui/opengl/stack.hpp +++ b/include/uui/opengl/stack.hpp @@ -6,24 +6,21 @@ #include "uui/opengl/component.hpp" #include "uui/opengl/container.hpp" -#include "uui/opengl/label.hpp" +#include "uui/stack.hpp" namespace uui { -class GlStack: public GlComponent, public GlContainer +class GlStack: virtual public GlComponent, virtual public GlContainer, virtual public Stack { friend class GlWindow; public: - void create_component(const position_t width, const position_t height, InitFunction init); + void create_component(const position_t width, const position_t height, InitFunction init) override; void create_label( - const std::string& text, const std::tuple& text_color, InitFunction init); + const std::string& text, const std::tuple& text_color, + InitFunction