Use wxPoint instead of (x,y) pair in wxPanGestureEvent

Using higher level objects makes the code generating and using this
event shorter and more clear.
This commit is contained in:
Vadim Zeitlin
2017-11-21 17:34:51 +01:00
parent db71a10b29
commit bb2887930f
7 changed files with 70 additions and 91 deletions

View File

@@ -1915,25 +1915,21 @@ public:
wxPanGestureEvent(wxWindowID winid = 0) wxPanGestureEvent(wxWindowID winid = 0)
: wxGestureEvent(winid, wxEVT_GESTURE_PAN) : wxGestureEvent(winid, wxEVT_GESTURE_PAN)
{ {
m_deltaX = 0;
m_deltaY = 0;
} }
wxPanGestureEvent(const wxPanGestureEvent& event) : wxGestureEvent(event) wxPanGestureEvent(const wxPanGestureEvent& event)
: wxGestureEvent(event),
m_delta(event.m_delta)
{ {
m_deltaX = event.m_deltaX;
m_deltaY = event.m_deltaY;
} }
int GetDeltaX() const { return m_deltaX; } wxPoint GetDelta() const { return m_delta; }
void SetDeltaX(int DeltaX) { m_deltaX = DeltaX; } void SetDelta(const wxPoint& delta) { m_delta = delta; }
int GetDeltaY() const { return m_deltaY; }
void SetDeltaY(int DeltaY) { m_deltaY = DeltaY; }
virtual wxEvent *Clone() const { return new wxPanGestureEvent(*this); } virtual wxEvent *Clone() const { return new wxPanGestureEvent(*this); }
private: private:
int m_deltaX, m_deltaY; wxPoint m_delta;
wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxPanGestureEvent); wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxPanGestureEvent);
}; };

View File

@@ -362,13 +362,13 @@ public:
// Common gesture event initialization, returns true if it is the initial // Common gesture event initialization, returns true if it is the initial
// event (GF_BEGIN set in flags), false otherwise. // event (GF_BEGIN set in flags), false otherwise.
bool InitGestureEvent(wxGestureEvent& event, int x, int y, WXDWORD flags); bool InitGestureEvent(wxGestureEvent& event, const wxPoint& pt, WXDWORD flags);
bool HandlePanGesture(int x, int y, WXDWORD flags); bool HandlePanGesture(const wxPoint& pt, WXDWORD flags);
bool HandleZoomGesture(int x, int y, WXDWORD fingerDistance, WXDWORD flags); bool HandleZoomGesture(const wxPoint& pt, WXDWORD fingerDistance, WXDWORD flags);
bool HandleRotateGesture(int x, int y, WXDWORD angleArgument, WXDWORD flags); bool HandleRotateGesture(const wxPoint& pt, WXDWORD angleArgument, WXDWORD flags);
bool HandleTwoFingerTap(int x, int y, WXDWORD flags); bool HandleTwoFingerTap(const wxPoint& pt, WXDWORD flags);
bool HandlePressAndTap(int x, int y, WXDWORD flags); bool HandlePressAndTap(const wxPoint& pt, WXDWORD flags);
bool HandleChar(WXWPARAM wParam, WXLPARAM lParam); bool HandleChar(WXWPARAM wParam, WXLPARAM lParam);
bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam); bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam);

View File

@@ -3726,24 +3726,14 @@ public:
wxPanGestureEvent(wxWindowID winid = 0); wxPanGestureEvent(wxWindowID winid = 0);
/** /**
Returns the horizontal component of the distance covered since the previous Pan event. Returns the distance covered since the previous panning event.
*/ */
int GetDeltaX() const; wxPoint GetDelta() const;
/** /**
Returns the vertical component of the distance covered since the previous Pan event. Sets the distance covered since the previous panning event.
*/ */
int GetDeltaY() const; void SetDelta(const wxPoint& delta);
/**
Sets the horizontal component of the distance covered since the previous Pan event.
*/
int SetDeltaX(int DeltaX);
/**
Sets the vertical component of the distance covered since the previous Pan event.
*/
int SetDeltaY(int DeltaY);
}; };

View File

