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_COMPONENTS_NG_GESTURES_RECOGNIZERS_PINCH_RECOGNIZER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PINCH_RECOGNIZER_H
18 
19 #include <cmath>
20 #include <functional>
21 #include <map>
22 
23 #include "core/components_ng/gestures/recognizers/multi_fingers_recognizer.h"
24 
25 namespace OHOS::Ace::NG {
26 
27 class PinchRecognizer : public MultiFingersRecognizer {
28     DECLARE_ACE_TYPE(PinchRecognizer, MultiFingersRecognizer);
29 
30 public:
31     PinchRecognizer(int32_t fingers, double distance);
32     ~PinchRecognizer() override = default;
33 
34     void OnAccepted() override;
35     void OnRejected() override;
36 
37     virtual RefPtr<GestureSnapshot> Dump() const override;
38 
39 private:
40     void HandleTouchDownEvent(const TouchEvent& event) override;
41     void HandleTouchUpEvent(const TouchEvent& event) override;
42     void HandleTouchMoveEvent(const TouchEvent& event) override;
43     void HandleTouchCancelEvent(const TouchEvent& event) override;
44     void HandleTouchDownEvent(const AxisEvent& event) override;
45     void HandleTouchUpEvent(const AxisEvent& event) override;
46     void HandleTouchMoveEvent(const AxisEvent& event) override;
47     void HandleTouchCancelEvent(const AxisEvent& event) override;
48 
49     bool ReconcileFrom(const RefPtr<NGGestureRecognizer>& recognizer) override;
50     double ComputeAverageDeviation();
51 
52     void OnResetStatus() override;
53     void SendCallbackMsg(const std::unique_ptr<GestureEventFunc>& callback);
54     GestureJudgeResult TriggerGestureJudgeCallback();
55     Offset ComputePinchCenter();
56 
57     bool IsCtrlBeingPressed(const AxisEvent& event);
58 
59     void OnFlushTouchEventsBegin() override;
60     void OnFlushTouchEventsEnd() override;
61 
62     double distance_ = 0.0;
63     double initialDev_ = 0.0;
64     double currentDev_ = 0.0;
65     double scale_ = 1.0;
66     Offset pinchCenter_;
67     TimeStamp time_;
68     TouchEvent lastTouchEvent_;
69     bool isFlushTouchEventsEnd_ = false;
70     bool isPinchEnd_ = false;
71     // The fingers are lifted completely in the last pinch gesture
72     bool isLastPinchFinished_ = true;
73     AxisEvent lastAxisEvent_;
74 };
75 
76 } // namespace OHOS::Ace::NG
77 
78 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_GESTURES_RECOGNIZERS_PINCH_RECOGNIZER_H
79