Cheetah Software  1.0
GameController Class Reference

#include <GameController.h>

+ Inheritance diagram for GameController:
+ Collaboration diagram for GameController:

Public Member Functions

 GameController (QObject *parent=0)
 
void updateGamepadCommand (GamepadCommand &gamepadCommand)
 
void findNewController ()
 
 ~GameController ()
 

Private Attributes

QGamepad * _qGamepad = nullptr
 

Detailed Description

Definition at line 17 of file GameController.h.

Constructor & Destructor Documentation

GameController::GameController ( QObject *  parent = 0)
explicit

By default, the game controller selects the "first" joystick, printing a warning if there are multiple joysticks On Linux, this is /dev/input/js0 If no joystick is found, it will print an error message, and will return zero. It is possible to change/add a joystick later with findNewController

Definition at line 22 of file GameController.cpp.

References findNewController().

22  : QObject(parent) {
24 }
void findNewController()

+ Here is the call graph for this function:

GameController::~GameController ( )

Definition at line 86 of file GameController.cpp.

References _qGamepad.

86 { delete _qGamepad; }
QGamepad * _qGamepad

Member Function Documentation

void GameController::findNewController ( )

Re-run the joystick finding code to select the "first" joystick. This can be used to set up the joystick if the simulator is started without a joystick plugged in

Definition at line 31 of file GameController.cpp.

References _qGamepad.

31  {
32  delete _qGamepad;
33  _qGamepad = nullptr; // in case this doesn't work!
34 
35  auto gamepadList = QGamepadManager::instance()->connectedGamepads();
36  if (gamepadList.empty()) {
37  printf(
38  "[ERROR: GameController] No controller was connected! All joystick "
39  "commands will be zero!\n");
40  } else {
41  if (gamepadList.size() > 1) {
42  printf(
43  "[ERROR: GameController] There are %d joysticks connected. Using "
44  "the first one.\n",
45  gamepadList.size());
46  } else {
47  printf("[GameController] Found 1 joystick\n");
48  }
49 
50  _qGamepad = new QGamepad(*gamepadList.begin());
51  }
52 }
QGamepad * _qGamepad

+ Here is the caller graph for this function:

void GameController::updateGamepadCommand ( GamepadCommand gamepadCommand)

Overwrite a driverCommand with the current joystick state. If there's no joystick, sends zeros TODO: what happens if the joystick is unplugged?

Definition at line 59 of file GameController.cpp.

References _qGamepad, GamepadCommand::a, GamepadCommand::b, GamepadCommand::back, GamepadCommand::leftBumper, GamepadCommand::leftStickAnalog, GamepadCommand::leftStickButton, GamepadCommand::leftTriggerAnalog, GamepadCommand::leftTriggerButton, GamepadCommand::rightBumper, GamepadCommand::rightStickAnalog, GamepadCommand::rightStickButton, GamepadCommand::rightTriggerAnalog, GamepadCommand::rightTriggerButton, GamepadCommand::start, GamepadCommand::x, GamepadCommand::y, and GamepadCommand::zero().

59  {
60  if (_qGamepad) {
61  gamepadCommand.leftBumper = _qGamepad->buttonL1();
62  gamepadCommand.rightBumper = _qGamepad->buttonR1();
63  gamepadCommand.leftTriggerButton = _qGamepad->buttonL2() != 0.;
64  gamepadCommand.rightTriggerButton = _qGamepad->buttonR2() != 0.;
65  gamepadCommand.back = _qGamepad->buttonSelect();
66  gamepadCommand.start = _qGamepad->buttonStart();
67  gamepadCommand.a = _qGamepad->buttonA();
68  gamepadCommand.b = _qGamepad->buttonB();
69  gamepadCommand.x = _qGamepad->buttonX();
70  gamepadCommand.y = _qGamepad->buttonY();
71  gamepadCommand.leftStickButton = _qGamepad->buttonL3();
72  gamepadCommand.rightStickButton = _qGamepad->buttonR3();
73  gamepadCommand.leftTriggerAnalog = (float)_qGamepad->buttonL2();
74  gamepadCommand.rightTriggerAnalog = (float)_qGamepad->buttonR2();
75  gamepadCommand.leftStickAnalog =
76  Vec2<float>(_qGamepad->axisLeftX(), -_qGamepad->axisLeftY());
77  gamepadCommand.rightStickAnalog =
78  Vec2<float>(_qGamepad->axisRightX(), -_qGamepad->axisRightY());
79  } else {
80  gamepadCommand.zero(); // no joystick, return all zeros
81  }
82 
83  // printf("%s\n", gamepadCommand.toString().c_str());
84 }
QGamepad * _qGamepad
Vec2< float > rightStickAnalog
typename Eigen::Matrix< T, 2, 1 > Vec2
Definition: cppTypes.h:22
Vec2< float > leftStickAnalog
float rightTriggerAnalog
float leftTriggerAnalog

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

QGamepad* GameController::_qGamepad = nullptr
private

Definition at line 26 of file GameController.h.


The documentation for this class was generated from the following files: