2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/text/text-scroller.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
26 #include <dali-toolkit/internal/text/text-scroller-interface.h>
34 #if defined(DEBUG_ENABLED)
35 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
38 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
41 * @brief How the text should be aligned horizontally when scrolling the text.
43 * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right.
44 * The final alignment depends on two factors:
45 * 1) The alignment value of the text label (Use Text::HorizontalAlignment enumerations).
46 * 2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
48 const float HORIZONTAL_ALIGNMENT_TABLE[Text::HorizontalAlignment::END + 1][2] =
50 // HorizontalAlignment::BEGIN
56 // HorizontalAlignment::CENTER
62 // HorizontalAlignment::END
69 * @brief How the text should be aligned vertically when scrolling the text.
71 * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom.
72 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
74 const float VERTICAL_ALIGNMENT_TABLE[Text::VerticalAlignment::BOTTOM + 1] =
76 -0.5f, // VerticalAlignment::TOP
77 0.0f, // VerticalAlignment::CENTER
78 0.5f // VerticalAlignment::BOTTOM
85 TextScrollerPtr TextScroller::New(ScrollerInterface& scrollerInterface)
87 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::New\n");
89 TextScrollerPtr textScroller(new TextScroller(scrollerInterface));
93 void TextScroller::SetGap(int gap)
95 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap);
96 mWrapGap = static_cast<float>(gap);
99 int TextScroller::GetGap() const
101 return static_cast<int>(mWrapGap);
104 void TextScroller::SetSpeed(int scrollSpeed)
106 mScrollSpeed = std::max(MINIMUM_SCROLL_SPEED, scrollSpeed);
109 int TextScroller::GetSpeed() const
114 void TextScroller::SetLoopCount(int loopCount)
118 mLoopCount = loopCount;
121 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount) ? "looping" : "stop");
124 int TextScroller::GetLoopCount() const
129 void TextScroller::SetLoopDelay(float delay)
134 float TextScroller::GetLoopDelay() const
139 void TextScroller::SetStopMode(TextLabel::AutoScrollStopMode::Type stopMode)
141 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n", (stopMode == TextLabel::AutoScrollStopMode::IMMEDIATE) ? "IMMEDIATE" : "FINISH_LOOP");
142 mStopMode = stopMode;
145 void TextScroller::StopScrolling()
151 case TextLabel::AutoScrollStopMode::IMMEDIATE:
154 mScrollAnimation.Stop();
155 mScrollerInterface.ScrollingFinished();
158 case TextLabel::AutoScrollStopMode::FINISH_LOOP:
161 mScrollAnimation.SetLoopCount(1); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
166 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n");
172 mScrollerInterface.ScrollingFinished();
176 bool TextScroller::IsStop()
181 bool TextScroller::IsScrolling()
183 return (mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING);
186 TextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
191 TextScroller::TextScroller(ScrollerInterface& scrollerInterface)
192 : mScrollerInterface(scrollerInterface),
193 mScrollDeltaIndex(Property::INVALID_INDEX),
194 mScrollSpeed(MINIMUM_SCROLL_SPEED),
198 mStopMode(TextLabel::AutoScrollStopMode::FINISH_LOOP),
201 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n");
204 TextScroller::~TextScroller()
208 void TextScroller::SetParameters(Actor scrollingTextActor, Renderer renderer, TextureSet textureSet, const Size& controlSize, const Size& textureSize, const float wrapGap, CharacterDirection direction, HorizontalAlignment::Type horizontalAlignment, VerticalAlignment::Type verticalAlignment)
210 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] textureSize[%f,%f] direction[%d]\n", controlSize.x, controlSize.y, textureSize.x, textureSize.y, direction);
211 mRenderer = renderer;
213 float animationProgress = 0.0f;
214 int remainedLoop = mLoopCount;
217 if(mScrollAnimation.GetState() == Animation::PLAYING)
219 animationProgress = mScrollAnimation.GetCurrentProgress();
221 if(mLoopCount > 0) // If not a ininity loop, then calculate remained loop
223 remainedLoop = mLoopCount - (mScrollAnimation.GetCurrentLoop());
224 remainedLoop = mIsStop ? 1 : (remainedLoop <= 0 ? 1 : remainedLoop);
227 mScrollAnimation.Clear();
229 // Reset to the original shader and texture before scrolling
230 mRenderer.SetShader(mShader);
233 mRenderer.SetTextures(mTextureSet);
237 mShader = mRenderer.GetShader();
238 mTextureSet = mRenderer.GetTextures();
240 // Set the shader and texture for scrolling
241 Shader shader = Shader::New(SHADER_TEXT_SCROLLER_SHADER_VERT, SHADER_TEXT_SCROLLER_SHADER_FRAG, Shader::Hint::NONE, "TEXT_SCROLLER");
242 mRenderer.SetShader(shader);
243 mRenderer.SetTextures(textureSet);
245 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetParameters wrapGap[%f]\n", wrapGap);
247 float horizontalAlign;
249 if(textureSize.x > controlSize.x)
251 // if Text is elided, scroll should start at the begin of text.
252 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[HorizontalAlignment::BEGIN][direction];
256 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[horizontalAlignment][direction];
259 const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[verticalAlignment];
261 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign);
263 shader.RegisterProperty("uTextureSize", textureSize);
264 shader.RegisterProperty("uHorizontalAlign", horizontalAlign);
265 shader.RegisterProperty("uVerticalAlign", verticalAlign);
266 shader.RegisterProperty("uGap", wrapGap);
267 mScrollDeltaIndex = shader.RegisterProperty("uDelta", 0.0f);
269 float scrollAmount = std::max(textureSize.width, controlSize.width);
270 float scrollDuration = scrollAmount / mScrollSpeed;
274 scrollAmount = -scrollAmount; // reverse direction of scrolling
277 StartScrolling(scrollingTextActor, scrollAmount, scrollDuration, remainedLoop);
278 mScrollAnimation.SetCurrentProgress(animationProgress);
281 void TextScroller::AutoScrollAnimationFinished(Dali::Animation& animation)
283 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n");
285 if(mStopMode == TextLabel::AutoScrollStopMode::FINISH_LOOP)
287 mScrollerInterface.ScrollingFinished();
291 void TextScroller::StartScrolling(Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount)
293 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed);
294 Shader shader = mRenderer.GetShader();
295 mScrollAnimation = Animation::New(scrollDuration);
296 mScrollAnimation.AnimateTo(Property(shader, mScrollDeltaIndex), scrollAmount, TimePeriod(mLoopDelay, scrollDuration));
297 mScrollAnimation.SetEndAction(Animation::DISCARD);
298 mScrollAnimation.SetLoopCount(loopCount);
299 mScrollAnimation.FinishedSignal().Connect(this, &TextScroller::AutoScrollAnimationFinished);
300 mScrollAnimation.Play();
307 } // namespace Toolkit