Implemented basic joystick selection

You can select joystick in the config file, there is still no GUI. By default, the first available joystick is used.
issue #171
master
krzys-h 2015-07-10 00:02:39 +02:00
parent bd4a661a06
commit 5a68558f4c
2 changed files with 45 additions and 5 deletions

View File

@ -516,6 +516,23 @@ bool CApplication::Create()
// Don't generate joystick events
SDL_JoystickEventState(SDL_IGNORE);
// Report joystick list to log, since we still don't have a GUI for them so you have to set the ID manually in the config
auto joysticks = GetJoystickList();
bool first = true;
for (const auto& joystick : joysticks)
{
if (first)
{
ChangeJoystick(joystick);
first = false;
}
GetLogger()->Info("Detected joystick: %s [ID %d]\n", joystick.name.c_str(), joystick.index);
}
if (first)
{
GetLogger()->Info("No joysticks detected\n");
}
if (!m_headless)
{
m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str());
@ -725,6 +742,8 @@ bool CApplication::OpenJoystick()
if ( (m_joystick.index < 0) || (m_joystick.index >= SDL_NumJoysticks()) )
return false;
GetLogger()->Info("Opening joystick %d\n", m_joystick.index);
m_private->joystick = SDL_JoystickOpen(m_joystick.index);
if (m_private->joystick == nullptr)
return false;
@ -746,6 +765,8 @@ void CApplication::CloseJoystick()
{
// Timer will remove itself automatically
GetLogger()->Info("Closing joystick\n");
SDL_JoystickClose(m_private->joystick);
m_private->joystick = nullptr;
}
@ -755,6 +776,8 @@ bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick)
if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) )
return false;
m_joystick = newJoystick;
if (m_private->joystick != nullptr)
CloseJoystick();

View File

@ -4821,6 +4821,7 @@ void CMainDialog::UpdateSetupButtons()
pc = static_cast<CCheck*>(pw->SearchControl(EVENT_INTERFACE_JOYSTICK));
if ( pc != 0 )
{
pc->SetState(STATE_ENABLE, m_app->GetJoystick().index >= 0);
pc->SetState(STATE_CHECK, m_app->GetJoystickEnabled());
}
@ -4981,6 +4982,7 @@ void CMainDialog::SetupMemorize()
GetProfile().SetIntProperty("Setup", "SkyMode", m_engine->GetSkyMode());
GetProfile().SetIntProperty("Setup", "PlanetMode", m_engine->GetPlanetMode());
GetProfile().SetIntProperty("Setup", "LightMode", m_engine->GetLightMode());
GetProfile().SetIntProperty("Setup", "UseJoystick", m_app->GetJoystickEnabled() ? m_app->GetJoystick().index : -1);
GetProfile().SetFloatProperty("Setup", "ParticleDensity", m_engine->GetParticleDensity());
GetProfile().SetFloatProperty("Setup", "ClippingDistance", m_engine->GetClippingDistance());
GetProfile().SetFloatProperty("Setup", "ObjectDetail", m_engine->GetObjectDetail());
@ -5168,11 +5170,26 @@ void CMainDialog::SetupRecall()
{
m_engine->SetLightMode(iValue);
}
// TODO
// if ( GetProfile().GetLocalProfileInt("Setup", "UseJoystick", iValue) )
// {
// m_engine->SetJoystick(iValue);
// }
if ( GetProfile().GetIntProperty("Setup", "UseJoystick", iValue) )
{
if(iValue >= 0)
{
auto joysticks = m_app->GetJoystickList();
for(const auto& joystick : joysticks)
{
if (joystick.index == iValue)
{
m_app->ChangeJoystick(joystick);
m_app->SetJoystickEnabled(true);
}
}
}
else
{
m_app->SetJoystickEnabled(false);
}
}
if ( GetProfile().GetFloatProperty("Setup", "ParticleDensity", fValue) )
{