From c513c5fe339c51473a85066cf1f8c8da4d1a46b5 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 10 Aug 2014 02:35:23 +0200 Subject: [PATCH] Implemented Menu::on_window_resize() --- src/gui/menu.cpp | 10 ++++++++-- src/gui/menu.hpp | 6 ++++-- src/gui/menu_manager.cpp | 42 +++++++++++++++++++++++------------------ src/supertux/menu/main_menu.cpp | 9 ++++++++- src/supertux/menu/main_menu.hpp | 1 + 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index f2c55a407..7acd17f90 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -62,7 +62,7 @@ Menu::~Menu() } void -Menu::set_pos(float x, float y) +Menu::set_center_pos(float x, float y) { pos.x = x; pos.y = y; @@ -586,7 +586,13 @@ Menu::get_height() const return items.size() * 24; } -/* Draw the current menu. */ +void +Menu::on_window_resize() +{ + pos.x = SCREEN_WIDTH / 2; + pos.y = SCREEN_HEIGHT / 2; +} + void Menu::draw(DrawingContext& context) { diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index f61994428..6ff1ed736 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -91,8 +91,8 @@ public: void set_active_item(int id); void draw(DrawingContext& context); - Vector get_pos() const { return pos; } - void set_pos(float x, float y); + Vector get_center_pos() const { return pos; } + void set_center_pos(float x, float y); void event(const SDL_Event& event); @@ -102,6 +102,8 @@ public: float get_width() const; float get_height() const; + virtual void on_window_resize(); + protected: /** Return the index of the menu item that was 'hit' (ie. the user clicked on it) in the last event() call */ diff --git a/src/gui/menu_manager.cpp b/src/gui/menu_manager.cpp index 575c0f512..d07ef4b63 100644 --- a/src/gui/menu_manager.cpp +++ b/src/gui/menu_manager.cpp @@ -41,10 +41,10 @@ namespace { Rectf menu2rect(const Menu& menu) { - return Rectf(menu.get_pos().x - menu.get_width() / 2, - menu.get_pos().y - menu.get_height() / 2, - menu.get_pos().x + menu.get_width() / 2, - menu.get_pos().y + menu.get_height() / 2); + return Rectf(menu.get_center_pos().x - menu.get_width() / 2, + menu.get_center_pos().y - menu.get_height() / 2, + menu.get_center_pos().x + menu.get_width() / 2, + menu.get_center_pos().y + menu.get_height() / 2); } } // namespace @@ -81,6 +81,11 @@ public: m_is_active = true; } + void set(const Rectf& rect) + { + m_to_rect = m_from_rect = rect; + } + void update() { if (m_is_active) @@ -172,23 +177,27 @@ MenuManager::event(const SDL_Event& event) void MenuManager::draw(DrawingContext& context) { - if (m_transition->is_active() || current()) + if (m_transition->is_active()) { m_transition->update(); m_transition->draw(context); } - - if (current()) + else { - if (!m_transition->is_active()) + if (current()) { + // brute force the transition into the right shape in case the + // menu has changed sizes + m_transition->set(menu2rect(*current())); + m_transition->draw(context); + current()->draw(context); } + } - if (MouseCursor::current()) - { - MouseCursor::current()->draw(context); - } + if (current() && MouseCursor::current()) + { + MouseCursor::current()->draw(context); } } @@ -278,10 +287,7 @@ MenuManager::on_window_resize() { for(auto i = m_menu_stack.begin(); i != m_menu_stack.end(); ++i) { - // FIXME: This is of course not quite right, since it ignores any - // previous set_pos() calls, it also doesn't update the - // transition-effect/background rectangle - (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2); + (*i)->on_window_resize(); } } @@ -314,7 +320,7 @@ MenuManager::transition(Menu* from, Menu* to) } else { - from_rect = Rectf(to->get_pos(), Sizef(0, 0)); + from_rect = Rectf(to->get_center_pos(), Sizef(0, 0)); } Rectf to_rect; @@ -324,7 +330,7 @@ MenuManager::transition(Menu* from, Menu* to) } else { - to_rect = Rectf(from->get_pos(), Sizef(0, 0)); + to_rect = Rectf(from->get_center_pos(), Sizef(0, 0)); } m_transition->start(from_rect, to_rect); diff --git a/src/supertux/menu/main_menu.cpp b/src/supertux/menu/main_menu.cpp index b4974a416..133da7574 100644 --- a/src/supertux/menu/main_menu.cpp +++ b/src/supertux/menu/main_menu.cpp @@ -34,7 +34,8 @@ MainMenu::MainMenu() { - set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35); + set_center_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35); + add_entry(MNID_STARTGAME, _("Start Game")); add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels")); add_entry(MNID_ADDONS, _("Add-ons")); @@ -44,6 +45,12 @@ MainMenu::MainMenu() } void +MainMenu::on_window_resize() +{ + set_center_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35); +} + +void MainMenu::check_menu() { switch (check()) diff --git a/src/supertux/menu/main_menu.hpp b/src/supertux/menu/main_menu.hpp index 9b6bd924f..1f9de8654 100644 --- a/src/supertux/menu/main_menu.hpp +++ b/src/supertux/menu/main_menu.hpp @@ -38,6 +38,7 @@ class MainMenu : public Menu public: MainMenu(); + void on_window_resize() override; void check_menu(); private: -- 2.11.0