[4.0] Add VisualEventSignal to Control and a property to AnimatedVectorImageVisual
[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 }
325
326 void ImageView::OnResourceReady( Toolkit::Control control )
327 {
328   // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
329   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
330 }
331
332 ///////////////////////////////////////////////////////////
333 //
334 // Properties
335 //
336
337 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
338 {
339   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
340
341   if ( imageView )
342   {
343     ImageView& impl = GetImpl( imageView );
344     switch ( index )
345     {
346       case Toolkit::ImageView::Property::RESOURCE_URL:
347       {
348         std::string imageUrl;
349         if( value.Get( imageUrl ) )
350         {
351           impl.SetImage( imageUrl, ImageDimensions() );
352         }
353         break;
354       }
355
356       case Toolkit::ImageView::Property::IMAGE:
357       {
358         std::string imageUrl;
359         Property::Map* map;
360         if( value.Get( imageUrl ) )
361         {
362           impl.SetImage( imageUrl, ImageDimensions() );
363         }
364         // if its not a string then get a Property::Map from the property if possible.
365         else
366         {
367           map = value.GetMap();
368           if( map )
369           {
370             Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
371             // set image only if property map contains image information other than custom shader
372             if( map->Count() > 1u ||  !shaderValue )
373             {
374               impl.SetImage( *map );
375             }
376             // the property map contains only the custom shader
377             else if( ( map->Count() == 1u )&&( shaderValue ) )
378             {
379               impl.mShaderMap = *( shaderValue->GetMap() );
380
381               if( !impl.mUrl.empty() )
382               {
383                 impl.SetImage( impl.mUrl, impl.mImageSize );
384               }
385               else if( impl.mImage )
386               {
387                 impl.SetImage( impl.mImage );
388               }
389               else if( !impl.mPropertyMap.Empty() )
390               {
391                 impl.SetImage( impl.mPropertyMap );
392               }
393             }
394           }
395         }
396         break;
397       }
398
399       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
400       {
401         bool isPre;
402         if( value.Get( isPre ) )
403         {
404           impl.EnablePreMultipliedAlpha( isPre );
405         }
406         break;
407       }
408     }
409   }
410 }
411
412 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
413 {
414   Property::Value value;
415
416   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
417
418   if ( imageview )
419   {
420     ImageView& impl = GetImpl( imageview );
421     switch ( propertyIndex )
422     {
423       case Toolkit::ImageView::Property::RESOURCE_URL:
424       {
425         if ( !impl.mUrl.empty() )
426         {
427           value = impl.mUrl;
428         }
429         break;
430       }
431
432       case Toolkit::ImageView::Property::IMAGE:
433       {
434         if ( !impl.mUrl.empty() )
435         {
436           value = impl.mUrl;
437         }
438         else if( impl.mImage )
439         {
440           Property::Map map;
441           Scripting::CreatePropertyMap( impl.mImage, map );
442           value = map;
443         }
444         else
445         {
446           Property::Map map;
447           Toolkit::Visual::Base visual = DevelControl::GetVisual( impl, Toolkit::ImageView::Property::IMAGE );
448           if( visual )
449           {
450             visual.CreatePropertyMap( map );
451           }
452           value = map;
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