• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "core/event/touch_event.h"
17 
18 #include "base/input_manager/input_manager.h"
19 #include "core/event/key_event.h"
20 
21 namespace OHOS::Ace {
CovertId()22 void TouchPoint::CovertId()
23 {
24     if (sourceTool == SourceTool::PEN) {
25         originalId = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
26         id = id + originalId;
27     }
28 }
29 
SetId(int32_t id)30 TouchEvent& TouchEvent::SetId(int32_t id)
31 {
32     this->id = id;
33     return *this;
34 }
35 
SetX(float x)36 TouchEvent& TouchEvent::SetX(float x)
37 {
38     this->x = x;
39     return *this;
40 }
41 
SetY(float y)42 TouchEvent& TouchEvent::SetY(float y)
43 {
44     this->y = y;
45     return *this;
46 }
47 
SetScreenX(float screenX)48 TouchEvent& TouchEvent::SetScreenX(float screenX)
49 {
50     this->screenX = screenX;
51     return *this;
52 }
53 
SetScreenY(float screenY)54 TouchEvent& TouchEvent::SetScreenY(float screenY)
55 {
56     this->screenY = screenY;
57     return *this;
58 }
59 
SetTime(TimeStamp time)60 TouchEvent& TouchEvent::SetTime(TimeStamp time)
61 {
62     this->time = time;
63     return *this;
64 }
65 
GetTimeStamp() const66 TimeStamp TouchEvent::GetTimeStamp() const
67 {
68     return this->time;
69 }
70 
SetType(TouchType type)71 TouchEvent& TouchEvent::SetType(TouchType type)
72 {
73     this->type = type;
74     return *this;
75 }
76 
SetPullType(TouchType pullType)77 TouchEvent& TouchEvent::SetPullType(TouchType pullType)
78 {
79     this->pullType = pullType;
80     return *this;
81 }
82 
SetSize(double size)83 TouchEvent& TouchEvent::SetSize(double size)
84 {
85     this->size = size;
86     return *this;
87 }
88 
SetForce(float force)89 TouchEvent& TouchEvent::SetForce(float force)
90 {
91     this->force = force;
92     return *this;
93 }
94 
SetTiltX(std::optional<float> tiltX)95 TouchEvent& TouchEvent::SetTiltX(std::optional<float> tiltX)
96 {
97     this->tiltX = tiltX;
98     return *this;
99 }
100 
SetTiltY(std::optional<float> tiltY)101 TouchEvent& TouchEvent::SetTiltY(std::optional<float> tiltY)
102 {
103     this->tiltY = tiltY;
104     return *this;
105 }
106 
SetRollAngle(std::optional<float> rollAngle)107 TouchEvent& TouchEvent::SetRollAngle(std::optional<float> rollAngle)
108 {
109     this->rollAngle = rollAngle;
110     return *this;
111 }
112 
SetDeviceId(int64_t deviceId)113 TouchEvent& TouchEvent::SetDeviceId(int64_t deviceId)
114 {
115     this->deviceId = deviceId;
116     return *this;
117 }
118 
SetTargetDisplayId(int32_t targetDisplayId)119 TouchEvent& TouchEvent::SetTargetDisplayId(int32_t targetDisplayId)
120 {
121     this->targetDisplayId = targetDisplayId;
122     return *this;
123 }
124 
SetSourceType(SourceType sourceType)125 TouchEvent& TouchEvent::SetSourceType(SourceType sourceType)
126 {
127     this->sourceType = sourceType;
128     return *this;
129 }
130 
SetSourceTool(SourceTool sourceTool)131 TouchEvent& TouchEvent::SetSourceTool(SourceTool sourceTool)
132 {
133     this->sourceTool = sourceTool;
134     return *this;
135 }
136 
SetTouchEventId(int32_t touchEventId)137 TouchEvent& TouchEvent::SetTouchEventId(int32_t touchEventId)
138 {
139     this->touchEventId = touchEventId;
140     return *this;
141 }
142 
SetIsInterpolated(bool isInterpolated)143 TouchEvent& TouchEvent::SetIsInterpolated(bool isInterpolated)
144 {
145     this->isInterpolated = isInterpolated;
146     return *this;
147 }
148 
SetPointers(std::vector<TouchPoint> pointers)149 TouchEvent& TouchEvent::SetPointers(std::vector<TouchPoint> pointers)
150 {
151     this->pointers = std::move(pointers);
152     return *this;
153 }
154 
SetPointerEvent(std::shared_ptr<const MMI::PointerEvent> pointerEvent)155 TouchEvent& TouchEvent::SetPointerEvent(std::shared_ptr<const MMI::PointerEvent> pointerEvent)
156 {
157     this->pointerEvent = std::move(pointerEvent);
158     return *this;
159 }
160 
GetTouchEventPointerEvent() const161 std::shared_ptr<MMI::PointerEvent> TouchEvent::GetTouchEventPointerEvent() const
162 {
163     return InputManager::CreatePointerEvent(pointerEvent);
164 }
165 
SetOriginalId(int32_t originalId)166 TouchEvent& TouchEvent::SetOriginalId(int32_t originalId)
167 {
168     this->originalId = originalId;
169     return *this;
170 }
171 
SetIsInjected(bool isInjected)172 TouchEvent& TouchEvent::SetIsInjected(bool isInjected)
173 {
174     this->isInjected = isInjected;
175     return *this;
176 }
177 
SetInputXDeltaSlope(float inputXDeltaSlope)178 TouchEvent& TouchEvent::SetInputXDeltaSlope(float inputXDeltaSlope)
179 {
180     this->inputXDeltaSlope = inputXDeltaSlope;
181     return *this;
182 }
183 
SetInputYDeltaSlope(float inputYDeltaSlope)184 TouchEvent& TouchEvent::SetInputYDeltaSlope(float inputYDeltaSlope)
185 {
186     this->inputYDeltaSlope = inputYDeltaSlope;
187     return *this;
188 }
189 
SetPressedKeyCodes(const std::vector<KeyCode> & pressedKeyCodes)190 TouchEvent& TouchEvent::SetPressedKeyCodes(const std::vector<KeyCode>& pressedKeyCodes)
191 {
192     this->pressedKeyCodes_ = pressedKeyCodes;
193     return *this;
194 }
195 
SetIsPassThroughMode(bool isPassThroughMode)196 TouchEvent& TouchEvent::SetIsPassThroughMode(bool isPassThroughMode)
197 {
198     this->isPassThroughMode = isPassThroughMode;
199     return *this;
200 }
201 
SetOperatingHand(int32_t operatingHand)202 TouchEvent& TouchEvent::SetOperatingHand(int32_t operatingHand)
203 {
204     this->operatingHand = operatingHand;
205     return *this;
206 }
207 
SetPressedTime(TimeStamp pressedTime)208 TouchEvent& TouchEvent::SetPressedTime(TimeStamp pressedTime)
209 {
210     this->pressedTime = pressedTime;
211     return *this;
212 }
213 
SetWidth(int32_t width)214 TouchEvent& TouchEvent::SetWidth(int32_t width)
215 {
216     this->width = width;
217     return *this;
218 }
219 
SetHeight(int32_t height)220 TouchEvent& TouchEvent::SetHeight(int32_t height)
221 {
222     this->height = height;
223     return *this;
224 }
225 
CloneWith(float scale) const226 TouchEvent TouchEvent::CloneWith(float scale) const
227 {
228     return CloneWith(scale, 0.0f, 0.0f, std::nullopt);
229 }
230 
CloneWith(float scale,float offsetX,float offsetY,std::optional<int32_t> pointId) const231 TouchEvent TouchEvent::CloneWith(float scale, float offsetX, float offsetY, std::optional<int32_t> pointId) const
232 {
233     TouchEvent event;
234     event.id = pointId.has_value() ? pointId.value() : id;
235     event.x = (x - offsetX) / scale;
236     event.y = (y - offsetY) / scale;
237     event.screenX = (screenX - offsetX) / scale;
238     event.screenY = (screenY - offsetY) / scale;
239     event.type = type;
240     event.pullType = pullType;
241     event.time = time;
242     event.size = size;
243     event.force = force;
244     event.tiltX = tiltX;
245     event.tiltY = tiltY;
246     event.rollAngle = rollAngle;
247     event.deviceId = deviceId;
248     event.targetDisplayId = targetDisplayId;
249     event.sourceType = sourceType;
250     event.sourceTool = sourceTool;
251     event.touchEventId = touchEventId;
252     event.isInterpolated = isInterpolated;
253     event.pointers = std::move(pointers);
254     event.pointerEvent = std::move(pointerEvent);
255     event.originalId = originalId;
256     event.pressedKeyCodes_ = pressedKeyCodes_;
257     event.isInjected = isInjected;
258     event.isPrivacyMode = isPrivacyMode;
259     event.inputXDeltaSlope = inputXDeltaSlope;
260     event.inputYDeltaSlope = inputYDeltaSlope;
261     event.eventType = UIInputEventType::TOUCH;
262     event.isPassThroughMode = isPassThroughMode;
263     event.operatingHand = operatingHand;
264     event.width = width;
265     event.height = height;
266     event.pressedTime = pressedTime;
267     return event;
268 }
269 
ToJsonValue(std::unique_ptr<JsonValue> & json) const270 void TouchEvent::ToJsonValue(std::unique_ptr<JsonValue>& json) const
271 {
272     json->Put("id", id);
273     json->Put("x", x);
274     json->Put("y", y);
275     json->Put("sx", screenX);
276     json->Put("sy", screenY);
277     json->Put("ty", static_cast<int32_t>(type));
278     int64_t timeValue = std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch()).count();
279     json->Put("ti", timeValue);
280     json->Put("si", size);
281     json->Put("f", force);
282     int32_t hasTiltX = tiltX.has_value() ? 1 : 0;
283     json->Put("hx", hasTiltX);
284     if (hasTiltX) {
285         json->Put("tx", tiltX.value());
286     }
287     int32_t hasTiltY = tiltY.has_value() ? 1 : 0;
288     json->Put("hy", hasTiltY);
289     if (tiltY.has_value()) {
290         json->Put("ty", tiltY.value());
291     }
292     int32_t hasRollAngle = rollAngle.has_value() ? 1 : 0;
293     json->Put("ha", hasRollAngle);
294     if (rollAngle.has_value()) {
295         json->Put("ta", rollAngle.value());
296     }
297     json->Put("d", deviceId);
298     json->Put("sty", static_cast<int32_t>(sourceType));
299     json->Put("sto", static_cast<int32_t>(sourceTool));
300 }
301 
FromJson(const std::unique_ptr<JsonValue> & json)302 void TouchEvent::FromJson(const std::unique_ptr<JsonValue>& json)
303 {
304     CHECK_NULL_VOID(json);
305     id = json->GetInt("id");
306     x = json->GetDouble("x");
307     y = json->GetDouble("y");
308     screenX = json->GetDouble("sx");
309     screenY = json->GetDouble("sy");
310     type = static_cast<TouchType>(json->GetInt("ty"));
311     int64_t timeValue = json->GetInt64("ti");
312     time = TimeStamp(std::chrono::nanoseconds(timeValue));
313     size = json->GetDouble("si");
314     force = json->GetDouble("f");
315     int32_t hasTiltX = json->GetInt("hx");
316     int32_t hasTiltY = json->GetInt("hy");
317     if (hasTiltX) {
318         tiltX = json->GetDouble("tx");
319     }
320     if (hasTiltY) {
321         tiltY = json->GetDouble("ty");
322     }
323     int32_t hasRollAngle = json->GetInt("ha");
324     if (hasRollAngle) {
325         rollAngle = json->GetDouble("ta");
326     }
327     deviceId = json->GetInt64("d");
328     sourceType = static_cast<SourceType>(json->GetInt("sty"));
329     sourceTool = static_cast<SourceTool>(json->GetInt("sto"));
330 }
331 
GetOffset() const332 Offset TouchEvent::GetOffset() const
333 {
334     return Offset(x, y);
335 }
336 
GetScreenOffset() const337 Offset TouchEvent::GetScreenOffset() const
338 {
339     return Offset(screenX, screenY);
340 }
341 
CovertId()342 void TouchEvent::CovertId()
343 {
344     if ((sourceType == SourceType::TOUCH) && (sourceTool == SourceTool::PEN)) {
345         id = id + TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
346         originalId = TOUCH_TOOL_BASE_ID + static_cast<int32_t>(sourceTool);
347     }
348 }
349 
CreateScalePoint(float scale) const350 TouchEvent TouchEvent::CreateScalePoint(float scale) const
351 {
352     if (NearZero(scale)) {
353         return CloneWith(1);
354     }
355     auto temp = pointers;
356     std::for_each(temp.begin(), temp.end(), [scale](auto&& point) {
357         point.x = point.x / scale;
358         point.y = point.y / scale;
359         point.screenX = point.screenX / scale;
360         point.screenY = point.screenY / scale;
361     });
362     return CloneWith(scale);
363 }
364 
UpdateScalePoint(float scale,float offsetX,float offsetY,int32_t pointId) const365 TouchEvent TouchEvent::UpdateScalePoint(float scale, float offsetX, float offsetY, int32_t pointId) const
366 {
367     auto temp = pointers;
368     if (NearZero(scale)) {
369         std::for_each(temp.begin(), temp.end(), [offsetX, offsetY](auto&& point) {
370             point.x = point.x - offsetX;
371             point.y = point.y - offsetY;
372             point.screenX = point.screenX - offsetX;
373             point.screenY = point.screenY - offsetY;
374         });
375         return CloneWith(1, offsetX, offsetY, pointId);
376     }
377 
378     std::for_each(temp.begin(), temp.end(), [scale, offsetX, offsetY](auto&& point) {
379         point.x = (point.x - offsetX) / scale;
380         point.y = (point.y - offsetY) / scale;
381         point.screenX = (point.screenX - offsetX) / scale;
382         point.screenY = (point.screenY - offsetY) / scale;
383     });
384     return CloneWith(scale, offsetX, offsetY, pointId);
385 }
386 
UpdatePointers() const387 TouchEvent TouchEvent::UpdatePointers() const
388 {
389     TouchPoint point { .id = id,
390         .x = x,
391         .y = y,
392         .screenX = screenX,
393         .screenY = screenY,
394         .downTime = time,
395         .size = size,
396         .force = force,
397         .isPressed = (type == TouchType::DOWN),
398         .operatingHand = operatingHand };
399     TouchEvent event;
400     event.SetId(id)
401         .SetX(x)
402         .SetY(y)
403         .SetScreenX(screenX)
404         .SetScreenY(screenY)
405         .SetType(type)
406         .SetTime(time)
407         .SetSize(size)
408         .SetForce(force)
409         .SetDeviceId(deviceId)
410         .SetTargetDisplayId(targetDisplayId)
411         .SetSourceType(sourceType)
412         .SetIsInterpolated(isInterpolated)
413         .SetPointerEvent(pointerEvent)
414         .SetOriginalId(originalId)
415         .SetIsPassThroughMode(isPassThroughMode)
416         .SetOperatingHand(operatingHand);
417     event.pointers.emplace_back(std::move(point));
418     return event;
419 }
420 
IsPenHoverEvent() const421 bool TouchEvent::IsPenHoverEvent() const
422 {
423     return sourceTool == SourceTool::PEN && (type == TouchType::PROXIMITY_IN || type == TouchType::PROXIMITY_OUT ||
424                                                 (type == TouchType::MOVE && NearZero(force)));
425 }
426 
GetTargetDisplayId() const427 int32_t TouchEvent::GetTargetDisplayId() const
428 {
429     return targetDisplayId;
430 }
431 
SetScreenX(float screenX)432 void TouchCallBackInfo::SetScreenX(float screenX)
433 {
434     screenX_ = screenX;
435 }
436 
GetScreenX() const437 float TouchCallBackInfo::GetScreenX() const
438 {
439     return screenX_;
440 }
441 
SetScreenY(float screenY)442 void TouchCallBackInfo::SetScreenY(float screenY)
443 {
444     screenY_ = screenY;
445 }
446 
GetScreenY() const447 float TouchCallBackInfo::GetScreenY() const
448 {
449     return screenY_;
450 }
451 
SetLocalX(float localX)452 void TouchCallBackInfo::SetLocalX(float localX)
453 {
454     localX_ = localX;
455 }
456 
GetLocalX() const457 float TouchCallBackInfo::GetLocalX() const
458 {
459     return localX_;
460 }
461 
SetLocalY(float localY)462 void TouchCallBackInfo::SetLocalY(float localY)
463 {
464     localY_ = localY;
465 }
466 
GetLocalY() const467 float TouchCallBackInfo::GetLocalY() const
468 {
469     return localY_;
470 }
471 
SetTouchType(TouchType type)472 void TouchCallBackInfo::SetTouchType(TouchType type)
473 {
474     touchType_ = type;
475 }
476 
GetTouchType() const477 TouchType TouchCallBackInfo::GetTouchType() const
478 {
479     return touchType_;
480 }
481 
SetTimeStamp(const TimeStamp & time)482 void TouchCallBackInfo::SetTimeStamp(const TimeStamp& time)
483 {
484     time_ = time;
485 }
486 
GetTimeStamp() const487 TimeStamp TouchCallBackInfo::GetTimeStamp() const
488 {
489     return time_;
490 }
491 
SetGlobalLocation(const Offset & globalLocation)492 TouchLocationInfo& TouchLocationInfo::SetGlobalLocation(const Offset& globalLocation)
493 {
494     globalLocation_ = globalLocation;
495     return *this;
496 }
497 
SetLocalLocation(const Offset & localLocation)498 TouchLocationInfo& TouchLocationInfo::SetLocalLocation(const Offset& localLocation)
499 {
500     localLocation_ = localLocation;
501     return *this;
502 }
503 
SetScreenLocation(const Offset & screenLocation)504 TouchLocationInfo& TouchLocationInfo::SetScreenLocation(const Offset& screenLocation)
505 {
506     screenLocation_ = screenLocation;
507     return *this;
508 }
509 
SetSize(double size)510 void TouchLocationInfo::SetSize(double size)
511 {
512     size_ = size;
513 }
514 
GetSize() const515 double TouchLocationInfo::GetSize() const
516 {
517     return size_;
518 }
519 
SetTouchDeviceId(int64_t deviceId)520 void TouchLocationInfo::SetTouchDeviceId(int64_t deviceId)
521 {
522     touchDeviceId_ = deviceId;
523 }
524 
GetTouchDeviceId() const525 int64_t TouchLocationInfo::GetTouchDeviceId() const
526 {
527     return touchDeviceId_;
528 }
529 
SetTouchType(TouchType type)530 void TouchLocationInfo::SetTouchType(TouchType type)
531 {
532     touchType_ = type;
533 }
534 
SetPressedTime(TimeStamp pressedTime)535 void TouchLocationInfo::SetPressedTime(TimeStamp pressedTime)
536 {
537     pressedTime_ = pressedTime;
538 }
539 
GetPressedTime() const540 TimeStamp TouchLocationInfo::GetPressedTime() const
541 {
542     return pressedTime_;
543 }
544 
SetWidth(int32_t width)545 void TouchLocationInfo::SetWidth(int32_t width)
546 {
547     width_ = width;
548 }
549 
GetWidth() const550 int32_t TouchLocationInfo::GetWidth() const
551 {
552     return width_;
553 }
554 
SetHeight(int32_t height)555 void TouchLocationInfo::SetHeight(int32_t height)
556 {
557     height_ = height;
558 }
559 
GetHeight() const560 int32_t TouchLocationInfo::GetHeight() const
561 {
562     return height_;
563 }
564 
Dump(std::list<std::pair<int32_t,std::string>> & dumpList,int32_t depth) const565 void StateRecord::Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
566 {
567     std::stringstream oss;
568     oss << "procedure: " << procedure;
569     if (!state.empty()) {
570         oss << ", "
571             << "state: " << state << ", "
572             << "disposal: " << disposal << ", "
573             << "extraInfo: " << extraInfo;
574     }
575     oss << ", "
576         << "timestamp: " << ConvertTimestampToStr(timestamp);
577     dumpList.emplace_back(std::make_pair(depth, oss.str()));
578 }
579 
Dump(std::unique_ptr<JsonValue> & json) const580 void StateRecord::Dump(std::unique_ptr<JsonValue>& json) const
581 {
582     json->Put("procedure", procedure.c_str());
583     if (!state.empty()) {
584         json->Put("state", state.c_str());
585         json->Put("disposal", disposal.c_str());
586         json->Put("extraInfo", extraInfo.c_str());
587     }
588     json->Put("timestamp", ConvertTimestampToStr(timestamp).c_str());
589 }
590 
AddProcedure(const std::string & procedure,const std::string & extraInfo,const std::string & state,const std::string & disposal,int64_t timestamp)591 void GestureSnapshot::AddProcedure(const std::string& procedure, const std::string& extraInfo,
592     const std::string& state, const std::string& disposal, int64_t timestamp)
593 {
594     if (timestamp == 0) {
595         timestamp = GetCurrentTimestamp();
596     }
597     stateHistory.emplace_back(StateRecord(procedure, extraInfo, state, disposal, timestamp));
598 }
599 
CheckNeedAddMove(const std::string & state,const std::string & disposal)600 bool GestureSnapshot::CheckNeedAddMove(const std::string& state, const std::string& disposal)
601 {
602     return stateHistory.empty() || stateHistory.back().state != state || stateHistory.back().disposal != disposal;
603 }
604 
Dump(std::list<std::pair<int32_t,std::string>> & dumpList,int32_t depth) const605 void GestureSnapshot::Dump(std::list<std::pair<int32_t, std::string>>& dumpList, int32_t depth) const
606 {
607     std::stringstream oss;
608     oss << "frameNodeId: " << nodeId << ", "
609         << "type: " << type << ", "
610         << "depth: " << this->depth << ", " << std::hex << "id: 0x" << id << ", "
611         << "parentId: 0x" << parentId;
612     if (!customInfo.empty()) {
613         oss << ", "
614             << "customInfo: " << customInfo;
615     }
616     dumpList.emplace_back(std::make_pair(depth + this->depth, oss.str()));
617     dumpList.emplace_back(std::make_pair(depth + 1 + this->depth, "stateHistory:"));
618     for (const auto& state : stateHistory) {
619         state.Dump(dumpList, depth + 1 + 1 + this->depth);
620     }
621 }
622 
TransTouchType(TouchType type)623 std::string GestureSnapshot::TransTouchType(TouchType type)
624 {
625     switch (type) {
626         case TouchType::DOWN:
627             return "TouchDown";
628         case TouchType::MOVE:
629             return "TouchMove";
630         case TouchType::UP:
631             return "TouchUp";
632         case TouchType::CANCEL:
633             return "TouchCancel";
634         default:
635             return std::string("Type:").append(std::to_string(static_cast<int32_t>(type)));
636     }
637 }
638 
GetIds() const639 std::tuple<std::string, std::string> GestureSnapshot::GetIds() const
640 {
641     std::stringstream oss;
642     oss << "0x" << std::hex << id;
643     std::string idStr = oss.str();
644     oss.str("");
645     oss << "0x" << std::hex << parentId;
646     std::string parentIdStr = oss.str();
647     return std::make_tuple(idStr, parentIdStr);
648 }
649 
Dump(std::unique_ptr<JsonValue> & json) const650 void GestureSnapshot::Dump(std::unique_ptr<JsonValue>& json) const
651 {
652     json->Put("frameNodeId", nodeId);
653     json->Put("type", type.c_str());
654     auto result = GetIds();
655     json->Put("id", std::get<0>(result).c_str());
656     json->Put("parentId", std::get<1>(result).c_str());
657     json->Put("depth", this->depth);
658     if (!customInfo.empty()) {
659         json->Put("customInfo", customInfo.c_str());
660     }
661     std::unique_ptr<JsonValue> children = JsonUtil::CreateArray(true);
662     for (const auto& state : stateHistory) {
663         std::unique_ptr<JsonValue> child = JsonUtil::Create(true);
664         state.Dump(child);
665         children->Put(child);
666     }
667     json->Put("stateHistory", children);
668 }
669 
SetTouchRestrict(const TouchRestrict & touchRestrict)670 void TouchEventTarget::SetTouchRestrict(const TouchRestrict& touchRestrict)
671 {
672     touchRestrict_ = touchRestrict;
673 }
674 
SetGetEventTargetImpl(const GetEventTargetImpl & getEventTargetImpl)675 void TouchEventTarget::SetGetEventTargetImpl(const GetEventTargetImpl& getEventTargetImpl)
676 {
677     getEventTargetImpl_ = getEventTargetImpl;
678 }
679 
GetEventTarget() const680 std::optional<EventTarget> TouchEventTarget::GetEventTarget() const
681 {
682     if (getEventTargetImpl_) {
683         return getEventTargetImpl_();
684     }
685     return std::nullopt;
686 }
687 
SetCoordinateOffset(const Offset & coordinateOffset)688 void TouchEventTarget::SetCoordinateOffset(const Offset& coordinateOffset)
689 {
690     coordinateOffset_ = coordinateOffset;
691 }
692 
GetCoordinateOffset() const693 const Offset& TouchEventTarget::GetCoordinateOffset() const
694 {
695     return coordinateOffset_;
696 }
697 
SetSubPipelineGlobalOffset(const Offset & subPipelineGlobalOffset,float viewScale)698 void TouchEventTarget::SetSubPipelineGlobalOffset(const Offset& subPipelineGlobalOffset, float viewScale)
699 {
700     subPipelineGlobalOffset_ = subPipelineGlobalOffset;
701     viewScale_ = viewScale;
702 }
703 
DispatchMultiContainerEvent(const TouchEvent & point)704 bool TouchEventTarget::DispatchMultiContainerEvent(const TouchEvent& point)
705 {
706 #ifdef OHOS_STANDARD_SYSTEM
707     if (!subPipelineGlobalOffset_.IsZero()) {
708         auto multiContainerPoint = point.UpdateScalePoint(
709             viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
710         return DispatchEvent(multiContainerPoint);
711     }
712 #endif
713     return DispatchEvent(point);
714 }
715 
HandleMultiContainerEvent(const TouchEvent & point)716 bool TouchEventTarget::HandleMultiContainerEvent(const TouchEvent& point)
717 {
718 #ifdef OHOS_STANDARD_SYSTEM
719     if (!subPipelineGlobalOffset_.IsZero()) {
720         auto multiContainerPoint = point.UpdateScalePoint(
721             viewScale_, subPipelineGlobalOffset_.GetX(), subPipelineGlobalOffset_.GetY(), point.id);
722         return HandleEvent(multiContainerPoint);
723     }
724 #endif
725     return HandleEvent(point);
726 }
727 
GetNodeName() const728 std::string TouchEventTarget::GetNodeName() const
729 {
730     return nodeName_;
731 }
732 
SetNodeId(int id)733 void TouchEventTarget::SetNodeId(int id)
734 {
735     if (nodeId_ != -1) {
736         return;
737     }
738     nodeId_ = id;
739 }
740 
GetNodeId() const741 int32_t TouchEventTarget::GetNodeId() const
742 {
743     return nodeId_;
744 }
745 
AttachFrameNode(const WeakPtr<NG::FrameNode> & node)746 void TouchEventTarget::AttachFrameNode(const WeakPtr<NG::FrameNode>& node)
747 {
748     if (!(node_.Invalid())) {
749         return;
750     }
751     node_ = node;
752 }
753 
GetAttachedNode() const754 WeakPtr<NG::FrameNode> TouchEventTarget::GetAttachedNode() const
755 {
756     return node_;
757 }
758 
Dump() const759 RefPtr<GestureSnapshot> TouchEventTarget::Dump() const
760 {
761     RefPtr<GestureSnapshot> info = AceType::MakeRefPtr<GestureSnapshot>();
762     info->type = GetTypeName();
763     info->id = reinterpret_cast<uintptr_t>(this);
764     return info;
765 }
766 
SetTargetComponent(const RefPtr<NG::TargetComponent> & targetComponent)767 void TouchEventTarget::SetTargetComponent(const RefPtr<NG::TargetComponent>& targetComponent)
768 {
769     if (!targetComponent_) {
770         targetComponent_ = targetComponent;
771     }
772 }
773 
GetTargetComponent()774 RefPtr<NG::TargetComponent> TouchEventTarget::GetTargetComponent()
775 {
776     return targetComponent_;
777 }
778 
SetIsPostEventResult(bool isPostEventResult)779 void TouchEventTarget::SetIsPostEventResult(bool isPostEventResult)
780 {
781     isPostEventResult_ = isPostEventResult;
782 }
783 
IsPostEventResult() const784 bool TouchEventTarget::IsPostEventResult() const
785 {
786     return isPostEventResult_;
787 }
788 
AddTouchLocationInfo(TouchLocationInfo && info)789 void TouchEventInfo::AddTouchLocationInfo(TouchLocationInfo&& info)
790 {
791     touches_.emplace_back(info);
792 }
793 
AddChangedTouchLocationInfo(TouchLocationInfo && info)794 void TouchEventInfo::AddChangedTouchLocationInfo(TouchLocationInfo&& info)
795 {
796     changedTouches_.emplace_back(info);
797 }
798 
AddHistoryLocationInfo(TouchLocationInfo && info)799 void TouchEventInfo::AddHistoryLocationInfo(TouchLocationInfo&& info)
800 {
801     history_.emplace_back(std::move(info));
802 }
803 
GetHistory() const804 const std::list<TouchLocationInfo>& TouchEventInfo::GetHistory() const
805 {
806     return history_;
807 }
808 
AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent> & info)809 void TouchEventInfo::AddHistoryPointerEvent(const std::shared_ptr<MMI::PointerEvent>& info)
810 {
811     historyPointerEvent_.emplace_back(info);
812 }
813 
GetHistoryPointerEvent() const814 const std::list<std::shared_ptr<MMI::PointerEvent>>& TouchEventInfo::GetHistoryPointerEvent() const
815 {
816     return historyPointerEvent_;
817 }
818 
SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)819 void TouchEventInfo::SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
820 {
821     pointerEvent_ = pointerEvent;
822 }
823 
GetPointerEvent() const824 const std::shared_ptr<MMI::PointerEvent>& TouchEventInfo::GetPointerEvent() const
825 {
826     return pointerEvent_;
827 }
828 
SetTouchEventsEnd(bool isTouchEventsEnd)829 void TouchEventInfo::SetTouchEventsEnd(bool isTouchEventsEnd)
830 {
831     isTouchEventsEnd_ = isTouchEventsEnd;
832 }
833 
GetTouchEventsEnd() const834 bool TouchEventInfo::GetTouchEventsEnd() const
835 {
836     return isTouchEventsEnd_;
837 }
838 
ConvertToTouchEvent() const839 TouchEvent TouchEventInfo::ConvertToTouchEvent() const
840 {
841     TouchEvent touchEvent;
842     if (!changedTouches_.empty()) {
843         touchEvent.x = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetX());
844         touchEvent.y = static_cast<float>(changedTouches_.front().GetGlobalLocation().GetY());
845         touchEvent.screenX = static_cast<float>(changedTouches_.front().GetScreenLocation().GetX());
846         touchEvent.screenY = static_cast<float>(changedTouches_.front().GetScreenLocation().GetY());
847         touchEvent.localX = static_cast<float>(changedTouches_.front().GetLocalLocation().GetX());
848         touchEvent.localY = static_cast<float>(changedTouches_.front().GetLocalLocation().GetY());
849         touchEvent.id = changedTouches_.front().GetFingerId();
850         touchEvent.force = changedTouches_.front().GetForce();
851         touchEvent.type = changedTouches_.front().GetTouchType();
852         touchEvent.tiltX = changedTouches_.front().GetTiltX();
853         touchEvent.tiltY = changedTouches_.front().GetTiltY();
854         touchEvent.rollAngle = changedTouches_.front().GetRollAngle();
855         touchEvent.width = changedTouches_.front().GetWidth();
856         touchEvent.height = changedTouches_.front().GetHeight();
857         touchEvent.pressedTime = changedTouches_.front().GetPressedTime();
858         const auto& targetLocalOffset = changedTouches_.front().GetTarget().area.GetOffset();
859         const auto& targetOrigin = changedTouches_.front().GetTarget().origin;
860         // width height x y globalx globaly
861         touchEvent.targetPositionX = targetLocalOffset.GetX().ConvertToPx();
862         touchEvent.targetPositionY = targetLocalOffset.GetY().ConvertToPx();
863         touchEvent.targetGlobalPositionX = targetOrigin.GetX().ConvertToPx() + targetLocalOffset.GetX().ConvertToPx();
864         touchEvent.targetGlobalPositionY = targetOrigin.GetY().ConvertToPx() + targetLocalOffset.GetY().ConvertToPx();
865         touchEvent.widthArea = changedTouches_.front().GetTarget().area.GetWidth().ConvertToPx();
866         touchEvent.heightArea = changedTouches_.front().GetTarget().area.GetHeight().ConvertToPx();
867         // deviceid
868         touchEvent.deviceId = changedTouches_.front().GetDeviceId();
869         // modifierkeystates
870         touchEvent.modifierKeyState = CalculateModifierKeyState(changedTouches_.front().GetPressedKeyCodes());
871     }
872     touchEvent.time = timeStamp_;
873     return touchEvent;
874 }
875 
GetEmbedId() const876 const std::string& NativeEmbeadTouchInfo::GetEmbedId() const
877 {
878     return embedId_;
879 }
880 
GetTouchEventInfo() const881 const TouchEventInfo& NativeEmbeadTouchInfo::GetTouchEventInfo() const
882 {
883     return touchEvent_;
884 }
GetResult() const885 const RefPtr<GestureEventResult>& NativeEmbeadTouchInfo::GetResult() const
886 {
887     return result_;
888 }
889 
890 } // namespace OHOS::Ace
891