1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MARQUEE_RENDER_MARQUEE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MARQUEE_RENDER_MARQUEE_H
18 
19 #include <functional>
20 
21 #include "core/animation/animator.h"
22 #include "core/animation/curve_animation.h"
23 #include "core/pipeline/base/render_node.h"
24 
25 namespace OHOS::Ace {
26 
27 class ACE_EXPORT RenderMarquee : public RenderNode {
28     DECLARE_ACE_TYPE(RenderMarquee, RenderNode);
29 
30 public:
31     static RefPtr<RenderNode> Create();
32     void Update(const RefPtr<Component>& component) override;
33     void GetPlayerCtr(const RefPtr<Component>& component, bool emdit, bool control);
34     void GetMarqueeCallback(const RefPtr<Component>& component);
35     void PerformLayout() override;
36     void OnHiddenChanged(bool hidden) override;
37     void Start();
38     void Stop();
GetValue()39     std::string GetValue() const
40     {
41         return value_;
42     }
GetStep()43     double GetStep() const
44     {
45         return scrollAmount_;
46     }
GetLoop()47     int32_t GetLoop() const
48     {
49         return loop_;
50     }
GetStart()51     bool GetStart() const
52     {
53         return start_;
54     }
GetFromStart()55     bool GetFromStart() const
56     {
57         if (direction_ == MarqueeDirection::LEFT) {
58             return true;
59         } else {
60             return false;
61         }
62     }
GetTextStyle()63     const TextStyle& GetTextStyle()
64     {
65         return textStyle_;
66     }
67 
68 protected:
69     virtual TextDirection GetTextDirection(const std::string& text) const = 0;
70 
SetOnStart(const std::function<void (void)> & value)71     void SetOnStart(const std::function<void(void)>& value)
72     {
73         onStartEvent_ = value;
74     }
75 
SetOnBounce(const std::function<void (void)> & value)76     void SetOnBounce(const std::function<void(void)>& value)
77     {
78         onBounceEvent_ = value;
79     }
80 
SetOnFinish(const std::function<void (void)> & value)81     void SetOnFinish(const std::function<void(void)>& value)
82     {
83         onFinishEvent_ = value;
84     }
85 
86 private:
87     void UpdateAnimation();
88     void UpdateChildPosition(double position);
89     void OnAnimationStart();
90     void OnAnimationStop();
91     bool NeedMarquee() const;
92     Offset GetTextPosition() const;
93 
94     RefPtr<RenderNode> childText_;
95     RefPtr<Animator> controller_;
96     RefPtr<CurveAnimation<double>> translate_;
97     Offset childPosition_ = Offset::ErrorOffset();
98 
99     bool needAnimation_ = true;
100     bool startAfterLayout_ = true;
101     bool startAfterShowed_ = false;
102     bool playerFinishControl_ = false;
103     bool isHidden_ = false;
104     bool start_ = true;
105     double scrollAmount_ = 0.0;
106     int32_t loop_ = ANIMATION_REPEAT_INFINITE;
107     int32_t currentLoop_ = 0;
108     std::string value_;
109     MarqueeDirection direction_ = MarqueeDirection::LEFT;
110     std::function<void()> bounceEvent_;
111     std::function<void()> finishEvent_;
112     std::function<void()> startEvent_;
113 
114     std::function<void()> onBounceEvent_;
115     std::function<void()> onFinishEvent_;
116     std::function<void()> onStartEvent_;
117     Size lastLayoutSize_;
118     TextStyle textStyle_;
119 };
120 
121 } // namespace OHOS::Ace
122 
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MARQUEE_RENDER_MARQUEE_H
124