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