Fix code to match correct style, i.e. methods first
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-data-impl.cpp
index 8e610f1..489dfb6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 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.
 
 // EXTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/devel-api/scripting/enum-helper.h>
+#include <dali/devel-api/scripting/scripting.h>
 #include <dali/integration-api/debug.h>
-#include <dali/public-api/object/property-array.h>
+
+// INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
+#include <dali-toolkit/internal/visuals/visual-string-constants.h>
 
 namespace Dali
 {
@@ -34,47 +39,52 @@ namespace Internal
 
 namespace
 {
-//custom shader
-const char * const CUSTOM_SHADER( "shader" );
-const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
-const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
-const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
-const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
-const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type STRING for a hint from the below hint strings or an ARRAY of of hint strings
-
-/**
- * where hints should be contain strings of the following shader hints:
- *   "none"                    | corresponds to Shader::Hint::NONE
- *   "outputIsTransparent"     | corresponds to Shader::Hint::OUTPUT_IS_TRANSPARENT
- *   "modifiesGeometry"        | corresponds to Shader::Hint::MODIFIES_GEOMETRY
- */
 
-Shader::Hint::Value HintFromString( std::string hintString )
+DALI_ENUM_TO_STRING_TABLE_BEGIN( SHADER_HINT )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, NONE )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, OUTPUT_IS_TRANSPARENT )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Shader::Hint, MODIFIES_GEOMETRY )
+DALI_ENUM_TO_STRING_TABLE_END( SHADER_HINT )
+
+DALI_ENUM_TO_STRING_TABLE_BEGIN( ALIGN )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, TOP_BEGIN )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, TOP_CENTER )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, TOP_END )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, CENTER_BEGIN )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, CENTER )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, CENTER_END )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, BOTTOM_BEGIN )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, BOTTOM_CENTER )
+DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Align, BOTTOM_END )
+DALI_ENUM_TO_STRING_TABLE_END( ALIGN )
+
+Dali::Vector2 PointToVector2( Toolkit::Align::Type point, Toolkit::Direction::Type direction )
 {
-  if( hintString == "none" )
-  {
-    return Shader::Hint::NONE;
-  }
-  else if( hintString == "outputIsTransparent" )
-  {
-    return Shader::Hint::OUTPUT_IS_TRANSPARENT;
-  }
-  else if( hintString == "modifiesGeometry" )
-  {
-    return Shader::Hint::MODIFIES_GEOMETRY;
-  }
-  else
+  static const float pointToVector2[] = { 0.0f,0.0f,
+                                          0.5f,0.0f,
+                                          1.0f,0.0f,
+                                          0.0f,0.5f,
+                                          0.5f,0.5f,
+                                          1.0f,0.5f,
+                                          0.0f,1.0f,
+                                          0.5f,1.0f,
+                                          1.0f,1.0f };
+
+  Vector2 result( &pointToVector2[point*2] );
+  if( direction == Direction::RIGHT_TO_LEFT )
   {
-    DALI_LOG_ERROR( "'%s' hint string is not recognised", hintString.c_str() );
+    result.x = 1.0f - result.x;
   }
 
-  return Shader::Hint::NONE;
+  return result;
 }
 
-}// unnamed namespace
+} // unnamed namespace
 
 Internal::Visual::Base::Impl::Impl()
-: mCustomShader(NULL),
+: mCustomShader( NULL ),
+  mTransform(),
+  mControlSize( Vector2::ZERO ),
   mDepthIndex( 0.0f ),
   mFlags( 0 )
 {
@@ -99,31 +109,31 @@ void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property:
   mGridSize = ImageDimensions( 1, 1 );
   mHints = Shader::Hint::NONE;
 
-  Property::Value* vertexShaderValue = shaderMap.Find( CUSTOM_VERTEX_SHADER );
+  Property::Value* vertexShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::VERTEX_SHADER, CUSTOM_VERTEX_SHADER );
   if( vertexShaderValue )
   {
     if( !vertexShaderValue->Get( mVertexShader ) )
     {
-      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_VERTEX_SHADER );
+      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_VERTEX_SHADER );
     }
   }
 
