2 * Copyright (c) 2017 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/public-api/common/stage.h>
23 #include <dali/integration-api/debug.h>
26 #include <dali-toolkit/internal/text/text-scroller-interface.h>
37 #if defined ( DEBUG_ENABLED )
38 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
41 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
43 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
44 attribute mediump vec2 aPosition;\n
45 varying highp vec2 vTexCoord;\n
46 varying highp float vRatio;\n
47 uniform mediump mat4 uMvpMatrix;\n
48 uniform mediump vec3 uSize;\n
49 uniform mediump float uDelta;\n
50 uniform mediump vec2 uTextureSize;\n
51 uniform mediump float uGap;\n
52 uniform mediump float uHorizontalAlign;\n
53 uniform mediump float uVerticalAlign;\n
58 float smallTextPadding = max( uSize.x - uTextureSize.x, 0. );\n
59 float gap = max( uGap, smallTextPadding );\n
60 float delta = floor ( uDelta ) + 0.5;\n
61 vTexCoord.x = ( delta + uHorizontalAlign * ( uTextureSize.x - uSize.x ) + floor( aPosition.x * uSize.x ) + 0.5 - gap * 0.5 ) / (uTextureSize.x + gap) + 0.5;\n
62 vTexCoord.y = ( uVerticalAlign * ( uTextureSize.y - uSize.y ) + floor( aPosition.y * uSize.y ) + 0.5 ) / ( uTextureSize.y ) + 0.5;\n
63 vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
64 gl_Position = uMvpMatrix * vec4( floor( aPosition * uSize.xy ), 0.0, 1.0 );\n
69 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
70 varying highp vec2 vTexCoord;\n
71 varying highp float vRatio;\n
72 uniform sampler2D sTexture;\n
76 highp vec2 texCoord;\n
77 texCoord.y = vTexCoord.y;\n
78 texCoord.x = fract( vTexCoord.x ) / vRatio;\n
79 if ( texCoord.x > 1.0 || texCoord.y > 1.0 )\n
82 gl_FragColor = texture2D( sTexture, texCoord );\n
87 * @brief How the text should be aligned horizontally when scrolling the text.
89 * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right.
90 * The final alignment depends on two factors:
91 * 1) The alignment value of the text label (Use Text::Layout::HorizontalAlignment enumerations).
92 * 2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
94 const float HORIZONTAL_ALIGNMENT_TABLE[ Text::Layout::HORIZONTAL_ALIGN_COUNT ][ 2 ] =
96 // HORIZONTAL_ALIGN_BEGIN
102 // HORIZONTAL_ALIGN_CENTER
108 // HORIZONTAL_ALIGN_END
116 * @brief How the text should be aligned vertically when scrolling the text.
118 * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom.
119 * The alignment depends on the alignment value of the text label (Use Text::Layout::VerticalAlignment enumerations).
121 const float VERTICAL_ALIGNMENT_TABLE[ Text::Layout::VERTICAL_ALIGN_COUNT ] =
123 -0.5f, // VERTICAL_ALIGN_TOP
124 0.0f, // VERTICAL_ALIGN_CENTER
125 0.5f // VERTICAL_ALIGN_BOTTOM
133 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
135 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
137 TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
141 void TextScroller::SetGap( int gap )
143 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
144 mWrapGap = static_cast<float>(gap);
147 int TextScroller::GetGap() const
149 return static_cast<int>(mWrapGap);
152 void TextScroller::SetSpeed( int scrollSpeed )
154 mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
157 int TextScroller::GetSpeed() const
162 void TextScroller::SetLoopCount( int loopCount )
164 if ( loopCount >= 0 )
166 mLoopCount = loopCount;
169 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
172 int TextScroller::GetLoopCount() const
177 void TextScroller::SetLoopDelay( float delay )
182 float TextScroller::GetLoopDelay() const
187 void TextScroller::SetStopMode( DevelTextLabel::AutoScrollStopMode::Type stopMode )
189 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == DevelTextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
190 mStopMode = stopMode;
193 void TextScroller::StopScrolling()
195 if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
199 case DevelTextLabel::AutoScrollStopMode::IMMEDIATE:
201 mScrollAnimation.Stop();
202 mScrollerInterface.ScrollingFinished();
205 case DevelTextLabel::AutoScrollStopMode::FINISH_LOOP:
207 mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
212 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
218 DevelTextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
223 TextScroller::TextScroller( ScrollerInterface& scrollerInterface )
224 : mScrollerInterface( scrollerInterface ),
225 mScrollDeltaIndex( Property::INVALID_INDEX ),
226 mScrollSpeed( MINIMUM_SCROLL_SPEED ),
230 mStopMode( DevelTextLabel::AutoScrollStopMode::FINISH_LOOP )
232 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
235 TextScroller::~TextScroller()
239 void TextScroller::SetParameters( Actor scrollingTextActor, Renderer renderer, TextureSet textureSet, const Size& controlSize, const Size& textNaturalSize, CharacterDirection direction, Layout::HorizontalAlignment horizontalAlignment, Layout::VerticalAlignment verticalAlignment )
241 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d]\n",
242 controlSize.x, controlSize.y, textNaturalSize.x, textNaturalSize.y, direction );
244 mRenderer = renderer;
246 float animationProgress = 0.0f;
247 int remainedLoop = mLoopCount;
248 if ( mScrollAnimation )
250 if( mScrollAnimation.GetState() == Animation::PLAYING )
252 animationProgress = mScrollAnimation.GetCurrentProgress();
254 if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop
256 remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() );
257 remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop );
260 mScrollAnimation.Clear();
262 // Reset to the original shader and texture before scrolling
263 mRenderer.SetShader(mShader);
264 mRenderer.SetTextures( mTextureSet );
267 mShader = mRenderer.GetShader();
268 mTextureSet = mRenderer.GetTextures();
270 // Set the shader and texture for scrolling
271 Shader shader = Shader::New( VERTEX_SHADER_SCROLL, FRAGMENT_SHADER, Shader::Hint::NONE );
272 mRenderer.SetShader( shader );
273 mRenderer.SetTextures( textureSet );
275 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap );
277 const float horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[ horizontalAlignment ][ direction ];
278 const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[ verticalAlignment ];
279 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign );
281 scrollingTextActor.RegisterProperty( "uTextureSize", textNaturalSize );
282 scrollingTextActor.RegisterProperty( "uHorizontalAlign", horizontalAlign );
283 scrollingTextActor.RegisterProperty( "uVerticalAlign", verticalAlign );
284 scrollingTextActor.RegisterProperty( "uGap", mWrapGap );
285 mScrollDeltaIndex = scrollingTextActor.RegisterProperty( "uDelta", 0.0f );
287 float scrollAmount = std::max( textNaturalSize.width + mWrapGap, controlSize.width );
288 float scrollDuration = scrollAmount / mScrollSpeed;
292 scrollAmount = -scrollAmount; // reverse direction of scrolling
295 StartScrolling( scrollingTextActor, scrollAmount, scrollDuration, remainedLoop );
296 mScrollAnimation.SetCurrentProgress(animationProgress);
299 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
301 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
302 mScrollerInterface.ScrollingFinished();
304 // Revert to the original shader and texture after scrolling
305 mRenderer.SetShader(mShader);
308 mRenderer.SetTextures( mTextureSet );
312 void TextScroller::StartScrolling( Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount )
314 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
316 mScrollAnimation = Animation::New( scrollDuration );
317 mScrollAnimation.AnimateTo( Property( scrollingTextActor, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
318 mScrollAnimation.SetEndAction( Animation::Discard );
319 mScrollAnimation.SetLoopCount( loopCount );
320 mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
321 mScrollAnimation.Play();
326 } // namespace Toolkit