Fix code format and add Style object (unused)

This commit is contained in:
Corentin 2022-10-01 05:03:16 +09:00
commit d15b25dba6
25 changed files with 320 additions and 201 deletions

View file

@ -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};
}

View file

@ -12,6 +12,7 @@ constexpr unsigned int FONT_TEXTURE_UNIT = GL_TEXTURE0;
namespace uui
{
typedef std::variant<int, float> position_t;
typedef std::variant<int, float> size_t;
enum Direction
{
HORIZONTAL,

View file

@ -30,7 +30,7 @@ public:
void run();
std::shared_ptr<GlWindow> create_window(int width, int height, const std::string& title);
std::shared_ptr<GlWindow> get_window(size_t index) const;
std::shared_ptr<GlWindow> get_window(std::size_t index) const;
private:
static GlApplication* application_pointer;
@ -55,9 +55,11 @@ 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<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(
const position_t x, const position_t y, const std::string& text, const std::tuple<float, float, float, float>& text_color);
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);

View file

@ -0,0 +1,29 @@
#pragma once
#include <memory>
#include <string>
#include <tuple>
#include "uui/opengl/component.hpp"
namespace uui
{
class GlButton: public GlComponent
{
friend class GlWindow;
protected:
GlButton(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction);
GlButton(
std::shared_ptr<GlWindow> 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<int, int> get_child_position(const std::size_t child_index) const;
};
} // namespace uui

View file

@ -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<GlComponent>
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<GlWindow> window;
size_t window_index;
std::size_t window_index;
std::shared_ptr<GlContainer> 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<float, 4> background_color;
@ -44,12 +46,13 @@ protected:
GLuint gl_ebo;
GlComponent(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> 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<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> 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<GlWindow> 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<GlWindow> 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;

View file

@ -2,6 +2,7 @@
#include <cstdlib>
#include <iostream>
#include <memory>
#include <vector>
@ -39,7 +40,11 @@ public:
std::vector<std::shared_ptr<GlComponent>> components;
Type type;
~GlContainer() { components.clear(); }
virtual ~GlContainer()
{
std::cout << "Destroying Container" << std::endl;
components.clear();
}
};
} // namespace uui

View file

@ -17,10 +17,12 @@ class GlFlex: public GlStack
protected:
GlFlex(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction);
GlFlex(std::shared_ptr<GlWindow> 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<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction);
GlFlex(
std::shared_ptr<GlWindow> 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<int, int> get_child_position(const std::size_t child_index) const override;

View file

@ -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<float, float, float, float>& text_color);
const std::string& text, Font& font, float x, float y, float scale_x, float scale_y,
const std::tuple<float, float, float, float>& text_color);
std::tuple<int, int> 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

View file

@ -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<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const std::string& text, const std::tuple<float, float, float, float>& text_color);
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color);
GlLabel(
std::shared_ptr<GlWindow> window, std::size_t window_index, const position_t x, const position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color):
GlLabel(window, window_index, window, window_index, x, y, text, text_color)
std::shared_ptr<GlWindow> window, std::size_t window_index, const position_t x, const position_t y,
const std::string& text, const std::tuple<float, float, float, float>& text_color):
GlLabel(window, window_index, window, window_index, x, y, text, text_color)
{}
void render();

View file

@ -16,7 +16,8 @@ class GlStack: public GlComponent, public GlContainer
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);
std::shared_ptr<GlLabel> create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color);
protected:
Direction direction;
@ -24,10 +25,12 @@ protected:
int content_height;
GlStack(
std::shared_ptr<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent, std::size_t parent_index,
const position_t x, const position_t y, const Direction direction);
GlStack(std::shared_ptr<GlWindow> 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<GlWindow> window, std::size_t window_index, std::shared_ptr<GlContainer> parent,
std::size_t parent_index, const position_t x, const position_t y, const Direction direction);
GlStack(
std::shared_ptr<GlWindow> 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;

46
include/uui/style.hpp Normal file
View file

@ -0,0 +1,46 @@
#pragma once
#include <memory>
#include <optional>
#include <tuple>
#include "config.hpp"
namespace uui
{
struct Style
{
enum Position
{
RELATIVE,
ABSOLUTE
};
enum Alignment
{
START,
CENTER,
END
};
struct AbsolutePostion
{
std::optional<position_t> left;
std::optional<position_t> right;
std::optional<position_t> top;
std::optional<position_t> bottom;
};
// Layout
Position position;
Alignment horizontal_alignment;
Alignment vertical_alignment;
std::optional<uui::size_t> width;
std::optional<uui::size_t> height;
std::optional<uui::size_t> margin;
std::optional<uui::size_t> padding;
// For absolute positioning
std::unique_ptr<AbsolutePostion> absolute_position;
// Color
std::optional<std::tuple<float, float, float, float>> background_color;
};
} // namespace uui