Bubba-3D  0.9.0
Awesome game engine!
ControlsManager.cpp
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 //
18 // Created by simon on 2015-12-20.
19 //
20 
21 #include <ControlsManager.h>
22 #include <map>
23 #include <vector>
24 #include <ControlStatus.h>
25 #include <string>
26 
27 
29 
30  static ControlsManager instance; // Guaranteed to be destroyed.
31  // Instantiated on first use.
32  return &instance;
33 
34 }
35 
36 ControlsManager::ControlsManager() { }
37 ControlsManager::~ControlsManager() {
38  for(auto i = functions.begin(); i!= functions.end() ; i++)
39  for(auto j = i->second.begin() ; j != i->second.end() ; j++)
40  delete (*j); //Delete the buttons
41 }
42 
44  functions = std::map<int,std::vector<Input*>>();
45 }
46 
47 void ControlsManager::addBinding(int function, Input* button) {
48  addBindings(function,{button});
49 }
50 
51 void ControlsManager::addBindings(int function, std::initializer_list<Input*> buttons) {
52  functionCollection::iterator elem = functions.find(function);
53  if(elem == functions.end()){
54  addBindingsForNewFunction(function,buttons);
55  }else {
56  addBindingsForExistingFunction(function,buttons,elem->second);
57  }
58 }
59 
60 void ControlsManager::addBindingsForNewFunction(int function, std::initializer_list<Input *> buttons) {
61  std::vector<Input *> list;
62  bool firstDuality = (*buttons.begin())->isDual();
63  for (auto it = buttons.begin(); it != buttons.end(); it++) {
64  if((*it)->isDual() != firstDuality)
65  throw "The duality of the bindings were different for function " + std::to_string(function) + ".";
66  list.insert(list.end(), *it);
67  }
68  functions.insert(std::pair<int,std::vector<Input*>>(function,list));
69 }
70 
71 void ControlsManager::addBindingsForExistingFunction(int function, std::initializer_list<Input *> buttons,
72  std::vector<Input *> bindings) {
73  bool shouldBeDual = bindings.front()->isDual();
74  for (auto it = buttons.begin(); it != buttons.end(); it++) {
75  if((*it)->isDual() != shouldBeDual)
76  throw ("The function " + std::to_string(function) + " is " + (shouldBeDual ? "" : "not ") +
77  "dual but a binding was " + (!shouldBeDual ? "not " : "") + "dual.");
78  removeBindingsWithActivator(&bindings, (*it)->getActivator());
79  bindings.insert(bindings.end(), *it);
80  }
81  functions.insert(std::pair<int,std::vector<Input*>>(function,bindings));
82 }
83 
84 void ControlsManager::removeBindingsWithActivator(std::vector<Input *> *bindings,
85  ControlStatus::Activator toRemove) {
86  for (auto it = bindings->begin(); it != bindings->end();) {
87  if ((*it)->getActivator() == toRemove) {
88  Input* remove = *it;
89  it = bindings->erase(it);
90  delete remove;
91  }else
92  it++;
93  }
94 }
95 
97  std::vector<Input*> buttons;
98  functionCollection::iterator elem = functions.find(function);
99  if(elem == functions.end())
100  return ControlStatus();
101  buttons = elem->second;
102  ControlStatus status = ControlStatus();
103  for(auto it = buttons.begin(); it != buttons.end() ; it++)
104  status.merge((*it)->getStatus());
105  return status;
106 }
107 
108 
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)