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