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/public-api/common/stage.h>
23 #include <dali/public-api/images/frame-buffer-image.h>
24 #include <dali/public-api/render-tasks/render-task-list.h>
25 #include <dali/public-api/rendering/geometry.h>
26 #include <dali/public-api/rendering/renderer.h>
27 #include <dali/public-api/rendering/sampler.h>
28 #include <dali/public-api/rendering/shader.h>
29 #include <dali/devel-api/images/texture-set-image.h>
30 #include <dali/integration-api/debug.h>
33 #include <dali-toolkit/internal/text/text-scroller-interface.h>
44 #if defined ( DEBUG_ENABLED )
45 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
48 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
50 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
51 attribute mediump vec2 aPosition;\n
52 varying highp vec2 vTexCoord;\n
53 varying highp float vRatio;\n
54 uniform mediump mat4 uMvpMatrix;\n
55 uniform mediump vec3 uSize;\n
56 uniform mediump float uDelta;\n
57 uniform mediump vec2 uTextureSize;
58 uniform mediump float uGap;\n
59 uniform mediump float uRtl;\n
64 mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
65 float smallTextPadding = max(uSize.x - uTextureSize.x, 0. );\n
66 float gap = max( uGap, smallTextPadding );\n
67 vTexCoord.x = ( uDelta + ( uRtl * ( uTextureSize.x - uSize.x ) ) + ( aPosition.x * uSize.x ) )/ ( uTextureSize.x+gap );\n
68 vTexCoord.y = aPosition.y;\n
69 vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
70 gl_Position = uMvpMatrix * vertexPosition;\n
75 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
76 varying mediump vec2 vTexCoord;\n
77 varying highp float vRatio;\n
78 uniform sampler2D sTexture;\n
82 mediump vec2 texCoord;\n
83 texCoord.y = vTexCoord.y;\n
84 texCoord.x = fract( vTexCoord.x ) / vRatio;\n
85 if ( texCoord.x > 1.0 )\n
88 gl_FragColor = texture2D( sTexture, texCoord );\n
93 * @brief Create and set up a camera for the render task to use
95 * @param[in] sizeOfTarget size of the source camera to look at
96 * @param[out] offscreenCamera custom camera
98 void CreateCameraActor( const Size& sizeOfTarget, CameraActor& offscreenCamera )
100 offscreenCamera = CameraActor::New();
101 offscreenCamera.SetOrthographicProjection( sizeOfTarget );
102 offscreenCamera.SetInvertYAxis( true );
106 * @brief Create a render task
108 * @param[in] sourceActor actor to be used as source
109 * @param[in] cameraActor camera looking at source
110 * @param[in] offscreenTarget resulting image from render task
111 * @param[out] renderTask render task that has been setup
113 void CreateRenderTask( Actor sourceActor, CameraActor cameraActor , FrameBufferImage offscreenTarget, RenderTask& renderTask )
115 Stage stage = Stage::GetCurrent();
116 RenderTaskList taskList = stage.GetRenderTaskList();
117 renderTask = taskList.CreateTask();
118 renderTask.SetSourceActor( sourceActor );
119 renderTask.SetExclusive( true );
120 renderTask.SetInputEnabled( false );
121 renderTask.SetClearEnabled( true );
122 renderTask.SetCameraActor( cameraActor );
123 renderTask.SetTargetFrameBuffer( offscreenTarget );
124 renderTask.SetClearColor( Color::TRANSPARENT );
125 renderTask.SetCullMode( false );
129 * @brief Create quad geometry for the mesh
131 * @param[out] geometry quad geometry that can be used for a mesh
133 void CreateGeometry( Geometry& geometry )
135 struct QuadVertex { Vector2 position; };
137 QuadVertex quadVertexData[4] =
139 { Vector2( 0.0f, 0.0f) },
140 { Vector2( 1.0f, 0.0f) },
141 { Vector2( 0.0f, 1.0f) },
142 { Vector2( 1.0f, 1.0f) },
145 const unsigned short indices[6] =
150 Property::Map quadVertexFormat;
151 quadVertexFormat["aPosition"] = Property::VECTOR2;
152 PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
153 quadVertices.SetData(quadVertexData, 4 );
155 geometry = Geometry::New();
156 geometry.AddVertexBuffer( quadVertices );
157 geometry.SetIndexBuffer( indices, sizeof(indices)/sizeof(indices[0]) );
162 * @brief Create a renderer
164 * @param[in] frameBufferImage texture to be used
165 * @param[out] renderer mesh renderer using the supplied texture
167 void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer )
169 Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::Hint::NONE );
171 Sampler sampler = Sampler::New();
172 sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST );
174 TextureSet textureSet = TextureSet::New();
175 TextureSetImage( textureSet, 0u, frameBufferImage );
176 textureSet.SetSampler( 0u, sampler );
178 Geometry meshGeometry;
179 CreateGeometry( meshGeometry );
181 renderer = Renderer::New( meshGeometry, shader );
182 renderer.SetTextures( textureSet );
190 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
192 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
194 TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
198 void TextScroller::SetGap( int gap )
200 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
201 mWrapGap = static_cast<float>(gap);
204 int TextScroller::GetGap() const
206 return static_cast<int>(mWrapGap);
209 void TextScroller::SetSpeed( int scrollSpeed )
211 mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
214 int TextScroller::GetSpeed() const
219 void TextScroller::SetLoopCount( int loopCount )
223 mLoopCount = loopCount;
226 if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
228 if ( loopCount == 0 ) // Request to stop looping
230 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount Single loop forced\n" );
231 mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
234 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
237 int TextScroller::GetLoopCount() const
242 Actor TextScroller::GetSourceCamera() const
244 return mOffscreenCameraActor;
247 Actor TextScroller::GetScrollingText() const
249 return mScrollingTextActor;
252 TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ),
253 mScrollDeltaIndex( Property::INVALID_INDEX ),
254 mScrollSpeed( MINIMUM_SCROLL_SPEED ),
258 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
261 TextScroller::~TextScroller()
266 void TextScroller::SetParameters( Actor sourceActor, const Size& controlSize, const Size& offScreenSize, CharacterDirection direction, float alignmentOffset )
268 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f]\n",
269 controlSize.x, controlSize.y, offScreenSize.x, offScreenSize.y, direction, alignmentOffset );
271 FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( offScreenSize.width, offScreenSize.height, Pixel::RGBA8888, Dali::Image::UNUSED );
274 CreateCameraActor( offScreenSize, mOffscreenCameraActor );
275 CreateRenderer( offscreenRenderTargetForText, renderer );
276 CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask );
278 // Reposition camera to match alignment of target, RTL text has direction=true
281 mOffscreenCameraActor.SetX( alignmentOffset + offScreenSize.width*0.5f );
285 mOffscreenCameraActor.SetX( offScreenSize.width * 0.5f );
288 mOffscreenCameraActor.SetY( offScreenSize.height * 0.5f );
290 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap )
292 mScrollingTextActor = Actor::New();
293 mScrollingTextActor.AddRenderer( renderer );
294 mScrollingTextActor.RegisterProperty( "uTextureSize", offScreenSize );
295 mScrollingTextActor.RegisterProperty( "uRtl", ((direction)?1.0f:0.0f) );
296 mScrollingTextActor.RegisterProperty( "uGap", mWrapGap );
297 mScrollingTextActor.SetSize( controlSize.width, std::min( offScreenSize.height, controlSize.height ) );
298 mScrollDeltaIndex = mScrollingTextActor.RegisterProperty( "uDelta", 0.0f );
300 float scrollAmount = std::max( offScreenSize.width + mWrapGap, controlSize.width );
301 float scrollDuration = scrollAmount / mScrollSpeed;
305 scrollAmount = -scrollAmount; // reverse direction of scrollung
308 StartScrolling( scrollAmount, scrollDuration, mLoopCount );
311 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
313 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
315 mScrollerInterface.ScrollingFinished();
318 void TextScroller::StartScrolling( float scrollAmount, float scrollDuration, int loopCount )
320 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
322 mScrollAnimation = Animation::New( scrollDuration );
323 mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount );
324 mScrollAnimation.SetEndAction( Animation::Discard );
325 mScrollAnimation.SetLoopCount( loopCount );
326 mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
327 mScrollAnimation.Play();
330 void TextScroller::CleanUp()
332 if ( Stage::IsInstalled() )
334 Stage stage = Stage::GetCurrent();
335 RenderTaskList taskList = stage.GetRenderTaskList();
336 UnparentAndReset( mScrollingTextActor );
337 UnparentAndReset( mOffscreenCameraActor );
338 taskList.RemoveTask( mRenderTask );
344 } // namespace Toolkit