• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "core/components/common/layout/constants.h"
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 
SetGlobalDisplayLocation(const Offset & globalDisplayLocation)129     GestureEvent& SetGlobalDisplayLocation(const Offset& globalDisplayLocation)
130     {
131         globalDisplayLocation_ = globalDisplayLocation;
132         return *this;
133     }
134 
GetGlobalDisplayLocation()135     const Offset& GetGlobalDisplayLocation() const
136     {
137         return globalDisplayLocation_;
138     }
139 
GetPinchCenter()140     const Offset& GetPinchCenter() const
141     {
142         return pinchCenter_;
143     }
144 
SetPinchCenter(const Offset & pinchCenter)145     GestureEvent& SetPinchCenter(const Offset& pinchCenter)
146     {
147         pinchCenter_ = pinchCenter;
148         return *this;
149     }
150 
GetFingerList()151     const std::list<FingerInfo>& GetFingerList() const
152     {
153         return fingerList_;
154     }
155 
SetFingerList(const std::list<FingerInfo> & fingerList)156     void SetFingerList(const std::list<FingerInfo>& fingerList)
157     {
158         fingerList_ = fingerList;
159     }
160 
SetSpeed(double speed)161     void SetSpeed(double speed)
162     {
163         speed_ = speed;
164     }
165 
GetSpeed()166     double GetSpeed() const
167     {
168         return speed_;
169     }
170 
SetMainSpeed(double mainSpeed)171     void SetMainSpeed(double mainSpeed)
172     {
173         mainSpeed_ = mainSpeed;
174     }
175 
GetMainSpeed()176     double GetMainSpeed() const
177     {
178         return mainSpeed_;
179     }
180 
SetVelocity(const Velocity & velocity)181     void SetVelocity(const Velocity& velocity)
182     {
183         velocity_ = velocity;
184     }
185 
GetVelocity()186     const Velocity& GetVelocity() const
187     {
188         return velocity_;
189     }
190 
SetMainVelocity(double mainVelocity)191     void SetMainVelocity(double mainVelocity)
192     {
193         mainVelocity_ = mainVelocity;
194     }
195 
GetMainVelocity()196     double GetMainVelocity() const
197     {
198         return mainVelocity_;
199     }
200 
SetPressed(bool pressed)201     void SetPressed(bool pressed)
202     {
203         pressed_ = pressed;
204     }
205 
GetPressed()206     bool GetPressed() const
207     {
208         return pressed_;
209     }
210 
SetDelta(const Offset & delta)211     void SetDelta(const Offset& delta)
212     {
213         delta_ = delta;
214     }
215 
GetDelta()216     const Offset& GetDelta() const
217     {
218         return delta_;
219     }
220 
SetMainDelta(double mainDelta)221     void SetMainDelta(double mainDelta)
222     {
223         mainDelta_ = mainDelta;
224     }
225 
GetMainDelta()226     double GetMainDelta() const
227     {
228         return mainDelta_;
229     }
230 
SetInputEventType(InputEventType type)231     void SetInputEventType(InputEventType type)
232     {
233         inputEventType_ = type;
234     }
235 
GetInputEventType()236     InputEventType GetInputEventType() const
237     {
238         return inputEventType_;
239     }
SetPointerId(int32_t pointerId)240     void SetPointerId(int32_t pointerId)
241     {
242         pointerId_ = pointerId;
243     }
244 
GetPointerId()245     int32_t GetPointerId() const
246     {
247         return pointerId_;
248     }
249 
SetPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)250     void SetPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
251     {
252         pointerEvent_ = pointerEvent;
253     }
GetPointerEvent()254     const std::shared_ptr<MMI::PointerEvent>& GetPointerEvent() const
255     {
256         return pointerEvent_;
257     }
258 
SetClickPointerEvent(const std::shared_ptr<MMI::PointerEvent> & clickPointerEvent)259     void SetClickPointerEvent(const std::shared_ptr<MMI::PointerEvent>& clickPointerEvent)
260     {
261         clickPointerEvent_ = clickPointerEvent;
262     }
GetClickPointerEvent()263     const std::shared_ptr<MMI::PointerEvent>& GetClickPointerEvent() const
264     {
265         return clickPointerEvent_;
266     }
267 
SetRawGlobalLocation(const Offset & rawGlobalLocation)268     void SetRawGlobalLocation(const Offset& rawGlobalLocation)
269     {
270         rawGlobalLocation_ = rawGlobalLocation;
271     }
272 
GetRawGlobalLocation()273     const Offset& GetRawGlobalLocation() const
274     {
275         return rawGlobalLocation_;
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 
SetPointerEventId(int32_t pointerEventId)308     void SetPointerEventId(int32_t pointerEventId)
309     {
310         pointerEventId_ = pointerEventId;
311     }
312 
GetPointerEventId()313     int32_t GetPointerEventId() const
314     {
315         return pointerEventId_;
316     }
317 
SetGestureTypeName(GestureTypeName gestureType)318     void SetGestureTypeName(GestureTypeName gestureType)
319     {
320         gestureType_ = gestureType;
321     }
322 
GetGestureTypeName()323     GestureTypeName GetGestureTypeName() const
324     {
325         return gestureType_;
326     }
327 
SetLastAction(int32_t action)328     void SetLastAction(int32_t action)
329     {
330         lastAction_.emplace(action);
331     }
332 
GetLastAction()333     std::optional<int32_t> GetLastAction() const
334     {
335         return lastAction_;
336     }
337 
SetPassThrough(bool passThrough)338     void SetPassThrough(bool passThrough)
339     {
340         passThrough_ = passThrough;
341     }
342 
GetPassThrough()343     bool GetPassThrough() const
344     {
345         return passThrough_;
346     }
347 #ifdef SECURITY_COMPONENT_ENABLE
SetDisplayX(double displayX)348     void SetDisplayX(double displayX)
349     {
350         displayX_ = displayX;
351     }
352 
GetDisplayX()353     double GetDisplayX() const
354     {
355         return displayX_;
356     }
357 
SetDisplayY(double displayY)358     void SetDisplayY(double displayY)
359     {
360         displayY_ = displayY;
361     }
362 
GetDisplayY()363     double GetDisplayY() const
364     {
365         return displayY_;
366     }
367 
SetSecCompHandleEvent(const std::shared_ptr<JsonValue> & event)368     void SetSecCompHandleEvent(const std::shared_ptr<JsonValue>& event)
369     {
370         secCompHandleEvent_ = event;
371     }
372 
GetSecCompHandleEvent()373     std::shared_ptr<JsonValue> GetSecCompHandleEvent() const
374     {
375         return secCompHandleEvent_;
376     }
377 #endif
378 private:
379     bool repeat_ = false;
380     bool pressed_ = false;
381     double offsetX_ = 0.0;
382     double offsetY_ = 0.0;
383     double scale_ = 1.0;
384     double angle_ = 0.0;
385     Velocity velocity_;
386     double mainVelocity_ = 0.0;
387     double speed_ = 0.0;
388     double mainSpeed_ = 0.0;
389     double mainDelta_ = 0.0;
390     // Will be used in drag.
391     int32_t pointerId_ = 0;
392     int32_t pointerEventId_ = 0;
393 #ifdef SECURITY_COMPONENT_ENABLE
394     double displayX_ = 0.0;
395     double displayY_ = 0.0;
396     std::shared_ptr<JsonValue> secCompHandleEvent_;
397 #endif
398     std::shared_ptr<MMI::PointerEvent> pointerEvent_;
399     std::shared_ptr<MMI::PointerEvent> clickPointerEvent_;
400     Point globalPoint_;
401     // global position at which the touch point contacts the screen.
402     Offset globalLocation_;
403     // Different from global location, The local location refers to the location of the contact point relative to the
404     // current node which has the recognizer.
405     Offset localLocation_;
406     // Will be used in drag.
407     Offset screenLocation_;
408     // The location where the touch point touches the screen when there are multiple screens.
409     Offset globalDisplayLocation_;
410     // Raw last touchPoint global location.
411     Offset rawGlobalLocation_;
412     Offset pinchCenter_;
413     Offset delta_;
414     std::list<FingerInfo> fingerList_;
415     InputEventType inputEventType_ = InputEventType::TOUCH_SCREEN;
416     // Save historical touch point slope.
417     bool isInterpolated_ = false;
418     float inputXDeltaSlope_ = 0.0f;
419     float inputYDeltaSlope_ = 0.0f;
420     GestureTypeName gestureType_ = GestureTypeName::UNKNOWN;
421     std::optional<int32_t> lastAction_;
422     bool passThrough_ = false;
423 };
424 
425 using GestureEventFunc = std::function<void(GestureEvent& info)>;
426 using GestureEventNoParameter = std::function<void()>;
427 
428 class ACE_EXPORT Gesture : public virtual AceType {
429     DECLARE_ACE_TYPE(Gesture, AceType);
430 
431 public:
432     Gesture() = default;
Gesture(int32_t fingers)433     explicit Gesture(int32_t fingers) : fingers_(fingers) {}
434     ~Gesture() override = default;
435 
SetOnActionId(const GestureEventFunc & onActionId)436     void SetOnActionId(const GestureEventFunc& onActionId)
437     {
438         onActionId_ = std::make_unique<GestureEventFunc>(onActionId);
439     }
SetOnActionStartId(const GestureEventFunc & onActionStartId)440     void SetOnActionStartId(const GestureEventFunc& onActionStartId)
441     {
442         onActionStartId_ = std::make_unique<GestureEventFunc>(onActionStartId);
443     }
SetOnActionUpdateId(const GestureEventFunc & onActionUpdateId)444     void SetOnActionUpdateId(const GestureEventFunc& onActionUpdateId)
445     {
446         onActionUpdateId_ = std::make_unique<GestureEventFunc>(onActionUpdateId);
447     }
SetOnActionEndId(const GestureEventFunc & onActionEndId)448     void SetOnActionEndId(const GestureEventFunc& onActionEndId)
449     {
450         onActionEndId_ = std::make_unique<GestureEventFunc>(onActionEndId);
451     }
SetOnActionCancelId(const GestureEventFunc & onActionCancelId)452     void SetOnActionCancelId(const GestureEventFunc& onActionCancelId)
453     {
454         onActionCancelId_ = std::make_unique<GestureEventFunc>(onActionCancelId);
455     }
SetPriority(GesturePriority priority)456     void SetPriority(GesturePriority priority)
457     {
458         priority_ = priority;
459     }
SetGestureMask(GestureMask gestureMask)460     void SetGestureMask(GestureMask gestureMask)
461     {
462         gestureMask_ = gestureMask;
463     }
464 
GetPriority()465     GesturePriority GetPriority() const
466     {
467         return priority_;
468     }
469 
GetGestureMask()470     GestureMask GetGestureMask() const
471     {
472         return gestureMask_;
473     }
474 
475     virtual RefPtr<GestureRecognizer> CreateRecognizer(WeakPtr<PipelineBase> context) = 0;
476 
477 protected:
478     int32_t fingers_ = 1;
479     GesturePriority priority_ = GesturePriority::Low;
480     GestureMask gestureMask_ = GestureMask::Normal;
481     std::unique_ptr<GestureEventFunc> onActionId_;
482     std::unique_ptr<GestureEventFunc> onActionStartId_;
483     std::unique_ptr<GestureEventFunc> onActionUpdateId_;
484     std::unique_ptr<GestureEventFunc> onActionEndId_;
485     std::unique_ptr<GestureEventFunc> onActionCancelId_;
486 };
487 
488 } // namespace OHOS::Ace
489 
490 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_EVENT_H