1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H 16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H 17 18 #include "core/gestures/gesture_info.h" 19 20 namespace OHOS::MMI { 21 class PointerEvent; 22 } // namespace OHOS::MMI 23 24 namespace OHOS::Ace { 25 26 class GestureEvent : public BaseEventInfo { 27 DECLARE_RELATIONSHIP_OF_CLASSES(GestureEvent, BaseEventInfo); 28 29 public: GestureEvent()30 GestureEvent() : BaseEventInfo("gesture") {} 31 ~GestureEvent() override = default; 32 SetRepeat(bool repeat)33 void SetRepeat(bool repeat) 34 { 35 repeat_ = repeat; 36 } 37 GetRepeat()38 bool GetRepeat() const 39 { 40 return repeat_; 41 } 42 SetOffsetX(double offsetX)43 void SetOffsetX(double offsetX) 44 { 45 offsetX_ = offsetX; 46 } 47 GetOffsetX()48 double GetOffsetX() const 49 { 50 return offsetX_; 51 } 52 SetOffsetY(double offsetY)53 void SetOffsetY(double offsetY) 54 { 55 offsetY_ = offsetY; 56 } 57 GetOffsetY()58 double GetOffsetY() const 59 { 60 return offsetY_; 61 } 62 SetScale(double scale)63 void SetScale(double scale) 64 { 65 scale_ = scale; 66 } 67 GetScale()68 double GetScale() const 69 { 70 return scale_; 71 } 72 SetAngle(double angle)73 void SetAngle(double angle) 74 { 75 angle_ = angle; 76 } 77 GetAngle()78 double GetAngle() const 79 { 80 return angle_; 81 } 82 SetGlobalPoint(const Point & globalPoint)83 GestureEvent& SetGlobalPoint(const Point& globalPoint) 84 { 85 globalPoint_ = globalPoint; 86 globalLocation_.SetX(globalPoint.GetX()); 87 globalLocation_.SetY(globalPoint.GetY()); 88 return *this; 89 } 90 GetGlobalPoint()91 const Point& GetGlobalPoint() const 92 { 93 return globalPoint_; 94 } 95 SetScreenLocation(const Offset & screenLocation)96 GestureEvent& SetScreenLocation(const Offset& screenLocation) 97 { 98 screenLocation_ = screenLocation; 99 return *this; 100 } 101 GetScreenLocation()102 const Offset& GetScreenLocation() const 103 { 104 return screenLocation_; 105 } 106 SetGlobalLocation(const Offset & globalLocation)107 GestureEvent& SetGlobalLocation(const Offset& globalLocation) 108 { 109 globalLocation_ = globalLocation; 110 globalPoint_.SetX(globalLocation.GetX()); 111 globalPoint_.SetY(globalLocation.GetY()); 112 return *this; 113 } SetLocalLocation(const Offset & localLocation)114 GestureEvent& SetLocalLocation(const Offset& localLocation) 115 { 116 localLocation_ = localLocation; 117 return *this; 118 } 119 GetLocalLocation()120 const Offset& GetLocalLocation() const 121 { 122 return localLocation_; 123 } GetGlobalLocation()124 const Offset& GetGlobalLocation() const 125 { 126 return globalLocation_; 127 } 128 GetPinchCenter()129 const Offset& GetPinchCenter() const 130 { 131 return pinchCenter_; 132 } 133 SetPinchCenter(const Offset & pinchCenter)134 GestureEvent& SetPinchCenter(const Offset& pinchCenter) 135 { 136 pinchCenter_ = pinchCenter; 137 return *this; 138 } 139 GetFingerList()140 const std::list<FingerInfo>& GetFingerList() const 141 { 142 return fingerList_; 143 } 144 SetFingerList(const std::list<FingerInfo> & fingerList)145 void SetFingerList(const std::list<FingerInfo>& fingerList) 146 { 147 fingerList_ = fingerList; 148 } 149 SetSpeed(double speed)150 void SetSpeed(double speed) 151 { 152 speed_ = speed; 153 } 154 GetSpeed()155 double GetSpeed() const 156 { 157 return speed_; 158 } 159 SetMainSpeed(double mainSpeed)160 void SetMainSpeed(double mainSpeed) 161 { 162 mainSpeed_ = mainSpeed; 163 } 164 GetMainSpeed()165 double GetMainSpeed() const 166 { 167 return mainSpeed_; 168 } 169 SetVelocity(const Velocity & velocity)170 void SetVelocity(const Velocity& velocity) 171 { 172 velocity_ = velocity; 173 } 174 GetVelocity()175 const Velocity& GetVelocity() const 176 { 177 return velocity_; 178 } 179 SetMainVelocity(double mainVelocity)180 void SetMainVelocity(double mainVelocity) 181 { 182 mainVelocity_ = mainVelocity; 183 } 184 GetMainVelocity()185 double GetMainVelocity() const 186 { 187 return mainVelocity_; 188 } 189 SetPressed(bool pressed)190 void SetPressed(bool pressed) 191 { 192 pressed_ = pressed; 193 } 194 GetPressed()195 bool GetPressed() const 196 { 197 return pressed_; 198 } 199 SetDelta(const Offset & delta)200 void SetDelta(const Offset& delta) 201 { 202 delta_ = delta; 203 } 204 GetDelta()205 const Offset& GetDelta() const 206 { 207 return delta_; 208 } 209 SetMainDelta(double mainDelta)210 void SetMainDelta(double mainDelta) 211 { 212 mainDelta_ = mainDelta; 213 } 214 GetMainDelta()215 double GetMainDelta() const 216 { 217 return mainDelta_; 218 } 219 SetInputEventType(InputEventType type)220 void SetInputEventType(InputEventType type) 221 { 222 inputEventType_ = type; 223 } 224 GetInputEventType()225 InputEventType GetInputEventType() const 226 { 227 return inputEventType_; 228 } SetPointerId(int32_t pointerId)229 void SetPointerId(int32_t pointerId) 230 { 231 pointerId_ = pointerId; 232 } 233 GetPointerId()234 int32_t GetPointerId() const 235 { 236 return pointerId_; 237 } 238 SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)239 void SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) 240 { 241 pointerEvent_ = pointerEvent; 242 } GetPointerEvent()243 const std::shared_ptr<MMI::PointerEvent>& GetPointerEvent() const 244 { 245 return pointerEvent_; 246 } 247 SetRawGlobalLocation(const Offset & rawGlobalLocation)248 void SetRawGlobalLocation(const Offset& rawGlobalLocation) 249 { 250 rawGlobalLocation_ = rawGlobalLocation; 251 } 252 GetRawGlobalLocation()253 const Offset& GetRawGlobalLocation() const 254 { 255 return rawGlobalLocation_; 256 } 257 GetHorizontalAxis()258 float GetHorizontalAxis() const 259 { 260 return horizontalAxis_; 261 } 262 GetVerticalAxis()263 float GetVerticalAxis() const 264 { 265 return verticalAxis_; 266 } 267 SetHorizontalAxis(float axis)268 void SetHorizontalAxis(float axis) 269 { 270 horizontalAxis_ = axis; 271 } 272 SetVerticalAxis(float axis)273 void SetVerticalAxis(float axis) 274 { 275 verticalAxis_ = axis; 276 } 277 GetInputXDeltaSlope()278 float GetInputXDeltaSlope() const 279 { 280 return inputXDeltaSlope_; 281 } 282 GetInputYDeltaSlope()283 float GetInputYDeltaSlope() const 284 { 285 return inputYDeltaSlope_; 286 } 287 IsInterpolated()288 bool IsInterpolated() const 289 { 290 return isInterpolated_; 291 } 292 SetInputXDeltaSlope(float inputXDeltaSlope)293 void SetInputXDeltaSlope(float inputXDeltaSlope) 294 { 295 inputXDeltaSlope_ = inputXDeltaSlope; 296 } 297 SetInputYDeltaSlope(float inputYDeltaSlope)298 void SetInputYDeltaSlope(float inputYDeltaSlope) 299 { 300 inputYDeltaSlope_ = inputYDeltaSlope; 301 } 302 SetIsInterpolated(bool isInterpolated)303 void SetIsInterpolated(bool isInterpolated) 304 { 305 isInterpolated_ = isInterpolated; 306 } 307 SetPointerEventId(int32_t pointerEventId)308 void SetPointerEventId(int32_t pointerEventId) 309 { 310 pointerEventId_ = pointerEventId; 311 } 312 GetPointerEventId()313 int32_t GetPointerEventId() const 314 { 315 return pointerEventId_; 316 } 317 #ifdef SECURITY_COMPONENT_ENABLE SetDisplayX(double displayX)318 void SetDisplayX(double displayX) 319 { 320 displayX_ = displayX; 321 } 322 GetDisplayX()323 double GetDisplayX() const 324 { 325 return displayX_; 326 } 327 SetDisplayY(double displayY)328 void SetDisplayY(double displayY) 329 { 330 displayY_ = displayY; 331 } 332 GetDisplayY()333 double GetDisplayY() const 334 { 335 return displayY_; 336 } 337 SetSecCompHandleEvent(const std::shared_ptr<JsonValue> & event)338 void SetSecCompHandleEvent(const std::shared_ptr<JsonValue>& event) 339 { 340 secCompHandleEvent_ = event; 341 } 342 GetSecCompHandleEvent()343 std::shared_ptr<JsonValue> GetSecCompHandleEvent() const 344 { 345 return secCompHandleEvent_; 346 } 347 #endif 348 private: 349 bool repeat_ = false; 350 bool pressed_ = false; 351 double offsetX_ = 0.0; 352 double offsetY_ = 0.0; 353 double scale_ = 1.0; 354 double angle_ = 0.0; 355 Velocity velocity_; 356 double mainVelocity_ = 0.0; 357 double speed_ = 0.0; 358 double mainSpeed_ = 0.0; 359 double mainDelta_ = 0.0; 360 // Will be used in drag. 361 int32_t pointerId_ = 0; 362 int32_t pointerEventId_ = 0; 363 #ifdef SECURITY_COMPONENT_ENABLE 364 double displayX_ = 0.0; 365 double displayY_ = 0.0; 366 std::shared_ptr<JsonValue> secCompHandleEvent_; 367 #endif 368 std::shared_ptr<MMI::PointerEvent> pointerEvent_; 369 Point globalPoint_; 370 // global position at which the touch point contacts the screen. 371 Offset globalLocation_; 372 // Different from global location, The local location refers to the location of the contact point relative to the 373 // current node which has the recognizer. 374 Offset localLocation_; 375 // Will be used in drag. 376 Offset screenLocation_; 377 // Raw last touchPoint global location. 378 Offset rawGlobalLocation_; 379 Offset pinchCenter_; 380 Offset delta_; 381 std::list<FingerInfo> fingerList_; 382 InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN; 383 float horizontalAxis_ = 0.0; 384 float verticalAxis_ = 0.0; 385 386 // Save historical touch point slope. 387 bool isInterpolated_ = false; 388 float inputXDeltaSlope_ = 0.0f; 389 float inputYDeltaSlope_ = 0.0f; 390 }; 391 392 using GestureEventFunc = std::function<void(GestureEvent& info)>; 393 using GestureEventNoParameter = std::function<void()>; 394 395 class ACE_EXPORT Gesture : public virtual AceType { 396 DECLARE_ACE_TYPE(Gesture, AceType); 397 398 public: 399 Gesture() = default; Gesture(int32_t fingers)400 explicit Gesture(int32_t fingers) : fingers_(fingers) {} 401 ~Gesture() override = default; 402 SetOnActionId(const GestureEventFunc & onActionId)403 void SetOnActionId(const GestureEventFunc& onActionId) 404 { 405 onActionId_ = std::make_unique<GestureEventFunc>(onActionId); 406 } SetOnActionStartId(const GestureEventFunc & onActionStartId)407 void SetOnActionStartId(const GestureEventFunc& onActionStartId) 408 { 409 onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId); 410 } SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)411 void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId) 412 { 413 onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId); 414 } SetOnActionEndId(const GestureEventFunc & onActionEndId)415 void SetOnActionEndId(const GestureEventFunc& onActionEndId) 416 { 417 onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId); 418 } SetOnActionCancelId(const GestureEventFunc & onActionCancelId)419 void SetOnActionCancelId(const GestureEventFunc& onActionCancelId) 420 { 421 onActionCancelId_ = std::make_unique<GestureEventFunc>(onActionCancelId); 422 } SetPriority(GesturePriority priority)423 void SetPriority(GesturePriority priority) 424 { 425 priority_ = priority; 426 } SetGestureMask(GestureMask gestureMask)427 void SetGestureMask(GestureMask gestureMask) 428 { 429 gestureMask_ = gestureMask; 430 } 431 GetPriority()432 GesturePriority GetPriority() const 433 { 434 return priority_; 435 } 436 GetGestureMask()437 GestureMask GetGestureMask() const 438 { 439 return gestureMask_; 440 } 441 442 virtual RefPtr<GestureRecognizer> CreateRecognizer(WeakPtr<PipelineBase> context) = 0; 443 444 protected: 445 int32_t fingers_ = 1; 446 GesturePriority priority_ = GesturePriority::Low; 447 GestureMask gestureMask_ = GestureMask::Normal; 448 std::unique_ptr<GestureEventFunc> onActionId_; 449 std::unique_ptr<GestureEventFunc> onActionStartId_; 450 std::unique_ptr<GestureEventFunc> onActionUpdateId_; 451 std::unique_ptr<GestureEventFunc> onActionEndId_; 452 std::unique_ptr<GestureEventFunc> onActionCancelId_; 453 }; 454 455 } // namespace OHOS::Ace 456 457 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H