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

2
.ccls
View file

@ -1,4 +1,4 @@
clang++
clangd
-std=c++17
-DDEBUG
-Wall

View file

@ -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
UseTab: Always

View file

@ -1,6 +1,8 @@
* layout
* button
* layout (constraint)
* lambda constructor
* scroll

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

View file

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

View file

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

View file

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

View file

@ -111,7 +111,7 @@ std::shared_ptr<uui::GlWindow> uui::GlApplication::create_window(int width, int
return window;
}
std::shared_ptr<uui::GlWindow> uui::GlApplication::get_window(size_t index) const
std::shared_ptr<uui::GlWindow> 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");

View file

@ -6,11 +6,11 @@
#include "uui/opengl/gl_utils.hpp"
uui::GlComponent::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):
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<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):
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<float>(std::get<0>(position_x)) / static_cast<float>(window->width);
float vert_y =
position_y.index() == 1 ? std::get<1>(position_y) : static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height);
float vert_w =
size_width.index() == 1 ? std::get<1>(size_width) : static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width);
float vert_h = size_height.index() == 1 ? std::get<1>(size_height)
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(window->height);
float vert_x = position_x.index() == 1
? std::get<1>(position_x)
: static_cast<float>(std::get<0>(position_x)) / static_cast<float>(window->width);
float vert_y = position_y.index() == 1
? std::get<1>(position_y)
: static_cast<float>(std::get<0>(position_y)) / static_cast<float>(window->height);
float vert_w = size_width.index() == 1
? std::get<1>(size_width)
: static_cast<float>(std::get<0>(size_width)) / static_cast<float>(window->width);
float vert_h = size_height.index() == 1
? std::get<1>(size_height)
: static_cast<float>(std::get<0>(size_height)) / static_cast<float>(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);
}

View file

@ -5,9 +5,9 @@
#include "uui/opengl/gl_utils.hpp"
uui::GlFlex::GlFlex(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> 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<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> 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<int, int> 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<int>(std::get<1>(position_x) * static_cast<float>(window->width));
const int initial_y =
position_y.index() == 0 ? std::get<0>(position_y) : static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
const int initial_x = position_x.index() == 0
? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
const int initial_y = position_y.index() == 0
? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
if(child_index == 0)
return {initial_x, initial_y};

View file

@ -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<int, int> 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<size_t>(current_char)];
const auto& char_info = font.char_infos[static_cast<std::size_t>(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<int, int> 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<float, float, float, float>& text_color)
const std::string& text, uui::GlFontManager::Font& font, float x, float y, float scale_x, float scale_y,
const std::tuple<float, float, float, float>& 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<size_t>(current_char)];
const auto& char_info = font.char_infos[static_cast<std::size_t>(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;

View file

@ -5,16 +5,18 @@
#include "uui/opengl/gl_utils.hpp"
uui::GlLabel::GlLabel(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::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):
GlComponent(window, window_index, parent, parent_index, x, y, 0.0f, 0.0f),
text(text), text_color(text_color)
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::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):
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<float>(window->width) : static_cast<float>(std::get<0>(position_x));
float text_y =
position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height) : static_cast<float>(std::get<0>(position_y));
float text_x = position_x.index() == 1 ? std::get<1>(position_x) * static_cast<float>(window->width)
: static_cast<float>(std::get<0>(position_x));
float text_y = position_y.index() == 1 ? std::get<1>(position_y) * static_cast<float>(window->height)
: static_cast<float>(std::get<0>(position_y));
text_y += static_cast<float>(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);
}

View file

@ -5,16 +5,14 @@
#include "uui/opengl/gl_utils.hpp"
uui::GlStack::GlStack(
std::shared_ptr<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> 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<uui::GlWindow> window, std::size_t window_index, std::shared_ptr<uui::GlContainer> 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::GlComponent> uui::GlStack::create_component(const position_
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlComponent> component(new uui::GlComponent(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, width,
height));
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(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::GlComponent> uui::GlStack::create_component(const position_
return component;
}
std::shared_ptr<uui::GlLabel> uui::GlStack::create_label(const std::string& text, const std::tuple<float, float, float, float>& text_color)
std::shared_ptr<uui::GlLabel> uui::GlStack::create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
glfwMakeContextCurrent(window->glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(shared_from_this()), components.size(), 0, 0, text,
text_color));
window, window->components.size(), std::dynamic_pointer_cast<uui::GlContainer>(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::GlLabel> uui::GlStack::create_label(const std::string& text
std::tuple<int, int> 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<int>(std::get<1>(position_x) * static_cast<float>(window->width));
int y =
position_y.index() == 0 ? std::get<0>(position_y) : static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
int x = position_x.index() == 0 ? std::get<0>(position_x)
: static_cast<int>(std::get<1>(position_x) * static_cast<float>(window->width));
int y = position_y.index() == 0 ? std::get<0>(position_y)
: static_cast<int>(std::get<1>(position_y) * static_cast<float>(window->height));
if(child_index == 0)
return {x, y};

View file

@ -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<float>(width), static_cast<float>(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<float>(width), static_cast<float>(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::GlComponent> 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<uui::GlComponent> component(new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height));
std::shared_ptr<uui::GlComponent> component(
new uui::GlComponent(shared_from_this(), components.size(), x, y, width, height));
push_child(component);
component->init();
return component;
}
std::shared_ptr<uui::GlLabel> uui::GlWindow::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)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create label while not initialized");
glfwMakeContextCurrent(glfw_window);
std::shared_ptr<uui::GlLabel> label(new uui::GlLabel(shared_from_this(), components.size(), x, y, text, text_color));
std::shared_ptr<uui::GlLabel> 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::GlFlex> uui::GlWindow::create_flex(const position_t x, const position_t y, const Direction direction)
std::shared_ptr<uui::GlFlex> 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::GlFlex> uui::GlWindow::create_flex(const position_t x, cons
return flex;
}
std::shared_ptr<uui::GlStack> uui::GlWindow::create_stack(const position_t x, const position_t y, const Direction direction)
std::shared_ptr<uui::GlStack> 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");

View file

@ -1,7 +1,7 @@
#include "uui/shader.hpp"
#include <fstream>
#include <ios>
std::vector<char> uui::readFile(const std::string& filename)
{
@ -10,10 +10,10 @@ std::vector<char> 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<std::size_t>(file.tellg());
std::vector<char> buffer(fileSize);
file.seekg(0);
file.read(buffer.data(), fileSize);
file.read(buffer.data(), static_cast<std::streamsize>(fileSize));
file.close();
return buffer;