793410958937fb0738f4587142c52a3a1c82f4f6
[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   mRelayoutRequired(true)
69 {
70 }
71
72 ImageView::~ImageView()
73 {
74 }
75
76 Toolkit::ImageView ImageView::New()
77 {
78   ImageView* impl = new ImageView();
79
80   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
81
82   // Second-phase init of the implementation
83   // This can only be done after the CustomActor connection has been made...
84   impl->Initialize();
85
86   return handle;
87 }
88
89 /////////////////////////////////////////////////////////////
90
91 void ImageView::OnInitialize()
92 {
93   // ImageView can relayout in the OnImageReady, alternative to a signal would be to have a upcall from the Control to ImageView
94   Dali::Toolkit::Control handle( GetOwner() );
95   Toolkit::DevelControl::ResourceReadySignal( handle ).Connect( this, &ImageView::OnResourceReady );
96 }
97
98 void ImageView::SetImage( Image image )
99 {
100   // Don't bother comparing if we had a visual previously, just drop old visual and create new one
101   mImage = image;
102   mUrl.clear();
103   mPropertyMap.Clear();
104
105   Toolkit::Visual::Base visual =  Toolkit::VisualFactory::Get().CreateVisual( image );
106   if (!mVisual)
107   {
108     mVisual = visual;
109   }
110
111   DevelControl::RegisterVisual( *this, Toolkit::ImageView::Property::IMAGE, visual  );
112 }
113
114 void ImageView::SetImage( const Property::Map& map )
115 {
116   // Comparing a property map is too expensive so just creating a new visual
117   mPropertyMap = map;
118   mUrl.clear();
119   mImage.Reset();
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 Vector3 ImageView::GetNaturalSize()
178 {
179   if( mVisual )
180   {
181     Vector2 rendererNaturalSize;
182     mVisual.GetNaturalSize( rendererNaturalSize );
183     return Vector3( rendererNaturalSize );
184   }
185
186   // if no visual then use Control's natural size
187   return Control::GetNaturalSize();
188 }
189
190 float ImageView::GetHeightForWidth( float width )
191 {
192   if( mVisual )
193   {
194     return mVisual.GetHeightForWidth( width );
195   }
196   else
197   {
198     return Control::GetHeightForWidth( width );
199   }
200 }
201
202 float ImageView::GetWidthForHeight( float height )
203 {
204   if( mVisual )
205   {
206     return mVisual.GetWidthForHeight( height );
207   }
208   else
209   {
210     return Control::GetWidthForHeight( height );
211   }
212 }
213
214 void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
215 {
216   Control::OnRelayout( size, container );
217
218   // If visual is being replaced then mVisual will be the replacement visual even if not ready.
219   mVisual = DevelControl::GetVisual( *this, Toolkit::ImageView::Property::IMAGE );
220
221   if( mVisual )
222   {
223     // Pass in an empty map which uses default transform values meaning our visual fills the control
224     // Should provide a transform that handles aspect ratio according to image size
225     mVisual.SetTransformAndSize( Property::Map(), size );
226   }
227 }
228
229 void ImageView::OnResourceReady( Toolkit::Control control )
230 {
231   if( mRelayoutRequired)
232   {
233     mRelayoutRequired = false;
234     RelayoutRequest();
235   }
236 }
237
238 ///////////////////////////////////////////////////////////
239 //
240 // Properties
241 //
242
243 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
244 {
245   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
246
247   if ( imageView )
248   {
249     ImageView& impl = GetImpl( imageView );
250     switch ( index )
251     {
252       case Toolkit::ImageView::Property::RESOURCE_URL:
253       {
254         std::string imageUrl;
255         if( value.Get( imageUrl ) )
256         {
257           impl.SetImage( imageUrl, ImageDimensions() );
258         }
259         break;
260       }
261
262       case Toolkit::ImageView::Property::IMAGE:
263       {
264         std::string imageUrl;
265         Property::Map* map;
266         if( value.Get( imageUrl ) )
267         {
268           impl.SetImage( imageUrl, ImageDimensions() );
269         }
270         // if its not a string then get a Property::Map from the property if possible.
271         else
272         {
273           map = value.GetMap();
274           if( map )
275           {
276             Property::Value* shaderValue = map->Find( Toolkit::DevelVisual::Property::SHADER, CUSTOM_SHADER );
277             // set image only if property map contains image information other than custom shader
278             if( map->Count() > 1u ||  !shaderValue )
279             {
280               impl.SetImage( *map );
281             }
282             // the property map contains only the custom shader
283             else if( ( impl.mVisual )&&( map->Count() == 1u )&&( shaderValue ) )
284             {
285               Property::Map* shaderMap = shaderValue->GetMap();
286               if( shaderMap )
287               {
288                 Internal::Visual::Base& visual = Toolkit::GetImplementation( impl.mVisual );
289                 visual.SetCustomShader( *shaderMap );
290                 if( imageView.OnStage() )
291                 {
292                   // force to create new core renderer to use the newly set shader
293                   visual.SetOffStage( imageView );
294                   visual.SetOnStage( imageView );
295                 }
296               }
297             }
298           }
299         }
300         break;
301       }
302
303       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
304       {
305         bool isPre;
306         if( value.Get( isPre ) )
307         {
308           impl.EnablePreMultipliedAlpha( isPre );
309         }
310         break;
311       }
312     }
313   }
314 }
315
316 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
317 {
318   Property::Value value;
319
320   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
321
322   if ( imageview )
323   {
324     ImageView& impl = GetImpl( imageview );
325     switch ( propertyIndex )
326     {
327       case Toolkit::ImageView::Property::RESOURCE_URL:
328       {
329         if ( !impl.mUrl.empty() )
330         {
331           value = impl.mUrl;
332         }
333         break;
334       }
335
336       case Toolkit::ImageView::Property::IMAGE:
337       {
338         if ( !impl.mUrl.empty() )
339         {
340           value = impl.mUrl;
341         }
342         else if( impl.mImage )
343         {
344           Property::Map map;
345           Scripting::CreatePropertyMap( impl.mImage, map );
346           value = map;
347         }
348         else if( !impl.mPropertyMap.Empty() )
349         {
350           value = impl.mPropertyMap;
351         }
352         break;
353       }
354
355       case Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA:
356       {
357         value = impl.IsPreMultipliedAlphaEnabled();
358         break;
359       }
360     }
361   }
362
363   return value;
364 }
365
366 } // namespace Internal
367 } // namespace Toolkit
368 } // namespace Dali