Fixed bug in 3x3 NPatch shader and Color visual shader
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.cpp
index 89fc8ea..4cf199a 100644 (file)
 #include <dali/public-api/images/resource-image.h>
 #include <dali/devel-api/images/texture-set-image.h>
 
-// INTERNAL IINCLUDES
+// INTERNAL INCLUDES
+#include <dali-toolkit/public-api/visuals/image-visual-properties.h>
+#include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
-#include <dali-toolkit/internal/visuals/visual-impl.h>
-#include <dali-toolkit/internal/visuals/visual-data-impl.h>
+#include <dali-toolkit/internal/visuals/visual-base-impl.h>
+#include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
 
 
 namespace Dali
@@ -53,6 +55,14 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
   uniform mediump vec2 uNinePatchFactorsX[ FACTOR_SIZE_X ];\n
   uniform mediump vec2 uNinePatchFactorsY[ FACTOR_SIZE_Y ];\n
   \n
+
+  //Visual size and offset
+  uniform mediump vec2 offset;\n
+  uniform mediump vec2 size;\n
+  uniform mediump vec4 offsetSizeMode;\n
+  uniform mediump vec2 origin;\n
+  uniform mediump vec2 anchorPoint;\n
+
   void main()\n
   {\n
     mediump vec2 fixedFactor  = vec2( uNinePatchFactorsX[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uNinePatchFactorsY[ int( ( aPosition.y + 1.0 ) * 0.5 ) ].x );\n
@@ -61,8 +71,13 @@ const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
     mediump vec2 fixedTotal   = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].x, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].x );\n
     mediump vec2 stretchTotal = vec2( uNinePatchFactorsX[ FACTOR_SIZE_X - 1 ].y, uNinePatchFactorsY[ FACTOR_SIZE_Y - 1 ].y );\n
     \n
-    mediump vec4 vertexPosition = vec4( ( fixedFactor + ( uSize.xy - fixedTotal ) * stretch / stretchTotal ), 0.0, 1.0 );\n
-    vertexPosition.xy -= uSize.xy * vec2( 0.5, 0.5 );\n
+
+    vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
+    vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
+
+    mediump vec4 vertexPosition = vec4( ( fixedFactor + ( visualSize.xy - fixedTotal ) * stretch / stretchTotal ) +  anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
+    vertexPosition.xy -= visualSize.xy * vec2( 0.5, 0.5 );\n
+
     vertexPosition = uMvpMatrix * vertexPosition;\n
     \n
     vTexCoord = ( fixedFactor + stretch ) / ( fixedTotal + stretchTotal );\n
@@ -80,18 +95,29 @@ const char* VERTEX_SHADER_3X3 = DALI_COMPOSE_SHADER(
     uniform mediump vec2 uFixed[ 3 ];\n
     uniform mediump vec2 uStretchTotal;\n
     \n
+
+    //Visual size and offset
+    uniform mediump vec2 offset;\n
+    uniform mediump vec2 size;\n
+    uniform mediump vec4 offsetSizeMode;\n
+    uniform mediump vec2 origin;\n
+    uniform mediump vec2 anchorPoint;\n
+
     void main()\n
     {\n
+      vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
+      vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
+
       mediump vec2 scale        = vec2( length( uModelMatrix[ 0 ].xyz ), length( uModelMatrix[ 1 ].xyz ) );\n
-      mediump vec2 size         = uSize.xy * scale;\n
+      mediump vec2 size         = visualSize.xy * scale;\n
       \n
       mediump vec2 fixedFactor  = vec2( uFixed[ int( ( aPosition.x + 1.0 ) * 0.5 ) ].x, uFixed[ int( ( aPosition.y  + 1.0 ) * 0.5 ) ].y );\n
       mediump vec2 stretch      = floor( aPosition * 0.5 );\n
       mediump vec2 fixedTotal   = uFixed[ 2 ];\n
       \n
-      mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );\n
+      mediump vec4 vertexPosition = vec4( fixedFactor + ( size - fixedTotal ) * stretch, 0.0, 1.0 );
       vertexPosition.xy -= size * vec2( 0.5, 0.5 );\n
-      vertexPosition.xy =  vertexPosition.xy / scale;\n
+      vertexPosition.xy =  vertexPosition.xy / scale + anchorPoint*size + (visualOffset + origin)*uSize.xy;\
       \n
       vertexPosition = uMvpMatrix * vertexPosition;\n
       \n
@@ -199,9 +225,41 @@ void RegisterStretchProperties( Renderer& renderer, const char * uniformName, co
 
 /////////////////NPatchVisual////////////////
 
-NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache )
-: Visual( factoryCache ),
-  mBorderOnly( false )
+NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache )
+{
+  return new NPatchVisual( factoryCache );
+}
+
+NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, const std::string& imageUrl, bool borderOnly )
+{
+  NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache, borderOnly );
+  nPatchVisual->mImageUrl = imageUrl;
+
+  NinePatchImage image = NinePatchImage::New( imageUrl );
+  nPatchVisual->InitializeFromImage( image );
+
+  return nPatchVisual;
+}
+
+NPatchVisualPtr NPatchVisual::New( VisualFactoryCache& factoryCache, NinePatchImage image, bool borderOnly )
+{
+  NPatchVisual* nPatchVisual = new NPatchVisual( factoryCache, borderOnly );
+  nPatchVisual->mImage = image;
+
+  nPatchVisual->InitializeFromImage( image );
+
+  return nPatchVisual;
+}
+
+NPatchVisual::NPatchVisual( VisualFactoryCache& factoryCache, bool borderOnly )
+: Visual::Base( factoryCache ),
+  mImage(),
+  mCroppedImage(),
+  mImageUrl(),
+  mStretchPixelsX(),
+  mStretchPixelsY(),
+  mImageSize(),
+  mBorderOnly( borderOnly )
 {
 }
 
