mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Added love.touch module and touch screen events. Resolves issue #825.
Note: this implementation is slightly different from the implementation in the love 0.9.x Android and iOS ports! Added love.touchpressed(id, x, y), love.touchmoved(id, x, y, dx, dy), and love.touchreleased(id, x, y, dx, dy). id is a unique identifier for the touch press that persists until love.touchreleased. After that it is no longer unique. x and y are the coordinates of the touch event in pixels within the window. dx and dy are the amount in pixels that the touch has moved since the last event. Added love.touch.getTouchCount and love.touch.getTouch(index). love.touch.getTouch takes an index (not the same as an id. indices are not stable and don't correspond to a unique touch press.) It returns the id of the touch and its current x and y coordinates in pixels. --HG-- branch : minor
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
#include "mouse/Mouse.h"
|
||||
#include "joystick/JoystickModule.h"
|
||||
#include "joystick/sdl/Joystick.h"
|
||||
#include "touch/sdl/Touch.h"
|
||||
#include "graphics/Graphics.h"
|
||||
#include "window/Window.h"
|
||||
#include "common/Exception.h"
|
||||
@@ -52,6 +53,19 @@ static void windowToPixelCoords(double *x, double *y)
|
||||
*y = window->toPixels(*y);
|
||||
}
|
||||
|
||||
static void normalizedToPixelCoords(double *x, double *y)
|
||||
{
|
||||
window::Window *window = Module::getInstance<window::Window>(Module::M_WINDOW);
|
||||
int w = 1, h = 1;
|
||||
|
||||
if (window)
|
||||
window->getPixelDimensions(w, h);
|
||||
|
||||
if (x)
|
||||
*x = ((*x) * (double) w);
|
||||
if (y)
|
||||
*y = ((*y) * (double) h);
|
||||
}
|
||||
|
||||
const char *Event::getName() const
|
||||
{
|
||||
@@ -114,10 +128,12 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
vargs.reserve(4);
|
||||
|
||||
love::keyboard::Keyboard *kb = nullptr;
|
||||
love::touch::sdl::Touch *touchmodule = nullptr;
|
||||
love::filesystem::Filesystem *filesystem = nullptr;
|
||||
|
||||
love::keyboard::Keyboard::Key key;
|
||||
love::mouse::Mouse::Button button;
|
||||
love::touch::Touch::TouchInfo touchinfo;
|
||||
const char *txt;
|
||||
std::map<SDL_Keycode, love::keyboard::Keyboard::Key>::const_iterator keyit;
|
||||
|
||||
@@ -202,6 +218,56 @@ Message *Event::convert(const SDL_Event &e) const
|
||||
vargs.push_back(new Variant((double) e.wheel.y));
|
||||
msg = new Message("wheelmoved", vargs);
|
||||
break;
|
||||
case SDL_FINGERDOWN:
|
||||
case SDL_FINGERUP:
|
||||
case SDL_FINGERMOTION:
|
||||
// Touch events are disabled in OS X because we only actually want touch
|
||||
// screen events, but most touch devices in OS X aren't touch screens
|
||||
// (and SDL doesn't differentiate.) Non-screen touch devices like Mac
|
||||
// trackpads won't give touch coords in the window's coordinate-space.
|
||||
#ifndef LOVE_MACOSX
|
||||
touchinfo.id = (int64) e.tfinger.fingerId;
|
||||
touchinfo.x = e.tfinger.x;
|
||||
touchinfo.y = e.tfinger.y;
|
||||
touchinfo.dx = e.tfinger.dx;
|
||||
touchinfo.dy = e.tfinger.dy;
|
||||
|
||||
#ifdef LOVE_LINUX
|
||||
// FIXME: hacky workaround for SDL not normalizing touch coordinates in
|
||||
// its X11 backend: https://bugzilla.libsdl.org/show_bug.cgi?id=2307
|
||||
if (fabs(touchinfo.x) >= 1.5 || fabs(touchinfo.y) >= 1.5 || fabs(touchinfo.dx) >= 1.5 || fabs(touchinfo.dy) >= 1.5)
|
||||
{
|
||||
windowToPixelCoords(&touchinfo.x, &touchinfo.y);
|
||||
windowToPixelCoords(&touchinfo.dx, &touchinfo.dy);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
// SDL's coords are normalized to [0, 1], but we want them in pixels.
|
||||
normalizedToPixelCoords(&touchinfo.x, &touchinfo.y);
|
||||
normalizedToPixelCoords(&touchinfo.dx, &touchinfo.dy);
|
||||
}
|
||||
|
||||
// We need to update the love.touch.sdl internal state from here.
|
||||
touchmodule = (touch::sdl::Touch *) Module::getInstance("love.touch.sdl");
|
||||
if (touchmodule)
|
||||
touchmodule->onEvent(e.type, touchinfo);
|
||||
|
||||
vargs.push_back(new Variant((double) touchinfo.id));
|
||||
vargs.push_back(new Variant(touchinfo.x));
|
||||
vargs.push_back(new Variant(touchinfo.y));
|
||||
vargs.push_back(new Variant(touchinfo.dx));
|
||||
vargs.push_back(new Variant(touchinfo.dy));
|
||||
|
||||
if (e.type == SDL_FINGERDOWN)
|
||||
txt = "touchpressed";
|
||||
else if (e.type == SDL_FINGERUP)
|
||||
txt = "touchreleased";
|
||||
else
|
||||
txt = "touchmoved";
|
||||
msg = new Message(txt, vargs);
|
||||
#endif
|
||||
break;
|
||||
case SDL_JOYBUTTONDOWN:
|
||||
case SDL_JOYBUTTONUP:
|
||||
case SDL_JOYAXISMOTION:
|
||||
|
||||
Reference in New Issue
Block a user