Make ui::contoller accept an external event loop
This commit is contained in:
parent
8c04087a44
commit
ec422cf48d
3 changed files with 27 additions and 7 deletions
|
|
@ -7,6 +7,8 @@
|
||||||
#include <psemek/ui/default_element_factory.hpp>
|
#include <psemek/ui/default_element_factory.hpp>
|
||||||
#include <psemek/ui/font.hpp>
|
#include <psemek/ui/font.hpp>
|
||||||
|
|
||||||
|
#include <psemek/async/event_loop.hpp>
|
||||||
|
|
||||||
#include <psemek/geom/camera.hpp>
|
#include <psemek/geom/camera.hpp>
|
||||||
|
|
||||||
using namespace psemek;
|
using namespace psemek;
|
||||||
|
|
@ -16,6 +18,7 @@ struct ui_example
|
||||||
{
|
{
|
||||||
ui_example()
|
ui_example()
|
||||||
: app("UI example", 1)
|
: app("UI example", 1)
|
||||||
|
, ui_controller(&loop)
|
||||||
{
|
{
|
||||||
auto style = std::make_shared<ui::style>();
|
auto style = std::make_shared<ui::style>();
|
||||||
style->font = ui::make_default_9x12_font();
|
style->font = ui::make_default_9x12_font();
|
||||||
|
|
@ -23,7 +26,11 @@ struct ui_example
|
||||||
element_factory.set_style(style);
|
element_factory.set_style(style);
|
||||||
|
|
||||||
auto screen = element_factory.make_screen();
|
auto screen = element_factory.make_screen();
|
||||||
screen->add(element_factory.make_button("Test"), ui::screen::x_policy::center, ui::screen::y_policy::center);
|
auto button = element_factory.make_button("Test");
|
||||||
|
button->on_click([button = button.get()]{
|
||||||
|
button->label()->set_text(std::string(button->label()->text()) + "0");
|
||||||
|
});
|
||||||
|
screen->add(button, ui::screen::x_policy::center, ui::screen::y_policy::center);
|
||||||
|
|
||||||
ui_controller.set_root(std::move(screen));
|
ui_controller.set_root(std::move(screen));
|
||||||
}
|
}
|
||||||
|
|
@ -52,12 +59,20 @@ struct ui_example
|
||||||
ui_controller.event(ui::mouse_click{ui::mouse_button::left, false});
|
ui_controller.event(ui::mouse_click{ui::mouse_button::left, false});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void update() override;
|
||||||
|
|
||||||
void present() override;
|
void present() override;
|
||||||
|
|
||||||
|
async::event_loop loop;
|
||||||
ui::controller ui_controller;
|
ui::controller ui_controller;
|
||||||
ui::default_element_factory element_factory;
|
ui::default_element_factory element_factory;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void ui_example::update()
|
||||||
|
{
|
||||||
|
loop.pump();
|
||||||
|
}
|
||||||
|
|
||||||
void ui_example::present()
|
void ui_example::present()
|
||||||
{
|
{
|
||||||
gl::ClearColor(0.8f, 0.8f, 0.8f, 1.f);
|
gl::ClearColor(0.8f, 0.8f, 0.8f, 1.f);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ namespace psemek::ui
|
||||||
|
|
||||||
struct controller
|
struct controller
|
||||||
{
|
{
|
||||||
controller();
|
controller(async::executor * loop);
|
||||||
~controller();
|
~controller();
|
||||||
|
|
||||||
std::shared_ptr<element> set_root(std::shared_ptr<element> r);
|
std::shared_ptr<element> set_root(std::shared_ptr<element> r);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
#include <psemek/ui/controller.hpp>
|
#include <psemek/ui/controller.hpp>
|
||||||
|
|
||||||
#include <psemek/ui/painter_impl.hpp>
|
#include <psemek/ui/painter_impl.hpp>
|
||||||
#include <psemek/async/event_loop.hpp>
|
|
||||||
#include <psemek/gfx/gl.hpp>
|
#include <psemek/gfx/gl.hpp>
|
||||||
#include <psemek/util/recursive.hpp>
|
#include <psemek/util/recursive.hpp>
|
||||||
#include <psemek/geom/camera.hpp>
|
#include <psemek/geom/camera.hpp>
|
||||||
|
|
@ -11,15 +10,21 @@ namespace psemek::ui
|
||||||
|
|
||||||
struct controller::impl
|
struct controller::impl
|
||||||
{
|
{
|
||||||
|
async::executor * loop;
|
||||||
painter_impl painter;
|
painter_impl painter;
|
||||||
std::shared_ptr<element> root;
|
std::shared_ptr<element> root;
|
||||||
async::event_loop loop;
|
|
||||||
geom::box<float, 2> shape{{{0.f, 0.f}, {0.f, 0.f}}};
|
geom::box<float, 2> shape{{{0.f, 0.f}, {0.f, 0.f}}};
|
||||||
|
|
||||||
|
impl(async::executor * loop);
|
||||||
|
|
||||||
template <typename E>
|
template <typename E>
|
||||||
bool event(E const & e);
|
bool event(E const & e);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
controller::impl::impl(async::executor * loop)
|
||||||
|
: loop(loop)
|
||||||
|
{}
|
||||||
|
|
||||||
template <typename E>
|
template <typename E>
|
||||||
bool controller::impl::event(E const & e)
|
bool controller::impl::event(E const & e)
|
||||||
{
|
{
|
||||||
|
|
@ -36,8 +41,8 @@ namespace psemek::ui
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
controller::controller()
|
controller::controller(async::executor * loop)
|
||||||
: pimpl_{make_impl()}
|
: pimpl_{make_impl(loop)}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
controller::~controller() = default;
|
controller::~controller() = default;
|
||||||
|
|
@ -49,7 +54,7 @@ namespace psemek::ui
|
||||||
if (old) old->set_loop(nullptr);
|
if (old) old->set_loop(nullptr);
|
||||||
if (impl().root)
|
if (impl().root)
|
||||||
{
|
{
|
||||||
impl().root->set_loop(&impl().loop);
|
impl().root->set_loop(impl().loop);
|
||||||
impl().root->reshape(impl().shape);
|
impl().root->reshape(impl().shape);
|
||||||
}
|
}
|
||||||
return old;
|
return old;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue