Browse Source

Removed love.touch.getTouchCount and love.touch.getTouch. Added love.touch.getTouchIDs and love.touch.getPosition.

love.touch.getTouchIDs returns an array of all currently active touch IDs. love.touch.getPosition takes an active touch ID and returns the current position of that touch.

--HG--
branch : minor
Alex Szpakowski 10 years ago
parent
commit
77d90e2260

+ 4 - 6
src/modules/touch/Touch.h

@@ -54,16 +54,14 @@ public:
 	virtual ModuleType getModuleType() const { return M_TOUCH; }
 	virtual ModuleType getModuleType() const { return M_TOUCH; }
 
 
 	/**
 	/**
-	 * Gets the number of current touch presses.
+	 * Gets a list of the IDs of all currently active touches.
 	 **/
 	 **/
-	virtual int getTouchCount() const = 0;
+	virtual std::vector<int64> getTouchIDs() const = 0;
 
 
 	/**
 	/**
-	 * Gets information about a touch press. The index should only be used for
-	 * iterating over the current touch presses - it may change throughout a
-	 * touch press' lifetime.
+	 * Gets the position in pixels of a specific touch, using its ID.
 	 **/
 	 **/
-	virtual TouchInfo getTouch(int index) const = 0;
+	virtual void getPosition(int64 id, double &x, double &y) const = 0;
 
 
 }; // Touch
 }; // Touch
 
 

+ 14 - 14
src/modules/touch/sdl/Touch.cpp

@@ -30,25 +30,25 @@ namespace touch
 namespace sdl
 namespace sdl
 {
 {
 
 
-int Touch::getTouchCount() const
+std::vector<int64> Touch::getTouchIDs() const
 {
 {
-	return (int) touches.size();
-}
+	std::vector<int64> ids;
+	ids.reserve(touches.size());
 
 
-Touch::TouchInfo Touch::getTouch(int index) const
-{
-	if (index < 0)
-		throw love::Exception("Invalid touch index: %d", index);
+	for (const auto &touch : touches)
+		ids.push_back(touch.first);
 
 
-	std::map<int64, TouchInfo>::const_iterator it = touches.begin();
+	return ids;
+}
 
 
-	for (int i = 0; i < index; i++)
-	{
-		if (++it == touches.end())
-			throw love::Exception("Invalid touch index: %d", index);
-	}
+void Touch::getPosition(int64 id, double &x, double &y) const
+{
+	const auto it = touches.find(id);
+	if (it == touches.end())
+		throw love::Exception("Invalid active touch ID: %d", id);
 
 
-	return it->second;
+	x = it->second.x;
+	y = it->second.y;
 }
 }
 
 
 const char *Touch::getName() const
 const char *Touch::getName() const

+ 2 - 2
src/modules/touch/sdl/Touch.h

@@ -43,8 +43,8 @@ public:
 
 
 	virtual ~Touch() {}
 	virtual ~Touch() {}
 
 
-	virtual int getTouchCount() const;
-	virtual TouchInfo getTouch(int index) const;
+	virtual std::vector<int64> getTouchIDs() const;
+	virtual void getPosition(int64 id, double &x, double &y) const;
 
 
 	// Implements Module.
 	// Implements Module.
 	virtual const char *getName() const;
 	virtual const char *getName() const;

+ 22 - 13
src/modules/touch/wrap_Touch.cpp

@@ -32,31 +32,40 @@ namespace touch
 
 
 #define instance() (Module::getInstance<Touch>(Module::M_TOUCH))
 #define instance() (Module::getInstance<Touch>(Module::M_TOUCH))
 
 
-int w_getTouchCount(lua_State *L)
+int w_getTouchIDs(lua_State *L)
 {
 {
-	lua_pushinteger(L, instance()->getTouchCount());
+	std::vector<int64> ids = instance()->getTouchIDs();
+
+	lua_createtable(L, (int) ids.size(), 0);
+
+	for (size_t i = 0; i < ids.size(); i++)
+	{
+		// Lets hope the ID can be accurately represented in a Lua number...
+		lua_pushnumber(L, (lua_Number) ids[i]);
+		lua_rawseti(L, -2, i + 1);
+	}
+
 	return 1;
 	return 1;
 }
 }
 
 
-int w_getTouch(lua_State *L)
+int w_getPosition(lua_State *L)
 {
 {
-	int index = luaL_checkint(L, 1) - 1;
+	int64 id = (int64) luaL_checknumber(L, 1);
 
 
-	Touch::TouchInfo info;
-	luax_catchexcept(L, [&](){ info = instance()->getTouch(index); });
+	double x = 0;
+	double y = 0;
+	luax_catchexcept(L, [&]() { instance()->getPosition(id, x, y); });
 
 
-	// Lets hope the ID can be accurately represented in a Lua number...
-	lua_pushnumber(L, (lua_Number) info.id);
-	lua_pushnumber(L, info.x);
-	lua_pushnumber(L, info.y);
+	lua_pushnumber(L, x);
+	lua_pushnumber(L, y);
 
 
-	return 3;
+	return 2;
 }
 }
 
 
 static const luaL_Reg functions[] =
 static const luaL_Reg functions[] =
 {
 {
-	{ "getTouchCount", w_getTouchCount },
-	{ "getTouch", w_getTouch },
+	{ "getTouchIDs", w_getTouchIDs },
+	{ "getPosition", w_getPosition },
 	{ 0, 0 }
 	{ 0, 0 }
 };
 };
 
 

+ 2 - 2
src/modules/touch/wrap_Touch.h

@@ -30,8 +30,8 @@ namespace love
 namespace touch
 namespace touch
 {
 {
 
 
-int w_getTouchCount(lua_State *L);
-int w_getTouch(lua_State *L);
+int w_getTouchIDs(lua_State *L);
+int w_getPosition(lua_State *L);
 extern "C" LOVE_EXPORT int luaopen_love_touch(lua_State *L);
 extern "C" LOVE_EXPORT int luaopen_love_touch(lua_State *L);
 
 
 } // touch
 } // touch