| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2017 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 "lapcountmenu.hpp" |
| 17 | |
| 18 | #include "menuitem.hpp" |
| 19 | #include "menuitemview.hpp" |
| 20 | #include "menumanager.hpp" |
| 21 | #include "textmenuitemview.hpp" |
| 22 | #include "trackselectionmenu.hpp" |
| 23 | |
| 24 | #include "game.hpp" |
| 25 | |
| 26 | #include <MCAssetManager> |
| 27 | #include <MCTextureText> |
| 28 | |
| 29 | #include "simple_logger.hpp" |
| 30 | |
| 31 | static const QString LAP_COUNT_KEY(Settings::lapCountKey()); |
| 32 | |
| 33 | static const int ITEM_TEXT_SIZE = 40; |
| 34 | |
| 35 | std::string LapCountMenu::MenuId = "lapCount"; |
| 36 | |
| 37 | LapCountMenu::LapCountMenu(int width, int height) |
| 38 | : SurfaceMenu("trackSelectionBack", MenuId, width, height, Menu::Style::HorizontalList) |
| 39 | , m_font(MCAssetManager::textureFontManager().font(Game::instance().fontName())) |
| 40 | { |
| 41 | static int LAP_COUNTS[] = { |
| 42 | 1, 3, 5, 10, 20, 50, 100 |
| 43 | }; |
| 44 | |
| 45 | const int numLapCounts = sizeof(LAP_COUNTS) / sizeof(int); |
| 46 | const int itemHeight = height / (numLapCounts + 2); |
| 47 | const int itemWidth = width / (numLapCounts + 2); |
| 48 | const int textSize = ITEM_TEXT_SIZE; |
| 49 | |
| 50 | using MTFH::MenuItem; |
| 51 | using MTFH::MenuManager; |
| 52 | |
| 53 | int selectedIndex = 0; |
| 54 | for (int i = 0; i < numLapCounts; i++) |
| 55 | { |
| 56 | std::wstringstream itemText; |
| 57 | itemText << LAP_COUNTS[i]; |
| 58 | |
| 59 | const auto lapCountItem = std::make_shared<MenuItem>(itemWidth, itemHeight, itemText.str()); |
| 60 | lapCountItem->setView(std::make_shared<TextMenuItemView>(textSize, *lapCountItem)); |
| 61 | lapCountItem->setAction( |
| 62 | [i]() { |
| 63 | juzzlin::L().info() << LAP_COUNTS[i] << " laps selected."; |
| 64 | Game::instance().setLapCount(LAP_COUNTS[i]); |
| 65 | Settings::instance().saveValue(LAP_COUNT_KEY, LAP_COUNTS[i]); |
| 66 | MenuManager::instance().pushMenu(TrackSelectionMenu::MenuId); |
| 67 | }); |
| 68 | |
| 69 | addItem(lapCountItem); |
| 70 | |
| 71 | if (Game::instance().lapCount() == LAP_COUNTS[i]) |
| 72 | { |
| 73 | selectedIndex = lapCountItem->index(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | setCurrentIndex(selectedIndex); |
| 78 | } |
| 79 | |
| 80 | void LapCountMenu::render() |
| 81 | { |
| 82 | SurfaceMenu::render(); |
| 83 | |
| 84 | MCTextureText text(QObject::tr("Choose lap count").toUpper().toStdWString()); |
| 85 | |
| 86 | const int shadowY = -2; |
| 87 | const int shadowX = 2; |
| 88 | |
| 89 | text.setGlyphSize(30, 30); |
| 90 | text.setShadowOffset(shadowX, shadowY); |
| 91 | text.render(x() + width() / 2 - text.width(m_font) / 2, y() + height() / 2 + text.height(m_font) * 2, nullptr, m_font); |
| 92 | } |