-  Property::Value* fragmentShaderValue = shaderMap.Find( CUSTOM_FRAGMENT_SHADER );
+  Property::Value* fragmentShaderValue = shaderMap.Find( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, CUSTOM_FRAGMENT_SHADER );
   if( fragmentShaderValue )
   {
     if( !fragmentShaderValue->Get( mFragmentShader ) )
     {
-      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_FRAGMENT_SHADER );
+      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string\n", CUSTOM_FRAGMENT_SHADER );
     }
   }
 
-  Property::Value* subdivideXValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_X );
+  Property::Value* subdivideXValue = shaderMap.Find( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X, CUSTOM_SUBDIVIDE_GRID_X );
   if( subdivideXValue )
   {
     int subdivideX;
     if( !subdivideXValue->Get( subdivideX ) || subdivideX < 1 )
     {
-      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_X );
+      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1\n", CUSTOM_SUBDIVIDE_GRID_X );
     }
     else
     {
@@ -131,13 +141,13 @@ void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property:
     }
   }
 
-  Property::Value* subdivideYValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_Y );
+  Property::Value* subdivideYValue = shaderMap.Find( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y, CUSTOM_SUBDIVIDE_GRID_Y );
   if( subdivideYValue )
   {
     int subdivideY;
     if( !subdivideYValue->Get( subdivideY ) || subdivideY < 1 )
     {
-      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_Y );
+      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1\n", CUSTOM_SUBDIVIDE_GRID_Y );
     }
     else
     {
@@ -145,37 +155,12 @@ void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property:
     }
   }
 
-  Property::Value* hintsValue = shaderMap.Find( CUSTOM_SHADER_HINTS );
+  Property::Value* hintsValue = shaderMap.Find( Toolkit::Visual::Shader::Property::HINTS, CUSTOM_SHADER_HINTS );
   if( hintsValue )
   {
-    std::string hintString;
-    Property::Array hintsArray;
-
-    if( hintsValue->Get( hintString ) )
-    {
-      mHints = HintFromString( hintString );
-    }
-    else if( hintsValue->Get( hintsArray ) )
+    if ( ! Scripting::GetBitmaskEnumerationProperty( *hintsValue, SHADER_HINT_TABLE, SHADER_HINT_TABLE_COUNT, mHints ) )
     {
-      int hints = Shader::Hint::NONE;
-      for( Property::Array::SizeType i = 0; i < hintsArray.Count(); ++i)
-      {
-        Property::Value hintValue = hintsArray[ i ];
-        if( hintValue.Get( hintString ) )
-        {
-          hints |= static_cast< int >( HintFromString( hintString ) );
-        }
-        else
-        {
-          DALI_LOG_ERROR( "'%s' parameter does not correctly specify an hint string at index %d", CUSTOM_SHADER_HINTS, i );
-        }
-
-        mHints = static_cast< Shader::Hint::Value >( hints );
-      }
-    }
-    else
-    {
-      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint string or an array of hint strings", CUSTOM_SHADER_HINTS );
+      DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint or an array of hint strings\n", CUSTOM_SHADER_HINTS );
     }
   }
 }
