dd07255e5ff8fdfbfd1caa8fbea5c675f3bd3cff
[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::draw(DrawingContext& context)
51 {
52   if (m_current)
53   {
54     m_current->draw(context);
55   }
56 }
57
58 void
59 MenuManager::push_current(Menu* menu)
60 {
61   m_previous = m_current;
62
63   if (m_current)
64   {
65     m_last_menus.push_back(m_current);
66   }
67
68   m_current = menu;
69   m_current->effect_start_time = real_time;
70   m_current->effect_progress = 0.0f;
71 }
72
73 void
74 MenuManager::pop_current()
75 {
76   m_previous = m_current;
77
78   if (m_last_menus.size() >= 1)
79   {
80     m_current = m_last_menus.back();
81     m_current->effect_start_time = real_time;
82     m_current->effect_progress   = 0.0f;
83     m_last_menus.pop_back();
84   }
85   else
86   {
87     set_current(nullptr);
88   }
89 }
90
91 void
92 MenuManager::set_current(Menu* menu)
93 {
94   if (m_current && m_current->close == true)
95   {
96     // do nothing
97   }
98   else
99   {
100     m_previous = m_current;
101
102     if (menu)
103     {
104       menu->effect_start_time = real_time;
105       menu->effect_progress = 0.0f;
106       m_current = menu;
107     }
108     else if (m_current)
109     {
110       m_last_menus.clear();                         //NULL new menu pointer => close all menus
111       m_current->effect_start_time = real_time;
112       m_current->effect_progress = 0.0f;
113       m_current->close = true;
114     }
115
116     // just to be sure...
117     g_input_manager->reset();
118   }
119 }
120
121 void
122 MenuManager::recalc_pos()
123 {
124   if (m_current)
125     m_current->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
126
127   for(auto i = m_all_menus.begin(); i != m_all_menus.end(); ++i)
128   {
129     // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
130     (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
131   }
132 }
133
134 /* EOF */