Add touch event to SDL2

This commit is contained in:
Benau 2020-06-11 12:00:14 +08:00
parent ea0380ae16
commit 05a75dcbce
2 changed files with 63 additions and 4 deletions

View File

@ -395,6 +395,20 @@ bool CIrrDeviceSDL::run()
{
switch ( SDL_event.type )
{
case SDL_FINGERMOTION:
case SDL_FINGERDOWN:
case SDL_FINGERUP:
irrevent.EventType = irr::EET_TOUCH_INPUT_EVENT;
irrevent.TouchInput.Event = SDL_event.type == SDL_FINGERMOTION ? irr::ETIE_MOVED :
SDL_event.type == SDL_FINGERDOWN ? irr::ETIE_PRESSED_DOWN : irr::ETIE_LEFT_UP;
irrevent.TouchInput.ID = getTouchId(SDL_event.tfinger.fingerId);
if (SDL_event.type == SDL_FINGERUP)
removeTouchId(SDL_event.tfinger.fingerId);
irrevent.TouchInput.X = SDL_event.tfinger.x * Width;
irrevent.TouchInput.Y = SDL_event.tfinger.y * Height;
postEventFromUser(irrevent);
break;
case SDL_MOUSEWHEEL:
if (SDL_event.wheel.x > 0 || SDL_event.wheel.x < 0)
break;
@ -410,8 +424,8 @@ bool CIrrDeviceSDL::run()
case SDL_MOUSEMOTION:
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
MouseX = irrevent.MouseInput.X = SDL_event.motion.x * NativeScale;
MouseY = irrevent.MouseInput.Y = SDL_event.motion.y * NativeScale;
irrevent.MouseInput.ButtonStates = MouseButtonStates;
postEventFromUser(irrevent);
@ -421,8 +435,8 @@ bool CIrrDeviceSDL::run()
case SDL_MOUSEBUTTONUP:
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.X = SDL_event.button.x;
irrevent.MouseInput.Y = SDL_event.button.y;
irrevent.MouseInput.X = SDL_event.button.x * NativeScale;
irrevent.MouseInput.Y = SDL_event.button.y * NativeScale;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
@ -930,6 +944,12 @@ void CIrrDeviceSDL::createKeyMap()
KeyMap.sort();
}
bool CIrrDeviceSDL::supportsTouchDevice() const
{
return SDL_GetNumTouchDevices() > 0;
}
} // end namespace irr
#endif // _IRR_COMPILE_WITH_SDL_DEVICE_

View File

@ -18,6 +18,8 @@
#include <SDL.h>
#include <SDL_syswm.h>
#include <map>
#include <set>
namespace irr
{
@ -99,6 +101,8 @@ namespace irr
virtual void setWindowMinimumSize(u32 width, u32 height);
virtual bool supportsTouchDevice() const;
//! Get the device type
virtual E_DEVICE_TYPE getType() const
{
@ -237,6 +241,41 @@ namespace irr
u32 MouseButtonStates;
u32 Width, Height;
std::map<SDL_FingerID, size_t> TouchIDMap;
//! Get a unique touch id per touch, create one if it's a new touch
size_t getTouchId(SDL_FingerID touch)
{
auto it = TouchIDMap.find(touch);
if (it == TouchIDMap.end())
{
std::set<size_t> ids;
for (auto& p : TouchIDMap)
ids.insert(p.second);
size_t cur_id = 0;
while (true)
{
if (ids.find(cur_id) == ids.end())
break;
cur_id++;
}
TouchIDMap[touch] = cur_id;
return cur_id;
}
return it->second;
}
//! Remove a unique touch id, free it for future usage
void removeTouchId(SDL_FingerID touch)
{
TouchIDMap.erase(touch);
}
//! Clear all unique touch ids, used when the app out focused
void clearAllTouchIds()
{
TouchIDMap.clear();
}
f32 TopPadding;
f32 BottomPadding;