Browse Source

Added function to redetect joysticks.

Lasse Öörni 13 years ago
parent
commit
c9bdf9eb35

+ 9 - 3
Docs/Reference.dox

@@ -790,7 +790,7 @@ Rendering detailed auxiliary views can easily have a large performance impact. S
 
 \page Input %Input
 
-The Input subsystem provides keyboard and mouse input via both a polled interface and events. It is always instantiated, even in headless mode, but is active only once the application window has been created. Once active, the subsystem takes over the application mouse cursor. It will be hidden, so the UI should be used to render a software cursor if necessary.
+The Input subsystem provides keyboard, mouse, joystick and touch input via both a polled interface and events. It is always instantiated, even in headless mode, but is active only once the application window has been created. Once active, the subsystem takes over the application mouse cursor. It will be hidden, so the UI should be used to render a software cursor if necessary.
 
 The input events include:
 
@@ -804,13 +804,19 @@ The input events include:
 - E_TOUCHBEGIN: a finger touched the screen.
 - E_TOUCHEND: a finger was lifted from the screen.
 - E_TOUCHMOVE: a finger moved on the screen.
+- E_JOYSTICKBUTTONDOWN: a joystick button was pressed.
+- E_JOYSTICKBUTTONUP: a joystick button was released.
+- E_JOYSTICKAXISMOVE: a joystick axis was moved.
+- E_JOYSTICKHATMOVE: a joystick POV hat was moved.
 
 The input polling API differentiates between the initiation of a key/mouse button press, and holding the key or button down. \ref Input::GetKeyPress "GetKeyPress()" and \ref Input::GetMouseButtonPress "GetMouseButtonPress()" return true only for one frame (the initiation) while \ref Input::GetKeyDown "GetKeyDown()" and \ref Input::GetMouseButtonDown "GetMouseButtonDown()" return true as long as the key or button is held down.
 
-From the input subsystem you can also query whether the application is active/inactive, or minimized.
-
 In script, the polling API is accessed via properties: input.keyDown[], input.keyPress[], input.mouseButtonDown[], input.mouseButtonPress[], input.mouseMove.
 
+To get joystick input, the joystick(s) must first be explicitly opened using \ref Input::OpenJoystick "OpenJoystick()". Accessing the polled joystick state using \ref Input::GetJoystick "GetJoystick()" also automatically opens the joystick. The plugged in joysticks are detected on application start and must be manually redetected using \ref Input::DetectJoysticks "DetectJoysticks()" if they are plugged in or disconnected during runtime. 
+
+From the input subsystem you can also query whether the application is active/inactive, or minimized.
+
 
 \page Audio %Audio
 

+ 2 - 1
Docs/ScriptAPI.dox

@@ -2402,7 +2402,8 @@ Input
 
 Methods:<br>
 - bool OpenJoystick(uint)
-- bool CloseJoystick(uint)
+- void CloseJoystick(uint)
+- bool DetectJoysticks()
 
 Properties:<br>
 - ShortStringHash type (readonly)

+ 2 - 1
Engine/Engine/InputAPI.cpp

@@ -140,7 +140,8 @@ static void RegisterInput(asIScriptEngine* engine)
     
     RegisterObject<Input>(engine, "Input");
     engine->RegisterObjectMethod("Input", "bool OpenJoystick(uint)", asMETHOD(Input, OpenJoystick), asCALL_THISCALL);
-    engine->RegisterObjectMethod("Input", "bool CloseJoystick(uint)", asMETHOD(Input, CloseJoystick), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "void CloseJoystick(uint)", asMETHOD(Input, CloseJoystick), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "bool DetectJoysticks()", asMETHOD(Input, DetectJoysticks), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "void set_toggleFullscreen(bool)", asMETHOD(Input, SetToggleFullscreen), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "bool get_toggleFullscreen() const", asMETHOD(Input, GetToggleFullscreen), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "bool get_keyDown(int) const", asMETHOD(Input, GetKeyDown), asCALL_THISCALL);

+ 24 - 3
Engine/Input/Input.cpp

@@ -194,6 +194,22 @@ void Input::SetToggleFullscreen(bool enable)
     toggleFullscreen_ = enable;
 }
 
+bool Input::DetectJoysticks()
+{
+    if (inputInstances.Size() > 1)
+    {
+        LOGERROR("Can not redetect joysticks with multiple application instances");
+        return false;
+    }
+    else
+    {
+        SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
+        SDL_InitSubSystem(SDL_INIT_JOYSTICK);
+        ResetJoysticks();
+        return true;
+    }
+}
+
 bool Input::OpenJoystick(unsigned index)
 {
     if (index >= joysticks_.Size())
@@ -367,12 +383,17 @@ void Input::Initialize()
         inputInstances[windowID_] = this;
     }
     
-    // Initialize joysticks
+    ResetJoysticks();
+    
+    LOGINFO("Initialized input");
+}
+
+void Input::ResetJoysticks()
+{
+    joysticks_.Clear();
     joysticks_.Resize(SDL_NumJoysticks());
     for (unsigned i = 0; i < joysticks_.Size(); ++i)
         joysticks_[i].name_ = SDL_JoystickName(i);
-    
-    LOGINFO("Initialized input");
 }
 
 void Input::MakeActive()

+ 4 - 0
Engine/Input/Input.h

@@ -130,6 +130,8 @@ public:
     bool OpenJoystick(unsigned index);
     /// Close a joystick.
     void CloseJoystick(unsigned index);
+    /// Redetect joysticks. Return true if successful.
+    bool DetectJoysticks();
     
     /// Check if a key is held down.
     bool GetKeyDown(int key) const;
@@ -173,6 +175,8 @@ public:
 private:
     /// Initialize when screen mode initially set.
     void Initialize();
+    /// Setup internal joystick structures.
+    void ResetJoysticks();
     /// Activate the application.
     void MakeActive();
     /// Deactivate the application.

+ 1 - 1
Engine/Input/InputEvents.h

@@ -136,7 +136,7 @@ EVENT(E_JOYSTICKAXISMOVE, JoystickAxisMove)
     PARAM(P_POSITION, Position);            // float
 }
 
-/// Joystick hat moved.
+/// Joystick POV hat moved.
 EVENT(E_JOYSTICKHATMOVE, JoystickHatMove)
 {
     PARAM(P_JOYSTICK, Joystick);            // int