Patch for multiple joysticks from const86 <const@mimas.ru>
[supertux.git] / src / gui / menu_manager.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "gui/menu_manager.hpp"
18
19 #include "control/joystickkeyboardcontroller.hpp"
20 #include "gui/menu.hpp"
21 #include "supertux/globals.hpp"
22 #include "supertux/timer.hpp"
23
24 std::vector<Menu*> MenuManager::last_menus;
25 std::list<Menu*> MenuManager::all_menus;
26 Menu* MenuManager::current_ = 0;
27 Menu* MenuManager::previous = 0;
28
29 void
30 MenuManager::push_current(Menu* pmenu)
31 {
32   previous = current_;
33
34   if (current_)
35     last_menus.push_back(current_);
36
37   current_ = pmenu;
38   current_->effect_start_time = real_time;
39   current_->effect_progress   = 0.0f;
40 }
41
42 void
43 MenuManager::pop_current()
44 {
45   previous = current_;
46
47   if (last_menus.size() >= 1) {
48     current_ = last_menus.back();
49     current_->effect_start_time = real_time;
50     current_->effect_progress   = 0.0f;
51     last_menus.pop_back();
52   } else {
53     set_current(NULL);
54   }
55 }
56
57 void
58 MenuManager::set_current(Menu* menu)
59 {
60   if (current_ && current_->close == true)
61     return;
62
63   previous = current_;
64
65   if (menu) {
66     menu->effect_start_time = real_time;
67     menu->effect_progress = 0.0f;
68     current_ = menu;
69   }
70   else if (current_) {
71     last_menus.clear();                         //NULL new menu pointer => close all menus
72     current_->effect_start_time = real_time;
73     current_->effect_progress = 0.0f;
74     current_->close = true;
75   }
76
77   // just to be sure...
78   g_jk_controller->reset();
79 }
80
81 void
82 MenuManager::recalc_pos()
83 {
84   if (current_)
85     current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
86
87   for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
88   {
89     // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
90     (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
91   }
92 }
93
94 /* EOF */