diff --git a/src/gui/menu_manager.cpp b/src/gui/menu_manager.cpp index c96fe7f124f..b029e0f1f55 100644 --- a/src/gui/menu_manager.cpp +++ b/src/gui/menu_manager.cpp @@ -24,6 +24,7 @@ #include "supertux/globals.hpp" #include "supertux/menu/menu_storage.hpp" #include "supertux/timer.hpp" +#include "util/log.hpp" #include "video/drawing_context.hpp" MenuManager* MenuManager::s_instance = 0; @@ -35,26 +36,106 @@ MenuManager::instance() return *s_instance; } -MenuManager::MenuManager() : - m_menu_stack() +namespace { + +Rectf menu2rect(const Menu& menu) { - s_instance = this; + return Rectf(menu.get_pos().x, + menu.get_pos().y, + menu.get_pos().x + menu.get_width(), + menu.get_pos().y + menu.get_height()); } -MenuManager::~MenuManager() +Rectf pos2rect(const Menu& menu) { - s_instance = nullptr; + return Rectf(menu.get_pos().x, + menu.get_pos().y, + menu.get_pos().x, + menu.get_pos().y); } -void -MenuManager::draw(DrawingContext& context) +} // namespace + +class MenuTransition { - if (current()) +private: + Rectf m_from_rect; + Rectf m_to_rect; + + bool m_closing_animation; + float m_effect_progress; + float m_effect_start_time; + bool m_is_active; +public: + MenuTransition() : + m_is_active(false) { + } + + void start(const Rectf& to_rect) + { + m_to_rect = to_rect; + } + + void start(const Rectf& from_rect, + const Rectf& to_rect) + { + m_from_rect = from_rect; + m_to_rect = to_rect; + + m_effect_start_time = real_time; + m_effect_progress = 0.0f; + + m_is_active = true; + } + + void update() + { + m_effect_progress = (real_time - m_effect_start_time) * 6.0f; + + if (m_effect_progress >= 1.0f) { + m_effect_progress = 1.0f; + + /* if (close) + { + MenuManager::instance().m_current = 0; + close = false; + }*/ + } + else if (m_effect_progress <= 0.0f) + { + m_effect_progress = 0.0f; + } + } + + void draw(DrawingContext& context) + { +#ifdef GRUMBEL Vector pos = current()->get_pos(); float menu_width = current()->get_width(); float menu_height = current()->get_height(); + float p = m_effect_progress; + if (p != 1.0f) + { + if (close) + { + menu_width *= 1.0f - p; + menu_height *= 1.0f - p; + } + else if (MenuManager::instance().m_previous) + { + menu_width = (menu_width * p) + (MenuManager::instance().m_previous->get_width() * (1.0f - p)); + menu_height = (menu_height * p) + (MenuManager::instance().m_previous->get_height() * (1.0f - p)); + //std::cout << p << " " << this << " " << last_menus.back() << std::endl; + } + else + { + menu_width *= p; + menu_height *= p; + } + } + // draw menu background rectangles context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4), Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)), @@ -67,56 +148,68 @@ MenuManager::draw(DrawingContext& context) Color(0.6f, 0.7f, 0.8f, 0.5f), 16.0f, LAYER_GUI-10); - - current()->draw(context); - - if (MouseCursor::current()) - { - MouseCursor::current()->draw(context); - } +#endif } -#ifdef GRUMBEL - if (effect_progress != 1.0f) + bool is_active() { - if (close) - { - menu_width *= 1.0f - effect_progress; - menu_height *= 1.0f - effect_progress; - } - else if (MenuManager::instance().m_previous) - { - menu_width = (menu_width * effect_progress) + (MenuManager::instance().m_previous->get_width() * (1.0f - effect_progress)); - menu_height = (menu_height * effect_progress) + (MenuManager::instance().m_previous->get_height() * (1.0f - effect_progress)); - //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl; - } - else - { - menu_width *= effect_progress; - menu_height *= effect_progress; - } + return m_is_active; } +}; - //update - effect_progress = (real_time - effect_start_time) * 6.0f; +MenuManager::MenuManager() : + m_menu_stack(), + m_transition(new MenuTransition) +{ + s_instance = this; +} - if(effect_progress >= 1.0f) { - effect_progress = 1.0f; +MenuManager::~MenuManager() +{ + s_instance = nullptr; +} - if (close) { - MenuManager::instance().m_current = 0; - close = false; - } +void +MenuManager::update() +{ + if (current()) + { + current()->update(); + } +} + +void +MenuManager::event(const SDL_Event& event) +{ + if (current()) + { + current()->event(event); } - else if (effect_progress <= 0.0f) { - effect_progress = 0.0f; +} + +void +MenuManager::draw(DrawingContext& context) +{ + if (!current()) + { + return; } + m_transition->update(); + m_transition->draw(context); + +#ifdef GRUMBEL // only pass events in non-anim states - if(effect_progress != 1.0f) + if(m_effect_progress != 1.0f) return; - #endif + + current()->draw(context); + + if (MouseCursor::current()) + { + MouseCursor::current()->draw(context); + } } bool @@ -142,31 +235,28 @@ MenuManager::push_menu(int id) void MenuManager::push_menu(std::unique_ptr