• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EVENT_TOUCH_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TOUCH_EVENT_H
18 
19 #include <list>
20 #include <utility>
21 
22 #include "base/geometry/offset.h"
23 #include "base/memory/ace_type.h"
24 #include "base/utils/time_util.h"
25 #include "core/components_ng/event/event_constants.h"
26 #include "core/components_ng/event/target_component.h"
27 #include "core/event/ace_events.h"
28 #include "core/event/axis_event.h"
29 
30 namespace OHOS::MMI {
31 class PointerEvent;
32 } // namespace OHOS::MMI
33 
34 namespace OHOS::Ace::NG {
35 class FrameNode;
36 } // namespace OHOS::Ace::NG
37 
38 namespace OHOS::Ace {
39 
40 static const int32_t TOUCH_TOOL_BASE_ID = 100;
41 
42 enum class TouchType : size_t {
43     DOWN = 0,
44     UP,
45     MOVE,
46     CANCEL,
47     PULL_DOWN,
48     PULL_UP,
49     PULL_MOVE,
50     PULL_IN_WINDOW,
51     PULL_OUT_WINDOW,
52     HOVER_ENTER,
53     HOVER_MOVE,
54     HOVER_EXIT,
55     HOVER_CANCEL,
56     PROXIMITY_IN,
57     PROXIMITY_OUT,
58     UNKNOWN,
59 };
60 
61 struct TouchPoint final {
62     int32_t id = 0;
63     float x = 0.0f;
64     float y = 0.0f;
65     float screenX = 0.0f;
66     float screenY = 0.0f;
67     TimeStamp downTime;
68     double size = 0.0;
69     float force = 0.0f;
70     std::optional<float> tiltX;
71     std::optional<float> tiltY;
72     SourceTool sourceTool = SourceTool::UNKNOWN;
73     bool isPressed = false;
74     int32_t originalId = 0;
75 };
76 
77 /**
78  * @brief TouchEvent contains the active change point and a list of all touch points.
79  */
80 struct TouchEvent final : public UIInputEvent {
81     ~TouchEvent() = default;
82     // the active changed point info
83     // The ID is used to identify the point of contact between the finger and the screen. Different fingers have
84     // different ids.
85     int32_t postEventNodeId = 0;
86     int32_t id = 0;
87     float x = 0.0f;
88     float y = 0.0f;
89     float screenX = 0.0f;
90     float screenY = 0.0f;
91     TouchType type = TouchType::UNKNOWN;
92     TouchType pullType = TouchType::UNKNOWN;
93     double size = 0.0;
94     float force = 0.0f;
95     std::optional<float> tiltX;
96     std::optional<float> tiltY;
97     int64_t deviceId = 0;
98     int32_t targetDisplayId = 0;
99     SourceType sourceType = SourceType::NONE;
100     SourceTool sourceTool = SourceTool::UNKNOWN;
101     int32_t touchEventId = 0;
102     bool isInterpolated = false;
103     bool isMouseTouchTest = false;
104     bool isFalsified = false;
105 
106     // all points on the touch screen.
107     std::vector<TouchPoint> pointers;
108     std::shared_ptr<MMI::PointerEvent> pointerEvent { nullptr };
109     // historical points
110     std::vector<TouchEvent> history;
111     std::vector<KeyCode> pressedKeyCodes_;
112 
113     std::list<std::string> childTouchTestList;
114 
115     // Coordinates relative to the upper-left corner of the current component
116     float localX = 0.0f;
117     float localY = 0.0f;
118     int32_t originalId = 0;
119     bool isInjected = false;
120     bool isPrivacyMode = false;
121 
122     // Save historical touch point slope.
123     float inputXDeltaSlope = 0.0f;
124     float inputYDeltaSlope = 0.0f;
125 
TouchEventfinal126     TouchEvent() {}
127 
SetIdfinal128     TouchEvent& SetId(int32_t id)
129     {
130         this->id = id;
131         return *this;
132     }
133 
SetXfinal134     TouchEvent& SetX(float x)
135     {
136         this->x = x;
137         return *this;
138     }
139 
SetYfinal140     TouchEvent& SetY(float y)
141     {
142         this->y = y;
143         return *this;
144     }
145 
SetScreenXfinal146     TouchEvent& SetScreenX(float screenX)
147     {
148         this->screenX = screenX;
149         return *this;
150     }
151 
SetScreenYfinal152     TouchEvent& SetScreenY(float screenY)
153     {
154         this->screenY = screenY;
155         return *this;
156     }
157 
SetTimefinal158     TouchEvent& SetTime(TimeStamp time)
159     {
160         this->time = time;
161         return *this;
162     }
163 
GetTimeStampfinal164     TimeStamp GetTimeStamp() const
165     {
166         return this->time;
167     }
168 
SetTypefinal169     TouchEvent& SetType(TouchType type)
170     {
171         this->type = type;
172         return *this;
173     }
174 
SetPullTypefinal175     TouchEvent& SetPullType(TouchType pullType)
176     {
177         this->pullType = pullType;
178         return *this;
179     }
180 
SetSizefinal181     TouchEvent& SetSize(double size)
182     {
183         this->size = size;
184         return *this;
185     }
186 
SetForcefinal187     TouchEvent& SetForce(float force)
188     {
189         this->force = force;
190         return *this;
191     }
192 
SetTiltXfinal193     TouchEvent& SetTiltX(std::optional<float> tiltX)
194     {
195         this->tiltX = tiltX;
196         return *this;
197     }
198 
SetTiltYfinal199     TouchEvent& SetTiltY(std::optional<float> tiltY)
200     {
201         this->tiltY = tiltY;
202         return *this;
203     }
204 
SetDeviceIdfinal205     TouchEvent& SetDeviceId(int64_t deviceId)
206     {
207         this->deviceId = deviceId;
208         return *this;
209     }
210 
SetTargetDisplayIdfinal211     TouchEvent& SetTargetDisplayId(int32_t targetDisplayId)
212     {
213         this->targetDisplayId = targetDisplayId;
214         return *this;
215     }
216 
SetSourceTypefinal217     TouchEvent& SetSourceType(SourceType sourceType)
218     {
219         this->sourceType = sourceType;
220         return *this;
221     }
222 
SetSourceToolfinal223     TouchEvent& SetSourceTool(SourceTool sourceTool)
224     {
225         this->sourceTool = sourceTool;
226         return *this;
227     }
228 
SetTouchEventIdfinal229     TouchEvent& SetTouchEventId(int32_t touchEventId)
230     {
231         this->touchEventId = touchEventId;
232         return *this;
233     }
234 
SetIsInterpolatedfinal235     TouchEvent& SetIsInterpolated(bool isInterpolated)
236     {
237         this->isInterpolated = isInterpolated;
238         return *this;
239     }
240 
SetPointersfinal241     TouchEvent& SetPointers(std::vector<TouchPoint> pointers)
242     {
243         this->pointers = std::move(pointers);
244         return *this;
245     }
246 
SetPointerEventfinal247     TouchEvent& SetPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
248     {
249         this->pointerEvent = std::move(pointerEvent);
250         return *this;
251     }
252 
SetOriginalIdfinal253     TouchEvent& SetOriginalId(int32_t originalId)
254     {
255         this->originalId = originalId;
256         return *this;
257     }
258 
SetIsInjectedfinal259     TouchEvent& SetIsInjected(bool isInjected)
260     {
261         this->isInjected = isInjected;
262         return *this;
263     }
264 
SetInputXDeltaSlopefinal265     TouchEvent& SetInputXDeltaSlope(float inputXDeltaSlope)
266     {
267         this->inputXDeltaSlope = inputXDeltaSlope;
268         return *this;
269     }
270 
SetInputYDeltaSlopefinal271     TouchEvent& SetInputYDeltaSlope(float inputYDeltaSlope)
272     {
273         this->inputYDeltaSlope = inputYDeltaSlope;
274         return *this;
275     }
276 
SetPressedKeyCodesfinal277     TouchEvent& SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
278     {
279         this->pressedKeyCodes_ = pressedKeyCodes;
280         return *this;
281     }
282 
CloneWithfinal283     TouchEvent CloneWith(float scale) const
284     {
285         return CloneWith(scale, 0.0f, 0.0f, std::nullopt);
286     }
287 
CloneWithfinal288     TouchEvent CloneWith(float scale, float offsetX, float offsetY, std::optional<int32_t> pointId) const
289     {
290         TouchEvent event;
291         event.id = pointId.has_value() ? pointId.value() : id;
292         event.x = (x - offsetX) / scale;
293         event.y = (y - offsetY) / scale;
294         event.screenX = (screenX - offsetX) / scale;
295         event.screenY = (screenY - offsetY) / scale;
296         event.type = type;
297         event.pullType = pullType;
298         event.time = time;
299         event.size = size;
300         event.force = force;
301         event.tiltX = tiltX;
302         event.tiltY = tiltY;
303         event.deviceId = deviceId;
304         event.targetDisplayId = targetDisplayId;
305         event.sourceType = sourceType;
306         event.sourceTool = sourceTool;
307         event.touchEventId = touchEventId;
308         event.isInterpolated = isInterpolated;
309         event.pointers = std::move(pointers);
310         event.pointerEvent = std::move(pointerEvent);
311         event.pressedKeyCodes_ = std::move(pressedKeyCodes_);
312         event.originalId = originalId;
313         event.isInjected = isInjected;
314         event.isPrivacyMode = isPrivacyMode;
315         event.inputXDeltaSlope = inputXDeltaSlope;
316         event.inputYDeltaSlope = inputYDeltaSlope;
317         return event;
318     }
319 
ToJsonValuefinal320     void ToJsonValue(std::unique_ptr<JsonValue>& json) const
321     {
322         json->Put("id", id);
323         json->Put("x", x);
324         json->Put("y", y);
325         json->Put("sx", screenX);
326         json->Put("sy", screenY);
327         json->Put("ty", static_cast<int32_t>(type));
328         int64_t timeValue = std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count();
329         json->Put("ti", timeValue);
330         json->Put("si", size);
331         json->Put("f", force);
332         int32_t hasTiltX = tiltX.has_value() ? 1 : 0;
333         json->Put("hx", hasTiltX);
334         if (hasTiltX) {
335             json->Put("tx", tiltX.value());
336         }
337         int32_t hasTiltY = tiltY.has_value() ? 1 : 0;
338         json->Put("hy", hasTiltY);
339         if (tiltY.has_value()) {
340             json->Put("ty", tiltY.value());
341         }
342         json->Put("d", deviceId);
343         json->Put("sty", static_cast<int32_t>(sourceType));
344         json->Put("sto", static_cast<int32_t>(sourceTool));
345     }
346 
FromJsonfinal347     void FromJson(const std::unique_ptr<JsonValue>& json)
348     {
349         id = json->GetInt("id");
350         x = json->GetDouble("x");
351         y = json->GetDouble("y");
352         screenX = json->GetDouble("sx");
353         screenY = json->GetDouble("sy");
354         type = static_cast<TouchType>(json->GetInt("ty"));
355         int64_t timeValue = json->GetInt64("ti");
356         time = TimeStamp(std::chrono::nanoseconds(timeValue));
357         size = json->GetDouble("si");
358         force = json->GetDouble("f");
359         int32_t hasTiltX = json->GetInt("hx");
360         int32_t hasTiltY = json->GetInt("hy");
361         if (hasTiltX) {
362             tiltX = json->GetDouble("tx");
363         }
364         if (hasTiltY) {
365             tiltY = json->GetDouble("ty");
366         }
367         deviceId = json->GetInt64("d");
368         sourceType = static_cast<SourceType>(json->GetInt("sty"));
369         sourceTool = static_cast<SourceTool>(json->GetInt("sto"));
370     }
371 
GetOffsetfinal372     Offset GetOffset() const
373     {
374         return Offset(x, y);
375     }
376 
GetScreenOffsetfinal377     Offset GetScreenOffset() const
378     {
379         return Offset(screenX, screenY);
380     }
381 
CovertIdfinal382     void CovertId()
383     {
384         if ((sourceType == SourceType::TOUCH) && (sourceTool == SourceTool::PEN)) {
385             id = id + TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
386             originalId = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
387         }
388     }
389 
CreateScalePointfinal390     TouchEvent CreateScalePoint(float scale) const
391     {
392         if (NearZero(scale)) {
393             return CloneWith(1);
394         }
395         auto temp = pointers;
396         std::for_each(temp.begin(), temp.end(), [scale](auto&& point) {
397             point.x = point.x / scale;
398             point.y = point.y / scale;
399             point.screenX = point.screenX / scale;
400             point.screenY = point.screenY / scale;
401         });
402         return CloneWith(scale);
403     }
404 
UpdateScalePointfinal405     TouchEvent UpdateScalePoint(float scale, float offsetX, float offsetY, int32_t pointId) const
406     {
407         auto temp = pointers;
408         if (NearZero(scale)) {
409             std::for_each(temp.begin(), temp.end(), [offsetX, offsetY](auto&& point) {
410                 point.x = point.x - offsetX;
411                 point.y = point.y - offsetY;
412                 point.screenX = point.screenX - offsetX;
413                 point.screenY = point.screenY - offsetY;
414             });
415             return CloneWith(1, offsetX, offsetY, pointId);
416         }
417 
418         std::for_each(temp.begin(), temp.end(), [scale, offsetX, offsetY](auto&& point) {
419             point.x = (point.x - offsetX) / scale;
420             point.y = (point.y - offsetY) / scale;
421             point.screenX = (point.screenX - offsetX) / scale;
422             point.screenY = (point.screenY - offsetY) / scale;
423         });
424         return CloneWith(scale, offsetX, offsetY, pointId);
425     }
426 
UpdatePointersfinal427     TouchEvent UpdatePointers() const
428     {
429         TouchPoint point { .id = id,
430             .x = x,
431             .y = y,
432             .screenX = screenX,
433             .screenY = screenY,
434             .downTime = time,
435             .size = size,
436             .force = force,
437             .isPressed = (type == TouchType::DOWN) };
438         TouchEvent event;
439         event.SetId(id)
440             .SetX(x)
441             .SetY(y)
442             .SetScreenX(screenX)
443             .SetScreenY(screenY)
444             .SetType(type)
445             .SetTime(time)
446             .SetSize(size)
447             .SetForce(force)
448             .SetDeviceId(deviceId)
449             .SetTargetDisplayId(targetDisplayId)
450             .SetSourceType(sourceType)
451             .SetIsInterpolated(isInterpolated)
452             .SetPointerEvent(pointerEvent)
453             .SetOriginalId(originalId);
454         event.pointers.emplace_back(std::move(point));
455         return event;
456     }
457 
IsPenHoverEventfinal458     bool IsPenHoverEvent() const
459     {
460         return sourceTool == SourceTool::PEN && (type == TouchType::PROXIMITY_IN ||
461         type == TouchType::PROXIMITY_OUT || (type == TouchType::MOVE && NearZero(force)));
462     }
463 };
464 
465 namespace Platform {
466 ACE_FORCE_EXPORT Offset GetTouchEventOriginOffset(const TouchEvent& event);
467 ACE_FORCE_EXPORT TimeStamp GetTouchEventOriginTimeStamp(const TouchEvent& event);
468 ACE_FORCE_EXPORT void UpdatePressedKeyCodes(std::vector<KeyCode>& pressedKeyCodes);
469 } // namespace Platform
470 
471 struct TouchRestrict final {
472     static constexpr uint32_t NONE = 0x00000000;
473     static constexpr uint32_t CLICK = 0x00000001;
474     static constexpr uint32_t LONG_PRESS = 0x00000010;
475     static constexpr uint32_t SWIPE_LEFT = 0x00000100;
476     static constexpr uint32_t SWIPE_RIGHT = 0x00000200;
477     static constexpr uint32_t SWIPE_UP = 0x00000400;
478     static constexpr uint32_t SWIPE_DOWN = 0x00000800;
479     static constexpr uint32_t SWIPE = 0x00000F00;
480     static constexpr uint32_t SWIPE_VERTICAL = 0x0000C00;   // Vertical
481     static constexpr uint32_t SWIPE_HORIZONTAL = 0x0000300; // Horizontal
482     static constexpr uint32_t TOUCH = 0xFFFFFFFF;
483 
484     uint32_t forbiddenType = NONE;
485 
UpdateForbiddenTypefinal486     void UpdateForbiddenType(uint32_t gestureType)
487     {
488         forbiddenType |= gestureType;
489     }
490     SourceType sourceType = SourceType::NONE;
491 
492     SourceType hitTestType = SourceType::TOUCH;
493 
494     InputEventType inputEventType = InputEventType::TOUCH_SCREEN;
495 
496     TouchEvent touchEvent;
497 
498     std::list<std::string> childTouchTestList;
499 
500     // use to dump event tree
501     NG::EventTreeType touchTestType = NG::EventTreeType::TOUCH;
502 };
503 
504 class TouchCallBackInfo : public BaseEventInfo {
505     DECLARE_RELATIONSHIP_OF_CLASSES(TouchCallBackInfo, BaseEventInfo);
506 
507 public:
TouchCallBackInfo(TouchType type)508     explicit TouchCallBackInfo(TouchType type) : BaseEventInfo("onTouchEvent"), touchType_(type) {}
509     ~TouchCallBackInfo() override = default;
510 
SetScreenX(float screenX)511     void SetScreenX(float screenX)
512     {
513         screenX_ = screenX;
514     }
GetScreenX()515     float GetScreenX() const
516     {
517         return screenX_;
518     }
SetScreenY(float screenY)519     void SetScreenY(float screenY)
520     {
521         screenY_ = screenY;
522     }
GetScreenY()523     float GetScreenY() const
524     {
525         return screenY_;
526     }
SetLocalX(float localX)527     void SetLocalX(float localX)
528     {
529         localX_ = localX;
530     }
GetLocalX()531     float GetLocalX() const
532     {
533         return localX_;
534     }
SetLocalY(float localY)535     void SetLocalY(float localY)
536     {
537         localY_ = localY;
538     }
GetLocalY()539     float GetLocalY() const
540     {
541         return localY_;
542     }
SetTouchType(TouchType type)543     void SetTouchType(TouchType type)
544     {
545         touchType_ = type;
546     }
GetTouchType()547     TouchType GetTouchType() const
548     {
549         return touchType_;
550     }
SetTimeStamp(const TimeStamp & time)551     void SetTimeStamp(const TimeStamp& time)
552     {
553         time_ = time;
554     }
GetTimeStamp()555     TimeStamp GetTimeStamp() const
556     {
557         return time_;
558     }
559 
560 private:
561     float screenX_ = 0.0f;
562     float screenY_ = 0.0f;
563     float localX_ = 0.0f;
564     float localY_ = 0.0f;
565     TouchType touchType_ = TouchType::UNKNOWN;
566     TimeStamp time_;
567 };
568 
569 class TouchLocationInfo : public BaseEventInfo {
570     DECLARE_RELATIONSHIP_OF_CLASSES(TouchLocationInfo, TypeInfoBase);
571 
572 public:
TouchLocationInfo(int32_t fingerId)573     explicit TouchLocationInfo(int32_t fingerId) : BaseEventInfo("default")
574     {
575         fingerId_ = fingerId;
576     }
TouchLocationInfo(const std::string & type,int32_t fingerId)577     explicit TouchLocationInfo(const std::string& type, int32_t fingerId) : BaseEventInfo(type)
578     {
579         fingerId_ = fingerId;
580     }
581     ~TouchLocationInfo() override = default;
582 
SetGlobalLocation(const Offset & globalLocation)583     TouchLocationInfo& SetGlobalLocation(const Offset& globalLocation)
584     {
585         globalLocation_ = globalLocation;
586         return *this;
587     }
SetLocalLocation(const Offset & localLocation)588     TouchLocationInfo& SetLocalLocation(const Offset& localLocation)
589     {
590         localLocation_ = localLocation;
591         return *this;
592     }
593 
SetScreenLocation(const Offset & screenLocation)594     TouchLocationInfo& SetScreenLocation(const Offset& screenLocation)
595     {
596         screenLocation_ = screenLocation;
597         return *this;
598     }
599 
GetScreenLocation()600     const Offset& GetScreenLocation() const
601     {
602         return screenLocation_;
603     }
604 
GetLocalLocation()605     const Offset& GetLocalLocation() const
606     {
607         return localLocation_;
608     }
GetGlobalLocation()609     const Offset& GetGlobalLocation() const
610     {
611         return globalLocation_;
612     }
GetFingerId()613     int32_t GetFingerId() const
614     {
615         return fingerId_;
616     }
617 
SetSize(double size)618     void SetSize(double size)
619     {
620         size_ = size;
621     }
622 
GetSize()623     double GetSize() const
624     {
625         return size_;
626     }
627 
SetTouchDeviceId(int64_t deviceId)628     void SetTouchDeviceId(int64_t deviceId)
629     {
630         touchDeviceId_ = deviceId;
631     }
632 
GetTouchDeviceId()633     int64_t GetTouchDeviceId() const
634     {
635         return touchDeviceId_;
636     }
637 
GetTouchType()638     TouchType GetTouchType() const
639     {
640         return touchType_;
641     }
SetTouchType(TouchType type)642     void SetTouchType(TouchType type)
643     {
644         touchType_ = type;
645     }
646 
647 private:
648     // The finger id is used to identify the point of contact between the finger and the screen. Different fingers have
649     // different ids.
650     int32_t fingerId_ = -1;
651 
652     // global position at which the touch point contacts the screen.
653     Offset globalLocation_;
654     // Different from global location, The local location refers to the location of the contact point relative to the
655     // current node which has the recognizer.
656     Offset localLocation_;
657 
658     Offset screenLocation_;
659 
660     // finger touch size
661     double size_ = 0.0;
662 
663     // input device id
664     int64_t touchDeviceId_ = 0;
665 
666     // touch type
667     TouchType touchType_ = TouchType::UNKNOWN;
668 };
669 
670 using GetEventTargetImpl = std::function<std::optional<EventTarget>()>;
671 
672 struct StateRecord {
673     std::string procedure;
674     std::string state;
675     std::string disposal;
676     int64_t timestamp = 0;
677 
StateRecordStateRecord678     StateRecord(const std::string& procedure, const std::string& state, const std::string& disposal,
679         int64_t timestamp):procedure(procedure), state(state), disposal(disposal), timestamp(timestamp)
680     {}
681 
DumpStateRecord682     void Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
683     {
684         std::stringstream oss;
685         oss << "procedure: " << procedure;
686         if (!state.empty()) {
687             oss << ", " << "state: " << state << ", "
688                 << "disposal: " << disposal;
689         }
690         oss << ", " << "timestamp: " << ConvertTimestampToStr(timestamp);
691         dumpList.emplace_back(std::make_pair(depth, oss.str()));
692     }
693 };
694 
695 struct GestureSnapshot : public virtual AceType {
696     DECLARE_ACE_TYPE(GestureSnapshot, AceType);
697 
698 public:
AddProcedureGestureSnapshot699     void AddProcedure(const std::string& procedure, const std::string& state, const std::string& disposal,
700         int64_t timestamp)
701     {
702         if (timestamp == 0) {
703             timestamp = GetCurrentTimestamp();
704         }
705         stateHistory.emplace_back(StateRecord(procedure, state, disposal, timestamp));
706     }
707 
CheckNeedAddMoveGestureSnapshot708     bool CheckNeedAddMove(const std::string& state, const std::string& disposal)
709     {
710         return stateHistory.empty() ||
711             stateHistory.back().state != state || stateHistory.back().disposal != disposal;
712     }
713 
DumpGestureSnapshot714     void Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
715     {
716         std::stringstream oss;
717         oss << "frameNodeId: " << nodeId << ", "
718             << "type: " << type << ", "
719             << "depth: " << this->depth << ", "
720             << std::hex
721             << "id: 0x" << id << ", "
722             << "parentId: 0x" << parentId;
723         if (!customInfo.empty()) {
724             oss << ", " << "customInfo: " << customInfo;
725         }
726         dumpList.emplace_back(std::make_pair(depth + this->depth, oss.str()));
727         dumpList.emplace_back(std::make_pair(depth + 1 + this->depth, "stateHistory:"));
728         for (const auto& state : stateHistory) {
729             state.Dump(dumpList, depth + 1 + 1 + this->depth);
730         }
731     }
732 
TransTouchTypeGestureSnapshot733     static std::string TransTouchType(TouchType type)
734     {
735         switch (type) {
736             case TouchType::DOWN:
737                 return "TouchDown";
738             case TouchType::MOVE:
739                 return "TouchMove";
740             case TouchType::UP:
741                 return "TouchUp";
742             case TouchType::CANCEL:
743                 return "TouchCancel";
744             default:
745                 return std::string("Type:").append(std::to_string(static_cast<int32_t>(type)));
746         }
747     }
748 
749     int32_t nodeId = -1;
750     std::string type;
751     uint64_t id = 0;
752     uint64_t parentId = 0;
753     int32_t depth = 0;
754     std::string customInfo;
755     std::list<StateRecord> stateHistory;
756 };
757 
758 class ACE_EXPORT TouchEventTarget : public virtual AceType {
759     DECLARE_ACE_TYPE(TouchEventTarget, AceType);
760 
761 public:
762     TouchEventTarget() = default;
TouchEventTarget(std::string nodeName,int32_t nodeId)763     TouchEventTarget(std::string nodeName, int32_t nodeId) : nodeName_(std::move(nodeName)), nodeId_(nodeId) {}
764     ~TouchEventTarget() override = default;
765 
766     // if return false means need to stop event dispatch.
767     virtual bool DispatchEvent(const TouchEvent& point) = 0;
768     // if return false means need to stop event bubbling.
769     virtual bool HandleEvent(const TouchEvent& point) = 0;
HandleEvent(const AxisEvent & event)770     virtual bool HandleEvent(const AxisEvent& event)
771     {
772         return true;
773     }
OnFlushTouchEventsBegin()774     virtual void OnFlushTouchEventsBegin() {}
OnFlushTouchEventsEnd()775     virtual void OnFlushTouchEventsEnd() {}
GetAxisDirection()776     virtual Axis GetAxisDirection()
777     {
778         return direction_;
779     }
780 
SetTouchRestrict(const TouchRestrict & touchRestrict)781     void SetTouchRestrict(const TouchRestrict& touchRestrict)
782     {
783         touchRestrict_ = touchRestrict;
784     }
785 
SetGetEventTargetImpl(const GetEventTargetImpl & getEventTargetImpl)786     void SetGetEventTargetImpl(const GetEventTargetImpl& getEventTargetImpl)
787     {
788         getEventTargetImpl_ = getEventTargetImpl;
789     }
790 
GetEventTarget()791     std::optional<EventTarget> GetEventTarget() const
792     {
793         if (getEventTargetImpl_) {
794             return getEventTargetImpl_();
795         }
796         return std::nullopt;
797     }
798 
799     // Coordinate offset is used to calculate the local location of the touch point in the render node.
SetCoordinateOffset(const Offset & coordinateOffset)800     void SetCoordinateOffset(const Offset& coordinateOffset)
801     {
802         coordinateOffset_ = coordinateOffset;
803     }
804 
805     // Gets the coordinate offset to calculate the local location of the touch point by manually.
GetCoordinateOffset()806     const Offset& GetCoordinateOffset() const
807     {
808         return coordinateOffset_;
809     }
810 
SetSubPipelineGlobalOffset(const Offset & subPipelineGlobalOffset,float viewScale)811     void SetSubPipelineGlobalOffset(const Offset& subPipelineGlobalOffset, float viewScale)
812     {
813         subPipelineGlobalOffset_ = subPipelineGlobalOffset;
814         viewScale_ = viewScale;
815     }
816 
DispatchMultiContainerEvent(const TouchEvent & point)817     bool DispatchMultiContainerEvent(const TouchEvent& point)
818     {
819 #ifdef OHOS_STANDARD_SYSTEM
820         if (!subPipelineGlobalOffset_.IsZero()) {
821             auto multiContainerPoint = point.UpdateScalePoint(
822                 viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
823             return DispatchEvent(multiContainerPoint);
824         }
825 #endif
826         return DispatchEvent(point);
827     }
828 
HandleMultiContainerEvent(const TouchEvent & point)829     bool HandleMultiContainerEvent(const TouchEvent& point)
830     {
831 #ifdef OHOS_STANDARD_SYSTEM
832         if (!subPipelineGlobalOffset_.IsZero()) {
833             auto multiContainerPoint = point.UpdateScalePoint(
834                 viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
835             return HandleEvent(multiContainerPoint);
836         }
837 #endif
838         return HandleEvent(point);
839     }
840 
GetNodeName()841     std::string GetNodeName() const
842     {
843         return nodeName_;
844     }
845 
SetNodeId(int id)846     void SetNodeId(int id)
847     {
848         if (nodeId_ != -1) {
849             return;
850         }
851         nodeId_ = id;
852     }
853 
GetNodeId()854     int32_t GetNodeId() const
855     {
856         return nodeId_;
857     }
858 
AttachFrameNode(const WeakPtr<NG::FrameNode> & node)859     virtual void AttachFrameNode(const WeakPtr<NG::FrameNode>& node)
860     {
861         if (!(node_.Invalid())) {
862             return;
863         }
864         node_ = node;
865     }
866 
GetAttachedNode()867     WeakPtr<NG::FrameNode> GetAttachedNode() const
868     {
869         return node_;
870     }
871 
Dump()872     virtual RefPtr<GestureSnapshot> Dump() const
873     {
874         RefPtr<GestureSnapshot> info = AceType::MakeRefPtr<GestureSnapshot>();
875         info->type = GetTypeName();
876         info->id = reinterpret_cast<uintptr_t>(this);
877         return info;
878     }
879 
SetTargetComponent(const RefPtr<NG::TargetComponent> & targetComponent)880     void SetTargetComponent(const RefPtr<NG::TargetComponent>& targetComponent)
881     {
882         if (!targetComponent_) {
883             targetComponent_ = targetComponent;
884         }
885     }
886 
GetTargetComponent()887     RefPtr<NG::TargetComponent> GetTargetComponent()
888     {
889         return targetComponent_;
890     }
891 
SetIsPostEventResult(bool isPostEventResult)892     void SetIsPostEventResult(bool isPostEventResult)
893     {
894         isPostEventResult_ = isPostEventResult;
895     }
896 
IsPostEventResult()897     bool IsPostEventResult() const
898     {
899         return isPostEventResult_;
900     }
901 
902 private:
ShouldResponse()903     virtual bool ShouldResponse() { return true; };
904 
905 protected:
906     Offset coordinateOffset_;
907     GetEventTargetImpl getEventTargetImpl_;
908     TouchRestrict touchRestrict_ { TouchRestrict::NONE };
909     Offset subPipelineGlobalOffset_;
910     float viewScale_ = 1.0f;
911     std::string nodeName_ = "NULL";
912     int32_t nodeId_ = -1;
913     WeakPtr<NG::FrameNode> node_ = nullptr;
914     Axis direction_ = Axis::NONE;
915     RefPtr<NG::TargetComponent> targetComponent_;
916     bool isPostEventResult_ = false;
917     std::optional<TimeStamp> firstInputTime_;
918 };
919 
920 using TouchTestResult = std::list<RefPtr<TouchEventTarget>>;
921 using ResponseLinkResult = std::list<RefPtr<NG::NGGestureRecognizer>>;
922 
923 class TouchEventInfo : public BaseEventInfo {
924     DECLARE_RELATIONSHIP_OF_CLASSES(TouchEventInfo, BaseEventInfo);
925 
926 public:
TouchEventInfo(const std::string & type)927     explicit TouchEventInfo(const std::string& type) : BaseEventInfo(type) {}
928     ~TouchEventInfo() override = default;
929 
AddTouchLocationInfo(TouchLocationInfo && info)930     void AddTouchLocationInfo(TouchLocationInfo&& info)
931     {
932         touches_.emplace_back(info);
933     }
AddChangedTouchLocationInfo(TouchLocationInfo && info)934     void AddChangedTouchLocationInfo(TouchLocationInfo&& info)
935     {
936         changedTouches_.emplace_back(info);
937     }
AddHistoryLocationInfo(TouchLocationInfo && info)938     void AddHistoryLocationInfo(TouchLocationInfo&& info)
939     {
940         history_.emplace_back(std::move(info));
941     }
942 
GetTouches()943     const std::list<TouchLocationInfo>& GetTouches() const
944     {
945         return touches_;
946     }
GetChangedTouches()947     const std::list<TouchLocationInfo>& GetChangedTouches() const
948     {
949         return changedTouches_;
950     }
GetHistory()951     const std::list<TouchLocationInfo>& GetHistory() const
952     {
953         return history_;
954     }
AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent> & info)955     void AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent>& info)
956     {
957         historyPointerEvent_.emplace_back(info);
958     }
GetHistoryPointerEvent()959     const std::list<std::shared_ptr<MMI::PointerEvent>>& GetHistoryPointerEvent() const
960     {
961         return historyPointerEvent_;
962     }
SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)963     void SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
964     {
965         pointerEvent_ = pointerEvent;
966     }
GetPointerEvent()967     const std::shared_ptr<MMI::PointerEvent>& GetPointerEvent() const
968     {
969         return pointerEvent_;
970     }
971 
SetTouchEventsEnd(bool isTouchEventsEnd)972     void SetTouchEventsEnd(bool isTouchEventsEnd)
973     {
974         isTouchEventsEnd_ = isTouchEventsEnd;
975     }
976 
GetTouchEventsEnd()977     bool GetTouchEventsEnd() const
978     {
979         return isTouchEventsEnd_;
980     }
981 
ConvertToTouchEvent()982     TouchEvent ConvertToTouchEvent() const
983     {
984         TouchEvent touchEvent;
985         if (!changedTouches_.empty()) {
986             touchEvent.x = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetX());
987             touchEvent.y = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetY());
988             touchEvent.screenX = static_cast<float>(changedTouches_.front().GetScreenLocation().GetX());
989             touchEvent.screenY = static_cast<float>(changedTouches_.front().GetScreenLocation().GetY());
990             touchEvent.localX = static_cast<float>(changedTouches_.front().GetLocalLocation().GetX());
991             touchEvent.localY = static_cast<float>(changedTouches_.front().GetLocalLocation().GetY());
992             touchEvent.id = changedTouches_.front().GetFingerId();
993             touchEvent.force = changedTouches_.front().GetForce();
994             touchEvent.type = changedTouches_.front().GetTouchType();
995             touchEvent.tiltX = changedTouches_.front().GetTiltX();
996             touchEvent.tiltY = changedTouches_.front().GetTiltY();
997         }
998         touchEvent.time = timeStamp_;
999         return touchEvent;
1000     }
1001 private:
1002     std::shared_ptr<MMI::PointerEvent> pointerEvent_;
1003     std::list<TouchLocationInfo> touches_;
1004     std::list<TouchLocationInfo> changedTouches_;
1005     std::list<TouchLocationInfo> history_;
1006     std::list<std::shared_ptr<MMI::PointerEvent>> historyPointerEvent_;
1007     bool isTouchEventsEnd_ { false };
1008 };
1009 
1010 class ACE_EXPORT GestureEventResult : public AceType {
1011     DECLARE_ACE_TYPE(GestureEventResult, AceType)
1012 
1013 public:
1014     GestureEventResult() = default;
1015     ~GestureEventResult() = default;
1016 
1017     virtual void SetGestureEventResult(bool result) = 0;
1018 };
1019 
1020 class NativeEmbeadTouchInfo : public BaseEventInfo {
1021     DECLARE_RELATIONSHIP_OF_CLASSES(NativeEmbeadTouchInfo, BaseEventInfo);
1022 
1023 public:
NativeEmbeadTouchInfo(const std::string & embedId,const TouchEventInfo & touchEventInfo,const RefPtr<GestureEventResult> & result)1024     NativeEmbeadTouchInfo(const std::string& embedId,
1025         const TouchEventInfo& touchEventInfo,
1026         const RefPtr<GestureEventResult>& result)
1027         : BaseEventInfo("NativeEmbeadTouchInfo"), embedId_(embedId), touchEvent_(touchEventInfo), result_(result) {}
1028     ~NativeEmbeadTouchInfo() override = default;
1029 
GetEmbedId()1030     const std::string& GetEmbedId() const
1031     {
1032         return embedId_;
1033     }
1034 
GetTouchEventInfo()1035     const TouchEventInfo& GetTouchEventInfo() const
1036     {
1037         return touchEvent_;
1038     }
GetResult()1039     const RefPtr<GestureEventResult>& GetResult() const
1040     {
1041         return result_;
1042     }
1043 private:
1044     std::string embedId_;
1045     TouchEventInfo touchEvent_;
1046     RefPtr<GestureEventResult> result_;
1047 };
1048 
1049 using TouchEventFunc = std::function<void(TouchEventInfo&)>;
1050 using OnTouchEventCallback = std::function<void(const TouchEventInfo&)>;
1051 using CatchTouchEventCallback = std::function<void()>;
1052 
1053 } // namespace OHOS::Ace
1054 
1055 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_EVENT_TOUCH_EVENT_H
1056