2
0
Эх сурвалжийг харах

Add new API to get joystick by its name.

Yao Wei Tjong 姚伟忠 9 жил өмнө
parent
commit
8ac6bc2666

+ 1 - 0
Source/Urho3D/AngelScript/InputAPI.cpp

@@ -597,6 +597,7 @@ static void RegisterInput(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Input", "uint get_numJoysticks() const", asMETHOD(Input, GetNumJoysticks), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "JoystickState@+ get_joysticks(int)", asMETHOD(Input, GetJoystick), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "JoystickState@+ get_joysticksByIndex(uint)", asMETHOD(Input, GetJoystickByIndex), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "JoystickState@+ get_joysticksByName(const String& in)", asMETHOD(Input, GetJoystickByName), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "bool get_focus() const", asMETHOD(Input, HasFocus), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "bool get_minimized() const", asMETHOD(Input, IsMinimized), asCALL_THISCALL);
     engine->RegisterGlobalFunction("Input@+ get_input()", asFUNCTION(GetInput), asCALL_CDECL);

+ 15 - 4
Source/Urho3D/Input/Input.cpp

@@ -560,7 +560,7 @@ void Input::SetMouseVisible(bool enable, bool suppressEvent)
                     SetMouseModeAbsolute(SDL_TRUE);
 #else
                 if (mouseMode_ == MM_ABSOLUTE && !emscriptenPointerLock_)
-                    emscriptenInput_->RequestPointerLock(MM_ABSOLUTE, suppressEvent);      
+                    emscriptenInput_->RequestPointerLock(MM_ABSOLUTE, suppressEvent);
 #endif
                 SDL_ShowCursor(SDL_FALSE);
                 mouseVisible_ = false;
@@ -568,7 +568,7 @@ void Input::SetMouseVisible(bool enable, bool suppressEvent)
             else if (mouseMode_ != MM_RELATIVE)
             {
                 SetMouseGrabbed(false, suppressEvent);
-                
+
                 SDL_ShowCursor(SDL_TRUE);
                 mouseVisible_ = true;
 
@@ -595,7 +595,7 @@ void Input::SetMouseVisible(bool enable, bool suppressEvent)
                     {
                         SetMousePosition(lastVisibleMousePosition_);
                         lastMousePosition_ = lastVisibleMousePosition_;
-                    } 
+                    }
                 }
 #else
                 if (mouseMode_ == MM_ABSOLUTE && emscriptenPointerLock_)
@@ -1410,6 +1410,17 @@ JoystickState* Input::GetJoystickByIndex(unsigned index)
     return 0;
 }
 
+JoystickState* Input::GetJoystickByName(const String& name)
+{
+    for (HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Begin(); i != joysticks_.End(); ++i)
+    {
+        if (i->second_.name_ == name)
+            return &(i->second_);
+    }
+
+    return 0;
+}
+
 JoystickState* Input::GetJoystick(SDL_JoystickID id)
 {
     HashMap<SDL_JoystickID, JoystickState>::Iterator i = joysticks_.Find(id);
@@ -1487,7 +1498,7 @@ void Input::Initialize()
 #ifdef __EMSCRIPTEN__
     SubscribeToEvent(E_ENDFRAME, URHO3D_HANDLER(Input, HandleEndFrame));
 #endif
-    
+
     URHO3D_LOGINFO("Initialized input");
 }
 

+ 2 - 0
Source/Urho3D/Input/Input.h

@@ -265,6 +265,8 @@ public:
     JoystickState* GetJoystick(SDL_JoystickID id);
     /// Return joystick state by index, or null if does not exist. 0 = first connected joystick.
     JoystickState* GetJoystickByIndex(unsigned index);
+    /// Return joystick state by name, or null if does not exist.
+    JoystickState* GetJoystickByName(const String& name);
 
     /// Return whether fullscreen toggle is enabled.
     bool GetToggleFullscreen() const { return toggleFullscreen_; }

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Input/Input.pkg

@@ -90,6 +90,7 @@ class Input : public Object
     unsigned GetNumJoysticks() const;
     JoystickState* GetJoystick(int id);
     JoystickState* GetJoystickByIndex(unsigned index);
+    JoystickState* GetJoystickByName(const String name);
     bool GetToggleFullscreen() const;
     bool GetScreenKeyboardSupport() const;
     bool IsScreenJoystickVisible(int id) const;