[4.0] Add MARGIN and PADDING property in Control
[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 {
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::OnInitialize()
91 {
92   // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
93   Dali::Toolkit::Control handle( GetOwner() );
94   handle.ResourceReadySignal().Connect( this, &ImageView::OnResourceReady );
95 }
96
97 void ImageView::SetImage( Image image )
98 {
99   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
100   mImage = image;
101   mUrl.clear();
102   mPropertyMap.Clear();
103
104   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( image );
105   if (!mVisual)
106   {
107     mVisual = visual;
108   }
109
110   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
111 }
112
113 void ImageView::SetImage( const Property::Map& map )
114 {
115   // Comparing a property map is too expensive so just creating a new visual
116   mPropertyMap = map;
117   mUrl.clear();
118   mImage.Reset();
119
120   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( mPropertyMap );
121   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
122   if (!mVisual)
123   {
124     mVisual = visual;
125   }
126
127   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
128 }
129
130 void ImageView::SetImage( const std::string& url, ImageDimensions size )
131 {
132   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
133   mUrl = url;
134   mImage.Reset();
135   mPropertyMap.Clear();
136
137   // Don't set mVisual until it is ready and shown. Getters will still use current visual.
138   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( url, size );
139   if (!mVisual)
140   {
141     mVisual = visual;
142   }
143
144   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual );
145 }
146
147 Image ImageView::GetImage() const
148 {
149   return mImage;
150 }
151
152 void ImageView::EnablePreMultipliedAlpha( bool preMultipled )
153 {
154   if( mVisual )
155   {
156     Toolkit::GetImplementation( mVisual ).EnablePreMultipliedAlpha( preMultipled );
157   }
158 }
159
160 bool ImageView::IsPreMultipliedAlphaEnabled() const
161 {
162   if( mVisual )
163   {
164     return Toolkit::GetImplementation( mVisual ).IsPreMultipliedAlphaEnabled();
165   }
166   return false;
167 }
168
169 void ImageView::SetDepthIndex( int depthIndex )
170 {
171   if( mVisual )
172   {
173     mVisual.SetDepthIndex( depthIndex );
174   }
175 }
176
177 void ImageView::OnStageConnection( int depth )
178 {
179   if( mImage )
180   {
181     mImage.UploadedSignal().Emit( mImage );
182   }
183
184   Dali::ResourceImage resourceImage = Dali::ResourceImage::DownCast( mImage );
185   if( resourceImage )
186   {
187     resourceImage.LoadingFinishedSignal().Emit( resourceImage );
188   }
189
190   Control::OnStageConnection( depth ); // Enabled visuals will be put on stage
191 }
192
193 Vector3 ImageView::GetNaturalSize()
194 {
195   if( mVisual )
196   {
197     Vector2 rendererNaturalSize;
198     mVisual.GetNaturalSize( rendererNaturalSize );
199
200     Extents padding;
201     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
202
203     rendererNaturalSize.width += ( padding.start + padding.end );
204     rendererNaturalSize.height += ( padding.top + padding.bottom );
205     return Vector3( rendererNaturalSize );
206   }
207
208   // if no visual then use Control's natural size
209   return Control::GetNaturalSize();
210 }
211
212 float ImageView::GetHeightForWidth( float width )
213 {
214   Extents padding;
215   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
216
217   if( mVisual )
218   {
219     return mVisual.GetHeightForWidth( width ) + padding.top + padding.bottom;
220   }
221   else
222   {
223     return Control::GetHeightForWidth( width ) + padding.top + padding.bottom;
224   }
225 }
226
227 float ImageView::GetWidthForHeight( float height )
228 {
229   Extents padding;
230   padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
231
232   if( mVisual )
233   {
234     return mVisual.GetWidthForHeight( height ) + padding.start + padding.end;
235   }
236   else
237   {
238     return Control::GetWidthForHeight( height ) + padding.start + padding.end;
239   }
240 }
241
242 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
243 {
244   Control::OnRelayout( size, container );
245
246   if( mVisual )
247   {
248     Extents margin;
249     margin = Self().GetProperty<Extents>( Toolkit::Control::Property::MARGIN );
250
251     Extents padding;
252     padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
253
254     Property::Map transformMap = Property::Map();
255
256     if( ( padding.start != 0 ) || ( padding.end != 0 ) || ( padding.top != 0 ) || ( padding.bottom != 0 ) ||
257         ( margin.start != 0 ) || ( margin.end != 0 ) || ( margin.top != 0 ) || ( margin.bottom != 0 ) )
258     {
259       transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( margin.start + padding.start, margin.top + padding.top ) )
260                   .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
261                   .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
262                   .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
263     }
264
265     // Should provide a transform that handles aspect ratio according to image size
266     mVisual.SetTransformAndSize( transformMap, size );
267   }
268 }
269
270 void ImageView::OnResourceReady( Toolkit::Control control )
271 {
272   // Visual ready so update visual attached to this ImageView, following call to RelayoutRequest will use this visual.
273   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
274 }
275
276 ///////////////////////////////////////////////////////////
277 //
278 // Properties
279 //
280
281 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
282 {
283   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
284
285   if ( imageView )
286   {
287     ImageView& impl = GetImpl( imageView );
288     switch ( index )
289     {
290       case Toolkit::ImageView::Property::RESOURCE_URL:
291       {
292         std::string imageUrl;
293         if( value.Get( imageUrl ) )
294         {
295           impl.SetImage( imageUrl, ImageDimensions() );
296         }
297         break;
298       }
299
300       case Toolkit::ImageView::Property::IMAGE:
301       {
302         std::string imageUrl;
303         Property::Map* map;
304         if( value.Get( imageUrl ) )
305         {
306           impl.SetImage( imageUrl, ImageDimensions() );
307         }
308         // if its not a string then get a Property::Map from the property if possible.
309         else
310         {
311           map = value.GetMap();
312           if( map )
313           {
314             Property::Value* shaderValue = map->Find( Toolkit::Visual::Property::SHADER, CUSTOM_SHADER );
315             // set image only if property map contains image information other than custom shader
316             if( map->Count() > 1u ||  !shaderValue )
317             {
318               impl.SetImage( *map );
319             }
320             // the property map contains only the custom shader
321             else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
322             {
323               Property::Map* shaderMap = shaderValue->GetMap();
324               if( shaderMap )
325               {
326                 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
327                 visual.SetCustomShader( *shaderMap );
328                 if( imageView.OnStage() )
329                 {
330                   // force to create new core renderer to use the newly set shader
331                   visual.SetOffStage( imageView );
332                   visual.SetOnStage( imageView );
333                 }
334               }
335             }
336           }
337         }
338         break;
339       }
340
341       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
342       {
343         bool isPre;
344         if( value.Get( isPre ) )
345         {
346           impl.EnablePreMultipliedAlpha( isPre );
347         }
348         break;
349       }
350     }
351   }
352 }
353
354 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
355 {
356   Property::Value value;
357
358   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
359
360   if ( imageview )
361   {
362     ImageView& impl = GetImpl( imageview );
363     switch ( propertyIndex )
364     {
365       case Toolkit::ImageView::Property::RESOURCE_URL:
366       {
367         if ( !impl.mUrl.empty() )
368         {
369           value = impl.mUrl;
370         }
371         break;
372       }
373
374       case Toolkit::ImageView::Property::IMAGE:
375       {
376         if ( !impl.mUrl.empty() )
377         {
378           value = impl.mUrl;
379         }
380         else if( impl.mImage )
381         {
382           Property::Map map;
383           Scripting::CreatePropertyMap( impl.mImage, map );
384           value = map;
385         }
386         else if( !impl.mPropertyMap.Empty() )
387         {
388           value = impl.mPropertyMap;
389         }
390         break;
391       }
392
393       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
394       {
395         value = impl.IsPreMultipliedAlphaEnabled();
396         break;
397       }
398     }
399   }
400
401   return value;
402 }
403
404 } // namespace Internal
405 } // namespace Toolkit
406 } // namespace Dali