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 #ifdef SECURITY_COMPONENT_ENABLE SetDisplayX(double displayX)248 void SetDisplayX(double displayX) 249 { 250 displayX_ = displayX; 251 } 252 GetDisplayX()253 double GetDisplayX() const 254 { 255 return displayX_; 256 } 257 SetDisplayY(double displayY)258 void SetDisplayY(double displayY) 259 { 260 displayY_ = displayY; 261 } 262 GetDisplayY()263 double GetDisplayY() const 264 { 265 return displayY_; 266 } 267 SetSecCompHandleEvent(const std::shared_ptr<JsonValue> & event)268 void SetSecCompHandleEvent(const std::shared_ptr<JsonValue>& event) 269 { 270 secCompHandleEvent_ = event; 271 } 272 GetSecCompHandleEvent()273 std::shared_ptr<JsonValue> GetSecCompHandleEvent() const 274 { 275 return secCompHandleEvent_; 276 } 277 #endif 278 private: 279 bool repeat_ = false; 280 bool pressed_ = false; 281 double offsetX_ = 0.0; 282 double offsetY_ = 0.0; 283 double scale_ = 1.0; 284 double angle_ = 0.0; 285 Velocity velocity_; 286 double mainVelocity_ = 0.0; 287 double speed_ = 0.0; 288 double mainSpeed_ = 0.0; 289 double mainDelta_ = 0.0; 290 int32_t pointerId_ = 0; 291 #ifdef SECURITY_COMPONENT_ENABLE 292 double displayX_ = 0.0; 293 double displayY_ = 0.0; 294 std::shared_ptr<JsonValue> secCompHandleEvent_; 295 #endif 296 std::shared_ptr<MMI::PointerEvent> pointerEvent_; 297 Point globalPoint_; 298 // global position at which the touch point contacts the screen. 299 Offset globalLocation_; 300 // Different from global location, The local location refers to the location of the contact point relative to the 301 // current node which has the recognizer. 302 Offset localLocation_; 303 Offset screenLocation_; 304 Offset pinchCenter_; 305 Offset delta_; 306 std::list<FingerInfo> fingerList_; 307 InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN; 308 }; 309 310 using GestureEventFunc = std::function<void(GestureEvent& info)>; 311 using GestureEventNoParameter = std::function<void()>; 312 313 class ACE_EXPORT Gesture : public virtual AceType { 314 DECLARE_ACE_TYPE(Gesture, AceType); 315 316 public: 317 Gesture() = default; Gesture(int32_t fingers)318 explicit Gesture(int32_t fingers) : fingers_(fingers) {} 319 ~Gesture() override = default; 320 SetOnActionId(const GestureEventFunc & onActionId)321 void SetOnActionId(const GestureEventFunc& onActionId) 322 { 323 onActionId_ = std::make_unique<GestureEventFunc>(onActionId); 324 } SetOnActionStartId(const GestureEventFunc & onActionStartId)325 void SetOnActionStartId(const GestureEventFunc& onActionStartId) 326 { 327 onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId); 328 } SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)329 void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId) 330 { 331 onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId); 332 } SetOnActionEndId(const GestureEventFunc & onActionEndId)333 void SetOnActionEndId(const GestureEventFunc& onActionEndId) 334 { 335 onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId); 336 } SetOnActionCancelId(const GestureEventNoParameter & onActionCancelId)337 void SetOnActionCancelId(const GestureEventNoParameter& onActionCancelId) 338 { 339 onActionCancelId_ = std::make_unique<GestureEventNoParameter>(onActionCancelId); 340 } SetPriority(GesturePriority priority)341 void SetPriority(GesturePriority priority) 342 { 343 priority_ = priority; 344 } SetGestureMask(GestureMask gestureMask)345 void SetGestureMask(GestureMask gestureMask) 346 { 347 gestureMask_ = gestureMask; 348 } 349 GetPriority()350 GesturePriority GetPriority() const 351 { 352 return priority_; 353 } 354 GetGestureMask()355 GestureMask GetGestureMask() const 356 { 357 return gestureMask_; 358 } 359 360 virtual RefPtr<GestureRecognizer> CreateRecognizer(WeakPtr<PipelineBase> context) = 0; 361 362 protected: 363 int32_t fingers_ = 1; 364 GesturePriority priority_ = GesturePriority::Low; 365 GestureMask gestureMask_ = GestureMask::Normal; 366 std::unique_ptr<GestureEventFunc> onActionId_; 367 std::unique_ptr<GestureEventFunc> onActionStartId_; 368 std::unique_ptr<GestureEventFunc> onActionUpdateId_; 369 std::unique_ptr<GestureEventFunc> onActionEndId_; 370 std::unique_ptr<GestureEventNoParameter> onActionCancelId_; 371 }; 372 373 } // namespace OHOS::Ace 374 375 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H