From 77d90e2260f5a4032f8e686ad0e473273e20ce69 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 17 Feb 2015 18:33:59 -0400 Subject: [PATCH] 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 --- src/modules/touch/Touch.h | 10 ++++----- src/modules/touch/sdl/Touch.cpp | 28 ++++++++++++------------- src/modules/touch/sdl/Touch.h | 4 ++-- src/modules/touch/wrap_Touch.cpp | 35 ++++++++++++++++++++------------ src/modules/touch/wrap_Touch.h | 4 ++-- 5 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/modules/touch/Touch.h b/src/modules/touch/Touch.h index b45581c53..21cbd2e4e 100644 --- a/src/modules/touch/Touch.h +++ b/src/modules/touch/Touch.h @@ -54,16 +54,14 @@ public: 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 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 diff --git a/src/modules/touch/sdl/Touch.cpp b/src/modules/touch/sdl/Touch.cpp index 3a32c9b40..b9fb13c7d 100644 --- a/src/modules/touch/sdl/Touch.cpp +++ b/src/modules/touch/sdl/Touch.cpp @@ -30,25 +30,25 @@ namespace touch namespace sdl { -int Touch::getTouchCount() const +std::vector Touch::getTouchIDs() const { - return (int) touches.size(); + std::vector ids; + ids.reserve(touches.size()); + + for (const auto &touch : touches) + ids.push_back(touch.first); + + return ids; } -Touch::TouchInfo Touch::getTouch(int index) const +void Touch::getPosition(int64 id, double &x, double &y) const { - if (index < 0) - throw love::Exception("Invalid touch index: %d", index); + const auto it = touches.find(id); + if (it == touches.end()) + throw love::Exception("Invalid active touch ID: %d", id); - std::map::const_iterator it = touches.begin(); - - for (int i = 0; i < index; i++) - { - if (++it == touches.end()) - throw love::Exception("Invalid touch index: %d", index); - } - - return it->second; + x = it->second.x; + y = it->second.y; } const char *Touch::getName() const diff --git a/src/modules/touch/sdl/Touch.h b/src/modules/touch/sdl/Touch.h index c123e599f..bfe263146 100644 --- a/src/modules/touch/sdl/Touch.h +++ b/src/modules/touch/sdl/Touch.h @@ -43,8 +43,8 @@ public: virtual ~Touch() {} - virtual int getTouchCount() const; - virtual TouchInfo getTouch(int index) const; + virtual std::vector getTouchIDs() const; + virtual void getPosition(int64 id, double &x, double &y) const; // Implements Module. virtual const char *getName() const; diff --git a/src/modules/touch/wrap_Touch.cpp b/src/modules/touch/wrap_Touch.cpp index 45e586ed2..bd5ccdfc2 100644 --- a/src/modules/touch/wrap_Touch.cpp +++ b/src/modules/touch/wrap_Touch.cpp @@ -32,31 +32,40 @@ namespace touch #define instance() (Module::getInstance(Module::M_TOUCH)) -int w_getTouchCount(lua_State *L) +int w_getTouchIDs(lua_State *L) { - lua_pushinteger(L, instance()->getTouchCount()); + std::vector 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; } -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[] = { - { "getTouchCount", w_getTouchCount }, - { "getTouch", w_getTouch }, + { "getTouchIDs", w_getTouchIDs }, + { "getPosition", w_getPosition }, { 0, 0 } }; diff --git a/src/modules/touch/wrap_Touch.h b/src/modules/touch/wrap_Touch.h index 89d376c54..1d8942460 100644 --- a/src/modules/touch/wrap_Touch.h +++ b/src/modules/touch/wrap_Touch.h @@ -30,8 +30,8 @@ namespace love 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); } // touch