Added love.touch.getPressure(touchid). Not useful on most touch-capable devices, but I do expect Apple's Force Touch to make it into iPhones in the near future.

This commit is contained in:
Alex Szpakowski
2015-03-24 20:45:21 -03:00
parent b8cb16dc95
commit 78dd8bc547
8 changed files with 62 additions and 26 deletions
+2
View File
@@ -277,6 +277,7 @@ Message *Event::convert(const SDL_Event &e) const
touchinfo.y = e.tfinger.y; touchinfo.y = e.tfinger.y;
touchinfo.dx = e.tfinger.dx; touchinfo.dx = e.tfinger.dx;
touchinfo.dy = e.tfinger.dy; touchinfo.dy = e.tfinger.dy;
touchinfo.pressure = e.tfinger.pressure;
#ifdef LOVE_LINUX #ifdef LOVE_LINUX
// FIXME: hacky workaround for SDL not normalizing touch coordinates in // FIXME: hacky workaround for SDL not normalizing touch coordinates in
@@ -310,6 +311,7 @@ Message *Event::convert(const SDL_Event &e) const
vargs.push_back(new Variant(touchinfo.y)); vargs.push_back(new Variant(touchinfo.y));
vargs.push_back(new Variant(touchinfo.dx)); vargs.push_back(new Variant(touchinfo.dx));
vargs.push_back(new Variant(touchinfo.dy)); vargs.push_back(new Variant(touchinfo.dy));
vargs.push_back(new Variant(touchinfo.pressure));
if (e.type == SDL_FINGERDOWN) if (e.type == SDL_FINGERDOWN)
txt = "touchpressed"; txt = "touchpressed";
+6
View File
@@ -46,6 +46,7 @@ public:
double y; // Position in pixels along the y-axis. double y; // Position in pixels along the y-axis.
double dx; // Amount in pixels moved along the x-axis. double dx; // Amount in pixels moved along the x-axis.
double dy; // Amount in pixels moved along the y-axis. double dy; // Amount in pixels moved along the y-axis.
double pressure;
}; };
virtual ~Touch() {} virtual ~Touch() {}
@@ -63,6 +64,11 @@ public:
**/ **/
virtual void getPosition(int64 id, double &x, double &y) const = 0; 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;
}; // Touch }; // Touch
} // touch } // touch
+9
View File
@@ -51,6 +51,15 @@ void Touch::getPosition(int64 id, double &x, double &y) const
y = it->second.y; 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;
}
const char *Touch::getName() const const char *Touch::getName() const
{ {
return "love.touch.sdl"; return "love.touch.sdl";
+4 -3
View File
@@ -43,11 +43,12 @@ public:
virtual ~Touch() {} virtual ~Touch() {}
virtual std::vector<int64> getIDs() const; std::vector<int64> getIDs() const override;
virtual void getPosition(int64 id, double &x, double &y) const; void getPosition(int64 id, double &x, double &y) const override;
double getPressure(int64 id) const override;
// Implements Module. // Implements Module.
virtual const char *getName() const; const char *getName() const override;
// SDL has functions to query the state of touch presses, but unfortunately // SDL has functions to query the state of touch presses, but unfortunately
// they are updated on a different thread in some backends, which causes // they are updated on a different thread in some backends, which causes
+19 -5
View File
@@ -19,7 +19,6 @@
**/ **/
#include "common/config.h" #include "common/config.h"
#include "common/int.h"
// LOVE // LOVE
#include "wrap_Touch.h" #include "wrap_Touch.h"
@@ -33,6 +32,14 @@ namespace touch
#define instance() (Module::getInstance<Touch>(Module::M_TOUCH)) #define instance() (Module::getInstance<Touch>(Module::M_TOUCH))
int64 luax_checktouchid(lua_State *L, int idx)
{
if (!lua_islightuserdata(L, idx))
return luax_typerror(L, idx, "touch id");
return (int64) (intptr_t) lua_touserdata(L, 1);
}
int w_getIDs(lua_State *L) int w_getIDs(lua_State *L)
{ {
std::vector<int64> ids = instance()->getIDs(); std::vector<int64> ids = instance()->getIDs();
@@ -55,10 +62,7 @@ int w_getIDs(lua_State *L)
int w_getPosition(lua_State *L) int w_getPosition(lua_State *L)
{ {
if (!lua_islightuserdata(L, 1)) int64 id = luax_checktouchid(L, 1);
return luax_typerror(L, 1, "touch id");
int64 id = (int64) (intptr_t) lua_touserdata(L, 1);
double x = 0; double x = 0;
double y = 0; double y = 0;
@@ -70,10 +74,20 @@ int w_getPosition(lua_State *L)
return 2; return 2;
} }
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);
return 1;
}
static const luaL_Reg functions[] = static const luaL_Reg functions[] =
{ {
{ "getIDs", w_getIDs }, { "getIDs", w_getIDs },
{ "getPosition", w_getPosition }, { "getPosition", w_getPosition },
{ "getPressure", w_getPressure },
{ 0, 0 } { 0, 0 }
}; };
+3
View File
@@ -24,14 +24,17 @@
// LOVE // LOVE
#include "Touch.h" #include "Touch.h"
#include "common/runtime.h" #include "common/runtime.h"
#include "common/int.h"
namespace love namespace love
{ {
namespace touch namespace touch
{ {
int64 luax_checktouchid(lua_State *L, int idx);
int w_getIDs(lua_State *L); int w_getIDs(lua_State *L);
int w_getPosition(lua_State *L); int w_getPosition(lua_State *L);
int w_getPressure(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
+8 -8
View File
@@ -178,14 +178,14 @@ function love.createhandlers()
wheelmoved = function (x,y) wheelmoved = function (x,y)
if love.wheelmoved then return love.wheelmoved(x,y) end if love.wheelmoved then return love.wheelmoved(x,y) end
end, end,
touchpressed = function (id,x,y,dx,dy) touchpressed = function (id,x,y,dx,dy,p)
if love.touchpressed then return love.touchpressed(id,x,y,dx,dy) end if love.touchpressed then return love.touchpressed(id,x,y,dx,dy,p) end
end, end,
touchreleased = function (id,x,y,dx,dy) touchreleased = function (id,x,y,dx,dy,p)
if love.touchreleased then return love.touchreleased(id,x,y,dx,dy) end if love.touchreleased then return love.touchreleased(id,x,y,dx,dy,p) end
end, end,
touchmoved = function (id,x,y,dx,dy) touchmoved = function (id,x,y,dx,dy,p)
if love.touchmoved then return love.touchmoved(id,x,y,dx,dy) end if love.touchmoved then return love.touchmoved(id,x,y,dx,dy,p) end
end, end,
joystickpressed = function (j,b) joystickpressed = function (j,b)
if love.joystickpressed then return love.joystickpressed(j,b) end if love.joystickpressed then return love.joystickpressed(j,b) end
@@ -504,13 +504,13 @@ function love.run()
-- Process events. -- Process events.
if love.event then if love.event then
love.event.pump() love.event.pump()
for name, a,b,c,d,e in love.event.poll() do for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then if name == "quit" then
if not love.quit or not love.quit() then if not love.quit or not love.quit() then
return return
end end
end end
love.handlers[name](a,b,c,d,e) love.handlers[name](a,b,c,d,e,f)
end end
end end
+11 -10
View File
@@ -316,28 +316,29 @@ const unsigned char boot_lua[] =
0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66,
0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78,
0x2c, 0x64, 0x79, 0x29, 0x0a, 0x2c, 0x64, 0x79, 0x2c, 0x70, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72,
0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20,
0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x28, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x28,
0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x2c, 0x70, 0x29, 0x20, 0x65, 0x6e,
0x64, 0x0a,
0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x20, 0x3d, 0x20,
0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64,
0x78, 0x2c, 0x64, 0x79, 0x29, 0x0a, 0x78, 0x2c, 0x64, 0x79, 0x2c, 0x70, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65,
0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e,
0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x72, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65,
0x64, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x2c, 0x70, 0x29, 0x20,
0x64, 0x0a, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x09, 0x09, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x75, 0x6e,
0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64,
0x79, 0x29, 0x0a, 0x79, 0x2c, 0x70, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f,
0x76, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x20, 0x6c, 0x6f,
0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x28, 0x69, 0x64, 0x2c, 0x78, 0x76, 0x65, 0x2e, 0x74, 0x6f, 0x75, 0x63, 0x68, 0x6d, 0x6f, 0x76, 0x65, 0x64, 0x28, 0x69, 0x64, 0x2c, 0x78,
0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a, 0x2c, 0x79, 0x2c, 0x64, 0x78, 0x2c, 0x64, 0x79, 0x2c, 0x70, 0x29, 0x20, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x2c, 0x0a,
0x09, 0x09, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20, 0x09, 0x09, 0x6a, 0x6f, 0x79, 0x73, 0x74, 0x69, 0x63, 0x6b, 0x70, 0x72, 0x65, 0x73, 0x73, 0x65, 0x64, 0x20,
0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x6a, 0x2c, 0x62, 0x29, 0x0a, 0x3d, 0x20, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x28, 0x6a, 0x2c, 0x62, 0x29, 0x0a,
@@ -921,8 +922,8 @@ const unsigned char boot_lua[] =
0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x75, 0x6d, 0x70, 0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x75, 0x6d, 0x70,
0x28, 0x29, 0x0a, 0x28, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x61, 0x2c, 0x62, 0x2c, 0x63, 0x09, 0x09, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x61, 0x2c, 0x62, 0x2c, 0x63,
0x2c, 0x64, 0x2c, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x2c, 0x64, 0x2c, 0x65, 0x2c, 0x66, 0x20, 0x69, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65,
0x2e, 0x70, 0x6f, 0x6c, 0x6c, 0x28, 0x29, 0x20, 0x64, 0x6f, 0x0a, 0x6e, 0x74, 0x2e, 0x70, 0x6f, 0x6c, 0x6c, 0x28, 0x29, 0x20, 0x64, 0x6f, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x71, 0x75, 0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x61, 0x6d, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x22, 0x71, 0x75,
0x69, 0x74, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a, 0x69, 0x74, 0x22, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x71, 0x09, 0x09, 0x09, 0x09, 0x09, 0x69, 0x66, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x71,
@@ -932,7 +933,7 @@ const unsigned char boot_lua[] =
0x09, 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x73, 0x5b, 0x09, 0x09, 0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x73, 0x5b,
0x6e, 0x61, 0x6d, 0x65, 0x5d, 0x28, 0x61, 0x2c, 0x62, 0x2c, 0x63, 0x2c, 0x64, 0x2c, 0x65, 0x29, 0x0a, 0x6e, 0x61, 0x6d, 0x65, 0x5d, 0x28, 0x61, 0x2c, 0x62, 0x2c, 0x63, 0x2c, 0x64, 0x2c, 0x65, 0x2c, 0x66, 0x29, 0x0a,
0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a, 0x09, 0x09, 0x65, 0x6e, 0x64, 0x0a,
0x09, 0x09, 0x2d, 0x2d, 0x20, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20, 0x64, 0x74, 0x2c, 0x20, 0x61, 0x73, 0x09, 0x09, 0x2d, 0x2d, 0x20, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20, 0x64, 0x74, 0x2c, 0x20, 0x61, 0x73,