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