Adding Visual namespace and Visual becomes Visual::Base
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-data-impl.cpp
1 /*
2  * Copyright (c) 2015 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/visuals/visual-base-data-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/object/property-array.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37 //custom shader
38 const char * const CUSTOM_SHADER( "shader" );
39 const char * const CUSTOM_VERTEX_SHADER( "vertexShader" );
40 const char * const CUSTOM_FRAGMENT_SHADER( "fragmentShader" );
41 const char * const CUSTOM_SUBDIVIDE_GRID_X( "subdivideGridX" );
42 const char * const CUSTOM_SUBDIVIDE_GRID_Y( "subdivideGridY" );
43 const char * const CUSTOM_SHADER_HINTS( "hints" ); ///< type STRING for a hint from the below hint strings or an ARRAY of of hint strings
44
45 /**
46  * where hints should be contain strings of the following shader hints:
47  *   "none"                    | corresponds to Shader::Hint::NONE
48  *   "outputIsTransparent"     | corresponds to Shader::Hint::OUTPUT_IS_TRANSPARENT
49  *   "modifiesGeometry"        | corresponds to Shader::Hint::MODIFIES_GEOMETRY
50  */
51
52 Shader::Hint::Value HintFromString( std::string hintString )
53 {
54   if( hintString == "none" )
55   {
56     return Shader::Hint::NONE;
57   }
58   else if( hintString == "outputIsTransparent" )
59   {
60     return Shader::Hint::OUTPUT_IS_TRANSPARENT;
61   }
62   else if( hintString == "modifiesGeometry" )
63   {
64     return Shader::Hint::MODIFIES_GEOMETRY;
65   }
66   else
67   {
68     DALI_LOG_ERROR( "'%s' hint string is not recognised", hintString.c_str() );
69   }
70
71   return Shader::Hint::NONE;
72 }
73
74 }// unnamed namespace
75
76 Internal::Visual::Base::Impl::Impl()
77 : mCustomShader(NULL),
78   mDepthIndex( 0.0f ),
79   mFlags( 0 )
80 {
81 }
82
83 Internal::Visual::Base::Impl::~Impl()
84 {
85   delete mCustomShader;
86 }
87
88 Internal::Visual::Base::Impl::CustomShader::CustomShader( const Property::Map& map )
89 : mGridSize( 1, 1 ),
90   mHints( Shader::Hint::NONE )
91 {
92   SetPropertyMap( map );
93 }
94
95 void Internal::Visual::Base::Impl::CustomShader::SetPropertyMap( const Property::Map& shaderMap )
96 {
97   mVertexShader.clear();
98   mFragmentShader.clear();
99   mGridSize = ImageDimensions( 1, 1 );
100   mHints = Shader::Hint::NONE;
101
102   Property::Value* vertexShaderValue = shaderMap.Find( CUSTOM_VERTEX_SHADER );
103   if( vertexShaderValue )
104   {
105     if( !vertexShaderValue->Get( mVertexShader ) )
106     {
107       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_VERTEX_SHADER );
108     }
109   }
110
111   Property::Value* fragmentShaderValue = shaderMap.Find( CUSTOM_FRAGMENT_SHADER );
112   if( fragmentShaderValue )
113   {
114     if( !fragmentShaderValue->Get( mFragmentShader ) )
115     {
116       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a string", CUSTOM_FRAGMENT_SHADER );
117     }
118   }
119
120   Property::Value* subdivideXValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_X );
121   if( subdivideXValue )
122   {
123     int subdivideX;
124     if( !subdivideXValue->Get( subdivideX ) || subdivideX < 1 )
125     {
126       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_X );
127     }
128     else
129     {
130       mGridSize = ImageDimensions( subdivideX, mGridSize.GetY() );
131     }
132   }
133
134   Property::Value* subdivideYValue = shaderMap.Find( CUSTOM_SUBDIVIDE_GRID_Y );
135   if( subdivideYValue )
136   {
137     int subdivideY;
138     if( !subdivideYValue->Get( subdivideY ) || subdivideY < 1 )
139     {
140       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a value greater than 1", CUSTOM_SUBDIVIDE_GRID_Y );
141     }
142     else
143     {
144       mGridSize = ImageDimensions( mGridSize.GetX(), subdivideY );
145     }
146   }
147
148   Property::Value* hintsValue = shaderMap.Find( CUSTOM_SHADER_HINTS );
149   if( hintsValue )
150   {
151     std::string hintString;
152     Property::Array hintsArray;
153
154     if( hintsValue->Get( hintString ) )
155     {
156       mHints = HintFromString( hintString );
157     }
158     else if( hintsValue->Get( hintsArray ) )
159     {
160       int hints = Shader::Hint::NONE;
161       for( Property::Array::SizeType i = 0; i < hintsArray.Count(); ++i)
162       {
163         Property::Value hintValue = hintsArray[ i ];
164         if( hintValue.Get( hintString ) )
165         {
166           hints |= static_cast< int >( HintFromString( hintString ) );
167         }
168         else
169         {
170           DALI_LOG_ERROR( "'%s' parameter does not correctly specify an hint string at index %d", CUSTOM_SHADER_HINTS, i );
171         }
172
173         mHints = static_cast< Shader::Hint::Value >( hints );
174       }
175     }
176     else
177     {
178       DALI_LOG_ERROR( "'%s' parameter does not correctly specify a hint string or an array of hint strings", CUSTOM_SHADER_HINTS );
179     }
180   }
181 }
182
183 void Internal::Visual::Base::Impl::CustomShader::CreatePropertyMap( Property::Map& map ) const
184 {
185   if( !mVertexShader.empty() || !mFragmentShader.empty() )
186   {
187     Property::Map customShader;
188     if( !mVertexShader.empty() )
189     {
190       customShader.Insert(CUSTOM_VERTEX_SHADER, mVertexShader );
191     }
192     if( !mFragmentShader.empty() )
193     {
194       customShader.Insert(CUSTOM_FRAGMENT_SHADER, mFragmentShader );
195     }
196
197     if( mGridSize.GetWidth() != 1 )
198     {
199       customShader.Insert(CUSTOM_SUBDIVIDE_GRID_X, mGridSize.GetWidth() );
200     }
201     if( mGridSize.GetHeight() != 1 )
202     {
203       customShader.Insert(CUSTOM_SUBDIVIDE_GRID_Y, mGridSize.GetHeight() );
204     }
205
206     if( mHints != Dali::Shader::Hint::NONE )
207     {
208       customShader.Insert(CUSTOM_SHADER_HINTS, static_cast< int >(mHints) );
209     }
210
211     map.Insert( CUSTOM_SHADER, customShader );
212   }
213 }
214
215 } // namespace Internal
216
217 } // namespace Toolkit
218
219 } // namespace Dali