Merge "Simple TextEditor demo." into devel/master
[platform/core/uifw/dali-demo.git] / examples / image-scaling-and-filtering / image-scaling-and-filtering-example.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 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/popup/popup.h>
21 #include "shared/view.h"
22 #include <iostream>
23
24 using namespace Dali;
25 using Toolkit::TextLabel;
26
27 namespace
28 {
29
30 const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
32
33 const char* BORDER_IMAGE( DEMO_IMAGE_DIR "border-4px.9.png" );
34 const int BORDER_WIDTH = ( 11.0f + 4.0f ); // Shadow size = 11, border size = 4.
35 const char* RESIZE_HANDLE_IMAGE( DEMO_IMAGE_DIR "resize-handle.png" );
36
37 const int MARGIN_SIZE = 10;
38
39 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
40 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
41 const char * const DALI_ICON_PLAY = DEMO_IMAGE_DIR "icon-play.png";
42
43 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
44 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
45 const char* const FITTING_BUTTON_TEXT = "Fitting";
46 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
47
48 const char* const STYLE_LABEL_TEXT  = "grouplabel";
49 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
50
51
52 const char* IMAGE_PATHS[] =
53 {
54   // Variety of sizes, shapes and formats:
55   DEMO_IMAGE_DIR "dali-logo.png",
56   DEMO_IMAGE_DIR "layer1.png",
57   DEMO_IMAGE_DIR "layer2.png",
58   DEMO_IMAGE_DIR "animation-list.png",
59   DEMO_IMAGE_DIR "music-libray-main-screen.png",
60   DEMO_IMAGE_DIR "music-libray-record-cover.png",
61   DEMO_IMAGE_DIR "contacts-background.png",
62   DEMO_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
63   DEMO_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
64   DEMO_IMAGE_DIR "square_primitive_shapes.bmp",
65   DEMO_IMAGE_DIR "gallery-large-14.jpg",
66   DEMO_IMAGE_DIR "book-landscape-cover.jpg",
67   DEMO_IMAGE_DIR "book-portrait-p1.jpg",
68   DEMO_IMAGE_DIR "book-landscape-cover-back.jpg",
69
70   // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
71   // black and white image:
72   DEMO_IMAGE_DIR "gallery-large-14.wbmp",
73
74   DEMO_IMAGE_DIR "background-1.jpg",
75   DEMO_IMAGE_DIR "background-blocks.jpg",
76   DEMO_IMAGE_DIR "background-magnifier.jpg",
77   DEMO_IMAGE_DIR "gallery-large-14.jpg",
78   NULL
79 };
80 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
81
82 /** Cycle the scaling mode options. */
83 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
84 {
85   FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
86   switch ( oldMode )
87   {
88     case FittingMode::SHRINK_TO_FIT:
89       newMode = FittingMode::SCALE_TO_FILL;
90       break;
91     case FittingMode::SCALE_TO_FILL:
92       newMode = FittingMode::FIT_WIDTH;
93       break;
94     case FittingMode::FIT_WIDTH:
95       newMode = FittingMode::FIT_HEIGHT;
96       break;
97     case FittingMode::FIT_HEIGHT:
98       newMode = FittingMode::SHRINK_TO_FIT;
99       break;
100   }
101   return newMode;
102 }
103
104 /** Cycle through filter mode options. */
105 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
106 {
107   SamplingMode::Type newMode = SamplingMode::BOX;
108
109   switch ( oldMode )
110   {
111     case SamplingMode::BOX:
112       newMode = SamplingMode::NEAREST;
113       break;
114     case SamplingMode::NEAREST:
115       newMode = SamplingMode::LINEAR;
116       break;
117     case SamplingMode::LINEAR:
118       newMode = SamplingMode::BOX_THEN_NEAREST;
119       break;
120     case SamplingMode::BOX_THEN_NEAREST:
121       newMode = SamplingMode::BOX_THEN_LINEAR;
122       break;
123     case SamplingMode::BOX_THEN_LINEAR:
124       newMode = SamplingMode::NO_FILTER;
125       break;
126     case SamplingMode::NO_FILTER:
127       newMode = SamplingMode::BOX;
128       break;
129     case SamplingMode::DONT_CARE:
130       newMode = SamplingMode::BOX;
131       break;
132   }
133   return newMode;
134 }
135
136 const char* StringFromScalingMode( FittingMode::Type scalingMode )
137 {
138   return scalingMode == FittingMode::SCALE_TO_FILL ? "SCALE_TO_FILL" : scalingMode == FittingMode::SHRINK_TO_FIT ? "SHRINK_TO_FIT" : scalingMode == FittingMode::FIT_WIDTH ? "FIT_WIDTH" : scalingMode == FittingMode::FIT_HEIGHT ? "FIT_HEIGHT" : "UnknownScalingMode";
139 }
140
141 const char* StringFromFilterMode( SamplingMode::Type filterMode )
142 {
143   return filterMode == SamplingMode::BOX ? "BOX" : filterMode == SamplingMode::BOX_THEN_NEAREST ? "BOX_THEN_NEAREST" : filterMode == SamplingMode::BOX_THEN_LINEAR ? "BOX_THEN_LINEAR" : filterMode == SamplingMode::NEAREST ? "NEAREST" : filterMode == SamplingMode::LINEAR ? "LINEAR" : filterMode == SamplingMode::NO_FILTER ? "NO_FILTER" : filterMode == SamplingMode::DONT_CARE ? "DONT_CARE" : "UnknownFilterMode";
144 }
145
146 }
147
148 // This example shows the load-time image scaling and filtering features.
149 //
150 class ImageScalingAndFilteringController : public ConnectionTracker
151 {
152 public:
153
154   ImageScalingAndFilteringController( Application& application )
155   : mApplication( application ),
156     mImageStageScale( 0.5f, 0.5f ),
157     mCurrentPath( 0 ),
158     mFittingMode( FittingMode::FIT_WIDTH ),
159     mSamplingMode( SamplingMode::BOX_THEN_LINEAR),
160     mImageLoading( false ),
161     mQueuedImageLoad( false )
162   {
163     // Connect to the Application's Init signal
164     mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
165   }
166
167   ~ImageScalingAndFilteringController()
168   {
169     // Nothing to do here;
170   }
171
172   // The Init signal is received once (only) during the Application lifetime
173   void Create( Application& application )
174   {
175     // Get a handle to the stage
176     Stage stage = Stage::GetCurrent();
177
178     // Background image:
179     Dali::Property::Map backgroundImage;
180     backgroundImage.Insert( "rendererType",  "image" );
181     backgroundImage.Insert( "url",  BACKGROUND_IMAGE );
182     backgroundImage.Insert( "desiredWidth",   stage.GetSize().width );
183     backgroundImage.Insert( "desiredHeight",   stage.GetSize().height );
184     backgroundImage.Insert( "fittingMode",   "SCALE_TO_FILL" );
185     backgroundImage.Insert( "samplingMode",   "BOX_THEN_NEAREST" );
186
187     Toolkit::ImageView background = Toolkit::ImageView::New();
188     background.SetProperty( Toolkit::ImageView::Property::IMAGE, backgroundImage );
189     background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
190     background.SetSize( stage.GetSize() );
191     stage.Add( background );
192
193     BufferImage heightBackground = BufferImage::WHITE();
194     PixelBuffer* const heightPixel = heightBackground.GetBuffer();
195     heightPixel[0] = 0x8f;
196     heightPixel[1] = 0x8f;
197     heightPixel[2] = 0x8f;
198
199     BufferImage widthBackground = BufferImage::WHITE();
200     PixelBuffer* const widthPixel = widthBackground.GetBuffer();
201     widthPixel[0] = 0x4f;
202     widthPixel[1] = 0x4f;
203     widthPixel[2] = 0x4f;
204
205     mHeightBox = Toolkit::ImageView::New( heightBackground );
206     mHeightBox.SetOpacity( 0.2f );
207     background.Add( mHeightBox );
208
209     mWidthBox = Toolkit::ImageView::New( widthBackground );
210     mWidthBox.SetOpacity( 0.2f );
211     background.Add( mWidthBox );
212
213     mDesiredBox = Toolkit::ImageView::New( BORDER_IMAGE );
214     background.Add( mDesiredBox );
215
216     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
217     mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
218     mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
219
220     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
221     mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
222     mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
223
224     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
225     mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
226     mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
227
228     // Initialize the actor
229     mImageView = Toolkit::ImageView::New( IMAGE_PATHS[ 0 ] );
230
231     // Reposition the actor
232     mImageView.SetParentOrigin( ParentOrigin::CENTER );
233     mImageView.SetAnchorPoint( AnchorPoint::CENTER );
234
235     // Display the actor on the stage
236     mDesiredBox.Add( mImageView );
237
238     mImageView.SetSize( stage.GetSize() * mImageStageScale );
239
240     // Setup the pinch detector for scaling the desired image load dimensions:
241     mPinchDetector = PinchGestureDetector::New();
242     mPinchDetector.Attach( mImageView );
243     mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
244
245     mGrabCorner = Toolkit::ImageView::New( RESIZE_HANDLE_IMAGE );
246     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
247     mGrabCorner.SetName( "GrabCorner" );
248     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
249     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
250     mGrabCorner.SetPosition( -BORDER_WIDTH, -BORDER_WIDTH );
251     mGrabCorner.SetOpacity( 0.6f );
252
253     Layer grabCornerLayer = Layer::New();
254     grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
255     grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
256     grabCornerLayer.Add( mGrabCorner );
257     mDesiredBox.Add( grabCornerLayer );
258
259     mPanGestureDetector = PanGestureDetector::New();
260     mPanGestureDetector.Attach( mGrabCorner );
261     mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
262
263     // Tie-in input event handlers:
264     stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
265
266     CreateControls();
267
268     ResizeImage();
269   }
270
271   /**
272    * Create the GUI controls which float above the scene
273    */
274   void CreateControls()
275   {
276     Stage stage = Stage::GetCurrent();
277
278     Dali::Layer controlsLayer = Dali::Layer::New();
279     controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
280     controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
281     controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
282     controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
283     stage.Add( controlsLayer );
284
285     // Back and next image buttons in corners of stage:
286     unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
287     Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
288     Toolkit::ImageView imagePrevious = Toolkit::ImageView::New( playImage );
289
290     // Last image button:
291     imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
292     imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
293     imagePrevious.SetY( playWidth * 0.5f );
294     imagePrevious.SetX( playWidth + playWidth * 0.5f );
295     imagePrevious.SetOpacity( 0.6f );
296     controlsLayer.Add( imagePrevious );
297     imagePrevious.SetName( PREVIOUS_BUTTON_ID );
298     imagePrevious.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
299
300     // Next image button:
301     Toolkit::ImageView imageNext = Toolkit::ImageView::New( playImage );
302     imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
303     imageNext.SetY( playWidth * 0.5f );
304     imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
305     imageNext.SetOpacity( 0.6f );
306     controlsLayer.Add( imageNext );
307     imageNext.SetName( NEXT_BUTTON_ID );
308     imageNext.TouchSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
309
310     // Buttons to popup selectors for fitting and sampling modes:
311
312     // Wrapper table to hold two buttons side by side:
313     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
314     modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
315     modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
316     modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
317     modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
318     modesGroupBackground.SetFitHeight( 0 );
319
320     modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
321     modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
322     modesGroupBackground.SetPosition( 0.0f, 0.0f );
323
324     controlsLayer.Add( modesGroupBackground );
325
326     {
327       // Vertical table to hold label and button:
328       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
329       fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
330       fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
331       fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
332       fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
333       fittingModeGroup.SetFitHeight( 0 );
334       fittingModeGroup.SetFitHeight( 1 );
335
336       TextLabel label = TextLabel::New( "Image fitting mode:" );
337       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
338       fittingModeGroup.Add( label );
339
340       Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
341       fittingModeGroup.Add( button );
342       mFittingModeButton = button;
343
344       modesGroupBackground.Add( fittingModeGroup );
345     }
346
347     {
348       // Vertical table to hold label and button:
349       Toolkit::TableView samplingModeGroup = Toolkit::TableView::New( 2, 1 );
350       samplingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
351       samplingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
352       samplingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
353       samplingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
354       samplingModeGroup.SetFitHeight( 0 );
355       samplingModeGroup.SetFitHeight( 1 );
356
357       TextLabel label = TextLabel::New( "Image sampling mode:" );
358       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
359       samplingModeGroup.Add( label );
360
361       Toolkit::PushButton button = CreateButton( SAMPLING_BUTTON_ID, StringFromFilterMode( mSamplingMode ) );
362       samplingModeGroup.Add( button );
363       mSamplingModeButton = button;
364
365       modesGroupBackground.Add( samplingModeGroup );
366     }
367   }
368
369   Toolkit::PushButton CreateButton( const char * id, const char * label )
370   {
371     Toolkit::PushButton button = Toolkit::PushButton::New();
372     button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
373     button.SetName( id );
374     button.SetLabelText( label );
375     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
376     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
377     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
378     return button;
379   }
380
381   Toolkit::Popup CreatePopup()
382   {
383     Stage stage = Stage::GetCurrent();
384     const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
385
386     Toolkit::Popup popup = Toolkit::Popup::New();
387     popup.SetName( "POPUP" );
388     popup.SetParentOrigin( ParentOrigin::CENTER );
389     popup.SetAnchorPoint( AnchorPoint::CENTER );
390     popup.SetSize( POPUP_WIDTH_DP, 0.0f );
391
392     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
393
394     return popup;
395   }
396
397   Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
398   {
399     Toolkit::PushButton button = Toolkit::PushButton::New();
400     button.SetName( id );
401     button.SetLabelText( id );
402
403     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
404     button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
405     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
406     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
407
408     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
409
410     parent.Add( button );
411     return button;
412   }
413
414   bool OnButtonClicked( Toolkit::Button button )
415   {
416     if( button.GetName() == FITTING_BUTTON_ID )
417     {
418       mPopup = CreatePopup();
419
420       // Four-row table to hold buttons:
421       Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
422       fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
423       fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
424       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
425       fittingModes.SetFitHeight( 0 );
426       fittingModes.SetFitHeight( 1 );
427       fittingModes.SetFitHeight( 2 );
428       fittingModes.SetFitHeight( 3 );
429
430       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
431       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
432       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
433       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
434
435       mPopup.SetContent( fittingModes );
436       Stage::GetCurrent().Add( mPopup );
437       mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
438     }
439     else if( button.GetName() == SAMPLING_BUTTON_ID )
440     {
441       mPopup = CreatePopup();
442
443       // Table to hold buttons for each sampling mode:
444       Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
445       samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
446       samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
447       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
448       samplingModes.SetFitHeight( 0 );
449       samplingModes.SetFitHeight( 1 );
450       samplingModes.SetFitHeight( 2 );
451       samplingModes.SetFitHeight( 3 );
452       samplingModes.SetFitHeight( 4 );
453       samplingModes.SetFitHeight( 5 );
454
455       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
456       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
457       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
458       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
459       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
460       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
461
462       mPopup.SetContent( samplingModes );
463       Stage::GetCurrent().Add( mPopup );
464       mPopup.SetDisplayState( Toolkit::Popup::SHOWN );
465     }
466     else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
467              CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
468              CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
469              CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
470     {
471     }
472     else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
473              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
474              CheckSamplingModeButton( button, SamplingMode::BOX ) ||
475              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
476              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
477              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
478              CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
479     {
480     }
481     return true;
482   }
483
484   bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
485   {
486     const char * const modeName = StringFromScalingMode( mode );
487     if( button.GetName() == modeName )
488     {
489       mFittingMode = mode;
490       mFittingModeButton.SetLabelText( modeName );
491       ResizeImage();
492       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
493       mPopup.Reset();
494       return true;
495     }
496     return false;
497   }
498
499   bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
500   {
501     const char * const modeName = StringFromFilterMode( mode );
502     if( button.GetName() == modeName )
503     {
504       mSamplingMode = mode;
505       mSamplingModeButton.SetLabelText( modeName );
506       ResizeImage();
507       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
508       mPopup.Reset();
509       return true;
510     }
511     return false;
512   }
513
514   void OnPopupOutsideTouched()
515   {
516     if( mPopup )
517     {
518       mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
519       mPopup.Reset();
520     }
521   }
522
523   void OnImageLoaded( ResourceImage image )
524   {
525     DALI_ASSERT_DEBUG( image == mNextImage );
526     mImageView.SetImage( image );
527     mImageView.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
528     mImageLoading = false;
529
530     // We have finished loading, if a resize had occured during the load, trigger another load now.
531     if( mQueuedImageLoad )
532     {
533       mQueuedImageLoad = false;
534       LoadImage();
535     }
536   }
537
538   bool OnControlTouched( Actor actor, const TouchData& event )
539   {
540     if(event.GetPointCount() > 0)
541     {
542       switch( event.GetState( 0 ) )
543       {
544         case PointState::UP:
545         {
546           const std::string & name = actor.GetName();
547           if( name == NEXT_BUTTON_ID )
548           {
549             mCurrentPath = mCurrentPath + 1;
550             mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
551             ResizeImage();
552           }
553           else if( name == PREVIOUS_BUTTON_ID )
554           {
555             mCurrentPath = mCurrentPath - 1;
556             mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
557             ResizeImage();
558           }
559           break;
560         }
561         default:
562         {
563           break;
564         }
565       } // end switch
566     }
567
568     return false;
569   }
570
571   void OnPinch( Actor actor, const PinchGesture& pinch )
572   {
573     if( pinch.state == Gesture::Started )
574     {
575       mLastPinchScale = pinch.scale;
576     }
577     const float scale = pinch.scale;
578
579     if( ! Equals( scale, mLastPinchScale ) )
580     {
581       if ( scale < mLastPinchScale )
582       {
583         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
584         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
585       }
586       else
587       {
588         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
589         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
590       }
591       ResizeImage();
592     }
593     mLastPinchScale = scale;
594   }
595
596   void OnPan( Actor actor, const PanGesture& gesture )
597   {
598     Stage stage = Stage::GetCurrent();
599     // 1.0f and 0.75f are the maximum size caps of the resized image, as a factor of stage-size.
600     mImageStageScale.x = std::max( 0.05f, std::min( 0.95f,  mImageStageScale.x + ( gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
601     mImageStageScale.y = std::max( 0.05f, std::min( 0.70f, mImageStageScale.y + ( gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
602
603     ResizeImage();
604   }
605
606   void OnKeyEvent(const KeyEvent& event)
607   {
608     if( event.state == KeyEvent::Down )
609     {
610       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
611       {
612         if( mPopup && mPopup.IsVisible() )
613         {
614           mPopup.SetDisplayState( Toolkit::Popup::HIDDEN );
615           mPopup.Reset();
616         }
617         else
618         {
619           mApplication.Quit();
620         }
621       }
622       else if ( event.keyPressedName == "Right" )
623       {
624         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
625       }
626       else if ( event.keyPressedName == "Left" )
627       {
628         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
629       }
630       else if ( event.keyPressedName == "Up" )
631       {
632         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
633       }
634       else if ( event.keyPressedName == "Down" )
635       {
636         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
637       }
638       else if ( event.keyPressedName == "o" )
639       {
640         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
641         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
642       }
643       else if ( event.keyPressedName == "p" )
644       {
645         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
646         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
647       }
648       else if ( event.keyPressedName == "n" )
649       {
650         mCurrentPath = mCurrentPath + 1;
651         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
652       }
653       else if ( event.keyPressedName == "b" )
654       {
655         mCurrentPath = mCurrentPath - 1;
656         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
657       }
658       // Cycle filter and scaling modes:
659       else if ( event.keyPressedName == "f" )
660       {
661         mSamplingMode = NextFilterMode( mSamplingMode );
662         mSamplingModeButton.SetLabelText( StringFromFilterMode( mSamplingMode ) );
663       }
664       // Cycle filter and scaling modes:
665       else if ( event.keyPressedName == "s" )
666       {
667         mFittingMode = NextScalingMode( mFittingMode );
668         mFittingModeButton.SetLabelText( StringFromScalingMode( mFittingMode ) );
669       }
670       else
671       {
672         return;
673       }
674
675       ResizeImage();
676     }
677   }
678
679 private:
680
681   void LoadImage()
682   {
683     mImageLoading = true;
684
685     const char * const path = IMAGE_PATHS[ mCurrentPath ];
686     Stage stage = Stage::GetCurrent();
687     Size imageSize = stage.GetSize() * mImageStageScale;
688     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
689
690     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
691
692     // If the image was cached, the load has already occured, bypass hooking the signal.
693     if( image.GetLoadingState() )
694     {
695       OnImageLoaded( image );
696     }
697     else
698     {
699       image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
700     }
701
702     mNextImage = image;
703   }
704
705   void ResizeImage()
706   {
707
708     Stage stage = Stage::GetCurrent();
709     Size imageSize = stage.GetSize() * mImageStageScale;
710
711     // If an image is already loading, queue another load when it has finished.
712     // This way we get continuous updates instead of constantly re-requesting loads.
713     if( mImageLoading )
714     {
715       mQueuedImageLoad = true;
716     }
717     else
718     {
719       LoadImage();
720     }
721
722     // Border size needs to be modified to take into account the width of the frame.
723     Vector2 borderScale( ( imageSize + Vector2( BORDER_WIDTH * 2.0f, BORDER_WIDTH * 2.0f ) ) / stage.GetSize() );
724     mDesiredBox.SetSize( stage.GetSize() * borderScale );
725
726     mHeightBox.SetSize( stage.GetSize().width, (stage.GetSize() * mImageStageScale).height );
727     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
728   }
729
730 private:
731   Application&  mApplication;
732   Toolkit::ImageView mDesiredBox; //< Background rectangle to show requested image size.
733   Toolkit::ImageView mHeightBox;  //< Background horizontal stripe to show requested image height.
734   Toolkit::ImageView mWidthBox;   //< Background vertical stripe to show requested image width.
735   Toolkit::PushButton mFittingModeButton;
736   Toolkit::PushButton mSamplingModeButton;
737   Toolkit::Popup mPopup;
738   PinchGestureDetector mPinchDetector;
739   float mLastPinchScale;
740   Toolkit::ImageView mGrabCorner;
741   PanGestureDetector mPanGestureDetector;
742   Toolkit::ImageView mImageView;
743   ResourceImage mNextImage; //< Currently-loading image
744   Vector2 mImageStageScale;
745   int mCurrentPath;
746   FittingMode::Type mFittingMode;
747   SamplingMode::Type mSamplingMode;
748   bool mImageLoading;
749   bool mQueuedImageLoad;
750
751 };
752
753 void RunTest( Application& application )
754 {
755   ImageScalingAndFilteringController test( application );
756
757   application.MainLoop();
758 }
759
760 // Entry point for Linux & Tizen applications
761 int DALI_EXPORT_API main( int argc, char **argv )
762 {
763   Application application = Application::New( &argc, &argv, DEMO_THEME_PATH );
764
765   RunTest( application );
766
767   return 0;
768 }