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

Added SDL on-screen keyboard support.

Lasse Öörni 12 жил өмнө
parent
commit
d1bbe45515

+ 33 - 0
Source/Engine/Input/Input.cpp

@@ -223,6 +223,20 @@ bool Input::DetectJoysticks()
     return true;
 }
 
+void Input::SetScreenKeyboardVisible(bool enable)
+{
+    if (!graphics_)
+        return;
+    
+    if (enable != IsScreenKeyboardVisible())
+    {
+        if (enable)
+            SDL_StartTextInput();
+        else
+            SDL_StopTextInput();
+    }
+}
+
 bool Input::OpenJoystick(unsigned index)
 {
     if (index >= joysticks_.Size())
@@ -377,6 +391,25 @@ JoystickState* Input::GetJoystick(unsigned index)
         return 0;
 }
 
+bool Input::GetScreenKeyboardSupport() const
+{
+    if (graphics_)
+        return SDL_HasScreenKeyboardSupport() ? true : false;
+    else
+        return false;
+}
+
+bool Input::IsScreenKeyboardVisible() const
+{
+    if (graphics_)
+    {
+        SDL_Window* window = graphics_->GetImpl()->GetWindow();
+        return SDL_IsScreenKeyboardShown(window) ? true : false;
+    }
+    else
+        return false;
+}
+
 bool Input::IsMinimized() const
 {
     // Return minimized state also when unfocused in fullscreen

+ 6 - 0
Source/Engine/Input/Input.h

@@ -136,6 +136,8 @@ public:
     void CloseJoystick(unsigned index);
     /// Redetect joysticks. Return true if successful.
     bool DetectJoysticks();
+    /// Show or hide on-screen keyboard on platforms that support it. When shown, keypresses from it are delivered as key events.
+    void SetScreenKeyboardVisible(bool enable);
     
     /// Check if a key is held down.
     bool GetKeyDown(int key) const;
@@ -173,6 +175,10 @@ public:
     JoystickState* GetJoystick(unsigned index);
     /// Return whether fullscreen toggle is enabled.
     bool GetToggleFullscreen() const { return toggleFullscreen_; }
+    /// Return whether on-screen keyboard is supported.
+    bool GetScreenKeyboardSupport() const;
+    /// Return whether on-screen keyboard is being shown.
+    bool IsScreenKeyboardVisible() const;
     /// Return whether the operating system mouse cursor is visible.
     bool IsMouseVisible() const { return mouseVisible_; }
     /// Return whether application window has input focus.

+ 9 - 4
Source/Engine/LuaScript/pkgs/Input/Input.pkg

@@ -14,12 +14,12 @@ struct JoystickState
     unsigned GetNumButtons() const;
     unsigned GetNumAxes() const;
     unsigned GetNumHats() const;
-    
+
     bool GetButtonDown(unsigned index) const;
     bool GetButtonPress(unsigned index) const;
     float GetAxisPosition(unsigned index) const;
     int GetHatPosition(unsigned index) const;
-    
+
     tolua_readonly tolua_property__get_set unsigned numButtons;
     tolua_readonly tolua_property__get_set unsigned numAxes;
     tolua_readonly tolua_property__get_set unsigned numHats;
@@ -32,7 +32,8 @@ class Input : public Object
     bool OpenJoystick(unsigned index);
     void CloseJoystick(unsigned index);
     bool DetectJoysticks();
-    
+    void SetScreenKeyboardVisible(bool enable);
+
     bool GetKeyDown(int key) const;
     bool GetKeyPress(int key) const;
     bool GetMouseButtonDown(int button) const;
@@ -51,10 +52,12 @@ class Input : public Object
     const String GetJoystickName(unsigned index) const;
     JoystickState* GetJoystick(unsigned index);
     bool GetToggleFullscreen() const;
+    bool GetScreenKeyboardSupport() const;
+    bool IsScreenKeyboardVisible() const;
     bool IsMouseVisible() const;
     bool HasFocus();
     bool IsMinimized() const;
-    
+
     tolua_readonly tolua_property__get_set int qualifiers;
     tolua_readonly tolua_property__get_set IntVector2 mousePosition;
     tolua_readonly tolua_property__get_set IntVector2& mouseMove;
@@ -64,6 +67,8 @@ class Input : public Object
     tolua_readonly tolua_property__get_set unsigned numTouches;
     tolua_readonly tolua_property__get_set unsigned numJoysticks;
     tolua_readonly tolua_property__get_set bool toggleFullscreen;
+    tolua_readonly tolua_property__get_set bool screenKeyboardSupport;
+    tolua_property__is_set bool screenKeyboardVisible;
     tolua_property__is_set bool mouseVisible;
     tolua_readonly tolua_property__has_set bool focus;
     tolua_readonly tolua_property__is_set bool minimized;

+ 3 - 0
Source/Engine/LuaScript/pkgs/UI/UI.pkg

@@ -21,6 +21,7 @@ class UI : public Object
     void SetMaxFontTextureSize(int size);
     void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
     void SetUseSystemClipBoard(bool enable);
+    void SetUseScreenKeyboard(bool enable);
     void SetUseMutableGlyphs(bool enable);
     void SetForceAutoHint(bool enable);
 
@@ -42,6 +43,7 @@ class UI : public Object
     int GetMaxFontTextureSize() const;
     bool IsNonFocusedMouseWheel() const;
     bool GetUseSystemClipBoard() const;
+    bool GetUseScreenKeyboard() const;
     bool GetUseMutableGlyphs() const;
     bool GetForceAutoHint() const;
     bool HasModalElement() const;
@@ -62,6 +64,7 @@ class UI : public Object
     tolua_property__get_set int maxFontTextureSize;
     tolua_property__is_set bool nonFocusedMouseWheel;
     tolua_property__get_set bool useSystemClipBoard;
+    tolua_property__get_set bool useScreenKeyboard;
     tolua_property__get_set bool useMutableGlyphs;
     tolua_property__get_set bool forceAutoHint;
     tolua_readonly tolua_property__has_set bool modalElement;

+ 3 - 0
Source/Engine/Script/InputAPI.cpp

@@ -148,6 +148,9 @@ static void RegisterInput(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Input", "bool DetectJoysticks()", asMETHOD(Input, DetectJoysticks), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "void set_mouseVisible(bool)", asMETHOD(Input, SetMouseVisible), asCALL_THISCALL);
     engine->RegisterObjectMethod("Input", "bool get_mouseVisible() const", asMETHOD(Input, IsMouseVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "void set_screenKeyboardVisible(bool)", asMETHOD(Input, SetScreenKeyboardVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "bool get_screenKeyboardVisible() const", asMETHOD(Input, IsScreenKeyboardVisible), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Input", "bool get_screenKeyboardSupport() const", asMETHOD(Input, GetScreenKeyboardSupport), 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);

+ 2 - 0
Source/Engine/Script/UIAPI.cpp

@@ -652,6 +652,8 @@ static void RegisterUI(asIScriptEngine* engine)
     engine->RegisterObjectMethod("UI", "bool get_nonFocusedMouseWheel() const", asMETHOD(UI, IsNonFocusedMouseWheel), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_useSystemClipBoard(bool)", asMETHOD(UI, SetUseSystemClipBoard), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_useSystemClipBoard() const", asMETHOD(UI, GetUseSystemClipBoard), asCALL_THISCALL);
+    engine->RegisterObjectMethod("UI", "void set_useScreenKeyboard(bool)", asMETHOD(UI, SetUseScreenKeyboard), asCALL_THISCALL);
+    engine->RegisterObjectMethod("UI", "bool get_useScreenKeyboard() const", asMETHOD(UI, GetUseScreenKeyboard), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_useMutableGlyphs(bool)", asMETHOD(UI, SetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "bool get_useMutableGlyphs() const", asMETHOD(UI, GetUseMutableGlyphs), asCALL_THISCALL);
     engine->RegisterObjectMethod("UI", "void set_forceAutoHint(bool)", asMETHOD(UI, SetForceAutoHint), asCALL_THISCALL);

+ 16 - 1
Source/Engine/UI/LineEdit.cpp

@@ -61,7 +61,7 @@ LineEdit::LineEdit(Context* context) :
 
     SubscribeToEvent(this, E_FOCUSED, HANDLER(LineEdit, HandleFocused));
     SubscribeToEvent(this, E_DEFOCUSED, HANDLER(LineEdit, HandleDefocused));
-    SubscribeToEvent(this, E_LAYOUTUPDATED, HANDLER(LineEdit, HandleFocused));
+    SubscribeToEvent(this, E_LAYOUTUPDATED, HANDLER(LineEdit, HandleLayoutUpdated));
 }
 
 LineEdit::~LineEdit()
@@ -386,6 +386,10 @@ void LineEdit::OnKey(int key, int buttons, int qualifiers)
     case KEY_RETURN2:
     case KEY_KP_ENTER:
         {
+            // If using the on-screen keyboard, defocus this element to hide it now
+            if (GetSubsystem<UI>()->GetUseScreenKeyboard() && HasFocus())
+                SetFocus(false);
+            
             using namespace TextFinished;
 
             VariantMap eventData;
@@ -617,11 +621,22 @@ void LineEdit::HandleFocused(StringHash eventType, VariantMap& eventData)
         text_->SetSelection(0);
     }
     UpdateCursor();
+    
+    if (GetSubsystem<UI>()->GetUseScreenKeyboard())
+        GetSubsystem<Input>()->SetScreenKeyboardVisible(true);
 }
 
 void LineEdit::HandleDefocused(StringHash eventType, VariantMap& eventData)
 {
     text_->ClearSelection();
+    
+    if (GetSubsystem<UI>()->GetUseScreenKeyboard())
+        GetSubsystem<Input>()->SetScreenKeyboardVisible(false);
+}
+
+void LineEdit::HandleLayoutUpdated(StringHash eventType, VariantMap& eventData)
+{
+    UpdateCursor();
 }
 
 }

+ 2 - 0
Source/Engine/UI/LineEdit.h

@@ -146,6 +146,8 @@ private:
     void HandleFocused(StringHash eventType, VariantMap& eventData);
     /// Handle being defocused.
     void HandleDefocused(StringHash eventType, VariantMap& eventData);
+    /// Handle the element layout having been updated.
+    void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData);
 };
 
 }

