Browse Source

Added a love.keyboard.setTextInput variant (setTextInput(enable, x, y, width, height)) to specify a rectangle where text is expected to be displayed on the screen. This is used as a hint so on-screen keyboards won't cover the text area.
Added love.keyboard.hasScreenKeyboard.

--HG--
branch : minor

Alex Szpakowski 10 years ago
parent
commit
44d10be928

+ 15 - 2
src/modules/keyboard/Keyboard.h

@@ -563,16 +563,29 @@ public:
 	virtual Scancode getScancodeFromKey(Key key) const = 0;
 
 	/**
-	 * Sets whether text input events should be sent
-	 * @param enable Whether to send text input events.
+	 * Sets whether text input events should be received.
+	 * @param enable Whether to receive text input events.
 	 **/
 	virtual void setTextInput(bool enable) = 0;
 
+	/**
+	 * Sets whether text input events should be received, and specifies where
+	 * on the screen the text will appear. This is used as a hint so on-screen
+	 * keyboards don't cover the text area.
+	 **/
+	virtual void setTextInput(bool enable, int x, int y, int w, int h) = 0;
+
 	/**
 	 * Gets whether text input events are enabled.
 	 **/
 	virtual bool hasTextInput() const = 0;
 
+	/**
+	 * Gets whether the system will display an on-screen keyboard when text input
+	 * events are enabled.
+	 **/
+	virtual bool hasScreenKeyboard() const = 0;
+
 	static bool getConstant(const char *in, Key &out);
 	static bool getConstant(Key in, const char *&out);
 

+ 15 - 0
src/modules/keyboard/sdl/Keyboard.cpp

@@ -117,11 +117,26 @@ void Keyboard::setTextInput(bool enable)
 		SDL_StopTextInput();
 }
 
+void Keyboard::setTextInput(bool enable, int x, int y, int w, int h)
+{
+	// TODO: SetTextInputRect expects coordinates in window-space, but
+	// setTextInput uses pixels. We should convert here.
+	SDL_Rect rect = {x, y, w, h};
+	SDL_SetTextInputRect(&rect);
+
+	setTextInput(enable);
+}
+
 bool Keyboard::hasTextInput() const
 {
 	return SDL_IsTextInputActive();
 }
 
+bool Keyboard::hasScreenKeyboard() const
+{
+	return SDL_HasScreenKeyboardSupport();
+}
+
 bool Keyboard::getConstant(Scancode in, SDL_Scancode &out)
 {
 	return scancodes.find(in, out);

+ 2 - 0
src/modules/keyboard/sdl/Keyboard.h

@@ -53,7 +53,9 @@ public:
 	Scancode getScancodeFromKey(Key key) const;
 
 	void setTextInput(bool enable);
+	void setTextInput(bool enable, int x, int y, int w, int h);
 	bool hasTextInput() const;
+	bool hasScreenKeyboard() const;
 
 	static bool getConstant(Scancode in, SDL_Scancode &out);
 	static bool getConstant(SDL_Scancode in, Scancode &out);

+ 20 - 1
src/modules/keyboard/wrap_Keyboard.cpp

@@ -113,7 +113,19 @@ int w_getKeyFromScancode(lua_State *L)
 
 int w_setTextInput(lua_State *L)
 {
-	instance()->setTextInput(luax_toboolean(L, 1));
+	bool enable = luax_toboolean(L, 1);
+
+	if (lua_gettop(L) <= 1)
+		instance()->setTextInput(enable);
+	else
+	{
+		int x = (int) luaL_checkinteger(L, 2);
+		int y = (int) luaL_checkinteger(L, 3);
+		int w = (int) luaL_checkinteger(L, 4);
+		int h = (int) luaL_checkinteger(L, 5);
+		instance()->setTextInput(enable, x, y, w, h);
+	}
+
 	return 0;
 }
 
@@ -123,6 +135,12 @@ int w_hasTextInput(lua_State *L)
 	return 1;
 }
 
+int w_hasScreenKeyboard(lua_State *L)
+{
+	luax_pushboolean(L, instance()->hasScreenKeyboard());
+	return 1;
+}
+
 // List of functions to wrap.
 static const luaL_Reg functions[] =
 {
@@ -130,6 +148,7 @@ static const luaL_Reg functions[] =
 	{ "hasKeyRepeat", w_hasKeyRepeat },
 	{ "setTextInput", w_setTextInput },
 	{ "hasTextInput", w_hasTextInput },
+	{ "hasScreenKeyboard", w_hasScreenKeyboard },
 	{ "isDown", w_isDown },
 	{ "isScancodeDown", w_isScancodeDown },
 	{ "getScancodeFromKey", w_getScancodeFromKey },

+ 1 - 0
src/modules/keyboard/wrap_Keyboard.h

@@ -38,6 +38,7 @@ int w_getKeyFromScancode(lua_State *L);
 int w_getScancodeFromkey(lua_State *L);
 int w_setTextInput(lua_State *L);
 int w_hasTextInput(lua_State *L);
+int w_hasScreenKeyboard(lua_State *L);
 extern "C" LOVE_EXPORT int luaopen_love_keyboard(lua_State *L);
 
 } // keyboard