2 * Copyright (c) 2015 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-renderer.h"
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/controls/renderers/renderer-factory-impl.h>
26 #include <dali-toolkit/internal/controls/renderers/renderer-factory-cache.h>
27 #include <dali-toolkit/internal/controls/renderers/control-renderer-data-impl.h>
40 const char * const RENDERER_TYPE("rendererType");
41 const char * const RENDERER_TYPE_VALUE("color");
42 const char * const COLOR_NAME("blendColor");
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 blendColor;\n
63 gl_FragColor = blendColor*uColor;\n
68 ColorRenderer::ColorRenderer( RendererFactoryCache& factoryCache )
69 : ControlRenderer( factoryCache ),
70 mBlendColorIndex( Property::INVALID_INDEX )
74 ColorRenderer::~ColorRenderer()
78 void ColorRenderer::DoInitialize( Actor& actor, const Property::Map& propertyMap )
80 Property::Value* color = propertyMap.Find( COLOR_NAME );
81 if( !( color && color->Get(mBlendColor) ) )
83 DALI_LOG_ERROR( "Fail to provide a color to the ColorRenderer object" );
87 void ColorRenderer::SetSize( const Vector2& size )
89 ControlRenderer::SetSize( size );
91 // ToDo: renderer responds to the size change
94 void ColorRenderer::SetClipRect( const Rect<int>& clipRect )
96 ControlRenderer::SetClipRect( clipRect );
98 //ToDo: renderer responds to the clipRect change
101 void ColorRenderer::SetOffset( const Vector2& offset )
103 //ToDo: renderer applies the offset
106 void ColorRenderer::DoSetOnStage( Actor& actor )
108 InitializeRenderer();
111 void ColorRenderer::DoCreatePropertyMap( Property::Map& map ) const
114 map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
115 map.Insert( COLOR_NAME, mBlendColor );
118 void ColorRenderer::InitializeRenderer()
120 Geometry geometry = mFactoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
123 geometry = RendererFactoryCache::CreateQuadGeometry();
124 mFactoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, geometry );
127 Shader shader = mFactoryCache.GetShader( RendererFactoryCache::COLOR_SHADER );
130 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
131 mFactoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, shader );
134 Material material = Material::New( shader );
135 mImpl->mRenderer = Renderer::New( geometry, material );
137 mBlendColorIndex = mImpl->mRenderer.RegisterProperty( COLOR_NAME, mBlendColor );
138 if( mBlendColor.a < 1.f )
140 mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
144 void ColorRenderer::SetColor(const Vector4& color)
148 if( mImpl->mRenderer )
150 (mImpl->mRenderer).SetProperty( mBlendColorIndex, color );
153 mImpl->mRenderer.SetProperty( Renderer::Property::BLENDING_MODE, BlendingMode::ON );
158 } // namespace Internal
160 } // namespace Toolkit