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