1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2013 Jussi Lind <jussi.lind@iki.fi>
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 "keyconfigmenu.hpp"
17 #include "eventhandler.hpp"
18 #include "game.hpp"
19 #include "inputhandler.hpp"
20 #include "textmenuitemview.hpp"
21 
22 #include <MenuItem>
23 #include <MenuItemAction>
24 #include <MenuItemView>
25 #include <MenuManager>
26 
27 #include <MCAssetManager>
28 #include <MCTextureFont>
29 #include <MCTextureText>
30 
31 #include <QObject> // For QObject::tr()
32 
33 //! "Menu" to select a key.
34 class PressKeyMenu : public SurfaceMenu
35 {
36 public:
37     //! Constructor.
38     PressKeyMenu(std::string id, int width, int height);
39 
40     //! \reimp
41     virtual void render() override;
42 
43 private:
44     std::wstring m_text;
45 };
46 
PressKeyMenu(std::string id,int width,int height)47 PressKeyMenu::PressKeyMenu(std::string id, int width, int height)
48   : SurfaceMenu("settingsBack", id, width, height, MTFH::Menu::Style::HorizontalList)
49   , m_text(QObject::tr("Press a key..").toUpper().toStdWString())
50 {
51 }
52 
render()53 void PressKeyMenu::render()
54 {
55     SurfaceMenu::render();
56 
57     MCTextureText text(m_text);
58 
59     const int shadowY = -2;
60     const int shadowX = 2;
61 
62     text.setColor(MCGLColor(0.25, 0.75, 1.0, 1.0));
63     text.setGlyphSize(20, 20);
64     text.setShadowOffset(shadowX, shadowY);
65 
66     auto && font = MCAssetManager::textureFontManager().font(Game::instance().fontName());
67     text.render(width() / 2 - text.width(font) / 2 + 20, height() / 2 + 60, nullptr, font);
68 }
69 
70 static const char * PRESS_KEY_MENU_ID = "pressKeyMenu";
71 
KeyConfigMenu(std::string id,int width,int height)72 KeyConfigMenu::KeyConfigMenu(std::string id, int width, int height)
73   : SurfaceMenu("settingsBack", id, width, height, Menu::Style::VerticalList)
74 {
75     MTFH::MenuManager::instance().addMenu(std::make_shared<PressKeyMenu>(PRESS_KEY_MENU_ID, width, height));
76 
77     addPlayerTwoConfig(width, height);
78     addPlayerOneConfig(width, height);
79 }
80 
addPlayerOneConfig(int width,int height)81 void KeyConfigMenu::addPlayerOneConfig(int width, int height)
82 {
83     const int itemHeight = height / 10;
84 
85     using MTFH::MenuItem;
86     using MTFH::MenuManager;
87 
88     const auto playerOneAccelerate =
89       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player One Accelerate").toUpper().toStdWString());
90     playerOneAccelerate->setView(std::make_shared<TextMenuItemView>(20, *playerOneAccelerate));
91     playerOneAccelerate->setAction(
92       []() {
93           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Up, 0);
94           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
95       });
96 
97     const auto playerOneBrake =
98       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player One Brake").toUpper().toStdWString());
99     playerOneBrake->setView(std::make_shared<TextMenuItemView>(20, *playerOneBrake));
100     playerOneBrake->setAction(
101       []() {
102           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Down, 0);
103           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
104       });
105 
106     const auto playerOneTurnLeft =
107       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player One Turn Left").toUpper().toStdWString());
108     playerOneTurnLeft->setView(std::make_shared<TextMenuItemView>(20, *playerOneTurnLeft));
109     playerOneTurnLeft->setAction(
110       []() {
111           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Left, 0);
112           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
113       });
114 
115     const auto playerOneTurnRight =
116       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player One Turn Right").toUpper().toStdWString());
117     playerOneTurnRight->setView(std::make_shared<TextMenuItemView>(20, *playerOneTurnRight));
118     playerOneTurnRight->setAction(
119       []() {
120           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Right, 0);
121           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
122       });
123 
124     addItem(playerOneBrake);
125     addItem(playerOneAccelerate);
126     addItem(playerOneTurnRight);
127     addItem(playerOneTurnLeft);
128 }
129 
addPlayerTwoConfig(int width,int height)130 void KeyConfigMenu::addPlayerTwoConfig(int width, int height)
131 {
132     const int itemHeight = height / 10;
133 
134     using MTFH::MenuItem;
135     using MTFH::MenuManager;
136 
137     const auto playerTwoAccelerate =
138       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player Two Accelerate").toUpper().toStdWString());
139     playerTwoAccelerate->setView(std::make_shared<TextMenuItemView>(20, *playerTwoAccelerate));
140     playerTwoAccelerate->setAction(
141       []() {
142           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Up, 1);
143           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
144       });
145 
146     const auto playerTwoBrake =
147       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player Two Brake").toUpper().toStdWString());
148     playerTwoBrake->setView(std::make_shared<TextMenuItemView>(20, *playerTwoBrake));
149     playerTwoBrake->setAction(
150       []() {
151           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Down, 1);
152           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
153       });
154 
155     const auto playerTwoTurnLeft =
156       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player Two Turn Left").toUpper().toStdWString());
157     playerTwoTurnLeft->setView(std::make_shared<TextMenuItemView>(20, *playerTwoTurnLeft));
158     playerTwoTurnLeft->setAction(
159       []() {
160           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Left, 1);
161           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
162       });
163 
164     const auto playerTwoTurnRight =
165       std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Player Two Turn Right").toUpper().toStdWString());
166     playerTwoTurnRight->setView(std::make_shared<TextMenuItemView>(20, *playerTwoTurnRight));
167     playerTwoTurnRight->setAction(
168       []() {
169           Game::instance().eventHandler().enableCaptureMode(InputHandler::Action::Right, 1);
170           MenuManager::instance().pushMenu(PRESS_KEY_MENU_ID);
171       });
172 
173     addItem(playerTwoBrake);
174     addItem(playerTwoAccelerate);
175     addItem(playerTwoTurnRight);
176     addItem(playerTwoTurnLeft);
177 }
178 
179 KeyConfigMenu::~KeyConfigMenu() = default;
180