| 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 "surfacemenu.hpp" |
| 17 | #include "game.hpp" |
| 18 | #include "renderer.hpp" |
| 19 | #include "textmenuitemview.hpp" |
| 20 | |
| 21 | #include <MCAssetManager> |
| 22 | #include <MCSurface> |
| 23 | #include <MCTextureFont> |
| 24 | |
| 25 | #include <MenuItem> |
| 26 | |
| 27 | #include <cassert> |
| 28 | |
| 29 | SurfaceMenu::SurfaceMenu(std::string surfaceId, std::string id, int width, int height, Menu::Style style, bool quitItem, bool prevItem, bool nextItem) |
| 30 | : Menu(id, width, height, style) |
| 31 | , m_back(MCAssetManager::surfaceManager().surface(surfaceId)) |
| 32 | , m_font(MCAssetManager::textureFontManager().font(Game::instance().fontName())) |
| 33 | { |
| 34 | m_back->setShaderProgram(Renderer::instance().program("menu")); |
| 35 | m_back->setColor({ 0.5, 0.5, 0.5, 1.0 }); |
| 36 | |
| 37 | if (quitItem) |
| 38 | { |
| 39 | const int textSize = 40; |
| 40 | const auto quit = std::make_shared<MTFH::MenuItem>(textSize, textSize, L"X"); |
| 41 | quit->setView(std::make_shared<TextMenuItemView>(textSize, *quit)); |
| 42 | addMouseItem(MTFH::Menu::MouseItemType::Quit, quit); |
| 43 | } |
| 44 | |
| 45 | if (prevItem) |
| 46 | { |
| 47 | const int textSize = 40; |
| 48 | const auto prev = std::make_shared<MTFH::MenuItem>(textSize, textSize, L"<"); |
| 49 | prev->setView(std::make_shared<TextMenuItemView>(textSize, *prev)); |
| 50 | addMouseItem(MTFH::Menu::MouseItemType::Prev, prev); |
| 51 | } |
| 52 | |
| 53 | if (nextItem) |
| 54 | { |
| 55 | const int textSize = 40; |
| 56 | const auto next = std::make_shared<MTFH::MenuItem>(textSize, textSize, L">"); |
| 57 | next->setView(std::make_shared<TextMenuItemView>(textSize, *next)); |
| 58 | addMouseItem(MTFH::Menu::MouseItemType::Next, next); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void SurfaceMenu::render() |
| 63 | { |
| 64 | m_back->bind(); |
| 65 | m_back->setSize(width(), width() * m_back->height() / m_back->width()); |
| 66 | m_back->render(nullptr, { x() + width() / 2, y() + height() / 2, 0 }, 0); |
| 67 | Menu::render(); |
| 68 | } |
| 69 | |
| 70 | void SurfaceMenu::enter() |
| 71 | { |
| 72 | Menu::enter(); |
| 73 | setPos(0, 0, 0, 0); |
| 74 | } |
| 75 | |
| 76 | void SurfaceMenu::pushEnter() |
| 77 | { |
| 78 | Menu::pushEnter(); |
| 79 | setPos(width(), 0, 0, 0); |
| 80 | } |
| 81 | |
| 82 | void SurfaceMenu::pushExit() |
| 83 | { |
| 84 | Menu::pushExit(); |
| 85 | setPos(0, 0, -width(), 0); |
| 86 | } |
| 87 | |
| 88 | void SurfaceMenu::popEnter() |
| 89 | { |
| 90 | Menu::popEnter(); |
| 91 | setPos(-width(), 0, 0, 0); |
| 92 | } |
| 93 | |
| 94 | void SurfaceMenu::popExit() |
| 95 | { |
| 96 | Menu::popExit(); |
| 97 | setPos(0, 0, width(), 0); |
| 98 | } |
| 99 | |
| 100 | MCTextureFont & SurfaceMenu::font() const |
| 101 | { |
| 102 | return m_font; |
| 103 | } |