1 /*
2  * Copyright (c) 2024 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 "knuckle_divergent_point.h"
17 
18 #include <random>
19 
20 #include "include/core/SkColorFilter.h"
21 #include "mmi_log.h"
22 #include "platform/ohos/overdraw/rs_overdraw_controller.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "KnuckleDivergentPoint"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 constexpr double PI { 3.14159265358979323846f };
31 constexpr double MOVE_SPEED { 10.0f };
32 constexpr double BASIC_GRAVITY_Y { 0.5f };
33 constexpr int32_t BASIC_LIFESPAN { 15 };
34 constexpr float DOUBLE { 2.0f };
35 constexpr float DYNAMIC_EFFECT_SIZE { 0.8f };
36 constexpr int32_t ARGB_COLOR_ARRAY { 0x20c8ffff };
37 } // namespace
38 
KnuckleDivergentPoint(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)39 KnuckleDivergentPoint::KnuckleDivergentPoint(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
40     : traceShadow_(pixelMap)
41 {
42     CALL_DEBUG_ENTER;
43     OHOS::Rosen::Drawing::Filter filter;
44     OHOS::Rosen::OverdrawColorArray colorArray = {
45         0x00000000,
46         0x00000000,
47         0x00000000,
48         0x00000000,
49         0x00000000,
50         ARGB_COLOR_ARRAY,
51     };
52 
53     auto protanomalyMat = OHOS::Rosen::Drawing::ColorFilter::CreateOverDrawColorFilter(colorArray.data());
54     filter.SetColorFilter(protanomalyMat);
55     brush_.SetFilter(filter);
56 }
57 
Update()58 void KnuckleDivergentPoint::Update()
59 {
60     CALL_DEBUG_ENTER;
61     if (IsEnded()) {
62         return;
63     }
64     lifespan_--;
65     pointX_ += moveVelocityX_;
66     pointY_ += moveVelocityY_;
67     moveVelocityY_ += BASIC_GRAVITY_Y;
68 }
69 
Clear()70 void KnuckleDivergentPoint::Clear()
71 {
72     CALL_DEBUG_ENTER;
73     lifespan_ = DEFAULT_LIFESPAN;
74 }
75 
Draw(Rosen::ExtendRecordingCanvas * canvas)76 void KnuckleDivergentPoint::Draw(Rosen::ExtendRecordingCanvas* canvas)
77 {
78     CALL_DEBUG_ENTER;
79     CHKPV(canvas);
80     if (IsEnded() || pointX_ <= 0 || pointY_ <= 0) {
81         return;
82     }
83 
84     std::random_device rd;
85     std::default_random_engine randomEngine(rd());
86     std::uniform_real_distribution<double> u(0.0, DYNAMIC_EFFECT_SIZE);
87     float proportion = u(randomEngine);
88     traceMatrix_.Reset();
89     traceMatrix_.PostScale(proportion, proportion, pointX_, pointY_);
90     canvas->SetMatrix(traceMatrix_);
91     canvas->AttachBrush(brush_);
92     Rosen::Drawing::Rect src = Rosen::Drawing::Rect(0, 0, traceShadow_->GetWidth(), traceShadow_->GetHeight());
93     Rosen::Drawing::Rect dst = Rosen::Drawing::Rect(pointX_, pointY_, pointX_ + traceShadow_->GetWidth(),
94         pointY_ + traceShadow_->GetHeight());
95     canvas->DrawPixelMapRect(traceShadow_, src, dst, Rosen::Drawing::SamplingOptions());
96     canvas->DetachBrush();
97 }
98 
Reset(double pointX,double pointY)99 void KnuckleDivergentPoint::Reset(double pointX, double pointY)
100 {
101     CALL_DEBUG_ENTER;
102     pointX_ = pointX;
103     pointY_ = pointY;
104     lifespan_ = BASIC_LIFESPAN;
105     std::random_device rd;
106     std::default_random_engine randomEngine(rd());
107     std::uniform_real_distribution<double> u(0.0, 1.0);
108     double baseVelocity = u(randomEngine) * DOUBLE * PI;
109 
110     moveVelocityX_ = std::cos(baseVelocity) * MOVE_SPEED;
111     moveVelocityY_ = std::sin(baseVelocity) * MOVE_SPEED;
112 }
113 
IsEnded() const114 bool KnuckleDivergentPoint::IsEnded() const
115 {
116     CALL_DEBUG_ENTER;
117     return lifespan_ < 0;
118 }
119 } // namespace MMI
120 } // namespace OHOS
121