Removed Texture Atlas for the GIF image.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / visual-base-impl.cpp
1 /*
2  * Copyright (c) 2017 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 "visual-base-impl.h"
20
21 // EXTERNAL HEADER
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/devel-api/object/handle-devel.h>
24 #include <dali/integration-api/debug.h>
25
26 //INTERNAL HEARDER
27 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
28 #include <dali-toolkit/public-api/visuals/primitive-visual-properties.h>
29 #include <dali-toolkit/public-api/visuals/visual-properties.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
31 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
32
33 namespace
34 {
35 #if defined(DEBUG_ENABLED)
36 Debug::Filter* gVisualBaseLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_VISUAL_BASE" );
37 #endif
38
39 const char * const PRE_MULTIPLIED_ALPHA_PROPERTY( "preMultipliedAlpha" );
40 }
41
42 namespace Dali
43 {
44
45 namespace Toolkit
46 {
47
48 namespace Internal
49 {
50
51 Visual::Base::Base( VisualFactoryCache& factoryCache )
52 : mImpl( new Impl() ),
53   mFactoryCache( factoryCache )
54 {
55 }
56
57 Visual::Base::~Base()
58 {
59   delete mImpl;
60 }
61
62 void Visual::Base::SetCustomShader( const Property::Map& shaderMap )
63 {
64   if( mImpl->mCustomShader )
65   {
66     mImpl->mCustomShader->SetPropertyMap( shaderMap );
67   }
68   else
69   {
70     mImpl->mCustomShader = new Impl::CustomShader( shaderMap );
71   }
72 }
73
74 void Visual::Base::SetProperties( const Property::Map& propertyMap )
75 {
76   for( size_t i = 0; i < propertyMap.Count(); ++i )
77   {
78     const KeyValuePair& pair = propertyMap.GetKeyValue( i );
79     const Property::Key& key = pair.first;
80     const Property::Value& value = pair.second;
81
82     Property::Key matchKey = key;
83     if( matchKey.type == Property::Key::STRING )
84     {
85       if( matchKey == CUSTOM_SHADER )
86       {
87         matchKey = Property::Key( Toolkit::Visual::Property::SHADER );
88       }
89       else if( matchKey == TRANSFORM )
90       {
91         matchKey = Property::Key( Toolkit::Visual::Property::TRANSFORM );
92       }
93       else if( matchKey == PREMULTIPLIED_ALPHA )
94       {
95         matchKey = Property::Key( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA );
96       }
97       else if( matchKey == MIX_COLOR )
98       {
99         matchKey = Property::Key( Toolkit::Visual::Property::MIX_COLOR );
100       }
101       else if( matchKey == OPACITY )
102       {
103         matchKey = Property::Key( Toolkit::Visual::Property::OPACITY );
104       }
105     }
106
107     switch( matchKey.indexKey )
108     {
109       case Toolkit::Visual::Property::SHADER:
110       {
111         Property::Map shaderMap;
112         if( value.Get( shaderMap ) )
113         {
114           SetCustomShader( shaderMap );
115         }
116         break;
117       }
118
119       case Toolkit::Visual::Property::TRANSFORM:
120       {
121         Property::Map map;
122         if( value.Get( map ) )
123         {
124           mImpl->mTransform.SetPropertyMap( map );
125         }
126         break;
127       }
128
129       case Toolkit::Visual::Property::PREMULTIPLIED_ALPHA:
130       {
131         bool premultipliedAlpha = false;
132         if( value.Get( premultipliedAlpha ) )
133         {
134           EnablePreMultipliedAlpha( premultipliedAlpha );
135         }
136         break;
137       }
138
139       case Toolkit::Visual::Property::MIX_COLOR:
140       {
141         Vector4 mixColor;
142         if( value.Get( mixColor ) )
143         {
144           if( value.GetType() == Property::VECTOR4 )
145           {
146             SetMixColor( mixColor );
147           }
148           else
149           {
150             Vector3 mixColor3(mixColor);
151             SetMixColor( mixColor3 );
152           }
153         }
154         break;
155       }
156       case Toolkit::Visual::Property::OPACITY:
157       {
158         float opacity;
159         if( value.Get( opacity ) )
160         {
161           mImpl->mMixColor.a = opacity;
162           SetMixColor( mImpl->mMixColor );
163         }
164         break;
165       }
166     }
167   }
168
169   DoSetProperties( propertyMap );
170 }
171
172 void Visual::Base::SetTransformAndSize( const Property::Map& transform, Size controlSize )
173 {
174   mImpl->mControlSize = controlSize;
175   mImpl->mTransform.UpdatePropertyMap( transform );
176
177 #if defined(DEBUG_ENABLED)
178   std::ostringstream oss;
179   oss << transform;
180   DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::SetTransformAndSize(%s) - [\e[1;32mtransform: %s  controlSize: (%3.1f, %3.1f)]\e[0m\n",
181                  GetName().c_str(), oss.str().c_str(), controlSize.x, controlSize.y );
182 #endif
183
184   OnSetTransform();
185 }
186
187 void Visual::Base::SetName( const std::string& name )
188 {
189   mImpl->mName = name;
190 }
191
192 const std::string& Visual::Base::GetName()
193 {
194   return mImpl->mName;
195 }
196
197 float Visual::Base::GetHeightForWidth( float width )
198 {
199   float aspectCorrectedHeight = 0.f;
200   Vector2 naturalSize;
201   GetNaturalSize( naturalSize );
202   if( naturalSize.width )
203   {
204     aspectCorrectedHeight = naturalSize.height * width / naturalSize.width;
205   }
206   return aspectCorrectedHeight;
207 }
208
209 float Visual::Base::GetWidthForHeight( float height )
210 {
211   float aspectCorrectedWidth = 0.f;
212   Vector2 naturalSize;
213   GetNaturalSize( naturalSize );
214   if( naturalSize.height > 0.0f )
215   {
216     aspectCorrectedWidth = naturalSize.width * height / naturalSize.height;
217   }
218   return aspectCorrectedWidth;
219 }
220
221 void Visual::Base::GetNaturalSize( Vector2& naturalSize )
222 {
223   naturalSize = Vector2::ZERO;
224 }
225
226 void Visual::Base::DoAction( const Property::Index actionId, const Property::Value attributes )
227 {
228   OnDoAction( actionId, attributes );
229 }
230
231 void Visual::Base::SetDepthIndex( int index )
232 {
233   mImpl->mDepthIndex = index;
234   if( mImpl->mRenderer )
235   {
236     mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
237   }
238 }
239
240 int Visual::Base::GetDepthIndex() const
241 {
242   return mImpl->mDepthIndex;
243 }
244
245 void Visual::Base::SetOnStage( Actor& actor )
246 {
247   if( !IsOnStage() )
248   {
249     // To display the actor correctly, renderer should not be added to actor until all required resources are ready.
250     // Thus the calling of actor.AddRenderer() should happen inside derived class as base class does not know the exact timing.
251     DoSetOnStage( actor );
252
253     if( mImpl->mRenderer )
254     {
255       RegisterMixColor();
256
257       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, IsPreMultipliedAlphaEnabled());
258       mImpl->mRenderer.SetProperty( Renderer::Property::DEPTH_INDEX, mImpl->mDepthIndex );
259       mImpl->mFlags |= Impl::IS_ON_STAGE; // Only sets the flag if renderer exists
260     }
261   }
262 }
263
264 void Visual::Base::SetOffStage( Actor& actor )
265 {
266   if( IsOnStage() )
267   {
268     DoSetOffStage( actor );
269     mImpl->mMixColorIndex = Property::INVALID_INDEX;
270     mImpl->mOpacityIndex = Property::INVALID_INDEX;
271     mImpl->mFlags &= ~Impl::IS_ON_STAGE;
272   }
273 }
274
275 void Visual::Base::CreatePropertyMap( Property::Map& map ) const
276 {
277   DoCreatePropertyMap( map );
278
279   if( mImpl->mCustomShader )
280   {
281     mImpl->mCustomShader->CreatePropertyMap( map );
282   }
283
284   Property::Map transform;
285   mImpl->mTransform.GetPropertyMap( transform );
286   map.Insert( Toolkit::Visual::Property::TRANSFORM, transform );
287
288   bool premultipliedAlpha( IsPreMultipliedAlphaEnabled() );
289   map.Insert( Toolkit::Visual::Property::PREMULTIPLIED_ALPHA, premultipliedAlpha );
290
291   // Note, Color and Primitive will also insert their own mix color into the map
292   // which is ok, because they have a different key value range.
293   map.Insert( Toolkit::Visual::Property::MIX_COLOR, mImpl->mMixColor ); // vec4
294   map.Insert( Toolkit::Visual::Property::OPACITY, mImpl->mMixColor.a );
295 }
296
297 void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const
298 {
299   DoCreateInstancePropertyMap( map );
300
301   if( mImpl->mCustomShader )
302   {
303     mImpl->mCustomShader->CreatePropertyMap( map );
304   }
305
306   //map.Insert( Toolkit::Visual::Property::DEPTH_INDEX, mImpl->mDepthIndex );
307   //map.Insert( Toolkit::Visual::Property::ENABLED, (bool) mImpl->mRenderer );
308 }
309
310
311 void Visual::Base::EnablePreMultipliedAlpha( bool preMultiplied )
312 {
313   if( preMultiplied )
314   {
315     mImpl->mFlags |= Impl::IS_PREMULTIPLIED_ALPHA;
316   }
317   else
318   {
319     mImpl->mFlags &= ~Impl::IS_PREMULTIPLIED_ALPHA;
320   }
321
322   if( mImpl->mRenderer )
323   {
324     mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA, preMultiplied);
325     mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, static_cast<float>( preMultiplied ) );
326   }
327 }
328
329 bool Visual::Base::IsPreMultipliedAlphaEnabled() const
330 {
331   return mImpl->mFlags & Impl::IS_PREMULTIPLIED_ALPHA;
332 }
333
334 void Visual::Base::DoSetOffStage( Actor& actor )
335 {
336   actor.RemoveRenderer( mImpl->mRenderer );
337   mImpl->mRenderer.Reset();
338 }
339
340 bool Visual::Base::IsOnStage() const
341 {
342   return mImpl->mFlags & Impl::IS_ON_STAGE;
343 }
344
345 void Visual::Base::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
346 {
347   // May be overriden by derived class
348 }
349
350 void Visual::Base::RegisterMixColor()
351 {
352   // Only register if not already registered.
353   // (Color and Primitive visuals will register their own and save to this index)
354   if( mImpl->mMixColorIndex == Property::INVALID_INDEX )
355   {
356     mImpl->mMixColorIndex = DevelHandle::RegisterProperty(
357       mImpl->mRenderer,
358       Toolkit::Visual::Property::MIX_COLOR,
359       MIX_COLOR,
360       Vector3(mImpl->mMixColor) );
361   }
362
363   if( mImpl->mMixColor.a < 1.f )
364   {
365     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
366   }
367
368   if( mImpl->mOpacityIndex == Property::INVALID_INDEX )
369   {
370     mImpl->mOpacityIndex = DevelHandle::RegisterProperty(
371       mImpl->mRenderer,
372       Toolkit::Visual::Property::OPACITY,
373       OPACITY,
374       mImpl->mMixColor.a );
375   }
376
377   float preMultipliedAlpha = 0.0f;
378   if( IsPreMultipliedAlphaEnabled() )
379   {
380     preMultipliedAlpha = 1.0f;
381   }
382   mImpl->mRenderer.RegisterProperty( PRE_MULTIPLIED_ALPHA_PROPERTY, preMultipliedAlpha );
383 }
384
385 void Visual::Base::SetMixColor( const Vector4& color )
386 {
387   mImpl->mMixColor = color;
388
389   if( mImpl->mRenderer )
390   {
391     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, Vector3(color) );
392     mImpl->mRenderer.SetProperty( mImpl->mOpacityIndex, color.a );
393     if( color.a < 1.f )
394     {
395       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
396     }
397   }
398 }
399
400 void Visual::Base::SetMixColor( const Vector3& color )
401 {
402   mImpl->mMixColor.r = color.r;
403   mImpl->mMixColor.g = color.g;
404   mImpl->mMixColor.b = color.b;
405
406   if( mImpl->mRenderer )
407   {
408     mImpl->mRenderer.SetProperty( mImpl->mMixColorIndex, color );
409   }
410 }
411
412 const Vector4& Visual::Base::GetMixColor() const
413 {
414   return mImpl->mMixColor;
415 }
416
417 void Visual::Base::AddResourceObserver( Visual::ResourceObserver& observer)
418 {
419   mImpl->mResourceObserver = &observer;
420 }
421
422 void Visual::Base::RemoveResourceObserver( Visual::ResourceObserver& observer )
423 {
424   mImpl->mResourceObserver = NULL;
425 }
426
427 void Visual::Base::ResourceReady(Toolkit::Visual::ResourceStatus resourceStatus)
428 {
429   if( mImpl->mResourceStatus != resourceStatus )
430   {
431     mImpl->mResourceStatus = resourceStatus;
432
433     if( mImpl->mResourceObserver )
434     {
435       // observer is currently a control impl
436       mImpl->mResourceObserver->ResourceReady( *this );
437     }
438   }
439 }
440
441 bool Visual::Base::IsResourceReady() const
442 {
443   return ( mImpl->mResourceStatus == Toolkit::Visual::ResourceStatus::READY );
444 }
445
446 Toolkit::Visual::ResourceStatus Visual::Base::GetResourceStatus() const
447 {
448   return mImpl->mResourceStatus;
449 }
450
451 Renderer Visual::Base::GetRenderer()
452 {
453   return mImpl->mRenderer;
454 }
455
456 Property::Index Visual::Base::GetPropertyIndex( Property::Key key )
457 {
458   Property::Index index = DevelHandle::GetPropertyIndex( mImpl->mRenderer, key );
459
460   if( index == Property::INVALID_INDEX )
461   {
462     // Is it a shader property?
463     Shader shader = mImpl->mRenderer.GetShader();
464     index = DevelHandle::GetPropertyIndex( shader, key );
465     if( index != Property::INVALID_INDEX )
466     {
467       // Yes - we should register it in the Renderer so it can be set / animated
468       // independently, as shaders are shared across multiple renderers.
469       std::string keyName;
470       Property::Index keyIndex( Property::INVALID_KEY );
471       if( key.type == Property::Key::INDEX )
472       {
473         keyName = shader.GetPropertyName( index );
474         keyIndex = key.indexKey;
475       }
476       else
477       {
478         keyName = key.stringKey;
479         // Leave keyIndex as INVALID_KEY - it can still be registered against the string key.
480       }
481       Property::Value value = shader.GetProperty( index );
482       index = DevelHandle::RegisterProperty( mImpl->mRenderer, keyIndex, keyName, value );
483     }
484   }
485   return index;
486 }
487
488 void Visual::Base::SetupTransition(
489   Dali::Animation& transition,
490   Internal::TransitionData::Animator& animator,
491   Property::Index index,
492   Property::Value& initialValue,
493   Property::Value& targetValue )
494 {
495   if( index != Property::INVALID_INDEX )
496   {
497     if( mImpl->mRenderer )
498     {
499       if( animator.animate == false )
500       {
501         mImpl->mRenderer.SetProperty( index, targetValue );
502       }
503       else
504       {
505         if( animator.initialValue.GetType() != Property::NONE )
506         {
507           mImpl->mRenderer.SetProperty( index, initialValue );
508         }
509
510         if( ! transition )
511         {
512           transition = Dali::Animation::New( 0.1f );
513         }
514
515         transition.AnimateTo( Property( mImpl->mRenderer, index ),
516                               targetValue,
517                               animator.alphaFunction,
518                               TimePeriod( animator.timePeriodDelay,
519                                           animator.timePeriodDuration ) );
520       }
521     }
522   }
523 }
524
525 void Visual::Base::AnimateProperty(
526   Dali::Animation& transition,
527   Internal::TransitionData::Animator& animator )
528 {
529 #if defined(DEBUG_ENABLED)
530   {
531     std::ostringstream oss;
532     oss << "Visual::Base::AnimateProperty(Visual:" << mImpl->mName << " Property:" << animator.propertyKey << " Target: " << animator.targetValue << std::endl;
533     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, oss.str().c_str() );
534   }
535 #endif
536
537   Property::Map map;
538   DoCreatePropertyMap( map );
539   Property::Value* valuePtr = map.Find( Toolkit::Visual::Property::TYPE );
540   int visualType = -1;
541   if( valuePtr )
542   {
543     valuePtr->Get( visualType );
544   }
545
546   if( animator.propertyKey == Toolkit::Visual::Property::MIX_COLOR ||
547       animator.propertyKey == MIX_COLOR ||
548       ( visualType == Toolkit::Visual::COLOR &&
549         animator.propertyKey == ColorVisual::Property::MIX_COLOR ) ||
550       ( visualType == Toolkit::Visual::PRIMITIVE &&
551         animator.propertyKey == PrimitiveVisual::Property::MIX_COLOR ) )
552   {
553     AnimateMixColorProperty( transition, animator );
554   }
555   else if(animator.propertyKey == Toolkit::Visual::Property::OPACITY ||
556           animator.propertyKey == OPACITY )
557   {
558     AnimateOpacityProperty( transition, animator );
559   }
560   else if( mImpl->mRenderer )
561   {
562     AnimateRendererProperty( transition, animator );
563   }
564 }
565
566 void Visual::Base::AnimateOpacityProperty(
567   Dali::Animation& transition,
568   Internal::TransitionData::Animator& animator )
569 {
570   Property::Index index = mImpl->mOpacityIndex;
571
572   bool isOpaque = mImpl->mMixColor.a >= 1.0f;
573
574   if( index != Property::INVALID_INDEX )
575   {
576     float initialOpacity;
577     if( animator.initialValue.Get( initialOpacity ) )
578     {
579       isOpaque = (initialOpacity >= 1.0f);
580     }
581
582     float targetOpacity;
583     if( animator.targetValue.Get( targetOpacity ) )
584     {
585       mImpl->mMixColor.a = targetOpacity;
586     }
587
588     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
589     SetupBlendMode( transition, isOpaque, animator.animate );
590   }
591 }
592
593 void Visual::Base::AnimateRendererProperty(
594   Dali::Animation& transition,
595   Internal::TransitionData::Animator& animator )
596 {
597   Property::Index index = GetPropertyIndex( animator.propertyKey );
598   if( index != Property::INVALID_INDEX )
599   {
600     if( animator.targetValue.GetType() != Property::NONE )
601     {
602       // Try writing target value into transform property map
603       // if it's not a valid key, then it won't alter mTransform
604       Property::Map map;
605       if( animator.propertyKey.type == Property::Key::INDEX )
606       {
607         map.Add( animator.propertyKey.indexKey, animator.targetValue );
608       }
609       else
610       {
611         map.Add( animator.propertyKey.stringKey, animator.targetValue );
612       }
613
614       mImpl->mTransform.UpdatePropertyMap( map );
615     }
616
617     SetupTransition( transition, animator, index, animator.initialValue, animator.targetValue );
618   }
619 }
620
621 void Visual::Base::AnimateMixColorProperty(
622   Dali::Animation& transition,
623   Internal::TransitionData::Animator& animator )
624 {
625   Property::Index index = mImpl->mMixColorIndex;
626   bool animateOpacity = false;
627   bool isOpaque = true;
628
629   Property::Value initialOpacity;
630   Property::Value targetOpacity;
631   Property::Value initialMixColor;
632   Property::Value targetMixColor;
633
634   if( index != Property::INVALID_INDEX )
635   {
636     Vector4 initialColor;
637     if( animator.initialValue.Get(initialColor) )
638     {
639       if( animator.initialValue.GetType() == Property::VECTOR4 )
640       {
641         // if there is an initial color specifying alpha, test it
642         isOpaque = initialColor.a >= 1.0f;
643         initialOpacity = initialColor.a;
644       }
645       initialMixColor = Vector3( initialColor );
646     }
647
648     // Set target value into data store
649     if( animator.targetValue.GetType() != Property::NONE )
650     {
651       Vector4 mixColor;
652       animator.targetValue.Get(mixColor);
653       if( animator.targetValue.GetType() == Property::VECTOR4 )
654       {
655         mImpl->mMixColor.a = mixColor.a;
656         targetOpacity = mixColor.a;
657         animateOpacity = true;
658       }
659
660       mImpl->mMixColor.r = mixColor.r;
661       mImpl->mMixColor.g = mixColor.g;
662       mImpl->mMixColor.b = mixColor.b;
663       targetMixColor = Vector3(mixColor);
664     }
665
666     SetupTransition( transition, animator, index, initialMixColor, targetMixColor );
667     if( animateOpacity )
668     {
669       SetupTransition( transition, animator, mImpl->mOpacityIndex, initialOpacity, targetOpacity );
670       SetupBlendMode( transition, isOpaque, animator.animate );
671     }
672   }
673 }
674
675 void Visual::Base::SetupBlendMode( Animation& transition, bool isInitialOpaque, bool animating )
676 {
677   // Ensure the blend mode is turned on if we are animating opacity, and
678   // turned off after the animation ends if the final value is opaque
679   if( ! isInitialOpaque || mImpl->mMixColor.a < 1.0f )
680   {
681     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
682
683     if( animating == true && mImpl->mMixColor.a >= 1.0f )
684     {
685       // When it becomes opaque, set the blend mode back to automatically
686       if( ! mImpl->mBlendSlotDelegate )
687       {
688         mImpl->mBlendSlotDelegate = new SlotDelegate<Visual::Base>(this);
689       }
690       transition.FinishedSignal().Connect( *(mImpl->mBlendSlotDelegate),
691                                            &Visual::Base::OnMixColorFinished );
692     }
693   }
694 }
695
696 void Visual::Base::OnMixColorFinished( Animation& animation )
697 {
698   if( mImpl->mRenderer )
699   {
700     DALI_LOG_INFO( gVisualBaseLogFilter, Debug::General, "Visual::Base::OnMixColorFinished()\n");
701     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE,
702                                   ( mImpl->mMixColor.a < 1.0 ) ? BlendMode::ON : BlendMode::AUTO );
703   }
704   delete mImpl->mBlendSlotDelegate;
705   mImpl->mBlendSlotDelegate = NULL;
706 }
707
708 } // namespace Internal
709
710 } // namespace Toolkit
711
712 } // namespace Dali