Turned MenuManager into a proper class instead of just a collection of static functio...
[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 <assert.h>
20
21 #include "control/input_manager.hpp"
22 #include "gui/menu.hpp"
23 #include "supertux/globals.hpp"
24 #include "supertux/timer.hpp"
25
26 MenuManager* MenuManager::s_instance = 0;
27
28 MenuManager&
29 MenuManager::instance()
30 {
31   assert(s_instance);
32   return *s_instance;
33 }
34
35 MenuManager::MenuManager() :
36   m_last_menus(),
37   m_all_menus(),
38   m_previous(),
39   m_current()
40 {
41   s_instance = this;
42 }
43
44 MenuManager::~MenuManager()
45 {
46   s_instance = nullptr;
47 }
48
49 void
50 MenuManager::push_current(Menu* menu)
51 {
52   m_previous = m_current;
53
54   if (m_current)
55   {
56     m_last_menus.push_back(m_current);
57   }
58
59   m_current = menu;
60   m_current->effect_start_time = real_time;
61   m_current->effect_progress = 0.0f;
62 }
63
64 void
65 MenuManager::pop_current()
66 {
67   m_previous = m_current;
68
69   if (m_last_menus.size() >= 1)
70   {
71     m_current = m_last_menus.back();
72     m_current->effect_start_time = real_time;
73     m_current->effect_progress   = 0.0f;
74     m_last_menus.pop_back();
75   }
76   else
77   {
78     set_current(nullptr);
79   }
80 }
81
82 void
83 MenuManager::set_current(Menu* menu)
84 {
85   if (m_current && m_current->close == true)
86   {
87     // do nothing
88   }
89   else
90   {
91     m_previous = m_current;
92
93     if (menu)
94     {
95       menu->effect_start_time = real_time;
96       menu->effect_progress = 0.0f;
97       m_current = menu;
98     }
99     else if (m_current)
100     {
101       m_last_menus.clear();                         //NULL new menu pointer => close all menus
102       m_current->effect_start_time = real_time;
103       m_current->effect_progress = 0.0f;
104       m_current->close = true;
105     }
106
107     // just to be sure...
108     g_input_manager->reset();
109   }
110 }
111
112 void
113 MenuManager::recalc_pos()
114 {
115   if (m_current)
116     m_current->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
117
118   for(auto i = m_all_menus.begin(); i != m_all_menus.end(); ++i)
119   {
120     // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
121     (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
122   }
123 }
124
125 /* EOF */