Skip to content

Commit

Permalink
Finally got around testing, debugging, and fixing touch input.
Browse files Browse the repository at this point in the history
This commit completes touch input implementation for Win32. Please give it a spin! :)

Note that this does *not* implement pen support, only touch.
  • Loading branch information
MarioLiebisch committed Dec 25, 2016
1 parent 31c4da6 commit 9f32b82
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
27 changes: 18 additions & 9 deletions src/SFML/Window/Win32/InputImpl.cpp
Expand Up @@ -35,6 +35,7 @@
#define _WIN32_WINNT 0x0501
#include <SFML/Window/Window.hpp>
#include <SFML/Window/Win32/InputImpl.hpp>
#include <SFML/Window/Win32/WindowImplWin32.hpp>
#include <windows.h>


Expand Down Expand Up @@ -229,26 +230,34 @@ void InputImpl::setMousePosition(const Vector2i& position, const Window& relativ


////////////////////////////////////////////////////////////
bool InputImpl::isTouchDown(unsigned int /*finger*/)
bool InputImpl::isTouchDown(unsigned int finger)
{
// Not applicable
return false;
return WindowImplWin32::isTouchDown(finger);
}


////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/)
Vector2i InputImpl::getTouchPosition(unsigned int finger)
{
// Not applicable
return Vector2i();
return WindowImplWin32::getTouchPosition(finger);
}


////////////////////////////////////////////////////////////
Vector2i InputImpl::getTouchPosition(unsigned int /*finger*/, const Window& /*relativeTo*/)
Vector2i InputImpl::getTouchPosition(unsigned int finger, const Window& relativeTo)
{
// Not applicable
return Vector2i();
WindowHandle handle = relativeTo.getSystemHandle();
Vector2i pos;

if (handle && WindowImplWin32::isTouchDown(finger))
{
pos = WindowImplWin32::getTouchPosition(finger);
POINT point = { pos.x, pos.y };
ScreenToClient(handle, &point);
pos.x = point.x;
pos.y = point.y;
}
return pos;
}

} // namespace priv
Expand Down
26 changes: 20 additions & 6 deletions src/SFML/Window/Win32/WindowImplWin32.cpp
Expand Up @@ -116,6 +116,9 @@ namespace
{
if (touchIDs[i] == id)
return i;
}
for (int i = 0; i < 10; ++i)
{
if (touchIDs[i] == -1)
{
touchIDs[i] = id;
Expand Down Expand Up @@ -1069,25 +1072,25 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam)
event.touch.finger = index;
point.x = TOUCH_COORD_TO_PIXEL(events[i].x);
point.y = TOUCH_COORD_TO_PIXEL(events[i].y);
ScreenToClient(m_handle, &point);
event.touch.x = point.x;
event.touch.y = point.y;

POINT cpoint = point;
ScreenToClient(m_handle, &cpoint);
event.touch.x = cpoint.x;
event.touch.y = cpoint.y;

if (events[i].dwFlags & TOUCHEVENTF_DOWN) {
event.type = Event::TouchBegan;
pushEvent(event);

// Prevent initial move event
touches[index] = point;
err() << "down: " << events[i].dwID << ", x: " << point.x << ", y: " << point.y << std::endl;
}
if (events[i].dwFlags & TOUCHEVENTF_UP) {
event.type = Event::TouchEnded;
pushEvent(event);

// Remove the stored ID
touchIDs[index] = -1;
err() << "up: " << events[i].dwID << ", x: " << point.x << ", y: " << point.y << std::endl;
}
if (events[i].dwFlags & TOUCHEVENTF_MOVE) {
// Only handle real movement
Expand All @@ -1096,7 +1099,6 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam)
touches[index] = point;
event.type = Event::TouchMoved;
pushEvent(event);
err() << "moved: " << events[i].dwID << ", x: " << point.x << ", y: " << point.y << std::endl;
}
}

Expand Down Expand Up @@ -1305,6 +1307,18 @@ void WindowImplWin32::prepareTouch()
RegisterTouchWindow(m_handle, 0);
}

////////////////////////////////////////////////////////////
bool WindowImplWin32::isTouchDown(unsigned int finger)
{
return touchIDs[finger] != -1;
}

////////////////////////////////////////////////////////////
Vector2i WindowImplWin32::getTouchPosition(unsigned int finger)
{
return Vector2i(touches[finger].x, touches[finger].y);
}

} // namespace priv

} // namespace sf
Expand Down
22 changes: 21 additions & 1 deletion src/SFML/Window/Win32/WindowImplWin32.hpp
Expand Up @@ -176,6 +176,26 @@ class WindowImplWin32 : public WindowImpl
////////////////////////////////////////////////////////////
virtual bool hasFocus() const;

////////////////////////////////////////////////////////////
/// \brief Get global touch state for a finger
///
/// \param finger Finger index
///
/// \return True, if the finger is active
///
////////////////////////////////////////////////////////////
static bool isTouchDown(unsigned int finger);

////////////////////////////////////////////////////////////
/// \brief Get global touch coordinates for a finger
///
/// \param finger Finger index
///
/// \return Touch position
///
////////////////////////////////////////////////////////////
static Vector2i getTouchPosition(unsigned int finger);

protected:

////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -265,7 +285,7 @@ class WindowImplWin32 : public WindowImpl
/// \brief Helper function to prepare and enable touch handling
///
////////////////////////////////////////////////////////////
void WindowImplWin32::prepareTouch();
void prepareTouch();

////////////////////////////////////////////////////////////
// Member data
Expand Down

0 comments on commit 9f32b82

Please sign in to comment.