• 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_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/offset.h"
26 #include "base/geometry/point.h"
27 #include "base/image/pixel_map.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 #include "core/gestures/velocity_tracker.h"
35 
36 namespace OHOS::Ace {
37 
38 constexpr int32_t DEFAULT_PAN_FINGER = 1;
39 constexpr double DEFAULT_PAN_DISTANCE = 5.0;
40 constexpr double 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 enum class GesturePriority {
49     Begin = -1,
50     Low = 0,
51     High,
52     Parallel,
53     End,
54 };
55 
56 enum class GestureMask {
57     Begin = -1,
58     Normal = 0,
59     IgnoreInternal,
60     End,
61 };
62 
63 enum class GestureMode {
64     Begin = -1,
65     Sequence = 0,
66     Parallel,
67     Exclusive,
68     End,
69 };
70 
71 enum class Direction {
72     BEGIN = -1,
73     ALL = 0,
74     HORIZONTAL,
75     VERTICAL,
76     END,
77 };
78 
79 enum class DragEventAction {
80     DRAG_EVENT_START = 0,
81     DRAG_EVENT_MOVE,
82     DRAG_EVENT_END,
83     DRAG_EVENT_OUT,
84 };
85 
86 enum class InputEventType {
87     TOUCH_SCREEN = 0,
88     TOUCH_PAD,
89     MOUSE_BUTTON,
90     AXIS,
91     KEYBOARD,
92 };
93 
94 struct PanDirection final {
95     static constexpr uint32_t NONE = 0;
96     static constexpr uint32_t LEFT = 1;
97     static constexpr uint32_t RIGHT = 2;
98     static constexpr uint32_t HORIZONTAL = 3;
99     static constexpr uint32_t UP = 4;
100     static constexpr uint32_t DOWN = 8;
101     static constexpr uint32_t VERTICAL = 12;
102     static constexpr uint32_t ALL = 15;
103 
104     uint32_t type = ALL;
105 };
106 
107 using OnPanFingersFunc = EventCallback<void(int32_t fingers)>;
108 using PanFingersFuncType = OnPanFingersFunc::FunctionType;
109 using OnPanDirectionFunc = EventCallback<void(const PanDirection& direction)>;
110 using PanDirectionFuncType = OnPanDirectionFunc::FunctionType;
111 using OnPanDistanceFunc = EventCallback<void(double distance)>;
112 using PanDistanceFuncType = OnPanDistanceFunc::FunctionType;
113 
114 class PanGestureOption : public AceType {
115     DECLARE_ACE_TYPE(PanGestureOption, AceType);
116 
117 public:
118     PanGestureOption() = default;
119     ~PanGestureOption() override = default;
120 
SetDirection(PanDirection direction)121     void SetDirection(PanDirection direction)
122     {
123         direction_ = direction;
124         for (const auto& callback : onPanDirectionIds_) {
125             (callback.second.GetCallback())(direction);
126         }
127     }
128 
GetDirection()129     const PanDirection& GetDirection() const
130     {
131         return direction_;
132     }
133 
SetDistance(double distance)134     void SetDistance(double distance)
135     {
136         distance_ = distance;
137         for (const auto& callback : onPanDistanceIds_) {
138             (callback.second.GetCallback())(distance);
139         }
140     }
141 
GetDistance()142     double GetDistance() const
143     {
144         return distance_;
145     }
146 
SetFingers(int32_t fingers)147     void SetFingers(int32_t fingers)
148     {
149         fingers_ = fingers;
150         for (const auto& callback : onPanFingersIds_) {
151             (callback.second.GetCallback())(fingers);
152         }
153     }
154 
GetFingers()155     int32_t GetFingers() const
156     {
157         return fingers_;
158     }
159 
GetOnPanFingersIds()160     std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc>& GetOnPanFingersIds()
161     {
162         return onPanFingersIds_;
163     }
164 
SetOnPanFingersId(const OnPanFingersFunc & onPanFingersId)165     void SetOnPanFingersId(const OnPanFingersFunc& onPanFingersId)
166     {
167         onPanFingersIds_.emplace(onPanFingersId.GetId(), onPanFingersId);
168     }
169 
GetOnPanDirectionIds()170     std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc>& GetOnPanDirectionIds()
171     {
172         return onPanDirectionIds_;
173     }
174 
SetOnPanDirectionId(const OnPanDirectionFunc & onPanDirectionId)175     void SetOnPanDirectionId(const OnPanDirectionFunc& onPanDirectionId)
176     {
177         onPanDirectionIds_.emplace(onPanDirectionId.GetId(), onPanDirectionId);
178     }
179 
GetOnPanDistanceIds()180     std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc>& GetOnPanDistanceIds()
181     {
182         return onPanDistanceIds_;
183     }
184 
SetOnPanDistanceId(const OnPanDistanceFunc & onPanDistanceId)185     void SetOnPanDistanceId(const OnPanDistanceFunc& onPanDistanceId)
186     {
187         onPanDistanceIds_.emplace(onPanDistanceId.GetId(), onPanDistanceId);
188     }
189 
190 private:
191     PanDirection direction_;
192     double distance_ = DEFAULT_PAN_DISTANCE;
193     int32_t fingers_ = 1;
194     std::unordered_map<typename OnPanFingersFunc::IdType, OnPanFingersFunc> onPanFingersIds_;
195     std::unordered_map<typename OnPanDirectionFunc::IdType, OnPanDirectionFunc> onPanDirectionIds_;
196     std::unordered_map<typename OnPanDistanceFunc::IdType, OnPanDistanceFunc> onPanDistanceIds_;
197 };
198 
199 struct SwipeDirection final {
200     static constexpr uint32_t NONE = 0;
201     static constexpr uint32_t HORIZONTAL = 1;
202     static constexpr uint32_t VERTICAL = 2;
203     static constexpr uint32_t ALL = 3;
204 
205     uint32_t type = ALL;
206 };
207 using OnSwipeFingersFunc = EventCallback<void(int32_t fingers)>;
208 using SwipeFingersFuncType = OnSwipeFingersFunc::FunctionType;
209 using OnSwipeDirectionFunc = EventCallback<void(const SwipeDirection& direction)>;
210 using SwipeDirectionFuncType = OnSwipeDirectionFunc::FunctionType;
211 using OnSwipeSpeedFunc = EventCallback<void(double speed)>;
212 using SwipeSpeedFuncType = OnSwipeSpeedFunc::FunctionType;
213 
214 class PasteData : public AceType {
215     DECLARE_ACE_TYPE(PasteData, AceType);
216 
217 public:
218     PasteData() = default;
219     ~PasteData() override = default;
220 
SetPlainText(const std::string & plainText)221     void SetPlainText(const std::string& plainText)
222     {
223         plainText_ = plainText;
224     }
225 
GetPlainText()226     const std::string& GetPlainText() const
227     {
228         return plainText_;
229     }
230 
231 private:
232     std::string plainText_;
233 };
234 
235 class DragEvent : public AceType {
236     DECLARE_ACE_TYPE(DragEvent, AceType)
237 
238 public:
239     DragEvent() = default;
240     ~DragEvent() override = default;
241 
SetPasteData(const RefPtr<PasteData> & pasteData)242     void SetPasteData(const RefPtr<PasteData>& pasteData)
243     {
244         pasteData_ = pasteData;
245     }
246 
GetPasteData()247     RefPtr<PasteData> GetPasteData() const
248     {
249         return pasteData_;
250     }
251 
GetX()252     double GetX() const
253     {
254         return x_;
255     }
256 
GetY()257     double GetY() const
258     {
259         return y_;
260     }
261 
SetX(double x)262     void SetX(double x)
263     {
264         x_ = x;
265     }
266 
SetY(double y)267     void SetY(double y)
268     {
269         y_ = y;
270     }
271 
SetDescription(const std::string & description)272     void SetDescription(const std::string& description)
273     {
274         description_ = description;
275     }
276 
GetDescription()277     const std::string& GetDescription() const
278     {
279         return description_;
280     }
281 
SetPixmap(const RefPtr<PixelMap> & pixelMap)282     void SetPixmap(const RefPtr<PixelMap>& pixelMap)
283     {
284         pixelMap_ = pixelMap;
285     }
286 
GetPixmap()287     RefPtr<PixelMap> GetPixmap() const
288     {
289         return pixelMap_;
290     }
291 
292 private:
293     RefPtr<PasteData> pasteData_;
294     double x_ = 0.0;
295     double y_ = 0.0;
296     std::string description_;
297     RefPtr<PixelMap> pixelMap_;
298 };
299 
300 struct FingerInfo {
301     int32_t fingerId_ = -1;
302     // global position at which the touch point contacts the screen.
303     Offset globalLocation_;
304     // Different from global location, The local location refers to the location of the contact point relative to the
305     // current node which has the recognizer.
306     Offset localLocation_;
307 };
308 
309 class ItemDragInfo : public BaseEventInfo {
310     DECLARE_RELATIONSHIP_OF_CLASSES(ItemDragInfo, BaseEventInfo);
311 
312 public:
ItemDragInfo()313     ItemDragInfo() : BaseEventInfo("itemDrag") {}
314     ~ItemDragInfo() override = default;
315 
GetX()316     double GetX() const
317     {
318         return x_;
319     }
320 
GetY()321     double GetY() const
322     {
323         return y_;
324     }
325 
SetX(double x)326     void SetX(double x)
327     {
328         x_ = x;
329     }
330 
SetY(double y)331     void SetY(double y)
332     {
333         y_ = y;
334     }
335 
336 private:
337     double x_ = 0.0;
338     double y_ = 0.0;
339 };
340 
341 class GestureEvent : public BaseEventInfo {
342     DECLARE_RELATIONSHIP_OF_CLASSES(GestureEvent, BaseEventInfo);
343 
344 public:
GestureEvent()345     GestureEvent() : BaseEventInfo("gesture") {}
346     ~GestureEvent() override = default;
347 
SetRepeat(bool repeat)348     void SetRepeat(bool repeat)
349     {
350         repeat_ = repeat;
351     }
352 
GetRepeat()353     bool GetRepeat() const
354     {
355         return repeat_;
356     }
357 
SetOffsetX(double offsetX)358     void SetOffsetX(double offsetX)
359     {
360         offsetX_ = offsetX;
361     }
362 
GetOffsetX()363     double GetOffsetX() const
364     {
365         return offsetX_;
366     }
367 
SetOffsetY(double offsetY)368     void SetOffsetY(double offsetY)
369     {
370         offsetY_ = offsetY;
371     }
372 
GetOffsetY()373     double GetOffsetY() const
374     {
375         return offsetY_;
376     }
377 
SetScale(double scale)378     void SetScale(double scale)
379     {
380         scale_ = scale;
381     }
382 
GetScale()383     double GetScale() const
384     {
385         return scale_;
386     }
387 
SetAngle(double angle)388     void SetAngle(double angle)
389     {
390         angle_ = angle;
391     }
392 
GetAngle()393     double GetAngle() const
394     {
395         return angle_;
396     }
397 
SetGlobalPoint(const Point & globalPoint)398     GestureEvent& SetGlobalPoint(const Point& globalPoint)
399     {
400         globalPoint_ = globalPoint;
401         globalLocation_.SetX(globalPoint.GetX());
402         globalLocation_.SetY(globalPoint.GetY());
403         return *this;
404     }
405 
GetGlobalPoint()406     const Point& GetGlobalPoint() const
407     {
408         return globalPoint_;
409     }
410 
SetScreenLocation(const Offset & screenLocation)411     GestureEvent& SetScreenLocation(const Offset& screenLocation)
412     {
413         screenLocation_ = screenLocation;
414         return *this;
415     }
416 
GetScreenLocation()417     const Offset& GetScreenLocation() const
418     {
419         return screenLocation_;
420     }
421 
SetGlobalLocation(const Offset & globalLocation)422     GestureEvent& SetGlobalLocation(const Offset& globalLocation)
423     {
424         globalLocation_ = globalLocation;
425         globalPoint_.SetX(globalLocation.GetX());
426         globalPoint_.SetY(globalLocation.GetY());
427         return *this;
428     }
SetLocalLocation(const Offset & localLocation)429     GestureEvent& SetLocalLocation(const Offset& localLocation)
430     {
431         localLocation_ = localLocation;
432         return *this;
433     }
434 
GetLocalLocation()435     const Offset& GetLocalLocation() const
436     {
437         return localLocation_;
438     }
GetGlobalLocation()439     const Offset& GetGlobalLocation() const
440     {
441         return globalLocation_;
442     }
443 
GetPinchCenter()444     const Offset& GetPinchCenter() const
445     {
446         return pinchCenter_;
447     }
448 
SetPinchCenter(const Offset & pinchCenter)449     GestureEvent& SetPinchCenter(const Offset& pinchCenter)
450     {
451         pinchCenter_ = pinchCenter;
452         return *this;
453     }
454 
GetFingerList()455     const std::list<FingerInfo>& GetFingerList() const
456     {
457         return fingerList_;
458     }
459 
SetFingerList(const std::list<FingerInfo> & fingerList)460     void SetFingerList(const std::list<FingerInfo>& fingerList)
461     {
462         fingerList_ = fingerList;
463     }
464 
SetSpeed(double speed)465     void SetSpeed(double speed)
466     {
467         speed_ = speed;
468     }
469 
GetSpeed()470     double GetSpeed() const
471     {
472         return speed_;
473     }
474 
SetMainSpeed(double mainSpeed)475     void SetMainSpeed(double mainSpeed)
476     {
477         mainSpeed_ = mainSpeed;
478     }
479 
GetMainSpeed()480     double GetMainSpeed() const
481     {
482         return mainSpeed_;
483     }
484 
SetVelocity(const Velocity & velocity)485     void SetVelocity(const Velocity& velocity)
486     {
487         velocity_ = velocity;
488     }
489 
GetVelocity()490     const Velocity& GetVelocity() const
491     {
492         return velocity_;
493     }
494 
SetMainVelocity(double mainVelocity)495     void SetMainVelocity(double mainVelocity)
496     {
497         mainVelocity_ = mainVelocity;
498     }
499 
GetMainVelocity()500     double GetMainVelocity() const
501     {
502         return mainVelocity_;
503     }
504 
SetPressed(bool pressed)505     void SetPressed(bool pressed)
506     {
507         pressed_ = pressed;
508     }
509 
GetPressed()510     bool GetPressed() const
511     {
512         return pressed_;
513     }
514 
SetDelta(const Offset & delta)515     void SetDelta(const Offset& delta)
516     {
517         delta_ = delta;
518     }
519 
GetDelta()520     const Offset& GetDelta() const
521     {
522         return delta_;
523     }
524 
SetMainDelta(double mainDelta)525     void SetMainDelta(double mainDelta)
526     {
527         mainDelta_ = mainDelta;
528     }
529 
GetMainDelta()530     double GetMainDelta() const
531     {
532         return mainDelta_;
533     }
534 
SetInputEventType(InputEventType type)535     void SetInputEventType(InputEventType type)
536     {
537         inputEventType_ = type;
538     }
539 
GetInputEventType()540     InputEventType GetInputEventType() const
541     {
542         return inputEventType_;
543     }
544 
545 private:
546     bool repeat_ = false;
547     bool pressed_ = false;
548     double offsetX_ = 0.0;
549     double offsetY_ = 0.0;
550     double scale_ = 1.0;
551     double angle_ = 0.0;
552     Velocity velocity_;
553     double mainVelocity_ = 0.0;
554     double speed_ = 0.0;
555     double mainSpeed_ = 0.0;
556     double mainDelta_ = 0.0;
557     Point globalPoint_;
558     // global position at which the touch point contacts the screen.
559     Offset globalLocation_;
560     // Different from global location, The local location refers to the location of the contact point relative to the
561     // current node which has the recognizer.
562     Offset localLocation_;
563     Offset screenLocation_;
564     Offset pinchCenter_;
565     Offset delta_;
566     std::list<FingerInfo> fingerList_;
567     InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN;
568 };
569 
570 using GestureEventFunc = std::function<void(GestureEvent& info)>;
571 using GestureEventNoParameter = std::function<void()>;
572 
573 class ACE_EXPORT Gesture : public virtual AceType {
574     DECLARE_ACE_TYPE(Gesture, AceType);
575 
576 public:
577     Gesture() = default;
Gesture(int32_t fingers)578     explicit Gesture(int32_t fingers) : fingers_(fingers) {}
579     ~Gesture() override = default;
580 
SetOnActionId(const GestureEventFunc & onActionId)581     void SetOnActionId(const GestureEventFunc& onActionId)
582     {
583         onActionId_ = std::make_unique<GestureEventFunc>(onActionId);
584     }
SetOnActionStartId(const GestureEventFunc & onActionStartId)585     void SetOnActionStartId(const GestureEventFunc& onActionStartId)
586     {
587         onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId);
588     }
SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)589     void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId)
590     {
591         onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId);
592     }
SetOnActionEndId(const GestureEventFunc & onActionEndId)593     void SetOnActionEndId(const GestureEventFunc& onActionEndId)
594     {
595         onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId);
596     }
SetOnActionCancelId(const GestureEventNoParameter & onActionCancelId)597     void SetOnActionCancelId(const GestureEventNoParameter& onActionCancelId)
598     {
599         onActionCancelId_ = std::make_unique<GestureEventNoParameter>(onActionCancelId);
600     }
SetPriority(GesturePriority priority)601     void SetPriority(GesturePriority priority)
602     {
603         priority_ = priority;
604     }
SetGestureMask(GestureMask gestureMask)605     void SetGestureMask(GestureMask gestureMask)
606     {
607         gestureMask_ = gestureMask;
608     }
609 
GetPriority()610     GesturePriority GetPriority() const
611     {
612         return priority_;
613     }
614 
GetGestureMask()615     GestureMask GetGestureMask() const
616     {
617         return gestureMask_;
618     }
619 
620     virtual RefPtr<GestureRecognizer> CreateRecognizer(WeakPtr<PipelineBase> context) = 0;
621 
622 protected:
623     int32_t fingers_ = 1;
624     GesturePriority priority_ = GesturePriority::Low;
625     GestureMask gestureMask_ = GestureMask::Normal;
626     std::unique_ptr<GestureEventFunc> onActionId_;
627     std::unique_ptr<GestureEventFunc> onActionStartId_;
628     std::unique_ptr<GestureEventFunc> onActionUpdateId_;
629     std::unique_ptr<GestureEventFunc> onActionEndId_;
630     std::unique_ptr<GestureEventNoParameter> onActionCancelId_;
631 };
632 
633 class ClickInfo : public TouchLocationInfo {
634     DECLARE_RELATIONSHIP_OF_CLASSES(ClickInfo, TouchLocationInfo);
635 
636 public:
ClickInfo(int32_t fingerId)637     explicit ClickInfo(int32_t fingerId) : TouchLocationInfo("onClick", fingerId) {}
638     ~ClickInfo() override = default;
639 };
640 using ClickCallback = std::function<void(const ClickInfo&)>;
641 
642 } // namespace OHOS::Ace
643 
644 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_INFO_H
645