Merge "Add KeyboardFocus History Stack" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2016 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-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
30 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 const char * const COLOR_NAME("mixColor");
45
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
50   \n
51
52   //Visual size and offset
53   uniform mediump vec2 offset;\n
54   uniform mediump vec2 size;\n
55   uniform mediump vec4 offsetSizeMode;\n
56   uniform mediump vec2 origin;\n
57   uniform mediump vec2 anchorPoint;\n
58
59   vec4 ComputeVertexPosition()\n
60   {\n
61     vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
62     vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
63     return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
64   }\n
65
66   void main()\n
67   {\n
68     gl_Position = uMvpMatrix * ComputeVertexPosition();\n
69   }\n
70 );
71
72 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
73   uniform lowp vec4 uColor;\n
74   uniform lowp vec4 mixColor;\n
75   \n
76   void main()\n
77   {\n
78     gl_FragColor = mixColor*uColor;\n
79   }\n
80 );
81 }
82
83 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
84 {
85   ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
86   colorVisualPtr->SetProperties( properties );
87   return colorVisualPtr;
88 }
89
90 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
91 : Visual::Base( factoryCache ),
92   mMixColorIndex( Property::INVALID_INDEX )
93 {
94 }
95
96 ColorVisual::~ColorVisual()
97 {
98 }
99
100 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
101 {
102   Property::Value* color = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME );
103   if( !( color && color->Get(mMixColor) ) )
104   {
105     DALI_LOG_ERROR( "Fail to provide a color to the ColorVisual object\n" );
106   }
107 }
108
109 void ColorVisual::DoSetOnStage( Actor& actor )
110 {
111   InitializeRenderer();
112
113   actor.AddRenderer( mImpl->mRenderer );
114 }
115
116 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
117 {
118   map.Clear();
119   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::COLOR );
120   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mMixColor );
121 }
122
123 void ColorVisual::OnSetTransform()
124 {
125   if( mImpl->mRenderer )
126   {
127     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
128   }
129 }
130
131 void ColorVisual::InitializeRenderer()
132 {
133   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
134   if( !geometry )
135   {
136     geometry =  VisualFactoryCache::CreateQuadGeometry();
137     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
138   }
139
140   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
141   if( !shader )
142   {
143     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
144     mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
145   }
146
147   mImpl->mRenderer = Renderer::New( geometry, shader );
148
149   mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME, mMixColor );
150   if( mMixColor.a < 1.f )
151   {
152     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
153   }
154
155   //Register transform properties
156   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
157 }
158
159 void ColorVisual::SetColor(const Vector4& color)
160 {
161   mMixColor = color;
162
163   if( mImpl->mRenderer )
164   {
165     (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
166     if( color.a < 1.f )
167     {
168       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
169     }
170   }
171 }
172
173 } // namespace Internal
174
175 } // namespace Toolkit
176
177 } // namespace Dali