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