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 ROUND_RECT_H
17 #define ROUND_RECT_H
18
19 #include <vector>
20
21 #include "utils/point.h"
22 #include "utils/rect.h"
23
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class RoundRect {
28 public:
29 enum CornerPos {
30 TOP_LEFT_POS,
31 TOP_RIGHT_POS,
32 BOTTOM_RIGHT_POS,
33 BOTTOM_LEFT_POS,
34 };
35
36 inline RoundRect() noexcept;
~RoundRect()37 inline ~RoundRect() {}
38
39 inline RoundRect(const RoundRect& roundRect) noexcept;
40 inline RoundRect(const Rect& r, scalar xRad, scalar yRad) noexcept;
41 inline RoundRect(const Rect& r, std::vector<Point>& radiusXY) noexcept;
42
43 inline void SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY);
44 inline Point GetCornerRadius(CornerPos pos) const;
45
46 inline void SetRect(const Rect& rect);
47 inline Rect GetRect() const;
48
49 inline void Offset(scalar dx, scalar dy);
50
51 private:
52 Rect rect_;
53 // Four radii are stored: top-left/top-right/bottom-left/bottom-right corner radii.
54 std::vector<Point> radiusXY_;
55 };
56
RoundRect()57 inline RoundRect::RoundRect() noexcept : rect_(), radiusXY_(4, Point(0, 0)) {}
58
RoundRect(const RoundRect & roundRect)59 inline RoundRect::RoundRect(const RoundRect& roundRect) noexcept : RoundRect()
60 {
61 rect_ = roundRect.rect_;
62 for (size_t i = 0; i < radiusXY_.size(); ++i) {
63 radiusXY_[i] = roundRect.radiusXY_[i];
64 }
65 }
66
RoundRect(const Rect & r,scalar xRad,scalar yRad)67 inline RoundRect::RoundRect(const Rect& r, scalar xRad, scalar yRad) noexcept : RoundRect()
68 {
69 rect_ = r;
70 for (size_t i = 0; i < radiusXY_.size(); ++i) {
71 radiusXY_[i].SetX(xRad);
72 radiusXY_[i].SetY(yRad);
73 }
74 }
75
RoundRect(const Rect & r,std::vector<Point> & radiusXY)76 inline RoundRect::RoundRect(const Rect& r, std::vector<Point>& radiusXY) noexcept : RoundRect()
77 {
78 rect_ = r;
79 for (size_t i = 0; i < radiusXY_.size(); ++i) {
80 radiusXY_[i] = radiusXY[i];
81 }
82 }
83
SetCornerRadius(CornerPos pos,scalar radiusX,scalar radiusY)84 inline void RoundRect::SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY)
85 {
86 radiusXY_[pos].SetX(radiusX);
87 radiusXY_[pos].SetY(radiusY);
88 }
89
GetCornerRadius(CornerPos pos)90 inline Point RoundRect::GetCornerRadius(CornerPos pos) const
91 {
92 return radiusXY_[pos];
93 }
94
SetRect(const Rect & rect)95 inline void RoundRect::SetRect(const Rect& rect)
96 {
97 rect_ = rect;
98 }
99
GetRect()100 inline Rect RoundRect::GetRect() const
101 {
102 return rect_;
103 }
104
Offset(scalar dx,scalar dy)105 inline void RoundRect::Offset(scalar dx, scalar dy)
106 {
107 rect_.Offset(dx, dy);
108 }
109 } // namespace Drawing
110 } // namespace Rosen
111 } // namespace OHOS
112 #endif