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-visual.h"
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
26 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
27 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
28 #include <dali-toolkit/internal/visuals/visual-data-impl.h>
41 const char * const COLOR_NAME("mixColor");
43 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
44 attribute mediump vec2 aPosition;\n
45 uniform mediump mat4 uMvpMatrix;\n
46 uniform mediump vec3 uSize;\n
50 mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
51 vertexPosition.xyz *= uSize;\n
52 gl_Position = uMvpMatrix * vertexPosition;\n
56 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
57 uniform lowp vec4 uColor;\n
58 uniform lowp vec4 mixColor;\n
62 gl_FragColor = mixColor*uColor;\n
67 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
68 : Visual( factoryCache ),
69 mMixColorIndex( Property::INVALID_INDEX )
73 ColorVisual::~ColorVisual()
77 void ColorVisual::DoInitialize( Actor& actor, const Property::Map& propertyMap )
79 Property::Value* color = propertyMap.Find( COLOR_NAME );
80 if( !( color && color->Get(mMixColor) ) )
82 DALI_LOG_ERROR( "Fail to provide a color to the ColorVisual object" );
86 void ColorVisual::SetSize( const Vector2& size )
88 Visual::SetSize( size );
90 // ToDo: renderer responds to the size change
93 void ColorVisual::SetClipRect( const Rect<int>& clipRect )
95 Visual::SetClipRect( clipRect );
97 //ToDo: renderer responds to the clipRect change
100 void ColorVisual::SetOffset( const Vector2& offset )
102 //ToDo: renderer applies the offset
105 void ColorVisual::DoSetOnStage( Actor& actor )
107 InitializeRenderer();
110 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
113 map.Insert( RENDERER_TYPE, COLOR_RENDERER );
114 map.Insert( COLOR_NAME, mMixColor );
117 void ColorVisual::InitializeRenderer()
119 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
122 geometry = VisualFactoryCache::CreateQuadGeometry();
123 mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
126 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
129 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
130 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
133 mImpl->mRenderer = Renderer::New( geometry, shader );
135 mMixColorIndex = mImpl->mRenderer.RegisterProperty( COLOR_NAME, mMixColor );
136 if( mMixColor.a < 1.f )
138 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
142 void ColorVisual::SetColor(const Vector4& color)
146 if( mImpl->mRenderer )
148 (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
151 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
156 } // namespace Internal
158 } // namespace Toolkit