(Control base)Use RendererFactory&ControlRenderer to handle background
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / color / color-renderer.cpp
1 /*
2  * Copyright (c) 2015 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-renderer.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
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>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 const char * const COLOR_NAME("blend-color");
41 const char * const COLOR_UNIFORM_NAME("uBlendColor");
42
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 uBlendColor;\n
60   \n
61   void main()\n
62   {\n
63     gl_FragColor = uBlendColor*uColor;\n
64   }\n
65 );
66 }
67
68 ColorRenderer::ColorRenderer()
69 : ControlRenderer(),
70   mBlendColorIndex( Property::INVALID_INDEX )
71 {
72 }
73
74 ColorRenderer::~ColorRenderer()
75 {
76 }
77
78 void ColorRenderer::Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap )
79 {
80   Initialize( factoryCache );
81
82   Property::Value* color = propertyMap.Find( COLOR_NAME );
83   if( !( color && color->Get(mBlendColor) ) )
84   {
85     DALI_LOG_ERROR( "Fail to provide a color to the ColorRenderer object" );
86   }
87 }
88
89 void ColorRenderer::SetSize( const Vector2& size )
90 {
91   ControlRenderer::SetSize( size );
92
93   // ToDo: renderer responds to the size change
94 }
95
96 void ColorRenderer::SetClipRect( const Rect<int>& clipRect )
97 {
98   ControlRenderer::SetClipRect( clipRect );
99
100   //ToDo: renderer responds to the clipRect change
101 }
102
103 void ColorRenderer::SetOffset( const Vector2& offset )
104 {
105   //ToDo: renderer applies the offset
106 }
107
108 void ColorRenderer::DoSetOnStage( Actor& actor )
109 {
110   mBlendColorIndex = (mImpl->mRenderer).RegisterProperty( COLOR_UNIFORM_NAME, mBlendColor );
111   if( mBlendColor.a < 1.f )
112   {
113     (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
114   }
115 }
116
117 void ColorRenderer::Initialize( RendererFactoryCache& factoryCache)
118 {
119   mImpl->mGeometry = factoryCache.GetGeometry( RendererFactoryCache::QUAD_GEOMETRY );
120   if( !(mImpl->mGeometry) )
121   {
122     mImpl->mGeometry =  RendererFactoryCache::CreateQuadGeometry();
123     factoryCache.SaveGeometry( RendererFactoryCache::QUAD_GEOMETRY, mImpl->mGeometry );
124   }
125
126   mImpl->mShader = factoryCache.GetShader( RendererFactoryCache::COLOR_SHADER );
127   if( !(mImpl->mShader) )
128   {
129     mImpl->mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
130     factoryCache.SaveShader( RendererFactoryCache::COLOR_SHADER, mImpl->mShader );
131   }
132 }
133
134 void ColorRenderer::SetColor(const Vector4& color)
135 {
136   mBlendColor = color;
137
138   if( mImpl->mIsOnStage )
139   {
140     (mImpl->mRenderer).SetProperty( mBlendColorIndex, color );
141     if( color.a < 1.f &&  (mImpl->mRenderer).GetMaterial().GetBlendMode() != BlendingMode::ON)
142     {
143       (mImpl->mRenderer).GetMaterial().SetBlendMode( BlendingMode::ON );
144     }
145   }
146 }
147
148 } // namespace Internal
149
150 } // namespace Toolkit
151
152 } // namespace Dali