Home | History | Annotate | Download | only in platform
      1 /*
      2  * Copyright (C) 2011 Apple Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef PlatformGestureEvent_h
     27 #define PlatformGestureEvent_h
     28 
     29 #include "platform/PlatformEvent.h"
     30 #include "platform/geometry/FloatPoint.h"
     31 #include "platform/geometry/IntPoint.h"
     32 #include "platform/geometry/IntSize.h"
     33 #include "wtf/Assertions.h"
     34 #include <string.h>
     35 
     36 namespace blink {
     37 
     38 class PlatformGestureEvent : public PlatformEvent {
     39 public:
     40     PlatformGestureEvent()
     41         : PlatformEvent(PlatformEvent::GestureScrollBegin)
     42     {
     43         memset(&m_data, 0, sizeof(m_data));
     44     }
     45 
     46     PlatformGestureEvent(Type type, const IntPoint& position, const IntPoint& globalPosition, const IntSize& area, double timestamp, bool shiftKey, bool ctrlKey, bool altKey, bool metaKey, float deltaX, float deltaY, float velocityX, float velocityY)
     47         : PlatformEvent(type, shiftKey, ctrlKey, altKey, metaKey, timestamp)
     48         , m_position(position)
     49         , m_globalPosition(globalPosition)
     50         , m_area(area)
     51     {
     52         memset(&m_data, 0, sizeof(m_data));
     53         if (type == PlatformEvent::GestureScrollBegin
     54             || type == PlatformEvent::GestureScrollEnd
     55             || type == PlatformEvent::GestureScrollUpdate
     56             || type == PlatformEvent::GestureScrollUpdateWithoutPropagation) {
     57             m_data.m_scrollUpdate.m_deltaX = deltaX;
     58             m_data.m_scrollUpdate.m_deltaY = deltaY;
     59             m_data.m_scrollUpdate.m_velocityX = velocityX;
     60             m_data.m_scrollUpdate.m_velocityY = velocityY;
     61         }
     62     }
     63 
     64     const IntPoint& position() const { return m_position; } // PlatformWindow coordinates.
     65     const IntPoint& globalPosition() const { return m_globalPosition; } // Screen coordinates.
     66 
     67     const IntSize& area() const { return m_area; }
     68 
     69     float deltaX() const
     70     {
     71         ASSERT(m_type == PlatformEvent::GestureScrollUpdate
     72             || m_type == PlatformEvent::GestureScrollUpdateWithoutPropagation);
     73         return m_data.m_scrollUpdate.m_deltaX;
     74     }
     75 
     76     float deltaY() const
     77     {
     78         ASSERT(m_type == PlatformEvent::GestureScrollUpdate
     79             || m_type == PlatformEvent::GestureScrollUpdateWithoutPropagation);
     80         return m_data.m_scrollUpdate.m_deltaY;
     81     }
     82 
     83     int tapCount() const
     84     {
     85         ASSERT(m_type == PlatformEvent::GestureTap);
     86         return m_data.m_tap.m_tapCount;
     87     }
     88 
     89     float velocityX() const
     90     {
     91         ASSERT(m_type == PlatformEvent::GestureScrollUpdate
     92             || m_type == PlatformEvent::GestureScrollUpdateWithoutPropagation);
     93         return m_data.m_scrollUpdate.m_velocityX;
     94     }
     95 
     96     float velocityY() const
     97     {
     98         ASSERT(m_type == PlatformEvent::GestureScrollUpdate
     99             || m_type == PlatformEvent::GestureScrollUpdateWithoutPropagation);
    100         return m_data.m_scrollUpdate.m_velocityY;
    101     }
    102 
    103     float scale() const
    104     {
    105         ASSERT(m_type == PlatformEvent::GesturePinchUpdate);
    106         return m_data.m_pinchUpdate.m_scale;
    107     }
    108 
    109     void applyTouchAdjustment(const IntPoint& adjustedPosition)
    110     {
    111         // Update the window-relative position of the event so that the node that was
    112         // ultimately hit is under this point (i.e. elementFromPoint for the client
    113         // co-ordinates in a 'click' event should yield the target). The global
    114         // position is intentionally left unmodified because it's intended to reflect
    115         // raw co-ordinates unrelated to any content.
    116         m_position = adjustedPosition;
    117     }
    118 
    119     bool isScrollEvent() const
    120     {
    121         switch (m_type) {
    122         case GestureScrollBegin:
    123         case GestureScrollEnd:
    124         case GestureScrollUpdate:
    125         case GestureScrollUpdateWithoutPropagation:
    126         case GestureFlingStart:
    127         case GesturePinchBegin:
    128         case GesturePinchEnd:
    129         case GesturePinchUpdate:
    130             return true;
    131         case GestureTap:
    132         case GestureTapUnconfirmed:
    133         case GestureTapDown:
    134         case GestureShowPress:
    135         case GestureTapDownCancel:
    136         case GestureTwoFingerTap:
    137         case GestureLongPress:
    138         case GestureLongTap:
    139             return false;
    140         default:
    141             ASSERT_NOT_REACHED();
    142             return false;
    143         }
    144     }
    145 
    146 protected:
    147     IntPoint m_position;
    148     IntPoint m_globalPosition;
    149     IntSize m_area;
    150 
    151     union {
    152         struct {
    153             int m_tapCount;
    154         } m_tap;
    155 
    156         struct {
    157             float m_deltaX;
    158             float m_deltaY;
    159             float m_velocityX;
    160             float m_velocityY;
    161         } m_scrollUpdate;
    162 
    163         struct {
    164             float m_scale;
    165         } m_pinchUpdate;
    166     } m_data;
    167 };
    168 
    169 } // namespace blink
    170 
    171 #endif // PlatformGestureEvent_h
    172