mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
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
This commit is contained in:
@@ -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<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
|
||||
|
||||
|
||||
@@ -30,25 +30,25 @@ namespace touch
|
||||
namespace sdl
|
||||
{
|
||||
|
||||
int Touch::getTouchCount() const
|
||||
std::vector<int64> Touch::getTouchIDs() const
|
||||
{
|
||||
return (int) touches.size();
|
||||
std::vector<int64> 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<int64, TouchInfo>::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
|
||||
|
||||
@@ -43,8 +43,8 @@ public:
|
||||
|
||||
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.
|
||||
virtual const char *getName() const;
|
||||
|
||||
@@ -32,31 +32,40 @@ namespace 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;
|
||||
}
|
||||
|
||||
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 }
|
||||
};
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user