Merge "LLVM/Emscripten fixes" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / image-actor-impl.cpp
1 /*
2  * Copyright (c) 2014 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 <dali/internal/event/actors/image-actor-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/images/nine-patch-image-impl.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/internal/event/common/property-index-ranges.h>
25 #include <dali/internal/event/images/image-connector.h>
26 #include <dali/public-api/scripting/scripting.h>
27
28 namespace Dali
29 {
30
31 const Property::Index ImageActor::PIXEL_AREA           = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
32 const Property::Index ImageActor::STYLE                = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 1;
33 const Property::Index ImageActor::BORDER               = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 2;
34 const Property::Index ImageActor::IMAGE                = Internal::DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT + 3;
35
36 namespace Internal
37 {
38 bool ImageActor::mFirstInstance = true;
39 Actor::DefaultPropertyLookup* ImageActor::mDefaultImageActorPropertyLookup = NULL;
40
41 namespace
42 {
43
44 BaseHandle Create()
45 {
46   return Dali::ImageActor::New();
47 }
48
49 TypeRegistration mType( typeid(Dali::ImageActor), typeid(Dali::RenderableActor), Create );
50
51 const std::string DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[] =
52 {
53   "pixel-area",
54   "style",
55   "border",
56   "image"
57 };
58 const int DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
59
60 const Property::Type DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT] =
61 {
62   Property::RECTANGLE,  // "pixel-area",
63   Property::STRING,     // "style",
64   Property::VECTOR4,    // "border",
65   Property::MAP,        // "image",
66 };
67
68 ImageActor::Style StyleEnum(const std::string &s)
69 {
70   if(s == "STYLE_NINE_PATCH")
71   {
72     return Dali::ImageActor::STYLE_NINE_PATCH;
73   }
74   else // if(s == "QUAD")
75   {
76     return Dali::ImageActor::STYLE_QUAD;
77   }
78 }
79
80 std::string StyleString(const ImageActor::Style style)
81 {
82   if(style == Dali::ImageActor::STYLE_NINE_PATCH)
83   {
84     return "STYLE_NINE_PATCH";
85   }
86   else // if(s == "QUAD")
87   {
88     return "STYLE_QUAD";
89   }
90 }
91 }
92
93 ImageActorPtr ImageActor::New()
94 {
95   ImageActorPtr actor( new ImageActor );
96
97   // Second-phase construction of base class
98   actor->Initialize();
99
100   // Create the attachment
101   actor->mImageAttachment = ImageAttachment::New( *actor->mNode );
102   actor->Attach( *actor->mImageAttachment );
103
104   return actor;
105 }
106
107 void ImageActor::OnInitialize()
108 {
109   if(ImageActor::mFirstInstance)
110   {
111     mDefaultImageActorPropertyLookup = new DefaultPropertyLookup();
112     const int start = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
113     for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i )
114     {
115       (*mDefaultImageActorPropertyLookup)[DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[i]] = i + start;
116     }
117     ImageActor::mFirstInstance = false;
118   }
119 }
120
121 void ImageActor::SetImage( ImagePtr& image )
122 {
123   ImagePtr currentImage = mImageAttachment->GetImage();
124   // early exit if it's the same image as we already have
125   if ( currentImage == image )
126   {
127     return;
128   }
129
130   // NOTE! image might be pointing to NULL, which is fine as in that case app wants to just remove the image
131   ImagePtr newImage( image );
132   // if image is not NULL, check for 9 patch
133   if( newImage )
134   {
135     // Automatically convert nine-patch images to cropped bitmap
136     NinePatchImage* ninePatchImage = NinePatchImage::DownCast( image.Get() );
137     if( ninePatchImage )
138     {
139       newImage = ninePatchImage->CreateCroppedBitmapImage();
140       SetStyle( Dali::ImageActor::STYLE_NINE_PATCH );
141       SetNinePatchBorder( ninePatchImage->GetStretchBorders(), true );
142     }
143   }
144   // set the actual image (normal or 9 patch) and natural size based on that
145   mImageAttachment->SetImage( newImage );
146   SetNaturalSize();
147 }
148
149 ImagePtr ImageActor::GetImage()
150 {
151   return mImageAttachment->GetImage();
152 }
153
154 void ImageActor::SetToNaturalSize()
155 {
156   mUsingNaturalSize = true;
157
158   SetNaturalSize();
159 }
160
161 void ImageActor::SetPixelArea( const PixelArea& pixelArea )
162 {
163   mImageAttachment->SetPixelArea( pixelArea );
164
165   SetNaturalSize();
166 }
167
168 const ImageActor::PixelArea& ImageActor::GetPixelArea() const
169 {
170   return mImageAttachment->GetPixelArea();
171 }
172
173 bool ImageActor::IsPixelAreaSet() const
174 {
175   return mImageAttachment->IsPixelAreaSet();
176 }
177
178 void ImageActor::ClearPixelArea()
179 {
180   mImageAttachment->ClearPixelArea();
181
182   if( mUsingNaturalSize )
183   {
184     ImagePtr image = mImageAttachment->GetImage();
185     if( image )
186     {
187       mInternalSetSize = true;
188       SetSize( image->GetNaturalSize() );
189       mInternalSetSize = false;
190     }
191   }
192 }
193
194 void ImageActor::SetStyle( Style style )
195 {
196   mImageAttachment->SetStyle( style );
197 }
198
199 ImageActor::Style ImageActor::GetStyle() const
200 {
201   return mImageAttachment->GetStyle();
202 }
203
204 void ImageActor::SetNinePatchBorder( const Vector4& border, bool inPixels )
205 {
206   mImageAttachment->SetNinePatchBorder( border, inPixels );
207 }
208
209 Vector4 ImageActor::GetNinePatchBorder() const
210 {
211   return mImageAttachment->GetNinePatchBorder();
212 }
213
214 ImageAttachment& ImageActor::GetImageAttachment()
215 {
216   return *mImageAttachment;
217 }
218
219 RenderableAttachment& ImageActor::GetRenderableAttachment() const
220 {
221   DALI_ASSERT_DEBUG( mImageAttachment && "ImageAttachment missing from ImageActor" );
222   return *mImageAttachment;
223 }
224
225 ImageActor::ImageActor()
226 : RenderableActor(),
227   mUsingNaturalSize(true),
228   mInternalSetSize(false)
229 {
230 }
231
232 ImageActor::~ImageActor()
233 {
234 }
235
236 void ImageActor::SetNaturalSize()
237 {
238   if( mUsingNaturalSize )
239   {
240     // if no image then natural size is 0
241     Vector2 size;
242     ImagePtr image = mImageAttachment->GetImage();
243     if( image )
244     {
245       if( IsPixelAreaSet() )
246       {
247         PixelArea area(GetPixelArea());
248         size.width = area.width;
249         size.height = area.height;
250       }
251       else
252       {
253         size = image->GetNaturalSize();
254       }
255     }
256     mInternalSetSize = true;
257     SetSize( size );
258     mInternalSetSize = false;
259   }
260 }
261
262 void ImageActor::OnSizeSet( const Vector3& targetSize )
263 {
264   if( !mInternalSetSize )
265   {
266     mUsingNaturalSize = false;
267   }
268 }
269
270 void ImageActor::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
271 {
272   mUsingNaturalSize = false;
273 }
274
275 void ImageActor::OnStageConnectionInternal()
276 {
277 }
278
279 void ImageActor::OnStageDisconnectionInternal()
280 {
281 }
282
283 unsigned int ImageActor::GetDefaultPropertyCount() const
284 {
285   return RenderableActor::GetDefaultPropertyCount() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT;
286 }
287
288 void ImageActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
289 {
290   RenderableActor::GetDefaultPropertyIndices( indices ); // RenderableActor class properties
291
292   indices.reserve( indices.size() + DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT );
293
294   int index = DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
295   for ( int i = 0; i < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT; ++i, ++index )
296   {
297     indices.push_back( index );
298   }
299 }
300
301 bool ImageActor::IsDefaultPropertyWritable( Property::Index index ) const
302 {
303   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
304   {
305     return RenderableActor::IsDefaultPropertyWritable(index);
306   }
307   else
308   {
309     return true;
310   }
311 }
312
313 bool ImageActor::IsDefaultPropertyAnimatable( Property::Index index ) const
314 {
315   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
316   {
317     return RenderableActor::IsDefaultPropertyAnimatable(index);
318   }
319   else
320   {
321     return false;
322   }
323 }
324
325 bool ImageActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
326 {
327   if( index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT )
328   {
329     return RenderableActor::IsDefaultPropertyAConstraintInput(index);
330   }
331   return true; // Our properties can be used as input to constraints.
332 }
333
334 Property::Type ImageActor::GetDefaultPropertyType( Property::Index index ) const
335 {
336   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
337   {
338     return RenderableActor::GetDefaultPropertyType(index);
339   }
340   else
341   {
342     index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
343
344     if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
345     {
346       return DEFAULT_IMAGE_ACTOR_PROPERTY_TYPES[index];
347     }
348     else
349     {
350       // index out-of-bounds
351       return Property::NONE;
352     }
353   }
354 }
355
356 const std::string& ImageActor::GetDefaultPropertyName( Property::Index index ) const
357 {
358   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
359   {
360     return RenderableActor::GetDefaultPropertyName(index);
361   }
362   else
363   {
364     index -= DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT;
365
366     if ( ( index >= 0 ) && ( index < DEFAULT_IMAGE_ACTOR_PROPERTY_COUNT ) )
367     {
368       return DEFAULT_IMAGE_ACTOR_PROPERTY_NAMES[index];
369     }
370     else
371     {
372       // index out-of-bounds
373       static const std::string INVALID_PROPERTY_NAME;
374       return INVALID_PROPERTY_NAME;
375     }
376   }
377 }
378
379 Property::Index ImageActor::GetDefaultPropertyIndex(const std::string& name) const
380 {
381   Property::Index index = Property::INVALID_INDEX;
382
383   DALI_ASSERT_DEBUG( NULL != mDefaultImageActorPropertyLookup );
384
385   // Look for name in current class' default properties
386   DefaultPropertyLookup::const_iterator result = mDefaultImageActorPropertyLookup->find( name );
387   if ( mDefaultImageActorPropertyLookup->end() != result )
388   {
389     index = result->second;
390   }
391   else
392   {
393     // If not found, check in base class
394     index = RenderableActor::GetDefaultPropertyIndex( name );
395   }
396
397   return index;
398 }
399
400 void ImageActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
401 {
402   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
403   {
404     RenderableActor::SetDefaultProperty(index, propertyValue);
405   }
406   else
407   {
408     switch(index)
409     {
410       case Dali::ImageActor::PIXEL_AREA:
411       {
412         SetPixelArea(propertyValue.Get<Rect<int> >());
413         break;
414       }
415       case Dali::ImageActor::STYLE:
416       {
417         SetStyle(StyleEnum(propertyValue.Get<std::string>()));
418         break;
419       }
420       case Dali::ImageActor::BORDER:
421       {
422         SetNinePatchBorder( propertyValue.Get<Vector4>(), true /*in pixels*/ );
423         break;
424       }
425       case Dali::ImageActor::IMAGE:
426       {
427         Dali::Image img = Scripting::NewImage( propertyValue );
428         if(img)
429         {
430           ImagePtr image( &GetImplementation(img) );
431           SetImage( image );
432         }
433         else
434         {
435           DALI_LOG_WARNING("Cannot create image from property value\n");
436         }
437         break;
438       }
439       default:
440       {
441         DALI_LOG_WARNING("Unknown property (%d)\n", index);
442         break;
443       }
444     } // switch(index)
445
446   } // else
447 }
448
449 Property::Value ImageActor::GetDefaultProperty( Property::Index index ) const
450 {
451   Property::Value ret;
452   if(index < DEFAULT_RENDERABLE_ACTOR_PROPERTY_MAX_COUNT)
453   {
454     ret = RenderableActor::GetDefaultProperty(index);
455   }
456   else
457   {
458     switch(index)
459     {
460       case Dali::ImageActor::PIXEL_AREA:
461       {
462         Rect<int> r = GetPixelArea();
463         ret = r;
464         break;
465       }
466       case Dali::ImageActor::STYLE:
467       {
468         ret = StyleString(GetStyle());
469         break;
470       }
471       case Dali::ImageActor::BORDER:
472       {
473         ret = GetNinePatchBorder();
474         break;
475       }
476       case Dali::ImageActor::IMAGE:
477       {
478         Property::Map map;
479         Scripting::CreatePropertyMap( Dali::Image( mImageAttachment->GetImage().Get() ), map );
480         ret = Property::Value( map );
481         break;
482       }
483       default:
484       {
485         DALI_LOG_WARNING("Unknown property (%d)\n", index);
486         break;
487       }
488     } // switch(index)
489   }
490
491   return ret;
492 }
493
494 } // namespace Internal
495
496 } // namespace Dali