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,8 +1,6 @@
#include <iostream>
#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)

View file

@ -1,10 +1,6 @@
#include <iostream>
#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();
}
}

14
src/test/sample.cpp Normal file
View file

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

View file

@ -97,7 +97,8 @@ void uui::GlApplication::run()
std::cout << "Ending the GlApplication" << std::endl;
}
std::shared_ptr<uui::GlWindow> 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<GlWindow> init)
{
std::shared_ptr<uui::GlWindow> window(new uui::GlWindow(this, width, height, title));
window->init();
@ -108,7 +109,9 @@ std::shared_ptr<uui::GlWindow> 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::GlWindow> uui::GlApplication::get_window(std::size_t index) const

View file

@ -17,7 +17,8 @@ uui::GlStack::GlStack(
coord_all_relative = false;
}
std::shared_ptr<uui::GlComponent> 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<uui::GlComponent> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
@ -31,11 +32,13 @@ std::shared_ptr<uui::GlComponent> 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::GlLabel> uui::GlStack::create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color)
void uui::GlStack::create_label(
const std::string& text, const std::tuple<float, float, float, float>& text_color,
uui::InitFunction<uui::GlLabel> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
@ -49,7 +52,8 @@ std::shared_ptr<uui::GlLabel> 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<int, int> uui::GlStack::get_child_position(const std::size_t child_index) const

View file

@ -253,8 +253,9 @@ void uui::GlWindow::render()
render_needed = false;
}
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)
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<GlComponent> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create component while not initialized");
@ -264,12 +265,14 @@ std::shared_ptr<uui::GlComponent> 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::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)
void uui::GlWindow::create_label(
const uui::position_t x, const uui::position_t y, const std::string& text,
const std::tuple<float, float, float, float>& text_color, uui::InitFunction<GlLabel> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create label while not initialized");
@ -279,11 +282,13 @@ std::shared_ptr<uui::GlLabel> 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::GlFlex> 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<GlFlex> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create flex while not initialized");
@ -292,11 +297,13 @@ std::shared_ptr<uui::GlFlex> uui::GlWindow::create_flex(
std::shared_ptr<uui::GlFlex> 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::GlStack> 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<GlStack> init)
{
if(!initialized)
throw std::runtime_error("GlWindow error : cannot create stack while not initialized");
@ -305,5 +312,7 @@ std::shared_ptr<uui::GlStack> uui::GlWindow::create_stack(
std::shared_ptr<uui::GlStack> 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);
}