1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci#include "components/ui_texture_mapper.h"
17a3e0fd82Sopenharmony_ci
18a3e0fd82Sopenharmony_cinamespace OHOS {
19a3e0fd82Sopenharmony_civoid UITextureMapper::TextureMapperAnimatorCallback::Callback(UIView* view)
20a3e0fd82Sopenharmony_ci{
21a3e0fd82Sopenharmony_ci    if (view == nullptr) {
22a3e0fd82Sopenharmony_ci        return;
23a3e0fd82Sopenharmony_ci    }
24a3e0fd82Sopenharmony_ci    UITextureMapper* mapper = static_cast<UITextureMapper*>(view);
25a3e0fd82Sopenharmony_ci    mapper->Callback();
26a3e0fd82Sopenharmony_ci}
27a3e0fd82Sopenharmony_ci
28a3e0fd82Sopenharmony_civoid UITextureMapper::TextureMapperAnimatorCallback::OnStop(UIView& view)
29a3e0fd82Sopenharmony_ci{
30a3e0fd82Sopenharmony_ci    UITextureMapper& mapper = static_cast<UITextureMapper&>(view);
31a3e0fd82Sopenharmony_ci    if (mapper.listener_ != nullptr) {
32a3e0fd82Sopenharmony_ci        mapper.listener_->OnAnimatorStop(view);
33a3e0fd82Sopenharmony_ci    }
34a3e0fd82Sopenharmony_ci}
35a3e0fd82Sopenharmony_ci
36a3e0fd82Sopenharmony_ciUITextureMapper::UITextureMapper()
37a3e0fd82Sopenharmony_ci    : animator_(&animatorCallback_, this, 0, false),
38a3e0fd82Sopenharmony_ci      listener_(nullptr),
39a3e0fd82Sopenharmony_ci      pivot_(0, 0),
40a3e0fd82Sopenharmony_ci      rotateCur_(0),
41a3e0fd82Sopenharmony_ci      rotateStart_(0),
42a3e0fd82Sopenharmony_ci      rotateEnd_(0),
43a3e0fd82Sopenharmony_ci      scaleCur_(SCALE_CONVERTION),
44a3e0fd82Sopenharmony_ci      scaleStart_(SCALE_CONVERTION),
45a3e0fd82Sopenharmony_ci      scaleEnd_(SCALE_CONVERTION),
46a3e0fd82Sopenharmony_ci      delayTime_(0),
47a3e0fd82Sopenharmony_ci      easingFunc_(EasingEquation::LinearEaseNone)
48a3e0fd82Sopenharmony_ci{
49a3e0fd82Sopenharmony_ci}
50a3e0fd82Sopenharmony_ci
51a3e0fd82Sopenharmony_ciUITextureMapper::~UITextureMapper() {}
52a3e0fd82Sopenharmony_ci
53a3e0fd82Sopenharmony_civoid UITextureMapper::Start()
54a3e0fd82Sopenharmony_ci{
55a3e0fd82Sopenharmony_ci    rotateStart_ = rotateCur_;
56a3e0fd82Sopenharmony_ci    scaleStart_ = scaleCur_;
57a3e0fd82Sopenharmony_ci    float scale = static_cast<float>(scaleStart_) / SCALE_CONVERTION;
58a3e0fd82Sopenharmony_ci    Scale(Vector2<float>(scale, scale), pivot_);
59a3e0fd82Sopenharmony_ci    Rotate(rotateStart_, pivot_);
60a3e0fd82Sopenharmony_ci    animator_.Start();
61a3e0fd82Sopenharmony_ci}
62a3e0fd82Sopenharmony_ci
63a3e0fd82Sopenharmony_civoid UITextureMapper::Cancel()
64a3e0fd82Sopenharmony_ci{
65a3e0fd82Sopenharmony_ci    animator_.Stop();
66a3e0fd82Sopenharmony_ci}
67a3e0fd82Sopenharmony_ci
68a3e0fd82Sopenharmony_civoid UITextureMapper::Reset()
69a3e0fd82Sopenharmony_ci{
70a3e0fd82Sopenharmony_ci    Invalidate();
71a3e0fd82Sopenharmony_ci    ResetTransParameter();
72a3e0fd82Sopenharmony_ci    Invalidate();
73a3e0fd82Sopenharmony_ci}
74a3e0fd82Sopenharmony_ci
75a3e0fd82Sopenharmony_civoid UITextureMapper::Callback()
76a3e0fd82Sopenharmony_ci{
77a3e0fd82Sopenharmony_ci    uint16_t curTime = animator_.GetRunTime();
78a3e0fd82Sopenharmony_ci    if (curTime >= delayTime_) {
79a3e0fd82Sopenharmony_ci        uint16_t actualTime = curTime - delayTime_;
80a3e0fd82Sopenharmony_ci        uint16_t durationTime = animator_.GetTime() - delayTime_;
81a3e0fd82Sopenharmony_ci
82a3e0fd82Sopenharmony_ci        if (scaleStart_ != scaleEnd_) {
83a3e0fd82Sopenharmony_ci            scaleCur_ = easingFunc_(scaleStart_, scaleEnd_, actualTime, durationTime);
84a3e0fd82Sopenharmony_ci        }
85a3e0fd82Sopenharmony_ci        float scale = static_cast<float>(scaleCur_) / SCALE_CONVERTION;
86a3e0fd82Sopenharmony_ci        Scale(Vector2<float>(scale, scale), pivot_);
87a3e0fd82Sopenharmony_ci
88a3e0fd82Sopenharmony_ci        if (rotateStart_ != rotateEnd_) {
89a3e0fd82Sopenharmony_ci            rotateCur_ = easingFunc_(rotateStart_, rotateEnd_, actualTime, durationTime);
90a3e0fd82Sopenharmony_ci        }
91a3e0fd82Sopenharmony_ci        Rotate(rotateCur_, pivot_);
92a3e0fd82Sopenharmony_ci    }
93a3e0fd82Sopenharmony_ci}
94a3e0fd82Sopenharmony_ci} // namespace OHOS
95