2 * Copyright (c) 2020 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/text/text-scroller-interface.h>
36 #if defined ( DEBUG_ENABLED )
37 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
40 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
42 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
43 attribute mediump vec2 aPosition;\n
44 varying highp vec2 vTexCoord;\n
45 uniform mediump vec3 uSize;\n
46 uniform mediump float uDelta;\n
47 uniform mediump vec2 uTextureSize;\n
48 uniform mediump float uGap;\n
49 uniform mediump float uHorizontalAlign;\n
50 uniform mediump float uVerticalAlign;\n
52 uniform highp mat4 uMvpMatrix;\n
54 //Visual size and offset
55 uniform mediump vec2 offset;\n
56 uniform mediump vec2 size;\n
57 uniform mediump vec4 offsetSizeMode;\n
58 uniform mediump vec2 origin;\n
59 uniform mediump vec2 anchorPoint;\n
63 mediump vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy );\n
64 mediump vec2 visualSize = mix( uSize.xy * size, size, offsetSizeMode.zw );\n
66 vTexCoord.x = ( uDelta + uHorizontalAlign * ( uTextureSize.x - visualSize.x - uGap ) + floor( aPosition.x * visualSize.x ) + 0.5 - uGap * 0.5 ) / uTextureSize.x + 0.5;\n
67 vTexCoord.y = ( uVerticalAlign * ( uTextureSize.y - visualSize.y ) + floor( aPosition.y * visualSize.y ) + 0.5 ) / ( uTextureSize.y ) + 0.5;\n
69 mediump vec4 vertexPosition = vec4( floor( ( aPosition + anchorPoint ) * visualSize + ( visualOffset + origin ) * uSize.xy ), 0.0, 1.0 );\n
71 gl_Position = uMvpMatrix * vertexPosition;\n
75 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
76 varying highp vec2 vTexCoord;\n
77 uniform sampler2D sTexture;\n
78 uniform lowp vec4 uColor;\n
79 uniform lowp vec3 mixColor;\n
83 if ( vTexCoord.y > 1.0 )\n
86 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
88 gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 );
93 * @brief How the text should be aligned horizontally when scrolling the text.
95 * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right.
96 * The final alignment depends on two factors:
97 * 1) The alignment value of the text label (Use Text::HorizontalAlignment enumerations).
98 * 2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
100 const float HORIZONTAL_ALIGNMENT_TABLE[ Text::HorizontalAlignment::END+1 ][ 2 ] =
102 // HorizontalAlignment::BEGIN
108 // HorizontalAlignment::CENTER
114 // HorizontalAlignment::END
122 * @brief How the text should be aligned vertically when scrolling the text.
124 * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom.
125 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
127 const float VERTICAL_ALIGNMENT_TABLE[ Text::VerticalAlignment::BOTTOM+1 ] =
129 -0.5f, // VerticalAlignment::TOP
130 0.0f, // VerticalAlignment::CENTER
131 0.5f // VerticalAlignment::BOTTOM
139 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
141 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
143 TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
147 void TextScroller::SetGap( int gap )
149 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
150 mWrapGap = static_cast<float>(gap);
153 int TextScroller::GetGap() const
155 return static_cast<int>(mWrapGap);
158 void TextScroller::SetSpeed( int scrollSpeed )
160 mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
163 int TextScroller::GetSpeed() const
168 void TextScroller::SetLoopCount( int loopCount )
170 if ( loopCount >= 0 )
172 mLoopCount = loopCount;
175 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
178 int TextScroller::GetLoopCount() const
183 void TextScroller::SetLoopDelay( float delay )
188 float TextScroller::GetLoopDelay() const
193 void TextScroller::SetStopMode( TextLabel::AutoScrollStopMode::Type stopMode )
195 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == TextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
196 mStopMode = stopMode;
199 void TextScroller::StopScrolling()
201 if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
205 case TextLabel::AutoScrollStopMode::IMMEDIATE:
207 mScrollAnimation.Stop();
208 mScrollerInterface.ScrollingFinished();
211 case TextLabel::AutoScrollStopMode::FINISH_LOOP:
213 mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
218 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
224 mScrollerInterface.ScrollingFinished();
228 TextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
233 TextScroller::TextScroller( ScrollerInterface& scrollerInterface )
234 : mScrollerInterface( scrollerInterface ),
235 mScrollDeltaIndex( Property::INVALID_INDEX ),
236 mScrollSpeed( MINIMUM_SCROLL_SPEED ),
240 mStopMode( TextLabel::AutoScrollStopMode::FINISH_LOOP )
242 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
245 TextScroller::~TextScroller()
249 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 )
251 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] textureSize[%f,%f] direction[%d]\n",
252 controlSize.x, controlSize.y, textureSize.x, textureSize.y, direction );
254 mRenderer = renderer;
256 float animationProgress = 0.0f;
257 int remainedLoop = mLoopCount;
258 if ( mScrollAnimation )
260 if( mScrollAnimation.GetState() == Animation::PLAYING )
262 animationProgress = mScrollAnimation.GetCurrentProgress();
264 if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop
266 remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() );
267 remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop );
270 mScrollAnimation.Clear();
272 // Reset to the original shader and texture before scrolling
273 mRenderer.SetShader(mShader);
274 mRenderer.SetTextures( mTextureSet );
277 mShader = mRenderer.GetShader();
278 mTextureSet = mRenderer.GetTextures();
280 // Set the shader and texture for scrolling
281 Shader shader = Shader::New( VERTEX_SHADER_SCROLL, FRAGMENT_SHADER, Shader::Hint::NONE );
282 mRenderer.SetShader( shader );
283 mRenderer.SetTextures( textureSet );
285 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters wrapGap[%f]\n", wrapGap );
287 float horizontalAlign;
289 if( textureSize.x > controlSize.x )
291 // if Text is elided, scroll should start at the begin of text.
292 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[HorizontalAlignment::BEGIN][ direction ];
296 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[ horizontalAlignment ][ direction ];
299 const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[ verticalAlignment ];
301 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign );
303 shader.RegisterProperty( "uTextureSize", textureSize );
304 shader.RegisterProperty( "uHorizontalAlign", horizontalAlign );
305 shader.RegisterProperty( "uVerticalAlign", verticalAlign );
306 shader.RegisterProperty( "uGap", wrapGap );
307 mScrollDeltaIndex = shader.RegisterProperty( "uDelta", 0.0f );
309 float scrollAmount = std::max( textureSize.width, controlSize.width );
310 float scrollDuration = scrollAmount / mScrollSpeed;
314 scrollAmount = -scrollAmount; // reverse direction of scrolling
317 StartScrolling( scrollingTextActor, scrollAmount, scrollDuration, remainedLoop );
318 mScrollAnimation.SetCurrentProgress(animationProgress);
321 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
323 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
324 mScrollerInterface.ScrollingFinished();
326 // Revert to the original shader and texture after scrolling
327 mRenderer.SetShader(mShader);
330 mRenderer.SetTextures( mTextureSet );
334 void TextScroller::StartScrolling( Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount )
336 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
338 Shader shader = mRenderer.GetShader();
339 mScrollAnimation = Animation::New( scrollDuration );
340 mScrollAnimation.AnimateTo( Property( shader, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
341 mScrollAnimation.SetEndAction( Animation::DISCARD );
342 mScrollAnimation.SetLoopCount( loopCount );
343 mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
344 mScrollAnimation.Play();
349 } // namespace Toolkit