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