2 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "gui/menu_manager.hpp"
21 #include "control/input_manager.hpp"
22 #include "gui/menu.hpp"
23 #include "gui/mousecursor.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/menu/menu_storage.hpp"
26 #include "supertux/timer.hpp"
27 #include "video/drawing_context.hpp"
29 MenuManager* MenuManager::s_instance = 0;
32 MenuManager::instance()
38 MenuManager::MenuManager() :
44 MenuManager::~MenuManager()
50 MenuManager::draw(DrawingContext& context)
54 Vector pos = current()->get_pos();
55 float menu_width = current()->get_width();
56 float menu_height = current()->get_height();
58 // draw menu background rectangles
59 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
60 Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
61 Color(0.2f, 0.3f, 0.4f, 0.8f),
65 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
66 Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
67 Color(0.6f, 0.7f, 0.8f, 0.5f),
71 current()->draw(context);
73 if (MouseCursor::current())
75 MouseCursor::current()->draw(context);
80 if (effect_progress != 1.0f)
84 menu_width *= 1.0f - effect_progress;
85 menu_height *= 1.0f - effect_progress;
87 else if (MenuManager::instance().m_previous)
89 menu_width = (menu_width * effect_progress) + (MenuManager::instance().m_previous->get_width() * (1.0f - effect_progress));
90 menu_height = (menu_height * effect_progress) + (MenuManager::instance().m_previous->get_height() * (1.0f - effect_progress));
91 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
95 menu_width *= effect_progress;
96 menu_height *= effect_progress;
101 effect_progress = (real_time - effect_start_time) * 6.0f;
103 if(effect_progress >= 1.0f) {
104 effect_progress = 1.0f;
107 MenuManager::instance().m_current = 0;
111 else if (effect_progress <= 0.0f) {
112 effect_progress = 0.0f;
115 // only pass events in non-anim states
116 if(effect_progress != 1.0f)
123 MenuManager::check_menu()
127 current()->check_menu();
137 MenuManager::push_menu(int id)
139 push_menu(MenuStorage::instance().create(static_cast<MenuStorage::MenuId>(id)));
143 MenuManager::push_menu(std::unique_ptr<Menu> menu)
145 m_menu_stack.push_back(std::move(menu));
147 //current()->effect_start_time = real_time;
148 //current()->effect_progress = 0.0f;
152 MenuManager::pop_menu()
154 if (!m_menu_stack.empty())
156 m_menu_stack.pop_back();
157 //current()->effect_start_time = real_time;
158 //current()->effect_progress = 0.0f;
162 set_menu(MenuStorage::NO_MENU);
167 MenuManager::clear_menu_stack()
169 set_menu(MenuStorage::NO_MENU);
173 MenuManager::set_menu(int id)
175 set_menu(MenuStorage::instance().create(static_cast<MenuStorage::MenuId>(id)));
179 MenuManager::set_menu(std::unique_ptr<Menu> menu)
183 m_menu_stack.push_back(std::move(menu));
184 //current()->effect_start_time = real_time;
185 //current()->effect_progress = 0.0f;
189 m_menu_stack.clear();
190 //current()->effect_start_time = real_time;
191 //current()->effect_progress = 0.0f;
192 //current()->close = true;
195 // just to be sure...
196 g_input_manager->reset();
200 MenuManager::recalc_pos()
202 for(auto i = m_menu_stack.begin(); i != m_menu_stack.end(); ++i)
204 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
205 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);