121 lines
3.3 KiB
C++
121 lines
3.3 KiB
C++
#include <psemek/app/app.hpp>
|
|
#include <psemek/app/main.hpp>
|
|
|
|
#include <psemek/gfx/gl.hpp>
|
|
|
|
#include <psemek/ui/controller.hpp>
|
|
#include <psemek/ui/default_element_factory.hpp>
|
|
#include <psemek/ui/font.hpp>
|
|
#include <psemek/ui/frame.hpp>
|
|
#include <psemek/ui/grid_layout.hpp>
|
|
#include <psemek/ui/button.hpp>
|
|
|
|
#include <psemek/async/event_loop.hpp>
|
|
|
|
#include <psemek/util/recursive.hpp>
|
|
#include <psemek/util/to_string.hpp>
|
|
|
|
#include <psemek/geom/camera.hpp>
|
|
|
|
using namespace psemek;
|
|
|
|
static std::string const lorem_ipsum =
|
|
R"(Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.)";
|
|
|
|
static std::string const to_be =
|
|
R"(To be, or not to be, that is the question:
|
|
Whether 'tis nobler in the mind to suffer
|
|
The slings and arrows of outrageous fortune,
|
|
Or to take Arms against a Sea of troubles,
|
|
And by opposing end them: to die, to sleep;
|
|
No more; and by a sleep, to say we end
|
|
The heart-ache, and the thousand natural shocks
|
|
That Flesh is heir to? 'Tis a consummation)";
|
|
|
|
struct ui_example
|
|
: app::app
|
|
{
|
|
ui_example()
|
|
: app("UI example", 1)
|
|
, ui_controller(&loop)
|
|
{
|
|
auto style = std::make_shared<ui::style>();
|
|
style->font = ui::make_default_9x12_font();
|
|
style->text_scale = 2;
|
|
|
|
auto screen = element_factory.make_screen();
|
|
screen->set_style(style);
|
|
|
|
auto window = element_factory.make_window("Settings");
|
|
auto frame = element_factory.make_frame();
|
|
{
|
|
auto style = std::make_shared<ui::style>();
|
|
style->border_width = 0;
|
|
style->shadow_offset = {0, 0};
|
|
frame->set_style(style);
|
|
}
|
|
auto label = element_factory.make_label(to_be);
|
|
label->set_overflow(ui::label::overflow_mode::ellipsis);
|
|
label->set_multiline(ui::label::multiline_mode::minimize_lines);
|
|
frame->set_child(label);
|
|
window->set_child(frame);
|
|
screen->add_child(window, ui::screen::x_policy::center, ui::screen::y_policy::center);
|
|
|
|
ui_controller.set_root(std::move(screen));
|
|
}
|
|
|
|
void on_resize(int width, int height) override
|
|
{
|
|
app::on_resize(width, height);
|
|
ui_controller.reshape({{{0, width}, {0, height}}});
|
|
}
|
|
|
|
void on_mouse_move(int x, int y, int dx, int dy) override
|
|
{
|
|
app::on_mouse_move(x, y, dx, dy);
|
|
ui_controller.event(ui::mouse_move{{x, y}});
|
|
}
|
|
|
|
void on_left_button_down() override
|
|
{
|
|
app::on_left_button_down();
|
|
ui_controller.event(ui::mouse_click{ui::mouse_button::left, true});
|
|
}
|
|
|
|
void on_left_button_up() override
|
|
{
|
|
app::on_left_button_up();
|
|
ui_controller.event(ui::mouse_click{ui::mouse_button::left, false});
|
|
}
|
|
|
|
void update() override;
|
|
|
|
void present() override;
|
|
|
|
async::event_loop loop;
|
|
ui::controller ui_controller;
|
|
ui::default_element_factory element_factory;
|
|
};
|
|
|
|
void ui_example::update()
|
|
{
|
|
loop.pump();
|
|
}
|
|
|
|
void ui_example::present()
|
|
{
|
|
gl::ClearColor(0.8f, 0.8f, 0.8f, 1.f);
|
|
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
|
|
|
|
gfx::render_target rt;
|
|
rt.framebuffer = &gfx::framebuffer::null();
|
|
rt.draw_buffer = gl::BACK_LEFT;
|
|
rt.viewport = {{{0, width()}, {0, height()}}};
|
|
|
|
ui_controller.render(rt);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
return app::main<ui_example>();
|
|
}
|