1 /*
2  * Copyright (c) 2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H
17 
18 #include "core/gestures/gesture_info.h"
19 
20 namespace OHOS::MMI {
21 class PointerEvent;
22 } // namespace OHOS::MMI
23 
24 namespace OHOS::Ace {
25 
26 class GestureEvent : public BaseEventInfo {
27     DECLARE_RELATIONSHIP_OF_CLASSES(GestureEvent, BaseEventInfo);
28 
29 public:
GestureEvent()30     GestureEvent() : BaseEventInfo("gesture") {}
31     ~GestureEvent() override = default;
32 
SetRepeat(bool repeat)33     void SetRepeat(bool repeat)
34     {
35         repeat_ = repeat;
36     }
37 
GetRepeat()38     bool GetRepeat() const
39     {
40         return repeat_;
41     }
42 
SetOffsetX(double offsetX)43     void SetOffsetX(double offsetX)
44     {
45         offsetX_ = offsetX;
46     }
47 
GetOffsetX()48     double GetOffsetX() const
49     {
50         return offsetX_;
51     }
52 
SetOffsetY(double offsetY)53     void SetOffsetY(double offsetY)
54     {
55         offsetY_ = offsetY;
56     }
57 
GetOffsetY()58     double GetOffsetY() const
59     {
60         return offsetY_;
61     }
62 
SetScale(double scale)63     void SetScale(double scale)
64     {
65         scale_ = scale;
66     }
67 
GetScale()68     double GetScale() const
69     {
70         return scale_;
71     }
72 
SetAngle(double angle)73     void SetAngle(double angle)
74     {
75         angle_ = angle;
76     }
77 
GetAngle()78     double GetAngle() const
79     {
80         return angle_;
81     }
82 
SetGlobalPoint(const Point & globalPoint)83     GestureEvent& SetGlobalPoint(const Point& globalPoint)
84     {
85         globalPoint_ = globalPoint;
86         globalLocation_.SetX(globalPoint.GetX());
87         globalLocation_.SetY(globalPoint.GetY());
88         return *this;
89     }
90 
GetGlobalPoint()91     const Point& GetGlobalPoint() const
92     {
93         return globalPoint_;
94     }
95 
SetScreenLocation(const Offset & screenLocation)96     GestureEvent& SetScreenLocation(const Offset& screenLocation)
97     {
98         screenLocation_ = screenLocation;
99         return *this;
100     }
101 
GetScreenLocation()102     const Offset& GetScreenLocation() const
103     {
104         return screenLocation_;
105     }
106 
SetGlobalLocation(const Offset & globalLocation)107     GestureEvent& SetGlobalLocation(const Offset& globalLocation)
108     {
109         globalLocation_ = globalLocation;
110         globalPoint_.SetX(globalLocation.GetX());
111         globalPoint_.SetY(globalLocation.GetY());
112         return *this;
113     }
SetLocalLocation(const Offset & localLocation)114     GestureEvent& SetLocalLocation(const Offset& localLocation)
115     {
116         localLocation_ = localLocation;
117         return *this;
118     }
119 
GetLocalLocation()120     const Offset& GetLocalLocation() const
121     {
122         return localLocation_;
123     }
GetGlobalLocation()124     const Offset& GetGlobalLocation() const
125     {
126         return globalLocation_;
127     }
128 
GetPinchCenter()129     const Offset& GetPinchCenter() const
130     {
131         return pinchCenter_;
132     }
133 
SetPinchCenter(const Offset & pinchCenter)134     GestureEvent& SetPinchCenter(const Offset& pinchCenter)
135     {
136         pinchCenter_ = pinchCenter;
137         return *this;
138     }
139 
GetFingerList()140     const std::list<FingerInfo>& GetFingerList() const
141     {
142         return fingerList_;
143     }
144 
SetFingerList(const std::list<FingerInfo> & fingerList)145     void SetFingerList(const std::list<FingerInfo>& fingerList)
146     {
147         fingerList_ = fingerList;
148     }
149 
SetSpeed(double speed)150     void SetSpeed(double speed)
151     {
152         speed_ = speed;
153     }
154 
GetSpeed()155     double GetSpeed() const
156     {
157         return speed_;
158     }
159 
SetMainSpeed(double mainSpeed)160     void SetMainSpeed(double mainSpeed)
161     {
162         mainSpeed_ = mainSpeed;
163     }
164 
GetMainSpeed()165     double GetMainSpeed() const
166     {
167         return mainSpeed_;
168     }
169 
SetVelocity(const Velocity & velocity)170     void SetVelocity(const Velocity& velocity)
171     {
172         velocity_ = velocity;
173     }
174 
GetVelocity()175     const Velocity& GetVelocity() const
176     {
177         return velocity_;
178     }
179 
SetMainVelocity(double mainVelocity)180     void SetMainVelocity(double mainVelocity)
181     {
182         mainVelocity_ = mainVelocity;
183     }
184 
GetMainVelocity()185     double GetMainVelocity() const
186     {
187         return mainVelocity_;
188     }
189 
SetPressed(bool pressed)190     void SetPressed(bool pressed)
191     {
192         pressed_ = pressed;
193     }
194 
GetPressed()195     bool GetPressed() const
196     {
197         return pressed_;
198     }
199 
SetDelta(const Offset & delta)200     void SetDelta(const Offset& delta)
201     {
202         delta_ = delta;
203     }
204 
GetDelta()205     const Offset& GetDelta() const
206     {
207         return delta_;
208     }
209 
SetMainDelta(double mainDelta)210     void SetMainDelta(double mainDelta)
211     {
212         mainDelta_ = mainDelta;
213     }
214 
GetMainDelta()215     double GetMainDelta() const
216     {
217         return mainDelta_;
218     }
219 
SetInputEventType(InputEventType type)220     void SetInputEventType(InputEventType type)
221     {
222         inputEventType_ = type;
223     }
224 
GetInputEventType()225     InputEventType GetInputEventType() const
226     {
227         return inputEventType_;
228     }
SetPointerId(int32_t pointerId)229     void SetPointerId(int32_t pointerId)
230     {
231         pointerId_ = pointerId;
232     }
233 
GetPointerId()234     int32_t GetPointerId() const
235     {
236         return pointerId_;
237     }
238 
SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)239     void SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
240     {
241         pointerEvent_ = pointerEvent;
242     }
GetPointerEvent()243     const std::shared_ptr<MMI::PointerEvent>& GetPointerEvent() const
244     {
245         return pointerEvent_;
246     }
247 
SetRawGlobalLocation(const Offset & rawGlobalLocation)248     void SetRawGlobalLocation(const Offset& rawGlobalLocation)
249     {
250         rawGlobalLocation_ = rawGlobalLocation;
251     }
252 
GetRawGlobalLocation()253     const Offset& GetRawGlobalLocation() const
254     {
255         return rawGlobalLocation_;
256     }
257 
GetHorizontalAxis()258     float GetHorizontalAxis() const
259     {
260         return horizontalAxis_;
261     }
262 
GetVerticalAxis()263     float GetVerticalAxis() const
264     {
265         return verticalAxis_;
266     }
267 
SetHorizontalAxis(float axis)268     void SetHorizontalAxis(float axis)
269     {
270         horizontalAxis_ = axis;
271     }
272 
SetVerticalAxis(float axis)273     void SetVerticalAxis(float axis)
274     {
275         verticalAxis_ = axis;
276     }
277 
GetInputXDeltaSlope()278     float GetInputXDeltaSlope() const
279     {
280         return inputXDeltaSlope_;
281     }
282 
GetInputYDeltaSlope()283     float GetInputYDeltaSlope() const
284     {
285         return inputYDeltaSlope_;
286     }
287 
IsInterpolated()288     bool IsInterpolated() const
289     {
290         return isInterpolated_;
291     }
292 
SetInputXDeltaSlope(float inputXDeltaSlope)293     void SetInputXDeltaSlope(float inputXDeltaSlope)
294     {
295         inputXDeltaSlope_ = inputXDeltaSlope;
296     }
297 
SetInputYDeltaSlope(float inputYDeltaSlope)298     void SetInputYDeltaSlope(float inputYDeltaSlope)
299     {
300         inputYDeltaSlope_ = inputYDeltaSlope;
301     }
302 
SetIsInterpolated(bool isInterpolated)303     void SetIsInterpolated(bool isInterpolated)
304     {
305         isInterpolated_ = isInterpolated;
306     }
307 #ifdef SECURITY_COMPONENT_ENABLE
SetDisplayX(double displayX)308     void SetDisplayX(double displayX)
309     {
310         displayX_ = displayX;
311     }
312 
GetDisplayX()313     double GetDisplayX() const
314     {
315         return displayX_;
316     }
317 
SetDisplayY(double displayY)318     void SetDisplayY(double displayY)
319     {
320         displayY_ = displayY;
321     }
322 
GetDisplayY()323     double GetDisplayY() const
324     {
325         return displayY_;
326     }
327 
SetSecCompHandleEvent(const std::shared_ptr<JsonValue> & event)328     void SetSecCompHandleEvent(const std::shared_ptr<JsonValue>& event)
329     {
330         secCompHandleEvent_ = event;
331     }
332 
GetSecCompHandleEvent()333     std::shared_ptr<JsonValue> GetSecCompHandleEvent() const
334     {
335         return secCompHandleEvent_;
336     }
337 #endif
338 private:
339     bool repeat_ = false;
340     bool pressed_ = false;
341     double offsetX_ = 0.0;
342     double offsetY_ = 0.0;
343     double scale_ = 1.0;
344     double angle_ = 0.0;
345     Velocity velocity_;
346     double mainVelocity_ = 0.0;
347     double speed_ = 0.0;
348     double mainSpeed_ = 0.0;
349     double mainDelta_ = 0.0;
350     // Will be used in drag.
351     int32_t pointerId_ = 0;
352 #ifdef SECURITY_COMPONENT_ENABLE
353     double displayX_ = 0.0;
354     double displayY_ = 0.0;
355     std::shared_ptr<JsonValue> secCompHandleEvent_;
356 #endif
357     std::shared_ptr<MMI::PointerEvent> pointerEvent_;
358     Point globalPoint_;
359     // global position at which the touch point contacts the screen.
360     Offset globalLocation_;
361     // Different from global location, The local location refers to the location of the contact point relative to the
362     // current node which has the recognizer.
363     Offset localLocation_;
364     // Will be used in drag.
365     Offset screenLocation_;
366     // Raw last touchPoint global location.
367     Offset rawGlobalLocation_;
368     Offset pinchCenter_;
369     Offset delta_;
370     std::list<FingerInfo> fingerList_;
371     InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN;
372     float horizontalAxis_ = 0.0;
373     float verticalAxis_ = 0.0;
374 
375     // Save historical touch point slope.
376     bool isInterpolated_ = false;
377     float inputXDeltaSlope_ = 0.0f;
378     float inputYDeltaSlope_ = 0.0f;
379 };
380 
381 using GestureEventFunc = std::function<void(GestureEvent& info)>;
382 using GestureEventNoParameter = std::function<void()>;
383 
384 class ACE_EXPORT Gesture : public virtual AceType {
385     DECLARE_ACE_TYPE(Gesture, AceType);
386 
387 public:
388     Gesture() = default;
Gesture(int32_t fingers)389     explicit Gesture(int32_t fingers) : fingers_(fingers) {}
390     ~Gesture() override = default;
391 
SetOnActionId(const GestureEventFunc & onActionId)392     void SetOnActionId(const GestureEventFunc& onActionId)
393     {
394         onActionId_ = std::make_unique<GestureEventFunc>(onActionId);
395     }
SetOnActionStartId(const GestureEventFunc & onActionStartId)396     void SetOnActionStartId(const GestureEventFunc& onActionStartId)
397     {
398         onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId);
399     }
SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)400     void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId)
401     {
402         onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId);
403     }
SetOnActionEndId(const GestureEventFunc & onActionEndId)404     void SetOnActionEndId(const GestureEventFunc& onActionEndId)
405     {
406         onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId);
407     }
SetOnActionCancelId(const GestureEventNoParameter & onActionCancelId)408     void SetOnActionCancelId(const GestureEventNoParameter& onActionCancelId)
409     {
410         onActionCancelId_ = std::make_unique<GestureEventNoParameter>(onActionCancelId);
411     }
SetPriority(GesturePriority priority)412     void SetPriority(GesturePriority priority)
413     {
414         priority_ = priority;
415     }
SetGestureMask(GestureMask gestureMask)416     void SetGestureMask(GestureMask gestureMask)
417     {
418         gestureMask_ = gestureMask;
419     }
420 
GetPriority()421     GesturePriority GetPriority() const
422     {
423         return priority_;
424     }
425 
GetGestureMask()426     GestureMask GetGestureMask() const
427     {
428         return gestureMask_;
429     }
430 
431     virtual RefPtr<GestureRecognizer> CreateRecognizer(WeakPtr<PipelineBase> context) = 0;
432 
433 protected:
434     int32_t fingers_ = 1;
435     GesturePriority priority_ = GesturePriority::Low;
436     GestureMask gestureMask_ = GestureMask::Normal;
437     std::unique_ptr<GestureEventFunc> onActionId_;
438     std::unique_ptr<GestureEventFunc> onActionStartId_;
439     std::unique_ptr<GestureEventFunc> onActionUpdateId_;
440     std::unique_ptr<GestureEventFunc> onActionEndId_;
441     std::unique_ptr<GestureEventNoParameter> onActionCancelId_;
442 };
443 
444 } // namespace OHOS::Ace
445 
446 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H