1/*
2 * Copyright (c) 2020-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 "animator/animator.h"
17
18#include "animator/animator_manager.h"
19#include "hal_tick.h"
20
21namespace OHOS {
22Animator::~Animator()
23{
24    AnimatorManager::GetInstance()->Remove(this);
25}
26
27void Animator::Start()
28{
29    SetState(START);
30    runTime_ = 0;
31    lastRunTime_ = HALTick::GetInstance().GetTime();
32    AnimatorManager::GetInstance()->Add(this);
33}
34
35void Animator::Stop()
36{
37    SetState(STOP);
38    if (callback_ != nullptr) {
39        callback_->OnStop(*view_);
40    }
41    AnimatorManager::GetInstance()->Remove(this);
42}
43
44void Animator::Pause()
45{
46    SetState(PAUSE);
47    AnimatorManager::GetInstance()->Remove(this);
48}
49
50void Animator::Resume()
51{
52    SetState(START);
53    lastRunTime_ = HALTick::GetInstance().GetTime();
54    AnimatorManager::GetInstance()->Add(this);
55}
56
57void Animator::Run()
58{
59    uint32_t elapse = HALTick::GetInstance().GetElapseTime(lastRunTime_);
60    lastRunTime_ = HALTick::GetInstance().GetTime();
61    runTime_ = (UINT32_MAX - elapse > runTime_) ? (runTime_ + elapse) : period_;
62
63    if (!repeat_ && (runTime_ >= period_)) {
64        runTime_ = period_;
65        if (callback_ != nullptr) {
66            callback_->Callback(view_);
67        }
68        Stop();
69        return;
70    }
71
72    if (callback_ != nullptr) {
73        callback_->Callback(view_);
74    }
75}
76} // namespace OHOS
77