1 /*
2  * Copyright (c) 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_COMPONENTS_NG_EVENT_PAN_EVENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_PAN_EVENT_H
18 
19 #include <list>
20 #include <utility>
21 
22 #include "base/memory/referenced.h"
23 #include "core/components_ng/event/gesture_event_actuator.h"
24 #include "core/components_ng/gestures/gesture_info.h"
25 #include "core/components_ng/gestures/recognizers/pan_recognizer.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class GestureEventHub;
30 class PanRecognizer;
31 
32 class PanEvent : public virtual AceType {
DECLARE_ACE_TYPE(PanEvent,AceType)33     DECLARE_ACE_TYPE(PanEvent, AceType)
34 public:
35     PanEvent(GestureEventFunc&& actionStart, GestureEventFunc&& actionUpdate, GestureEventFunc&& actionEnd,
36         GestureEventNoParameter&& actionCancel)
37         : actionStart_(std::move(actionStart)), actionUpdate_(std::move(actionUpdate)),
38           actionEnd_(std::move(actionEnd)), actionCancel_(std::move(actionCancel))
39     {}
40     ~PanEvent() override = default;
41 
GetActionStartEventFunc()42     const GestureEventFunc& GetActionStartEventFunc() const
43     {
44         return actionStart_;
45     }
46 
GetActionUpdateEventFunc()47     const GestureEventFunc& GetActionUpdateEventFunc() const
48     {
49         return actionUpdate_;
50     }
51 
GetActionEndEventFunc()52     const GestureEventFunc& GetActionEndEventFunc() const
53     {
54         return actionEnd_;
55     }
56 
GetActionCancelEventFunc()57     const GestureEventNoParameter& GetActionCancelEventFunc() const
58     {
59         return actionCancel_;
60     }
61 
62 private:
63     GestureEventFunc actionStart_;
64     GestureEventFunc actionUpdate_;
65     GestureEventFunc actionEnd_;
66     GestureEventNoParameter actionCancel_;
67 };
68 
69 class ACE_FORCE_EXPORT PanEventActuator : public GestureEventActuator {
70     DECLARE_ACE_TYPE(PanEventActuator, GestureEventActuator)
71 public:
72     PanEventActuator(const WeakPtr<GestureEventHub>& gestureEventHub, PanDirection direction, int32_t fingers,
73         float distance, bool isOverrideDistance = false);
74     ~PanEventActuator() override = default;
75 
ReplacePanEvent(const RefPtr<PanEvent> & panEvent)76     void ReplacePanEvent(const RefPtr<PanEvent>& panEvent)
77     {
78         if (userCallback_) {
79             userCallback_.Reset();
80         }
81         userCallback_ = panEvent;
82     }
83 
AddPanEvent(const RefPtr<PanEvent> & panEvent)84     void AddPanEvent(const RefPtr<PanEvent>& panEvent)
85     {
86         if (panEvents_.empty()) {
87             panEvents_.emplace_back(panEvent);
88             return;
89         }
90         if (std::find(panEvents_.begin(), panEvents_.end(), panEvent) == panEvents_.end()) {
91             panEvents_.emplace_back(panEvent);
92         }
93     }
94 
RemovePanEvent(const RefPtr<PanEvent> & panEvent)95     void RemovePanEvent(const RefPtr<PanEvent>& panEvent)
96     {
97         panEvents_.remove(panEvent);
98     }
99 
100     void SetPanEventType(GestureTypeName typeName);
101 
102     void OnCollectTouchTarget(const OffsetF& coordinateOffset, const TouchRestrict& touchRestrict,
103         const GetEventTargetImpl& getEventTargetImpl, TouchTestResult& result,
104         ResponseLinkResult& responseLinkResult) override;
105 
GetDirection()106     PanDirection GetDirection() const
107     {
108         return direction_;
109     }
110 
SetIsAllowMouse(bool isAllowMouse)111     void SetIsAllowMouse(bool isAllowMouse) const
112     {
113         CHECK_NULL_VOID(panRecognizer_);
114         panRecognizer_->SetIsAllowMouse(isAllowMouse);
115     }
116 
IsPanEventEmpty()117     bool IsPanEventEmpty() const
118     {
119         return panEvents_.empty();
120     }
121 
122 private:
123     WeakPtr<GestureEventHub> gestureEventHub_;
124     std::list<RefPtr<PanEvent>> panEvents_;
125     RefPtr<PanEvent> userCallback_;
126     RefPtr<PanRecognizer> panRecognizer_;
127 
128     PanDirection direction_;
129     int32_t fingers_ = 1;
130     float distance_ = 0.0;
131 };
132 
133 } // namespace OHOS::Ace::NG
134 
135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_EVENT_PAN_EVENT_H
136