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 #include "core/components/shape/render_shape.h"
17 
18 #include "base/log/event_report.h"
19 
20 namespace OHOS::Ace {
21 
Update(const RefPtr<Component> & component)22 void RenderShape::Update(const RefPtr<Component>& component)
23 {
24     auto shapeComponent = AceType::DynamicCast<ShapeComponent>(component);
25     if (!shapeComponent) {
26         LOGE("RenderShape update with nullptr");
27         EventReport::SendRenderException(RenderExcepType::RENDER_COMPONENT_ERR);
28         return;
29     }
30     component_ = shapeComponent;
31     shapeType_ = shapeComponent->GetShapeType();
32     topLeftRadius_ = shapeComponent->GetTopLeftRadius();
33     topRightRadius_ = shapeComponent->GetTopRightRadius();
34     bottomRightRadius_ = shapeComponent->GetBottomRightRadius();
35     bottomLeftRadius_ = shapeComponent->GetBottomLeftRadius();
36     start_ = shapeComponent->GetStart();
37     end_ = shapeComponent->GetEnd();
38     fillState_ = shapeComponent->GetFillState();
39     strokeState_ = shapeComponent->GetStrokeState();
40     antiAlias_ = shapeComponent->GetAntiAlias();
41     pathCmd_ = shapeComponent->GetPathCmd();
42     points_ = shapeComponent->GetPoints();
43     NormalToPxOfShape(shapeComponent->GetWidth(), width_);
44     NormalToPxOfShape(shapeComponent->GetHeight(), height_);
45     MarkNeedLayout();
46 }
47 
PerformLayout()48 void RenderShape::PerformLayout()
49 {
50     auto size = CalcSize();
51     if (size.IsValid()) {
52         size = GetLayoutParam().Constrain(size);
53     }
54     SetLayoutSize(size);
55 }
56 
OnAnimationCallback()57 void RenderShape::OnAnimationCallback()
58 {
59     CalcSize();
60 }
61 
NormalToPxOfShape(AnimatableDimension sizeFromComponent,AnimatableDimension & sizeOfCurrent)62 void RenderShape::NormalToPxOfShape(AnimatableDimension sizeFromComponent, AnimatableDimension& sizeOfCurrent)
63 {
64     if (sizeFromComponent.Unit() == DimensionUnit::PERCENT || sizeFromComponent.Unit() == DimensionUnit::PX) {
65         sizeOfCurrent = sizeFromComponent;
66     } else {
67         auto context = context_.Upgrade();
68         if (!context) {
69             return;
70         }
71         sizeOfCurrent.SetValue(context->NormalizeToPx(sizeFromComponent));
72         sizeOfCurrent.SetUnit(DimensionUnit::PX);
73     }
74 }
75 
NotifySizeTransition(const AnimationOption & option,Size fromSize,Size toSize,int32_t nodeId)76 void RenderShape::NotifySizeTransition(const AnimationOption& option, Size fromSize, Size toSize, int32_t nodeId)
77 {
78     RenderNode::NotifySizeTransition(option, fromSize, toSize, nodeId);
79     auto context = context_.Upgrade();
80     if (!context) {
81         return;
82     }
83     if (option.IsValid()) {
84         auto optionOrigin = context->GetExplicitAnimationOption();
85         context->SaveExplicitAnimationOption(option);
86         width_.MoveTo(fromSize.Width());
87         height_.MoveTo(fromSize.Height());
88         width_ = AnimatableDimension(toSize.Width());
89         height_ = AnimatableDimension(toSize.Height());
90         context->SaveExplicitAnimationOption(optionOrigin);
91         CalcSize();
92     }
93 }
94 
95 } // namespace OHOS::Ace
96