@@ -209,13 +267,13 @@ NPatchVisual::~NPatchVisual()
 {
 }
 
-void NPatchVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
+void NPatchVisual::DoSetProperties( const Property::Map& propertyMap )
 {
-  Property::Value* imageURLValue = propertyMap.Find( IMAGE_URL_NAME );
+  Property::Value* imageURLValue = propertyMap.Find( Toolkit::ImageVisual::Property::URL, IMAGE_URL_NAME );
   if( imageURLValue )
   {
     //Read the borderOnly property first since InitialiseFromImage relies on mBorderOnly to be properly set
-    Property::Value* borderOnlyValue = propertyMap.Find( BORDER_ONLY );
+    Property::Value* borderOnlyValue = propertyMap.Find( Toolkit::ImageVisual::Property::BORDER_ONLY, BORDER_ONLY );
     if( borderOnlyValue )
     {
       borderOnlyValue->Get( mBorderOnly );
@@ -253,17 +311,6 @@ void NPatchVisual::GetNaturalSize( Vector2& naturalSize ) const
   }
 }
 
-void NPatchVisual::SetClipRect( const Rect<int>& clipRect )
-{
-  Visual::SetClipRect( clipRect );
-  //ToDo: renderer responds to the clipRect change
-}
-
-void NPatchVisual::SetOffset( const Vector2& offset )
-{
-  //ToDo: renderer applies the offset
-}
-
 Geometry NPatchVisual::CreateGeometry()
 {
   Geometry geometry;
@@ -324,7 +371,7 @@ Shader NPatchVisual::CreateShader()
   else
   {
     const char* fragmentShader = FRAGMENT_SHADER;
-    Dali::Shader::ShaderHints hints = Dali::Shader::HINT_NONE;
+    Dali::Shader::Hint::Value hints = Dali::Shader::Hint::NONE;
 
     if( !mImpl->mCustomShader->mFragmentShader.empty() )
     {
@@ -364,6 +411,9 @@ void NPatchVisual::InitializeRenderer()
   TextureSet textureSet = TextureSet::New();
   mImpl->mRenderer = Renderer::New( geometry, shader );
   mImpl->mRenderer.SetTextures( textureSet );
+
+  //Register transform properties
+  mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
 }
 
 
@@ -389,6 +439,8 @@ void NPatchVisual::DoSetOnStage( Actor& actor )
   {
     ApplyImageToSampler();
   }
+
+  actor.AddRenderer( mImpl->mRenderer );
 }
 
 void NPatchVisual::DoSetOffStage( Actor& actor )
@@ -401,18 +453,38 @@ void NPatchVisual::DoSetOffStage( Actor& actor )
 void NPatchVisual::DoCreatePropertyMap( Property::Map& map ) const
 {
   map.Clear();
-  map.Insert( RENDERER_TYPE, IMAGE_RENDERER );
+  map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::IMAGE );
   if( !mImageUrl.empty() )
   {
-    map.Insert( IMAGE_URL_NAME, mImageUrl );
+    map.Insert( Toolkit::ImageVisual::Property::URL, mImageUrl );
   }
   else if( mImage )
   {
-    map.Insert( IMAGE_URL_NAME, mImage.GetUrl() );
+    map.Insert( Toolkit::ImageVisual::Property::URL, mImage.GetUrl() );
   }
-  map.Insert( BORDER_ONLY, mBorderOnly );
+  map.Insert( Toolkit::ImageVisual::Property::BORDER_ONLY, mBorderOnly );
+}
+
+void NPatchVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
+{
+  // TODO
 }
 
+Dali::Property::Value NPatchVisual::DoGetProperty( Dali::Property::Index index )
+{
+  // TODO
+  return Dali::Property::Value();
+}
+
+void NPatchVisual::OnSetTransform()
+{
+  if( mImpl->mRenderer )
+  {
+    mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
+  }
+}
+
+
 void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY )
 {
   //check to see if the border style has changed
@@ -449,60 +521,6 @@ void NPatchVisual::ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t o
   }
 }
 