@@ -187,31 +172,109 @@ void Internal::Visual::Base::Impl::CustomShader::CreatePropertyMap( Property::Ma
     Property::Map customShader;
     if( !mVertexShader.empty() )
     {
-      customShader.Insert(CUSTOM_VERTEX_SHADER, mVertexShader );
+      customShader.Insert( Toolkit::Visual::Shader::Property::VERTEX_SHADER, mVertexShader );
     }
     if( !mFragmentShader.empty() )
     {
-      customShader.Insert(CUSTOM_FRAGMENT_SHADER, mFragmentShader );
+      customShader.Insert( Toolkit::Visual::Shader::Property::FRAGMENT_SHADER, mFragmentShader );
     }
 
     if( mGridSize.GetWidth() != 1 )
     {
-      customShader.Insert(CUSTOM_SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
+      customShader.Insert( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
     }
     if( mGridSize.GetHeight() != 1 )
     {
-      customShader.Insert(CUSTOM_SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
+      customShader.Insert( Toolkit::Visual::Shader::Property::SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
     }
 
     if( mHints != Dali::Shader::Hint::NONE )
     {
-      customShader.Insert(CUSTOM_SHADER_HINTS, static_cast< int >(mHints) );
+      customShader.Insert( Toolkit::Visual::Shader::Property::HINTS, static_cast< int >( mHints ) );
     }
 
-    map.Insert( CUSTOM_SHADER, customShader );
+    map.Insert( Toolkit::DevelVisual::Property::SHADER, customShader );
+  }
+}
+
+Internal::Visual::Base::Impl::Transform::Transform()
+: mOffset( 0.0f,0.0f ),
+  mSize( 1.0f,1.0f ),
+  mOffsetSizeMode( 0.0f,0.0f,0.0f,0.0f ),
+  mOrigin( Toolkit::Align::CENTER ),
+  mAnchorPoint( Toolkit::Align::CENTER )
+{
+}
+
+void Internal::Visual::Base::Impl::Transform::SetPropertyMap( const Property::Map& map )
+{
+  //Set default values
+  mOffset = Vector2( 0.0f,0.0f );
+  mSize = Vector2( 1.0f,1.0f );
+  mOffsetSizeMode = Vector4( 0.0f,0.0f,0.0f,0.0f );
+  mOrigin = Toolkit::Align::CENTER;
+  mAnchorPoint = Toolkit::Align::CENTER;
+
+  for( Property::Map::SizeType i(0); i<map.Count(); ++i )
+  {
+    KeyValuePair keyValue = map.GetKeyValue( i );
+    if( keyValue.first == Toolkit::DevelVisual::Transform::Property::OFFSET )
+    {
+      keyValue.second.Get( mOffset );
+    }
+    else if( keyValue.first == Toolkit::DevelVisual::Transform::Property::SIZE )
+    {
+      keyValue.second.Get( mSize );
+    }
+    else if( keyValue.first == Toolkit::DevelVisual::Transform::Property::ORIGIN )
+    {
+      Toolkit::Align::Type align(Toolkit::Align::CENTER);
+      if( Scripting::GetEnumerationProperty< Toolkit::Align::Type >( keyValue.second, ALIGN_TABLE, ALIGN_TABLE_COUNT, align ) )
+      {
+        mOrigin = align;
+      }
+    }
+    else if( keyValue.first == Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT )
+    {
+      Toolkit::Align::Type align(Toolkit::Align::CENTER);
+      if( Scripting::GetEnumerationProperty< Toolkit::Align::Type >( keyValue.second, ALIGN_TABLE, ALIGN_TABLE_COUNT, align ) )
+      {
+        mAnchorPoint = align;
+      }
+    }
+    else if( keyValue.first == Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE )
+    {
+      keyValue.second.Get( mOffsetSizeMode );
+    }
   }
 }
 
+void Internal::Visual::Base::Impl::Transform::GetPropertyMap( Property::Map& map ) const
+{
+  map.Clear();
+  map.Add( Toolkit::DevelVisual::Transform::Property::OFFSET, mOffset )
+     .Add( Toolkit::DevelVisual::Transform::Property::SIZE, mSize )
+     .Add( Toolkit::DevelVisual::Transform::Property::ORIGIN, mOrigin )
+     .Add( Toolkit::DevelVisual::Transform::Property::ANCHOR_POINT, mAnchorPoint )
+     .Add( Toolkit::DevelVisual::Transform::Property::OFFSET_SIZE_MODE, mOffsetSizeMode );
+}
+
+void Internal::Visual::Base::Impl::Transform::RegisterUniforms( Dali::Renderer renderer, Toolkit::Direction::Type direction )
+{
+  renderer.RegisterProperty( SIZE, mSize );
+  renderer.RegisterProperty( OFFSET, direction == Toolkit::Direction::LEFT_TO_RIGHT ? mOffset : mOffset * Vector2(-1.0f,1.0f));
+  renderer.RegisterProperty( OFFSET_SIZE_MODE, mOffsetSizeMode );
+  renderer.RegisterProperty( ORIGIN, PointToVector2( mOrigin, direction ) - Vector2(0.5,0.5) );
+  renderer.RegisterProperty( ANCHOR_POINT, Vector2(0.5,0.5) - PointToVector2( mAnchorPoint, direction ) );
+}
+
+Vector2 Internal::Visual::Base::Impl::Transform::GetVisualSize( const Vector2& controlSize )
+{
+  return Vector2( Lerp( mOffsetSizeMode.x, mSize.x * controlSize.x, mSize.x ),
+                  Lerp( mOffsetSizeMode.y, mSize.y * controlSize.y, mSize.y ) );
+}
+
+
 } // namespace Internal
 
 } // namespace Toolkit