Merge "Update README for dali-swig" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / image-view / image-view-impl.cpp
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd.
2
3 // CLASS HEADER
4 #include "image-view-impl.h"
5
6 // EXTERNAL INCLUDES
7 #include <dali/public-api/images/resource-image.h>
8 #include <dali/public-api/object/type-registry.h>
9 #include <dali/public-api/object/type-registry-helper.h>
10 #include <dali/devel-api/scripting/scripting.h>
11
12 // INTERNAL INCLUDES
13 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
14 #include <dali-toolkit/devel-api/visual-factory/devel-visual-properties.h>
15 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
16 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
17 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
18 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
19
20 namespace Dali
21 {
22
23 namespace Toolkit
24 {
25
26 namespace Internal
27 {
28
29 namespace
30 {
31
32 BaseHandle Create()
33 {
34   return Toolkit::ImageView::New();
35 }
36
37 // Setup properties, signals and actions using the type-registry.
38 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
39 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
40 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
41 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
42
43 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
44 DALI_TYPE_REGISTRATION_END()
45
46
47 void SetDefaultTransformMap( Property::Map& transformMap )
48 {
49   transformMap.Clear();
50   transformMap
51     .Add( Toolkit::Visual::DevelProperty::Transform::Property::OFFSET, Vector2(0.0f, 0.0f) )
52     .Add( Toolkit::Visual::DevelProperty::Transform::Property::SIZE, Vector2(1.0f, 1.0f) )
53     .Add( Toolkit::Visual::DevelProperty::Transform::Property::ORIGIN, Toolkit::Align::CENTER )
54     .Add( Toolkit::Visual::DevelProperty::Transform::Property::ANCHOR_POINT, Toolkit::Align::CENTER )
55     .Add( Toolkit::Visual::DevelProperty::Transform::Property::OFFSET_SIZE_MODE, Vector4::ZERO );
56
57 }
58
59 } // anonymous namespace
60
61 using namespace Dali;
62
63 ImageView::ImageView()
64 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
65 {
66 }
67
68 ImageView::~ImageView()
69 {
70 }
71
72 Toolkit::ImageView ImageView::New()
73 {
74   ImageView* impl = new ImageView();
75
76   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
77
78   // Second-phase init of the implementation
79   // This can only be done after the CustomActor connection has been made...
80   impl->Initialize();
81
82   return handle;
83 }
84
85 /////////////////////////////////////////////////////////////
86
87 void ImageView::SetImage( Image image )
88 {
89   if( ( mImage != image ) ||
90       ! mUrl.empty()      ||   // If we're changing from a URL type to an Image type
91       ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
92   {
93     mUrl.clear();
94     mPropertyMap.Clear();
95
96     mImage = image;
97
98     mVisual =  Toolkit::VisualFactory::Get().CreateVisual( image );
99     RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
100     mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
101
102     RelayoutRequest();
103   }
104 }
105
106 void ImageView::SetImage( Property::Map map )
107 {
108   mUrl.clear();
109   mImage.Reset();
110   mPropertyMap = map;
111
112   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
113   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
114
115   Property::Value* widthValue = mPropertyMap.Find( "width" );
116   if( widthValue )
117   {
118     int width;
119     if( widthValue->Get( width ) )
120     {
121       mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
122     }
123   }
124
125   Property::Value* heightValue = mPropertyMap.Find( "height" );
126   if( heightValue )
127   {
128     int height;
129     if( heightValue->Get( height ) )
130     {
131       mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
132     }
133   }
134
135   RelayoutRequest();
136 }
137
138 void ImageView::SetImage( const std::string& url, ImageDimensions size )
139 {
140   if( ( mUrl != url ) ||
141         mImage        ||       // If we're changing from an Image type to a URL type
142       ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
143   {
144     mImage.Reset();
145     mPropertyMap.Clear();
146
147     mUrl = url;
148
149     if( size.GetWidth() != 0u && size.GetHeight() != 0u )
150     {
151       mImageSize = size;
152     }
153
154     mVisual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
155     RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
156
157     // This transform fills the control
158     // Should provide a transform that handles aspect ratio according to image size
159     Property::Map transformMap;
160     SetDefaultTransformMap( transformMap );
161     mVisual.SetTransformAndSize( transformMap, mSizeSet );
162
163     RelayoutRequest();
164   }
165 }
166
167 Image ImageView::GetImage() const
168 {
169   return mImage;
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     return Vector3( rendererNaturalSize );
204   }
205
206   Vector3 size;
207   size.x = mImageSize.GetWidth();
208   size.y = mImageSize.GetHeight();
209
210   if( size.x > 0 && size.y > 0 )
211   {
212     size.z = std::min(size.x, size.y);
213     return size;
214   }
215   else
216   {
217     // if no image then use Control's natural size
218     return Control::GetNaturalSize();
219   }
220 }
221
222 float ImageView::GetHeightForWidth( float width )
223 {
224   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
225   {
226     return GetHeightForWidthBase( width );
227   }
228   else
229   {
230     return Control::GetHeightForWidth( width );
231   }
232 }
233
234 float ImageView::GetWidthForHeight( float height )
235 {
236   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
237   {
238     return GetWidthForHeightBase( height );
239   }
240   else
241   {
242     return Control::GetWidthForHeight( height );
243   }
244 }
245
246
247 ///////////////////////////////////////////////////////////
248 //
249 // Private methods
250 //
251
252 void ImageView::OnSizeSet( const Vector3& targetSize )
253 {
254   Control::OnSizeSet( targetSize );
255   mSizeSet = targetSize;
256
257   if( mVisual )
258   {
259     Vector2 size( targetSize );
260
261     // This transform fills the control
262     // Should provide a transform that handles aspect ratio according to image size
263     Property::Map transformMap;
264     SetDefaultTransformMap( transformMap );
265     mVisual.SetTransformAndSize( transformMap, size );
266   }
267 }
268
269 ///////////////////////////////////////////////////////////
270 //
271 // Properties
272 //
273
274 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
275 {
276   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
277
278   if ( imageView )
279   {
280     ImageView& impl = GetImpl( imageView );
281     switch ( index )
282     {
283       case Toolkit::ImageView::Property::RESOURCE_URL:
284       {
285         std::string imageUrl;
286         if( value.Get( imageUrl ) )
287         {
288           impl.SetImage( imageUrl, ImageDimensions() );
289         }
290         break;
291       }
292
293       case Toolkit::ImageView::Property::IMAGE:
294       {
295         std::string imageUrl;
296         Property::Map map;
297         if( value.Get( imageUrl ) )
298         {
299           impl.SetImage( imageUrl, ImageDimensions() );
300         }
301         // if its not a string then get a Property::Map from the property if possible.
302         else if( value.Get( map ) )
303         {
304           Property::Value* shaderValue = map.Find( Toolkit::VisualProperty::SHADER, CUSTOM_SHADER );
305           // set image only if property map contains image information other than custom shader
306           if( map.Count() > 1u ||  !shaderValue )
307           {
308             impl.SetImage( map );
309           }
310           // the property map contains only the custom shader
311           else if(  impl.mVisual && map.Count() == 1u &&  shaderValue )
312           {
313             Property::Map shaderMap;
314             if( shaderValue->Get( shaderMap ) )
315             {
316               Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
317               visual.SetCustomShader( shaderMap );
318               if( imageView.OnStage() )
319               {
320                 // force to create new core renderer to use the newly set shader
321                 visual.SetOffStage( imageView );
322                 visual.SetOnStage( imageView );
323               }
324             }
325           }
326         }
327         break;
328       }
329
330       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
331       {
332         bool isPre;
333         if( value.Get( isPre ) )
334         {
335           impl.EnablePreMultipliedAlpha( isPre );
336         }
337         break;
338       }
339     }
340   }
341 }
342
343 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
344 {
345   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 ( propertyIndex )
353     {
354       case Toolkit::ImageView::Property::RESOURCE_URL:
355       {
356         if ( !impl.mUrl.empty() )
357         {
358           value = impl.mUrl;
359         }
360         break;
361       }
362
363       case Toolkit::ImageView::Property::IMAGE:
364       {
365         if ( !impl.mUrl.empty() )
366         {
367           value = impl.mUrl;
368         }
369         else if( impl.mImage )
370         {
371           Property::Map map;
372           Scripting::CreatePropertyMap( impl.mImage, map );
373           value = map;
374         }
375         else if( !impl.mPropertyMap.Empty() )
376         {
377           value = impl.mPropertyMap;
378         }
379         break;
380       }
381
382       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
383       {
384         value = impl.IsPreMultipliedAlphaEnabled();
385         break;
386       }
387     }
388   }
389
390   return value;
391 }
392
393 } // namespace Internal
394 } // namespace Toolkit
395 } // namespace Dali