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