mirror of
https://github.com/love2d/love.git
synced 2026-08-18 03:34:23 +02:00
love.touch.getTouches now returns the list of ids in a stable order, instead of being effectively randomly ordered.
The least recently pressed (longest-pressed) active touch id is first in the list, etc.
This commit is contained in:
@@ -23,6 +23,9 @@
|
||||
#include "common/Exception.h"
|
||||
#include "Touch.h"
|
||||
|
||||
// C++
|
||||
#include <algorithm>
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace touch
|
||||
@@ -30,34 +33,20 @@ namespace touch
|
||||
namespace sdl
|
||||
{
|
||||
|
||||
std::vector<int64> Touch::getTouches() const
|
||||
const std::vector<Touch::TouchInfo> &Touch::getTouches() const
|
||||
{
|
||||
std::vector<int64> 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;
|
||||
|
||||
@@ -24,9 +24,6 @@
|
||||
// LOVE
|
||||
#include "touch/Touch.h"
|
||||
|
||||
// C++
|
||||
#include <map>
|
||||
|
||||
// SDL
|
||||
#include <SDL_events.h>
|
||||
|
||||
@@ -43,9 +40,8 @@ public:
|
||||
|
||||
virtual ~Touch() {}
|
||||
|
||||
std::vector<int64> getTouches() const override;
|
||||
void getPosition(int64 id, double &x, double &y) const override;
|
||||
double getPressure(int64 id) const override;
|
||||
const std::vector<TouchInfo> &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<int64, TouchInfo> touches;
|
||||
// All current touches.
|
||||
std::vector<TouchInfo> touches;
|
||||
|
||||
}; // Touch
|
||||
|
||||
|
||||
Reference in New Issue
Block a user