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