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