Files
OpenSpace/src/interaction/joystickinputstate.cpp
Alexander Bock 7004c02b86 Happy new year
2021-01-02 15:26:51 +01:00

71 lines
3.2 KiB
C++

/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2021 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <openspace/interaction/joystickinputstate.h>
#include <ghoul/glm.h>
#include <ghoul/misc/invariants.h>
#include <ghoul/misc/stringconversion.h>
#include <algorithm>
#include <map>
#include <numeric>
namespace openspace::interaction {
float JoystickInputStates::axis(int axis) const {
ghoul_precondition(axis >= 0, "axis must be 0 or positive");
float res = std::accumulate(
begin(),
end(),
0.f,
[axis](float value, const JoystickInputState& state) {
if (state.isConnected) {
value += state.axes[axis];
}
return value;
}
);
// If multiple joysticks are connected, we might get values outside the -1,1 range by
// summing them up
glm::clamp(res, -1.f, 1.f);
return res;
}
bool JoystickInputStates::button(int button, JoystickAction action) const {
ghoul_precondition(button >= 0, "button must be 0 or positive");
bool res = std::any_of(
begin(),
end(),
[button, action](const JoystickInputState& state) {
return state.isConnected ? (state.buttons[button] == action) : false;
}
);
return res;
}
} // namespace openspace::interaction