1 /*
2  * Copyright (c) 2021 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_TRIANGLE_RENDER_TRIANGLE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRIANGLE_RENDER_TRIANGLE_H
18 
19 #include "base/geometry/arc.h"
20 #include "core/gestures/click_recognizer.h"
21 #include "core/pipeline/base/render_node.h"
22 
23 namespace OHOS::Ace {
24 
25 class TriangleRotationCalculator {
26 public:
SetPointOne(double x,double y)27     void SetPointOne(double x, double y)
28     {
29         x1_ = x;
30         y1_ = y;
31     }
32 
SetPointTwo(double x,double y)33     void SetPointTwo(double x, double y)
34     {
35         x2_ = x;
36         y2_ = y;
37     }
38 
SetPointThree(double x,double y)39     void SetPointThree(double x, double y)
40     {
41         x3_ = x;
42         y3_ = y;
43     }
44 
SetAngle(double value)45     void SetAngle(double value)
46     {
47         angle_ = value;
48     }
49 
SetRadius(double value)50     void SetRadius(double value)
51     {
52         radius_ = value;
53     }
54 
55     bool Calculate(double xOffset, double yOffset);
56 
57     bool IsDown() const;
58 
GetOutArc1()59     const Arc& GetOutArc1()
60     {
61         return outArc1_;
62     }
63 
GetOutArc2()64     const Arc& GetOutArc2()
65     {
66         return outArc2_;
67     }
68 
GetOutArc3()69     const Arc& GetOutArc3()
70     {
71         return outArc3_;
72     }
73 
74 private:
75     // point one
76     double x1_ = 0.0;
77     double y1_ = 0.0;
78     // point two
79     double x2_ = 0.0;
80     double y2_ = 0.0;
81     // point three
82     double x3_ = 0.0;
83     double y3_ = 0.0;
84     // rotate angle
85     double angle_ = 0.0;
86     // radius value
87     double radius_ = 0.0;
88     // out arc 1
89     Arc outArc1_;
90     // out arc 2
91     Arc outArc2_;
92     // out arc 3
93     Arc outArc3_;
94 };
95 
96 class RenderTriangle : public RenderNode {
97     DECLARE_ACE_TYPE(RenderTriangle, RenderNode);
98 
99 public:
100     static RefPtr<RenderNode> Create();
101 
102     void Update(const RefPtr<Component>& component) override;
103     void PerformLayout() override;
104 
GetOnClick()105     const std::function<void(bool)>& GetOnClick() const
106     {
107         return onClick_;
108     }
SetOnClick(const std::function<void (bool)> & value)109     void SetOnClick(const std::function<void(bool)>& value)
110     {
111         onClick_ = value;
112     }
113 
UpdateAngle(double value)114     void UpdateAngle(double value)
115     {
116         data_.SetAngle(value);
117         MarkNeedRender();
118     }
119 
120     void OnTouchTestHit(const Offset&, const TouchRestrict&, TouchTestResult& result) override;
121 
122 protected:
123     void CalculateTrianglePoints();
124     void HandleClick();
125 
126     Dimension width_;
127     Dimension height_;
128     Dimension padding_;
129     Color color_;
130 
131     TriangleRotationCalculator data_;
132 
133 private:
134     RefPtr<ClickRecognizer> click_;
135     std::function<void(bool)> onClick_;
136 };
137 
138 } // namespace OHOS::Ace
139 
140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TRIANGLE_RENDER_TRIANGLE_H
141