Merge "Added option to test harness execution to only summarize failures" into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-scroller.cpp
1 /*
2  * Copyright (c) 2017 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/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/text-scroller-interface.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace
35 {
36
37 #if defined ( DEBUG_ENABLED )
38   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
39 #endif
40
41 const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
42
43 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
44   attribute mediump vec2 aPosition;\n
45   varying highp vec2 vTexCoord;\n
46   varying highp float vRatio;\n
47   uniform mediump vec3 uSize;\n
48   uniform mediump float uDelta;\n
49   uniform mediump vec2 uTextureSize;\n
50   uniform mediump float uGap;\n
51   uniform mediump float uHorizontalAlign;\n
52   uniform mediump float uVerticalAlign;\n
53   \n
54   uniform mediump mat4 uModelMatrix;\n
55   uniform mediump mat4 uViewMatrix;\n
56   uniform mediump mat4 uProjection;\n
57   \n
58   //Visual size and offset
59   uniform mediump vec2 offset;\n
60   uniform mediump vec2 size;\n
61   uniform mediump vec4 offsetSizeMode;\n
62   uniform mediump vec2 origin;\n
63   uniform mediump vec2 anchorPoint;\n
64
65   void main()\n
66   {\n
67     mediump vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy );\n
68     mediump vec2 visualSize = mix( uSize.xy * size, size, offsetSizeMode.zw );\n
69     \n
70     mediump float smallTextPadding = max( visualSize.x - uTextureSize.x, 0. );\n
71     mediump float gap = max( uGap, smallTextPadding );\n
72     mediump float delta = floor ( uDelta ) + 0.5;\n
73     vTexCoord.x = ( delta + uHorizontalAlign * ( uTextureSize.x - visualSize.x ) + floor( aPosition.x * visualSize.x ) + 0.5 - gap * 0.5 ) / ( uTextureSize.x + gap ) + 0.5;\n
74     vTexCoord.y = ( uVerticalAlign * ( uTextureSize.y - visualSize.y ) + floor( aPosition.y * visualSize.y ) + 0.5 ) / ( uTextureSize.y ) + 0.5;\n
75     vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
76     \n
77     mediump vec4 vertexPosition = vec4( floor( ( aPosition + anchorPoint ) * visualSize + ( visualOffset + origin ) * uSize.xy ), 0.0, 1.0 );\n
78     mediump vec4 nonAlignedVertex = uViewMatrix * uModelMatrix * vertexPosition;\n
79     mediump vec4 pixelAlignedVertex = vec4 ( floor( nonAlignedVertex.xyz ), 1.0 );\n
80     \n
81     gl_Position = uProjection * pixelAlignedVertex;\n
82   }\n
83 );
84
85 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
86   varying highp vec2 vTexCoord;\n
87   varying highp float vRatio;\n
88   uniform sampler2D sTexture;\n
89   \n
90   void main()\n
91   {\n
92     highp vec2 texCoord;\n
93     texCoord.y = vTexCoord.y;\n
94     texCoord.x = fract( vTexCoord.x ) / vRatio;\n
95     if ( texCoord.x > 1.0 || texCoord.y > 1.0 )\n
96       discard;\n
97     \n
98     gl_FragColor = texture2D( sTexture, texCoord );\n
99   }\n
100 );
101
102 /**
103  * @brief How the text should be aligned horizontally when scrolling the text.
104  *
105  * -0.5f aligns the text to the left, 0.0f aligns the text to the center, 0.5f aligns the text to the right.
106  * The final alignment depends on two factors:
107  *   1) The alignment value of the text label (Use Text::Layout::HorizontalAlignment enumerations).
108  *   2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
109  */
110 const float HORIZONTAL_ALIGNMENT_TABLE[ Text::Layout::HORIZONTAL_ALIGN_COUNT ][ 2 ] =
111 {
112   // HORIZONTAL_ALIGN_BEGIN
113   {
114     -0.5f, // LTR
115     0.5f   // RTL
116   },
117
118   // HORIZONTAL_ALIGN_CENTER
119   {
120     0.0f,  // LTR
121     0.0f   // RTL
122   },
123
124   // HORIZONTAL_ALIGN_END
125   {
126     0.5f,  // LTR
127     -0.5f  // RTL
128   }
129 };
130
131 /**
132  * @brief How the text should be aligned vertically when scrolling the text.
133  *
134  * -0.5f aligns the text to the top, 0.0f aligns the text to the center, 0.5f aligns the text to the bottom.
135  * The alignment depends on the alignment value of the text label (Use Text::Layout::VerticalAlignment enumerations).
136  */
137 const float VERTICAL_ALIGNMENT_TABLE[ Text::Layout::VERTICAL_ALIGN_COUNT ] =
138 {
139   -0.5f, // VERTICAL_ALIGN_TOP
140   0.0f,  // VERTICAL_ALIGN_CENTER
141   0.5f   // VERTICAL_ALIGN_BOTTOM
142 };
143
144 } // namespace
145
146 namespace Text
147 {
148
149 TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
150 {
151   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::New\n" );
152
153   TextScrollerPtr textScroller( new TextScroller( scrollerInterface) );
154   return textScroller;
155 }
156
157 void TextScroller::SetGap( int gap )
158 {
159   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
160   mWrapGap = static_cast<float>(gap);
161 }
162
163 int TextScroller::GetGap() const
164 {
165   return static_cast<int>(mWrapGap);
166 }
167
168 void TextScroller::SetSpeed( int scrollSpeed )
169 {
170   mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
171 }
172
173 int TextScroller::GetSpeed() const
174 {
175   return mScrollSpeed;
176 }
177
178 void TextScroller::SetLoopCount( int loopCount )
179 {
180   if ( loopCount >= 0 )
181   {
182     mLoopCount = loopCount;
183   }
184
185   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
186 }
187
188 int TextScroller::GetLoopCount() const
189 {
190   return mLoopCount;
191 }
192
193 void TextScroller::SetLoopDelay( float delay )
194 {
195   mLoopDelay = delay;
196 }
197
198 float TextScroller::GetLoopDelay() const
199 {
200   return mLoopDelay;
201 }
202
203 void TextScroller::SetStopMode( DevelTextLabel::AutoScrollStopMode::Type stopMode )
204 {
205   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == DevelTextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
206   mStopMode = stopMode;
207 }
208
209 void TextScroller::StopScrolling()
210 {
211   if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
212   {
213     switch( mStopMode )
214     {
215       case DevelTextLabel::AutoScrollStopMode::IMMEDIATE:
216       {
217         mScrollAnimation.Stop();
218         mScrollerInterface.ScrollingFinished();
219         break;
220       }
221       case DevelTextLabel::AutoScrollStopMode::FINISH_LOOP:
222       {
223         mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
224         break;
225       }
226       default:
227       {
228         DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
229       }
230     }
231   }
232 }
233
234 DevelTextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
235 {
236   return mStopMode;
237 }
238
239 TextScroller::TextScroller( ScrollerInterface& scrollerInterface )
240 : mScrollerInterface( scrollerInterface ),
241   mScrollDeltaIndex( Property::INVALID_INDEX ),
242   mScrollSpeed( MINIMUM_SCROLL_SPEED ),
243   mLoopCount( 1 ),
244   mLoopDelay( 0.0f ),
245   mWrapGap( 0.0f ),
246   mStopMode( DevelTextLabel::AutoScrollStopMode::FINISH_LOOP )
247 {
248   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
249 }
250
251 TextScroller::~TextScroller()
252 {
253 }
254
255 void TextScroller::SetParameters( Actor scrollingTextActor, Renderer renderer, TextureSet textureSet, const Size& controlSize, const Size& textNaturalSize, CharacterDirection direction, Layout::HorizontalAlignment horizontalAlignment, Layout::VerticalAlignment verticalAlignment )
256 {
257   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d]\n",
258                  controlSize.x, controlSize.y, textNaturalSize.x, textNaturalSize.y, direction );
259
260   mRenderer = renderer;
261
262   float animationProgress = 0.0f;
263   int   remainedLoop = mLoopCount;
264   if ( mScrollAnimation )
265   {
266     if( mScrollAnimation.GetState() == Animation::PLAYING )
267     {
268       animationProgress = mScrollAnimation.GetCurrentProgress();
269
270       if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop
271       {
272         remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() );
273         remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop );
274       }
275     }
276     mScrollAnimation.Clear();
277
278     // Reset to the original shader and texture before scrolling
279     mRenderer.SetShader(mShader);
280     mRenderer.SetTextures( mTextureSet );
281   }
282
283   mShader = mRenderer.GetShader();
284   mTextureSet = mRenderer.GetTextures();
285
286   // Set the shader and texture for scrolling
287   Shader shader = Shader::New( VERTEX_SHADER_SCROLL, FRAGMENT_SHADER, Shader::Hint::NONE );
288   mRenderer.SetShader( shader );
289   mRenderer.SetTextures( textureSet );
290
291   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap );
292
293   const float horizontalAlign = HORIZONTAL_ALIGNMENT_TABLE[ horizontalAlignment ][ direction ];
294   const float verticalAlign = VERTICAL_ALIGNMENT_TABLE[ verticalAlignment ];
295   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters horizontalAlign[%f], verticalAlign[%f]\n", horizontalAlign, verticalAlign );
296
297   scrollingTextActor.RegisterProperty( "uTextureSize", textNaturalSize );
298   scrollingTextActor.RegisterProperty( "uHorizontalAlign", horizontalAlign );
299   scrollingTextActor.RegisterProperty( "uVerticalAlign", verticalAlign );
300   scrollingTextActor.RegisterProperty( "uGap", mWrapGap );
301   mScrollDeltaIndex = scrollingTextActor.RegisterProperty( "uDelta", 0.0f );
302
303   float scrollAmount = std::max( textNaturalSize.width + mWrapGap, controlSize.width );
304   float scrollDuration =  scrollAmount / mScrollSpeed;
305
306   if ( direction  )
307   {
308      scrollAmount = -scrollAmount; // reverse direction of scrolling
309   }
310
311   StartScrolling( scrollingTextActor, scrollAmount, scrollDuration, remainedLoop );
312   mScrollAnimation.SetCurrentProgress(animationProgress);
313 }
314
315 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
316 {
317   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::AutoScrollAnimationFinished\n" );
318   mScrollerInterface.ScrollingFinished();
319
320   // Revert to the original shader and texture after scrolling
321   mRenderer.SetShader(mShader);
322   if ( mTextureSet )
323   {
324     mRenderer.SetTextures( mTextureSet );
325   }
326 }
327
328 void TextScroller::StartScrolling( Actor scrollingTextActor, float scrollAmount, float scrollDuration, int loopCount )
329 {
330   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
331
332   mScrollAnimation = Animation::New( scrollDuration );
333   mScrollAnimation.AnimateTo( Property( scrollingTextActor, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
334   mScrollAnimation.SetEndAction( Animation::Discard );
335   mScrollAnimation.SetLoopCount( loopCount );
336   mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
337   mScrollAnimation.Play();
338 }
339
340 } // namespace Text
341
342 } // namespace Toolkit
343
344 } // namespace Dali