• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_ng/render/adapter/moon_progress_modifier.h"
17 
18 #include <cmath>
19 
20 #include "base/memory/ace_type.h"
21 #include "base/utils/utils.h"
22 #include "core/common/container.h"
23 #include "core/common/container_scope.h"
24 #include "core/components_ng/render/animation_utils.h"
25 #include "core/components_ng/render/drawing_prop_convertor.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27 
28 namespace OHOS::Ace::NG {
29 namespace {
30 constexpr int32_t DIFFUSE_DURATION = 300;
31 constexpr float INITIAL_RATIO = 1.0f;
32 constexpr int32_t INT32_TWO = 2;
33 constexpr int32_t ANGLE_90 = 90;
34 constexpr int32_t ANGLE_180 = 180;
35 constexpr int32_t ANGLE_270 = 270;
36 constexpr float FLOAT_ZERO_FIVE = 0.5f;
37 constexpr float FLOAT_ZERO_SEVEN = 0.7f;
38 constexpr float FLOAT_ONE_ZERO = 1.0f;
39 constexpr float SPRING_MOTION_RESPONSE = 0.314f;
40 constexpr float SPRING_MOTION_DAMPING_FRACTION = 0.95f;
41 const float EPSLION = 1e-5;
42 const float DEFAULT_MAXVALUE = 100.0f;
43 } // namespace
44 
MoonProgressModifier()45 MoonProgressModifier::MoonProgressModifier()
46     : maskColor_(AceType::MakeRefPtr<AnimatablePropertyColor>(LinearColor(Color::TRANSPARENT))),
47       ratio_(AceType::MakeRefPtr<AnimatablePropertyFloat>(INITIAL_RATIO)),
48       value_(AceType::MakeRefPtr<AnimatablePropertyFloat>(.0f)),
49       maxValue_(AceType::MakeRefPtr<PropertyFloat>(DEFAULT_MAXVALUE))
50 {
51     AttachProperty(maskColor_);
52     AttachProperty(ratio_);
53     AttachProperty(value_);
54     AttachProperty(maxValue_);
55 }
56 
onDraw(DrawingContext & context)57 void MoonProgressModifier::onDraw(DrawingContext& context)
58 {
59     SizeF frameSize(context.width, context.height);
60     SetBigRadius(frameSize);
61     PaintSquareMoon(context.canvas, frameSize);
62 }
63 
SetMaskColor(LinearColor color)64 void MoonProgressModifier::SetMaskColor(LinearColor color)
65 {
66     maskColor_->Set(color);
67 }
68 
SetValue(float value)69 void MoonProgressModifier::SetValue(float value)
70 {
71     auto finishCallback = [weak = AceType::WeakClaim(this), bigRadius = bigRadius_, smallRadius = smallRadius_,
72                               id = Container::CurrentId()]() {
73         ContainerScope scope(id);
74         auto pipeline = PipelineContext::GetCurrentContext();
75         CHECK_NULL_VOID(pipeline);
76         auto task = [weak, bigRadius, smallRadius]() {
77             auto modifier = weak.Upgrade();
78             CHECK_NULL_VOID(modifier);
79             double angle = (modifier->value_->Get() / modifier->maxValue_->Get()) * 1;
80             if (NearEqual(std::abs(angle - FLOAT_ONE_ZERO), EPSLION)) {
81                 modifier->SetMoonAnimate(bigRadius / smallRadius);
82             }
83         };
84         pipeline->PostSyncEvent(task);
85         pipeline->RequestFrame();
86     };
87 
88     AnimationOption option;
89     auto motion = AceType::MakeRefPtr<ResponsiveSpringMotion>(SPRING_MOTION_RESPONSE, SPRING_MOTION_DAMPING_FRACTION);
90     option.SetCurve(motion);
91     AnimationUtils::Animate(
92         option,
93         [weak = AceType::WeakClaim(AceType::RawPtr(value_)), valueTo = value]() {
94             auto value = weak.Upgrade();
95             CHECK_NULL_VOID(value);
96             value->Set(valueTo);
97         },
98         finishCallback);
99 
100     CHECK_NULL_VOID(maxValue_);
101     if (value < maxValue_->Get() && std::abs(maxValue_->Get() - value) > EPSLION &&
102         std::abs(ratio_->Get() - INITIAL_RATIO) > EPSLION) {
103         InitRatio();
104     }
105 }
106 
SetMaxValue(float value)107 void MoonProgressModifier::SetMaxValue(float value)
108 {
109     maxValue_->Set(value);
110 }
111 
GetMaxValue()112 float MoonProgressModifier::GetMaxValue()
113 {
114     if (maxValue_) {
115         return maxValue_->Get();
116     } else {
117         return DEFAULT_MAXVALUE;
118     }
119 }
120 
InitRatio()121 void MoonProgressModifier::InitRatio()
122 {
123     ratio_->Set(INITIAL_RATIO);
124 }
125 
SetMoonAnimate(float value) const126 void MoonProgressModifier::SetMoonAnimate(float value) const
127 {
128     if (ratio_) {
129         AnimationOption option;
130         option.SetDuration(DIFFUSE_DURATION);
131         option.SetDelay(0);
132         option.SetCurve(Curves::SHARP);
133         AnimationUtils::Animate(option, [weak = AceType::WeakClaim(AceType::RawPtr(ratio_)), value]() {
134             auto ratio = weak.Upgrade();
135             CHECK_NULL_VOID(ratio);
136             ratio->Set(value);
137         });
138     }
139 }
140 
SetBigRadius(const SizeF & frameSize)141 void MoonProgressModifier::SetBigRadius(const SizeF& frameSize)
142 {
143     bigRadius_ = std::sqrt(std::pow(frameSize.Width() / INT32_TWO, 2) + std::pow(frameSize.Height() / INT32_TWO, 2));
144     double radius = (std::min(frameSize.Width() / INT32_TWO, frameSize.Height() / INT32_TWO));
145     smallRadius_ = radius * INITIAL_RATIO * FLOAT_ZERO_SEVEN;
146 }
147 
PaintSquareMoon(RSCanvas & canvas,const SizeF & frameSize) const148 void MoonProgressModifier::PaintSquareMoon(RSCanvas& canvas, const SizeF& frameSize) const
149 {
150     static int32_t totalDegree = 1;
151     PointF centerPt = PointF(frameSize.Width() / INT32_TWO, frameSize.Height() / INT32_TWO);
152     RSBrush brush;
153     double angle = (value_->Get() / maxValue_->Get()) * totalDegree;
154     RSPath path;
155     brush.SetAntiAlias(true);
156     brush.SetColor(ToRSColor((maskColor_->Get())));
157     canvas.AttachBrush(brush);
158 #ifndef USE_ROSEN_DRAWING
159     path.SetFillStyle(RSPathFillType::EVENTODD);
160 #else
161     path.SetFillStyle(RSPathFillType::EVENTODD);
162 #endif
163     path.AddCircle(centerPt.GetX(), centerPt.GetY(), bigRadius_, RSPathDirection::CW_DIRECTION);
164     if (NearZero(std::abs(ratio_->Get() - INITIAL_RATIO), EPSLION)) {
165         path.AddArc(
166             { centerPt.GetX() - smallRadius_, centerPt.GetY() - smallRadius_,
167             centerPt.GetX() + smallRadius_, centerPt.GetY() + smallRadius_ }, ANGLE_90, ANGLE_180);
168         if (angle <= FLOAT_ZERO_FIVE) {
169             double progressOffset = smallRadius_ - smallRadius_ * angle / FLOAT_ZERO_FIVE;
170             path.MoveTo(centerPt.GetX(), centerPt.GetY() - smallRadius_);
171             // startAngle:270  sweepAngle:-180
172             path.AddArc(
173                 { centerPt.GetX() - progressOffset, centerPt.GetY() - smallRadius_,
174                 centerPt.GetX() + progressOffset, centerPt.GetY() + smallRadius_ }, ANGLE_270, -ANGLE_180);
175             canvas.DrawPath(path);
176         } else {
177             double progressOffset = smallRadius_ * (angle - FLOAT_ZERO_FIVE) / FLOAT_ZERO_FIVE;
178             path.MoveTo(centerPt.GetX(), centerPt.GetY() - smallRadius_);
179             // startAngle:270  sweepAngle:180
180             path.AddArc(
181                 { centerPt.GetX() - progressOffset, centerPt.GetY() - smallRadius_,
182                 centerPt.GetX() + progressOffset, centerPt.GetY() + smallRadius_ }, ANGLE_270, ANGLE_180);
183             canvas.DrawPath(path);
184         }
185     } else {
186         path.MoveTo(centerPt.GetX(), centerPt.GetY() -  smallRadius_ * ratio_->Get());
187         path.AddCircle(centerPt.GetX(), centerPt.GetY(), smallRadius_ * ratio_->Get(),
188             RSPathDirection::CW_DIRECTION);
189         canvas.DrawPath(path);
190     }
191 }
192 } // namespace OHOS::Ace::NG
193