Active scenes can access parent app

This commit is contained in:
Nikita Lisitsa 2021-03-03 17:27:31 +03:00
parent fe60386ad7
commit 908b6cc5c9
4 changed files with 13 additions and 10 deletions

View file

@ -10,11 +10,13 @@
namespace psemek::app namespace psemek::app
{ {
struct app;
struct scene struct scene
{ {
virtual ~scene() = 0; virtual ~scene() = 0;
virtual void on_scene_enter() {} virtual void on_scene_enter(app * /* parent */) {}
virtual void on_scene_exit() {} virtual void on_scene_exit() {}
virtual void on_resize(int /* width */, int /* height */) {} virtual void on_resize(int /* width */, int /* height */) {}
@ -42,8 +44,8 @@ namespace psemek::app
struct scene_base struct scene_base
: scene : scene
{ {
void on_scene_enter() override { active_ = true; } void on_scene_enter(app * parent) override { parent_ = parent; }
void on_scene_exit() override { active_ = false; } void on_scene_exit() override { parent_ = nullptr; }
void on_resize(int width, int height) override { width_ = width; height_ = height; } void on_resize(int width, int height) override { width_ = width; height_ = height; }
@ -57,7 +59,8 @@ namespace psemek::app
void on_key_down(SDL_Keycode key) override { keys_.insert(key); } void on_key_down(SDL_Keycode key) override { keys_.insert(key); }
void on_key_up(SDL_Keycode key) override { keys_.erase(key); } void on_key_up(SDL_Keycode key) override { keys_.erase(key); }
bool active() const { return active_; } bool active() const { return parent_ != nullptr; }
app * parent() const { return parent_; }
bool is_left_button_down() const { return left_button_down_; } bool is_left_button_down() const { return left_button_down_; }
bool is_middle_button_down() const { return middle_button_down_; } bool is_middle_button_down() const { return middle_button_down_; }
@ -71,7 +74,7 @@ namespace psemek::app
int height() const { return height_; } int height() const { return height_; }
private: private:
bool active_ = false; app * parent_ = nullptr;
int width_ = 0; int width_ = 0;
int height_ = 0; int height_ = 0;

View file

@ -12,7 +12,7 @@ namespace psemek::app
{ {
ui_scene(ui::controller & controller); ui_scene(ui::controller & controller);
void on_scene_enter() override; void on_scene_enter(app * parent) override;
void on_scene_exit() override; void on_scene_exit() override;
void on_resize(int width, int height) override; void on_resize(int width, int height) override;

View file

@ -200,7 +200,7 @@ namespace psemek::app
SDL_ShowWindow(impl().window); SDL_ShowWindow(impl().window);
impl().running = true; impl().running = true;
if (impl().current_scene == nullptr) if (impl().current_scene == nullptr)
on_scene_enter(); on_scene_enter(this);
while (running()) while (running())
{ {
poll_events(); poll_events();
@ -230,7 +230,7 @@ namespace psemek::app
impl().had_initial_resize = false; impl().had_initial_resize = false;
std::swap(s, impl().current_scene); std::swap(s, impl().current_scene);
impl().get_scene()->on_scene_enter(); impl().get_scene()->on_scene_enter(this);
return s; return s;
} }

View file

@ -7,9 +7,9 @@ namespace psemek::app
: controller_(controller) : controller_(controller)
{} {}
void ui_scene::on_scene_enter() void ui_scene::on_scene_enter(app * parent)
{ {
scene_base::on_scene_enter(); scene_base::on_scene_enter(parent);
controller_.set_root(ui_); controller_.set_root(ui_);
} }