1 /* 2 * Copyright (c) 2021-2022 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 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_INFO_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_INFO_H 18 19 #include <functional> 20 #include <list> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "base/geometry/matrix4.h" 26 #include "base/geometry/offset.h" 27 #include "base/geometry/point.h" 28 #include "base/memory/ace_type.h" 29 #include "base/utils/event_callback.h" 30 #include "base/utils/macros.h" 31 #include "base/utils/type_definition.h" 32 #include "core/event/ace_events.h" 33 #include "core/gestures/velocity.h" 34 35 namespace OHOS::Ace { 36 37 constexpr int32_t DEFAULT_PAN_FINGER = 1; 38 constexpr Dimension DEFAULT_PAN_DISTANCE = 5.0_vp; 39 constexpr Dimension DRAG_PAN_DISTANCE_MOUSE = 1.0_vp; 40 constexpr Dimension DEFAULT_SLIDE_DISTANCE = DEFAULT_PAN_DISTANCE; 41 constexpr Dimension DEFAULT_PEN_PAN_DISTANCE = 8.0_vp; 42 constexpr int32_t DEFAULT_SLIDE_FINGER = DEFAULT_PAN_FINGER; 43 constexpr double DEFAULT_SLIDE_SPEED = 300.0; 44 constexpr int32_t DEFAULT_LONG_PRESS_DURATION = 100; 45 46 class GestureRecognizer; 47 class PipelineBase; 48 49 struct TransformConfig { 50 double scaleX = 1.0; 51 double scaleY = 1.0; 52 double centerX = 0.0; 53 double centerY = 0.0; 54 double offsetX = 0.0; 55 double offsetY = 0.0; 56 double translateX = 0.0; 57 double translateY = 0.0; 58 double degree = 0.0; 59 int id = -1; 60 Matrix4 localMat; 61 bool operator==(TransformConfig tc) 62 { 63 return scaleX = tc.scaleX && scaleY == tc.scaleY && centerX == tc.centerX && centerY == tc.centerY && 64 offsetX == tc.offsetX && offsetY == tc.offsetY && translateX == tc.translateX && 65 translateY == tc.translateY && degree == tc.degree; 66 } 67 }; 68 69 struct AncestorNodeInfo { 70 int parentId = 0; 71 }; 72 73 enum class GesturePriority { 74 Begin = -1, 75 Low = 0, 76 High, 77 Parallel, 78 End, 79 }; 80 81 enum class GestureMask { 82 Begin = -1, 83 Normal = 0, 84 IgnoreInternal, 85 End, 86 }; 87 88 enum class GestureMode { 89 Begin = -1, 90 Sequence = 0, 91 Parallel, 92 Exclusive, 93 End, 94 }; 95 96 enum class Direction { 97 BEGIN = -1, 98 ALL = 0, 99 HORIZONTAL, 100 VERTICAL, 101 END, 102 }; 103 104 enum class DragEventAction { 105 DRAG_EVENT_START = 0, 106 DRAG_EVENT_MOVE, 107 DRAG_EVENT_END, 108 DRAG_EVENT_OUT, 109 DRAG_EVENT_START_FOR_CONTROLLER, 110 DRAG_EVENT_PULL_CANCEL, 111 DRAG_EVENT_PULL_THROW, 112 }; 113 114 enum class InputEventType { 115 TOUCH_SCREEN = 0, 116 TOUCH_PAD, 117 MOUSE_BUTTON, 118 AXIS, 119 KEYBOARD, 120 }; 121 122 enum class RecognizerDelayStatus { 123 NONE = 0, 124 START, 125 END, 126 }; 127 128 struct PanDirection final { 129 static constexpr uint32_t NONE = 0; 130 static constexpr uint32_t LEFT = 1; 131 static constexpr uint32_t RIGHT = 2; 132 static constexpr uint32_t HORIZONTAL = 3; 133 static constexpr uint32_t UP = 4; 134 static constexpr uint32_t DOWN = 8; 135 static constexpr uint32_t VERTICAL = 12; 136 static constexpr uint32_t ALL = 15; 137 138 uint32_t type = ALL; 139 }; 140 141 using OnPanFingersFunc = EventCallback<void(int32_t fingers)>; 142 using PanFingersFuncType = OnPanFingersFunc::FunctionType; 143 using OnPanDirectionFunc = EventCallback<void(const PanDirection& direction)>; 144 using PanDirectionFuncType = OnPanDirectionFunc::FunctionType; 145 using OnPanDistanceFunc = EventCallback<void(double distance)>; 146 using PanDistanceFuncType = OnPanDistanceFunc::FunctionType; 147 using PanDistanceMap = std::unordered_map<SourceTool, double>; 148 using PanDistanceMapDimension = std::unordered_map<SourceTool, Dimension>; 149 150 class PanGestureOption : public AceType { 151 DECLARE_ACE_TYPE(PanGestureOption, AceType); 152 153 public: 154 PanGestureOption() = default; 155 ~PanGestureOption() override = default; 156 SetDirection(PanDirection direction)157 void SetDirection(PanDirection direction) 158 { 159 direction_ = direction; 160 for (const auto& callback : onPanDirectionIds_) { 161 (callback.second.GetCallback())(direction); 162 } 163 } 164 GetDirection()165 const PanDirection& GetDirection() const 166 { 167 return direction_; 168 } 169 SetDistance(double distance)170 void SetDistance(double distance) 171 { 172 distance_ = distance; 173 distanceMap_[SourceTool::UNKNOWN] = Dimension( 174 Dimension(distance_, DimensionUnit::PX).ConvertToVp(), DimensionUnit::VP); 175 for (const auto& callback : onPanDistanceIds_) { 176 (callback.second.GetCallback())(distance); 177 } 178 } 179 SetDistanceMap(const PanDistanceMapDimension & distanceMap)180 void SetDistanceMap(const PanDistanceMapDimension& distanceMap) 181 { 182 distanceMap_ = distanceMap; 183 } 184 GetPanDistanceMap()185 const PanDistanceMapDimension& GetPanDistanceMap() const 186 { 187 return distanceMap_; 188 } 189 GetDistance()190 double GetDistance() const 191 { 192 return distance_; 193 } 194 SetFingers(int32_t fingers)195 void SetFingers(int32_t fingers) 196 { 197 fingers_ = fingers; 198 for (const auto& callback : onPanFingersIds_) { 199 (callback.second.GetCallback())(fingers); 200 } 201 } 202 GetFingers()203 int32_t GetFingers() const 204 { 205 return fingers_; 206 } 207 SetIsLimitFingerCount(bool isLimitFingerCount)208 void SetIsLimitFingerCount(bool isLimitFingerCount) 209 { 210 isLimitFingerCount_ = isLimitFingerCount; 211 } 212 GetIsLimitFingerCount()213 bool GetIsLimitFingerCount() const 214 { 215 return isLimitFingerCount_; 216 } 217 GetOnPanFingersIds()218 std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc>& GetOnPanFingersIds() 219 { 220 return onPanFingersIds_; 221 } 222 SetOnPanFingersId(const OnPanFingersFunc & onPanFingersId)223 void SetOnPanFingersId(const OnPanFingersFunc& onPanFingersId) 224 { 225 onPanFingersIds_.emplace(onPanFingersId.GetId(), onPanFingersId); 226 } 227 GetOnPanDirectionIds()228 std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc>& GetOnPanDirectionIds() 229 { 230 return onPanDirectionIds_; 231 } 232 SetOnPanDirectionId(const OnPanDirectionFunc & onPanDirectionId)233 void SetOnPanDirectionId(const OnPanDirectionFunc& onPanDirectionId) 234 { 235 onPanDirectionIds_.emplace(onPanDirectionId.GetId(), onPanDirectionId); 236 } 237 GetOnPanDistanceIds()238 std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc>& GetOnPanDistanceIds() 239 { 240 return onPanDistanceIds_; 241 } 242 SetOnPanDistanceId(const OnPanDistanceFunc & onPanDistanceId)243 void SetOnPanDistanceId(const OnPanDistanceFunc& onPanDistanceId) 244 { 245 onPanDistanceIds_.emplace(onPanDistanceId.GetId(), onPanDistanceId); 246 } 247 248 private: 249 PanDirection direction_; 250 double distance_ = DEFAULT_PAN_DISTANCE.ConvertToPx(); 251 PanDistanceMapDimension distanceMap_; 252 int32_t fingers_ = 1; 253 bool isLimitFingerCount_ = false; 254 std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc> onPanFingersIds_; 255 std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc> onPanDirectionIds_; 256 std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc> onPanDistanceIds_; 257 }; 258 259 struct SwipeDirection final { 260 static constexpr uint32_t NONE = 0; 261 static constexpr uint32_t HORIZONTAL = 1; 262 static constexpr uint32_t VERTICAL = 2; 263 static constexpr uint32_t ALL = 3; 264 265 uint32_t type = ALL; 266 }; 267 using OnSwipeFingersFunc = EventCallback<void(int32_t fingers)>; 268 using SwipeFingersFuncType = OnSwipeFingersFunc::FunctionType; 269 using OnSwipeDirectionFunc = EventCallback<void(const SwipeDirection& direction)>; 270 using SwipeDirectionFuncType = OnSwipeDirectionFunc::FunctionType; 271 using OnSwipeSpeedFunc = EventCallback<void(double speed)>; 272 using SwipeSpeedFuncType = OnSwipeSpeedFunc::FunctionType; 273 274 struct FingerInfo { 275 int32_t fingerId_ = -1; 276 int32_t operatingHand_ = 0; 277 // global position at which the touch point contacts the screen. 278 Offset globalLocation_; 279 // Different from global location, The local location refers to the location of the contact point relative to the 280 // current node which has the recognizer. 281 Offset localLocation_; 282 283 //screen position at which the touch point contacts the screen. 284 Offset screenLocation_; 285 // The location where the touch point touches the screen when there are multiple screens. 286 Offset globalDisplayLocation_; 287 SourceType sourceType_ = SourceType::NONE; 288 SourceTool sourceTool_ = SourceTool::UNKNOWN; 289 }; 290 } // namespace OHOS::Ace 291 292 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_INFO_H 293