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