+ 10 - 0
Source/Engine/UI/UI.cpp

@@ -95,6 +95,11 @@ UI::UI(Context* context) :
     nonFocusedMouseWheel_(true),     // Default Mac OS X and Linux behaviour
     #endif
     useSystemClipBoard_(false),
+    #if defined(ANDROID) || defined(IOS)
+    useScreenKeyboard_(true),
+    #else
+    useScreenKeyboard_(false),
+    #endif
     useMutableGlyphs_(false),
     forceAutoHint_(false),
     dragBeginPending_(false),
@@ -493,6 +498,11 @@ void UI::SetUseSystemClipBoard(bool enable)
     useSystemClipBoard_ = enable;
 }
 
+void UI::SetUseScreenKeyboard(bool enable)
+{
+    useScreenKeyboard_ = enable;
+}
+
 void UI::SetUseMutableGlyphs(bool enable)
 {
     if (enable != useMutableGlyphs_)

+ 6 - 0
Source/Engine/UI/UI.h

@@ -89,6 +89,8 @@ public:
     void SetNonFocusedMouseWheel(bool nonFocusedMouseWheel);
     /// Set whether to use system clipboard. Default false.
     void SetUseSystemClipBoard(bool enable);
+    /// Set whether to show the on-screen keyboard (if supported) when a %LineEdit is focused. Default true on mobile devices.
+    void SetUseScreenKeyboard(bool enable);
     /// Set whether to use mutable (eraseable) glyphs to ensure a font face never expands to more than one texture. Default false.
     void SetUseMutableGlyphs(bool enable);
     /// Set whether to force font autohinting instead of using FreeType's TTF bytecode interpreter.
@@ -128,6 +130,8 @@ public:
     bool IsNonFocusedMouseWheel() const { return nonFocusedMouseWheel_; }
     /// Return whether is using the system clipboard.
     bool GetUseSystemClipBoard() const { return useSystemClipBoard_; }
+    /// Return whether focusing a %LineEdit will show the on-screen keyboard.
+    bool GetUseScreenKeyboard() const { return useScreenKeyboard_; }
     /// Return whether is using mutable (eraseable) glyphs for fonts.
     bool GetUseMutableGlyphs() const { return useMutableGlyphs_; }
     /// Return whether is using forced autohinting.
@@ -259,6 +263,8 @@ private:
     bool nonFocusedMouseWheel_;
     /// Flag for using operating system clipboard instead of internal.
     bool useSystemClipBoard_;
+    /// Flag for showing the on-screen keyboard on focusing a %LineEdit.
+    bool useScreenKeyboard_;
     /// Flag for using mutable (eraseable) font glyphs.
     bool useMutableGlyphs_;
     /// Flag for forcing FreeType autohinting.