• 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 // A style class indicates the way to render shadow effect
30 class Shadow final {
31 public:
32     static Shadow Blend(const Shadow& to, const Shadow& from, float progress);
33 
34     Shadow() = default;
35     ~Shadow() = default;
36 
37     // create shadow for hardware rending.
Shadow(float elevation,Offset offset,Color spotColor)38     Shadow(float elevation, Offset offset, Color spotColor) : offset_(offset), color_(spotColor)
39     {
40         SetElevation(elevation);
41     };
42 
43     // create shadow for software rending.
Shadow(double blurRadius,double spreadRadius,Offset offset,Color spotColor)44     Shadow(double blurRadius, double spreadRadius, Offset offset, Color spotColor)
45         : spreadRadius_(spreadRadius), offset_(offset), color_(spotColor)
46     {
47         SetBlurRadius(blurRadius);
48     };
49 
50     bool operator==(const Shadow& rhs) const
51     {
52         return color_ == rhs.color_ && NearEqual(blurRadius_, rhs.blurRadius_) && offset_ == rhs.offset_ &&
53                NearEqual(spreadRadius_, rhs.spreadRadius_) && NearEqual(elevation_, rhs.elevation_);
54     }
55 
56     bool operator!=(const Shadow& rhs) const
57     {
58         return !(rhs == *this);
59     }
60 
SetColor(const Color & newColor)61     void SetColor(const Color& newColor)
62     {
63         color_ = newColor;
64     }
65 
GetColor()66     const Color& GetColor() const
67     {
68         return color_;
69     }
70 
SetBlurRadius(double blurRadius)71     void SetBlurRadius(double blurRadius)
72     {
73         if (blurRadius >= 0.0) {
74             blurRadius_ = blurRadius;
75             isHardwareAcceleration_ = false;
76             return;
77         }
78         blurRadius_ = 0.0;
79     }
80 
GetBlurRadius()81     double GetBlurRadius() const
82     {
83         return blurRadius_;
84     }
85 
SetOffset(const Offset & offset)86     void SetOffset(const Offset& offset)
87     {
88         offset_ = offset;
89     }
90 
GetOffset()91     const Offset& GetOffset() const
92     {
93         return offset_;
94     }
95 
SetOffsetX(double x)96     void SetOffsetX(double x)
97     {
98         offset_.SetX(x);
99     }
100 
SetOffsetY(double y)101     void SetOffsetY(double y)
102     {
103         offset_.SetY(y);
104     }
105 
SetSpreadRadius(double spreadRadius)106     void SetSpreadRadius(double spreadRadius)
107     {
108         spreadRadius_ = spreadRadius;
109         isHardwareAcceleration_ = false;
110     }
111 
GetSpreadRadius()112     double GetSpreadRadius() const
113     {
114         return spreadRadius_;
115     }
116 
SetElevation(float elevation)117     void SetElevation(float elevation)
118     {
119         if (elevation >= 0.0f && elevation < LIGHT_HEIGHT) {
120             elevation_ = elevation;
121             isHardwareAcceleration_ = true;
122             return;
123         }
124         elevation_ = 0.0f;
125     }
126 
GetElevation()127     float GetElevation() const
128     {
129         return elevation_;
130     }
131 
SetHardwareAcceleration(bool acceleration)132     void SetHardwareAcceleration(bool acceleration)
133     {
134         isHardwareAcceleration_ = acceleration;
135     }
136 
GetHardwareAcceleration()137     bool GetHardwareAcceleration() const
138     {
139         return isHardwareAcceleration_;
140     }
141 
SetLightHeight(float lightHeight)142     void SetLightHeight(float lightHeight)
143     {
144         if (lightHeight > 0.0f) {
145             lightHeight_ = lightHeight;
146         }
147     }
148 
GetLightHeight()149     float GetLightHeight() const
150     {
151         return lightHeight_;
152     }
153 
SetLightRadius(float lightRadius)154     void SetLightRadius(float lightRadius)
155     {
156         if (lightRadius > 0.0f) {
157             lightRadius_ = lightRadius;
158         }
159     }
160 
GetLightRadius()161     float GetLightRadius() const
162     {
163         return lightRadius_;
164     }
165 
IsValid()166     bool IsValid() const
167     {
168         if (isHardwareAcceleration_) {
169             return elevation_ > 0.0f && elevation_ < LIGHT_HEIGHT;
170         }
171         return blurRadius_ > 0.0 || spreadRadius_ > 0.0 || offset_ != Offset::Zero();
172     }
173 
174 private:
175     float lightHeight_ = LIGHT_HEIGHT;
176     float lightRadius_ = LIGHT_RADIUS;
177     float elevation_ = 0.0f; // Rosen always needs a non-zero elevation.
178     double blurRadius_ = 0.0;
179     double spreadRadius_ = 0.0;
180     Offset offset_;
181     Color color_ = Color::BLACK;
182     bool isHardwareAcceleration_ = false;
183 };
184 
185 } // namespace OHOS::Ace
186 
187 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_PROPERTIES_SHADOW_H
188