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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_PATTERN_LOCK_RENDER_PATTERN_LOCK_H
16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_PATTERN_LOCK_RENDER_PATTERN_LOCK_H
17 #include "base/utils/system_properties.h"
18 #include "core/components/common/properties/color.h"
19 #include "core/components_v2/pattern_lock/pattern_lock_component.h"
20 #include "core/gestures/raw_recognizer.h"
21 #include "core/pipeline/base/render_node.h"
22 
23 namespace OHOS::Ace::V2 {
24 using CallbackForJS = std::function<void(const std::string&)>;
25 using PatternCompleteFunc = std::function<void(std::shared_ptr<PatternCompleteEvent>&)>;
26 constexpr int16_t COL_COUNT = 3;
27 constexpr double SCALE_SELECTED_CIRCLE_RADIUS = 26.00 / 14.00;
28 constexpr double SCALE_ACTIVE_CIRCLE_RADIUS = 16.00 / 14.00;
29 constexpr double SCALE_DECREASE = 26.00 / 16.00;
30 constexpr double GRADUAL_CHANGE_POINT = 0.5;
31 constexpr int32_t DOWN_DURATION = 150;
32 constexpr Dimension DEFAULT_LINE_WIDTH = 34.0_vp;
33 constexpr int32_t MAX_ALPHA = 255;
34 constexpr int32_t RADIUS_TO_DIAMETER = 2;
35 class RenderPatternLock : public RenderNode {
36     DECLARE_ACE_TYPE(RenderPatternLock, RenderNode);
37 
38 public:
39     static RefPtr<RenderNode> Create();
40     void Update(const RefPtr<Component>& component) override;
41     void PerformLayout() override;
GetCircleRadius()42     const Dimension& GetCircleRadius() const
43     {
44         return circleRadius_;
45     }
GetSideLength()46     const Dimension& GetSideLength() const
47     {
48         return sideLength_;
49     }
GetStrokeWidth()50     const Dimension& GetStrokeWidth() const
51     {
52         return strokeWidth_;
53     }
GetRegularColor()54     const Color& GetRegularColor() const
55     {
56         return regularColor_;
57     }
GetSelectedColor()58     const Color& GetSelectedColor() const
59     {
60         return selectedColor_;
61     }
GetActiveColor()62     const Color& GetActiveColor() const
63     {
64         return activeColor_;
65     }
GetPathColor()66     const Color& GetPathColor() const
67     {
68         return pathColor_;
69     }
GetAutoReset()70     bool GetAutoReset() const
71     {
72         return autoReset_;
73     }
GetPatternLockController()74     RefPtr<V2::PatternLockController> GetPatternLockController() const
75     {
76         return patternLockController_;
77     }
78 
79 protected:
80     bool AddChoosePoint(Offset offset, int16_t x, int16_t y);
81     void AddPassPoint(Offset offset, int16_t x, int16_t y);
82     bool CheckChoosePoint(int16_t x, int16_t y) const;
83     bool CheckChoosePointIsLastIndex(int16_t x, int16_t y, int16_t index) const;
84     Offset GetCircleCenterByXY(const Offset& offset, int16_t x, int16_t y);
85     void HandleCellTouchDown(const Offset& offset);
86     void HandleCellTouchMove(const Offset& offset);
87     void HandleCellTouchUp();
88     void HandleCellTouchCancel();
89     void HandleReset();
90     bool CheckAutoReset() const;
91     void UpdateAttr(const RefPtr<Component>& component);
92 
93 protected:
94     class PatternLockCell {
95     public:
PatternLockCell(int16_t column,int16_t row)96         PatternLockCell(int16_t column, int16_t row)
97         {
98             column_ = column;
99             row_ = row;
100             code_ = COL_COUNT * (row - 1) + (column - 1);
101         };
102         ~PatternLockCell() = default;
GetColumn()103         int16_t GetColumn() const
104         {
105             return column_;
106         }
GetRow()107         int16_t GetRow() const
108         {
109             return row_;
110         }
GetCode()111         int16_t GetCode() const
112         {
113             return code_;
114         }
115     private:
116         int16_t column_;
117         int16_t row_;
118         int16_t code_;
119     };
120     RenderPatternLock();
121     void OnTouchTestHit(
122         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
123     Dimension circleRadius_;
124     Dimension circleRadiusAnimatorToIncrease_;
125     Dimension circleRadiusAnimatorToDecrease_;
126     Dimension sideLength_;
127     Color regularColor_;
128     Color selectedColor_;
129     Color activeColor_;
130     Color pathColor_;
131     Dimension strokeWidth_;
132     PatternCompleteFunc callbackForJS_;
133     RefPtr<RawRecognizer> touchRecognizer_;
134     bool isMoveEventValid_ = false;
135     Offset cellCenter_ = Offset::Zero();
136     std::vector<PatternLockCell> choosePoint_;
137     RefPtr<Animator> animator_;
138     bool autoReset_ = true;
139     int16_t passPointCount_ = 0;
140     RefPtr<V2::PatternLockController> patternLockController_;
141 };
142 } // namespace OHOS::Ace::V2
143 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_PATTERN_LOCK_RENDER_PATTERN_LOCK_H