Merge "Text scroller renderer bug fix" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-scroller.cpp
index ee1d241..590f919 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/internal/text/text-scroller-interface.h>
-#include <dali-toolkit/internal/text/text-scroller-data.h>
 
 namespace Dali
 {
@@ -39,11 +38,6 @@ namespace Dali
 namespace Toolkit
 {
 
-namespace Text
-{
-extern const int MINIMUM_SCROLL_SPEED;
-} // namespace
-
 namespace
 {
 
@@ -51,39 +45,47 @@ namespace
   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_SCROLLING");
 #endif
 
+const int MINIMUM_SCROLL_SPEED = 1; // Speed should be set by Property system.
+
 const char* VERTEX_SHADER_SCROLL = DALI_COMPOSE_SHADER(
   attribute mediump vec2 aPosition;\n
   varying highp vec2 vTexCoord;\n
   varying highp float vRatio;\n
-  uniform mediump mat4 uMvpMatrix;\n
+  uniform mediump mat4 uModelMatrix;\n
+  uniform mediump mat4 uViewMatrix;\n
+  uniform mediump mat4 uProjection;\n
   uniform mediump vec3 uSize;\n
   uniform mediump float uDelta;\n
   uniform mediump vec2 uTextureSize;
   uniform mediump float uGap;\n
-  uniform mediump float uRtl;\n
+  uniform mediump float uAlign;\n
   \n
   void main()\n
   {\n
     {\n
-      mediump vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
+      highp vec4 vertexPosition = vec4(aPosition*uSize.xy, 0.0, 1.0);\n
+      vertexPosition = uViewMatrix *  uModelMatrix  * vertexPosition ;\n
+      vertexPosition.x = floor( vertexPosition.x ) + 0.5;
+      vertexPosition.y = floor( vertexPosition.y ) + 0.5;
       float smallTextPadding = max(uSize.x - uTextureSize.x, 0. );\n
       float gap = max( uGap, smallTextPadding );\n
-      vTexCoord.x = ( uDelta + ( uRtl * ( uTextureSize.x - uSize.x ) )  + ( aPosition.x * uSize.x ) )/ ( uTextureSize.x+gap );\n
-      vTexCoord.y = aPosition.y;\n
+      float delta = floor ( uDelta ) + 0.5;
+      vTexCoord.x = ( delta  + ( uAlign * ( uTextureSize.x - uSize.x ) ) + (  aPosition.x * uSize.x ) )/ ( uTextureSize.x + gap );\n
+      vTexCoord.y = ( 0.5 + floor(  aPosition.y * uSize.y ) )/ ( uTextureSize.y ) ;\n
       vRatio = uTextureSize.x / ( uTextureSize.x + gap );\n
-      gl_Position = uMvpMatrix * vertexPosition;\n
+      gl_Position = uProjection * vertexPosition;
     }\n
   }\n
 );
 
 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
-  varying mediump vec2 vTexCoord;\n
+  varying highp vec2 vTexCoord;\n
   varying highp float vRatio;\n
   uniform sampler2D sTexture;\n
   \n
   void main()\n
   {\n
-    mediump vec2 texCoord;\n
+    highp vec2 texCoord;\n
     texCoord.y = vTexCoord.y;\n
     texCoord.x = fract( vTexCoord.x ) / vRatio;\n
     if ( texCoord.x > 1.0 )\n
@@ -94,6 +96,54 @@ const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
 );
 
 /**
+ * @brief How the text should be aligned when scrolling the text.
+ *
+ * 0.0f aligns the text to the left, 1.0f aligns the text to the right.
+ * The final alignment depends on three factors:
+ *   1) The alignment value of the text label (Use Text::Layout::HorizontalAlignment enumerations).
+ *   2) The text direction, i.e. whether it's LTR or RTL (0 = LTR, 1 = RTL).
+ *   3) Whether the text is greater than the size of the control ( 0 = Text width <= Control width, 1 = Text width > Control width ).
+ */
+const float ALIGNMENT_TABLE[ Text::Layout::HORIZONTAL_ALIGN_COUNT ][ 2 ][ 2 ] =
+{
+  // HORIZONTAL_ALIGN_BEGIN
+  {
+    { // LTR
+      0.0f, // Text width <= Control width
+      0.0f  // Text width >  Control width
+    },
+    { // RTL
+      1.0f, // Text width <= Control width
+      1.0f  // Text width >  Control width
+    }
+  },
+
+  // HORIZONTAL_ALIGN_CENTER
+  {
+    { // LTR
+      0.5f, // Text width <= Control width
+      0.0f  // Text width >  Control width
+    },
+    { // RTL
+      0.5f, // Text width <= Control width
+      1.0f  // Text width >  Control width
+    }
+  },
+
+  // HORIZONTAL_ALIGN_END
+  {
+    { // LTR
+      1.0f, // Text width <= Control width
+      0.0f  // Text width >  Control width
+    },
+    { // RTL
+      0.0f, // Text width <= Control width
+      1.0f  // Text width >  Control width
+    }
+  }
+};
+
+/**
  * @brief Create and set up a camera for the render task to use
  *
  * @param[in] sizeOfTarget size of the source camera to look at
@@ -173,7 +223,7 @@ void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer
   Shader shader = Shader::New( VERTEX_SHADER_SCROLL , FRAGMENT_SHADER, Shader::Hint::NONE );
 
   Sampler sampler = Sampler::New();
-  sampler.SetFilterMode(FilterMode::NEAREST, FilterMode::NEAREST );
+  sampler.SetFilterMode(FilterMode::LINEAR, FilterMode::LINEAR );
 
   TextureSet textureSet = TextureSet::New();
   TextureSetImage( textureSet, 0u, frameBufferImage );
@@ -183,6 +233,7 @@ void CreateRenderer( FrameBufferImage frameBufferImage, Dali::Renderer& renderer
   CreateGeometry( meshGeometry );
 
   renderer = Renderer::New( meshGeometry, shader );
+  renderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, true );
   renderer.SetTextures( textureSet );
 }
 
@@ -199,6 +250,89 @@ TextScrollerPtr TextScroller::New( ScrollerInterface& scrollerInterface )
   return textScroller;
 }
 
+void TextScroller::SetGap( int gap )
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetGap gap[%d]\n", gap );
+  mWrapGap = static_cast<float>(gap);
+}
+
+int TextScroller::GetGap() const
+{
+  return static_cast<int>(mWrapGap);
+}
+
+void TextScroller::SetSpeed( int scrollSpeed )
+{
+  mScrollSpeed = std::max( MINIMUM_SCROLL_SPEED, scrollSpeed );
+}
+
+int TextScroller::GetSpeed() const
+{
+  return mScrollSpeed;
+}
+
+void TextScroller::SetLoopCount( int loopCount )
+{
+  if ( loopCount >= 0 )
+  {
+    mLoopCount = loopCount;
+  }
+
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount [%d] Status[%s]\n", mLoopCount, (loopCount)?"looping":"stop" );
+}
+
+int TextScroller::GetLoopCount() const
+{
+  return mLoopCount;
+}
+
+void TextScroller::SetLoopDelay( float delay )
+{
+  mLoopDelay = delay;
+}
+
+float TextScroller::GetLoopDelay() const
+{
+  return mLoopDelay;
+}
+
+void TextScroller::SetStopMode( DevelTextLabel::AutoScrollStopMode::Type stopMode )
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetAutoScrollStopMode [%s]\n",(stopMode == DevelTextLabel::AutoScrollStopMode::IMMEDIATE)?"IMMEDIATE":"FINISH_LOOP" );
+  mStopMode = stopMode;
+}
+
+void TextScroller::StopScrolling()
+{
+  if ( mScrollAnimation && mScrollAnimation.GetState() == Animation::PLAYING )
+  {
+    switch( mStopMode )
+    {
+      case DevelTextLabel::AutoScrollStopMode::IMMEDIATE:
+      {
+        mScrollAnimation.Stop();
+        CleanUp();
+        mScrollerInterface.ScrollingFinished();
+        break;
+      }
+      case DevelTextLabel::AutoScrollStopMode::FINISH_LOOP:
+      {
+        mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
+        break;
+      }
+      default:
+      {
+        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Undifined AutoScrollStopMode\n" );
+      }
+    }
+  }
+}
+
+DevelTextLabel::AutoScrollStopMode::Type TextScroller::GetStopMode() const
+{
+  return mStopMode;
+}
+
 Actor TextScroller::GetSourceCamera() const
 {
   return mOffscreenCameraActor;
@@ -209,9 +343,13 @@ Actor TextScroller::GetScrollingText() const
   return mScrollingTextActor;
 }
 
-TextScroller::TextScroller( ScrollerInterface& scrollerInterface )
-: mScrollerInterface( scrollerInterface ),
-  mScrollDeltaIndex( Property::INVALID_INDEX )
+TextScroller::TextScroller( ScrollerInterface& scrollerInterface ) : mScrollerInterface( scrollerInterface ),
+                            mScrollDeltaIndex( Property::INVALID_INDEX ),
+                            mScrollSpeed( MINIMUM_SCROLL_SPEED ),
+                            mLoopCount( 1 ),
+                            mLoopDelay( 0.0f ),
+                            mWrapGap( 0.0f ),
+                            mStopMode( DevelTextLabel::AutoScrollStopMode::FINISH_LOOP )
 {
   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller Default Constructor\n" );
 }
@@ -221,71 +359,103 @@ TextScroller::~TextScroller()
   CleanUp();
 }
 
-void TextScroller::StartScrolling( Actor sourceActor,
-                                   const ScrollerData& data )
+void TextScroller::SetParameters( Actor sourceActor, const Size& controlSize, const Size& offScreenSize, CharacterDirection direction, float alignmentOffset, Layout::HorizontalAlignment horizontalAlignment )
 {
-  DALI_LOG_INFO( gLogFilter,
-                 Debug::Verbose,
-                 "TextScroller::StartScrolling controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f]\n",
-                 data.mControlSize.x, data.mControlSize.y,
-                 data.mOffscreenSize.x, data.mOffscreenSize.y,
-                 data.mAutoScrollDirectionRTL,
-                 data.mAlignmentOffset );
-
-  FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( data.mOffscreenSize.width, data.mOffscreenSize.height, Pixel::RGBA8888 );
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters controlSize[%f,%f] offscreenSize[%f,%f] direction[%d] alignmentOffset[%f]\n",
+                 controlSize.x, controlSize.y, offScreenSize.x, offScreenSize.y, direction, alignmentOffset );
+
+  CleanUp(); //  If already scrolling then restart with new parameters
+
+  float animationProgress = 0.0f;
+  int   remainedLoop = mLoopCount;
+  if ( mScrollAnimation )
+  {
+    if( mScrollAnimation.GetState() == Animation::PLAYING )
+    {
+      animationProgress = mScrollAnimation.GetCurrentProgress();
+
+      if( mLoopCount > 0 ) // If not a ininity loop, then calculate remained loop
+      {
+        remainedLoop = mLoopCount - ( mScrollAnimation.GetCurrentLoop() );
+        remainedLoop = ( remainedLoop <= 0 ? 1 : remainedLoop );
+      }
+    }
+    mScrollAnimation.Clear();
+  }
+
+  FrameBufferImage offscreenRenderTargetForText = FrameBufferImage::New( offScreenSize.width, offScreenSize.height, Pixel::RGBA8888 );
   Renderer renderer;
 
-  CreateCameraActor( data.mOffscreenSize, mOffscreenCameraActor );
+  CreateCameraActor( offScreenSize, mOffscreenCameraActor );
   CreateRenderer( offscreenRenderTargetForText, renderer );
   CreateRenderTask( sourceActor, mOffscreenCameraActor, offscreenRenderTargetForText, mRenderTask );
 
-  // Reposition camera to match alignment of target, RTL text has direction=true
-  if( data.mAutoScrollDirectionRTL )
-  {
-    mOffscreenCameraActor.SetX( data.mAlignmentOffset + data.mOffscreenSize.width * 0.5f );
-  }
-  else
+  float xPosition = 0.0f;
+  switch( horizontalAlignment )
   {
-    mOffscreenCameraActor.SetX( data.mOffscreenSize.width * 0.5f );
+    case Layout::HORIZONTAL_ALIGN_BEGIN:
+    {
+      // Reposition camera to match alignment of target, RTL text has direction=true
+      if ( direction )
+      {
+        xPosition = alignmentOffset + offScreenSize.width * 0.5f;
+      }
+      else
+      {
+        xPosition = offScreenSize.width * 0.5f;
+      }
+      break;
+    }
+
+    case Layout::HORIZONTAL_ALIGN_CENTER:
+    {
+      xPosition = controlSize.width * 0.5f;
+      break;
+    }
+
+    case Layout::HORIZONTAL_ALIGN_END:
+    {
+      // Reposition camera to match alignment of target, RTL text has direction=true
+      if ( direction )
+      {
+        xPosition = offScreenSize.width * 0.5f;
+      }
+      else
+      {
+        xPosition = alignmentOffset + offScreenSize.width * 0.5f;
+      }
+      break;
+    }
   }
 
-  mOffscreenCameraActor.SetY( data.mOffscreenSize.height * 0.5f );
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters xPosition[%f]\n", xPosition );
 
-  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", data.mWrapGap )
+  mOffscreenCameraActor.SetX( xPosition );
+  mOffscreenCameraActor.SetY( offScreenSize.height * 0.5f );
+
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters mWrapGap[%f]\n", mWrapGap );
+
+  const float align = ALIGNMENT_TABLE[ horizontalAlignment ][ direction ][ offScreenSize.width > controlSize.width ];
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetParameters align[%f]\n", align );
 
   mScrollingTextActor = Actor::New();
   mScrollingTextActor.AddRenderer( renderer );
-  mScrollingTextActor.RegisterProperty( "uTextureSize", data.mOffscreenSize );
-  mScrollingTextActor.RegisterProperty( "uRtl", ( data.mAutoScrollDirectionRTL ? 1.f : 0.f ) );
-  mScrollingTextActor.RegisterProperty( "uGap", data.mWrapGap );
-  mScrollingTextActor.SetSize( data.mControlSize.width, std::min( data.mOffscreenSize.height, data.mControlSize.height ) );
+  mScrollingTextActor.RegisterProperty( "uTextureSize", offScreenSize );
+  mScrollingTextActor.RegisterProperty( "uAlign", align );
+  mScrollingTextActor.RegisterProperty( "uGap", mWrapGap );
+  mScrollingTextActor.SetSize( controlSize.width, std::min( offScreenSize.height, controlSize.height ) );
   mScrollDeltaIndex = mScrollingTextActor.RegisterProperty( "uDelta", 0.0f );
 
-  float scrollAmount = std::max( data.mOffscreenSize.width + data.mWrapGap, data.mControlSize.width );
-  float scrollSpeed = std::max( MINIMUM_SCROLL_SPEED, data.mScrollSpeed );
-  float scrollDuration =  scrollAmount / scrollSpeed;
+  float scrollAmount = std::max( offScreenSize.width + mWrapGap, controlSize.width );
+  float scrollDuration =  scrollAmount / mScrollSpeed;
 
-  if( data.mAutoScrollDirectionRTL )
+  if ( direction  )
   {
      scrollAmount = -scrollAmount; // reverse direction of scrollung
   }
 
-  mScrollAnimation = Animation::New( scrollDuration );
-  mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount );
-  mScrollAnimation.SetEndAction( Animation::Discard );
-  mScrollAnimation.SetLoopCount( data.mLoopCount );
-  mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
-  mScrollAnimation.Play();
-}
-
-void TextScroller::StopScrolling()
-{
-  if( mScrollAnimation &&
-      ( mScrollAnimation.GetState() == Animation::PLAYING ) )
-  {
-    DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::SetLoopCount Single loop forced\n" );
-    mScrollAnimation.SetLoopCount( 1 ); // As animation already playing this allows the current animation to finish instead of trying to stop mid-way
-  }
+  StartScrolling( scrollAmount, scrollDuration, remainedLoop );
+  mScrollAnimation.SetCurrentProgress(animationProgress);
 }
 
 void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
@@ -295,6 +465,18 @@ void TextScroller::AutoScrollAnimationFinished( Dali::Animation& animation )
   mScrollerInterface.ScrollingFinished();
 }
 
+void TextScroller::StartScrolling( float scrollAmount, float scrollDuration, int loopCount )
+{
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextScroller::StartScrolling scrollAmount[%f] scrollDuration[%f], loop[%d] speed[%d]\n", scrollAmount, scrollDuration, loopCount, mScrollSpeed );
+
+  mScrollAnimation = Animation::New( scrollDuration );
+  mScrollAnimation.AnimateTo( Property( mScrollingTextActor, mScrollDeltaIndex ), scrollAmount, TimePeriod( mLoopDelay, scrollDuration ) );
+  mScrollAnimation.SetEndAction( Animation::Discard );
+  mScrollAnimation.SetLoopCount( loopCount );
+  mScrollAnimation.FinishedSignal().Connect( this, &TextScroller::AutoScrollAnimationFinished );
+  mScrollAnimation.Play();
+}
+
 void TextScroller::CleanUp()
 {
   if ( Stage::IsInstalled() )