Revert public API changes. Handle methods.
[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 )
84 {
85   return new ColorVisual( factoryCache );
86 }
87
88 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
89 : Visual::Base( factoryCache ),
90   mMixColorIndex( Property::INVALID_INDEX )
91 {
92 }
93
94 ColorVisual::~ColorVisual()
95 {
96 }
97
98 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
99 {
100   Property::Value* color = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME );
101   if( !( color && color->Get(mMixColor) ) )
102   {
103     DALI_LOG_ERROR( "Fail to provide a color to the ColorVisual object\n" );
104   }
105 }
106
107 void ColorVisual::DoSetOnStage( Actor& actor )
108 {
109   InitializeRenderer();
110
111   actor.AddRenderer( mImpl->mRenderer );
112 }
113
114 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
115 {
116   map.Clear();
117   map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::COLOR );
118   map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mMixColor );
119 }
120
121 void ColorVisual::OnSetTransform()
122 {
123   if( mImpl->mRenderer )
124   {
125     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
126   }
127 }
128
129 void ColorVisual::InitializeRenderer()
130 {
131   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
132   if( !geometry )
133   {
134     geometry =  VisualFactoryCache::CreateQuadGeometry();
135     mFactoryCache.SaveGeometry( VisualFactoryCache::QUAD_GEOMETRY, geometry );
136   }
137
138   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
139   if( !shader )
140   {
141     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
142     mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
143   }
144
145   mImpl->mRenderer = Renderer::New( geometry, shader );
146
147   mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, COLOR_NAME, mMixColor );
148   if( mMixColor.a < 1.f )
149   {
150     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
151   }
152
153   //Register transform properties
154   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
155 }
156
157 void ColorVisual::SetColor(const Vector4& color)
158 {
159   mMixColor = color;
160
161   if( mImpl->mRenderer )
162   {
163     (mImpl->mRenderer).SetProperty( mMixColorIndex, color );
164     if( color.a < 1.f )
165     {
166       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
167     }
168   }
169 }
170
171 } // namespace Internal
172
173 } // namespace Toolkit
174
175 } // namespace Dali