| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D 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 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #include "menumanager.hpp" |
| 17 | #include "menu.hpp" |
| 18 | |
| 19 | #include <cassert> |
| 20 | |
| 21 | namespace MTFH { |
| 22 | |
| 23 | MenuManager * MenuManager::m_instance = nullptr; |
| 24 | |
| 25 | MenuManager::MenuManager() |
| 26 | { |
| 27 | assert(!m_instance); |
| 28 | m_instance = this; |
| 29 | } |
| 30 | |
| 31 | MenuPtr MenuManager::activeMenu() const |
| 32 | { |
| 33 | if (m_menuStack.size()) |
| 34 | { |
| 35 | return m_menuStack.back(); |
| 36 | } |
| 37 | |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
| 41 | MenuPtr MenuManager::getMenuById(std::string menuId) |
| 42 | { |
| 43 | assert(m_idToMenuMap[menuId]); |
| 44 | return m_idToMenuMap[menuId]; |
| 45 | } |
| 46 | |
| 47 | void MenuManager::addMenu(MenuPtr newMenu) |
| 48 | { |
| 49 | m_idToMenuMap[newMenu->id()] = newMenu; |
| 50 | } |
| 51 | |
| 52 | void MenuManager::enterMenu(MenuPtr newMenu) |
| 53 | { |
| 54 | if (m_menuStack.size()) |
| 55 | { |
| 56 | m_prevMenu = m_menuStack.back(); |
| 57 | } |
| 58 | |
| 59 | m_menuStack.clear(); |
| 60 | m_menuStack.push_back(newMenu); |
| 61 | newMenu->enter(); |
| 62 | } |
| 63 | |
| 64 | void MenuManager::enterMenu(std::string menuId) |
| 65 | { |
| 66 | assert(m_idToMenuMap[menuId]); |
| 67 | enterMenu(m_idToMenuMap[menuId]); |
| 68 | } |
| 69 | |
| 70 | void MenuManager::pushMenu(MenuPtr newMenu) |
| 71 | { |
| 72 | if (!m_menuStack.size() || m_menuStack.back() != newMenu) |
| 73 | { |
| 74 | if (m_menuStack.size()) |
| 75 | { |
| 76 | m_prevMenu = m_menuStack.back(); |
| 77 | m_prevMenu->pushExit(); |
| 78 | } |
| 79 | |
| 80 | m_menuStack.push_back(newMenu); |
| 81 | newMenu->pushEnter(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void MenuManager::pushMenu(std::string menuId) |
| 86 | { |
| 87 | assert(m_idToMenuMap[menuId]); |
| 88 | pushMenu(m_idToMenuMap[menuId]); |
| 89 | } |
| 90 | |
| 91 | void MenuManager::popMenu() |
| 92 | { |
| 93 | if (m_menuStack.size()) |
| 94 | { |
| 95 | m_prevMenu = m_menuStack.back(); |
| 96 | m_prevMenu->popExit(); |
| 97 | m_menuStack.pop_back(); |
| 98 | |
| 99 | if (m_menuStack.size()) |
| 100 | { |
| 101 | m_menuStack.back()->popEnter(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void MenuManager::popToMenu(std::string menuId) |
| 107 | { |
| 108 | if (m_menuStack.size()) |
| 109 | { |
| 110 | m_prevMenu = m_menuStack.back(); |
| 111 | m_prevMenu->popExit(); |
| 112 | |
| 113 | m_menuStack.clear(); |
| 114 | } |
| 115 | |
| 116 | assert(m_idToMenuMap[menuId]); |
| 117 | |
| 118 | m_menuStack.push_back(m_idToMenuMap[menuId]); |
| 119 | |
| 120 | m_idToMenuMap[menuId]->popEnter(); |
| 121 | } |
| 122 | |
| 123 | void MenuManager::enterCurrentMenu() |
| 124 | { |
| 125 | if (m_menuStack.size()) |
| 126 | { |
| 127 | m_menuStack.back()->enter(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void MenuManager::exit() |
| 132 | { |
| 133 | m_menuStack.clear(); |
| 134 | } |
| 135 | |
| 136 | MenuManager & MenuManager::instance() |
| 137 | { |
| 138 | assert(m_instance); |
| 139 | return *m_instance; |
| 140 | } |
| 141 | |
| 142 | void MenuManager::render() |
| 143 | { |
| 144 | if (m_prevMenu) |
| 145 | { |
| 146 | m_prevMenu->render(); |
| 147 | } |
| 148 | |
| 149 | if (m_menuStack.size()) |
| 150 | { |
| 151 | m_menuStack.back()->render(); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void MenuManager::up() |
| 156 | { |
| 157 | if (m_menuStack.size()) |
| 158 | { |
| 159 | m_menuStack.back()->up(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void MenuManager::down() |
| 164 | { |
| 165 | if (m_menuStack.size()) |
| 166 | { |
| 167 | m_menuStack.back()->down(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | void MenuManager::left() |
| 172 | { |
| 173 | if (m_menuStack.size()) |
| 174 | { |
| 175 | m_menuStack.back()->left(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void MenuManager::right() |
| 180 | { |
| 181 | if (m_menuStack.size()) |
| 182 | { |
| 183 | m_menuStack.back()->right(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | bool MenuManager::mousePress(int x, int y, int screenWidth, int screenHeight) |
| 188 | { |
| 189 | if (m_menuStack.size()) |
| 190 | { |
| 191 | return m_menuStack.back()->handleMousePress(x, y, screenWidth, screenHeight); |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool MenuManager::mouseRelease(int x, int y, int screenWidth, int screenHeight) |
| 198 | { |
| 199 | if (m_menuStack.size()) |
| 200 | { |
| 201 | return m_menuStack.back()->handleMouseRelease(x, y, screenWidth, screenHeight); |
| 202 | } |
| 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | void MenuManager::selectCurrentItem() |
| 208 | { |
| 209 | if (m_menuStack.size()) |
| 210 | { |
| 211 | m_menuStack.back()->selectCurrentItem(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | bool MenuManager::isDone() const |
| 216 | { |
| 217 | if (!m_menuStack.size()) |
| 218 | { |
| 219 | return true; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | return m_menuStack.back()->isDone(); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | void MenuManager::stepTime(std::chrono::milliseconds timeStep) |
| 228 | { |
| 229 | for (auto && menuIter : m_idToMenuMap) |
| 230 | { |
| 231 | menuIter.second->stepTime(static_cast<int>(timeStep.count())); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | } // namespace MTFH |