[dali_1.2.41] Merge branch '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/visuals/visual-properties-devel.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 } // anonymous namespace
47
48 using namespace Dali;
49
50 ImageView::ImageView()
51 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) )
52 {
53 }
54
55 ImageView::~ImageView()
56 {
57 }
58
59 Toolkit::ImageView ImageView::New()
60 {
61   ImageView* impl = new ImageView();
62
63   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
64
65   // Second-phase init of the implementation
66   // This can only be done after the CustomActor connection has been made...
67   impl->Initialize();
68
69   return handle;
70 }
71
72 /////////////////////////////////////////////////////////////
73
74 void ImageView::SetImage( Image image )
75 {
76   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
77   mImage = image;
78   mUrl.clear();
79   mPropertyMap.Clear();
80
81   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( image );
82   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
83
84   RelayoutRequest();
85 }
86
87 void ImageView::SetImage( const Property::Map& map )
88 {
89   // Comparing a property map is too expensive so just creating a new visual
90   mPropertyMap = map;
91   mUrl.clear();
92   mImage.Reset();
93
94   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
95   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual  );
96
97   RelayoutRequest();
98 }
99
100 void ImageView::SetImage( const std::string& url, ImageDimensions size )
101 {
102   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
103   mUrl = url;
104   mImage.Reset();
105   mPropertyMap.Clear();
106
107   mVisual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
108   RegisterVisual( Toolkit::ImageView::Property::IMAGE, mVisual );
109
110   RelayoutRequest();
111 }
112
113 Image ImageView::GetImage() const
114 {
115   return mImage;
116 }
117
118 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
119 {
120   if( mVisual )
121   {
122     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
123   }
124 }
125
126 bool ImageView::IsPreMultipliedAlphaEnabled() const
127 {
128   if( mVisual )
129   {
130     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
131   }
132   return false;
133 }
134
135 void ImageView::SetDepthIndex( int depthIndex )
136 {
137   if( mVisual )
138   {
139     mVisual.SetDepthIndex( depthIndex );
140   }
141 }
142
143 Vector3 ImageView::GetNaturalSize()
144 {
145   if( mVisual )
146   {
147     Vector2 rendererNaturalSize;
148     mVisual.GetNaturalSize( rendererNaturalSize );
149     return Vector3( rendererNaturalSize );
150   }
151
152   // if no visual then use Control's natural size
153   return Control::GetNaturalSize();
154 }
155
156 float ImageView::GetHeightForWidth( float width )
157 {
158   if( mVisual )
159   {
160     return mVisual.GetHeightForWidth( width );
161   }
162   else
163   {
164     return Control::GetHeightForWidth( width );
165   }
166 }
167
168 float ImageView::GetWidthForHeight( float height )
169 {
170   if( mVisual )
171   {
172     return mVisual.GetWidthForHeight( height );
173   }
174   else
175   {
176     return Control::GetWidthForHeight( height );
177   }
178 }
179
180 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
181 {
182   Control::OnRelayout( size, container );
183
184   if( mVisual )
185   {
186     // Pass in an empty map which uses default transform values meaning our visual fills the control
187     // Should provide a transform that handles aspect ratio according to image size
188     mVisual.SetTransformAndSize( Property::Map(), size );
189   }
190 }
191
192 ///////////////////////////////////////////////////////////
193 //
194 // Properties
195 //
196
197 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
198 {
199   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
200
201   if ( imageView )
202   {
203     ImageView& impl = GetImpl( imageView );
204     switch ( index )
205     {
206       case Toolkit::ImageView::Property::RESOURCE_URL:
207       {
208         std::string imageUrl;
209         if( value.Get( imageUrl ) )
210         {
211           impl.SetImage( imageUrl, ImageDimensions() );
212         }
213         break;
214       }
215
216       case Toolkit::ImageView::Property::IMAGE:
217       {
218         std::string imageUrl;
219         Property::Map* map;
220         if( value.Get( imageUrl ) )
221         {
222           impl.SetImage( imageUrl, ImageDimensions() );
223         }
224         // if its not a string then get a Property::Map from the property if possible.
225         else
226         {
227           map = value.GetMap();
228           if( map )
229           {
230             Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
231             // set image only if property map contains image information other than custom shader
232             if( map->Count() > 1u ||  !shaderValue )
233             {
234               impl.SetImage( *map );
235             }
236             // the property map contains only the custom shader
237             else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
238             {
239               Property::Map* shaderMap = shaderValue->GetMap();
240               if( shaderMap )
241               {
242                 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
243                 visual.SetCustomShader( *shaderMap );
244                 if( imageView.OnStage() )
245                 {
246                   // force to create new core renderer to use the newly set shader
247                   visual.SetOffStage( imageView );
248                   visual.SetOnStage( imageView );
249                 }
250               }
251             }
252           }
253         }
254         break;
255       }
256
257       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
258       {
259         bool isPre;
260         if( value.Get( isPre ) )
261         {
262           impl.EnablePreMultipliedAlpha( isPre );
263         }
264         break;
265       }
266     }
267   }
268 }
269
270 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
271 {
272   Property::Value value;
273
274   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
275
276   if ( imageview )
277   {
278     ImageView& impl = GetImpl( imageview );
279     switch ( propertyIndex )
280     {
281       case Toolkit::ImageView::Property::RESOURCE_URL:
282       {
283         if ( !impl.mUrl.empty() )
284         {
285           value = impl.mUrl;
286         }
287         break;
288       }
289
290       case Toolkit::ImageView::Property::IMAGE:
291       {
292         if ( !impl.mUrl.empty() )
293         {
294           value = impl.mUrl;
295         }
296         else if( impl.mImage )
297         {
298           Property::Map map;
299           Scripting::CreatePropertyMap( impl.mImage, map );
300           value = map;
301         }
302         else if( !impl.mPropertyMap.Empty() )
303         {
304           value = impl.mPropertyMap;
305         }
306         break;
307       }
308
309       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
310       {
311         value = impl.IsPreMultipliedAlphaEnabled();
312         break;
313       }
314     }
315   }
316
317   return value;
318 }
319
320 } // namespace Internal
321 } // namespace Toolkit
322 } // namespace Dali