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 int32_t DEFAULT_SLIDE_FINGER = DEFAULT_PAN_FINGER; 42 constexpr double DEFAULT_SLIDE_SPEED = 300.0; 43 constexpr int32_t DEFAULT_LONG_PRESS_DURATION = 100; 44 45 class GestureRecognizer; 46 class PipelineBase; 47 48 struct TransformConfig { 49 double scaleX = 1.0; 50 double scaleY = 1.0; 51 double centerX = 0.0; 52 double centerY = 0.0; 53 double offsetX = 0.0; 54 double offsetY = 0.0; 55 double translateX = 0.0; 56 double translateY = 0.0; 57 double degree = 0.0; 58 int id = -1; 59 Matrix4 localMat; 60 bool operator==(TransformConfig tc) 61 { 62 return scaleX = tc.scaleX && scaleY == tc.scaleY && centerX == tc.centerX && centerY == tc.centerY && 63 offsetX == tc.offsetX && offsetY == tc.offsetY && translateX == tc.translateX && 64 translateY == tc.translateY && degree == tc.degree; 65 } 66 }; 67 68 struct AncestorNodeInfo { 69 int parentId = 0; 70 }; 71 72 enum class GesturePriority { 73 Begin = -1, 74 Low = 0, 75 High, 76 Parallel, 77 End, 78 }; 79 80 enum class GestureMask { 81 Begin = -1, 82 Normal = 0, 83 IgnoreInternal, 84 End, 85 }; 86 87 enum class GestureMode { 88 Begin = -1, 89 Sequence = 0, 90 Parallel, 91 Exclusive, 92 End, 93 }; 94 95 enum class Direction { 96 BEGIN = -1, 97 ALL = 0, 98 HORIZONTAL, 99 VERTICAL, 100 END, 101 }; 102 103 enum class DragEventAction { 104 DRAG_EVENT_START = 0, 105 DRAG_EVENT_MOVE, 106 DRAG_EVENT_END, 107 DRAG_EVENT_OUT, 108 DRAG_EVENT_START_FOR_CONTROLLER, 109 }; 110 111 enum class InputEventType { 112 TOUCH_SCREEN = 0, 113 TOUCH_PAD, 114 MOUSE_BUTTON, 115 AXIS, 116 KEYBOARD, 117 }; 118 119 struct PanDirection final { 120 static constexpr uint32_t NONE = 0; 121 static constexpr uint32_t LEFT = 1; 122 static constexpr uint32_t RIGHT = 2; 123 static constexpr uint32_t HORIZONTAL = 3; 124 static constexpr uint32_t UP = 4; 125 static constexpr uint32_t DOWN = 8; 126 static constexpr uint32_t VERTICAL = 12; 127 static constexpr uint32_t ALL = 15; 128 129 uint32_t type = ALL; 130 }; 131 132 using OnPanFingersFunc = EventCallback<void(int32_t fingers)>; 133 using PanFingersFuncType = OnPanFingersFunc::FunctionType; 134 using OnPanDirectionFunc = EventCallback<void(const PanDirection& direction)>; 135 using PanDirectionFuncType = OnPanDirectionFunc::FunctionType; 136 using OnPanDistanceFunc = EventCallback<void(double distance)>; 137 using PanDistanceFuncType = OnPanDistanceFunc::FunctionType; 138 139 class PanGestureOption : public AceType { 140 DECLARE_ACE_TYPE(PanGestureOption, AceType); 141 142 public: 143 PanGestureOption() = default; 144 ~PanGestureOption() override = default; 145 SetDirection(PanDirection direction)146 void SetDirection(PanDirection direction) 147 { 148 direction_ = direction; 149 for (const auto& callback : onPanDirectionIds_) { 150 (callback.second.GetCallback())(direction); 151 } 152 } 153 GetDirection()154 const PanDirection& GetDirection() const 155 { 156 return direction_; 157 } 158 SetDistance(double distance)159 void SetDistance(double distance) 160 { 161 distance_ = distance; 162 for (const auto& callback : onPanDistanceIds_) { 163 (callback.second.GetCallback())(distance); 164 } 165 } 166 GetDistance()167 double GetDistance() const 168 { 169 return distance_; 170 } 171 SetFingers(int32_t fingers)172 void SetFingers(int32_t fingers) 173 { 174 fingers_ = fingers; 175 for (const auto& callback : onPanFingersIds_) { 176 (callback.second.GetCallback())(fingers); 177 } 178 } 179 GetFingers()180 int32_t GetFingers() const 181 { 182 return fingers_; 183 } 184 GetOnPanFingersIds()185 std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc>& GetOnPanFingersIds() 186 { 187 return onPanFingersIds_; 188 } 189 SetOnPanFingersId(const OnPanFingersFunc & onPanFingersId)190 void SetOnPanFingersId(const OnPanFingersFunc& onPanFingersId) 191 { 192 onPanFingersIds_.emplace(onPanFingersId.GetId(), onPanFingersId); 193 } 194 GetOnPanDirectionIds()195 std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc>& GetOnPanDirectionIds() 196 { 197 return onPanDirectionIds_; 198 } 199 SetOnPanDirectionId(const OnPanDirectionFunc & onPanDirectionId)200 void SetOnPanDirectionId(const OnPanDirectionFunc& onPanDirectionId) 201 { 202 onPanDirectionIds_.emplace(onPanDirectionId.GetId(), onPanDirectionId); 203 } 204 GetOnPanDistanceIds()205 std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc>& GetOnPanDistanceIds() 206 { 207 return onPanDistanceIds_; 208 } 209 SetOnPanDistanceId(const OnPanDistanceFunc & onPanDistanceId)210 void SetOnPanDistanceId(const OnPanDistanceFunc& onPanDistanceId) 211 { 212 onPanDistanceIds_.emplace(onPanDistanceId.GetId(), onPanDistanceId); 213 } 214 215 private: 216 PanDirection direction_; 217 double distance_ = DEFAULT_PAN_DISTANCE.ConvertToPx(); 218 int32_t fingers_ = 1; 219 std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc> onPanFingersIds_; 220 std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc> onPanDirectionIds_; 221 std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc> onPanDistanceIds_; 222 }; 223 224 struct SwipeDirection final { 225 static constexpr uint32_t NONE = 0; 226 static constexpr uint32_t HORIZONTAL = 1; 227 static constexpr uint32_t VERTICAL = 2; 228 static constexpr uint32_t ALL = 3; 229 230 uint32_t type = ALL; 231 }; 232 using OnSwipeFingersFunc = EventCallback<void(int32_t fingers)>; 233 using SwipeFingersFuncType = OnSwipeFingersFunc::FunctionType; 234 using OnSwipeDirectionFunc = EventCallback<void(const SwipeDirection& direction)>; 235 using SwipeDirectionFuncType = OnSwipeDirectionFunc::FunctionType; 236 using OnSwipeSpeedFunc = EventCallback<void(double speed)>; 237 using SwipeSpeedFuncType = OnSwipeSpeedFunc::FunctionType; 238 239 struct FingerInfo { 240 int32_t fingerId_ = -1; 241 // global position at which the touch point contacts the screen. 242 Offset globalLocation_; 243 // Different from global location, The local location refers to the location of the contact point relative to the 244 // current node which has the recognizer. 245 Offset localLocation_; 246 247 //screen position at which the touch point contacts the screen. 248 Offset screenLocation_; 249 SourceType sourceType_ = SourceType::NONE; 250 SourceTool sourceTool_ = SourceTool::UNKNOWN; 251 }; 252 } // namespace OHOS::Ace 253 254 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_INFO_H 255