qzdoom/src/m_joy.cpp

145 lines
4.1 KiB
C++
Raw Normal View History

// HEADER FILES ------------------------------------------------------------
#include "m_joy.h"
#include "gameconfigfile.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// CODE --------------------------------------------------------------------
//==========================================================================
//
// M_SetJoystickConfigSection
//
// Sets up the config for reading or writing this controller's axis config.
//
//==========================================================================
static bool M_SetJoystickConfigSection(IJoystickConfig *joy, bool create)
{
FString id = "Joy:";
id += joy->GetIdentifier();
return GameConfig->SetSection(id, create);
}
//==========================================================================
//
// M_LoadJoystickConfig
//
//==========================================================================
bool M_LoadJoystickConfig(IJoystickConfig *joy)
{
char key[32];
const char *value;
int axislen;
int numaxes;
joy->SetDefaultConfig();
if (!M_SetJoystickConfigSection(joy, false))
{
return false;
}
value = GameConfig->GetValueForKey("Sensitivity");
if (value != NULL)
{
joy->SetSensitivity((float)atof(value));
}
numaxes = joy->GetNumAxes();
for (int i = 0; i < numaxes; ++i)
{
axislen = mysnprintf(key, countof(key), "Axis%u", i);
mysnprintf(key + axislen, countof(key) - axislen, "deadzone");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
{
joy->SetAxisDeadZone(i, (float)atof(value));
}
mysnprintf(key + axislen, countof(key) - axislen, "scale");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
{
joy->SetAxisScale(i, (float)atof(value));
}
mysnprintf(key + axislen, countof(key) - axislen, "map");
value = GameConfig->GetValueForKey(key);
if (value != NULL)
{
EJoyAxis gameaxis = (EJoyAxis)atoi(value);
if (gameaxis < JOYAXIS_None || gameaxis >= NUM_JOYAXIS)
{
gameaxis = JOYAXIS_None;
}
joy->SetAxisMap(i, gameaxis);
}
}
return true;
}
//==========================================================================
//
// M_SaveJoystickConfig
//
// Only saves settings that are not at their defaults.
//
//==========================================================================
void M_SaveJoystickConfig(IJoystickConfig *joy)
{
char key[32], value[32];
int axislen, numaxes;
if (M_SetJoystickConfigSection(joy, true))
{
GameConfig->ClearCurrentSection();
if (!joy->IsSensitivityDefault())
{
mysnprintf(value, countof(value), "%g", joy->GetSensitivity());
GameConfig->SetValueForKey("Sensitivity", value);
}
numaxes = joy->GetNumAxes();
for (int i = 0; i < numaxes; ++i)
{
axislen = mysnprintf(key, countof(key), "Axis%u", i);
if (!joy->IsAxisDeadZoneDefault(i))
{
mysnprintf(key + axislen, countof(key) - axislen, "deadzone");
mysnprintf(value, countof(value), "%g", joy->GetAxisDeadZone(i));
GameConfig->SetValueForKey(key, value);
}
if (!joy->IsAxisScaleDefault(i))
{
mysnprintf(key + axislen, countof(key) - axislen, "scale");
mysnprintf(value, countof(value), "%g", joy->GetAxisScale(i));
GameConfig->SetValueForKey(key, value);
}
if (!joy->IsAxisMapDefault(i))
{
mysnprintf(key + axislen, countof(key) - axislen, "map");
mysnprintf(value, countof(value), "%d", joy->GetAxisMap(i));
GameConfig->SetValueForKey(key, value);
}
}
// If the joystick is entirely at its defaults, delete this section
// so that we don't write out a lone section header.
if (GameConfig->SectionIsEmpty())
{
GameConfig->DeleteCurrentSection();
}
}
}