Small joystick related cleanup

This commit is contained in:
Emma Broman
2025-12-17 16:18:04 +01:00
parent 87488edca2
commit fa9f1d8fc5
6 changed files with 85 additions and 70 deletions
+15 -42
View File
@@ -241,60 +241,33 @@ void checkJoystickStatus() {
for (int i = GLFW_JOYSTICK_1; i <= GLFW_JOYSTICK_LAST; i++) {
ZoneScopedN("Joystick state");
JoystickInputState& state = global::interactionHandler->joystickInputStates().at(i);
JoystickInputState& joystick = global::interactionHandler->joystickInputStates().at(i);
const int present = glfwJoystickPresent(i);
if (present == GLFW_FALSE) {
state.isConnected = false;
const int isPresent = glfwJoystickPresent(i);
if (isPresent == GLFW_FALSE) {
joystick.isConnected = false;
continue;
}
if (!state.isConnected) {
if (!joystick.isConnected) {
// Joystick was added
state.isConnected = true;
state.name = glfwGetJoystickName(i);
joystick.isConnected = true;
joystick.name = glfwGetJoystickName(i);
// Check axes and buttons
glfwGetJoystickAxes(i, &state.nAxes);
glfwGetJoystickButtons(i, &state.nButtons);
state.axes.resize(state.nAxes);
state.buttons.resize(state.nButtons);
glfwGetJoystickAxes(i, &joystick.nAxes);
glfwGetJoystickButtons(i, &joystick.nButtons);
std::fill(state.axes.begin(), state.axes.end(), 0.f);
std::fill(state.buttons.begin(), state.buttons.end(), JoystickAction::Idle);
joystick.initializeAxesAndButtons();
}
const float* axes = glfwGetJoystickAxes(i, &state.nAxes);
std::memcpy(state.axes.data(), axes, state.nAxes * sizeof(float));
const float* axes = glfwGetJoystickAxes(i, &joystick.nAxes);
std::memcpy(joystick.axes.data(), axes, joystick.nAxes * sizeof(float));
const unsigned char* buttons = glfwGetJoystickButtons(i, &state.nButtons);
for (int j = 0; j < state.nButtons; j++) {
const unsigned char* buttons = glfwGetJoystickButtons(i, &joystick.nButtons);
for (int j = 0; j < joystick.nButtons; j++) {
const bool currentlyPressed = buttons[j] == GLFW_PRESS;
if (currentlyPressed) {
switch (state.buttons[j]) {
case JoystickAction::Idle:
case JoystickAction::Release:
state.buttons[j] = JoystickAction::Press;
break;
case JoystickAction::Press:
case JoystickAction::Repeat:
state.buttons[j] = JoystickAction::Repeat;
break;
}
}
else {
switch (state.buttons[j]) {
case JoystickAction::Idle:
case JoystickAction::Release:
state.buttons[j] = JoystickAction::Idle;
break;
case JoystickAction::Press:
case JoystickAction::Repeat:
state.buttons[j] = JoystickAction::Release;
break;
}
}
joystick.updateButtonState(currentlyPressed, j);
}
}
}
@@ -86,8 +86,6 @@ private:
WebsocketInputStates _websocketInputStates;
JoystickInputStates _joystickInputStates;
// TODO: add joystick and websocket input state
// Keeps track of when interaction has happened
InteractionMonitor _interactionMonitor;
@@ -71,6 +71,21 @@ struct JoystickInputState {
int nButtons = 0;
/// The status of each button
std::vector<JoystickAction> buttons;
/**
* Initialize the aces and buttons vectors based on the currently set number of
* axes and buttons.
*/
void initializeAxesAndButtons();
/**
* Update the button state for the button with the given index, based on the provided
* isPressed value and the previous value.
*
* \param isPressed Whether the button is currently pressed
* \param buttonIndex The index of the button to update the state for
*/
void updateButtonState(bool isPressed, int buttonIndex);
};
/// The maximum number of joysticks that are supported by this system. This number is
@@ -118,7 +118,7 @@ public:
private:
// There is one of these structs for each connected joystick, describing the orbital
// input mapping for that given joystick
struct Joystick {
struct JoystickMapping {
std::string name;
// We use a vector for the axes and a map for the buttons since the axis are going
@@ -147,7 +147,7 @@ private:
};
// There may be multiple joysticks connected, each with their own state and mapping
std::vector<Joystick> _joysticks;
std::vector<JoystickMapping> _joysticks;
/**
* Get the mapped information for a given joystick.
@@ -155,8 +155,8 @@ private:
* \param joystickName The name of the joystick to get the mapping for
* \return The joystick mapping information, or nullptr if no mapping exists
*/
Joystick* joystickMapping(const std::string& joystickName);
const Joystick* joystickMapping(const std::string& joystickName) const;
JoystickMapping* joystickMapping(const std::string& joystickName);
const JoystickMapping* joystickMapping(const std::string& joystickName) const;
/**
* Find the mapped information for a joystick if it exists. Otherwise, add a new
@@ -167,7 +167,7 @@ private:
* \return The joystick mapping information for either the found or added joystick,
* or nullptr if the joystick could not be added
*/
Joystick* findOrAddJoystickMapping(const std::string& joystickName);
JoystickMapping* findOrAddJoystickMapping(const std::string& joystickName);
};
} // namespace openspace::interaction
+34
View File
@@ -31,6 +31,40 @@
namespace openspace::interaction {
void JoystickInputState::initializeAxesAndButtons() {
axes.resize(nAxes);
buttons.resize(nButtons);
std::fill(axes.begin(), axes.end(), 0.f);
std::fill(buttons.begin(), buttons.end(), JoystickAction::Idle);
}
void JoystickInputState::updateButtonState(bool isPressed, int buttonIndex) {
if (isPressed) {
switch (buttons[buttonIndex]) {
case JoystickAction::Idle:
case JoystickAction::Release:
buttons[buttonIndex] = JoystickAction::Press;
break;
case JoystickAction::Press:
case JoystickAction::Repeat:
buttons[buttonIndex] = JoystickAction::Repeat;
break;
}
}
else {
switch (buttons[buttonIndex]) {
case JoystickAction::Idle:
case JoystickAction::Release:
buttons[buttonIndex] = JoystickAction::Idle;
break;
case JoystickAction::Press:
case JoystickAction::Repeat:
buttons[buttonIndex] = JoystickAction::Release;
break;
}
}
}
int JoystickInputStates::numAxes(const std::string& joystickName) const {
if (joystickName.empty()) {
int maxNumAxes = -1;
@@ -67,7 +67,7 @@ void JoystickCameraStates::updateStateFromInput(
continue;
}
Joystick* joystick = joystickMapping(joystickInputState.name);
JoystickMapping* joystick = joystickMapping(joystickInputState.name);
if (!joystick) {
continue;
@@ -249,7 +249,7 @@ void JoystickCameraStates::setAxisMapping(const std::string& joystickName,
AxisFlip shouldFlip,
double sensitivity)
{
Joystick* joystickMapping = findOrAddJoystickMapping(joystickName);
JoystickMapping* joystickMapping = findOrAddJoystickMapping(joystickName);
if (!joystickMapping) {
return;
}
@@ -278,7 +278,7 @@ void JoystickCameraStates::setAxisMappingProperty(const std::string& joystickNam
AxisInvert shouldInvert,
bool isRemote)
{
Joystick* joystickMapping = findOrAddJoystickMapping(joystickName);
JoystickMapping* joystickMapping = findOrAddJoystickMapping(joystickName);
if (!joystickMapping) {
return;
}
@@ -304,13 +304,9 @@ JoystickCameraStates::AxisInformation JoystickCameraStates::axisMapping(
const std::string& joystickName,
int axis) const
{
const Joystick* joystick = joystickMapping(joystickName);
if (!joystick) {
JoystickCameraStates::AxisInformation dummy;
return dummy;
}
const JoystickMapping* joystick = joystickMapping(joystickName);
if (axis >= static_cast<int>(joystick->axisMapping.size())) {
if (!joystick || axis >= static_cast<int>(joystick->axisMapping.size())) {
JoystickCameraStates::AxisInformation dummy;
return dummy;
}
@@ -321,7 +317,7 @@ JoystickCameraStates::AxisInformation JoystickCameraStates::axisMapping(
void JoystickCameraStates::setDeadzone(const std::string& joystickName, int axis,
float deadzone)
{
Joystick* joystick = findOrAddJoystickMapping(joystickName);
JoystickMapping* joystick = findOrAddJoystickMapping(joystickName);
if (!joystick) {
return;
}
@@ -336,7 +332,7 @@ void JoystickCameraStates::setDeadzone(const std::string& joystickName, int axis
}
float JoystickCameraStates::deadzone(const std::string& joystickName, int axis) const {
const Joystick* joystick = joystickMapping(joystickName);
const JoystickMapping* joystick = joystickMapping(joystickName);
if (!joystick) {
return 0.f;
}
@@ -354,7 +350,7 @@ void JoystickCameraStates::bindButtonCommand(const std::string& joystickName,
ButtonCommandRemote remote,
std::string documentation)
{
Joystick* joystickMapping = findOrAddJoystickMapping(joystickName);
JoystickMapping* joystickMapping = findOrAddJoystickMapping(joystickName);
if (!joystickMapping) {
return;
}
@@ -368,7 +364,7 @@ void JoystickCameraStates::bindButtonCommand(const std::string& joystickName,
void JoystickCameraStates::clearButtonCommand(const std::string& joystickName,
int button)
{
Joystick* joystick = joystickMapping(joystickName);
JoystickMapping* joystick = joystickMapping(joystickName);
if (!joystick) {
return;
}
@@ -392,7 +388,7 @@ std::vector<std::string> JoystickCameraStates::buttonCommand(
int button) const
{
std::vector<std::string> result;
const Joystick* joystick = joystickMapping(joystickName);
const JoystickMapping* joystick = joystickMapping(joystickName);
if (!joystick) {
return result;
}
@@ -404,10 +400,10 @@ std::vector<std::string> JoystickCameraStates::buttonCommand(
return result;
}
JoystickCameraStates::Joystick* JoystickCameraStates::joystickMapping(
JoystickCameraStates::JoystickMapping* JoystickCameraStates::joystickMapping(
const std::string& joystickName)
{
for (Joystick& j : _joysticks) {
for (JoystickMapping& j : _joysticks) {
if (j.name == joystickName) {
return &j;
}
@@ -415,10 +411,10 @@ JoystickCameraStates::Joystick* JoystickCameraStates::joystickMapping(
return nullptr;
}
const JoystickCameraStates::Joystick*
const JoystickCameraStates::JoystickMapping*
JoystickCameraStates::joystickMapping(const std::string& joystickName) const
{
for (const Joystick& j : _joysticks) {
for (const JoystickMapping& j : _joysticks) {
if (j.name == joystickName) {
return &j;
}
@@ -428,10 +424,10 @@ JoystickCameraStates::joystickMapping(const std::string& joystickName) const
return nullptr;
}
JoystickCameraStates::Joystick*
JoystickCameraStates::JoystickMapping*
JoystickCameraStates::findOrAddJoystickMapping(const std::string& joystickName)
{
Joystick* joystick = joystickMapping(joystickName);
JoystickMapping* joystick = joystickMapping(joystickName);
if (!joystick) {
if (_joysticks.size() < JoystickInputStates::MaxNumJoysticks) {
_joysticks.emplace_back();
@@ -456,5 +452,4 @@ JoystickCameraStates::findOrAddJoystickMapping(const std::string& joystickName)
return joystick;
}
} // namespace openspace::interaction