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_MARQUEE_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MARQUEE_MARQUEE_COMPONENT_H
18 
19 #include "core/components/marquee/marquee_element.h"
20 #include "core/components/marquee/render_marquee.h"
21 #include "core/components/text/text_component.h"
22 #include "core/components_v2/common/common_def.h"
23 #include "core/pipeline/base/sole_child_component.h"
24 
25 namespace OHOS::Ace {
26 
27 inline constexpr double DEFAULT_MARQUEE_SCROLL_AMOUNT = 6.0;
28 inline constexpr int32_t DEFAULT_MARQUEE_LOOP = -1;
29 
30 using MarqueeStartFunc = std::function<void()>;
31 using MarqueeStopFunc = std::function<void()>;
32 
33 class ACE_EXPORT MarqueeController : public virtual AceType {
34     DECLARE_ACE_TYPE(MarqueeController, AceType);
35 
36 public:
Start()37     void Start() const
38     {
39         if (startFunc_) {
40             startFunc_();
41         }
42     }
43 
Stop()44     void Stop() const
45     {
46         if (stopFunc_) {
47             stopFunc_();
48         }
49     }
50 
SetFunction(const MarqueeStartFunc & startFunc,const MarqueeStopFunc & stopFunc)51     void SetFunction(const MarqueeStartFunc& startFunc, const MarqueeStopFunc& stopFunc)
52     {
53         startFunc_ = startFunc;
54         stopFunc_ = stopFunc;
55     }
56 
57 private:
58     MarqueeStartFunc startFunc_;
59     MarqueeStopFunc stopFunc_;
60 };
61 
62 class ACE_EXPORT MarqueeComponent : public SoleChildComponent {
63     DECLARE_ACE_TYPE(MarqueeComponent, SoleChildComponent);
64 
65 public:
MarqueeComponent()66     MarqueeComponent() : SoleChildComponent()
67     {
68         textChild_ = AceType::MakeRefPtr<TextComponent>("");
69         SetChild(textChild_);
70         controller_ = AceType::MakeRefPtr<MarqueeController>();
71     }
72 
73     ~MarqueeComponent() override = default;
CreateElement()74     RefPtr<Element> CreateElement() override
75     {
76         return AceType::MakeRefPtr<MarqueeElement>();
77     }
78 
CreateRenderNode()79     RefPtr<RenderNode> CreateRenderNode() override
80     {
81         return RenderMarquee::Create();
82     }
83 
GetValue()84     const std::string& GetValue() const
85     {
86         return textChild_->GetData();
87     }
88 
SetValue(const std::string & value)89     void SetValue(const std::string& value)
90     {
91         textChild_->SetData(value);
92     }
93 
GetTextStyle()94     const TextStyle& GetTextStyle() const
95     {
96         return textChild_->GetTextStyle();
97     }
98 
SetTextStyle(const TextStyle & textStyle)99     void SetTextStyle(const TextStyle& textStyle)
100     {
101         textChild_->SetTextStyle(textStyle);
102         textChild_->SetFocusColor(textStyle.GetTextColor());
103     }
104 
GetScrollAmount()105     double GetScrollAmount() const
106     {
107         return scrollAmount_;
108     }
109 
SetScrollAmount(double scrollAmount)110     void SetScrollAmount(double scrollAmount)
111     {
112         scrollAmount_ = scrollAmount;
113     }
114 
GetLoop()115     int32_t GetLoop() const
116     {
117         return loop_;
118     }
119 
SetLoop(int32_t loop)120     void SetLoop(int32_t loop)
121     {
122         loop_ = loop;
123     }
124 
GetDirection()125     MarqueeDirection GetDirection() const
126     {
127         return direction_;
128     }
129 
SetDirection(MarqueeDirection direction)130     void SetDirection(MarqueeDirection direction)
131     {
132         direction_ = direction;
133     }
134 
SetBounceEventId(const EventMarker & eventId)135     void SetBounceEventId(const EventMarker& eventId)
136     {
137         bounceEventId_ = eventId;
138     }
139 
GetBounceEventId()140     const EventMarker& GetBounceEventId() const
141     {
142         return bounceEventId_;
143     }
144 
SetFinishEventId(const EventMarker & eventId)145     void SetFinishEventId(const EventMarker& eventId)
146     {
147         finishEventId_ = eventId;
148     }
149 
GetFinishEventId()150     const EventMarker& GetFinishEventId() const
151     {
152         return finishEventId_;
153     }
154 
SetStartEventId(const EventMarker & eventId)155     void SetStartEventId(const EventMarker& eventId)
156     {
157         startEventId_ = eventId;
158     }
159 
GetStartEventId()160     const EventMarker& GetStartEventId() const
161     {
162         return startEventId_;
163     }
164 
GetController()165     RefPtr<MarqueeController> GetController() const
166     {
167         return controller_;
168     }
169 
SetPlayerStatus(bool playerStatus)170     void SetPlayerStatus(bool playerStatus)
171     {
172         playerStatus_ = playerStatus;
173     }
174 
GetPlayerStatus()175     bool GetPlayerStatus() const
176     {
177         return playerStatus_;
178     }
179 
180     ACE_DEFINE_COMPONENT_EVENT(OnStart, void());
181     ACE_DEFINE_COMPONENT_EVENT(OnBounce, void());
182     ACE_DEFINE_COMPONENT_EVENT(OnFinish, void());
183 
184 private:
185     RefPtr<TextComponent> textChild_;
186     double scrollAmount_ = DEFAULT_MARQUEE_SCROLL_AMOUNT;
187     int32_t loop_ = DEFAULT_MARQUEE_LOOP;
188     bool playerStatus_ = false;
189     MarqueeDirection direction_ { MarqueeDirection::LEFT };
190     EventMarker bounceEventId_;
191     EventMarker finishEventId_;
192     EventMarker startEventId_;
193     RefPtr<MarqueeController> controller_;
194 };
195 
196 } // namespace OHOS::Ace
197 
198 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_MARQUEE_MARQUEE_COMPONENT_H
199