Bubba-3D  0.9.0
Awesome game engine!
JoystickAxis.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-22.
19 //
20 
21 #include <JoystickAxis.h>
22 #include <SFML/Window.hpp>
23 #include "Input.h"
24 #include <cmath>
25 #include <JoystickTranslation.h>
26 #include <JoystickTranslator.h>
27 
28 JoystickAxis::JoystickAxis(JoystickTranslation::Axis axis, bool dual) : Input(dual){
29  this->axisPos = axis;
30 }
31 
32 ControlStatus::Activator JoystickAxis::getActivator() {
33  return ControlStatus::JOYSTICK;
34 }
35 
36 JoystickAxis::JoystickAxis(IJoystickTranslation::Axis axis, Separation separation) : Input(false) {
37  this->axisPos = axis;
38  this->separation = separation;
39  separated = true;
40 }
41 JoystickAxis::JoystickAxis(IJoystickTranslation::Axis axisPos, IJoystickTranslation::Axis axisNeg) : Input(true) {
42  this->axisPos = axisPos;
43  this->axisNeg = axisNeg;
44  doubleAxed = true;
45 }
46 
47 ControlStatus JoystickAxis::getStatus() {
48  if(separated)
49  return getStatusSeparated();
50  else
51  return getStatusNormal();
52 }
53 
54 ControlStatus JoystickAxis::getStatusSeparated() {
55  ControlStatus buttonsPressed = ControlStatus();
56  JoystickTranslator* jt = JoystickTranslator::getInstance();
57  for(unsigned int i = 0; i < sf::Joystick::Count; i++)
58  if(sf::Joystick::isConnected(i)) {
59  float val = jt->getTranslation(i)->getAxisValue(axisPos);
60  if(separation == NEGATIVE && val < 0.0f)
61  val = -val;
62  else if(!(separation == POSITIVE && val > 0.0f))
63  val = 0.0f;
64  buttonsPressed.addButton(ControlStatus::activatorFromJoystickNumber(i),removeDeadZone(val));
65  }
66  return buttonsPressed;
67 }
68 
69 ControlStatus JoystickAxis::getStatusNormal() {
70  ControlStatus buttonsPressed = ControlStatus();
71  JoystickTranslator* jt = JoystickTranslator::getInstance();
72  for(unsigned int i = 0; i < sf::Joystick::Count; i++)
73  if(sf::Joystick::isConnected(i)) {
74  float val = jt->getTranslation(i)->getAxisValue(axisPos);
75  if(!dual)
76  val = (val+100)/2; //transform [-100,100] to [0,100]
77  else if(doubleAxed) {
78  val = (val+100)/2; //pos should be [0,100]
79  val -= (jt->getTranslation(i)->getAxisValue(axisNeg) + 100)/2; //neg should be [0,-100]
80  }
81  buttonsPressed.addButton(ControlStatus::activatorFromJoystickNumber(i),removeDeadZone(val));
82  }
83  return buttonsPressed;
84 }
85 
86 
87 float JoystickAxis::removeDeadZone(float val) {
88  if(std::abs(val) <= 15.0f)
89  return 0.0f;
90  val += val < 0.0f ? 15.0f : -15.0f;
91  return val*(100.0f/85.0f);
92 }
The class that contains information about a function at the state of creation.
Definition: ControlStatus.h:59
ControlStatus::Activator getActivator()
static float removeDeadZone(float val)
Definition: Input.h:26
JoystickAxis(IJoystickTranslation::Axis axis, bool dual)