[dali_1.0.48] Merge branch '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( DALI_IMAGE_DIR "background-gradient.jpg" );
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
32
33 const int MARGIN_SIZE = 10;
34
35 const char* const NEXT_BUTTON_ID = "NEXT_BUTTON";
36 const char* const PREVIOUS_BUTTON_ID = "PREVIOUS_BUTTON";
37 const char * const DALI_ICON_PLAY = DALI_IMAGE_DIR "icon-play.png";
38
39 const char* const FITTING_BUTTON_ID = "FITTING_BUTTON";
40 const char* const SAMPLING_BUTTON_ID = "SAMPLING_BUTTON";
41 const char* const FITTING_BUTTON_TEXT = "Fitting";
42 const char* const SAMPLING_BUTTON_TEXT = "Sampling";
43
44 const char* const STYLE_LABEL_TEXT  = "grouplabel";
45 const char* const STYLE_BUTTON_TEXT = "buttonlabel";
46
47
48
49 const char* IMAGE_PATHS[] =
50 {
51   // Worst case for aliasing in downscaling, 2k x 2k 1 bit per pixel dithered
52   // black and white image:
53   DALI_IMAGE_DIR "gallery-large-14.wbmp",
54   // Variety of sizes, shapes and formats:
55   DALI_IMAGE_DIR "animation-list.png",
56   DALI_IMAGE_DIR "layer1.png",
57   DALI_IMAGE_DIR "layer2.png",
58   DALI_IMAGE_DIR "music-libray-main-screen.png",
59   DALI_IMAGE_DIR "music-libray-record-cover.png",
60   DALI_IMAGE_DIR "contacts-background.png",
61   DALI_IMAGE_DIR "portrait_screen_primitive_shapes.gif",
62   DALI_IMAGE_DIR "landscape_screen_primitive_shapes.gif",
63   DALI_IMAGE_DIR "square_primitive_shapes.bmp",
64   DALI_IMAGE_DIR "dali-logo.png",
65   DALI_IMAGE_DIR "com.samsung.dali-demo.ico",
66   DALI_IMAGE_DIR "gallery-large-14.jpg",
67   DALI_IMAGE_DIR "book-landscape-cover.jpg",
68   DALI_IMAGE_DIR "book-portrait-p1.jpg",
69   DALI_IMAGE_DIR "book-landscape-cover-back.jpg",
70   DALI_IMAGE_DIR "background-1.jpg",
71   DALI_IMAGE_DIR "background-blocks.jpg",
72   DALI_IMAGE_DIR "background-magnifier.jpg",
73   DALI_IMAGE_DIR "gallery-large-14.jpg",
74   NULL
75 };
76 const int NUM_IMAGE_PATHS = sizeof(IMAGE_PATHS) / sizeof(IMAGE_PATHS[0]) - 1u;
77
78 /** Cycle the scaling mode options. */
79 FittingMode::Type NextScalingMode( FittingMode::Type oldMode )
80 {
81   FittingMode::Type newMode = FittingMode::SHRINK_TO_FIT;
82   switch ( oldMode )
83   {
84     case FittingMode::SHRINK_TO_FIT:
85       newMode = FittingMode::SCALE_TO_FILL;
86       break;
87     case FittingMode::SCALE_TO_FILL:
88       newMode = FittingMode::FIT_WIDTH;
89       break;
90     case FittingMode::FIT_WIDTH:
91       newMode = FittingMode::FIT_HEIGHT;
92       break;
93     case FittingMode::FIT_HEIGHT:
94       newMode = FittingMode::SHRINK_TO_FIT;
95       break;
96   }
97   return newMode;
98 }
99
100 /** Cycle through filter mode options. */
101 SamplingMode::Type NextFilterMode( SamplingMode::Type oldMode )
102 {
103   SamplingMode::Type newMode = SamplingMode::BOX;
104
105   switch ( oldMode )
106   {
107     case SamplingMode::BOX:
108       newMode = SamplingMode::NEAREST;
109       break;
110     case SamplingMode::NEAREST:
111       newMode = SamplingMode::LINEAR;
112       break;
113     case SamplingMode::LINEAR:
114       newMode = SamplingMode::BOX_THEN_NEAREST;
115       break;
116     case SamplingMode::BOX_THEN_NEAREST:
117       newMode = SamplingMode::BOX_THEN_LINEAR;
118       break;
119     case SamplingMode::BOX_THEN_LINEAR:
120       newMode = SamplingMode::NO_FILTER;
121       break;
122     case SamplingMode::NO_FILTER:
123       newMode = SamplingMode::BOX;
124       break;
125     case SamplingMode::DONT_CARE:
126       newMode = SamplingMode::BOX;
127       break;
128   }
129   return newMode;
130 }
131
132 const char* StringFromScalingMode( FittingMode::Type scalingMode )
133 {
134   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";
135 }
136
137 const char* StringFromFilterMode( SamplingMode::Type filterMode )
138 {
139   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";
140 }
141
142 }
143
144 // This example shows the load-time image scaling and filtering features.
145 //
146 class ImageScalingAndFilteringController : public ConnectionTracker
147 {
148 public:
149
150   ImageScalingAndFilteringController( Application& application )
151   : mApplication( application ),
152     mImageStageScale( 0.5f, 0.5f ),
153     mCurrentPath( 0 ),
154     mFittingMode( FittingMode::SCALE_TO_FILL ),
155     mSamplingMode( SamplingMode::BOX_THEN_LINEAR)
156   {
157     // Connect to the Application's Init signal
158     mApplication.InitSignal().Connect( this, &ImageScalingAndFilteringController::Create );
159   }
160
161   ~ImageScalingAndFilteringController()
162   {
163     // Nothing to do here;
164   }
165
166   // The Init signal is received once (only) during the Application lifetime
167   void Create( Application& application )
168   {
169     // Get a handle to the stage
170     Stage stage = Stage::GetCurrent();
171
172     // Background image:
173     ResourceImage backgroundImage = ResourceImage::New( BACKGROUND_IMAGE, ImageDimensions( stage.GetSize().width, stage.GetSize().height ), FittingMode::SCALE_TO_FILL, SamplingMode::BOX_THEN_LINEAR );
174     ImageActor background = ImageActor::New( backgroundImage );
175     background.SetZ( -2.0f );
176     background.SetAnchorPoint( AnchorPoint::TOP_LEFT );
177     background.SetSize( stage.GetSize() );
178     stage.Add( background );
179
180     // Make grey pixels for the desired box, the desired height the desired width:
181     BufferImage desiredBackground = BufferImage::WHITE();
182
183     BufferImage heightBackground = BufferImage::WHITE();
184     PixelBuffer* const heightPixel = heightBackground.GetBuffer();
185     heightPixel[0] = 0x8f;
186     heightPixel[1] = 0x8f;
187     heightPixel[2] = 0x8f;
188
189     BufferImage widthBackground = BufferImage::WHITE();
190     PixelBuffer* const widthPixel = widthBackground.GetBuffer();
191     widthPixel[0] = 0x4f;
192     widthPixel[1] = 0x4f;
193     widthPixel[2] = 0x4f;
194
195     mHeightBox = ImageActor::New( heightBackground );
196     mHeightBox.SetOpacity( 0.2f );
197     stage.Add( mHeightBox );
198
199     mWidthBox = ImageActor::New( widthBackground );
200     mWidthBox.SetOpacity( 0.2f );
201     stage.Add( mWidthBox );
202
203     mDesiredBox = ImageActor::New( desiredBackground );
204     stage.Add( mDesiredBox );
205
206     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
207     mDesiredBox.SetParentOrigin( ParentOrigin::CENTER );
208     mDesiredBox.SetAnchorPoint( AnchorPoint::CENTER );
209     mDesiredBox.SetPosition( 0, 0, -1 );
210     mDesiredBox.SetSortModifier(4.f);
211
212     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
213     mHeightBox.SetParentOrigin( ParentOrigin::CENTER );
214     mHeightBox.SetAnchorPoint( AnchorPoint::CENTER );
215     mHeightBox.SetPosition( 0, 0, -1 );
216     mHeightBox.SetSortModifier(3.f);
217
218     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
219     mWidthBox.SetParentOrigin( ParentOrigin::CENTER );
220     mWidthBox.SetAnchorPoint( AnchorPoint::CENTER );
221     mWidthBox.SetPosition( 0, 0, -1 );
222     mWidthBox.SetSortModifier(2.f);
223
224     // Make a grab-handle for resizing the image:
225     mGrabCorner = Toolkit::PushButton::New();
226     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
227     mGrabCorner.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
228     mGrabCorner.SetName( "GrabCorner" );
229     mGrabCorner.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
230     mGrabCorner.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
231     mGrabCorner.SetSize( Vector2( stage.GetSize().width*0.08f, stage.GetSize().width*0.08f ) );
232     mGrabCorner.SetOpacity( 0.6f );
233
234     Layer grabCornerLayer = Layer::New();
235     grabCornerLayer.SetAnchorPoint( AnchorPoint::BOTTOM_RIGHT );
236     grabCornerLayer.SetParentOrigin( ParentOrigin::BOTTOM_RIGHT );
237
238     grabCornerLayer.Add( mGrabCorner );
239     mDesiredBox.Add( grabCornerLayer );
240     mPanGestureDetector = PanGestureDetector::New();
241     mPanGestureDetector.Attach( mGrabCorner );
242     mPanGestureDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPan );
243
244     // Initialize the actor
245     mImageActor = ImageActor::New();
246
247     // Reposition the actor
248     mImageActor.SetParentOrigin( ParentOrigin::CENTER );
249     mImageActor.SetAnchorPoint( AnchorPoint::CENTER );
250     mImageActor.SetSortModifier(5.f);
251
252     // Display the actor on the stage
253     stage.Add( mImageActor );
254
255     mImageActor.SetSize( stage.GetSize() * mImageStageScale );
256
257     // Setup the pinch detector for scaling the desired image load dimensions:
258     mPinchDetector = PinchGestureDetector::New();
259     mPinchDetector.Attach( mImageActor );
260     mPinchDetector.DetectedSignal().Connect( this, &ImageScalingAndFilteringController::OnPinch );
261
262     // Tie-in input event handlers:
263     stage.KeyEventSignal().Connect( this, &ImageScalingAndFilteringController::OnKeyEvent );
264
265     CreateControls();
266
267     ResizeImage();
268   }
269
270   /**
271    * Create the GUI controls which float above the scene
272    */
273   void CreateControls()
274   {
275     Stage stage = Stage::GetCurrent();
276
277     Dali::Layer controlsLayer = Dali::Layer::New();
278     controlsLayer.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::ALL_DIMENSIONS );
279     controlsLayer.SetSizeModeFactor( Vector3( 1.0f, 1.0f, 1.0f ) );
280     controlsLayer.SetAnchorPoint( AnchorPoint::TOP_LEFT);
281     controlsLayer.SetParentOrigin( ParentOrigin::TOP_LEFT);
282     stage.Add( controlsLayer );
283
284     // Back and next image buttons in corners of stage:
285     unsigned int playWidth = std::min( stage.GetSize().x * (1 / 5.0f), 58.0f );
286     Image playImage = ResourceImage::New( DALI_ICON_PLAY, ImageDimensions( playWidth, playWidth ), FittingMode::SHRINK_TO_FIT, SamplingMode::BOX_THEN_LINEAR );
287     Actor imagePrevious = ImageActor::New( playImage );
288
289     // Last image button:
290     imagePrevious.SetAnchorPoint( AnchorPoint::TOP_LEFT );
291     imagePrevious.RotateBy( Radian(3.14159265358979323846f), Vector3( 0, 1.0f, 0 ) );
292     imagePrevious.SetY( playWidth * 0.5f );
293     imagePrevious.SetX( playWidth + playWidth * 0.5f );
294     imagePrevious.SetOpacity( 0.6f );
295     controlsLayer.Add( imagePrevious );
296     imagePrevious.SetName( PREVIOUS_BUTTON_ID );
297     imagePrevious.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
298
299     // Next image button:
300     Actor imageNext = ImageActor::New( playImage );
301     imageNext.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
302     imageNext.SetY( playWidth * 0.5f );
303     imageNext.SetX( stage.GetSize().x - playWidth * 0.5f );
304     imageNext.SetOpacity( 0.6f );
305     controlsLayer.Add( imageNext );
306     imageNext.SetName( NEXT_BUTTON_ID );
307     imageNext.TouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnControlTouched );
308
309     // Buttons to popup selectors for fitting and sampling modes:
310
311     // Wrapper table to hold two buttons side by side:
312     Toolkit::TableView modesGroupBackground = Toolkit::TableView::New( 1, 2 );
313     modesGroupBackground.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
314     modesGroupBackground.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
315     modesGroupBackground.SetBackgroundColor( BACKGROUND_COLOUR );
316     modesGroupBackground.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE ) );
317     modesGroupBackground.SetFitHeight( 0 );
318
319     modesGroupBackground.SetAnchorPoint( AnchorPoint::BOTTOM_LEFT );
320     modesGroupBackground.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
321     modesGroupBackground.SetPosition( 0.0f, 0.0f );
322
323     controlsLayer.Add( modesGroupBackground );
324
325     {
326       // Vertical table to hold label and button:
327       Toolkit::TableView fittingModeGroup = Toolkit::TableView::New( 2, 1 );
328       fittingModeGroup.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
329       fittingModeGroup.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
330       fittingModeGroup.SetBackgroundColor( BACKGROUND_COLOUR );
331       fittingModeGroup.SetCellPadding( Size( MARGIN_SIZE * 0.5f, MARGIN_SIZE * 0.5f ) );
332       fittingModeGroup.SetFitHeight( 0 );
333       fittingModeGroup.SetFitHeight( 1 );
334
335       TextLabel label = TextLabel::New( "Image fitting mode:" );
336       label.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_LABEL_TEXT );
337       fittingModeGroup.Add( label );
338
339       Toolkit::PushButton button = CreateButton( FITTING_BUTTON_ID, StringFromScalingMode( mFittingMode ) );
340       button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
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       button.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
363       samplingModeGroup.Add( button );
364       mSamplingModeButton = button;
365
366       modesGroupBackground.Add( samplingModeGroup );
367     }
368   }
369
370   Toolkit::PushButton CreateButton( const char * id, const char * label )
371   {
372     Toolkit::PushButton button = Toolkit::PushButton::New();
373     button.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
374     button.SetName( id );
375     button.SetLabel( label );
376     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
377     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
378     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
379     return button;
380   }
381
382   Toolkit::Popup CreatePopup()
383   {
384     Stage stage = Stage::GetCurrent();
385     const float POPUP_WIDTH_DP = stage.GetSize().width * 0.75f;
386
387     Toolkit::Popup popup = Toolkit::Popup::New();
388     popup.SetName( "POPUP" );
389     popup.SetParentOrigin( ParentOrigin::CENTER );
390     popup.SetAnchorPoint( AnchorPoint::CENTER );
391     popup.SetSize( POPUP_WIDTH_DP, 0.0f );
392     popup.HideTail();
393
394     popup.OutsideTouchedSignal().Connect( this, &ImageScalingAndFilteringController::OnPopupOutsideTouched );
395
396     return popup;
397   }
398
399   //void CreatePopupButton( Toolkit::Popup popup, const char* id )
400   Toolkit::PushButton CreatePopupButton( Actor parent, const char* id )
401   {
402     Toolkit::PushButton button = Toolkit::PushButton::New();
403     button.SetName( id );
404     button.SetLabel( id );
405     Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( button.GetLabel() );
406     textLabel.SetProperty( TextLabel::Property::POINT_SIZE, 12.0f );
407
408     button.SetAnchorPoint( AnchorPoint::TOP_LEFT );
409     button.SetParentOrigin( ParentOrigin::BOTTOM_LEFT );
410     button.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
411     button.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
412
413     button.ClickedSignal().Connect( this, &ImageScalingAndFilteringController::OnButtonClicked );
414
415     parent.Add( button );
416     return button;
417   }
418
419   bool OnButtonClicked( Toolkit::Button button )
420   {
421     if( button.GetName() == FITTING_BUTTON_ID )
422     {
423       mPopup = CreatePopup();
424
425       // Four-row table to hold buttons:
426       Toolkit::TableView fittingModes = Toolkit::TableView::New( 4, 1 );
427       fittingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
428       fittingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
429       fittingModes.SetBackgroundColor( BACKGROUND_COLOUR );
430       fittingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
431       fittingModes.SetFitHeight( 0 );
432       fittingModes.SetFitHeight( 1 );
433       fittingModes.SetFitHeight( 2 );
434       fittingModes.SetFitHeight( 3 );
435
436       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SCALE_TO_FILL ) );
437       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::SHRINK_TO_FIT ) );
438       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_WIDTH ) );
439       CreatePopupButton( fittingModes, StringFromScalingMode( FittingMode::FIT_HEIGHT ) );
440
441       mPopup.Add( fittingModes );
442       mPopup.Show();
443     }
444     else if( button.GetName() == SAMPLING_BUTTON_ID )
445     {
446       mPopup = CreatePopup();
447
448       // Table to hold buttons for each sampling mode:
449       Toolkit::TableView samplingModes = Toolkit::TableView::New( 6, 1 );
450       samplingModes.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
451       samplingModes.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
452       samplingModes.SetBackgroundColor( BACKGROUND_COLOUR );
453       samplingModes.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE * 0.5 ) );
454       samplingModes.SetFitHeight( 0 );
455       samplingModes.SetFitHeight( 1 );
456       samplingModes.SetFitHeight( 2 );
457       samplingModes.SetFitHeight( 3 );
458       samplingModes.SetFitHeight( 4 );
459       samplingModes.SetFitHeight( 5 );
460
461       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NEAREST ) );
462       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::LINEAR ) );
463       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX ) );
464       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_NEAREST ) );
465       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::BOX_THEN_LINEAR ) );
466       CreatePopupButton( samplingModes, StringFromFilterMode( SamplingMode::NO_FILTER ) );
467
468       mPopup.Add( samplingModes );
469       mPopup.Show();
470     }
471     else if( CheckFittingModeButton( button, FittingMode::SCALE_TO_FILL) ||
472              CheckFittingModeButton( button, FittingMode::SHRINK_TO_FIT) ||
473              CheckFittingModeButton( button, FittingMode::FIT_WIDTH) ||
474              CheckFittingModeButton( button, FittingMode::FIT_HEIGHT) )
475     {
476     }
477     else if( CheckSamplingModeButton( button, SamplingMode::NEAREST ) ||
478              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
479              CheckSamplingModeButton( button, SamplingMode::BOX ) ||
480              CheckSamplingModeButton( button, SamplingMode::LINEAR ) ||
481              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_NEAREST ) ||
482              CheckSamplingModeButton( button, SamplingMode::BOX_THEN_LINEAR ) ||
483              CheckSamplingModeButton( button, SamplingMode::NO_FILTER ) )
484     {
485     }
486     return true;
487   }
488
489   bool CheckFittingModeButton( Actor &button, FittingMode::Type mode )
490   {
491     const char * const modeName = StringFromScalingMode( mode );
492     if( button.GetName() == modeName )
493     {
494       mFittingMode = mode;
495       mFittingModeButton.SetLabel( modeName );
496       mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
497       ResizeImage();
498       mPopup.Hide();
499       mPopup.Reset();
500       return true;
501     }
502     return false;
503   }
504
505   bool CheckSamplingModeButton( Actor &button, SamplingMode::Type mode )
506   {
507     const char * const modeName = StringFromFilterMode( mode );
508     if( button.GetName() == modeName )
509     {
510       mSamplingMode = mode;
511       mSamplingModeButton.SetLabel( modeName );
512       mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
513       ResizeImage();
514       mPopup.Hide();
515       mPopup.Reset();
516       return true;
517     }
518     return false;
519   }
520
521   void OnPopupOutsideTouched()
522   {
523     if( mPopup )
524     {
525       mPopup.Hide();
526       mPopup.Reset();
527     }
528   }
529
530   void OnImageLoaded( ResourceImage image )
531   {
532       DALI_ASSERT_DEBUG( image == mNextImage );
533       mImageActor.SetImage( image );
534       mImageActor.SetSize( Size( image.GetWidth(), image.GetHeight() ) );
535   }
536
537   bool OnControlTouched( Actor actor, const TouchEvent& event )
538   {
539     if(event.GetPointCount() > 0)
540     {
541       const TouchPoint& point = event.GetPoint(0);
542       switch(point.state)
543       {
544         case TouchPoint::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( 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     mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x + (gesture.displacement.x * 2.0f / stage.GetSize().width ) ) );
600     mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y + (gesture.displacement.y * 2.0f / stage.GetSize().height ) ) );
601     ResizeImage();
602   }
603
604   void OnKeyEvent(const KeyEvent& event)
605   {
606     if( event.state == KeyEvent::Down )
607     {
608       if( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
609       {
610         if( mPopup && mPopup.IsVisible() )
611         {
612           mPopup.Hide();
613           mPopup.Reset();
614         }
615         else
616         {
617           mApplication.Quit();
618         }
619       }
620       else if ( event.keyPressedName == "Right" )
621       {
622         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
623       }
624       else if ( event.keyPressedName == "Left" )
625       {
626         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
627       }
628       else if ( event.keyPressedName == "Up" )
629       {
630         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
631       }
632       else if ( event.keyPressedName == "Down" )
633       {
634         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
635       }
636       else if ( event.keyPressedName == "o" )
637       {
638         mImageStageScale.x = std::max( 0.05f, mImageStageScale.x * 0.9f );
639         mImageStageScale.y = std::max( 0.05f, mImageStageScale.y * 0.9f );
640       }
641       else if ( event.keyPressedName == "p" )
642       {
643         mImageStageScale.x = std::max( 0.05f, std::min( 1.0f, mImageStageScale.x * 1.1f ) );
644         mImageStageScale.y = std::max( 0.05f, std::min( 1.0f, mImageStageScale.y * 1.1f ) );
645       }
646       else if ( event.keyPressedName == "n" )
647       {
648         mCurrentPath = mCurrentPath + 1;
649         mCurrentPath = mCurrentPath <  NUM_IMAGE_PATHS ? mCurrentPath : 0;
650       }
651       else if ( event.keyPressedName == "b" )
652       {
653         mCurrentPath = mCurrentPath - 1;
654         mCurrentPath = mCurrentPath >= 0 ? mCurrentPath : NUM_IMAGE_PATHS - 1;
655       }
656       // Cycle filter and scaling modes:
657       else if ( event.keyPressedName == "f" )
658       {
659         mSamplingMode = NextFilterMode( mSamplingMode );
660         mSamplingModeButton.SetLabel( StringFromFilterMode( mSamplingMode ) );
661         mSamplingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
662       }
663       // Cycle filter and scaling modes:
664       else if ( event.keyPressedName == "s" )
665       {
666         mFittingMode = NextScalingMode( mFittingMode );
667         mFittingModeButton.SetLabel( StringFromScalingMode( mFittingMode ) );
668         mFittingModeButton.GetLabel().SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_TEXT );
669       }
670       else
671       {
672         return;
673       }
674
675       ResizeImage();
676     }
677   }
678
679 private:
680   void ResizeImage()
681   {
682     const char * const path = IMAGE_PATHS[mCurrentPath];
683
684     Stage stage = Stage::GetCurrent();
685     Size imageSize = stage.GetSize() * mImageStageScale;
686     const ImageDimensions imageSizeInt = ImageDimensions::FromFloatArray( &imageSize.x );
687
688     ResourceImage image = ResourceImage::New( path, imageSizeInt, mFittingMode, mSamplingMode );
689     image.LoadingFinishedSignal().Connect( this, &ImageScalingAndFilteringController::OnImageLoaded );
690
691     mNextImage = image;
692
693     mDesiredBox.SetSize( stage.GetSize() * mImageStageScale );
694     mHeightBox.SetSize( stage.GetSize().width,  (stage.GetSize() * mImageStageScale).height );
695     mWidthBox.SetSize( (stage.GetSize() * mImageStageScale).width, stage.GetSize().height );
696   }
697
698 private:
699   Application&  mApplication;
700   ImageActor mDesiredBox; //< Background rectangle to show requested image size.
701   ImageActor mHeightBox;  //< Background horizontal stripe to show requested image height.
702   ImageActor mWidthBox;   //< Background vertical stripe to show requested image width.
703   Toolkit::PushButton mFittingModeButton;
704   Toolkit::PushButton mSamplingModeButton;
705   Toolkit::Popup mPopup;
706   PinchGestureDetector mPinchDetector;
707   float mLastPinchScale;
708   Toolkit::PushButton  mGrabCorner;
709   PanGestureDetector mPanGestureDetector;
710   ImageActor mImageActor;
711   ResourceImage mNextImage; //< Currently-loading image
712   Vector2 mImageStageScale;
713   int mCurrentPath;
714   FittingMode::Type mFittingMode;
715   SamplingMode::Type mSamplingMode;
716 };
717
718 void RunTest( Application& application )
719 {
720   ImageScalingAndFilteringController test( application );
721
722   application.MainLoop();
723 }
724
725 // Entry point for Linux & Tizen applications
726 int main( int argc, char **argv )
727 {
728   Application application = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
729
730   RunTest( application );
731
732   return 0;
733 }