1 /* 2 * Copyright (c) 2021 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_EVENT_TOUCH_EVENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TOUCH_EVENT_H 18 19 #include <list> 20 21 #include "base/geometry/offset.h" 22 #include "base/memory/ace_type.h" 23 #include "core/event/ace_events.h" 24 25 namespace OHOS::Ace { 26 27 enum class TouchType : size_t { 28 DOWN = 0, 29 UP, 30 MOVE, 31 CANCEL, 32 UNKNOWN, 33 }; 34 35 struct TouchRestrict final { 36 static constexpr uint32_t NONE = 0x00000000; 37 static constexpr uint32_t CLICK = 0x00000001; 38 static constexpr uint32_t LONG_PRESS = 0x00000010; 39 static constexpr uint32_t SWIPE_LEFT = 0x00000100; 40 static constexpr uint32_t SWIPE_RIGHT = 0x00000200; 41 static constexpr uint32_t SWIPE_UP = 0x00000400; 42 static constexpr uint32_t SWIPE_DOWN = 0x00000800; 43 static constexpr uint32_t SWIPE = 0x00000F00; 44 static constexpr uint32_t SWIPE_VERTICAL = 0x0000C00; // Vertical 45 static constexpr uint32_t SWIPE_HORIZONTAL = 0x0000300; // Horizontal 46 static constexpr uint32_t TOUCH = 0xFFFFFFFF; 47 48 uint32_t forbiddenType = NONE; 49 UpdateForbiddenTypefinal50 void UpdateForbiddenType(uint32_t gestureType) 51 { 52 forbiddenType |= gestureType; 53 } 54 }; 55 56 struct TouchPoint final { 57 int32_t id = 0; 58 float x = 0.0f; 59 float y = 0.0f; 60 float screenX = 0.0f; 61 float screenY = 0.0f; 62 TimeStamp downTime; 63 double size = 0.0; 64 float force = 0.0f; 65 bool isPressed = false; 66 }; 67 68 /** 69 * @brief TouchEvent contains the active change point and a list of all touch points. 70 */ 71 struct TouchEvent final { 72 // the active changed point info 73 // The ID is used to identify the point of contact between the finger and the screen. Different fingers have 74 // different ids. 75 int32_t id = 0; 76 float x = 0.0f; 77 float y = 0.0f; 78 float screenX = 0.0f; 79 float screenY = 0.0f; 80 TouchType type = TouchType::UNKNOWN; 81 // nanosecond time stamp. 82 TimeStamp time; 83 double size = 0.0; 84 float force = 0.0f; 85 int64_t deviceId = 0; 86 SourceType sourceType = SourceType::NONE; 87 88 // all points on the touch screen. 89 std::vector<TouchPoint> pointers; 90 GetOffsetfinal91 Offset GetOffset() const 92 { 93 return Offset(x, y); 94 } 95 GetScreenOffsetfinal96 Offset GetScreenOffset() const 97 { 98 return Offset(screenX, screenY); 99 } 100 CreateScalePointfinal101 TouchEvent CreateScalePoint(float scale) const 102 { 103 if (NearZero(scale)) { 104 return { id, x, y, screenX, screenY, type, time, size, force, deviceId, sourceType, pointers }; 105 } 106 auto temp = pointers; 107 std::for_each(temp.begin(), temp.end(), [scale](auto&& point) { 108 point.x = point.x / scale; 109 point.y = point.y / scale; 110 point.screenX = point.screenX / scale; 111 point.screenY = point.screenY / scale; 112 }); 113 return { id, x / scale, y / scale, screenX / scale, screenY / scale, type, time, size, force, deviceId, 114 sourceType, temp }; 115 } 116 UpdateScalePointfinal117 TouchEvent UpdateScalePoint(float scale, float offsetX, float offsetY, int32_t pointId) const 118 { 119 auto temp = pointers; 120 if (NearZero(scale)) { 121 std::for_each(temp.begin(), temp.end(), [offsetX, offsetY](auto&& point) { 122 point.x = point.x - offsetX; 123 point.y = point.y - offsetY; 124 point.screenX = point.screenX - offsetX; 125 point.screenY = point.screenY - offsetY; 126 }); 127 return { pointId, x - offsetX, y - offsetY, screenX - offsetX, screenY - offsetY, type, time, size, force, 128 deviceId, sourceType, temp }; 129 } 130 131 std::for_each(temp.begin(), temp.end(), [scale, offsetX, offsetY](auto&& point) { 132 point.x = (point.x - offsetX) / scale; 133 point.y = (point.y - offsetY) / scale; 134 point.screenX = (point.screenX - offsetX) / scale; 135 point.screenY = (point.screenY - offsetY) / scale; 136 }); 137 return { pointId, (x - offsetX) / scale, (y - offsetY) / scale, (screenX - offsetX) / scale, 138 (screenY - offsetY) / scale, type, time, size, force, deviceId, sourceType, temp }; 139 } 140 UpdatePointersfinal141 TouchEvent UpdatePointers() const 142 { 143 TouchPoint point { .id = id, 144 .x = x, 145 .y = y, 146 .screenX = screenX, 147 .screenY = screenY, 148 .downTime = time, 149 .size = size, 150 .force = force, 151 .isPressed = (type == TouchType::DOWN) }; 152 TouchEvent event { .id = id, 153 .x = x, 154 .y = y, 155 .screenX = screenX, 156 .screenY = screenY, 157 .type = type, 158 .time = time, 159 .size = size, 160 .force = force, 161 .deviceId = deviceId, 162 .sourceType = sourceType }; 163 event.pointers.emplace_back(std::move(point)); 164 return event; 165 } 166 }; 167 168 class TouchCallBackInfo : public BaseEventInfo { 169 DECLARE_RELATIONSHIP_OF_CLASSES(TouchCallBackInfo, BaseEventInfo); 170 171 public: TouchCallBackInfo(TouchType type)172 explicit TouchCallBackInfo(TouchType type) : BaseEventInfo("onTouchEvent"), touchType_(type) {} 173 ~TouchCallBackInfo() override = default; 174 SetScreenX(float screenX)175 void SetScreenX(float screenX) 176 { 177 screenX_ = screenX; 178 } GetScreenX()179 float GetScreenX() const 180 { 181 return screenX_; 182 } SetScreenY(float screenY)183 void SetScreenY(float screenY) 184 { 185 screenY_ = screenY; 186 } GetScreenY()187 float GetScreenY() const 188 { 189 return screenY_; 190 } SetLocalX(float localX)191 void SetLocalX(float localX) 192 { 193 localX_ = localX; 194 } GetLocalX()195 float GetLocalX() const 196 { 197 return localX_; 198 } SetLocalY(float localY)199 void SetLocalY(float localY) 200 { 201 localY_ = localY; 202 } GetLocalY()203 float GetLocalY() const 204 { 205 return localY_; 206 } SetTouchType(TouchType type)207 void SetTouchType(TouchType type) 208 { 209 touchType_ = type; 210 } GetTouchType()211 TouchType GetTouchType() const 212 { 213 return touchType_; 214 } SetTimeStamp(const TimeStamp & time)215 void SetTimeStamp(const TimeStamp& time) 216 { 217 time_ = time; 218 } GetTimeStamp()219 TimeStamp GetTimeStamp() const 220 { 221 return time_; 222 } SetForce(float force)223 void SetForce(float force) 224 { 225 force_ = force; 226 } GetForce()227 float GetForce() const 228 { 229 return force_; 230 } 231 232 private: 233 float screenX_ = 0.0f; 234 float screenY_ = 0.0f; 235 float localX_ = 0.0f; 236 float localY_ = 0.0f; 237 float force_ = 0.0f; 238 TouchType touchType_ = TouchType::UNKNOWN; 239 TimeStamp time_; 240 }; 241 242 class TouchLocationInfo : public virtual TypeInfoBase { 243 DECLARE_RELATIONSHIP_OF_CLASSES(TouchLocationInfo, TypeInfoBase); 244 245 public: TouchLocationInfo(int32_t fingerId)246 explicit TouchLocationInfo(int32_t fingerId) : fingerId_(fingerId) {} 247 ~TouchLocationInfo() override = default; 248 SetGlobalLocation(const Offset & globalLocation)249 TouchLocationInfo& SetGlobalLocation(const Offset& globalLocation) 250 { 251 globalLocation_ = globalLocation; 252 return *this; 253 } SetLocalLocation(const Offset & localLocation)254 TouchLocationInfo& SetLocalLocation(const Offset& localLocation) 255 { 256 localLocation_ = localLocation; 257 return *this; 258 } 259 SetScreenLocation(const Offset & screenLocation)260 TouchLocationInfo& SetScreenLocation(const Offset& screenLocation) 261 { 262 screenLocation_ = screenLocation; 263 return *this; 264 } 265 GetScreenLocation()266 const Offset& GetScreenLocation() const 267 { 268 return screenLocation_; 269 } 270 GetLocalLocation()271 const Offset& GetLocalLocation() const 272 { 273 return localLocation_; 274 } GetGlobalLocation()275 const Offset& GetGlobalLocation() const 276 { 277 return globalLocation_; 278 } GetFingerId()279 int32_t GetFingerId() const 280 { 281 return fingerId_; 282 } 283 SetSize(double size)284 void SetSize(double size) 285 { 286 size_ = size; 287 } 288 GetSize()289 double GetSize() const 290 { 291 return size_; 292 } 293 SetTouchDeviceId(int64_t deviceId)294 void SetTouchDeviceId(int64_t deviceId) 295 { 296 touchDeviceId_ = deviceId; 297 } 298 GetTouchDeviceId()299 int64_t GetTouchDeviceId() const 300 { 301 return touchDeviceId_; 302 } 303 SetForce(float force)304 void SetForce(float force) 305 { 306 force_ = force; 307 } GetForce()308 float GetForce() const 309 { 310 return force_; 311 } GetTouchType()312 TouchType GetTouchType() const 313 { 314 return touchType_; 315 } SetTouchType(TouchType type)316 void SetTouchType(TouchType type) 317 { 318 touchType_ = type; 319 } 320 321 private: 322 // The finger id is used to identify the point of contact between the finger and the screen. Different fingers have 323 // different ids. 324 int32_t fingerId_ = -1; 325 326 // global position at which the touch point contacts the screen. 327 Offset globalLocation_; 328 // Different from global location, The local location refers to the location of the contact point relative to the 329 // current node which has the recognizer. 330 Offset localLocation_; 331 332 Offset screenLocation_; 333 334 // finger touch size 335 double size_ = 0.0; 336 337 // input device id 338 int64_t touchDeviceId_ = 0; 339 340 // touch pressure 341 float force_ = 0.0f; 342 343 // touch type 344 TouchType touchType_ = TouchType::UNKNOWN; 345 }; 346 347 class ACE_EXPORT TouchEventTarget : public virtual AceType { 348 DECLARE_ACE_TYPE(TouchEventTarget, AceType); 349 350 public: 351 virtual bool DispatchEvent(const TouchEvent& point) = 0; 352 virtual bool HandleEvent(const TouchEvent& point) = 0; 353 SetTouchRestrict(const TouchRestrict & touchRestrict)354 void SetTouchRestrict(const TouchRestrict& touchRestrict) 355 { 356 touchRestrict_ = touchRestrict; 357 } 358 359 protected: 360 TouchRestrict touchRestrict_ { TouchRestrict::NONE }; 361 }; 362 363 using TouchTestResult = std::list<RefPtr<TouchEventTarget>>; 364 365 } // namespace OHOS::Ace 366 367 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TOUCH_EVENT_H 368