Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef InputData_h__ |
michael@0 | 7 | #define InputData_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsDebug.h" |
michael@0 | 10 | #include "nsPoint.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "Units.h" |
michael@0 | 13 | #include "mozilla/EventForwards.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | |
michael@0 | 18 | enum InputType |
michael@0 | 19 | { |
michael@0 | 20 | MULTITOUCH_INPUT, |
michael@0 | 21 | PINCHGESTURE_INPUT, |
michael@0 | 22 | TAPGESTURE_INPUT |
michael@0 | 23 | }; |
michael@0 | 24 | |
michael@0 | 25 | class MultiTouchInput; |
michael@0 | 26 | class PinchGestureInput; |
michael@0 | 27 | class TapGestureInput; |
michael@0 | 28 | |
michael@0 | 29 | // This looks unnecessary now, but as we add more and more classes that derive |
michael@0 | 30 | // from InputType (eventually probably almost as many as *Events.h has), it |
michael@0 | 31 | // will be more and more clear what's going on with a macro that shortens the |
michael@0 | 32 | // definition of the RTTI functions. |
michael@0 | 33 | #define INPUTDATA_AS_CHILD_TYPE(type, enumID) \ |
michael@0 | 34 | const type& As##type() const \ |
michael@0 | 35 | { \ |
michael@0 | 36 | NS_ABORT_IF_FALSE(mInputType == enumID, "Invalid cast of InputData."); \ |
michael@0 | 37 | return (const type&) *this; \ |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | /** Base input data class. Should never be instantiated. */ |
michael@0 | 41 | class InputData |
michael@0 | 42 | { |
michael@0 | 43 | public: |
michael@0 | 44 | InputType mInputType; |
michael@0 | 45 | // Time in milliseconds that this data is relevant to. This only really |
michael@0 | 46 | // matters when this data is used as an event. We use uint32_t instead of |
michael@0 | 47 | // TimeStamp because it is easier to convert from WidgetInputEvent. The time |
michael@0 | 48 | // is platform-specific but it in the case of B2G and Fennec it is since |
michael@0 | 49 | // startup. |
michael@0 | 50 | uint32_t mTime; |
michael@0 | 51 | |
michael@0 | 52 | Modifiers modifiers; |
michael@0 | 53 | |
michael@0 | 54 | INPUTDATA_AS_CHILD_TYPE(MultiTouchInput, MULTITOUCH_INPUT) |
michael@0 | 55 | INPUTDATA_AS_CHILD_TYPE(PinchGestureInput, PINCHGESTURE_INPUT) |
michael@0 | 56 | INPUTDATA_AS_CHILD_TYPE(TapGestureInput, TAPGESTURE_INPUT) |
michael@0 | 57 | |
michael@0 | 58 | InputData() |
michael@0 | 59 | { |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | protected: |
michael@0 | 63 | InputData(InputType aInputType, uint32_t aTime, Modifiers aModifiers) |
michael@0 | 64 | : mInputType(aInputType), |
michael@0 | 65 | mTime(aTime), |
michael@0 | 66 | modifiers(aModifiers) |
michael@0 | 67 | { |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | } |
michael@0 | 71 | }; |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Data container for a single touch input. Similar to dom::Touch, but used in |
michael@0 | 75 | * off-main-thread situations. This is more for just storing touch data, whereas |
michael@0 | 76 | * dom::Touch is more useful for dispatching through the DOM (which can only |
michael@0 | 77 | * happen on the main thread). dom::Touch also bears the problem of storing |
michael@0 | 78 | * pointers to nsIWidget instances which can only be used on the main thread, |
michael@0 | 79 | * so if instead we used dom::Touch and ever set these pointers |
michael@0 | 80 | * off-main-thread, Bad Things Can Happen(tm). |
michael@0 | 81 | * |
michael@0 | 82 | * Note that this doesn't inherit from InputData because this itself is not an |
michael@0 | 83 | * event. It is only a container/struct that should have any number of instances |
michael@0 | 84 | * within a MultiTouchInput. |
michael@0 | 85 | * |
michael@0 | 86 | * fixme/bug 775746: Make dom::Touch inherit from this class. |
michael@0 | 87 | */ |
michael@0 | 88 | class SingleTouchData |
michael@0 | 89 | { |
michael@0 | 90 | public: |
michael@0 | 91 | SingleTouchData(int32_t aIdentifier, |
michael@0 | 92 | ScreenIntPoint aScreenPoint, |
michael@0 | 93 | ScreenSize aRadius, |
michael@0 | 94 | float aRotationAngle, |
michael@0 | 95 | float aForce) |
michael@0 | 96 | : mIdentifier(aIdentifier), |
michael@0 | 97 | mScreenPoint(aScreenPoint), |
michael@0 | 98 | mRadius(aRadius), |
michael@0 | 99 | mRotationAngle(aRotationAngle), |
michael@0 | 100 | mForce(aForce) |
michael@0 | 101 | { |
michael@0 | 102 | |
michael@0 | 103 | |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | SingleTouchData() |
michael@0 | 107 | { |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | // A unique number assigned to each SingleTouchData within a MultiTouchInput so |
michael@0 | 111 | // that they can be easily distinguished when handling a touch start/move/end. |
michael@0 | 112 | int32_t mIdentifier; |
michael@0 | 113 | |
michael@0 | 114 | // Point on the screen that the touch hit, in device pixels. They are |
michael@0 | 115 | // coordinates on the screen. |
michael@0 | 116 | ScreenIntPoint mScreenPoint; |
michael@0 | 117 | |
michael@0 | 118 | // Radius that the touch covers, i.e. if you're using your thumb it will |
michael@0 | 119 | // probably be larger than using your pinky, even with the same force. |
michael@0 | 120 | // Radius can be different along x and y. For example, if you press down with |
michael@0 | 121 | // your entire finger vertically, the y radius will be much larger than the x |
michael@0 | 122 | // radius. |
michael@0 | 123 | ScreenSize mRadius; |
michael@0 | 124 | |
michael@0 | 125 | float mRotationAngle; |
michael@0 | 126 | |
michael@0 | 127 | // How hard the screen is being pressed. |
michael@0 | 128 | float mForce; |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Similar to WidgetTouchEvent, but for use off-main-thread. Also only stores a |
michael@0 | 133 | * screen touch point instead of the many different coordinate spaces |
michael@0 | 134 | * WidgetTouchEvent stores its touch point in. This includes a way to initialize |
michael@0 | 135 | * itself from a WidgetTouchEvent by copying all relevant data over. Note that |
michael@0 | 136 | * this copying from WidgetTouchEvent functionality can only be used on the main |
michael@0 | 137 | * thread. |
michael@0 | 138 | * |
michael@0 | 139 | * Stores an array of SingleTouchData. |
michael@0 | 140 | */ |
michael@0 | 141 | class MultiTouchInput : public InputData |
michael@0 | 142 | { |
michael@0 | 143 | public: |
michael@0 | 144 | enum MultiTouchType |
michael@0 | 145 | { |
michael@0 | 146 | MULTITOUCH_START, |
michael@0 | 147 | MULTITOUCH_MOVE, |
michael@0 | 148 | MULTITOUCH_END, |
michael@0 | 149 | MULTITOUCH_ENTER, |
michael@0 | 150 | MULTITOUCH_LEAVE, |
michael@0 | 151 | MULTITOUCH_CANCEL |
michael@0 | 152 | }; |
michael@0 | 153 | |
michael@0 | 154 | MultiTouchInput(MultiTouchType aType, uint32_t aTime, Modifiers aModifiers) |
michael@0 | 155 | : InputData(MULTITOUCH_INPUT, aTime, aModifiers), |
michael@0 | 156 | mType(aType) |
michael@0 | 157 | { |
michael@0 | 158 | |
michael@0 | 159 | |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | MultiTouchInput() |
michael@0 | 163 | { |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | MultiTouchInput(const WidgetTouchEvent& aTouchEvent); |
michael@0 | 167 | |
michael@0 | 168 | // This conversion from WidgetMouseEvent to MultiTouchInput is needed because |
michael@0 | 169 | // on the B2G emulator we can only receive mouse events, but we need to be |
michael@0 | 170 | // able to pan correctly. To do this, we convert the events into a format that |
michael@0 | 171 | // the panning code can handle. This code is very limited and only supports |
michael@0 | 172 | // SingleTouchData. It also sends garbage for the identifier, radius, force |
michael@0 | 173 | // and rotation angle. |
michael@0 | 174 | MultiTouchInput(const WidgetMouseEvent& aMouseEvent); |
michael@0 | 175 | |
michael@0 | 176 | MultiTouchType mType; |
michael@0 | 177 | nsTArray<SingleTouchData> mTouches; |
michael@0 | 178 | }; |
michael@0 | 179 | |
michael@0 | 180 | /** |
michael@0 | 181 | * Encapsulation class for pinch events. In general, these will be generated by |
michael@0 | 182 | * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and |
michael@0 | 183 | * determining whether or not the user was trying to do a gesture. |
michael@0 | 184 | */ |
michael@0 | 185 | class PinchGestureInput : public InputData |
michael@0 | 186 | { |
michael@0 | 187 | public: |
michael@0 | 188 | enum PinchGestureType |
michael@0 | 189 | { |
michael@0 | 190 | PINCHGESTURE_START, |
michael@0 | 191 | PINCHGESTURE_SCALE, |
michael@0 | 192 | PINCHGESTURE_END |
michael@0 | 193 | }; |
michael@0 | 194 | |
michael@0 | 195 | PinchGestureInput(PinchGestureType aType, |
michael@0 | 196 | uint32_t aTime, |
michael@0 | 197 | const ScreenPoint& aFocusPoint, |
michael@0 | 198 | float aCurrentSpan, |
michael@0 | 199 | float aPreviousSpan, |
michael@0 | 200 | Modifiers aModifiers) |
michael@0 | 201 | : InputData(PINCHGESTURE_INPUT, aTime, aModifiers), |
michael@0 | 202 | mType(aType), |
michael@0 | 203 | mFocusPoint(aFocusPoint), |
michael@0 | 204 | mCurrentSpan(aCurrentSpan), |
michael@0 | 205 | mPreviousSpan(aPreviousSpan) |
michael@0 | 206 | { |
michael@0 | 207 | |
michael@0 | 208 | |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | PinchGestureType mType; |
michael@0 | 212 | |
michael@0 | 213 | // Center point of the pinch gesture. That is, if there are two fingers on the |
michael@0 | 214 | // screen, it is their midpoint. In the case of more than two fingers, the |
michael@0 | 215 | // point is implementation-specific, but can for example be the midpoint |
michael@0 | 216 | // between the very first and very last touch. This is in device pixels and |
michael@0 | 217 | // are the coordinates on the screen of this midpoint. |
michael@0 | 218 | ScreenPoint mFocusPoint; |
michael@0 | 219 | |
michael@0 | 220 | // The distance in device pixels (though as a float for increased precision |
michael@0 | 221 | // and because it is the distance along both the x and y axis) between the |
michael@0 | 222 | // touches responsible for the pinch gesture. |
michael@0 | 223 | float mCurrentSpan; |
michael@0 | 224 | |
michael@0 | 225 | // The previous |mCurrentSpan| in the PinchGestureInput preceding this one. |
michael@0 | 226 | // This is only really relevant during a PINCHGESTURE_SCALE because when it is |
michael@0 | 227 | // of this type then there must have been a history of spans. |
michael@0 | 228 | float mPreviousSpan; |
michael@0 | 229 | }; |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * Encapsulation class for tap events. In general, these will be generated by |
michael@0 | 233 | * a gesture listener by looking at SingleTouchData/MultiTouchInput instances and |
michael@0 | 234 | * determining whether or not the user was trying to do a gesture. |
michael@0 | 235 | */ |
michael@0 | 236 | class TapGestureInput : public InputData |
michael@0 | 237 | { |
michael@0 | 238 | public: |
michael@0 | 239 | enum TapGestureType |
michael@0 | 240 | { |
michael@0 | 241 | TAPGESTURE_LONG, |
michael@0 | 242 | TAPGESTURE_LONG_UP, |
michael@0 | 243 | TAPGESTURE_UP, |
michael@0 | 244 | TAPGESTURE_CONFIRMED, |
michael@0 | 245 | TAPGESTURE_DOUBLE, |
michael@0 | 246 | TAPGESTURE_CANCEL |
michael@0 | 247 | }; |
michael@0 | 248 | |
michael@0 | 249 | TapGestureInput(TapGestureType aType, |
michael@0 | 250 | uint32_t aTime, |
michael@0 | 251 | const ScreenIntPoint& aPoint, |
michael@0 | 252 | Modifiers aModifiers) |
michael@0 | 253 | : InputData(TAPGESTURE_INPUT, aTime, aModifiers), |
michael@0 | 254 | mType(aType), |
michael@0 | 255 | mPoint(aPoint) |
michael@0 | 256 | { |
michael@0 | 257 | |
michael@0 | 258 | |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | TapGestureType mType; |
michael@0 | 262 | ScreenIntPoint mPoint; |
michael@0 | 263 | }; |
michael@0 | 264 | |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | #endif // InputData_h__ |