Merge "Fixed an issue where the multi-tap did not work properly. (https://review...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / color / color-visual.cpp
1 /*
2  * Copyright (c) 2021 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/devel-api/rendering/renderer-devel.h>
23 #include <dali/integration-api/debug.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/visuals/color-visual-actions-devel.h>
27 #include <dali-toolkit/devel-api/visuals/color-visual-properties-devel.h>
28 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
29 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
30 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
31 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
34 #include <dali-toolkit/public-api/visuals/visual-properties.h>
35
36 namespace Dali
37 {
38 namespace Toolkit
39 {
40 namespace Internal
41 {
42 ColorVisualPtr ColorVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
43 {
44   ColorVisualPtr colorVisualPtr(new ColorVisual(factoryCache));
45   colorVisualPtr->SetProperties(properties);
46   colorVisualPtr->Initialize();
47   return colorVisualPtr;
48 }
49
50 ColorVisual::ColorVisual(VisualFactoryCache& factoryCache)
51 : Visual::Base(factoryCache, Visual::FittingMode::FILL, Toolkit::Visual::COLOR),
52   mBlurRadius(0.0f),
53   mBlurRadiusIndex(Property::INVALID_INDEX),
54   mNeedBlurRadius(false)
55 {
56 }
57
58 ColorVisual::~ColorVisual()
59 {
60 }
61
62 void ColorVisual::DoSetProperties(const Property::Map& propertyMap)
63 {
64   // By virtue of DoSetProperties being called last, this will override
65   // anything set by Toolkit::Visual::Property::MIX_COLOR
66   Property::Value* colorValue = propertyMap.Find(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR);
67   if(colorValue)
68   {
69     Vector4 color;
70     if(colorValue->Get(color))
71     {
72       Property::Type type = colorValue->GetType();
73       if(type == Property::VECTOR4)
74       {
75         SetMixColor(color);
76       }
77       else if(type == Property::VECTOR3)
78       {
79         Vector3 color3(color);
80         SetMixColor(color3);
81       }
82     }
83     else
84     {
85       DALI_LOG_ERROR("ColorVisual: mixColor property has incorrect type\n");
86     }
87   }
88
89   Property::Value* blurRadiusValue = propertyMap.Find(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME);
90   if(blurRadiusValue)
91   {
92     if(!blurRadiusValue->Get(mBlurRadius))
93     {
94       DALI_LOG_ERROR("ColorVisual:DoSetProperties:: BLUR_RADIUS property has incorrect type: %d\n", blurRadiusValue->GetType());
95     }
96   }
97 }
98
99 void ColorVisual::DoSetOnScene(Actor& actor)
100 {
101   actor.AddRenderer(mImpl->mRenderer);
102
103   // Color Visual generated and ready to display
104   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
105 }
106
107 void ColorVisual::DoSetOffScene(Actor& actor)
108 {
109   actor.RemoveRenderer(mImpl->mRenderer);
110 }
111
112 void ColorVisual::DoCreatePropertyMap(Property::Map& map) const
113 {
114   map.Clear();
115   map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
116   map.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, mImpl->mMixColor);
117
118   if(mImpl->mRenderer && mBlurRadiusIndex != Property::INVALID_INDEX)
119   {
120     // Update values from Renderer
121     float blurRadius = mImpl->mRenderer.GetProperty<float>(mBlurRadiusIndex);
122     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, blurRadius);
123   }
124   else
125   {
126     map.Insert(Toolkit::DevelColorVisual::Property::BLUR_RADIUS, mBlurRadius);
127   }
128 }
129
130 void ColorVisual::DoCreateInstancePropertyMap(Property::Map& map) const
131 {
132   // Do nothing
133 }
134
135 void ColorVisual::OnSetTransform()
136 {
137   if(mImpl->mRenderer)
138   {
139     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
140   }
141 }
142
143 void ColorVisual::OnDoAction(const Property::Index actionId, const Property::Value& attributes)
144 {
145   // Check if action is valid for this visual type and perform action if possible
146   switch(actionId)
147   {
148     case DevelColorVisual::Action::UPDATE_PROPERTY:
149     {
150       const Property::Map* map = attributes.GetMap();
151       if(map)
152       {
153         DoSetProperties(*map);
154       }
155       break;
156     }
157   }
158 }
159
160 void ColorVisual::UpdateShader()
161 {
162   if(mImpl->mRenderer)
163   {
164     Shader shader = GetShader();
165     mImpl->mRenderer.SetShader(shader);
166   }
167 }
168
169 void ColorVisual::OnInitialize()
170 {
171   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY);
172
173   Shader shader = GetShader();
174
175   mImpl->mRenderer = Renderer::New(geometry, shader);
176
177   // ColorVisual has it's own index key for mix color - use this instead
178   // of using the new base index to avoid changing existing applications
179   // String keys will get to this property.
180   mImpl->mMixColorIndex = mImpl->mRenderer.RegisterProperty(Toolkit::ColorVisual::Property::MIX_COLOR, MIX_COLOR, Vector3(mImpl->mMixColor));
181
182   if(!EqualsZero(mBlurRadius))
183   {
184     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
185     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
186   }
187
188   // Register transform properties
189   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
190 }
191
192 Shader ColorVisual::GetShader()
193 {
194   Shader shader;
195   if(!EqualsZero(mBlurRadius) || mNeedBlurRadius)
196   {
197     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER_BLUR_EDGE);
198     if(!shader)
199     {
200       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_BLUR_EDGE_SHADER_FRAG.data());
201       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER_BLUR_EDGE, shader);
202     }
203   }
204   else if(!IsRoundedCornerRequired())
205   {
206     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER);
207     if(!shader)
208     {
209       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_SHADER_FRAG.data());
210       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER, shader);
211     }
212   }
213   else
214   {
215     shader = mFactoryCache.GetShader(VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER);
216     if(!shader)
217     {
218       shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_COLOR_VISUAL_ROUNDED_CORNER_SHADER_FRAG.data());
219       mFactoryCache.SaveShader(VisualFactoryCache::COLOR_SHADER_ROUNDED_CORNER, shader);
220     }
221   }
222
223   return shader;
224 }
225
226 Dali::Property ColorVisual::OnGetPropertyObject(Dali::Property::Key key)
227 {
228   if(!mImpl->mRenderer)
229   {
230     Handle handle;
231     return Dali::Property(handle, Property::INVALID_INDEX);
232   }
233
234   if((key.type == Property::Key::INDEX && key.indexKey == DevelColorVisual::Property::BLUR_RADIUS) || (key.type == Property::Key::STRING && key.stringKey == BLUR_RADIUS_NAME))
235   {
236     mBlurRadiusIndex = mImpl->mRenderer.RegisterProperty(DevelColorVisual::Property::BLUR_RADIUS, BLUR_RADIUS_NAME, mBlurRadius);
237
238     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
239
240     mNeedBlurRadius = true;
241
242     // Change shader
243     UpdateShader();
244
245     return Dali::Property(mImpl->mRenderer, mBlurRadiusIndex);
246   }
247
248   Handle handle;
249   return Dali::Property(handle, Property::INVALID_INDEX);
250 }
251
252 } // namespace Internal
253
254 } // namespace Toolkit
255
256 } // namespace Dali