2 * Copyright (c) 2016 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/devel-api/rendering/geometry.h>
23 #include <dali/devel-api/rendering/renderer.h>
24 #include <dali/devel-api/rendering/sampler.h>
25 #include <dali/devel-api/rendering/shader.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/common/stage.h>
28 #include <dali/public-api/images/frame-buffer-image.h>
29 #include <dali/public-api/render-tasks/render-task-list.h>
32 #include <dali-toolkit/internal/text/text-scroller-interface.h>
43 #if defined ( DEBUG_ENABLED )
44 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
47 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
49 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
50 attribute mediump vec2 aPosition;\n
51 varying highp vec2 vTexCoord;\n
52 varying highp float vRatio;\n
53 uniform mediump mat4 uMvpMatrix;\n
54 uniform mediump vec3 uSize;\n
55 uniform mediump float uDelta;\n
56 uniform mediump vec2 uTextureSize;
57 uniform mediump float uGap;\n
58 uniform mediump float uRtl;\n
63 mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
64 float smallTextPadding = max(uSize.x - uTextureSize.x, 0. );\n
65 float gap = max( uGap, smallTextPadding );\n
66 vTexCoord.x = ( uDelta + ( uRtl * ( uTextureSize.x - uSize.x ) ) + ( aPosition.x * uSize.x ) )/ ( uTextureSize.x+gap );\n
67 vTexCoord.y = aPosition.y;\n
68 vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
69 gl_Position = uMvpMatrix * vertexPosition;\n
74 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
75 varying mediump vec2 vTexCoord;\n
76 varying highp float vRatio;\n
77 uniform sampler2D sTexture;\n
81 mediump vec2 texCoord;\n
82 texCoord.y = vTexCoord.y;\n
83 texCoord.x = fract( vTexCoord.x ) / vRatio;\n
84 if ( texCoord.x > 1.0 )\n
87 gl_FragColor = texture2D( sTexture, texCoord );\n
92 * @brief Create and set up a camera for the render task to use
94 * @param[in] sizeOfTarget size of the source camera to look at
95 * @param[out] offscreenCamera custom camera
97 void CreateCameraActor( const Size& sizeOfTarget, CameraActor& offscreenCamera )
99 offscreenCamera = CameraActor::New();
100 offscreenCamera.SetOrthographicProjection( sizeOfTarget );
101 offscreenCamera.SetInvertYAxis( true );
105 * @brief Create a render task
107 * @param[in] sourceActor actor to be used as source
108 * @param[in] cameraActor camera looking at source
109 * @param[in] offscreenTarget resulting image from render task
110 * @param[out] renderTask render task that has been setup
112 void CreateRenderTask( Actor sourceActor, CameraActor cameraActor , FrameBufferImage offscreenTarget, RenderTask& renderTask )
114 Stage stage = Stage::GetCurrent();
115 RenderTaskList taskList = stage.GetRenderTaskList();
116 renderTask = taskList.CreateTask();
117 renderTask.SetSourceActor( sourceActor );
118 renderTask.SetExclusive( true );
119 renderTask.SetInputEnabled( false );
120 renderTask.SetClearEnabled( true );
121 renderTask.SetCameraActor( cameraActor );
122 renderTask.SetTargetFrameBuffer( offscreenTarget );
123 renderTask.SetClearColor( Color::TRANSPARENT );
124 renderTask.SetCullMode( false );
128 * @brief Create quad geometry for the mesh
130 * @param[out] geometry quad geometry that can be used for a mesh
132 void CreateGeometry( Geometry& geometry )
134 struct QuadVertex { Vector2 position; };
136 QuadVertex quadVertexData[4] =
138 { Vector2( 0.0f, 0.0f) },
139 { Vector2( 1.0f, 0.0f) },
140 { Vector2( 0.0f, 1.0f) },
141 { Vector2( 1.0f, 1.0f) },
144 const unsigned short indices[6] =
149 Property::Map quadVertexFormat;
150 quadVertexFormat["aPosition"] = Property::VECTOR2;
151 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
152 quadVertices.SetData(quadVertexData, 4 );
154 geometry = Geometry::New();
155 geometry.AddVertexBuffer( quadVertices );
156 geometry.SetIndexBuffer( indices, sizeof(indices)/sizeof(indices[0]) );
161 * @brief Create a renderer
163 * @param[in] frameBufferImage texture to be used
164 * @param[out] renderer mesh renderer using the supplied texture
166 void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer )
168 Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::HINT_NONE );
170 Sampler sampler = Sampler::New();
171 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST );
173 TextureSet textureSet = TextureSet::New();
174 textureSet.SetImage( 0u, frameBufferImage );
175 textureSet.SetSampler( 0u, sampler );
177 Geometry meshGeometry;
178 CreateGeometry( meshGeometry );
180 renderer = Renderer::New( meshGeometry, shader );
181 renderer.SetTextures( textureSet );
189 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
191 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
193 TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
197 void TextScroller::SetGap( int gap )
199 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
203 int TextScroller::GetGap() const
208 void TextScroller::SetSpeed( int scrollSpeed )
210 mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
213 int TextScroller::GetSpeed() const
218 void TextScroller::SetLoopCount( int loopCount )
222 mLoopCount = loopCount;
225 if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
227 if ( loopCount == 0 ) // Request to stop looping
229 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount Single loop forced\n" );
230 mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
233 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
236 int TextScroller::GetLoopCount() const
241 Actor TextScroller::GetSourceCamera() const
243 return mOffscreenCameraActor;
246 Actor TextScroller::GetScrollingText() const
248 return mScrollingTextActor;
251 TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ),
252 mScrollDeltaIndex( Property::INVALID_INDEX ),
253 mScrollSpeed( MINIMUM_SCROLL_SPEED ),
257 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
260 TextScroller::~TextScroller()
265 void TextScroller::SetParameters( Actor sourceActor, const Size& controlSize, const Size& offScreenSize, CharacterDirection direction, const Vector2 alignmentOffset )
267 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f,%f]\n",
268 controlSize.x, controlSize.y, offScreenSize.x, offScreenSize.y, direction, alignmentOffset.x, alignmentOffset.y );
270 FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( offScreenSize.width, offScreenSize.height, Pixel::RGBA8888, Dali::Image::UNUSED );
273 CreateCameraActor( offScreenSize, mOffscreenCameraActor );
274 CreateRenderer( offscreenRenderTargetForText, renderer );
275 CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask );
277 // Reposition camera to match alignment of target, RTL text has direction=true
280 mOffscreenCameraActor.SetX( alignmentOffset.x + offScreenSize.width*0.5f );
284 mOffscreenCameraActor.SetX( offScreenSize.width * 0.5f );
287 mOffscreenCameraActor.SetY( offScreenSize.height * 0.5f );
289 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%d]\n", mWrapGap )
291 mScrollingTextActor = Actor::New();
292 mScrollingTextActor.AddRenderer( renderer );
293 mScrollingTextActor.RegisterProperty( "uTextureSize", offScreenSize );
294 mScrollingTextActor.RegisterProperty( "uRtl", ((direction)?1.0f:0.0f) );
295 mScrollingTextActor.RegisterProperty( "uGap", mWrapGap );
296 mScrollingTextActor.SetSize( controlSize.width, std::min( offScreenSize.height, controlSize.height ) );
297 mScrollDeltaIndex = mScrollingTextActor.RegisterProperty( "uDelta", 0.0f );
299 float scrollAmount = std::max( offScreenSize.width + mWrapGap, controlSize.width );
300 float scrollDuration = scrollAmount / mScrollSpeed;
304 scrollAmount = -scrollAmount; // reverse direction of scrollung
307 StartScrolling( scrollAmount, scrollDuration, mLoopCount );
310 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
312 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
314 mScrollerInterface.ScrollingFinished();
317 void TextScroller::StartScrolling( float scrollAmount, float scrollDuration, int loopCount )
319 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
321 mScrollAnimation = Animation::New( scrollDuration );
322 mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount );
323 mScrollAnimation.SetEndAction( Animation::Discard );
324 mScrollAnimation.SetLoopCount( loopCount );
325 mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
326 mScrollAnimation.Play();
329 void TextScroller::CleanUp()
331 if ( Stage::IsInstalled() )
333 Stage stage = Stage::GetCurrent();
334 RenderTaskList taskList = stage.GetRenderTaskList();
335 UnparentAndReset( mScrollingTextActor );
336 UnparentAndReset( mOffscreenCameraActor );
337 taskList.RemoveTask( mRenderTask );
343 } // namespace Toolkit