• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BASE_PROPERTIES_SHADOW_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_SHADOW_H
18 
19 #include "base/geometry/offset.h"
20 #include "core/components/common/properties/color.h"
21 
22 namespace OHOS::Ace {
23 
24 constexpr float LIGHT_HEIGHT = 600.0f; // System recommended value.
25 constexpr float LIGHT_RADIUS = 800.0f; // System recommended value.
26 constexpr float LIGHT_POSITION_X = 540.0f; // System recommended value.
27 constexpr float LIGHT_POSITION_Y = 0.0f; // System recommended value.
28 
29 enum class ShadowStyle {
30     OuterDefaultXS,
31     OuterDefaultSM,
32     OuterDefaultMD,
33     OuterDefaultLG,
34     OuterFloatingSM,
35     OuterFloatingMD,
36     None,
37 };
38 
39 enum class ShadowType {
40     COLOR,
41     BLUR,
42 };
43 
44 enum class ShadowColorStrategy : char {
45     NONE,
46     AVERAGE,
47     PRIMARY
48 };
49 // A style class indicates the way to render shadow effect
50 class Shadow final {
51 public:
52     static Shadow Blend(const Shadow& to, const Shadow& from, float progress);
53 
54     Shadow() = default;
55     ~Shadow() = default;
56 
57     // create shadow for hardware rending.
Shadow(float elevation,Offset offset,Color spotColor,ShadowStyle style)58     Shadow(float elevation, Offset offset, Color spotColor, ShadowStyle style)
59         : offset_(offset), color_(spotColor), style_(style)
60     {
61         SetElevation(elevation);
62     };
63 
64     // create shadow for software rending.
Shadow(double blurRadius,double spreadRadius,Offset offset,Color spotColor)65     Shadow(double blurRadius, double spreadRadius, Offset offset, Color spotColor)
66         : spreadRadius_(spreadRadius), offset_(offset), color_(spotColor)
67     {
68         SetBlurRadius(blurRadius);
69     };
70 
71     static Shadow CreateShadow(ShadowStyle style);
72 
73     bool operator==(const Shadow& rhs) const
74     {
75         return color_ == rhs.color_ && NearEqual(blurRadius_, rhs.blurRadius_) && offset_ == rhs.offset_ &&
76                NearEqual(spreadRadius_, rhs.spreadRadius_) && NearEqual(elevation_, rhs.elevation_) &&
77                isFilled_ == rhs.isFilled_ && colorStrategy_ == rhs.colorStrategy_ && type_ == rhs.type_;
78     }
79 
80     bool operator!=(const Shadow& rhs) const
81     {
82         return !(rhs == *this);
83     }
84 
SetColor(const Color & newColor)85     void SetColor(const Color& newColor)
86     {
87         color_ = newColor;
88     }
89 
GetColor()90     const Color& GetColor() const
91     {
92         return color_;
93     }
94 
SetBlurRadius(double blurRadius)95     void SetBlurRadius(double blurRadius)
96     {
97         if (blurRadius >= 0.0) {
98             blurRadius_ = blurRadius;
99             isHardwareAcceleration_ = false;
100             return;
101         }
102         blurRadius_ = 0.0;
103     }
104 
GetBlurRadius()105     double GetBlurRadius() const
106     {
107         return blurRadius_;
108     }
109 
SetOffset(const Offset & offset)110     void SetOffset(const Offset& offset)
111     {
112         offset_ = offset;
113     }
114 
GetOffset()115     const Offset& GetOffset() const
116     {
117         return offset_;
118     }
119 
SetOffsetX(double x)120     void SetOffsetX(double x)
121     {
122         offset_.SetX(x);
123     }
124 
SetOffsetY(double y)125     void SetOffsetY(double y)
126     {
127         offset_.SetY(y);
128     }
129 
SetSpreadRadius(double spreadRadius)130     void SetSpreadRadius(double spreadRadius)
131     {
132         spreadRadius_ = spreadRadius;
133         isHardwareAcceleration_ = false;
134     }
135 
GetSpreadRadius()136     double GetSpreadRadius() const
137     {
138         return spreadRadius_;
139     }
140 
SetElevation(float elevation)141     void SetElevation(float elevation)
142     {
143         if (elevation >= 0.0f && elevation < LIGHT_HEIGHT) {
144             elevation_ = elevation;
145             isHardwareAcceleration_ = true;
146             return;
147         }
148         elevation_ = 0.0f;
149     }
150 
GetElevation()151     float GetElevation() const
152     {
153         return elevation_;
154     }
155 
SetHardwareAcceleration(bool acceleration)156     void SetHardwareAcceleration(bool acceleration)
157     {
158         isHardwareAcceleration_ = acceleration;
159     }
160 
SetIsFilled(bool isFilled)161     void SetIsFilled(bool isFilled)
162     {
163         isFilled_ = isFilled;
164     }
165 
GetHardwareAcceleration()166     bool GetHardwareAcceleration() const
167     {
168         return isHardwareAcceleration_;
169     }
170 
SetLightHeight(float lightHeight)171     void SetLightHeight(float lightHeight)
172     {
173         if (lightHeight > 0.0f) {
174             lightHeight_ = lightHeight;
175         }
176     }
177 
GetLightHeight()178     float GetLightHeight() const
179     {
180         return lightHeight_;
181     }
182 
SetLightRadius(float lightRadius)183     void SetLightRadius(float lightRadius)
184     {
185         if (lightRadius > 0.0f) {
186             lightRadius_ = lightRadius;
187         }
188     }
189 
GetLightRadius()190     float GetLightRadius() const
191     {
192         return lightRadius_;
193     }
194 
GetStyle()195     ShadowStyle GetStyle() const
196     {
197         return style_;
198     }
199 
SetShadowType(ShadowType type)200     void SetShadowType(ShadowType type)
201     {
202         type_ = type;
203     }
204 
GetShadowType()205     ShadowType GetShadowType() const
206     {
207         return type_;
208     }
209 
SetShadowColorStrategy(ShadowColorStrategy colorStrategy)210     void SetShadowColorStrategy(ShadowColorStrategy colorStrategy)
211     {
212         colorStrategy_ = colorStrategy;
213     }
214 
GetShadowColorStrategy()215     ShadowColorStrategy GetShadowColorStrategy() const
216     {
217         return colorStrategy_;
218     }
219 
GetIsFilled()220     bool GetIsFilled() const
221     {
222         return isFilled_;
223     }
224 
IsValid()225     bool IsValid() const
226     {
227         if (isHardwareAcceleration_) {
228             return elevation_ > 0.0f && elevation_ < LIGHT_HEIGHT;
229         }
230         return blurRadius_ > 0.0 || spreadRadius_ > 0.0 || offset_ != Offset::Zero();
231     }
232 
233 private:
234     float lightHeight_ = LIGHT_HEIGHT;
235     float lightRadius_ = LIGHT_RADIUS;
236     float elevation_ = 0.0f; // Rosen always needs a non-zero elevation.
237     double blurRadius_ = 0.0;
238     double spreadRadius_ = 0.0;
239     Offset offset_;
240     Color color_ = Color::BLACK;
241     bool isHardwareAcceleration_ = false;
242     bool isFilled_ = false;
243     ShadowStyle style_ = ShadowStyle::None;
244     ShadowType type_ = ShadowType::COLOR;
245     ShadowColorStrategy colorStrategy_ = ShadowColorStrategy::NONE;
246 };
247 
248 } // namespace OHOS::Ace
249 
250 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_SHADOW_H
251