2012-07-22 21:15:55 +00:00
# include "GuiInputConfig.h"
# include "GuiGameList.h"
# include <iostream>
# include <fstream>
std : : string GuiInputConfig : : sConfigPath = " ./input.cfg " ;
2012-11-12 15:27:07 +00:00
std : : string GuiInputConfig : : sInputs [ ] = { " UNKNOWN " , " UP " , " DOWN " , " LEFT " , " RIGHT " , " BUTTON1 (Accept) " , " BUTTON2 (Back) " , " START (Menu) " , " SELECT (Jump-to-letter) " , " PAGE UP " , " PAGE DOWN " } ; //must be same order as InputManager::InputButton enum; only add to the end to preserve backwards compatibility
2012-12-08 18:55:54 +00:00
int GuiInputConfig : : sInputCount = 11 ;
2012-07-22 21:15:55 +00:00
GuiInputConfig : : GuiInputConfig ( )
{
2012-07-23 17:27:38 +00:00
mInputNum = 1 ;
2012-07-22 21:15:55 +00:00
mDone = false ;
2012-07-23 17:27:38 +00:00
mLastAxis = - 1 ;
2012-07-22 21:15:55 +00:00
Renderer : : registerComponent ( this ) ;
InputManager : : registerComponent ( this ) ;
if ( SDL_NumJoysticks ( ) < 1 )
{
std : : cerr < < " Error - GuiInputConfig found no SDL joysticks! \n " ;
mJoystick = NULL ;
mDone = true ;
return ;
} else {
2012-09-14 18:22:01 +00:00
std : : cout < < " Opening joystick \" " < < SDL_JoystickName ( 0 ) < < " \" for configuration... \n " ;
2012-07-22 21:15:55 +00:00
mJoystick = SDL_JoystickOpen ( 0 ) ;
}
}
GuiInputConfig : : ~ GuiInputConfig ( )
{
Renderer : : unregisterComponent ( this ) ;
InputManager : : unregisterComponent ( this ) ;
}
void GuiInputConfig : : onRender ( )
{
2012-10-17 18:21:56 +00:00
Renderer : : drawRect ( 0 , 0 , Renderer : : getScreenWidth ( ) , Renderer : : getScreenHeight ( ) , 0xFFFFFFFF ) ;
2012-07-22 21:15:55 +00:00
2012-10-24 15:28:37 +00:00
Font * font = Renderer : : getDefaultFont ( Renderer : : MEDIUM ) ;
2012-08-02 01:43:55 +00:00
2012-10-24 15:28:37 +00:00
int height = font - > getHeight ( ) + 6 ;
Renderer : : drawCenteredText ( " It looks like you have a joystick plugged in! " , 0 , 2 , 0x000000FF , font ) ;
Renderer : : drawCenteredText ( " POV hats (some D-Pads) are automatically mapped to directions. " , 0 , height , 0x000000FF , font ) ;
Renderer : : drawCenteredText ( " You can press a keyboard key to skip any input. " , 0 , height * 2 , 0x000000FF , font ) ;
2012-10-25 18:03:35 +00:00
Renderer : : drawCenteredText ( " If you want to remap later, delete ~/.emulationstation/es_input.cfg. " , 0 , height * 3 , 0x000000FF , font ) ;
2012-10-24 15:28:37 +00:00
Renderer : : drawCenteredText ( " This interface only configures the first joystick plugged in. " , 0 , height * 4 , 0x000000FF , font ) ;
Renderer : : drawCenteredText ( " Remember - you'll need to set up your emulator separately! " , 0 , Renderer : : getScreenHeight ( ) - height , 0x000000FF , font ) ;
2012-07-23 23:53:33 +00:00
2012-07-22 21:15:55 +00:00
if ( mDone )
2012-10-24 15:28:37 +00:00
Renderer : : drawCenteredText ( " All done! Press a keyboard key to save. " , 0 , height * 5 , 0x00BB00FF , font ) ;
2012-07-22 21:15:55 +00:00
else
2012-10-24 15:28:37 +00:00
Renderer : : drawCenteredText ( " Please press the axis/button for " + sInputs [ mInputNum ] , 0 , height * 5 , 0x00C000FF , font ) ;
2012-07-22 21:15:55 +00:00
}
void GuiInputConfig : : onInput ( InputManager : : InputButton button , bool keyDown )
{
if ( mDone )
{
2012-07-23 23:53:33 +00:00
if ( InputManager : : lastEvent - > type = = SDL_KEYUP )
2012-07-22 21:15:55 +00:00
{
2012-07-23 23:53:33 +00:00
writeConfig ( ) ;
2012-07-22 21:15:55 +00:00
if ( mJoystick )
SDL_JoystickClose ( mJoystick ) ;
2012-07-23 23:53:33 +00:00
InputManager : : loadConfig ( ) ;
2012-07-22 21:15:55 +00:00
delete this ;
2012-10-05 20:18:36 +00:00
GuiGameList : : create ( ) ;
2012-07-22 21:15:55 +00:00
}
return ;
}
SDL_Event * event = InputManager : : lastEvent ;
2012-10-25 18:03:35 +00:00
if ( event - > type = = SDL_KEYUP )
{
//keyboard key pressed; skip and continue
mInputNum + + ;
}
2012-07-22 21:15:55 +00:00
if ( event - > type = = SDL_JOYBUTTONDOWN )
{
mButtonMap [ event - > jbutton . button ] = ( InputManager : : InputButton ) mInputNum ;
2012-08-02 01:43:55 +00:00
std : : cout < < " Mapping " < < sInputs [ mInputNum ] < < " to button " < < ( int ) event - > jbutton . button < < " \n " ;
2012-07-22 21:15:55 +00:00
mInputNum + + ;
}
2012-07-22 22:03:54 +00:00
if ( event - > type = = SDL_JOYAXISMOTION )
{
2012-07-23 17:27:38 +00:00
//std::cout << "motion on axis " << event->jaxis.axis << " to value " << event->jaxis.value << "\n";
2012-07-22 22:03:54 +00:00
2012-07-23 17:27:38 +00:00
if ( event - > jaxis . axis = = mLastAxis )
{
if ( event - > jaxis . value < InputManager : : deadzone & & event - > jaxis . value > - InputManager : : deadzone )
mLastAxis = - 1 ;
return ;
}
2012-07-22 22:03:54 +00:00
if ( event - > jaxis . value > InputManager : : deadzone )
{
2012-07-23 17:27:38 +00:00
mAxisPosMap [ event - > jaxis . axis ] = ( InputManager : : InputButton ) mInputNum ;
2012-07-22 22:03:54 +00:00
mInputNum + + ;
2012-07-23 17:27:38 +00:00
mLastAxis = event - > jaxis . axis ;
2012-08-02 01:43:55 +00:00
std : : cout < < " Mapping " < < sInputs [ mInputNum - 1 ] < < " to axis+ " < < mLastAxis < < " \n " ;
2012-07-22 22:03:54 +00:00
} else if ( event - > jaxis . value < - InputManager : : deadzone )
{
2012-07-23 17:27:38 +00:00
mAxisNegMap [ event - > jaxis . axis ] = ( InputManager : : InputButton ) mInputNum ;
2012-07-22 22:03:54 +00:00
mInputNum + + ;
2012-07-23 17:27:38 +00:00
mLastAxis = event - > jaxis . axis ;
2012-08-02 01:43:55 +00:00
std : : cout < < " Mapping " < < sInputs [ mInputNum - 1 ] < < " to axis- " < < mLastAxis < < " \n " ;
2012-07-22 22:03:54 +00:00
}
}
2012-07-22 21:15:55 +00:00
if ( mInputNum > = sInputCount )
{
mDone = true ;
return ;
}
}
2012-07-23 23:53:33 +00:00
void GuiInputConfig : : writeConfig ( )
2012-07-22 21:15:55 +00:00
{
2012-07-23 23:53:33 +00:00
std : : string path = InputManager : : getConfigPath ( ) ;
2012-07-22 21:15:55 +00:00
std : : ofstream file ( path . c_str ( ) ) ;
2012-09-14 18:22:01 +00:00
if ( SDL_JoystickName ( 0 ) )
file < < " JOYNAME " < < SDL_JoystickName ( 0 ) < < " \n " ;
2012-07-22 21:15:55 +00:00
typedef std : : map < int , InputManager : : InputButton > : : iterator it_type ;
for ( it_type iter = mButtonMap . begin ( ) ; iter ! = mButtonMap . end ( ) ; iter + + )
{
file < < " BUTTON " < < iter - > first < < " " < < iter - > second < < " \n " ;
}
2012-07-23 17:27:38 +00:00
for ( it_type iter = mAxisPosMap . begin ( ) ; iter ! = mAxisPosMap . end ( ) ; iter + + )
2012-07-22 21:15:55 +00:00
{
2012-07-23 17:27:38 +00:00
file < < " AXISPOS " < < iter - > first < < " " < < iter - > second < < " \n " ;
2012-07-22 21:15:55 +00:00
}
2012-07-23 17:27:38 +00:00
for ( it_type iter = mAxisNegMap . begin ( ) ; iter ! = mAxisNegMap . end ( ) ; iter + + )
{
file < < " AXISNEG " < < iter - > first < < " " < < iter - > second < < " \n " ;
}
2012-08-04 21:38:37 +00:00
file . close ( ) ;
2012-07-22 21:15:55 +00:00
}