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