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