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