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