-void NPatchVisual::SetImage( const std::string& imageUrl, bool borderOnly )
-{
-  bool oldBorderOnly = mBorderOnly;
-  size_t oldGridX = mStretchPixelsX.Size();
-  size_t oldGridY = mStretchPixelsY.Size();
-
-  mBorderOnly = borderOnly;
-  mImage.Reset();
-  if( mImageUrl == imageUrl )
-  {
-    return;
-  }
-
-  mImageUrl = imageUrl;
-  if( mImpl->mRenderer )
-  {
-    NinePatchImage nPatch = NinePatchImage::New( mImageUrl );
-    InitializeFromImage( nPatch );
-
-    ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
-
-    if( mCroppedImage )
-    {
-      ApplyImageToSampler();
-    }
-  }
-}
-
-void NPatchVisual::SetImage( NinePatchImage image, bool borderOnly )
-{
-  bool oldBorderOnly = mBorderOnly;
-  size_t oldGridX = mStretchPixelsX.Size();
-  size_t oldGridY = mStretchPixelsY.Size();
-
-  mBorderOnly = borderOnly;
-  mImageUrl.empty();
-  if( mImage == image )
-  {
-    return;
-  }
-
-  mImage = image;
-  if( mImpl->mRenderer )
-  {
-    InitializeFromImage( mImage );
-    ChangeRenderer( oldBorderOnly, oldGridX, oldGridY );
-
-    if( mCroppedImage )
-    {
-      ApplyImageToSampler();
-    }
-  }
-}
-
 void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
 {
   mCroppedImage = nPatch.CreateCroppedBufferImage();
@@ -521,7 +539,7 @@ void NPatchVisual::InitializeFromImage( NinePatchImage nPatch )
 
 void NPatchVisual::InitializeFromBrokenImage()
 {
-  mCroppedImage = VisualFactory::GetBrokenRendererImage();
+  mCroppedImage = VisualFactoryCache::GetBrokenVisualImage();
   mImageSize = ImageDimensions( mCroppedImage.GetWidth(), mCroppedImage.GetHeight() );
 
   mStretchPixelsX.Clear();