X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Ftext%2Ftext-scroller.cpp;h=22f115692a3917c74e39d7ef4da4fee290a82e4b;hp=ee1d241f2a9ed606edc6d0a1526594a9dfbf5660;hb=73eaac8af8efb9ac721db3fcf3c71a7a3115282a;hpb=07bdc775bf08ac2f53c8f6a3a8856077a299959b diff --git a/dali-toolkit/internal/text/text-scroller.cpp b/dali-toolkit/internal/text/text-scroller.cpp index ee1d241..22f1156 100644 --- a/dali-toolkit/internal/text/text-scroller.cpp +++ b/dali-toolkit/internal/text/text-scroller.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,19 +19,10 @@ #include // EXTERNAL INCLUDES -#include -#include -#include -#include -#include -#include -#include -#include #include // INTERNAL INCLUDES #include -#include namespace Dali { @@ -39,11 +30,6 @@ namespace Dali namespace Toolkit { -namespace Text -{ -extern const int MINIMUM_SCROLL_SPEED; -} // namespace - namespace { @@ -51,262 +37,313 @@ namespace Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING"); #endif +const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system. + const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER( attribute mediump vec2 aPosition;\n varying highp vec2 vTexCoord;\n - varying highp float vRatio;\n - uniform mediump mat4 uMvpMatrix;\n uniform mediump vec3 uSize;\n uniform mediump float uDelta;\n - uniform mediump vec2 uTextureSize; + uniform mediump vec2 uTextureSize;\n uniform mediump float uGap;\n - uniform mediump float uRtl;\n + uniform mediump float uHorizontalAlign;\n + uniform mediump float uVerticalAlign;\n + \n + uniform highp mat4 uMvpMatrix;\n \n + //Visual size and offset + uniform mediump vec2 offset;\n + uniform mediump vec2 size;\n + uniform mediump vec4 offsetSizeMode;\n + uniform mediump vec2 origin;\n + uniform mediump vec2 anchorPoint;\n + void main()\n {\n - {\n - mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n - float smallTextPadding = max(uSize.x - uTextureSize.x, 0. );\n - float gap = max( uGap, smallTextPadding );\n - vTexCoord.x = ( uDelta + ( uRtl * ( uTextureSize.x - uSize.x ) ) + ( aPosition.x * uSize.x ) )/ ( uTextureSize.x+gap );\n - vTexCoord.y = aPosition.y;\n - vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n - gl_Position = uMvpMatrix * vertexPosition;\n - }\n + mediump vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy );\n + mediump vec2 visualSize = mix( uSize.xy * size, size, offsetSizeMode.zw );\n + \n + vTexCoord.x = ( uDelta + uHorizontalAlign * ( uTextureSize.x - visualSize.x - uGap ) + floor( aPosition.x * visualSize.x ) + 0.5 - uGap * 0.5 ) / uTextureSize.x + 0.5;\n + vTexCoord.y = ( uVerticalAlign * ( uTextureSize.y - visualSize.y ) + floor( aPosition.y * visualSize.y ) + 0.5 ) / ( uTextureSize.y ) + 0.5;\n + \n + mediump vec4 vertexPosition = vec4( floor( ( aPosition + anchorPoint ) * visualSize + ( visualOffset + origin ) * uSize.xy ), 0.0, 1.0 );\n + \n + gl_Position = uMvpMatrix * vertexPosition;\n }\n ); const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER( - varying mediump vec2 vTexCoord;\n - varying highp float vRatio;\n + varying highp vec2 vTexCoord;\n uniform sampler2D sTexture;\n + uniform lowp vec4 uColor;\n + uniform lowp vec3 mixColor;\n \n void main()\n {\n - mediump vec2 texCoord;\n - texCoord.y = vTexCoord.y;\n - texCoord.x = fract( vTexCoord.x ) / vRatio;\n - if ( texCoord.x > 1.0 )\n + if ( vTexCoord.y > 1.0 )\n discard;\n \n - gl_FragColor = texture2D( sTexture, texCoord );\n + mediump vec4 textTexture = texture2D( sTexture, vTexCoord );\n + \n + gl_FragColor = textTexture * uColor * vec4( mixColor, 1.0 ); }\n ); /** - * @brief Create and set up a camera for the render task to use + * @brief How the text should be aligned horizontally when scrolling the text. * - * @param[in] sizeOfTarget size of the source camera to look at - * @param[out] offscreenCamera custom camera + * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right. + * The final alignment depends on two factors: + * 1) The alignment value of the text label (Use Text::HorizontalAlignment enumerations). + * 2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL). */ -void CreateCameraActor( const Size& sizeOfTarget, CameraActor& offscreenCamera ) +const float HORIZONTAL_ALIGNMENT_TABLE[ Text::HorizontalAlignment::END+1 ][ 2 ] = { - offscreenCamera = CameraActor::New(); - offscreenCamera.SetOrthographicProjection( sizeOfTarget ); - offscreenCamera.SetInvertYAxis( true ); -} + // HorizontalAlignment::BEGIN + { + -0.5f, // LTR + 0.5f // RTL + }, -/** - * @brief Create a render task - * - * @param[in] sourceActor actor to be used as source - * @param[in] cameraActor camera looking at source - * @param[in] offscreenTarget resulting image from render task - * @param[out] renderTask render task that has been setup - */ -void CreateRenderTask( Actor sourceActor, CameraActor cameraActor , FrameBufferImage offscreenTarget, RenderTask& renderTask ) -{ - Stage stage = Stage::GetCurrent(); - RenderTaskList taskList = stage.GetRenderTaskList(); - renderTask = taskList.CreateTask(); - renderTask.SetSourceActor( sourceActor ); - renderTask.SetExclusive( true ); - renderTask.SetInputEnabled( false ); - renderTask.SetClearEnabled( true ); - renderTask.SetCameraActor( cameraActor ); - renderTask.SetTargetFrameBuffer( offscreenTarget ); - renderTask.SetClearColor( Color::TRANSPARENT ); - renderTask.SetCullMode( false ); -} + // HorizontalAlignment::CENTER + { + 0.0f, // LTR + 0.0f // RTL + }, + + // HorizontalAlignment::END + { + 0.5f, // LTR + -0.5f // RTL + } +}; /** - * @brief Create quad geometry for the mesh + * @brief How the text should be aligned vertically when scrolling the text. * - * @param[out] geometry quad geometry that can be used for a mesh + * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom. + * The alignment depends on the alignment value of the text label (Use Text::VerticalAlignment enumerations). */ -void CreateGeometry( Geometry& geometry ) +const float VERTICAL_ALIGNMENT_TABLE[ Text::VerticalAlignment::BOTTOM+1 ] = { - struct QuadVertex { Vector2 position; }; + -0.5f, // VerticalAlignment::TOP + 0.0f, // VerticalAlignment::CENTER + 0.5f // VerticalAlignment::BOTTOM +}; - QuadVertex quadVertexData[4] = - { - { Vector2( 0.0f, 0.0f) }, - { Vector2( 1.0f, 0.0f) }, - { Vector2( 0.0f, 1.0f) }, - { Vector2( 1.0f, 1.0f) }, - }; +} // namespace - const unsigned short indices[6] = - { - 3,1,0,0,2,3 - }; +namespace Text +{ - Property::Map quadVertexFormat; - quadVertexFormat["aPosition"] = Property::VECTOR2; - PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat ); - quadVertices.SetData(quadVertexData, 4 ); +TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface ) +{ + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" ); - geometry = Geometry::New(); - geometry.AddVertexBuffer( quadVertices ); - geometry.SetIndexBuffer( indices, sizeof(indices)/sizeof(indices[0]) ); + TextScrollerPtr textScroller( new TextScroller( scrollerInterface) ); + return textScroller; } +void TextScroller::SetGap( int gap ) +{ + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap ); + mWrapGap = static_cast(gap); +} -/** - * @brief Create a renderer - * - * @param[in] frameBufferImage texture to be used - * @param[out] renderer mesh renderer using the supplied texture - */ -void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer ) +int TextScroller::GetGap() const { - Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::Hint::NONE ); + return static_cast(mWrapGap); +} - Sampler sampler = Sampler::New(); - sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST ); +void TextScroller::SetSpeed( int scrollSpeed ) +{ + mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed ); +} - TextureSet textureSet = TextureSet::New(); - TextureSetImage( textureSet, 0u, frameBufferImage ); - textureSet.SetSampler( 0u, sampler ); +int TextScroller::GetSpeed() const +{ + return mScrollSpeed; +} - Geometry meshGeometry; - CreateGeometry( meshGeometry ); +void TextScroller::SetLoopCount( int loopCount ) +{ + if ( loopCount >= 0 ) + { + mLoopCount = loopCount; + } - renderer = Renderer::New( meshGeometry, shader ); - renderer.SetTextures( textureSet ); + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" ); } -} // namespace +int TextScroller::GetLoopCount() const +{ + return mLoopCount; +} -namespace Text +void TextScroller::SetLoopDelay( float delay ) { + mLoopDelay = delay; +} -TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface ) +float TextScroller::GetLoopDelay() const { - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" ); + return mLoopDelay; +} - TextScrollerPtr textScroller( new TextScroller( scrollerInterface) ); - return textScroller; +void TextScroller::SetStopMode( TextLabel::AutoScrollStopMode::Type stopMode ) +{ + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == TextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" ); + mStopMode = stopMode; } -Actor TextScroller::GetSourceCamera() const +void TextScroller::StopScrolling() { - return mOffscreenCameraActor; + if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING ) + { + switch( mStopMode ) + { + case TextLabel::AutoScrollStopMode::IMMEDIATE: + { + mScrollAnimation.Stop(); + mScrollerInterface.ScrollingFinished(); + break; + } + case TextLabel::AutoScrollStopMode::FINISH_LOOP: + { + mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way + break; + } + default: + { + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" ); + } + } + } + else + { + mScrollerInterface.ScrollingFinished(); + } } -Actor TextScroller::GetScrollingText() const +TextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const { - return mScrollingTextActor; + return mStopMode; } TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ), - mScrollDeltaIndex( Property::INVALID_INDEX ) + mScrollDeltaIndex( Property::INVALID_INDEX ), + mScrollSpeed( MINIMUM_SCROLL_SPEED ), + mLoopCount( 1 ), + mLoopDelay( 0.0f ), + mWrapGap( 0.0f ), + mStopMode( TextLabel::AutoScrollStopMode::FINISH_LOOP ) { DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" ); } TextScroller::~TextScroller() { - CleanUp(); } -void TextScroller::StartScrolling( Actor sourceActor, - const ScrollerData& data ) +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 ) { - DALI_LOG_INFO( gLogFilter, - Debug::Verbose, - "TextScroller::StartScrolling controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f]\n", - data.mControlSize.x, data.mControlSize.y, - data.mOffscreenSize.x, data.mOffscreenSize.y, - data.mAutoScrollDirectionRTL, - data.mAlignmentOffset ); - - FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( data.mOffscreenSize.width, data.mOffscreenSize.height, Pixel::RGBA8888 ); - Renderer renderer; - - CreateCameraActor( data.mOffscreenSize, mOffscreenCameraActor ); - CreateRenderer( offscreenRenderTargetForText, renderer ); - CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask ); - - // Reposition camera to match alignment of target, RTL text has direction=true - if( data.mAutoScrollDirectionRTL ) - { - mOffscreenCameraActor.SetX( data.mAlignmentOffset + data.mOffscreenSize.width * 0.5f ); - } - else + 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 ); + + mRenderer = renderer; + + float animationProgress = 0.0f; + int remainedLoop = mLoopCount; + if ( mScrollAnimation ) { - mOffscreenCameraActor.SetX( data.mOffscreenSize.width * 0.5f ); + if( mScrollAnimation.GetState() == Animation::PLAYING ) + { + animationProgress = mScrollAnimation.GetCurrentProgress(); + + if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop + { + remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() ); + remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop ); + } + } + mScrollAnimation.Clear(); + + // Reset to the original shader and texture before scrolling + mRenderer.SetShader(mShader); + mRenderer.SetTextures( mTextureSet ); } - mOffscreenCameraActor.SetY( data.mOffscreenSize.height * 0.5f ); + mShader = mRenderer.GetShader(); + mTextureSet = mRenderer.GetTextures(); - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", data.mWrapGap ) + // Set the shader and texture for scrolling + Shader shader = Shader::New( VERTEX_SHADER_SCROLL, FRAGMENT_SHADER, Shader::Hint::NONE ); + mRenderer.SetShader( shader ); + mRenderer.SetTextures( textureSet ); - mScrollingTextActor = Actor::New(); - mScrollingTextActor.AddRenderer( renderer ); - mScrollingTextActor.RegisterProperty( "uTextureSize", data.mOffscreenSize ); - mScrollingTextActor.RegisterProperty( "uRtl", ( data.mAutoScrollDirectionRTL ? 1.f : 0.f ) ); - mScrollingTextActor.RegisterProperty( "uGap", data.mWrapGap ); - mScrollingTextActor.SetSize( data.mControlSize.width, std::min( data.mOffscreenSize.height, data.mControlSize.height ) ); - mScrollDeltaIndex = mScrollingTextActor.RegisterProperty( "uDelta", 0.0f ); + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters wrapGap[%f]\n", wrapGap ); - float scrollAmount = std::max( data.mOffscreenSize.width + data.mWrapGap, data.mControlSize.width ); - float scrollSpeed = std::max( MINIMUM_SCROLL_SPEED, data.mScrollSpeed ); - float scrollDuration = scrollAmount / scrollSpeed; + float horizontalAlign; - if( data.mAutoScrollDirectionRTL ) + if( textureSize.x > controlSize.x ) { - scrollAmount = -scrollAmount; // reverse direction of scrollung + // if Text is elided, scroll should start at the begin of text. + horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[HorizontalAlignment::BEGIN][ direction ]; + } + else + { + horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[ horizontalAlignment ][ direction ]; } - mScrollAnimation = Animation::New( scrollDuration ); - mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount ); - mScrollAnimation.SetEndAction( Animation::Discard ); - mScrollAnimation.SetLoopCount( data.mLoopCount ); - mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished ); - mScrollAnimation.Play(); -} + const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[ verticalAlignment ]; -void TextScroller::StopScrolling() -{ - if( mScrollAnimation && - ( mScrollAnimation.GetState() == Animation::PLAYING ) ) + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign ); + + shader.RegisterProperty( "uTextureSize", textureSize ); + shader.RegisterProperty( "uHorizontalAlign", horizontalAlign ); + shader.RegisterProperty( "uVerticalAlign", verticalAlign ); + shader.RegisterProperty( "uGap", wrapGap ); + mScrollDeltaIndex = shader.RegisterProperty( "uDelta", 0.0f ); + + float scrollAmount = std::max( textureSize.width, controlSize.width ); + float scrollDuration = scrollAmount / mScrollSpeed; + + if ( direction ) { - DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount Single loop forced\n" ); - mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way + scrollAmount = -scrollAmount; // reverse direction of scrolling } + + StartScrolling( scrollingTextActor, scrollAmount, scrollDuration, remainedLoop ); + mScrollAnimation.SetCurrentProgress(animationProgress); } void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation ) { DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" ); - CleanUp(); mScrollerInterface.ScrollingFinished(); -} -void TextScroller::CleanUp() -{ - if ( Stage::IsInstalled() ) + // Revert to the original shader and texture after scrolling + mRenderer.SetShader(mShader); + if ( mTextureSet ) { - Stage stage = Stage::GetCurrent(); - RenderTaskList taskList = stage.GetRenderTaskList(); - UnparentAndReset( mScrollingTextActor ); - UnparentAndReset( mOffscreenCameraActor ); - taskList.RemoveTask( mRenderTask ); + mRenderer.SetTextures( mTextureSet ); } } +void TextScroller::StartScrolling( Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount ) +{ + DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed ); + + Shader shader = mRenderer.GetShader(); + mScrollAnimation = Animation::New( scrollDuration ); + mScrollAnimation.AnimateTo( Property( shader, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) ); + mScrollAnimation.SetEndAction( Animation::DISCARD ); + mScrollAnimation.SetLoopCount( loopCount ); + mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished ); + mScrollAnimation.Play(); +} + } // namespace Text } // namespace Toolkit