[4.0] 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) 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 "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 {
70 }
71
72 ImageView::~ImageView()
73 {
74 }
75
76 Toolkit::ImageView ImageView::New()
77 {
78   ImageView* impl = new ImageView();
79
80   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
81
82   // Second-phase init of the implementation
83   // This can only be done after the CustomActor connection has been made...
84   impl->Initialize();
85
86   return handle;
87 }
88
89 /////////////////////////////////////////////////////////////
90
91 void ImageView::OnInitialize()
92 {
93   // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
94   Dali::Toolkit::Control handle( GetOwner() );
95   handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
96 }
97
98 void ImageView::SetImage( Image image )
99 {
100   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
101   mImage = image;
102   mUrl.clear();
103   mPropertyMap.Clear();
104
105   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( image );
106   if( visual )
107   {
108     if( !mVisual )
109     {
110       mVisual = visual;
111     }
112
113     if( !mShaderMap.Empty() )
114     {
115       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
116       visualImpl.SetCustomShader( mShaderMap );
117     }
118
119     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
120   }
121   else
122   {
123     // Unregister the exsiting visual
124     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
125
126     // Trigger a size negotiation request that may be needed when unregistering a visual.
127     RelayoutRequest();
128   }
129 }
130
131 void ImageView::SetImage( const Property::Map& map )
132 {
133   // Comparing a property map is too expensive so just creating a new visual
134   mPropertyMap = map;
135   mUrl.clear();
136   mImage.Reset();
137
138   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
139   if( visual )
140   {
141     // Don't set mVisual until it is ready and shown. Getters will still use current visual.
142     if( !mVisual )
143     {
144       mVisual = visual;
145     }
146
147     if( !mShaderMap.Empty() )
148     {
149       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
150       visualImpl.SetCustomShader( mShaderMap );
151     }
152
153     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
154   }
155   else
156   {
157     // Unregister the exsiting visual
158     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
159
160     // Trigger a size negotiation request that may be needed when unregistering a visual.
161     RelayoutRequest();
162   }
163 }
164
165 void ImageView::SetImage( const std::string& url, ImageDimensions size )
166 {
167   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
168   mUrl = url;
169   mImageSize = size;
170   mImage.Reset();
171   mPropertyMap.Clear();
172
173   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
174   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
175   if( visual )
176   {
177     if( !mVisual )
178     {
179       mVisual = visual;
180     }
181
182     if( !mShaderMap.Empty() )
183     {
184       Internal::Visual::Base& visualImpl = Toolkit::GetImplementation( visual );
185       visualImpl.SetCustomShader( mShaderMap );
186     }
187
188     DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
189   }
190   else
191   {
192     // Unregister the exsiting visual
193     DevelControl::UnregisterVisual( *this, Toolkit::ImageView::Property::IMAGE );
194
195     // Trigger a size negotiation request that may be needed when unregistering a visual.
196     RelayoutRequest();
197   }
198 }
199
200 Image ImageView::GetImage() const
201 {
202   return mImage;
203 }
204
205 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
206 {
207   if( mVisual )
208   {
209     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
210   }
211 }
212
213 bool ImageView::IsPreMultipliedAlphaEnabled() const
214 {
215   if( mVisual )
216   {
217     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
218   }
219   return false;
220 }
221
222 void ImageView::SetDepthIndex( int depthIndex )
223 {
224   if( mVisual )
225   {
226     mVisual.SetDepthIndex( depthIndex );
227   }
228 }
229
230 void ImageView::OnStageConnection( int depth )
231 {
232   if( mImage )
233   {
234     mImage.UploadedSignal().Emit( mImage );
235   }
236
237   Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
238   if( resourceImage )
239   {
240     resourceImage.LoadingFinishedSignal().Emit( resourceImage );
241   }
242
243   Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
244 }
245
246 Vector3 ImageView::GetNaturalSize()
247 {
248   if( mVisual )
249   {
250     Vector2 rendererNaturalSize;
251     mVisual.GetNaturalSize( rendererNaturalSize );
252
253     Extents padding;
254     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
255
256     rendererNaturalSize.width += ( padding.start + padding.end );
257     rendererNaturalSize.height += ( padding.top + padding.bottom );
258     return Vector3( rendererNaturalSize );
259   }
260
261   // if no visual then use Control's natural size
262   return Control::GetNaturalSize();
263 }
264
265 float ImageView::GetHeightForWidth( float width )
266 {
267   Extents padding;
268   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
269
270   if( mVisual )
271   {
272     return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
273   }
274   else
275   {
276     return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
277   }
278 }
279
280 float ImageView::GetWidthForHeight( float height )
281 {
282   Extents padding;
283   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
284
285   if( mVisual )
286   {
287     return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
288   }
289   else
290   {
291     return Control::GetWidthForHeight( height ) + padding.start + padding.end;
292   }
293 }
294
295 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
296 {
297   Control::OnRelayout( size, container );
298
299   if( mVisual )
300   {
301     Extents padding;
302     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
303
304     Property::Map transformMap = Property::Map();
305
306     if( ( padding.start != 0 ) || ( padding.end != 0 ) || ( padding.top != 0 ) || ( padding.bottom != 0 ) )
307     {
308       Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>( Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>() );
309
310       if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
311       {
312         std::swap(padding.start, padding.end);
313       }
314
315       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( padding.start, padding.top ) )
316                   .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
317                   .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
318                   .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
319     }
320
321     // Should provide a transform that handles aspect ratio according to image size
322     mVisual.SetTransformAndSize( transformMap, size );
323
324     // mVisual is not updated util the resource is ready in the case of visual replacement.
325     // So apply the transform and size to the new visual.
326     Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
327     if( visual && visual != mVisual )
328     {
329       visual.SetTransformAndSize( transformMap, size );
330     }
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( ( map->Count() == 1u )&&( shaderValue ) )
386             {
387               impl.mShaderMap = *( shaderValue->GetMap() );
388
389               if( !impl.mUrl.empty() )
390               {
391                 impl.SetImage( impl.mUrl, impl.mImageSize );
392               }
393               else if( impl.mImage )
394               {
395                 impl.SetImage( impl.mImage );
396               }
397               else if( !impl.mPropertyMap.Empty() )
398               {
399                 impl.SetImage( impl.mPropertyMap );
400               }
401             }
402           }
403         }
404         break;
405       }
406
407       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
408       {
409         bool isPre;
410         if( value.Get( isPre ) )
411         {
412           impl.EnablePreMultipliedAlpha( isPre );
413         }
414         break;
415       }
416     }
417   }
418 }
419
420 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
421 {
422   Property::Value value;
423
424   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
425
426   if ( imageview )
427   {
428     ImageView& impl = GetImpl( imageview );
429     switch ( propertyIndex )
430     {
431       case Toolkit::ImageView::Property::RESOURCE_URL:
432       {
433         if ( !impl.mUrl.empty() )
434         {
435           value = impl.mUrl;
436         }
437         break;
438       }
439
440       case Toolkit::ImageView::Property::IMAGE:
441       {
442         if ( !impl.mUrl.empty() )
443         {
444           value = impl.mUrl;
445         }
446         else if( impl.mImage )
447         {
448           Property::Map map;
449           Scripting::CreatePropertyMap( impl.mImage, map );
450           value = map;
451         }
452         else
453         {
454           Property::Map map;
455           Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
456           if( visual )
457           {
458             visual.CreatePropertyMap( map );
459           }
460           value = map;
461         }
462         break;
463       }
464
465       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
466       {
467         value = impl.IsPreMultipliedAlphaEnabled();
468         break;
469       }
470     }
471   }
472
473   return value;
474 }
475
476 } // namespace Internal
477 } // namespace Toolkit
478 } // namespace Dali