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 uniform mediump vec3 uSize;\n
47 uniform mediump float uDelta;\n
48 uniform mediump vec2 uTextureSize;\n
49 uniform mediump float uGap;\n
50 uniform mediump float uHorizontalAlign;\n
51 uniform mediump float uVerticalAlign;\n
53 uniform mediump mat4 uModelMatrix;\n
54 uniform mediump mat4 uViewMatrix;\n
55 uniform mediump mat4 uProjection;\n
57 //Visual size and offset
58 uniform mediump vec2 offset;\n
59 uniform mediump vec2 size;\n
60 uniform mediump vec4 offsetSizeMode;\n
61 uniform mediump vec2 origin;\n
62 uniform mediump vec2 anchorPoint;\n
66 mediump vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy );\n
67 mediump vec2 visualSize = mix( uSize.xy * size, size, offsetSizeMode.zw );\n
69 vTexCoord.x = ( uDelta + uHorizontalAlign * ( uTextureSize.x - visualSize.x - uGap ) + floor( aPosition.x * visualSize.x ) + 0.5 - uGap * 0.5 ) / uTextureSize.x + 0.5;\n
70 vTexCoord.y = ( uVerticalAlign * ( uTextureSize.y - visualSize.y ) + floor( aPosition.y * visualSize.y ) + 0.5 ) / ( uTextureSize.y ) + 0.5;\n
72 mediump vec4 vertexPosition = vec4( floor( ( aPosition + anchorPoint ) * visualSize + ( visualOffset + origin ) * uSize.xy ), 0.0, 1.0 );\n
74 gl_Position = uProjection * uViewMatrix * uModelMatrix * vertexPosition;\n
78 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
79 varying highp vec2 vTexCoord;\n
80 uniform sampler2D sTexture;\n
81 uniform lowp vec4 uColor;\n
82 uniform lowp vec3 mixColor;\n
83 uniform lowp float opacity;\n
84 uniform lowp float preMultipliedAlpha;\n
86 lowp vec4 visualMixColor()\n
88 return vec4( mixColor * mix( 1.0, opacity, preMultipliedAlpha ), opacity );\n
93 if ( vTexCoord.y > 1.0 )\n
96 mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n
97 textTexture.rgb *= mix( 1.0, textTexture.a, preMultipliedAlpha );\n
99 gl_FragColor = textTexture * uColor * visualMixColor();
104 * @brief How the text should be aligned horizontally when scrolling the text.
106 * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right.
107 * The final alignment depends on two factors:
108 * 1) The alignment value of the text label (Use Text::HorizontalAlignment enumerations).
109 * 2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
111 const float HORIZONTAL_ALIGNMENT_TABLE[ Text::HorizontalAlignment::END+1 ][ 2 ] =
113 // HorizontalAlignment::BEGIN
119 // HorizontalAlignment::CENTER
125 // HorizontalAlignment::END
133 * @brief How the text should be aligned vertically when scrolling the text.
135 * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom.
136 * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations).
138 const float VERTICAL_ALIGNMENT_TABLE[ Text::VerticalAlignment::BOTTOM+1 ] =
140 -0.5f, // VerticalAlignment::TOP
141 0.0f, // VerticalAlignment::CENTER
142 0.5f // VerticalAlignment::BOTTOM
150 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
152 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
154 TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
158 void TextScroller::SetGap( int gap )
160 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
161 mWrapGap = static_cast<float>(gap);
164 int TextScroller::GetGap() const
166 return static_cast<int>(mWrapGap);
169 void TextScroller::SetSpeed( int scrollSpeed )
171 mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
174 int TextScroller::GetSpeed() const
179 void TextScroller::SetLoopCount( int loopCount )
181 if ( loopCount >= 0 )
183 mLoopCount = loopCount;
186 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
189 int TextScroller::GetLoopCount() const
194 void TextScroller::SetLoopDelay( float delay )
199 float TextScroller::GetLoopDelay() const
204 void TextScroller::SetStopMode( TextLabel::AutoScrollStopMode::Type stopMode )
206 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == TextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
207 mStopMode = stopMode;
210 void TextScroller::StopScrolling()
212 if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
216 case TextLabel::AutoScrollStopMode::IMMEDIATE:
218 mScrollAnimation.Stop();
219 mScrollerInterface.ScrollingFinished();
222 case TextLabel::AutoScrollStopMode::FINISH_LOOP:
224 mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
229 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
235 mScrollerInterface.ScrollingFinished();
239 TextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
244 TextScroller::TextScroller( ScrollerInterface& scrollerInterface )
245 : mScrollerInterface( scrollerInterface ),
246 mScrollDeltaIndex( Property::INVALID_INDEX ),
247 mScrollSpeed( MINIMUM_SCROLL_SPEED ),
251 mStopMode( TextLabel::AutoScrollStopMode::FINISH_LOOP )
253 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
256 TextScroller::~TextScroller()
260 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 )
262 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] textureSize[%f,%f] direction[%d]\n",
263 controlSize.x, controlSize.y, textureSize.x, textureSize.y, direction );
265 mRenderer = renderer;
267 float animationProgress = 0.0f;
268 int remainedLoop = mLoopCount;
269 if ( mScrollAnimation )
271 if( mScrollAnimation.GetState() == Animation::PLAYING )
273 animationProgress = mScrollAnimation.GetCurrentProgress();
275 if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop
277 remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() );
278 remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop );
281 mScrollAnimation.Clear();
283 // Reset to the original shader and texture before scrolling
284 mRenderer.SetShader(mShader);
285 mRenderer.SetTextures( mTextureSet );
288 mShader = mRenderer.GetShader();
289 mTextureSet = mRenderer.GetTextures();
291 // Set the shader and texture for scrolling
292 Shader shader = Shader::New( VERTEX_SHADER_SCROLL, FRAGMENT_SHADER, Shader::Hint::NONE );
293 mRenderer.SetShader( shader );
294 mRenderer.SetTextures( textureSet );
296 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters wrapGap[%f]\n", wrapGap );
298 float horizontalAlign;
300 if( textureSize.x > controlSize.x )
302 // if Text is elided, scroll should start at the begin of text.
303 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[HorizontalAlignment::BEGIN][ direction ];
307 horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[ horizontalAlignment ][ direction ];
310 const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[ verticalAlignment ];
312 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign );
314 scrollingTextActor.RegisterProperty( "uTextureSize", textureSize );
315 scrollingTextActor.RegisterProperty( "uHorizontalAlign", horizontalAlign );
316 scrollingTextActor.RegisterProperty( "uVerticalAlign", verticalAlign );
317 scrollingTextActor.RegisterProperty( "uGap", wrapGap );
318 mScrollDeltaIndex = scrollingTextActor.RegisterProperty( "uDelta", 0.0f );
320 float scrollAmount = std::max( textureSize.width, controlSize.width );
321 float scrollDuration = scrollAmount / mScrollSpeed;
325 scrollAmount = -scrollAmount; // reverse direction of scrolling
328 StartScrolling( scrollingTextActor, scrollAmount, scrollDuration, remainedLoop );
329 mScrollAnimation.SetCurrentProgress(animationProgress);
332 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
334 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
335 mScrollerInterface.ScrollingFinished();
337 // Revert to the original shader and texture after scrolling
338 mRenderer.SetShader(mShader);
341 mRenderer.SetTextures( mTextureSet );
345 void TextScroller::StartScrolling( Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount )
347 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
349 mScrollAnimation = Animation::New( scrollDuration );
350 mScrollAnimation.AnimateTo( Property( scrollingTextActor, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
351 mScrollAnimation.SetEndAction( Animation::Discard );
352 mScrollAnimation.SetLoopCount( loopCount );
353 mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
354 mScrollAnimation.Play();
359 } // namespace Toolkit