(Visuals) Added visual indices
[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/public-api/visuals/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
19 namespace Dali
20 {
21
22 namespace Toolkit
23 {
24
25 namespace Internal
26 {
27
28 namespace
29 {
30
31 BaseHandle Create()
32 {
33   return Toolkit::ImageView::New();
34 }
35
36 // Setup properties, signals and actions using the type-registry.
37 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
38 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "resourceUrl", STRING, RESOURCE_URL )
39 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
40 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "preMultipliedAlpha", BOOLEAN, PRE_MULTIPLIED_ALPHA )
41
42 DALI_ANIMATABLE_PROPERTY_REGISTRATION_WITH_DEFAULT( Toolkit, ImageView, "pixelArea", Vector4(0.f, 0.f, 1.f, 1.f), PIXEL_AREA )
43 DALI_TYPE_REGISTRATION_END()
44
45 } // anonymous namespace
46
47 using namespace Dali;
48
49 ImageView::ImageView()
50 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) )
51 {
52 }
53
54 ImageView::~ImageView()
55 {
56 }
57
58 Toolkit::ImageView ImageView::New()
59 {
60   ImageView* impl = new ImageView();
61
62   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
63
64   // Second-phase init of the implementation
65   // This can only be done after the CustomActor connection has been made...
66   impl->Initialize();
67
68   return handle;
69 }
70
71 /////////////////////////////////////////////////////////////
72
73 void ImageView::SetImage( Image image )
74 {
75   if( ( mImage != image ) ||
76       ! mUrl.empty()      ||   // If we're changing from a URL type to an Image type
77       ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
78   {
79     mUrl.clear();
80     mPropertyMap.Clear();
81
82     mImage = image;
83
84     Actor self( Self() );
85     InitializeVisual( self, mVisual, image );
86     mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
87
88     RelayoutRequest();
89   }
90 }
91
92 void ImageView::SetImage( Property::Map map )
93 {
94   mUrl.clear();
95   mImage.Reset();
96   mPropertyMap = map;
97
98   Actor self( Self() );
99   InitializeVisual( self, mVisual, mPropertyMap );
100
101   Property::Value* widthValue = mPropertyMap.Find( "width" );
102   if( widthValue )
103   {
104     int width;
105     if( widthValue->Get( width ) )
106     {
107       mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
108     }
109   }
110
111   Property::Value* heightValue = mPropertyMap.Find( "height" );
112   if( heightValue )
113   {
114     int height;
115     if( heightValue->Get( height ) )
116     {
117       mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
118     }
119   }
120
121   RelayoutRequest();
122 }
123
124 void ImageView::SetImage( const std::string& url, ImageDimensions size )
125 {
126   if( ( mUrl != url ) ||
127         mImage        ||       // If we're changing from an Image type to a URL type
128       ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
129   {
130     mImage.Reset();
131     mPropertyMap.Clear();
132
133     mUrl = url;
134
135     if( size.GetWidth() != 0u && size.GetHeight() != 0u )
136     {
137       mImageSize = size;
138     }
139
140     Actor self( Self() );
141     InitializeVisual( self, mVisual, url, size );
142
143     mVisual.SetSize( mSizeSet );
144
145     RelayoutRequest();
146   }
147 }
148
149 Image ImageView::GetImage() const
150 {
151   return mImage;
152 }
153
154 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
155 {
156   if( mVisual )
157   {
158     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
159   }
160 }
161
162 bool ImageView::IsPreMultipliedAlphaEnabled() const
163 {
164   if( mVisual )
165   {
166     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
167   }
168   return false;
169 }
170
171 void ImageView::SetDepthIndex( int depthIndex )
172 {
173   if( mVisual )
174   {
175     mVisual.SetDepthIndex( depthIndex );
176   }
177 }
178
179 Vector3 ImageView::GetNaturalSize()
180 {
181   if( mVisual )
182   {
183     Vector2 rendererNaturalSize;
184     mVisual.GetNaturalSize( rendererNaturalSize );
185     return Vector3( rendererNaturalSize );
186   }
187
188   Vector3 size;
189   size.x = mImageSize.GetWidth();
190   size.y = mImageSize.GetHeight();
191
192   if( size.x > 0 && size.y > 0 )
193   {
194     size.z = std::min(size.x, size.y);
195     return size;
196   }
197   else
198   {
199     // if no image then use Control's natural size
200     return Control::GetNaturalSize();
201   }
202 }
203
204 float ImageView::GetHeightForWidth( float width )
205 {
206   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
207   {
208     return GetHeightForWidthBase( width );
209   }
210   else
211   {
212     return Control::GetHeightForWidth( width );
213   }
214 }
215
216 float ImageView::GetWidthForHeight( float height )
217 {
218   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
219   {
220     return GetWidthForHeightBase( height );
221   }
222   else
223   {
224     return Control::GetWidthForHeight( height );
225   }
226 }
227
228
229 ///////////////////////////////////////////////////////////
230 //
231 // Private methods
232 //
233
234 void ImageView::OnStageConnection( int depth )
235 {
236   Control::OnStageConnection( depth );
237
238   if( mVisual )
239   {
240     CustomActor self = Self();
241     mVisual.SetOnStage( self );
242   }
243 }
244
245 void ImageView::OnStageDisconnection()
246 {
247   if( mVisual )
248   {
249     CustomActor self = Self();
250     mVisual.SetOffStage( self );
251   }
252
253   Control::OnStageDisconnection();
254 }
255
256 void ImageView::OnSizeSet( const Vector3& targetSize )
257 {
258   Control::OnSizeSet( targetSize );
259   mSizeSet = targetSize;
260
261   if( mVisual )
262   {
263     Vector2 size( targetSize );
264     mVisual.SetSize( size );
265   }
266 }
267
268 ///////////////////////////////////////////////////////////
269 //
270 // Properties
271 //
272
273 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
274 {
275   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
276
277   if ( imageView )
278   {
279     ImageView& impl = GetImpl( imageView );
280     switch ( index )
281     {
282       case Toolkit::ImageView::Property::RESOURCE_URL:
283       {
284         std::string imageUrl;
285         if( value.Get( imageUrl ) )
286         {
287           impl.SetImage( imageUrl, ImageDimensions() );
288         }
289         break;
290       }
291
292       case Toolkit::ImageView::Property::IMAGE:
293       {
294         std::string imageUrl;
295         Property::Map map;
296         if( value.Get( imageUrl ) )
297         {
298           impl.SetImage( imageUrl, ImageDimensions() );
299         }
300         // if its not a string then get a Property::Map from the property if possible.
301         else if( value.Get( map ) )
302         {
303           Property::Value* shaderValue = map.Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
304           // set image only if property map contains image information other than custom shader
305           if( map.Count() > 1u ||  !shaderValue )
306           {
307             impl.SetImage( map );
308           }
309           // the property map contains only the custom shader
310           else if(  impl.mVisual && map.Count() == 1u &&  shaderValue )
311           {
312             Property::Map shaderMap;
313             if( shaderValue->Get( shaderMap ) )
314             {
315               Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
316               visual.SetCustomShader( shaderMap );
317               if( imageView.OnStage() )
318               {
319                 // force to create new core renderer to use the newly set shader
320                 visual.SetOffStage( imageView );
321                 visual.SetOnStage( imageView );
322               }
323             }
324           }
325         }
326         break;
327       }
328
329       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
330       {
331         bool isPre;
332         if( value.Get( isPre ) )
333         {
334           impl.EnablePreMultipliedAlpha( isPre );
335         }
336         break;
337       }
338     }
339   }
340 }
341
342 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
343 {
344   Property::Value value;
345
346   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
347
348   if ( imageview )
349   {
350     ImageView& impl = GetImpl( imageview );
351     switch ( propertyIndex )
352     {
353       case Toolkit::ImageView::Property::RESOURCE_URL:
354       {
355         if ( !impl.mUrl.empty() )
356         {
357           value = impl.mUrl;
358         }
359         break;
360       }
361
362       case Toolkit::ImageView::Property::IMAGE:
363       {
364         if ( !impl.mUrl.empty() )
365         {
366           value = impl.mUrl;
367         }
368         else if( impl.mImage )
369         {
370           Property::Map map;
371           Scripting::CreatePropertyMap( impl.mImage, map );
372           value = map;
373         }
374         else if( !impl.mPropertyMap.Empty() )
375         {
376           value = impl.mPropertyMap;
377         }
378         break;
379       }
380
381       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
382       {
383         value = impl.IsPreMultipliedAlphaEnabled();
384         break;
385       }
386     }
387   }
388
389   return value;
390 }
391
392 } // namespace Internal
393 } // namespace Toolkit
394 } // namespace Dali