2 * Copyright (c) 2016 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>
23 #include <dali/devel-api/object/handle-devel.h>
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>
45 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
46 attribute mediump vec2 aPosition;\n
47 uniform mediump mat4 uMvpMatrix;\n
48 uniform mediump vec3 uSize;\n
51 //Visual size and offset
52 uniform mediump vec2 offset;\n
53 uniform mediump vec2 size;\n
54 uniform mediump vec4 offsetSizeMode;\n
55 uniform mediump vec2 origin;\n
56 uniform mediump vec2 anchorPoint;\n
58 vec4 ComputeVertexPosition()\n
60 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
61 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
62 return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
67 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
71 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
72 uniform lowp vec4 uColor;\n
73 uniform lowp vec4 mixColor;\n
77 gl_FragColor = mixColor*uColor;\n
82 ColorVisualPtr ColorVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
84 ColorVisualPtr colorVisualPtr( new ColorVisual( factoryCache ) );
85 colorVisualPtr->SetProperties( properties );
86 return colorVisualPtr;
89 ColorVisual::ColorVisual( VisualFactoryCache& factoryCache )
90 : Visual::Base( factoryCache )
94 ColorVisual::~ColorVisual()
98 void ColorVisual::DoSetProperties( const Property::Map& propertyMap )
100 // By virtue of DoSetProperties being called last, this will override
101 // anything set by DevelVisual::Property::MIX_COLOR
102 Property::Value* colorValue = propertyMap.Find( Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR );
106 if( colorValue->Get( color ) )
108 SetMixColor( color );
112 DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
117 void ColorVisual::DoSetOnStage( Actor& actor )
119 InitializeRenderer();
121 actor.AddRenderer( mImpl->mRenderer );
124 void ColorVisual::DoCreatePropertyMap( Property::Map& map ) const
127 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::COLOR );
128 map.Insert( Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor );
131 void ColorVisual::OnSetTransform()
133 if( mImpl->mRenderer )
135 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
139 void ColorVisual::InitializeRenderer()
141 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
143 Shader shader = mFactoryCache.GetShader( VisualFactoryCache::COLOR_SHADER );
146 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
147 mFactoryCache.SaveShader( VisualFactoryCache::COLOR_SHADER, shader );
150 mImpl->mRenderer = Renderer::New( geometry, shader );
152 // ColorVisual has it's own index key for mix color - use this instead
153 // of using the new base index to avoid changing existing applications
154 // String keys will get to this property.
155 mImpl->mMixColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, mImpl->mMixColor );
156 if( mImpl->mMixColor.a < 1.f )
158 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
161 // Register transform properties
162 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
165 } // namespace Internal
167 } // namespace Toolkit