2023-07-31 07:27:01 +00:00
|
|
|
#include "screen.h"
|
|
|
|
|
2023-07-31 11:39:46 +00:00
|
|
|
// standard library includes
|
|
|
|
#include <cassert>
|
|
|
|
#ifndef NDEBUG
|
|
|
|
#include <iostream>
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
|
|
// local includes
|
|
|
|
#include "screen_test.h"
|
|
|
|
|
2023-07-31 07:27:01 +00:00
|
|
|
Screen::Screen(std::weak_ptr<ScreenStack> stack) : stack(stack) {}
|
|
|
|
|
2023-07-31 11:39:46 +00:00
|
|
|
ScreenStack::PendingAction::PendingAction() : screen(), action(Action::NOP) {}
|
|
|
|
|
|
|
|
ScreenStack::PendingAction::PendingAction(Action action)
|
|
|
|
: screen(), action(action) {}
|
|
|
|
|
|
|
|
ScreenStack::PendingAction::PendingAction(Screen::Ptr &&screen)
|
|
|
|
: screen(std::forward<Screen::Ptr>(screen)), action(Action::PUSH_SCREEN) {}
|
|
|
|
|
2023-07-31 07:27:01 +00:00
|
|
|
ScreenStack::Ptr ScreenStack::new_instance() {
|
|
|
|
std::shared_ptr<ScreenStack> ptr =
|
|
|
|
std::shared_ptr<ScreenStack>(new ScreenStack{});
|
|
|
|
ptr->self_weak = ptr;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenStack::update(float dt) {
|
2023-07-31 11:39:46 +00:00
|
|
|
handle_pending_actions();
|
|
|
|
|
2023-07-31 07:27:01 +00:00
|
|
|
auto idx = stack.size();
|
2023-07-31 11:39:46 +00:00
|
|
|
if (idx == 0) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::cerr << "WARNING: Stack is empty, pushing TestScreen...\n";
|
|
|
|
#endif // NDEBUG
|
|
|
|
push_screen(Screen::new_screen<TestScreen>(self_weak));
|
|
|
|
update(dt);
|
|
|
|
return;
|
|
|
|
}
|
2023-07-31 07:27:01 +00:00
|
|
|
while (idx > 0 && stack.at(--idx)->update(dt)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenStack::draw() {
|
|
|
|
auto idx = stack.size();
|
|
|
|
while (idx > 0 && stack.at(--idx)->draw()) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenStack::push_screen(Screen::Ptr &&screen) {
|
2023-07-31 11:39:46 +00:00
|
|
|
actions.push_back(PendingAction(std::forward<Screen::Ptr>(screen)));
|
2023-07-31 07:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScreenStack::pop_screen() {
|
2023-07-31 11:39:46 +00:00
|
|
|
actions.push_back(PendingAction(Action::POP_SCREEN));
|
2023-07-31 07:27:01 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 11:39:46 +00:00
|
|
|
ScreenStack::ScreenStack() : self_weak(), stack(), actions() {}
|
|
|
|
|
|
|
|
void ScreenStack::handle_pending_actions() {
|
|
|
|
while (!actions.empty()) {
|
|
|
|
switch (actions.front().action) {
|
|
|
|
case Action::PUSH_SCREEN:
|
|
|
|
stack.push_back(std::move(actions.front().screen));
|
|
|
|
break;
|
|
|
|
case Action::POP_SCREEN:
|
|
|
|
if (!stack.empty()) {
|
|
|
|
stack.pop_back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Action::NOP:
|
|
|
|
// Intentionally left blank.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(!"unreachable");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
actions.pop_front();
|
|
|
|
}
|
|
|
|
}
|