Bubba-3D  0.9.0
Awesome game engine!
JoystickTranslation.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 2016-01-14.
19 //
20 
21 #include <JoystickTranslation.h>
22 #include <IJoystickTranslation.h>
23 
24 JoystickTranslation::JoystickTranslation(unsigned int joystickID, bool defaultMapping) {
25  this->joystickID = joystickID;
26  this->defaultMapping = defaultMapping;
27 }
28 unsigned int JoystickTranslation::getJoystickID() {
29  return joystickID;
30 }
31 
32 void JoystickTranslation::addAxis(IJoystickTranslation::Axis axis, valueRetriever valRetrieve) {
33  xboxToGenericAxis.insert(xboxToGenericAxis.end(),std::pair<Axis,valueRetriever>(axis,valRetrieve));
34 }
35 
36 void JoystickTranslation::addButton(IJoystickTranslation::Button button, valueRetriever valRetrieve) {
37  xboxToGenericButton.insert(xboxToGenericButton.end(),std::pair<Button,valueRetriever>(button,valRetrieve));
38 }
39 
40 float JoystickTranslation::getAxisValue(IJoystickTranslation::Axis axis) {
41  auto elem = xboxToGenericAxis.find(axis);
42  if(elem == xboxToGenericAxis.end())
43  return 0.0f;
44  else
45  return elem->second(joystickID);
46 }
47 
48 float JoystickTranslation::getButtonValue(IJoystickTranslation::Button button) {
49  auto elem = xboxToGenericButton.find(button);
50  if(elem == xboxToGenericButton.end())
51  return 0.0f;
52  else
53  return elem->second(joystickID);
54 }
55 
56 JoystickTranslation::valueRetriever JoystickTranslation::valRetriever(float retrieve) {
57  return [retrieve](unsigned int x) -> float { return retrieve; };
58 }
59 
60 JoystickTranslation::valueRetriever JoystickTranslation::buttonValueRetriever(unsigned int button) {
61  return [button](unsigned int id) -> float {return sf::Joystick::isButtonPressed(id,button) ? 100.0f : 0.0f;};
62 }
63 
64 JoystickTranslation::valueRetriever JoystickTranslation::axisValueRetriever(sf::Joystick::Axis axis, bool inverted) {
65  return [axis,inverted](unsigned int id) -> float {return (inverted ? -1 : 1)*sf::Joystick::getAxisPosition(id,axis);};
66 }
67 
68 JoystickTranslation::valueRetriever JoystickTranslation::axisFromButtonsRetriever(unsigned int buttonPos, unsigned int buttonNeg) {
69  return [buttonNeg,buttonPos](unsigned int id) -> float {
70  float val = sf::Joystick::isButtonPressed(id,buttonPos) ? 100.0f : 0.0f;
71  val -= sf::Joystick::isButtonPressed(id,buttonNeg) ? 100.0f : 0.0f;
72  return val;
73  };
74 }
75 
76 JoystickTranslation::valueRetriever JoystickTranslation::buttonFromAxisRetriever(sf::Joystick::Axis axis) {
77  return [axis](unsigned int id) -> float {return (sf::Joystick::getAxisPosition(id,axis)+100.0f)/2.0f;};
78 }
79 
80 JoystickTranslation::valueRetriever JoystickTranslation::buttonFromHalfAxisRetriever(sf::Joystick::Axis axis,
81  bool positiveElseNegative) {
82  return [axis,positiveElseNegative](unsigned int id) -> float {
83  float val = sf::Joystick::getAxisPosition(id,axis);
84  if(val > 0.0f && positiveElseNegative)
85  return val;
86  else if(val < 0.0f && !positiveElseNegative)
87  return val * -1;
88  else
89  return 0.0f;
90  };
91 }
92 
93 JoystickTranslation::valueRetriever JoystickTranslation::axisFromButtonRetriever(unsigned int button) {
94  return [button](unsigned int id) -> float {return sf::Joystick::isButtonPressed(id,button) ? 100.0f : -100.0f;};
95 }
96 
97 bool JoystickTranslation::isDefaultMapping() {
98  return defaultMapping;
99 }