adcf90138f85efaf142be87c4b30a9eaf6757609
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-scroller.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/text-scroller.h>
20
21 // EXTERNAL INCLUDES
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>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/internal/text/text-scroller-interface.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace
42 {
43
44 #if defined ( DEBUG_ENABLED )
45   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
46 #endif
47
48 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
49
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 uModelMatrix;\n
55   uniform mediump mat4 uViewMatrix;\n
56   uniform mediump mat4 uProjection;\n
57   uniform mediump vec3 uSize;\n
58   uniform mediump float uDelta;\n
59   uniform mediump vec2 uTextureSize;
60   uniform mediump float uGap;\n
61   uniform mediump float uRtl;\n
62   \n
63   void main()\n
64   {\n
65     {\n
66       highp vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
67       vertexPosition = uViewMatrix *  uModelMatrix  * vertexPosition ;\n
68       vertexPosition.x = floor( vertexPosition.x ) + 0.5;
69       vertexPosition.y = floor( vertexPosition.y ) + 0.5;
70       float smallTextPadding = max(uSize.x - uTextureSize.x, 0. );\n
71       float gap = max( uGap, smallTextPadding );\n
72       float delta = floor ( uDelta ) + 0.5;
73       vTexCoord.x = ( delta  + ( uRtl * ( uTextureSize.x - uSize.x ) ) + (  aPosition.x * uSize.x ) )/ ( uTextureSize.x + gap );\n
74       vTexCoord.y = ( 0.5 + floor(  aPosition.y * uSize.y ) )/ ( uTextureSize.y ) ;\n
75       vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
76       gl_Position = uProjection * vertexPosition;
77     }\n
78   }\n
79 );
80
81 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
82   varying highp vec2 vTexCoord;\n
83   varying highp float vRatio;\n
84   uniform sampler2D sTexture;\n
85   \n
86   void main()\n
87   {\n
88     highp vec2 texCoord;\n
89     texCoord.y = vTexCoord.y;\n
90     texCoord.x = fract( vTexCoord.x ) / vRatio;\n
91     if ( texCoord.x > 1.0 )\n
92       discard;\n
93     \n
94     gl_FragColor = texture2D( sTexture, texCoord );\n
95   }\n
96 );
97
98 /**
99  * @brief Create and set up a camera for the render task to use
100  *
101  * @param[in] sizeOfTarget size of the source camera to look at
102  * @param[out] offscreenCamera custom camera
103  */
104 void CreateCameraActor( const Size& sizeOfTarget, CameraActor& offscreenCamera )
105 {
106   offscreenCamera = CameraActor::New();
107   offscreenCamera.SetOrthographicProjection( sizeOfTarget );
108   offscreenCamera.SetInvertYAxis( true );
109 }
110
111 /**
112  * @brief Create a render task
113  *
114  * @param[in] sourceActor actor to be used as source
115  * @param[in] cameraActor camera looking at source
116  * @param[in] offscreenTarget resulting image from render task
117  * @param[out] renderTask render task that has been setup
118  */
119 void CreateRenderTask( Actor sourceActor, CameraActor cameraActor , FrameBufferImage offscreenTarget, RenderTask& renderTask )
120 {
121   Stage stage = Stage::GetCurrent();
122   RenderTaskList taskList = stage.GetRenderTaskList();
123   renderTask = taskList.CreateTask();
124   renderTask.SetSourceActor( sourceActor );
125   renderTask.SetExclusive( true );
126   renderTask.SetInputEnabled( false );
127   renderTask.SetClearEnabled( true );
128   renderTask.SetCameraActor( cameraActor );
129   renderTask.SetTargetFrameBuffer( offscreenTarget );
130   renderTask.SetClearColor( Color::TRANSPARENT );
131   renderTask.SetCullMode( false );
132 }
133
134 /**
135  * @brief Create quad geometry for the mesh
136  *
137  * @param[out] geometry quad geometry that can be used for a mesh
138  */
139 void CreateGeometry( Geometry& geometry )
140 {
141   struct QuadVertex { Vector2 position;  };
142
143   QuadVertex quadVertexData[4] =
144   {
145       { Vector2( 0.0f, 0.0f) },
146       { Vector2( 1.0f, 0.0f) },
147       { Vector2( 0.0f, 1.0f) },
148       { Vector2( 1.0f, 1.0f) },
149   };
150
151   const unsigned short indices[6] =
152   {
153      3,1,0,0,2,3
154   };
155
156   Property::Map quadVertexFormat;
157   quadVertexFormat["aPosition"] = Property::VECTOR2;
158   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
159   quadVertices.SetData(quadVertexData, 4 );
160
161   geometry = Geometry::New();
162   geometry.AddVertexBuffer( quadVertices );
163   geometry.SetIndexBuffer( indices, sizeof(indices)/sizeof(indices[0]) );
164 }
165
166
167 /**
168  * @brief Create a renderer
169  *
170  * @param[in] frameBufferImage texture to be used
171  * @param[out] renderer mesh renderer using the supplied texture
172  */
173 void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer )
174 {
175   Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::Hint::NONE );
176
177   Sampler sampler = Sampler::New();
178   sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR );
179
180   TextureSet textureSet = TextureSet::New();
181   TextureSetImage( textureSet, 0u, frameBufferImage );
182   textureSet.SetSampler( 0u, sampler );
183
184   Geometry meshGeometry;
185   CreateGeometry( meshGeometry );
186
187   renderer = Renderer::New( meshGeometry, shader );
188   renderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
189   renderer.SetTextures( textureSet );
190 }
191
192 } // namespace
193
194 namespace Text
195 {
196
197 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
198 {
199   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
200
201   TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
202   return textScroller;
203 }
204
205 void TextScroller::SetGap( int gap )
206 {
207   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
208   mWrapGap = static_cast<float>(gap);
209 }
210
211 int TextScroller::GetGap() const
212 {
213   return static_cast<int>(mWrapGap);
214 }
215
216 void TextScroller::SetSpeed( int scrollSpeed )
217 {
218   mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
219 }
220
221 int TextScroller::GetSpeed() const
222 {
223   return mScrollSpeed;
224 }
225
226 void TextScroller::SetLoopCount( int loopCount )
227 {
228   if ( loopCount > 0 )
229   {
230     mLoopCount = loopCount;
231   }
232
233   if (  mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
234   {
235     if ( loopCount == 0 ) // Request to stop looping
236     {
237       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount Single loop forced\n" );
238       switch( mStopMode )
239       {
240         case DevelTextLabel::AutoScrollStopMode::IMMEDIATE:
241         {
242           mScrollAnimation.Stop();
243           break;
244         }
245         case DevelTextLabel::AutoScrollStopMode::FINISH_LOOP:
246         {
247           mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
248           break;
249         }
250         default:
251         {
252            DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
253         }
254       }
255     }
256   }
257   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
258 }
259
260 int TextScroller::GetLoopCount() const
261 {
262   return mLoopCount;
263 }
264
265 void TextScroller::SetLoopDelay( float delay )
266 {
267   mLoopDelay = delay;
268 }
269
270 float TextScroller::GetLoopDelay() const
271 {
272   return mLoopDelay;
273 }
274
275 void TextScroller::SetStopMode( DevelTextLabel::AutoScrollStopMode::Type stopMode )
276 {
277   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == DevelTextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
278   mStopMode = stopMode;
279 }
280
281 DevelTextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
282 {
283   return mStopMode;
284 }
285
286 Actor TextScroller::GetSourceCamera() const
287 {
288   return mOffscreenCameraActor;
289 }
290
291 Actor TextScroller::GetScrollingText() const
292 {
293   return mScrollingTextActor;
294 }
295
296 TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ),
297                             mScrollDeltaIndex( Property::INVALID_INDEX ),
298                             mScrollSpeed( MINIMUM_SCROLL_SPEED ),
299                             mLoopCount( 1 ),
300                             mLoopDelay( 0.0f ),
301                             mWrapGap( 0.0f ),
302                             mStopMode( DevelTextLabel::AutoScrollStopMode::FINISH_LOOP )
303 {
304   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
305 }
306
307 TextScroller::~TextScroller()
308 {
309   CleanUp();
310 }
311
312 void TextScroller::SetParameters( Actor sourceActor, const Size& controlSize, const Size& offScreenSize, CharacterDirection direction, float alignmentOffset )
313 {
314   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f]\n",
315                  controlSize.x, controlSize.y, offScreenSize.x, offScreenSize.y, direction, alignmentOffset );
316
317   CleanUp(); //  If already scrolling then restart with new parameters
318
319   if ( mScrollAnimation )
320   {
321     mScrollAnimation.Clear();
322   }
323
324   FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( offScreenSize.width, offScreenSize.height, Pixel::RGBA8888 );
325   Renderer renderer;
326
327   CreateCameraActor( offScreenSize, mOffscreenCameraActor );
328   CreateRenderer( offscreenRenderTargetForText, renderer );
329   CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask );
330
331   // Reposition camera to match alignment of target, RTL text has direction=true
332   if ( direction )
333   {
334     mOffscreenCameraActor.SetX( alignmentOffset + offScreenSize.width*0.5f );
335   }
336   else
337   {
338     mOffscreenCameraActor.SetX( offScreenSize.width * 0.5f );
339   }
340
341   mOffscreenCameraActor.SetY( offScreenSize.height * 0.5f );
342
343   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap )
344
345   mScrollingTextActor = Actor::New();
346   mScrollingTextActor.AddRenderer( renderer );
347   mScrollingTextActor.RegisterProperty( "uTextureSize", offScreenSize );
348   mScrollingTextActor.RegisterProperty( "uRtl", ((direction)?1.0f:0.0f) );
349   mScrollingTextActor.RegisterProperty( "uGap", mWrapGap );
350   mScrollingTextActor.SetSize( controlSize.width, std::min( offScreenSize.height, controlSize.height ) );
351   mScrollDeltaIndex = mScrollingTextActor.RegisterProperty( "uDelta", 0.0f );
352
353   float scrollAmount = std::max( offScreenSize.width + mWrapGap, controlSize.width );
354   float scrollDuration =  scrollAmount / mScrollSpeed;
355
356   if ( direction  )
357   {
358      scrollAmount = -scrollAmount; // reverse direction of scrollung
359   }
360
361   StartScrolling( scrollAmount, scrollDuration, mLoopCount );
362 }
363
364 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
365 {
366   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
367   CleanUp();
368   mScrollerInterface.ScrollingFinished();
369 }
370
371 void TextScroller::StartScrolling( float scrollAmount, float scrollDuration, int loopCount )
372 {
373   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
374
375   mScrollAnimation = Animation::New( scrollDuration );
376   mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
377   mScrollAnimation.SetEndAction( Animation::Discard );
378   mScrollAnimation.SetLoopCount( loopCount );
379   mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
380   mScrollAnimation.Play();
381 }
382
383 void TextScroller::CleanUp()
384 {
385   if ( Stage::IsInstalled() )
386   {
387     Stage stage = Stage::GetCurrent();
388     RenderTaskList taskList = stage.GetRenderTaskList();
389     UnparentAndReset( mScrollingTextActor );
390     UnparentAndReset( mOffscreenCameraActor );
391     taskList.RemoveTask( mRenderTask );
392   }
393 }
394
395 } // namespace Text
396
397 } // namespace Toolkit
398
399 } // namespace Dali