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