2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "color-visual.h"
22 #include <dali/integration-api/debug.h>
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>
42 const char * const COLOR_NAME("mixColor");
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
51 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
52 vertexPosition.xyz *= uSize;\n
53 gl_Position = uMvpMatrix * vertexPosition;\n
57 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
58 uniform lowp vec4 uColor;\n
59 uniform lowp vec4 mixColor;\n
63 gl_FragColor = mixColor*uColor;\n
68 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache )
70 return new ColorVisual( factoryCache );
73 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
74 : Visual::Base( factoryCache ),
75 mMixColorIndex( Property::INVALID_INDEX )
79 ColorVisual::~ColorVisual()
83 void ColorVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
85 Property::Value* color = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME );
86 if( !( color && color->Get(mMixColor) ) )
88 DALI_LOG_ERROR( "Fail to provide a color to the ColorVisual object\n" );
92 void ColorVisual::SetSize( const Vector2& size )
94 Visual::Base::SetSize( size );
96 // ToDo: renderer responds to the size change
99 void ColorVisual::DoSetOnStage( Actor& actor )
101 InitializeRenderer();
103 actor.AddRenderer( mImpl->mRenderer );
106 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
109 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR );
110 map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mMixColor );
113 void ColorVisual::DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue )
116 /* David Steele comented :
118 Some things to bear in mind:
120 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 The user can get the renderer and animate the mixColor property (it's registered, so is automatically a scene-graph property).
124 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.
128 Dali::Property::Value ColorVisual::DoGetProperty( Dali::Property::Index index )
131 return Dali::Property::Value();
134 void ColorVisual::InitializeRenderer()
136 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
139 geometry = VisualFactoryCache::CreateQuadGeometry();
140 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
143 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
146 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
147 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
150 mImpl->mRenderer = Renderer::New( geometry, shader );
152 mMixColorIndex = mImpl->mRenderer.RegisterProperty( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME, mMixColor );
153 if( mMixColor.a < 1.f )
155 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
159 void ColorVisual::SetColor(const Vector4& color)
163 if( mImpl->mRenderer )
165 (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
168 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
173 } // namespace Internal
175 } // namespace Toolkit