Skip to content

Commit

Permalink
replace some macOS-specific event code with more generic code.
Browse files Browse the repository at this point in the history
  • Loading branch information
slime73 committed Oct 22, 2024
1 parent d73036f commit 2655a42
Showing 1 changed file with 39 additions and 45 deletions.
84 changes: 39 additions & 45 deletions src/modules/event/sdl/Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ static void clampToWindow(double *x, double *y)
window->clampPositionInWindow(x, y);
}

#ifndef LOVE_MACOS
static void normalizedToDPICoords(double *x, double *y)
{
double w = 1.0, h = 1.0;
Expand All @@ -79,7 +78,6 @@ static void normalizedToDPICoords(double *x, double *y)
if (y)
*y = ((*y) * h);
}
#endif

// SDL's event watch callbacks trigger when the event is actually posted inside
// SDL, unlike with SDL_PollEvents. This is useful for some events which require
Expand Down Expand Up @@ -201,10 +199,8 @@ Message *Event::convert(const SDL_Event &e)
const char *txt;
const char *txt2;

#ifndef LOVE_MACOS
love::touch::sdl::Touch *touchmodule = nullptr;
love::touch::Touch::TouchInfo touchinfo = {};
#endif

switch (e.type)
{
Expand Down Expand Up @@ -321,47 +317,45 @@ Message *Event::convert(const SDL_Event &e)
case SDL_EVENT_FINGER_DOWN:
case SDL_EVENT_FINGER_UP:
case SDL_EVENT_FINGER_MOTION:
// 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_MACOS
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;
touchinfo.pressure = e.tfinger.pressure;

// SDL's coords are normalized to [0, 1], but we want screen coords.
normalizedToDPICoords(&touchinfo.x, &touchinfo.y);
normalizedToDPICoords(&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);

// 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.
vargs.emplace_back((void *) (intptr_t) touchinfo.id);
vargs.emplace_back(touchinfo.x);
vargs.emplace_back(touchinfo.y);
vargs.emplace_back(touchinfo.dx);
vargs.emplace_back(touchinfo.dy);
vargs.emplace_back(touchinfo.pressure);

if (e.type == SDL_EVENT_FINGER_DOWN)
txt = "touchpressed";
else if (e.type == SDL_EVENT_FINGER_UP)
txt = "touchreleased";
else
txt = "touchmoved";
msg = new Message(txt, vargs);
#endif
// TODO: Expose APIs to enable different touch device types.
if (SDL_GetTouchDeviceType(e.tfinger.touchID) == SDL_TOUCH_DEVICE_DIRECT)
{
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;
touchinfo.pressure = e.tfinger.pressure;

// SDL's coords are normalized to [0, 1], but we want screen coords.
normalizedToDPICoords(&touchinfo.x, &touchinfo.y);
normalizedToDPICoords(&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);

// 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.
vargs.emplace_back((void *) (intptr_t) touchinfo.id);
vargs.emplace_back(touchinfo.x);
vargs.emplace_back(touchinfo.y);
vargs.emplace_back(touchinfo.dx);
vargs.emplace_back(touchinfo.dy);
vargs.emplace_back(touchinfo.pressure);

if (e.type == SDL_EVENT_FINGER_DOWN)
txt = "touchpressed";
else if (e.type == SDL_EVENT_FINGER_UP)
txt = "touchreleased";
else
txt = "touchmoved";
msg = new Message(txt, vargs);
}
break;
case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
case SDL_EVENT_JOYSTICK_BUTTON_UP:
Expand Down

0 comments on commit 2655a42

Please sign in to comment.