Remove reserved properties from toolkit
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / image-view / image-view-impl.cpp
1 /*
2  * Copyright (c) 2020 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 "image-view-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/images/resource-image.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/devel-api/scripting/scripting.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
29 #include <dali-toolkit/devel-api/controls/control-devel.h>
30 #include <dali-toolkit/public-api/visuals/visual-properties.h>
31 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
32 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 namespace
46 {
47
48 const Vector4 FULL_TEXTURE_RECT(0.f, 0.f, 1.f, 1.f);
49
50 BaseHandle Create()
51 {
52   return Toolkit::ImageView::New();
53 }
54
55 // Setup properties, signals and actions using the type-registry.
56 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
57 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
58 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
59
60 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
61 DALI_TYPE_REGISTRATION_END()
62
63 } // anonymous namespace
64
65 using namespace Dali;
66
67 ImageView::ImageView()
68 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
69   mImageSize(),
70   mImageVisualPaddingSetByTransform( false ),
71   mImageViewPixelAreaSetByFittingMode( false )
72 {
73 }
74
75 ImageView::~ImageView()
76 {
77 }
78
79 Toolkit::ImageView ImageView::New()
80 {
81   ImageView* impl = new ImageView();
82
83   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
84
85   // Second-phase init of the implementation
86   // This can only be done after the CustomActor connection has been made...
87   impl->Initialize();
88
89   return handle;
90 }
91
92 /////////////////////////////////////////////////////////////
93
94 void ImageView::OnInitialize()
95 {
96   // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
97   Dali::Toolkit::Control handle( GetOwner() );
98   handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
99 }
100
101 void ImageView::SetImage( const Property::Map& map )
102 {
103   // Comparing a property map is too expensive so just creating a new visual
104   mPropertyMap = map;
105   mUrl.clear();
106
107   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
108   if( visual )
109   {
110     // Don't set mVisual until it is ready and shown. Getters will still use current visual.
111     if( !mVisual )
112     {
113       mVisual = visual;
114     }
115
116     if( !mShaderMap.Empty() )
117     {
118       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
119       visualImpl.SetCustomShader( mShaderMap );
120     }
121
122     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
123   }
124   else
125   {
126     // Unregister the exsiting visual
127     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
128
129     // Trigger a size negotiation request that may be needed when unregistering a visual.
130     RelayoutRequest();
131   }
132
133   // Signal that a Relayout may be needed
134 }
135
136 void ImageView::SetImage( const std::string& url, ImageDimensions size )
137 {
138   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
139   mUrl = url;
140   mImageSize = size;
141   mPropertyMap.Clear();
142
143   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
144   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
145   if( visual )
146   {
147     if( !mVisual )
148     {
149       mVisual = visual;
150     }
151
152     if( !mShaderMap.Empty() )
153     {
154       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
155       visualImpl.SetCustomShader( mShaderMap );
156     }
157
158     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
159   }
160   else
161   {
162     // Unregister the exsiting visual
163     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
164
165     // Trigger a size negotiation request that may be needed when unregistering a visual.
166     RelayoutRequest();
167   }
168
169   // Signal that a Relayout may be needed
170 }
171
172 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
173 {
174   if( mVisual )
175   {
176     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
177   }
178 }
179
180 bool ImageView::IsPreMultipliedAlphaEnabled() const
181 {
182   if( mVisual )
183   {
184     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
185   }
186   return false;
187 }
188
189 void ImageView::SetDepthIndex( int depthIndex )
190 {
191   if( mVisual )
192   {
193     mVisual.SetDepthIndex( depthIndex );
194   }
195 }
196
197 Vector3 ImageView::GetNaturalSize()
198 {
199   if( mVisual )
200   {
201     Vector2 rendererNaturalSize;
202     mVisual.GetNaturalSize( rendererNaturalSize );
203
204     Extents padding;
205     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
206
207     rendererNaturalSize.width += ( padding.start + padding.end );
208     rendererNaturalSize.height += ( padding.top + padding.bottom );
209     return Vector3( rendererNaturalSize );
210   }
211
212   // if no visual then use Control's natural size
213   return Control::GetNaturalSize();
214 }
215
216 float ImageView::GetHeightForWidth( float width )
217 {
218   Extents padding;
219   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
220
221   if( mVisual )
222   {
223     return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
224   }
225   else
226   {
227     return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
228   }
229 }
230
231 float ImageView::GetWidthForHeight( float height )
232 {
233   Extents padding;
234   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
235
236   if( mVisual )
237   {
238     return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
239   }
240   else
241   {
242     return Control::GetWidthForHeight( height ) + padding.start + padding.end;
243   }
244 }
245
246 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
247 {
248   Control::OnRelayout( size, container );
249   if( mVisual )
250   {
251     Property::Map transformMap = Property::Map();
252
253     Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
254
255     bool zeroPadding = ( padding == Extents() );
256
257     Vector2 naturalSize;
258     mVisual.GetNaturalSize( naturalSize );
259
260     Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
261           Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
262     if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
263     {
264       std::swap( padding.start, padding.end );
265     }
266
267     // remove padding from the size to know how much is left for the visual
268     Vector2 finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
269     Vector2 finalOffset = Vector2( padding.start, padding.top );
270
271     ApplyFittingMode( finalSize, naturalSize, finalOffset, zeroPadding , transformMap );
272
273     mVisual.SetTransformAndSize( transformMap, size );
274
275     // mVisual is not updated util the resource is ready in the case of visual replacement.
276     // in this case, the Property Map must be initialized so that the previous value is not reused.
277     // after mVisual is updated, the correct value will be reset.
278     Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
279     if( visual && visual != mVisual )
280     {
281       visual.SetTransformAndSize( Property::Map(), size );
282     }
283   }
284 }
285
286 void ImageView::OnResourceReady( Toolkit::Control control )
287 {
288   // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
289   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
290   // Signal that a Relayout may be needed
291 }
292
293 void ImageView::SetTransformMapForFittingMode( Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, Visual::FittingMode fittingMode, Property::Map& transformMap )
294 {
295   switch(fittingMode)
296   {
297     case Visual::FittingMode::FIT_KEEP_ASPECT_RATIO:
298     {
299       auto availableVisualSize = finalSize;
300
301       // scale to fit the padded area
302       finalSize = naturalSize * std::min( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
303                                             ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
304
305       // calculate final offset within the padded area
306       finalOffset += ( availableVisualSize - finalSize ) * .5f;
307
308       // populate the transform map
309       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
310                   .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
311       break;
312     }
313     case Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO:
314     {
315       mImageViewPixelAreaSetByFittingMode = true;
316       auto availableVisualSize = finalSize;
317       finalSize = naturalSize * std::max( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
318                                           ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
319
320       auto originalOffset = finalOffset;
321       finalOffset += ( availableVisualSize - finalSize ) * .5f;
322
323       float x = abs( (availableVisualSize.width - finalSize.width ) / finalSize.width )* .5f;
324       float y = abs( (availableVisualSize.height - finalSize.height ) / finalSize.height ) * .5f;
325       float widthRatio = 1.f - abs( (availableVisualSize.width - finalSize.width ) / finalSize.width );
326       float heightRatio = 1.f - abs( (availableVisualSize.height - finalSize.height ) / finalSize.height );
327       Vector4 pixelArea = Vector4( x, y, widthRatio, heightRatio);
328       Self().SetProperty( Toolkit::ImageView::Property::PIXEL_AREA, pixelArea );
329
330       // populate the transform map
331       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, originalOffset )
332                   .Add( Toolkit::Visual::Transform::Property::SIZE, availableVisualSize );
333       break;
334     }
335     case Visual::FittingMode::CENTER:
336     {
337       auto availableVisualSize = finalSize;
338       if( availableVisualSize.width > naturalSize.width && availableVisualSize.height > naturalSize.height )
339       {
340         finalSize = naturalSize;
341       }
342       else
343       {
344         finalSize = naturalSize * std::min( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
345                                           ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
346       }
347
348       finalOffset += ( availableVisualSize - finalSize ) * .5f;
349
350       // populate the transform map
351       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
352                   .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
353       break;
354     }
355     case Visual::FittingMode::FILL:
356     {
357       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
358                   .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize );
359       break;
360     }
361     case Visual::FittingMode::FIT_WIDTH:
362     case Visual::FittingMode::FIT_HEIGHT:
363     {
364       // This FittingMode already converted
365       break;
366     }
367   }
368 }
369
370 void ImageView::ApplyFittingMode( Vector2 finalSize, Vector2 naturalSize, Vector2 finalOffset, bool zeroPadding , Property::Map& transformMap )
371 {
372     Visual::FittingMode fittingMode = Toolkit::GetImplementation(mVisual).GetFittingMode();
373
374     // Reset PIXEL_AREA after using OVER_FIT_KEEP_ASPECT_RATIO
375     if( mImageViewPixelAreaSetByFittingMode )
376     {
377       Self().SetProperty( Toolkit::ImageView::Property::PIXEL_AREA, FULL_TEXTURE_RECT );
378       mImageViewPixelAreaSetByFittingMode = false;
379     }
380
381     if( ( !zeroPadding ) || // If padding is not zero
382         ( fittingMode != Visual::FittingMode::FILL ) )
383     {
384       mImageVisualPaddingSetByTransform = true;
385
386       // If FittingMode use FIT_WIDTH or FIT_HEIGTH, it need to change proper fittingMode
387       if( fittingMode == Visual::FittingMode::FIT_WIDTH )
388       {
389         fittingMode = ( finalSize.height  / naturalSize.height ) < ( finalSize.width / naturalSize.width ) ? Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::FIT_KEEP_ASPECT_RATIO;
390       }
391       else if( fittingMode == Visual::FittingMode::FIT_HEIGHT )
392       {
393         fittingMode = ( finalSize.height  / naturalSize.height ) < ( finalSize.width / naturalSize.width ) ? Visual::FittingMode::FIT_KEEP_ASPECT_RATIO : Visual::FittingMode::OVER_FIT_KEEP_ASPECT_RATIO;
394       }
395
396       SetTransformMapForFittingMode( finalSize, naturalSize, finalOffset, fittingMode, transformMap );
397
398       // Set extra value for applying transformMap
399       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
400                         Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
401                   .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
402                   .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
403                   .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
404                         Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
405     }
406     else if ( mImageVisualPaddingSetByTransform && zeroPadding )  // Reset offset to zero only if padding applied previously
407     {
408       mImageVisualPaddingSetByTransform = false;
409       // Reset the transform map
410       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2::ZERO )
411                   .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
412                         Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) )
413                   .Add( Toolkit::Visual::Transform::Property::SIZE, Vector2::ONE )
414                   .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
415                         Vector2( Toolkit::Visual::Transform::Policy::RELATIVE, Toolkit::Visual::Transform::Policy::RELATIVE ) );
416     }
417 }
418
419 ///////////////////////////////////////////////////////////
420 //
421 // Properties
422 //
423
424 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
425 {
426   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
427
428   if ( imageView )
429   {
430     ImageView& impl = GetImpl( imageView );
431     switch ( index )
432     {
433       case Toolkit::ImageView::Property::IMAGE:
434       {
435         std::string imageUrl;
436         Property::Map* map;
437         if( value.Get( imageUrl ) )
438         {
439           impl.SetImage( imageUrl, ImageDimensions() );
440         }
441         // if its not a string then get a Property::Map from the property if possible.
442         else
443         {
444           map = value.GetMap();
445           if( map )
446           {
447             Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
448             // set image only if property map contains image information other than custom shader
449             if( map->Count() > 1u ||  !shaderValue )
450             {
451               impl.SetImage( *map );
452             }
453             // the property map contains only the custom shader
454             else if( ( map->Count() == 1u )&&( shaderValue ) )
455             {
456               Property::Map* shaderMap = shaderValue->GetMap();
457               if( shaderMap )
458               {
459                 impl.mShaderMap = *shaderMap;
460
461                 if( !impl.mUrl.empty() )
462                 {
463                   impl.SetImage( impl.mUrl, impl.mImageSize );
464                 }
465                 else if( !impl.mPropertyMap.Empty() )
466                 {
467                   impl.SetImage( impl.mPropertyMap );
468                 }
469               }
470             }
471           }
472         }
473         break;
474       }
475
476       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
477       {
478         bool isPre;
479         if( value.Get( isPre ) )
480         {
481           impl.EnablePreMultipliedAlpha( isPre );
482         }
483         break;
484       }
485     }
486   }
487 }
488
489 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
490 {
491   Property::Value value;
492
493   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
494
495   if ( imageview )
496   {
497     ImageView& impl = GetImpl( imageview );
498     switch ( propertyIndex )
499     {
500       case Toolkit::ImageView::Property::IMAGE:
501       {
502         if ( !impl.mUrl.empty() )
503         {
504           value = impl.mUrl;
505         }
506         else
507         {
508           Property::Map map;
509           Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
510           if( visual )
511           {
512             visual.CreatePropertyMap( map );
513           }
514           value = map;
515         }
516         break;
517       }
518
519       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
520       {
521         value = impl.IsPreMultipliedAlphaEnabled();
522         break;
523       }
524     }
525   }
526
527   return value;
528 }
529
530 } // namespace Internal
531 } // namespace Toolkit
532 } // namespace Dali