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