1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-01 12:01:51 +00:00
OpenMW/extern/oics/ICSInputControlSystem_joystick.cpp

316 lines
12 KiB
C++
Raw Normal View History

2012-08-12 18:45:02 +00:00
/* -------------------------------------------------------
Copyright (c) 2011 Alberto G. Salguero (alberto.salguero (at) uca.es)
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 "ICSInputControlSystem.h"
#define SDL_JOY_AXIS_MIN -32768
2014-12-09 03:57:32 +00:00
#define SDL_JOY_AXIS_MAX 32767
#define DEADZONE 0.1f
2012-08-12 18:45:02 +00:00
namespace ICS
{
// load xml
void InputControlSystem::loadJoystickAxisBinders(TiXmlElement* xmlControlNode)
{
2014-12-09 03:57:32 +00:00
TiXmlElement* xmlJoystickBinder = xmlControlNode->FirstChildElement("JoystickAxisBinder");
2012-08-12 18:45:02 +00:00
while(xmlJoystickBinder)
{
Control::ControlChangingDirection dir = Control::STOP;
if(std::string(xmlJoystickBinder->Attribute("direction")) == "INCREASE")
{
dir = Control::INCREASE;
}
else if(std::string(xmlJoystickBinder->Attribute("direction")) == "DECREASE")
{
dir = Control::DECREASE;
}
2014-12-09 03:57:32 +00:00
addJoystickAxisBinding(mControls.back(), FromString<int>(xmlJoystickBinder->Attribute("axis")), dir);
2012-08-12 18:45:02 +00:00
xmlJoystickBinder = xmlJoystickBinder->NextSiblingElement("JoystickAxisBinder");
}
}
void InputControlSystem::loadJoystickButtonBinders(TiXmlElement* xmlControlNode)
{
2014-12-09 03:57:32 +00:00
TiXmlElement* xmlJoystickButtonBinder = xmlControlNode->FirstChildElement("JoystickButtonBinder");
2012-08-12 18:45:02 +00:00
while(xmlJoystickButtonBinder)
{
Control::ControlChangingDirection dir = Control::STOP;
if(std::string(xmlJoystickButtonBinder->Attribute("direction")) == "INCREASE")
{
dir = Control::INCREASE;
}
else if(std::string(xmlJoystickButtonBinder->Attribute("direction")) == "DECREASE")
{
dir = Control::DECREASE;
}
2014-12-09 03:57:32 +00:00
addJoystickButtonBinding(mControls.back(), FromString<int>(xmlJoystickButtonBinder->Attribute("button")), dir);
2012-08-12 18:45:02 +00:00
xmlJoystickButtonBinder = xmlJoystickButtonBinder->NextSiblingElement("JoystickButtonBinder");
}
}
// add bindings
2014-12-09 03:57:32 +00:00
void InputControlSystem::addJoystickAxisBinding(Control* control, int axis, Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ICS_LOG("\tAdding AxisBinder [axis="
2012-08-12 18:45:02 +00:00
+ ToString<int>(axis) + ", direction="
2014-12-09 03:57:32 +00:00
+ ToString<int>(direction) + "]");
control->setValue(0.5f); //all joystick axis start at .5, so do that
2012-08-12 18:45:02 +00:00
ControlAxisBinderItem controlAxisBinderItem;
controlAxisBinderItem.control = control;
controlAxisBinderItem.direction = direction;
2014-12-09 03:57:32 +00:00
mControlsJoystickAxisBinderMap[ axis ] = controlAxisBinderItem;
2012-08-12 18:45:02 +00:00
}
2014-12-09 03:57:32 +00:00
void InputControlSystem::addJoystickButtonBinding(Control* control, unsigned int button, Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ICS_LOG("\tAdding JoystickButtonBinder [button="
2012-08-12 18:45:02 +00:00
+ ToString<int>(button) + ", direction="
+ ToString<int>(direction) + "]");
ControlButtonBinderItem controlJoystickButtonBinderItem;
controlJoystickButtonBinderItem.direction = direction;
controlJoystickButtonBinderItem.control = control;
2014-12-09 03:57:32 +00:00
mControlsJoystickButtonBinderMap[ button ] = controlJoystickButtonBinderItem;
2012-08-12 18:45:02 +00:00
}
// get bindings
2014-12-09 03:57:32 +00:00
int InputControlSystem::getJoystickAxisBinding(Control* control, ICS::Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ControlsAxisBinderMapType::iterator it = mControlsJoystickAxisBinderMap.begin();
while(it != mControlsJoystickAxisBinderMap.end())
{
if(it->first >= 0 && it->second.control == control && it->second.direction == direction)
{
return it->first;
}
++it;
}
2012-08-12 18:45:02 +00:00
return /*NamedAxis::*/UNASSIGNED;
}
2014-12-09 03:57:32 +00:00
unsigned int InputControlSystem::getJoystickButtonBinding(Control* control, ICS::Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ControlsButtonBinderMapType::iterator it = mControlsJoystickButtonBinderMap.begin();
while(it != mControlsJoystickButtonBinderMap.end())
{
if(it->second.control == control && it->second.direction == direction)
{
return it->first;
}
++it;
}
2012-08-12 18:45:02 +00:00
return ICS_MAX_DEVICE_BUTTONS;
}
// remove bindings
2014-12-09 03:57:32 +00:00
void InputControlSystem::removeJoystickAxisBinding(int axis)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ControlsButtonBinderMapType::iterator it = mControlsJoystickAxisBinderMap.find(axis);
if(it != mControlsJoystickAxisBinderMap.end())
{
mControlsJoystickAxisBinderMap.erase(it);
}
2012-08-12 18:45:02 +00:00
}
2014-12-09 03:57:32 +00:00
void InputControlSystem::removeJoystickButtonBinding(unsigned int button)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ControlsButtonBinderMapType::iterator it = mControlsJoystickButtonBinderMap.find(button);
if(it != mControlsJoystickButtonBinderMap.end())
{
mControlsJoystickButtonBinderMap.erase(it);
}
2012-08-12 18:45:02 +00:00
}
// joyStick listeners
2014-12-09 03:57:32 +00:00
void InputControlSystem::buttonPressed(const SDL_ControllerButtonEvent &evt)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
if(mActive)
2012-08-12 18:45:02 +00:00
{
if(!mDetectingBindingControl)
{
2014-12-09 03:57:32 +00:00
ControlsButtonBinderMapType::const_iterator it = mControlsJoystickButtonBinderMap.find(evt.button);
if(it != mControlsJoystickButtonBinderMap.end())
{
it->second.control->setIgnoreAutoReverse(false);
if(!it->second.control->getAutoChangeDirectionOnLimitsAfterStop())
{
it->second.control->setChangingDirection(it->second.direction);
}
else
{
if(it->second.control->getValue() == 1)
{
it->second.control->setChangingDirection(Control::DECREASE);
}
else if(it->second.control->getValue() == 0)
{
it->second.control->setChangingDirection(Control::INCREASE);
}
}
}
2012-08-12 18:45:02 +00:00
}
else if(mDetectingBindingListener)
{
mDetectingBindingListener->joystickButtonBindingDetected(this,
2014-12-09 03:57:32 +00:00
mDetectingBindingControl, evt.button, mDetectingBindingDirection);
2012-08-12 18:45:02 +00:00
}
}
}
2014-12-09 03:57:32 +00:00
void InputControlSystem::buttonReleased(const SDL_ControllerButtonEvent &evt)
2012-08-12 18:45:02 +00:00
{
if(mActive)
{
2014-12-09 03:57:32 +00:00
ControlsButtonBinderMapType::const_iterator it = mControlsJoystickButtonBinderMap.find(evt.button);
if(it != mControlsJoystickButtonBinderMap.end())
{
it->second.control->setChangingDirection(Control::STOP);
}
2012-08-12 18:45:02 +00:00
}
}
2014-12-09 03:57:32 +00:00
void InputControlSystem::axisMoved(const SDL_ControllerAxisEvent &evt)
{
2012-08-12 18:45:02 +00:00
if(mActive)
{
if(!mDetectingBindingControl)
{
2014-12-09 03:57:32 +00:00
ControlAxisBinderItem joystickBinderItem = mControlsJoystickAxisBinderMap[evt.axis]; // joystic axis start at 0 index
Control* ctrl = joystickBinderItem.control;
if(ctrl)
{
ctrl->setIgnoreAutoReverse(true);
float axisRange = SDL_JOY_AXIS_MAX - SDL_JOY_AXIS_MIN;
float valDisplaced = (float)(evt.value - SDL_JOY_AXIS_MIN);
float percent = valDisplaced / axisRange * (1+DEADZONE*2) - DEADZONE; //Assures all values, 0 through 1, are seen
if(percent > .5-DEADZONE && percent < .5+DEADZONE) //close enough to center
percent = .5;
else if(percent > .5)
percent -= DEADZONE;
else
percent += DEADZONE;
if(joystickBinderItem.direction == Control::INCREASE)
{
ctrl->setValue( percent );
}
else if(joystickBinderItem.direction == Control::DECREASE)
{
ctrl->setValue( 1 - ( percent ) );
}
}
2012-08-12 18:45:02 +00:00
}
else if(mDetectingBindingListener)
{
//ControlAxisBinderItem joystickBinderItem = mControlsJoystickAxisBinderMap[ evt.which ][ axis ]; // joystic axis start at 0 index
2012-08-12 18:45:02 +00:00
//Control* ctrl = joystickBinderItem.control;
//if(ctrl && ctrl->isAxisBindable())
if(mDetectingBindingControl && mDetectingBindingControl->isAxisBindable())
{
if( abs( evt.value ) > ICS_JOYSTICK_AXIS_BINDING_MARGIN)
2012-08-12 18:45:02 +00:00
{
mDetectingBindingListener->joystickAxisBindingDetected(this,
2014-12-09 03:57:32 +00:00
mDetectingBindingControl, evt.axis, mDetectingBindingDirection);
2012-08-12 18:45:02 +00:00
}
}
}
}
2014-12-09 03:57:32 +00:00
}
void InputControlSystem::controllerAdded(const SDL_ControllerDeviceEvent &args)
{
2014-12-09 03:57:32 +00:00
ICS_LOG("Adding joystick (index: " + ToString<int>(args.which) + ")");
SDL_GameController* cntrl = SDL_GameControllerOpen(args.which);
int instanceID = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(cntrl));
if(mJoystickIDList.empty()) //
{
for(int j = 0 ; j < ICS_MAX_JOYSTICK_AXIS ; j++)
{
if(mControlsJoystickAxisBinderMap.find(j) == mControlsJoystickAxisBinderMap.end())
{
ControlAxisBinderItem controlJoystickBinderItem;
controlJoystickBinderItem.direction = Control::STOP;
controlJoystickBinderItem.control = NULL;
mControlsJoystickAxisBinderMap[j] = controlJoystickBinderItem;
}
}
}
mJoystickIDList[instanceID] = cntrl;
}
void InputControlSystem::controllerRemoved(const SDL_ControllerDeviceEvent &args)
{
ICS_LOG("Removing joystick (instance id: " + ToString<int>(args.which) + ")");
if(mJoystickIDList.count(args.which)!=0)
{
SDL_GameControllerClose(mJoystickIDList.at(args.which));
mJoystickIDList.erase(args.which);
}
2012-08-12 18:45:02 +00:00
}
// joystick auto bindings
2014-12-09 03:57:32 +00:00
void DetectingBindingListener::joystickAxisBindingDetected(InputControlSystem* ICS, Control* control, int axis, Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
// if the joystick axis is used by another control, remove it
2014-12-09 03:57:32 +00:00
ICS->removeJoystickAxisBinding(axis);
2012-08-12 18:45:02 +00:00
// if the control has an axis assigned, remove it
2014-12-09 03:57:32 +00:00
int oldAxis = ICS->getJoystickAxisBinding(control, direction);
if(oldAxis != InputControlSystem::UNASSIGNED)
2012-08-12 18:45:02 +00:00
{
2014-12-09 03:57:32 +00:00
ICS->removeJoystickAxisBinding(oldAxis);
2012-08-12 18:45:02 +00:00
}
2014-12-09 03:57:32 +00:00
ICS->addJoystickAxisBinding(control, axis, direction);
2012-08-12 18:45:02 +00:00
ICS->cancelDetectingBindingState();
}
void DetectingBindingListener::joystickButtonBindingDetected(InputControlSystem* ICS, Control* control
2014-12-09 03:57:32 +00:00
, unsigned int button, Control::ControlChangingDirection direction)
2012-08-12 18:45:02 +00:00
{
// if the joystick button is used by another control, remove it
2014-12-09 03:57:32 +00:00
ICS->removeJoystickButtonBinding(button);
2012-08-12 18:45:02 +00:00
// if the control has a joystick button assigned, remove it
2014-12-09 03:57:32 +00:00
unsigned int oldButton = ICS->getJoystickButtonBinding(control, direction);
2012-08-12 18:45:02 +00:00
if(oldButton != ICS_MAX_DEVICE_BUTTONS)
{
2014-12-09 03:57:32 +00:00
ICS->removeJoystickButtonBinding(oldButton);
2012-08-12 18:45:02 +00:00
}
2014-12-09 03:57:32 +00:00
ICS->addJoystickButtonBinding(control, button, direction);
2012-08-12 18:45:02 +00:00
ICS->cancelDetectingBindingState();
}
}