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