115 lines
2.6 KiB
C++
115 lines
2.6 KiB
C++
#include <psemek/app/ui_scene.hpp>
|
|
|
|
#include <psemek/log/log.hpp>
|
|
|
|
namespace psemek::app
|
|
{
|
|
|
|
ui_scene::ui_scene(ui::controller & controller)
|
|
: controller_(controller)
|
|
{}
|
|
|
|
void ui_scene::on_scene_enter(app * parent)
|
|
{
|
|
scene_base::on_scene_enter(parent);
|
|
controller_.set_root(ui_);
|
|
update_clock_.restart();
|
|
}
|
|
|
|
void ui_scene::on_scene_exit()
|
|
{
|
|
scene_base::on_scene_exit();
|
|
controller_.set_root(nullptr);
|
|
}
|
|
|
|
void ui_scene::on_resize(int width, int height)
|
|
{
|
|
scene_base::on_resize(width, height);
|
|
controller_.reshape({{{0.f, width}, {0.f, height}}});
|
|
}
|
|
|
|
void ui_scene::on_mouse_move(int x, int y, int dx, int dy)
|
|
{
|
|
scene_base::on_mouse_move(x, y, dx, dy);
|
|
controller_.event(ui::mouse_move{{x, y}});
|
|
}
|
|
|
|
void ui_scene::on_mouse_wheel(int delta)
|
|
{
|
|
scene_base::on_mouse_wheel(delta);
|
|
controller_.event(ui::mouse_wheel{delta});
|
|
}
|
|
|
|
void ui_scene::on_left_button_down()
|
|
{
|
|
scene_base::on_left_button_down();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::left, true});
|
|
}
|
|
|
|
void ui_scene::on_left_button_up()
|
|
{
|
|
scene_base::on_left_button_up();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::left, false});
|
|
}
|
|
|
|
void ui_scene::on_middle_button_down()
|
|
{
|
|
scene_base::on_left_button_down();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::middle, true});
|
|
}
|
|
|
|
void ui_scene::on_middle_button_up()
|
|
{
|
|
scene_base::on_left_button_down();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::middle, false});
|
|
}
|
|
|
|
void ui_scene::on_right_button_down()
|
|
{
|
|
scene_base::on_right_button_down();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::right, true});
|
|
}
|
|
|
|
void ui_scene::on_right_button_up()
|
|
{
|
|
scene_base::on_right_button_down();
|
|
controller_.event(ui::mouse_click{ui::mouse_button::right, false});
|
|
}
|
|
|
|
void ui_scene::on_key_down(SDL_Keycode key)
|
|
{
|
|
scene_base::on_key_down(key);
|
|
controller_.event(ui::key_press{key, true});
|
|
}
|
|
|
|
void ui_scene::on_key_up(SDL_Keycode key)
|
|
{
|
|
scene_base::on_key_up(key);
|
|
controller_.event(ui::key_press{key, false});
|
|
}
|
|
|
|
void ui_scene::update()
|
|
{
|
|
controller_.update(update_clock_.restart().count());
|
|
std::size_t events = controller_.loop()->pump(max_events_per_frame_);
|
|
if (max_events_per_frame_ && events == *max_events_per_frame_)
|
|
log::warning() << "UI event loop had more than " << *max_events_per_frame_ << " events, delaying others";
|
|
}
|
|
|
|
void ui_scene::present()
|
|
{
|
|
gfx::render_target rt;
|
|
rt.viewport = {{{0, width()}, {0, height()}}};
|
|
rt.framebuffer = &gfx::framebuffer::null();
|
|
rt.draw_buffer = gl::BACK_LEFT;
|
|
controller_.render(rt);
|
|
}
|
|
|
|
void ui_scene::set_ui(std::shared_ptr<ui::element> ui)
|
|
{
|
|
ui_ = std::move(ui);
|
|
if (active())
|
|
controller_.set_root(ui_);
|
|
}
|
|
|
|
}
|