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_SHAPE_SHAPE_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_SHAPE_COMPONENT_H 18 19 #include "base/geometry/shape.h" 20 #include "core/pipeline/base/measurable.h" 21 #include "frameworks/core/components/common/properties/animatable_path.h" 22 #include "frameworks/core/components/common/properties/radius.h" 23 #include "frameworks/core/components/common/properties/svg_paint_state.h" 24 #include "frameworks/core/pipeline/base/render_component.h" 25 26 namespace OHOS::Ace { 27 28 // keep same as the doc 29 static const char* SHAPE_TYPE_STRINGS[] = { "Rect", "Circle", "Ellipse", "Line", "Polygon", "Polyline", "Path"}; 30 31 class ACE_EXPORT ShapeComponent : public RenderComponent, public Measurable { 32 DECLARE_ACE_TYPE(ShapeComponent, RenderComponent, Measurable); 33 34 public: ShapeComponent(ShapeType shapeType)35 explicit ShapeComponent(ShapeType shapeType) : RenderComponent(), shapeType_(shapeType) {} 36 ~ShapeComponent() override = default; 37 RefPtr<RenderNode> CreateRenderNode() override; 38 RefPtr<Element> CreateElement() override; 39 SetTopLeftRadius(const Radius & topLeftRadius)40 void SetTopLeftRadius(const Radius& topLeftRadius) 41 { 42 topLeftRadius_ = topLeftRadius; 43 } 44 SetTopRightRadius(const Radius & topRightRadius)45 void SetTopRightRadius(const Radius& topRightRadius) 46 { 47 topRightRadius_ = topRightRadius; 48 } 49 SetBottomRightRadius(const Radius & bottomRightRadius)50 void SetBottomRightRadius(const Radius& bottomRightRadius) 51 { 52 bottomRightRadius_ = bottomRightRadius; 53 } 54 SetBottomLeftRadius(const Radius & bottomLeftRadius)55 void SetBottomLeftRadius(const Radius& bottomLeftRadius) 56 { 57 bottomLeftRadius_ = bottomLeftRadius; 58 } 59 SetStart(const ShapePoint & start)60 void SetStart(const ShapePoint& start) 61 { 62 start_ = start; 63 } 64 SetEnd(const ShapePoint & end)65 void SetEnd(const ShapePoint& end) 66 { 67 end_ = end; 68 } 69 SetPoints(const ShapePoints & points)70 void SetPoints(const ShapePoints& points) 71 { 72 points_ = points; 73 } 74 75 void SetPathCmd(const std::string& pathCmd, const AnimationOption& option = AnimationOption()) 76 { 77 pathCmd_ = AnimatablePath(pathCmd, option); 78 } 79 GetShapeType()80 ShapeType GetShapeType() const 81 { 82 return shapeType_; 83 } 84 GetTopLeftRadius()85 Radius GetTopLeftRadius() const 86 { 87 return topLeftRadius_; 88 } 89 GetTopRightRadius()90 Radius GetTopRightRadius() const 91 { 92 return topRightRadius_; 93 } 94 GetBottomRightRadius()95 Radius GetBottomRightRadius() const 96 { 97 return bottomRightRadius_; 98 } 99 GetBottomLeftRadius()100 Radius GetBottomLeftRadius() const 101 { 102 return bottomLeftRadius_; 103 } 104 105 void SetRadiusWidth(const Dimension& value, const AnimationOption& option = AnimationOption()) 106 { 107 topLeftRadius_.SetX(value, option); 108 topRightRadius_.SetX(value, option); 109 bottomRightRadius_.SetX(value, option); 110 bottomLeftRadius_.SetX(value, option); 111 } 112 113 void SetRadiusHeight(const Dimension& value, const AnimationOption& option = AnimationOption()) 114 { 115 topLeftRadius_.SetY(value, option); 116 topRightRadius_.SetY(value, option); 117 bottomRightRadius_.SetY(value, option); 118 bottomLeftRadius_.SetY(value, option); 119 } 120 GetStart()121 ShapePoint GetStart() const 122 { 123 return start_; 124 } 125 GetEnd()126 ShapePoint GetEnd() const 127 { 128 return end_; 129 } 130 GetPathCmd()131 const AnimatablePath& GetPathCmd() const 132 { 133 return pathCmd_; 134 } 135 GetPoints()136 const std::vector<ShapePoint>& GetPoints() const 137 { 138 return points_; 139 } 140 GetFillState()141 FillState GetFillState() const 142 { 143 return fillState_; 144 } 145 GetStrokeState()146 StrokeState GetStrokeState() const 147 { 148 return strokeState_; 149 } 150 SetAntiAlias(bool antiAlias)151 void SetAntiAlias(bool antiAlias) 152 { 153 antiAlias_.first = true; 154 antiAlias_.second = antiAlias; 155 } 156 GetAntiAlias()157 const std::pair<bool, bool>& GetAntiAlias() const 158 { 159 return antiAlias_; 160 } 161 162 void SetStroke(const Color& color, const AnimationOption& option = AnimationOption()) 163 { 164 strokeState_.SetColor(color, true, option); 165 } 166 167 void SetFill(const Color& color, const AnimationOption& option = AnimationOption()) 168 { 169 fillState_.SetColor(color, true, option); 170 } 171 172 void SetStrokeDashOffset(const Dimension& dashOffset, const AnimationOption& option = AnimationOption()) 173 { 174 strokeState_.SetStrokeDashOffset(dashOffset, true, option); 175 } 176 SetStrokeLineCap(LineCapStyle lineCapStyle)177 void SetStrokeLineCap(LineCapStyle lineCapStyle) 178 { 179 strokeState_.SetLineCap(lineCapStyle); 180 } 181 SetStrokeLineJoin(LineJoinStyle lineJoinStyle)182 void SetStrokeLineJoin(LineJoinStyle lineJoinStyle) 183 { 184 strokeState_.SetLineJoin(lineJoinStyle); 185 } 186 SetStrokeMiterLimit(double miterLimit)187 void SetStrokeMiterLimit(double miterLimit) 188 { 189 strokeState_.SetMiterLimit(miterLimit); 190 } 191 192 void SetStrokeOpacity(double opacity, const AnimationOption& option = AnimationOption()) 193 { 194 strokeState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option); 195 } 196 197 void SetFillOpacity(double opacity, const AnimationOption& option = AnimationOption()) 198 { 199 fillState_.SetOpacity(std::clamp(opacity, 0.0, 1.0), true, option); 200 } 201 202 void SetStrokeWidth(const Dimension& lineWidth, const AnimationOption& option = AnimationOption()) 203 { 204 strokeState_.SetLineWidth(lineWidth, true, option); 205 } 206 SetStrokeDashArray(const std::vector<Dimension> & segments)207 void SetStrokeDashArray(const std::vector<Dimension>& segments) 208 { 209 strokeState_.SetStrokeDashArray(segments); 210 } 211 212 void InheritShapeStyle(const FillState& fillState, const StrokeState& strokeState, bool antiAlias); 213 214 private: 215 ShapeType shapeType_ = ShapeType::RECT; 216 Radius topLeftRadius_ = Radius(-1); 217 Radius topRightRadius_ = Radius(-1); 218 Radius bottomRightRadius_ = Radius(-1); 219 Radius bottomLeftRadius_ = Radius(-1); 220 ShapePoint start_; 221 ShapePoint end_; 222 std::vector<ShapePoint> points_; 223 AnimatablePath pathCmd_; 224 FillState fillState_; 225 StrokeState strokeState_; 226 std::pair<bool, bool> antiAlias_ = std::make_pair(false, true); 227 }; 228 229 } // namespace OHOS::Ace 230 231 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SHAPE_SHAPE_COMPONENT_H 232