Bubba-3D  0.9.0
Awesome game engine!
ControlsManager.h
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 // Created by simon on 2015-12-20.
18 //
19 
20 #ifndef BUBBA_3D_CONTROLSMANAGER_H
21 #define BUBBA_3D_CONTROLSMANAGER_H
22 
23 #include <Input.h>
24 #include <vector>
25 
26 typedef std::map<int,std::vector<Input*>> functionCollection ;
27 
28 /*
29  * \brief Used to handle player controls.
30  *
31  * This class is used to handle the controls of the game. A short explanation
32  * of how to use the class is used is given here. You can get the singleton by calling
33  * getInstance().
34  *
35  * The bindings can be a dual binding. This means that a function has a negative
36  * and positive meaning. This is a very common concept for example turning right/left
37  * or accelerating forwards/backwards. A joystick axis is naturally dual but to make
38  * keyboard buttons dual you have to specify two buttons. An example of a turning function
39  * (controlled with A/D or the left joystick thumb stick x-axis) and an acceleration function (controlled
40  * by W/S or the left joystick thumb stick y-axis) is given below. We also add a jumping button as an example
41  * of a non-dual button.
42  *
43  * \code{.cpp}
44  * //Global enum defined elsewhere:
45  * enum Controls : int {TURN,ACCELERATE,JUMP};
46  *
47  * ControlsManager cm = ControlsManager::getInstance();
48  * cm->addBindings(ACCELERATE,{new KeyboardButton(sf::Keyboard::S,sf::Keyboard::W),new JoystickAxis(IJoystickTranslation::LEFT_THUMBSTICK_Y,true)});
49  * cm->addBindings(TURN,{new KeyboardButton(sf::Keyboard::D,sf::Keyboard::A),new JoystickAxis(IJoystickTranslation::LEFT_THUMBSTICK_X,true)});
50  * cm->addBindings(JUMP,{new KeyboardButton(sf::Keyboard::Space),new JoystickButton(IJoystickTranslation::A)});
51  * \endcode
52  *
53  * The boolean true in the second argument to the joystick axis says that the joystick is truly dual.
54  * If the keyboard buttons and the joystick steers in different directions just switch there order in the parameters
55  * ((sf::Keyboard::D,sf::Keyboard::A) -> (sf::Keyboard::A,sf::Keyboard::D)).
56  *
57  * \warning A function can only have one assignment from one device. The turn function can't have both A/D
58  * and arrow left/arrow right as both are on the keyboard. The latest specified overrides the old.
59  */
61 public:
62 
68  void addBinding(int function, Input* button);
69 
83  void addBindings(int function, std::initializer_list<Input*> buttons);
84 
92  void clearBindings();
93 
100  ControlStatus getStatus(int function);
101 
105  static ControlsManager* getInstance();
106 
107 private:
108  ControlsManager();
109  ~ControlsManager();
110  functionCollection functions;
111  void addBindingsForNewFunction(int function, std::initializer_list<Input*> buttons);
112  void addBindingsForExistingFunction(int function, std::initializer_list<Input*> buttons,
113  std::vector<Input*> bindings);
114  void removeBindingsWithActivator(std::vector<Input*> *bindings,ControlStatus::Activator toRemove);
115 };
116 
117 
118 #endif //BUBBA_3D_CONTROLSMANAGER_H
The class that contains information about a function at the state of creation.
Definition: ControlStatus.h:59
ControlStatus getStatus(int function)
void addBinding(int function, Input *button)
Adds a button to specified function.
Definition: Input.h:26
static ControlsManager * getInstance()
Gets the singleton instance of the controls manager.
void addBindings(int function, std::initializer_list< Input * > buttons)