diff --git a/src/modules/touch/Touch.h b/src/modules/touch/Touch.h index 08c7557bf..d4012190d 100644 --- a/src/modules/touch/Touch.h +++ b/src/modules/touch/Touch.h @@ -55,19 +55,14 @@ public: virtual ModuleType getModuleType() const { return M_TOUCH; } /** - * Gets a list of the IDs of all currently active touches. + * Gets all currently active touches. **/ - virtual std::vector getTouches() const = 0; + virtual const std::vector &getTouches() const = 0; /** - * Gets the position in pixels of a specific touch, using its ID. + * Gets a specific touch, using its ID. **/ - virtual void getPosition(int64 id, double &x, double &y) const = 0; - - /** - * Gets the pressure of a specific touch, using its ID. - **/ - virtual double getPressure(int64 id) const = 0; + virtual const TouchInfo &getTouch(int64 id) const = 0; }; // Touch diff --git a/src/modules/touch/sdl/Touch.cpp b/src/modules/touch/sdl/Touch.cpp index 9f8885641..8409e8ac2 100644 --- a/src/modules/touch/sdl/Touch.cpp +++ b/src/modules/touch/sdl/Touch.cpp @@ -23,6 +23,9 @@ #include "common/Exception.h" #include "Touch.h" +// C++ +#include + namespace love { namespace touch @@ -30,34 +33,20 @@ namespace touch namespace sdl { -std::vector Touch::getTouches() const +const std::vector &Touch::getTouches() const { - std::vector ids; - ids.reserve(touches.size()); + return touches; +} +const Touch::TouchInfo &Touch::getTouch(int64 id) const +{ for (const auto &touch : touches) - ids.push_back(touch.first); + { + if (touch.id == id) + return touch; + } - return ids; -} - -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); - - x = it->second.x; - y = it->second.y; -} - -double Touch::getPressure(int64 id) const -{ - const auto it = touches.find(id); - if (it == touches.end()) - throw love::Exception("Invalid active touch ID: %d", id); - - return it->second.pressure; + throw love::Exception("Invalid active touch ID: %d", id); } const char *Touch::getName() const @@ -67,14 +56,28 @@ const char *Touch::getName() const void Touch::onEvent(Uint32 eventtype, const TouchInfo &info) { + auto compare = [&](const TouchInfo &touch) -> bool + { + return touch.id == info.id; + }; + switch (eventtype) { case SDL_FINGERDOWN: - case SDL_FINGERMOTION: - touches[info.id] = info; + touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end()); + touches.push_back(info); break; + case SDL_FINGERMOTION: + { + for (TouchInfo &touch : touches) + { + if (touch.id == info.id) + touch = info; + } + break; + } case SDL_FINGERUP: - touches.erase(info.id); + touches.erase(std::remove_if(touches.begin(), touches.end(), compare), touches.end()); break; default: break; diff --git a/src/modules/touch/sdl/Touch.h b/src/modules/touch/sdl/Touch.h index 207303200..1d2d09f7a 100644 --- a/src/modules/touch/sdl/Touch.h +++ b/src/modules/touch/sdl/Touch.h @@ -24,9 +24,6 @@ // LOVE #include "touch/Touch.h" -// C++ -#include - // SDL #include @@ -43,9 +40,8 @@ public: virtual ~Touch() {} - std::vector getTouches() const override; - void getPosition(int64 id, double &x, double &y) const override; - double getPressure(int64 id) const override; + const std::vector &getTouches() const override; + const TouchInfo &getTouch(int64 id) const override; // Implements Module. const char *getName() const override; @@ -59,8 +55,8 @@ public: private: - // All current touches, indexed by their IDs. - std::map touches; + // All current touches. + std::vector touches; }; // Touch diff --git a/src/modules/touch/wrap_Touch.cpp b/src/modules/touch/wrap_Touch.cpp index 95d51aafb..7967f3148 100644 --- a/src/modules/touch/wrap_Touch.cpp +++ b/src/modules/touch/wrap_Touch.cpp @@ -42,18 +42,18 @@ int64 luax_checktouchid(lua_State *L, int idx) int w_getTouches(lua_State *L) { - std::vector ids = instance()->getTouches(); + const std::vector &touches = instance()->getTouches(); - lua_createtable(L, (int) ids.size(), 0); + lua_createtable(L, (int) touches.size(), 0); - for (size_t i = 0; i < ids.size(); i++) + for (size_t i = 0; i < touches.size(); i++) { // This is a bit hackish and we lose the higher 32 bits of the id on // 32-bit systems, but SDL only ever gives id's that at most use as many // bits as can fit in a pointer (for now.) // We use lightuserdata instead of a lua_Number (double) because doubles // can't represent all possible id values on 64-bit systems. - lua_pushlightuserdata(L, (void *) (intptr_t) ids[i]); + lua_pushlightuserdata(L, (void *) (intptr_t) touches[i].id); lua_rawseti(L, -2, (int) i + 1); } @@ -64,12 +64,11 @@ int w_getPosition(lua_State *L) { int64 id = luax_checktouchid(L, 1); - double x = 0; - double y = 0; - luax_catchexcept(L, [&]() { instance()->getPosition(id, x, y); }); + Touch::TouchInfo touch = {}; + luax_catchexcept(L, [&]() { touch = instance()->getTouch(id); }); - lua_pushnumber(L, x); - lua_pushnumber(L, y); + lua_pushnumber(L, touch.x); + lua_pushnumber(L, touch.y); return 2; } @@ -77,9 +76,11 @@ int w_getPosition(lua_State *L) int w_getPressure(lua_State *L) { int64 id = luax_checktouchid(L, 1); - double pressure = 0.0; - luax_catchexcept(L, [&](){ pressure = instance()->getPressure(id); }); - lua_pushnumber(L, pressure); + + Touch::TouchInfo touch = {}; + luax_catchexcept(L, [&](){ touch = instance()->getTouch(id); }); + + lua_pushnumber(L, touch.pressure); return 1; }