Merge "Fix PageFactory to take in Texture rather than Image" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2016 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 "color-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
26 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
27 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43 const char * const COLOR_NAME("mixColor");
44
45 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
46   attribute mediump vec2 aPosition;\n
47   uniform mediump mat4 uMvpMatrix;\n
48   uniform mediump vec3 uSize;\n
49   \n
50   void main()\n
51   {\n
52     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
53     vertexPosition.xyz *= uSize;\n
54     gl_Position = uMvpMatrix * vertexPosition;\n
55   }\n
56 );
57
58 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
59   uniform lowp vec4 uColor;\n
60   uniform lowp vec4 mixColor;\n
61   \n
62   void main()\n
63   {\n
64     gl_FragColor = mixColor*uColor;\n
65   }\n
66 );
67 }
68
69 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache )
70 {
71   return new ColorVisual( factoryCache );
72 }
73
74 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
75 : Visual::Base( factoryCache ),
76   mMixColorIndex( Property::INVALID_INDEX )
77 {
78 }
79
80 ColorVisual::~ColorVisual()
81 {
82 }
83
84 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
85 {
86   Property::Value* color = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME );
87   if( !( color && color->Get(mMixColor) ) )
88   {
89     DALI_LOG_ERROR( "Fail to provide a color to the ColorVisual object\n" );
90   }
91 }
92
93 void ColorVisual::SetSize( const Vector2& size )
94 {
95   Visual::Base::SetSize( size );
96
97   // ToDo: renderer responds to the size change
98 }
99
100 void ColorVisual::DoSetOnStage( Actor& actor )
101 {
102   InitializeRenderer();
103
104   actor.AddRenderer( mImpl->mRenderer );
105 }
106
107 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
108 {
109   map.Clear();
110   map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::COLOR );
111   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mMixColor );
112 }
113
114 void ColorVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
115 {
116   // TODO
117   /* David Steele comented :
118
119      Some things to bear in mind:
120
121      We currently keep a copy of the mix color in the ColorVisual object, which is then used to instantiate the registered property on the renderer.
122
123      The user can get the renderer and animate the mixColor property (it's registered, so is automatically a scene-graph property).
124
125      The GetProperty method will have to read from the renderer, or from the cached value in the Visual, and the SetProperty will have to write to cache and to the renderer if present.
126   */
127 }
128
129 Dali::Property::Value ColorVisual::DoGetProperty( Dali::Property::Index index )
130 {
131   // TODO
132   return Dali::Property::Value();
133 }
134
135 void ColorVisual::InitializeRenderer()
136 {
137   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
138   if( !geometry )
139   {
140     geometry =  VisualFactoryCache::CreateQuadGeometry();
141     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
142   }
143
144   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
145   if( !shader )
146   {
147     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
148     mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
149   }
150
151   mImpl->mRenderer = Renderer::New( geometry, shader );
152
153   mMixColorIndex = mImpl->mRenderer.RegisterProperty( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME, mMixColor );
154   if( mMixColor.a < 1.f )
155   {
156     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
157   }
158 }
159
160 void ColorVisual::SetColor(const Vector4& color)
161 {
162   mMixColor = color;
163
164   if( mImpl->mRenderer )
165   {
166     (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
167     if( color.a < 1.f )
168     {
169       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
170     }
171   }
172 }
173
174 } // namespace Internal
175
176 } // namespace Toolkit
177
178 } // namespace Dali