Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-demo.git] / examples / image-scaling-irregular-grid / image-scaling-irregular-grid-example.cpp
1 /*
2  * Copyright (c) 2019 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 /**
19  * @file image-scaling-irregular-grid-example.cpp
20  * @brief Demonstrates how to use image scaling modes when loading images.
21  *
22  * If an image is going to be drawn on-screen at a lower resolution than it is
23  * stored at on-disk, the scaling feature of the image loader can be used to
24  * reduce the image to save memory, improve performance, and potentially display
25  * a better small version of the image than if the default size were loaded.
26  *
27  * The functions CreateImage and CreateImageView below show how to build an
28  * image using a scaling mode to have %Dali resize it during loading.
29  *
30  * This demo defaults to the SCALE_TO_FILL mode of ImageAttributes which makes
31  * sure that every pixel in the loaded image is filled with a source colour
32  * from the image's central region while losing the minimum number of pixels
33  * from its periphery.
34  * It is the best option for producing thumbnails of input images that have
35  * diverse aspect ratios.
36  *
37  * The other four scaling modes of dali can be cycled-through for the whole
38  * grid  using the button in the top-right of the toolbar.
39  * A single image can be cycled by clicking the image directly.
40  *
41  * @see CreateImage CreateImageView
42  */
43
44 // EXTERNAL INCLUDES
45 #include <algorithm>
46 #include <map>
47 #include <dali-toolkit/dali-toolkit.h>
48 #include <iostream>
49 #include <dali-toolkit/devel-api/controls/control-devel.h>
50
51 // INTERNAL INCLUDES
52 #include "grid-flags.h"
53 #include "shared/view.h"
54
55 using namespace Dali;
56 using namespace Dali::Toolkit;
57 using namespace Dali::Demo;
58
59 namespace
60 {
61
62 /** Controls the output of application logging. */
63 //#define DEBUG_PRINT_DIAGNOSTICS;
64
65 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
66 const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
67 const char* APPLICATION_TITLE( "Image Scaling Modes" );
68 const char* TOGGLE_SCALING_IMAGE( DEMO_IMAGE_DIR "icon-change.png" );
69 const char* TOGGLE_SCALING_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-change-selected.png" );
70
71 /** The width of the grid in whole grid cells. */
72 const unsigned GRID_WIDTH = 9;
73 /** Limit the grid to be no higher than this in units of a cell. */
74 const unsigned GRID_MAX_HEIGHT = 600;
75
76 /** The space between the edge of a grid cell and the image embedded within it. */
77 const unsigned GRID_CELL_PADDING = 4;
78
79 /** The aspect ratio of cells in the image grid. */
80 const float CELL_ASPECT_RATIO = 1.33333333333333333333f;
81
82 const Dali::FittingMode::Type DEFAULT_SCALING_MODE = Dali::FittingMode::SCALE_TO_FILL;
83
84 /** The number of times to spin an image on touching, each spin taking a second.*/
85 const float SPIN_DURATION = 1.0f;
86
87 /** The target image sizes in grid cells. */
88 const Vector2 IMAGE_SIZES[] = {
89  Vector2( 1, 1 ),
90  Vector2( 2, 1 ),
91  Vector2( 3, 1 ),
92  Vector2( 1, 2 ),
93  Vector2( 1, 3 ),
94  Vector2( 2, 3 ),
95  Vector2( 3, 2 ),
96  // Large, tall configuration:
97  Vector2( GRID_WIDTH / 2, GRID_WIDTH + GRID_WIDTH / 2 ),
98  // Large, square-ish images to show shrink-to-fit well with wide and tall images:
99  Vector2( GRID_WIDTH / 2, GRID_WIDTH / 2.0f * CELL_ASPECT_RATIO + 0.5f ),
100  Vector2( GRID_WIDTH - 2, (GRID_WIDTH - 2) * CELL_ASPECT_RATIO + 0.5f ),
101 };
102 const unsigned NUM_IMAGE_SIZES = sizeof(IMAGE_SIZES) / sizeof(IMAGE_SIZES[0]);
103
104 /** Images to load into the grid. These are mostly large and non-square to
105  *  show the scaling. */
106 const char* IMAGE_PATHS[] = {
107
108   DEMO_IMAGE_DIR "dali-logo.png",
109   DEMO_IMAGE_DIR "com.samsung.dali-demo.ico",
110   DEMO_IMAGE_DIR "square_primitive_shapes.bmp",
111   DEMO_IMAGE_DIR "gallery-large-14.wbmp",
112
113   // Images that show aspect ratio changes clearly in primitive shapes:
114
115   DEMO_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
116   DEMO_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
117
118   // Images from other demos that are tall, wide or just large:
119
120   DEMO_IMAGE_DIR "gallery-large-1.jpg",
121   DEMO_IMAGE_DIR "gallery-large-2.jpg",
122   DEMO_IMAGE_DIR "gallery-large-3.jpg",
123   DEMO_IMAGE_DIR "gallery-large-4.jpg",
124   DEMO_IMAGE_DIR "gallery-large-5.jpg",
125   DEMO_IMAGE_DIR "gallery-large-6.jpg",
126   DEMO_IMAGE_DIR "gallery-large-7.jpg",
127   DEMO_IMAGE_DIR "gallery-large-8.jpg",
128   DEMO_IMAGE_DIR "gallery-large-9.jpg",
129   DEMO_IMAGE_DIR "gallery-large-10.jpg",
130   DEMO_IMAGE_DIR "gallery-large-11.jpg",
131   DEMO_IMAGE_DIR "gallery-large-12.jpg",
132   DEMO_IMAGE_DIR "gallery-large-13.jpg",
133   DEMO_IMAGE_DIR "gallery-large-14.jpg",
134   DEMO_IMAGE_DIR "gallery-large-15.jpg",
135   DEMO_IMAGE_DIR "gallery-large-16.jpg",
136   DEMO_IMAGE_DIR "gallery-large-17.jpg",
137   DEMO_IMAGE_DIR "gallery-large-18.jpg",
138   DEMO_IMAGE_DIR "gallery-large-19.jpg",
139   DEMO_IMAGE_DIR "gallery-large-20.jpg",
140   DEMO_IMAGE_DIR "gallery-large-21.jpg",
141
142   DEMO_IMAGE_DIR "background-1.jpg",
143   DEMO_IMAGE_DIR "background-2.jpg",
144   DEMO_IMAGE_DIR "background-3.jpg",
145   DEMO_IMAGE_DIR "background-4.jpg",
146   DEMO_IMAGE_DIR "background-5.jpg",
147   DEMO_IMAGE_DIR "background-blocks.jpg",
148   DEMO_IMAGE_DIR "background-magnifier.jpg",
149
150   DEMO_IMAGE_DIR "background-1.jpg",
151   DEMO_IMAGE_DIR "background-2.jpg",
152   DEMO_IMAGE_DIR "background-3.jpg",
153   DEMO_IMAGE_DIR "background-4.jpg",
154   DEMO_IMAGE_DIR "background-5.jpg",
155   DEMO_IMAGE_DIR "background-blocks.jpg",
156   DEMO_IMAGE_DIR "background-magnifier.jpg",
157
158   DEMO_IMAGE_DIR "book-landscape-cover-back.jpg",
159   DEMO_IMAGE_DIR "book-landscape-cover.jpg",
160   DEMO_IMAGE_DIR "book-landscape-p1.jpg",
161   DEMO_IMAGE_DIR "book-landscape-p2.jpg",
162
163   DEMO_IMAGE_DIR "book-portrait-cover.jpg",
164   DEMO_IMAGE_DIR "book-portrait-p1.jpg",
165   DEMO_IMAGE_DIR "book-portrait-p2.jpg",
166   NULL
167 };
168 const unsigned NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
169 const unsigned int INITIAL_IMAGES_TO_LOAD = 10;
170
171
172 /**
173  * Creates an ImageView
174  *
175  * @param[in] filename The path of the image.
176  * @param[in] width The width of the image in pixels.
177  * @param[in] height The height of the image in pixels.
178  * @param[in] fittingMode The mode to use when scaling the image to fit the desired dimensions.
179  */
180 ImageView CreateImageView(const std::string& filename, int width, int height, Dali::FittingMode::Type fittingMode )
181 {
182
183   ImageView imageView = ImageView::New();
184
185   Property::Map map;
186   map[Toolkit::ImageVisual::Property::URL] = filename;
187   map[Toolkit::ImageVisual::Property::DESIRED_WIDTH] = width;
188   map[Toolkit::ImageVisual::Property::DESIRED_HEIGHT] = height;
189   map[Toolkit::ImageVisual::Property::FITTING_MODE] = fittingMode;
190   imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
191
192   imageView.SetProperty( Dali::Actor::Property::NAME, filename );
193   imageView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
194   imageView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
195
196   return imageView;
197 }
198
199 /** Cycle the scaling mode options. */
200 Dali::FittingMode::Type NextMode( const Dali::FittingMode::Type oldMode )
201 {
202   Dali::FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
203   switch ( oldMode )
204   {
205     case FittingMode::SHRINK_TO_FIT:
206       newMode = FittingMode::SCALE_TO_FILL;
207       break;
208     case FittingMode::SCALE_TO_FILL:
209       newMode = FittingMode::FIT_WIDTH;
210       break;
211     case FittingMode::FIT_WIDTH:
212       newMode = FittingMode::FIT_HEIGHT;
213       break;
214     case FittingMode::FIT_HEIGHT:
215       newMode = FittingMode::SHRINK_TO_FIT;
216       break;
217   }
218   return newMode;
219 }
220
221 /**
222  * Bundle an image path with the rectangle to pack it into.
223  * */
224 struct ImageConfiguration
225 {
226   ImageConfiguration( const char * const path, const Vector2 dimensions ) :
227     path( path ),
228     dimensions( dimensions )
229   {}
230   const char * path;
231   Vector2 dimensions;
232 };
233
234 /**
235  * Post-layout image data.
236  */
237 struct PositionedImage
238 {
239   PositionedImage(ImageConfiguration& configuration, unsigned cellX, unsigned cellY, Vector2 imageGridDims) :
240     configuration( configuration ),
241     cellX( cellX ),
242     cellY( cellY ),
243     imageGridDims( imageGridDims )
244   {}
245
246   ImageConfiguration configuration;
247   unsigned cellX;
248   unsigned cellY;
249   Vector2 imageGridDims;
250 };
251
252 }
253
254 /**
255  * @brief The main class of the demo.
256  */
257 class ImageScalingIrregularGridController : public ConnectionTracker
258 {
259 public:
260
261   ImageScalingIrregularGridController( Application& application )
262   : mApplication( application ),
263     mScrolling( false ),
264     mImagesLoaded( 0 )
265   {
266     std::cout << "ImageScalingIrregularGridController::ImageScalingIrregularGridController" << std::endl;
267
268     // Connect to the Application's Init signal
269     mApplication.InitSignal().Connect( this, &ImageScalingIrregularGridController::Create );
270   }
271
272   ~ImageScalingIrregularGridController()
273   {
274     // Nothing to do here.
275   }
276
277   /**
278    * Called everytime an ImageView has loaded it's image
279    */
280   void ResourceReadySignal( Toolkit::Control control )
281   {
282     mImagesLoaded++;
283     // To allow fast startup, we only place a small number of ImageViews on stage first
284     if ( mImagesLoaded == INITIAL_IMAGES_TO_LOAD )
285     {
286       // Adding the ImageViews to the stage will trigger loading of the Images
287       mGridActor.Add( mOffStageImageViews );
288     }
289   }
290
291
292   /**
293    * One-time setup in response to Application InitSignal.
294    */
295   void Create( Application& application )
296   {
297     std::cout << "ImageScalingIrregularGridController::Create" << std::endl;
298
299     // Get a handle to the stage:
300     Stage stage = Stage::GetCurrent();
301
302     // Connect to input event signals:
303     stage.KeyEventSignal().Connect(this, &ImageScalingIrregularGridController::OnKeyEvent);
304
305     // Hide the indicator bar
306     mApplication.GetWindow().ShowIndicator(Dali::Window::INVISIBLE);
307
308     // Create a default view with a default tool bar:
309     mContentLayer = DemoHelper::CreateView( mApplication,
310                                             mView,
311                                             mToolBar,
312                                             BACKGROUND_IMAGE,
313                                             TOOLBAR_IMAGE,
314                                             "" );
315
316     // Create an image scaling toggle button. (right of toolbar)
317     Toolkit::PushButton toggleScalingButton = Toolkit::PushButton::New();
318     toggleScalingButton.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE );
319     toggleScalingButton.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, TOGGLE_SCALING_IMAGE_SELECTED );
320     toggleScalingButton.ClickedSignal().Connect( this, &ImageScalingIrregularGridController::OnToggleScalingTouched );
321     mToolBar.AddControl( toggleScalingButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING  );
322
323     SetTitle( APPLICATION_TITLE );
324
325     mOffStageImageViews = Actor::New();
326     mOffStageImageViews.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
327     mOffStageImageViews.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
328     mOffStageImageViews.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
329
330     // Build the main content of the widow:
331     PopulateContentLayer( DEFAULT_SCALING_MODE );
332   }
333
334   /**
335    * Build the main part of the application's view.
336    */
337   void PopulateContentLayer( const Dali::FittingMode::Type fittingMode )
338   {
339     Stage stage = Stage::GetCurrent();
340     Vector2 stageSize = stage.GetSize();
341
342     float fieldHeight;
343     Actor imageField = BuildImageField( stageSize.x, GRID_WIDTH, GRID_MAX_HEIGHT, fittingMode, fieldHeight );
344
345     mScrollView = ScrollView::New();
346
347     mScrollView.ScrollStartedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollStarted );
348     mScrollView.ScrollCompletedSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollCompleted );
349
350     mScrollView.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::CENTER);
351     mScrollView.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::CENTER);
352
353     mScrollView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
354
355     mScrollView.SetAxisAutoLock( true );
356     mScrollView.SetAxisAutoLockGradient( 1.0f );
357
358     // Restrict scrolling to mostly vertical only, but with some horizontal wiggle-room:
359
360     RulerPtr rulerX = new FixedRuler( stageSize.width ); //< Pull the view back to the grid's centre-line when touch is release using a snapping ruler.
361     rulerX->SetDomain( RulerDomain( stageSize.width * -0.125f, stageSize.width * 1.125f ) ); //< Scroll slightly left/right of image field.
362     mScrollView.SetRulerX ( rulerX );
363
364     RulerPtr rulerY = new DefaultRuler(); //< Snap in multiples of a screen / stage height
365     rulerY->SetDomain( RulerDomain( - fieldHeight * 0.5f + stageSize.height * 0.5f - GRID_CELL_PADDING, fieldHeight * 0.5f + stageSize.height * 0.5f + GRID_CELL_PADDING ) );
366     mScrollView.SetRulerY ( rulerY );
367
368     mContentLayer.Add( mScrollView );
369     mScrollView.Add( imageField );
370     mGridActor = imageField;
371
372     // Create the scroll bar
373     mScrollBarVertical = ScrollBar::New(Toolkit::ScrollBar::Vertical);
374     mScrollBarVertical.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_RIGHT);
375     mScrollBarVertical.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_RIGHT);
376     mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FILL_TO_PARENT, Dali::Dimension::HEIGHT);
377     mScrollBarVertical.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH);
378     mScrollView.Add(mScrollBarVertical);
379
380     mScrollBarHorizontal = ScrollBar::New(Toolkit::ScrollBar::Horizontal);
381     mScrollBarHorizontal.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::BOTTOM_LEFT);
382     mScrollBarHorizontal.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
383     mScrollBarHorizontal.SetResizePolicy(Dali::ResizePolicy::FIT_TO_CHILDREN, Dali::Dimension::WIDTH);
384     mScrollBarHorizontal.SetProperty( Actor::Property::ORIENTATION, Quaternion( Quaternion( Radian( 1.5f * Math::PI ), Vector3::ZAXIS) ) );
385     mScrollView.Add(mScrollBarHorizontal);
386
387     mScrollView.OnRelayoutSignal().Connect( this, &ImageScalingIrregularGridController::OnScrollViewRelayout );
388
389     // Scroll to top of grid so first images loaded are on-screen:
390     mScrollView.ScrollTo( Vector2( 0, -1000000 ) );
391   }
392
393   void OnScrollViewRelayout(Actor actor)
394   {
395     // Make the height of the horizontal scroll bar to be the same as the width of scroll view.
396     mScrollBarHorizontal.SetProperty( Actor::Property::SIZE, Vector2(0.0f, mScrollView.GetRelayoutSize( Dimension::WIDTH) ));
397   }
398
399   /**
400    * Build a field of images scaled into a variety of shapes from very wide,
401    * through square, to very tall. The images are direct children of the Dali::Actor
402    * returned.
403    **/
404   Actor BuildImageField( const float fieldWidth,
405                            const unsigned gridWidth,
406                            const unsigned maxGridHeight,
407                            Dali::FittingMode::Type fittingMode,
408                            float & outFieldHeight )
409   {
410     // Generate the list of image configurations to be fitted into the field:
411
412     std::vector<ImageConfiguration> configurations;
413     configurations.reserve( NUM_IMAGE_PATHS * NUM_IMAGE_SIZES );
414     for( unsigned imageIndex = 0; imageIndex < NUM_IMAGE_PATHS; ++imageIndex )
415     {
416       for( unsigned dimensionsIndex = 0; dimensionsIndex < NUM_IMAGE_SIZES; ++ dimensionsIndex )
417       {
418         configurations.push_back( ImageConfiguration( IMAGE_PATHS[imageIndex], IMAGE_SIZES[dimensionsIndex] ) );
419       }
420     }
421     // Stir-up the list to get some nice irregularity in the generated field:
422     std::random_shuffle( configurations.begin(), configurations.end() );
423     std::random_shuffle( configurations.begin(), configurations.end() );
424
425     // Place the images in the grid:
426
427     std::vector<ImageConfiguration>::iterator config, end;
428     GridFlags grid( gridWidth, maxGridHeight );
429     std::vector<PositionedImage> placedImages;
430
431     for( config = configurations.begin(), end = configurations.end(); config != end; ++config )
432     {
433       unsigned cellX, cellY;
434       Vector2 imageGridDims;
435
436       // Allocate a region of the grid for the image:
437       bool allocated = grid.AllocateRegion( config->dimensions, cellX, cellY, imageGridDims );
438       if( !allocated )
439       {
440 #ifdef DEBUG_PRINT_DIAGNOSTICS
441           fprintf( stderr, "Failed to allocate image in grid with dims (%f, %f) and path: %s.\n", config->dimensions.x, config->dimensions.y, config->path );
442 #endif
443         continue;
444       }
445
446       placedImages.push_back( PositionedImage( *config, cellX, cellY, imageGridDims ) );
447     }
448     DALI_ASSERT_DEBUG( grid.DebugCheckGridValid() && "Cells were set more than once, indicating erroneous overlap in placing images on the grid." );
449     const unsigned actualGridHeight = grid.GetHighestUsedRow() + 1;
450
451     // Take the images images in the grid and turn their logical locations into
452     // coordinates in a frame defined by a parent actor:
453
454     Actor gridActor = Actor::New();
455     gridActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
456     gridActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
457     gridActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
458
459     // Work out the constants of the grid and cell dimensions and positions:
460     const float cellWidth = fieldWidth / gridWidth;
461     const float cellHeight = cellWidth / CELL_ASPECT_RATIO;
462     const Vector2 cellSize = Vector2( cellWidth, cellHeight );
463     outFieldHeight = actualGridHeight * cellHeight;
464     const Vector2 gridOrigin = Vector2( -fieldWidth * 0.5f, -outFieldHeight * 0.5 );
465
466      unsigned int count = 0;
467     // Build the image actors in their right locations in their parent's frame:
468     for( std::vector<PositionedImage>::const_iterator i = placedImages.begin(), end = placedImages.end(); i != end; ++i, ++count )
469     {
470       const PositionedImage& imageSource = *i;
471       const Vector2 imageSize = imageSource.imageGridDims * cellSize - Vector2( GRID_CELL_PADDING * 2, GRID_CELL_PADDING * 2 );
472       const Vector2 imageRegionCorner = gridOrigin + cellSize * Vector2( imageSource.cellX, imageSource.cellY );
473       const Vector2 imagePosition = imageRegionCorner + Vector2( GRID_CELL_PADDING , GRID_CELL_PADDING ) + imageSize * 0.5f;
474
475       ImageView image = CreateImageView( imageSource.configuration.path, imageSize.x, imageSize.y, fittingMode );
476       image.SetProperty( Actor::Property::POSITION, Vector3( imagePosition.x, imagePosition.y, 0 ) );
477       image.SetProperty( Actor::Property::SIZE, imageSize );
478       image.TouchSignal().Connect( this, &ImageScalingIrregularGridController::OnTouchImage );
479       image.ResourceReadySignal().Connect( this, &ImageScalingIrregularGridController::ResourceReadySignal );
480       mFittingModes[image.GetId()] = fittingMode;
481       mResourceUrls[image.GetId()] = imageSource.configuration.path;
482       mSizes[image.GetId()] = imageSize;
483       if ( count < INITIAL_IMAGES_TO_LOAD )
484       {
485         gridActor.Add( image );
486       }
487       else
488       {
489         // Store the ImageView in an offstage actor until the inital batch of ImageViews have finished loading their images
490         // Required
491         mOffStageImageViews.Add( image );
492       }
493     }
494
495     return gridActor;
496   }
497
498  /**
499   * Upon Touching an image (Release), change its scaling mode and make it spin, provided we're not scrolling.
500   * @param[in] actor The actor touched
501   * @param[in] event The Touch information.
502   */
503   bool OnTouchImage( Actor actor, const TouchData& event )
504   {
505     if( ( event.GetPointCount() > 0 ) && ( !mScrolling ) )
506     {
507       if( event.GetState( 0 ) == PointState::UP )
508       {
509         // Spin the image a few times:
510         Animation animation = Animation::New(SPIN_DURATION);
511         animation.AnimateBy( Property( actor, Actor::Property::ORIENTATION ), Quaternion( Radian( Degree(360.0f * SPIN_DURATION) ), Vector3::XAXIS ), AlphaFunction::EASE_OUT );
512         animation.Play();
513
514         // Change the scaling mode:
515         const unsigned id = actor.GetId();
516         Dali::FittingMode::Type newMode = NextMode( mFittingModes[id] );
517         const Vector2 imageSize = mSizes[actor.GetId()];
518
519         ImageView imageView = ImageView::DownCast( actor );
520         if( imageView)
521         {
522           Property::Map map;
523           map[Visual::Property::TYPE] = Visual::IMAGE;
524           map[ImageVisual::Property::URL] = mResourceUrls[id];
525           map[ImageVisual::Property::DESIRED_WIDTH] = imageSize.width + 0.5f;
526           map[ImageVisual::Property::DESIRED_HEIGHT] =  imageSize.height + 0.5f;
527           map[ImageVisual::Property::FITTING_MODE] = newMode;
528           imageView.SetProperty( ImageView::Property::IMAGE, map );
529         }
530
531         mFittingModes[id] = newMode;
532       }
533     }
534     return false;
535   }
536
537  /**
538   * Main key event handler.
539   * Quit on escape key.
540   */
541   void OnKeyEvent(const KeyEvent& event)
542   {
543     if( event.state == KeyEvent::Down )
544     {
545       if( IsKey( event, Dali::DALI_KEY_ESCAPE )
546           || IsKey( event, Dali::DALI_KEY_BACK ) )
547       {
548         mApplication.Quit();
549       }
550     }
551   }
552
553  /**
554   * Signal handler, called when the 'Scaling' button has been touched.
555   *
556   * @param[in] button The button that was pressed.
557   */
558   bool OnToggleScalingTouched( Button button )
559   {
560     const unsigned numChildren = mGridActor.GetChildCount();
561
562     for( unsigned i = 0; i < numChildren; ++i )
563     {
564       ImageView gridImageView = ImageView::DownCast( mGridActor.GetChildAt( i ) );
565       if( gridImageView )
566       {
567         // Cycle the scaling mode options:
568         unsigned int id = gridImageView.GetId();
569
570         const Vector2 imageSize = mSizes[ id ];
571         Dali::FittingMode::Type newMode = NextMode( mFittingModes[ id ] );
572
573         Property::Map map;
574         map[Visual::Property::TYPE] = Visual::IMAGE;
575         map[ImageVisual::Property::URL] = mResourceUrls[id];
576         map[ImageVisual::Property::DESIRED_WIDTH] = imageSize.width;
577         map[ImageVisual::Property::DESIRED_HEIGHT] =  imageSize.height;
578         map[ImageVisual::Property::FITTING_MODE] = newMode;
579         gridImageView.SetProperty( ImageView::Property::IMAGE, map );
580
581
582
583         mFittingModes[ id ] = newMode;
584
585         SetTitle( std::string( newMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : newMode == FittingMode::SCALE_TO_FILL ?  "SCALE_TO_FILL" : newMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : "FIT_HEIGHT" ) );
586       }
587     }
588     return true;
589   }
590
591   /**
592    * Sets/Updates the title of the View
593    * @param[in] title The new title for the view.
594    */
595   void SetTitle(const std::string& title)
596   {
597     if(!mTitleActor)
598     {
599       mTitleActor = DemoHelper::CreateToolBarLabel( "" );
600       // Add title to the tool bar.
601       mToolBar.AddControl( mTitleActor, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarTitlePercentage, Alignment::HorizontalCenter );
602     }
603
604     mTitleActor.SetProperty( TextLabel::Property::TEXT, title );
605   }
606
607   /**
608    * When scroll starts (i.e. user starts to drag scrollview),
609    * note this state (mScrolling = true)
610    * @param[in] position Current Scroll Position
611    */
612   void OnScrollStarted( const Vector2& position )
613   {
614     mScrolling = true;
615   }
616
617   /**
618    * When scroll starts (i.e. user stops dragging scrollview, and scrollview has snapped to destination),
619    * note this state (mScrolling = false).
620    * @param[in] position Current Scroll Position
621    */
622   void OnScrollCompleted( const Vector2& position )
623   {
624     mScrolling = false;
625   }
626
627 private:
628   Application&  mApplication;
629
630   Layer mContentLayer;                ///< The content layer (contains non gui chrome actors)
631   Toolkit::Control mView;             ///< The View instance.
632   Toolkit::ToolBar mToolBar;          ///< The View's Toolbar.
633   TextLabel mTitleActor;               ///< The Toolbar's Title.
634   Actor mGridActor;                   ///< The container for the grid of images
635   Actor mOffStageImageViews;          ///< ImageViews held off stage until the inital batch have loaded their images
636   ScrollView mScrollView;             ///< ScrollView UI Component
637   ScrollBar mScrollBarVertical;
638   ScrollBar mScrollBarHorizontal;
639   bool mScrolling;                    ///< ScrollView scrolling state (true = scrolling, false = stationary)
640   std::map<unsigned, Dali::FittingMode::Type> mFittingModes; ///< Stores the current scaling mode of each image, keyed by image actor id.
641   std::map<unsigned, std::string> mResourceUrls; ///< Stores the url of each image, keyed by image actor id.
642   std::map<unsigned, Vector2> mSizes; ///< Stores the current size of each image, keyed by image actor id.
643   unsigned int mImagesLoaded;         ///< How many images have been loaded
644 };
645
646 int DALI_EXPORT_API main( int argc, char **argv )
647 {
648   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
649   ImageScalingIrregularGridController test( application );
650   application.MainLoop();
651   return 0;
652 }