@@ -86,19 +86,22 @@ void MyGesturePanel::OnPan(wxPanGestureEvent& event)
wxLogMessage("Pan gesture started\n"); wxLogMessage("Pan gesture started\n");
} }
wxLogMessage("Pan gesture performed with deltaX = %d and deltaY = %d, with current position (%d,%d)\n", const wxPoint delta = event.GetDelta();
event.GetDeltaX(), event.GetDeltaY(), event.GetPosition().x, event.GetPosition().y); wxLogMessage("Pan gesture performed with delta = (%d,%d), "
"with current position (%d,%d)",
delta.x, delta.y,
event.GetPosition().x, event.GetPosition().y);
// Transform the distance using the tranpose of the matrix, // Transform the distance using the transpose of the matrix,
// in order to translate the image to match the screen coordinates // in order to translate the image to match the screen coordinates
wxMatrix2D m; wxMatrix2D m;
m_affineMatrix.Get(&m, NULL); m_affineMatrix.Get(&m, NULL);
wxPoint2DDouble delta(m.m_11 * event.GetDeltaX() + m.m_12 * event.GetDeltaY(), wxPoint2DDouble deltaD(m.m_11 * delta.x + m.m_12 * delta.y,
m.m_21 * event.GetDeltaX() + m.m_22 * event.GetDeltaY()); m.m_21 * delta.x + m.m_22 * delta.y);
// Add it to the total translation // Add it to the total translation
m_translateDistance += delta; m_translateDistance += deltaD;
if ( event.IsGestureEnd() ) if ( event.IsGestureEnd() )
{ {

View File

@@ -2998,22 +2998,22 @@ pan_gesture_callback(GtkGesture* gesture, GtkPanDirection direction, gdouble off
{ {
case GTK_PAN_DIRECTION_UP: case GTK_PAN_DIRECTION_UP:
data->m_allowedGestures |= vertical_pan; data->m_allowedGestures |= vertical_pan;
event.SetDeltaY(-delta); event.SetDelta(wxPoint(0, -delta));
break; break;
case GTK_PAN_DIRECTION_DOWN: case GTK_PAN_DIRECTION_DOWN:
data->m_allowedGestures |= vertical_pan; data->m_allowedGestures |= vertical_pan;
event.SetDeltaY(delta); event.SetDelta(wxPoint(0, delta));
break; break;
case GTK_PAN_DIRECTION_RIGHT: case GTK_PAN_DIRECTION_RIGHT:
data->m_allowedGestures |= horizontal_pan; data->m_allowedGestures |= horizontal_pan;
event.SetDeltaX(delta); event.SetDelta(wxPoint(delta, 0));
break; break;
case GTK_PAN_DIRECTION_LEFT: case GTK_PAN_DIRECTION_LEFT:
data->m_allowedGestures |= horizontal_pan; data->m_allowedGestures |= horizontal_pan;
event.SetDeltaX(-delta); event.SetDelta(wxPoint(-delta, 0));
break; break;
} }

View File

@@ -3220,47 +3220,49 @@ wxWindowMSW::MSWHandleMessage(WXLRESULT *result,
wxLogDebug("This is Not the window targeted by this gesture!"); wxLogDebug("This is Not the window targeted by this gesture!");
} }
int x = gestureInfo.ptsLocation.x; const wxPoint pt = ScreenToClient
int y = gestureInfo.ptsLocation.y; (
ScreenToClient(&x, &y); wxPoint(gestureInfo.ptsLocation.x,
gestureInfo.ptsLocation.y)
);
// dwID field is used to determine the type of gesture // dwID field is used to determine the type of gesture
switch ( gestureInfo.dwID ) switch ( gestureInfo.dwID )
{ {
case GID_PAN: case GID_PAN:
// (x,y) is the current position of the pan // Point contains the current position of the pan.
processed = HandlePanGesture(x, y, gestureInfo.dwFlags); processed = HandlePanGesture(pt, gestureInfo.dwFlags);
break; break;
case GID_ZOOM: case GID_ZOOM:
// (x,y) is the mid-point of 2 fingers and ullArgument // Point is the mid-point of 2 fingers and ullArgument
// contains the distance between the fingers in its lower // contains the distance between the fingers in its lower
// half // half
processed = HandleZoomGesture processed = HandleZoomGesture
( (
x, y, pt,
static_cast<DWORD>(gestureInfo.ullArguments), static_cast<DWORD>(gestureInfo.ullArguments),
gestureInfo.dwFlags gestureInfo.dwFlags
); );
break; break;
case GID_ROTATE: case GID_ROTATE:
// (x,y) is the center point of rotation and ullArguments // Point is the center point of rotation and ullArguments
// contains the angle of rotation // contains the angle of rotation
processed = HandleRotateGesture processed = HandleRotateGesture
( (
x, y, pt,
static_cast<DWORD>(gestureInfo.ullArguments), static_cast<DWORD>(gestureInfo.ullArguments),
gestureInfo.dwFlags gestureInfo.dwFlags
); );
break; break;
case GID_TWOFINGERTAP: case GID_TWOFINGERTAP:
processed = HandleTwoFingerTap(x, y, gestureInfo.dwFlags); processed = HandleTwoFingerTap(pt, gestureInfo.dwFlags);
break; break;
case GID_PRESSANDTAP: case GID_PRESSANDTAP:
processed = HandlePressAndTap(x, y, gestureInfo.dwFlags); processed = HandlePressAndTap(pt, gestureInfo.dwFlags);
break; break;
} }
@@ -5648,12 +5650,12 @@ void wxWindowMSW::GenerateMouseLeave()
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
bool wxWindowMSW::InitGestureEvent(wxGestureEvent& event, bool wxWindowMSW::InitGestureEvent(wxGestureEvent& event,
int x, int y, const wxPoint& pt,
WXDWORD flags) WXDWORD flags)
{ {
event.SetEventObject(this); event.SetEventObject(this);
event.SetTimestamp(::GetMessageTime()); event.SetTimestamp(::GetMessageTime());
event.SetPosition(wxPoint(x, y)); event.SetPosition(pt);
if ( flags & GF_BEGIN ) if ( flags & GF_BEGIN )
event.SetGestureStart(); event.SetGestureStart();
@@ -5664,36 +5666,31 @@ bool wxWindowMSW::InitGestureEvent(wxGestureEvent& event,
return (flags & GF_BEGIN) != 0; return (flags & GF_BEGIN) != 0;
} }
bool wxWindowMSW::HandlePanGesture(int x, int y, WXDWORD flags) bool wxWindowMSW::HandlePanGesture(const wxPoint& pt, WXDWORD flags)
{ {
// wxEVT_GESTURE_PAN // wxEVT_GESTURE_PAN
wxPanGestureEvent event(GetId()); wxPanGestureEvent event(GetId());
// These are used to calculate the pan delta // This is used to calculate the pan delta.
static int s_previousLocationX, s_previousLocationY; static wxPoint s_previousLocation;
// If the gesture has just started, store the current point to determine // If the gesture has just started, store the current point to determine
// the pan delta later on. // the pan delta later on.
if ( InitGestureEvent(event, x, y, flags) ) if ( InitGestureEvent(event, pt, flags) )
{ {
s_previousLocationX = x; s_previousLocation = pt;
s_previousLocationY = y;
} }
// Determine the horizontal and vertical changes // Determine the horizontal and vertical changes
int DeltaX = x - s_previousLocationX, DeltaY = y - s_previousLocationY; event.SetDelta(pt - s_previousLocation);
event.SetDeltaX(DeltaX);
event.SetDeltaY(DeltaY);
// Update the last gesture event point // Update the last gesture event point
s_previousLocationX = x; s_previousLocation = pt;
s_previousLocationY = y;
return HandleWindowEvent(event); return HandleWindowEvent(event);
} }
bool wxWindowMSW::HandleZoomGesture(int x, int y, bool wxWindowMSW::HandleZoomGesture(const wxPoint& pt,
WXDWORD fingerDistance, WXDWORD fingerDistance,
WXDWORD flags) WXDWORD flags)
{ {
@@ -5701,14 +5698,14 @@ bool wxWindowMSW::HandleZoomGesture(int x, int y,
wxZoomGestureEvent event(GetId()); wxZoomGestureEvent event(GetId());
// These are used to calculate the center of the zoom and zoom factor // These are used to calculate the center of the zoom and zoom factor
static int s_previousLocationX, s_previousLocationY, s_intialFingerDistance; static wxPoint s_previousLocation;
static int s_intialFingerDistance;
// This flag indicates that the gesture has just started, store the current // This flag indicates that the gesture has just started, store the current
// point and distance between the fingers for future calculations. // point and distance between the fingers for future calculations.
if ( InitGestureEvent(event, x, y, flags) ) if ( InitGestureEvent(event, pt, flags) )
{ {
s_previousLocationX = x; s_previousLocation = pt;
s_previousLocationY = y;
s_intialFingerDistance = fingerDistance; s_intialFingerDistance = fingerDistance;
} }
@@ -5717,32 +5714,28 @@ bool wxWindowMSW::HandleZoomGesture(int x, int y,
// is usually some error, which can cause the center to shift slightly. So, // is usually some error, which can cause the center to shift slightly. So,
// it is recommended to take the average of center of fingers in the // it is recommended to take the average of center of fingers in the
// current and last positions. // current and last positions.
wxPoint pt; const wxPoint ptCenter = (s_previousLocation + pt)/2;
pt.x = (s_previousLocationX + x) / 2;
pt.y = (s_previousLocationY + y) / 2;
const double zoomFactor = (double) fingerDistance / s_intialFingerDistance; const double zoomFactor = (double) fingerDistance / s_intialFingerDistance;
event.SetZoomFactor(zoomFactor); event.SetZoomFactor(zoomFactor);
// This is not a gesture point but the center of a zoom event.SetPosition(ptCenter);
event.SetPosition(pt);
// Update gesture event point // Update gesture event point
s_previousLocationX = x; s_previousLocation = pt;
s_previousLocationY = y;
return HandleWindowEvent(event); return HandleWindowEvent(event);
} }
bool wxWindowMSW::HandleRotateGesture(int x, int y, bool wxWindowMSW::HandleRotateGesture(const wxPoint& pt,
WXDWORD angleArgument, WXDWORD angleArgument,
WXDWORD flags) WXDWORD flags)
{ {
// wxEVT_GESTURE_ROTATE // wxEVT_GESTURE_ROTATE
wxRotateGestureEvent event(GetId()); wxRotateGestureEvent event(GetId());
if ( InitGestureEvent(event, x, y, flags) ) if ( InitGestureEvent(event, pt, flags) )
{ {
event.SetRotationAngle(angleArgument); event.SetRotationAngle(angleArgument);
} }
@@ -5769,21 +5762,21 @@ bool wxWindowMSW::HandleRotateGesture(int x, int y,
return HandleWindowEvent(event); return HandleWindowEvent(event);
} }
bool wxWindowMSW::HandleTwoFingerTap(int x, int y, WXDWORD flags) bool wxWindowMSW::HandleTwoFingerTap(const wxPoint& pt, WXDWORD flags)
{ {
// wxEVT_TWO_FINGER_TAP // wxEVT_TWO_FINGER_TAP
wxTwoFingerTapEvent event(GetId()); wxTwoFingerTapEvent event(GetId());
InitGestureEvent(event, x, y, flags); InitGestureEvent(event, pt, flags);
return HandleWindowEvent(event); return HandleWindowEvent(event);
} }
bool wxWindowMSW::HandlePressAndTap(int x, int y, WXDWORD flags) bool wxWindowMSW::HandlePressAndTap(const wxPoint& pt, WXDWORD flags)
{ {
wxPressAndTapEvent event(GetId()); wxPressAndTapEvent event(GetId());
InitGestureEvent(event, x, y, flags); InitGestureEvent(event, pt, flags);
return HandleWindowEvent(event); return HandleWindowEvent(event);
} }

View File

@@ -1525,13 +1525,12 @@ void wxWidgetCocoaImpl::PanGestureEvent(NSPanGestureRecognizer* panGestureRecogn
nspoint = [panGestureRecognizer translationInView:m_osxView]; nspoint = [panGestureRecognizer translationInView:m_osxView];
pt = wxFromNSPoint(m_osxView, nspoint); pt = wxFromNSPoint(m_osxView, nspoint);
static int s_lastLocationX = 0, s_lastLocationY = 0; static wxPoint s_lastLocation;
if ( gestureState == NSGestureRecognizerStateBegan ) if ( gestureState == NSGestureRecognizerStateBegan )
{ {
wxevent.SetGestureStart(); wxevent.SetGestureStart();
s_lastLocationX = 0; s_lastLocation = wxPoint(0, 0);
s_lastLocationY = 0;
} }
if ( gestureState == NSGestureRecognizerStateEnded ) if ( gestureState == NSGestureRecognizerStateEnded )
@@ -1539,12 +1538,10 @@ void wxWidgetCocoaImpl::PanGestureEvent(NSPanGestureRecognizer* panGestureRecogn
wxevent.SetGestureEnd(); wxevent.SetGestureEnd();
} }
// Set the offsets // Set the offset
wxevent.SetDeltaX(pt.x - s_lastLocationX); wxevent.SetDelta(pt - s_lastLocation);
wxevent.SetDeltaY(pt.y - s_lastLocationY);
s_lastLocationX = pt.x; s_lastLocation = pt;
s_lastLocationY = pt.y;
GetWXPeer()->HandleWindowEvent(wxevent); GetWXPeer()->HandleWindowEvent(wxevent);
} }