| 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 "vsyncmenu.hpp" |
| 17 | #include "confirmationmenu.hpp" |
| 18 | #include "game.hpp" |
| 19 | #include "settings.hpp" |
| 20 | #include "textmenuitemview.hpp" |
| 21 | |
| 22 | #include <MenuItem> |
| 23 | #include <MenuItemAction> |
| 24 | #include <MenuItemView> |
| 25 | #include <MenuManager> |
| 26 | |
| 27 | #include "simple_logger.hpp" |
| 28 | |
| 29 | #include <QObject> // for tr() |
| 30 | |
| 31 | #include <memory> |
| 32 | |
| 33 | class VSyncItem : public MTFH::MenuItem |
| 34 | { |
| 35 | public: |
| 36 | class SaveVSyncAction : public MTFH::MenuItemAction |
| 37 | { |
| 38 | public: |
| 39 | //! Constructor. |
| 40 | SaveVSyncAction(VSyncItem & parent) |
| 41 | : m_parent { parent } |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | //! \reimp |
| 46 | virtual void fire() override |
| 47 | { |
| 48 | const int vsync = m_parent.vsync(); |
| 49 | Settings::instance().saveValue(Settings::vsyncKey(), vsync); |
| 50 | juzzlin::L().info() << "VSync set: " << vsync; |
| 51 | Game::instance().exitGame(); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | VSyncItem & m_parent; |
| 56 | }; |
| 57 | |
| 58 | //! Constructor. |
| 59 | VSyncItem(ConfirmationMenuPtr confirmationMenu, int vsync, int width, int height, std::wstring text = L"") |
| 60 | : MenuItem(width, height, text) |
| 61 | , m_confirmationMenu(confirmationMenu) |
| 62 | , m_saveVSyncAction(new SaveVSyncAction(*this)) |
| 63 | , m_vsync(vsync) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | //! Destructor. |
| 68 | virtual ~VSyncItem() override = default; |
| 69 | |
| 70 | //! \reimp |
| 71 | virtual void setSelected(bool flag) override |
| 72 | { |
| 73 | MenuItem::setSelected(flag); |
| 74 | MTFH::MenuManager::instance().pushMenu(m_confirmationMenu->id()); |
| 75 | m_confirmationMenu->setText(QObject::tr("Restart to change VSync setting.").toStdWString()); |
| 76 | m_confirmationMenu->setAcceptAction(m_saveVSyncAction); |
| 77 | m_confirmationMenu->setCurrentIndex(1); |
| 78 | } |
| 79 | |
| 80 | int vsync() const |
| 81 | { |
| 82 | return m_vsync; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | ConfirmationMenuPtr m_confirmationMenu; |
| 87 | |
| 88 | MTFH::MenuItemActionPtr m_saveVSyncAction; |
| 89 | |
| 90 | int m_vsync; |
| 91 | }; |
| 92 | |
| 93 | VSyncMenu::VSyncMenu(ConfirmationMenuPtr confirmationMenu, std::string id, int width, int height) |
| 94 | : SurfaceMenu("settingsBack", id, width, height) |
| 95 | , m_confirmationMenu(confirmationMenu) |
| 96 | { |
| 97 | const int itemHeight = height / 10; |
| 98 | |
| 99 | const auto off = std::make_shared<VSyncItem>(m_confirmationMenu, 0, width, itemHeight, QObject::tr("Off").toStdWString()); |
| 100 | off->setView(std::make_shared<TextMenuItemView>(20, *off)); |
| 101 | addItem(off); |
| 102 | |
| 103 | auto on = std::make_shared<VSyncItem>(m_confirmationMenu, 1, width, itemHeight, QObject::tr("On").toStdWString()); |
| 104 | on->setView(std::make_shared<TextMenuItemView>(20, *on)); |
| 105 | addItem(on); |
| 106 | |
| 107 | VSyncMenu::enter(); |
| 108 | } |
| 109 | |
| 110 | void VSyncMenu::enter() |
| 111 | { |
| 112 | const int vsync = Settings::instance().loadVSync(); |
| 113 | for (unsigned int i = 0; i < itemCount(); i++) |
| 114 | { |
| 115 | if (const auto vsyncItem = std::dynamic_pointer_cast<VSyncItem>(item(i)); vsyncItem) |
| 116 | { |
| 117 | if (vsyncItem->vsync() == vsync) |
| 118 | { |
| 119 | vsyncItem->setCurrent(); |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |