Add init function for cleaner usage

This commit is contained in:
Corentin 2022-10-01 08:06:38 +09:00
commit af734cda73
12 changed files with 156 additions and 91 deletions

View file

@ -1,5 +1,6 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <variant>
@ -12,6 +13,7 @@
namespace uui
{
template<typename T> using InitFunction = const std::function<void(T&)>&;
class GlWindow;
class GlApplication
{
@ -29,7 +31,7 @@ public:
~GlApplication();
void run();
std::shared_ptr<GlWindow> create_window(int width, int height, const std::string& title);
void create_window(int width, int height, const std::string& title, InitFunction<GlWindow> init);
std::shared_ptr<GlWindow> get_window(std::size_t index) const;
private:
@ -55,13 +57,14 @@ public:
GlWindow(GlWindow&&) = delete;
~GlWindow();
std::shared_ptr<GlComponent> create_component(
const position_t x, const position_t y, const position_t width, const position_t height);
std::shared_ptr<GlLabel> create_label(
void create_component(
const position_t x, const position_t y, const position_t width, const position_t height,
InitFunction<GlComponent> init);
void create_label(
const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color);
std::shared_ptr<GlFlex> create_flex(const position_t x, const position_t y, const Direction direction);
std::shared_ptr<GlStack> create_stack(const position_t x, const position_t y, const Direction direction);
const std::tuple<float, float, float, float>& text_color, InitFunction<GlLabel> init);
void create_flex(const position_t x, const position_t y, const Direction direction, InitFunction<GlFlex> init);
void create_stack(const position_t x, const position_t y, const Direction direction, InitFunction<GlStack> init);
protected:
GlApplication* app;

View file

@ -31,6 +31,11 @@ protected:
virtual std::tuple<int, int> 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();
}
};

View file

@ -15,9 +15,9 @@ class GlStack: public GlComponent, public GlContainer
friend class GlWindow;
public:
std::shared_ptr<GlComponent> create_component(const position_t width, const position_t height);
std::shared_ptr<GlLabel> create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color);
void create_component(const position_t width, const position_t height, InitFunction<GlComponent> init);
void create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color, InitFunction<GlLabel> init);
protected:
Direction direction;

View file

@ -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"