Merge "Add descriptions and example codes" into devel/master
[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 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
60   \n
61   void main()\n
62   {\n
63     {\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
71     }\n
72   }\n
73 );
74
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
79   \n
80   void main()\n
81   {\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
86       discard;\n
87     \n
88     gl_FragColor = texture2D( sTexture, texCoord );\n
89   }\n
90 );
91
92 /**
93  * @brief Create and set up a camera for the render task to use
94  *
95  * @param[in] sizeOfTarget size of the source camera to look at
96  * @param[out] offscreenCamera custom camera
97  */
98 void CreateCameraActor( const Size& sizeOfTarget, CameraActor& offscreenCamera )
99 {
100   offscreenCamera = CameraActor::New();
101   offscreenCamera.SetOrthographicProjection( sizeOfTarget );
102   offscreenCamera.SetInvertYAxis( true );
103 }
104
105 /**
106  * @brief Create a render task
107  *
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
112  */
113 void CreateRenderTask( Actor sourceActor, CameraActor cameraActor , FrameBufferImage offscreenTarget, RenderTask& renderTask )
114 {
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 );
126 }
127
128 /**
129  * @brief Create quad geometry for the mesh
130  *
131  * @param[out] geometry quad geometry that can be used for a mesh
132  */
133 void CreateGeometry( Geometry& geometry )
134 {
135   struct QuadVertex { Vector2 position;  };
136
137   QuadVertex quadVertexData[4] =
138   {
139       { Vector2( 0.0f, 0.0f) },
140       { Vector2( 1.0f, 0.0f) },
141       { Vector2( 0.0f, 1.0f) },
142       { Vector2( 1.0f, 1.0f) },
143   };
144
145   const unsigned short indices[6] =
146   {
147      3,1,0,0,2,3
148   };
149
150   Property::Map quadVertexFormat;
151   quadVertexFormat["aPosition"] = Property::VECTOR2;
152   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
153   quadVertices.SetData(quadVertexData, 4 );
154
155   geometry = Geometry::New();
156   geometry.AddVertexBuffer( quadVertices );
157   geometry.SetIndexBuffer( indices, sizeof(indices)/sizeof(indices[0]) );
158 }
159
160
161 /**
162  * @brief Create a renderer
163  *
164  * @param[in] frameBufferImage texture to be used
165  * @param[out] renderer mesh renderer using the supplied texture
166  */
167 void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer )
168 {
169   Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::Hint::NONE );
170
171   Sampler sampler = Sampler::New();
172   sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST );
173
174   TextureSet textureSet = TextureSet::New();
175   TextureSetImage( textureSet, 0u, frameBufferImage );
176   textureSet.SetSampler( 0u, sampler );
177
178   Geometry meshGeometry;
179   CreateGeometry( meshGeometry );
180
181   renderer = Renderer::New( meshGeometry, shader );
182   renderer.SetTextures( textureSet );
183 }
184
185 } // namespace
186
187 namespace Text
188 {
189
190 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
191 {
192   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
193
194   TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
195   return textScroller;
196 }
197
198 void TextScroller::SetGap( int gap )
199 {
200   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
201   mWrapGap = static_cast<float>(gap);
202 }
203
204 int TextScroller::GetGap() const
205 {
206   return static_cast<int>(mWrapGap);
207 }
208
209 void TextScroller::SetSpeed( int scrollSpeed )
210 {
211   mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
212 }
213
214 int TextScroller::GetSpeed() const
215 {
216   return mScrollSpeed;
217 }
218
219 void TextScroller::SetLoopCount( int loopCount )
220 {
221   if ( loopCount > 0 )
222   {
223     mLoopCount = loopCount;
224   }
225
226   if (  mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
227   {
228     if ( loopCount == 0 ) // Request to stop looping
229     {
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
232     }
233   }
234   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
235 }
236
237 int TextScroller::GetLoopCount() const
238 {
239   return mLoopCount;
240 }
241
242 Actor TextScroller::GetSourceCamera() const
243 {
244   return mOffscreenCameraActor;
245 }
246
247 Actor TextScroller::GetScrollingText() const
248 {
249   return mScrollingTextActor;
250 }
251
252 TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ),
253                             mScrollDeltaIndex( Property::INVALID_INDEX ),
254                             mScrollSpeed( MINIMUM_SCROLL_SPEED ),
255                             mLoopCount( 1 ),
256                             mWrapGap( 0.0f )
257 {
258   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
259 }
260
261 TextScroller::~TextScroller()
262 {
263   CleanUp();
264 }
265
266 void TextScroller::SetParameters( Actor sourceActor, const Size& controlSize, const Size& offScreenSize, CharacterDirection direction, float alignmentOffset )
267 {
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 );
270
271   FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( offScreenSize.width, offScreenSize.height, Pixel::RGBA8888, Dali::Image::UNUSED );
272   Renderer renderer;
273
274   CreateCameraActor( offScreenSize, mOffscreenCameraActor );
275   CreateRenderer( offscreenRenderTargetForText, renderer );
276   CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask );
277
278   // Reposition camera to match alignment of target, RTL text has direction=true
279   if ( direction )
280   {
281     mOffscreenCameraActor.SetX( alignmentOffset + offScreenSize.width*0.5f );
282   }
283   else
284   {
285     mOffscreenCameraActor.SetX( offScreenSize.width * 0.5f );
286   }
287
288   mOffscreenCameraActor.SetY( offScreenSize.height * 0.5f );
289
290   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap )
291
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 );
299
300   float scrollAmount = std::max( offScreenSize.width + mWrapGap, controlSize.width );
301   float scrollDuration =  scrollAmount / mScrollSpeed;
302
303   if ( direction  )
304   {
305      scrollAmount = -scrollAmount; // reverse direction of scrollung
306   }
307
308   StartScrolling( scrollAmount, scrollDuration, mLoopCount );
309 }
310
311 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
312 {
313   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
314   CleanUp();
315   mScrollerInterface.ScrollingFinished();
316 }
317
318 void TextScroller::StartScrolling( float scrollAmount, float scrollDuration, int loopCount )
319 {
320   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
321
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();
328 }
329
330 void TextScroller::CleanUp()
331 {
332   if ( Stage::IsInstalled() )
333   {
334     Stage stage = Stage::GetCurrent();
335     RenderTaskList taskList = stage.GetRenderTaskList();
336     UnparentAndReset( mScrollingTextActor );
337     UnparentAndReset( mOffscreenCameraActor );
338     taskList.RemoveTask( mRenderTask );
339   }
340 }
341
342 } // namespace Text
343
344 } // namespace Toolkit
345
346 } // namespace Dali