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("renderer-type");
41 const char * const RENDERER_TYPE_VALUE("color-renderer");
42 const char * const COLOR_NAME("blend-color");
43 const char * const COLOR_UNIFORM_NAME("uBlendColor");
46 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
47 attribute mediump vec2 aPosition;\n
48 uniform mediump mat4 uMvpMatrix;\n
49 uniform mediump vec3 uSize;\n
53 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
54 vertexPosition.xyz *= uSize;\n
55 gl_Position = uMvpMatrix * vertexPosition;\n
59 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
60 uniform lowp vec4 uColor;\n
61 uniform lowp vec4 uBlendColor;\n
65 gl_FragColor = uBlendColor*uColor;\n
70 ColorRenderer::ColorRenderer()
72 mBlendColorIndex( Property::INVALID_INDEX )
76 ColorRenderer::~ColorRenderer()
80 void ColorRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
82 Initialize( factoryCache );
84 Property::Value* color = propertyMap.Find( COLOR_NAME );
85 if( !( color && color->Get(mBlendColor) ) )
87 DALI_LOG_ERROR( "Fail to provide a color to the ColorRenderer object" );
91 void ColorRenderer::SetSize( const Vector2& size )
93 ControlRenderer::SetSize( size );
95 // ToDo: renderer responds to the size change
98 void ColorRenderer::SetClipRect( const Rect<int>& clipRect )
100 ControlRenderer::SetClipRect( clipRect );
102 //ToDo: renderer responds to the clipRect change
105 void ColorRenderer::SetOffset( const Vector2& offset )
107 //ToDo: renderer applies the offset
110 void ColorRenderer::CreatePropertyMap( Property::Map& map ) const
113 map.Insert( RENDERER_TYPE, RENDERER_TYPE_VALUE );
114 map.Insert( COLOR_NAME, mBlendColor );
117 void ColorRenderer::DoSetOnStage( Actor& actor )
119 mBlendColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_UNIFORM_NAME, mBlendColor );
120 if( mBlendColor.a < 1.f )
122 (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
126 void ColorRenderer::Initialize( RendererFactoryCache& factoryCache)
128 mImpl->mGeometry = factoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
129 if( !(mImpl->mGeometry) )
131 mImpl->mGeometry = RendererFactoryCache::CreateQuadGeometry();
132 factoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, mImpl->mGeometry );
135 mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::COLOR_SHADER );
136 if( !(mImpl->mShader) )
138 mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
139 factoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, mImpl->mShader );
143 void ColorRenderer::SetColor(const Vector4& color)
147 if( mImpl->mIsOnStage )
149 (mImpl->mRenderer).SetProperty( mBlendColorIndex, color );
150 if( color.a < 1.f && (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
152 (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
157 } // namespace Internal
159 } // namespace Toolkit